1199767f8SToomas Soome /*-
2199767f8SToomas Soome * Copyright (c) 2008-2014, Juniper Networks, Inc.
3199767f8SToomas Soome * All rights reserved.
4199767f8SToomas Soome *
5199767f8SToomas Soome * Redistribution and use in source and binary forms, with or without
6199767f8SToomas Soome * modification, are permitted provided that the following conditions
7199767f8SToomas Soome * are met:
8199767f8SToomas Soome * 1. Redistributions of source code must retain the above copyright
9199767f8SToomas Soome * notice, this list of conditions and the following disclaimer.
10199767f8SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
11199767f8SToomas Soome * notice, this list of conditions and the following disclaimer in the
12199767f8SToomas Soome * documentation and/or other materials provided with the distribution.
13199767f8SToomas Soome *
14199767f8SToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15199767f8SToomas Soome * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16199767f8SToomas Soome * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17199767f8SToomas Soome * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18199767f8SToomas Soome * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19199767f8SToomas Soome * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20199767f8SToomas Soome * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21199767f8SToomas Soome * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22199767f8SToomas Soome * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23199767f8SToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24199767f8SToomas Soome * SUCH DAMAGE.
25199767f8SToomas Soome */
26199767f8SToomas Soome
27199767f8SToomas Soome #include <sys/cdefs.h>
28199767f8SToomas Soome __FBSDID("$FreeBSD$");
29199767f8SToomas Soome
30199767f8SToomas Soome #include <sys/param.h>
31199767f8SToomas Soome #include <sys/socket.h>
32199767f8SToomas Soome #include <net/if.h>
33199767f8SToomas Soome #include <netinet/in.h>
34199767f8SToomas Soome #include <netinet/in_systm.h>
35199767f8SToomas Soome
36199767f8SToomas Soome #include <stand.h>
37199767f8SToomas Soome #include <net.h>
38199767f8SToomas Soome #include <string.h>
39199767f8SToomas Soome
40199767f8SToomas Soome #include "bootstrap.h"
41199767f8SToomas Soome
42199767f8SToomas Soome extern struct in_addr rootip;
43199767f8SToomas Soome extern struct in_addr servip;
44199767f8SToomas Soome
45199767f8SToomas Soome extern int pkgfs_init(const char *, struct fs_ops *);
46199767f8SToomas Soome extern void pkgfs_cleanup(void);
47199767f8SToomas Soome
48199767f8SToomas Soome COMMAND_SET(install, "install", "install software package", command_install);
49199767f8SToomas Soome
50199767f8SToomas Soome static char *inst_kernel;
51199767f8SToomas Soome static char **inst_modules;
52199767f8SToomas Soome static char *inst_rootfs;
53199767f8SToomas Soome
54199767f8SToomas Soome static int
setpath(char ** what,char * val)55199767f8SToomas Soome setpath(char **what, char *val)
56199767f8SToomas Soome {
57199767f8SToomas Soome char *path;
58199767f8SToomas Soome size_t len;
59199767f8SToomas Soome int rel;
60199767f8SToomas Soome
61199767f8SToomas Soome len = strlen(val) + 1;
62199767f8SToomas Soome rel = (val[0] != '/') ? 1 : 0;
63199767f8SToomas Soome path = malloc(len + rel);
64199767f8SToomas Soome if (path == NULL)
65199767f8SToomas Soome return (ENOMEM);
66199767f8SToomas Soome path[0] = '/';
67199767f8SToomas Soome strcpy(path + rel, val);
68199767f8SToomas Soome
69199767f8SToomas Soome *what = path;
70199767f8SToomas Soome return (0);
71199767f8SToomas Soome }
72199767f8SToomas Soome
73199767f8SToomas Soome static int
setmultipath(char *** what,char * val)74199767f8SToomas Soome setmultipath(char ***what, char *val)
75199767f8SToomas Soome {
76199767f8SToomas Soome char *s, *v;
77199767f8SToomas Soome int count, error, idx;
78199767f8SToomas Soome
79199767f8SToomas Soome count = 0;
80199767f8SToomas Soome v = val;
81199767f8SToomas Soome do {
82199767f8SToomas Soome count++;
83199767f8SToomas Soome s = strchr(v, ',');
84199767f8SToomas Soome v = (s == NULL) ? NULL : s + 1;
85199767f8SToomas Soome } while (v != NULL);
86199767f8SToomas Soome
87199767f8SToomas Soome *what = calloc(count + 1, sizeof(char *));
88199767f8SToomas Soome if (*what == NULL)
89199767f8SToomas Soome return (ENOMEM);
90199767f8SToomas Soome
91199767f8SToomas Soome for (idx = 0; idx < count; idx++) {
92199767f8SToomas Soome s = strchr(val, ',');
93199767f8SToomas Soome if (s != NULL)
94199767f8SToomas Soome *s++ = '\0';
95199767f8SToomas Soome error = setpath(*what + idx, val);
96199767f8SToomas Soome if (error)
97199767f8SToomas Soome return (error);
98199767f8SToomas Soome val = s;
99199767f8SToomas Soome }
100199767f8SToomas Soome
101199767f8SToomas Soome return (0);
102199767f8SToomas Soome }
103199767f8SToomas Soome
104199767f8SToomas Soome static int
read_metatags(int fd)105199767f8SToomas Soome read_metatags(int fd)
106199767f8SToomas Soome {
107199767f8SToomas Soome char buf[1024];
108199767f8SToomas Soome char *p, *tag, *val;
109199767f8SToomas Soome ssize_t fsize;
110199767f8SToomas Soome int error;
111199767f8SToomas Soome
112199767f8SToomas Soome fsize = read(fd, buf, sizeof(buf));
113199767f8SToomas Soome if (fsize == -1)
114199767f8SToomas Soome return (errno);
115199767f8SToomas Soome
116199767f8SToomas Soome /*
117199767f8SToomas Soome * Assume that if we read a whole buffer worth of data, we
118199767f8SToomas Soome * haven't read the entire file. In other words, the buffer
119199767f8SToomas Soome * size must always be larger than the file size. That way
120199767f8SToomas Soome * we can append a '\0' and use standard string operations.
121199767f8SToomas Soome * Return an error if this is not possible.
122199767f8SToomas Soome */
123199767f8SToomas Soome if (fsize == sizeof(buf))
124199767f8SToomas Soome return (ENOMEM);
125199767f8SToomas Soome
126199767f8SToomas Soome buf[fsize] = '\0';
127199767f8SToomas Soome error = 0;
128199767f8SToomas Soome tag = buf;
129199767f8SToomas Soome while (!error && *tag != '\0') {
130199767f8SToomas Soome val = strchr(tag, '=');
131199767f8SToomas Soome if (val == NULL) {
132199767f8SToomas Soome error = EINVAL;
133199767f8SToomas Soome break;
134199767f8SToomas Soome }
135199767f8SToomas Soome *val++ = '\0';
136199767f8SToomas Soome p = strchr(val, '\n');
137199767f8SToomas Soome if (p == NULL) {
138199767f8SToomas Soome error = EINVAL;
139199767f8SToomas Soome break;
140199767f8SToomas Soome }
141199767f8SToomas Soome *p++ = '\0';
142199767f8SToomas Soome
143199767f8SToomas Soome if (strcmp(tag, "KERNEL") == 0)
144199767f8SToomas Soome error = setpath(&inst_kernel, val);
145199767f8SToomas Soome else if (strcmp(tag, "MODULES") == 0)
146199767f8SToomas Soome error = setmultipath(&inst_modules, val);
147199767f8SToomas Soome else if (strcmp(tag, "ROOTFS") == 0)
148199767f8SToomas Soome error = setpath(&inst_rootfs, val);
149199767f8SToomas Soome
150199767f8SToomas Soome tag = p;
151199767f8SToomas Soome }
152199767f8SToomas Soome
153199767f8SToomas Soome return (error);
154199767f8SToomas Soome }
155199767f8SToomas Soome
156199767f8SToomas Soome static void
cleanup(void)157199767f8SToomas Soome cleanup(void)
158199767f8SToomas Soome {
159199767f8SToomas Soome u_int i;
160199767f8SToomas Soome
161199767f8SToomas Soome if (inst_kernel != NULL) {
162199767f8SToomas Soome free(inst_kernel);
163199767f8SToomas Soome inst_kernel = NULL;
164199767f8SToomas Soome }
165199767f8SToomas Soome if (inst_modules != NULL) {
166199767f8SToomas Soome i = 0;
167199767f8SToomas Soome while (inst_modules[i] != NULL)
168199767f8SToomas Soome free(inst_modules[i++]);
169199767f8SToomas Soome free(inst_modules);
170199767f8SToomas Soome inst_modules = NULL;
171199767f8SToomas Soome }
172199767f8SToomas Soome if (inst_rootfs != NULL) {
173199767f8SToomas Soome free(inst_rootfs);
174199767f8SToomas Soome inst_rootfs = NULL;
175199767f8SToomas Soome }
176199767f8SToomas Soome pkgfs_cleanup();
177199767f8SToomas Soome }
178199767f8SToomas Soome
179199767f8SToomas Soome /*
180199767f8SToomas Soome * usage: install URL
181199767f8SToomas Soome * where: URL = (tftp|file)://[host]/<package>
182199767f8SToomas Soome */
183199767f8SToomas Soome static int
install(char * pkgname)184199767f8SToomas Soome install(char *pkgname)
185199767f8SToomas Soome {
186199767f8SToomas Soome static char buf[256];
187199767f8SToomas Soome struct fs_ops *proto;
188199767f8SToomas Soome struct preloaded_file *fp;
189199767f8SToomas Soome char *s, *currdev;
190199767f8SToomas Soome const char *devname;
191199767f8SToomas Soome int error, fd, i, local;
192199767f8SToomas Soome
193199767f8SToomas Soome s = strstr(pkgname, "://");
194199767f8SToomas Soome if (s == NULL)
195199767f8SToomas Soome goto invalid_url;
196199767f8SToomas Soome
197199767f8SToomas Soome i = s - pkgname;
198199767f8SToomas Soome if (i == 4 && !strncasecmp(pkgname, "tftp", i)) {
199199767f8SToomas Soome devname = "net0";
200199767f8SToomas Soome proto = &tftp_fsops;
201199767f8SToomas Soome local = 0;
202199767f8SToomas Soome } else if (i == 4 && !strncasecmp(pkgname, "file", i)) {
203199767f8SToomas Soome currdev = getenv("currdev");
204199767f8SToomas Soome if (currdev != NULL && strcmp(currdev, "pxe0:") == 0) {
205199767f8SToomas Soome devname = "pxe0";
206199767f8SToomas Soome proto = NULL;
207199767f8SToomas Soome } else {
208199767f8SToomas Soome devname = "disk1";
209199767f8SToomas Soome proto = &dosfs_fsops;
210199767f8SToomas Soome }
211199767f8SToomas Soome local = 1;
212199767f8SToomas Soome } else
213199767f8SToomas Soome goto invalid_url;
214199767f8SToomas Soome
215199767f8SToomas Soome s += 3;
216199767f8SToomas Soome if (*s == '\0')
217199767f8SToomas Soome goto invalid_url;
218199767f8SToomas Soome
219199767f8SToomas Soome if (*s != '/' ) {
220199767f8SToomas Soome if (local)
221199767f8SToomas Soome goto invalid_url;
222199767f8SToomas Soome
223199767f8SToomas Soome pkgname = strchr(s, '/');
224199767f8SToomas Soome if (pkgname == NULL)
225199767f8SToomas Soome goto invalid_url;
226199767f8SToomas Soome
227199767f8SToomas Soome *pkgname = '\0';
228199767f8SToomas Soome servip.s_addr = inet_addr(s);
229199767f8SToomas Soome if (servip.s_addr == htonl(INADDR_NONE))
230199767f8SToomas Soome goto invalid_url;
231199767f8SToomas Soome
232199767f8SToomas Soome setenv("serverip", inet_ntoa(servip), 1);
233199767f8SToomas Soome
234199767f8SToomas Soome *pkgname = '/';
235199767f8SToomas Soome } else
236199767f8SToomas Soome pkgname = s;
237199767f8SToomas Soome
238199767f8SToomas Soome if (strlen(devname) + strlen(pkgname) + 2 > sizeof(buf)) {
239199767f8SToomas Soome command_errmsg = "package name too long";
240199767f8SToomas Soome return (CMD_ERROR);
241199767f8SToomas Soome }
242199767f8SToomas Soome sprintf(buf, "%s:%s", devname, pkgname);
243199767f8SToomas Soome setenv("install_package", buf, 1);
244199767f8SToomas Soome
245199767f8SToomas Soome error = pkgfs_init(buf, proto);
246199767f8SToomas Soome if (error) {
247199767f8SToomas Soome command_errmsg = "cannot open package";
248199767f8SToomas Soome goto fail;
249199767f8SToomas Soome }
250199767f8SToomas Soome
251199767f8SToomas Soome /*
252199767f8SToomas Soome * Point of no return: unload anything that may have been
253199767f8SToomas Soome * loaded and prune the environment from harmful variables.
254199767f8SToomas Soome */
255199767f8SToomas Soome unload();
256199767f8SToomas Soome unsetenv("vfs.root.mountfrom");
257199767f8SToomas Soome
258199767f8SToomas Soome /*
259199767f8SToomas Soome * read the metatags file.
260199767f8SToomas Soome */
261199767f8SToomas Soome fd = open("/metatags", O_RDONLY);
262199767f8SToomas Soome if (fd != -1) {
263199767f8SToomas Soome error = read_metatags(fd);
264199767f8SToomas Soome close(fd);
265199767f8SToomas Soome if (error) {
266199767f8SToomas Soome command_errmsg = "cannot load metatags";
267199767f8SToomas Soome goto fail;
268199767f8SToomas Soome }
269199767f8SToomas Soome }
270199767f8SToomas Soome
271199767f8SToomas Soome s = (inst_kernel == NULL) ? "/kernel" : inst_kernel;
272199767f8SToomas Soome error = mod_loadkld(s, 0, NULL);
273199767f8SToomas Soome if (error) {
274199767f8SToomas Soome command_errmsg = "cannot load kernel from package";
275199767f8SToomas Soome goto fail;
276199767f8SToomas Soome }
277199767f8SToomas Soome
278199767f8SToomas Soome i = 0;
279199767f8SToomas Soome while (inst_modules != NULL && inst_modules[i] != NULL) {
280199767f8SToomas Soome error = mod_loadkld(inst_modules[i], 0, NULL);
281199767f8SToomas Soome if (error) {
282199767f8SToomas Soome command_errmsg = "cannot load module(s) from package";
283199767f8SToomas Soome goto fail;
284199767f8SToomas Soome }
285199767f8SToomas Soome i++;
286199767f8SToomas Soome }
287199767f8SToomas Soome
288199767f8SToomas Soome s = (inst_rootfs == NULL) ? "/install.iso" : inst_rootfs;
289199767f8SToomas Soome if (file_loadraw(s, "mfs_root") == NULL) {
290199767f8SToomas Soome error = errno;
291199767f8SToomas Soome command_errmsg = "cannot load root file system";
292199767f8SToomas Soome goto fail;
293199767f8SToomas Soome }
294199767f8SToomas Soome
295199767f8SToomas Soome cleanup();
296199767f8SToomas Soome
297199767f8SToomas Soome fp = file_findfile(NULL, NULL);
298199767f8SToomas Soome if (fp != NULL)
299199767f8SToomas Soome file_formats[fp->f_loader]->l_exec(fp);
300199767f8SToomas Soome error = CMD_ERROR;
301199767f8SToomas Soome command_errmsg = "unable to start installation";
302199767f8SToomas Soome
303199767f8SToomas Soome fail:
304199767f8SToomas Soome sprintf(buf, "%s (error %d)", command_errmsg, error);
305199767f8SToomas Soome cleanup();
306199767f8SToomas Soome unload();
307199767f8SToomas Soome exclusive_file_system = NULL;
308199767f8SToomas Soome command_errmsg = buf; /* buf is static. */
309199767f8SToomas Soome return (CMD_ERROR);
310199767f8SToomas Soome
311199767f8SToomas Soome invalid_url:
312199767f8SToomas Soome command_errmsg = "invalid URL";
313199767f8SToomas Soome return (CMD_ERROR);
314199767f8SToomas Soome }
315199767f8SToomas Soome
316199767f8SToomas Soome static int
command_install(int argc,char * argv[])317199767f8SToomas Soome command_install(int argc, char *argv[])
318199767f8SToomas Soome {
319199767f8SToomas Soome int argidx;
320199767f8SToomas Soome
321199767f8SToomas Soome unsetenv("install_format");
322199767f8SToomas Soome
323199767f8SToomas Soome argidx = 1;
324199767f8SToomas Soome while (1) {
325199767f8SToomas Soome if (argc == argidx) {
326199767f8SToomas Soome command_errmsg =
327199767f8SToomas Soome "usage: install [--format] <URL>";
328199767f8SToomas Soome return (CMD_ERROR);
329199767f8SToomas Soome }
330199767f8SToomas Soome if (!strcmp(argv[argidx], "--format")) {
331199767f8SToomas Soome setenv("install_format", "yes", 1);
332199767f8SToomas Soome argidx++;
333199767f8SToomas Soome continue;
334199767f8SToomas Soome }
335199767f8SToomas Soome break;
336199767f8SToomas Soome }
337199767f8SToomas Soome
338199767f8SToomas Soome return (install(argv[argidx]));
339199767f8SToomas Soome }
340