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 31 #if defined(LIBC_SCCS) && !defined(lint) 32 static 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 <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 60 static char *OPSYS = "unix"; 61 #ifdef YP 62 static char *NETID = "netid.byname"; 63 #endif 64 static char *NETIDFILE = "/etc/netid"; 65 66 static int getnetid( char *, char * ); 67 static int _getgroups( char *, gid_t * ); 68 69 /* 70 * Convert network-name into unix credential 71 */ 72 int 73 netname2user(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 153 static 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 } 186 toomany: 187 endgrent(); 188 return (ngroups); 189 } 190 191 /* 192 * Convert network-name to hostname 193 */ 194 int 195 netname2host(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 */ 243 int 244 getnetid(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 } 332