Typo. Shouldn't depend on assert().
[rsync.git] / access.c
blob050e8cc4f5b68905262527faad249aa4e70e359f
1 /*
2 Copyright (C) Andrew Tridgell 1998
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 hosts allow/deny code for rsync
24 #include "rsync.h"
27 static int match_hostname(char *host, char *tok)
29 if (!host || !*host) return 0;
30 return (fnmatch(tok, host, 0) == 0);
34 static int match_address(char *addr, char *tok)
36 char *p;
37 unsigned long a, t, mask = (unsigned long)~0;
39 if (!addr || !*addr) return 0;
41 if (!isdigit(tok[0])) return 0;
43 p = strchr(tok,'/');
44 if (p) *p = 0;
46 a = inet_addr(addr);
47 t = inet_addr(tok);
49 if (p) {
50 *p = '/';
53 if (t == INADDR_NONE) {
54 rprintf(FERROR,"malformed address %s\n", tok);
55 return 0;
58 a = ntohl(a);
59 t = ntohl(t);
61 if (p) {
62 if (strchr(p+1,'.')) {
63 mask = inet_addr(p+1);
64 if (mask == INADDR_NONE) {
65 rprintf(FERROR,"malformed mask in %s\n", tok);
66 return 0;
68 mask = ntohl(mask);
69 } else {
70 int bits = atoi(p+1);
71 if (bits == 0) return 1;
72 if (bits <= 0 || bits > 32) {
73 rprintf(FERROR,"malformed mask in %s\n", tok);
74 return 0;
76 mask &= (mask << (32-bits));
80 return ((a&mask) == (t&mask));
83 static int access_match(char *list, char *addr, char *host)
85 char *tok;
86 char *list2 = strdup(list);
88 if (!list2) out_of_memory("access_match");
90 strlower(list2);
91 if (host) strlower(host);
93 for (tok=strtok(list2," ,\t"); tok; tok=strtok(NULL," ,\t")) {
94 if (match_hostname(host, tok) || match_address(addr, tok)) {
95 free(list2);
96 return 1;
100 free(list2);
101 return 0;
104 int allow_access(char *addr, char *host, char *allow_list, char *deny_list)
106 /* if theres no deny list and no allow list then allow access */
107 if ((!deny_list || !*deny_list) && (!allow_list || !*allow_list))
108 return 1;
110 /* if there is an allow list but no deny list then allow only hosts
111 on the allow list */
112 if (!deny_list || !*deny_list)
113 return(access_match(allow_list, addr, host));
115 /* if theres a deny list but no allow list then allow
116 all hosts not on the deny list */
117 if (!allow_list || !*allow_list)
118 return(!access_match(deny_list,addr,host));
120 /* if there are both type of list then allow all hosts on the
121 allow list */
122 if (access_match(allow_list,addr,host))
123 return 1;
125 /* if there are both type of list and it's not on the allow then
126 allow it if its not on the deny */
127 if (access_match(deny_list,addr,host))
128 return 0;
130 return 1;