9b3b7dc4f545b6f43ae281bc06e7f9cc833eb171
[platform/upstream/libtirpc.git] / src / netnamer.c
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 static char    *OPSYS = "unix";
51 static char    *NETID = "netid.byname";
52 static char    *NETIDFILE = "/etc/netid";
53
54 static int getnetid( char *, char * );
55 static int _getgroups( char *, gid_t * );
56
57 #ifndef NGROUPS
58 #define NGROUPS 16
59 #endif
60
61 /*
62  * Convert network-name into unix credential
63  */
64 int
65 netname2user(netname, uidp, gidp, gidlenp, gidlist)
66         char            netname[MAXNETNAMELEN + 1];
67         uid_t            *uidp;
68         gid_t            *gidp;
69         int            *gidlenp;
70         gid_t          *gidlist;
71 {
72         char           *p;
73         int             gidlen;
74         uid_t           uid;
75         long            luid;
76         struct passwd  *pwd;
77         char            val[1024];
78         char           *val1, *val2;
79         char           *domain;
80         int             vallen;
81         int             err;
82
83         if (getnetid(netname, val)) {
84                 char *res = val;
85
86                 p = strsep(&res, ":");
87                 if (p == NULL)
88                         return (0);
89                 *uidp = (uid_t) atol(p);
90                 p = strsep(&res, "\n,");
91                 if (p == NULL) {
92                         return (0);
93                 }
94                 *gidp = (gid_t) atol(p);
95                 gidlen = 0;
96                 for (gidlen = 0; gidlen < NGROUPS; gidlen++) {
97                         p = strsep(&res, "\n,");
98                         if (p == NULL)
99                                 break;
100                         gidlist[gidlen] = (gid_t) atol(p);
101                 }
102                 *gidlenp = gidlen;
103
104                 return (1);
105         }
106         val1 = strchr(netname, '.');
107         if (val1 == NULL)
108                 return (0);
109         if (strncmp(netname, OPSYS, (val1-netname)))
110                 return (0);
111         val1++;
112         val2 = strchr(val1, '@');
113         if (val2 == NULL)
114                 return (0);
115         vallen = val2 - val1;
116         if (vallen > (1024 - 1))
117                 vallen = 1024 - 1;
118         (void) strncpy(val, val1, 1024);
119         val[vallen] = 0;
120
121         err = __rpc_get_default_domain(&domain);        /* change to rpc */
122         if (err)
123                 return (0);
124
125         if (strcmp(val2 + 1, domain))
126                 return (0);     /* wrong domain */
127
128         if (sscanf(val, "%ld", &luid) != 1)
129                 return (0);
130         uid = luid;
131
132         /* use initgroups method */
133         pwd = getpwuid(uid);
134         if (pwd == NULL)
135                 return (0);
136         *uidp = pwd->pw_uid;
137         *gidp = pwd->pw_gid;
138         *gidlenp = _getgroups(pwd->pw_name, gidlist);
139         return (1);
140 }
141
142 /*
143  * initgroups
144  */
145
146 static int
147 _getgroups(uname, groups)
148         char           *uname;
149         gid_t          groups[NGROUPS];
150 {
151         gid_t           ngroups = 0;
152         struct group *grp;
153         int    i;
154         int    j;
155         int             filter;
156
157         setgrent();
158         while ((grp = getgrent())) {
159                 for (i = 0; grp->gr_mem[i]; i++)
160                         if (!strcmp(grp->gr_mem[i], uname)) {
161                                 if (ngroups == NGROUPS) {
162 #ifdef DEBUG
163                                         fprintf(stderr,
164                                 "initgroups: %s is in too many groups\n", uname);
165 #endif
166                                         goto toomany;
167                                 }
168                                 /* filter out duplicate group entries */
169                                 filter = 0;
170                                 for (j = 0; j < ngroups; j++)
171                                         if (groups[j] == grp->gr_gid) {
172                                                 filter++;
173                                                 break;
174                                         }
175                                 if (!filter)
176                                         groups[ngroups++] = grp->gr_gid;
177                         }
178         }
179 toomany:
180         endgrent();
181         return (ngroups);
182 }
183
184 /*
185  * Convert network-name to hostname
186  */
187 int
188 netname2host(netname, hostname, hostlen)
189         char            netname[MAXNETNAMELEN + 1];
190         char           *hostname;
191         int             hostlen;
192 {
193         int             err;
194         char            valbuf[1024];
195         char           *val;
196         char           *val2;
197         int             vallen;
198         char           *domain;
199
200         if (getnetid(netname, valbuf)) {
201                 val = valbuf;
202                 if ((*val == '0') && (val[1] == ':')) {
203                         (void) strncpy(hostname, val + 2, hostlen);
204                         return (1);
205                 }
206         }
207         val = strchr(netname, '.');
208         if (val == NULL)
209                 return (0);
210         if (strncmp(netname, OPSYS, (val - netname)))
211                 return (0);
212         val++;
213         val2 = strchr(val, '@');
214         if (val2 == NULL)
215                 return (0);
216         vallen = val2 - val;
217         if (vallen > (hostlen - 1))
218                 vallen = hostlen - 1;
219         (void) strncpy(hostname, val, vallen);
220         hostname[vallen] = 0;
221
222         err = __rpc_get_default_domain(&domain);        /* change to rpc */
223         if (err)
224                 return (0);
225
226         if (strcmp(val2 + 1, domain))
227                 return (0);     /* wrong domain */
228         else
229                 return (1);
230 }
231
232 /*
233  * reads the file /etc/netid looking for a + to optionally go to the
234  * network information service.
235  */
236 int
237 getnetid(key, ret)
238         char           *key, *ret;
239 {
240         char            buf[1024];      /* big enough */
241         char           *res;
242         char           *mkey;
243         char           *mval;
244         FILE           *fd;
245 #ifdef YP
246         char           *domain;
247         int             err;
248         char           *lookup;
249         int             len;
250 #endif
251
252         fd = fopen(NETIDFILE, "r");
253         if (fd == NULL) {
254 #ifdef YP
255                 res = "+";
256                 goto getnetidyp;
257 #else
258                 return (0);
259 #endif
260         }
261         for (;;) {
262                 if (fd == NULL)
263                         return (0);     /* getnetidyp brings us here */
264                 res = fgets(buf, sizeof(buf), fd);
265                 if (res == NULL) {
266                         fclose(fd);
267                         return (0);
268                 }
269                 if (res[0] == '#')
270                         continue;
271                 else if (res[0] == '+') {
272 #ifdef YP
273         getnetidyp:
274                         err = yp_get_default_domain(&domain);
275                         if (err) {
276                                 continue;
277                         }
278                         lookup = NULL;
279                         err = yp_match(domain, NETID, key,
280                                 strlen(key), &lookup, &len);
281                         if (err) {
282 #ifdef DEBUG
283                                 fprintf(stderr, "match failed error %d\n", err);
284 #endif
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 #ifdef DEBUG
295                         fprintf(stderr,
296 "Bad record in %s '+' -- NIS not supported in this library copy\n",
297                                 NETIDFILE);
298 #endif
299                         continue;
300 #endif  /* YP */
301                 } else {
302                         mkey = strsep(&res, "\t ");
303                         if (mkey == NULL) {
304                                 fprintf(stderr,
305                 "Bad record in %s -- %s", NETIDFILE, buf);
306                                 continue;
307                         }
308                         do {
309                                 mval = strsep(&res, " \t#\n");
310                         } while (mval != NULL && !*mval);
311                         if (mval == NULL) {
312                                 fprintf(stderr,
313                 "Bad record in %s val problem - %s", NETIDFILE, buf);
314                                 continue;
315                         }
316                         if (strcmp(mkey, key) == 0) {
317                                 strcpy(ret, mval);
318                                 fclose(fd);
319                                 return (1);
320
321                         }
322                 }
323         }
324 }