forked from cesanta/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
291 lines (245 loc) · 8.22 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (c) 2004-2009 Sergey Lyubka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* $Id: main.c 518 2010-05-03 12:55:35Z valenok $
*/
#if defined(_WIN32)
#define _CRT_SECURE_NO_WARNINGS /* Disable deprecation warning in VS2005 */
#endif /* _WIN32 */
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <stddef.h>
#include "mongoose.h"
#ifdef _WIN32
#include <windows.h>
#include <winsvc.h>
#define DIRSEP '\\'
#define snprintf _snprintf
#if !defined(__LCC__)
#define strdup(x) _strdup(x)
#endif /* !MINGW */
#define sleep(x) Sleep((x) * 1000)
#else
#include <sys/wait.h>
#include <unistd.h> /* For pause() */
#define DIRSEP '/'
#endif /* _WIN32 */
static int exit_flag; /* Program termination flag */
#if !defined(CONFIG_FILE)
#define CONFIG_FILE "mongoose.conf"
#endif /* !CONFIG_FILE */
#define MAX_OPTIONS 40
static void signal_handler(int sig_num) {
#if !defined(_WIN32)
if (sig_num == SIGCHLD) {
do {
} while (waitpid(-1, &sig_num, WNOHANG) > 0);
} else
#endif /* !_WIN32 */
{
exit_flag = sig_num;
}
}
/*
* Edit the passwords file.
*/
static int mg_edit_passwords(const char *fname, const char *domain,
const char *user, const char *pass) {
struct mg_context *ctx;
const char *options[] = {"authentication_domain", NULL, NULL};
int success;
options[1] = domain;
ctx = mg_start(NULL, options);
success = mg_modify_passwords_file(ctx, fname, user, pass);
mg_stop(ctx);
return success;
}
static void show_usage_and_exit(void) {
const char **names;
int i, len;
fprintf(stderr, "Mongoose version %s (c) Sergey Lyubka\n", mg_version());
fprintf(stderr, "Usage:\n");
fprintf(stderr, " mongoose -A <htpasswd_file> <realm> <user> <passwd>\n");
fprintf(stderr, " mongoose <config_file>\n");
fprintf(stderr, " mongoose [-option value ...]\n");
fprintf(stderr, "OPTIONS:\n ");
names = mg_get_valid_option_names();
len = 2;
for (i = 0; names[i] != NULL; i++) {
len += strlen(names[i]) + 1;
if (len >= 80) {
len = strlen(names[i]) + 3;
fprintf(stderr, "%s", "\n ");
}
fprintf(stderr, "%s%c", names[i], names[i + 1] == NULL ? '\n' : ',');
}
fprintf(stderr, "See http://code.google.com/p/mongoose/wiki/MongooseManual"
" for more details.\n");
fprintf(stderr, "Example:\n mongoose -listening_ports 80,443s "
"-enable_directory_listing no\n");
exit(EXIT_FAILURE);
}
static void verify_document_root(const char *root) {
const char *p, *path;
char buf[PATH_MAX];
struct stat st;
path = root;
if ((p = strchr(root, ',')) != NULL && (size_t) (p - root) < sizeof(buf)) {
strncpy(buf, root, p - root);
path = buf;
}
if (stat(path, &st) != 0) {
fprintf(stderr, "Invalid root directory: \"%s\"\n", root);
exit(EXIT_FAILURE);
}
}
static char *sdup(const char *str) {
char *p;
if ((p = malloc(strlen(str) + 1)) != NULL) {
strcpy(p, str);
}
return p;
}
static void set_option(char **options, const char *name, const char *value) {
int i;
if (!strcmp(name, "document_root")) {
verify_document_root(value);
}
for (i = 0; i < MAX_OPTIONS - 3; i++) {
if (options[i] == NULL) {
options[i] = sdup(name);
options[i + 1] = sdup(value);
options[i + 2] = NULL;
break;
}
}
if (i == MAX_OPTIONS - 3) {
fprintf(stderr, "%s\n", "Too many options specified");
exit(EXIT_FAILURE);
}
}
static void process_command_line_arguments(char *argv[], char **options) {
const char *config_file = CONFIG_FILE;
char line[512], opt[512], *vals[100], val[512], path[FILENAME_MAX], *p;
FILE *fp;
size_t i, line_no = 0;
/* First find out, which config file to open */
for (i = 1; argv[i] != NULL && argv[i][0] == '-'; i += 2)
if (argv[i + 1] == NULL)
show_usage_and_exit();
if (argv[i] != NULL && argv[i + 1] != NULL) {
/* More than one non-option arguments are given */
show_usage_and_exit();
} else if (argv[i] != NULL) {
/* Just one non-option argument is given, this is config file */
config_file = argv[i];
} else {
/* No config file specified. Look for one where binary lives */
if ((p = strrchr(argv[0], DIRSEP)) != 0) {
snprintf(path, sizeof(path), "%.*s%s",
(int) (p - argv[0]) + 1, argv[0], config_file);
config_file = path;
}
}
fp = fopen(config_file, "r");
/* If config file was set in command line and open failed, exit */
if (fp == NULL && argv[i] != NULL) {
fprintf(stderr, "cannot open config file %s: %s\n",
config_file, strerror(errno));
exit(EXIT_FAILURE);
}
/* Reset temporary value holders */
(void) memset(vals, 0, sizeof(vals));
if (fp != NULL) {
printf("Loading config file %s, ignoring command line arguments\n",
config_file);
/* Loop over the lines in config file */
while (fgets(line, sizeof(line), fp) != NULL) {
line_no++;
/* Ignore empty lines and comments */
if (line[0] == '#' || line[0] == '\n')
continue;
if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) {
fprintf(stderr, "%s: line %d is invalid\n",
config_file, (int) line_no);
exit(EXIT_FAILURE);
}
set_option(options, opt, val);
}
(void) fclose(fp);
} else {
for (i = 1; argv[i] != NULL && argv[i][0] == '-'; i += 2)
set_option(options, &argv[i][1], argv[i + 1]);
}
}
int main(int argc, char *argv[]) {
struct mg_context *ctx;
char *options[MAX_OPTIONS];
int i;
/* Edit passwords file if -A option is specified */
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'A') {
if (argc != 6) {
show_usage_and_exit();
}
exit(mg_edit_passwords(argv[2], argv[3], argv[4], argv[5]) ?
EXIT_SUCCESS : EXIT_FAILURE);
}
/* Show usage if -h or --help options are specified */
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
show_usage_and_exit();
}
/* Update config based on command line arguments */
options[0] = NULL;
process_command_line_arguments(argv, options);
/* Setup signal handler: quit on Ctrl-C */
#ifndef _WIN32
signal(SIGCHLD, signal_handler);
#endif /* _WIN32 */
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
/* Start Mongoose */
ctx = mg_start(NULL, (const char **) options);
for (i = 0; options[i] != NULL; i++) {
free(options[i]);
}
if (ctx == NULL) {
(void) printf("%s\n", "Cannot initialize Mongoose context");
exit(EXIT_FAILURE);
}
printf("Mongoose %s started on port(s) %s with web root [%s]\n",
mg_version(), mg_get_option(ctx, "listening_ports"),
mg_get_option(ctx, "document_root"));
fflush(stdout);
while (exit_flag == 0) {
sleep(1);
}
printf("Exiting on signal %d, waiting for all threads to finish...",
exit_flag);
fflush(stdout);
mg_stop(ctx);
printf("%s", " done.\n");
return EXIT_SUCCESS;
}