2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * Sergey Lyubka wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return.
13 static int isbyte(int n) { return (n >= 0 && n <= 255); }
16 set_acl(struct shttpd_ctx *ctx, const char *s)
18 struct acl *acl = NULL;
20 int len, a, b, c, d, n, mask;
21 struct llhead *lp, *tmp;
23 /* Delete the old ACLs if any */
24 LL_FOREACH_SAFE(&ctx->acl, lp, tmp)
25 free(LL_ENTRY(lp, struct acl, link));
27 FOR_EACH_WORD_IN_LIST(s, len) {
31 if (sscanf(s, "%c%d.%d.%d.%d%n",&flag,&a,&b,&c,&d,&n) != 5) {
32 elog(E_FATAL, NULL, "[%s]: subnet must be "
33 "[+|-]x.x.x.x[/x]", s);
34 } else if (flag != '+' && flag != '-') {
35 elog(E_FATAL, NULL, "flag must be + or -: [%s]", s);
36 } else if (!isbyte(a)||!isbyte(b)||!isbyte(c)||!isbyte(d)) {
37 elog(E_FATAL, NULL, "bad ip address: [%s]", s);
38 } else if ((acl = malloc(sizeof(*acl))) == NULL) {
39 elog(E_FATAL, NULL, "%s", "cannot malloc subnet");
40 } else if (sscanf(s + n, "/%d", &mask) == 0) {
41 /* Do nothing, no mask specified */
42 } else if (mask < 0 || mask > 32) {
43 elog(E_FATAL, NULL, "bad subnet mask: %d [%s]", n, s);
46 acl->ip = (a << 24) | (b << 16) | (c << 8) | d;
47 acl->mask = mask ? 0xffffffffU << (32 - mask) : 0;
49 LL_TAIL(&ctx->acl, &acl->link);
55 * Dynamically load SSL library. Set up ctx->ssl_ctx pointer.
58 set_ssl(struct shttpd_ctx *ctx, const char *pem)
64 /* Load SSL library dynamically */
65 if ((lib = dlopen(SSL_LIB, RTLD_LAZY)) == NULL)
66 elog(E_FATAL, NULL, "set_ssl: cannot load %s", SSL_LIB);
68 for (fp = ssl_sw; fp->name != NULL; fp++)
69 if ((fp->ptr.v_void = dlsym(lib, fp->name)) == NULL)
70 elog(E_FATAL, NULL,"set_ssl: cannot find %s", fp->name);
72 /* Initialize SSL crap */
75 if ((CTX = SSL_CTX_new(SSLv23_server_method())) == NULL)
76 elog(E_FATAL, NULL, "SSL_CTX_new error");
77 else if (SSL_CTX_use_certificate_file(CTX, pem, SSL_FILETYPE_PEM) == 0)
78 elog(E_FATAL, NULL, "cannot open %s", pem);
79 else if (SSL_CTX_use_PrivateKey_file(CTX, pem, SSL_FILETYPE_PEM) == 0)
80 elog(E_FATAL, NULL, "cannot open %s", pem);
86 open_log_file(FILE **fpp, const char *path)
93 } else if ((*fpp = fopen(path, "a")) == NULL) {
94 elog(E_FATAL, NULL, "cannot open log file %s: %s",
95 path, strerror(errno));
100 set_alog(struct shttpd_ctx *ctx, const char *path)
102 open_log_file(&ctx->access_log, path);
106 set_elog(struct shttpd_ctx *ctx, const char *path)
108 open_log_file(&ctx->error_log, path);
111 static void show_cfg_page(struct shttpd_arg *arg);
114 set_cfg_uri(struct shttpd_ctx *ctx, const char *uri)
116 free_list(&ctx->registered_uris, ®istered_uri_destructor);
119 shttpd_register_uri(ctx, uri, &show_cfg_page, ctx);
124 set_ports(struct shttpd_ctx *ctx, const char *p)
128 free_list(&ctx->listeners, &listener_destructor);
130 FOR_EACH_WORD_IN_LIST(p, len) {
131 is_ssl = p[len - 1] == 's' ? 1 : 0;
132 if (shttpd_listen(ctx, atoi(p), is_ssl) == -1)
134 "Cannot open socket on port %d", atoi(p));
138 static const struct opt {
139 int index; /* Index in shttpd_ctx */
140 const char *name; /* Option name in config file */
141 const char *description; /* Description */
142 const char *default_value; /* Default option value */
143 void (*setter)(struct shttpd_ctx *, const char *);
144 } known_options[] = {
145 {OPT_ROOT, "root", "\tWeb root directory", ".", NULL},
146 {OPT_INDEX_FILES, "index_files", "Index files", INDEX_FILES, NULL},
147 {OPT_PORTS, "ports", "Listening ports", LISTENING_PORTS, set_ports},
148 {OPT_DIR_LIST, "dir_list", "Directory listing", "1", NULL},
149 {OPT_CFG_URI, "cfg_uri", "Config uri", NULL, set_cfg_uri},
150 {OPT_PROTECT, "protect", "URI to htpasswd mapping", NULL, NULL},
152 {OPT_CGI_EXTENSIONS, "cgi_ext", "CGI extensions", CGI_EXT, NULL},
153 {OPT_CGI_INTERPRETER, "cgi_interp", "CGI interpreter", NULL, NULL},
154 {OPT_CGI_ENVIRONMENT, "cgi_env", "Additional CGI env vars", NULL, NULL},
156 {OPT_SSI_EXTENSIONS, "ssi_ext", "SSI extensions", SSI_EXT, NULL},
158 {OPT_AUTH_REALM, "auth_realm", "Authentication domain name",REALM,NULL},
159 {OPT_AUTH_GPASSWD, "auth_gpass", "Global passwords file", NULL, NULL},
160 {OPT_AUTH_PUT, "auth_PUT", "PUT,DELETE auth file", NULL, NULL},
161 #endif /* !NO_AUTH */
162 {OPT_ACCESS_LOG, "access_log", "Access log file", NULL, set_alog},
163 {OPT_ERROR_LOG, "error_log", "Error log file", NULL, set_elog},
164 {OPT_MIME_TYPES, "mime_types", "Additional mime types list", NULL,NULL},
166 {OPT_SSL_CERTIFICATE, "ssl_cert", "SSL certificate file", NULL,set_ssl},
168 {OPT_ALIASES, "aliases", "Path=URI mappings", NULL, NULL},
169 {OPT_ACL, "acl", "\tAllow/deny IP addresses/subnets", NULL, set_acl},
172 {OPT_INETD, "inetd", "Inetd mode", "0", NULL},
173 {OPT_UID, "uid", "\tRun as user", NULL, NULL},
175 {-1, NULL, NULL, NULL, NULL}
178 void shttpd_set_option(struct shttpd_ctx *ctx, const char *opt, const char *val)
182 for (o = known_options; o->name != NULL; o++)
183 if (!strcmp(opt, o->name))
187 elog(E_FATAL, NULL, "no such option: [%s]", opt);
189 /* Call option setter first, so it can use both new and old values */
190 if (o->setter != NULL)
193 /* Free old value if any */
194 if (ctx->options[o->index] != NULL)
195 free(ctx->options[o->index]);
197 /* Set new option value */
198 ctx->options[o->index] = val ? my_strdup(val) : NULL;
202 show_cfg_page(struct shttpd_arg *arg)
204 struct shttpd_ctx *ctx = arg->user_data;
205 char opt_name[20], value[BUFSIZ];
208 opt_name[0] = value[0] = '\0';
210 if (!strcmp(shttpd_get_env(arg, "REQUEST_METHOD"), "POST")) {
211 if (arg->flags & SHTTPD_MORE_POST_DATA)
213 (void) shttpd_get_var("o", arg->in.buf, arg->in.len,
214 opt_name, sizeof(opt_name));
215 (void) shttpd_get_var("v", arg->in.buf, arg->in.len,
216 value, sizeof(value));
217 shttpd_set_option(ctx, opt_name, value[0] ? value : NULL);
220 shttpd_printf(arg, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
221 "<html><body><h1>SHTTPD v. %s</h1>", shttpd_version());
223 shttpd_printf(arg, "%s", "<table border=1"
224 "<tr><th>Option</th><th>Description</th>"
225 "<th colspan=2>Value</th></tr>");
227 if (opt_name[0] != '\0' && value[0] != '\0')
228 shttpd_printf(arg, "<p style='color: green'>Saved: %s=%s</p>",
229 opt_name, value[0] ? value : "NULL");
232 for (o = known_options; o->name != NULL; o++) {
234 "<form method=post><tr><td>%s</td><td>%s</td>"
235 "<input type=hidden name=o value='%s'>"
236 "<td><input type=text name=v value='%s'></td>"
237 "<td><input type=submit value=save></td></form></tr>",
238 o->name, o->description, o->name,
239 ctx->options[o->index] ? ctx->options[o->index] : "");
242 shttpd_printf(arg, "%s", "</table></body></html>");
243 arg->flags |= SHTTPD_END_OF_OUTPUT;
246 * Show usage string and exit.
249 usage(const char *prog)
253 (void) fprintf(stderr,
254 "SHTTPD version %s (c) Sergey Lyubka\n"
255 "usage: %s [options] [config_file]\n", VERSION, prog);
257 #if !defined(NO_AUTH)
258 fprintf(stderr, " -A <htpasswd_file> <realm> <user> <passwd>\n");
261 for (o = known_options; o->name != NULL; o++) {
262 (void) fprintf(stderr, " -%s\t%s", o->name, o->description);
263 if (o->default_value != NULL)
264 fprintf(stderr, " (default: %s)", o->default_value);
271 struct shttpd_ctx *shttpd_init(void)
273 struct shttpd_ctx *ctx;
277 if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
278 elog(E_FATAL, NULL, "cannot allocate shttpd context");
280 /* Set default values */
281 for (o = known_options; o->name != NULL; o++) {
282 ctx->options[o->index] = o->default_value == NULL ?
283 NULL : my_strdup(o->default_value);
286 current_time = ctx->start_time = time(NULL);
287 tm = localtime(¤t_time);
290 tm->tm_gmtoff - 3600 * (tm->tm_isdst > 0 ? 1 : 0);
293 InitializeCriticalSection(&ctx->mutex);
295 LL_INIT(&ctx->connections);
296 LL_INIT(&ctx->registered_uris);
297 LL_INIT(&ctx->error_handlers);
299 LL_INIT(&ctx->ssi_funcs);
300 LL_INIT(&ctx->listeners);
303 {WSADATA data; WSAStartup(MAKEWORD(2,2), &data);}