Back to index

glibc  2.9
nis_ismember.c
Go to the documentation of this file.
00001 /* Copyright (c) 1997, 1998, 1999, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <string.h>
00021 #include <rpcsvc/nis.h>
00022 
00023 /* internal_nis_ismember ()
00024    return codes: -1 principal is in -group
00025                   0 principal isn't in any group
00026                 1 pirncipal is in group */
00027 static int
00028 internal_ismember (const_nis_name principal, const_nis_name group)
00029 {
00030   size_t grouplen = strlen (group);
00031   char buf[grouplen + 50];
00032   char leafbuf[grouplen + 2];
00033   char domainbuf[grouplen + 2];
00034   nis_result *res;
00035   char *cp, *cp2;
00036   u_int i;
00037 
00038   cp = stpcpy (buf, nis_leaf_of_r (group, leafbuf, sizeof (leafbuf) - 1));
00039   cp = stpcpy (cp, ".groups_dir");
00040   cp2 = nis_domain_of_r (group, domainbuf, sizeof (domainbuf) - 1);
00041   if (cp2 != NULL && cp2[0] != '\0')
00042     {
00043       *cp++ = '.';
00044       strcpy (cp, cp2);
00045     }
00046 
00047   res = nis_lookup (buf, EXPAND_NAME|FOLLOW_LINKS);
00048   if (res == NULL || NIS_RES_STATUS (res) != NIS_SUCCESS)
00049     {
00050       nis_freeresult (res);
00051       return 0;
00052     }
00053 
00054   if ((NIS_RES_NUMOBJ (res) != 1) ||
00055       (__type_of (NIS_RES_OBJECT (res)) != NIS_GROUP_OBJ))
00056     {
00057       nis_freeresult (res);
00058       return 0;
00059     }
00060 
00061   /* We search twice in the list, at first, if we have the name
00062      with a "-", then if without. "-member" has priority */
00063   for (i = 0; i < NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_len; ++i)
00064     {
00065       cp = NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val[i];
00066       if (cp[0] == '-')
00067        {
00068          if (strcmp (&cp[1], principal) == 0)
00069            {
00070              nis_freeresult (res);
00071              return -1;
00072            }
00073          if (cp[1] == '@')
00074            switch (internal_ismember (principal, &cp[2]))
00075              {
00076              case -1:
00077               nis_freeresult (res);
00078               return -1;
00079              case 1:
00080               nis_freeresult (res);
00081               return 1;
00082              default:
00083               break;
00084              }
00085          else
00086            if (cp[1] == '*')
00087              {
00088               char buf1[strlen (principal) + 2];
00089               char buf2[strlen (cp) + 2];
00090 
00091               if (strcmp (nis_domain_of_r (principal, buf1, sizeof buf1),
00092                          nis_domain_of_r (cp, buf2, sizeof buf2)) == 0)
00093                 {
00094                   nis_freeresult (res);
00095                   return -1;
00096                 }
00097              }
00098        }
00099     }
00100 
00101   for (i = 0; i < NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_len; ++i)
00102     {
00103       cp = NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val[i];
00104       if (cp[0] != '-')
00105        {
00106          if (strcmp (cp, principal) == 0)
00107            {
00108              nis_freeresult (res);
00109              return 1;
00110            }
00111          if (cp[0] == '@')
00112            switch (internal_ismember (principal, &cp[1]))
00113              {
00114              case -1:
00115               nis_freeresult (res);
00116               return -1;
00117              case 1:
00118               nis_freeresult (res);
00119               return 1;
00120              default:
00121               break;
00122              }
00123          else
00124            if (cp[0] == '*')
00125              {
00126               char buf1[strlen (principal) + 2];
00127               char buf2[strlen (cp) + 2];
00128 
00129               if (strcmp (nis_domain_of_r (principal, buf1, sizeof buf1),
00130                          nis_domain_of_r (cp, buf2, sizeof buf2)) == 0)
00131                 {
00132                   nis_freeresult (res);
00133                   return 1;
00134                 }
00135              }
00136        }
00137     }
00138   nis_freeresult (res);
00139   return 0;
00140 }
00141 
00142 bool_t
00143 nis_ismember (const_nis_name principal, const_nis_name group)
00144 {
00145   if (group != NULL && group[0] != '\0' && principal != NULL)
00146     return internal_ismember (principal, group) == 1 ? TRUE : FALSE;
00147   else
00148     return FALSE;
00149 }