Update.
[glibc.git] / nis / nis_ismember.c
blob4a33f02cffc184c98f68cb300e59b0f63c539914
1 /* Copyright (c) 1997, 1998 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>
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)
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;
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')
43 *cp++ = '.';
44 strcpy (cp, cp2);
46 res = nis_lookup (buf, EXPAND_NAME|FOLLOW_LINKS);
47 if (NIS_RES_STATUS (res) != NIS_SUCCESS)
48 return 0;
50 if ((NIS_RES_NUMOBJ (res) != 1) ||
51 (__type_of (NIS_RES_OBJECT (res)) != NIS_GROUP_OBJ))
52 return 0;
54 /* We search twice in the list, at first, if we have the name
55 with a "-", then if without. "-member" has priority */
56 for (i = 0; i < NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_len; ++i)
58 cp = NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val[i];
59 if (cp[0] == '-')
61 if (strcmp (&cp[1], principal) == 0)
62 return -1;
63 if (cp[1] == '@')
64 switch (internal_ismember (principal, &cp[2]))
66 case -1:
67 return -1;
68 case 1:
69 return -1;
70 default:
71 break;
73 else
74 if (cp[1] == '*')
76 char buf1[strlen (principal) + 2];
77 char buf2[strlen (cp) + 2];
79 strcpy (buf1, nis_domain_of (principal));
80 strcpy (buf2, nis_domain_of (cp));
81 if (strcmp (buf1, buf2) == 0)
82 return -1;
86 for (i = 0; i < NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_len; ++i)
88 cp = NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val[i];
89 if (cp[0] != '-')
91 if (strcmp (cp, principal) == 0)
92 return 1;
93 if (cp[0] == '@')
94 switch (internal_ismember (principal, &cp[1]))
96 case -1:
97 return -1;
98 case 1:
99 return 1;
100 default:
101 break;
103 else
104 if (cp[0] == '*')
106 char buf1[strlen (principal) + 2];
107 char buf2[strlen (cp) + 2];
109 if (strcmp (nis_domain_of_r (principal, buf1, sizeof buf1),
110 nis_domain_of_r (cp, buf2, sizeof buf2)) == 0)
111 return 1;
115 return 0;
118 bool_t
119 nis_ismember (const_nis_name principal, const_nis_name group)
121 if (group != NULL && group[0] != '\0' && principal != NULL)
122 return internal_ismember (principal, group) == 1 ? TRUE : FALSE;
123 else
124 return FALSE;