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