Add a colon if a non-empty pre-xfer exec message follows.
[rsync/rsync.git] / access.c
index b05059f..367fd44 100644 (file)
--- a/access.c
+++ b/access.c
@@ -1,49 +1,82 @@
-/* 
-   Copyright (C) Andrew Tridgell 1998
-   
-   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 2 of the License, 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.
-*/
-
 /*
-  hosts allow/deny code for rsync
-
-  */
+ * Routines to authenticate access to a daemon (hosts allow/deny).
+ *
+ * Copyright (C) 1998 Andrew Tridgell
+ * Copyright (C) 2004-2009 Wayne Davison
+ *
+ * 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 3 of the License, 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, visit the http://fsf.org website.
+ */
 
 #include "rsync.h"
 
+static int allow_forward_dns;
+
+extern const char undetermined_hostname[];
 
-static int match_hostname(char *host, char *tok)
+static int match_hostname(const char **host_ptr, const char *addr, const char *tok)
 {
-       if (!host || !*host) return 0;
-       return (fnmatch(tok, host, 0) == 0);
+       struct hostent *hp;
+       unsigned int i;
+       const char *host = *host_ptr;
+
+       if (!host || !*host)
+               return 0;
+
+       /* First check if the reverse-DNS-determined hostname matches. */
+       if (iwildmatch(tok, host))
+               return 1;
+
+       if (!allow_forward_dns)
+               return 0;
+
+       /* Fail quietly if tok is an address or wildcarded entry, not a simple hostname. */
+       if (!tok[strspn(tok, ".0123456789")] || tok[strcspn(tok, ":/*?[")])
+               return 0;
+
+       /* Now try forward-DNS on the token (config-specified hostname) and see if the IP matches. */
+       if (!(hp = gethostbyname(tok)))
+               return 0;
+
+       for (i = 0; hp->h_addr_list[i] != NULL; i++) {
+               if (strcmp(addr, inet_ntoa(*(struct in_addr*)(hp->h_addr_list[i]))) == 0) {
+                       /* If reverse lookups are off, we'll use the conf-specified
+                        * hostname in preference to UNDETERMINED. */
+                       if (host == undetermined_hostname) {
+                               if (!(*host_ptr = strdup(tok)))
+                                       *host_ptr = undetermined_hostname;
+                       }
+                       return 1;
+               }
+       }
+
+       return 0;
 }
 
-static int match_binary(char *b1, char *b2, char *mask, int addrlen)
+static int match_binary(const char *b1, const char *b2, const char *mask, int addrlen)
 {
        int i;
 
-       for (i=0; i<addrlen; i++) {
-               if ((b1[i]^b2[i])&mask[i]) {
+       for (i = 0; i < addrlen; i++) {
+               if ((b1[i] ^ b2[i]) & mask[i])
                        return 0;
-               }
        }
 
        return 1;
 }
 
-static void make_mask(char *mask, int plen, int addrlen) {
+static void make_mask(char *mask, int plen, int addrlen)
+{
        int w, b;
 
        w = plen >> 3;
@@ -59,7 +92,7 @@ static void make_mask(char *mask, int plen, int addrlen) {
        return;
 }
 
-static int match_address(char *addr, char *tok)
+static int match_address(const char *addr, const char *tok)
 {
        char *p;
        struct addrinfo hints, *resa, *rest;
@@ -74,10 +107,19 @@ static int match_address(char *addr, char *tok)
        char mask[16];
        char *a = NULL, *t = NULL;
 
-       if (!addr || !*addr) return 0;
+       if (!addr || !*addr)
+               return 0;
 
        p = strchr(tok,'/');
-       if (p) *p = 0;
+       if (p)
+               *p = '\0';
+
+       /* Fail quietly if tok is a hostname, not an address. */
+       if (tok[strspn(tok, ".0123456789")] && strchr(tok, ':') == NULL) {
+               if (p)
+                       *p = '/';
+               return 0;
+       }
 
        memset(&hints, 0, sizeof(hints));
        hints.ai_family = PF_UNSPEC;
@@ -86,14 +128,18 @@ static int match_address(char *addr, char *tok)
        hints.ai_flags = AI_NUMERICHOST;
 #endif
 
-       gai = getaddrinfo(addr, NULL, &hints, &resa);
-       if (gai) return 0;
+       if (getaddrinfo(addr, NULL, &hints, &resa) != 0) {
+               if (p)
+                       *p = '/';
+               return 0;
+       }
 
        gai = getaddrinfo(tok, NULL, &hints, &rest);
        if (p)
                *p++ = '/';
-       if (gai) {
-               rprintf(FERROR,"malformed address %s\n", tok);
+       if (gai != 0) {
+               rprintf(FLOG, "error matching address %s: %s\n",
+                       tok, gai_strerror(gai));
                freeaddrinfo(resa);
                return 0;
        }
@@ -136,7 +182,7 @@ static int match_address(char *addr, char *tok)
            }
 #endif
        default:
-           rprintf(FERROR,"unknown family %u\n", rest->ai_family);
+           rprintf(FLOG, "unknown family %u\n", rest->ai_family);
            ret = 0;
            goto out;
        }
@@ -153,14 +199,14 @@ static int match_address(char *addr, char *tok)
 #ifdef HAVE_STRTOL
                        bits = strtol(p, &ep, 10);
                        if (!*p || *ep) {
-                               rprintf(FERROR,"malformed mask in %s\n", tok);
+                               rprintf(FLOG, "malformed mask in %s\n", tok);
                                ret = 0;
                                goto out;
                        }
 #else
                        for (pp = (unsigned char *)p; *pp; pp++) {
                                if (!isascii(*pp) || !isdigit(*pp)) {
-                                       rprintf(FERROR,"malformed mask in %s\n", tok);
+                                       rprintf(FLOG, "malformed mask in %s\n", tok);
                                        ret = 0;
                                        goto out;
                                }
@@ -172,7 +218,7 @@ static int match_address(char *addr, char *tok)
                                goto out;
                        }
                        if (bits < 0 || bits > (addrlen << 3)) {
-                               rprintf(FERROR,"malformed mask in %s\n", tok);
+                               rprintf(FLOG, "malformed mask in %s\n", tok);
                                ret = 0;
                                goto out;
                        }
@@ -186,24 +232,24 @@ static int match_address(char *addr, char *tok)
 
        ret = match_binary(a, t, mask, addrlen);
 
-out:
+  out:
        freeaddrinfo(resa);
        freeaddrinfo(rest);
        return ret;
 }
 
-static int access_match(char *list, char *addr, char *host)
+static int access_match(const char *list, const char *addr, const char **host_ptr)
 {
        char *tok;
        char *list2 = strdup(list);
 
-       if (!list2) out_of_memory("access_match");
+       if (!list2)
+               out_of_memory("access_match");
 
        strlower(list2);
-       if (host) strlower(host);
 
-       for (tok=strtok(list2," ,\t"); tok; tok=strtok(NULL," ,\t")) {
-               if (match_hostname(host, tok) || match_address(addr, tok)) {
+       for (tok = strtok(list2, " ,\t"); tok; tok = strtok(NULL, " ,\t")) {
+               if (match_hostname(host_ptr, addr, tok) || match_address(addr, tok)) {
                        free(list2);
                        return 1;
                }
@@ -213,31 +259,32 @@ static int access_match(char *list, char *addr, char *host)
        return 0;
 }
 
-int allow_access(char *addr, char *host, char *allow_list, char *deny_list)
+int allow_access(const char *addr, const char **host_ptr, int i)
 {
-       /* if theres no deny list and no allow list then allow access */
-       if ((!deny_list || !*deny_list) && (!allow_list || !*allow_list))
-               return 1;
+       const char *allow_list = lp_hosts_allow(i);
+       const char *deny_list = lp_hosts_deny(i);
 
-       /* if there is an allow list but no deny list then allow only hosts
-          on the allow list */
-       if (!deny_list || !*deny_list)
-               return(access_match(allow_list, addr, host));
+       if (allow_list && !*allow_list)
+               allow_list = NULL;
+       if (deny_list && !*deny_list)
+               deny_list = NULL;
 
-       /* if theres a deny list but no allow list then allow
-          all hosts not on the deny list */
-       if (!allow_list || !*allow_list)
-               return(!access_match(deny_list,addr,host));
+       allow_forward_dns = lp_forward_lookup(i);
 
-       /* if there are both type of list then allow all hosts on the
-           allow list */
-       if (access_match(allow_list,addr,host))
-               return 1;
+       /* If we match an allow-list item, we always allow access. */
+       if (allow_list) {
+               if (access_match(allow_list, addr, host_ptr))
+                       return 1;
+               /* For an allow-list w/o a deny-list, disallow non-matches. */
+               if (!deny_list)
+                       return 0;
+       }
 
-       /* if there are both type of list and it's not on the allow then
-          allow it if its not on the deny */
-       if (access_match(deny_list,addr,host))
+       /* If we match a deny-list item (and got past any allow-list
+        * items), we always disallow access. */
+       if (deny_list && access_match(deny_list, addr, host_ptr))
                return 0;
 
+       /* Allow all other access. */
        return 1;
 }