xref: /onnv-gate/usr/src/cmd/fs.d/autofs/autod_autofs.c (revision 2170:eb691d2a219e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2170Sevanl  * Common Development and Distribution License (the "License").
6*2170Sevanl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  *	autod_autofs.c
230Sstevel@tonic-gate  *
24*2170Sevanl  *	Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25*2170Sevanl  *	Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/mntent.h>
370Sstevel@tonic-gate #include <sys/mnttab.h>
380Sstevel@tonic-gate #include <sys/mount.h>
390Sstevel@tonic-gate #include <sys/utsname.h>
400Sstevel@tonic-gate #include <sys/tiuser.h>
410Sstevel@tonic-gate #include <syslog.h>
420Sstevel@tonic-gate #include <string.h>
430Sstevel@tonic-gate #include <errno.h>
440Sstevel@tonic-gate #include <fslib.h>
450Sstevel@tonic-gate #include <sys/vfs.h>
460Sstevel@tonic-gate #include <assert.h>
470Sstevel@tonic-gate #include "automount.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static int process_opts(char *options, int *directp, int *sawnestp);
500Sstevel@tonic-gate void netbuf_free(struct netbuf *);
510Sstevel@tonic-gate 
520Sstevel@tonic-gate int
mount_autofs(struct mapent * me,char * mntpnt,action_list * alp,char * rootp,char * subdir,char * key)530Sstevel@tonic-gate mount_autofs(
540Sstevel@tonic-gate 	struct mapent *me,
550Sstevel@tonic-gate 	char *mntpnt,
560Sstevel@tonic-gate 	action_list *alp,
570Sstevel@tonic-gate 	char *rootp,
580Sstevel@tonic-gate 	char *subdir,
590Sstevel@tonic-gate 	char *key
600Sstevel@tonic-gate )
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	int mntflags = 0;
630Sstevel@tonic-gate 	struct utsname utsname;
64*2170Sevanl 	autofs_args *fnip = NULL;
650Sstevel@tonic-gate 	int mount_timeout = AUTOFS_MOUNT_TIMEOUT;
660Sstevel@tonic-gate 	int sawnest, len, error = 0;
670Sstevel@tonic-gate 	char *buf, rel_mntpnt[MAXPATHLEN];
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if (trace > 1)
700Sstevel@tonic-gate 		trace_prt(1, "  mount_autofs %s on %s\n",
710Sstevel@tonic-gate 		me->map_fs->mfs_dir, mntpnt);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (strcmp(mntpnt, "/-") == 0) {
740Sstevel@tonic-gate 		syslog(LOG_ERR, "invalid mountpoint: /-");
750Sstevel@tonic-gate 		return (ENOENT);
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	/*
790Sstevel@tonic-gate 	 * get relative mountpoint
800Sstevel@tonic-gate 	 */
810Sstevel@tonic-gate 	sprintf(rel_mntpnt, ".%s", mntpnt+strlen(rootp));
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	if (trace > 2)
840Sstevel@tonic-gate 		trace_prt(1, "rel_mntpnt = %s\n", rel_mntpnt);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (uname(&utsname) < 0) {
870Sstevel@tonic-gate 		error = errno;
880Sstevel@tonic-gate 		syslog(LOG_ERR, "uname %s", strerror(error));
890Sstevel@tonic-gate 		return (error);
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 
92*2170Sevanl 	if ((fnip = (autofs_args *)
93*2170Sevanl 	    malloc(sizeof (autofs_args))) == NULL) {
940Sstevel@tonic-gate 		goto free_mem;
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 	(void) memset((void *) fnip, 0, sizeof (*fnip));
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if ((fnip->addr.buf = (char *)malloc(MAXADDRLEN)) == NULL)
990Sstevel@tonic-gate 		goto free_mem;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	(void) strcpy(fnip->addr.buf, utsname.nodename);
1020Sstevel@tonic-gate 	(void) strcat(fnip->addr.buf, ".autofs");
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if ((fnip->opts = malloc(MAX_MNTOPT_STR)) == NULL)
1050Sstevel@tonic-gate 		goto free_mem;
1060Sstevel@tonic-gate 	strcpy(fnip->opts, me->map_mntopts);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if (process_opts(fnip->opts, &fnip->direct, &sawnest) != 0)
1090Sstevel@tonic-gate 		goto free_mem;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	fnip->addr.len = strlen(fnip->addr.buf);
1120Sstevel@tonic-gate 	fnip->addr.maxlen = fnip->addr.len;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	/*
1150Sstevel@tonic-gate 	 * get absolute mountpoint
1160Sstevel@tonic-gate 	 */
1170Sstevel@tonic-gate 	if ((fnip->path = strdup(mntpnt)) == NULL)
1180Sstevel@tonic-gate 		goto free_mem;
1190Sstevel@tonic-gate 	if ((fnip->map = strdup(me->map_fs->mfs_dir)) == NULL)
1200Sstevel@tonic-gate 		goto free_mem;
1210Sstevel@tonic-gate 	if ((fnip->subdir = strdup(subdir)) == NULL)
1220Sstevel@tonic-gate 		goto free_mem;
123*2170Sevanl 
1240Sstevel@tonic-gate 	/*
1250Sstevel@tonic-gate 	 * This timeout is really ignored by autofs, it uses the
1260Sstevel@tonic-gate 	 * parent directory's timeout since it's really the one
1270Sstevel@tonic-gate 	 * specified/inherited from the original mount by 'automount'
1280Sstevel@tonic-gate 	 */
1290Sstevel@tonic-gate 	fnip->mount_to = mount_timeout;	/* IGNORED */
1300Sstevel@tonic-gate 	fnip->rpc_to = AUTOFS_RPC_TIMEOUT;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (fnip->direct) {
1330Sstevel@tonic-gate 		if (me->map_modified == TRUE || me->map_faked == TRUE) {
1340Sstevel@tonic-gate 			if ((fnip->key = strdup(key)) == NULL)
1350Sstevel@tonic-gate 				goto free_mem;
1360Sstevel@tonic-gate 		} else {
1370Sstevel@tonic-gate 			/* wierd case of a direct map pointer in another map */
1380Sstevel@tonic-gate 			if ((fnip->key = strdup(fnip->path)) == NULL)
1390Sstevel@tonic-gate 				goto free_mem;
1400Sstevel@tonic-gate 		}
1410Sstevel@tonic-gate 	} else {
1420Sstevel@tonic-gate 		fnip->key = NULL;
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * Fill out action list.
1470Sstevel@tonic-gate 	 */
1480Sstevel@tonic-gate 	alp->action.action = AUTOFS_MOUNT_RQ;
1490Sstevel@tonic-gate 	if ((alp->action.action_list_entry_u.mounta.spec =
1500Sstevel@tonic-gate 	    strdup(me->map_fs->mfs_dir)) == NULL)
1510Sstevel@tonic-gate 		goto free_mem;
1520Sstevel@tonic-gate 	if ((alp->action.action_list_entry_u.mounta.dir =
1530Sstevel@tonic-gate 	    strdup(rel_mntpnt)) == NULL)
1540Sstevel@tonic-gate 		goto free_mem;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	len = strlen(fnip->opts);
1570Sstevel@tonic-gate 	/*
1580Sstevel@tonic-gate 	 * Get a buffer for the option string, it holds the map options plus
1590Sstevel@tonic-gate 	 * space for "nest" if it isn't already in the option string
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	if ((buf = (char *)malloc(MAX_MNTOPT_STR)) == NULL)
1620Sstevel@tonic-gate 		goto free_mem;
1630Sstevel@tonic-gate 	strcpy(buf, fnip->opts);
1640Sstevel@tonic-gate 	if (!sawnest) {
1650Sstevel@tonic-gate 		if (len + strlen(",nest") + 1 > MAX_MNTOPT_STR)
1660Sstevel@tonic-gate 			goto free_mem;
1670Sstevel@tonic-gate 		if (len)
1680Sstevel@tonic-gate 			(void) strcat(buf, ",");
1690Sstevel@tonic-gate 		(void) strcat(buf, "nest");
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 	alp->action.action_list_entry_u.mounta.optptr = buf;
172*2170Sevanl 	alp->action.action_list_entry_u.mounta.optlen = strlen(buf) + 1;
1730Sstevel@tonic-gate 	alp->action.action_list_entry_u.mounta.flags =
1740Sstevel@tonic-gate 		mntflags | MS_DATA | MS_OPTIONSTR;
1750Sstevel@tonic-gate 	if ((alp->action.action_list_entry_u.mounta.fstype =
1760Sstevel@tonic-gate 	    strdup(MNTTYPE_AUTOFS)) == NULL)
1770Sstevel@tonic-gate 		goto free_mem;
1780Sstevel@tonic-gate 	alp->action.action_list_entry_u.mounta.dataptr = (char *)fnip;
1790Sstevel@tonic-gate 	alp->action.action_list_entry_u.mounta.datalen = sizeof (*fnip);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	return (0);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate free_mem:
1840Sstevel@tonic-gate 	/*
1850Sstevel@tonic-gate 	 * We got an error, free the memory we allocated.
1860Sstevel@tonic-gate 	 */
1870Sstevel@tonic-gate 	syslog(LOG_ERR, "mount_autofs: memory allocation failure");
1880Sstevel@tonic-gate 	free_autofs_args(fnip);
1890Sstevel@tonic-gate 	alp->action.action_list_entry_u.mounta.dataptr = NULL;
1900Sstevel@tonic-gate 	alp->action.action_list_entry_u.mounta.datalen = 0;
1910Sstevel@tonic-gate 	free_mounta(&alp->action.action_list_entry_u.mounta);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	return (error ? error : ENOMEM);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate  * Set *directp to 1 if "direct" is found, and 0 otherwise
1980Sstevel@tonic-gate  * (mounts are indirect by default).  If both "direct" and "indirect" are
1990Sstevel@tonic-gate  * found, the last one wins.
2000Sstevel@tonic-gate  */
2010Sstevel@tonic-gate static int
process_opts(char * options,int * directp,int * sawnestp)2020Sstevel@tonic-gate process_opts(char *options, int *directp, int *sawnestp)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	char *opt, *opts, *lasts;
2050Sstevel@tonic-gate 	char buf[AUTOFS_MAXOPTSLEN];
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	assert(strlen(options)+1 < AUTOFS_MAXOPTSLEN);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	*sawnestp = 0;
2100Sstevel@tonic-gate 	strcpy(buf, options);
2110Sstevel@tonic-gate 	opts = buf;
2120Sstevel@tonic-gate 	options[0] = '\0';
2130Sstevel@tonic-gate 	*directp = 0;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	while ((opt = strtok_r(opts, ",", &lasts)) != NULL) {
2160Sstevel@tonic-gate 		opts = NULL;
2170Sstevel@tonic-gate 		while (isspace(*opt)) {
2180Sstevel@tonic-gate 			opt++;
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 		if (strcmp(opt, "direct") == 0) {
2210Sstevel@tonic-gate 			*directp = 1;
2220Sstevel@tonic-gate 		} else if (strcmp(opt, "indirect") == 0) {
2230Sstevel@tonic-gate 			*directp = 0;
2240Sstevel@tonic-gate 		} else if (strcmp(opt, "ignore") != 0) {
2250Sstevel@tonic-gate 			if (strcmp(opt, "nest") == 0) {
2260Sstevel@tonic-gate 				*sawnestp = 1;
2270Sstevel@tonic-gate 			}
2280Sstevel@tonic-gate 			if (options[0] != '\0') {
2290Sstevel@tonic-gate 				(void) strcat(options, ",");
2300Sstevel@tonic-gate 			}
2310Sstevel@tonic-gate 			(void) strcat(options, opt);
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 	};
2340Sstevel@tonic-gate 	return (0);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate  * Free autofs_args structure
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate void
free_autofs_args(autofs_args * p)241*2170Sevanl free_autofs_args(autofs_args *p)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	if (p == NULL)
2440Sstevel@tonic-gate 		return;
2450Sstevel@tonic-gate 	if (p->addr.buf)
2460Sstevel@tonic-gate 		free(p->addr.buf);
2470Sstevel@tonic-gate 	if (p->path)
2480Sstevel@tonic-gate 		free(p->path);
2490Sstevel@tonic-gate 	if (p->opts)
2500Sstevel@tonic-gate 		free(p->opts);
2510Sstevel@tonic-gate 	if (p->map)
2520Sstevel@tonic-gate 		free(p->map);
2530Sstevel@tonic-gate 	if (p->subdir)
2540Sstevel@tonic-gate 		free(p->subdir);
2550Sstevel@tonic-gate 	if (p->key)
2560Sstevel@tonic-gate 		free(p->key);
2570Sstevel@tonic-gate 	free(p);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate /*
2610Sstevel@tonic-gate  * free mounta structure. Assumes that m->dataptr has
2620Sstevel@tonic-gate  * been freed already
2630Sstevel@tonic-gate  */
2640Sstevel@tonic-gate void
free_mounta(struct mounta * m)2650Sstevel@tonic-gate free_mounta(struct mounta *m)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate 	if (m == NULL)
2680Sstevel@tonic-gate 		return;
2690Sstevel@tonic-gate 	if (m->spec)
2700Sstevel@tonic-gate 		free(m->spec);
2710Sstevel@tonic-gate 	if (m->dir)
2720Sstevel@tonic-gate 		free(m->dir);
2730Sstevel@tonic-gate 	if (m->fstype)
2740Sstevel@tonic-gate 		free(m->fstype);
2750Sstevel@tonic-gate 	if (m->optptr)
2760Sstevel@tonic-gate 		free(m->optptr);
2770Sstevel@tonic-gate 	assert(m->dataptr == NULL);
2780Sstevel@tonic-gate 	assert(m->datalen == 0);
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * no need to free 'm' since it is part of the
2810Sstevel@tonic-gate 	 * action_list_entry structure.
2820Sstevel@tonic-gate 	 */
2830Sstevel@tonic-gate }
284