source: rtems-libbsd/services/librpc/src/rpc/netnamer.c @ e599318

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e599318 was e599318, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/13 at 20:52:54

Update files to match FreeBSD layout

Add compatibility with Newlib header files. Some FreeBSD header files
are mapped by the translation script:

o rtems/bsd/sys/_types.h
o rtems/bsd/sys/errno.h
o rtems/bsd/sys/lock.h
o rtems/bsd/sys/param.h
o rtems/bsd/sys/resource.h
o rtems/bsd/sys/time.h
o rtems/bsd/sys/timespec.h
o rtems/bsd/sys/types.h
o rtems/bsd/sys/unistd.h

It is now possible to include <sys/socket.h> directly for example.

Generate one Makefile which builds everything including tests.

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part.  Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user or with the express written consent of
8 * Sun Microsystems, Inc.
9 *
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 *
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
17 *
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
21 *
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
25 *
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California  94043
29 */
30#if !defined(lint) && defined(SCCSIDS)
31static char sccsid[] = "@(#)netnamer.c 1.13 91/03/11 Copyr 1986 Sun Micro";
32#endif
33/*
34 * netname utility routines convert from unix names to network names and
35 * vice-versa This module is operating system dependent! What we define here
36 * will work with any unix system that has adopted the sun NIS domain
37 * architecture.
38 */
39#ifdef HAVE_CONFIG_H
40#include "config.h"
41#endif
42
43#include <sys/param.h>
44#include <rpc/rpc.h>
45#include <rpc/rpc_com.h>
46#ifdef YP
47#include <rpcsvc/yp_prot.h>
48#include <rpcsvc/ypclnt.h>
49#endif
50#include <ctype.h>
51#include <stdio.h>
52#include <grp.h>
53#include <pwd.h>
54#include <string.h>
55#include <stdlib.h>
56#include <unistd.h>
57
58static char    *OPSYS = "unix";
59#ifdef YP
60static char    *NETID = "netid.byname";
61#endif
62static char    *NETIDFILE = "/etc/netid";
63
64static int getnetid ( char *, char * );
65static int _getgroups ( char *, gid_t * );
66
67#ifndef NGROUPS
68#define NGROUPS 16
69#endif
70
71/*
72 * Convert network-name into unix credential
73 */
74int
75netname2user(
76        char            netname[MAXNETNAMELEN + 1],
77        uid_t            *uidp,
78        gid_t            *gidp,
79        int            *gidlenp,
80        gid_t          *gidlist)
81{
82        char           *p;
83        int             gidlen;
84        uid_t           uid;
85        long            luid;
86        struct passwd  *pwd;
87        char            val[1024];
88        char           *val1, *val2;
89        char           *domain;
90        int             vallen;
91        int             err;
92
93        if (getnetid(netname, val)) {
94                p = strtok(val, ":");
95                if (p == NULL)
96                        return (0);
97                *uidp = (uid_t) atol(val);
98                p = strtok(NULL, "\n,");
99                *gidp = (gid_t) atol(p);
100                if (p == NULL) {
101                        return (0);
102                }
103                gidlen = 0;
104                for (gidlen = 0; gidlen < NGROUPS; gidlen++) {
105                        p = strtok(NULL, "\n,");
106                        if (p == NULL)
107                                break;
108                        gidlist[gidlen] = (gid_t) atol(p);
109                }
110                *gidlenp = gidlen;
111
112                return (1);
113        }
114        val1 = strchr(netname, '.');
115        if (val1 == NULL)
116                return (0);
117        if (strncmp(netname, OPSYS, (val1-netname)))
118                return (0);
119        val1++;
120        val2 = strchr(val1, '@');
121        if (val2 == NULL)
122                return (0);
123        vallen = val2 - val1;
124        if (vallen > (1024 - 1))
125                vallen = 1024 - 1;
126        (void) strncpy(val, val1, 1024);
127        val[vallen] = 0;
128
129        err = _rpc_get_default_domain(&domain); /* change to rpc */
130        if (err)
131                return (0);
132
133        if (strcmp(val2 + 1, domain))
134                return (0);     /* wrong domain */
135
136        if (sscanf(val, "%ld", &luid) != 1)
137                return (0);
138        uid = luid;
139
140        /* use initgroups method */
141        pwd = getpwuid(uid);
142        if (pwd == NULL)
143                return (0);
144        *uidp = pwd->pw_uid;
145        *gidp = pwd->pw_gid;
146        *gidlenp = _getgroups(pwd->pw_name, gidlist);
147        return (1);
148}
149
150/*
151 * initgroups
152 */
153
154static int
155_getgroups(
156        char           *uname,
157        gid_t          groups[NGROUPS])
158{
159        gid_t           ngroups = 0;
160        register struct group *grp;
161        register int    i;
162        register int    j;
163        int             filter;
164
165        setgrent();
166        while ((grp = getgrent())) {
167                for (i = 0; grp->gr_mem[i]; i++)
168                        if (!strcmp(grp->gr_mem[i], uname)) {
169                                if (ngroups == NGROUPS) {
170#ifdef DEBUG
171                                        fprintf(stderr,
172                                "initgroups: %s is in too many groups\n", uname);
173#endif
174                                        goto toomany;
175                                }
176                                /* filter out duplicate group entries */
177                                filter = 0;
178                                for (j = 0; j < ngroups; j++)
179                                        if (groups[j] == grp->gr_gid) {
180                                                filter++;
181                                                break;
182                                        }
183                                if (!filter)
184                                        groups[ngroups++] = grp->gr_gid;
185                        }
186        }
187toomany:
188        endgrent();
189        return (ngroups);
190}
191
192/*
193 * Convert network-name to hostname
194 */
195int
196netname2host(
197        char            netname[MAXNETNAMELEN + 1],
198        char           *hostname,
199        int             hostlen)
200{
201        int             err;
202        char            valbuf[1024];
203        char           *val;
204        char           *val2;
205        int             vallen;
206        char           *domain;
207
208        if (getnetid(netname, valbuf)) {
209                val = valbuf;
210                if ((*val == '0') && (val[1] == ':')) {
211                        (void) strncpy(hostname, val + 2, hostlen);
212                        return (1);
213                }
214        }
215        val = strchr(netname, '.');
216        if (val == NULL)
217                return (0);
218        if (strncmp(netname, OPSYS, (val - netname)))
219                return (0);
220        val++;
221        val2 = strchr(val, '@');
222        if (val2 == NULL)
223                return (0);
224        vallen = val2 - val;
225        if (vallen > (hostlen - 1))
226                vallen = hostlen - 1;
227        (void) strncpy(hostname, val, vallen);
228        hostname[vallen] = 0;
229
230        err = _rpc_get_default_domain(&domain); /* change to rpc */
231        if (err)
232                return (0);
233
234        if (strcmp(val2 + 1, domain))
235                return (0);     /* wrong domain */
236        else
237                return (1);
238}
239
240/*
241 * reads the file /etc/netid looking for a + to optionally go to the
242 * network information service.
243 */
244int
245getnetid(
246        char *key,
247        char *ret)
248{
249        char            buf[1024];      /* big enough */
250        char           *res;
251        char           *mkey;
252        char           *mval;
253        FILE           *fd;
254#ifdef YP
255        char           *domain;
256        int             err;
257        char           *lookup;
258        int             len;
259#endif
260
261        fd = fopen(NETIDFILE, "r");
262        if (fd == (FILE *) 0) {
263#ifdef YP
264                res = "+";
265                goto getnetidyp;
266#else
267                return (0);
268#endif
269        }
270        for (;;) {
271                if (fd == (FILE *) 0)
272                        return (0);     /* getnetidyp brings us here */
273                res = fgets(buf, 1024, fd);
274                if (res == 0) {
275                        fclose(fd);
276                        return (0);
277                }
278                if (res[0] == '#')
279                        continue;
280                else if (res[0] == '+') {
281#ifdef YP
282        getnetidyp:
283                        err = yp_get_default_domain(&domain);
284                        if (err) {
285                                continue;
286                        }
287                        lookup = NULL;
288                        err = yp_match(domain, NETID, key,
289                                strlen(key), &lookup, &len);
290                        if (err) {
291#ifdef DEBUG
292                                fprintf(stderr, "match failed error %d\n", err);
293#endif
294                                continue;
295                        }
296                        lookup[len] = 0;
297                        strcpy(ret, lookup);
298                        free(lookup);
299                        if (fd != NULL)
300                                fclose(fd);
301                        return (2);
302#else   /* YP */
303#ifdef DEBUG
304                        fprintf(stderr,
305"Bad record in %s '+' -- NIS not supported in this library copy\n",
306                                NETIDFILE);
307#endif
308                        continue;
309#endif  /* YP */
310                } else {
311                        mkey = strtok(buf, "\t ");
312                        if (mkey == NULL) {
313                                fprintf(stderr,
314                "Bad record in %s -- %s", NETIDFILE, buf);
315                                continue;
316                        }
317                        mval = strtok(NULL, " \t#\n");
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.