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