2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1995, 1996
5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * netid map generator program
36 * Written by Bill Paul <wpaul@ctr.columbia.edu>
37 * Center for Telecommunications Research
38 * Columbia University, New York City
41 #include <sys/types.h>
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
59 static const char rcsid[] =
66 /* Default location of group file. */
67 char *groupfile = _PATH_GROUP;
68 /* Default location of master.passwd file. */
69 char *passfile = _PATH_PASSWD;
70 /* Default location of hosts file. */
71 char *hostsfile = _PATH_HOSTS;
72 /* Default location of netid file */
73 char *netidfile = "/etc/netid";
76 * Stored hash table of 'reverse' group member database
77 * which we will construct.
79 struct member_entry *mtable[TABLESIZE];
82 * Dupe table: used to keep track of entries so we don't
83 * print the same thing twice.
85 struct member_entry *dtable[TABLESIZE];
87 extern struct group *_getgrent(void);
88 extern int _setgrent(void);
89 extern void _endgrent(void);
94 fprintf (stderr, "%s\n%s\n",
95 "usage: mknetid [-q] [-g group_file] [-p passwd_file] [-h hosts_file]",
96 " [-n netid_file] [-d domain]");
103 main(int argc, char *argv[])
105 FILE *gfp, *pfp, *hfp, *nfp;
106 char readbuf[LINSIZ];
107 char writebuf[LINSIZ];
109 struct grouplist *glist;
113 char *ptr, *pidptr, *gidptr, *hptr;
117 while ((ch = getopt(argc, argv, "g:p:h:n:d:q")) != -1) {
143 if (domain == NULL) {
144 if (yp_get_default_domain(&domain))
145 errx(1, "no domain name specified and default \
149 if ((gfp = fopen(groupfile, "r")) == NULL) {
150 err(1, "%s", groupfile);
153 if ((pfp = fopen(passfile, "r")) == NULL) {
154 err(1, "%s", passfile);
157 if ((hfp = fopen(hostsfile, "r")) == NULL) {
158 err(1, "%s", hostsfile);
161 if ((nfp = fopen(netidfile, "r")) == NULL) {
162 /* netid is optional -- just continue */
168 /* Load all the group membership info into a hash table. */
171 while((gr = _getgrent()) != NULL) {
173 mstore(mtable, *gr->gr_mem, gr->gr_gid, 0);
182 * Now parse the passwd database, spewing out the extra
183 * group information we just stored if necessary.
185 while(fgets(readbuf, LINSIZ, pfp)) {
186 /* Ignore comments: ^[ \t]*# */
187 for (ptr = readbuf; *ptr != '\0'; ptr++)
188 if (*ptr != ' ' && *ptr != '\t')
190 if (*ptr == '#' || *ptr == '\0')
192 if ((ptr = strchr(readbuf, ':')) == NULL) {
193 warnx("bad passwd file entry: %s", readbuf);
198 if ((ptr = strchr(ptr, ':')) == NULL) {
199 warnx("bad passwd file entry: %s", readbuf);
205 if ((ptr = strchr(ptr, ':')) == NULL) {
206 warnx("bad passwd file entry: %s", readbuf);
212 if ((ptr = strchr(ptr, ':')) == NULL) {
213 warnx("bad passwd file entry: %s", readbuf);
219 snprintf(writebuf, sizeof(writebuf), "%s.%s@%s", OPSYS,
222 if (lookup(dtable, writebuf)) {
224 warnx("duplicate netid '%s.%s@%s' -- skipping",
225 OPSYS, pidptr, domain);
228 mstore(dtable, writebuf, 0, 1);
230 printf("%s.%s@%s %s:%s", OPSYS, pidptr, domain, pidptr, gidptr);
231 if ((glist = lookup(mtable, (char *)&readbuf)) != NULL) {
233 if (glist->groupid != i)
234 printf(",%lu", (u_long)glist->groupid);
244 * Now parse the hosts database (this part sucks).
247 while ((ptr = fgets(readbuf, LINSIZ, hfp))) {
250 if (!(hptr = strpbrk(ptr, "#\n")))
253 if (!(hptr = strpbrk(ptr, " \t")))
257 while (*ptr == ' ' || *ptr == '\t')
259 if (!(hptr = strpbrk(ptr, " \t")))
262 snprintf(writebuf, sizeof(writebuf), "%s.%s@%s", OPSYS,
264 if (lookup(dtable, (char *)&writebuf)) {
266 warnx("duplicate netid '%s' -- skipping",
270 mstore(dtable, (char *)&writebuf, 0, 1);
272 printf ("%s.%s@%s 0:%s\n", OPSYS, ptr, domain, ptr);
278 * Lastly, copy out any extra information in the netid
279 * file. If it's not open, just ignore it: it's optional anyway.
283 while(fgets(readbuf, LINSIZ, nfp)) {
284 if (readbuf[0] == '#')
286 if ((ptr = strpbrk((char*)&readbuf, " \t")) == NULL) {
287 warnx("bad netid entry: '%s'", readbuf);
293 if (lookup(dtable, (char *)&readbuf)) {
295 warnx("duplicate netid '%s' -- skipping",
299 mstore(dtable, (char *)&readbuf, 0, 1);
302 printf("%s",readbuf);