1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28*0Sstevel@tonic-gate /*	  All Rights Reserved   */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
32*0Sstevel@tonic-gate  * under license from the Regents of the University of
33*0Sstevel@tonic-gate  * California.
34*0Sstevel@tonic-gate  */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate  * DESCRIPTION:	This file contains various functions used by more than one NIS
40*0Sstevel@tonic-gate  *		components. A lot of this code started off in ypxfr and then
41*0Sstevel@tonic-gate  *		got used by other components. Some of it has become a little
42*0Sstevel@tonic-gate  *		'quirky' and should probably be re-worked.
43*0Sstevel@tonic-gate  */
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include <unistd.h>
46*0Sstevel@tonic-gate #include <syslog.h>
47*0Sstevel@tonic-gate #include <sys/mman.h>
48*0Sstevel@tonic-gate #include <thread.h>
49*0Sstevel@tonic-gate #include <synch.h>
50*0Sstevel@tonic-gate #include <stdarg.h>
51*0Sstevel@tonic-gate #include <ndbm.h>
52*0Sstevel@tonic-gate #include "../ypsym.h"
53*0Sstevel@tonic-gate #include "../ypdefs.h"
54*0Sstevel@tonic-gate #include "shim.h"
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate USE_DBM
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /*
59*0Sstevel@tonic-gate  * Globals
60*0Sstevel@tonic-gate  */
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * DESCRIPTION : Utility functions used by everything.
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate bool check_map_existence(char *);
66*0Sstevel@tonic-gate void logprintf2(char *format, ...);
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * This checks to see if the source map files exist, then renames them to the
70*0Sstevel@tonic-gate  * target names.  This is a boolean function.  The file names from.pag and
71*0Sstevel@tonic-gate  * from.dir will be changed to to.pag and to.dir in the success case.
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  * Note:  If the second of the two renames fails, yprename_map will try to
74*0Sstevel@tonic-gate  * un-rename the first pair, and leave the world in the state it was on entry.
75*0Sstevel@tonic-gate  * This might fail, too, though...
76*0Sstevel@tonic-gate  *
77*0Sstevel@tonic-gate  * GIVEN :	Name of map to copy from
78*0Sstevel@tonic-gate  *		Name of map to copy to
79*0Sstevel@tonic-gate  *		Flag indicating if map is secure.
80*0Sstevel@tonic-gate  */
81*0Sstevel@tonic-gate bool
82*0Sstevel@tonic-gate rename_map(from, to, secure_map)
83*0Sstevel@tonic-gate 	char *from;
84*0Sstevel@tonic-gate 	char *to;
85*0Sstevel@tonic-gate 	bool_t secure_map;
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate 	char fromfile[MAXNAMLEN + 1];
88*0Sstevel@tonic-gate 	char tofile[MAXNAMLEN + 1];
89*0Sstevel@tonic-gate 	char savefile[MAXNAMLEN + 1];
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if (!from || !to) {
92*0Sstevel@tonic-gate 		return (FALSE);
93*0Sstevel@tonic-gate 	}
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	if (!check_map_existence(from)) {
96*0Sstevel@tonic-gate 		return (FALSE);
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	(void) strcpy(fromfile, from);
100*0Sstevel@tonic-gate 	(void) strcat(fromfile, dbm_pag);
101*0Sstevel@tonic-gate 	(void) strcpy(tofile, to);
102*0Sstevel@tonic-gate 	(void) strcat(tofile, dbm_pag);
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	if (rename(fromfile, tofile)) {
105*0Sstevel@tonic-gate 		logprintf2("Can't mv %s to %s.\n", fromfile,
106*0Sstevel@tonic-gate 		    tofile);
107*0Sstevel@tonic-gate 		return (FALSE);
108*0Sstevel@tonic-gate 	}
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	(void) strcpy(savefile, tofile);
111*0Sstevel@tonic-gate 	(void) strcpy(fromfile, from);
112*0Sstevel@tonic-gate 	(void) strcat(fromfile, dbm_dir);
113*0Sstevel@tonic-gate 	(void) strcpy(tofile, to);
114*0Sstevel@tonic-gate 	(void) strcat(tofile, dbm_dir);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	if (rename(fromfile, tofile)) {
117*0Sstevel@tonic-gate 		logprintf2("Can't mv %s to %s.\n", fromfile,
118*0Sstevel@tonic-gate 		    tofile);
119*0Sstevel@tonic-gate 		(void) strcpy(fromfile, from);
120*0Sstevel@tonic-gate 		(void) strcat(fromfile, dbm_pag);
121*0Sstevel@tonic-gate 		(void) strcpy(tofile, to);
122*0Sstevel@tonic-gate 		(void) strcat(tofile, dbm_pag);
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 		if (rename(tofile, fromfile)) {
125*0Sstevel@tonic-gate 			logprintf2(
126*0Sstevel@tonic-gate 			    "Can't recover from rename failure.\n");
127*0Sstevel@tonic-gate 			return (FALSE);
128*0Sstevel@tonic-gate 		}
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 		return (FALSE);
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	if (!secure_map) {
134*0Sstevel@tonic-gate 		chmod(savefile, 0644);
135*0Sstevel@tonic-gate 		chmod(tofile, 0644);
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	return (TRUE);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /*
142*0Sstevel@tonic-gate  * Function :	delete_map()
143*0Sstevel@tonic-gate  *
144*0Sstevel@tonic-gate  * Description:	Deletes a map
145*0Sstevel@tonic-gate  *
146*0Sstevel@tonic-gate  * Given :	Map name
147*0Sstevel@tonic-gate  *
148*0Sstevel@tonic-gate  * Return :	TRUE = Map deleted
149*0Sstevel@tonic-gate  *		FALSE = Map not completly deleted
150*0Sstevel@tonic-gate  */
151*0Sstevel@tonic-gate bool
152*0Sstevel@tonic-gate delete_map(name)
153*0Sstevel@tonic-gate 	char *name;
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate 	char fromfile[MAXNAMLEN + 1];
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	if (!name) {
158*0Sstevel@tonic-gate 		return (FALSE);
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	if (!check_map_existence(name)) {
162*0Sstevel@tonic-gate 		/* Already gone */
163*0Sstevel@tonic-gate 		return (TRUE);
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	(void) strcpy(fromfile, name);
167*0Sstevel@tonic-gate 	(void) strcat(fromfile, dbm_pag);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	if (unlink(fromfile)) {
170*0Sstevel@tonic-gate 		logprintf2("Can't unlink %s.\n", fromfile);
171*0Sstevel@tonic-gate 		return (FALSE);
172*0Sstevel@tonic-gate 	}
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	(void) strcpy(fromfile, name);
175*0Sstevel@tonic-gate 	(void) strcat(fromfile, dbm_dir);
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	if (unlink(fromfile)) {
178*0Sstevel@tonic-gate 		logprintf2("Can't unlink %s.\n", fromfile);
179*0Sstevel@tonic-gate 		return (FALSE);
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	return (TRUE);
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate  * This performs an existence check on the dbm data base files <pname>.pag and
187*0Sstevel@tonic-gate  * <pname>.dir.
188*0Sstevel@tonic-gate  */
189*0Sstevel@tonic-gate bool
190*0Sstevel@tonic-gate check_map_existence(pname)
191*0Sstevel@tonic-gate 	char *pname;
192*0Sstevel@tonic-gate {
193*0Sstevel@tonic-gate 	char dbfile[MAXNAMLEN + 1];
194*0Sstevel@tonic-gate 	struct stat filestat;
195*0Sstevel@tonic-gate 	int len;
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	if (!pname || ((len = strlen(pname)) == 0) ||
198*0Sstevel@tonic-gate 	    (len + 5) > (MAXNAMLEN + 1)) {
199*0Sstevel@tonic-gate 		return (FALSE);
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	errno = 0;
203*0Sstevel@tonic-gate 	(void) strcpy(dbfile, pname);
204*0Sstevel@tonic-gate 	(void) strcat(dbfile, dbm_dir);
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	if (stat(dbfile, &filestat) != -1) {
207*0Sstevel@tonic-gate 		(void) strcpy(dbfile, pname);
208*0Sstevel@tonic-gate 		(void) strcat(dbfile, dbm_pag);
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 		if (stat(dbfile, &filestat) != -1) {
211*0Sstevel@tonic-gate 			return (TRUE);
212*0Sstevel@tonic-gate 		} else {
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 			if (errno != ENOENT) {
215*0Sstevel@tonic-gate 				logprintf2(
216*0Sstevel@tonic-gate 				    "Stat error on map file %s.\n",
217*0Sstevel@tonic-gate 				    dbfile);
218*0Sstevel@tonic-gate 			}
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 			return (FALSE);
221*0Sstevel@tonic-gate 		}
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	} else {
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 		if (errno != ENOENT) {
226*0Sstevel@tonic-gate 			logprintf2(
227*0Sstevel@tonic-gate 			    "Stat error on map file %s.\n",
228*0Sstevel@tonic-gate 			    dbfile);
229*0Sstevel@tonic-gate 		}
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 		return (FALSE);
232*0Sstevel@tonic-gate 	}
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate /*
236*0Sstevel@tonic-gate  * FUNCTION :	logprintf2()
237*0Sstevel@tonic-gate  *
238*0Sstevel@tonic-gate  * DESCRIPTION:	The functions in this file were oringinaly shared between
239*0Sstevel@tonic-gate  *		ypxfr and ypserv. On error they called logprintf().
240*0Sstevel@tonic-gate  *		Unfortunatly this had been implemented differently in the two
241*0Sstevel@tonic-gate  *		sources and not at all in some of the NIS components required
242*0Sstevel@tonic-gate  *		for N2L.
243*0Sstevel@tonic-gate  *
244*0Sstevel@tonic-gate  *		This function is simplified version of logprinf() as/when
245*0Sstevel@tonic-gate  *		possible the other error calls should be migrated to use this
246*0Sstevel@tonic-gate  *		common version. If a common set of functionality can be found
247*0Sstevel@tonic-gate  *		this versions should be modified to support it.
248*0Sstevel@tonic-gate  */
249*0Sstevel@tonic-gate void
250*0Sstevel@tonic-gate logprintf2(char *format, ...)
251*0Sstevel@tonic-gate {
252*0Sstevel@tonic-gate 	va_list ap;
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	va_start(ap, format);
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	syslog(LOG_ERR, format, ap);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	va_end(ap);
259*0Sstevel@tonic-gate }
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate /*
262*0Sstevel@tonic-gate  * This performs an existence check on the dbm data base files <name>.pag and
263*0Sstevel@tonic-gate  * <name>.dir.  pname is a ptr to the filename.  This should be an absolute
264*0Sstevel@tonic-gate  * path.
265*0Sstevel@tonic-gate  * Returns TRUE if the map exists and is accessable; else FALSE.
266*0Sstevel@tonic-gate  *
267*0Sstevel@tonic-gate  * Note:  The file name should be a "base" form, without a file "extension" of
268*0Sstevel@tonic-gate  * .dir or .pag appended.  See ypmkfilename for a function which will generate
269*0Sstevel@tonic-gate  * the name correctly.  Errors in the stat call will be reported at this level,
270*0Sstevel@tonic-gate  * however, the non-existence of a file is not considered an error, and so will
271*0Sstevel@tonic-gate  * not be reported.
272*0Sstevel@tonic-gate  */
273*0Sstevel@tonic-gate bool
274*0Sstevel@tonic-gate ypcheck_map_existence(char *pname)
275*0Sstevel@tonic-gate {
276*0Sstevel@tonic-gate 	char dbfile[MAXNAMLEN + sizeof (TTL_POSTFIX) + 1];
277*0Sstevel@tonic-gate 	struct stat filestat;
278*0Sstevel@tonic-gate 	int len;
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	if (!pname || ((len = (int)strlen(pname)) == 0) ||
281*0Sstevel@tonic-gate 		(len + sizeof (dbm_pag)) > (MAXNAMLEN + 1)) {
282*0Sstevel@tonic-gate 		return (FALSE);
283*0Sstevel@tonic-gate 	}
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	errno = 0;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	/* Check for existance of .dir file */
288*0Sstevel@tonic-gate 	(void) strcpy(dbfile, pname);
289*0Sstevel@tonic-gate 	(void) strcat(dbfile, dbm_dir);
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	if (stat(dbfile, &filestat) == -1) {
292*0Sstevel@tonic-gate 		if (errno != ENOENT) {
293*0Sstevel@tonic-gate 			(void) fprintf(stderr,
294*0Sstevel@tonic-gate 				"ypserv:  Stat error on map file %s.\n",
295*0Sstevel@tonic-gate 									dbfile);
296*0Sstevel@tonic-gate 		}
297*0Sstevel@tonic-gate 		return (FALSE);
298*0Sstevel@tonic-gate 	}
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	/* Check for existance of .pag file */
301*0Sstevel@tonic-gate 	(void) strcpy(dbfile, pname);
302*0Sstevel@tonic-gate 	(void) strcat(dbfile, dbm_pag);
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	if (stat(dbfile, &filestat) == -1) {
305*0Sstevel@tonic-gate 		if (errno != ENOENT) {
306*0Sstevel@tonic-gate 			(void) fprintf(stderr,
307*0Sstevel@tonic-gate 				"ypserv:  Stat error on map file %s.\n",
308*0Sstevel@tonic-gate 									dbfile);
309*0Sstevel@tonic-gate 		    }
310*0Sstevel@tonic-gate 		return (FALSE);
311*0Sstevel@tonic-gate 	}
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	if (yptol_mode) {
314*0Sstevel@tonic-gate 		/* Check for existance of TTL .dir file */
315*0Sstevel@tonic-gate 		(void) strcpy(dbfile, pname);
316*0Sstevel@tonic-gate 		(void) strcat(dbfile, TTL_POSTFIX);
317*0Sstevel@tonic-gate 		(void) strcat(dbfile, dbm_dir);
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 		if (stat(dbfile, &filestat) == -1) {
320*0Sstevel@tonic-gate 			if (errno != ENOENT) {
321*0Sstevel@tonic-gate 				(void) fprintf(stderr,
322*0Sstevel@tonic-gate 					"ypserv:  Stat error on map file %s.\n",
323*0Sstevel@tonic-gate 									dbfile);
324*0Sstevel@tonic-gate 			}
325*0Sstevel@tonic-gate 			return (FALSE);
326*0Sstevel@tonic-gate 		}
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 		/* Check for existance of TTL .pag file */
329*0Sstevel@tonic-gate 		(void) strcpy(dbfile, pname);
330*0Sstevel@tonic-gate 		(void) strcat(dbfile, TTL_POSTFIX);
331*0Sstevel@tonic-gate 		(void) strcat(dbfile, dbm_pag);
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 		if (stat(dbfile, &filestat) == -1) {
334*0Sstevel@tonic-gate 			if (errno != ENOENT) {
335*0Sstevel@tonic-gate 				(void) fprintf(stderr,
336*0Sstevel@tonic-gate 					"ypserv:  Stat error on map file %s.\n",
337*0Sstevel@tonic-gate 									dbfile);
338*0Sstevel@tonic-gate 			    }
339*0Sstevel@tonic-gate 			    return (FALSE);
340*0Sstevel@tonic-gate 		}
341*0Sstevel@tonic-gate 	}
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	return (TRUE);
344*0Sstevel@tonic-gate }
345