home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / samba-1.9.18p7.tar.gz / samba-1.9.18p7.tar / samba-1.9.18p7 / source / access.c < prev    next >
C/C++ Source or Header  |  1998-05-04  |  8KB  |  290 lines

  1. /* 
  2. This module is an adaption of code from the tcpd-1.4 package written
  3. by Wietse Venema, Eindhoven University of Technology, The Netherlands.
  4.  
  5. The code is used here with permission.
  6.  
  7. The code has been considerably changed from the original. Bug reports
  8. should be sent to samba-bugs@samba.anu.edu.au
  9. */
  10.  
  11. #include "includes.h"
  12.  
  13. #define ALLOW_PURE_ADDRESSES
  14.  
  15. extern int DEBUGLEVEL;
  16.  
  17. #ifndef    INADDR_NONE
  18. #define    INADDR_NONE    ((uint32)~0)
  19. #endif
  20.  
  21.  
  22. #define Good True
  23. #define Bad False
  24.  
  25. #define CLIENT_MATCH client_match
  26.  
  27. /* Delimiters for lists of daemons or clients. */
  28.  
  29. static char sep[] = ", \t";
  30.  
  31. /* Constants to be used in assignments only, not in comparisons... */
  32.  
  33. #define    YES        1
  34. #define    NO        0
  35. #define    FAIL        (-1)
  36.  
  37. /* Forward declarations. */
  38. static int list_match(char *list,char *item, int (*match_fn)(char *, char *));
  39. static int client_match(char *tok,char *item);
  40. static int string_match(char *tok,char *s);
  41. static int masked_match(char *tok, char *slash, char *s);
  42.  
  43. /* Size of logical line buffer. */
  44. #define    BUFLEN 2048
  45.  
  46. /* return true if access should be allowed to a service*/
  47. BOOL check_access(int snum)
  48. {
  49.   char *denyl,*allowl;
  50.   BOOL ret = False;
  51.  
  52.   denyl = lp_hostsdeny(snum);
  53.   if (denyl) denyl = strdup(denyl);
  54.  
  55.   allowl = lp_hostsallow(snum);
  56.   if (allowl) allowl = strdup(allowl);
  57.  
  58.   if ((!denyl || *denyl==0) && (!allowl || *allowl==0))
  59.     ret = True;
  60.  
  61.   if (!ret)
  62.     {
  63.       if (allow_access(denyl,allowl,client_name(),client_addr()))
  64.     {
  65.       if (snum >= 0)
  66.         DEBUG(2,("Allowed connection from %s (%s) to %s\n",
  67.              client_name(),client_addr(),
  68.              lp_servicename(snum)));
  69.       ret = True;
  70.     }
  71.       else
  72.     if (snum >= 0)
  73.       DEBUG(0,("%s Denied connection from %s (%s) to %s\n",
  74.            timestring(), client_name(),client_addr(),
  75.            lp_servicename(snum)));
  76.     }
  77.  
  78.   if (denyl) free(denyl);
  79.   if (allowl) free(allowl);
  80.   return(ret);
  81. }
  82.  
  83.  
  84. /* return true if access should be allowed */
  85. BOOL allow_access(char *deny_list,char *allow_list,char *cname,char *caddr)
  86. {
  87.   char *client[2];
  88.  
  89.   client[0] = cname;
  90.   client[1] = caddr;  
  91.  
  92.   /* if theres no deny list and no allow list then allow access */
  93.   if ((!deny_list || *deny_list == 0) && (!allow_list || *allow_list == 0))
  94.     return(True);  
  95.  
  96.   /* if there is an allow list but no deny list then allow only hosts
  97.      on the allow list */
  98.   if (!deny_list || *deny_list == 0)
  99.     return(list_match(allow_list,(char *)client,CLIENT_MATCH));
  100.  
  101.   /* if theres a deny list but no allow list then allow
  102.      all hosts not on the deny list */
  103.   if (!allow_list || *allow_list == 0)
  104.     return(!list_match(deny_list,(char *)client,CLIENT_MATCH));
  105.  
  106.   /* if there are both type of list then allow all hosts on the allow list */
  107.   if (list_match(allow_list,(char *)client,CLIENT_MATCH))
  108.     return (True);
  109.  
  110.   /* if there are both type of list and it's not on the allow then
  111.      allow it if its not on the deny */
  112.   if (list_match(deny_list,(char *)client,CLIENT_MATCH))
  113.     return (False);
  114.  
  115.   return (True);
  116. }
  117.  
  118. /* list_match - match an item against a list of tokens with exceptions */
  119. /* (All modifications are marked with the initials "jkf") */
  120. static int list_match(char *list,char *item, int (*match_fn)(char *,char *))
  121. {
  122.     char   *tok;
  123.     char   *listcopy;        /* jkf */
  124.     int     match = NO;
  125.  
  126.     /*
  127.      * jkf@soton.ac.uk -- 31 August 1994 -- Stop list_match()
  128.      * overwriting the list given as its first parameter.
  129.      */
  130.  
  131.     /* jkf -- can get called recursively with NULL list */
  132.     listcopy = (list == 0) ? (char *)0 : strdup(list);
  133.  
  134.     /*
  135.      * Process tokens one at a time. We have exhausted all possible matches
  136.      * when we reach an "EXCEPT" token or the end of the list. If we do find
  137.      * a match, look for an "EXCEPT" list and recurse to determine whether
  138.      * the match is affected by any exceptions.
  139.      */
  140.  
  141.     for (tok = strtok(listcopy, sep); tok ; tok = strtok(NULL, sep)) {
  142.     if (strcasecmp(tok, "EXCEPT") == 0)    /* EXCEPT: give up */
  143.         break;
  144.     if ((match = (*match_fn) (tok, item)))    /* YES or FAIL */
  145.         break;
  146.     }
  147.     /* Process exceptions to YES or FAIL matches. */
  148.  
  149.     if (match != NO) {
  150.     while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
  151.          /* VOID */ ;
  152.     if (tok == 0 || list_match((char *) 0, item, match_fn) == NO) {
  153.         if (listcopy != 0) free(listcopy); /* jkf */
  154.         return (match);
  155.     }
  156.     }
  157.  
  158.     if (listcopy != 0) free(listcopy); /* jkf */
  159.     return (NO);
  160. }
  161.  
  162.  
  163. /* client_match - match host name and address against token */
  164. static int client_match(char *tok,char *item)
  165. {
  166.     char **client = (char **)item;
  167.     int     match;
  168.  
  169.     /*
  170.      * Try to match the address first. If that fails, try to match the host
  171.      * name if available.
  172.      */
  173.  
  174.     if ((match = string_match(tok, client[1])) == 0)
  175.     if (client[0][0] != 0)
  176.         match = string_match(tok, client[0]);
  177.     return (match);
  178. }
  179.  
  180. /* string_match - match string against token */
  181. static int string_match(char *tok,char *s)
  182. {
  183.     int     tok_len;
  184.     int     str_len;
  185.     char   *cut;
  186.  
  187.     /*
  188.      * Return YES if a token has the magic value "ALL". Return FAIL if the
  189.      * token is "FAIL". If the token starts with a "." (domain name), return
  190.      * YES if it matches the last fields of the string. If the token has the
  191.      * magic value "LOCAL", return YES if the string does not contain a "."
  192.      * character. If the token ends on a "." (network number), return YES if
  193.      * it matches the first fields of the string. If the token begins with a
  194.      * "@" (netgroup name), return YES if the string is a (host) member of
  195.      * the netgroup. Return YES if the token fully matches the string. If the
  196.      * token is a netnumber/netmask pair, return YES if the address is a
  197.      * member of the specified subnet.
  198.      */
  199.  
  200.     if (tok[0] == '.') {            /* domain: match last fields */
  201.     if ((str_len = strlen(s)) > (tok_len = strlen(tok))
  202.         && strcasecmp(tok, s + str_len - tok_len) == 0)
  203.         return (YES);
  204.     } else if (tok[0] == '@') {            /* netgroup: look it up */
  205. #ifdef    NETGROUP
  206.       static char *mydomain = NULL;
  207.       char *hostname = NULL;
  208.       BOOL netgroup_ok = False;
  209.  
  210.       if (!mydomain) yp_get_default_domain(&mydomain);
  211.  
  212.       if (!mydomain) {
  213.         DEBUG(0,("Unable to get default yp domain.\n"));
  214.         return NO;
  215.       }
  216.       if (!(hostname = strdup(s))) {
  217.     DEBUG(1,("out of memory for strdup!\n"));
  218.     return NO;
  219.       }
  220.  
  221.       netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
  222.  
  223.       DEBUG(5,("looking for %s of domain %s in netgroup %s gave %s\n", 
  224.            hostname,
  225.            mydomain, 
  226.            tok+1,
  227.            BOOLSTR(netgroup_ok)));
  228.  
  229. #ifdef NETGROUP_INSECURE
  230.       /* if you really want netgroups that match non qualified names
  231.      then define NETGROUP_INSECURE. It can, however, be a big
  232.      security hole */
  233.       {
  234.     char        *clnt_domain;
  235.     if (!netgroup_ok && (clnt_domain=strchr(hostname,'.'))) {
  236.       *clnt_domain++ = '\0';
  237.       netgroup_ok = innetgr(tok + 1, hostname, (char *) 0, mydomain);
  238.     }
  239.       }
  240. #endif
  241.  
  242.       free(hostname);
  243.       
  244.       if (netgroup_ok) return(YES);
  245. #else
  246.       DEBUG(0,("access: netgroup support is not configured\n"));
  247.       return (NO);
  248. #endif
  249.     } else if (strcasecmp(tok, "ALL") == 0) {    /* all: match any */
  250.     return (YES);
  251.     } else if (strcasecmp(tok, "FAIL") == 0) {    /* fail: match any */
  252.     return (FAIL);
  253.     } else if (strcasecmp(tok, "LOCAL") == 0) {    /* local: no dots */
  254.     if (strchr(s, '.') == 0 && strcasecmp(s, "unknown") != 0)
  255.         return (YES);
  256.     } else if (!strcasecmp(tok, s)) {    /* match host name or address */
  257.     return (YES);
  258.     } else if (tok[(tok_len = strlen(tok)) - 1] == '.') {    /* network */
  259.     if (strncmp(tok, s, tok_len) == 0)
  260.         return (YES);
  261.     } else if ((cut = strchr(tok, '/')) != 0) {    /* netnumber/netmask */
  262.     if (isdigit(s[0]) && masked_match(tok, cut, s))
  263.         return (YES);
  264.     }
  265.     return (NO);
  266. }
  267.  
  268. /* masked_match - match address against netnumber/netmask */
  269. static int masked_match(char *tok, char *slash, char *s)
  270. {
  271.   uint32 net;
  272.   uint32 mask;
  273.   uint32 addr;
  274.  
  275.   if ((addr = interpret_addr(s)) == INADDR_NONE)
  276.     return (NO);
  277.   *slash = 0;
  278.   net = interpret_addr(tok);
  279.   *slash = '/';
  280.   if (net == INADDR_NONE || (mask = interpret_addr(slash + 1)) == INADDR_NONE) {
  281.     DEBUG(0,("access: bad net/mask access control: %s", tok));
  282.     return (NO);
  283.   }
  284.   return ((addr & mask) == net);
  285. }
  286.  
  287.  
  288.  
  289.  
  290.