home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / netgroup / getnetgrent.c next >
Encoding:
C/C++ Source or Header  |  1994-08-25  |  7.0 KB  |  283 lines

  1. /*
  2.  * Copyright (c) 1994 Swen Thuemmler <swen@uni-paderborn.de>
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 1. Redistributions of source code must retain the above copyright
  8.  *    notice, this list of conditions and the following disclaimer.
  9.  * 2. Redistributions in binary form must reproduce the above copyright
  10.  *    notice, this list of conditions and the following disclaimer in the
  11.  *    documentation and/or other materials provided with the distribution.
  12.  * 3. The name of the author may not be used to endorse or promote
  13.  *    products derived from this software without specific prior written
  14.  *    permission.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  17.  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  20.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26.  * SUCH DAMAGE.
  27.  */
  28.  
  29.  
  30. #include <ansidecl.h>
  31. #include <stddef.h>
  32. #include <ctype.h>
  33.  
  34. #ifdef YP
  35. #include <rpcsvc/yp_prot.h>
  36. #include <rpcsvc/ypclnt.h>
  37.  
  38. extern void setnetgrent(const char *);
  39. extern void endnetgrent(void);
  40. extern int getnetgrent(char **, char **, char **);
  41. extern int innetgr(const char *, const char *, const char *, const char *);
  42.  
  43. struct netgrentry 
  44. {
  45.   char *host;
  46.   char *user;
  47.   char *domain;
  48. };
  49.  
  50.  
  51. struct netgrlist 
  52. {
  53.   int maxmembers;
  54.   int members;
  55.   struct netgrentry *list;
  56. };
  57.  
  58.  
  59. static void _expand_netgroupentry(const char *, struct netgrlist *);
  60. static void _parse_entry(char *, char *, struct netgrlist *);
  61. static void _netgr_free(struct netgrlist *);
  62. static struct netgrlist list = { 0, 0, NULL };
  63. static int first = 1;
  64. static char *netgroup = NULL ;
  65. static char *nisdomain = NULL ;
  66.  
  67. void
  68. setnetgrent(const char *netgr)
  69. {
  70.   if (NULL == netgroup || 0 != strcmp(netgroup, netgr))
  71.     {
  72.       endnetgrent();
  73.       netgroup = strdup(netgr);
  74.       _expand_netgroupentry(netgr, &list);
  75.     }
  76.   first = 1;
  77. }
  78.  
  79. void
  80. endnetgrent(void)
  81. {
  82.   if (NULL != netgroup)
  83.     {
  84.       free(netgroup);
  85.       netgroup = NULL;
  86.     }
  87.   
  88.   if (NULL != list.list)
  89.       _netgr_free(&list);
  90.   first = 1;
  91. }
  92.  
  93. int
  94. getnetgrent(char **machinep, char **userp, char **domainp)
  95. {
  96.   static int current = 0;
  97.   struct netgrentry *entry;
  98.     
  99.   if (1 == first)
  100.     current = first = 0;
  101.   else
  102.     current++;
  103.  
  104.   if (current < list.members)
  105.     {
  106.       entry = &list.list[current];
  107.       *machinep = entry->host;
  108.       *userp = entry->user;
  109.       *domainp = entry->domain;
  110.       return 1;
  111.     }
  112.   return 0;
  113. }
  114.  
  115. int
  116. innetgr(const char *netgr, const char *host, const char *user, const char *domain)
  117. {
  118.   struct netgrlist list = { 0, 0, NULL };
  119.   struct netgrentry *entry;
  120.   int current, status = 0;
  121.   
  122.   _expand_netgroupentry(netgr, &list);
  123.   for (current = 0; current < list.members; current++)
  124.     {
  125.       entry = &list.list[current];
  126.           /* This may look weired, but it is correct */
  127.       status =
  128.         ((NULL == host) ? 1 : ((NULL == entry->host) ? 1 : 0 == strcmp(host, entry->host)))
  129.         && ((NULL == user) ? 1 : ((NULL == entry->user) ? 1 : 0 == strcmp(user, entry->user)))
  130.         && ((NULL == domain) ? 1 : ((NULL == entry->domain) ? 1 : 0 == strcmp(domain, entry->domain)));
  131.       if (1 == status)
  132.         break;
  133.     }
  134.   if (NULL != list.list)
  135.     _netgr_free(&list);
  136.   return status;
  137. }
  138.  
  139. static void
  140. _netgr_free(struct netgrlist *list)
  141. {
  142.   int i;
  143.   for (i = 0; i < list->members; i++)
  144.   {
  145.     free(list->list[i].host);
  146.     free(list->list[i].user);
  147.     free(list->list[i].domain);
  148.   }
  149.   free(list->list);
  150.   list->maxmembers = 0;
  151.   list->members = 0;
  152.   list->list = NULL;
  153. }
  154.  
  155. static void
  156. _expand_netgroupentry(const char *netgr, struct netgrlist *list)
  157. {
  158.   char *outval;
  159.   int outvallen, status;
  160.   char *start, *end, *realend;
  161.     
  162.   if ('\0' == *netgr)
  163.     return;
  164.   if (1 == __yp_check(NULL))
  165.     {
  166.       if (NULL == nisdomain)
  167.         yp_get_default_domain(&nisdomain);
  168.       status = yp_match(nisdomain, "netgroup",
  169.                         netgr, strlen(netgr),
  170.                         &outval, &outvallen);
  171.       if (0 != status)
  172.         return ;
  173.           /* outval enthaelt den Eintrag. Zuerst Leerzeichen ueberlesen */
  174.       start = outval;
  175.       realend = start + strlen(outval);
  176.       while (isspace(*start) && start < realend)
  177.         start++;
  178.       while (start < realend)
  179.         {
  180.           if ( '(' == *start ) /* Eintrag gefunden */
  181.             {
  182.               end = strchr(start, ')') ;
  183.               if (NULL == end)
  184.                 return ;
  185.               _parse_entry(start + 1, end, list);
  186.             }
  187.           else
  188.             {
  189.               end = start + 1;
  190.               while ('\0' != *end && !isspace(*end))
  191.                 end++;
  192.               *end = '\0';
  193.               _expand_netgroupentry(start, list);
  194.             }
  195.           start = end + 1 ;
  196.           while (isspace(*start) && start < realend)
  197.             start++;
  198.         }
  199.       free(outval);
  200.     }
  201. }
  202.  
  203. static void
  204. _parse_entry(char *start, char *end, struct netgrlist *list)
  205. {
  206.   char *host, *user, *domain;
  207.   struct netgrentry *entry;
  208.       /* First split entry into fields. Return, when finding malformed entry */
  209.   host = start ;
  210.   start = strchr(host, ',') ;
  211.   if (NULL == start || start >= end)
  212.     return ;
  213.   *start = '\0' ;
  214.   user = start + 1 ;
  215.   start = strchr(user, ',') ;
  216.   if (NULL == start || start >= end)
  217.     return ;
  218.   *start = '\0';
  219.   domain = start + 1;
  220.   if (start > end)
  221.     return ;
  222.   *end = '\0' ;
  223.       /* Entry is correctly formed, put it into the list */
  224.   if (0 == list->maxmembers)
  225.     {
  226.       list->list = malloc(10 * sizeof(struct netgrentry));
  227.       if ( NULL != list->list )
  228.         list->maxmembers = 10 ;
  229.     }
  230.  
  231.   if (list->members == list->maxmembers)
  232.     {
  233.       list->list = realloc(list->list,
  234.                            (list->maxmembers + 10) * sizeof(struct netgrentry));
  235.       if (NULL == list->list)
  236.         {
  237.           list->maxmembers = 0;
  238.           list->members = 0;
  239.           return;
  240.         }
  241.       list->maxmembers += 10;
  242.     }
  243.       /*
  244.        * FIXME: this will not handle entries of the form ( asdf, sdfa , asdf )
  245.        * (note the spaces). This should be handled better!
  246.        */
  247.   entry = &list->list[list->members];
  248.   entry->user = ('\0' == *user) ? NULL : strdup(user);
  249.   entry->host = ('\0' == *host) ? NULL : strdup(host);
  250.   entry->domain = ('\0' == *domain) ? NULL : strdup(domain);
  251.   list->members++ ;
  252.   return;
  253. }
  254.  
  255. #else
  256.  
  257. int
  258. getnetgrent(char **machinep, char **userp, char **domainp)
  259. {
  260.   return 0;
  261. }
  262.  
  263. void
  264. setnetgrent(char *netgroup)
  265. {
  266.   return ;
  267. }
  268.  
  269.  
  270. void
  271. endnetgrent(void)
  272. {
  273.   return ;
  274. }
  275.  
  276. int
  277. innetgr(char *netgroup, char *machine, char *user, char *domain)
  278. {
  279.   return 0;
  280. }
  281.  
  282. #endif
  283.