xref: /illumos-gate/usr/src/cmd/ypcmd/yp_getalias.c (revision e4d060fb4c00d44cd578713eb9a921f594b733b8)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <stdio.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/statvfs.h>
41 #include "ypsym.h"
42 /* this is 14 less the space for temps pids and .pag more or less */
43 
44 #ifdef DEBUG
45 #define	YPDBDIR		"/var/ypnew"
46 #define	ALIASLIST	"/var/ypnew/aliases"
47 #else
48 #define	YPDBDIR		"/var/yp"
49 #define	ALIASLIST	"/var/yp/aliases"
50 #endif
51 #define	issep(c) (c == ' ' || c == '\t')
52 
53 #ifdef SYSVCONFIG
54 #define	isvar_sysv() (wasitsysv)
55 
56 static int wasitsysv = TRUE;
57 static int first_time = TRUE;
58 static listofnames *list = NULL;
59 #endif
60 
61 void sysv_exit();
62 extern listofnames *names();
63 extern void exit();
64 extern void free_listofnames();
65 
66 /*
67  * Setup alias file, check /var/yp filesystem type
68  * Note: The *never* checks for aliases under Solaris, so there is no need
69  *	 for this function.  As of 1.1 beta 4, I will ifdef it out (cause
70  *	 you never know...).  Should go away completely soon.
71  */
72 
73 #ifdef SYSVCONFIG
74 void
75 sysvconfig(void)
76 {
77 	struct statvfs statbuf;
78 
79 	sigset(SIGCHLD, SIG_IGN);
80 	/*
81 	 * if neccesary free previous list, then read in aliaslist
82 	 */
83 
84 	if (!first_time)
85 		free_listofnames(list);
86 	else
87 		first_time = FALSE;
88 
89 	list = names(ALIASLIST);
90 
91 	/*
92 	 *	Check if YP database directory is in a system v filesystem
93 	 */
94 
95 	if (statvfs(YPDBDIR, &statbuf) != 0) {
96 		fprintf(stderr, "Cannot stat %s\n", YPDBDIR);
97 		exit(-1);
98 	} else {
99 		/* if (strcmp(statbuf.f_basetype,"s5")) (doesn't work in k13) */
100 		if (statbuf.f_namemax == 14)
101 			wasitsysv = TRUE;
102 		else
103 			wasitsysv = FALSE;
104 	}
105 	sigset(SIGCHLD, (void (*)())sysvconfig);
106 }
107 #endif
108 
109 /*
110  * Match key to alias
111  */
112 int
113 yp_getalias(key, key_alias, maxlen)
114 	char *key;
115 	char *key_alias;
116 	int maxlen;
117 {
118 	listofnames *entry;
119 	char *longname;
120 	char *alias;
121 	char name[256];
122 
123 #ifndef SYSVCONFIG
124 	strcpy(key_alias, key);
125 	return (0);
126 #else
127 	/* sysvconfig must be run before this routine */
128 	if (key == NULL || first_time)
129 		return (-1);
130 
131 	if (!isvar_sysv()) {
132 		strcpy(key_alias, key);
133 		return (0);
134 	}
135 
136 	for (entry = list, strcpy(name, entry->name); entry;
137 		entry = entry->nextname, strcpy(name, entry->name)) {
138 
139 		longname = strtok(name, " \t");
140 		alias = strtok(NULL, " \t\n");
141 		if (longname == NULL || alias == NULL) {
142 			continue;
143 		}
144 		if (strcmp(longname, key) == 0) {
145 			if ((int)strlen(alias) > (maxlen)) {
146 				strncpy(key_alias, alias, (maxlen));
147 				key_alias[maxlen] = '\0';
148 			} else {
149 				strcpy(key_alias, alias);
150 			}
151 			return (0);
152 		}
153 	}
154 	/* alias not found */
155 	return (-1);
156 #endif
157 }
158 
159 /*
160  * Match alias to key
161  */
162 int
163 yp_getkey(key_alias, key, maxlen)
164 	char *key_alias;
165 	char *key;
166 	int maxlen;
167 {
168 	listofnames *entry;
169 	char *longname;
170 	char *alias;
171 	char name[256];
172 
173 #ifndef SYSVCONFIG
174 	strcpy(key, key_alias);
175 	return (0);
176 #else
177 	if (key_alias == NULL || first_time) {
178 		return (-1);
179 	}
180 
181 	if (!isvar_sysv()) {
182 		strcpy(key, key_alias);
183 		return (0);
184 	}
185 
186 	for (entry = list, strcpy(name, entry->name);
187 		entry; entry = entry->nextname, strcpy(name, entry->name)) {
188 
189 		longname = strtok(name, " \t");
190 		alias = strtok(NULL, " \t\n");
191 		if (alias == NULL || longname == NULL) {
192 			continue;
193 		}
194 		if ((strcmp(alias, key_alias) == 0) ||
195 		    (strncmp(alias, key_alias, maxlen) == 0)) {
196 			strcpy(key, longname);
197 			return (0);
198 		}
199 	}
200 	/* key not found */
201 	return (-1);
202 #endif
203 }
204