Update.
[glibc.git] / nis / nis_ismember.c
blobf0d087ca51b49043a5fa34b436c809b11f3eb7ee
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.
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.
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.
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. */
20 #include <string.h>
21 #include <rpcsvc/nis.h>
22 #include <rpcsvc/nislib.h>
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)
31 if (group != NULL && strlen (group) > 0)
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;
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)
45 cp = stpcpy (cp, ".");
46 strcpy (cp, cp2);
48 res = nis_lookup (buf, EXPAND_NAME|FOLLOW_LINKS);
49 if (res->status != NIS_SUCCESS && res->status != NIS_S_SUCCESS)
50 return 0;
52 if ((res->objects.objects_len != 1) ||
53 (res->objects.objects_val[0].zo_data.zo_type != GROUP_OBJ))
54 return 0;
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)
62 cp =res->objects.objects_val[0].GR_data.gr_members.gr_members_val[i];
63 if (cp[0] == '-')
65 if (strcmp (&cp[1], principal) == 0)
66 return -1;
67 if (cp[1] == '@')
68 switch (internal_ismember (principal, &cp[2]))
70 case -1:
71 return -1;
72 case 1:
73 return -1;
74 default:
75 break;
77 else
78 if (cp[1] == '*')
80 char buf1[strlen (principal) + 2];
81 char buf2[strlen (cp) + 2];
83 strcpy (buf1, nis_domain_of (principal));
84 strcpy (buf2, nis_domain_of (cp));
85 if (strcmp (buf1, buf2) == 0)
86 return -1;
90 for (i = 0;
91 i < res->objects.objects_val[0].GR_data.gr_members.gr_members_len;
92 ++i)
94 cp =res->objects.objects_val[0].GR_data.gr_members.gr_members_val[i];
95 if (cp[0] != '-')
97 if (strcmp (cp, principal) == 0)
98 return 1;
99 if (cp[0] == '@')
100 switch (internal_ismember (principal, &cp[1]))
102 case -1:
103 return -1;
104 case 1:
105 return 1;
106 default:
107 break;
109 else
110 if (cp[0] == '*')
112 char buf1[strlen (principal) + 2];
113 char buf2[strlen (cp) + 2];
115 strcpy (buf1, nis_domain_of (principal));
116 strcpy (buf2, nis_domain_of (cp));
117 if (strcmp (buf1, buf2) == 0)
118 return 1;
124 return 0;
127 bool_t
128 nis_ismember (const_nis_name principal, const_nis_name group)
130 if (group != NULL && strlen (group) > 0)
132 int status;
134 status = internal_ismember (principal, group);
135 if (status == 1)
136 return TRUE;
137 else
138 return FALSE;
140 else
141 return FALSE;