-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathalias.c
632 lines (556 loc) · 18.4 KB
/
alias.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*
* Boa, an http server
* Copyright (C) 1995 Paul Phillips <[email protected]>
* Some changes Copyright (C) 1996 Larry Doolittle <[email protected]>
* Some changes Copyright (C) 1996 Russ Nelson <[email protected]>
* Some changes Copyright (C) 1996-2002 Jon Nelson <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/* $Id: alias.c,v 1.70.2.4 2002/07/28 02:46:52 jnelson Exp $ */
#include "boa.h"
static alias *alias_hashtable[ALIAS_HASHTABLE_SIZE];
int get_alias_hash_value(char *file);
/*
* Name: get_alias_hash_value
*
* Description: adds the ASCII values of the file letters
* and mods by the hashtable size to get the hash value
* Note: stops at first '/' (or '\0')
*/
int get_alias_hash_value(char *file)
{
unsigned int hash = 0;
unsigned int index = 0;
unsigned char c;
hash = file[index++];
while ((c = file[index++]) && c != '/')
hash += (unsigned int) c;
return hash % ALIAS_HASHTABLE_SIZE;
}
/*
* Name: add_alias
*
* Description: add an Alias, Redirect, or ScriptAlias to the
* alias hash table.
*/
void add_alias(char *fakename, char *realname, int type)
{
int hash;
alias *old, *new;
int fakelen, reallen;
/* sanity checking */
if (fakename == NULL || realname == NULL) {
DIE("NULL values sent to add_alias");
}
fakelen = strlen(fakename);
reallen = strlen(realname);
if (fakelen == 0 || reallen == 0) {
DIE("empty values sent to add_alias");
}
hash = get_alias_hash_value(fakename);
old = alias_hashtable[hash];
if (old) {
while (old->next) {
if (!strcmp(fakename, old->fakename)) /* don't add twice */
return;
old = old->next;
}
}
new = (alias *) malloc(sizeof (alias));
if (!new) {
DIE("out of memory adding alias to hash");
}
if (old)
old->next = new;
else
alias_hashtable[hash] = new;
new->fakename = strdup(fakename);
if (!new->fakename) {
DIE("failed strdup");
}
new->fake_len = fakelen;
/* check for "here" */
if (realname[0] == '.' && realname[1] == '/') {
new->realname = normalize_path(realname+2);
if (!new->realname) {
/* superfluous - normalize_path checks for NULL return values. */
DIE("normalize_path returned NULL");
}
reallen = strlen(new->realname);
} else {
new->realname = strdup(realname);
if (!new->realname) {
DIE("strdup of realname failed");
}
}
new->real_len = reallen;
new->type = type;
new->next = NULL;
}
/*
* Name: find_alias
*
* Description: Locates uri in the alias hashtable if it exists.
*
* Returns:
*
* alias structure or NULL if not found
*/
alias *find_alias(char *uri, int urilen)
{
alias *current;
int hash;
/* Find ScriptAlias, Alias, or Redirect */
if (urilen == 0)
urilen = strlen(uri);
hash = get_alias_hash_value(uri);
current = alias_hashtable[hash];
while (current) {
#ifdef FASCIST_LOGGING
fprintf(stderr,
"%s:%d - comparing \"%s\" (request) to \"%s\" (alias): ",
__FILE__, __LINE__, uri, current->fakename);
#endif
/* current->fake_len must always be:
* shorter or equal to the uri
*/
/*
* when performing matches:
* If the virtual part of the url ends in '/', and
* we get a match, stop there.
* Otherwise, we require '/' or '\0' at the end of the url.
* We only check if the virtual path does *not* end in '/'
*/
if (current->fake_len <= urilen &&
!memcmp(uri, current->fakename, current->fake_len) &&
(current->fakename[current->fake_len - 1] == '/' ||
(current->fakename[current->fake_len - 1] != '/' &&
(uri[current->fake_len] == '\0' ||
uri[current->fake_len] == '/')))) {
#ifdef FASCIST_LOGGING
fprintf(stderr, "Got it!\n");
#endif
return current;
}
#ifdef FASCIST_LOGGING
else
fprintf(stderr, "Don't Got it!\n");
#endif
current = current->next;
}
return current;
}
/*
* Name: translate_uri
*
* Description: Parse a request's virtual path.
* Sets query_string, pathname directly.
* Also sets path_info, path_translated, and script_name via
* init_script_alias
*
* Note: NPH in user dir is currently broken
*
* Note -- this should be broken up.
*
* Return values:
* 0: failure, close it down
* 1: success, continue
*/
int translate_uri(request * req)
{
static char buffer[MAX_HEADER_LENGTH + 1];
char *req_urip;
alias *current;
char *p;
int uri_len;
req_urip = req->request_uri;
if (req_urip[0] != '/') {
send_r_bad_request(req);
return 0;
}
uri_len = strlen(req->request_uri);
current = find_alias(req->request_uri, uri_len);
if (current) {
if (current->type == SCRIPTALIAS) /* Script */
return init_script_alias(req, current, uri_len);
/* not a script alias, therefore begin filling in data */
{
int len;
len = current->real_len;
len += uri_len - current->fake_len;
if (len > MAX_HEADER_LENGTH) {
log_error_doc(req);
fputs("uri too long!\n", stderr);
send_r_bad_request(req);
return 0;
}
memcpy(buffer, current->realname, current->real_len);
memcpy(buffer + current->real_len,
req->request_uri + current->fake_len,
uri_len - current->fake_len + 1);
}
if (current->type == REDIRECT) { /* Redirect */
if (req->method == M_POST) { /* POST to non-script */
/* it's not a cgi, but we try to POST??? */
send_r_bad_request(req);
return 0; /* not a script alias, therefore begin filling in data */
}
send_r_moved_temp(req, buffer, "");
return 0;
} else { /* Alias */
req->pathname = strdup(buffer);
if (!req->pathname) {
send_r_error(req);
WARN("unable to strdup buffer onto req->pathname");
return 0;
}
return 1;
}
}
/*
The reason why this is *not* an 'else if' is that,
after aliasing, we still have to check for '~' expansion
*/
if (user_dir && req->request_uri[1] == '~') {
char *user_homedir;
req_urip = req->request_uri + 2;
/* since we have uri_len which is from strlen(req->request_uri) */
p = memchr(req_urip, '/', uri_len - 2);
if (p)
*p = '\0';
user_homedir = get_home_dir(req_urip);
if (p) /* have to restore request_uri in case of error */
*p = '/';
if (!user_homedir) { /*no such user */
send_r_not_found(req);
return 0;
}
{
int l1 = strlen(user_homedir);
int l2 = strlen(user_dir);
int l3 = (p ? strlen(p) : 0);
if (l1 + l2 + l3 + 1 > MAX_HEADER_LENGTH) {
log_error_doc(req);
fputs("uri too long!\n", stderr);
send_r_bad_request(req);
return 0;
}
memcpy(buffer, user_homedir, l1);
buffer[l1] = '/';
memcpy(buffer + l1 + 1, user_dir, l2 + 1);
if (p)
memcpy(buffer + l1 + 1 + l2, p, l3 + 1);
}
} else if (document_root) {
/* no aliasing, no userdir... */
int l1, l2, l3;
l1 = strlen(document_root);
l2 = strlen(req->request_uri);
if (virtualhost)
l3 = strlen(req->local_ip_addr);
else
l3 = 0;
if (l1 + l2 + l3 + 1 > MAX_HEADER_LENGTH) {
log_error_doc(req);
fputs("uri too long!\n", stderr);
send_r_bad_request(req);
return 0;
}
/* the 'l2 + 1' is there so we copy the '\0' as well */
memcpy(buffer, document_root, l1);
if (virtualhost) {
buffer[l1] = '/';
memcpy(buffer + l1 + 1, req->local_ip_addr, l3);
memcpy(buffer + l1 + 1 + l3, req->request_uri, l2 + 1);
} else
memcpy(buffer + l1, req->request_uri, l2 + 1);
} else {
/* not aliased. not userdir. not part of document_root. BAIL */
send_r_not_found(req);
return 0;
}
/* if here,
* o it may be aliased but it's not a redirect or a script...
* o it may be a homedir
* o it may be a document_root resource (with or without virtual host)
*/
req->pathname = strdup(buffer);
if (!req->pathname) {
WARN("Could not strdup buffer for req->pathname!");
send_r_error(req);
return 0;
}
/* below we support cgis outside of a ScriptAlias */
if (strcmp(CGI_MIME_TYPE, get_mime_type(buffer)) == 0) { /* cgi */
#ifdef FASCIST_LOGGING
log_error_time();
fprintf(stderr, "%s:%d - buffer is: \"%s\"\n",
__FILE__, __LINE__, buffer);
#endif
/* FIXME */
/* script_name could end up as /cgi-bin/bob/extra_path */
req->script_name = strdup(req->request_uri);
if (!req->script_name) {
WARN("Could not strdup req->request_uri for req->script_name");
send_r_error(req);
return 0;
}
if (req->simple)
req->is_cgi = NPH;
else
req->is_cgi = CGI;
return 1;
} else if (req->method == M_POST) { /* POST to non-script */
/* it's not a cgi, but we try to POST??? */
send_r_bad_request(req);
return 0;
} else /* we are done!! */
return 1;
}
/*
* Name: init_script_alias
*
* Description: Performs full parsing on a ScriptAlias request
* Sets path_info and script_name
*
* Return values:
*
* 0: failure, shut down
* 1: success, continue
*/
int init_script_alias(request * req, alias * current1, int uri_len)
{
static char pathname[MAX_HEADER_LENGTH + 1];
struct stat statbuf;
static char buffer[MAX_HEADER_LENGTH + 1];
int index = 0;
char c;
int err;
/* copies the "real" path + the non-alias portion of the
uri to pathname.
*/
if (uri_len - current1->fake_len + current1->real_len >
MAX_HEADER_LENGTH) {
log_error_doc(req);
fputs("uri too long!\n", stderr);
send_r_bad_request(req);
return 0;
}
memcpy(pathname, current1->realname, current1->real_len);
memcpy(pathname + current1->real_len,
&req->request_uri[current1->fake_len],
uri_len - current1->fake_len + 1); /* the +1 copies the NUL */
#ifdef FASCIST_LOGGING
log_error_time();
fprintf(stderr,
"%s:%d - pathname in init_script_alias is: \"%s\" (\"%s\")\n",
__FILE__, __LINE__, pathname, pathname + current1->real_len);
#endif
if (strncmp("nph-", pathname + current1->real_len, 4) == 0
|| req->simple) req->is_cgi = NPH;
else
req->is_cgi = CGI;
/* start at the beginning of the actual uri...
(in /cgi-bin/bob, start at the 'b' in bob */
index = current1->real_len;
/* go to first and successive '/' and keep checking
* if it is a full pathname
* on success (stat (not lstat) of file is a *regular file*)
*/
do {
c = pathname[++index];
if (c == '/') {
pathname[index] = '\0';
err = stat(pathname, &statbuf);
pathname[index] = '/';
if (err == -1) {
send_r_not_found(req);
return 0;
}
/* is it a dir? */
if (!S_ISDIR(statbuf.st_mode)) {
/* check access */
if (!(statbuf.st_mode &
(S_IFREG | /* regular file */
(S_IRUSR | S_IXUSR) | /* u+rx */
(S_IRGRP | S_IXGRP) | /* g+rx */
(S_IROTH | S_IXOTH)))) { /* o+rx */
send_r_forbidden(req);
return 0;
}
/* stop here */
break;
}
}
} while (c != '\0');
req->script_name = strdup(req->request_uri);
if (!req->script_name) {
send_r_error(req);
WARN("unable to strdup req->request_uri for req->script_name");
return 0;
}
if (c == '\0') {
err = stat(pathname, &statbuf);
if (err == -1) {
send_r_not_found(req);
return 0;
}
/* is it a dir? */
if (!S_ISDIR(statbuf.st_mode)) {
/* check access */
if (!(statbuf.st_mode &
(S_IFREG | /* regular file */
(S_IRUSR | S_IXUSR) | /* u+rx */
(S_IRGRP | S_IXGRP) | /* g+rx */
(S_IROTH | S_IXOTH)))) { /* o+rx */
send_r_forbidden(req);
return 0;
}
/* stop here */
} else {
send_r_forbidden(req);
return 0;
}
}
/* we have path_info if c == '/'... still have to check for query */
else if (c == '/') {
int hash;
alias *current;
int path_len;
req->path_info = strdup(pathname + index);
if (!req->path_info) {
send_r_error(req);
WARN("unable to strdup pathname + index for req->path_info");
return 0;
}
pathname[index] = '\0'; /* strip path_info from path */
path_len = strlen(req->path_info);
/* we need to fix script_name here */
/* index points into pathname, which is
* realname/cginame/foo
* and index points to the '/foo' part
*/
req->script_name[strlen(req->script_name) - path_len] = '\0'; /* zap off the /foo part */
/* now, we have to re-alias the extra path info....
this sucks.
*/
hash = get_alias_hash_value(req->path_info);
current = alias_hashtable[hash];
while (current && !req->path_translated) {
if (!strncmp(req->path_info, current->fakename,
current->fake_len)) {
if (current->real_len +
path_len - current->fake_len > MAX_HEADER_LENGTH) {
log_error_doc(req);
fputs("uri too long!\n", stderr);
send_r_bad_request(req);
return 0;
}
memcpy(buffer, current->realname, current->real_len);
strcpy(buffer + current->real_len,
&req->path_info[current->fake_len]);
req->path_translated = strdup(buffer);
if (!req->path_translated) {
send_r_error(req);
WARN("unable to strdup buffer for req->path_translated");
return 0;
}
}
current = current->next;
}
/* no alias... try userdir */
if (!req->path_translated && user_dir && req->path_info[1] == '~') {
char *user_homedir;
char *p;
p = strchr(pathname + index + 1, '/');
if (p)
*p = '\0';
user_homedir = get_home_dir(pathname + index + 2);
if (p)
*p = '/';
if (!user_homedir) { /* no such user */
send_r_not_found(req);
return 0;
}
{
int l1 = strlen(user_homedir);
int l2 = strlen(user_dir);
int l3;
if (p)
l3 = strlen(p);
else
l3 = 0;
req->path_translated = malloc(l1 + l2 + l3 + 2);
if (req->path_translated == NULL) {
send_r_error(req);
WARN("unable to malloc memory for req->path_translated");
return 0;
}
memcpy(req->path_translated, user_homedir, l1);
req->path_translated[l1] = '/';
memcpy(req->path_translated + l1 + 1, user_dir, l2 + 1);
if (p)
memcpy(req->path_translated + l1 + 1 + l2, p, l3 + 1);
}
}
if (!req->path_translated && document_root) {
/* no userdir, no aliasing... try document root */
int l1, l2;
l1 = strlen(document_root);
l2 = path_len;
req->path_translated = malloc(l1 + l2 + 1);
if (req->path_translated == NULL) {
send_r_error(req);
WARN("unable to malloc memory for req->path_translated");
return 0;
}
memcpy(req->path_translated, document_root, l1);
memcpy(req->path_translated + l1, req->path_info, l2 + 1);
}
}
req->pathname = strdup(pathname);
if (!req->pathname) {
send_r_error(req);
WARN("unable to strdup pathname for req->pathname");
return 0;
}
return 1;
}
/*
* Empties the alias hashtable, deallocating any allocated memory.
*/
void dump_alias(void)
{
int i;
alias *temp;
for (i = 0; i < ALIAS_HASHTABLE_SIZE; ++i) { /* these limits OK? */
if (alias_hashtable[i]) {
temp = alias_hashtable[i];
while (temp) {
alias *temp_next;
if (temp->fakename)
free(temp->fakename);
if (temp->realname)
free(temp->realname);
temp_next = temp->next;
free(temp);
temp = temp_next;
}
alias_hashtable[i] = NULL;
}
}
}