In webcit-ng, allow -h to specify the Citadel Server data directory. This will allow...
[citadel.git] / webcit-ng / main.c
1 // Main entry point for the program.
2 //
3 // Copyright (c) 1996-2022 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                         ctdl_dir = strdup(optarg);
36                         break;
37                 case 'd':
38                         running_as_daemon = 1;
39                         break;
40                 case 'D':
41                         running_as_daemon = 1;
42                         pid_file = strdup(optarg);
43                         break;
44                 case 'g':
45                         // FIXME set up the default landing page
46                         break;
47                 case 'B':
48                 case 't':
49                 case 'x':
50                 case 'T':
51                 case 'v':
52                         // The above options are no longer used, but ignored so old scripts don't break
53                         break;
54                 case 'i':
55                         webserver_interface = optarg;
56                         break;
57                 case 'p':
58                         webserver_port = atoi(optarg);
59                         break;
60                 case 'Z':
61                         // FIXME when gzip is added, disable it if this flag is set
62                         break;
63                 case 'f':
64                         //follow_xff = 1;
65                         break;
66                 case 'c':
67                         break;
68                 case 's':
69                         is_https = 1;
70                         webserver_protocol = WEBSERVER_HTTPS;
71                         break;
72                 case 'S':
73                         is_https = 1;
74                         webserver_protocol = WEBSERVER_HTTPS;
75                         //ssl_cipher_list = strdup(optarg);
76                         break;
77                 case 'G':
78                         //DumpTemplateI18NStrings = 1;
79                         //I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
80                         //I18nDumpFile = optarg;
81                         break;
82                 default:
83                         fprintf(stderr, "usage:\nwebcit "
84                                 "[-i ip_addr] [-p http_port] "
85                                 "[-c] [-f] "
86                                 "[-d] [-Z] [-G i18ndumpfile] "
87                                 "[-u uid] [-h homedirectory] "
88                                 "[-D daemonizepid] [-v] "
89                                 "[-g defaultlandingpage] [-B basesize] "
90                                 "[-s] [-S cipher_suites]"
91                                 "[-h citadel_server_directory]\n"
92                         );
93                         return 1;
94                 }
95
96         while (optind < argc) {
97                 ctdl_dir = strdup(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-2022 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 }