xref: /netbsd-src/sbin/mount_ptyfs/mount_ptyfs.c (revision fd33bfb7f2a74c2ddd9aea91bb08026696cc083c)
1*fd33bfb7Smlelstv /*	$NetBSD: mount_ptyfs.c,v 1.18 2020/07/26 08:20:23 mlelstv Exp $	*/
21bd4f21dSchristos 
31bd4f21dSchristos /*
41bd4f21dSchristos  * Copyright (c) 1992, 1993, 1994
51bd4f21dSchristos  *	The Regents of the University of California.  All rights reserved.
61bd4f21dSchristos  *
71bd4f21dSchristos  * This code is derived from software contributed to Berkeley by
81bd4f21dSchristos  * Jan-Simon Pendry.
91bd4f21dSchristos  *
101bd4f21dSchristos  * Redistribution and use in source and binary forms, with or without
111bd4f21dSchristos  * modification, are permitted provided that the following conditions
121bd4f21dSchristos  * are met:
131bd4f21dSchristos  * 1. Redistributions of source code must retain the above copyright
141bd4f21dSchristos  *    notice, this list of conditions and the following disclaimer.
151bd4f21dSchristos  * 2. Redistributions in binary form must reproduce the above copyright
161bd4f21dSchristos  *    notice, this list of conditions and the following disclaimer in the
171bd4f21dSchristos  *    documentation and/or other materials provided with the distribution.
181bd4f21dSchristos  * 3. Neither the name of the University nor the names of its contributors
191bd4f21dSchristos  *    may be used to endorse or promote products derived from this software
201bd4f21dSchristos  *    without specific prior written permission.
211bd4f21dSchristos  *
221bd4f21dSchristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231bd4f21dSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241bd4f21dSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251bd4f21dSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261bd4f21dSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271bd4f21dSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281bd4f21dSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291bd4f21dSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301bd4f21dSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311bd4f21dSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321bd4f21dSchristos  * SUCH DAMAGE.
331bd4f21dSchristos  */
341bd4f21dSchristos 
351bd4f21dSchristos /*
361bd4f21dSchristos  * Copyright (c) 1990, 1992 Jan-Simon Pendry
371bd4f21dSchristos  *
381bd4f21dSchristos  * This code is derived from software contributed to Berkeley by
391bd4f21dSchristos  * Jan-Simon Pendry.
401bd4f21dSchristos  *
411bd4f21dSchristos  * Redistribution and use in source and binary forms, with or without
421bd4f21dSchristos  * modification, are permitted provided that the following conditions
431bd4f21dSchristos  * are met:
441bd4f21dSchristos  * 1. Redistributions of source code must retain the above copyright
451bd4f21dSchristos  *    notice, this list of conditions and the following disclaimer.
461bd4f21dSchristos  * 2. Redistributions in binary form must reproduce the above copyright
471bd4f21dSchristos  *    notice, this list of conditions and the following disclaimer in the
481bd4f21dSchristos  *    documentation and/or other materials provided with the distribution.
491bd4f21dSchristos  * 3. All advertising materials mentioning features or use of this software
501bd4f21dSchristos  *    must display the following acknowledgement:
511bd4f21dSchristos  *	This product includes software developed by the University of
521bd4f21dSchristos  *	California, Berkeley and its contributors.
531bd4f21dSchristos  * 4. Neither the name of the University nor the names of its contributors
541bd4f21dSchristos  *    may be used to endorse or promote products derived from this software
551bd4f21dSchristos  *    without specific prior written permission.
561bd4f21dSchristos  *
571bd4f21dSchristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
581bd4f21dSchristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
591bd4f21dSchristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
601bd4f21dSchristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
611bd4f21dSchristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
621bd4f21dSchristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
631bd4f21dSchristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
641bd4f21dSchristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
651bd4f21dSchristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
661bd4f21dSchristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
671bd4f21dSchristos  * SUCH DAMAGE.
681bd4f21dSchristos  */
691bd4f21dSchristos 
701bd4f21dSchristos #include <sys/cdefs.h>
711bd4f21dSchristos #ifndef lint
726543a91fSlukem __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
736543a91fSlukem  The Regents of the University of California.  All rights reserved.");
741bd4f21dSchristos #endif /* not lint */
751bd4f21dSchristos 
761bd4f21dSchristos #ifndef lint
771bd4f21dSchristos #if 0
781bd4f21dSchristos static char sccsid[] = "@(#)mount_ptyfs.c	8.3 (Berkeley) 5/4/95";
791bd4f21dSchristos #else
80*fd33bfb7Smlelstv __RCSID("$NetBSD: mount_ptyfs.c,v 1.18 2020/07/26 08:20:23 mlelstv Exp $");
811bd4f21dSchristos #endif
821bd4f21dSchristos #endif /* not lint */
831bd4f21dSchristos 
841bd4f21dSchristos #include <sys/param.h>
85f11900e3Schristos #include <sys/types.h>
861bd4f21dSchristos #include <sys/mount.h>
87f11900e3Schristos #include <sys/stat.h>
88f11900e3Schristos 
89f11900e3Schristos #include <fs/ptyfs/ptyfs.h>
901bd4f21dSchristos 
911bd4f21dSchristos #include <err.h>
921bd4f21dSchristos #include <unistd.h>
931bd4f21dSchristos #include <stdio.h>
941bd4f21dSchristos #include <stdlib.h>
951bd4f21dSchristos #include <string.h>
96f11900e3Schristos #include <grp.h>
979f97ab94Schristos #include <util.h>
981bd4f21dSchristos 
991bd4f21dSchristos #include <mntopts.h>
1001bd4f21dSchristos 
101*fd33bfb7Smlelstv #include "mountprog.h"
102*fd33bfb7Smlelstv 
103d9e31e35Sriastradh #define ALTF_GROUP	0x1
104d9e31e35Sriastradh #define ALTF_MODE	0x2
1059db5ca4eSchristos #define ALTF_CHROOT	0x4			/* compat */
106f11900e3Schristos 
1071bd4f21dSchristos static const struct mntopt mopts[] = {
1081bd4f21dSchristos 	MOPT_STDOPTS,
1091bd4f21dSchristos 	MOPT_GETARGS,
110a0647817Schristos 	MOPT_NOATIME,
111a0647817Schristos 	MOPT_RELATIME,
112f11900e3Schristos 	{ "group", 0, ALTF_GROUP, 1 },
113f11900e3Schristos 	{ "mode", 0, ALTF_MODE, 1 },
1149db5ca4eSchristos 	{ "chroot", 0, ALTF_CHROOT, 1 },	/* compat */
1155afd1c0aSchristos 	MOPT_NULL,
1161bd4f21dSchristos };
1171bd4f21dSchristos 
1181bd4f21dSchristos int	mount_ptyfs(int argc, char **argv);
119f11900e3Schristos 
120f11900e3Schristos static gid_t	getgrp(const char *name);
121baa8e84bSjoerg __dead static void	usage(void);
1221bd4f21dSchristos 
1231bd4f21dSchristos #ifndef MOUNT_NOMAIN
1241bd4f21dSchristos int
main(int argc,char * argv[])1251bd4f21dSchristos main(int argc, char *argv[])
1261bd4f21dSchristos {
1271bd4f21dSchristos 	return mount_ptyfs(argc, argv);
1281bd4f21dSchristos }
1291bd4f21dSchristos #endif
1301bd4f21dSchristos 
131f11900e3Schristos static gid_t
getgrp(const char * name)132f11900e3Schristos getgrp(const char *name)
133f11900e3Schristos {
134f11900e3Schristos 	char *ep;
135f11900e3Schristos 	struct group *grp;
136f11900e3Schristos 	long l;
137f11900e3Schristos 
138f11900e3Schristos 	if (name == NULL)
139f11900e3Schristos 		errx(1, "Missing group name");
140f11900e3Schristos 
141f11900e3Schristos 	l = strtol(name, &ep, 0);
142f11900e3Schristos 
143f11900e3Schristos 	if (name == ep || *ep)
144f11900e3Schristos 		grp = getgrnam(name);
145f11900e3Schristos 	else
146f11900e3Schristos 		grp = getgrgid((gid_t)l);
147f11900e3Schristos 
148f11900e3Schristos 	if (grp == NULL)
149f11900e3Schristos 		errx(1, "Cannot find group `%s'", name);
150f11900e3Schristos 
151f11900e3Schristos 	return grp->gr_gid;
152f11900e3Schristos }
153f11900e3Schristos 
154f11900e3Schristos 
1551bd4f21dSchristos int
mount_ptyfs(int argc,char * argv[])1561bd4f21dSchristos mount_ptyfs(int argc, char *argv[])
1571bd4f21dSchristos {
158f11900e3Schristos 	int ch, mntflags = 0, altflags = 0;
159f11900e3Schristos 	struct ptyfs_args args;
160f11900e3Schristos 	mntoptparse_t mp;
161fdd16c54Serh 	char canon_dir[MAXPATHLEN];
162f11900e3Schristos 
1631bd4f21dSchristos 
1641bd4f21dSchristos 	setprogname(argv[0]);
1651bd4f21dSchristos 
166f11900e3Schristos 	args.version = PTYFS_ARGSVERSION;
167f11900e3Schristos 	args.gid = getgrp("tty");
168f11900e3Schristos 	args.mode = S_IRUSR|S_IWUSR|S_IWGRP;
1699f97ab94Schristos 	args.flags = 0;
170f11900e3Schristos 
1719db5ca4eSchristos 	while ((ch = getopt(argc, argv, "cg:m:o:")) != -1)
1721bd4f21dSchristos 		switch (ch) {
1739db5ca4eSchristos 		case 'c':	/* compat */
1749db5ca4eSchristos 		compat:
1759db5ca4eSchristos 			warnx("-c and -o chroot options are obsolete");
1769db5ca4eSchristos 			break;
1771bd4f21dSchristos 		case 'o':
178f11900e3Schristos 			altflags = 0;
179f11900e3Schristos 			mp = getmntopts(optarg, mopts, &mntflags, &altflags);
1807067dcb9Schristos 			if (mp == NULL)
1817067dcb9Schristos 				err(1, "getmntopts");
182f11900e3Schristos 			if (altflags & ALTF_GROUP)
183f11900e3Schristos 				args.gid = getgrp(getmntoptstr(mp, "group"));
184f11900e3Schristos 			if (altflags & ALTF_MODE)
185f11900e3Schristos 				args.mode = (mode_t)getmntoptnum(mp, "mode");
1869db5ca4eSchristos 			if (altflags & ALTF_CHROOT)
1879db5ca4eSchristos 				goto compat;
188f11900e3Schristos 			freemntopts(mp);
189f11900e3Schristos 			break;
190f11900e3Schristos 		case 'g':
191f11900e3Schristos 			args.gid = getgrp(optarg);
192f11900e3Schristos 			break;
193f11900e3Schristos 		case 'm':
194f11900e3Schristos 			args.mode = (mode_t)strtol(optarg, NULL, 0);
1951bd4f21dSchristos 			break;
1961bd4f21dSchristos 		case '?':
1971bd4f21dSchristos 		default:
1981bd4f21dSchristos 			usage();
1991bd4f21dSchristos 		}
200f11900e3Schristos 
2011bd4f21dSchristos 	argc -= optind;
2021bd4f21dSchristos 	argv += optind;
2031bd4f21dSchristos 
2041bd4f21dSchristos 	if (argc != 2)
2051bd4f21dSchristos 		usage();
2061bd4f21dSchristos 
207*fd33bfb7Smlelstv 	pathadj(argv[1], canon_dir);
208fdd16c54Serh 
209704e0753Spooka 	if (mount(MOUNT_PTYFS, canon_dir, mntflags, &args, sizeof args) == -1)
210fdd16c54Serh 		err(1, "ptyfs on %s", canon_dir);
2119f97ab94Schristos 	if (mntflags & MNT_GETARGS) {
2127097f02aSchristos 		printf("version=%d, gid=%lu, mode=0%o\n", args.version,
2137097f02aSchristos 		    (unsigned long)args.gid, args.mode);
2149f97ab94Schristos 	}
215f11900e3Schristos 	return 0;
2161bd4f21dSchristos }
2171bd4f21dSchristos 
2181bd4f21dSchristos static void
usage(void)2191bd4f21dSchristos usage(void)
2201bd4f21dSchristos {
2211bd4f21dSchristos 	(void)fprintf(stderr,
2227097f02aSchristos 	    "Usage: %s [-g <group|gid>] [-m <mode>] [-o options] "
2239f97ab94Schristos 	    "ptyfs mountpoint\n", getprogname());
2241bd4f21dSchristos 	exit(1);
2251bd4f21dSchristos }
226