]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nis_ismember.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nis / nis_ismember.c
CommitLineData
04277e02 1/* Copyright (c) 1997-2019 Free Software Foundation, Inc.
51702635 2 This file is part of the GNU C Library.
32abdb71 3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
51702635
UD
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
51702635
UD
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
41bdb6e2 13 Lesser General Public License for more details.
51702635 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
51702635
UD
18
19#include <string.h>
20#include <rpcsvc/nis.h>
82f43dd2 21#include <shlib-compat.h>
51702635
UD
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 */
27static int
28internal_ismember (const_nis_name principal, const_nis_name group)
29{
a53bad16
UD
30 size_t grouplen = strlen (group);
31 char buf[grouplen + 50];
32 char leafbuf[grouplen + 2];
33 char domainbuf[grouplen + 2];
dfd2257a
UD
34 nis_result *res;
35 char *cp, *cp2;
36 u_int i;
51702635 37
dfd2257a
UD
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);
a53bad16 41 if (cp2 != NULL && cp2[0] != '\0')
dfd2257a
UD
42 {
43 *cp++ = '.';
44 strcpy (cp, cp2);
45 }
32abdb71 46
dfd2257a 47 res = nis_lookup (buf, EXPAND_NAME|FOLLOW_LINKS);
32abdb71
UD
48 if (res == NULL || NIS_RES_STATUS (res) != NIS_SUCCESS)
49 {
7960f2a7 50 nis_freeresult (res);
32abdb71
UD
51 return 0;
52 }
51702635 53
34a5a146
JM
54 if ((NIS_RES_NUMOBJ (res) != 1)
55 || (__type_of (NIS_RES_OBJECT (res)) != NIS_GROUP_OBJ))
32abdb71
UD
56 {
57 nis_freeresult (res);
58 return 0;
59 }
51702635 60
dfd2257a
UD
61 /* We search twice in the list, at first, if we have the name
62 with a "-", then if without. "-member" has priority */
63 for (i = 0; i < NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_len; ++i)
64 {
65 cp = NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val[i];
66 if (cp[0] == '-')
51702635 67 {
dfd2257a 68 if (strcmp (&cp[1], principal) == 0)
32abdb71
UD
69 {
70 nis_freeresult (res);
71 return -1;
72 }
dfd2257a
UD
73 if (cp[1] == '@')
74 switch (internal_ismember (principal, &cp[2]))
75 {
76 case -1:
32abdb71 77 nis_freeresult (res);
51702635 78 return -1;
dfd2257a 79 case 1:
32abdb71
UD
80 nis_freeresult (res);
81 return 1;
dfd2257a
UD
82 default:
83 break;
84 }
85 else
86 if (cp[1] == '*')
87 {
88 char buf1[strlen (principal) + 2];
89 char buf2[strlen (cp) + 2];
51702635 90
32abdb71
UD
91 if (strcmp (nis_domain_of_r (principal, buf1, sizeof buf1),
92 nis_domain_of_r (cp, buf2, sizeof buf2)) == 0)
93 {
94 nis_freeresult (res);
95 return -1;
96 }
dfd2257a 97 }
51702635 98 }
dfd2257a 99 }
32abdb71 100
dfd2257a
UD
101 for (i = 0; i < NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_len; ++i)
102 {
103 cp = NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val[i];
104 if (cp[0] != '-')
51702635 105 {
dfd2257a 106 if (strcmp (cp, principal) == 0)
32abdb71
UD
107 {
108 nis_freeresult (res);
109 return 1;
110 }
dfd2257a
UD
111 if (cp[0] == '@')
112 switch (internal_ismember (principal, &cp[1]))
113 {
114 case -1:
32abdb71 115 nis_freeresult (res);
dfd2257a
UD
116 return -1;
117 case 1:
32abdb71 118 nis_freeresult (res);
51702635 119 return 1;
dfd2257a
UD
120 default:
121 break;
122 }
123 else
124 if (cp[0] == '*')
125 {
126 char buf1[strlen (principal) + 2];
127 char buf2[strlen (cp) + 2];
51702635 128
dfd2257a
UD
129 if (strcmp (nis_domain_of_r (principal, buf1, sizeof buf1),
130 nis_domain_of_r (cp, buf2, sizeof buf2)) == 0)
32abdb71
UD
131 {
132 nis_freeresult (res);
133 return 1;
134 }
dfd2257a 135 }
51702635
UD
136 }
137 }
32abdb71 138 nis_freeresult (res);
51702635
UD
139 return 0;
140}
141
142bool_t
143nis_ismember (const_nis_name principal, const_nis_name group)
144{
a53bad16 145 if (group != NULL && group[0] != '\0' && principal != NULL)
714a562f 146 return internal_ismember (principal, group) == 1 ? TRUE : FALSE;
51702635
UD
147 else
148 return FALSE;
149}
1e4d83f6 150libnsl_hidden_nolink_def (nis_ismember, GLIBC_2_1)