9e2483cc4ab373ae1b5e6c40c07fc9dcd35d8c4e
[citadel.git] / webcit-ng / main.c
1 /*
2  * Main entry point for the program.
3  *
4  * Copyright (c) 1996-2019 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "webcit.h"             // All other headers are included from this header.
16
17 char *ctdlhost = CTDLHOST;
18 char *ctdlport = CTDLPORT;
19
20 /*
21  * Main entry point for the web server.
22  */
23 int main(int argc, char **argv)
24 {
25         int webserver_port = WEBSERVER_PORT;
26         char *webserver_interface = WEBSERVER_INTERFACE;
27         int running_as_daemon = 0;
28         int webserver_protocol = WEBSERVER_HTTP;
29         int a;
30         char *pid_file = NULL;
31
32         /* Parse command line */
33         while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:g:dD:G:cfsS:Z:v:")) != EOF)
34                 switch (a) {
35                 case 'u':
36                         setuid(atoi(optarg));
37                         break;
38                 case 'h':
39                         break;
40                 case 'd':
41                         running_as_daemon = 1;
42                         break;
43                 case 'D':
44                         running_as_daemon = 1;
45                         pid_file = strdup(optarg);
46                         break;
47                 case 'g':
48                         // FIXME set up the default landing page
49                         break;
50                 case 'B':
51                 case 't':
52                 case 'x':
53                 case 'T':
54                 case 'v':
55                         /* The above options are no longer used, but ignored so old scripts don't break */
56                         break;
57                 case 'i':
58                         webserver_interface = optarg;
59                         break;
60                 case 'p':
61                         webserver_port = atoi(optarg);
62                         break;
63                 case 'Z':
64                         // FIXME when gzip is added, disable it if this flag is set
65                         break;
66                 case 'f':
67                         //follow_xff = 1;
68                         break;
69                 case 'c':
70                         break;
71                 case 's':
72                         is_https = 1;
73                         webserver_protocol = WEBSERVER_HTTPS;
74                         break;
75                 case 'S':
76                         is_https = 1;
77                         webserver_protocol = WEBSERVER_HTTPS;
78                         //ssl_cipher_list = strdup(optarg);
79                         break;
80                 case 'G':
81                         //DumpTemplateI18NStrings = 1;
82                         //I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
83                         //I18nDumpFile = optarg;
84                         break;
85                 default:
86                         fprintf(stderr, "usage:\nwebcit "
87                                 "[-i ip_addr] [-p http_port] "
88                                 "[-c] [-f] "
89                                 "[-d] [-Z] [-G i18ndumpfile] "
90                                 "[-u uid] [-h homedirectory] "
91                                 "[-D daemonizepid] [-v] "
92                                 "[-g defaultlandingpage] [-B basesize] " "[-s] [-S cipher_suites]" "[remotehost [remoteport]]\n");
93                         return 1;
94                 }
95
96         if (optind < argc) {
97                 ctdlhost = argv[optind];
98                 if (++optind < argc)
99                         ctdlport = argv[optind];
100         }
101
102         /* Start the logger */
103         openlog("webcit", (running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR)), LOG_DAEMON);
104
105         /* Tell 'em who's in da house */
106         syslog(LOG_NOTICE, "MAKE WEBCIT GREAT AGAIN!");
107         syslog(LOG_NOTICE, "Copyright (C) 1996-2019 by the citadel.org team");
108         syslog(LOG_NOTICE, " ");
109         syslog(LOG_NOTICE, "This program is open source software: you can redistribute it and/or");
110         syslog(LOG_NOTICE, "modify it under the terms of the GNU General Public License, version 3.");
111         syslog(LOG_NOTICE, " ");
112         syslog(LOG_NOTICE, "This program is distributed in the hope that it will be useful,");
113         syslog(LOG_NOTICE, "but WITHOUT ANY WARRANTY; without even the implied warranty of");
114         syslog(LOG_NOTICE, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the");
115         syslog(LOG_NOTICE, "GNU General Public License for more details.");
116         syslog(LOG_NOTICE, " ");
117
118         /* Ensure that we are linked to the correct version of libcitadel */
119         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
120                 syslog(LOG_INFO, " You are running libcitadel version %d", libcitadel_version_number());
121                 syslog(LOG_INFO, "WebCit was compiled against version %d", LIBCITADEL_VERSION_NUMBER);
122                 return (1);
123         }
124
125         /* Go into the background if we were asked to run as a daemon */
126         if (running_as_daemon) {
127                 daemon(1, 0);
128                 if (pid_file != NULL) {
129                         FILE *pfp = fopen(pid_file, "w");
130                         if (pfp) {
131                                 fprintf(pfp, "%d\n", getpid());
132                                 fclose(pfp);
133                         }
134                 }
135         }
136
137         return webserver(webserver_interface, webserver_port, webserver_protocol);
138 }