-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.c
More file actions
231 lines (201 loc) · 7.73 KB
/
Copy pathoptions.c
File metadata and controls
231 lines (201 loc) · 7.73 KB
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
/*
* Gophernicus - Copyright (c) 2009-2014 Kim Holviala <kim@holviala.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ugopherserver.h"
#include "sys/types.h"
#include "pwd.h"
#include "grp.h"
/*
* Add one suffix->filetype mapping to the filetypes array
*/
void add_ftype_mapping(state *st, char *suffix)
{
char *type;
int i;
/* Let's not do anything stupid */
if (!*suffix) return;
if (!(type = strchr(suffix, '='))) return;
/* Extract type from the suffix=X string */
*type++ = '\0';
if (!*type) return;
/* Loop through the filetype array */
for (i = 0; i < st->filetype_count; i++) {
/* Old entry found? */
if (strcasecmp(st->filetype[i].suffix, suffix) == MATCH) {
st->filetype[i].type = *type;
return;
}
}
/* No old entry found - add new entry */
if (i < MAX_FILETYPES) {
sstrlcpy(st->filetype[i].suffix, suffix);
st->filetype[i].type = *type;
st->filetype_count++;
}
}
/*
* Add one selector rewrite mapping to the array
*/
void add_rewrite_mapping(state *st, char *match)
{
char *replace;
/* Check input and split it into match & replace */
if (!*match) return;
if (!(replace = strchr(match, '='))) return;
*replace++ = '\0';
if (!*replace) return;
/* Insert match/replace values into the array */
if (st->rewrite_count < MAX_REWRITE) {
sstrlcpy(st->rewrite[st->rewrite_count].match, match);
sstrlcpy(st->rewrite[st->rewrite_count].replace, replace);
st->rewrite_count++;
}
}
/*
* Parse command-line arguments
*/
void parse_args(state *st, int argc, char *argv[])
{
FILE *fp;
static const char readme[] = README;
static const char license[] = LICENSE;
struct stat file;
char buf[BUFSIZE];
int opt;
struct passwd *pwnam;
struct group *grnam;
/* Parse args */
st->out_protection = !TRUE;
while ((opt = getopt(argc, argv, "h:p:r:t:g:H:T:F:U:G:a:c:u:m:l:w:o:s:i:k:f:e:R:D:L:A:P:S:K:n:db?-")) != ERROR) {
switch(opt) {
case 'h': sstrlcpy(st->server_host, optarg); break;
case 'p': st->server_port = atoi(optarg); break;
case 'r': sstrlcpy(st->server_root, optarg); break;
case 't': st->default_filetype = *optarg; break;
case 'g': sstrlcpy(st->map_file, optarg); break;
case 'a': sstrlcpy(st->tag_file, optarg); break;
case 'H': sstrlcpy(st->hdr_ext, optarg); break;
case 'T': sstrlcpy(st->tag_ext, optarg); break;
case 'F': sstrlcpy(st->ftr_ext, optarg); break;
case 'c': sstrlcpy(st->cgi_file, optarg); break;
case 'u': sstrlcpy(st->user_dir, optarg); break;
case 'm': /* obsolete, replaced by -l */
case 'l': sstrlcpy(st->log_file, optarg); break;
case 'w': st->out_width = atoi(optarg); break;
case 'o':
if (sstrncasecmp(optarg, "UTF-8") == MATCH) st->out_charset = UTF_8;
if (sstrncasecmp(optarg, "ISO-8859-1") == MATCH) st->out_charset = ISO_8859_1;
break;
case 'S':
st->out_protection = TRUE;
sstrlcpy(st->protection_certkeyfile, optarg);
break;
case 'K':
sstrlcpy(st->protection_cipherlist, optarg);
break;
case 'U':
pwnam = getpwnam(optarg);
if (pwnam == NULL && getuid() == 0) {
printf("3 is the server administrator smoking craq? i'm running as root and the drop-user (%s) is fake!\t/\tfake\t0" CRLF, optarg);
exit(0);
break;
} else if (pwnam->pw_uid < 1 && getuid() == 0) {
printf("3 is the server administrator smoking craq? i'm running as root and the drop-user (%i: %s) is root!\t/\tfake\t0" CRLF, pwnam->pw_uid, optarg);
exit(0);
break;
}
st->drop_uid = pwnam->pw_uid;
if (st->drop_gid == 0) {
st->drop_gid = pwnam->pw_gid;
}
break;
case 'G':
grnam = getgrnam(optarg);
if (grnam == NULL && getgid() == 0) {
printf("3 is the server administrator smoking craq? i'm running as root's group and drop-group (%s) is fake!\t/\tfake\t0" CRLF, optarg);
exit(0);
break;
} else if (grnam->gr_gid < 1 && getgid() == 0) {
printf("3 is the server administrator smoking craq? i'm running as root's group and the drop-group (%i: %s) is root's group!\t/\tfake\t0" CRLF, grnam->gr_gid, optarg);
exit(0);
break;
}
st->drop_gid = grnam->gr_gid;
break;
case 's': st->session_timeout = atoi(optarg); break;
case 'i': st->session_max_kbytes = abs(atoi(optarg)); break;
case 'k': st->session_max_hits = abs(atoi(optarg)); break;
case 'f': sstrlcpy(st->filter_dir, optarg); break;
case 'e': add_ftype_mapping(st, optarg); break;
case 'R': add_rewrite_mapping(st, optarg); break;
case 'D': sstrlcpy(st->server_description, optarg); break;
case 'L': sstrlcpy(st->server_location, optarg); break;
case 'A': sstrlcpy(st->server_admin, optarg); break;
case 'n':
if (*optarg == 'v') { st->opt_vhost = FALSE; break; }
if (*optarg == 'l') { st->opt_parent = FALSE; break; }
if (*optarg == 'h') { st->opt_header = FALSE; break; }
if (*optarg == 'f') { st->opt_footer = FALSE; break; }
if (*optarg == 'd') { st->opt_date = FALSE; break; }
if (*optarg == 'c') { st->opt_magic = FALSE; break; }
if (*optarg == 'o') { st->opt_iconv = FALSE; break; }
if (*optarg == 'q') { st->opt_query = FALSE; break; }
if (*optarg == 's') { st->opt_syslog = FALSE; break; }
if (*optarg == 'a') { st->opt_caps = FALSE; break; }
if (*optarg == 'm') { st->opt_shm = FALSE; break; }
if (*optarg == 'r') { st->opt_root = FALSE; break; }
break;
case 'd': st->debug = TRUE; break;
case 'b': puts(license); exit(EXIT_SUCCESS);
default : puts(readme); exit(EXIT_SUCCESS);
}
}
/* Sanitize options */
if (st->out_width > MAX_WIDTH) st->out_width = MAX_WIDTH;
if (st->out_width < MIN_WIDTH) st->out_width = MIN_WIDTH;
if (st->out_width < MIN_WIDTH + DATE_WIDTH) st->opt_date = FALSE;
if (!st->opt_syslog) st->debug = FALSE;
/* Primary vhost directory must exist or we disable vhosting */
if (st->opt_vhost) {
snprintf(buf, sizeof(buf), "%s/%s", st->server_root, st->server_host);
if (stat(buf, &file) == ERROR) st->opt_vhost = FALSE;
}
/* If -D arg looks like a file load the file contents */
if (*st->server_description == '/') {
if ((fp = fopen(st->server_description , "r"))) {
fgets(st->server_description, sizeof(st->server_description), fp);
chomp(st->server_description);
fclose(fp);
}
else strclear(st->server_description);
}
/* If -L arg looks like a file load the file contents */
if (*st->server_location == '/') {
if ((fp = fopen(st->server_location , "r"))) {
fgets(st->server_location, sizeof(st->server_location), fp);
chomp(st->server_location);
fclose(fp);
}
else strclear(st->server_location);
}
}