Print this page
5910 libnisdb won't build with modern GCC
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libnisdb/yptol/shim_ancil.c
+++ new/usr/src/lib/libnisdb/yptol/shim_ancil.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 + * Copyright 2015 Gary Mills
22 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 24 * Use is subject to license terms.
24 25 */
25 26
26 27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 28 /* All Rights Reserved */
28 29
29 30 /*
30 31 * Portions of this source code were derived from Berkeley 4.3 BSD
31 32 * under license from the Regents of the University of California.
32 33 */
33 34
34 -#pragma ident "%Z%%M% %I% %E% SMI"
35 -
36 -#ifndef lint
37 -static char sccsid[] = "%Z%%M% %I% %E% SMI";
38 -#endif
39 -
40 35 #include <stdlib.h>
41 36 #include <dirent.h>
42 37 #include <strings.h>
43 38 #include "ypsym.h"
44 39 #include "ypdefs.h"
45 40 USE_YPDBPATH
46 41 USE_DBM
47 42 #include "shim.h"
48 43 #include "../ldap_util.h"
49 44
50 45 /*
51 46 * This constructs a file name from a passed domain name, a passed map name,
52 47 * and a globally known YP data base path prefix.
53 48 *
54 49 * Has to be in shim because it needs the N2L prefix
55 50 *
56 51 * RETURNS : TRUE = A name was successfully created
57 52 * FALSE = A name could not be created
58 53 */
59 54
60 55 bool_t
61 56 ypmkfilename(domain, map, path)
62 57 char *domain;
63 58 char *map;
64 59 char *path;
65 60 {
66 61 int length;
67 62
68 63 /* Do not allow any path as a domain name. */
69 64 if (strchr(domain, '/') != NULL)
70 65 return (FALSE);
71 66
72 67 length = strlen(domain) + strlen(map) + ypdbpath_sz + 3;
73 68 if (yptol_mode)
74 69 length += strlen(NTOL_PREFIX) + 1;
75 70
76 71 if ((MAXNAMLEN + 1) < length) {
77 72 (void) fprintf(stderr, "ypserv: Map name string too long.\n");
78 73 return (FALSE);
79 74 }
80 75
81 76 strcpy(path, ypdbpath);
82 77 strcat(path, "/");
83 78 strcat(path, domain);
84 79 strcat(path, "/");
85 80
86 81 /* If in N2L mode add N2L prefix */
87 82 if (yptol_mode)
88 83 strcat(path, NTOL_PREFIX);
89 84 strcat(path, map);
90 85
91 86 return (TRUE);
92 87 }
93 88
94 89 /*
95 90 * check whether a map is already in an array/list
96 91 *
97 92 * RETURNS: TRUE if yes
98 93 * FALSE if not
99 94 */
100 95 bool_t
101 96 on_maplist(char *mapname, char **list) {
102 97 int i = 0;
103 98
104 99 if (list == NULL) {
105 100 return (FALSE);
106 101 }
107 102
108 103 while (list[i] != NULL) {
109 104 if (strcmp(mapname, list[i++]) == 0) {
110 105 return (TRUE);
111 106 }
112 107 }
113 108
114 109 return (FALSE);
115 110 }
116 111
117 112 /*
118 113 * add a map at the end of an array/list
119 114 *
120 115 * list_len: if -1, we do not know list length
121 116 *
122 117 * RETURNS: TRUE if map was added
123 118 * FALSE if not
124 119 */
125 120 bool_t
126 121 add_in_maplist(char *mapname, char ***list, int *list_len) {
127 122 int i = 0;
128 123 char **list_tmp;
129 124
130 125 if (list == NULL) {
131 126 return (FALSE);
132 127 }
133 128
134 129 list_tmp = *list;
135 130
136 131 if (list_tmp == NULL) {
137 132 *list_len = 0;
138 133 } else {
139 134 /* find 1st free element */
140 135 while (list_tmp[i] != NULL) {
141 136 /*
142 137 * increment in loop so that
143 138 * list_tmp[i] == NULL
144 139 * when exiting
145 140 */
146 141 i++;
147 142 }
148 143 }
149 144
150 145 /* if we don't know list length, assume we reach its end */
151 146 if (*list_len == -1) {
152 147 *list_len = i;
153 148 }
154 149
155 150 /* do we need to reallocate ? */
156 151 if (i+1 >= *list_len) {
157 152 list_tmp = (char **)realloc(list_tmp,
158 153 (*list_len + ARRAY_CHUNK) *
159 154 sizeof (char *));
160 155 if (list_tmp == NULL) {
161 156 return (FALSE);
162 157 }
163 158 *list = list_tmp;
164 159 *list_len += ARRAY_CHUNK;
165 160 }
166 161
167 162 /* add in list */
168 163 (*list)[i] = strdup(mapname);
169 164 if ((*list)[i] == NULL) {
170 165 /* strdup() failed */
171 166 return (FALSE);
172 167 }
173 168 (*list)[++i] = NULL;
174 169
175 170 return (TRUE);
176 171 }
177 172
178 173 /*
179 174 * This checks to see whether a domain name is present at the local node as a
180 175 * subdirectory of ypdbpath
181 176 *
182 177 * Was originally in cmd/ypcmd/shared/ancil.c as ypcheck_domain(domain).
183 178 * Now ypcheck_domain(domain) calls this function.
184 179 */
185 180 bool
186 181 ypcheck_domain_yptol(char *domain)
187 182 {
188 183 char path[MAXNAMLEN + 1];
189 184 struct stat filestat;
190 185 bool present = FALSE;
191 186
192 187 strcpy(path, ypdbpath);
193 188 strcat(path, "/");
194 189 if (strlcat(path, domain, MAXNAMLEN + 1) >= MAXNAMLEN + 1)
195 190 return (present);
196 191
197 192 if (stat(path, &filestat) != -1) {
198 193 if (S_ISDIR(filestat.st_mode))
199 194 present = TRUE;
200 195 }
201 196 return (present);
202 197 }
203 198
204 199 /*
205 200 * This performs an existence check on the dbm data base files <name>.pag and
206 201 * <name>.dir. pname is a ptr to the filename. This should be an absolute
207 202 * path.
208 203 * Returns TRUE if the map exists and is accessible; else FALSE.
209 204 *
210 205 * Note: The file name should be a "base" form, without a file "extension" of
211 206 * .dir or .pag appended. See ypmkfilename for a function which will generate
212 207 * the name correctly. Errors in the stat call will be reported at this level,
213 208 * however, the non-existence of a file is not considered an error, and so will
214 209 * not be reported.
215 210 *
216 211 * Was originally in cmd/ypcmd/shared/utils.c as ypcheck_map_existence().
217 212 * Now ypcheck_map_existence() calls this function.
218 213 */
219 214 bool
220 215 ypcheck_map_existence_yptol(char *pname)
221 216 {
222 217 char dbfile[MAXNAMLEN + sizeof (TTL_POSTFIX) + 1];
223 218 struct stat64 filestat;
224 219 int len;
225 220
226 221 if (!pname || ((len = (int)strlen(pname)) == 0) ||
227 222 (len + sizeof (dbm_pag) + sizeof (TTL_POSTFIX)) >
228 223 sizeof (dbfile)) {
229 224 return (FALSE);
230 225 }
231 226
232 227 errno = 0;
233 228
234 229 /* Check for existance of .dir file */
235 230 (void) strcpy(dbfile, pname);
236 231 (void) strcat(dbfile, dbm_dir);
237 232
238 233 if (stat64(dbfile, &filestat) == -1) {
239 234 if (errno != ENOENT) {
240 235 (void) fprintf(stderr,
241 236 "ypserv: Stat error on map file %s.\n",
242 237 dbfile);
243 238 }
244 239 return (FALSE);
245 240 }
246 241
247 242 /* Check for existance of .pag file */
248 243 (void) strcpy(dbfile, pname);
249 244 (void) strcat(dbfile, dbm_pag);
250 245
251 246 if (stat64(dbfile, &filestat) == -1) {
252 247 if (errno != ENOENT) {
253 248 (void) fprintf(stderr,
254 249 "ypserv: Stat error on map file %s.\n",
255 250 dbfile);
256 251 }
257 252 return (FALSE);
258 253 }
259 254
260 255 if (yptol_mode) {
261 256 /* Check for existance of TTL .dir file */
262 257 (void) strcpy(dbfile, pname);
263 258 (void) strcat(dbfile, TTL_POSTFIX);
264 259 (void) strcat(dbfile, dbm_dir);
265 260
266 261 if (stat64(dbfile, &filestat) == -1) {
267 262 if (errno != ENOENT) {
268 263 (void) fprintf(stderr,
269 264 "ypserv: Stat error on map file %s.\n",
270 265 dbfile);
271 266 }
272 267 return (FALSE);
273 268 }
274 269
275 270 /* Check for existance of TTL .pag file */
276 271 (void) strcpy(dbfile, pname);
277 272 (void) strcat(dbfile, TTL_POSTFIX);
278 273 (void) strcat(dbfile, dbm_pag);
279 274
280 275 if (stat64(dbfile, &filestat) == -1) {
281 276 if (errno != ENOENT) {
282 277 (void) fprintf(stderr,
283 278 "ypserv: Stat error on map file %s.\n",
284 279 dbfile);
285 280 }
286 281 return (FALSE);
287 282 }
288 283 }
289 284
290 285 return (TRUE);
291 286 }
292 287
293 288 /*
294 289 * This adds maps in a domain to a given list,
295 290 * from maps in /var/yp/<domain>
296 291 * Inspired from yplist_maps() in cmd/ypcmd/ypserv_ancil.c
297 292 *
298 293 * domain is the relevant domain name
299 294 * map_list is the list of maps in an array of map names,
300 295 * which may or may not be empty
301 296 *
302 297 * RETURNS : TRUE = everything went fine
303 298 * FALSE = an error occured
304 299 */
305 300 bool_t
306 301 add_map_domain_to_list(char *domain, char ***map_list)
307 302 {
308 303 char domdir[MAXNAMLEN + 1];
309 304 char path[MAXNAMLEN + 1];
310 305 int domdir_len = sizeof (domdir);
311 306 DIR *dirp;
312 307 struct dirent *dp;
313 308 int name_len;
314 309 int dbm_pag_len = sizeof (dbm_pag);
315 310 char *ext;
316 311 char *mapname;
317 312 int map_list_len = -1;
318 313
319 314 if (map_list == NULL) {
320 315 return (FALSE);
321 316 }
322 317
323 318 /* no domain, not a problem */
324 319 if (domain == NULL) {
325 320 return (TRUE);
326 321 }
327 322
328 323 /* not a valid domain, not a problem */
329 324 if (!ypcheck_domain_yptol(domain)) {
330 325 return (TRUE);
331 326 }
332 327
333 328 if (snprintf(domdir, domdir_len, "%s/%s", ypdbpath, domain)
334 329 > domdir_len) {
335 330 return (FALSE);
336 331 }
337 332
338 333 if ((dirp = opendir(domdir)) == NULL) {
339 334 return (FALSE);
340 335 }
341 336
342 337 for (dp = readdir(dirp); dp != NULL;
343 338 dp = readdir(dirp)) {
344 339 /*
345 340 * If it's possible that the file name is one of the two files
346 341 * implementing a map, remove the extension (dbm_pag or dbm_dir)
347 342 */
348 343 name_len = (int)strlen(dp->d_name);
349 344
350 345 if (name_len < dbm_pag_len - 1) {
351 346 continue; /* Too Short */
352 347 }
353 348
354 349 ext = &(dp->d_name[name_len - (dbm_pag_len - 1)]);
355 350
356 351 if (strcmp(ext, dbm_pag) != 0) {
357 352 continue; /* No dbm file extension */
358 353 }
359 354
360 355 *ext = '\0';
361 356
362 357 /*
363 358 * In yptol mode look at LDAP_ prefixed maps. In non yptol mode
364 359 * ignore them.
365 360 */
366 361 if (yptol_mode) {
367 362 if (0 != strncmp(dp->d_name, NTOL_PREFIX,
368 363 strlen(NTOL_PREFIX))) {
369 364 continue;
370 365 }
371 366
372 367 /*
373 368 * Already have an LDAP_ prefix. Don't want to add it
374 369 * twice.
375 370 */
376 371 mapname = dp->d_name + strlen(NTOL_PREFIX);
377 372 } else {
378 373 if (0 == strncmp(dp->d_name, NTOL_PREFIX,
379 374 strlen(NTOL_PREFIX))) {
380 375 continue;
381 376 }
382 377 mapname = dp->d_name;
383 378 }
384 379
385 380 if (ypmkfilename(domain, mapname, path) == FALSE) {
386 381 (void) closedir(dirp);
387 382 return (FALSE);
388 383 }
389 384
390 385 /*
391 386 * At this point, path holds the map file base name (no dbm
392 387 * file extension), and mapname holds the map name.
393 388 */
394 389 if (ypcheck_map_existence_yptol(path) &&
395 390 !on_maplist(mapname, *map_list)) {
396 391 if (add_in_maplist(mapname, map_list, &map_list_len) ==
397 392 FALSE) {
398 393 (void) closedir(dirp);
399 394 return (FALSE);
400 395 }
401 396 }
402 397 }
403 398
404 399 (void) closedir(dirp);
405 400 return (TRUE);
406 401 }
↓ open down ↓ |
357 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX