aboutsummaryrefslogtreecommitdiff
path: root/stand/common/install.c
blob: 418799822e4eac3953e56c32e03afca8155ef0f7 (plain) (blame)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*-
 * Copyright (c) 2008-2014, Juniper Networks, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>

#include <stand.h>
#include <net.h>
#include <string.h>

#include "bootstrap.h"

extern struct in_addr servip;

extern int pkgfs_init(const char *, struct fs_ops *);
extern void pkgfs_cleanup(void);

COMMAND_SET(install, "install", "install software package", command_install);

static char *inst_kernel;
static char **inst_modules;
static char *inst_rootfs;
static char *inst_loader_rc;

static int
setpath(char **what, char *val)
{
	char *path;
	size_t len;
	int rel;

	len = strlen(val) + 1;
	rel = (val[0] != '/') ? 1 : 0;
	path = malloc(len + rel);
	if (path == NULL)
		return (ENOMEM);
	path[0] = '/';
	strcpy(path + rel, val);

	*what = path;
	return (0);
}

static int
setmultipath(char ***what, char *val)
{
	char *s, *v;
	int count, error, idx;

	count = 0;
	v = val;
	do {
		count++;
		s = strchr(v, ',');
		v = (s == NULL) ? NULL : s + 1;
	} while (v != NULL);

	*what = calloc(count + 1, sizeof(char *));
	if (*what == NULL)
		return (ENOMEM);

	for (idx = 0; idx < count; idx++) {
		s = strchr(val, ',');
		if (s != NULL)
			*s++ = '\0';
		error = setpath(*what + idx, val);
		if (error)
			return (error);
		val = s;
	}

	return (0);
}

static int
read_metatags(int fd)
{
	char buf[1024];
	char *p, *tag, *val;
	ssize_t fsize;
	int error;

	fsize = read(fd, buf, sizeof(buf));
	if (fsize == -1)
		return (errno);

	/*
	 * Assume that if we read a whole buffer worth of data, we
	 * haven't read the entire file. In other words, the buffer
	 * size must always be larger than the file size. That way
	 * we can append a '\0' and use standard string operations.
	 * Return an error if this is not possible.
	 */
	if (fsize == sizeof(buf))
		return (ENOMEM);

	buf[fsize] = '\0';
	error = 0;
	tag = buf;
	while (!error && *tag != '\0') {
		val = strchr(tag, '=');
		if (val == NULL) {
			error = EINVAL;
			break;
		}
		*val++ = '\0';
		p = strchr(val, '\n');
		if (p == NULL) {
			error = EINVAL;
			break;
		}
		*p++ = '\0';

		if (strcmp(tag, "KERNEL") == 0)
			error = setpath(&inst_kernel, val);
		else if (strcmp(tag, "MODULES") == 0)
			error = setmultipath(&inst_modules, val);
		else if (strcmp(tag, "ROOTFS") == 0)
			error = setpath(&inst_rootfs, val);
		else if (strcmp(tag, "LOADER_RC") == 0)
			error = setpath(&inst_loader_rc, val);

		tag = p;
	}

	return (error);
}

static void
cleanup(void)
{
	u_int i;

	if (inst_kernel != NULL) {
		free(inst_kernel);
		inst_kernel = NULL;
	}
	if (inst_modules != NULL) {
		i = 0;
		while (inst_modules[i] != NULL)
			free(inst_modules[i++]);
		free(inst_modules);
		inst_modules = NULL;
	}
	if (inst_rootfs != NULL) {
		free(inst_rootfs);
		inst_rootfs = NULL;
	}
	if (inst_loader_rc != NULL) {
		free(inst_loader_rc);
		inst_loader_rc = NULL;
	}
	pkgfs_cleanup();
}

/*
 * usage: install URL
 * where: URL = tftp://[host]/<package>
 *	or	file://[devname[:fstype]]/<package>
 */
static int
install(char *pkgname)
{
	static char buf[256];
	struct fs_ops *proto;
	struct preloaded_file *fp;
	char *e, *s, *currdev;
	char *devname;
	size_t devnamelen;
	int error, fd, i, local;

	s = strstr(pkgname, "://");
	if (s == NULL)
		goto invalid_url;

	i = s - pkgname;
	s += 3;
	if (*s == '\0')
		goto invalid_url;

	devname = NULL;
	devnamelen = 0;
	proto = NULL;
	local = 0;

	if (i == 4 && !strncasecmp(pkgname, "tftp", i)) {
		devname = "net0";
		devnamelen = 4;
		proto = &tftp_fsops;
	} else if (i == 4 && !strncasecmp(pkgname, "file", i)) {
		currdev = getenv("currdev");
		local = 1;

		if (*s == '/') {	/* file:/// */
			if (devname == NULL)
				devname = currdev;
			if (devname == NULL)
				devname = "disk1";
		} else {		/* file://devname[:fstype]/ */
			devname = s;
			e = strchr(devname, '/');
			if (!e)
				goto invalid_url;
			devnamelen = e - devname;
			s = e;		/* consume devname */
		}
		if ((e = strchr(devname, ':')) != NULL) {
			/* could be :fstype */
			devnamelen = e - devname;
			switch (e[1]) {
			case '\0':	/* just currdev */
				break;
			case 'd':
				proto = &dosfs_fsops;
				break;
#ifdef HOSTPROG
			case 'h':
				{
					extern struct fs_ops host_fsops;

					proto = &host_fsops;
				}
				break;
#endif
			case 'u':
				proto = &ufs_fsops;
				break;
			}
		}
		if (proto == NULL && strncmp(devname, "disk", 4) == 0) {
			proto = &dosfs_fsops;
		}
	}

	if (devname == NULL)
		goto invalid_url;

	if (devnamelen == 0) {
		/* default is currdev which ends with ':' */
		devnamelen = strlen(devname);
		if (devname[devnamelen - 1] == ':')
			devnamelen--;
	}

	if (*s != '/' ) {
		if (local)
			goto invalid_url;

		pkgname = strchr(s, '/');
		if (pkgname == NULL)
			goto invalid_url;

		*pkgname = '\0';
		servip.s_addr = inet_addr(s);
		if (servip.s_addr == htonl(INADDR_NONE))
			goto invalid_url;

		setenv("serverip", inet_ntoa(servip), 1);

		*pkgname = '/';
	} else
		pkgname = s;

	i = snprintf(buf, sizeof(buf), "%.*s:%s",
	    (int) devnamelen, devname, pkgname);
	if (i >= (int) sizeof(buf)) {
		command_errmsg = "package name too long";
		return (CMD_ERROR);
	}
	setenv("install_package", buf, 1);

	error = pkgfs_init(buf, proto);
	if (error) {
		command_errmsg = "cannot open package";
		goto fail;
	}

	/*
	 * Point of no return: unload anything that may have been
	 * loaded and prune the environment from harmful variables.
	 */
	unload();
	unsetenv("vfs.root.mountfrom");

	/*
	 * read the metatags file.
	 */
	fd = open("/metatags", O_RDONLY);
	if (fd != -1) {
		error = read_metatags(fd);
		close(fd);
		if (error) {
			command_errmsg = "cannot load metatags";
			goto fail;
		}
	}

	s = (inst_kernel == NULL) ? "/kernel" : inst_kernel;
	error = mod_loadkld(s, 0, NULL);
	if (error) {
		command_errmsg = "cannot load kernel from package";
		goto fail;
	}

	/* If there is a loader.rc in the package, execute it */
	s = (inst_loader_rc == NULL) ? "/loader.rc" : inst_loader_rc;
	fd = open(s, O_RDONLY);
	if (fd != -1) {
		close(fd);
		error = interp_include(s);
		if (error == CMD_ERROR)
			goto fail;
	}

	i = 0;
	while (inst_modules != NULL && inst_modules[i] != NULL) {
		error = mod_loadkld(inst_modules[i], 0, NULL);
		if (error) {
			command_errmsg = "cannot load module(s) from package";
			goto fail;
		}
		i++;
	}

	s = (inst_rootfs == NULL) ? "/install.iso" : inst_rootfs;
	if (file_loadraw(s, "mfs_root", 1) == NULL) {
		error = errno;
		command_errmsg = "cannot load root file system";
		goto fail;
	}

	cleanup();

	fp = file_findfile(NULL, NULL);
	if (fp != NULL)
		file_formats[fp->f_loader]->l_exec(fp);
	error = CMD_ERROR;
	command_errmsg = "unable to start installation";

 fail:
	sprintf(buf, "%s (error %d)", command_errmsg, error);
	cleanup();
	unload();
	exclusive_file_system = NULL;
	command_errmsg = buf;	/* buf is static. */
	return (CMD_ERROR);

 invalid_url:
	command_errmsg = "invalid URL";
	return (CMD_ERROR);
}

static int
command_install(int argc, char *argv[])
{
	int argidx;

	unsetenv("install_format");

	argidx = 1;
	while (1) {
		if (argc == argidx) {
			command_errmsg =
			    "usage: install [--format] <URL>";
			return (CMD_ERROR);
		}
		if (!strcmp(argv[argidx], "--format")) {
			setenv("install_format", "yes", 1);
			argidx++;
			continue;
		}
		break;
	}

	return (install(argv[argidx]));
}