mknetid.c revision 21673
154359Sroberto/* 2132451Sroberto * Copyright (c) 1995, 1996 354359Sroberto * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 454359Sroberto * 554359Sroberto * Redistribution and use in source and binary forms, with or without 654359Sroberto * modification, are permitted provided that the following conditions 754359Sroberto * are met: 854359Sroberto * 1. Redistributions of source code must retain the above copyright 954359Sroberto * notice, this list of conditions and the following disclaimer. 1054359Sroberto * 2. Redistributions in binary form must reproduce the above copyright 1154359Sroberto * notice, this list of conditions and the following disclaimer in the 1254359Sroberto * documentation and/or other materials provided with the distribution. 1354359Sroberto * 3. All advertising materials mentioning features or use of this software 1454359Sroberto * must display the following acknowledgement: 1554359Sroberto * This product includes software developed by Bill Paul. 1654359Sroberto * 4. Neither the name of the author nor the names of any co-contributors 1754359Sroberto * may be used to endorse or promote products derived from this software 1854359Sroberto * without specific prior written permission. 1954359Sroberto * 2054359Sroberto * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 2154359Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2254359Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2354359Sroberto * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 2454359Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2554359Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2654359Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2754359Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2854359Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2954359Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3054359Sroberto * SUCH DAMAGE. 3154359Sroberto * 3254359Sroberto * netid map generator program 3354359Sroberto * 3454359Sroberto * Written by Bill Paul <wpaul@ctr.columbia.edu> 3554359Sroberto * Center for Telecommunications Research 3654359Sroberto * Columbia University, New York City 3754359Sroberto * 38132451Sroberto * $FreeBSD: head/libexec/mknetid/mknetid.c 21673 1997-01-14 07:20:47Z jkh $ 3954359Sroberto */ 4054359Sroberto 4154359Sroberto#include <sys/types.h> 4254359Sroberto 4354359Sroberto#include <rpc/rpc.h> 4454359Sroberto#include <rpcsvc/yp_prot.h> 4554359Sroberto#include <rpcsvc/ypclnt.h> 4682498Sroberto 4782498Sroberto#include <err.h> 4882498Sroberto#include <grp.h> 4954359Sroberto#include <pwd.h> 5054359Sroberto#include <netdb.h> 51132451Sroberto#include <stdio.h> 52182007Sroberto#include <stdlib.h> 53132451Sroberto#include <string.h> 5454359Sroberto 55285612Sdelphij#include "hash.h" 5654359Sroberto 5754359Sroberto#ifndef lint 5854359Srobertostatic const char rcsid[] = "$FreeBSD: head/libexec/mknetid/mknetid.c 21673 1997-01-14 07:20:47Z jkh $"; 59285612Sdelphij#endif 60285612Sdelphij 6154359Sroberto#define LINSIZ 1024 6254359Sroberto#define OPSYS "unix" 6354359Sroberto 6454359Sroberto/* Default location of group file. */ 6554359Srobertochar *groupfile = _PATH_GROUP; 6654359Sroberto/* Default location of master.passwd file. */ 6754359Srobertochar *passfile = _PATH_PASSWD; 6854359Sroberto/* Default location of hosts file. */ 6954359Srobertochar *hostsfile = _PATH_HOSTS; 7054359Sroberto/* Default location of netid file */ 7154359Srobertochar *netidfile = "/etc/netid"; 7254359Sroberto 7354359Sroberto/* 7454359Sroberto * Stored hash table of 'reverse' group member database 7554359Sroberto * which we will construct. 7654359Sroberto */ 7754359Srobertostruct member_entry *mtable[TABLESIZE]; 7854359Sroberto 7954359Sroberto/* 8054359Sroberto * Dupe table: used to keep track of entries so we don't 8154359Sroberto * print the same thing twice. 8254359Sroberto */ 8354359Srobertostruct member_entry *dtable[TABLESIZE]; 8454359Sroberto 8554359Srobertoextern struct group *_getgrent __P(( void )); 8654359Srobertoextern int _setgrent __P(( void )); 8754359Srobertoextern void _endgrent __P(( void )); 8854359Srobertovoid usage(prog) 8954359Srobertochar *prog; 9054359Sroberto{ 9154359Sroberto fprintf (stderr,"usage: %s [-q] [-g group file] [-p passwd file] \ 92309008Sdelphij[-h hosts file]\n\t\t\t[-d netid file] [-d domain]\n",prog); 93309008Sdelphij exit(1); 94309008Sdelphij} 95309008Sdelphij 96309008Sdelphijextern char *optarg; 9754359Srobertoextern FILE *_gr_fp; 9854359Sroberto 9954359Srobertomain(argc, argv) 10054359Sroberto int argc; 10154359Sroberto char *argv[]; 10254359Sroberto{ 10354359Sroberto FILE *gfp, *pfp, *hfp, *nfp; 10454359Sroberto char readbuf[LINSIZ]; 10554359Sroberto char writebuf[LINSIZ]; 10654359Sroberto struct group *gr; 10754359Sroberto struct grouplist *glist; 10854359Sroberto char *domain; 109132451Sroberto int ch; 110132451Sroberto gid_t i; 11154359Sroberto char *ptr, *pidptr, *gidptr, *hptr; 11254359Sroberto int quiet = 0; 113132451Sroberto 114132451Sroberto while ((ch = getopt(argc, argv, "g:p:h:n:d:q")) != EOF) { 115132451Sroberto switch(ch) { 116132451Sroberto case 'g': 117132451Sroberto groupfile = optarg; 118182007Sroberto break; 119132451Sroberto case 'p': 120132451Sroberto passfile = optarg; 121132451Sroberto break; 122132451Sroberto case 'h': 123132451Sroberto hostsfile = optarg; 12454359Sroberto break; 12554359Sroberto case 'n': 126132451Sroberto netidfile = optarg; 12754359Sroberto break; 12854359Sroberto case 'd': 12954359Sroberto domain = optarg; 130132451Sroberto break; 13154359Sroberto case 'q': 13254359Sroberto quiet++; 13354359Sroberto break; 13454359Sroberto default: 13554359Sroberto usage(argv[0]); 13654359Sroberto break; 13754359Sroberto } 138285612Sdelphij } 139285612Sdelphij 140285612Sdelphij if (domain == NULL) { 141285612Sdelphij if (yp_get_default_domain(&domain)) 142338531Sdelphij errx(1, "no domain name specified and default \ 143285612Sdelphijdomain not set"); 144285612Sdelphij } 145285612Sdelphij 146285612Sdelphij if ((gfp = fopen(groupfile, "r")) == NULL) { 147285612Sdelphij err(1, "%s", groupfile); 148285612Sdelphij } 149132451Sroberto 150285612Sdelphij if ((pfp = fopen(passfile, "r")) == NULL) { 151285612Sdelphij err(1, "%s", passfile); 152132451Sroberto } 153285612Sdelphij 154285612Sdelphij if ((hfp = fopen(hostsfile, "r")) == NULL) { 155285612Sdelphij err(1, "%s", hostsfile); 156285612Sdelphij } 157285612Sdelphij 158285612Sdelphij if ((nfp = fopen(netidfile, "r")) == NULL) { 159285612Sdelphij /* netid is optional -- just continue */ 16054359Sroberto nfp = NULL; 161309008Sdelphij } 162309008Sdelphij 163309008Sdelphij _gr_fp = gfp; 164309008Sdelphij 16554359Sroberto /* Load all the group membership info into a hash table. */ 16654359Sroberto 16754359Sroberto _setgrent(); 16854359Sroberto while((gr = _getgrent()) != NULL) { 16954359Sroberto while(*gr->gr_mem) { 17054359Sroberto mstore(mtable, *gr->gr_mem, gr->gr_gid, 0); 17154359Sroberto gr->gr_mem++; 172132451Sroberto } 17354359Sroberto } 17454359Sroberto 17554359Sroberto fclose(gfp); 17654359Sroberto _endgrent(); 17754359Sroberto 17854359Sroberto /* 17954359Sroberto * Now parse the passwd database, spewing out the extra 18054359Sroberto * group information we just stored if necessary. 18154359Sroberto */ 18254359Sroberto while(fgets(readbuf, LINSIZ, pfp)) { 183132451Sroberto if ((ptr = strchr(readbuf, ':')) == NULL) 184132451Sroberto warnx("bad passwd file entry: %s", readbuf); 18554359Sroberto *ptr = '\0'; 18654359Sroberto ptr++; 18754359Sroberto if ((ptr = strchr(ptr, ':')) == NULL) 188132451Sroberto warnx("bad passwd file entry: %s", readbuf); 189285612Sdelphij *ptr = '\0'; 19054359Sroberto ptr++; 19154359Sroberto pidptr = ptr; 19254359Sroberto if ((ptr = strchr(ptr, ':')) == NULL) 19354359Sroberto warnx("bad passwd file entry: %s", readbuf); 19454359Sroberto *ptr = '\0'; 195285612Sdelphij ptr++; 196132451Sroberto gidptr = ptr; 197285612Sdelphij if ((ptr = strchr(ptr, ':')) == NULL) 198285612Sdelphij warnx("bad passwd file entry: %s", readbuf); 199285612Sdelphij *ptr = '\0'; 20054359Sroberto i = atol(gidptr); 20154359Sroberto 20254359Sroberto snprintf(writebuf, sizeof(writebuf), "%s.%s@%s", OPSYS, 20354359Sroberto pidptr, domain); 204285612Sdelphij 205132451Sroberto if (lookup(dtable, writebuf)) { 20654359Sroberto if (!quiet) 20754359Sroberto warnx("duplicate netid '%s.%s@%s' -- skipping", 208285612Sdelphij OPSYS, pidptr, domain); 20954359Sroberto continue; 21054359Sroberto } else { 21154359Sroberto mstore(dtable, writebuf, 0, 1); 212285612Sdelphij } 213285612Sdelphij printf("%s.%s@%s %s:%s", OPSYS, pidptr, domain, pidptr, gidptr); 214132451Sroberto if ((glist = lookup(mtable, (char *)&readbuf)) != NULL) { 21554359Sroberto while(glist) { 21654359Sroberto if (glist->groupid != i) 217285612Sdelphij printf(",%lu", glist->groupid); 21854359Sroberto glist = glist->next; 21954359Sroberto } 22054359Sroberto } 22154359Sroberto printf ("\n"); 22254359Sroberto } 22354359Sroberto 22454359Sroberto fclose(pfp); 22554359Sroberto 226132451Sroberto /* 227182007Sroberto * Now parse the hosts database (this part sucks). 228182007Sroberto */ 229132451Sroberto 230132451Sroberto while ((ptr = fgets(readbuf, LINSIZ, hfp))) { 231132451Sroberto if (*ptr == '#') 232132451Sroberto continue; 233132451Sroberto if (!(hptr = strpbrk(ptr, "#\n"))) 234132451Sroberto continue; 235132451Sroberto *hptr = '\0'; 236132451Sroberto if (!(hptr = strpbrk(ptr, " \t"))) 237132451Sroberto continue; 238182007Sroberto *hptr++ = '\0'; 239132451Sroberto ptr = hptr; 240132451Sroberto while (*ptr == ' ' || *ptr == '\t') 24154359Sroberto ptr++; 24254359Sroberto if (!(hptr = strpbrk(ptr, " \t"))) 243132451Sroberto continue; 244132451Sroberto *hptr++ = '\0'; 24554359Sroberto snprintf(writebuf, sizeof(writebuf), "%s.%s@%s", OPSYS, 24654359Sroberto ptr, domain); 247132451Sroberto if (lookup(dtable, (char *)&writebuf)) { 248132451Sroberto if (!quiet) 249132451Sroberto warnx("duplicate netid '%s' -- skipping", 250132451Sroberto writebuf); 251132451Sroberto continue; 25254359Sroberto } else { 25354359Sroberto mstore(dtable, (char *)&writebuf, 0, 1); 25454359Sroberto } 25554359Sroberto printf ("%s.%s@%s 0:%s\n", OPSYS, ptr, domain, ptr); 25654359Sroberto } 25754359Sroberto 258132451Sroberto fclose(hfp); 25954359Sroberto 260132451Sroberto /* 26154359Sroberto * Lastly, copy out any extra information in the netid 26254359Sroberto * file. If it's not open, just ignore it: it's optional anyway. 26354359Sroberto */ 264285612Sdelphij 265182007Sroberto if (nfp != NULL) { 266132451Sroberto while(fgets(readbuf, LINSIZ, nfp)) { 267132451Sroberto if (readbuf[0] == '#') 268132451Sroberto continue; 269132451Sroberto if ((ptr = strpbrk((char*)&readbuf, " \t")) == NULL) { 270132451Sroberto warnx("bad netid entry: '%s'", readbuf); 271132451Sroberto continue; 272132451Sroberto } 273132451Sroberto 274132451Sroberto writebuf[0] = *ptr; 275285612Sdelphij *ptr = '\0'; 276285612Sdelphij if (lookup(dtable, (char *)&readbuf)) { 277132451Sroberto if (!quiet) 27854359Sroberto warnx("duplicate netid '%s' -- skipping", 27954359Sroberto readbuf); 28054359Sroberto continue; 28154359Sroberto } else { 28254359Sroberto mstore(dtable, (char *)&readbuf, 0, 1); 283132451Sroberto } 284132451Sroberto *ptr = writebuf[0]; 28554359Sroberto printf("%s",readbuf); 286285612Sdelphij } 28754359Sroberto fclose(nfp); 28854359Sroberto } 28954359Sroberto 29054359Sroberto exit(0); 291132451Sroberto} 292132451Sroberto