From 8e5abdc931819ca4618049e6099f1fc3b3be113f Mon Sep 17 00:00:00 2001 From: Thomas Ries Date: Tue, 6 Jul 2004 17:23:50 +0000 Subject: [PATCH] - patch from Dan Weber: Open the password file for SIP accounts at startup, so we still have access after chroot()ing. PID filename can be given at command-line. --- ChangeLog | 3 +++ src/auth.c | 14 +++++++----- src/custom_fw_module.c | 7 ++++-- src/proxy.c | 3 +-- src/siproxd.c | 51 +++++++++++++++++++++++++++++------------- src/siproxd.h | 6 +++-- 6 files changed, 57 insertions(+), 27 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4039e80..467778d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 0.5.8 ===== + 06-Jul-2004: - patch from Dan Weber: Open the password file for SIP + accounts at startup, so we still have access after + chroot()ing. PID filename can be given at command-line. 27-Jun-2004: - fix: in some configurations incoming requests could be sent to a wrong port number. diff --git a/src/auth.c b/src/auth.c index f8d04c7..3cdc516 100644 --- a/src/auth.c +++ b/src/auth.c @@ -42,6 +42,9 @@ static char const ident[]="$Id$"; /* configuration storage */ extern struct siproxd_config configuration; +/* Global File instance on pw file */ +extern FILE *siproxd_passwordfile; + /* local protorypes */ static char *auth_generate_nonce(void); static int auth_check(osip_proxy_authorization_t *proxy_auth); @@ -248,7 +251,6 @@ static char *auth_getpwd(char *username) { char password[PASSWORD_SIZE]; } auth_cache_t; - FILE *pwdfile; char buff[128]; int i; static auth_cache_t *auth_cache=NULL; @@ -258,14 +260,16 @@ static char *auth_getpwd(char *username) { if (auth_cache==NULL) { DEBUGC(DBCLASS_AUTH,"initialize password cache"); - pwdfile=fopen(configuration.proxy_auth_pwfile,"r"); + /* config file not found or unable to open for read */ - if (pwdfile==NULL) { + if (siproxd_passwordfile==NULL) { ERROR ("could not open password file: %s", strerror(errno)); return NULL; } - while (fgets(buff,sizeof(buff),pwdfile) != NULL) { + rewind(siproxd_passwordfile); + + while (fgets(buff,sizeof(buff),siproxd_passwordfile) != NULL) { /* life insurance */ buff[sizeof(buff)-1]='\0'; @@ -302,8 +306,6 @@ static char *auth_getpwd(char *username) { if (i == 2) auth_cache_count++; } - fclose(pwdfile); - } /* initialize cache */ /* search cache for user */ diff --git a/src/custom_fw_module.c b/src/custom_fw_module.c index b4aaa81..3f9e50c 100644 --- a/src/custom_fw_module.c +++ b/src/custom_fw_module.c @@ -71,6 +71,7 @@ char *utils_inet_ntoa(struct in_addr in); */ int custom_fw_control(fw_ctl_t fwdata) { static char tmp[256]; + size_t pos; tmp[0]='\0'; switch (fwdata.action) { @@ -97,11 +98,13 @@ int custom_fw_control(fw_ctl_t fwdata) { break; } - sprintf(&tmp[strlen(tmp)],"[lcl %s:%i] ", + pos = strlen(tmp); + sprintf(&tmp[pos],"[lcl %s:%i] ", utils_inet_ntoa(fwdata.local_ipaddr), fwdata.local_port); - sprintf(&tmp[strlen(tmp)],"[rem %s:%i] ", + pos = strlen(tmp); + sprintf(&tmp[pos],"[rem %s:%i] ", utils_inet_ntoa(fwdata.remote_ipaddr), fwdata.remote_port); diff --git a/src/proxy.c b/src/proxy.c index cedb923..24e0079 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -38,9 +38,8 @@ static char const ident[]="$Id$"; /* configuration storage */ -extern struct siproxd_config configuration; +extern struct siproxd_config configuration; /* defined in siproxd.c */ -extern int errno; extern struct urlmap_s urlmap[]; /* URL mapping table */ extern struct lcl_if_s local_addresses; diff --git a/src/siproxd.c b/src/siproxd.c index fb545a5..d52cbfd 100644 --- a/src/siproxd.c +++ b/src/siproxd.c @@ -43,14 +43,18 @@ static char const ident[]="$Id$"; /* configuration storage */ struct siproxd_config configuration; +/* Global File instance on pw file */ +FILE *siproxd_passwordfile; + /* -h help option text */ static const char str_helpmsg[] = PACKAGE "-" VERSION "-" BUILDSTR " (c) 2002-2004 Thomas Ries\n" "\nUsage: siproxd [options]\n\n" "options:\n" -" --help (-h) help\n" -" --debug (-d) set debug-pattern\n" -" --config (-c) use the specified config file\n" +" --help (-h) help\n" +" --debug (-d) set debug-pattern\n" +" --config (-c) use the specified config file\n" +" --pid-file (-p) create pid file \n" ""; @@ -75,13 +79,13 @@ int main (int argc, char *argv[]) char buff [BUFFER_SIZE]; sip_ticket_t ticket; - extern char *optarg; + extern char *optarg; /* Defined in libc getopt and unistd.h */ int ch1; char configfile[64]="siproxd"; /* basename of configfile */ int config_search=1; /* search the config file */ int cmdline_debuglevel=0; - + char *pidfilename=NULL; struct sigaction act; log_set_stdout(1); @@ -111,6 +115,16 @@ int main (int argc, char *argv[]) log_set_pattern(configuration.debuglevel); +/* + * open a the pwfile instance, so we still have access after + * we possibly have chroot()ed to somewhere. + */ + if (configuration.proxy_auth_pwfile) { + siproxd_passwordfile = fopen(configuration.proxy_auth_pwfile, "r"); + } else { + siproxd_passwordfile = NULL; + } + /* * parse command line */ @@ -121,13 +135,14 @@ int main (int argc, char *argv[]) {"help", no_argument, NULL, 'h'}, {"config", required_argument, NULL, 'c'}, {"debug", required_argument, NULL, 'd'}, + {"pid-file", required_argument, NULL,'p'}, {0,0,0,0} }; - while ((ch1 = getopt_long(argc, argv, "hc:d:n", + while ((ch1 = getopt_long(argc, argv, "hc:d:p:", long_options, &option_index)) != -1) { #else /* ! HAVE_GETOPT_LONG */ - while ((ch1 = getopt(argc, argv, "hc:d:n:")) != -1) { + while ((ch1 = getopt(argc, argv, "hc:d:p:")) != -1) { #endif switch (ch1) { case 'h': /* help */ @@ -138,8 +153,9 @@ int main (int argc, char *argv[]) case 'c': /* load config file */ DEBUGC(DBCLASS_CONFIG,"option: config file=%s",optarg); - strncpy(configfile,optarg,sizeof(configfile)-1); - configfile[sizeof(configfile)]='\0'; + i=sizeof(configfile)-1; + strncpy(configfile,optarg,i-1); + configfile[i]='\0'; config_search=0; break; @@ -149,6 +165,10 @@ int main (int argc, char *argv[]) log_set_pattern(cmdline_debuglevel); break; + case 'p': + pidfilename = optarg; + break; + default: DEBUGC(DBCLASS_CONFIG,"no command line options"); break; @@ -187,12 +207,13 @@ int main (int argc, char *argv[]) } /* write PID file of main thread */ - if (configuration.pid_file) { + if (pidfilename == NULL) pidfilename = configuration.pid_file; + if (pidfilename) { FILE *pidfile; - DEBUGC(DBCLASS_CONFIG,"creating PID file [%s]", configuration.pid_file); + DEBUGC(DBCLASS_CONFIG,"creating PID file [%s]", pidfilename); sts=unlink(configuration.pid_file); if ((sts==0) ||(errno == ENOENT)) { - if ((pidfile=fopen(configuration.pid_file, "w"))) { + if ((pidfile=fopen(pidfilename, "w"))) { fprintf(pidfile,"%i\n",(int)getpid()); fclose(pidfile); } else { @@ -473,9 +494,9 @@ int main (int argc, char *argv[]) INFO("properly terminating siproxd"); /* remove PID file */ - if (configuration.pid_file) { - DEBUGC(DBCLASS_CONFIG,"deleting PID file [%s]", configuration.pid_file); - sts=unlink(configuration.pid_file); + if (pidfilename) { + DEBUGC(DBCLASS_CONFIG,"deleting PID file [%s]", pidfilename); + sts=unlink(pidfilename); if (sts != 0) { WARN("couldn't delete old PID file: %s", strerror(errno)); } diff --git a/src/siproxd.h b/src/siproxd.h index 57a3848..a9d3d7c 100644 --- a/src/siproxd.h +++ b/src/siproxd.h @@ -104,8 +104,8 @@ typedef struct { /* function returns STS_* status values vvv */ /* sock.c */ -int sipsock_listen (void); /*X*/ -int sipsock_wait(); +int sipsock_listen(void); /*X*/ +int sipsock_wait(void); int sipsock_read(void *buf, size_t bufsize, struct sockaddr_in *from, int *protocol); int sipsock_send(struct in_addr addr, int port, int protocol, /*X*/ @@ -232,4 +232,6 @@ int fwapi_stop_rtp(int rtp_direction, #define DIR_OUTGOING 2 /* various */ +#ifndef satoi #define satoi atoi /* used in libosips MSG_TEST_CODE macro ... */ +#endif