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