xref: /illumos-gate/usr/src/cmd/ypcmd/nick.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1986-1992 by Sun Microsystems Inc.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #include <stdio.h>
27*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
28*7c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
29*7c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
30*7c478bd9Sstevel@tonic-gate #include <string.h>
31*7c478bd9Sstevel@tonic-gate #include <unistd.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #define	MAXMAPS 500
34*7c478bd9Sstevel@tonic-gate /* number of nickname file entries arbitrarily limited */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate static char NICKFILE[] = "/var/yp/nicknames";
37*7c478bd9Sstevel@tonic-gate static char *transtable[MAXMAPS];
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * This will read nicknames from file /var/yp/nicknames
41*7c478bd9Sstevel@tonic-gate  * and print it out or save it for future use.
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate void
maketable(dump)44*7c478bd9Sstevel@tonic-gate maketable(dump)
45*7c478bd9Sstevel@tonic-gate int dump;
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate 	FILE *nickp;
48*7c478bd9Sstevel@tonic-gate 	int i = 0;
49*7c478bd9Sstevel@tonic-gate 	char nickbuf[2*YPMAXMAP+3], nickname[YPMAXMAP+1], mapname[YPMAXMAP+1];
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 	if ((nickp = fopen(NICKFILE, "r")) == NULL) {
52*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "nickname file %s does not exist\n",
53*7c478bd9Sstevel@tonic-gate 			NICKFILE);
54*7c478bd9Sstevel@tonic-gate 		exit(1);
55*7c478bd9Sstevel@tonic-gate 	}
56*7c478bd9Sstevel@tonic-gate 	while (fgets(nickbuf, YPMAXMAP, nickp) != NULL) {
57*7c478bd9Sstevel@tonic-gate 		if (strchr(nickbuf, '\n') == NULL) {
58*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
59*7c478bd9Sstevel@tonic-gate 				"garbled nickname file %s\n", NICKFILE);
60*7c478bd9Sstevel@tonic-gate 			exit(1);
61*7c478bd9Sstevel@tonic-gate 		}
62*7c478bd9Sstevel@tonic-gate 		(void) memset(nickname, 0, YPMAXMAP+1);
63*7c478bd9Sstevel@tonic-gate 		(void) memset(mapname, 0, YPMAXMAP+1);
64*7c478bd9Sstevel@tonic-gate 		if (sscanf(nickbuf, "%s %s\n", nickname, mapname) != 2) {
65*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
66*7c478bd9Sstevel@tonic-gate 				"garbled nickname file %s\n", NICKFILE);
67*7c478bd9Sstevel@tonic-gate 			exit(1);
68*7c478bd9Sstevel@tonic-gate 		}
69*7c478bd9Sstevel@tonic-gate 		if (!dump) {
70*7c478bd9Sstevel@tonic-gate 			transtable[i] = strdup(nickname);
71*7c478bd9Sstevel@tonic-gate 			transtable[i+1] = strdup(mapname);
72*7c478bd9Sstevel@tonic-gate 			i += 2;
73*7c478bd9Sstevel@tonic-gate 			transtable[i] = NULL;
74*7c478bd9Sstevel@tonic-gate 		} else {
75*7c478bd9Sstevel@tonic-gate 			printf("Use \"%s\"\tfor map \"%s\"\n",
76*7c478bd9Sstevel@tonic-gate 				nickname, mapname);
77*7c478bd9Sstevel@tonic-gate 		}
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 	fclose(nickp);
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate /*
83*7c478bd9Sstevel@tonic-gate  * This will get the mapname for a given nickname from the file.
84*7c478bd9Sstevel@tonic-gate  */
85*7c478bd9Sstevel@tonic-gate int
getmapname(nick,map)86*7c478bd9Sstevel@tonic-gate getmapname(nick, map)
87*7c478bd9Sstevel@tonic-gate char *nick, *map;
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	FILE *nickp;
90*7c478bd9Sstevel@tonic-gate 	char nickbuf[2*YPMAXMAP+3], nickname[YPMAXMAP+1];
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	if ((nickp = fopen(NICKFILE, "r")) == NULL)
93*7c478bd9Sstevel@tonic-gate 		return (0);
94*7c478bd9Sstevel@tonic-gate 	while (fgets(nickbuf, YPMAXMAP, nickp) != NULL) {
95*7c478bd9Sstevel@tonic-gate 		if (strchr(nickbuf, '\n') == NULL) {
96*7c478bd9Sstevel@tonic-gate 			fclose(nickp);
97*7c478bd9Sstevel@tonic-gate 			return (0);
98*7c478bd9Sstevel@tonic-gate 		}
99*7c478bd9Sstevel@tonic-gate 		(void) memset(nickname, 0, YPMAXMAP+1);
100*7c478bd9Sstevel@tonic-gate 		(void) memset(map, 0, YPMAXMAP+1);
101*7c478bd9Sstevel@tonic-gate 		if (sscanf(nickbuf, "%s %s\n", nickname, map) != 2) {
102*7c478bd9Sstevel@tonic-gate 			fclose(nickp);
103*7c478bd9Sstevel@tonic-gate 			return (0);
104*7c478bd9Sstevel@tonic-gate 		}
105*7c478bd9Sstevel@tonic-gate 		if (strcmp(nick, nickname) == 0) {
106*7c478bd9Sstevel@tonic-gate 			fclose(nickp);
107*7c478bd9Sstevel@tonic-gate 			return (1);
108*7c478bd9Sstevel@tonic-gate 		}
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate 	fclose(nickp);
111*7c478bd9Sstevel@tonic-gate 	return (0);
112*7c478bd9Sstevel@tonic-gate }
113