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