summaryrefslogtreecommitdiff
path: root/9mount.c
blob: 65bccac74cfabc2670992b4e16926f421380831c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* © 2008 sqweek <sqweek@gmail.com>
 * See COPYING for details.
 */
#include <err.h>
#include <mntent.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/mount.h>
#include <arpa/inet.h>
#include <pwd.h>
#include <netdb.h>

#define nelem(x) (sizeof(x)/sizeof(*(x)))

struct {char *mnemonic; int mask;} debug_flags[] = {
	{"err", 0x001},
	{"devel", 0x002},
	{"9p", 0x004},
	{"vfs", 0x008},
	{"conv", 0x010},
	{"mux", 0x020},
	{"trans", 0x040},
	{"alloc", 0x080},
	{"fcall", 0x100}
};

char*
append(char **dest, char *src, int *destlen)
{
	while (strlen(*dest) + 1 + strlen(src) > *destlen)
		*destlen *= 2;
	if (!(*dest=realloc(*dest, *destlen)))
		errx(1, "out of memory");

	if (**dest)
		strcat(*dest, ",");
	strcat(*dest, src);
	return *dest;
}

char*
getarg(char opt, char *cp, char*** argv)
{
	if (*(cp+1)) {
		return cp+1;
	} else if (*(*argv+1)) {
		return *++(*argv);
	} else {
		errx(1, "-%c: expected argument", opt);
	}
	return NULL;
}

void
parsedial(char *dial, char **network, char **netaddr, int *port)
{
	char *cp;
	if (!(*network=strtok(dial, "!"))) {
		errx(1, "empty dial string");
	}
	if (strcmp(*network, "unix") != 0
	&& strcmp(*network, "tcp") != 0
	&& strcmp(*network, "virtio") != 0) {
		errx(1, "%s: unknown network (expecting unix, tcp or virtio)", *network);
	}
	if (!(*netaddr=strtok(NULL, "!"))) {
		errx(1, "missing dial netaddress");
	}
	if (strcmp(*network, "tcp") == 0) {
		char *service;
		if ((service=strtok(NULL, "!"))) {
			if (strspn(service, "0123456789") == strlen(service)) {
				*port = atoi(service);
			} else {
				struct servent *sv;
				if ((sv=getservbyname(service, *network))) {
					/* sv->s_port is a 16-bit big endian masquerading as an int */
					*port = ntohs((uint16_t)sv->s_port);
					endservent();
				} else {
					errx(1, "%s: unknown service", service);
				}
			}
		}
	}
	if ((cp=strtok(NULL, "!"))) {
		errx(1, "%s: junk trailing dial string", cp);
	}
}

int
main(int argc, char **argv)
{
	char buf[256], *opts, *dial = NULL, *mountpt = NULL;
	int optlen = 64, port = 0, i;
	struct stat stbuf;
	struct passwd *pw;
	int axess = 0, dotu = 0, uidgid = 0, dev = 0, debug = 0, dryrun = 0;
	char *debugstr = NULL, *msize = NULL, *cache = NULL, *aname = NULL;
	char *cp, *proto, *addr;

	if (!(opts=calloc(optlen, 1))) {
		err(1, "calloc");
	}
	while (*++argv) {
		if (**argv == '-' && (*argv)[1] != '\0') {
			for (cp=*argv+1; *cp; ++cp) {
				switch (*cp) {
					case 'i': uidgid = 1; break;
					case 'n': dryrun = 1; break;
					case 's': axess = -1; break;
					case 'u': dotu = 1; break;
					case 'v': dev = 1; break;
					case 'x': axess = getuid(); break;
					case 'a':
						aname = getarg('a', cp, &argv);
						*cp-- = '\0'; /* breaks out of for loop */
						break;
					case 'c':
						cache = getarg('c', cp, &argv);
						*cp-- = '\0';
						break;
					case 'd':
						debugstr = getarg('d', cp, &argv);
						*cp-- = '\0';
						break;
					case 'm':
						msize = getarg('m', cp, &argv);
						*cp-- = '\0';
						break;
				}
			}
		} else if (!dial) {
			dial = *argv;
		} else if (!mountpt) {
			mountpt = *argv;
		} else {
			errx(1, "%s: too many arguments", *argv);
		}
	}

	if (!dial || !mountpt) {
		errx(1, "usage: 9mount [ -insuvx ] [ -a spec ] [ -c cache ] [ -d debug ] [ -m msize ] dial mountpt");
	}

	if(!(pw=getpwuid(getuid()))) {
		err(1, "who are you?? getpwuid failed");
	}
	/* Make sure mount exists, is writable, and not sticky */
	if (stat(mountpt, &stbuf) || access(mountpt, W_OK)) {
		err(1, "%s", mountpt);
	}
	if (stbuf.st_mode & S_ISVTX) {
		errx(1, "%s: refusing to mount over sticky directory", mountpt);
	}

	if (strcmp(dial, "-") == 0) {
		proto = "fd";
		addr = "nodev";
		append(&opts, "rfdno=0,wrfdno=1", &optlen);
	} else {
		parsedial(dial, &proto, &addr, &port);
	}

	/* set up mount options */
	append(&opts, proto, &optlen); /* < 2.6.24 */
	snprintf(buf, sizeof(buf), "trans=%s", proto);
	append(&opts, buf, &optlen); /* >= 2.6.24 */

	if (aname) {
		if (strchr(aname, ',')) {
			errx(1, "%s: spec can't contain commas", aname);
		}
		snprintf(buf, sizeof(buf), "aname=%s", aname);
		append(&opts, buf, &optlen);
	}

	if (cache) {
		if (strcmp(cache, "loose") != 0) {
			errx(1, "%s: unknown cache mode (expecting loose)", cache);
		}
		snprintf(buf, sizeof(buf), "cache=%s", cache);
		append(&opts, buf, &optlen);
	}

	if (debugstr) {
		for (cp=strtok(debugstr, ","); cp; cp=strtok(NULL, ",")) {
			for (i=0; i<nelem(debug_flags); ++i) {
				if (strcmp(cp, debug_flags[i].mnemonic) == 0) {
					debug |= debug_flags[i].mask;
					break;
				}
			}
			if (i >= nelem(debug_flags)) {
				errx(1, "%s: unrecognised debug channel", cp);
			}
		}
		snprintf(buf, sizeof(buf), "debug=0x%04x", debug);
		append(&opts, buf, &optlen);
	}

	if (msize) {
		if (strspn(msize, "0123456789") < strlen(msize)) {
			errx(1, "%s: msize must be an integer", msize);
		}
		snprintf(buf, sizeof(buf), "msize=%s", msize);
		append(&opts, buf, &optlen);
	}

	snprintf(buf, sizeof(buf), "name=%s", pw->pw_name);
	append(&opts, buf, &optlen);

	if (getenv("USER")) {
		snprintf(buf, sizeof(buf), "uname=%s", getenv("USER"));
	} else {
		snprintf(buf, sizeof(buf), "uname=%s", pw->pw_name);
	}
	if (strchr(buf, ',')) {
		errx(1, "%s: username can't contain commas", buf+6);
	}
	append(&opts, buf, &optlen);

	if (axess == -1) {
		append(&opts, "access=any", &optlen);
	} else if (axess) {
		snprintf(buf, sizeof(buf), "access=%d", axess);
		append(&opts, buf, &optlen);
	}
	if (!dotu) {
		append(&opts, "noextend", &optlen);
	}
	if (!dev) {
		append(&opts, "nodev", &optlen);
	}
	if (uidgid) {
		snprintf(buf, sizeof(buf), "uid=%d,gid=%d", getuid(), getgid());
		append(&opts, buf, &optlen); /* < 2.6.24 */
		snprintf(buf, sizeof(buf), "dfltuid=%d,dfltgid=%d", getuid(), getgid());
		append(&opts, buf, &optlen); /* >= 2.6.24 */
	}
	if (port) {
		snprintf(buf, sizeof(buf), "port=%d", port);
		append(&opts, buf, &optlen);
	}

	if (strcmp(proto, "tcp") == 0) {
		struct addrinfo *ai;
		int r;
		if ((r=getaddrinfo(addr, NULL, NULL, &ai))) {
			errx(1, "getaddrinfo: %s", gai_strerror(r));
		}
		if ((r=getnameinfo(ai->ai_addr, ai->ai_addrlen, buf,
						sizeof(buf), NULL, 0, NI_NUMERICHOST))) {
			errx(1, "getnameinfo: %s", gai_strerror(r));
		}
	} else { /* unix socket, virtio device or fd transport */
		snprintf(buf, sizeof(buf), "%s", addr);
	}

	if(dryrun) {
		fprintf(stderr, "mount -t 9p -o %s %s %s\n", opts, buf, mountpt);
	} else if (mount(buf, mountpt, "9p", 0, (void*)opts)) {
		err(1, "mount");
	}

	return 0;
}