xref: /titanic_44/usr/src/cmd/fs.d/autofs/autod_lookup.c (revision 28167c24ba5be8b7c1d05e02d053f4a55cd21cc9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	autod_lookup.c
24  *
25  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <errno.h>
36 #include <locale.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <assert.h>
40 #include "automount.h"
41 
42 int
43 do_lookup1(mapname, key, subdir, mapopts, path, isdirect, action, linkp, cred)
44 	char *mapname;
45 	char *key;
46 	char *subdir;
47 	char *mapopts;
48 	char *path;
49 	uint_t isdirect;
50 	enum autofs_action *action;
51 	struct linka *linkp;
52 	struct authunix_parms *cred;
53 {
54 	struct mapline ml;
55 	struct mapent *mapents = NULL;
56 	int err;
57 	struct rddir_cache *rdcp;
58 	int found = 0;
59 	bool_t iswildcard = FALSE;
60 	bool_t isrestricted = hasrestrictopt(mapopts);
61 	char *stack[STACKSIZ];
62 	char **stkptr = stack;
63 
64 #ifdef lint
65 	path = path;
66 #endif /* lint */
67 
68 	/*
69 	 * Default action is for no work to be done by kernel AUTOFS.
70 	 */
71 	*action = AUTOFS_NONE;
72 
73 	/*
74 	 * Is there a cache for this map?
75 	 */
76 	rw_rdlock(&rddir_cache_lock);
77 	err = rddir_cache_lookup(mapname, &rdcp);
78 	if (!err && rdcp->full) {
79 		rw_unlock(&rddir_cache_lock);
80 		/*
81 		 * Try to lock readdir cache entry for reading, if
82 		 * the entry can not be locked, then avoid blocking
83 		 * and go to the name service. I'm assuming it is
84 		 * faster to go to the name service than to wait for
85 		 * the cache to be populated.
86 		 */
87 		if (rw_tryrdlock(&rdcp->rwlock) == 0) {
88 			found = (rddir_entry_lookup(key, rdcp->entp) != NULL);
89 			rw_unlock(&rdcp->rwlock);
90 		}
91 	} else
92 		rw_unlock(&rddir_cache_lock);
93 
94 	if (!err) {
95 		/*
96 		 * release reference on cache entry
97 		 */
98 		mutex_lock(&rdcp->lock);
99 		rdcp->in_use--;
100 		assert(rdcp->in_use >= 0);
101 		mutex_unlock(&rdcp->lock);
102 	}
103 
104 	if (found)
105 		return (0);
106 
107 	/*
108 	 * entry not found in cache, try the name service now
109 	 */
110 	err = 0;
111 
112 	/* initialize the stack of open files for this thread */
113 	stack_op(INIT, NULL, stack, &stkptr);
114 
115 	err = getmapent(key, mapname, &ml, stack, &stkptr, &iswildcard,
116 		isrestricted);
117 	if (err == 0) /* call parser w default mount_access = TRUE */
118 		mapents = parse_entry(key, mapname, mapopts, &ml,
119 				    subdir, isdirect, TRUE);
120 
121 	/*
122 	 * Now we indulge in a bit of hanky-panky.
123 	 * If the entry isn't found in the map and the
124 	 * name begins with an "=" then we assume that
125 	 * the name is an undocumented control message
126 	 * for the daemon.  This is accessible only
127 	 * to superusers.
128 	 */
129 	if (mapents == NULL && *action == AUTOFS_NONE) {
130 		if (*key == '=' && cred->aup_uid == 0) {
131 			if (isdigit(*(key+1))) {
132 				/*
133 				 * If next character is a digit
134 				 * then set the trace level.
135 				 */
136 				trace = atoi(key+1);
137 				trace_prt(1, "Automountd: trace level = %d\n",
138 					trace);
139 			} else if (*(key+1) == 'v') {
140 				/*
141 				 * If it's a "v" then
142 				 * toggle verbose mode.
143 				 */
144 				verbose = !verbose;
145 				trace_prt(1, "Automountd: verbose %s\n",
146 						verbose ? "on" : "off");
147 			}
148 		}
149 
150 		err = ENOENT;
151 		goto done;
152 	}
153 
154 	/*
155 	 * Each mapent in the list describes a mount to be done.
156 	 * Since I'm only doing a lookup, I only care whether a mapentry
157 	 * was found or not. The mount will be done on a later RPC to
158 	 * do_mount1.
159 	 */
160 	if (mapents == NULL && *action == AUTOFS_NONE)
161 		err = ENOENT;
162 
163 done:	if (mapents)
164 		free_mapent(mapents);
165 
166 	if (*action == AUTOFS_NONE && (iswildcard == TRUE)) {
167 		*action = AUTOFS_MOUNT_RQ;
168 	}
169 	if (trace > 1) {
170 		trace_prt(1, "  do_lookup1: action=%d wildcard=%s error=%d\n",
171 			*action, iswildcard ? "TRUE" : "FALSE", err);
172 	}
173 	return (err);
174 }
175