home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / contrib / samba / samba-1.8 / samba-1 / samba-1.8.05 / access.c next >
Encoding:
C/C++ Source or Header  |  1994-10-27  |  10.2 KB  |  350 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 Andrew.Tridgell@anu.edu.au
  9. */
  10.  
  11. #include "includes.h"
  12. #include "loadparm.h"
  13.  
  14. extern int DEBUGLEVEL;
  15.  
  16. #ifndef    INADDR_NONE
  17. #define    INADDR_NONE    ((unsigned long)~0)
  18. #endif
  19.  
  20.  
  21. #define FROM_ADDRLEN  (4*3+3+1)
  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. BOOL allow_access(char *deny_list,char *allow_list,struct from_host *client);
  39. static int list_match(char *list,char *item, int (*match_fn)());
  40. static int client_match(char *tok,char *item);
  41. static int string_match(char *tok,char *string);
  42. static int masked_match(char *tok, char *slash, char *string);
  43. static int matchname(char *remotehost,struct in_addr  addr);
  44. BOOL fromhost(int sock,struct from_host *f);
  45.  
  46.  
  47. /* Size of logical line buffer. */
  48. #define    BUFLEN 2048
  49.  
  50.  
  51. /* return true if access should be allowed to a service*/
  52. BOOL check_access(int snum)
  53. {
  54.   extern int Client;
  55.   extern struct from_host Client_info;
  56.   char *denyl,*allowl;
  57.   BOOL ret = False;
  58.  
  59.   denyl = lp_hostsdeny(snum);
  60.   if (denyl) denyl = strdup(denyl);
  61.  
  62.   allowl = lp_hostsallow(snum);
  63.   if (allowl) allowl = strdup(allowl);
  64.  
  65.  
  66.   fromhost(Client,&Client_info);
  67.  
  68.   if ((!denyl || *denyl==0) && (!allowl || *allowl==0))
  69.     ret = True;
  70.  
  71.   if (!ret)
  72.     {
  73.       if (!fromhost(Client,&Client_info))
  74.     DEBUG(0,("ERROR: Can't get from_host info\n"));
  75.       else
  76.     {
  77.       if (allow_access(denyl,allowl,&Client_info))
  78.         {
  79.           DEBUG(2,("Allowed connection from %s (%s) to %s\n",
  80.             Client_info.name,Client_info.addr,lp_servicename(snum)));
  81.           ret = True;
  82.         }
  83.       else
  84.         DEBUG(0,("Denied connection from %s (%s) to %s\n",
  85.           Client_info.name,Client_info.addr,lp_servicename(snum)));
  86.     }
  87.     }
  88.  
  89.   if (denyl) free(denyl);
  90.   if (allowl) free(allowl);
  91.   return(ret);
  92. }
  93.  
  94.  
  95. /* return true if access should be allowed */
  96. BOOL allow_access(char *deny_list,char *allow_list,struct from_host *client)
  97. {
  98.   /* if theres no deny list and no allow list then allow access */
  99.   if ((!deny_list || *deny_list == 0) && (!allow_list || *allow_list == 0))
  100.     return(True);  
  101.  
  102.   /* if there is an allow list but no deny list then allow only hosts
  103.      on the allow list */
  104.   if (!deny_list || *deny_list == 0)
  105.     return(list_match(allow_list,(char *)client,CLIENT_MATCH));
  106.  
  107.   /* if theres a deny list but no allow list then allow
  108.      all hosts not on the deny list */
  109.   if (!allow_list || *allow_list == 0)
  110.     return(!list_match(deny_list,(char *)client,CLIENT_MATCH));
  111.  
  112.   /* if there are both type of list then allow all hosts on the allow list */
  113.   if (list_match(allow_list,(char *)client,CLIENT_MATCH))
  114.     return (True);
  115.  
  116.   /* if there are both type of list and it's not on the allow then
  117.      allow it if its not on the deny */
  118.   if (list_match(deny_list,(char *)client,CLIENT_MATCH))
  119.     return (False);
  120.  
  121.   return (True);
  122. }
  123.  
  124. /* list_match - match an item against a list of tokens with exceptions */
  125. /* (All modifications are marked with the initials "jkf") */
  126. static int list_match(char *list,char *item, int (*match_fn)())
  127. {
  128.     char   *tok;
  129.     char   *listcopy;        /* jkf */
  130.     int     match = NO;
  131.  
  132.     /*
  133.      * jkf@soton.ac.uk -- 31 August 1994 -- Stop list_match()
  134.      * overwriting the list given as its first parameter.
  135.      */
  136.  
  137.     /* jkf -- can get called recursively with NULL list */
  138.     listcopy = (list == 0) ? (char *)0 : strdup(list);
  139.  
  140.     /*
  141.      * Process tokens one at a time. We have exhausted all possible matches
  142.      * when we reach an "EXCEPT" token or the end of the list. If we do find
  143.      * a match, look for an "EXCEPT" list and recurse to determine whether
  144.      * the match is affected by any exceptions.
  145.      */
  146.  
  147.     for (tok = strtok(listcopy, sep); tok ; tok = strtok(NULL, sep)) {
  148.     if (strcasecmp(tok, "EXCEPT") == 0)    /* EXCEPT: give up */
  149.         break;
  150.     if ((match = (*match_fn) (tok, item)))    /* YES or FAIL */
  151.         break;
  152.     }
  153.     /* Process exceptions to YES or FAIL matches. */
  154.  
  155.     if (match != NO) {
  156.     while ((tok = strtok((char *) 0, sep)) && strcasecmp(tok, "EXCEPT"))
  157.          /* VOID */ ;
  158.     if (tok == 0 || list_match((char *) 0, item, match_fn) == NO) {
  159.         if (listcopy != 0) free(listcopy); /* jkf */
  160.         return (match);
  161.     }
  162.     }
  163.  
  164.     if (listcopy != 0) free(listcopy); /* jkf */
  165.     return (NO);
  166. }
  167.  
  168.  
  169. /* client_match - match host name and address against token */
  170. static int client_match(char *tok,char *item)
  171. {
  172.     struct from_host *client = (struct from_host *) item;
  173.     int     match;
  174.  
  175.     /*
  176.      * Try to match the address first. If that fails, try to match the host
  177.      * name if available.
  178.      */
  179.  
  180.     if ((match = string_match(tok, client->addr)) == 0)
  181.     if (client->name[0] != 0)
  182.         match = string_match(tok, client->name);
  183.     return (match);
  184. }
  185.  
  186. /* string_match - match string against token */
  187. static int string_match(char *tok,char *string)
  188. {
  189.     int     tok_len;
  190.     int     str_len;
  191.     char   *cut;
  192. #ifdef    NETGROUP
  193.     static char *mydomain = 0;
  194. #endif
  195.  
  196.     /*
  197.      * Return YES if a token has the magic value "ALL". Return FAIL if the
  198.      * token is "FAIL". If the token starts with a "." (domain name), return
  199.      * YES if it matches the last fields of the string. If the token has the
  200.      * magic value "LOCAL", return YES if the string does not contain a "."
  201.      * character. If the token ends on a "." (network number), return YES if
  202.      * it matches the first fields of the string. If the token begins with a
  203.      * "@" (netgroup name), return YES if the string is a (host) member of
  204.      * the netgroup. Return YES if the token fully matches the string. If the
  205.      * token is a netnumber/netmask pair, return YES if the address is a
  206.      * member of the specified subnet.
  207.      */
  208.  
  209.     if (tok[0] == '.') {            /* domain: match last fields */
  210.     if ((str_len = strlen(string)) > (tok_len = strlen(tok))
  211.         && strcasecmp(tok, string + str_len - tok_len) == 0)
  212.         return (YES);
  213.     } else if (tok[0] == '@') {            /* netgroup: look it up */
  214. #ifdef    NETGROUP
  215.     if (mydomain == 0)
  216.         yp_get_default_domain(&mydomain);
  217.     if (!isdigit(string[0])
  218.         && innetgr(tok + 1, string, (char *) 0, mydomain))
  219.         return (YES);
  220. #else
  221.     DEBUG(0,("access: netgroup support is not configured"));
  222.     return (NO);
  223. #endif
  224.     } else if (strcasecmp(tok, "ALL") == 0) {    /* all: match any */
  225.     return (YES);
  226.     } else if (strcasecmp(tok, "FAIL") == 0) {    /* fail: match any */
  227.     return (FAIL);
  228.     } else if (strcasecmp(tok, "LOCAL") == 0) {    /* local: no dots */
  229.     if (strchr(string, '.') == 0 && strcasecmp(string, "unknown") != 0)
  230.         return (YES);
  231.     } else if (!strcasecmp(tok, string)) {    /* match host name or address */
  232.     return (YES);
  233.     } else if (tok[(tok_len = strlen(tok)) - 1] == '.') {    /* network */
  234.     if (strncmp(tok, string, tok_len) == 0)
  235.         return (YES);
  236.     } else if ((cut = strchr(tok, '/')) != 0) {    /* netnumber/netmask */
  237.     if (isdigit(string[0]) && masked_match(tok, cut, string))
  238.         return (YES);
  239.     }
  240.     return (NO);
  241. }
  242.  
  243. /* masked_match - match address against netnumber/netmask */
  244. static int masked_match(char *tok, char *slash, char *string)
  245. {
  246.     unsigned long net;
  247.     unsigned long mask;
  248.     unsigned long addr;
  249.  
  250.     if ((addr = interpret_addr(string)) == INADDR_NONE)
  251.     return (NO);
  252.     *slash = 0;
  253.     net = interpret_addr(tok);
  254.     *slash = '/';
  255.     if (net == INADDR_NONE || (mask = interpret_addr(slash + 1)) == INADDR_NONE) {
  256.     DEBUG(0,("access: bad net/mask access control: %s", tok));
  257.     return (NO);
  258.     }
  259.     return ((addr & mask) == net);
  260. }
  261.  
  262.  
  263. /* fromhost - find out what is at the other end of a socket */
  264. BOOL fromhost(int sock,struct from_host *f)
  265. {
  266.     static struct sockaddr sa;
  267.     struct sockaddr_in *sin = (struct sockaddr_in *) (&sa);
  268.     struct hostent *hp;
  269.     int     length = sizeof(sa);
  270.     static char addr_buf[FROM_ADDRLEN];
  271.     static char name_buf[MAXHOSTNAMELEN];
  272.  
  273.     if (getpeername(sock, &sa, &length) < 0) 
  274.       {
  275.     DEBUG(0,("getpeername failed\n"));
  276.     return(False);
  277.       }
  278.  
  279.     f->sin = sin;
  280.     f->addr = strcpy(addr_buf,(char *)inet_ntoa(sin->sin_addr));
  281.  
  282.     /* Look up the remote host name. */
  283.     if ((hp = gethostbyaddr((char *) &sin->sin_addr,
  284.                 sizeof(sin->sin_addr),
  285.                 AF_INET)) == 0) {
  286.       DEBUG(0,("Gethostbyaddr failed\n"));
  287.       return(False);
  288.     }
  289.  
  290.     /* Save the host name. A later gethostbyxxx() call may clobber it. */
  291.     f->name = StrnCpy(name_buf, (char *)hp->h_name, sizeof(name_buf) - 1);
  292.  
  293.     /*
  294.      * Verify that the host name does not belong to someone else. If host
  295.      * name verification fails, pretend that the host name lookup failed.
  296.      */
  297.     if (!matchname(f->name, sin->sin_addr))
  298.       {
  299.     DEBUG(0,("Matchname failed\n"));
  300.     return(False);
  301.       }
  302.  
  303.     return(True);    
  304. }
  305.  
  306. /* matchname - determine if host name matches IP address */
  307. static int matchname(char *remotehost,struct in_addr  addr)
  308. {
  309.   struct hostent *hp;
  310.   int     i;
  311.   
  312.   if ((hp = Get_Hostbyname(remotehost)) == 0) {
  313.     DEBUG(0,("Get_Hostbyname(%s): lookup failure", remotehost));
  314.     return (Bad);
  315.   } 
  316.  
  317.     /*
  318.      * Make sure that gethostbyname() returns the "correct" host name.
  319.      * Unfortunately, gethostbyname("localhost") sometimes yields
  320.      * "localhost.domain". Since the latter host name comes from the
  321.      * local DNS, we just have to trust it (all bets are off if the local
  322.      * DNS is perverted). We always check the address list, though.
  323.      */
  324.   
  325.   if (strcasecmp(remotehost, hp->h_name)
  326.       && strcasecmp(remotehost, "localhost")) {
  327.     DEBUG(0,("host name/name mismatch: %s != %s",
  328.       remotehost, hp->h_name));
  329.     return (Bad);
  330.   }
  331.     
  332.   /* Look up the host address in the address list we just got. */
  333.   for (i = 0; hp->h_addr_list[i]; i++) {
  334.     if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
  335.       return (Good);
  336.   }
  337.  
  338.   /*
  339.    * The host name does not map to the original host address. Perhaps
  340.    * someone has compromised a name server. More likely someone botched
  341.    * it, but that could be dangerous, too.
  342.    */
  343.   
  344.   DEBUG(0,("host name/address mismatch: %s != %s",
  345.     inet_ntoa(addr), hp->h_name));
  346.   return (Bad);
  347. }
  348.  
  349.  
  350.