source: rtems-libbsd/freebsd/lib/libc/rpc/netnamer.c @ 60b1d40

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 60b1d40 was 60b1d40, checked in by Sebastian Huber <sebastian.huber@…>, on 06/09/16 at 08:23:57

RPC(3): Import from FreeBSD

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