utils.c revision 7035:f93a1cefda5f
1228763Smm/*
2228753Smm * CDDL HEADER START
3228753Smm *
4228753Smm * The contents of this file are subject to the terms of the
5228753Smm * Common Development and Distribution License (the "License").
6228753Smm * You may not use this file except in compliance with the License.
7228753Smm *
8228753Smm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9228753Smm * or http://www.opensolaris.org/os/licensing.
10228753Smm * See the License for the specific language governing permissions
11228753Smm * and limitations under the License.
12228753Smm *
13228753Smm * When distributing Covered Code, include this CDDL HEADER in each
14228753Smm * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15228753Smm * If applicable, add the following below this CDDL HEADER, with the
16228753Smm * fields enclosed by brackets "[]" replaced with your own identifying
17228753Smm * information: Portions Copyright [yyyy] [name of copyright owner]
18228753Smm *
19228753Smm * CDDL HEADER END
20228753Smm */
21228753Smm/*
22228753Smm * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23228753Smm * Use is subject to license terms.
24228753Smm */
25228753Smm
26228753Smm/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27228753Smm/*	  All Rights Reserved   */
28228753Smm
29228753Smm/*
30228753Smm * Portions of this source code were derived from Berkeley
31228753Smm * under license from the Regents of the University of
32228753Smm * California.
33228753Smm */
34228753Smm
35228753Smm#pragma ident	"%Z%%M%	%I%	%E% SMI"
36228753Smm
37228753Smm/*
38228753Smm * DESCRIPTION:	This file contains various functions used by more than one NIS
39228753Smm *		components. A lot of this code started off in ypxfr and then
40228753Smm *		got used by other components. Some of it has become a little
41228753Smm *		'quirky' and should probably be re-worked.
42228753Smm */
43228753Smm
44228753Smm#include <unistd.h>
45228753Smm#include <syslog.h>
46228753Smm#include <sys/mman.h>
47228753Smm#include <thread.h>
48228753Smm#include <synch.h>
49228753Smm#include <stdarg.h>
50228753Smm#include <ndbm.h>
51228753Smm#include "../ypsym.h"
52228753Smm#include "../ypdefs.h"
53228753Smm#include "shim.h"
54228753Smm
55228753SmmUSE_DBM
56228753Smm
57228753Smm/*
58228753Smm * Globals
59228753Smm */
60228753Smm
61228753Smm/*
62228753Smm * DESCRIPTION : Utility functions used by everything.
63228753Smm */
64228753Smmbool check_map_existence(char *);
65228753Smmvoid logprintf2(char *format, ...);
66228753Smmextern bool ypcheck_map_existence_yptol();
67228753Smm
68228753Smm/*
69228753Smm * This checks to see if the source map files exist, then renames them to the
70228753Smm * target names.  This is a boolean function.  The file names from.pag and
71228753Smm * from.dir will be changed to to.pag and to.dir in the success case.
72228753Smm *
73228753Smm * Note:  If the second of the two renames fails, yprename_map will try to
74228753Smm * un-rename the first pair, and leave the world in the state it was on entry.
75228753Smm * This might fail, too, though...
76228753Smm *
77228753Smm * GIVEN :	Name of map to copy from
78228753Smm *		Name of map to copy to
79228753Smm *		Flag indicating if map is secure.
80228753Smm */
81228753Smmbool
82228753Smmrename_map(from, to, secure_map)
83228753Smm	char *from;
84228753Smm	char *to;
85228753Smm	bool_t secure_map;
86228753Smm{
87228753Smm	char fromfile[MAXNAMLEN + 1];
88228753Smm	char tofile[MAXNAMLEN + 1];
89228753Smm	char savefile[MAXNAMLEN + 1];
90228753Smm
91228753Smm	if (!from || !to) {
92228753Smm		return (FALSE);
93228753Smm	}
94228753Smm
95228753Smm	if (!check_map_existence(from)) {
96228753Smm		return (FALSE);
97228753Smm	}
98228753Smm
99228753Smm	(void) strcpy(fromfile, from);
100228753Smm	(void) strcat(fromfile, dbm_pag);
101228753Smm	(void) strcpy(tofile, to);
102228753Smm	(void) strcat(tofile, dbm_pag);
103228753Smm
104228753Smm	if (rename(fromfile, tofile)) {
105228753Smm		logprintf2("Can't mv %s to %s.\n", fromfile,
106228753Smm		    tofile);
107228753Smm		return (FALSE);
108228753Smm	}
109228753Smm
110228753Smm	(void) strcpy(savefile, tofile);
111228753Smm	(void) strcpy(fromfile, from);
112228753Smm	(void) strcat(fromfile, dbm_dir);
113228753Smm	(void) strcpy(tofile, to);
114228753Smm	(void) strcat(tofile, dbm_dir);
115228753Smm
116228753Smm	if (rename(fromfile, tofile)) {
117228753Smm		logprintf2("Can't mv %s to %s.\n", fromfile,
118228753Smm		    tofile);
119228753Smm		(void) strcpy(fromfile, from);
120228753Smm		(void) strcat(fromfile, dbm_pag);
121228753Smm		(void) strcpy(tofile, to);
122228753Smm		(void) strcat(tofile, dbm_pag);
123228753Smm
124228753Smm		if (rename(tofile, fromfile)) {
125228753Smm			logprintf2(
126228753Smm			    "Can't recover from rename failure.\n");
127228753Smm			return (FALSE);
128228753Smm		}
129228753Smm
130228753Smm		return (FALSE);
131228753Smm	}
132228753Smm
133228753Smm	if (!secure_map) {
134228753Smm		chmod(savefile, 0644);
135228753Smm		chmod(tofile, 0644);
136228753Smm	}
137228753Smm
138228753Smm	return (TRUE);
139228753Smm}
140228753Smm
141228753Smm/*
142228753Smm * Function :	delete_map()
143228753Smm *
144228753Smm * Description:	Deletes a map
145228753Smm *
146228753Smm * Given :	Map name
147228753Smm *
148228753Smm * Return :	TRUE = Map deleted
149228753Smm *		FALSE = Map not completly deleted
150228753Smm */
151228753Smmbool
152228753Smmdelete_map(name)
153228753Smm	char *name;
154228753Smm{
155228753Smm	char fromfile[MAXNAMLEN + 1];
156228753Smm
157228753Smm	if (!name) {
158228753Smm		return (FALSE);
159228753Smm	}
160228753Smm
161228753Smm	if (!check_map_existence(name)) {
162228753Smm		/* Already gone */
163228753Smm		return (TRUE);
164228753Smm	}
165228753Smm
166228753Smm	(void) strcpy(fromfile, name);
167228753Smm	(void) strcat(fromfile, dbm_pag);
168228753Smm
169228753Smm	if (unlink(fromfile)) {
170228753Smm		logprintf2("Can't unlink %s.\n", fromfile);
171228753Smm		return (FALSE);
172228753Smm	}
173228753Smm
174228753Smm	(void) strcpy(fromfile, name);
175228753Smm	(void) strcat(fromfile, dbm_dir);
176228753Smm
177228753Smm	if (unlink(fromfile)) {
178228753Smm		logprintf2("Can't unlink %s.\n", fromfile);
179228753Smm		return (FALSE);
180228753Smm	}
181228753Smm
182228753Smm	return (TRUE);
183228753Smm}
184228753Smm
185228753Smm/*
186228753Smm * This performs an existence check on the dbm data base files <pname>.pag and
187228753Smm * <pname>.dir.
188228753Smm */
189228753Smmbool
190228753Smmcheck_map_existence(pname)
191228753Smm	char *pname;
192228753Smm{
193228753Smm	char dbfile[MAXNAMLEN + 1];
194228753Smm	struct stat64 filestat;
195228753Smm	int len;
196228753Smm
197228753Smm	if (!pname || ((len = strlen(pname)) == 0) ||
198228753Smm	    (len + 5) > (MAXNAMLEN + 1)) {
199228753Smm		return (FALSE);
200228753Smm	}
201228753Smm
202228753Smm	errno = 0;
203228753Smm	(void) strcpy(dbfile, pname);
204228753Smm	(void) strcat(dbfile, dbm_dir);
205228753Smm
206228753Smm	if (stat64(dbfile, &filestat) != -1) {
207228753Smm		(void) strcpy(dbfile, pname);
208228753Smm		(void) strcat(dbfile, dbm_pag);
209228753Smm
210228753Smm		if (stat64(dbfile, &filestat) != -1) {
211228753Smm			return (TRUE);
212228753Smm		} else {
213228753Smm
214228753Smm			if (errno != ENOENT) {
215228753Smm				logprintf2(
216228753Smm				    "Stat error on map file %s.\n",
217228753Smm				    dbfile);
218228753Smm			}
219228753Smm
220228753Smm			return (FALSE);
221228753Smm		}
222228753Smm
223228753Smm	} else {
224228753Smm
225228753Smm		if (errno != ENOENT) {
226228753Smm			logprintf2(
227228753Smm			    "Stat error on map file %s.\n",
228228753Smm			    dbfile);
229228753Smm		}
230228753Smm
231228753Smm		return (FALSE);
232228753Smm	}
233228753Smm}
234228753Smm
235228753Smm/*
236228753Smm * FUNCTION :	logprintf2()
237228753Smm *
238228753Smm * DESCRIPTION:	The functions in this file were oringinaly shared between
239228753Smm *		ypxfr and ypserv. On error they called logprintf().
240228753Smm *		Unfortunatly this had been implemented differently in the two
241228753Smm *		sources and not at all in some of the NIS components required
242228753Smm *		for N2L.
243228753Smm *
244228753Smm *		This function is simplified version of logprinf() as/when
245228753Smm *		possible the other error calls should be migrated to use this
246228753Smm *		common version. If a common set of functionality can be found
247228753Smm *		this versions should be modified to support it.
248228753Smm */
249228753Smmvoid
250228753Smmlogprintf2(char *format, ...)
251228753Smm{
252228753Smm	va_list ap;
253228753Smm
254228753Smm	va_start(ap, format);
255228753Smm
256228753Smm	syslog(LOG_ERR, format, ap);
257228753Smm
258228753Smm	va_end(ap);
259228753Smm}
260228753Smm
261228753Smm/*
262228753Smm * This performs an existence check on the dbm data base files <name>.pag and
263228753Smm * <name>.dir.  pname is a ptr to the filename.  This should be an absolute
264228753Smm * path.
265228753Smm * Returns TRUE if the map exists and is accessable; else FALSE.
266228753Smm *
267228753Smm * Note:  The file name should be a "base" form, without a file "extension" of
268228753Smm * .dir or .pag appended.  See ypmkfilename for a function which will generate
269228753Smm * the name correctly.  Errors in the stat call will be reported at this level,
270228753Smm * however, the non-existence of a file is not considered an error, and so will
271228753Smm * not be reported.
272228753Smm *
273228753Smm * Calls ypcheck_map_existence_yptol() defined in
274228753Smm * usr/src/lib/libnisdb/yptol/shim_ancil.c
275228753Smm */
276228753Smmbool
277228753Smmypcheck_map_existence(char *pname)
278228753Smm{
279228753Smm	return (ypcheck_map_existence_yptol(pname));
280228753Smm}
281228753Smm