treewide: stat64() -> stat()
[unleashed.git] / usr / src / cmd / ypcmd / shared / utils.c
blob87467180e7b1d3dff26cbc3af833b073960bee6c
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley
31 * under license from the Regents of the University of
32 * California.
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * DESCRIPTION: This file contains various functions used by more than one NIS
39 * components. A lot of this code started off in ypxfr and then
40 * got used by other components. Some of it has become a little
41 * 'quirky' and should probably be re-worked.
44 #include <unistd.h>
45 #include <syslog.h>
46 #include <sys/mman.h>
47 #include <thread.h>
48 #include <synch.h>
49 #include <stdarg.h>
50 #include <ndbm.h>
51 #include "../ypsym.h"
52 #include "../ypdefs.h"
53 #include "shim.h"
55 USE_DBM
58 * Globals
62 * DESCRIPTION : Utility functions used by everything.
64 bool check_map_existence(char *);
65 void logprintf2(char *format, ...);
66 extern bool ypcheck_map_existence_yptol();
69 * This checks to see if the source map files exist, then renames them to the
70 * target names. This is a boolean function. The file names from.pag and
71 * from.dir will be changed to to.pag and to.dir in the success case.
73 * Note: If the second of the two renames fails, yprename_map will try to
74 * un-rename the first pair, and leave the world in the state it was on entry.
75 * This might fail, too, though...
77 * GIVEN : Name of map to copy from
78 * Name of map to copy to
79 * Flag indicating if map is secure.
81 bool
82 rename_map(from, to, secure_map)
83 char *from;
84 char *to;
85 bool_t secure_map;
87 char fromfile[MAXNAMLEN + 1];
88 char tofile[MAXNAMLEN + 1];
89 char savefile[MAXNAMLEN + 1];
91 if (!from || !to) {
92 return (FALSE);
95 if (!check_map_existence(from)) {
96 return (FALSE);
99 (void) strcpy(fromfile, from);
100 (void) strcat(fromfile, dbm_pag);
101 (void) strcpy(tofile, to);
102 (void) strcat(tofile, dbm_pag);
104 if (rename(fromfile, tofile)) {
105 logprintf2("Can't mv %s to %s.\n", fromfile,
106 tofile);
107 return (FALSE);
110 (void) strcpy(savefile, tofile);
111 (void) strcpy(fromfile, from);
112 (void) strcat(fromfile, dbm_dir);
113 (void) strcpy(tofile, to);
114 (void) strcat(tofile, dbm_dir);
116 if (rename(fromfile, tofile)) {
117 logprintf2("Can't mv %s to %s.\n", fromfile,
118 tofile);
119 (void) strcpy(fromfile, from);
120 (void) strcat(fromfile, dbm_pag);
121 (void) strcpy(tofile, to);
122 (void) strcat(tofile, dbm_pag);
124 if (rename(tofile, fromfile)) {
125 logprintf2(
126 "Can't recover from rename failure.\n");
127 return (FALSE);
130 return (FALSE);
133 if (!secure_map) {
134 chmod(savefile, 0644);
135 chmod(tofile, 0644);
138 return (TRUE);
142 * Function : delete_map()
144 * Description: Deletes a map
146 * Given : Map name
148 * Return : TRUE = Map deleted
149 * FALSE = Map not completly deleted
151 bool
152 delete_map(name)
153 char *name;
155 char fromfile[MAXNAMLEN + 1];
157 if (!name) {
158 return (FALSE);
161 if (!check_map_existence(name)) {
162 /* Already gone */
163 return (TRUE);
166 (void) strcpy(fromfile, name);
167 (void) strcat(fromfile, dbm_pag);
169 if (unlink(fromfile)) {
170 logprintf2("Can't unlink %s.\n", fromfile);
171 return (FALSE);
174 (void) strcpy(fromfile, name);
175 (void) strcat(fromfile, dbm_dir);
177 if (unlink(fromfile)) {
178 logprintf2("Can't unlink %s.\n", fromfile);
179 return (FALSE);
182 return (TRUE);
186 * This performs an existence check on the dbm data base files <pname>.pag and
187 * <pname>.dir.
189 bool
190 check_map_existence(pname)
191 char *pname;
193 char dbfile[MAXNAMLEN + 1];
194 struct stat filestat;
195 int len;
197 if (!pname || ((len = strlen(pname)) == 0) ||
198 (len + 5) > (MAXNAMLEN + 1)) {
199 return (FALSE);
202 errno = 0;
203 (void) strcpy(dbfile, pname);
204 (void) strcat(dbfile, dbm_dir);
206 if (stat(dbfile, &filestat) != -1) {
207 (void) strcpy(dbfile, pname);
208 (void) strcat(dbfile, dbm_pag);
210 if (stat(dbfile, &filestat) != -1) {
211 return (TRUE);
212 } else {
214 if (errno != ENOENT) {
215 logprintf2(
216 "Stat error on map file %s.\n",
217 dbfile);
220 return (FALSE);
223 } else {
225 if (errno != ENOENT) {
226 logprintf2(
227 "Stat error on map file %s.\n",
228 dbfile);
231 return (FALSE);
236 * FUNCTION : logprintf2()
238 * DESCRIPTION: The functions in this file were oringinaly shared between
239 * ypxfr and ypserv. On error they called logprintf().
240 * Unfortunatly this had been implemented differently in the two
241 * sources and not at all in some of the NIS components required
242 * for N2L.
244 * This function is simplified version of logprinf() as/when
245 * possible the other error calls should be migrated to use this
246 * common version. If a common set of functionality can be found
247 * this versions should be modified to support it.
249 void
250 logprintf2(char *format, ...)
252 va_list ap;
254 va_start(ap, format);
256 syslog(LOG_ERR, format, ap);
258 va_end(ap);
262 * This performs an existence check on the dbm data base files <name>.pag and
263 * <name>.dir. pname is a ptr to the filename. This should be an absolute
264 * path.
265 * Returns TRUE if the map exists and is accessable; else FALSE.
267 * Note: The file name should be a "base" form, without a file "extension" of
268 * .dir or .pag appended. See ypmkfilename for a function which will generate
269 * the name correctly. Errors in the stat call will be reported at this level,
270 * however, the non-existence of a file is not considered an error, and so will
271 * not be reported.
273 * Calls ypcheck_map_existence_yptol() defined in
274 * usr/src/lib/libnisdb/yptol/shim_ancil.c
276 bool
277 ypcheck_map_existence(char *pname)
279 return (ypcheck_map_existence_yptol(pname));