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