netnamer.c revision 37300
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#include <sys/param.h> 40#include <rpc/rpc.h> 41#include <rpc/rpc_com.h> 42#ifdef YP 43#include <rpcsvc/yp_prot.h> 44#include <rpcsvc/ypclnt.h> 45#endif 46#include <ctype.h> 47#include <stdio.h> 48#include <grp.h> 49#include <pwd.h> 50#include <string.h> 51#include <stdlib.h> 52#include <unistd.h> 53 54static char *OPSYS = "unix"; 55static char *NETID = "netid.byname"; 56static char *NETIDFILE = "/etc/netid"; 57 58static int getnetid __P(( char *, char * )); 59static int _getgroups __P(( char *, gid_t * )); 60 61#ifndef NGROUPS 62#define NGROUPS 16 63#endif 64 65/* 66 * Convert network-name into unix credential 67 */ 68int 69netname2user(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 p = strtok(val, ":"); 89 if (p == NULL) 90 return (0); 91 *uidp = (uid_t) atol(val); 92 p = strtok(NULL, "\n,"); 93 *gidp = (gid_t) atol(p); 94 if (p == NULL) { 95 return (0); 96 } 97 gidlen = 0; 98 for (gidlen = 0; gidlen < NGROUPS; gidlen++) { 99 p = strtok(NULL, "\n,"); 100 if (p == NULL) 101 break; 102 gidlist[gidlen] = (gid_t) atol(p); 103 } 104 *gidlenp = gidlen; 105 106 return (1); 107 } 108 val1 = strchr(netname, '.'); 109 if (val1 == NULL) 110 return (0); 111 if (strncmp(netname, OPSYS, (val1-netname))) 112 return (0); 113 val1++; 114 val2 = strchr(val1, '@'); 115 if (val2 == NULL) 116 return (0); 117 vallen = val2 - val1; 118 if (vallen > (1024 - 1)) 119 vallen = 1024 - 1; 120 (void) strncpy(val, val1, 1024); 121 val[vallen] = 0; 122 123 err = _rpc_get_default_domain(&domain); /* change to rpc */ 124 if (err) 125 return (0); 126 127 if (strcmp(val2 + 1, domain)) 128 return (0); /* wrong domain */ 129 130 if (sscanf(val, "%ld", &luid) != 1) 131 return (0); 132 uid = luid; 133 134 /* use initgroups method */ 135 pwd = getpwuid(uid); 136 if (pwd == NULL) 137 return (0); 138 *uidp = pwd->pw_uid; 139 *gidp = pwd->pw_gid; 140 *gidlenp = _getgroups(pwd->pw_name, gidlist); 141 return (1); 142} 143 144/* 145 * initgroups 146 */ 147 148static int 149_getgroups(uname, groups) 150 char *uname; 151 gid_t groups[NGROUPS]; 152{ 153 gid_t ngroups = 0; 154 register struct group *grp; 155 register int i; 156 register int j; 157 int filter; 158 159 setgrent(); 160 while ((grp = getgrent())) { 161 for (i = 0; grp->gr_mem[i]; i++) 162 if (!strcmp(grp->gr_mem[i], uname)) { 163 if (ngroups == NGROUPS) { 164#ifdef DEBUG 165 fprintf(stderr, 166 "initgroups: %s is in too many groups\n", uname); 167#endif 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 } 181toomany: 182 endgrent(); 183 return (ngroups); 184} 185 186/* 187 * Convert network-name to hostname 188 */ 189int 190netname2host(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 */ 238int 239getnetid(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 == (FILE *) 0) { 256#ifdef YP 257 res = "+"; 258 goto getnetidyp; 259#else 260 return (0); 261#endif 262 } 263 for (;;) { 264 if (fd == (FILE *) 0) 265 return (0); /* getnetidyp brings us here */ 266 res = fgets(buf, 1024, fd); 267 if (res == 0) { 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#ifdef DEBUG 285 fprintf(stderr, "match failed error %d\n", err); 286#endif 287 continue; 288 } 289 lookup[len] = 0; 290 strcpy(ret, lookup); 291 free(lookup); 292 if (fd != NULL) 293 fclose(fd); 294 return (2); 295#else /* YP */ 296#ifdef DEBUG 297 fprintf(stderr, 298"Bad record in %s '+' -- NIS not supported in this library copy\n", 299 NETIDFILE); 300#endif 301 continue; 302#endif /* YP */ 303 } else { 304 mkey = strtok(buf, "\t "); 305 if (mkey == NULL) { 306 fprintf(stderr, 307 "Bad record in %s -- %s", NETIDFILE, buf); 308 continue; 309 } 310 mval = strtok(NULL, " \t#\n"); 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} 325