]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (c) 1997-2015 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 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
51702635 UD |
18 | |
19 | #include <string.h> | |
20 | #include <rpcsvc/nis.h> | |
51702635 UD |
21 | |
22 | /* internal_nis_ismember () | |
23 | return codes: -1 principal is in -group | |
24 | 0 principal isn't in any group | |
25 | 1 pirncipal is in group */ | |
26 | static int | |
27 | internal_ismember (const_nis_name principal, const_nis_name group) | |
28 | { | |
a53bad16 UD |
29 | size_t grouplen = strlen (group); |
30 | char buf[grouplen + 50]; | |
31 | char leafbuf[grouplen + 2]; | |
32 | char domainbuf[grouplen + 2]; | |
dfd2257a UD |
33 | nis_result *res; |
34 | char *cp, *cp2; | |
35 | u_int i; | |
51702635 | 36 | |
dfd2257a UD |
37 | cp = stpcpy (buf, nis_leaf_of_r (group, leafbuf, sizeof (leafbuf) - 1)); |
38 | cp = stpcpy (cp, ".groups_dir"); | |
39 | cp2 = nis_domain_of_r (group, domainbuf, sizeof (domainbuf) - 1); | |
a53bad16 | 40 | if (cp2 != NULL && cp2[0] != '\0') |
dfd2257a UD |
41 | { |
42 | *cp++ = '.'; | |
43 | strcpy (cp, cp2); | |
44 | } | |
32abdb71 | 45 | |
dfd2257a | 46 | res = nis_lookup (buf, EXPAND_NAME|FOLLOW_LINKS); |
32abdb71 UD |
47 | if (res == NULL || NIS_RES_STATUS (res) != NIS_SUCCESS) |
48 | { | |
7960f2a7 | 49 | nis_freeresult (res); |
32abdb71 UD |
50 | return 0; |
51 | } | |
51702635 | 52 | |
dfd2257a UD |
53 | if ((NIS_RES_NUMOBJ (res) != 1) || |
54 | (__type_of (NIS_RES_OBJECT (res)) != NIS_GROUP_OBJ)) | |
32abdb71 UD |
55 | { |
56 | nis_freeresult (res); | |
57 | return 0; | |
58 | } | |
51702635 | 59 | |
dfd2257a UD |
60 | /* We search twice in the list, at first, if we have the name |
61 | with a "-", then if without. "-member" has priority */ | |
62 | for (i = 0; i < NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_len; ++i) | |
63 | { | |
64 | cp = NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val[i]; | |
65 | if (cp[0] == '-') | |
51702635 | 66 | { |
dfd2257a | 67 | if (strcmp (&cp[1], principal) == 0) |
32abdb71 UD |
68 | { |
69 | nis_freeresult (res); | |
70 | return -1; | |
71 | } | |
dfd2257a UD |
72 | if (cp[1] == '@') |
73 | switch (internal_ismember (principal, &cp[2])) | |
74 | { | |
75 | case -1: | |
32abdb71 | 76 | nis_freeresult (res); |
51702635 | 77 | return -1; |
dfd2257a | 78 | case 1: |
32abdb71 UD |
79 | nis_freeresult (res); |
80 | return 1; | |
dfd2257a UD |
81 | default: |
82 | break; | |
83 | } | |
84 | else | |
85 | if (cp[1] == '*') | |
86 | { | |
87 | char buf1[strlen (principal) + 2]; | |
88 | char buf2[strlen (cp) + 2]; | |
51702635 | 89 | |
32abdb71 UD |
90 | if (strcmp (nis_domain_of_r (principal, buf1, sizeof buf1), |
91 | nis_domain_of_r (cp, buf2, sizeof buf2)) == 0) | |
92 | { | |
93 | nis_freeresult (res); | |
94 | return -1; | |
95 | } | |
dfd2257a | 96 | } |
51702635 | 97 | } |
dfd2257a | 98 | } |
32abdb71 | 99 | |
dfd2257a UD |
100 | for (i = 0; i < NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_len; ++i) |
101 | { | |
102 | cp = NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val[i]; | |
103 | if (cp[0] != '-') | |
51702635 | 104 | { |
dfd2257a | 105 | if (strcmp (cp, principal) == 0) |
32abdb71 UD |
106 | { |
107 | nis_freeresult (res); | |
108 | return 1; | |
109 | } | |
dfd2257a UD |
110 | if (cp[0] == '@') |
111 | switch (internal_ismember (principal, &cp[1])) | |
112 | { | |
113 | case -1: | |
32abdb71 | 114 | nis_freeresult (res); |
dfd2257a UD |
115 | return -1; |
116 | case 1: | |
32abdb71 | 117 | nis_freeresult (res); |
51702635 | 118 | return 1; |
dfd2257a UD |
119 | default: |
120 | break; | |
121 | } | |
122 | else | |
123 | if (cp[0] == '*') | |
124 | { | |
125 | char buf1[strlen (principal) + 2]; | |
126 | char buf2[strlen (cp) + 2]; | |
51702635 | 127 | |
dfd2257a UD |
128 | if (strcmp (nis_domain_of_r (principal, buf1, sizeof buf1), |
129 | nis_domain_of_r (cp, buf2, sizeof buf2)) == 0) | |
32abdb71 UD |
130 | { |
131 | nis_freeresult (res); | |
132 | return 1; | |
133 | } | |
dfd2257a | 134 | } |
51702635 UD |
135 | } |
136 | } | |
32abdb71 | 137 | nis_freeresult (res); |
51702635 UD |
138 | return 0; |
139 | } | |
140 | ||
141 | bool_t | |
142 | nis_ismember (const_nis_name principal, const_nis_name group) | |
143 | { | |
a53bad16 | 144 | if (group != NULL && group[0] != '\0' && principal != NULL) |
714a562f | 145 | return internal_ismember (principal, group) == 1 ? TRUE : FALSE; |
51702635 UD |
146 | else |
147 | return FALSE; | |
148 | } |