Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/netnamer.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * netname utility routines convert from unix names to network names and
31 * vice-versa This module is operating system dependent! What we define here
32 * will work with any unix system that has adopted the sun NIS domain
33 * architecture.
34 */
35 #include <sys/param.h>
36 #include <rpc/rpc.h>
37 #include "rpc_com.h"
38 #ifdef YP
39 #include <rpcsvc/yp_prot.h>
40 #include <rpcsvc/ypclnt.h>
41 #endif
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <grp.h>
45 #include <pwd.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49
50 #include "debug.h"
51
52 static char *OPSYS = "unix";
53 #ifdef YP
54 static char *NETID = "netid.byname";
55 #endif
56 static char *NETIDFILE = "/etc/netid";
57
58 static int getnetid( char *, char * );
59 static int _getgroups( char *, gid_t * );
60
61 #ifndef NGROUPS
62 #define NGROUPS 16
63 #endif
64
65 /*
66 * Convert network-name into unix credential
67 */
68 int
69 netname2user(netname, uidp, gidp, gidlenp, gidlist)
70 char netname[MAXNETNAMELEN + 1];
71 uid_t *uidp;
72 gid_t *gidp;
73 int *gidlenp;
74 gid_t *gidlist;
75 {
76 char *p;
77 int gidlen;
78 uid_t uid;
79 long luid;
80 struct passwd *pwd;
81 char val[1024];
82 char *val1, *val2;
83 char *domain;
84 int vallen;
85 int err;
86
87 if (getnetid(netname, val)) {
88 char *res = val;
89
90 p = strsep(&res, ":");
91 if (p == NULL)
92 return (0);
93 *uidp = (uid_t) atol(p);
94 p = strsep(&res, "\n,");
95 if (p == NULL) {
96 return (0);
97 }
98 *gidp = (gid_t) atol(p);
99 gidlen = 0;
100 for (gidlen = 0; gidlen < NGROUPS; gidlen++) {
101 p = strsep(&res, "\n,");
102 if (p == NULL)
103 break;
104 gidlist[gidlen] = (gid_t) atol(p);
105 }
106 *gidlenp = gidlen;
107
108 return (1);
109 }
110 val1 = strchr(netname, '.');
111 if (val1 == NULL)
112 return (0);
113 if (strncmp(netname, OPSYS, (val1-netname)))
114 return (0);
115 val1++;
116 val2 = strchr(val1, '@');
117 if (val2 == NULL)
118 return (0);
119 vallen = val2 - val1;
120 if (vallen > (1024 - 1))
121 vallen = 1024 - 1;
122 (void) strncpy(val, val1, 1024);
123 val[vallen] = 0;
124
125 err = __rpc_get_default_domain(&domain); /* change to rpc */
126 if (err)
127 return (0);
128
129 if (strcmp(val2 + 1, domain))
130 return (0); /* wrong domain */
131
132 if (sscanf(val, "%ld", &luid) != 1)
133 return (0);
134 uid = luid;
135
136 /* use initgroups method */
137 pwd = getpwuid(uid);
138 if (pwd == NULL)
139 return (0);
140 *uidp = pwd->pw_uid;
141 *gidp = pwd->pw_gid;
142 *gidlenp = _getgroups(pwd->pw_name, gidlist);
143 return (1);
144 }
145
146 /*
147 * initgroups
148 */
149
150 static int
151 _getgroups(uname, groups)
152 char *uname;
153 gid_t groups[NGROUPS];
154 {
155 gid_t ngroups = 0;
156 struct group *grp;
157 int i;
158 int j;
159 int filter;
160
161 setgrent();
162 while ((grp = getgrent())) {
163 for (i = 0; grp->gr_mem[i]; i++)
164 if (!strcmp(grp->gr_mem[i], uname)) {
165 if (ngroups == NGROUPS) {
166 LIBTIRPC_DEBUG(1,
167 ("_getgroups: %s is in too many groups\n", uname));
168 goto toomany;
169 }
170 /* filter out duplicate group entries */
171 filter = 0;
172 for (j = 0; j < ngroups; j++)
173 if (groups[j] == grp->gr_gid) {
174 filter++;
175 break;
176 }
177 if (!filter)
178 groups[ngroups++] = grp->gr_gid;
179 }
180 }
181 toomany:
182 endgrent();
183 return (ngroups);
184 }
185
186 /*
187 * Convert network-name to hostname
188 */
189 int
190 netname2host(netname, hostname, hostlen)
191 char netname[MAXNETNAMELEN + 1];
192 char *hostname;
193 int hostlen;
194 {
195 int err;
196 char valbuf[1024];
197 char *val;
198 char *val2;
199 int vallen;
200 char *domain;
201
202 if (getnetid(netname, valbuf)) {
203 val = valbuf;
204 if ((*val == '0') && (val[1] == ':')) {
205 (void) strncpy(hostname, val + 2, hostlen);
206 return (1);
207 }
208 }
209 val = strchr(netname, '.');
210 if (val == NULL)
211 return (0);
212 if (strncmp(netname, OPSYS, (val - netname)))
213 return (0);
214 val++;
215 val2 = strchr(val, '@');
216 if (val2 == NULL)
217 return (0);
218 vallen = val2 - val;
219 if (vallen > (hostlen - 1))
220 vallen = hostlen - 1;
221 (void) strncpy(hostname, val, vallen);
222 hostname[vallen] = 0;
223
224 err = __rpc_get_default_domain(&domain); /* change to rpc */
225 if (err)
226 return (0);
227
228 if (strcmp(val2 + 1, domain))
229 return (0); /* wrong domain */
230 else
231 return (1);
232 }
233
234 /*
235 * reads the file /etc/netid looking for a + to optionally go to the
236 * network information service.
237 */
238 static int
239 getnetid(key, ret)
240 char *key, *ret;
241 {
242 char buf[1024]; /* big enough */
243 char *res;
244 char *mkey;
245 char *mval;
246 FILE *fd;
247 #ifdef YP
248 char *domain;
249 int err;
250 char *lookup;
251 int len;
252 #endif
253
254 fd = fopen(NETIDFILE, "r");
255 if (fd == NULL) {
256 #ifdef YP
257 res = "+";
258 goto getnetidyp;
259 #else
260 return (0);
261 #endif
262 }
263 for (;;) {
264 if (fd == NULL)
265 return (0); /* getnetidyp brings us here */
266 res = fgets(buf, sizeof(buf), fd);
267 if (res == NULL) {
268 fclose(fd);
269 return (0);
270 }
271 if (res[0] == '#')
272 continue;
273 else if (res[0] == '+') {
274 #ifdef YP
275 getnetidyp:
276 err = yp_get_default_domain(&domain);
277 if (err) {
278 continue;
279 }
280 lookup = NULL;
281 err = yp_match(domain, NETID, key,
282 strlen(key), &lookup, &len);
283 if (err) {
284 LIBTIRPC_DEBUG(1, ("getnetid: match failed error %d", err));
285 continue;
286 }
287 lookup[len] = 0;
288 strcpy(ret, lookup);
289 free(lookup);
290 if (fd != NULL)
291 fclose(fd);
292 return (2);
293 #else /* YP */
294 LIBTIRPC_DEBUG(1,
295 ("Bad record in %s '+' -- NIS not supported in this library copy\n",
296 NETIDFILE));
297 continue;
298 #endif /* YP */
299 } else {
300 mkey = strsep(&res, "\t ");
301 if (mkey == NULL) {
302 fprintf(stderr,
303 "Bad record in %s -- %s", NETIDFILE, buf);
304 continue;
305 }
306 do {
307 mval = strsep(&res, " \t#\n");
308 } while (mval != NULL && !*mval);
309 if (mval == NULL) {
310 fprintf(stderr,
311 "Bad record in %s val problem - %s", NETIDFILE, buf);
312 continue;
313 }
314 if (strcmp(mkey, key) == 0) {
315 strcpy(ret, mval);
316 fclose(fd);
317 return (1);
318
319 }
320 }
321 }
322 }