xref: /titanic_51/usr/src/cmd/fs.d/autofs/autod_lookup.c (revision 3bfb48feb84bb78924286a801c68f80198912fa7)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  *	autod_lookup.c
237c478bd9Sstevel@tonic-gate  *
24*3bfb48feSsemery  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <syslog.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <locale.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <assert.h>
397c478bd9Sstevel@tonic-gate #include "automount.h"
407c478bd9Sstevel@tonic-gate 
4111606941Sjwahlig int
4239d3e169Sevanl do_lookup1(
4339d3e169Sevanl 	char *mapname,
4439d3e169Sevanl 	char *key,
4539d3e169Sevanl 	char *subdir,
4639d3e169Sevanl 	char *mapopts,
4739d3e169Sevanl 	char *path,
4839d3e169Sevanl 	uint_t isdirect,
49*3bfb48feSsemery 	uid_t uid,
5039d3e169Sevanl 	autofs_action_t *action,
51*3bfb48feSsemery 	struct linka *linkp)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	struct mapline ml;
547c478bd9Sstevel@tonic-gate 	struct mapent *mapents = NULL;
557c478bd9Sstevel@tonic-gate 	int err;
5639d3e169Sevanl 	struct autofs_rddir_cache *rdcp;
577c478bd9Sstevel@tonic-gate 	int found = 0;
587c478bd9Sstevel@tonic-gate 	bool_t iswildcard = FALSE;
597c478bd9Sstevel@tonic-gate 	bool_t isrestricted = hasrestrictopt(mapopts);
607c478bd9Sstevel@tonic-gate 	char *stack[STACKSIZ];
617c478bd9Sstevel@tonic-gate 	char **stkptr = stack;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	/*
647c478bd9Sstevel@tonic-gate 	 * Default action is for no work to be done by kernel AUTOFS.
657c478bd9Sstevel@tonic-gate 	 */
667c478bd9Sstevel@tonic-gate 	*action = AUTOFS_NONE;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/*
697c478bd9Sstevel@tonic-gate 	 * Is there a cache for this map?
707c478bd9Sstevel@tonic-gate 	 */
7139d3e169Sevanl 	rw_rdlock(&autofs_rddir_cache_lock);
7239d3e169Sevanl 	err = autofs_rddir_cache_lookup(mapname, &rdcp);
737c478bd9Sstevel@tonic-gate 	if (!err && rdcp->full) {
7439d3e169Sevanl 		rw_unlock(&autofs_rddir_cache_lock);
757c478bd9Sstevel@tonic-gate 		/*
767c478bd9Sstevel@tonic-gate 		 * Try to lock readdir cache entry for reading, if
777c478bd9Sstevel@tonic-gate 		 * the entry can not be locked, then avoid blocking
787c478bd9Sstevel@tonic-gate 		 * and go to the name service. I'm assuming it is
797c478bd9Sstevel@tonic-gate 		 * faster to go to the name service than to wait for
807c478bd9Sstevel@tonic-gate 		 * the cache to be populated.
817c478bd9Sstevel@tonic-gate 		 */
827c478bd9Sstevel@tonic-gate 		if (rw_tryrdlock(&rdcp->rwlock) == 0) {
837c478bd9Sstevel@tonic-gate 			found = (rddir_entry_lookup(key, rdcp->entp) != NULL);
847c478bd9Sstevel@tonic-gate 			rw_unlock(&rdcp->rwlock);
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 	} else
8739d3e169Sevanl 		rw_unlock(&autofs_rddir_cache_lock);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (!err) {
907c478bd9Sstevel@tonic-gate 		/*
917c478bd9Sstevel@tonic-gate 		 * release reference on cache entry
927c478bd9Sstevel@tonic-gate 		 */
937c478bd9Sstevel@tonic-gate 		mutex_lock(&rdcp->lock);
947c478bd9Sstevel@tonic-gate 		rdcp->in_use--;
957c478bd9Sstevel@tonic-gate 		assert(rdcp->in_use >= 0);
967c478bd9Sstevel@tonic-gate 		mutex_unlock(&rdcp->lock);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (found)
1007c478bd9Sstevel@tonic-gate 		return (0);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/*
1037c478bd9Sstevel@tonic-gate 	 * entry not found in cache, try the name service now
1047c478bd9Sstevel@tonic-gate 	 */
1057c478bd9Sstevel@tonic-gate 	err = 0;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/* initialize the stack of open files for this thread */
1087c478bd9Sstevel@tonic-gate 	stack_op(INIT, NULL, stack, &stkptr);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	err = getmapent(key, mapname, &ml, stack, &stkptr, &iswildcard,
1117c478bd9Sstevel@tonic-gate 		isrestricted);
1127c478bd9Sstevel@tonic-gate 	if (err == 0) /* call parser w default mount_access = TRUE */
1137c478bd9Sstevel@tonic-gate 		mapents = parse_entry(key, mapname, mapopts, &ml,
1147c478bd9Sstevel@tonic-gate 				    subdir, isdirect, TRUE);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/*
1177c478bd9Sstevel@tonic-gate 	 * Now we indulge in a bit of hanky-panky.
1187c478bd9Sstevel@tonic-gate 	 * If the entry isn't found in the map and the
1197c478bd9Sstevel@tonic-gate 	 * name begins with an "=" then we assume that
1207c478bd9Sstevel@tonic-gate 	 * the name is an undocumented control message
1217c478bd9Sstevel@tonic-gate 	 * for the daemon.  This is accessible only
1227c478bd9Sstevel@tonic-gate 	 * to superusers.
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 	if (mapents == NULL && *action == AUTOFS_NONE) {
125*3bfb48feSsemery 		if (*key == '=' && uid == 0) {
1267c478bd9Sstevel@tonic-gate 			if (isdigit(*(key+1))) {
1277c478bd9Sstevel@tonic-gate 				/*
1287c478bd9Sstevel@tonic-gate 				 * If next character is a digit
1297c478bd9Sstevel@tonic-gate 				 * then set the trace level.
1307c478bd9Sstevel@tonic-gate 				 */
1317c478bd9Sstevel@tonic-gate 				trace = atoi(key+1);
1327c478bd9Sstevel@tonic-gate 				trace_prt(1, "Automountd: trace level = %d\n",
1337c478bd9Sstevel@tonic-gate 					trace);
1347c478bd9Sstevel@tonic-gate 			} else if (*(key+1) == 'v') {
1357c478bd9Sstevel@tonic-gate 				/*
1367c478bd9Sstevel@tonic-gate 				 * If it's a "v" then
1377c478bd9Sstevel@tonic-gate 				 * toggle verbose mode.
1387c478bd9Sstevel@tonic-gate 				 */
1397c478bd9Sstevel@tonic-gate 				verbose = !verbose;
1407c478bd9Sstevel@tonic-gate 				trace_prt(1, "Automountd: verbose %s\n",
1417c478bd9Sstevel@tonic-gate 						verbose ? "on" : "off");
1427c478bd9Sstevel@tonic-gate 			}
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		err = ENOENT;
1467c478bd9Sstevel@tonic-gate 		goto done;
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * Each mapent in the list describes a mount to be done.
1517c478bd9Sstevel@tonic-gate 	 * Since I'm only doing a lookup, I only care whether a mapentry
1527c478bd9Sstevel@tonic-gate 	 * was found or not. The mount will be done on a later RPC to
1537c478bd9Sstevel@tonic-gate 	 * do_mount1.
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	if (mapents == NULL && *action == AUTOFS_NONE)
1567c478bd9Sstevel@tonic-gate 		err = ENOENT;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate done:	if (mapents)
1597c478bd9Sstevel@tonic-gate 		free_mapent(mapents);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (*action == AUTOFS_NONE && (iswildcard == TRUE)) {
1627c478bd9Sstevel@tonic-gate 		*action = AUTOFS_MOUNT_RQ;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	if (trace > 1) {
1657c478bd9Sstevel@tonic-gate 		trace_prt(1, "  do_lookup1: action=%d wildcard=%s error=%d\n",
1667c478bd9Sstevel@tonic-gate 			*action, iswildcard ? "TRUE" : "FALSE", err);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	return (err);
1697c478bd9Sstevel@tonic-gate }
170