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