]>
Commit | Line | Data |
---|---|---|
688903eb | 1 | /* Copyright (C) 1997-2018 Free Software Foundation, Inc. |
68361572 UD |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU Lesser General Public | |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Lesser General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
68361572 UD |
17 | |
18 | #include <atomic.h> | |
19 | #include <nss.h> | |
20 | #include <grp.h> | |
21 | #include <ctype.h> | |
22 | #include <errno.h> | |
23 | #include <string.h> | |
ec999b8e | 24 | #include <libc-lock.h> |
68361572 UD |
25 | #include <rpcsvc/nis.h> |
26 | ||
27 | #include "nss-nisplus.h" | |
28 | #include "nisplus-parser.h" | |
29 | #include <libnsl.h> | |
30 | #include <nis_intern.h> | |
31 | #include <nis_xdr.h> | |
32 | ||
33 | #define NISOBJVAL(col, obj) \ | |
34 | ((obj)->EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val) | |
35 | ||
36 | #define NISOBJLEN(col, obj) \ | |
37 | ((obj)->EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len) | |
38 | ||
39 | extern nis_name grp_tablename_val attribute_hidden; | |
40 | extern size_t grp_tablename_len attribute_hidden; | |
41 | extern enum nss_status _nss_grp_create_tablename (int *errnop); | |
42 | ||
43 | ||
44 | enum nss_status | |
45 | _nss_nisplus_initgroups_dyn (const char *user, gid_t group, long int *start, | |
46 | long int *size, gid_t **groupsp, long int limit, | |
47 | int *errnop) | |
48 | { | |
49 | if (grp_tablename_val == NULL) | |
50 | { | |
51 | enum nss_status status = _nss_grp_create_tablename (errnop); | |
52 | ||
53 | if (status != NSS_STATUS_SUCCESS) | |
54 | return status; | |
55 | } | |
56 | ||
57 | nis_result *result; | |
58 | char buf[strlen (user) + 12 + grp_tablename_len]; | |
59 | ||
60 | snprintf (buf, sizeof (buf), "[members=%s],%s", user, grp_tablename_val); | |
61 | ||
62 | result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH | ALL_RESULTS, NULL, NULL); | |
63 | ||
64 | if (result == NULL) | |
65 | { | |
66 | *errnop = ENOMEM; | |
67 | return NSS_STATUS_TRYAGAIN; | |
68 | } | |
69 | ||
a1ffb40e | 70 | if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS)) |
68361572 UD |
71 | { |
72 | enum nss_status status = niserr2nss (result->status); | |
73 | ||
74 | nis_freeresult (result); | |
75 | return status; | |
76 | } | |
77 | ||
78 | if (NIS_RES_NUMOBJ (result) == 0) | |
79 | { | |
80 | errout: | |
81 | nis_freeresult (result); | |
82 | return NSS_STATUS_NOTFOUND; | |
83 | } | |
84 | ||
85 | gid_t *groups = *groupsp; | |
86 | nis_object *obj = NIS_RES_OBJECT (result); | |
87 | for (unsigned int cnt = 0; cnt < NIS_RES_NUMOBJ (result); ++cnt, ++obj) | |
88 | { | |
89 | if (__type_of (obj) != NIS_ENTRY_OBJ | |
90 | || strcmp (obj->EN_data.en_type, "group_tbl") != 0 | |
91 | || obj->EN_data.en_cols.en_cols_len < 4) | |
92 | continue; | |
93 | ||
94 | char *numstr = NISOBJVAL (2, obj); | |
95 | size_t len = NISOBJLEN (2, obj); | |
96 | if (len == 0 || numstr[0] == '\0') | |
97 | continue; | |
98 | ||
99 | gid_t gid; | |
100 | char *endp; | |
a1ffb40e | 101 | if (__glibc_unlikely (numstr[len - 1] != '\0')) |
68361572 UD |
102 | { |
103 | char numstrbuf[len + 1]; | |
104 | memcpy (numstrbuf, numstr, len); | |
105 | numstrbuf[len] = '\0'; | |
106 | gid = strtoul (numstrbuf, &endp, 10); | |
107 | if (*endp) | |
108 | continue; | |
109 | } | |
110 | else | |
111 | { | |
112 | gid = strtoul (numstr, &endp, 10); | |
113 | if (*endp) | |
114 | continue; | |
115 | } | |
116 | ||
117 | if (gid == group) | |
118 | continue; | |
119 | ||
120 | /* Insert this group. */ | |
121 | if (*start == *size) | |
122 | { | |
123 | /* Need a bigger buffer. */ | |
124 | long int newsize; | |
125 | ||
126 | if (limit > 0 && *size == limit) | |
127 | /* We reached the maximum. */ | |
128 | break; | |
129 | ||
130 | if (limit <= 0) | |
131 | newsize = 2 * *size; | |
132 | else | |
133 | newsize = MIN (limit, 2 * *size); | |
134 | ||
135 | gid_t *newgroups = realloc (groups, newsize * sizeof (*groups)); | |
136 | if (newgroups == NULL) | |
137 | goto errout; | |
138 | *groupsp = groups = newgroups; | |
139 | *size = newsize; | |
140 | } | |
141 | ||
142 | groups[*start] = gid; | |
143 | *start += 1; | |
144 | } | |
145 | ||
146 | nis_freeresult (result); | |
147 | return NSS_STATUS_SUCCESS; | |
148 | } |