1/*	$OpenBSD: mount_tmpfs.c,v 1.8 2022/12/04 23:50:47 cheloha Exp $	*/
2/*	$NetBSD: mount_tmpfs.c,v 1.24 2008/08/05 20:57:45 pooka Exp $	*/
3
4/*
5 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
10 * 2005 program.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/types.h>
35#include <sys/mount.h>
36#include <sys/stat.h>
37
38#include <ctype.h>
39#include <err.h>
40#include <errno.h>
41#include <grp.h>
42#include <mntopts.h>
43#include <pwd.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48#include <limits.h>
49#include <util.h>
50
51#include "mount_tmpfs.h"
52
53/* --------------------------------------------------------------------- */
54
55static const struct mntopt mopts[] = {
56	MOPT_STDOPTS,
57	MOPT_WXALLOWED,
58	MOPT_UPDATE,
59	{ NULL },
60};
61
62/* --------------------------------------------------------------------- */
63
64static void	usage(void) __dead;
65static uid_t	a_uid(const char *);
66static gid_t	a_gid(const char *);
67static uid_t	a_gid(const char *);
68static int	a_num(const char *, const char *);
69static mode_t	a_mask(const char *);
70static void	pathadj(const char *, char *);
71
72/* --------------------------------------------------------------------- */
73
74void
75mount_tmpfs_parseargs(int argc, char *argv[],
76	struct tmpfs_args *args, int *mntflags,
77	char *canon_dev, char *canon_dir)
78{
79	int gidset, modeset, uidset; /* Ought to be 'bool'. */
80	int ch;
81	gid_t gid;
82	uid_t uid;
83	mode_t mode;
84	int64_t tmpnumber;
85	struct stat sb;
86
87	/* Set default values for mount point arguments. */
88	memset(args, 0, sizeof(*args));
89	args->ta_version = TMPFS_ARGS_VERSION;
90	args->ta_size_max = 0;
91	args->ta_nodes_max = 0;
92	*mntflags = 0;
93
94	gidset = 0; gid = 0;
95	uidset = 0; uid = 0;
96	modeset = 0; mode = 0;
97
98	optind = optreset = 1;
99	while ((ch = getopt(argc, argv, "g:m:n:o:s:u:")) != -1 ) {
100		switch (ch) {
101		case 'g':
102			gid = a_gid(optarg);
103			gidset = 1;
104			break;
105
106		case 'm':
107			mode = a_mask(optarg);
108			modeset = 1;
109			break;
110
111		case 'n':
112
113			if (scan_scaled(optarg, &tmpnumber) == -1)
114				err(EXIT_FAILURE, "failed to parse nodes `%s'",
115				    optarg);
116			args->ta_nodes_max = tmpnumber;
117			break;
118
119		case 'o':
120			getmntopts(optarg, mopts, mntflags);
121			break;
122
123		case 's':
124			if (scan_scaled(optarg, &tmpnumber) == -1)
125				err(EXIT_FAILURE, "failed to parse size `%s'",
126				    optarg);
127			args->ta_size_max = tmpnumber;
128			break;
129
130		case 'u':
131			uid = a_uid(optarg);
132			uidset = 1;
133			break;
134
135		default:
136			usage();
137		}
138	}
139	argc -= optind;
140	argv += optind;
141
142	if (argc != 2)
143		usage();
144
145	strlcpy(canon_dev, argv[0], PATH_MAX);
146	pathadj(argv[1], canon_dir);
147
148	if (stat(canon_dir, &sb) == -1)
149		err(EXIT_FAILURE, "cannot stat `%s'", canon_dir);
150
151	args->ta_root_uid = uidset ? uid : sb.st_uid;
152	args->ta_root_gid = gidset ? gid : sb.st_gid;
153	args->ta_root_mode = modeset ? mode : sb.st_mode;
154}
155
156/* --------------------------------------------------------------------- */
157
158static void
159usage(void)
160{
161	extern char *__progname;
162	(void)fprintf(stderr,
163	    "usage: %s [-g group] [-m mode] [-n nodes] [-o options] [-s size]\n"
164	    "           [-u user] tmpfs mount_point\n", __progname);
165	exit(1);
166}
167
168/* --------------------------------------------------------------------- */
169
170int
171mount_tmpfs(int argc, char *argv[])
172{
173	struct tmpfs_args args;
174	char canon_dev[PATH_MAX], canon_dir[PATH_MAX];
175	int mntflags;
176
177	mount_tmpfs_parseargs(argc, argv, &args, &mntflags,
178	    canon_dev, canon_dir);
179
180	if (mount(MOUNT_TMPFS, canon_dir, mntflags, &args) == -1)
181		err(EXIT_FAILURE, "tmpfs on %s", canon_dir);
182
183	return EXIT_SUCCESS;
184}
185
186int
187main(int argc, char *argv[])
188{
189
190	/* setprogname(argv[0]); */
191	return mount_tmpfs(argc, argv);
192}
193
194static uid_t
195a_uid(const char *s)
196{
197	struct passwd *pw;
198
199	if ((pw = getpwnam(s)) != NULL)
200		return pw->pw_uid;
201	return a_num(s, "user");
202}
203
204static gid_t
205a_gid(const char *s)
206{
207	struct group *gr;
208
209	if ((gr = getgrnam(s)) != NULL)
210		return gr->gr_gid;
211	return a_num(s, "group");
212}
213
214static int
215a_num(const char *s, const char *id_type)
216{
217	int id;
218	char *ep;
219
220	id = strtol(s, &ep, 0);
221	if (*ep || s == ep || id < 0)
222		errx(1, "unknown %s id: %s", id_type, s);
223	return id;
224}
225
226static mode_t
227a_mask(const char *s)
228{
229	int rv;
230	char *ep;
231
232	rv = strtol(s, &ep, 8);
233	if (s == ep || *ep || rv < 0)
234		errx(1, "invalid file mode: %s", s);
235	return rv;
236}
237
238static void
239pathadj(const char *input, char *adjusted)
240{
241
242	if (realpath(input, adjusted) == NULL)
243		err(1, "realpath %s", input);
244	if (strncmp(input, adjusted, PATH_MAX)) {
245		warnx("\"%s\" is a relative path.", input);
246		warnx("using \"%s\" instead.", adjusted);
247	}
248}
249