First C programming under Freebsd

Hello FreeBSd community...I have been using FreeBSD for a while now and since i do programming i wanted to start programming something for FreeBSD.One thing that is frustrating me is that there is no GUI Wireless tool.So i decided to program my own.I know i can create a process and call a shell command with system() function (e.g system ("date"); ) but i need to issue a scan command and i need root privileges for that (what i mean is "ifconfig wlan0 up scan").How can i do that?And is calling system() function safe for such a thing?
 
Look at the ifconfig source regarding scanning routines, you can also set SUID-0-bit for your program's binary to get around the root permission restrictions but its not the most common path. There are many papers on Internet regarding authentication and permission managing in your software.
 
There's net/pcbsd-netmanager, but it requires KDE4.

Code:
$ less pkg-descr 
PC-BSD Networking Utilities

Includes GUI's for setting up and managing ethernet and wireless devices.
 
yes i know about that pc-bsd tool but it is for kde4 and I'm runing fluxbox so I'm not sure how would that work...i may use it as a reference but that is coded in c++ since it's made with qt.I was thinking doing my in pure C and DE independent...
 
psycho said:
wait? You want to use pure C without any toolkit and create GUI program??? good luck :)

no no... ofc I will use some toolkit for GUI but first i want to make a console app a then put that "engine" in a GUI...

Is this approach wrong?

I suggest to check this page http://www.freebsdsoftware.org/x11-toolkits/ and choose something else to write that program with. Perl(<3) , Python(-.-)??

Thank you for your suggestions but I don't know Pearl and I don't like Python :D...i like C...

Btw. still no answer to my original question...how can i call a root command from C (and get it's output in a char array)...is that even possible?Or are you suggesting me toolkits because the have some built-in functions for such a thing?

Thx
 
taz said:
no no... ofc I will use some toolkit for GUI but first i want to make a console app a then put that "engine" in a GUI...

Is this approach wrong?



Thank you for your suggestions but I don't know Pearl and I don't like Python :D...i like C...

Btw. still no answer to my original question...how can i call a root command from C (and get it's output in a char array)...is that even possible?Or are you suggesting me toolkits because the have some built-in functions for such a thing?

Thx

popen
 
Code:
#include <stdio.h>
#include <stdlib.h>

#define PATH_MAX 100

int main() {
	
	FILE *fp;
	int status;
	char path[PATH_MAX];
	char *comand = "ifconfig wlan0 up scan";


	fp = popen(comand, "r");
	if (fp == NULL)
		/* Handle error */;


	while (fgets(path, PATH_MAX, fp) != NULL)
		printf("%s", path);


	status = pclose(fp);
	if (status == -1) {
		/* Error reported by pclose() */
    
	} else {
    /* Use macros described under wait() to inspect `status' in order
       to determine success/failure of command executed by popen() */
    
	}
	
    return 0;
}

If you compile this code you will get "ifconfig : up: permission denied".So i still do know hot to run my program in root mode so i can execute root commands :).I am sorry if i am troubling you maybe you don't understand my question and hope this example helps in understanding my problem...
 
hmm i see now i have may been little unclear...this is what i want to do...
if a normal user runs my program i want to ask him for the superuser password so he can then issue the scan command and every thing else he my need su privileges...

just like the pc-bsd wireless tool you need to enter the su pass before you can run that app...i want to do that to but in console...
 
maybe you could use IF. this next code is just guessing. You should not compile it.
Code:
IF ("echo $USER" == "root")
     {
        continue your program...
     }
ELSE 
     {
std::cout << "You must be logged in as root to use this program.\n";
after this line, execute shell command: login ; which will give user login and password prompt, and then 
he'll loign as root.
This $USER is variable, which you can check if you type: echo $USER in your terminal, that is why I use it, because it returns user name;
Though, this all command UP should be implemented with popen because this way it won't work.
 
taz said:
hmm i see now i have may been little unclear...this is what i want to do...
if a normal user runs my program i want to ask him for the superuser password so he can then issue the scan command and every thing else he my need su privileges...

just like the pc-bsd wireless tool you need to enter the su pass before you can run that app...i want to do that to but in console...

For this you will need to setup SUID-0-bit on your binary and use pam(3) interface to authenticate password.
 
What I would suggest is two solutions...

1) If the gui application is not run as root, throw an error message until they actually run it using sudo etc...

2) With the system command, do system("sudo ifconfig...")
and then edit the sudoers files so that users in a specific group do not need to type in a password when sudoing ifconfig etc...

I am working on a similar project for a wifi tool for OpenCDE and I am enjoying using wxWidgets for the GUI library (C++ only unfortunately). So if you don't mind using c++ rather than C, then I highly recommend it.
 
kpedersen said:
What I would suggest is two solutions...

1) If the gui application is not run as root, throw an error message until they actually run it using sudo etc...

2) With the system command, do system("sudo ifconfig...")
and then edit the sudoers files so that users in a specific group do not need to type in a password when sudoing ifconfig etc...

I am working on a similar project for a wifi tool for OpenCDE and I am enjoying using wxWidgets for the GUI library (C++ only unfortunately). So if you don't mind using c++ rather than C, then I highly recommend it.

the problem with sudo is that it dose not come by default on freebsd...so not all users will have this "installed"...for example i never use sudo and i don't have it...

For C GTK+ is best, a toolkit written by C developers for C developers what can I add more...

yeah i think so to that for C gtk+ is the way to go...
 
I prefer Qt4. Qt4 designer is one of the best GUI creation tools around. You can generate sources using uic4 or pyuic4 (for python).

There's another good GUI for Python called Tkinter which utilizes Tcl/Tk and may have lesser dependencies. I have already written a few GUI wrappers for command line programs. Check out my mencoder GUI frontend BiaMove (Python/Tkinter):
http://harishankar.org/software/biamove.php
 
Qt is... too fat!
GTK+ has... loads of dependencies.
Motif is... a little too old.
wxWidgets... still requires one of the above (unless you like ugly).

Take your choice... They are all good :p
 
taz said:
the problem with sudo is that it dose not come by default on freebsd...so not all users will have this "installed"...for example i never use sudo and i don't have it...
Neither are X, GTK or any of the other GUI toolkits you're going to use.

I would suggest trying to integrate it with sysutils/policykit.
 
PolicyKit, does that not depend on hald / dbus?

Personally I am slightly weary about integrating with those things. I thought hald was gonna be depricated?

Is there a way to statically link sudo into the application (libsudo)? Although it would still need the /etc/sudoers file I guess.

I am quite interested in this because I need to do similar for OpenCDE.

A bad but slightly novelty solution would be to run a local light http/php server as root (mongoose + php) and control your wifi network via a webpage :).
 
Back
Top