Update to LGPL v2.1.
[jlayton/glibc.git] / nis / nis_ismember.c
1 /* Copyright (c) 1997, 1998, 1999 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <string.h>
21 #include <rpcsvc/nis.h>
22
23 /* internal_nis_ismember ()
24    return codes: -1 principal is in -group
25                   0 principal isn't in any group
26                   1 pirncipal is in group */
27 static int
28 internal_ismember (const_nis_name principal, const_nis_name group)
29 {
30   size_t grouplen = strlen (group);
31   char buf[grouplen + 50];
32   char leafbuf[grouplen + 2];
33   char domainbuf[grouplen + 2];
34   nis_result *res;
35   char *cp, *cp2;
36   u_int i;
37
38   cp = stpcpy (buf, nis_leaf_of_r (group, leafbuf, sizeof (leafbuf) - 1));
39   cp = stpcpy (cp, ".groups_dir");
40   cp2 = nis_domain_of_r (group, domainbuf, sizeof (domainbuf) - 1);
41   if (cp2 != NULL && cp2[0] != '\0')
42     {
43       *cp++ = '.';
44       strcpy (cp, cp2);
45     }
46
47   res = nis_lookup (buf, EXPAND_NAME|FOLLOW_LINKS);
48   if (res == NULL || NIS_RES_STATUS (res) != NIS_SUCCESS)
49     {
50       if (res)
51         nis_freeresult (res);
52       return 0;
53     }
54
55   if ((NIS_RES_NUMOBJ (res) != 1) ||
56       (__type_of (NIS_RES_OBJECT (res)) != NIS_GROUP_OBJ))
57     {
58       nis_freeresult (res);
59       return 0;
60     }
61
62   /* We search twice in the list, at first, if we have the name
63      with a "-", then if without. "-member" has priority */
64   for (i = 0; i < NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_len; ++i)
65     {
66       cp = NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val[i];
67       if (cp[0] == '-')
68         {
69           if (strcmp (&cp[1], principal) == 0)
70             {
71               nis_freeresult (res);
72               return -1;
73             }
74           if (cp[1] == '@')
75             switch (internal_ismember (principal, &cp[2]))
76               {
77               case -1:
78                 nis_freeresult (res);
79                 return -1;
80               case 1:
81                 nis_freeresult (res);
82                 return 1;
83               default:
84                 break;
85               }
86           else
87             if (cp[1] == '*')
88               {
89                 char buf1[strlen (principal) + 2];
90                 char buf2[strlen (cp) + 2];
91
92                 if (strcmp (nis_domain_of_r (principal, buf1, sizeof buf1),
93                             nis_domain_of_r (cp, buf2, sizeof buf2)) == 0)
94                   {
95                     nis_freeresult (res);
96                     return -1;
97                   }
98               }
99         }
100     }
101
102   for (i = 0; i < NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_len; ++i)
103     {
104       cp = NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val[i];
105       if (cp[0] != '-')
106         {
107           if (strcmp (cp, principal) == 0)
108             {
109               nis_freeresult (res);
110               return 1;
111             }
112           if (cp[0] == '@')
113             switch (internal_ismember (principal, &cp[1]))
114               {
115               case -1:
116                 nis_freeresult (res);
117                 return -1;
118               case 1:
119                 nis_freeresult (res);
120                 return 1;
121               default:
122                 break;
123               }
124           else
125             if (cp[0] == '*')
126               {
127                 char buf1[strlen (principal) + 2];
128                 char buf2[strlen (cp) + 2];
129
130                 if (strcmp (nis_domain_of_r (principal, buf1, sizeof buf1),
131                             nis_domain_of_r (cp, buf2, sizeof buf2)) == 0)
132                   {
133                     nis_freeresult (res);
134                     return 1;
135                   }
136               }
137         }
138     }
139   nis_freeresult (res);
140   return 0;
141 }
142
143 bool_t
144 nis_ismember (const_nis_name principal, const_nis_name group)
145 {
146   if (group != NULL && group[0] != '\0' && principal != NULL)
147     return internal_ismember (principal, group) == 1 ? TRUE : FALSE;
148   else
149     return FALSE;
150 }