[openbnc-cvs] openbnc/src openbnc.c,1.6,1.7
Status: Beta
Brought to you by:
andrereis
From: Mateusz K. <sh...@us...> - 2004-07-06 13:47:59
|
Update of /cvsroot/openbnc/openbnc/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9537/openbnc/src Modified Files: openbnc.c Log Message: - Add command line : -u ( setting uid ) -g ( setting gid ) ( gid || uid == 0 is deny ) ( ex. usage ./openbnc -u foo -g bar ) Index: openbnc.c =================================================================== RCS file: /cvsroot/openbnc/openbnc/src/openbnc.c,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- openbnc.c 6 Jul 2004 11:03:49 -0000 1.6 +++ openbnc.c 6 Jul 2004 13:47:50 -0000 1.7 @@ -63,6 +63,10 @@ #include <p_global.h> #include <p_data.h> +#include <sys/types.h> +#include <unistd.h> +#include <pwd.h> +#include <grp.h> int slice=0; @@ -146,6 +150,36 @@ return; } + +int user_uid(char user[255]) { + + struct passwd *passwd; + + passwd=getpwnam(user); + + if ( passwd != 0 ) { + return(passwd->pw_uid); + } else { + printf("ERROR: No such user %s\n",user); + exit(127); + } +} + +int group_gid(char grp[255]) { + + struct group *group; + + group=getgrnam(grp); + + if ( group != NULL ) { + return(group->gr_gid); + } else { + printf("ERROR: No such group %s\n",grp); + exit(127); + } +} + + /* main bounce-loop */ int bncmain(void) { @@ -197,22 +231,17 @@ char buf[200]; char *bversion; FILE *pidfile,*conffile; - int i; - - /* Checking uid - IF root -> exit - - it may be really unsecure */ - - if ( getuid() == 0 ) { - printf("Don't run OpenBNC as root!\n"); - exit(127); - } - + int i,t_uid,t_gid=0; /* if(argc>1) { strmncpy(configfile,argv[1],sizeof(configfile)); } else { strcpy(configfile,"openbnc.conf"); } */ + + + strcpy(configfile,"openbnc.conf"); + /* Checking parametrs */ @@ -251,14 +280,71 @@ exit(0); } + /* --user | -u - Run as specific user */ + if ( !strcasecmp("--user",argv[i]) || !strcasecmp("-u",argv[i]) ) { + + if ( getuid() == 0 ) { + + if ( argv[i+1] != NULL ) { + t_uid = user_uid(argv[i+1]); + } else { + printf("ERROR: --user without parametr!\n"); + exit(127); + } + + } else { + + printf("Sorry only root can do that!\n"); + exit(127); + + } + + } + + /* --group | -g - Run as specific group */ + if ( !strcasecmp("--group",argv[i]) || !strcasecmp("-g",argv[i]) ) { + + if ( getuid() == 0 ) { + + if ( argv[i+1] != NULL ) { + t_gid = group_gid(argv[i+1]); + } else { + printf("ERROR: --group without parametr!\n"); + exit(127); + } + + } else { + + printf("Sorry only root can do that!\n"); + exit(127); + + } + + } + } - } else { - - /* if no paramers - reading standard file */ - strcpy(configfile,"openbnc.conf"); + } + + if ( t_gid != 0 ) { + setgid(t_gid); + } + + if ( t_uid != 0 ) { + setuid(t_uid); + } + + + /* Checking uid - IF root -> exit + - it may be really unsecure */ + + if ( getuid() == 0 ) { + printf("Don't run OpenBNC as root!\n"); + exit(127); } + + conffile=fopen(configfile,"r"); if(conffile==NULL) |