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