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