chiark / gitweb /
bus: add APIs for adding iovecs to messages as string or arrays
[elogind.git] / src / libsystemd-bus / bus-message.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 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 <errno.h>
23 #include <fcntl.h>
24 #include <sys/mman.h>
25
26 #include "util.h"
27 #include "utf8.h"
28 #include "strv.h"
29 #include "time-util.h"
30 #include "cgroup-util.h"
31
32 #include "sd-bus.h"
33 #include "bus-message.h"
34 #include "bus-internal.h"
35 #include "bus-type.h"
36 #include "bus-signature.h"
37
38 static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
39
40 static void *adjust_pointer(const void *p, void *old_base, size_t sz, void *new_base) {
41
42         if (p == NULL)
43                 return NULL;
44
45         if (old_base == new_base)
46                 return (void*) p;
47
48         if ((uint8_t*) p < (uint8_t*) old_base)
49                 return (void*) p;
50
51         if ((uint8_t*) p >= (uint8_t*) old_base + sz)
52                 return (void*) p;
53
54         return (uint8_t*) new_base + ((uint8_t*) p - (uint8_t*) old_base);
55 }
56
57 static void message_free_part(sd_bus_message *m, struct bus_body_part *part) {
58         assert(m);
59         assert(part);
60
61         if (part->memfd >= 0) {
62                 /* If we can reuse the memfd, try that. For that it
63                  * can't be sealed yet. */
64
65                 if (!part->sealed)
66                         bus_kernel_push_memfd(m->bus, part->memfd, part->data, part->mapped);
67                 else {
68                         if (part->mapped > 0)
69                                 assert_se(munmap(part->data, part->mapped) == 0);
70
71                         close_nointr_nofail(part->memfd);
72                 }
73
74         } else if (part->munmap_this)
75                 munmap(part->data, part->mapped);
76         else if (part->free_this)
77                 free(part->data);
78
79         if (part != &m->body)
80                 free(part);
81 }
82
83 static void message_reset_parts(sd_bus_message *m) {
84         struct bus_body_part *part;
85
86         assert(m);
87
88         part = &m->body;
89         while (m->n_body_parts > 0) {
90                 struct bus_body_part *next = part->next;
91                 message_free_part(m, part);
92                 part = next;
93                 m->n_body_parts--;
94         }
95
96         m->body_end = NULL;
97
98         m->cached_rindex_part = NULL;
99         m->cached_rindex_part_begin = 0;
100 }
101
102 static void message_reset_containers(sd_bus_message *m) {
103         unsigned i;
104
105         assert(m);
106
107         for (i = 0; i < m->n_containers; i++)
108                 free(m->containers[i].signature);
109
110         free(m->containers);
111         m->containers = NULL;
112
113         m->n_containers = 0;
114         m->root_container.index = 0;
115 }
116
117 static void message_free(sd_bus_message *m) {
118         assert(m);
119
120         if (m->free_header)
121                 free(m->header);
122
123         message_reset_parts(m);
124
125         if (m->free_kdbus)
126                 free(m->kdbus);
127
128         if (m->release_kdbus) {
129                 uint64_t off;
130
131                 off = (uint8_t *)m->kdbus - (uint8_t *)m->bus->kdbus_buffer;
132                 ioctl(m->bus->input_fd, KDBUS_CMD_MSG_RELEASE, &off);
133         }
134
135         if (m->bus)
136                 sd_bus_unref(m->bus);
137
138         if (m->free_fds) {
139                 close_many(m->fds, m->n_fds);
140                 free(m->fds);
141         }
142
143         if (m->iovec != m->iovec_fixed)
144                 free(m->iovec);
145
146         free(m->cmdline_array);
147
148         message_reset_containers(m);
149         free(m->root_container.signature);
150
151         free(m->peeked_signature);
152
153         free(m->unit);
154         free(m->user_unit);
155         free(m->session);
156         free(m);
157 }
158
159 static void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz) {
160         void *op, *np;
161         size_t old_size, new_size, start;
162
163         assert(m);
164
165         if (m->poisoned)
166                 return NULL;
167
168         old_size = sizeof(struct bus_header) + m->header->fields_size;
169         start = ALIGN_TO(old_size, align);
170         new_size = start + sz;
171
172         if (old_size == new_size)
173                 return (uint8_t*) m->header + old_size;
174
175         if (new_size > (size_t) ((uint32_t) -1))
176                 goto poison;
177
178         if (m->free_header) {
179                 np = realloc(m->header, ALIGN8(new_size));
180                 if (!np)
181                         goto poison;
182         } else {
183                 /* Initially, the header is allocated as part of of
184                  * the sd_bus_message itself, let's replace it by
185                  * dynamic data */
186
187                 np = malloc(ALIGN8(new_size));
188                 if (!np)
189                         goto poison;
190
191                 memcpy(np, m->header, sizeof(struct bus_header));
192         }
193
194         /* Zero out padding */
195         if (start > old_size)
196                 memset((uint8_t*) np + old_size, 0, start - old_size);
197
198         op = m->header;
199         m->header = np;
200         m->header->fields_size = new_size - sizeof(struct bus_header);
201
202         /* Adjust quick access pointers */
203         m->path = adjust_pointer(m->path, op, old_size, m->header);
204         m->interface = adjust_pointer(m->interface, op, old_size, m->header);
205         m->member = adjust_pointer(m->member, op, old_size, m->header);
206         m->destination = adjust_pointer(m->destination, op, old_size, m->header);
207         m->sender = adjust_pointer(m->sender, op, old_size, m->header);
208         m->error.name = adjust_pointer(m->error.name, op, old_size, m->header);
209
210         m->free_header = true;
211
212         return (uint8_t*) np + start;
213
214 poison:
215         m->poisoned = true;
216         return NULL;
217 }
218
219 static int message_append_field_string(
220                 sd_bus_message *m,
221                 uint8_t h,
222                 char type,
223                 const char *s,
224                 const char **ret) {
225
226         size_t l;
227         uint8_t *p;
228
229         assert(m);
230
231         l = strlen(s);
232         if (l > (size_t) (uint32_t) -1)
233                 return -EINVAL;
234
235         /* field id byte + signature length + signature 's' + NUL + string length + string + NUL */
236         p = message_extend_fields(m, 8, 4 + 4 + l + 1);
237         if (!p)
238                 return -ENOMEM;
239
240         p[0] = h;
241         p[1] = 1;
242         p[2] = type;
243         p[3] = 0;
244
245         ((uint32_t*) p)[1] = l;
246         memcpy(p + 8, s, l + 1);
247
248         if (ret)
249                 *ret = (char*) p + 8;
250
251         return 0;
252 }
253
254 static int message_append_field_signature(
255                 sd_bus_message *m,
256                 uint8_t h,
257                 const char *s,
258                 const char **ret) {
259
260         size_t l;
261         uint8_t *p;
262
263         assert(m);
264
265         l = strlen(s);
266         if (l > 255)
267                 return -EINVAL;
268
269         /* field id byte + signature length + signature 'g' + NUL + string length + string + NUL */
270         p = message_extend_fields(m, 8, 4 + 1 + l + 1);
271         if (!p)
272                 return -ENOMEM;
273
274         p[0] = h;
275         p[1] = 1;
276         p[2] = SD_BUS_TYPE_SIGNATURE;
277         p[3] = 0;
278         p[4] = l;
279         memcpy(p + 5, s, l + 1);
280
281         if (ret)
282                 *ret = (const char*) p + 5;
283
284         return 0;
285 }
286
287 static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x) {
288         uint8_t *p;
289
290         assert(m);
291
292         /* field id byte + signature length + signature 'u' + NUL + value */
293         p = message_extend_fields(m, 8, 4 + 4);
294         if (!p)
295                 return -ENOMEM;
296
297         p[0] = h;
298         p[1] = 1;
299         p[2] = SD_BUS_TYPE_UINT32;
300         p[3] = 0;
301
302         ((uint32_t*) p)[1] = x;
303
304         return 0;
305 }
306
307 int bus_message_from_header(
308                 void *buffer,
309                 size_t length,
310                 int *fds,
311                 unsigned n_fds,
312                 const struct ucred *ucred,
313                 const char *label,
314                 size_t extra,
315                 sd_bus_message **ret) {
316
317         sd_bus_message *m;
318         struct bus_header *h;
319         size_t a, label_sz;
320
321         assert(buffer || length <= 0);
322         assert(fds || n_fds <= 0);
323         assert(ret);
324
325         if (length < sizeof(struct bus_header))
326                 return -EBADMSG;
327
328         h = buffer;
329         if (h->version != 1)
330                 return -EBADMSG;
331
332         if (h->serial == 0)
333                 return -EBADMSG;
334
335         if (h->type == _SD_BUS_MESSAGE_TYPE_INVALID)
336                 return -EBADMSG;
337
338         if (h->endian != SD_BUS_LITTLE_ENDIAN &&
339             h->endian != SD_BUS_BIG_ENDIAN)
340                 return -EBADMSG;
341
342         a = ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
343
344         if (label) {
345                 label_sz = strlen(label);
346                 a += label_sz + 1;
347         }
348
349         m = malloc0(a);
350         if (!m)
351                 return -ENOMEM;
352
353         m->n_ref = 1;
354         m->sealed = true;
355         m->header = h;
356         m->fds = fds;
357         m->n_fds = n_fds;
358
359         if (ucred) {
360                 m->uid = ucred->uid;
361                 m->pid = ucred->pid;
362                 m->gid = ucred->gid;
363                 m->uid_valid = m->gid_valid = true;
364         }
365
366         if (label) {
367                 m->label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
368                 memcpy(m->label, label, label_sz + 1);
369         }
370
371         *ret = m;
372         return 0;
373 }
374
375 int bus_message_from_malloc(
376                 void *buffer,
377                 size_t length,
378                 int *fds,
379                 unsigned n_fds,
380                 const struct ucred *ucred,
381                 const char *label,
382                 sd_bus_message **ret) {
383
384         sd_bus_message *m;
385         int r;
386
387         r = bus_message_from_header(buffer, length, fds, n_fds, ucred, label, 0, &m);
388         if (r < 0)
389                 return r;
390
391         if (length != BUS_MESSAGE_SIZE(m)) {
392                 r = -EBADMSG;
393                 goto fail;
394         }
395
396         m->n_body_parts = 1;
397         m->body.data = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
398         m->body.size = length - sizeof(struct bus_header) - ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
399         m->body.sealed = true;
400         m->body.memfd = -1;
401
402         m->n_iovec = 1;
403         m->iovec = m->iovec_fixed;
404         m->iovec[0].iov_base = buffer;
405         m->iovec[0].iov_len = length;
406
407         r = bus_message_parse_fields(m);
408         if (r < 0)
409                 goto fail;
410
411         /* We take possession of the memory and fds now */
412         m->free_header = true;
413         m->free_fds = true;
414
415         *ret = m;
416         return 0;
417
418 fail:
419         message_free(m);
420         return r;
421 }
422
423 static sd_bus_message *message_new(sd_bus *bus, uint8_t type) {
424         sd_bus_message *m;
425
426         m = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(struct bus_header));
427         if (!m)
428                 return NULL;
429
430         m->n_ref = 1;
431         m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message)));
432         m->header->endian = SD_BUS_NATIVE_ENDIAN;
433         m->header->type = type;
434         m->header->version = bus ? bus->message_version : 1;
435         m->allow_fds = !bus || bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING);
436
437         if (bus)
438                 m->bus = sd_bus_ref(bus);
439
440         return m;
441 }
442
443 _public_ int sd_bus_message_new_signal(
444                 sd_bus *bus,
445                 const char *path,
446                 const char *interface,
447                 const char *member,
448                 sd_bus_message **m) {
449
450         sd_bus_message *t;
451         int r;
452
453         assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN);
454         assert_return(object_path_is_valid(path), -EINVAL);
455         assert_return(interface_name_is_valid(interface), -EINVAL);
456         assert_return(member_name_is_valid(member), -EINVAL);
457         assert_return(m, -EINVAL);
458
459         t = message_new(bus, SD_BUS_MESSAGE_SIGNAL);
460         if (!t)
461                 return -ENOMEM;
462
463         t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
464
465         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
466         if (r < 0)
467                 goto fail;
468         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
469         if (r < 0)
470                 goto fail;
471         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
472         if (r < 0)
473                 goto fail;
474
475         *m = t;
476         return 0;
477
478 fail:
479         sd_bus_message_unref(t);
480         return r;
481 }
482
483 _public_ int sd_bus_message_new_method_call(
484                 sd_bus *bus,
485                 const char *destination,
486                 const char *path,
487                 const char *interface,
488                 const char *member,
489                 sd_bus_message **m) {
490
491         sd_bus_message *t;
492         int r;
493
494         assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN);
495         assert_return(!destination || service_name_is_valid(destination), -EINVAL);
496         assert_return(object_path_is_valid(path), -EINVAL);
497         assert_return(!interface || interface_name_is_valid(interface), -EINVAL);
498         assert_return(member_name_is_valid(member), -EINVAL);
499         assert_return(m, -EINVAL);
500
501         t = message_new(bus, SD_BUS_MESSAGE_METHOD_CALL);
502         if (!t)
503                 return -ENOMEM;
504
505         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
506         if (r < 0)
507                 goto fail;
508         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
509         if (r < 0)
510                 goto fail;
511
512         if (interface) {
513                 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
514                 if (r < 0)
515                         goto fail;
516         }
517
518         if (destination) {
519                 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination);
520                 if (r < 0)
521                         goto fail;
522         }
523
524         *m = t;
525         return 0;
526
527 fail:
528         message_free(t);
529         return r;
530 }
531
532 static int message_new_reply(
533                 sd_bus *bus,
534                 sd_bus_message *call,
535                 uint8_t type,
536                 sd_bus_message **m) {
537
538         sd_bus_message *t;
539         int r;
540
541         assert_return(!bus || bus->state != BUS_UNSET, -ENOTCONN);
542         assert_return(call, -EINVAL);
543         assert_return(call->sealed, -EPERM);
544         assert_return(call->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
545         assert_return(m, -EINVAL);
546
547         t = message_new(bus, type);
548         if (!t)
549                 return -ENOMEM;
550
551         t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
552         t->reply_serial = BUS_MESSAGE_SERIAL(call);
553
554         r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
555         if (r < 0)
556                 goto fail;
557
558         if (call->sender) {
559                 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->destination);
560                 if (r < 0)
561                         goto fail;
562         }
563
564         t->dont_send = !!(call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED);
565
566         *m = t;
567         return 0;
568
569 fail:
570         message_free(t);
571         return r;
572 }
573
574 _public_ int sd_bus_message_new_method_return(
575                 sd_bus *bus,
576                 sd_bus_message *call,
577                 sd_bus_message **m) {
578
579         return message_new_reply(bus, call, SD_BUS_MESSAGE_METHOD_RETURN, m);
580 }
581
582 _public_ int sd_bus_message_new_method_error(
583                 sd_bus *bus,
584                 sd_bus_message *call,
585                 const sd_bus_error *e,
586                 sd_bus_message **m) {
587
588         sd_bus_message *t;
589         int r;
590
591         assert_return(sd_bus_error_is_set(e), -EINVAL);
592         assert_return(m, -EINVAL);
593
594         r = message_new_reply(bus, call, SD_BUS_MESSAGE_METHOD_ERROR, &t);
595         if (r < 0)
596                 return r;
597
598         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
599         if (r < 0)
600                 goto fail;
601
602         if (e->message) {
603                 r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
604                 if (r < 0)
605                         goto fail;
606         }
607
608         *m = t;
609         return 0;
610
611 fail:
612         message_free(t);
613         return r;
614 }
615
616 _public_ int sd_bus_message_new_method_errorf(
617                 sd_bus *bus,
618                 sd_bus_message *call,
619                 sd_bus_message **m,
620                 const char *name,
621                 const char *format,
622                 ...) {
623
624         _cleanup_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
625         va_list ap;
626         int r;
627
628         assert_return(name, -EINVAL);
629         assert_return(m, -EINVAL);
630
631         va_start(ap, format);
632         r = bus_error_setfv(&error, name, format, ap);
633         va_end(ap);
634
635         if (r < 0)
636                 return r;
637
638         return sd_bus_message_new_method_error(bus, call, &error, m);
639 }
640
641 _public_ int sd_bus_message_new_method_errno(
642                 sd_bus *bus,
643                 sd_bus_message *call,
644                 int error,
645                 const sd_bus_error *p,
646                 sd_bus_message **m) {
647
648         _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
649
650         if (sd_bus_error_is_set(p))
651                 return sd_bus_message_new_method_error(bus, call, p, m);
652
653         sd_bus_error_set_errno(&berror, error);
654
655         return sd_bus_message_new_method_error(bus, call, &berror, m);
656 }
657
658 _public_ int sd_bus_message_new_method_errnof(
659                 sd_bus *bus,
660                 sd_bus_message *call,
661                 sd_bus_message **m,
662                 int error,
663                 const char *format,
664                 ...) {
665
666         _cleanup_free_ sd_bus_error berror = SD_BUS_ERROR_NULL;
667         va_list ap;
668         int r;
669
670         va_start(ap, format);
671         r = bus_error_set_errnofv(&berror, error, format, ap);
672         va_end(ap);
673
674         if (r < 0)
675                 return r;
676
677         return sd_bus_message_new_method_error(bus, call, &berror, m);
678 }
679
680 int bus_message_new_synthetic_error(
681                 sd_bus *bus,
682                 uint64_t serial,
683                 const sd_bus_error *e,
684                 sd_bus_message **m) {
685
686         sd_bus_message *t;
687         int r;
688
689         assert(sd_bus_error_is_set(e));
690         assert(m);
691
692         t = message_new(bus, SD_BUS_MESSAGE_METHOD_ERROR);
693         if (!t)
694                 return -ENOMEM;
695
696         t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
697         t->reply_serial = serial;
698
699         r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
700         if (r < 0)
701                 goto fail;
702
703         if (bus && bus->unique_name) {
704                 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, bus->unique_name, &t->destination);
705                 if (r < 0)
706                         goto fail;
707         }
708
709         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
710         if (r < 0)
711                 goto fail;
712
713         if (e->message) {
714                 r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
715                 if (r < 0)
716                         goto fail;
717         }
718
719         *m = t;
720         return 0;
721
722 fail:
723         message_free(t);
724         return r;
725 }
726
727 _public_ sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
728         assert_return(m, NULL);
729
730         assert(m->n_ref > 0);
731         m->n_ref++;
732
733         return m;
734 }
735
736 _public_ sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
737         assert_return(m, NULL);
738
739         assert(m->n_ref > 0);
740         m->n_ref--;
741
742         if (m->n_ref <= 0)
743                 message_free(m);
744
745         return NULL;
746 }
747
748 _public_ int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) {
749         assert_return(m, -EINVAL);
750         assert_return(type, -EINVAL);
751
752         *type = m->header->type;
753         return 0;
754 }
755
756 _public_ int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial) {
757         assert_return(m, -EINVAL);
758         assert_return(serial, -EINVAL);
759         assert_return(m->header->serial != 0, -ENOENT);
760
761         *serial = BUS_MESSAGE_SERIAL(m);
762         return 0;
763 }
764
765 _public_ int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial) {
766         assert_return(m, -EINVAL);
767         assert_return(serial, -EINVAL);
768         assert_return(m->reply_serial != 0, -ENOENT);
769
770         *serial = m->reply_serial;
771         return 0;
772 }
773
774 _public_ int sd_bus_message_get_no_reply(sd_bus_message *m) {
775         assert_return(m, -EINVAL);
776
777         return m->header->type == SD_BUS_MESSAGE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0;
778 }
779
780 _public_ const char *sd_bus_message_get_path(sd_bus_message *m) {
781         assert_return(m, NULL);
782
783         return m->path;
784 }
785
786 _public_ const char *sd_bus_message_get_interface(sd_bus_message *m) {
787         assert_return(m, NULL);
788
789         return m->interface;
790 }
791
792 _public_ const char *sd_bus_message_get_member(sd_bus_message *m) {
793         assert_return(m, NULL);
794
795         return m->member;
796 }
797
798 _public_ const char *sd_bus_message_get_destination(sd_bus_message *m) {
799         assert_return(m, NULL);
800
801         return m->destination;
802 }
803
804 _public_ const char *sd_bus_message_get_sender(sd_bus_message *m) {
805         assert_return(m, NULL);
806
807         return m->sender;
808 }
809
810 _public_ const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) {
811         assert_return(m, NULL);
812         assert_return(sd_bus_error_is_set(&m->error), NULL);
813
814         return &m->error;
815 }
816
817 _public_ int sd_bus_message_get_uid(sd_bus_message *m, uid_t *uid) {
818         assert_return(m, -EINVAL);
819         assert_return(uid, -EINVAL);
820         assert_return(m->uid_valid, -ESRCH);
821
822         *uid = m->uid;
823         return 0;
824 }
825
826 _public_ int sd_bus_message_get_gid(sd_bus_message *m, gid_t *gid) {
827         assert_return(m, -EINVAL);
828         assert_return(gid, -EINVAL);
829         assert_return(m->gid_valid, -ESRCH);
830
831         *gid = m->gid;
832         return 0;
833 }
834
835 _public_ int sd_bus_message_get_pid(sd_bus_message *m, pid_t *pid) {
836         assert_return(m, -EINVAL);
837         assert_return(pid, -EINVAL);
838         assert_return(m->pid > 0, -ESRCH);
839
840         *pid = m->pid;
841         return 0;
842 }
843
844 _public_ int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid) {
845         assert_return(m, -EINVAL);
846         assert_return(tid, -EINVAL);
847         assert_return(m->tid > 0, -ESRCH);
848
849         *tid = m->tid;
850         return 0;
851 }
852
853 _public_ int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec) {
854         assert_return(m, -EINVAL);
855         assert_return(usec, -EINVAL);
856         assert_return(m->pid_starttime > 0, -ESRCH);
857
858         *usec = m->pid_starttime;
859         return 0;
860 }
861
862 _public_ int sd_bus_message_get_selinux_context(sd_bus_message *m, const char **ret) {
863         assert_return(m, -EINVAL);
864         assert_return(m->label, -ESRCH);
865
866         *ret = m->label;
867         return 0;
868 }
869
870 _public_ int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) {
871         assert_return(m, -EINVAL);
872         assert_return(usec, -EINVAL);
873         assert_return(m->monotonic > 0, -ESRCH);
874
875         *usec = m->monotonic;
876         return 0;
877 }
878
879 _public_ int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) {
880         assert_return(m, -EINVAL);
881         assert_return(usec, -EINVAL);
882         assert_return(m->realtime > 0, -ESRCH);
883
884         *usec = m->realtime;
885         return 0;
886 }
887
888 _public_ int sd_bus_message_get_comm(sd_bus_message *m, const char **ret) {
889         assert_return(m, -EINVAL);
890         assert_return(ret, -EINVAL);
891         assert_return(m->comm, -ESRCH);
892
893         *ret = m->comm;
894         return 0;
895 }
896
897 _public_ int sd_bus_message_get_tid_comm(sd_bus_message *m, const char **ret) {
898         assert_return(m, -EINVAL);
899         assert_return(ret, -EINVAL);
900         assert_return(m->tid_comm, -ESRCH);
901
902         *ret = m->tid_comm;
903         return 0;
904 }
905
906 _public_ int sd_bus_message_get_exe(sd_bus_message *m, const char **ret) {
907         assert_return(m, -EINVAL);
908         assert_return(ret, -EINVAL);
909         assert_return(m->exe, -ESRCH);
910
911         *ret = m->exe;
912         return 0;
913 }
914
915 _public_ int sd_bus_message_get_cgroup(sd_bus_message *m, const char **ret) {
916         assert_return(m, -EINVAL);
917         assert_return(ret, -EINVAL);
918         assert_return(m->cgroup, -ESRCH);
919
920         *ret = m->cgroup;
921         return 0;
922 }
923
924 _public_ int sd_bus_message_get_unit(sd_bus_message *m, const char **ret) {
925         int r;
926
927         assert_return(m, -EINVAL);
928         assert_return(ret, -EINVAL);
929         assert_return(m->cgroup, -ESRCH);
930
931         if (!m->unit) {
932                 r = cg_path_get_unit(m->cgroup, &m->unit);
933                 if (r < 0)
934                         return r;
935         }
936
937         *ret = m->unit;
938         return 0;
939 }
940
941 _public_ int sd_bus_message_get_user_unit(sd_bus_message *m, const char **ret) {
942         int r;
943
944         assert_return(m, -EINVAL);
945         assert_return(ret, -EINVAL);
946         assert_return(m->cgroup, -ESRCH);
947
948         if (!m->user_unit) {
949                 r = cg_path_get_user_unit(m->cgroup, &m->user_unit);
950                 if (r < 0)
951                         return r;
952         }
953
954         *ret = m->user_unit;
955         return 0;
956 }
957
958 _public_ int sd_bus_message_get_session(sd_bus_message *m, const char **ret) {
959         int r;
960
961         assert_return(m, -EINVAL);
962         assert_return(ret, -EINVAL);
963         assert_return(m->cgroup, -ESRCH);
964
965         if (!m->session) {
966                 r = cg_path_get_session(m->cgroup, &m->session);
967                 if (r < 0)
968                         return r;
969         }
970
971         *ret = m->session;
972         return 0;
973 }
974
975 _public_ int sd_bus_message_get_owner_uid(sd_bus_message *m, uid_t *uid) {
976         assert_return(m, -EINVAL);
977         assert_return(uid, -EINVAL);
978         assert_return(m->cgroup, -ESRCH);
979
980         return cg_path_get_owner_uid(m->cgroup, uid);
981 }
982
983 _public_ int sd_bus_message_get_cmdline(sd_bus_message *m, char ***cmdline) {
984         size_t n, i;
985         const char *p;
986         bool first;
987
988         assert_return(m, -EINVAL);
989         assert_return(m->cmdline, -ESRCH);
990
991         for (p = m->cmdline, n = 0; p < m->cmdline + m->cmdline_length; p++)
992                 if (*p == 0)
993                         n++;
994
995         m->cmdline_array = new(char*, n + 1);
996         if (!m->cmdline_array)
997                 return -ENOMEM;
998
999         for (p = m->cmdline, i = 0, first = true; p < m->cmdline + m->cmdline_length; p++) {
1000                 if (first)
1001                         m->cmdline_array[i++] = (char*) p;
1002
1003                 first = *p == 0;
1004         }
1005
1006         m->cmdline_array[i] = NULL;
1007         *cmdline = m->cmdline_array;
1008
1009         return 0;
1010 }
1011
1012 _public_ int sd_bus_message_get_audit_sessionid(sd_bus_message *m, uint32_t *sessionid) {
1013         assert_return(m, -EINVAL);
1014         assert_return(sessionid, -EINVAL);
1015         assert_return(m->audit, -ESRCH);
1016
1017         *sessionid = m->audit->sessionid;
1018         return 0;
1019 }
1020
1021 _public_ int sd_bus_message_get_audit_loginuid(sd_bus_message *m, uid_t *uid) {
1022         assert_return(m, -EINVAL);
1023         assert_return(uid, -EINVAL);
1024         assert_return(m->audit, -ESRCH);
1025
1026         *uid = m->audit->loginuid;
1027         return 0;
1028 }
1029
1030 _public_ int sd_bus_message_has_effective_cap(sd_bus_message *m, int capability) {
1031         unsigned sz;
1032
1033         assert_return(m, -EINVAL);
1034         assert_return(capability < 0, -EINVAL);
1035         assert_return(!m->capability, -ESRCH);
1036
1037         sz = m->capability_size / 4;
1038         if ((unsigned) capability >= sz*8)
1039                 return 0;
1040
1041         return !!(m->capability[2 * sz + (capability / 8)] & (1 << (capability % 8)));
1042 }
1043
1044 _public_ int sd_bus_message_is_signal(sd_bus_message *m,
1045                                       const char *interface,
1046                                       const char *member) {
1047         assert_return(m, -EINVAL);
1048
1049         if (m->header->type != SD_BUS_MESSAGE_SIGNAL)
1050                 return 0;
1051
1052         if (interface && (!m->interface || !streq(m->interface, interface)))
1053                 return 0;
1054
1055         if (member &&  (!m->member || !streq(m->member, member)))
1056                 return 0;
1057
1058         return 1;
1059 }
1060
1061 _public_ int sd_bus_message_is_method_call(sd_bus_message *m,
1062                                            const char *interface,
1063                                            const char *member) {
1064         assert_return(m, -EINVAL);
1065
1066         if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
1067                 return 0;
1068
1069         if (interface && (!m->interface || !streq(m->interface, interface)))
1070                 return 0;
1071
1072         if (member &&  (!m->member || !streq(m->member, member)))
1073                 return 0;
1074
1075         return 1;
1076 }
1077
1078 _public_ int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) {
1079         assert_return(m, -EINVAL);
1080
1081         if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1082                 return 0;
1083
1084         if (name && (!m->error.name || !streq(m->error.name, name)))
1085                 return 0;
1086
1087         return 1;
1088 }
1089
1090 _public_ int sd_bus_message_set_no_reply(sd_bus_message *m, int b) {
1091         assert_return(m, -EINVAL);
1092         assert_return(!m->sealed, -EPERM);
1093         assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EPERM);
1094
1095         if (b)
1096                 m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
1097         else
1098                 m->header->flags &= ~SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
1099
1100         return 0;
1101 }
1102
1103 static struct bus_container *message_get_container(sd_bus_message *m) {
1104         assert(m);
1105
1106         if (m->n_containers == 0)
1107                 return &m->root_container;
1108
1109         assert(m->containers);
1110         return m->containers + m->n_containers - 1;
1111 }
1112
1113 struct bus_body_part *message_append_part(sd_bus_message *m) {
1114         struct bus_body_part *part;
1115
1116         assert(m);
1117
1118         if (m->poisoned)
1119                 return NULL;
1120
1121         if (m->n_body_parts <= 0) {
1122                 part = &m->body;
1123                 zero(*part);
1124         } else {
1125                 assert(m->body_end);
1126
1127                 part = new0(struct bus_body_part, 1);
1128                 if (!part) {
1129                         m->poisoned = true;
1130                         return NULL;
1131                 }
1132
1133                 m->body_end->next = part;
1134         }
1135
1136         part->memfd = -1;
1137         m->body_end = part;
1138         m->n_body_parts ++;
1139
1140         return part;
1141 }
1142
1143 static void part_zero(struct bus_body_part *part, size_t sz) {
1144         assert(part);
1145         assert(sz > 0);
1146         assert(sz < 8);
1147
1148         /* All other fields can be left in their defaults */
1149         assert(!part->data);
1150         assert(part->memfd < 0);
1151
1152         part->size = sz;
1153         part->is_zero = true;
1154         part->sealed = true;
1155 }
1156
1157 static int part_make_space(
1158                 struct sd_bus_message *m,
1159                 struct bus_body_part *part,
1160                 size_t sz,
1161                 void **q) {
1162
1163         void *n;
1164         int r;
1165
1166         assert(m);
1167         assert(part);
1168         assert(!part->sealed);
1169
1170         if (m->poisoned)
1171                 return -ENOMEM;
1172
1173         if (!part->data && part->memfd < 0)
1174                 part->memfd = bus_kernel_pop_memfd(m->bus, &part->data, &part->mapped);
1175
1176         if (part->memfd >= 0) {
1177                 uint64_t u = sz;
1178
1179                 r = ioctl(part->memfd, KDBUS_CMD_MEMFD_SIZE_SET, &u);
1180                 if (r < 0) {
1181                         m->poisoned = true;
1182                         return -errno;
1183                 }
1184
1185                 if (!part->data || sz > part->mapped) {
1186                         size_t psz = PAGE_ALIGN(sz > 0 ? sz : 1);
1187
1188                         if (part->mapped <= 0)
1189                                 n = mmap(NULL, psz, PROT_READ|PROT_WRITE, MAP_SHARED, part->memfd, 0);
1190                         else
1191                                 n = mremap(part->data, part->mapped, psz, MREMAP_MAYMOVE);
1192
1193                         if (n == MAP_FAILED) {
1194                                 m->poisoned = true;
1195                                 return -errno;
1196                         }
1197
1198                         part->mapped = psz;
1199                         part->data = n;
1200                 }
1201
1202                 part->munmap_this = true;
1203         } else {
1204                 n = realloc(part->data, MAX(sz, 1u));
1205                 if (!n) {
1206                         m->poisoned = true;
1207                         return -ENOMEM;
1208                 }
1209
1210                 part->data = n;
1211                 part->free_this = true;
1212         }
1213
1214         if (q)
1215                 *q = part->data ? (uint8_t*) part->data + part->size : NULL;
1216
1217         part->size = sz;
1218         return 0;
1219 }
1220
1221 static void message_extend_containers(sd_bus_message *m, size_t expand) {
1222         struct bus_container *c;
1223
1224         assert(m);
1225
1226         if (expand <= 0)
1227                 return;
1228
1229         /* Update counters */
1230         for (c = m->containers; c < m->containers + m->n_containers; c++)
1231                 if (c->array_size)
1232                         *c->array_size += expand;
1233 }
1234
1235 static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
1236         struct bus_body_part *part = NULL;
1237         size_t start_body, end_body, padding, start_part, end_part, added;
1238         bool add_new_part;
1239         void *p;
1240         int r;
1241
1242         assert(m);
1243         assert(align > 0);
1244         assert(!m->sealed);
1245
1246         if (m->poisoned)
1247                 return NULL;
1248
1249         start_body = ALIGN_TO((size_t) m->header->body_size, align);
1250         end_body = start_body + sz;
1251
1252         padding = start_body - m->header->body_size;
1253         added = padding + sz;
1254
1255         /* Check for 32bit overflows */
1256         if (end_body > (size_t) ((uint32_t) -1)) {
1257                 m->poisoned = true;
1258                 return NULL;
1259         }
1260
1261         add_new_part =
1262                 m->n_body_parts <= 0 ||
1263                 m->body_end->sealed ||
1264                 padding != ALIGN_TO(m->body_end->size, align) - m->body_end->size;
1265
1266         if (add_new_part) {
1267                 if (padding > 0) {
1268                         part = message_append_part(m);
1269                         if (!part)
1270                                 return NULL;
1271
1272                         part_zero(part, padding);
1273                 }
1274
1275                 part = message_append_part(m);
1276                 if (!part)
1277                         return NULL;
1278
1279                 r = part_make_space(m, part, sz, &p);
1280                 if (r < 0)
1281                         return NULL;
1282         } else {
1283                 struct bus_container *c;
1284                 void *op;
1285                 size_t os;
1286
1287                 part = m->body_end;
1288                 op = part->data;
1289                 os = part->size;
1290
1291                 start_part = ALIGN_TO(part->size, align);
1292                 end_part = start_part + sz;
1293
1294                 r = part_make_space(m, part, end_part, &p);
1295                 if (r < 0)
1296                         return NULL;
1297
1298                 if (padding > 0) {
1299                         memset(p, 0, padding);
1300                         p = (uint8_t*) p + padding;
1301                 }
1302
1303                 /* Readjust pointers */
1304                 for (c = m->containers; c < m->containers + m->n_containers; c++)
1305                         c->array_size = adjust_pointer(c->array_size, op, os, part->data);
1306
1307                 m->error.message = (const char*) adjust_pointer(m->error.message, op, os, part->data);
1308         }
1309
1310         m->header->body_size = end_body;
1311         message_extend_containers(m, added);
1312
1313         return p;
1314 }
1315
1316 int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored) {
1317         struct bus_container *c;
1318         ssize_t align, sz;
1319         uint32_t k;
1320         void *a;
1321         int fd = -1;
1322         uint32_t fdi = 0;
1323         int r;
1324
1325         assert_return(m, -EINVAL);
1326         assert_return(!m->sealed, -EPERM);
1327         assert_return(bus_type_is_basic(type), -EINVAL);
1328         assert_return(!m->poisoned, -ESTALE);
1329
1330         c = message_get_container(m);
1331
1332         if (c->signature && c->signature[c->index]) {
1333                 /* Container signature is already set */
1334
1335                 if (c->signature[c->index] != type)
1336                         return -ENXIO;
1337         } else {
1338                 char *e;
1339
1340                 /* Maybe we can append to the signature? But only if this is the top-level container*/
1341                 if (c->enclosing != 0)
1342                         return -ENXIO;
1343
1344                 e = strextend(&c->signature, CHAR_TO_STR(type), NULL);
1345                 if (!e) {
1346                         m->poisoned = true;
1347                         return -ENOMEM;
1348                 }
1349         }
1350
1351         switch (type) {
1352
1353         case SD_BUS_TYPE_STRING:
1354                 /* To make things easy we'll serialize a NULL string
1355                  * into the empty string */
1356                 p = strempty(p);
1357
1358                 /* Fall through... */
1359         case SD_BUS_TYPE_OBJECT_PATH:
1360
1361                 if (!p) {
1362                         r = -EINVAL;
1363                         goto fail;
1364                 }
1365
1366                 align = 4;
1367                 sz = 4 + strlen(p) + 1;
1368                 break;
1369
1370         case SD_BUS_TYPE_SIGNATURE:
1371
1372                 if (!p) {
1373                         r = -EINVAL;
1374                         goto fail;
1375                 }
1376
1377                 align = 1;
1378                 sz = 1 + strlen(p) + 1;
1379                 break;
1380
1381         case SD_BUS_TYPE_BOOLEAN:
1382
1383                 if (!p) {
1384                         r = -EINVAL;
1385                         goto fail;
1386                 }
1387
1388                 align = sz = 4;
1389
1390                 assert_cc(sizeof(int) == sizeof(uint32_t));
1391                 memcpy(&k, p, 4);
1392                 k = !!k;
1393                 p = &k;
1394                 break;
1395
1396         case SD_BUS_TYPE_UNIX_FD: {
1397                 int z, *f;
1398
1399                 if (!p) {
1400                         r = -EINVAL;
1401                         goto fail;
1402                 }
1403
1404                 if (!m->allow_fds) {
1405                         r = -ENOTSUP;
1406                         goto fail;
1407                 }
1408
1409                 align = sz = 4;
1410
1411                 z = *(int*) p;
1412                 if (z < 0) {
1413                         r = -EINVAL;
1414                         goto fail;
1415                 }
1416
1417                 fd = fcntl(z, F_DUPFD_CLOEXEC, 3);
1418                 if (fd < 0) {
1419                         r = -errno;
1420                         goto fail;
1421                 }
1422
1423                 f = realloc(m->fds, sizeof(int) * (m->n_fds + 1));
1424                 if (!f) {
1425                         m->poisoned = true;
1426                         r = -ENOMEM;
1427                         goto fail;
1428                 }
1429
1430                 fdi = m->n_fds;
1431                 f[fdi] = fd;
1432                 m->fds = f;
1433                 m->free_fds = true;
1434                 break;
1435         }
1436
1437         default:
1438                 if (!p) {
1439                         r = -EINVAL;
1440                         goto fail;
1441                 }
1442
1443                 align = bus_type_get_alignment(type);
1444                 sz = bus_type_get_size(type);
1445                 break;
1446         }
1447
1448         assert(align > 0);
1449         assert(sz > 0);
1450
1451         a = message_extend_body(m, align, sz);
1452         if (!a) {
1453                 r = -ENOMEM;
1454                 goto fail;
1455         }
1456
1457         if (type == SD_BUS_TYPE_STRING || type == SD_BUS_TYPE_OBJECT_PATH) {
1458                 *(uint32_t*) a = sz - 5;
1459                 memcpy((uint8_t*) a + 4, p, sz - 4);
1460
1461                 if (stored)
1462                         *stored = (const uint8_t*) a + 4;
1463
1464         } else if (type == SD_BUS_TYPE_SIGNATURE) {
1465                 *(uint8_t*) a = sz - 1;
1466                 memcpy((uint8_t*) a + 1, p, sz - 1);
1467
1468                 if (stored)
1469                         *stored = (const uint8_t*) a + 1;
1470         } else if (type == SD_BUS_TYPE_UNIX_FD) {
1471                 *(uint32_t*) a = fdi;
1472
1473                 if (stored)
1474                         *stored = a;
1475
1476                 m->n_fds ++;
1477
1478         } else {
1479                 memcpy(a, p, sz);
1480
1481                 if (stored)
1482                         *stored = a;
1483         }
1484
1485         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1486                 c->index++;
1487
1488         return 0;
1489
1490 fail:
1491         if (fd >= 0)
1492                 close_nointr_nofail(fd);
1493
1494         return r;
1495 }
1496
1497 _public_ int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) {
1498         return message_append_basic(m, type, p, NULL);
1499 }
1500
1501 _public_ int sd_bus_message_append_string_space(
1502                 sd_bus_message *m,
1503                 size_t size,
1504                 char **s) {
1505
1506         struct bus_container *c;
1507         void *a;
1508
1509         assert_return(m, -EINVAL);
1510         assert_return(s, -EINVAL);
1511         assert_return(!m->sealed, -EPERM);
1512         assert_return(!m->poisoned, -ESTALE);
1513
1514         c = message_get_container(m);
1515
1516         if (c->signature && c->signature[c->index]) {
1517                 /* Container signature is already set */
1518
1519                 if (c->signature[c->index] != SD_BUS_TYPE_STRING)
1520                         return -ENXIO;
1521         } else {
1522                 char *e;
1523
1524                 /* Maybe we can append to the signature? But only if this is the top-level container*/
1525                 if (c->enclosing != 0)
1526                         return -ENXIO;
1527
1528                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
1529                 if (!e) {
1530                         m->poisoned = true;
1531                         return -ENOMEM;
1532                 }
1533         }
1534
1535         a = message_extend_body(m, 4, 4 + size + 1);
1536         if (!a)
1537                 return -ENOMEM;
1538
1539         *(uint32_t*) a = size;
1540         *s = (char*) a + 4;
1541
1542         (*s)[size] = 0;
1543
1544         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1545                 c->index++;
1546
1547         return 0;
1548 }
1549
1550 _public_ int sd_bus_message_append_string_iovec(
1551                 sd_bus_message *m,
1552                 const struct iovec *iov,
1553                 unsigned n) {
1554
1555         size_t size;
1556         unsigned i;
1557         char *p;
1558         int r;
1559
1560         assert_return(m, -EINVAL);
1561         assert_return(!m->sealed, -EPERM);
1562         assert_return(iov || n == 0, -EINVAL);
1563         assert_return(!m->poisoned, -ESTALE);
1564
1565         size = IOVEC_TOTAL_SIZE(iov, n);
1566
1567         r = sd_bus_message_append_string_space(m, size, &p);
1568         if (r < 0)
1569                 return r;
1570
1571         for (i = 0; i < n; i++) {
1572
1573                 if (iov[i].iov_base)
1574                         memcpy(p, iov[i].iov_base, iov[i].iov_len);
1575                 else
1576                         memset(p, ' ', iov[i].iov_len);
1577
1578                 p += iov[i].iov_len;
1579         }
1580
1581         return 0;
1582 }
1583
1584 static int bus_message_open_array(
1585                 sd_bus_message *m,
1586                 struct bus_container *c,
1587                 const char *contents,
1588                 uint32_t **array_size) {
1589
1590         unsigned nindex;
1591         void *a, *op;
1592         int alignment;
1593         size_t os;
1594         struct bus_body_part *o;
1595
1596         assert(m);
1597         assert(c);
1598         assert(contents);
1599         assert(array_size);
1600
1601         if (!signature_is_single(contents, true))
1602                 return -EINVAL;
1603
1604         alignment = bus_type_get_alignment(contents[0]);
1605         if (alignment < 0)
1606                 return alignment;
1607
1608         if (c->signature && c->signature[c->index]) {
1609
1610                 /* Verify the existing signature */
1611
1612                 if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
1613                         return -ENXIO;
1614
1615                 if (!startswith(c->signature + c->index + 1, contents))
1616                         return -ENXIO;
1617
1618                 nindex = c->index + 1 + strlen(contents);
1619         } else {
1620                 char *e;
1621
1622                 if (c->enclosing != 0)
1623                         return -ENXIO;
1624
1625                 /* Extend the existing signature */
1626
1627                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents, NULL);
1628                 if (!e) {
1629                         m->poisoned = true;
1630                         return -ENOMEM;
1631                 }
1632
1633                 nindex = e - c->signature;
1634         }
1635
1636         a = message_extend_body(m, 4, 4);
1637         if (!a)
1638                 return -ENOMEM;
1639
1640         o = m->body_end;
1641         op = m->body_end->data;
1642         os = m->body_end->size;
1643
1644         /* Add alignment between size and first element */
1645         if (!message_extend_body(m, alignment, 0))
1646                 return -ENOMEM;
1647
1648         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1649                 c->index = nindex;
1650
1651         /* location of array size might have changed so let's readjust a */
1652         if (o == m->body_end)
1653                 a = adjust_pointer(a, op, os, m->body_end->data);
1654
1655         *(uint32_t*) a = 0;
1656         *array_size = a;
1657         return 0;
1658 }
1659
1660 static int bus_message_open_variant(
1661                 sd_bus_message *m,
1662                 struct bus_container *c,
1663                 const char *contents) {
1664
1665         size_t l;
1666         void *a;
1667
1668         assert(m);
1669         assert(c);
1670         assert(contents);
1671
1672         if (!signature_is_single(contents, false))
1673                 return -EINVAL;
1674
1675         if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
1676                 return -EINVAL;
1677
1678         if (c->signature && c->signature[c->index]) {
1679
1680                 if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
1681                         return -ENXIO;
1682
1683         } else {
1684                 char *e;
1685
1686                 if (c->enclosing != 0)
1687                         return -ENXIO;
1688
1689                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT), NULL);
1690                 if (!e) {
1691                         m->poisoned = true;
1692                         return -ENOMEM;
1693                 }
1694         }
1695
1696         l = strlen(contents);
1697         a = message_extend_body(m, 1, 1 + l + 1);
1698         if (!a)
1699                 return -ENOMEM;
1700
1701         *(uint8_t*) a = l;
1702         memcpy((uint8_t*) a + 1, contents, l + 1);
1703
1704         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1705                 c->index++;
1706
1707         return 0;
1708 }
1709
1710 static int bus_message_open_struct(
1711                 sd_bus_message *m,
1712                 struct bus_container *c,
1713                 const char *contents) {
1714
1715         size_t nindex;
1716
1717         assert(m);
1718         assert(c);
1719         assert(contents);
1720
1721         if (!signature_is_valid(contents, false))
1722                 return -EINVAL;
1723
1724         if (c->signature && c->signature[c->index]) {
1725                 size_t l;
1726
1727                 l = strlen(contents);
1728
1729                 if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
1730                     !startswith(c->signature + c->index + 1, contents) ||
1731                     c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
1732                         return -ENXIO;
1733
1734                 nindex = c->index + 1 + l + 1;
1735         } else {
1736                 char *e;
1737
1738                 if (c->enclosing != 0)
1739                         return -ENXIO;
1740
1741                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL);
1742                 if (!e) {
1743                         m->poisoned = true;
1744                         return -ENOMEM;
1745                 }
1746
1747                 nindex = e - c->signature;
1748         }
1749
1750         /* Align contents to 8 byte boundary */
1751         if (!message_extend_body(m, 8, 0))
1752                 return -ENOMEM;
1753
1754         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1755                 c->index = nindex;
1756
1757         return 0;
1758 }
1759
1760 static int bus_message_open_dict_entry(
1761                 sd_bus_message *m,
1762                 struct bus_container *c,
1763                 const char *contents) {
1764
1765         size_t nindex;
1766
1767         assert(m);
1768         assert(c);
1769         assert(contents);
1770
1771         if (!signature_is_pair(contents))
1772                 return -EINVAL;
1773
1774         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1775                 return -ENXIO;
1776
1777         if (c->signature && c->signature[c->index]) {
1778                 size_t l;
1779
1780                 l = strlen(contents);
1781
1782                 if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
1783                     !startswith(c->signature + c->index + 1, contents) ||
1784                     c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
1785                         return -ENXIO;
1786
1787                 nindex = c->index + 1 + l + 1;
1788         } else
1789                 return -ENXIO;
1790
1791         /* Align contents to 8 byte boundary */
1792         if (!message_extend_body(m, 8, 0))
1793                 return -ENOMEM;
1794
1795         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1796                 c->index = nindex;
1797
1798         return 0;
1799 }
1800
1801 _public_ int sd_bus_message_open_container(
1802                 sd_bus_message *m,
1803                 char type,
1804                 const char *contents) {
1805
1806         struct bus_container *c, *w;
1807         uint32_t *array_size = NULL;
1808         char *signature;
1809         size_t before;
1810         int r;
1811
1812         assert_return(m, -EINVAL);
1813         assert_return(!m->sealed, -EPERM);
1814         assert_return(contents, -EINVAL);
1815         assert_return(!m->poisoned, -ESTALE);
1816
1817         /* Make sure we have space for one more container */
1818         w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
1819         if (!w) {
1820                 m->poisoned = true;
1821                 return -ENOMEM;
1822         }
1823
1824         m->containers = w;
1825
1826         c = message_get_container(m);
1827
1828         signature = strdup(contents);
1829         if (!signature) {
1830                 m->poisoned = true;
1831                 return -ENOMEM;
1832         }
1833
1834         /* Save old index in the parent container, in case we have to
1835          * abort this container */
1836         c->saved_index = c->index;
1837         before = m->header->body_size;
1838
1839         if (type == SD_BUS_TYPE_ARRAY)
1840                 r = bus_message_open_array(m, c, contents, &array_size);
1841         else if (type == SD_BUS_TYPE_VARIANT)
1842                 r = bus_message_open_variant(m, c, contents);
1843         else if (type == SD_BUS_TYPE_STRUCT)
1844                 r = bus_message_open_struct(m, c, contents);
1845         else if (type == SD_BUS_TYPE_DICT_ENTRY)
1846                 r = bus_message_open_dict_entry(m, c, contents);
1847         else
1848                 r = -EINVAL;
1849
1850         if (r < 0) {
1851                 free(signature);
1852                 return r;
1853         }
1854
1855         /* OK, let's fill it in */
1856         w += m->n_containers++;
1857         w->enclosing = type;
1858         w->signature = signature;
1859         w->index = 0;
1860         w->array_size = array_size;
1861         w->before = before;
1862         w->begin = m->rindex;
1863
1864         return 0;
1865 }
1866
1867 _public_ int sd_bus_message_close_container(sd_bus_message *m) {
1868         struct bus_container *c;
1869
1870         assert_return(m, -EINVAL);
1871         assert_return(!m->sealed, -EPERM);
1872         assert_return(m->n_containers > 0, -EINVAL);
1873         assert_return(!m->poisoned, -ESTALE);
1874
1875         c = message_get_container(m);
1876         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1877                 if (c->signature && c->signature[c->index] != 0)
1878                         return -EINVAL;
1879
1880         free(c->signature);
1881         m->n_containers--;
1882
1883         return 0;
1884 }
1885
1886 typedef struct {
1887         const char *types;
1888         unsigned n_struct;
1889         unsigned n_array;
1890 } TypeStack;
1891
1892 static int type_stack_push(TypeStack *stack, unsigned max, unsigned *i, const char *types, unsigned n_struct, unsigned n_array) {
1893         assert(stack);
1894         assert(max > 0);
1895
1896         if (*i >= max)
1897                 return -EINVAL;
1898
1899         stack[*i].types = types;
1900         stack[*i].n_struct = n_struct;
1901         stack[*i].n_array = n_array;
1902         (*i)++;
1903
1904         return 0;
1905 }
1906
1907 static int type_stack_pop(TypeStack *stack, unsigned max, unsigned *i, const char **types, unsigned *n_struct, unsigned *n_array) {
1908         assert(stack);
1909         assert(max > 0);
1910         assert(types);
1911         assert(n_struct);
1912         assert(n_array);
1913
1914         if (*i <= 0)
1915                 return 0;
1916
1917         (*i)--;
1918         *types = stack[*i].types;
1919         *n_struct = stack[*i].n_struct;
1920         *n_array = stack[*i].n_array;
1921
1922         return 1;
1923 }
1924
1925 int bus_message_append_ap(
1926                 sd_bus_message *m,
1927                 const char *types,
1928                 va_list ap) {
1929
1930         unsigned n_array, n_struct;
1931         TypeStack stack[BUS_CONTAINER_DEPTH];
1932         unsigned stack_ptr = 0;
1933         int r;
1934
1935         assert(m);
1936
1937         if (!types)
1938                 return 0;
1939
1940         n_array = (unsigned) -1;
1941         n_struct = strlen(types);
1942
1943         for (;;) {
1944                 const char *t;
1945
1946                 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
1947                         r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
1948                         if (r < 0)
1949                                 return r;
1950                         if (r == 0)
1951                                 break;
1952
1953                         r = sd_bus_message_close_container(m);
1954                         if (r < 0)
1955                                 return r;
1956
1957                         continue;
1958                 }
1959
1960                 t = types;
1961                 if (n_array != (unsigned) -1)
1962                         n_array --;
1963                 else {
1964                         types ++;
1965                         n_struct--;
1966                 }
1967
1968                 switch (*t) {
1969
1970                 case SD_BUS_TYPE_BYTE: {
1971                         uint8_t x;
1972
1973                         x = (uint8_t) va_arg(ap, int);
1974                         r = sd_bus_message_append_basic(m, *t, &x);
1975                         break;
1976                 }
1977
1978                 case SD_BUS_TYPE_BOOLEAN:
1979                 case SD_BUS_TYPE_INT32:
1980                 case SD_BUS_TYPE_UINT32:
1981                 case SD_BUS_TYPE_UNIX_FD: {
1982                         uint32_t x;
1983
1984                         /* We assume a boolean is the same as int32_t */
1985                         assert_cc(sizeof(int32_t) == sizeof(int));
1986
1987                         x = va_arg(ap, uint32_t);
1988                         r = sd_bus_message_append_basic(m, *t, &x);
1989                         break;
1990                 }
1991
1992                 case SD_BUS_TYPE_INT16:
1993                 case SD_BUS_TYPE_UINT16: {
1994                         uint16_t x;
1995
1996                         x = (uint16_t) va_arg(ap, int);
1997                         r = sd_bus_message_append_basic(m, *t, &x);
1998                         break;
1999                 }
2000
2001                 case SD_BUS_TYPE_INT64:
2002                 case SD_BUS_TYPE_UINT64:
2003                 case SD_BUS_TYPE_DOUBLE: {
2004                         uint64_t x;
2005
2006                         x = va_arg(ap, uint64_t);
2007                         r = sd_bus_message_append_basic(m, *t, &x);
2008                         break;
2009                 }
2010
2011                 case SD_BUS_TYPE_STRING:
2012                 case SD_BUS_TYPE_OBJECT_PATH:
2013                 case SD_BUS_TYPE_SIGNATURE: {
2014                         const char *x;
2015
2016                         x = va_arg(ap, const char*);
2017                         r = sd_bus_message_append_basic(m, *t, x);
2018                         break;
2019                 }
2020
2021                 case SD_BUS_TYPE_ARRAY: {
2022                         size_t k;
2023
2024                         r = signature_element_length(t + 1, &k);
2025                         if (r < 0)
2026                                 return r;
2027
2028                         {
2029                                 char s[k + 1];
2030                                 memcpy(s, t + 1, k);
2031                                 s[k] = 0;
2032
2033                                 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
2034                                 if (r < 0)
2035                                         return r;
2036                         }
2037
2038                         if (n_array == (unsigned) -1) {
2039                                 types += k;
2040                                 n_struct -= k;
2041                         }
2042
2043                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2044                         if (r < 0)
2045                                 return r;
2046
2047                         types = t + 1;
2048                         n_struct = k;
2049                         n_array = va_arg(ap, unsigned);
2050
2051                         break;
2052                 }
2053
2054                 case SD_BUS_TYPE_VARIANT: {
2055                         const char *s;
2056
2057                         s = va_arg(ap, const char*);
2058                         if (!s)
2059                                 return -EINVAL;
2060
2061                         r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, s);
2062                         if (r < 0)
2063                                 return r;
2064
2065                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2066                         if (r < 0)
2067                                 return r;
2068
2069                         types = s;
2070                         n_struct = strlen(s);
2071                         n_array = (unsigned) -1;
2072
2073                         break;
2074                 }
2075
2076                 case SD_BUS_TYPE_STRUCT_BEGIN:
2077                 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
2078                         size_t k;
2079
2080                         r = signature_element_length(t, &k);
2081                         if (r < 0)
2082                                 return r;
2083
2084                         {
2085                                 char s[k - 1];
2086
2087                                 memcpy(s, t + 1, k - 2);
2088                                 s[k - 2] = 0;
2089
2090                                 r = sd_bus_message_open_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
2091                                 if (r < 0)
2092                                         return r;
2093                         }
2094
2095                         if (n_array == (unsigned) -1) {
2096                                 types += k - 1;
2097                                 n_struct -= k - 1;
2098                         }
2099
2100                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2101                         if (r < 0)
2102                                 return r;
2103
2104                         types = t + 1;
2105                         n_struct = k - 2;
2106                         n_array = (unsigned) -1;
2107
2108                         break;
2109                 }
2110
2111                 default:
2112                         r = -EINVAL;
2113                 }
2114
2115                 if (r < 0)
2116                         return r;
2117         }
2118
2119         return 0;
2120 }
2121
2122 _public_ int sd_bus_message_append(sd_bus_message *m, const char *types, ...) {
2123         va_list ap;
2124         int r;
2125
2126         assert_return(m, -EINVAL);
2127         assert_return(types, -EINVAL);
2128         assert_return(!m->sealed, -EPERM);
2129         assert_return(!m->poisoned, -ESTALE);
2130
2131         va_start(ap, types);
2132         r = bus_message_append_ap(m, types, ap);
2133         va_end(ap);
2134
2135         return r;
2136 }
2137
2138 _public_ int sd_bus_message_append_array_space(sd_bus_message *m,
2139                                                char type,
2140                                                size_t size,
2141                                                void **ptr) {
2142         ssize_t align, sz;
2143         void *a;
2144         int r;
2145
2146         assert_return(m, -EINVAL);
2147         assert_return(!m->sealed, -EPERM);
2148         assert_return(bus_type_is_trivial(type), -EINVAL);
2149         assert_return(ptr || size == 0, -EINVAL);
2150         assert_return(!m->poisoned, -ESTALE);
2151
2152         align = bus_type_get_alignment(type);
2153         sz = bus_type_get_size(type);
2154
2155         assert_se(align > 0);
2156         assert_se(sz > 0);
2157
2158         if (size % sz != 0)
2159                 return -EINVAL;
2160
2161         r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
2162         if (r < 0)
2163                 return r;
2164
2165         a = message_extend_body(m, align, size);
2166         if (!a)
2167                 return -ENOMEM;
2168
2169         r = sd_bus_message_close_container(m);
2170         if (r < 0)
2171                 return r;
2172
2173         *ptr = a;
2174         return 0;
2175 }
2176
2177 _public_ int sd_bus_message_append_array(sd_bus_message *m,
2178                                          char type,
2179                                          const void *ptr,
2180                                          size_t size) {
2181         int r;
2182         void *p;
2183
2184         assert_return(m, -EINVAL);
2185         assert_return(!m->sealed, -EPERM);
2186         assert_return(bus_type_is_trivial(type), -EINVAL);
2187         assert_return(ptr || size == 0, -EINVAL);
2188         assert_return(!m->poisoned, -ESTALE);
2189
2190         r = sd_bus_message_append_array_space(m, type, size, &p);
2191         if (r < 0)
2192                 return r;
2193
2194         if (size > 0)
2195                 memcpy(p, ptr, size);
2196
2197         return 0;
2198 }
2199
2200 _public_ int sd_bus_message_append_array_iovec(
2201                 sd_bus_message *m,
2202                 char type,
2203                 const struct iovec *iov,
2204                 unsigned n) {
2205
2206         size_t size;
2207         unsigned i;
2208         void *p;
2209         int r;
2210
2211         assert_return(m, -EINVAL);
2212         assert_return(!m->sealed, -EPERM);
2213         assert_return(bus_type_is_trivial(type), -EINVAL);
2214         assert_return(iov || n == 0, -EINVAL);
2215         assert_return(!m->poisoned, -ESTALE);
2216
2217         size = IOVEC_TOTAL_SIZE(iov, n);
2218
2219         r = sd_bus_message_append_array_space(m, type, size, &p);
2220         if (r < 0)
2221                 return r;
2222
2223         for (i = 0; i < n; i++) {
2224
2225                 if (iov[i].iov_base)
2226                         memcpy(p, iov[i].iov_base, iov[i].iov_len);
2227                 else
2228                         memset(p, 0, iov[i].iov_len);
2229
2230                 p = (uint8_t*) p + iov[i].iov_len;
2231         }
2232
2233         return 0;
2234 }
2235
2236 _public_ int sd_bus_message_append_array_memfd(sd_bus_message *m,
2237                                                char type,
2238                                                sd_memfd *memfd) {
2239         _cleanup_close_ int copy_fd = -1;
2240         struct bus_body_part *part;
2241         ssize_t align, sz;
2242         uint64_t size;
2243         void *a;
2244         int r;
2245
2246         if (!m)
2247                 return -EINVAL;
2248         if (!memfd)
2249                 return -EINVAL;
2250         if (m->sealed)
2251                 return -EPERM;
2252         if (!bus_type_is_trivial(type))
2253                 return -EINVAL;
2254         if (m->poisoned)
2255                 return -ESTALE;
2256
2257         r = sd_memfd_set_sealed(memfd, true);
2258         if (r < 0)
2259                 return r;
2260
2261         copy_fd = sd_memfd_dup_fd(memfd);
2262         if (copy_fd < 0)
2263                 return copy_fd;
2264
2265         r = sd_memfd_get_size(memfd, &size);
2266         if (r < 0)
2267                 return r;
2268
2269         align = bus_type_get_alignment(type);
2270         sz = bus_type_get_size(type);
2271
2272         assert_se(align > 0);
2273         assert_se(sz > 0);
2274
2275         if (size % sz != 0)
2276                 return -EINVAL;
2277
2278         if (size > (uint64_t) (uint32_t) -1)
2279                 return -EINVAL;
2280
2281         r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
2282         if (r < 0)
2283                 return r;
2284
2285         a = message_extend_body(m, align, 0);
2286         if (!a)
2287                 return -ENOMEM;
2288
2289         part = message_append_part(m);
2290         if (!part)
2291                 return -ENOMEM;
2292
2293         part->memfd = copy_fd;
2294         part->sealed = true;
2295         part->size = size;
2296         copy_fd = -1;
2297
2298         message_extend_containers(m, size);
2299         m->header->body_size += size;
2300
2301         return sd_bus_message_close_container(m);
2302 }
2303
2304 _public_ int sd_bus_message_append_string_memfd(sd_bus_message *m, sd_memfd *memfd) {
2305         _cleanup_close_ int copy_fd = -1;
2306         struct bus_body_part *part;
2307         struct bus_container *c;
2308         uint64_t size;
2309         void *a;
2310         int r;
2311
2312         assert_return(m, -EINVAL);
2313         assert_return(memfd, -EINVAL);
2314         assert_return(!m->sealed, -EPERM);
2315         assert_return(!m->poisoned, -ESTALE);
2316
2317         r = sd_memfd_set_sealed(memfd, true);
2318         if (r < 0)
2319                 return r;
2320
2321         copy_fd = sd_memfd_dup_fd(memfd);
2322         if (copy_fd < 0)
2323                 return copy_fd;
2324
2325         r = sd_memfd_get_size(memfd, &size);
2326         if (r < 0)
2327                 return r;
2328
2329         /* We require this to be NUL terminated */
2330         if (size == 0)
2331                 return -EINVAL;
2332
2333         if (size > (uint64_t) (uint32_t) -1)
2334                 return -EINVAL;
2335
2336         c = message_get_container(m);
2337         if (c->signature && c->signature[c->index]) {
2338                 /* Container signature is already set */
2339
2340                 if (c->signature[c->index] != SD_BUS_TYPE_STRING)
2341                         return -ENXIO;
2342         } else {
2343                 char *e;
2344
2345                 /* Maybe we can append to the signature? But only if this is the top-level container*/
2346                 if (c->enclosing != 0)
2347                         return -ENXIO;
2348
2349                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
2350                 if (!e) {
2351                         m->poisoned = true;
2352                         return -ENOMEM;
2353                 }
2354         }
2355
2356         a = message_extend_body(m, 4, 4);
2357         if (!a)
2358                 return -ENOMEM;
2359
2360         *(uint32_t*) a = size - 1;
2361
2362         part = message_append_part(m);
2363         if (!part)
2364                 return -ENOMEM;
2365
2366         part->memfd = copy_fd;
2367         part->sealed = true;
2368         part->size = size;
2369         copy_fd = -1;
2370
2371         message_extend_containers(m, size);
2372         m->header->body_size += size;
2373
2374         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2375                 c->index++;
2376
2377         return 0;
2378 }
2379
2380 _public_ int sd_bus_message_append_strv(sd_bus_message *m, char **l) {
2381         char **i;
2382         int r;
2383
2384         assert_return(m, -EINVAL);
2385         assert_return(!m->sealed, -EPERM);
2386         assert_return(!m->poisoned, -ESTALE);
2387
2388         r = sd_bus_message_open_container(m, 'a', "s");
2389         if (r < 0)
2390                 return r;
2391
2392         STRV_FOREACH(i, l) {
2393                 r = sd_bus_message_append_basic(m, 's', *i);
2394                 if (r < 0)
2395                         return r;
2396         }
2397
2398         return sd_bus_message_close_container(m);
2399 }
2400
2401 int bus_body_part_map(struct bus_body_part *part) {
2402         void *p;
2403         size_t psz;
2404
2405         assert_se(part);
2406
2407         if (part->data)
2408                 return 0;
2409
2410         if (part->size <= 0)
2411                 return 0;
2412
2413         /* For smaller zero parts (as used for padding) we don't need to map anything... */
2414         if (part->memfd < 0 && part->is_zero && part->size < 8) {
2415                 static const uint8_t zeroes[7] = { };
2416                 part->data = (void*) zeroes;
2417                 return 0;
2418         }
2419
2420         psz = PAGE_ALIGN(part->size);
2421
2422         if (part->memfd >= 0)
2423                 p = mmap(NULL, psz, PROT_READ, MAP_SHARED, part->memfd, 0);
2424         else if (part->is_zero)
2425                 p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
2426         else
2427                 return -EINVAL;
2428
2429         if (p == MAP_FAILED)
2430                 return -errno;
2431
2432         part->mapped = psz;
2433         part->data = p;
2434         part->munmap_this = true;
2435
2436         return 0;
2437 }
2438
2439 void bus_body_part_unmap(struct bus_body_part *part) {
2440
2441         assert_se(part);
2442
2443         if (part->memfd < 0)
2444                 return;
2445
2446         if (!part->data)
2447                 return;
2448
2449         if (!part->munmap_this)
2450                 return;
2451
2452         assert_se(munmap(part->data, part->mapped) == 0);
2453
2454         part->data = NULL;
2455         part->mapped = 0;
2456         part->munmap_this = false;
2457
2458         return;
2459 }
2460
2461 static int buffer_peek(const void *p, uint32_t sz, size_t *rindex, size_t align, size_t nbytes, void **r) {
2462         size_t k, start, end;
2463
2464         assert(rindex);
2465         assert(align > 0);
2466
2467         start = ALIGN_TO((size_t) *rindex, align);
2468         end = start + nbytes;
2469
2470         if (end > sz)
2471                 return -EBADMSG;
2472
2473         /* Verify that padding is 0 */
2474         for (k = *rindex; k < start; k++)
2475                 if (((const uint8_t*) p)[k] != 0)
2476                         return -EBADMSG;
2477
2478         if (r)
2479                 *r = (uint8_t*) p + start;
2480
2481         *rindex = end;
2482
2483         return 1;
2484 }
2485
2486 static bool message_end_of_signature(sd_bus_message *m) {
2487         struct bus_container *c;
2488
2489         assert(m);
2490
2491         c = message_get_container(m);
2492         return !c->signature || c->signature[c->index] == 0;
2493 }
2494
2495 static bool message_end_of_array(sd_bus_message *m, size_t index) {
2496         struct bus_container *c;
2497
2498         assert(m);
2499
2500         c = message_get_container(m);
2501         if (!c->array_size)
2502                 return false;
2503
2504         return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size);
2505 }
2506
2507 _public_ int sd_bus_message_at_end(sd_bus_message *m, int complete) {
2508         assert_return(m, -EINVAL);
2509         assert_return(m->sealed, -EPERM);
2510
2511         if (complete && m->n_containers > 0)
2512                 return false;
2513
2514         if (message_end_of_signature(m))
2515                 return true;
2516
2517         if (message_end_of_array(m, m->rindex))
2518                 return true;
2519
2520         return false;
2521 }
2522
2523 static struct bus_body_part* find_part(sd_bus_message *m, size_t index, size_t sz, void **p) {
2524         struct bus_body_part *part;
2525         size_t begin;
2526         int r;
2527
2528         assert(m);
2529
2530         if (m->cached_rindex_part && index >= m->cached_rindex_part_begin) {
2531                 part = m->cached_rindex_part;
2532                 begin = m->cached_rindex_part_begin;
2533         } else {
2534                 part = &m->body;
2535                 begin = 0;
2536         }
2537
2538         while (part) {
2539                 if (index < begin)
2540                         return NULL;
2541
2542                 if (index + sz <= begin + part->size) {
2543
2544                         r = bus_body_part_map(part);
2545                         if (r < 0)
2546                                 return NULL;
2547
2548                         if (p)
2549                                 *p = (uint8_t*) part->data + index - begin;
2550
2551                         m->cached_rindex_part = part;
2552                         m->cached_rindex_part_begin = begin;
2553
2554                         return part;
2555                 }
2556
2557                 begin += part->size;
2558                 part = part->next;
2559         }
2560
2561         return NULL;
2562 }
2563
2564 static int message_peek_body(
2565                 sd_bus_message *m,
2566                 size_t *rindex,
2567                 size_t align,
2568                 size_t nbytes,
2569                 void **ret) {
2570
2571         size_t k, start, end, padding;
2572         struct bus_body_part *part;
2573         uint8_t *q;
2574
2575         assert(m);
2576         assert(rindex);
2577         assert(align > 0);
2578
2579         if (message_end_of_array(m, *rindex))
2580                 return 0;
2581
2582         start = ALIGN_TO((size_t) *rindex, align);
2583         padding = start - *rindex;
2584         end = start + nbytes;
2585
2586         if (end > BUS_MESSAGE_BODY_SIZE(m))
2587                 return -EBADMSG;
2588
2589         part = find_part(m, *rindex, padding, (void**) &q);
2590         if (!part)
2591                 return -EBADMSG;
2592
2593         if (q) {
2594                 /* Verify padding */
2595                 for (k = 0; k < padding; k++)
2596                         if (q[k] != 0)
2597                                 return -EBADMSG;
2598         }
2599
2600         part = find_part(m, start, nbytes, (void**) &q);
2601         if (!part || !q)
2602                 return -EBADMSG;
2603
2604         *rindex = end;
2605
2606         if (ret)
2607                 *ret = q;
2608
2609         return 1;
2610 }
2611
2612 static bool validate_nul(const char *s, size_t l) {
2613
2614         /* Check for NUL chars in the string */
2615         if (memchr(s, 0, l))
2616                 return false;
2617
2618         /* Check for NUL termination */
2619         if (s[l] != 0)
2620                 return false;
2621
2622         return true;
2623 }
2624
2625 static bool validate_string(const char *s, size_t l) {
2626
2627         if (!validate_nul(s, l))
2628                 return false;
2629
2630         /* Check if valid UTF8 */
2631         if (!utf8_is_valid(s))
2632                 return false;
2633
2634         return true;
2635 }
2636
2637 static bool validate_signature(const char *s, size_t l) {
2638
2639         if (!validate_nul(s, l))
2640                 return false;
2641
2642         /* Check if valid signature */
2643         if (!signature_is_valid(s, true))
2644                 return false;
2645
2646         return true;
2647 }
2648
2649 static bool validate_object_path(const char *s, size_t l) {
2650
2651         if (!validate_nul(s, l))
2652                 return false;
2653
2654         if (!object_path_is_valid(s))
2655                 return false;
2656
2657         return true;
2658 }
2659
2660 _public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
2661         struct bus_container *c;
2662         void *q;
2663         int r;
2664
2665         assert_return(m, -EINVAL);
2666         assert_return(m->sealed, -EPERM);
2667         assert_return(bus_type_is_basic(type), -EINVAL);
2668
2669         if (message_end_of_signature(m))
2670                 return -ENXIO;
2671
2672         if (message_end_of_array(m, m->rindex))
2673                 return 0;
2674
2675         c = message_get_container(m);
2676         if (c->signature[c->index] != type)
2677                 return -ENXIO;
2678
2679         switch (type) {
2680
2681         case SD_BUS_TYPE_STRING:
2682         case SD_BUS_TYPE_OBJECT_PATH: {
2683                 uint32_t l;
2684                 size_t rindex;
2685
2686                 rindex = m->rindex;
2687                 r = message_peek_body(m, &rindex, 4, 4, &q);
2688                 if (r <= 0)
2689                         return r;
2690
2691                 l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2692                 r = message_peek_body(m, &rindex, 1, l+1, &q);
2693                 if (r < 0)
2694                         return r;
2695                 if (r == 0)
2696                         return -EBADMSG;
2697
2698                 if (type == SD_BUS_TYPE_OBJECT_PATH) {
2699                         if (!validate_object_path(q, l))
2700                                 return -EBADMSG;
2701                 } else {
2702                         if (!validate_string(q, l))
2703                                 return -EBADMSG;
2704                 }
2705
2706                 m->rindex = rindex;
2707                 if (p)
2708                         *(const char**) p = q;
2709
2710                 break;
2711         }
2712
2713         case SD_BUS_TYPE_SIGNATURE: {
2714                 uint8_t l;
2715                 size_t rindex;
2716
2717                 rindex = m->rindex;
2718                 r = message_peek_body(m, &rindex, 1, 1, &q);
2719                 if (r <= 0)
2720                         return r;
2721
2722                 l = *(uint8_t*) q;
2723                 r = message_peek_body(m, &rindex, 1, l+1, &q);
2724                 if (r < 0)
2725                         return r;
2726                 if (r == 0)
2727                         return -EBADMSG;
2728
2729                 if (!validate_signature(q, l))
2730                         return -EBADMSG;
2731
2732                 m->rindex = rindex;
2733
2734                 if (p)
2735                         *(const char**) p = q;
2736                 break;
2737         }
2738
2739         default: {
2740                 ssize_t sz, align;
2741                 size_t rindex;
2742
2743                 align = bus_type_get_alignment(type);
2744                 sz = bus_type_get_size(type);
2745                 assert(align > 0 && sz > 0);
2746
2747                 rindex = m->rindex;
2748                 r = message_peek_body(m, &rindex, align, sz, &q);
2749                 if (r <= 0)
2750                         return r;
2751
2752                 switch (type) {
2753
2754                 case SD_BUS_TYPE_BYTE:
2755                         if (p)
2756                                 *(uint8_t*) p = *(uint8_t*) q;
2757                         break;
2758
2759                 case SD_BUS_TYPE_BOOLEAN:
2760                         if (p)
2761                                 *(int*) p = !!*(uint32_t*) q;
2762                         break;
2763
2764                 case SD_BUS_TYPE_INT16:
2765                 case SD_BUS_TYPE_UINT16:
2766                         if (p)
2767                                 *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
2768                         break;
2769
2770                 case SD_BUS_TYPE_INT32:
2771                 case SD_BUS_TYPE_UINT32:
2772                         if (p)
2773                                 *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2774                         break;
2775
2776                 case SD_BUS_TYPE_INT64:
2777                 case SD_BUS_TYPE_UINT64:
2778                 case SD_BUS_TYPE_DOUBLE:
2779                         if (p)
2780                                 *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
2781                         break;
2782
2783                 case SD_BUS_TYPE_UNIX_FD: {
2784                         uint32_t j;
2785
2786                         j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2787                         if (j >= m->n_fds)
2788                                 return -EBADMSG;
2789
2790                         if (p)
2791                                 *(int*) p = m->fds[j];
2792                         break;
2793                 }
2794
2795                 default:
2796                         assert_not_reached("Unknown basic type...");
2797                 }
2798
2799                 m->rindex = rindex;
2800
2801                 break;
2802         }
2803         }
2804
2805         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2806                 c->index++;
2807
2808         return 1;
2809 }
2810
2811 static int bus_message_enter_array(
2812                 sd_bus_message *m,
2813                 struct bus_container *c,
2814                 const char *contents,
2815                 uint32_t **array_size) {
2816
2817         size_t rindex;
2818         void *q;
2819         int r, alignment;
2820
2821         assert(m);
2822         assert(c);
2823         assert(contents);
2824         assert(array_size);
2825
2826         if (!signature_is_single(contents, true))
2827                 return -EINVAL;
2828
2829         alignment = bus_type_get_alignment(contents[0]);
2830         if (alignment < 0)
2831                 return alignment;
2832
2833         if (!c->signature || c->signature[c->index] == 0)
2834                 return 0;
2835
2836         if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
2837                 return -ENXIO;
2838
2839         if (!startswith(c->signature + c->index + 1, contents))
2840                 return -ENXIO;
2841
2842         rindex = m->rindex;
2843         r = message_peek_body(m, &rindex, 4, 4, &q);
2844         if (r <= 0)
2845                 return r;
2846
2847         if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > BUS_ARRAY_MAX_SIZE)
2848                 return -EBADMSG;
2849
2850         r = message_peek_body(m, &rindex, alignment, 0, NULL);
2851         if (r < 0)
2852                 return r;
2853         if (r == 0)
2854                 return -EBADMSG;
2855
2856         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2857                 c->index += 1 + strlen(contents);
2858
2859         m->rindex = rindex;
2860
2861         *array_size = (uint32_t*) q;
2862
2863         return 1;
2864 }
2865
2866 static int bus_message_enter_variant(
2867                 sd_bus_message *m,
2868                 struct bus_container *c,
2869                 const char *contents) {
2870
2871         size_t rindex;
2872         uint8_t l;
2873         void *q;
2874         int r;
2875
2876         assert(m);
2877         assert(c);
2878         assert(contents);
2879
2880         if (!signature_is_single(contents, false))
2881                 return -EINVAL;
2882
2883         if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
2884                 return -EINVAL;
2885
2886         if (!c->signature || c->signature[c->index] == 0)
2887                 return 0;
2888
2889         if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
2890                 return -ENXIO;
2891
2892         rindex = m->rindex;
2893         r = message_peek_body(m, &rindex, 1, 1, &q);
2894         if (r <= 0)
2895                 return r;
2896
2897         l = *(uint8_t*) q;
2898         r = message_peek_body(m, &rindex, 1, l+1, &q);
2899         if (r < 0)
2900                 return r;
2901         if (r == 0)
2902                 return -EBADMSG;
2903
2904         if (!validate_signature(q, l))
2905                 return -EBADMSG;
2906
2907         if (!streq(q, contents))
2908                 return -ENXIO;
2909
2910         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2911                 c->index++;
2912
2913         m->rindex = rindex;
2914
2915         return 1;
2916 }
2917
2918 static int bus_message_enter_struct(
2919                 sd_bus_message *m,
2920                 struct bus_container *c,
2921                 const char *contents) {
2922
2923         size_t l;
2924         int r;
2925
2926         assert(m);
2927         assert(c);
2928         assert(contents);
2929
2930         if (!signature_is_valid(contents, false))
2931                 return -EINVAL;
2932
2933         if (!c->signature || c->signature[c->index] == 0)
2934                 return 0;
2935
2936         l = strlen(contents);
2937
2938         if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
2939             !startswith(c->signature + c->index + 1, contents) ||
2940             c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
2941                 return -ENXIO;
2942
2943         r = message_peek_body(m, &m->rindex, 8, 0, NULL);
2944         if (r <= 0)
2945                 return r;
2946
2947         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2948                 c->index += 1 + l + 1;
2949
2950         return 1;
2951 }
2952
2953 static int bus_message_enter_dict_entry(
2954                 sd_bus_message *m,
2955                 struct bus_container *c,
2956                 const char *contents) {
2957
2958         size_t l;
2959         int r;
2960
2961         assert(m);
2962         assert(c);
2963         assert(contents);
2964
2965         if (!signature_is_pair(contents))
2966                 return -EINVAL;
2967
2968         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2969                 return -ENXIO;
2970
2971         if (!c->signature || c->signature[c->index] == 0)
2972                 return 0;
2973
2974         l = strlen(contents);
2975
2976         if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
2977             !startswith(c->signature + c->index + 1, contents) ||
2978             c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
2979                 return -ENXIO;
2980
2981         r = message_peek_body(m, &m->rindex, 8, 0, NULL);
2982         if (r <= 0)
2983                 return r;
2984
2985         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2986                 c->index += 1 + l + 1;
2987
2988         return 1;
2989 }
2990
2991 _public_ int sd_bus_message_enter_container(sd_bus_message *m,
2992                                             char type,
2993                                             const char *contents) {
2994         struct bus_container *c, *w;
2995         uint32_t *array_size = NULL;
2996         char *signature;
2997         size_t before;
2998         int r;
2999
3000         assert_return(m, -EINVAL);
3001         assert_return(m->sealed, -EPERM);
3002         assert_return(type != 0 || !contents, -EINVAL);
3003
3004         if (type == 0 || !contents) {
3005                 const char *cc;
3006                 char tt;
3007
3008                 /* Allow entering into anonymous containers */
3009                 r = sd_bus_message_peek_type(m, &tt, &cc);
3010                 if (r <= 0)
3011                         return r;
3012
3013                 if (type != 0 && type != tt)
3014                         return -ENXIO;
3015
3016                 if (contents && !streq(contents, cc))
3017                         return -ENXIO;
3018
3019                 type = tt;
3020                 contents = cc;
3021         }
3022
3023         /*
3024          * We enforce a global limit on container depth, that is much
3025          * higher than the 32 structs and 32 arrays the specification
3026          * mandates. This is simpler to implement for us, and we need
3027          * this only to ensure our container array doesn't grow
3028          * without bounds. We are happy to return any data from a
3029          * message as long as the data itself is valid, even if the
3030          * overall message might be not.
3031          *
3032          * Note that the message signature is validated when
3033          * parsing the headers, and that validation does check the
3034          * 32/32 limit.
3035          *
3036          * Note that the specification defines no limits on the depth
3037          * of stacked variants, but we do.
3038          */
3039         if (m->n_containers >= BUS_CONTAINER_DEPTH)
3040                 return -EBADMSG;
3041
3042         w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
3043         if (!w)
3044                 return -ENOMEM;
3045         m->containers = w;
3046
3047         if (message_end_of_signature(m))
3048                 return -ENXIO;
3049
3050         if (message_end_of_array(m, m->rindex))
3051                 return 0;
3052
3053         c = message_get_container(m);
3054
3055         signature = strdup(contents);
3056         if (!signature)
3057                 return -ENOMEM;
3058
3059         c->saved_index = c->index;
3060         before = m->rindex;
3061
3062         if (type == SD_BUS_TYPE_ARRAY)
3063                 r = bus_message_enter_array(m, c, contents, &array_size);
3064         else if (type == SD_BUS_TYPE_VARIANT)
3065                 r = bus_message_enter_variant(m, c, contents);
3066         else if (type == SD_BUS_TYPE_STRUCT)
3067                 r = bus_message_enter_struct(m, c, contents);
3068         else if (type == SD_BUS_TYPE_DICT_ENTRY)
3069                 r = bus_message_enter_dict_entry(m, c, contents);
3070         else
3071                 r = -EINVAL;
3072
3073         if (r <= 0) {
3074                 free(signature);
3075                 return r;
3076         }
3077
3078         /* OK, let's fill it in */
3079         w += m->n_containers++;
3080         w->enclosing = type;
3081         w->signature = signature;
3082         w->index = 0;
3083         w->array_size = array_size;
3084         w->before = before;
3085         w->begin = m->rindex;
3086
3087         return 1;
3088 }
3089
3090 _public_ int sd_bus_message_exit_container(sd_bus_message *m) {
3091         struct bus_container *c;
3092
3093         assert_return(m, -EINVAL);
3094         assert_return(m->sealed, -EPERM);
3095         assert_return(m->n_containers > 0, -ENXIO);
3096
3097         c = message_get_container(m);
3098         if (c->enclosing == SD_BUS_TYPE_ARRAY) {
3099                 uint32_t l;
3100
3101                 l = BUS_MESSAGE_BSWAP32(m, *c->array_size);
3102                 if (c->begin + l != m->rindex)
3103                         return -EBUSY;
3104
3105         } else {
3106                 if (c->signature && c->signature[c->index] != 0)
3107                         return -EBUSY;
3108         }
3109
3110         free(c->signature);
3111         m->n_containers--;
3112
3113         return 1;
3114 }
3115
3116 static void message_quit_container(sd_bus_message *m) {
3117         struct bus_container *c;
3118
3119         assert(m);
3120         assert(m->sealed);
3121         assert(m->n_containers > 0);
3122
3123         c = message_get_container(m);
3124
3125         /* Undo seeks */
3126         assert(m->rindex >= c->before);
3127         m->rindex = c->before;
3128
3129         /* Free container */
3130         free(c->signature);
3131         m->n_containers--;
3132
3133         /* Correct index of new top-level container */
3134         c = message_get_container(m);
3135         c->index = c->saved_index;
3136 }
3137
3138 _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents) {
3139         struct bus_container *c;
3140         int r;
3141
3142         assert_return(m, -EINVAL);
3143         assert_return(m->sealed, -EPERM);
3144
3145         if (message_end_of_signature(m))
3146                 goto eof;
3147
3148         if (message_end_of_array(m, m->rindex))
3149                 goto eof;
3150
3151         c = message_get_container(m);
3152
3153         if (bus_type_is_basic(c->signature[c->index])) {
3154                 if (contents)
3155                         *contents = NULL;
3156                 if (type)
3157                         *type = c->signature[c->index];
3158                 return 1;
3159         }
3160
3161         if (c->signature[c->index] == SD_BUS_TYPE_ARRAY) {
3162
3163                 if (contents) {
3164                         size_t l;
3165                         char *sig;
3166
3167                         r = signature_element_length(c->signature+c->index+1, &l);
3168                         if (r < 0)
3169                                 return r;
3170
3171                         assert(l >= 1);
3172
3173                         sig = strndup(c->signature + c->index + 1, l);
3174                         if (!sig)
3175                                 return -ENOMEM;
3176
3177                         free(m->peeked_signature);
3178                         m->peeked_signature = sig;
3179
3180                         *contents = sig;
3181                 }
3182
3183                 if (type)
3184                         *type = SD_BUS_TYPE_ARRAY;
3185
3186                 return 1;
3187         }
3188
3189         if (c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ||
3190             c->signature[c->index] == SD_BUS_TYPE_DICT_ENTRY_BEGIN) {
3191
3192                 if (contents) {
3193                         size_t l;
3194                         char *sig;
3195
3196                         r = signature_element_length(c->signature+c->index, &l);
3197                         if (r < 0)
3198                                 return r;
3199
3200                         assert(l >= 2);
3201                         sig = strndup(c->signature + c->index + 1, l - 2);
3202                         if (!sig)
3203                                 return -ENOMEM;
3204
3205                         free(m->peeked_signature);
3206                         m->peeked_signature = sig;
3207
3208                         *contents = sig;
3209                 }
3210
3211                 if (type)
3212                         *type = c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY;
3213
3214                 return 1;
3215         }
3216
3217         if (c->signature[c->index] == SD_BUS_TYPE_VARIANT) {
3218                 if (contents) {
3219                         size_t rindex, l;
3220                         void *q;
3221
3222                         rindex = m->rindex;
3223                         r = message_peek_body(m, &rindex, 1, 1, &q);
3224                         if (r < 0)
3225                                 return r;
3226                         if (r == 0)
3227                                 goto eof;
3228
3229                         l = *(uint8_t*) q;
3230                         r = message_peek_body(m, &rindex, 1, l+1, &q);
3231                         if (r < 0)
3232                                 return r;
3233                         if (r == 0)
3234                                 return -EBADMSG;
3235
3236                         if (!validate_signature(q, l))
3237                                 return -EBADMSG;
3238
3239                         *contents = q;
3240                 }
3241
3242                 if (type)
3243                         *type = SD_BUS_TYPE_VARIANT;
3244
3245                 return 1;
3246         }
3247
3248         return -EINVAL;
3249
3250 eof:
3251         if (type)
3252                 *type = 0;
3253         if (contents)
3254                 *contents = NULL;
3255         return 0;
3256 }
3257
3258 _public_ int sd_bus_message_rewind(sd_bus_message *m, int complete) {
3259         struct bus_container *c;
3260
3261         assert_return(m, -EINVAL);
3262         assert_return(m->sealed, -EPERM);
3263
3264         if (complete) {
3265                 message_reset_containers(m);
3266                 m->rindex = 0;
3267                 m->root_container.index = 0;
3268
3269                 c = message_get_container(m);
3270         } else {
3271                 c = message_get_container(m);
3272
3273                 c->index = 0;
3274                 m->rindex = c->begin;
3275         }
3276
3277         return !isempty(c->signature);
3278 }
3279 static int message_read_ap(
3280                 sd_bus_message *m,
3281                 const char *types,
3282                 va_list ap) {
3283
3284         unsigned n_array, n_struct;
3285         TypeStack stack[BUS_CONTAINER_DEPTH];
3286         unsigned stack_ptr = 0;
3287         unsigned n_loop = 0;
3288         int r;
3289
3290         assert(m);
3291
3292         if (isempty(types))
3293                 return 0;
3294
3295         /* Ideally, we'd just call ourselves recursively on every
3296          * complex type. However, the state of a va_list that is
3297          * passed to a function is undefined after that function
3298          * returns. This means we need to docode the va_list linearly
3299          * in a single stackframe. We hence implement our own
3300          * home-grown stack in an array. */
3301
3302         n_array = (unsigned) -1; /* lenght of current array entries */
3303         n_struct = strlen(types); /* length of current struct contents signature */
3304
3305         for (;;) {
3306                 const char *t;
3307
3308                 n_loop++;
3309
3310                 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
3311                         r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
3312                         if (r < 0)
3313                                 return r;
3314                         if (r == 0)
3315                                 break;
3316
3317                         r = sd_bus_message_exit_container(m);
3318                         if (r < 0)
3319                                 return r;
3320
3321                         continue;
3322                 }
3323
3324                 t = types;
3325                 if (n_array != (unsigned) -1)
3326                         n_array --;
3327                 else {
3328                         types ++;
3329                         n_struct--;
3330                 }
3331
3332                 switch (*t) {
3333
3334                 case SD_BUS_TYPE_BYTE:
3335                 case SD_BUS_TYPE_BOOLEAN:
3336                 case SD_BUS_TYPE_INT16:
3337                 case SD_BUS_TYPE_UINT16:
3338                 case SD_BUS_TYPE_INT32:
3339                 case SD_BUS_TYPE_UINT32:
3340                 case SD_BUS_TYPE_INT64:
3341                 case SD_BUS_TYPE_UINT64:
3342                 case SD_BUS_TYPE_DOUBLE:
3343                 case SD_BUS_TYPE_STRING:
3344                 case SD_BUS_TYPE_OBJECT_PATH:
3345                 case SD_BUS_TYPE_SIGNATURE:
3346                 case SD_BUS_TYPE_UNIX_FD: {
3347                         void *p;
3348
3349                         p = va_arg(ap, void*);
3350                         r = sd_bus_message_read_basic(m, *t, p);
3351                         if (r < 0)
3352                                 return r;
3353                         if (r == 0) {
3354                                 if (n_loop <= 1)
3355                                         return 0;
3356
3357                                 return -ENXIO;
3358                         }
3359
3360                         break;
3361                 }
3362
3363                 case SD_BUS_TYPE_ARRAY: {
3364                         size_t k;
3365
3366                         r = signature_element_length(t + 1, &k);
3367                         if (r < 0)
3368                                 return r;
3369
3370                         {
3371                                 char s[k + 1];
3372                                 memcpy(s, t + 1, k);
3373                                 s[k] = 0;
3374
3375                                 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
3376                                 if (r < 0)
3377                                         return r;
3378                                 if (r == 0) {
3379                                         if (n_loop <= 1)
3380                                                 return 0;
3381
3382                                         return -ENXIO;
3383                                 }
3384                         }
3385
3386                         if (n_array == (unsigned) -1) {
3387                                 types += k;
3388                                 n_struct -= k;
3389                         }
3390
3391                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
3392                         if (r < 0)
3393                                 return r;
3394
3395                         types = t + 1;
3396                         n_struct = k;
3397                         n_array = va_arg(ap, unsigned);
3398
3399                         break;
3400                 }
3401
3402                 case SD_BUS_TYPE_VARIANT: {
3403                         const char *s;
3404
3405                         s = va_arg(ap, const char *);
3406                         if (!s)
3407                                 return -EINVAL;
3408
3409                         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, s);
3410                         if (r < 0)
3411                                 return r;
3412                         if (r == 0) {
3413                                 if (n_loop <= 1)
3414                                         return 0;
3415
3416                                 return -ENXIO;
3417                         }
3418
3419                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
3420                         if (r < 0)
3421                                 return r;
3422
3423                         types = s;
3424                         n_struct = strlen(s);
3425                         n_array = (unsigned) -1;
3426
3427                         break;
3428                 }
3429
3430                 case SD_BUS_TYPE_STRUCT_BEGIN:
3431                 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
3432                         size_t k;
3433
3434                         r = signature_element_length(t, &k);
3435                         if (r < 0)
3436                                 return r;
3437
3438                         {
3439                                 char s[k - 1];
3440                                 memcpy(s, t + 1, k - 2);
3441                                 s[k - 2] = 0;
3442
3443                                 r = sd_bus_message_enter_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
3444                                 if (r < 0)
3445                                         return r;
3446                                 if (r == 0) {
3447                                         if (n_loop <= 1)
3448                                                 return 0;
3449                                         return -ENXIO;
3450                                 }
3451                         }
3452
3453                         if (n_array == (unsigned) -1) {
3454                                 types += k - 1;
3455                                 n_struct -= k - 1;
3456                         }
3457
3458                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
3459                         if (r < 0)
3460                                 return r;
3461
3462                         types = t + 1;
3463                         n_struct = k - 2;
3464                         n_array = (unsigned) -1;
3465
3466                         break;
3467                 }
3468
3469                 default:
3470                         return -EINVAL;
3471                 }
3472         }
3473
3474         return 1;
3475 }
3476
3477 _public_ int sd_bus_message_read(sd_bus_message *m, const char *types, ...) {
3478         va_list ap;
3479         int r;
3480
3481         assert_return(m, -EINVAL);
3482         assert_return(m->sealed, -EPERM);
3483         assert_return(types, -EINVAL);
3484
3485         va_start(ap, types);
3486         r = message_read_ap(m, types, ap);
3487         va_end(ap);
3488
3489         return r;
3490 }
3491
3492 _public_ int sd_bus_message_skip(sd_bus_message *m, const char *types) {
3493         int r;
3494
3495         assert_return(m, -EINVAL);
3496         assert_return(m->sealed, -EPERM);
3497         assert_return(types, -EINVAL);
3498
3499         if (isempty(types))
3500                 return 0;
3501
3502         switch (*types) {
3503
3504         case SD_BUS_TYPE_BYTE:
3505         case SD_BUS_TYPE_BOOLEAN:
3506         case SD_BUS_TYPE_INT16:
3507         case SD_BUS_TYPE_UINT16:
3508         case SD_BUS_TYPE_INT32:
3509         case SD_BUS_TYPE_UINT32:
3510         case SD_BUS_TYPE_INT64:
3511         case SD_BUS_TYPE_UINT64:
3512         case SD_BUS_TYPE_DOUBLE:
3513         case SD_BUS_TYPE_STRING:
3514         case SD_BUS_TYPE_OBJECT_PATH:
3515         case SD_BUS_TYPE_SIGNATURE:
3516         case SD_BUS_TYPE_UNIX_FD:
3517
3518                 r = sd_bus_message_read_basic(m, *types, NULL);
3519                 if (r <= 0)
3520                         return r;
3521
3522                 r = sd_bus_message_skip(m, types + 1);
3523                 if (r < 0)
3524                         return r;
3525
3526                 return 1;
3527
3528         case SD_BUS_TYPE_ARRAY: {
3529                 size_t k;
3530
3531                 r = signature_element_length(types + 1, &k);
3532                 if (r < 0)
3533                         return r;
3534
3535                 {
3536                         char s[k+1];
3537                         memcpy(s, types+1, k);
3538                         s[k] = 0;
3539
3540                         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
3541                         if (r <= 0)
3542                                 return r;
3543
3544                         for (;;) {
3545                                 r = sd_bus_message_skip(m, s);
3546                                 if (r < 0)
3547                                         return r;
3548                                 if (r == 0)
3549                                         break;
3550                         }
3551
3552                         r = sd_bus_message_exit_container(m);
3553                         if (r < 0)
3554                                 return r;
3555                 }
3556
3557                 r = sd_bus_message_skip(m, types + 1 + k);
3558                 if (r < 0)
3559                         return r;
3560
3561                 return 1;
3562         }
3563
3564         case SD_BUS_TYPE_VARIANT: {
3565                 const char *contents;
3566                 char x;
3567
3568                 r = sd_bus_message_peek_type(m, &x, &contents);
3569                 if (r <= 0)
3570                         return r;
3571
3572                 if (x != SD_BUS_TYPE_VARIANT)
3573                         return -ENXIO;
3574
3575                 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
3576                 if (r <= 0)
3577                         return r;
3578
3579                 r = sd_bus_message_skip(m, contents);
3580                 if (r < 0)
3581                         return r;
3582                 assert(r != 0);
3583
3584                 r = sd_bus_message_exit_container(m);
3585                 if (r < 0)
3586                         return r;
3587
3588                 r = sd_bus_message_skip(m, types + 1);
3589                 if (r < 0)
3590                         return r;
3591
3592                 return 1;
3593         }
3594
3595         case SD_BUS_TYPE_STRUCT_BEGIN:
3596         case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
3597                 size_t k;
3598
3599                 r = signature_element_length(types, &k);
3600                 if (r < 0)
3601                         return r;
3602
3603                 {
3604                         char s[k-1];
3605                         memcpy(s, types+1, k-2);
3606                         s[k-2] = 0;
3607
3608                         r = sd_bus_message_enter_container(m, *types == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
3609                         if (r <= 0)
3610                                 return r;
3611
3612                         r = sd_bus_message_skip(m, s);
3613                         if (r < 0)
3614                                 return r;
3615                         assert(r != 0);
3616
3617                         r = sd_bus_message_exit_container(m);
3618                         if (r < 0)
3619                                 return r;
3620                 }
3621
3622                 r = sd_bus_message_skip(m, types + k);
3623                 if (r < 0)
3624                         return r;
3625
3626                 return 1;
3627         }
3628
3629         default:
3630                 return -EINVAL;
3631         }
3632 }
3633
3634 _public_ int sd_bus_message_read_array(sd_bus_message *m,
3635                                        char type,
3636                                        const void **ptr,
3637                                        size_t *size) {
3638         struct bus_container *c;
3639         void *p;
3640         size_t sz;
3641         ssize_t align;
3642         int r;
3643
3644         assert_return(m, -EINVAL);
3645         assert_return(m->sealed, -EPERM);
3646         assert_return(bus_type_is_trivial(type), -EINVAL);
3647         assert_return(ptr, -EINVAL);
3648         assert_return(size, -EINVAL);
3649         assert_return(!BUS_MESSAGE_NEED_BSWAP(m), -ENOTSUP);
3650
3651         align = bus_type_get_alignment(type);
3652         if (align < 0)
3653                 return align;
3654
3655         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
3656         if (r <= 0)
3657                 return r;
3658
3659         c = message_get_container(m);
3660         sz = BUS_MESSAGE_BSWAP32(m, *c->array_size);
3661
3662         if (sz == 0)
3663                 /* Zero length array, let's return some aligned
3664                  * pointer that is not NULL */
3665                 p = (uint8_t*) NULL + align;
3666         else {
3667                 r = message_peek_body(m, &m->rindex, align, sz, &p);
3668                 if (r < 0)
3669                         goto fail;
3670                 if (r == 0) {
3671                         r = -EBADMSG;
3672                         goto fail;
3673                 }
3674         }
3675
3676         r = sd_bus_message_exit_container(m);
3677         if (r < 0)
3678                 goto fail;
3679
3680         *ptr = (const void*) p;
3681         *size = sz;
3682
3683         return 1;
3684
3685 fail:
3686         message_quit_container(m);
3687         return r;
3688 }
3689
3690 static int message_peek_fields(
3691                 sd_bus_message *m,
3692                 size_t *rindex,
3693                 size_t align,
3694                 size_t nbytes,
3695                 void **ret) {
3696
3697         assert(m);
3698         assert(rindex);
3699         assert(align > 0);
3700
3701         return buffer_peek(BUS_MESSAGE_FIELDS(m), BUS_MESSAGE_FIELDS_SIZE(m), rindex, align, nbytes, ret);
3702 }
3703
3704 static int message_peek_field_uint32(
3705                 sd_bus_message *m,
3706                 size_t *ri,
3707                 uint32_t *ret) {
3708
3709         int r;
3710         void *q;
3711
3712         assert(m);
3713         assert(ri);
3714
3715         r = message_peek_fields(m, ri, 4, 4, &q);
3716         if (r < 0)
3717                 return r;
3718
3719         if (ret)
3720                 *ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
3721
3722         return 0;
3723 }
3724
3725 static int message_peek_field_string(
3726                 sd_bus_message *m,
3727                 bool (*validate)(const char *p),
3728                 size_t *ri,
3729                 const char **ret) {
3730
3731         uint32_t l;
3732         int r;
3733         void *q;
3734
3735         assert(m);
3736         assert(ri);
3737
3738         r = message_peek_field_uint32(m, ri, &l);
3739         if (r < 0)
3740                 return r;
3741
3742         r = message_peek_fields(m, ri, 1, l+1, &q);
3743         if (r < 0)
3744                 return r;
3745
3746         if (validate) {
3747                 if (!validate_nul(q, l))
3748                         return -EBADMSG;
3749
3750                 if (!validate(q))
3751                         return -EBADMSG;
3752         } else {
3753                 if (!validate_string(q, l))
3754                         return -EBADMSG;
3755         }
3756
3757         if (ret)
3758                 *ret = q;
3759
3760         return 0;
3761 }
3762
3763 static int message_peek_field_signature(
3764                 sd_bus_message *m,
3765                 size_t *ri,
3766                 const char **ret) {
3767
3768         size_t l;
3769         int r;
3770         void *q;
3771
3772         assert(m);
3773         assert(ri);
3774
3775         r = message_peek_fields(m, ri, 1, 1, &q);
3776         if (r < 0)
3777                 return r;
3778
3779         l = *(uint8_t*) q;
3780         r = message_peek_fields(m, ri, 1, l+1, &q);
3781         if (r < 0)
3782                 return r;
3783
3784         if (!validate_signature(q, l))
3785                 return -EBADMSG;
3786
3787         if (ret)
3788                 *ret = q;
3789
3790         return 0;
3791 }
3792
3793 static int message_skip_fields(
3794                 sd_bus_message *m,
3795                 size_t *ri,
3796                 uint32_t array_size,
3797                 const char **signature) {
3798
3799         size_t original_index;
3800         int r;
3801
3802         assert(m);
3803         assert(ri);
3804         assert(signature);
3805
3806         original_index = *ri;
3807
3808         for (;;) {
3809                 char t;
3810                 size_t l;
3811
3812                 if (array_size != (uint32_t) -1 &&
3813                     array_size <= *ri - original_index)
3814                         return 0;
3815
3816                 t = **signature;
3817                 if (!t)
3818                         return 0;
3819
3820                 if (t == SD_BUS_TYPE_STRING) {
3821
3822                         r = message_peek_field_string(m, NULL, ri, NULL);
3823                         if (r < 0)
3824                                 return r;
3825
3826                         (*signature)++;
3827
3828                 } else if (t == SD_BUS_TYPE_OBJECT_PATH) {
3829
3830                         r = message_peek_field_string(m, object_path_is_valid, ri, NULL);
3831                         if (r < 0)
3832                                 return r;
3833
3834                         (*signature)++;
3835
3836                 } else if (t == SD_BUS_TYPE_SIGNATURE) {
3837
3838                         r = message_peek_field_signature(m, ri, NULL);
3839                         if (r < 0)
3840                                 return r;
3841
3842                         (*signature)++;
3843
3844                 } else if (bus_type_is_basic(t)) {
3845                         ssize_t align, k;
3846
3847                         align = bus_type_get_alignment(t);
3848                         k = bus_type_get_size(t);
3849                         assert(align > 0 && k > 0);
3850
3851                         r = message_peek_fields(m, ri, align, k, NULL);
3852                         if (r < 0)
3853                                 return r;
3854
3855                         (*signature)++;
3856
3857                 } else if (t == SD_BUS_TYPE_ARRAY) {
3858
3859                         r = signature_element_length(*signature+1, &l);
3860                         if (r < 0)
3861                                 return r;
3862
3863                         assert(l >= 1);
3864                         {
3865                                 char sig[l-1], *s;
3866                                 uint32_t nas;
3867                                 int alignment;
3868
3869                                 strncpy(sig, *signature + 1, l-1);
3870                                 s = sig;
3871
3872                                 alignment = bus_type_get_alignment(sig[0]);
3873                                 if (alignment < 0)
3874                                         return alignment;
3875
3876                                 r = message_peek_field_uint32(m, ri, &nas);
3877                                 if (r < 0)
3878                                         return r;
3879                                 if (nas > BUS_ARRAY_MAX_SIZE)
3880                                         return -EBADMSG;
3881
3882                                 r = message_peek_fields(m, ri, alignment, 0, NULL);
3883                                 if (r < 0)
3884                                         return r;
3885
3886                                 r = message_skip_fields(m, ri, nas, (const char**) &s);
3887                                 if (r < 0)
3888                                         return r;
3889                         }
3890
3891                         (*signature) += 1 + l;
3892
3893                 } else if (t == SD_BUS_TYPE_VARIANT) {
3894                         const char *s;
3895
3896                         r = message_peek_field_signature(m, ri, &s);
3897                         if (r < 0)
3898                                 return r;
3899
3900                         r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
3901                         if (r < 0)
3902                                 return r;
3903
3904                         (*signature)++;
3905
3906                 } else if (t == SD_BUS_TYPE_STRUCT ||
3907                            t == SD_BUS_TYPE_DICT_ENTRY) {
3908
3909                         r = signature_element_length(*signature, &l);
3910                         if (r < 0)
3911                                 return r;
3912
3913                         assert(l >= 2);
3914                         {
3915                                 char sig[l-1], *s;
3916                                 strncpy(sig, *signature + 1, l-1);
3917                                 s = sig;
3918
3919                                 r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
3920                                 if (r < 0)
3921                                         return r;
3922                         }
3923
3924                         *signature += l;
3925                 } else
3926                         return -EINVAL;
3927         }
3928 }
3929
3930 int bus_message_parse_fields(sd_bus_message *m) {
3931         size_t ri;
3932         int r;
3933         uint32_t unix_fds = 0;
3934
3935         assert(m);
3936
3937         for (ri = 0; ri < BUS_MESSAGE_FIELDS_SIZE(m); ) {
3938                 const char *signature;
3939                 uint8_t *header;
3940
3941                 r = message_peek_fields(m, &ri, 8, 1, (void**) &header);
3942                 if (r < 0)
3943                         return r;
3944
3945                 r = message_peek_field_signature(m, &ri, &signature);
3946                 if (r < 0)
3947                         return r;
3948
3949                 switch (*header) {
3950                 case _SD_BUS_MESSAGE_HEADER_INVALID:
3951                         return -EBADMSG;
3952
3953                 case SD_BUS_MESSAGE_HEADER_PATH:
3954
3955                         if (m->path)
3956                                 return -EBADMSG;
3957
3958                         if (!streq(signature, "o"))
3959                                 return -EBADMSG;
3960
3961                         r = message_peek_field_string(m, object_path_is_valid, &ri, &m->path);
3962                         break;
3963
3964                 case SD_BUS_MESSAGE_HEADER_INTERFACE:
3965
3966                         if (m->interface)
3967                                 return -EBADMSG;
3968
3969                         if (!streq(signature, "s"))
3970                                 return -EBADMSG;
3971
3972                         r = message_peek_field_string(m, interface_name_is_valid, &ri, &m->interface);
3973                         break;
3974
3975                 case SD_BUS_MESSAGE_HEADER_MEMBER:
3976
3977                         if (m->member)
3978                                 return -EBADMSG;
3979
3980                         if (!streq(signature, "s"))
3981                                 return -EBADMSG;
3982
3983                         r = message_peek_field_string(m, member_name_is_valid, &ri, &m->member);
3984                         break;
3985
3986                 case SD_BUS_MESSAGE_HEADER_ERROR_NAME:
3987
3988                         if (m->error.name)
3989                                 return -EBADMSG;
3990
3991                         if (!streq(signature, "s"))
3992                                 return -EBADMSG;
3993
3994                         r = message_peek_field_string(m, error_name_is_valid, &ri, &m->error.name);
3995                         break;
3996
3997                 case SD_BUS_MESSAGE_HEADER_DESTINATION:
3998
3999                         if (m->destination)
4000                                 return -EBADMSG;
4001
4002                         if (!streq(signature, "s"))
4003                                 return -EBADMSG;
4004
4005                         r = message_peek_field_string(m, sender_name_is_valid, &ri, &m->destination);
4006                         break;
4007
4008                 case SD_BUS_MESSAGE_HEADER_SENDER:
4009
4010                         if (m->sender)
4011                                 return -EBADMSG;
4012
4013                         if (!streq(signature, "s"))
4014                                 return -EBADMSG;
4015
4016                         r = message_peek_field_string(m, sender_name_is_valid, &ri, &m->sender);
4017                         break;
4018
4019
4020                 case SD_BUS_MESSAGE_HEADER_SIGNATURE: {
4021                         const char *s;
4022                         char *c;
4023
4024                         if (m->root_container.signature)
4025                                 return -EBADMSG;
4026
4027                         if (!streq(signature, "g"))
4028                                 return -EBADMSG;
4029
4030                         r = message_peek_field_signature(m, &ri, &s);
4031                         if (r < 0)
4032                                 return r;
4033
4034                         c = strdup(s);
4035                         if (!c)
4036                                 return -ENOMEM;
4037
4038                         free(m->root_container.signature);
4039                         m->root_container.signature = c;
4040                         break;
4041                 }
4042
4043                 case SD_BUS_MESSAGE_HEADER_REPLY_SERIAL:
4044                         if (m->reply_serial != 0)
4045                                 return -EBADMSG;
4046
4047                         if (!streq(signature, "u"))
4048                                 return -EBADMSG;
4049
4050                         r = message_peek_field_uint32(m, &ri, &m->reply_serial);
4051                         if (r < 0)
4052                                 return r;
4053
4054                         if (m->reply_serial == 0)
4055                                 return -EBADMSG;
4056
4057                         break;
4058
4059                 case SD_BUS_MESSAGE_HEADER_UNIX_FDS:
4060                         if (unix_fds != 0)
4061                                 return -EBADMSG;
4062
4063                         if (!streq(signature, "u"))
4064                                 return -EBADMSG;
4065
4066                         r = message_peek_field_uint32(m, &ri, &unix_fds);
4067                         if (r < 0)
4068                                 return -EBADMSG;
4069
4070                         if (unix_fds == 0)
4071                                 return -EBADMSG;
4072
4073                         break;
4074
4075                 default:
4076                         r = message_skip_fields(m, &ri, (uint32_t) -1, (const char **) &signature);
4077                 }
4078
4079                 if (r < 0)
4080                         return r;
4081         }
4082
4083         if (m->n_fds != unix_fds)
4084                 return -EBADMSG;
4085
4086         if (isempty(m->root_container.signature) != (BUS_MESSAGE_BODY_SIZE(m) == 0))
4087                 return -EBADMSG;
4088
4089         switch (m->header->type) {
4090
4091         case SD_BUS_MESSAGE_SIGNAL:
4092                 if (!m->path || !m->interface || !m->member)
4093                         return -EBADMSG;
4094                 break;
4095
4096         case SD_BUS_MESSAGE_METHOD_CALL:
4097
4098                 if (!m->path || !m->member)
4099                         return -EBADMSG;
4100
4101                 break;
4102
4103         case SD_BUS_MESSAGE_METHOD_RETURN:
4104
4105                 if (m->reply_serial == 0)
4106                         return -EBADMSG;
4107                 break;
4108
4109         case SD_BUS_MESSAGE_METHOD_ERROR:
4110
4111                 if (m->reply_serial == 0 || !m->error.name)
4112                         return -EBADMSG;
4113                 break;
4114         }
4115
4116         /* Try to read the error message, but if we can't it's a non-issue */
4117         if (m->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
4118                 sd_bus_message_read(m, "s", &m->error.message);
4119
4120         return 0;
4121 }
4122
4123 int bus_message_seal(sd_bus_message *m, uint64_t serial) {
4124         struct bus_body_part *part;
4125         size_t l, a;
4126         unsigned i;
4127         int r;
4128
4129         assert(m);
4130
4131         if (m->sealed)
4132                 return -EPERM;
4133
4134         if (m->n_containers > 0)
4135                 return -EBADMSG;
4136
4137         if (m->poisoned)
4138                 return -ESTALE;
4139
4140         /* If there's a non-trivial signature set, then add it in here */
4141         if (!isempty(m->root_container.signature)) {
4142                 r = message_append_field_signature(m, SD_BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, NULL);
4143                 if (r < 0)
4144                         return r;
4145         }
4146
4147         if (m->n_fds > 0) {
4148                 r = message_append_field_uint32(m, SD_BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds);
4149                 if (r < 0)
4150                         return r;
4151         }
4152
4153         /* Add padding at the end of the fields part, since we know
4154          * the body needs to start at an 8 byte alignment. We made
4155          * sure we allocated enough space for this, so all we need to
4156          * do here is to zero it out. */
4157         l = BUS_MESSAGE_FIELDS_SIZE(m);
4158         a = ALIGN8(l) - l;
4159         if (a > 0)
4160                 memset((uint8_t*) BUS_MESSAGE_FIELDS(m) + l, 0, a);
4161
4162         /* If this is something we can send as memfd, then let's seal
4163         the memfd now. Note that we can send memfds as payload only
4164         for directed messages, and not for broadcasts. */
4165         if (m->destination && m->bus && m->bus->use_memfd) {
4166                 MESSAGE_FOREACH_PART(part, i, m)
4167                         if (part->memfd >= 0 && !part->sealed && (part->size > MEMFD_MIN_SIZE || m->bus->use_memfd < 0)) {
4168                                 bus_body_part_unmap(part);
4169
4170                                 if (ioctl(part->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) >= 0)
4171                                         part->sealed = true;
4172                         }
4173         }
4174
4175         m->header->serial = serial;
4176         m->sealed = true;
4177
4178         return 0;
4179 }
4180
4181 _public_ int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) {
4182         assert_return(m, -EINVAL);
4183         assert_return(destination, -EINVAL);
4184         assert_return(!m->sealed, -EPERM);
4185         assert_return(!m->destination, -EEXIST);
4186
4187         return message_append_field_string(m, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
4188 }
4189
4190 int bus_message_dump(sd_bus_message *m, FILE *f, bool with_header) {
4191         const char *u = NULL, *uu = NULL, *s = NULL;
4192         char **cmdline = NULL;
4193         unsigned level = 1;
4194         int r;
4195         uid_t owner, audit_loginuid;
4196         uint32_t audit_sessionid;
4197
4198         assert(m);
4199
4200         if (!f)
4201                 f = stdout;
4202
4203         if (with_header) {
4204                 fprintf(f,
4205                         "Message %p\n"
4206                         "\tn_ref=%u\n"
4207                         "\tendian=%c\n"
4208                         "\ttype=%i\n"
4209                         "\tflags=%u\n"
4210                         "\tversion=%u\n"
4211                         "\tserial=%u\n"
4212                         "\tfields_size=%u\n"
4213                         "\tbody_size=%u\n"
4214                         "\tpath=%s\n"
4215                         "\tinterface=%s\n"
4216                         "\tmember=%s\n"
4217                         "\tdestination=%s\n"
4218                         "\tsender=%s\n"
4219                         "\tsignature=%s\n"
4220                         "\treply_serial=%u\n"
4221                         "\tsealed=%s\n"
4222                         "\tn_body_parts=%u\n",
4223                         m,
4224                         m->n_ref,
4225                         m->header->endian,
4226                         m->header->type,
4227                         m->header->flags,
4228                         m->header->version,
4229                         BUS_MESSAGE_SERIAL(m),
4230                         BUS_MESSAGE_FIELDS_SIZE(m),
4231                         BUS_MESSAGE_BODY_SIZE(m),
4232                         strna(m->path),
4233                         strna(m->interface),
4234                         strna(m->member),
4235                         strna(m->destination),
4236                         strna(m->sender),
4237                         strna(m->root_container.signature),
4238                         m->reply_serial,
4239                         yes_no(m->sealed),
4240                         m->n_body_parts);
4241
4242                 if (sd_bus_error_is_set(&m->error))
4243                         fprintf(f,
4244                                 "\terror.name=%s\n"
4245                                 "\terror.message=%s\n",
4246                                 strna(m->error.name),
4247                                 strna(m->error.message));
4248
4249                 if (m->pid != 0)
4250                         fprintf(f, "\tpid=%lu\n", (unsigned long) m->pid);
4251                 if (m->tid != 0)
4252                         fprintf(f, "\ttid=%lu\n", (unsigned long) m->tid);
4253                 if (m->uid_valid)
4254                         fprintf(f, "\tuid=%lu\n", (unsigned long) m->uid);
4255                 if (m->gid_valid)
4256                         fprintf(f, "\tgid=%lu\n", (unsigned long) m->gid);
4257                 if (m->pid_starttime != 0)
4258                         fprintf(f, "\tpid_starttime=%llu\n", (unsigned long long) m->pid_starttime);
4259                 if (m->monotonic != 0)
4260                         fprintf(f, "\tmonotonic=%llu\n", (unsigned long long) m->monotonic);
4261                 if (m->realtime != 0)
4262                         fprintf(f, "\trealtime=%llu\n", (unsigned long long) m->realtime);
4263                 if (m->exe)
4264                         fprintf(f, "\texe=[%s]\n", m->exe);
4265                 if (m->comm)
4266                         fprintf(f, "\tcomm=[%s]\n", m->comm);
4267                 if (m->tid_comm)
4268                         fprintf(f, "\ttid_comm=[%s]\n", m->tid_comm);
4269                 if (m->label)
4270                         fprintf(f, "\tlabel=[%s]\n", m->label);
4271                 if (m->cgroup)
4272                         fprintf(f, "\tcgroup=[%s]\n", m->cgroup);
4273
4274                 sd_bus_message_get_unit(m, &u);
4275                 if (u)
4276                         fprintf(f, "\tunit=[%s]\n", u);
4277                 sd_bus_message_get_user_unit(m, &uu);
4278                 if (uu)
4279                         fprintf(f, "\tuser_unit=[%s]\n", uu);
4280                 sd_bus_message_get_session(m, &s);
4281                 if (s)
4282                         fprintf(f, "\tsession=[%s]\n", s);
4283                 if (sd_bus_message_get_owner_uid(m, &owner) >= 0)
4284                         fprintf(f, "\towner_uid=%lu\n", (unsigned long) owner);
4285                 if (sd_bus_message_get_audit_loginuid(m, &audit_loginuid) >= 0)
4286                         fprintf(f, "\taudit_loginuid=%lu\n", (unsigned long) audit_loginuid);
4287                 if (sd_bus_message_get_audit_sessionid(m, &audit_sessionid) >= 0)
4288                         fprintf(f, "\taudit_sessionid=%lu\n", (unsigned long) audit_sessionid);
4289
4290                 r = sd_bus_message_has_effective_cap(m, 5);
4291                 if (r >= 0)
4292                         fprintf(f, "\tCAP_KILL=%s\n", yes_no(r));
4293
4294                 if (sd_bus_message_get_cmdline(m, &cmdline) >= 0) {
4295                         char **c;
4296
4297                         fputs("\tcmdline=[", f);
4298                         STRV_FOREACH(c, cmdline) {
4299                                 if (c != cmdline)
4300                                         fputc(' ', f);
4301
4302                                 fputs(*c, f);
4303                         }
4304
4305                         fputs("]\n", f);
4306                 }
4307         }
4308
4309         r = sd_bus_message_rewind(m, true);
4310         if (r < 0) {
4311                 log_error("Failed to rewind: %s", strerror(-r));
4312                 return r;
4313         }
4314
4315         fprintf(f, "MESSAGE \"%s\" {\n", strempty(m->root_container.signature));
4316
4317         for(;;) {
4318                 _cleanup_free_ char *prefix = NULL;
4319                 const char *contents = NULL;
4320                 char type;
4321                 union {
4322                         uint8_t u8;
4323                         uint16_t u16;
4324                         int16_t s16;
4325                         uint32_t u32;
4326                         int32_t s32;
4327                         uint64_t u64;
4328                         int64_t s64;
4329                         double d64;
4330                         const char *string;
4331                         int i;
4332                 } basic;
4333
4334                 r = sd_bus_message_peek_type(m, &type, &contents);
4335                 if (r < 0) {
4336                         log_error("Failed to peek type: %s", strerror(-r));
4337                         return r;
4338                 }
4339
4340                 if (r == 0) {
4341                         if (level <= 1)
4342                                 break;
4343
4344                         r = sd_bus_message_exit_container(m);
4345                         if (r < 0) {
4346                                 log_error("Failed to exit container: %s", strerror(-r));
4347                                 return r;
4348                         }
4349
4350                         level--;
4351
4352                         prefix = strrep("\t", level);
4353                         if (!prefix)
4354                                 return log_oom();
4355
4356                         fprintf(f, "%s};\n", prefix);
4357                         continue;
4358                 }
4359
4360                 prefix = strrep("\t", level);
4361                 if (!prefix)
4362                         return log_oom();
4363
4364                 if (bus_type_is_container(type) > 0) {
4365                         r = sd_bus_message_enter_container(m, type, contents);
4366                         if (r < 0) {
4367                                 log_error("Failed to enter container: %s", strerror(-r));
4368                                 return r;
4369                         }
4370
4371                         if (type == SD_BUS_TYPE_ARRAY)
4372                                 fprintf(f, "%sARRAY \"%s\" {\n", prefix, contents);
4373                         else if (type == SD_BUS_TYPE_VARIANT)
4374                                 fprintf(f, "%sVARIANT \"%s\" {\n", prefix, contents);
4375                         else if (type == SD_BUS_TYPE_STRUCT)
4376                                 fprintf(f, "%sSTRUCT \"%s\" {\n", prefix, contents);
4377                         else if (type == SD_BUS_TYPE_DICT_ENTRY)
4378                                 fprintf(f, "%sDICT_ENTRY \"%s\" {\n", prefix, contents);
4379
4380                         level ++;
4381
4382                         continue;
4383                 }
4384
4385                 r = sd_bus_message_read_basic(m, type, &basic);
4386                 if (r < 0) {
4387                         log_error("Failed to get basic: %s", strerror(-r));
4388                         return r;
4389                 }
4390
4391                 assert(r > 0);
4392
4393                 switch (type) {
4394
4395                 case SD_BUS_TYPE_BYTE:
4396                         fprintf(f, "%sBYTE %u;\n", prefix, basic.u8);
4397                         break;
4398
4399                 case SD_BUS_TYPE_BOOLEAN:
4400                         fprintf(f, "%sBOOLEAN %s;\n", prefix, yes_no(basic.i));
4401                         break;
4402
4403                 case SD_BUS_TYPE_INT16:
4404                         fprintf(f, "%sINT16 %i;\n", prefix, basic.s16);
4405                         break;
4406
4407                 case SD_BUS_TYPE_UINT16:
4408                         fprintf(f, "%sUINT16 %u;\n", prefix, basic.u16);
4409                         break;
4410
4411                 case SD_BUS_TYPE_INT32:
4412                         fprintf(f, "%sINT32 %i;\n", prefix, basic.s32);
4413                         break;
4414
4415                 case SD_BUS_TYPE_UINT32:
4416                         fprintf(f, "%sUINT32 %u;\n", prefix, basic.u32);
4417                         break;
4418
4419                 case SD_BUS_TYPE_INT64:
4420                         fprintf(f, "%sINT64 %lli;\n", prefix, (long long) basic.s64);
4421                         break;
4422
4423                 case SD_BUS_TYPE_UINT64:
4424                         fprintf(f, "%sUINT64 %llu;\n", prefix, (unsigned long long) basic.u64);
4425                         break;
4426
4427                 case SD_BUS_TYPE_DOUBLE:
4428                         fprintf(f, "%sDOUBLE %g;\n", prefix, basic.d64);
4429                         break;
4430
4431                 case SD_BUS_TYPE_STRING:
4432                         fprintf(f, "%sSTRING \"%s\";\n", prefix, basic.string);
4433                         break;
4434
4435                 case SD_BUS_TYPE_OBJECT_PATH:
4436                         fprintf(f, "%sOBJECT_PATH \"%s\";\n", prefix, basic.string);
4437                         break;
4438
4439                 case SD_BUS_TYPE_SIGNATURE:
4440                         fprintf(f, "%sSIGNATURE \"%s\";\n", prefix, basic.string);
4441                         break;
4442
4443                 case SD_BUS_TYPE_UNIX_FD:
4444                         fprintf(f, "%sUNIX_FD %i;\n", prefix, basic.i);
4445                         break;
4446
4447                 default:
4448                         assert_not_reached("Unknown basic type.");
4449                 }
4450         }
4451
4452         fprintf(f, "};\n");
4453         return 0;
4454 }
4455
4456 int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
4457         size_t total;
4458         void *p, *e;
4459         unsigned i;
4460         struct bus_body_part *part;
4461
4462         assert(m);
4463         assert(buffer);
4464         assert(sz);
4465
4466         total = BUS_MESSAGE_SIZE(m);
4467
4468         p = malloc(total);
4469         if (!p)
4470                 return -ENOMEM;
4471
4472         e = mempcpy(p, m->header, BUS_MESSAGE_BODY_BEGIN(m));
4473         MESSAGE_FOREACH_PART(part, i, m)
4474                 e = mempcpy(e, part->data, part->size);
4475
4476         assert(total == (size_t) ((uint8_t*) e - (uint8_t*) p));
4477
4478         *buffer = p;
4479         *sz = total;
4480
4481         return 0;
4482 }
4483
4484 int bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
4485         int r;
4486
4487         assert(m);
4488         assert(l);
4489
4490         r = sd_bus_message_enter_container(m, 'a', "s");
4491         if (r <= 0)
4492                 return r;
4493
4494         for (;;) {
4495                 const char *s;
4496
4497                 r = sd_bus_message_read_basic(m, 's', &s);
4498                 if (r < 0)
4499                         return r;
4500                 if (r == 0)
4501                         break;
4502
4503                 r = strv_extend(l, s);
4504                 if (r < 0)
4505                         return r;
4506         }
4507
4508         r = sd_bus_message_exit_container(m);
4509         if (r < 0)
4510                 return r;
4511
4512         return 1;
4513 }
4514
4515 _public_ int sd_bus_message_read_strv(sd_bus_message *m, char ***l) {
4516         char **strv = NULL;
4517         int r;
4518
4519         assert_return(m, -EINVAL);
4520         assert_return(m->sealed, -EPERM);
4521         assert_return(l, -EINVAL);
4522
4523         r = bus_message_read_strv_extend(m, &strv);
4524         if (r <= 0) {
4525                 strv_free(strv);
4526                 return r;
4527         }
4528
4529         *l = strv;
4530         return 1;
4531 }
4532
4533 const char* bus_message_get_arg(sd_bus_message *m, unsigned i) {
4534         int r;
4535         const char *t = NULL;
4536         unsigned j;
4537
4538         assert(m);
4539
4540         r = sd_bus_message_rewind(m, true);
4541         if (r < 0)
4542                 return NULL;
4543
4544         for (j = 0; j <= i; j++) {
4545                 char type;
4546
4547                 r = sd_bus_message_peek_type(m, &type, NULL);
4548                 if (r < 0)
4549                         return NULL;
4550
4551                 if (type != SD_BUS_TYPE_STRING &&
4552                     type != SD_BUS_TYPE_OBJECT_PATH &&
4553                     type != SD_BUS_TYPE_SIGNATURE)
4554                         return NULL;
4555
4556                 r = sd_bus_message_read_basic(m, type, &t);
4557                 if (r < 0)
4558                         return NULL;
4559         }
4560
4561         return t;
4562 }
4563
4564 bool bus_header_is_complete(struct bus_header *h, size_t size) {
4565         size_t full;
4566
4567         assert(h);
4568         assert(size);
4569
4570         if (size < sizeof(struct bus_header))
4571                 return false;
4572
4573         full = sizeof(struct bus_header) +
4574                 (h->endian == SD_BUS_NATIVE_ENDIAN ? h->fields_size : bswap_32(h->fields_size));
4575
4576         return size >= full;
4577 }
4578
4579 int bus_header_message_size(struct bus_header *h, size_t *sum) {
4580         size_t fs, bs;
4581
4582         assert(h);
4583         assert(sum);
4584
4585         if (h->endian == SD_BUS_NATIVE_ENDIAN) {
4586                 fs = h->fields_size;
4587                 bs = h->body_size;
4588         } else if (h->endian == SD_BUS_REVERSE_ENDIAN) {
4589                 fs = bswap_32(h->fields_size);
4590                 bs = bswap_32(h->body_size);
4591         } else
4592                 return -EBADMSG;
4593
4594         *sum = sizeof(struct bus_header) + ALIGN8(fs) + bs;
4595         return 0;
4596 }
4597
4598 _public_ int sd_bus_message_get_errno(sd_bus_message *m) {
4599         assert_return(m, -EINVAL);
4600
4601         if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
4602                 return 0;
4603
4604         return sd_bus_error_get_errno(&m->error);
4605 }
4606
4607 _public_ const char* sd_bus_message_get_signature(sd_bus_message *m, int complete) {
4608         struct bus_container *c;
4609
4610         assert_return(m, NULL);
4611
4612         c = complete ? &m->root_container : message_get_container(m);
4613         return c->signature ?: "";
4614 }
4615
4616 _public_ int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int all) {
4617         bool done_something = false;
4618         int r;
4619
4620         assert_return(m, -EINVAL);
4621         assert_return(source, -EINVAL);
4622         assert_return(!m->sealed, -EPERM);
4623         assert_return(source->sealed, -EPERM);
4624
4625         do {
4626                 const char *contents;
4627                 char type;
4628                 union {
4629                         uint8_t u8;
4630                         uint16_t u16;
4631                         int16_t s16;
4632                         uint32_t u32;
4633                         int32_t s32;
4634                         uint64_t u64;
4635                         int64_t s64;
4636                         double d64;
4637                         const char *string;
4638                         int i;
4639                 } basic;
4640
4641                 r = sd_bus_message_peek_type(source, &type, &contents);
4642                 if (r < 0)
4643                         return r;
4644                 if (r == 0)
4645                         break;
4646
4647                 done_something = true;
4648
4649                 if (bus_type_is_container(type) > 0) {
4650
4651                         r = sd_bus_message_enter_container(source, type, contents);
4652                         if (r < 0)
4653                                 return r;
4654
4655                         r = sd_bus_message_open_container(m, type, contents);
4656                         if (r < 0)
4657                                 return r;
4658
4659                         r = sd_bus_message_copy(m, source, true);
4660                         if (r < 0)
4661                                 return r;
4662
4663                         r = sd_bus_message_close_container(m);
4664                         if (r < 0)
4665                                 return r;
4666
4667                         r = sd_bus_message_exit_container(source);
4668                         if (r < 0)
4669                                 return r;
4670
4671                         continue;
4672                 }
4673
4674                 r = sd_bus_message_read_basic(source, type, &basic);
4675                 if (r < 0)
4676                         return r;
4677
4678                 assert(r > 0);
4679
4680                 if (type == SD_BUS_TYPE_OBJECT_PATH ||
4681                     type == SD_BUS_TYPE_SIGNATURE ||
4682                     type == SD_BUS_TYPE_STRING)
4683                         r = sd_bus_message_append_basic(m, type, basic.string);
4684                 else
4685                         r = sd_bus_message_append_basic(m, type, &basic);
4686
4687                 if (r < 0)
4688                         return r;
4689
4690         } while (all);
4691
4692         return done_something;
4693 }
4694
4695 _public_ int sd_bus_message_verify_type(sd_bus_message *m, char type, const char *contents) {
4696         const char *c;
4697         char t;
4698         int r;
4699
4700         assert_return(m, -EINVAL);
4701         assert_return(m->sealed, -EPERM);
4702         assert_return(!type || bus_type_is_valid(type), -EINVAL);
4703         assert_return(!contents || signature_is_valid(contents, true), -EINVAL);
4704         assert_return(type || contents, -EINVAL);
4705         assert_return(!contents || !type || bus_type_is_container(type), -EINVAL);
4706
4707         r = sd_bus_message_peek_type(m, &t, &c);
4708         if (r <= 0)
4709                 return r;
4710
4711         if (type != 0 && type != t)
4712                 return 0;
4713
4714         if (contents && !streq_ptr(contents, c))
4715                 return 0;
4716
4717         return 1;
4718 }