# First C programming under Freebsd



## taz (Apr 9, 2010)

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?


----------



## expl (Apr 9, 2010)

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.


----------



## tankist02 (Apr 10, 2010)

There is a GUI networking (including wireless) tool in PC BSD. You may want to use its sources as reference.


----------



## DutchDaemon (Apr 10, 2010)

There's net/pcbsd-netmanager, but it requires KDE4.


```
$ less pkg-descr 
PC-BSD Networking Utilities

Includes GUI's for setting up and managing ethernet and wireless devices.
```


----------



## taz (Apr 10, 2010)

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 (Apr 10, 2010)

wait? You want to use pure C without any toolkit and create GUI program??? good luck


----------



## psycho (Apr 10, 2010)

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


----------



## taz (Apr 10, 2010)

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 ...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


----------



## expl (Apr 10, 2010)

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?
> 
> ...



popen


----------



## psycho (Apr 10, 2010)

and here is example: http://www.opengroup.org/onlinepubs/009695399/functions/popen.html


----------



## taz (Apr 11, 2010)

```
#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...


----------



## taz (Apr 11, 2010)

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...


----------



## psycho (Apr 11, 2010)

maybe you could use IF. this next code is just guessing. You should not compile it.

```
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.


----------



## psycho (Apr 11, 2010)

for this purpose you could also try to do this with perl code which will be implemented at your main C code. see this: http://docstore.mik.ua/orelly/perl/prog3/ch21_04.htm


----------



## expl (Apr 11, 2010)

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.


----------



## taz (Apr 11, 2010)

expl said:
			
		

> For this you will need to setup SUID-0-bit on your binary and use pam(3) interface to authenticate password.



Thank you very much expl,I will look it up...


----------



## kpedersen (Apr 11, 2010)

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.


----------



## expl (Apr 12, 2010)

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


----------



## taz (Apr 14, 2010)

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...
> 
> ...



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...


----------



## harishankar (May 10, 2010)

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


----------



## kpedersen (May 11, 2010)

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


----------



## SirDice (May 11, 2010)

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.


----------



## kpedersen (May 11, 2010)

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 .


----------



## SirDice (May 11, 2010)

kpedersen said:
			
		

> PolicyKit, does that not depend on hald / dbus?


Yes.



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


HAL is but sysutils/consolekit and PolicyKit aren't.


----------



## vermaden (May 11, 2010)

SirDice said:
			
		

> I would suggest trying to integrate it with sysutils/policykit.





			
				SirDice said:
			
		

> HAL is but sysutils/consolekit and PolicyKit aren't.



Ever heard about AVOIDING LINUXISM?
http://wiki.freebsd.org/AvoidingLinuxisms

Stay away from that Linux shit (like hal/${NO_MATTER_WHAT}Kit/alsa/pulseaudio) for writing proper software.


----------



## SirDice (May 11, 2010)

vermaden said:
			
		

> Ever heard about AVOIDING LINUXISM?
> http://wiki.freebsd.org/AvoidingLinuxisms


It's not linuxism.

http://www.freedesktop.org/wiki/Software/PolicyKit
http://www.freedesktop.org/wiki/Software/ConsoleKit


----------



## expl (May 11, 2010)

Whats wrong with just using local PAM daemon and its rules to authenticate like all native FreeBSD software does?


----------



## SirDice (May 11, 2010)

expl said:
			
		

> Whats wrong with just using local PAM daemon and its rules to authenticate like all native FreeBSD software does?



They don't integrate into Gnome and/or KDE. ConsoleKit/PolicyKit do.


----------



## expl (May 11, 2010)

SirDice said:
			
		

> They don't integrate into Gnome and/or KDE. ConsoleKit/PolicyKit do.



If you mean code for OpenPAM is not portable then there isn't much need for portability when you write a WiFi manager for FreeBSD. And you can integrate it to QT/GTK apps.


----------



## SirDice (May 11, 2010)

expl said:
			
		

> If you mean code for OpenPAM is not portable then there isn't much need for portability when you write a WiFi manager for FreeBSD. And you can integrate it to QT/GTK apps.



Sorry, but you fail to see the added benifits of ConsoleKit. With ConsoleKit you get a framework to assign users different privileges. Like changing WiFi settings i.e. Sure this can be done using PAM but ConsoleKit makes this so much nicer and more fine grained. It's more or less how Windows Group Policies work.

There's a rather big difference between _authenticating_ a user and _authorizing_ a user. PAM takes care of the authentication while ConsoleKit/PolicyKit can take care of the authorization. Why would a user need to authenticate again when that user's already logged in? ConsoleKit/PolicyKit can authorize that user to use that function, no need for passwords, popups or whatever.


----------



## expl (May 11, 2010)

SirDice said:
			
		

> Why would a user need to authenticate again when that user's already logged in? ConsoleKit/PolicyKit can authorize that user to use that function, no need for passwords, popups or whatever.



User needs to authenticate because he needs to do something that only root is allowed to and if you let normal users do that without authorization every time then its not that secure ain't it?


----------

