summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/autofs/ns_fnreaddir.c
blob: 256f25dc7b9a87a676348e06270f48cd5d8abcd4 (plain)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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
 */
/*
 * ns_fnreaddir.c
 *
 * Copyright (c) 1995 - 1996, by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <rpc/rpc.h>
#include <xfn/xfn.h>
#include "automount.h"
#include "ns_fnutils.h"


/*
 * Given the name of an XFN map, create a list of the map entries for a
 * given user.  Set error to zero on success.
 *
 *	extern void
 *	getmapkeys_fn(const char *map, struct dir_entry **, int *error,
 *	    int *cache_time, uid_t);
 */

/*
 * Given a multi-component composite name, construct the corresponding
 * context handle and the context handle of its prefix.  The prefix is
 * that part of the name up to (and possibly including) the last slash
 * in the name.  Return zero on success.
 *
 * eg:	user/jane/service  =>  user/jane + service
 *	org/ssi.eng/user   =>  org/ssi.eng/ + user
 */
static int
get_contexts(const FN_composite_name_t *, FN_ctx_t **ctxp,
    FN_ctx_t **prefix_ctxp, FN_ctx_t *init_ctx, FN_status_t *);

/*
 * Split a multi-component composite name into its last component and
 * its other components.  Return zero on success.
 */
static int
split_cname(const FN_composite_name_t *name, FN_composite_name_t **last,
    FN_composite_name_t **lead);

/*
 * Given a context and its prefix context (defined above), determine
 * whether the context, its NNS context, or both should be listed.
 * (The syntaxes of the contexts are used to help make this
 * determination.)  Add the subdirectories of the appropriate
 * context(s) to the dir_entry list.  Return zero on success.
 *
 * eg:	"ls /xfn/user		=>  list context only
 *	"ls /xfn/org/ssi.eng"	=>  list NNS only
 *	"ls /xfn/.../c=us"	=>  list context and NNS
 */
static int
list_ctx_and_or_nns(FN_ctx_t *ctx, FN_ctx_t *prefix_ctx, struct dir_entry **,
    FN_status_t *);

/*
 * Given a context and its prefix context (defined above), return true
 * if the NNS of the context should be listed but the context itself
 * should not.
 */
static bool_t
need_nns_only(FN_ctx_t *ctx, FN_ctx_t *prefix_ctx, FN_status_t *);

/*
 * Return true if both the given context and its NNS should be listed.
 */
static bool_t
need_ctx_and_nns(FN_ctx_t *, FN_status_t *);

/*
 * Add the subdirectories of a context to the dir_entry list.  Return
 * zero on success.
 */
static int
list_ctx(FN_ctx_t *, struct dir_entry **, FN_status_t *);

/*
 * Given a context and its name relative to the root of its rightmost
 * naming system, add the context's subdirectories to the dir_entry
 * list.  If syntax is non-NULL recursively list names until a context
 * with a different syntax is encountered, otherwise list one level
 * only.  May modify "name".  Return zero on success.
 *
 * eg:  For the context org/eng with syntax "dot-separated, right-to-left",
 * the compound name "eng" would be passed in, and the following might
 * be added to the dir_entry list:
 * 	ssi.eng
 *	feds.ssi.eng
 * 	ste.eng
 */
static int
list_ctx_aux(FN_ctx_t *, FN_compound_name_t *name, const FN_attrset_t *syntax,
    struct dir_entry **, FN_status_t *);

/*
 * Add a name to a dir_entry list.  Return zero on success.
 */
static int
add_name_to_dirlist(const FN_compound_name_t *, struct dir_entry **);

/*
 * Return true if a set of syntax attributes correspond to a
 * hierarchical namespace with a slash separator.  Return false on
 * error.
 */
static bool_t
slash_hierarchy(const FN_attrset_t *syntax);

/*
 * Return true if a set of syntax attributes correspond to a
 * hierarchical namespace with a separator other than a slash.
 * Return false on error.
 */
static bool_t
non_slash_hierarchy(const FN_attrset_t *syntax);

/*
 * Return true if two syntax attribute sets are equal.
 */
static bool_t
syntax_attrs_equal(const FN_attrset_t *, const FN_attrset_t *);

/*
 * Return a value of a given attribute in an attribute set, or NULL
 * on error.
 */
static const FN_attrvalue_t *
get_attrval(const FN_attrset_t *, const FN_identifier_t *attr_id);

/*
 * Lookup a name and return the corresponding context handle.  On
 * error return NULL and, if "log" is true or the error is transient,
 * log an error message.
 */
static FN_ctx_t *
lookup_ctx(FN_ctx_t *, const FN_composite_name_t *, bool_t log, FN_status_t *);


/*
 * Unlike during a lookup or mount, transient errors are tolerated.  A
 * potentially transient error during a readdir() (such as no response
 * from an X.500 server) could result in an incomplete listing, but at
 * least readdir() will return everything that it can.  Note that it
 * is still possible to mount a directory that for some reason did not
 * show up in a prior readdir().
 */
void
getmapkeys_fn(const char *map, struct dir_entry **entries_p, int *error,
    int *cache_time, uid_t uid)
{
	FN_composite_name_t	*name;
	FN_status_t		*status;
	FN_ctx_t		*init_ctx;
	FN_ctx_t		*ctx;
	FN_ctx_t		*prefix_ctx;
	struct dir_entry	*p;

	*cache_time = RDDIR_CACHE_TIME;

	if ((init_fn() != 0) || (status = fn_status_create()) == NULL) {
		log_mem_failure();
		*error = -1;
		return;
	}
	init_ctx = _fn_ctx_handle_from_initial_with_uid(uid, 0, status);
	if (init_ctx == NULL) {
		logstat(status, "", "No initial context");
		fn_status_destroy(status);
		return;
	}

	if (strcmp(map, FNPREFIX) == 0) {
		/*
		 * List the initial context.
		 * Contents of initial ctx is user-relative
		 */
		*cache_time = 0;
		*error = list_ctx(init_ctx, entries_p, status);
	} else if (strcmp(map, FNPREFIX "/_dns") == 0) {
		/* Cannot list DNS; report success but no entries. */
		*cache_time = 1000000;	/* no sense trying again */
		*error = 0;
	} else {
		if (strcmp(map, FNPREFIX "/...") == 0) {
			/* List X.500 but not DNS. */
			name = new_cname("_x500");
		} else {
			name = new_cname(map + FNPREFIXLEN + 1);
		}
		if (name == NULL) {
			*error = -1;
		} else if (fn_composite_name_count(name) == 1) {

			/* List an atomic name. */
			ctx = lookup_ctx(init_ctx, name, TRUE, status);
			if (ctx != NULL) {
				*error = list_ctx_and_or_nns(ctx, init_ctx,
				    entries_p, status);
				fn_ctx_handle_destroy(ctx);
			} else {
				*error = -1;
			}
		} else {

			/* List a multi-component name. */
			*error = get_contexts(name, &ctx, &prefix_ctx,
			    init_ctx, status);
			if (*error == 0) {
				*error = list_ctx_and_or_nns(ctx, prefix_ctx,
				    entries_p, status);
				fn_ctx_handle_destroy(ctx);
				fn_ctx_handle_destroy(prefix_ctx);
			}
		}
		fn_composite_name_destroy(name);
	}
	fn_status_destroy(status);
	fn_ctx_handle_destroy(init_ctx);

	if (*error == 0) {
		/*
		 * create the binary tree of entries
		 */
		for (p = *entries_p; p != NULL; p = p->next)
			btree_enter(entries_p, p);
	}
}


static int
get_contexts(const FN_composite_name_t *name, FN_ctx_t **ctxp,
    FN_ctx_t **prefix_ctxp, FN_ctx_t *init_ctx, FN_status_t *status)
{
	FN_composite_name_t	*prefix = NULL;
	FN_composite_name_t	*suffix = NULL;
	FN_ctx_t		*nns_ctx;

	/*
	 * Break a name such as "pre/fix/suffix" into "pre/fix/" and
	 * "suffix".  If that fails, try "pre/fix" and "suffix".  This
	 * can be more efficient than doing it the reverse order.
	 */
	if (split_cname(name, &suffix, &prefix) != 0) {
		return (-1);
	}
	*ctxp = NULL;
	*prefix_ctxp = lookup_ctx(init_ctx, prefix, TRUE, status);
	fn_composite_name_destroy(prefix);

	if (*prefix_ctxp != NULL) {
		nns_ctx = lookup_ctx(*prefix_ctxp, slash_cname, FALSE, status);
		if (nns_ctx != NULL) {
			*ctxp = lookup_ctx(nns_ctx, suffix, FALSE, status);
			if (*ctxp != NULL) {
				fn_ctx_handle_destroy(*prefix_ctxp);
				*prefix_ctxp = nns_ctx;
			} else {
				fn_ctx_handle_destroy(nns_ctx);
			}
		}
		if (*ctxp == NULL) {
			*ctxp =
			    lookup_ctx(*prefix_ctxp, suffix, FALSE, status);
		}
	}
	fn_composite_name_destroy(suffix);
	return (*ctxp != NULL ? 0 : -1);
}


static int
split_cname(const FN_composite_name_t *name, FN_composite_name_t **last,
    FN_composite_name_t **lead)
{
	void	*iter;

	(void) fn_composite_name_last(name, &iter);
	*last = fn_composite_name_suffix(name, iter);
	*lead = fn_composite_name_prefix(name, iter);
	if (*last == NULL || *lead == NULL) {
		log_mem_failure();
		fn_composite_name_destroy(*last);
		fn_composite_name_destroy(*lead);
		return (-1);
	}
	return (0);
}


static int
list_ctx_and_or_nns(FN_ctx_t *ctx, FN_ctx_t *prefix_ctx,
    struct dir_entry **entries_p, FN_status_t *status)
{
	FN_ctx_t	*nns_ctx;
	int		rc;

	if (!need_nns_only(ctx, prefix_ctx, status)) {
		if (list_ctx(ctx, entries_p, status) != 0) {
			return (-1);
		}
		if (!need_ctx_and_nns(ctx, status)) {
			return (0);
		}
	}
	nns_ctx = lookup_ctx(ctx, slash_cname, FALSE, status);
	if (nns_ctx == NULL) {
		return (0);
	}
	rc = list_ctx(nns_ctx, entries_p, status);
	fn_ctx_handle_destroy(nns_ctx);
	return (rc);
}


/*
 * True if ctx has a hierarchical syntax with a non-slash separator
 * and prefix_ctx either has the same syntax or does not provide any
 * syntax ("..." should be the only example of the latter condition).
 */
static bool_t
need_nns_only(FN_ctx_t *ctx, FN_ctx_t *prefix_ctx, FN_status_t *status)
{
	FN_attrset_t	*syn;
	FN_attrset_t	*prefix_syn;
	bool_t		retval;

	syn = fn_ctx_get_syntax_attrs(ctx, empty_cname, status);
	if (syn == NULL || !non_slash_hierarchy(syn)) {
		fn_attrset_destroy(syn);
		return (FALSE);
	}
	/*
	 * ctx is hierarchical and not slash-separated.  How about prefix_ctx?
	 */
	prefix_syn = fn_ctx_get_syntax_attrs(prefix_ctx, empty_cname, status);
	retval = (prefix_syn == NULL) || syntax_attrs_equal(syn, prefix_syn);

	fn_attrset_destroy(syn);
	fn_attrset_destroy(prefix_syn);
	return (retval);
}


/*
 * True if ctx has a slash-separated hierarchical syntax.
 */
static bool_t
need_ctx_and_nns(FN_ctx_t *ctx, FN_status_t *status)
{
	FN_attrset_t	*syn;
	bool_t		retval;

	syn = fn_ctx_get_syntax_attrs(ctx, empty_cname, status);
	if (syn == NULL) {
		return (FALSE);
	}
	retval = slash_hierarchy(syn);
	fn_attrset_destroy(syn);
	return (retval);
}


static int
list_ctx(FN_ctx_t *ctx, struct dir_entry **entries_p, FN_status_t *status)
{
	FN_attrset_t		*syntax;
	FN_compound_name_t	*name;
	int			retval;

	syntax = fn_ctx_get_syntax_attrs(ctx, empty_cname, status);
	if (syntax == NULL) {
		logstat(status, "", "bad syntax attributes");
		return (-1);
	}
	name =
	    fn_compound_name_from_syntax_attrs(syntax, empty_string, status);
	if (name == NULL) {
		logstat(status, "", "could not create compound name");
		fn_attrset_destroy(syntax);
		return (-1);
	}
	if (!non_slash_hierarchy(syntax)) {
		fn_attrset_destroy(syntax);
		syntax = NULL;
	}
	retval = list_ctx_aux(ctx, name, syntax, entries_p, status);
	fn_attrset_destroy(syntax);
	fn_compound_name_destroy(name);
	return (retval);
}


static int
list_ctx_aux(FN_ctx_t *ctx, FN_compound_name_t *name,
    const FN_attrset_t *syntax, struct dir_entry **entries_p,
    FN_status_t *status)
{
	FN_bindinglist_t	*bindings;
	FN_string_t		*child;
	FN_ref_t		*ref;
	unsigned int		stat;
	int			rc = 0;
	void			*iter;

	bindings = fn_ctx_list_bindings(ctx, empty_cname, status);
	if (bindings == NULL) {
		return (0);
	}
	while ((child = fn_bindinglist_next(bindings, &ref, status)) != NULL) {
		if (fn_compound_name_append_comp(name, child, &stat) == 0) {
			rc = -1;
			break;
		}
		if (add_name_to_dirlist(name, entries_p) != 0) {
			rc = -1;
			break;
		}
		if (syntax != NULL) {
			/* Traverse hierarchy. */
			ctx = fn_ctx_handle_from_ref(ref, XFN2(0) status);
			if (ctx != NULL) {
				rc = list_ctx_aux(ctx, name, syntax, entries_p,
				    status);
				fn_ctx_handle_destroy(ctx);
				if (rc != 0) {
					break;
				}
			}
		}
		fn_ref_destroy(ref);
		fn_string_destroy(child);
		(void) fn_compound_name_last(name, &iter);
		(void) fn_compound_name_next(name, &iter);
		(void) fn_compound_name_delete_comp(name, &iter);
	}
	fn_string_destroy(child);
	fn_bindinglist_destroy(bindings XFN1(status));
	return (rc);
}


static int
add_name_to_dirlist(const FN_compound_name_t *name,
    struct dir_entry **entries_p)
{
	FN_string_t		*string;
	char			*str;
	unsigned int		stat;
	struct dir_entry	*entry;

	string = fn_string_from_compound_name(name);
	if (string == NULL) {
		log_mem_failure();
		return (-1);
	}
	str = (char *)fn_string_str(string, &stat);
	if (str != NULL) {
		str = auto_rddir_strdup(str);
	}
	fn_string_destroy(string);
	if (str == NULL) {
		log_mem_failure();
		return (-1);
	}

	/* LINTED pointer alignment */
	entry = (struct dir_entry *)
		auto_rddir_malloc(sizeof (*entry));
	if (entry == NULL) {
		log_mem_failure();
		free(str);
		return (-1);
	}
	(void) memset((char *)entry, 0, sizeof (*entry));
	entry->name = str;
	entry->next = *entries_p;
	*entries_p = entry;
	return (0);
}


/*
 * Identifiers of syntax attributes for direction and separator.
 */

static const FN_identifier_t syntax_direction = {
	FN_ID_STRING,
	sizeof ("fn_std_syntax_direction") - 1,
	"fn_std_syntax_direction"
};

static const FN_identifier_t syntax_separator = {
	FN_ID_STRING,
	sizeof ("fn_std_syntax_separator") - 1,
	"fn_std_syntax_separator"
};


static bool_t
slash_hierarchy(const FN_attrset_t *syntax)
{
	const FN_attrvalue_t	*dir = get_attrval(syntax, &syntax_direction);
	const FN_attrvalue_t	*sep = get_attrval(syntax, &syntax_separator);

	return (dir != NULL &&
	    memcmp("flat", dir->contents, dir->length) != 0 &&
	    sep != NULL &&
	    memcmp("/", sep->contents, sep->length) == 0);
}


static bool_t
non_slash_hierarchy(const FN_attrset_t *syntax)
{
	const FN_attrvalue_t	*dir = get_attrval(syntax, &syntax_direction);
	const FN_attrvalue_t	*sep = get_attrval(syntax, &syntax_separator);

	return (dir != NULL &&
	    memcmp("flat", dir->contents, dir->length) != 0 &&
	    sep != NULL &&
	    memcmp("/", sep->contents, sep->length) != 0);
}


static bool_t
syntax_attrs_equal(const FN_attrset_t *syn1, const FN_attrset_t *syn2)
{
	const FN_attribute_t	*attr;
	const FN_attrvalue_t	*val1;
	const FN_attrvalue_t	*val2;
	void			*iter1;
	void			*iter2;

	if (fn_attrset_count(syn1) != fn_attrset_count(syn2)) {
		return (FALSE);
	}
	for (attr = fn_attrset_first(syn1, &iter1);
	    attr != NULL;
	    attr = fn_attrset_next(syn1, &iter1)) {
		val1 = fn_attribute_first(attr, &iter2);
		val2 = get_attrval(syn2, fn_attribute_identifier(attr));
		if ((val1 == NULL && val2 != NULL) ||
		    (val1 != NULL && val2 == NULL)) {
			return (FALSE);
		}
		if (val1 != NULL && val2 != NULL) {
			if (val1->length != val2->length ||
			    memcmp(val1->contents, val2->contents,
				    val1->length) != 0) {
				return (FALSE);
			}
		}
	}
	return (TRUE);
}


static const FN_attrvalue_t *
get_attrval(const FN_attrset_t *attrs, const FN_identifier_t *attr_id)
{
	const FN_attribute_t	*attr;
	void			*iter;

	attr = fn_attrset_get(attrs, attr_id);
	if (attr != NULL) {
		return (fn_attribute_first(attr, &iter));
	} else {
		return (NULL);
	}
}


static FN_ctx_t *
lookup_ctx(FN_ctx_t *ctx, const FN_composite_name_t *name, bool_t log,
    FN_status_t *status)
{
	FN_ref_t	*ref;
	char		*msg;

	ref = fn_ctx_lookup(ctx, name, status);
	if (ref == NULL) {
		ctx = NULL;
		msg = "lookup failed";
	} else {
		ctx = fn_ctx_handle_from_ref(ref, XFN2(0) status);
		fn_ref_destroy(ref);
		if (ctx == NULL) {
			msg = "could not construct context handle";
		}
	}
	if (ctx == NULL && verbose && (log || transient(status))) {
		logstat(status, "", msg);
	}
	return (ctx);
}