[openbnc-cvs] openbnc/src openbnc.c,1.11,1.12
Status: Beta
Brought to you by:
andrereis
From: Kiraly Z. <yo...@us...> - 2004-07-20 10:39:40
|
Update of /cvsroot/openbnc/openbnc/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6324/openbnc/src Modified Files: openbnc.c Log Message: fixed for *BSD Index: openbnc.c =================================================================== RCS file: /cvsroot/openbnc/openbnc/src/openbnc.c,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- openbnc.c 13 Jul 2004 21:41:32 -0000 1.11 +++ openbnc.c 20 Jul 2004 10:39:31 -0000 1.12 @@ -56,11 +56,9 @@ */ #ifndef lint -static char rcsid[] = "@(#)$Id$"; #endif #define P_MAIN - #include <p_global.h> #include <p_data.h> #include <sys/types.h> @@ -77,15 +75,16 @@ SSL_load_error_strings(); OpenSSL_add_ssl_algorithms(); srvctx=SSL_CTX_new(SSLv23_server_method()); - /* setting up the server context */ + + /* setting up the server context */ if(srvctx==NULL) { - log(LOG_ERROR,-1,lngtxt(970)); + log(LOG_ERROR,-1,lngtxt(970)); exit(0x1); } if (SSL_CTX_use_certificate_file(srvctx, SSLCERT, SSL_FILETYPE_PEM) <= 0) { - log(LOG_ERROR,-1,lngtxt(971), SSLCERT); + log(LOG_ERROR,-1,lngtxt(971), SSLCERT); exit(0x1); } if (SSL_CTX_use_PrivateKey_file(srvctx, SSLKEY, SSL_FILETYPE_PEM) <= 0) @@ -95,9 +94,10 @@ } if (!SSL_CTX_check_private_key(srvctx)) { - log(LOG_ERROR,-1,lngtxt(973)); - exit(0x1); + log(LOG_ERROR,-1,lngtxt(973)); + exit(0x1); } + /* create client context */ clnctx=SSL_CTX_new(SSLv23_client_method()); if(clnctx==NULL) @@ -112,8 +112,8 @@ } if (SSL_CTX_use_PrivateKey_file(clnctx, SSLKEY, SSL_FILETYPE_PEM) <= 0) { - log(LOG_ERROR,-1,lngtxt(976), SSLKEY); - exit(4); + log(LOG_ERROR,-1,lngtxt(976), SSLKEY); + exit(4); } if (!SSL_CTX_check_private_key(clnctx)) { @@ -127,20 +127,20 @@ #endif /* alarm-loop */ - int bncalarm(void) { slice++; delayinc=1; if(slice==1) checkclients(); - else + else if(slice==2) - checklinks(); - else + checklinks(); + else if(slice==3) - { + { checkdccs(); + #ifdef TRANSLATE cleartranslates(); #endif @@ -150,75 +150,43 @@ 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); - } + + struct passwd *passwd; + + passwd=getpwnam(user); + + if ( passwd != 0 ) { + return (passwd->pw_uid); + } else { + printf("ERROR: No such user %s\n",user); + exit(127); + } } - /* main bounce-loop */ - int bncmain(void) { - unsigned long em=0; - delayinc=1; - while(1) - { - em+=socketdriver(); - if(em>=5) - { - em=0; - bncalarm(); - } - checkstonednick(); - } - return 0x0; /* i wonder how often we get here */ + unsigned long em=0; + delayinc=1; + while(1) + { + em+=socketdriver(); + if(em>=5) + { + em=0; + bncalarm(); + } + checkstonednick(); + } + return 0x0; } -/* printing the banner */ - int printbanner(void) { -/* int i; - for(i=979;i<990;i++) - { - fprintf(stdout,"%s",lngtxt(i)); - if(i==984) - fprintf(stdout,"%s", APPVER); - } - if(getuid()==0) - fprintf(stdout,lngtxt(990)); - fflush(stdout); - */ - - printf("OpenIRC Advanced IRC Bouncer\n"); - printf("Copyright (C) 2004 The OpenBNC Development Team\n"); - printf("Based on psyBNC, by 'the most psychoid'.\n"); - - return 0x0; + printf("OpenBNC - Advanced IRC Bouncer\n"); + printf("Copyright (C) The OpenBNC Development Team\n"); + printf("Based on psyBNC, by 'the most psychoid'.\n"); + return 0x0; } /* installation loop */ @@ -226,283 +194,206 @@ int main (int argc, char **argv) { - int rc; - char *pt; - char buf[200],t_pid[200]; - char *bversion; - FILE *pidfile,*conffile; - int i,t_uid,t_gid=0; -/* if(argc>1) - { - strmncpy(configfile,argv[1],sizeof(configfile)); - } else { - strcpy(configfile,"openbnc.conf"); - } */ - - - + int rc; + char *pt; + char buf[200]; + char *bversion; + FILE *pidfile,*conffile; + int i,t_uid,t_gid=0; - +/* if(argc==2) + { + strmncpy(configfile,argv[1],sizeof(configfile)); + } else { +*/ strcpy(configfile,"openbnc.conf"); - - /* Checking parametrs */ - - if ( argc > 1 ) { + +if ( argc > 1 ) { - for ( i = 1 ; i < argc ; i++ ) { - - /* --version | -v - Output APPNAME and VERSION */ - if ( !strcasecmp("--version",argv[i]) || !strcasecmp("-v",argv[i]) ) { - printf("%s %s\n",APPNAME,APPVER); - exit(0); - } - - /* --config | -c - Reading config from specify file */ - if ( !strcasecmp("--config",argv[i]) || !strcasecmp("-c",argv[i]) ) { - if ( argv[i+1] != NULL ) { - strcpy(configfile,argv[i+1]); - } else { - printf("ERROR: --config without parametr!\n"); - exit(127); - } - } - - /* --pid-file | -p - Write pid to specified file */ - if ( !strcasecmp("--pid-file",argv[i]) || !strcasecmp("-p",argv[i]) ) { - if ( argv[i+1] != NULL ) { - - if ( strlen(argv[i+1]) < 201 ) { - strcpy(t_pid,argv[i+1]); - } else { - printf("ERROR: Pid file name too long!\n"); - } - - } else { - printf("ERROR: --pid-file without parametr!\n"); - exit(127); - } - } + for ( i = 1 ; i < argc ; i++ ) { - /* --help | -h - Display Help */ if ( !strcasecmp("--help",argv[i]) || !strcasecmp("-h",argv[i]) ) { printf("%s %s\n\n",APPNAME,APPVER); printf("Options :\n"); printf("---------\n\n"); printf("-h, --help Display help and exit\n"); - printf("-v, --version Display Version and exit\n"); + printf("-v, --version Display version and exit\n"); printf("-c, --config [file] Read configuration from specified file\n"); - printf("-p, --pid-file [file] Write pid to specified file\n"); printf("-u, --user [user] Run as specific user [*]\n"); printf("-g, --group [group] Run as specific group [*]\n"); printf("-l, --user-limit Max no. of users\n"); - printf("-i, --interval Time between connection attempts to IRC server\n\n"); + printf("-i, --interval Time between connection attempts to IRC server\n\n"); exit(0); } - - /* --user | -u - Run as specific user */ + if ( !strcasecmp("--version",argv[i]) || !strcasecmp("-v",argv[i]) ) { + printf("%s %s\n",APPNAME,APPVER); + exit(0); + } + if ( !strcasecmp("--config",argv[i]) || !strcasecmp("-c",argv[i]) ) { + if ( argv[i+1] != NULL ) { + strcpy(configfile,argv[i+1]); + } else { + printf("ERROR: --config without parameter!\n"); + exit(127); + } + } 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); - - } - + if ( argv[i+1] != NULL ) { + t_uid = user_uid(argv[i+1]); + } else { + printf("ERROR: --user without parameter!\n"); + exit(127); } + } else { - /* --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); - - } - + printf("Sorry only root can dot that!\n"); + exit(127); } - - } - - } + } +} +} + conffile=fopen(configfile,"r"); + if(conffile==NULL) + { + printf("Configuration file %s not exist\n",conffile); + exit (0x0); + } + - - if ( t_gid != 0 ) { - setgid(t_gid); - } + /* Checking uid - if root -> exit + if ( getuid() == 0 ) { + printf("Don't run OpenBNC as root!\n"); + exit(127); + } +*/ + + fclose(conffile); + readconfig(); + rc = getini("SYSTEM","LANGUAGE",INIFILE); + if(rc!=0) + { + rc=loadlanguage("english"); + } else { + rc=loadlanguage(value); + if(rc<0) + rc=loadlanguage("english"); + } + if(rc<0) + { + printf("Could not load language file, aborting\n"); + exit(0x0); + } + printbanner(); + printf(lngtxt(991),configfile); + printf(lngtxt(992),langname); + ap_snprintf(logfile,sizeof(logfile),lngtxt(993)); + rc = getini(lngtxt(994),lngtxt(995),INIFILE); + if (rc != 0) { + printf(lngtxt(996)); + exit (0x0); + } + listenport = atoi(value); + rc = getini(lngtxt(997),"ME",INIFILE); + if (rc < 0) { + memset(value,0x0,sizeof(value)); + } + pt=strchr(value,' '); + if(pt) + *pt=0; + ap_snprintf(me,sizeof(me),"%s",value); + oldfile(logfile); + + /* creating the socket-root */ + socketnode=(struct socketnodes *) pmalloc(sizeof(struct usernodes)); + socketnode->sock=NULL; + socketnode->next=NULL; + + /* creating the daemon socket */ + rc = createlisteners(); + if (rc == 0) { + printf(lngtxt(1002)); + exit (0x0); + } + + /* creating background */ + pidfile = fopen(lngtxt(1003),"w"); + if(pidfile==NULL) + { + printf(lngtxt(1004)); + exit(0x0); + } + if(mainlog!=NULL) + { + fclose(mainlog); + mainlog=NULL; + } + fflush(stdout); + pid = fork(); + if (pid < 0) { + + } + if (pid == 0) { +// rc= errorhandlig(); + makesalt(); - if ( t_uid != 0 ) { - setuid(t_uid); - } +#ifdef HAVE_SSL + initSSL(); + pcontext; +#endif + U_CREATE=0; - /* Checking uid - IF root -> exit - - it may be really unsecure */ - - if ( getuid() == 0 ) { - printf("Don't run OpenBNC as root!\n"); - exit(127); - } +#ifdef PARTYCHANNEL + /* partychannel setup */ + strmncpy(partytopic,lngtxt(1005),sizeof(partytopic)); + partyusers=NULL; - - conffile=fopen(configfile,"r"); - if(conffile==NULL) - { - printf("Configuration File %s not found, aborting\nRun 'make menuconfig' for creating a configuration or create the file manually.\n",conffile); /* rcsid */ - exit (0x0); - } - fclose(conffile); - readconfig(); - rc = getini("SYSTEM","LANGUAGE",INIFILE); /* rcsid */ - if(rc!=0) - { - rc=loadlanguage("english"); /* rcsid */ - } else { - rc=loadlanguage(value); - if(rc<0) - rc=loadlanguage("english"); /* rcsid */ - } - if(rc<0) - { - printf("Could not load language file, aborting.\n"); /* rcsid */ - exit(0x0); - } - printbanner(); - // printf(lngtxt(991),configfile); - if ( configfile[0] != '/' ) { - printf("> Reading config file : %s/%s\n",configfile); - } else { - printf("> Reading config file : %s\n",configfile); - } - // printf(lngtxt(992),langname); - // ap_snprintf(logfile,sizeof(logfile),lngtxt(993)); - printf("> Logging set to : %s/%s",logfile); - rc = getini(lngtxt(994),lngtxt(995),INIFILE); - if (rc != 0) { - printf(lngtxt(996)); - exit (0x0); - } - listenport = atoi(value); - rc = getini(lngtxt(997),"ME",INIFILE); - if (rc < 0) { - memset(value,0x0,sizeof(value)); - } - pt=strchr(value,' '); /* shortening bouncername to no spaces */ - if(pt) - *pt=0; - ap_snprintf(me,sizeof(me),"%s",value); - rc = getini(lngtxt(998),lngtxt(999),INIFILE); - if (rc < 0) { - printf("%s \n",lngtxt(1001)); - // ap_snprintf(value,sizeof(value),lngtxt(1001)); - } - ap_snprintf(logfile,sizeof(logfile),"%s",value); - oldfile(logfile); - /* creating the socket-root */ - socketnode=(struct socketnodes *) pmalloc(sizeof(struct usernodes)); - socketnode->sock=NULL; - socketnode->next=NULL; - /* creating the demon socket */ - rc = createlisteners(); - if (rc == 0) { - printf(lngtxt(1002)); - exit (0x0); - } - /* creating background */ - - /* UGLY! */ - if ( strlen(t_pid) > 0 ) { - pidfile = fopen(t_pid,"w"); - } else { - - pidfile = fopen(lngtxt(1003),"w"); - } - - if(pidfile==NULL) - { - printf(lngtxt(1004)); - exit(0x0); - } - if(mainlog!=NULL) - { - fclose(mainlog); - mainlog=NULL; - } - fflush(stdout); - pid = fork(); - if (pid < 0) { - - } - if (pid == 0) { - rc= errorhandling(); - makesalt(); -#ifdef HAVE_SSL - initSSL(); - pcontext; #endif - U_CREATE=0; -#ifdef PARTYCHANNEL - /* partychannel setup */ - strcpy(partytopic,lngtxt(1005)); - partyusers=NULL; -#endif - /* creating the usernode-root */ - usernode=(struct usernodes *) pmalloc(sizeof(struct usernodes)); - usernode->uid=0; - usernode->user=NULL; - usernode->next=NULL; - /* creating the newpeer-root */ - peernode=(struct peernodes *) pmalloc(sizeof(struct peernodes)); - peernode->uid=0; - peernode->peer=NULL; - peernode->next=NULL; - /* creating the datalink-root */ - linknode=(struct linknodes *) pmalloc(sizeof(struct linknodes)); - linknode->uid=0; - linknode->link=NULL; - linknode->next=NULL; - /* loading the users */ - loadusers(); - loadlinks(); - pcontext; - /* loading the hostallows */ - hostallows=loadlist(lngtxt(1006),hostallows); - } - pcontext; - if (pid) { - // bversion=buildversion(); - printf("%s v%s is running (PID=%i, UID=%i, GID=%i)\n",APPNAME,APPVER,pid,getuid(),getgid()); - log(LOG_INFO,-1,lngtxt(1008),bversion,pid); - fprintf( pidfile,"%d\n",pid); - fclose(pidfile); - exit (0x0); - } - pcontext; + + /* creating the usernode-root */ + usernode=(struct usernodes *) pmalloc(sizeof(struct usernodes)); + usernode->uid=0; + usernode->user=NULL; + usernode->next=NULL; + + /* creating the newpeer-root */ + peernode=(struct peernodes *) pmalloc(sizeof(struct peernodes)); + peernode->uid=0; + peernode->peer=NULL; + peernode->next=NULL; + + /* creating the datalink-root */ + linknode=(struct linknodes *) pmalloc(sizeof(struct linknodes)); + linknode->uid=0; + linknode->link=NULL; + linknode->next=NULL; + + /* loading the users */ + loadusers(); + loadlinks(); + pcontext; + + /* loading the hostallows */ + hostallows=loadlist(lngtxt(1006),hostallows); + } + pcontext; + if (pid) { + bversion=buildversion(); + printf(lngtxt(1007),bversion,pid); + log(LOG_INFO,-1,lngtxt(1008),bversion,pid); + fprintf( pidfile,"%d\n",pid); + fclose(pidfile); + exit (0x0); + } + pcontext; + #ifndef BLOCKDNS - if(init_dns_core()==0) - { - dns_err(0,1); - } + if(init_dns_core()==0) + { + dns_err(0,1); + } #endif - bncmain(); + bncmain(); } |