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