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