1 /*-
2  * Copyright (c) 2018 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Christos Zoulas.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/mount.h>
32 
33 #include <err.h>
34 #include <mntopts.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sysexits.h>
39 #include <unistd.h>
40 
41 #include <vfs/autofs/autofs_mount.h>
42 
43 #include "mount_autofs.h"
44 
45 /*
46  * Copied from NetBSD.
47  * There is a reason to have const char*, so keep them const.
48  */
49 #define __UNCONST(a)    ((void *)(unsigned long)(const void *)(a))
50 
51 static const struct mntopt mopts[] = {
52 	MOPT_STDOPTS,
53 	MOPT_NULL,
54 };
55 
56 static void	usage(void) __dead2;
57 
58 int
59 main(int argc, char **argv)
60 {
61 	return mount_autofs(argc, argv);
62 }
63 
64 void
65 mount_autofs_parseargs(int argc, char *argv[], void *v, int *mntflags,
66 	char *canon_dev, char *canon_dir)
67 {
68 	int ch;
69 	struct autofs_mount_info *am = v;
70 
71 	*mntflags = 0;
72 	while ((ch = getopt(argc, argv, "f:o:O:p:")) != -1)
73 		switch (ch) {
74 		case 'f':
75 			strlcpy(__UNCONST(am->from), optarg, MAXPATHLEN);
76 			break;
77 		case 'o':
78 			getmntopts(optarg, mopts, mntflags, 0);
79 			break;
80 		case 'O':
81 			strlcpy(__UNCONST(am->master_options), optarg,
82 			    MAXPATHLEN);
83 			break;
84 		case 'p':
85 			strlcpy(__UNCONST(am->master_prefix), optarg,
86 			    MAXPATHLEN);
87 			break;
88 		case '?':
89 		default:
90 			usage();
91 		}
92 	argc -= optind;
93 	argv += optind;
94 
95 	if (argc != 2)
96 		usage();
97 
98 	strlcpy(canon_dev, argv[0], MAXPATHLEN);
99 	if (realpath(argv[1], canon_dir) == NULL)    /* Check mounton path */
100 		err(EXIT_FAILURE, "realpath %s", canon_dir);
101 	if (strncmp(argv[1], canon_dir, MAXPATHLEN)) {
102 		warnx("\"%s\" is a relative path.", argv[1]);
103 		warnx("using \"%s\" instead.", canon_dir);
104 	}
105 }
106 
107 int
108 mount_autofs(int argc, char *argv[])
109 {
110 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
111 	char from[MAXPATHLEN], master_options[MAXPATHLEN];
112 	char master_prefix[MAXPATHLEN];
113 	int mntflags, error;
114 	struct vfsconf vfc;
115 	struct autofs_mount_info am = {
116 		.from = from,
117 		.master_options = master_options,
118 		.master_prefix = master_prefix,
119 	};
120 
121 	mount_autofs_parseargs(argc, argv, &am, &mntflags,
122 	    canon_dev, canon_dir);
123 
124 	error = getvfsbyname("autofs", &vfc);
125 	if (error && vfsisloadable("autofs")) {
126 		if(vfsload("autofs"))
127 			err(EX_OSERR, "vfsload(%s)", "autofs");
128 		endvfsent();
129 		error = getvfsbyname("autofs", &vfc);
130 	}
131 	if (error)
132 		errx(EX_OSERR, "%s filesystem not available", "autofs");
133 
134 	if (mount(vfc.vfc_name, canon_dir, mntflags, &am) == -1)
135 		err(EXIT_FAILURE, "autofs on %s", canon_dir);
136 
137 	return EXIT_SUCCESS;
138 }
139 
140 static void
141 usage(void)
142 {
143 	(void)fprintf(stderr,
144 	    "Usage: %s [-o options] [-O master_options] [-p master_prefix] "
145 		"[-f <from>] autofs mount_point\n", getprogname());
146 	exit(EXIT_FAILURE);
147 }
148