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
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
)
37 unsigned long a
, t
, mask
= (unsigned long)~0;
39 if (!addr
|| !*addr
) return 0;
41 if (!isdigit(tok
[0])) return 0;
53 if (t
== INADDR_NONE
) {
54 rprintf(FERROR
,"malformed address %s\n", tok
);
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
);
71 if (bits
== 0) return 1;
72 if (bits
<= 0 || bits
> 32) {
73 rprintf(FERROR
,"malformed mask in %s\n", tok
);
76 mask
&= (mask
<< (32-bits
));
80 return ((a
&mask
) == (t
&mask
));
83 static int access_match(char *list
, char *addr
, char *host
)
86 char *list2
= strdup(list
);
88 if (!list2
) out_of_memory("access_match");
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
)) {
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
))
110 /* if there is an allow list but no deny list then allow only hosts
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
122 if (access_match(allow_list
,addr
,host
))
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
))