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