]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/importd.c
Merge pull request #1024 from poettering/sd-bus-explicit
[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 false,
739 UID_INVALID,
740 &m->polkit_registry,
741 error);
742 if (r < 0)
743 return r;
744 if (r == 0)
745 return 1; /* Will call us back */
746
747 r = sd_bus_message_read(msg, "hsbb", &fd, &local, &force, &read_only);
748 if (r < 0)
749 return r;
750
751 if (!machine_name_is_valid(local))
752 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Local name %s is invalid", local);
753
754 r = setup_machine_directory((uint64_t) -1, error);
755 if (r < 0)
756 return r;
757
758 type = streq_ptr(sd_bus_message_get_member(msg), "ImportTar") ? TRANSFER_IMPORT_TAR : TRANSFER_IMPORT_RAW;
759
760 r = transfer_new(m, &t);
761 if (r < 0)
762 return r;
763
764 t->type = type;
765 t->force_local = force;
766 t->read_only = read_only;
767
768 t->local = strdup(local);
769 if (!t->local)
770 return -ENOMEM;
771
772 t->stdin_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
773 if (t->stdin_fd < 0)
774 return -errno;
775
776 r = transfer_start(t);
777 if (r < 0)
778 return r;
779
780 object = t->object_path;
781 id = t->id;
782 t = NULL;
783
784 return sd_bus_reply_method_return(msg, "uo", id, object);
785 }
786
787 static int method_export_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
788 _cleanup_(transfer_unrefp) Transfer *t = NULL;
789 int fd, r;
790 const char *local, *object, *format;
791 Manager *m = userdata;
792 TransferType type;
793 uint32_t id;
794
795 assert(msg);
796 assert(m);
797
798 r = bus_verify_polkit_async(
799 msg,
800 CAP_SYS_ADMIN,
801 "org.freedesktop.import1.export",
802 false,
803 UID_INVALID,
804 &m->polkit_registry,
805 error);
806 if (r < 0)
807 return r;
808 if (r == 0)
809 return 1; /* Will call us back */
810
811 r = sd_bus_message_read(msg, "shs", &local, &fd, &format);
812 if (r < 0)
813 return r;
814
815 if (!machine_name_is_valid(local))
816 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Local name %s is invalid", local);
817
818 type = streq_ptr(sd_bus_message_get_member(msg), "ExportTar") ? TRANSFER_EXPORT_TAR : TRANSFER_EXPORT_RAW;
819
820 r = transfer_new(m, &t);
821 if (r < 0)
822 return r;
823
824 t->type = type;
825
826 if (!isempty(format)) {
827 t->format = strdup(format);
828 if (!t->format)
829 return -ENOMEM;
830 }
831
832 t->local = strdup(local);
833 if (!t->local)
834 return -ENOMEM;
835
836 t->stdout_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
837 if (t->stdout_fd < 0)
838 return -errno;
839
840 r = transfer_start(t);
841 if (r < 0)
842 return r;
843
844 object = t->object_path;
845 id = t->id;
846 t = NULL;
847
848 return sd_bus_reply_method_return(msg, "uo", id, object);
849 }
850
851 static int method_pull_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
852 _cleanup_(transfer_unrefp) Transfer *t = NULL;
853 const char *remote, *local, *verify, *object;
854 Manager *m = userdata;
855 ImportVerify v;
856 TransferType type;
857 int force, r;
858 uint32_t id;
859
860 assert(msg);
861 assert(m);
862
863 r = bus_verify_polkit_async(
864 msg,
865 CAP_SYS_ADMIN,
866 "org.freedesktop.import1.pull",
867 false,
868 UID_INVALID,
869 &m->polkit_registry,
870 error);
871 if (r < 0)
872 return r;
873 if (r == 0)
874 return 1; /* Will call us back */
875
876 r = sd_bus_message_read(msg, "sssb", &remote, &local, &verify, &force);
877 if (r < 0)
878 return r;
879
880 if (!http_url_is_valid(remote))
881 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "URL %s is invalid", remote);
882
883 if (isempty(local))
884 local = NULL;
885 else if (!machine_name_is_valid(local))
886 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Local name %s is invalid", local);
887
888 if (isempty(verify))
889 v = IMPORT_VERIFY_SIGNATURE;
890 else
891 v = import_verify_from_string(verify);
892 if (v < 0)
893 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown verification mode %s", verify);
894
895 r = setup_machine_directory((uint64_t) -1, error);
896 if (r < 0)
897 return r;
898
899 type = streq_ptr(sd_bus_message_get_member(msg), "PullTar") ? TRANSFER_PULL_TAR : TRANSFER_PULL_RAW;
900
901 if (manager_find(m, type, NULL, remote))
902 return sd_bus_error_setf(error, BUS_ERROR_TRANSFER_IN_PROGRESS, "Transfer for %s already in progress.", remote);
903
904 r = transfer_new(m, &t);
905 if (r < 0)
906 return r;
907
908 t->type = type;
909 t->verify = v;
910 t->force_local = force;
911
912 t->remote = strdup(remote);
913 if (!t->remote)
914 return -ENOMEM;
915
916 if (local) {
917 t->local = strdup(local);
918 if (!t->local)
919 return -ENOMEM;
920 }
921
922 r = transfer_start(t);
923 if (r < 0)
924 return r;
925
926 object = t->object_path;
927 id = t->id;
928 t = NULL;
929
930 return sd_bus_reply_method_return(msg, "uo", id, object);
931 }
932
933 static int method_pull_dkr(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
934 _cleanup_(transfer_unrefp) Transfer *t = NULL;
935 const char *index_url, *remote, *tag, *local, *verify, *object;
936 Manager *m = userdata;
937 ImportVerify v;
938 int force, r;
939 uint32_t id;
940
941 assert(msg);
942 assert(m);
943
944 r = bus_verify_polkit_async(
945 msg,
946 CAP_SYS_ADMIN,
947 "org.freedesktop.import1.pull",
948 false,
949 UID_INVALID,
950 &m->polkit_registry,
951 error);
952 if (r < 0)
953 return r;
954 if (r == 0)
955 return 1; /* Will call us back */
956
957 r = sd_bus_message_read(msg, "sssssb", &index_url, &remote, &tag, &local, &verify, &force);
958 if (r < 0)
959 return r;
960
961 if (isempty(index_url))
962 index_url = DEFAULT_DKR_INDEX_URL;
963 if (!index_url)
964 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Index URL must be specified.");
965 if (!http_url_is_valid(index_url))
966 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Index URL %s is invalid", index_url);
967
968 if (!dkr_name_is_valid(remote))
969 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Remote name %s is not valid", remote);
970
971 if (isempty(tag))
972 tag = "latest";
973 else if (!dkr_tag_is_valid(tag))
974 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Tag %s is not valid", tag);
975
976 if (isempty(local))
977 local = NULL;
978 else if (!machine_name_is_valid(local))
979 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Local name %s is invalid", local);
980
981 if (isempty(verify))
982 v = IMPORT_VERIFY_SIGNATURE;
983 else
984 v = import_verify_from_string(verify);
985 if (v < 0)
986 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown verification mode %s", verify);
987
988 if (v != IMPORT_VERIFY_NO)
989 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "DKR does not support verification.");
990
991 r = setup_machine_directory((uint64_t) -1, error);
992 if (r < 0)
993 return r;
994
995 if (manager_find(m, TRANSFER_PULL_DKR, index_url, remote))
996 return sd_bus_error_setf(error, BUS_ERROR_TRANSFER_IN_PROGRESS, "Transfer for %s already in progress.", remote);
997
998 r = transfer_new(m, &t);
999 if (r < 0)
1000 return r;
1001
1002 t->type = TRANSFER_PULL_DKR;
1003 t->verify = v;
1004 t->force_local = force;
1005
1006 t->dkr_index_url = strdup(index_url);
1007 if (!t->dkr_index_url)
1008 return -ENOMEM;
1009
1010 t->remote = strjoin(remote, ":", tag, NULL);
1011 if (!t->remote)
1012 return -ENOMEM;
1013
1014 if (local) {
1015 t->local = strdup(local);
1016 if (!t->local)
1017 return -ENOMEM;
1018 }
1019
1020 r = transfer_start(t);
1021 if (r < 0)
1022 return r;
1023
1024 object = t->object_path;
1025 id = t->id;
1026 t = NULL;
1027
1028 return sd_bus_reply_method_return(msg, "uo", id, object);
1029 }
1030
1031 static int method_list_transfers(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
1032 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1033 Manager *m = userdata;
1034 Transfer *t;
1035 Iterator i;
1036 int r;
1037
1038 assert(msg);
1039 assert(m);
1040
1041 r = sd_bus_message_new_method_return(msg, &reply);
1042 if (r < 0)
1043 return r;
1044
1045 r = sd_bus_message_open_container(reply, 'a', "(usssdo)");
1046 if (r < 0)
1047 return r;
1048
1049 HASHMAP_FOREACH(t, m->transfers, i) {
1050
1051 r = sd_bus_message_append(
1052 reply,
1053 "(usssdo)",
1054 t->id,
1055 transfer_type_to_string(t->type),
1056 t->remote,
1057 t->local,
1058 (double) t->progress_percent / 100.0,
1059 t->object_path);
1060 if (r < 0)
1061 return r;
1062 }
1063
1064 r = sd_bus_message_close_container(reply);
1065 if (r < 0)
1066 return r;
1067
1068 return sd_bus_send(NULL, reply, NULL);
1069 }
1070
1071 static int method_cancel(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
1072 Transfer *t = userdata;
1073 int r;
1074
1075 assert(msg);
1076 assert(t);
1077
1078 r = bus_verify_polkit_async(
1079 msg,
1080 CAP_SYS_ADMIN,
1081 "org.freedesktop.import1.pull",
1082 false,
1083 UID_INVALID,
1084 &t->manager->polkit_registry,
1085 error);
1086 if (r < 0)
1087 return r;
1088 if (r == 0)
1089 return 1; /* Will call us back */
1090
1091 r = transfer_cancel(t);
1092 if (r < 0)
1093 return r;
1094
1095 return sd_bus_reply_method_return(msg, NULL);
1096 }
1097
1098 static int method_cancel_transfer(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
1099 Manager *m = userdata;
1100 Transfer *t;
1101 uint32_t id;
1102 int r;
1103
1104 assert(msg);
1105 assert(m);
1106
1107 r = bus_verify_polkit_async(
1108 msg,
1109 CAP_SYS_ADMIN,
1110 "org.freedesktop.import1.pull",
1111 false,
1112 UID_INVALID,
1113 &m->polkit_registry,
1114 error);
1115 if (r < 0)
1116 return r;
1117 if (r == 0)
1118 return 1; /* Will call us back */
1119
1120 r = sd_bus_message_read(msg, "u", &id);
1121 if (r < 0)
1122 return r;
1123 if (id <= 0)
1124 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid transfer id");
1125
1126 t = hashmap_get(m->transfers, UINT32_TO_PTR(id));
1127 if (!t)
1128 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_TRANSFER, "No transfer by id %" PRIu32, id);
1129
1130 r = transfer_cancel(t);
1131 if (r < 0)
1132 return r;
1133
1134 return sd_bus_reply_method_return(msg, NULL);
1135 }
1136
1137 static int property_get_progress(
1138 sd_bus *bus,
1139 const char *path,
1140 const char *interface,
1141 const char *property,
1142 sd_bus_message *reply,
1143 void *userdata,
1144 sd_bus_error *error) {
1145
1146 Transfer *t = userdata;
1147
1148 assert(bus);
1149 assert(reply);
1150 assert(t);
1151
1152 return sd_bus_message_append(reply, "d", (double) t->progress_percent / 100.0);
1153 }
1154
1155 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, transfer_type, TransferType);
1156 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_verify, import_verify, ImportVerify);
1157
1158 static const sd_bus_vtable transfer_vtable[] = {
1159 SD_BUS_VTABLE_START(0),
1160 SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Transfer, id), SD_BUS_VTABLE_PROPERTY_CONST),
1161 SD_BUS_PROPERTY("Local", "s", NULL, offsetof(Transfer, local), SD_BUS_VTABLE_PROPERTY_CONST),
1162 SD_BUS_PROPERTY("Remote", "s", NULL, offsetof(Transfer, remote), SD_BUS_VTABLE_PROPERTY_CONST),
1163 SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Transfer, type), SD_BUS_VTABLE_PROPERTY_CONST),
1164 SD_BUS_PROPERTY("Verify", "s", property_get_verify, offsetof(Transfer, verify), SD_BUS_VTABLE_PROPERTY_CONST),
1165 SD_BUS_PROPERTY("Progress", "d", property_get_progress, 0, 0),
1166 SD_BUS_METHOD("Cancel", NULL, NULL, method_cancel, SD_BUS_VTABLE_UNPRIVILEGED),
1167 SD_BUS_SIGNAL("LogMessage", "us", 0),
1168 SD_BUS_VTABLE_END,
1169 };
1170
1171 static const sd_bus_vtable manager_vtable[] = {
1172 SD_BUS_VTABLE_START(0),
1173 SD_BUS_METHOD("ImportTar", "hsbb", "uo", method_import_tar_or_raw, SD_BUS_VTABLE_UNPRIVILEGED),
1174 SD_BUS_METHOD("ImportRaw", "hsbb", "uo", method_import_tar_or_raw, SD_BUS_VTABLE_UNPRIVILEGED),
1175 SD_BUS_METHOD("ExportTar", "shs", "uo", method_export_tar_or_raw, SD_BUS_VTABLE_UNPRIVILEGED),
1176 SD_BUS_METHOD("ExportRaw", "shs", "uo", method_export_tar_or_raw, SD_BUS_VTABLE_UNPRIVILEGED),
1177 SD_BUS_METHOD("PullTar", "sssb", "uo", method_pull_tar_or_raw, SD_BUS_VTABLE_UNPRIVILEGED),
1178 SD_BUS_METHOD("PullRaw", "sssb", "uo", method_pull_tar_or_raw, SD_BUS_VTABLE_UNPRIVILEGED),
1179 SD_BUS_METHOD("PullDkr", "sssssb", "uo", method_pull_dkr, SD_BUS_VTABLE_UNPRIVILEGED),
1180 SD_BUS_METHOD("ListTransfers", NULL, "a(usssdo)", method_list_transfers, SD_BUS_VTABLE_UNPRIVILEGED),
1181 SD_BUS_METHOD("CancelTransfer", "u", NULL, method_cancel_transfer, SD_BUS_VTABLE_UNPRIVILEGED),
1182 SD_BUS_SIGNAL("TransferNew", "uo", 0),
1183 SD_BUS_SIGNAL("TransferRemoved", "uos", 0),
1184 SD_BUS_VTABLE_END,
1185 };
1186
1187 static int transfer_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
1188 Manager *m = userdata;
1189 Transfer *t;
1190 const char *p;
1191 uint32_t id;
1192 int r;
1193
1194 assert(bus);
1195 assert(path);
1196 assert(interface);
1197 assert(found);
1198 assert(m);
1199
1200 p = startswith(path, "/org/freedesktop/import1/transfer/_");
1201 if (!p)
1202 return 0;
1203
1204 r = safe_atou32(p, &id);
1205 if (r < 0 || id == 0)
1206 return 0;
1207
1208 t = hashmap_get(m->transfers, UINT32_TO_PTR(id));
1209 if (!t)
1210 return 0;
1211
1212 *found = t;
1213 return 1;
1214 }
1215
1216 static int transfer_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
1217 _cleanup_strv_free_ char **l = NULL;
1218 Manager *m = userdata;
1219 Transfer *t;
1220 unsigned k = 0;
1221 Iterator i;
1222
1223 l = new0(char*, hashmap_size(m->transfers) + 1);
1224 if (!l)
1225 return -ENOMEM;
1226
1227 HASHMAP_FOREACH(t, m->transfers, i) {
1228
1229 l[k] = strdup(t->object_path);
1230 if (!l[k])
1231 return -ENOMEM;
1232
1233 k++;
1234 }
1235
1236 *nodes = l;
1237 l = NULL;
1238
1239 return 1;
1240 }
1241
1242 static int manager_add_bus_objects(Manager *m) {
1243 int r;
1244
1245 assert(m);
1246
1247 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/import1", "org.freedesktop.import1.Manager", manager_vtable, m);
1248 if (r < 0)
1249 return log_error_errno(r, "Failed to register object: %m");
1250
1251 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/import1/transfer", "org.freedesktop.import1.Transfer", transfer_vtable, transfer_object_find, m);
1252 if (r < 0)
1253 return log_error_errno(r, "Failed to register object: %m");
1254
1255 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/import1/transfer", transfer_node_enumerator, m);
1256 if (r < 0)
1257 return log_error_errno(r, "Failed to add transfer enumerator: %m");
1258
1259 r = sd_bus_request_name(m->bus, "org.freedesktop.import1", 0);
1260 if (r < 0)
1261 return log_error_errno(r, "Failed to register name: %m");
1262
1263 r = sd_bus_attach_event(m->bus, m->event, 0);
1264 if (r < 0)
1265 return log_error_errno(r, "Failed to attach bus to event loop: %m");
1266
1267 return 0;
1268 }
1269
1270 static bool manager_check_idle(void *userdata) {
1271 Manager *m = userdata;
1272
1273 return hashmap_isempty(m->transfers);
1274 }
1275
1276 static int manager_run(Manager *m) {
1277 assert(m);
1278
1279 return bus_event_loop_with_idle(
1280 m->event,
1281 m->bus,
1282 "org.freedesktop.import1",
1283 DEFAULT_EXIT_USEC,
1284 manager_check_idle,
1285 m);
1286 }
1287
1288 int main(int argc, char *argv[]) {
1289 _cleanup_(manager_unrefp) Manager *m = NULL;
1290 int r;
1291
1292 log_set_target(LOG_TARGET_AUTO);
1293 log_parse_environment();
1294 log_open();
1295
1296 umask(0022);
1297
1298 if (argc != 1) {
1299 log_error("This program takes no arguments.");
1300 r = -EINVAL;
1301 goto finish;
1302 }
1303
1304 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD, -1) >= 0);
1305
1306 r = manager_new(&m);
1307 if (r < 0) {
1308 log_error_errno(r, "Failed to allocate manager object: %m");
1309 goto finish;
1310 }
1311
1312 r = manager_add_bus_objects(m);
1313 if (r < 0)
1314 goto finish;
1315
1316 r = manager_run(m);
1317 if (r < 0) {
1318 log_error_errno(r, "Failed to run event loop: %m");
1319 goto finish;
1320 }
1321
1322 finish:
1323 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1324 }