Removed the obsolete 'setup' utility. It is not used by any currently supported...
[citadel.git] / webcit / webserver.c
1 // Copyright (c) 1996-2021 by the citadel.org team
2 //
3 // This program is open source software.  You can redistribute it and/or
4 // modify it under the terms of the GNU General Public License version 3.
5 //
6 // This program is distributed in the hope that it will be useful,
7 // but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9 // GNU General Public License for more details.
10
11 #include "webcit.h"
12 #include "webserver.h"
13 #include "modules_init.h"
14
15 extern int msock;                               /* master listening socket */
16 extern char static_icon_dir[PATH_MAX];          /* where should we find our mime icons */
17 int is_https = 0;                               /* Nonzero if I am an HTTPS service */
18 int follow_xff = 0;                             /* Follow X-Forwarded-For: header? */
19 int DisableGzip = 0;
20 char *default_landing_page = NULL;
21 extern pthread_mutex_t SessionListMutex;
22
23 extern void *housekeeping_loop(void);
24 extern int webcit_tcp_server(char *ip_addr, int port_number, int queue_len);
25 extern int webcit_uds_server(char *sockpath, int queue_len);
26 extern void graceful_shutdown_watcher(int signum);
27 extern void graceful_shutdown(int signum);
28 extern void start_daemon(char *pid_file);
29 extern void webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome);
30 extern void worker_entry(void);
31 extern void drop_root(uid_t UID);
32
33 char socket_dir[PATH_MAX];      /* where to talk to our citadel server */
34 char *server_cookie = NULL;     /* our Cookie connection to the client */
35 int http_port = PORT_NUM;       /* Port to listen on */
36 char *ctdlhost = DEFAULT_HOST;  /* Host name or IP address of Citadel server */
37 char *ctdlport = DEFAULT_PORT;  /* Port number of Citadel server */
38 int running_as_daemon = 0;      /* should we deamonize on startup? */
39
40 /* #define DBG_PRINNT_HOOKS_AT_START */
41 #ifdef DBG_PRINNT_HOOKS_AT_START
42 extern HashList *HandlerHash;
43 const char foobuf[32];
44 const char *nix(void *vptr) {snprintf(foobuf, 32, "%0x", (long) vptr); return foobuf;}
45 #endif 
46 extern int verbose;
47 extern int dbg_analyze_msg;
48 extern int dbg_backtrace_template_errors;
49 extern int DumpTemplateI18NStrings;
50 extern StrBuf *I18nDump;
51 void InitTemplateCache(void);
52 extern int LoadTemplates;
53
54 void LoadMimeBlacklist(void);
55
56 /*
57  * Here's where it all begins.
58  */
59 int main(int argc, char **argv) {
60         uid_t UID = -1;
61         pthread_t SessThread;           /* Thread descriptor */
62         pthread_attr_t attr;            /* Thread attributes */
63         int a;                          /* General-purpose variable */
64         char ip_addr[256]="*";
65         int relh=0;
66         int home=0;
67         char relhome[PATH_MAX]="";
68         char webcitdir[PATH_MAX] = DATADIR;
69         char *pidfile = NULL;
70         char *hdir;
71         const char *basedir = NULL;
72         char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
73         const char *I18nDumpFile = NULL;
74         int max_log_level = LOG_INFO;
75
76         WildFireInitBacktrace(argv[0], 2);
77
78         start_modules();
79
80 #ifdef DBG_PRINNT_HOOKS_AT_START
81 /*      dbg_PrintHash(HandlerHash, nix, NULL);*/
82 #endif
83
84         /* Ensure that we are linked to the correct version of libcitadel */
85         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
86                 fprintf(stderr, " You are running libcitadel version %d\n", libcitadel_version_number() );
87                 fprintf(stderr, "WebCit was compiled against version %d\n", LIBCITADEL_VERSION_NUMBER );
88                 return(1);
89         }
90
91         strcpy(uds_listen_path, "");
92
93         /* Parse command line */
94 #ifdef HAVE_OPENSSL
95         while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:g:dD:G:cfsS:Z:v:")) != EOF)
96 #else
97         while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:g:dD:G:cfZ:v:")) != EOF)
98 #endif
99                 switch (a) {
100                 case 'u':
101                         UID = atol(optarg);
102                         break;
103                 case 'h':
104                         hdir = strdup(optarg);
105                         relh=hdir[0]!='/';
106                         if (!relh) {
107                                 safestrncpy(webcitdir, hdir, sizeof webcitdir);
108                         }
109                         else {
110                                 safestrncpy(relhome, relhome, sizeof relhome);
111                         }
112                         /* free(hdir); TODO: SHOULD WE DO THIS? */
113                         home=1;
114                         break;
115                 case 'd':
116                         running_as_daemon = 1;
117                         break;
118                 case 'D':
119                         pidfile = strdup(optarg);
120                         running_as_daemon = 1;
121                         break;
122                 case 'g':
123                         default_landing_page = strdup(optarg);
124                         break;
125                 case 'B': /* deprecated */
126                         break;
127                 case 'i':
128                         safestrncpy(ip_addr, optarg, sizeof ip_addr);
129                         break;
130                 case 'p':
131                         http_port = atoi(optarg);
132                         if (http_port == 0) {
133                                 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
134                         }
135                         break;
136                 case 't':
137                         /* no longer used, but ignored so old scripts don't break */
138                         break;
139                 case 'T':
140                         LoadTemplates = atoi(optarg);
141                         dbg_analyze_msg = (LoadTemplates & (1<<1)) != 0;
142                         dbg_backtrace_template_errors = (LoadTemplates & (1<<2)) != 0;
143                         break;
144                 case 'Z':
145                         DisableGzip = 1;
146                         break;
147                 case 'x':
148                         max_log_level = atoi(optarg);
149                         break;
150                 case 'f':
151                         follow_xff = 1;
152                         break;
153                 case 'c':
154                         server_cookie = malloc(256);
155                         if (server_cookie != NULL) {
156                                 safestrncpy(server_cookie, "Set-cookie: wcserver=", 256);
157                                 if (gethostname (&server_cookie[strlen(server_cookie)], 200) != 0) {
158                                         syslog(LOG_INFO, "gethostname: %s", strerror(errno));
159                                         free(server_cookie);
160                                 }
161                         }
162                         break;
163 #ifdef HAVE_OPENSSL
164                 case 's':
165                         is_https = 1;
166                         break;
167                 case 'S':
168                         is_https = 1;
169                         ssl_cipher_list = strdup(optarg);
170                         break;
171 #endif
172                 case 'G':
173                         DumpTemplateI18NStrings = 1;
174                         I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
175                         I18nDumpFile = optarg;
176                         break;
177                 case 'v':
178                         verbose=1;
179                         break;
180                 default:
181                         fprintf(stderr, "usage:\nwebcit "
182                                 "[-i ip_addr] [-p http_port] "
183                                 "[-c] [-f] "
184                                 "[-T Templatedebuglevel] "
185                                 "[-d] [-Z] [-G i18ndumpfile] "
186                                 "[-u uid] [-h homedirectory] "
187                                 "[-D daemonizepid] [-v] "
188                                 "[-g defaultlandingpage] "
189 #ifdef HAVE_OPENSSL
190                                 "[-s] [-S cipher_suites]"
191 #endif
192                                 "[remotehost [remoteport]]\n");
193                         return 1;
194                 }
195
196         /* Start the logger */
197         setlogmask(LOG_UPTO(max_log_level));
198         openlog("webcit",
199                 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
200                 LOG_DAEMON
201         );
202
203         if (optind < argc) {
204                 ctdlhost = argv[optind];
205                 if (++optind < argc)
206                         ctdlport = argv[optind];
207         }
208
209         /* daemonize, if we were asked to */
210         if (!DumpTemplateI18NStrings && running_as_daemon) {
211                 start_daemon(pidfile);
212         }
213         else {
214                 signal(SIGINT, graceful_shutdown);
215                 signal(SIGHUP, graceful_shutdown);
216         }
217
218         webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
219         LoadMimeBlacklist();
220         LoadIconDir(static_icon_dir);
221
222         /* Tell 'em who's in da house */
223         syslog(LOG_NOTICE, "%s", PACKAGE_STRING);
224         syslog(LOG_NOTICE, "Copyright (C) 1996-2020 by the citadel.org team");
225         syslog(LOG_NOTICE, " ");
226         syslog(LOG_NOTICE, "This program is open source software: you can redistribute it and/or");
227         syslog(LOG_NOTICE, "modify it under the terms of the GNU General Public License, version 3.");
228         syslog(LOG_NOTICE, " ");
229         syslog(LOG_NOTICE, "This program is distributed in the hope that it will be useful,");
230         syslog(LOG_NOTICE, "but WITHOUT ANY WARRANTY; without even the implied warranty of");
231         syslog(LOG_NOTICE, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the");
232         syslog(LOG_NOTICE, "GNU General Public License for more details.");
233         syslog(LOG_NOTICE, " ");
234
235         /* run from the webcit home directory */
236         if (chdir(webcitdir) != 0) {
237                 syslog(LOG_ERR, "webcit: %s: %m", webcitdir);
238                 exit(errno);
239         }
240
241         /* initialize various subsystems */
242         initialise_modules();
243         initialise2_modules();
244         InitTemplateCache();
245         if (DumpTemplateI18NStrings) {
246                 FILE *fd;
247                 StrBufAppendBufPlain(I18nDump, HKEY("}\n"), 0);
248                 if (StrLength(I18nDump) < 50) {
249                         syslog(LOG_INFO, "*******************************************************************\n");
250                         syslog(LOG_INFO, "*   No strings found in templates!  Are you sure they're there?   *\n");
251                         syslog(LOG_INFO, "*******************************************************************\n");
252                         return -1;
253                 }
254                 fd = fopen(I18nDumpFile, "w");
255                 if (fd == NULL) {
256                         syslog(LOG_INFO, "***********************************************\n");
257                         syslog(LOG_INFO, "*   unable to open I18N dumpfile [%s]         *\n", I18nDumpFile);
258                         syslog(LOG_INFO, "***********************************************\n");
259                         return -1;
260                 }
261                 fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
262                 fclose(fd);
263                 return 0;
264         }
265
266         /* Tell libical to return an error instead of aborting if it sees badly formed iCalendar data. */
267
268 #ifdef LIBICAL_ICAL_EXPORT      // cheap and sleazy way to detect libical >=2.0
269                 icalerror_set_errors_are_fatal(0);
270 #else
271                 icalerror_errors_are_fatal = 0;
272 #endif
273
274         /* Use our own prefix on tzid's generated from system tzdata */
275         icaltimezone_set_tzid_prefix("/citadel.org/");
276
277         /*
278          * Set up a place to put thread-specific data.
279          * We only need a single pointer per thread - it points to the
280          * wcsession struct to which the thread is currently bound.
281          */
282         if (pthread_key_create(&MyConKey, NULL) != 0) {
283                 syslog(LOG_ERR, "Can't create TSD key: %s", strerror(errno));
284         }
285         InitialiseSemaphores();
286
287         /*
288          * Set up a place to put thread-specific SSL data.
289          * We don't stick this in the wcsession struct because SSL starts
290          * up before the session is bound, and it gets torn down between
291          * transactions.
292          */
293 #ifdef HAVE_OPENSSL
294         if (pthread_key_create(&ThreadSSL, NULL) != 0) {
295                 syslog(LOG_ERR, "Can't create TSD key: %s", strerror(errno));
296         }
297 #endif
298
299         /*
300          * Bind the server to our favorite port.
301          * There is no need to check for errors, because webcit_tcp_server()
302          * exits if it doesn't succeed.
303          */
304
305         if (!IsEmptyStr(uds_listen_path)) {
306                 syslog(LOG_DEBUG, "Attempting to create listener socket at %s...", uds_listen_path);
307                 msock = webcit_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
308         }
309         else {
310                 syslog(LOG_DEBUG, "Attempting to bind to port %d...", http_port);
311                 msock = webcit_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
312         }
313         if (msock < 0)
314         {
315                 ShutDownWebcit();
316                 return -msock;
317         }
318
319         syslog(LOG_INFO, "Listening on socket %d", msock);
320         signal(SIGPIPE, SIG_IGN);
321
322         pthread_mutex_init(&SessionListMutex, NULL);
323
324         /*
325          * Start up the housekeeping thread
326          */
327         pthread_attr_init(&attr);
328         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
329         pthread_create(&SessThread, &attr, (void *(*)(void *)) housekeeping_loop, NULL);
330
331         /*
332          * If this is an HTTPS server, fire up SSL
333          */
334 #ifdef HAVE_OPENSSL
335         if (is_https) {
336                 init_ssl();
337         }
338 #endif
339         drop_root(UID);
340
341         /* Become a worker thread.  More worker threads will be spawned as they are needed. */
342         worker_entry();
343         ShutDownLibCitadel();
344         return 0;
345 }
346
347
348
349
350
351
352