1 |
/* |
2 |
* Boa, an http server |
3 |
* Copyright (C) 2002 Nikos Mavroyanopoulos <nmav@gnutls.org> |
4 |
* Based on patch for Boa by Peter Korsgaard <jacmet@sunsite.dk> |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 1, or (at your option) |
9 |
* any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
/* $Id$ */ |
22 |
|
23 |
#include "boa.h" |
24 |
#include "access.h" |
25 |
|
26 |
#ifdef ENABLE_ACCESS_LISTS |
27 |
|
28 |
#include <fnmatch.h> |
29 |
|
30 |
void access_add(const char* hostname, const char *pattern, const int type) |
31 |
{ |
32 |
virthost* vhost; |
33 |
|
34 |
if (hostname == NULL || pattern == NULL) { |
35 |
DIE("NULL values sent to access_add"); |
36 |
} |
37 |
|
38 |
vhost = find_virthost( hostname, 0); |
39 |
if (vhost == NULL) { |
40 |
fprintf(stderr, "Tried to add Access for host %s, which does not exist.\n", hostname); |
41 |
exit(1); |
42 |
} |
43 |
|
44 |
vhost->n_access++; |
45 |
vhost->access_nodes = realloc(vhost->access_nodes, |
46 |
vhost->n_access*sizeof(struct access_node)); |
47 |
|
48 |
if (vhost->access_nodes == NULL) { |
49 |
DIE("out of memory"); |
50 |
} |
51 |
|
52 |
vhost->access_nodes[vhost->n_access - 1].type = type; |
53 |
vhost->access_nodes[vhost->n_access - 1].pattern = strdup(pattern); |
54 |
|
55 |
} /* access_add */ |
56 |
|
57 |
|
58 |
int access_allow(const char* hostname, const char *file) |
59 |
{ |
60 |
int i; |
61 |
virthost* vhost; |
62 |
|
63 |
vhost = find_virthost( hostname, 0); |
64 |
if (vhost == NULL) { |
65 |
return ACCESS_ALLOW; |
66 |
} |
67 |
|
68 |
/* find first match in allow / deny rules */ |
69 |
for (i=0; i<vhost->n_access; i++) { |
70 |
if (fnmatch(vhost->access_nodes[i].pattern, file, 0) == 0) { |
71 |
return vhost->access_nodes[i].type; |
72 |
} |
73 |
} |
74 |
/* default to allow */ |
75 |
return ACCESS_ALLOW; |
76 |
|
77 |
} /* access_allow */ |
78 |
|
79 |
#endif |