/*************************************** WWWOFFLE - World Wide Web Offline Explorer - Version 2.9j. A demon program to maintain the database and spawn the servers. ******************/ /****************** Written by Andrew M. Bishop This file Copyright 1996-2013 Andrew M. Bishop It may be distributed under the GNU Public License, version 2, or any higher version. See section COPYING of the GNU Public license for conditions under which this file may be redistributed. ***************************************/ #include "autoconfig.h" #define _GNU_SOURCE /* To pick up setresgid() & setresgid() */ #include #include #include #include #include #include #if TIME_WITH_SYS_TIME # include # include #else # if HAVE_SYS_TIME_H # include # else # include # endif #endif #include #include #include #include #include #include #include "wwwoffle.h" #include "io.h" #include "misc.h" #include "errors.h" #include "config.h" #include "sockets.h" #include "certificates.h" #include "version.h" static void usage(int verbose); static void demoninit(void); static void install_sighandlers(void); static void sigchild(int signum); static void sigexit(int signum); static void sighup(int signum); /*+ The server sockets that we listen on +*/ int http_fd[2]={-1,-1}, /*+ for the HTTP connections. +*/ https_fd[2]={-1,-1}, /*+ for the HTTPS connections. +*/ wwwoffle_fd[2]={-1,-1}; /*+ for the WWWOFFLE connections. +*/ /*+ The online / offline /autodial status. +*/ int online=0; /*+ The current number of active servers +*/ int n_servers=0, /*+ in total. +*/ n_fetch_servers=0; /*+ fetching a page. +*/ /*+ The maximum number of servers +*/ int max_servers=0, /*+ in total. +*/ max_fetch_servers=0; /*+ fetching a page. +*/ /*+ The wwwoffle client file descriptor when fetching. +*/ int fetch_fd=-1; /*+ The pids of the servers. +*/ int server_pids[MAX_SERVERS]; /*+ The pids of the servers that are fetching. +*/ int fetch_pids[MAX_FETCH_SERVERS]; /*+ The current purge status. +*/ int purging=0; /*+ The pid of the purge process. +*/ int purge_pid=0; /*+ The current status, fetching or not. +*/ int fetching=0; /*+ Set when the demon is to shutdown due to a sigexit. +*/ static sig_atomic_t got_sigexit=0; /*+ Set when the demon is to re-read config file due to a sighup. +*/ static sig_atomic_t got_sighup=0; /*+ Remember that we got a SIGCHLD +*/ static sig_atomic_t got_sigchld=0; /*+ True if run as a demon +*/ static int detached=1; /*+ True if the pid of the daemon should be printed at startup +*/ static int print_pid=0; /*+ The name of the configuration file specified. +*/ static char *config_file=NULL; /*+ The name of the log file specified. +*/ char *log_file=NULL; /*+ True if the -f option was passed on the command line. +*/ int nofork=0; /*++++++++++++++++++++++++++++++++++++++ The main program. ++++++++++++++++++++++++++++++++++++++*/ int main(int argc, char** argv) { int i; int err; struct stat buf; int uid,gid; /* Parse the command line options */ for(i=1;iFatal) {fprintf(stderr,"wwwoffled: The '-d' option requires a number between 0 and %d.\n",Fatal+1); exit(1);} } continue; } if(!strcmp(argv[i],"-l")) { if(++i>=argc) {fprintf(stderr,"wwwoffled: The '-l' option requires a filename.\n"); exit(1);} if(argv[i][0]!='/') {fprintf(stderr,"wwwoffled: The '-l' option requires an absolute pathname.\n"); exit(1);} log_file=argv[i]; continue; } if(!strcmp(argv[i],"-p")) { print_pid=1; continue; } if(!strcmp(argv[i],"-f")) { nofork=1; detached=0; continue; } if(!strcmp(argv[i],"-c")) { if(++i>=argc) {fprintf(stderr,"wwwoffled: The '-c' option requires a filename.\n"); exit(1);} config_file=argv[i]; continue; } fprintf(stderr,"wwwoffled: Unknown option '%s'.\n",argv[i]); exit(1); } /* Combination options */ if(log_file) { if(nofork) log_file=NULL; /* -f overrides -l */ else detached=1; /* -l overrides -d */ if(StderrLevel==-1) StderrLevel=Inform; /* -l sets log level if no -d */ } /* Initialise things. */ for(i=0;i=nfds) nfds=http_fd[nfd]+1; if(https_fd[nfd]>=nfds) nfds=https_fd[nfd]+1; if(wwwoffle_fd[nfd]>=nfds) nfds=wwwoffle_fd[nfd]+1; if(n_servers=0) { init_io(client); configure_io_timeout(client,ConfigInteger(SocketTimeout),ConfigInteger(SocketTimeout)); if(SocketRemoteName(client,&host,&ip,&port)) { finish_io(client); CloseSocket(client); } else { char *canonical_ip=CanonicaliseHost(ip); if(IsAllowedConnectHost(host) || IsAllowedConnectHost(canonical_ip)) { PrintMessage(Important,"WWWOFFLE Connection from host %s (%s).",host,canonical_ip); /* Used in audit-usage.pl */ CommandConnect(client); if(fetch_fd!=client) { finish_io(client); CloseSocket(client); } } else { PrintMessage(Warning,"WWWOFFLE Connection rejected from host %s (%s).",host,canonical_ip); /* Used in audit-usage.pl */ finish_io(client); CloseSocket(client); } free(canonical_ip); } } } if(http_fd[nfd]!=-1 && FD_ISSET(http_fd[nfd],&readfd)) { char *host,*ip; int port,client; client=AcceptConnect(http_fd[nfd]); if(client>=0) { init_io(client); configure_io_timeout(client,ConfigInteger(SocketTimeout),ConfigInteger(SocketTimeout)); if(!SocketRemoteName(client,&host,&ip,&port)) { char *canonical_ip=CanonicaliseHost(ip); if(IsAllowedConnectHost(host) || IsAllowedConnectHost(canonical_ip)) { PrintMessage(Inform,"HTTP Proxy connection from host %s (%s).",host,canonical_ip); /* Used in audit-usage.pl */ ForkServer(client); } else PrintMessage(Warning,"HTTP Proxy connection rejected from host %s (%s).",host,canonical_ip); /* Used in audit-usage.pl */ free(canonical_ip); } if(nofork) got_sigexit=1; if(!nofork) { finish_io(client); CloseSocket(client); } } } #if USE_GNUTLS if(https_fd[nfd]!=-1 && FD_ISSET(https_fd[nfd],&readfd)) { char *host,*ip; int port,client; client=AcceptConnect(https_fd[nfd]); if(client>=0) { init_io(client); configure_io_timeout(client,ConfigInteger(SocketTimeout),ConfigInteger(SocketTimeout)); if(!SocketRemoteName(client,&host,&ip,&port)) { char *canonical_ip=CanonicaliseHost(ip); if(IsAllowedConnectHost(host) || IsAllowedConnectHost(canonical_ip)) { PrintMessage(Inform,"HTTPS Proxy connection from host %s (%s).",host,canonical_ip); /* Used in audit-usage.pl */ ForkServer(client); } else PrintMessage(Warning,"HTTPS Proxy connection rejected from host %s (%s).",host,canonical_ip); /* Used in audit-usage.pl */ free(canonical_ip); } if(nofork) got_sigexit=1; if(!nofork) { finish_io(client); CloseSocket(client); } } } #endif /* USE_GNUTLS */ #if USE_IPV6 } #endif } if(got_sighup) { got_sighup=0; PrintMessage(Important,"SIGHUP signalled."); if(log_file) { PrintMessage(Important,"Closing and opening log file."); OpenErrorLog(log_file); } PrintMessage(Important,"WWWOFFLE Re-reading Configuration File."); if(ReadConfigurationFile(-1)) PrintMessage(Warning,"Error in configuration file; keeping old values."); PrintMessage(Important,"WWWOFFLE Finished Re-reading Configuration File."); } if(got_sigchld) { int pid, status; int isserver=0; /* To avoid race conditions, reset the flag before fetching the status */ got_sigchld=0; while((pid=waitpid(-1,&status,WNOHANG))>0) { int i; int exitval=0; if(WIFEXITED(status)) exitval=WEXITSTATUS(status); else if(WIFSIGNALED(status)) exitval=-WTERMSIG(status); if(purging) if(purge_pid==pid) { if(exitval>=0) PrintMessage(Inform,"Purge process exited with status %d (pid=%d).",exitval,pid); else PrintMessage(Important,"Purge process terminated by signal %d (pid=%d).",-exitval,pid); purging=0; } for(i=0;i=0) PrintMessage(Inform,"Child wwwoffles exited with status %d (pid=%d).",exitval,pid); else PrintMessage(Important,"Child wwwoffles terminated by signal %d (pid=%d).",-exitval,pid); break; } /* Check if the child that terminated is one of the fetching wwwoffles */ for(i=0;i0) { if(WIFEXITED(status)) exitval=WEXITSTATUS(status); else if(WIFSIGNALED(status)) exitval=-WTERMSIG(status); for(i=0;i=0) PrintMessage(Inform,"Child wwwoffles exited with status %d (pid=%d).",exitval,pid); else PrintMessage(Important,"Child wwwoffles terminated by signal %d (pid=%d).",-exitval,pid); break; } if(purging) if(purge_pid==pid) { if(exitval>=0) PrintMessage(Inform,"Purge process exited with status %d (pid=%d).",exitval,pid); else PrintMessage(Important,"Purge process terminated by signal %d (pid=%d).",-exitval,pid); purging=0; } } } PrintMessage(Important,"Exiting."); FinishConfigurationFile(); #if USE_GNUTLS FreeLoadedCredentials(); #endif return(0); } /*++++++++++++++++++++++++++++++++++++++ Print the program usage in long or short format. int verbose Set to 0 for short format, 1 for long format, 2 for version. ++++++++++++++++++++++++++++++++++++++*/ static void usage(int verbose) { if(verbose==2) { fprintf(stderr, "wwwoffled version %s\n", WWWOFFLE_VERSION); exit(0); } fprintf(stderr, "\n" "WWWOFFLED - World Wide Web Offline Explorer (Daemon) - Version %s\n" "\n",WWWOFFLE_VERSION); if(verbose) fprintf(stderr, "(c) Andrew M. Bishop 1996-2013 [http://www.gedanken.org.uk/software/wwwoffle/]\n" "\n"); fprintf(stderr, "Usage: wwwoffled -h | --help | --version\n" " wwwoffled [-c ] [-d []] [-l ] [-f] [-p]\n" "\n"); if(verbose) fprintf(stderr, " -h | --help : Display this help.\n" "\n" " --version : Display the version number of wwwoffled.\n" "\n" " -c : The name of the configuration file to use.\n" " (Default %s).\n" "\n" " -d [] : Do not detach from the terminal and use stderr.\n" " 0 <= log-level <= %d (default in config file).\n" "\n" " -l : Detach from the terminal and use specified log file.\n" " (Using log-level = 4 unless -d specifies different.)\n" "\n" " -f : Do not fork children for each HTTP request.\n" " (Exits after first request, implies -d, overrides -l.)\n" "\n" " -p : Print the pid of wwwoffled on stdout (unless -d).\n" "\n",ConfigurationFileName(),Fatal+1); exit(0); } /*++++++++++++++++++++++++++++++++++++++ Detach ourself from the controlling terminal. ++++++++++++++++++++++++++++++++++++++*/ static void demoninit(void) { pid_t pid=0; pid=fork(); if(pid==-1) PrintMessage(Fatal,"Cannot fork() to detach(1) [%!s]."); else if(pid) exit(0); setsid(); pid=fork(); if(pid==-1) PrintMessage(Fatal,"Cannot fork() to detach(2) [%!s]."); else if(pid) { /* print the child pid on stdout as requested */ if(print_pid) printf("%d\n", pid); exit(0); } } /*++++++++++++++++++++++++++++++++++++++ Install the signal handlers. ++++++++++++++++++++++++++++++++++++++*/ static void install_sighandlers(void) { struct sigaction action; /* SIGCHLD */ action.sa_handler = sigchild; sigemptyset(&action.sa_mask); action.sa_flags = 0; if(sigaction(SIGCHLD, &action, NULL) != 0) PrintMessage(Warning, "Cannot install SIGCHLD handler."); /* SIGINT, SIGQUIT, SIGTERM */ action.sa_handler = sigexit; sigemptyset(&action.sa_mask); sigaddset(&action.sa_mask, SIGINT); /* Block all of them */ sigaddset(&action.sa_mask, SIGQUIT); sigaddset(&action.sa_mask, SIGTERM); action.sa_flags = 0; if(sigaction(SIGINT, &action, NULL) != 0) PrintMessage(Warning, "Cannot install SIGINT handler."); if(sigaction(SIGQUIT, &action, NULL) != 0) PrintMessage(Warning, "Cannot install SIGQUIT handler."); if(sigaction(SIGTERM, &action, NULL) != 0) PrintMessage(Warning, "Cannot install SIGTERM handler."); /* SIGHUP */ action.sa_handler = sighup; sigemptyset(&action.sa_mask); action.sa_flags = 0; if(sigaction(SIGHUP, &action, NULL) != 0) PrintMessage(Warning, "Cannot install SIGHUP handler."); /* SIGPIPE */ action.sa_handler = SIG_IGN; sigemptyset(&action.sa_mask); action.sa_flags = 0; if(sigaction(SIGPIPE, &action, NULL) != 0) PrintMessage(Warning, "Cannot ignore SIGPIPE."); } /*++++++++++++++++++++++++++++++++++++++ The signal handler for the child exiting. int signum The signal number. ++++++++++++++++++++++++++++++++++++++*/ static void sigchild(/*@unused@*/ int signum) { got_sigchld=1; } /*++++++++++++++++++++++++++++++++++++++ The signal handler for the signals to tell us to exit. int signum The signal number. ++++++++++++++++++++++++++++++++++++++*/ static void sigexit(/*@unused@*/ int signum) { got_sigexit=1; } /*++++++++++++++++++++++++++++++++++++++ The signal handler for the signals to tell us to re-read the config file. int signum The signal number. ++++++++++++++++++++++++++++++++++++++*/ static void sighup(/*@unused@*/ int signum) { got_sighup=1; } .