Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

access.c

Go to the documentation of this file.
00001 /* 
00002    Copyright (C) Andrew Tridgell 1998
00003    
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 2 of the License, or
00007    (at your option) any later version.
00008    
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013    
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the Free Software
00016    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 /*
00020   hosts allow/deny code for rsync
00021 
00022   */
00023 
00024 #include "rsync.h"
00025 
00026 
00027 static int match_hostname(char *host, char *tok)
00028 {
00029         if (!host || !*host) return 0;
00030         return (fnmatch(tok, host, 0) == 0);
00031 }
00032 
00033 
00034 static int match_address(char *addr, char *tok)
00035 {
00036         char *p;
00037         unsigned long a, t, mask = (unsigned long)~0;
00038 
00039         if (!addr || !*addr) return 0;
00040 
00041         if (!isdigit(* (unsigned char *) tok)) return 0;
00042 
00043         p = strchr(tok,'/');
00044         if (p) *p = 0;
00045 
00046         a = inet_addr(addr);
00047         t = inet_addr(tok);
00048 
00049         if (p) {
00050                 *p = '/';
00051         }
00052 
00053         if (t == INADDR_NONE) {
00054                 rprintf(FERROR,"malformed address %s\n", tok);
00055                 return 0;
00056         }
00057 
00058         a = ntohl(a);
00059         t = ntohl(t);
00060 
00061         if (p) {
00062                 if (strchr(p+1,'.')) {
00063                         mask = inet_addr(p+1);
00064                         if (mask == INADDR_NONE) {
00065                                 rprintf(FERROR,"malformed mask in %s\n", tok);
00066                                 return 0;
00067                         }
00068                         mask = ntohl(mask);
00069                 } else {
00070                         int bits = atoi(p+1);
00071                         if (bits == 0) return 1;
00072                         if (bits <= 0 || bits > 32) {
00073                                 rprintf(FERROR,"malformed mask in %s\n", tok);
00074                                 return 0;
00075                         }
00076                         mask &= (mask << (32-bits));
00077                 }
00078         }
00079 
00080         return ((a&mask) == (t&mask));
00081 }
00082 
00083 static int access_match(char *list, char *addr, char *host)
00084 {
00085         char *tok;
00086         char *list2 = strdup(list);
00087 
00088         if (!list2) out_of_memory("access_match");
00089 
00090         strlower(list2);
00091         if (host) strlower(host);
00092 
00093         for (tok=strtok(list2," ,\t"); tok; tok=strtok(NULL," ,\t")) {
00094                 if (match_hostname(host, tok) || match_address(addr, tok)) {
00095                         free(list2);
00096                         return 1;
00097                 }
00098         }
00099 
00100         free(list2);
00101         return 0;
00102 }
00103 
00104 int allow_access(char *addr, char *host, char *allow_list, char *deny_list)
00105 {
00106         /* if theres no deny list and no allow list then allow access */
00107         if ((!deny_list || !*deny_list) && (!allow_list || !*allow_list))
00108                 return 1;
00109 
00110         /* if there is an allow list but no deny list then allow only hosts
00111            on the allow list */
00112         if (!deny_list || !*deny_list)
00113                 return(access_match(allow_list, addr, host));
00114 
00115         /* if theres a deny list but no allow list then allow
00116            all hosts not on the deny list */
00117         if (!allow_list || !*allow_list)
00118                 return(!access_match(deny_list,addr,host));
00119 
00120         /* if there are both type of list then allow all hosts on the
00121            allow list */
00122         if (access_match(allow_list,addr,host))
00123                 return 1;
00124 
00125         /* if there are both type of list and it's not on the allow then
00126            allow it if its not on the deny */
00127         if (access_match(deny_list,addr,host))
00128                 return 0;
00129 
00130         return 1;
00131 }

Generated on Tue Apr 16 12:37:36 2002 for rsync by doxygen1.2.15