netnamer.c revision 65220
126219Swpaul/* 226219Swpaul * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 326219Swpaul * unrestricted use provided that this legend is included on all tape 426219Swpaul * media and as a part of the software program in whole or part. Users 526219Swpaul * may copy or modify Sun RPC without charge, but are not authorized 626219Swpaul * to license or distribute it to anyone else except as part of a product or 726219Swpaul * program developed by the user or with the express written consent of 826219Swpaul * Sun Microsystems, Inc. 926219Swpaul * 1026219Swpaul * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 1126219Swpaul * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 1226219Swpaul * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 1326219Swpaul * 1426219Swpaul * Sun RPC is provided with no support and without any obligation on the 1526219Swpaul * part of Sun Microsystems, Inc. to assist in its use, correction, 1626219Swpaul * modification or enhancement. 1726219Swpaul * 1826219Swpaul * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 1926219Swpaul * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 2026219Swpaul * OR ANY PART THEREOF. 2126219Swpaul * 2226219Swpaul * In no event will Sun Microsystems, Inc. be liable for any lost revenue 2326219Swpaul * or profits or other special, indirect and consequential damages, even if 2426219Swpaul * Sun has been advised of the possibility of such damages. 2526219Swpaul * 2626219Swpaul * Sun Microsystems, Inc. 2726219Swpaul * 2550 Garcia Avenue 2826219Swpaul * Mountain View, California 94043 2965220Sache * 3065220Sache * $FreeBSD: head/lib/libc/rpc/netnamer.c 65220 2000-08-29 21:04:07Z ache $ 3126219Swpaul */ 3226219Swpaul#if !defined(lint) && defined(SCCSIDS) 3326219Swpaulstatic char sccsid[] = "@(#)netnamer.c 1.13 91/03/11 Copyr 1986 Sun Micro"; 3426219Swpaul#endif 3526219Swpaul/* 3626219Swpaul * netname utility routines convert from unix names to network names and 3726219Swpaul * vice-versa This module is operating system dependent! What we define here 3826219Swpaul * will work with any unix system that has adopted the sun NIS domain 3926219Swpaul * architecture. 4026219Swpaul */ 4126219Swpaul#include <sys/param.h> 4226219Swpaul#include <rpc/rpc.h> 4326219Swpaul#include <rpc/rpc_com.h> 4426219Swpaul#ifdef YP 4526219Swpaul#include <rpcsvc/yp_prot.h> 4626219Swpaul#include <rpcsvc/ypclnt.h> 4726219Swpaul#endif 4826219Swpaul#include <ctype.h> 4926219Swpaul#include <stdio.h> 5026219Swpaul#include <grp.h> 5126219Swpaul#include <pwd.h> 5226219Swpaul#include <string.h> 5326219Swpaul#include <stdlib.h> 5426219Swpaul#include <unistd.h> 5526219Swpaul 5626219Swpaulstatic char *OPSYS = "unix"; 5726219Swpaulstatic char *NETID = "netid.byname"; 5826219Swpaulstatic char *NETIDFILE = "/etc/netid"; 5926219Swpaul 6026219Swpaulstatic int getnetid __P(( char *, char * )); 6126219Swpaulstatic int _getgroups __P(( char *, gid_t * )); 6226219Swpaul 6326219Swpaul#ifndef NGROUPS 6426219Swpaul#define NGROUPS 16 6526219Swpaul#endif 6626219Swpaul 6726219Swpaul/* 6826219Swpaul * Convert network-name into unix credential 6926219Swpaul */ 7026219Swpaulint 7126219Swpaulnetname2user(netname, uidp, gidp, gidlenp, gidlist) 7226219Swpaul char netname[MAXNETNAMELEN + 1]; 7326219Swpaul uid_t *uidp; 7426219Swpaul gid_t *gidp; 7526219Swpaul int *gidlenp; 7626219Swpaul gid_t *gidlist; 7726219Swpaul{ 7826219Swpaul char *p; 7926219Swpaul int gidlen; 8026219Swpaul uid_t uid; 8137300Sbde long luid; 8226219Swpaul struct passwd *pwd; 8326219Swpaul char val[1024]; 8426219Swpaul char *val1, *val2; 8526219Swpaul char *domain; 8626219Swpaul int vallen; 8726219Swpaul int err; 8826219Swpaul 8926219Swpaul if (getnetid(netname, val)) { 9065220Sache char *res = val; 9165220Sache 9265220Sache p = strsep(&res, ":"); 9326219Swpaul if (p == NULL) 9426219Swpaul return (0); 9565220Sache *uidp = (uid_t) atol(p); 9665220Sache p = strsep(&res, "\n,"); 9726219Swpaul if (p == NULL) { 9826219Swpaul return (0); 9926219Swpaul } 10065220Sache *gidp = (gid_t) atol(p); 10126219Swpaul gidlen = 0; 10226219Swpaul for (gidlen = 0; gidlen < NGROUPS; gidlen++) { 10365220Sache p = strsep(&res, "\n,"); 10426219Swpaul if (p == NULL) 10526219Swpaul break; 10626219Swpaul gidlist[gidlen] = (gid_t) atol(p); 10726219Swpaul } 10826219Swpaul *gidlenp = gidlen; 10926219Swpaul 11026219Swpaul return (1); 11126219Swpaul } 11226219Swpaul val1 = strchr(netname, '.'); 11326219Swpaul if (val1 == NULL) 11426219Swpaul return (0); 11526219Swpaul if (strncmp(netname, OPSYS, (val1-netname))) 11626219Swpaul return (0); 11726219Swpaul val1++; 11826219Swpaul val2 = strchr(val1, '@'); 11926219Swpaul if (val2 == NULL) 12026219Swpaul return (0); 12126219Swpaul vallen = val2 - val1; 12226219Swpaul if (vallen > (1024 - 1)) 12326219Swpaul vallen = 1024 - 1; 12426219Swpaul (void) strncpy(val, val1, 1024); 12526219Swpaul val[vallen] = 0; 12626219Swpaul 12726219Swpaul err = _rpc_get_default_domain(&domain); /* change to rpc */ 12826219Swpaul if (err) 12926219Swpaul return (0); 13026219Swpaul 13126219Swpaul if (strcmp(val2 + 1, domain)) 13226219Swpaul return (0); /* wrong domain */ 13326219Swpaul 13437300Sbde if (sscanf(val, "%ld", &luid) != 1) 13526219Swpaul return (0); 13637300Sbde uid = luid; 13737300Sbde 13826219Swpaul /* use initgroups method */ 13926219Swpaul pwd = getpwuid(uid); 14026219Swpaul if (pwd == NULL) 14126219Swpaul return (0); 14226219Swpaul *uidp = pwd->pw_uid; 14326219Swpaul *gidp = pwd->pw_gid; 14426219Swpaul *gidlenp = _getgroups(pwd->pw_name, gidlist); 14526219Swpaul return (1); 14626219Swpaul} 14726219Swpaul 14826219Swpaul/* 14926219Swpaul * initgroups 15026219Swpaul */ 15126219Swpaul 15226219Swpaulstatic int 15326219Swpaul_getgroups(uname, groups) 15426219Swpaul char *uname; 15526219Swpaul gid_t groups[NGROUPS]; 15626219Swpaul{ 15726219Swpaul gid_t ngroups = 0; 15826219Swpaul register struct group *grp; 15926219Swpaul register int i; 16026219Swpaul register int j; 16126219Swpaul int filter; 16226219Swpaul 16326219Swpaul setgrent(); 16426219Swpaul while ((grp = getgrent())) { 16526219Swpaul for (i = 0; grp->gr_mem[i]; i++) 16626219Swpaul if (!strcmp(grp->gr_mem[i], uname)) { 16726219Swpaul if (ngroups == NGROUPS) { 16826219Swpaul#ifdef DEBUG 16926219Swpaul fprintf(stderr, 17026219Swpaul "initgroups: %s is in too many groups\n", uname); 17126219Swpaul#endif 17226219Swpaul goto toomany; 17326219Swpaul } 17426219Swpaul /* filter out duplicate group entries */ 17526219Swpaul filter = 0; 17626219Swpaul for (j = 0; j < ngroups; j++) 17726219Swpaul if (groups[j] == grp->gr_gid) { 17826219Swpaul filter++; 17926219Swpaul break; 18026219Swpaul } 18126219Swpaul if (!filter) 18226219Swpaul groups[ngroups++] = grp->gr_gid; 18326219Swpaul } 18426219Swpaul } 18526219Swpaultoomany: 18626219Swpaul endgrent(); 18726219Swpaul return (ngroups); 18826219Swpaul} 18926219Swpaul 19026219Swpaul/* 19126219Swpaul * Convert network-name to hostname 19226219Swpaul */ 19326219Swpaulint 19426219Swpaulnetname2host(netname, hostname, hostlen) 19526219Swpaul char netname[MAXNETNAMELEN + 1]; 19626219Swpaul char *hostname; 19726219Swpaul int hostlen; 19826219Swpaul{ 19926219Swpaul int err; 20026219Swpaul char valbuf[1024]; 20126219Swpaul char *val; 20226219Swpaul char *val2; 20326219Swpaul int vallen; 20426219Swpaul char *domain; 20526219Swpaul 20626219Swpaul if (getnetid(netname, valbuf)) { 20726219Swpaul val = valbuf; 20826219Swpaul if ((*val == '0') && (val[1] == ':')) { 20926219Swpaul (void) strncpy(hostname, val + 2, hostlen); 21026219Swpaul return (1); 21126219Swpaul } 21226219Swpaul } 21326219Swpaul val = strchr(netname, '.'); 21426219Swpaul if (val == NULL) 21526219Swpaul return (0); 21626219Swpaul if (strncmp(netname, OPSYS, (val - netname))) 21726219Swpaul return (0); 21826219Swpaul val++; 21926219Swpaul val2 = strchr(val, '@'); 22026219Swpaul if (val2 == NULL) 22126219Swpaul return (0); 22226219Swpaul vallen = val2 - val; 22326219Swpaul if (vallen > (hostlen - 1)) 22426219Swpaul vallen = hostlen - 1; 22526219Swpaul (void) strncpy(hostname, val, vallen); 22626219Swpaul hostname[vallen] = 0; 22726219Swpaul 22826219Swpaul err = _rpc_get_default_domain(&domain); /* change to rpc */ 22926219Swpaul if (err) 23026219Swpaul return (0); 23126219Swpaul 23226219Swpaul if (strcmp(val2 + 1, domain)) 23326219Swpaul return (0); /* wrong domain */ 23426219Swpaul else 23526219Swpaul return (1); 23626219Swpaul} 23726219Swpaul 23826219Swpaul/* 23926219Swpaul * reads the file /etc/netid looking for a + to optionally go to the 24026219Swpaul * network information service. 24126219Swpaul */ 24226219Swpaulint 24326219Swpaulgetnetid(key, ret) 24426219Swpaul char *key, *ret; 24526219Swpaul{ 24626219Swpaul char buf[1024]; /* big enough */ 24726219Swpaul char *res; 24826219Swpaul char *mkey; 24926219Swpaul char *mval; 25026219Swpaul FILE *fd; 25126219Swpaul#ifdef YP 25226219Swpaul char *domain; 25326219Swpaul int err; 25426219Swpaul char *lookup; 25526219Swpaul int len; 25626219Swpaul#endif 25726219Swpaul 25826219Swpaul fd = fopen(NETIDFILE, "r"); 25965220Sache if (fd == NULL) { 26026219Swpaul#ifdef YP 26126219Swpaul res = "+"; 26226219Swpaul goto getnetidyp; 26326219Swpaul#else 26426219Swpaul return (0); 26526219Swpaul#endif 26626219Swpaul } 26726219Swpaul for (;;) { 26865220Sache if (fd == NULL) 26926219Swpaul return (0); /* getnetidyp brings us here */ 27065220Sache res = fgets(buf, sizeof(buf), fd); 27165220Sache if (res == NULL) { 27226219Swpaul fclose(fd); 27326219Swpaul return (0); 27426219Swpaul } 27526219Swpaul if (res[0] == '#') 27626219Swpaul continue; 27726219Swpaul else if (res[0] == '+') { 27826219Swpaul#ifdef YP 27926219Swpaul getnetidyp: 28026219Swpaul err = yp_get_default_domain(&domain); 28126219Swpaul if (err) { 28226219Swpaul continue; 28326219Swpaul } 28426219Swpaul lookup = NULL; 28526219Swpaul err = yp_match(domain, NETID, key, 28626219Swpaul strlen(key), &lookup, &len); 28726219Swpaul if (err) { 28826219Swpaul#ifdef DEBUG 28926219Swpaul fprintf(stderr, "match failed error %d\n", err); 29026219Swpaul#endif 29126219Swpaul continue; 29226219Swpaul } 29326219Swpaul lookup[len] = 0; 29426219Swpaul strcpy(ret, lookup); 29526219Swpaul free(lookup); 29626583Swpaul if (fd != NULL) 29726583Swpaul fclose(fd); 29826219Swpaul return (2); 29926219Swpaul#else /* YP */ 30026219Swpaul#ifdef DEBUG 30126219Swpaul fprintf(stderr, 30226219Swpaul"Bad record in %s '+' -- NIS not supported in this library copy\n", 30326219Swpaul NETIDFILE); 30426219Swpaul#endif 30526219Swpaul continue; 30626219Swpaul#endif /* YP */ 30726219Swpaul } else { 30865220Sache mkey = strsep(&res, "\t "); 30926219Swpaul if (mkey == NULL) { 31026219Swpaul fprintf(stderr, 31126219Swpaul "Bad record in %s -- %s", NETIDFILE, buf); 31226219Swpaul continue; 31326219Swpaul } 31465220Sache do { 31565220Sache mval = strsep(&res, " \t#\n"); 31665220Sache } while (mval != NULL && !*mval); 31726219Swpaul if (mval == NULL) { 31826219Swpaul fprintf(stderr, 31926219Swpaul "Bad record in %s val problem - %s", NETIDFILE, buf); 32026219Swpaul continue; 32126219Swpaul } 32226219Swpaul if (strcmp(mkey, key) == 0) { 32326219Swpaul strcpy(ret, mval); 32426219Swpaul fclose(fd); 32526219Swpaul return (1); 32626219Swpaul 32726219Swpaul } 32826219Swpaul } 32926219Swpaul } 33026219Swpaul} 331