]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/importd.c
Merge pull request #1165 from poettering/nspawn-files
[thirdparty/systemd.git] / src / import / importd.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2015 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/prctl.h>
23
24 #include "sd-bus.h"
25 #include "util.h"
26 #include "strv.h"
27 #include "bus-util.h"
28 #include "bus-common-errors.h"
29 #include "socket-util.h"
30 #include "mkdir.h"
31 #include "def.h"
32 #include "missing.h"
33 #include "machine-pool.h"
34 #include "path-util.h"
35 #include "import-util.h"
36 #include "process-util.h"
37 #include "signal-util.h"
38 #include "hostname-util.h"
39
40 typedef struct Transfer Transfer;
41 typedef struct Manager Manager;
42
43 typedef enum TransferType {
44 TRANSFER_IMPORT_TAR,
45 TRANSFER_IMPORT_RAW,
46 TRANSFER_EXPORT_TAR,
47 TRANSFER_EXPORT_RAW,
48 TRANSFER_PULL_TAR,
49 TRANSFER_PULL_RAW,
50 TRANSFER_PULL_DKR,
51 _TRANSFER_TYPE_MAX,
52 _TRANSFER_TYPE_INVALID = -1,
53 } TransferType;
54
55 struct Transfer {
56 Manager *manager;
57
58 uint32_t id;
59 char *object_path;
60
61 TransferType type;
62 ImportVerify verify;
63
64 char *remote;
65 char *local;
66 bool force_local;
67 bool read_only;
68
69 char *dkr_index_url;
70 char *format;
71
72 pid_t pid;
73
74 int log_fd;
75
76 char log_message[LINE_MAX];
77 size_t log_message_size;
78
79 sd_event_source *pid_event_source;
80 sd_event_source *log_event_source;
81
82 unsigned n_canceled;
83 unsigned progress_percent;
84
85 int stdin_fd;
86 int stdout_fd;
87 };
88
89 struct Manager {
90 sd_event *event;
91 sd_bus *bus;
92
93 uint32_t current_transfer_id;
94 Hashmap *transfers;
95
96 Hashmap *polkit_registry;
97
98 int notify_fd;
99
100 sd_event_source *notify_event_source;
101 };
102
103 #define TRANSFERS_MAX 64
104
105 static const char* const transfer_type_table[_TRANSFER_TYPE_MAX] = {
106 [TRANSFER_IMPORT_TAR] = "import-tar",
107 [TRANSFER_IMPORT_RAW] = "import-raw",
108 [TRANSFER_EXPORT_TAR] = "export-tar",
109 [TRANSFER_EXPORT_RAW] = "export-raw",
110 [TRANSFER_PULL_TAR] = "pull-tar",
111 [TRANSFER_PULL_RAW] = "pull-raw",
112 [TRANSFER_PULL_DKR] = "pull-dkr",
113 };
114
115 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(transfer_type, TransferType);
116
117 static Transfer *transfer_unref(Transfer *t) {
118 if (!t)
119 return NULL;
120
121 if (t->manager)
122 hashmap_remove(t->manager->transfers, UINT32_TO_PTR(t->id));
123
124 sd_event_source_unref(t->pid_event_source);
125 sd_event_source_unref(t->log_event_source);
126
127 free(t->remote);
128 free(t->local);
129 free(t->dkr_index_url);
130 free(t->format);
131 free(t->object_path);
132
133 if (t->pid > 0) {
134 (void) kill_and_sigcont(t->pid, SIGKILL);
135 (void) wait_for_terminate(t->pid, NULL);
136 }
137
138 safe_close(t->log_fd);
139 safe_close(t->stdin_fd);
140 safe_close(t->stdout_fd);
141
142 free(t);
143 return NULL;
144 }
145
146 DEFINE_TRIVIAL_CLEANUP_FUNC(Transfer*, transfer_unref);
147
148 static int transfer_new(Manager *m, Transfer **ret) {
149 _cleanup_(transfer_unrefp) Transfer *t = NULL;
150 uint32_t id;
151 int r;
152
153 assert(m);
154 assert(ret);
155
156 if (hashmap_size(m->transfers) >= TRANSFERS_MAX)
157 return -E2BIG;
158
159 r = hashmap_ensure_allocated(&m->transfers, &trivial_hash_ops);
160 if (r < 0)
161 return r;
162
163 t = new0(Transfer, 1);
164 if (!t)
165 return -ENOMEM;
166
167 t->type = _TRANSFER_TYPE_INVALID;
168 t->log_fd = -1;
169 t->stdin_fd = -1;
170 t->verify = _IMPORT_VERIFY_INVALID;
171
172 id = m->current_transfer_id + 1;
173
174 if (asprintf(&t->object_path, "/org/freedesktop/import1/transfer/_%" PRIu32, id) < 0)
175 return -ENOMEM;
176
177 r = hashmap_put(m->transfers, UINT32_TO_PTR(id), t);
178 if (r < 0)
179 return r;
180
181 m->current_transfer_id = id;
182
183 t->manager = m;
184 t->id = id;
185
186 *ret = t;
187 t = NULL;
188
189 return 0;
190 }
191
192 static void transfer_send_log_line(Transfer *t, const char *line) {
193 int r, priority = LOG_INFO;
194
195 assert(t);
196 assert(line);
197
198 syslog_parse_priority(&line, &priority, true);
199
200 log_full(priority, "(transfer%" PRIu32 ") %s", t->id, line);
201
202 r = sd_bus_emit_signal(
203 t->manager->bus,
204 t->object_path,
205 "org.freedesktop.import1.Transfer",
206 "LogMessage",
207 "us",
208 priority,
209 line);
210 if (r < 0)
211 log_error_errno(r, "Cannot emit message: %m");
212 }
213
214 static void transfer_send_logs(Transfer *t, bool flush) {
215 assert(t);
216
217 /* Try to send out all log messages, if we can. But if we
218 * can't we remove the messages from the buffer, but don't
219 * fail */
220
221 while (t->log_message_size > 0) {
222 _cleanup_free_ char *n = NULL;
223 char *e;
224
225 if (t->log_message_size >= sizeof(t->log_message))
226 e = t->log_message + sizeof(t->log_message);
227 else {
228 char *a, *b;
229
230 a = memchr(t->log_message, 0, t->log_message_size);
231 b = memchr(t->log_message, '\n', t->log_message_size);
232
233 if (a && b)
234 e = a < b ? a : b;
235 else if (a)
236 e = a;
237 else
238 e = b;
239 }
240
241 if (!e) {
242 if (!flush)
243 return;
244
245 e = t->log_message + t->log_message_size;
246 }
247
248 n = strndup(t->log_message, e - t->log_message);
249
250 /* Skip over NUL and newlines */
251 while ((e < t->log_message + t->log_message_size) && (*e == 0 || *e == '\n'))
252 e++;
253
254 memmove(t->log_message, e, t->log_message + sizeof(t->log_message) - e);
255 t->log_message_size -= e - t->log_message;
256
257 if (!n) {
258 log_oom();
259 continue;
260 }
261
262 if (isempty(n))
263 continue;
264
265 transfer_send_log_line(t, n);
266 }
267 }
268
269 static int transfer_finalize(Transfer *t, bool success) {
270 int r;
271
272 assert(t);
273
274 transfer_send_logs(t, true);
275
276 r = sd_bus_emit_signal(
277 t->manager->bus,
278 "/org/freedesktop/import1",
279 "org.freedesktop.import1.Manager",
280 "TransferRemoved",
281 "uos",
282 t->id,
283 t->object_path,
284 success ? "done" :
285 t->n_canceled > 0 ? "canceled" : "failed");
286
287 if (r < 0)
288 log_error_errno(r, "Cannot emit message: %m");
289
290 transfer_unref(t);
291 return 0;
292 }
293
294 static int transfer_cancel(Transfer *t) {
295 int r;
296
297 assert(t);
298
299 r = kill_and_sigcont(t->pid, t->n_canceled < 3 ? SIGTERM : SIGKILL);
300 if (r < 0)
301 return r;
302
303 t->n_canceled++;
304 return 0;
305 }
306
307 static int transfer_on_pid(sd_event_source *s, const siginfo_t *si, void *userdata) {
308 Transfer *t = userdata;
309 bool success = false;
310
311 assert(s);
312 assert(t);
313
314 if (si->si_code == CLD_EXITED) {
315 if (si->si_status != 0)
316 log_error("Import process failed with exit code %i.", si->si_status);
317 else {
318 log_debug("Import process succeeded.");
319 success = true;
320 }
321
322 } else if (si->si_code == CLD_KILLED ||
323 si->si_code == CLD_DUMPED)
324
325 log_error("Import process terminated by signal %s.", signal_to_string(si->si_status));
326 else
327 log_error("Import process failed due to unknown reason.");
328
329 t->pid = 0;
330
331 return transfer_finalize(t, success);
332 }
333
334 static int transfer_on_log(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
335 Transfer *t = userdata;
336 ssize_t l;
337
338 assert(s);
339 assert(t);
340
341 l = read(fd, t->log_message + t->log_message_size, sizeof(t->log_message) - t->log_message_size);
342 if (l <= 0) {
343 /* EOF/read error. We just close the pipe here, and
344 * close the watch, waiting for the SIGCHLD to arrive,
345 * before we do anything else. */
346
347 if (l < 0)
348 log_error_errno(errno, "Failed to read log message: %m");
349
350 t->log_event_source = sd_event_source_unref(t->log_event_source);
351 return 0;
352 }
353
354 t->log_message_size += l;
355
356 transfer_send_logs(t, false);
357
358 return 0;
359 }
360
361 static int transfer_start(Transfer *t) {
362 _cleanup_close_pair_ int pipefd[2] = { -1, -1 };
363 int r;
364
365 assert(t);
366 assert(t->pid <= 0);
367
368 if (pipe2(pipefd, O_CLOEXEC) < 0)
369 return -errno;
370
371 t->pid = fork();
372 if (t->pid < 0)
373 return -errno;
374 if (t->pid == 0) {
375 const char *cmd[] = {
376 NULL, /* systemd-import, systemd-export or systemd-pull */
377 NULL, /* tar, raw, dkr */
378 NULL, /* --verify= */
379 NULL, /* verify argument */
380 NULL, /* maybe --force */
381 NULL, /* maybe --read-only */
382 NULL, /* maybe --dkr-index-url */
383 NULL, /* if so: the actual URL */
384 NULL, /* maybe --format= */
385 NULL, /* if so: the actual format */
386 NULL, /* remote */
387 NULL, /* local */
388 NULL
389 };
390 unsigned k = 0;
391
392 /* Child */
393
394 (void) reset_all_signal_handlers();
395 (void) reset_signal_mask();
396 assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
397
398 pipefd[0] = safe_close(pipefd[0]);
399
400 if (dup2(pipefd[1], STDERR_FILENO) != STDERR_FILENO) {
401 log_error_errno(errno, "Failed to dup2() fd: %m");
402 _exit(EXIT_FAILURE);
403 }
404
405 if (t->stdout_fd >= 0) {
406 if (dup2(t->stdout_fd, STDOUT_FILENO) != STDOUT_FILENO) {
407 log_error_errno(errno, "Failed to dup2() fd: %m");
408 _exit(EXIT_FAILURE);
409 }
410
411 if (t->stdout_fd != STDOUT_FILENO)
412 safe_close(t->stdout_fd);
413 } else {
414 if (dup2(pipefd[1], STDOUT_FILENO) != STDOUT_FILENO) {
415 log_error_errno(errno, "Failed to dup2() fd: %m");
416 _exit(EXIT_FAILURE);
417 }
418 }
419
420 if (pipefd[1] != STDOUT_FILENO && pipefd[1] != STDERR_FILENO)
421 pipefd[1] = safe_close(pipefd[1]);
422
423 if (t->stdin_fd >= 0) {
424 if (dup2(t->stdin_fd, STDIN_FILENO) != STDIN_FILENO) {
425 log_error_errno(errno, "Failed to dup2() fd: %m");
426 _exit(EXIT_FAILURE);
427 }
428
429 if (t->stdin_fd != STDIN_FILENO)
430 safe_close(t->stdin_fd);
431 } else {
432 int null_fd;
433
434 null_fd = open("/dev/null", O_RDONLY|O_NOCTTY);
435 if (null_fd < 0) {
436 log_error_errno(errno, "Failed to open /dev/null: %m");
437 _exit(EXIT_FAILURE);
438 }
439
440 if (dup2(null_fd, STDIN_FILENO) != STDIN_FILENO) {
441 log_error_errno(errno, "Failed to dup2() fd: %m");
442 _exit(EXIT_FAILURE);
443 }
444
445 if (null_fd != STDIN_FILENO)
446 safe_close(null_fd);
447 }
448
449 fd_cloexec(STDIN_FILENO, false);
450 fd_cloexec(STDOUT_FILENO, false);
451 fd_cloexec(STDERR_FILENO, false);
452
453 setenv("SYSTEMD_LOG_TARGET", "console-prefixed", 1);
454 setenv("NOTIFY_SOCKET", "/run/systemd/import/notify", 1);
455
456 if (IN_SET(t->type, TRANSFER_IMPORT_TAR, TRANSFER_IMPORT_RAW))
457 cmd[k++] = SYSTEMD_IMPORT_PATH;
458 else if (IN_SET(t->type, TRANSFER_EXPORT_TAR, TRANSFER_EXPORT_RAW))
459 cmd[k++] = SYSTEMD_EXPORT_PATH;
460 else
461 cmd[k++] = SYSTEMD_PULL_PATH;
462
463 if (IN_SET(t->type, TRANSFER_IMPORT_TAR, TRANSFER_EXPORT_TAR, TRANSFER_PULL_TAR))
464 cmd[k++] = "tar";
465 else if (IN_SET(t->type, TRANSFER_IMPORT_RAW, TRANSFER_EXPORT_RAW, TRANSFER_PULL_RAW))
466 cmd[k++] = "raw";
467 else
468 cmd[k++] = "dkr";
469
470 if (t->verify != _IMPORT_VERIFY_INVALID) {
471 cmd[k++] = "--verify";
472 cmd[k++] = import_verify_to_string(t->verify);
473 }
474
475 if (t->force_local)
476 cmd[k++] = "--force";
477 if (t->read_only)
478 cmd[k++] = "--read-only";
479
480 if (t->dkr_index_url) {
481 cmd[k++] = "--dkr-index-url";
482 cmd[k++] = t->dkr_index_url;
483 }
484
485 if (t->format) {
486 cmd[k++] = "--format";
487 cmd[k++] = t->format;
488 }
489
490 if (!IN_SET(t->type, TRANSFER_EXPORT_TAR, TRANSFER_EXPORT_RAW)) {
491 if (t->remote)
492 cmd[k++] = t->remote;
493 else
494 cmd[k++] = "-";
495 }
496
497 if (t->local)
498 cmd[k++] = t->local;
499 cmd[k] = NULL;
500
501 execv(cmd[0], (char * const *) cmd);
502 log_error_errno(errno, "Failed to execute %s tool: %m", cmd[0]);
503 _exit(EXIT_FAILURE);
504 }
505
506 pipefd[1] = safe_close(pipefd[1]);
507 t->log_fd = pipefd[0];
508 pipefd[0] = -1;
509
510 t->stdin_fd = safe_close(t->stdin_fd);
511
512 r = sd_event_add_child(t->manager->event, &t->pid_event_source, t->pid, WEXITED, transfer_on_pid, t);
513 if (r < 0)
514 return r;
515
516 r = sd_event_add_io(t->manager->event, &t->log_event_source, t->log_fd, EPOLLIN, transfer_on_log, t);
517 if (r < 0)
518 return r;
519
520 /* Make sure always process logging before SIGCHLD */
521 r = sd_event_source_set_priority(t->log_event_source, SD_EVENT_PRIORITY_NORMAL -5);
522 if (r < 0)
523 return r;
524
525 r = sd_bus_emit_signal(
526 t->manager->bus,
527 "/org/freedesktop/import1",
528 "org.freedesktop.import1.Manager",
529 "TransferNew",
530 "uo",
531 t->id,
532 t->object_path);
533 if (r < 0)
534 return r;
535
536 return 0;
537 }
538
539 static Manager *manager_unref(Manager *m) {
540 Transfer *t;
541
542 if (!m)
543 return NULL;
544
545 sd_event_source_unref(m->notify_event_source);
546 safe_close(m->notify_fd);
547
548 while ((t = hashmap_first(m->transfers)))
549 transfer_unref(t);
550
551 hashmap_free(m->transfers);
552
553 bus_verify_polkit_async_registry_free(m->polkit_registry);
554
555 m->bus = sd_bus_flush_close_unref(m->bus);
556 sd_event_unref(m->event);
557
558 free(m);
559 return NULL;
560 }
561
562 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref);
563
564 static int manager_on_notify(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
565
566 char buf[NOTIFY_BUFFER_MAX+1];
567 struct iovec iovec = {
568 .iov_base = buf,
569 .iov_len = sizeof(buf)-1,
570 };
571 union {
572 struct cmsghdr cmsghdr;
573 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
574 CMSG_SPACE(sizeof(int) * NOTIFY_FD_MAX)];
575 } control = {};
576 struct msghdr msghdr = {
577 .msg_iov = &iovec,
578 .msg_iovlen = 1,
579 .msg_control = &control,
580 .msg_controllen = sizeof(control),
581 };
582 struct ucred *ucred = NULL;
583 Manager *m = userdata;
584 struct cmsghdr *cmsg;
585 unsigned percent;
586 char *p, *e;
587 Transfer *t;
588 Iterator i;
589 ssize_t n;
590 int r;
591
592 n = recvmsg(fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
593 if (n < 0) {
594 if (errno == EAGAIN || errno == EINTR)
595 return 0;
596
597 return -errno;
598 }
599
600 cmsg_close_all(&msghdr);
601
602 CMSG_FOREACH(cmsg, &msghdr) {
603 if (cmsg->cmsg_level == SOL_SOCKET &&
604 cmsg->cmsg_type == SCM_CREDENTIALS &&
605 cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
606
607 ucred = (struct ucred*) CMSG_DATA(cmsg);
608 }
609 }
610
611 if (msghdr.msg_flags & MSG_TRUNC) {
612 log_warning("Got overly long notification datagram, ignoring.");
613 return 0;
614 }
615
616 if (!ucred || ucred->pid <= 0) {
617 log_warning("Got notification datagram lacking credential information, ignoring.");
618 return 0;
619 }
620
621 HASHMAP_FOREACH(t, m->transfers, i)
622 if (ucred->pid == t->pid)
623 break;
624
625 if (!t) {
626 log_warning("Got notification datagram from unexpected peer, ignoring.");
627 return 0;
628 }
629
630 buf[n] = 0;
631
632 p = startswith(buf, "X_IMPORT_PROGRESS=");
633 if (!p) {
634 p = strstr(buf, "\nX_IMPORT_PROGRESS=");
635 if (!p)
636 return 0;
637
638 p += 19;
639 }
640
641 e = strchrnul(p, '\n');
642 *e = 0;
643
644 r = safe_atou(p, &percent);
645 if (r < 0 || percent > 100) {
646 log_warning("Got invalid percent value, ignoring.");
647 return 0;
648 }
649
650 t->progress_percent = percent;
651
652 log_debug("Got percentage from client: %u%%", percent);
653 return 0;
654 }
655
656 static int manager_new(Manager **ret) {
657 _cleanup_(manager_unrefp) Manager *m = NULL;
658 static const union sockaddr_union sa = {
659 .un.sun_family = AF_UNIX,
660 .un.sun_path = "/run/systemd/import/notify",
661 };
662 static const int one = 1;
663 int r;
664
665 assert(ret);
666
667 m = new0(Manager, 1);
668 if (!m)
669 return -ENOMEM;
670
671 r = sd_event_default(&m->event);
672 if (r < 0)
673 return r;
674
675 sd_event_set_watchdog(m->event, true);
676
677 r = sd_bus_default_system(&m->bus);
678 if (r < 0)
679 return r;
680
681 m->notify_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
682 if (m->notify_fd < 0)
683 return -errno;
684
685 (void) mkdir_parents_label(sa.un.sun_path, 0755);
686 (void) unlink(sa.un.sun_path);
687
688 if (bind(m->notify_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path)) < 0)
689 return -errno;
690
691 if (setsockopt(m->notify_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0)
692 return -errno;
693
694 r = sd_event_add_io(m->event, &m->notify_event_source, m->notify_fd, EPOLLIN, manager_on_notify, m);
695 if (r < 0)
696 return r;
697
698 *ret = m;
699 m = NULL;
700
701 return 0;
702 }
703
704 static Transfer *manager_find(Manager *m, TransferType type, const char *dkr_index_url, const char *remote) {
705 Transfer *t;
706 Iterator i;
707
708 assert(m);
709 assert(type >= 0);
710 assert(type < _TRANSFER_TYPE_MAX);
711
712 HASHMAP_FOREACH(t, m->transfers, i) {
713
714 if (t->type == type &&
715 streq_ptr(t->remote, remote) &&
716 streq_ptr(t->dkr_index_url, dkr_index_url))
717 return t;
718 }
719
720 return NULL;
721 }
722
723 static int method_import_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
724 _cleanup_(transfer_unrefp) Transfer *t = NULL;
725 int fd, force, read_only, r;
726 const char *local, *object;
727 Manager *m = userdata;
728 TransferType type;
729 uint32_t id;
730
731 assert(msg);
732 assert(m);
733
734 r = bus_verify_polkit_async(
735 msg,
736 CAP_SYS_ADMIN,
737 "org.freedesktop.import1.import",
738 NULL,
739 false,
740 UID_INVALID,
741 &m->polkit_registry,
742 error);
743 if (r < 0)
744 return r;
745 if (r == 0)
746 return 1; /* Will call us back */
747
748 r = sd_bus_message_read(msg, "hsbb", &fd, &local, &force, &read_only);
749 if (r < 0)
750 return r;
751
752 if (!machine_name_is_valid(local))
753 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Local name %s is invalid", local);
754
755 r = setup_machine_directory((uint64_t) -1, error);
756 if (r < 0)
757 return r;
758
759 type = streq_ptr(sd_bus_message_get_member(msg), "ImportTar") ? TRANSFER_IMPORT_TAR : TRANSFER_IMPORT_RAW;
760
761 r = transfer_new(m, &t);
762 if (r < 0)
763 return r;
764
765 t->type = type;
766 t->force_local = force;
767 t->read_only = read_only;
768
769 t->local = strdup(local);
770 if (!t->local)
771 return -ENOMEM;
772
773 t->stdin_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
774 if (t->stdin_fd < 0)
775 return -errno;
776
777 r = transfer_start(t);
778 if (r < 0)
779 return r;
780
781 object = t->object_path;
782 id = t->id;
783 t = NULL;
784
785 return sd_bus_reply_method_return(msg, "uo", id, object);
786 }
787
788 static int method_export_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
789 _cleanup_(transfer_unrefp) Transfer *t = NULL;
790 int fd, r;
791 const char *local, *object, *format;
792 Manager *m = userdata;
793 TransferType type;
794 uint32_t id;
795
796 assert(msg);
797 assert(m);
798
799 r = bus_verify_polkit_async(
800 msg,
801 CAP_SYS_ADMIN,
802 "org.freedesktop.import1.export",
803 NULL,
804 false,
805 UID_INVALID,
806 &m->polkit_registry,
807 error);
808 if (r < 0)
809 return r;
810 if (r == 0)
811 return 1; /* Will call us back */
812
813 r = sd_bus_message_read(msg, "shs", &local, &fd, &format);
814 if (r < 0)
815 return r;
816
817 if (!machine_name_is_valid(local))
818 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Local name %s is invalid", local);
819
820 type = streq_ptr(sd_bus_message_get_member(msg), "ExportTar") ? TRANSFER_EXPORT_TAR : TRANSFER_EXPORT_RAW;
821
822 r = transfer_new(m, &t);
823 if (r < 0)
824 return r;
825
826 t->type = type;
827
828 if (!isempty(format)) {
829 t->format = strdup(format);
830 if (!t->format)
831 return -ENOMEM;
832 }
833
834 t->local = strdup(local);
835 if (!t->local)
836 return -ENOMEM;
837
838 t->stdout_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
839 if (t->stdout_fd < 0)
840 return -errno;
841
842 r = transfer_start(t);
843 if (r < 0)
844 return r;
845
846 object = t->object_path;
847 id = t->id;
848 t = NULL;
849
850 return sd_bus_reply_method_return(msg, "uo", id, object);
851 }
852
853 static int method_pull_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
854 _cleanup_(transfer_unrefp) Transfer *t = NULL;
855 const char *remote, *local, *verify, *object;
856 Manager *m = userdata;
857 ImportVerify v;
858 TransferType type;
859 int force, r;
860 uint32_t id;
861
862 assert(msg);
863 assert(m);
864
865 r = bus_verify_polkit_async(
866 msg,
867 CAP_SYS_ADMIN,
868 "org.freedesktop.import1.pull",
869 NULL,
870 false,
871 UID_INVALID,
872 &m->polkit_registry,
873 error);
874 if (r < 0)
875 return r;
876 if (r == 0)
877 return 1; /* Will call us back */
878
879 r = sd_bus_message_read(msg, "sssb", &remote, &local, &verify, &force);
880 if (r < 0)
881 return r;
882
883 if (!http_url_is_valid(remote))
884 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "URL %s is invalid", remote);
885
886 if (isempty(local))
887 local = NULL;
888 else if (!machine_name_is_valid(local))
889 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Local name %s is invalid", local);
890
891 if (isempty(verify))
892 v = IMPORT_VERIFY_SIGNATURE;
893 else
894 v = import_verify_from_string(verify);
895 if (v < 0)
896 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown verification mode %s", verify);
897
898 r = setup_machine_directory((uint64_t) -1, error);
899 if (r < 0)
900 return r;
901
902 type = streq_ptr(sd_bus_message_get_member(msg), "PullTar") ? TRANSFER_PULL_TAR : TRANSFER_PULL_RAW;
903
904 if (manager_find(m, type, NULL, remote))
905 return sd_bus_error_setf(error, BUS_ERROR_TRANSFER_IN_PROGRESS, "Transfer for %s already in progress.", remote);
906
907 r = transfer_new(m, &t);
908 if (r < 0)
909 return r;
910
911 t->type = type;
912 t->verify = v;
913 t->force_local = force;
914
915 t->remote = strdup(remote);
916 if (!t->remote)
917 return -ENOMEM;
918
919 if (local) {
920 t->local = strdup(local);
921 if (!t->local)
922 return -ENOMEM;
923 }
924
925 r = transfer_start(t);
926 if (r < 0)
927 return r;
928
929 object = t->object_path;
930 id = t->id;
931 t = NULL;
932
933 return sd_bus_reply_method_return(msg, "uo", id, object);
934 }
935
936 static int method_pull_dkr(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
937 _cleanup_(transfer_unrefp) Transfer *t = NULL;
938 const char *index_url, *remote, *tag, *local, *verify, *object;
939 Manager *m = userdata;
940 ImportVerify v;
941 int force, r;
942 uint32_t id;
943
944 assert(msg);
945 assert(m);
946
947 r = bus_verify_polkit_async(
948 msg,
949 CAP_SYS_ADMIN,
950 "org.freedesktop.import1.pull",
951 NULL,
952 false,
953 UID_INVALID,
954 &m->polkit_registry,
955 error);
956 if (r < 0)
957 return r;
958 if (r == 0)
959 return 1; /* Will call us back */
960
961 r = sd_bus_message_read(msg, "sssssb", &index_url, &remote, &tag, &local, &verify, &force);
962 if (r < 0)
963 return r;
964
965 if (isempty(index_url))
966 index_url = DEFAULT_DKR_INDEX_URL;
967 if (!index_url)
968 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Index URL must be specified.");
969 if (!http_url_is_valid(index_url))
970 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Index URL %s is invalid", index_url);
971
972 if (!dkr_name_is_valid(remote))
973 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Remote name %s is not valid", remote);
974
975 if (isempty(tag))
976 tag = "latest";
977 else if (!dkr_tag_is_valid(tag))
978 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Tag %s is not valid", tag);
979
980 if (isempty(local))
981 local = NULL;
982 else if (!machine_name_is_valid(local))
983 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Local name %s is invalid", local);
984
985 if (isempty(verify))
986 v = IMPORT_VERIFY_SIGNATURE;
987 else
988 v = import_verify_from_string(verify);
989 if (v < 0)
990 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown verification mode %s", verify);
991
992 if (v != IMPORT_VERIFY_NO)
993 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "DKR does not support verification.");
994
995 r = setup_machine_directory((uint64_t) -1, error);
996 if (r < 0)
997 return r;
998
999 if (manager_find(m, TRANSFER_PULL_DKR, index_url, remote))
1000 return sd_bus_error_setf(error, BUS_ERROR_TRANSFER_IN_PROGRESS, "Transfer for %s already in progress.", remote);
1001
1002 r = transfer_new(m, &t);
1003 if (r < 0)
1004 return r;
1005
1006 t->type = TRANSFER_PULL_DKR;
1007 t->verify = v;
1008 t->force_local = force;
1009
1010 t->dkr_index_url = strdup(index_url);
1011 if (!t->dkr_index_url)
1012 return -ENOMEM;
1013
1014 t->remote = strjoin(remote, ":", tag, NULL);
1015 if (!t->remote)
1016 return -ENOMEM;
1017
1018 if (local) {
1019 t->local = strdup(local);
1020 if (!t->local)
1021 return -ENOMEM;
1022 }
1023
1024 r = transfer_start(t);
1025 if (r < 0)
1026 return r;
1027
1028 object = t->object_path;
1029 id = t->id;
1030 t = NULL;
1031
1032 return sd_bus_reply_method_return(msg, "uo", id, object);
1033 }
1034
1035 static int method_list_transfers(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
1036 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1037 Manager *m = userdata;
1038 Transfer *t;
1039 Iterator i;
1040 int r;
1041
1042 assert(msg);
1043 assert(m);
1044
1045 r = sd_bus_message_new_method_return(msg, &reply);
1046 if (r < 0)
1047 return r;
1048
1049 r = sd_bus_message_open_container(reply, 'a', "(usssdo)");
1050 if (r < 0)
1051 return r;
1052
1053 HASHMAP_FOREACH(t, m->transfers, i) {
1054
1055 r = sd_bus_message_append(
1056 reply,
1057 "(usssdo)",
1058 t->id,
1059 transfer_type_to_string(t->type),
1060 t->remote,
1061 t->local,
1062 (double) t->progress_percent / 100.0,
1063 t->object_path);
1064 if (r < 0)
1065 return r;
1066 }
1067
1068 r = sd_bus_message_close_container(reply);
1069 if (r < 0)
1070 return r;
1071
1072 return sd_bus_send(NULL, reply, NULL);
1073 }
1074
1075 static int method_cancel(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
1076 Transfer *t = userdata;
1077 int r;
1078
1079 assert(msg);
1080 assert(t);
1081
1082 r = bus_verify_polkit_async(
1083 msg,
1084 CAP_SYS_ADMIN,
1085 "org.freedesktop.import1.pull",
1086 NULL,
1087 false,
1088 UID_INVALID,
1089 &t->manager->polkit_registry,
1090 error);
1091 if (r < 0)
1092 return r;
1093 if (r == 0)
1094 return 1; /* Will call us back */
1095
1096 r = transfer_cancel(t);
1097 if (r < 0)
1098 return r;
1099
1100 return sd_bus_reply_method_return(msg, NULL);
1101 }
1102
1103 static int method_cancel_transfer(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
1104 Manager *m = userdata;
1105 Transfer *t;
1106 uint32_t id;
1107 int r;
1108
1109 assert(msg);
1110 assert(m);
1111
1112 r = bus_verify_polkit_async(
1113 msg,
1114 CAP_SYS_ADMIN,
1115 "org.freedesktop.import1.pull",
1116 NULL,
1117 false,
1118 UID_INVALID,
1119 &m->polkit_registry,
1120 error);
1121 if (r < 0)
1122 return r;
1123 if (r == 0)
1124 return 1; /* Will call us back */
1125
1126 r = sd_bus_message_read(msg, "u", &id);
1127 if (r < 0)
1128 return r;
1129 if (id <= 0)
1130 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid transfer id");
1131
1132 t = hashmap_get(m->transfers, UINT32_TO_PTR(id));
1133 if (!t)
1134 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_TRANSFER, "No transfer by id %" PRIu32, id);
1135
1136 r = transfer_cancel(t);
1137 if (r < 0)
1138 return r;
1139
1140 return sd_bus_reply_method_return(msg, NULL);
1141 }
1142
1143 static int property_get_progress(
1144 sd_bus *bus,
1145 const char *path,
1146 const char *interface,
1147 const char *property,
1148 sd_bus_message *reply,
1149 void *userdata,
1150 sd_bus_error *error) {
1151
1152 Transfer *t = userdata;
1153
1154 assert(bus);
1155 assert(reply);
1156 assert(t);
1157
1158 return sd_bus_message_append(reply, "d", (double) t->progress_percent / 100.0);
1159 }
1160
1161 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, transfer_type, TransferType);
1162 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_verify, import_verify, ImportVerify);
1163
1164 static const sd_bus_vtable transfer_vtable[] = {
1165 SD_BUS_VTABLE_START(0),
1166 SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Transfer, id), SD_BUS_VTABLE_PROPERTY_CONST),
1167 SD_BUS_PROPERTY("Local", "s", NULL, offsetof(Transfer, local), SD_BUS_VTABLE_PROPERTY_CONST),
1168 SD_BUS_PROPERTY("Remote", "s", NULL, offsetof(Transfer, remote), SD_BUS_VTABLE_PROPERTY_CONST),
1169 SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Transfer, type), SD_BUS_VTABLE_PROPERTY_CONST),
1170 SD_BUS_PROPERTY("Verify", "s", property_get_verify, offsetof(Transfer, verify), SD_BUS_VTABLE_PROPERTY_CONST),
1171 SD_BUS_PROPERTY("Progress", "d", property_get_progress, 0, 0),
1172 SD_BUS_METHOD("Cancel", NULL, NULL, method_cancel, SD_BUS_VTABLE_UNPRIVILEGED),
1173 SD_BUS_SIGNAL("LogMessage", "us", 0),
1174 SD_BUS_VTABLE_END,
1175 };
1176
1177 static const sd_bus_vtable manager_vtable[] = {
1178 SD_BUS_VTABLE_START(0),
1179 SD_BUS_METHOD("ImportTar", "hsbb", "uo", method_import_tar_or_raw, SD_BUS_VTABLE_UNPRIVILEGED),
1180 SD_BUS_METHOD("ImportRaw", "hsbb", "uo", method_import_tar_or_raw, SD_BUS_VTABLE_UNPRIVILEGED),
1181 SD_BUS_METHOD("ExportTar", "shs", "uo", method_export_tar_or_raw, SD_BUS_VTABLE_UNPRIVILEGED),
1182 SD_BUS_METHOD("ExportRaw", "shs", "uo", method_export_tar_or_raw, SD_BUS_VTABLE_UNPRIVILEGED),
1183 SD_BUS_METHOD("PullTar", "sssb", "uo", method_pull_tar_or_raw, SD_BUS_VTABLE_UNPRIVILEGED),
1184 SD_BUS_METHOD("PullRaw", "sssb", "uo", method_pull_tar_or_raw, SD_BUS_VTABLE_UNPRIVILEGED),
1185 SD_BUS_METHOD("PullDkr", "sssssb", "uo", method_pull_dkr, SD_BUS_VTABLE_UNPRIVILEGED),
1186 SD_BUS_METHOD("ListTransfers", NULL, "a(usssdo)", method_list_transfers, SD_BUS_VTABLE_UNPRIVILEGED),
1187 SD_BUS_METHOD("CancelTransfer", "u", NULL, method_cancel_transfer, SD_BUS_VTABLE_UNPRIVILEGED),
1188 SD_BUS_SIGNAL("TransferNew", "uo", 0),
1189 SD_BUS_SIGNAL("TransferRemoved", "uos", 0),
1190 SD_BUS_VTABLE_END,
1191 };
1192
1193 static int transfer_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
1194 Manager *m = userdata;
1195 Transfer *t;
1196 const char *p;
1197 uint32_t id;
1198 int r;
1199
1200 assert(bus);
1201 assert(path);
1202 assert(interface);
1203 assert(found);
1204 assert(m);
1205
1206 p = startswith(path, "/org/freedesktop/import1/transfer/_");
1207 if (!p)
1208 return 0;
1209
1210 r = safe_atou32(p, &id);
1211 if (r < 0 || id == 0)
1212 return 0;
1213
1214 t = hashmap_get(m->transfers, UINT32_TO_PTR(id));
1215 if (!t)
1216 return 0;
1217
1218 *found = t;
1219 return 1;
1220 }
1221
1222 static int transfer_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
1223 _cleanup_strv_free_ char **l = NULL;
1224 Manager *m = userdata;
1225 Transfer *t;
1226 unsigned k = 0;
1227 Iterator i;
1228
1229 l = new0(char*, hashmap_size(m->transfers) + 1);
1230 if (!l)
1231 return -ENOMEM;
1232
1233 HASHMAP_FOREACH(t, m->transfers, i) {
1234
1235 l[k] = strdup(t->object_path);
1236 if (!l[k])
1237 return -ENOMEM;
1238
1239 k++;
1240 }
1241
1242 *nodes = l;
1243 l = NULL;
1244
1245 return 1;
1246 }
1247
1248 static int manager_add_bus_objects(Manager *m) {
1249 int r;
1250
1251 assert(m);
1252
1253 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/import1", "org.freedesktop.import1.Manager", manager_vtable, m);
1254 if (r < 0)
1255 return log_error_errno(r, "Failed to register object: %m");
1256
1257 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/import1/transfer", "org.freedesktop.import1.Transfer", transfer_vtable, transfer_object_find, m);
1258 if (r < 0)
1259 return log_error_errno(r, "Failed to register object: %m");
1260
1261 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/import1/transfer", transfer_node_enumerator, m);
1262 if (r < 0)
1263 return log_error_errno(r, "Failed to add transfer enumerator: %m");
1264
1265 r = sd_bus_request_name(m->bus, "org.freedesktop.import1", 0);
1266 if (r < 0)
1267 return log_error_errno(r, "Failed to register name: %m");
1268
1269 r = sd_bus_attach_event(m->bus, m->event, 0);
1270 if (r < 0)
1271 return log_error_errno(r, "Failed to attach bus to event loop: %m");
1272
1273 return 0;
1274 }
1275
1276 static bool manager_check_idle(void *userdata) {
1277 Manager *m = userdata;
1278
1279 return hashmap_isempty(m->transfers);
1280 }
1281
1282 static int manager_run(Manager *m) {
1283 assert(m);
1284
1285 return bus_event_loop_with_idle(
1286 m->event,
1287 m->bus,
1288 "org.freedesktop.import1",
1289 DEFAULT_EXIT_USEC,
1290 manager_check_idle,
1291 m);
1292 }
1293
1294 int main(int argc, char *argv[]) {
1295 _cleanup_(manager_unrefp) Manager *m = NULL;
1296 int r;
1297
1298 log_set_target(LOG_TARGET_AUTO);
1299 log_parse_environment();
1300 log_open();
1301
1302 umask(0022);
1303
1304 if (argc != 1) {
1305 log_error("This program takes no arguments.");
1306 r = -EINVAL;
1307 goto finish;
1308 }
1309
1310 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);
1311
1312 r = manager_new(&m);
1313 if (r < 0) {
1314 log_error_errno(r, "Failed to allocate manager object: %m");
1315 goto finish;
1316 }
1317
1318 r = manager_add_bus_objects(m);
1319 if (r < 0)
1320 goto finish;
1321
1322 r = manager_run(m);
1323 if (r < 0) {
1324 log_error_errno(r, "Failed to run event loop: %m");
1325 goto finish;
1326 }
1327
1328 finish:
1329 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1330 }