xref: /illumos-gate/usr/src/cmd/ypcmd/yp_getalias.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  *
22  * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /*	  All Rights Reserved   */
28 
29 /*
30  * Portions of this source code were derived from Berkeley
31  * under license from the Regents of the University of
32  * California.
33  */
34 
35 #include <stdio.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/statvfs.h>
39 #include "ypsym.h"
40 /* this is 14 less the space for temps pids and .pag more or less */
41 
42 #ifdef DEBUG
43 #define	YPDBDIR		"/var/ypnew"
44 #define	ALIASLIST	"/var/ypnew/aliases"
45 #else
46 #define	YPDBDIR		"/var/yp"
47 #define	ALIASLIST	"/var/yp/aliases"
48 #endif
49 #define	issep(c) (c == ' ' || c == '\t')
50 
51 #ifdef SYSVCONFIG
52 #define	isvar_sysv() (wasitsysv)
53 
54 static int wasitsysv = TRUE;
55 static int first_time = TRUE;
56 static listofnames *list = NULL;
57 #endif
58 
59 void sysv_exit();
60 extern listofnames *names();
61 extern void exit();
62 extern void free_listofnames();
63 
64 /*
65  * Setup alias file, check /var/yp filesystem type
66  * Note: The *never* checks for aliases under Solaris, so there is no need
67  *	 for this function.  As of 1.1 beta 4, I will ifdef it out (cause
68  *	 you never know...).  Should go away completely soon.
69  */
70 
71 #ifdef SYSVCONFIG
72 void
73 sysvconfig(void)
74 {
75 	struct statvfs statbuf;
76 
77 	sigset(SIGCHLD, SIG_IGN);
78 	/*
79 	 * if neccesary free previous list, then read in aliaslist
80 	 */
81 
82 	if (!first_time)
83 		free_listofnames(list);
84 	else
85 		first_time = FALSE;
86 
87 	list = names(ALIASLIST);
88 
89 	/*
90 	 *	Check if YP database directory is in a system v filesystem
91 	 */
92 
93 	if (statvfs(YPDBDIR, &statbuf) != 0) {
94 		fprintf(stderr, "Cannot stat %s\n", YPDBDIR);
95 		exit(-1);
96 	} else {
97 		/* if (strcmp(statbuf.f_basetype,"s5")) (doesn't work in k13) */
98 		if (statbuf.f_namemax == 14)
99 			wasitsysv = TRUE;
100 		else
101 			wasitsysv = FALSE;
102 	}
103 	sigset(SIGCHLD, (void (*)())sysvconfig);
104 }
105 #endif
106 
107 /*
108  * Match key to alias
109  */
110 int
111 yp_getalias(key, key_alias, maxlen)
112 	char *key;
113 	char *key_alias;
114 	int maxlen;
115 {
116 	listofnames *entry;
117 	char *longname;
118 	char *alias;
119 	char name[256];
120 
121 #ifndef SYSVCONFIG
122 	strcpy(key_alias, key);
123 	return (0);
124 #else
125 	/* sysvconfig must be run before this routine */
126 	if (key == NULL || first_time)
127 		return (-1);
128 
129 	if (!isvar_sysv()) {
130 		strcpy(key_alias, key);
131 		return (0);
132 	}
133 
134 	for (entry = list, strcpy(name, entry->name); entry;
135 		entry = entry->nextname, strcpy(name, entry->name)) {
136 
137 		longname = strtok(name, " \t");
138 		alias = strtok(NULL, " \t\n");
139 		if (longname == NULL || alias == NULL) {
140 			continue;
141 		}
142 		if (strcmp(longname, key) == 0) {
143 			if ((int)strlen(alias) > (maxlen)) {
144 				strncpy(key_alias, alias, (maxlen));
145 				key_alias[maxlen] = '\0';
146 			} else {
147 				strcpy(key_alias, alias);
148 			}
149 			return (0);
150 		}
151 	}
152 	/* alias not found */
153 	return (-1);
154 #endif
155 }
156 
157 /*
158  * Match alias to key
159  */
160 int
161 yp_getkey(key_alias, key, maxlen)
162 	char *key_alias;
163 	char *key;
164 	int maxlen;
165 {
166 	listofnames *entry;
167 	char *longname;
168 	char *alias;
169 	char name[256];
170 
171 #ifndef SYSVCONFIG
172 	strcpy(key, key_alias);
173 	return (0);
174 #else
175 	if (key_alias == NULL || first_time) {
176 		return (-1);
177 	}
178 
179 	if (!isvar_sysv()) {
180 		strcpy(key, key_alias);
181 		return (0);
182 	}
183 
184 	for (entry = list, strcpy(name, entry->name);
185 		entry; entry = entry->nextname, strcpy(name, entry->name)) {
186 
187 		longname = strtok(name, " \t");
188 		alias = strtok(NULL, " \t\n");
189 		if (alias == NULL || longname == NULL) {
190 			continue;
191 		}
192 		if ((strcmp(alias, key_alias) == 0) ||
193 		    (strncmp(alias, key_alias, maxlen) == 0)) {
194 			strcpy(key, longname);
195 			return (0);
196 		}
197 	}
198 	/* key not found */
199 	return (-1);
200 #endif
201 }
202