Update.
[platform/upstream/glibc.git] / nis / nis_ismember.c
1 /* Copyright (c) 1997 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    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    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA. */
19
20 #include <string.h>
21 #include <rpcsvc/nis.h>
22 #include <rpcsvc/nislib.h>
23
24 /* internal_nis_ismember ()
25    return codes: -1 principal is in -group
26                   0 principal isn't in any group
27                   1 pirncipal is in group */
28 static int
29 internal_ismember (const_nis_name principal, const_nis_name group)
30 {
31   if (group != NULL && strlen (group) > 0)
32     {
33       char buf[strlen (group) + 50];
34       char leafbuf[strlen (group) + 2];
35       char domainbuf[strlen (group) + 2];
36       nis_result *res;
37       char *cp, *cp2;
38       u_int i;
39
40       cp = stpcpy (buf, nis_leaf_of_r (group, leafbuf, sizeof (leafbuf) - 1));
41       cp = stpcpy (cp, ".groups_dir");
42       cp2 = nis_domain_of_r (group, domainbuf, sizeof (domainbuf) - 1);
43       if (cp2 != NULL && strlen (cp2) > 0)
44         {
45           cp = stpcpy (cp, ".");
46           strcpy (cp, cp2);
47         }
48       res = nis_lookup (buf, EXPAND_NAME|FOLLOW_LINKS);
49       if (res->status != NIS_SUCCESS && res->status != NIS_S_SUCCESS)
50         return 0;
51
52       if ((res->objects.objects_len != 1) ||
53           (res->objects.objects_val[0].zo_data.zo_type != GROUP_OBJ))
54         return 0;
55
56       /* We search twice in the list, at first, if we have the name
57          with a "-", then if without. "-member" has priority */
58       for (i = 0;
59            i < res->objects.objects_val[0].GR_data.gr_members.gr_members_len;
60            ++i)
61         {
62           cp =res->objects.objects_val[0].GR_data.gr_members.gr_members_val[i];
63           if (cp[0] == '-')
64             {
65               if (strcmp (&cp[1], principal) == 0)
66                 return -1;
67               if (cp[1] == '@')
68                 switch (internal_ismember (principal, &cp[2]))
69                   {
70                   case -1:
71                     return -1;
72                   case 1:
73                     return -1;
74                   default:
75                     break;
76                   }
77               else
78                 if (cp[1] == '*')
79                   {
80                     char buf1[strlen (principal) + 2];
81                     char buf2[strlen (cp) + 2];
82
83                     strcpy (buf1, nis_domain_of (principal));
84                     strcpy (buf2, nis_domain_of (cp));
85                     if (strcmp (buf1, buf2) == 0)
86                       return -1;
87                   }
88             }
89         }
90       for (i = 0;
91            i < res->objects.objects_val[0].GR_data.gr_members.gr_members_len;
92            ++i)
93         {
94           cp =res->objects.objects_val[0].GR_data.gr_members.gr_members_val[i];
95           if (cp[0] != '-')
96             {
97               if (strcmp (cp, principal) == 0)
98                 return 1;
99               if (cp[0] == '@')
100                 switch (internal_ismember (principal, &cp[1]))
101                   {
102                   case -1:
103                     return -1;
104                   case 1:
105                     return 1;
106                   default:
107                     break;
108                   }
109               else
110                 if (cp[0] == '*')
111                   {
112                     char buf1[strlen (principal) + 2];
113                     char buf2[strlen (cp) + 2];
114
115                     strcpy (buf1, nis_domain_of (principal));
116                     strcpy (buf2, nis_domain_of (cp));
117                     if (strcmp (buf1, buf2) == 0)
118                       return 1;
119                   }
120             }
121         }
122     }
123
124   return 0;
125 }
126
127 bool_t
128 nis_ismember (const_nis_name principal, const_nis_name group)
129 {
130   if (group != NULL && strlen (group) > 0)
131     {
132       int status;
133
134       status = internal_ismember (principal, group);
135       if (status == 1)
136         return TRUE;
137       else
138         return FALSE;
139     }
140   else
141     return FALSE;
142 }