ui/ncurses: Fix boot editor segfault on update
[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 #include "i18n/i18n.h"
18
19 #include "paths.h"
20 #include "device-handler.h"
21 #include "sysinfo.h"
22
23 #define DEVICE_MOUNT_BASE (LOCAL_STATE_DIR "/petitboot/mnt")
24
25
26 struct list     pending_network_jobs;
27
28 struct network_job {
29         struct load_task        *task;
30         int                     flags;
31
32         struct list_item        list;
33 };
34
35 struct load_task {
36         struct pb_url           *url;
37         struct process          *process;
38         struct load_url_result  *result;
39         bool                    async;
40         load_url_complete       async_cb;
41         void                    *async_data;
42 };
43
44 static inline bool have_busybox(void)
45 {
46 #ifdef WITH_BUSYBOX
47         return true;
48 #else
49         return false;
50 #endif
51 }
52
53 const char *mount_base(void)
54 {
55         return DEVICE_MOUNT_BASE;
56 }
57
58 char *join_paths(void *alloc_ctx, const char *a, const char *b)
59 {
60         char *full_path;
61
62         full_path = talloc_array(alloc_ctx, char, strlen(a) + strlen(b) + 2);
63
64         strcpy(full_path, a);
65         if (b[0] != '/' && a[strlen(a) - 1] != '/')
66                 strcat(full_path, "/");
67         strcat(full_path, b);
68
69         return full_path;
70 }
71
72 #ifndef PETITBOOT_TEST
73
74 static char *local_name(void *ctx)
75 {
76         char *ret, tmp[] = "/tmp/pb-XXXXXX";
77         mode_t oldmask;
78         int fd;
79
80         oldmask = umask(0644);
81         fd = mkstemp(tmp);
82         umask(oldmask);
83
84         if (fd < 0)
85                 return NULL;
86
87         close(fd);
88
89         ret = talloc_strdup(ctx, tmp);
90
91         return ret;
92 }
93
94 static void load_url_result_cleanup_local(struct load_url_result *result)
95 {
96         if (result->cleanup_local)
97                 unlink(result->local);
98 }
99
100 static void load_url_process_exit(struct process *process)
101 {
102         struct load_task *task = process->data;
103         struct load_url_result *result;
104         load_url_complete cb;
105         void *data;
106
107         pb_debug("The download client '%s' [pid %d, url %s] exited, rc %d\n",
108                         process->path, process->pid, task->url->full,
109                         process->exit_status);
110
111         result = task->result;
112         data = task->async_data;
113         cb = task->async_cb;
114
115         if (result->status == LOAD_CANCELLED) {
116                 load_url_result_cleanup_local(result);
117         } else if (process_exit_ok(process)) {
118                 result->status = LOAD_OK;
119         } else {
120                 result->status = LOAD_ERROR;
121                 load_url_result_cleanup_local(result);
122         }
123
124         if (result->status == LOAD_OK && process->stdout_data)
125                 device_handler_status_info(process->stdout_data,
126                                 _("Download complete: %s"), task->url->file);
127
128         /* The load callback may well free the ctx, which was the
129          * talloc parent of the task. Therefore, we want to do our cleanup
130          * before invoking it
131          */
132         process_release(process);
133         talloc_free(task);
134         result->task = NULL;
135
136         cb(result, data);
137 }
138
139 /*
140  * Callback to retrieve progress information from Busybox utilities.
141  * Busybox utilities use a common progress bar format which progress percentage
142  * and current size can be can be parsed from.
143  */
144 static int busybox_progress_cb(void *arg)
145 {
146         const char *busybox_fmt = "%*s %u%*[%* |]%u%c %*u:%*u:%*u ETA\n";
147         struct process_info *procinfo = arg;
148         char *n, *s, suffix, *line = NULL;
149         struct device_handler *handler;
150         unsigned int percentage, size;
151         struct process *p;
152         int rc;
153
154         if (!arg)
155                 return -1;
156
157         p = procinfo_get_process(procinfo);
158         handler = p->stdout_data;
159
160         rc = process_stdout_custom(procinfo, &line);
161
162         if (rc) {
163                 /* Unregister ourselves from progress tracking */
164                 device_handler_status_download_remove(handler, procinfo);
165         }
166
167         if (rc || !line)
168                 return rc;
169
170         rc = sscanf(line, busybox_fmt, &percentage, &size, &suffix);
171
172         /*
173          * Many unrecognised lines are partial updates. If we see a partial
174          * line with a newline character, see if we can match a valid line
175          * at the end of stdout_buf
176          */
177         if (rc != 3) {
178                 n = strchr(line, '\n');
179                 if (n)
180                         for (s = n - 1; s >= p->stdout_buf; s--)
181                                 if (*s == '\n') {
182                                         rc = sscanf(s + 1, busybox_fmt,
183                                                 &percentage, &size, &suffix);
184                                         break;
185                                 }
186         }
187
188         if (rc != 3)
189                 percentage = size = 0;
190
191         device_handler_status_download(handler, procinfo,
192                         percentage, size, suffix);
193
194         return 0;
195 }
196
197
198
199 static void load_process_to_local_file(struct load_task *task,
200                 const char **argv, int argv_local_idx)
201 {
202         int rc;
203
204         task->result->local = local_name(task->result);
205         if (!task->result->local) {
206                 task->result->status = LOAD_ERROR;
207                 return;
208         }
209         task->result->cleanup_local = true;
210
211         if (argv_local_idx)
212                 argv[argv_local_idx] = task->result->local;
213
214         task->process->argv = argv;
215         task->process->path = argv[0];
216
217         if (task->async) {
218                 rc = process_run_async(task->process);
219                 if (rc) {
220                         process_release(task->process);
221                         task->process = NULL;
222                 }
223                 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
224         } else {
225                 rc = process_run_sync(task->process);
226                 if (rc || !process_exit_ok(task->process))
227                         task->result->status = LOAD_ERROR;
228                 else
229                         task->result->status = LOAD_OK;
230                 process_release(task->process);
231                 task->process = NULL;
232         }
233 }
234
235 /**
236  * pb_load_nfs - Create a mountpoint, set the local file within that
237  * mountpoint, and run the appropriate mount command
238  */
239
240 static void load_nfs(struct load_task *task)
241 {
242         char *mountpoint, *opts;
243         int rc;
244         const char *argv[] = {
245                         pb_system_apps.mount,
246                         "-t", "nfs",
247                         NULL,                   /* 3: opts */
248                         task->url->host,
249                         task->url->dir,
250                         NULL,                   /* 6: mountpoint */
251                         NULL,
252         };
253
254         task->result->status = LOAD_ERROR;
255         mountpoint = local_name(task->result);
256         if (!mountpoint)
257                 return;
258         task->result->cleanup_local = true;
259         argv[6] = mountpoint;
260
261         rc = pb_mkdir_recursive(mountpoint);
262         if (rc)
263                 return;
264
265         opts = talloc_strdup(NULL, "ro,nolock,nodiratime");
266         argv[3] = opts;
267
268         if (task->url->port)
269                 opts = talloc_asprintf_append(opts, ",port=%s",
270                                                 task->url->port);
271
272         task->result->local = talloc_asprintf(task->result, "%s/%s",
273                                                         mountpoint,
274                                                         task->url->path);
275
276         task->process->path = pb_system_apps.mount;
277         task->process->argv = argv;
278
279         if (task->async) {
280                 rc = process_run_async(task->process);
281                 if (rc) {
282                         process_release(task->process);
283                         task->process = NULL;
284                 }
285                 task->result->status = rc ? LOAD_ERROR : LOAD_ASYNC;
286         } else {
287                 rc = process_run_sync(task->process);
288                 task->result->status = rc ? LOAD_ERROR : LOAD_OK;
289                 process_release(task->process);
290                 task->process = NULL;
291         }
292
293         talloc_free(opts);
294 }
295
296 static void load_sftp(struct load_task *task)
297 {
298         const char *argv[] = {
299                         pb_system_apps.sftp,
300                         NULL,           /* 1: host:path */
301                         NULL,           /* 2: local file */
302                         NULL,
303         };
304
305         argv[1] = talloc_asprintf(task, "%s:%s",
306                                 task->url->host, task->url->path);
307         load_process_to_local_file(task, argv, 2);
308 }
309
310 static enum tftp_type check_tftp_type(void *ctx)
311 {
312         const char *argv[] = { pb_system_apps.tftp, "-V", NULL };
313         struct process *process;
314         enum tftp_type type;
315         int rc;
316
317         process = process_create(ctx);
318         process->path = pb_system_apps.tftp;
319         process->argv = argv;
320         process->keep_stdout = true;
321         process->add_stderr = true;
322         rc = process_run_sync(process);
323
324         if (rc || !process->stdout_buf || process->stdout_len == 0) {
325                 pb_log("Can't check TFTP client type!\n");
326                 type = TFTP_TYPE_BROKEN;
327
328         } else if (memmem(process->stdout_buf, process->stdout_len,
329                                 "tftp-hpa", strlen("tftp-hpa"))) {
330                 pb_debug("Found TFTP client type: tftp-hpa\n");
331                 type = TFTP_TYPE_HPA;
332
333         } else if (memmem(process->stdout_buf, process->stdout_len,
334                                 "BusyBox", strlen("BusyBox"))) {
335                 pb_debug("Found TFTP client type: BusyBox tftp\n");
336                 type = TFTP_TYPE_BUSYBOX;
337
338         } else {
339                 pb_log("Unknown TFTP client type!\n");
340                 type = TFTP_TYPE_BROKEN;
341         }
342
343         process_release(process);
344         return type;
345 }
346
347 static void load_tftp(struct load_task *task)
348 {
349         const char *port = "69";
350         const char *argv[10] = {
351                 pb_system_apps.tftp,
352         };
353
354         if (task->url->port)
355                 port = task->url->port;
356
357         if (tftp_type == TFTP_TYPE_UNKNOWN)
358                 tftp_type = check_tftp_type(task);
359
360         if (tftp_type == TFTP_TYPE_BUSYBOX) {
361                 argv[1] = "-g";
362                 argv[2] = "-l";
363                 argv[3] = NULL; /* 3: local file */
364                 argv[4] = "-r";
365                 argv[5] = task->url->path;
366                 argv[6] = task->url->host;
367                 argv[7] = port;
368                 argv[8] = NULL;
369
370                 load_process_to_local_file(task, argv, 3);
371
372         } else if (tftp_type == TFTP_TYPE_HPA) {
373                 argv[1] = "-m";
374                 argv[2] = "binary";
375                 argv[3] = task->url->host;
376                 argv[4] = port;
377                 argv[5] = "-c";
378                 argv[6] = "get";
379                 argv[7] = task->url->path;
380                 argv[8] = NULL; /* 8: local file */
381                 argv[9] = NULL;
382                 load_process_to_local_file(task, argv, 8);
383
384         } else
385                 task->result->status = LOAD_ERROR;
386 }
387
388 enum wget_flags {
389         wget_empty                      = 0x1,
390         wget_no_check_certificate       = 0x2,
391         wget_verbose                    = 0x4,
392 };
393
394 /**
395  * pb_load_wget - Loads a remote file via wget and returns the local file path.
396  *
397  * Returns the local file path in a talloc'ed character string on success,
398  * or NULL on error.
399  */
400
401 static void load_wget(struct load_task *task, int flags)
402 {
403         const char *argv[] = {
404                 pb_system_apps.wget,
405                 "-O",
406                 NULL, /* 2: local file */
407                 NULL, /* 3 (optional): --quiet */
408                 NULL, /* 4 (optional): --no-check-certificate */
409                 NULL, /* 5: URL */
410                 NULL,
411         };
412         int i;
413
414         if (task->process->stdout_cb)
415                 flags |= wget_verbose;
416
417         i = 3;
418 #if defined(DEBUG)
419         flags |= wget_verbose;
420 #endif
421         if ((flags & wget_verbose) == 0)
422                 argv[i++] = "--quiet";
423
424         if (flags & wget_no_check_certificate)
425                 argv[i++] = "--no-check-certificate";
426
427         argv[i] = task->url->full;
428
429         load_process_to_local_file(task, argv, 2);
430 }
431
432 /* Although we don't need to load anything for a local path (we just return
433  * the path from the file:// URL), the other load helpers will error-out on
434  * non-existant files. So, do the same here with an access() check on the local
435  * filename.
436  */
437 static void load_local(struct load_task *task)
438 {
439         struct load_url_result *result = task->result;
440         int rc;
441
442         rc = access(task->url->path, F_OK);
443         if (rc) {
444                 result->status = LOAD_ERROR;
445         } else {
446                 result->local = talloc_strdup(task->result, task->url->path);
447                 result->status = LOAD_OK;
448         }
449
450         task->async_cb(task->result, task->async_data);
451 }
452
453 static void load_url_async_start_pending(struct load_task *task, int flags)
454 {
455         pb_log("Starting pending job for %s\n", task->url->full);
456
457         switch (task->url->scheme) {
458         case pb_url_ftp:
459         case pb_url_http:
460                 load_wget(task, flags);
461                 break;
462         case pb_url_https:
463                 flags |= wget_no_check_certificate;
464                 load_wget(task, flags);
465                 break;
466         case pb_url_nfs:
467                 load_nfs(task);
468                 break;
469         case pb_url_sftp:
470                 load_sftp(task);
471                 break;
472         case pb_url_tftp:
473                 load_tftp(task);
474                 break;
475         default:
476                 /* Shouldn't be a need via this path but.. */
477                 load_local(task);
478                 break;
479         }
480
481         if (task->result->status == LOAD_ERROR) {
482                 pb_log("Pending job failed for %s\n", task->url->full);
483                 load_url_result_cleanup_local(task->result);
484                 talloc_free(task->result);
485                 talloc_free(task);
486         }
487 }
488
489 void pending_network_jobs_start(void)
490 {
491         struct network_job *job, *tmp;
492
493         if (!pending_network_jobs.head.next)
494                 return;
495
496         list_for_each_entry_safe(&pending_network_jobs, job, tmp, list) {
497                 load_url_async_start_pending(job->task, job->flags);
498                 list_remove(&job->list);
499         }
500 }
501
502 void pending_network_jobs_cancel(void)
503 {
504         struct network_job *job, *tmp;
505
506         if (!pending_network_jobs.head.next)
507                 return;
508
509         list_for_each_entry_safe(&pending_network_jobs, job, tmp, list)
510                 talloc_free(job);
511         list_init(&pending_network_jobs);
512 }
513
514 static void pending_network_jobs_add(struct load_task *task, int flags)
515 {
516         struct network_job *job;
517
518         if (!pending_network_jobs.head.next)
519                 list_init(&pending_network_jobs);
520
521         job = talloc(task, struct network_job);
522         if (!job) {
523                 pb_log("Failed to allocate space for pending job\n");
524                 return;
525         }
526
527         job->task = task;
528         job->flags = flags;
529         list_add_tail(&pending_network_jobs, &job->list);
530 }
531
532
533 /**
534  * load_url - Loads a (possibly) remote URL and returns the local file
535  * path.
536  * @ctx: The talloc context to associate with the returned string.
537  * @url: The remote file URL.
538  * @tempfile: An optional variable pointer to be set when a temporary local
539  *  file is created.
540  * @url_cb: An optional callback pointer if the caller wants to load url
541  *  asynchronously.
542  *
543  * Returns the local file path in a talloc'ed character string on success,
544  * or NULL on error.
545  */
546
547 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
548                 load_url_complete async_cb, void *async_data,
549                 waiter_cb stdout_cb, void *stdout_data)
550 {
551         struct load_url_result *result;
552         struct load_task *task;
553         int flags = 0;
554
555         if (!url)
556                 return NULL;
557
558         task = talloc_zero(ctx, struct load_task);
559         task->url = url;
560         task->async = async_cb != NULL;
561         task->result = talloc_zero(ctx, struct load_url_result);
562         task->result->task = task;
563         task->result->url = url;
564         task->process = process_create(task);
565         if (task->async) {
566                 task->async_cb = async_cb;
567                 task->async_data = async_data;
568                 task->process->exit_cb = load_url_process_exit;
569                 task->process->data = task;
570                 task->process->stdout_cb = stdout_cb;
571                 task->process->stdout_data = stdout_data;
572         }
573
574         if (!stdout_cb && stdout_data && have_busybox())
575                 task->process->stdout_cb = busybox_progress_cb;
576
577         /* Make sure we save output for any task that has a custom handler */
578         if (task->process->stdout_cb) {
579                 task->process->add_stderr = true;
580                 task->process->keep_stdout = true;
581         }
582
583         /* If the url is remote but network is not yet available queue up this
584          * load for later */
585         if (!system_info_network_available() && url->scheme != pb_url_file) {
586                 pb_log("load task for %s queued pending network\n", url->full);
587                 pending_network_jobs_add(task, flags);
588                 task->result->status = LOAD_ASYNC;
589                 return task->result;
590         }
591
592         switch (url->scheme) {
593         case pb_url_ftp:
594         case pb_url_http:
595                 load_wget(task, flags);
596                 break;
597         case pb_url_https:
598                 flags |= wget_no_check_certificate;
599                 load_wget(task, flags);
600                 break;
601         case pb_url_nfs:
602                 load_nfs(task);
603                 break;
604         case pb_url_sftp:
605                 load_sftp(task);
606                 break;
607         case pb_url_tftp:
608                 load_tftp(task);
609                 break;
610         default:
611                 load_local(task);
612                 break;
613         }
614
615         result = task->result;
616         if (result->status == LOAD_ERROR) {
617                 load_url_result_cleanup_local(task->result);
618                 talloc_free(result);
619                 talloc_free(task);
620                 return NULL;
621         }
622
623         if (!task->async || result->status == LOAD_OK)
624                 talloc_free(task);
625
626         return result;
627 }
628
629 struct load_url_result *load_url(void *ctx, struct pb_url *url)
630 {
631         return load_url_async(ctx, url, NULL, NULL, NULL, NULL);
632 }
633
634 void load_url_async_cancel(struct load_url_result *res)
635 {
636         struct load_task *task = res->task;
637
638         /* the completion callback may have already been called; this clears
639          * res->task */
640         if (!task)
641                 return;
642
643         if (res->status == LOAD_CANCELLED)
644                 return;
645
646         assert(task->async);
647         assert(task->process);
648
649         res->status = LOAD_CANCELLED;
650         process_stop_async(task->process);
651 }
652
653 #else
654
655 static void __attribute__((unused)) load_local(
656                 struct load_task *task __attribute__((unused)))
657 {
658 }
659 static void __attribute__((unused)) load_wget(
660                 struct load_task *task __attribute__((unused)),
661                 int flags __attribute__((unused)))
662 {
663 }
664 static void __attribute__((unused)) load_tftp(
665                 struct load_task *task __attribute__((unused)))
666 {
667 }
668 static void __attribute__((unused)) load_sftp(
669                 struct load_task *task __attribute__((unused)))
670 {
671 }
672 static void __attribute__((unused)) load_nfs(
673                 struct load_task *task __attribute__((unused)))
674 {
675 }
676 static void __attribute__((unused)) load_url_process_exit(
677                 struct process *process __attribute__((unused)))
678 {
679 }
680
681 #endif