]> git.ozlabs.org Git - petitboot/blob - discover/paths.c
Add support for GPG signature enforcement on booted
[petitboot] / discover / paths.c
1 #if defined(HAVE_CONFIG_H)
2 #include "config.h"
3 #endif
4
5 #include <assert.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11
12 #include <talloc/talloc.h>
13 #include <system/system.h>
14 #include <process/process.h>
15 #include <url/url.h>
16 #include <log/log.h>
17
18 #include "paths.h"
19
20 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
21
22 struct load_task {
23         struct pb_url           *url;
24         struct process          *process;
25         struct load_url_result  *result;
26         bool                    async;
27         load_url_complete       async_cb;
28         void                    *async_data;
29 };
30
31 const char *mount_base(void)
32 {
33         return DEVICE_MOUNT_BASE;
34 }
35
36 char *join_paths(void *alloc_ctx, const char *a, const char *b)
37 {
38         char *full_path;
39
40         full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
41
42         strcpy(full_path, a);
43         if (b[0] != '/' && a[strlen(a) - 1] != '/')
44                 strcat(full_path, "/");
45         strcat(full_path, b);
46
47         return full_path;
48 }
49
50 #ifndef PETITBOOT_TEST
51
52 static char *local_name(void *ctx)
53 {
54         char *ret, tmp[] = "/tmp/pb-XXXXXX";
55         mode_t oldmask;
56         int fd;
57
58         oldmask = umask(0644);
59         fd = mkstemp(tmp);
60         umask(oldmask);
61
62         if (fd < 0)
63                 return NULL;
64
65         close(fd);
66
67         ret = talloc_strdup(ctx, tmp);
68
69         return ret;
70 }
71
72 static void load_url_result_cleanup_local(struct load_url_result *result)
73 {
74         if (result->cleanup_local)
75                 unlink(result->local);
76 }
77
78 static void load_url_process_exit(struct process *process)
79 {
80         struct load_task *task = process->data;
81         struct load_url_result *result;
82         load_url_complete cb;
83         void *data;
84
85         pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
86                         process->path, process->pid, task->url->full,
87                         process->exit_status);
88
89         result = task->result;
90         data = task->async_data;
91         cb = task->async_cb;
92
93         if (result->status == LOAD_CANCELLED) {
94                 load_url_result_cleanup_local(result);
95         } else if (process_exit_ok(process)) {
96                 result->status = LOAD_OK;
97         } else {
98                 result->status = LOAD_ERROR;
99                 load_url_result_cleanup_local(result);
100         }
101
102         /* The load callback may well free the ctx, which was the
103          * talloc parent of the task. Therefore, we want to do our cleanup
104          * before invoking it
105          */
106         process_release(process);
107         talloc_free(task);
108         result->task = NULL;
109
110         cb(result, data);
111 }
112
113 static void load_process_to_local_file(struct load_task *task,
114                 const char **argv, int argv_local_idx)
115 {
116         int rc;
117
118         task->result->local = local_name(task->result);
119         if (!task->result->local) {
120                 task->result->status = LOAD_ERROR;
121                 return;
122         }
123         task->result->cleanup_local = true;
124
125         if (argv_local_idx)
126                 argv[argv_local_idx] = task->result->local;
127
128         task->process->argv = argv;
129         task->process->path = argv[0];
130
131         if (task->async) {
132                 rc = process_run_async(task->process);
133                 if (rc) {
134                         process_release(task->process);
135                         task->process = NULL;
136                 }
137                 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
138         } else {
139                 rc = process_run_sync(task->process);
140                 if (rc || !process_exit_ok(task->process))
141                         task->result->status = LOAD_ERROR;
142                 else
143                         task->result->status = LOAD_OK;
144                 process_release(task->process);
145                 task->process = NULL;
146         }
147 }
148
149 /**
150  * pb_load_nfs - Create a mountpoint, set the local file within that
151  * mountpoint, and run the appropriate mount command
152  */
153
154 static void load_nfs(struct load_task *task)
155 {
156         char *mountpoint, *opts;
157         int rc;
158         const char *argv[] = {
159                         pb_system_apps.mount,
160                         "-t", "nfs",
161                         NULL,                   /* 3: opts */
162                         task->url->host,
163                         task->url->dir,
164                         NULL,                   /* 6: mountpoint */
165                         NULL,
166         };
167
168         task->result->status = LOAD_ERROR;
169         mountpoint = local_name(task->result);
170         if (!mountpoint)
171                 return;
172         task->result->cleanup_local = true;
173         argv[6] = mountpoint;
174
175         rc = pb_mkdir_recursive(mountpoint);
176         if (rc)
177                 return;
178
179         opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
180         argv[3] = opts;
181
182         if (task->url->port)
183                 opts = talloc_asprintf_append(opts, ",port=%s",
184                                                 task->url->port);
185
186         task->result->local = talloc_asprintf(task->result, "%s/%s",
187                                                         mountpoint,
188                                                         task->url->path);
189
190         task->process->path = pb_system_apps.mount;
191         task->process->argv = argv;
192
193         if (task->async) {
194                 rc = process_run_async(task->process);
195                 if (rc) {
196                         process_release(task->process);
197                         task->process = NULL;
198                 }
199                 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
200         } else {
201                 rc = process_run_sync(task->process);
202                 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
203                 process_release(task->process);
204                 task->process = NULL;
205         }
206
207         talloc_free(opts);
208 }
209
210 static void load_sftp(struct load_task *task)
211 {
212         const char *argv[] = {
213                         pb_system_apps.sftp,
214                         NULL,           /* 1: host:path */
215                         NULL,           /* 2: local file */
216                         NULL,
217         };
218
219         argv[1] = talloc_asprintf(task, "%s:%s",
220                                 task->url->host, task->url->path);
221         load_process_to_local_file(task, argv, 2);
222 }
223
224 static enum tftp_type check_tftp_type(void *ctx)
225 {
226         const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
227         struct process *process;
228         enum tftp_type type;
229         int rc;
230
231         process = process_create(ctx);
232         process->path = pb_system_apps.tftp;
233         process->argv = argv;
234         process->keep_stdout = true;
235         process->add_stderr = true;
236         rc = process_run_sync(process);
237
238         if (rc || !process->stdout_buf || process->stdout_len == 0) {
239                 pb_log("Can't check TFTP client type!\n");
240                 type = TFTP_TYPE_BROKEN;
241
242         } else if (memmem(process->stdout_buf, process->stdout_len,
243                                 "tftp-hpa", strlen("tftp-hpa"))) {
244                 pb_debug("Found TFTP client type: tftp-hpa\n");
245                 type = TFTP_TYPE_HPA;
246
247         } else if (memmem(process->stdout_buf, process->stdout_len,
248                                 "BusyBox", strlen("BusyBox"))) {
249                 pb_debug("Found TFTP client type: BusyBox tftp\n");
250                 type = TFTP_TYPE_BUSYBOX;
251
252         } else {
253                 pb_log("Unknown TFTP client type!\n");
254                 type = TFTP_TYPE_BROKEN;
255         }
256
257         process_release(process);
258         return type;
259 }
260
261 static void load_tftp(struct load_task *task)
262 {
263         const char *port = "69";
264         const char *argv[10] = {
265                 pb_system_apps.tftp,
266         };
267
268         if (task->url->port)
269                 port = task->url->port;
270
271         if (tftp_type == TFTP_TYPE_UNKNOWN)
272                 tftp_type = check_tftp_type(task);
273
274         if (tftp_type == TFTP_TYPE_BUSYBOX) {
275                 argv[1] = "-g";
276                 argv[2] = "-l";
277                 argv[3] = NULL; /* 3: local file */
278                 argv[4] = "-r";
279                 argv[5] = task->url->path;
280                 argv[6] = task->url->host;
281                 argv[7] = port;
282                 argv[8] = NULL;
283
284                 load_process_to_local_file(task, argv, 3);
285
286         } else if (tftp_type == TFTP_TYPE_HPA) {
287                 argv[1] = "-m";
288                 argv[2] = "binary";
289                 argv[3] = task->url->host;
290                 argv[4] = port;
291                 argv[5] = "-c";
292                 argv[6] = "get";
293                 argv[7] = task->url->path;
294                 argv[8] = NULL; /* 8: local file */
295                 argv[9] = NULL;
296                 load_process_to_local_file(task, argv, 8);
297
298         } else
299                 task->result->status = LOAD_ERROR;
300 }
301
302 enum wget_flags {
303         wget_empty = 0,
304         wget_no_check_certificate = 1,
305 };
306
307 /**
308  * pb_load_wget - Loads a remote file via wget and returns the local file path.
309  *
310  * Returns the local file path in a talloc'ed character string on success,
311  * or NULL on error.
312  */
313
314 static void load_wget(struct load_task *task, int flags)
315 {
316         const char *argv[] = {
317                 pb_system_apps.wget,
318                 "-O",
319                 NULL, /* 2: local file */
320                 NULL, /* 3 (optional): --quiet */
321                 NULL, /* 4 (optional): --no-check-certificate */
322                 NULL, /* 5: URL */
323                 NULL,
324         };
325         int i;
326
327         i = 3;
328 #if !defined(DEBUG)
329         argv[i++] = "--quiet";
330 #endif
331         if (flags & wget_no_check_certificate)
332                 argv[i++] = "--no-check-certificate";
333
334         argv[i] = task->url->full;
335
336         load_process_to_local_file(task, argv, 2);
337 }
338
339 /* Although we don't need to load anything for a local path (we just return
340  * the path from the file:// URL), the other load helpers will error-out on
341  * non-existant files. So, do the same here with an access() check on the local
342  * filename.
343  */
344 static void load_local(struct load_task *task)
345 {
346         int rc;
347
348         rc = access(task->url->path, F_OK);
349         if (rc) {
350                 task->result->status = LOAD_ERROR;
351         } else {
352                 task->result->local = talloc_strdup(task->result,
353                                                     task->url->path);
354                 task->result->status = LOAD_OK;
355         }
356 }
357
358 /**
359  * load_url - Loads a (possibly) remote URL and returns the local file
360  * path.
361  * @ctx: The talloc context to associate with the returned string.
362  * @url: The remote file URL.
363  * @tempfile: An optional variable pointer to be set when a temporary local
364  *  file is created.
365  * @url_cb: An optional callback pointer if the caller wants to load url
366  *  asynchronously.
367  *
368  * Returns the local file path in a talloc'ed character string on success,
369  * or NULL on error.
370  */
371
372 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
373                 load_url_complete async_cb, void *async_data)
374 {
375         struct load_url_result *result;
376         struct load_task *task;
377
378         if (!url)
379                 return NULL;
380
381         task = talloc_zero(ctx, struct load_task);
382         task->url = url;
383         task->async = async_cb != NULL;
384         task->result = talloc_zero(ctx, struct load_url_result);
385         task->result->task = task;
386         task->process = process_create(task);
387         if (task->async) {
388                 task->async_cb = async_cb;
389                 task->async_data = async_data;
390                 task->process->exit_cb = load_url_process_exit;
391                 task->process->data = task;
392         }
393
394         switch (url->scheme) {
395         case pb_url_ftp:
396         case pb_url_http:
397                 load_wget(task, 0);
398                 break;
399         case pb_url_https:
400                 load_wget(task, wget_no_check_certificate);
401                 break;
402         case pb_url_nfs:
403                 load_nfs(task);
404                 break;
405         case pb_url_sftp:
406                 load_sftp(task);
407                 break;
408         case pb_url_tftp:
409                 load_tftp(task);
410                 break;
411         default:
412                 load_local(task);
413                 break;
414         }
415
416         result = task->result;
417         if (result->status == LOAD_ERROR) {
418                 load_url_result_cleanup_local(task->result);
419                 talloc_free(result);
420                 talloc_free(task);
421                 return NULL;
422         }
423
424         if (!task->async)
425                 talloc_free(task);
426
427         return result;
428 }
429
430 struct load_url_result *load_url(void *ctx, struct pb_url *url)
431 {
432         return load_url_async(ctx, url, NULL, NULL);
433 }
434
435 void load_url_async_cancel(struct load_url_result *res)
436 {
437         struct load_task *task = res->task;
438
439         /* the completion callback may have already been called; this clears
440          * res->task */
441         if (!task)
442                 return;
443
444         if (res->status == LOAD_CANCELLED)
445                 return;
446
447         assert(task->async);
448         assert(task->process);
449
450         res->status = LOAD_CANCELLED;
451         process_stop_async(task->process);
452 }
453
454 #else
455
456 static void __attribute__((unused)) load_local(
457                 struct load_task *task __attribute__((unused)))
458 {
459 }
460 static void __attribute__((unused)) load_wget(
461                 struct load_task *task __attribute__((unused)),
462                 int flags __attribute__((unused)))
463 {
464 }
465 static void __attribute__((unused)) load_tftp(
466                 struct load_task *task __attribute__((unused)))
467 {
468 }
469 static void __attribute__((unused)) load_sftp(
470                 struct load_task *task __attribute__((unused)))
471 {
472 }
473 static void __attribute__((unused)) load_nfs(
474                 struct load_task *task __attribute__((unused)))
475 {
476 }
477 static void __attribute__((unused)) load_url_process_exit(
478                 struct process *process __attribute__((unused)))
479 {
480 }
481
482 #endif