mount_ptyfs.c revision 1.16
11.16Schristos/*	$NetBSD: mount_ptyfs.c,v 1.16 2016/02/21 22:51:29 christos Exp $	*/
21.1Schristos
31.1Schristos/*
41.1Schristos * Copyright (c) 1992, 1993, 1994
51.1Schristos *	The Regents of the University of California.  All rights reserved.
61.1Schristos *
71.1Schristos * This code is derived from software contributed to Berkeley by
81.1Schristos * Jan-Simon Pendry.
91.1Schristos *
101.1Schristos * Redistribution and use in source and binary forms, with or without
111.1Schristos * modification, are permitted provided that the following conditions
121.1Schristos * are met:
131.1Schristos * 1. Redistributions of source code must retain the above copyright
141.1Schristos *    notice, this list of conditions and the following disclaimer.
151.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
161.1Schristos *    notice, this list of conditions and the following disclaimer in the
171.1Schristos *    documentation and/or other materials provided with the distribution.
181.1Schristos * 3. Neither the name of the University nor the names of its contributors
191.1Schristos *    may be used to endorse or promote products derived from this software
201.1Schristos *    without specific prior written permission.
211.1Schristos *
221.1Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231.1Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241.1Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251.1Schristos * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261.1Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271.1Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281.1Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291.1Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301.1Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311.1Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321.1Schristos * SUCH DAMAGE.
331.1Schristos */
341.1Schristos
351.1Schristos/*
361.1Schristos * Copyright (c) 1990, 1992 Jan-Simon Pendry
371.1Schristos *
381.1Schristos * This code is derived from software contributed to Berkeley by
391.1Schristos * Jan-Simon Pendry.
401.1Schristos *
411.1Schristos * Redistribution and use in source and binary forms, with or without
421.1Schristos * modification, are permitted provided that the following conditions
431.1Schristos * are met:
441.1Schristos * 1. Redistributions of source code must retain the above copyright
451.1Schristos *    notice, this list of conditions and the following disclaimer.
461.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
471.1Schristos *    notice, this list of conditions and the following disclaimer in the
481.1Schristos *    documentation and/or other materials provided with the distribution.
491.1Schristos * 3. All advertising materials mentioning features or use of this software
501.1Schristos *    must display the following acknowledgement:
511.1Schristos *	This product includes software developed by the University of
521.1Schristos *	California, Berkeley and its contributors.
531.1Schristos * 4. Neither the name of the University nor the names of its contributors
541.1Schristos *    may be used to endorse or promote products derived from this software
551.1Schristos *    without specific prior written permission.
561.1Schristos *
571.1Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
581.1Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
591.1Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
601.1Schristos * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
611.1Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
621.1Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
631.1Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
641.1Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
651.1Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
661.1Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
671.1Schristos * SUCH DAMAGE.
681.1Schristos */
691.1Schristos
701.1Schristos#include <sys/cdefs.h>
711.1Schristos#ifndef lint
721.9Slukem__COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
731.9Slukem The Regents of the University of California.  All rights reserved.");
741.1Schristos#endif /* not lint */
751.1Schristos
761.1Schristos#ifndef lint
771.1Schristos#if 0
781.1Schristosstatic char sccsid[] = "@(#)mount_ptyfs.c	8.3 (Berkeley) 5/4/95";
791.1Schristos#else
801.16Schristos__RCSID("$NetBSD: mount_ptyfs.c,v 1.16 2016/02/21 22:51:29 christos Exp $");
811.1Schristos#endif
821.1Schristos#endif /* not lint */
831.1Schristos
841.1Schristos#include <sys/param.h>
851.2Schristos#include <sys/types.h>
861.1Schristos#include <sys/mount.h>
871.2Schristos#include <sys/stat.h>
881.2Schristos
891.2Schristos#include <fs/ptyfs/ptyfs.h>
901.1Schristos
911.1Schristos#include <err.h>
921.1Schristos#include <unistd.h>
931.1Schristos#include <stdio.h>
941.1Schristos#include <stdlib.h>
951.1Schristos#include <string.h>
961.2Schristos#include <grp.h>
971.10Schristos#include <util.h>
981.1Schristos
991.1Schristos#include <mntopts.h>
1001.1Schristos
1011.11Sriastrad#define ALTF_GROUP	0x1
1021.11Sriastrad#define ALTF_MODE	0x2
1031.15Schristos#define ALTF_CHROOT	0x4			/* compat */
1041.2Schristos
1051.1Schristosstatic const struct mntopt mopts[] = {
1061.1Schristos	MOPT_STDOPTS,
1071.1Schristos	MOPT_GETARGS,
1081.16Schristos	MOPT_NOATIME,
1091.16Schristos	MOPT_RELATIME,
1101.2Schristos	{ "group", 0, ALTF_GROUP, 1 },
1111.2Schristos	{ "mode", 0, ALTF_MODE, 1 },
1121.15Schristos	{ "chroot", 0, ALTF_CHROOT, 1 },	/* compat */
1131.6Schristos	MOPT_NULL,
1141.1Schristos};
1151.1Schristos
1161.1Schristosint	main(int, char *[]);
1171.1Schristosint	mount_ptyfs(int argc, char **argv);
1181.2Schristos
1191.2Schristosstatic gid_t	getgrp(const char *name);
1201.12Sjoerg__dead static void	usage(void);
1211.1Schristos
1221.1Schristos#ifndef MOUNT_NOMAIN
1231.1Schristosint
1241.1Schristosmain(int argc, char *argv[])
1251.1Schristos{
1261.1Schristos	return mount_ptyfs(argc, argv);
1271.1Schristos}
1281.1Schristos#endif
1291.1Schristos
1301.2Schristosstatic gid_t
1311.2Schristosgetgrp(const char *name)
1321.2Schristos{
1331.2Schristos	char *ep;
1341.2Schristos	struct group *grp;
1351.2Schristos	long l;
1361.2Schristos
1371.2Schristos	if (name == NULL)
1381.2Schristos		errx(1, "Missing group name");
1391.2Schristos
1401.2Schristos	l = strtol(name, &ep, 0);
1411.2Schristos
1421.2Schristos	if (name == ep || *ep)
1431.2Schristos		grp = getgrnam(name);
1441.2Schristos	else
1451.2Schristos		grp = getgrgid((gid_t)l);
1461.2Schristos
1471.2Schristos	if (grp == NULL)
1481.2Schristos		errx(1, "Cannot find group `%s'", name);
1491.2Schristos
1501.2Schristos	return grp->gr_gid;
1511.2Schristos}
1521.2Schristos
1531.2Schristos
1541.1Schristosint
1551.1Schristosmount_ptyfs(int argc, char *argv[])
1561.1Schristos{
1571.2Schristos	int ch, mntflags = 0, altflags = 0;
1581.2Schristos	struct ptyfs_args args;
1591.2Schristos	mntoptparse_t mp;
1601.4Serh	char canon_dir[MAXPATHLEN];
1611.2Schristos
1621.1Schristos
1631.1Schristos	setprogname(argv[0]);
1641.1Schristos
1651.2Schristos	args.version = PTYFS_ARGSVERSION;
1661.2Schristos	args.gid = getgrp("tty");
1671.2Schristos	args.mode = S_IRUSR|S_IWUSR|S_IWGRP;
1681.10Schristos	args.flags = 0;
1691.2Schristos
1701.15Schristos	while ((ch = getopt(argc, argv, "cg:m:o:")) != -1)
1711.1Schristos		switch (ch) {
1721.15Schristos		case 'c':	/* compat */
1731.15Schristos		compat:
1741.15Schristos			warnx("-c and -o chroot options are obsolete");
1751.15Schristos			break;
1761.1Schristos		case 'o':
1771.2Schristos			altflags = 0;
1781.2Schristos			mp = getmntopts(optarg, mopts, &mntflags, &altflags);
1791.5Schristos			if (mp == NULL)
1801.5Schristos				err(1, "getmntopts");
1811.2Schristos			if (altflags & ALTF_GROUP)
1821.2Schristos				args.gid = getgrp(getmntoptstr(mp, "group"));
1831.2Schristos			if (altflags & ALTF_MODE)
1841.2Schristos				args.mode = (mode_t)getmntoptnum(mp, "mode");
1851.15Schristos			if (altflags & ALTF_CHROOT)
1861.15Schristos				goto compat;
1871.2Schristos			freemntopts(mp);
1881.2Schristos			break;
1891.2Schristos		case 'g':
1901.2Schristos			args.gid = getgrp(optarg);
1911.2Schristos			break;
1921.2Schristos		case 'm':
1931.2Schristos			args.mode = (mode_t)strtol(optarg, NULL, 0);
1941.1Schristos			break;
1951.1Schristos		case '?':
1961.1Schristos		default:
1971.1Schristos			usage();
1981.1Schristos		}
1991.2Schristos
2001.1Schristos	argc -= optind;
2011.1Schristos	argv += optind;
2021.1Schristos
2031.1Schristos	if (argc != 2)
2041.1Schristos		usage();
2051.1Schristos
2061.4Serh	if (realpath(argv[1], canon_dir) == NULL)   /* Check mounton path */
2071.4Serh		err(1, "realpath %s", argv[1]);
2081.4Serh	if (strncmp(argv[1], canon_dir, MAXPATHLEN)) {
2091.4Serh		warnx("\"%s\" is a relative path.", argv[1]);
2101.4Serh		warnx("using \"%s\" instead.", canon_dir);
2111.4Serh	}
2121.4Serh
2131.8Spooka	if (mount(MOUNT_PTYFS, canon_dir, mntflags, &args, sizeof args) == -1)
2141.4Serh		err(1, "ptyfs on %s", canon_dir);
2151.10Schristos	if (mntflags & MNT_GETARGS) {
2161.14Schristos		printf("version=%d, gid=%lu, mode=0%o\n", args.version,
2171.14Schristos		    (unsigned long)args.gid, args.mode);
2181.10Schristos	}
2191.2Schristos	return 0;
2201.1Schristos}
2211.1Schristos
2221.1Schristosstatic void
2231.1Schristosusage(void)
2241.1Schristos{
2251.1Schristos	(void)fprintf(stderr,
2261.14Schristos	    "Usage: %s [-g <group|gid>] [-m <mode>] [-o options] "
2271.10Schristos	    "ptyfs mountpoint\n", getprogname());
2281.1Schristos	exit(1);
2291.1Schristos}
230