chiark / gitweb /
4b53a6c5095bd793661f0f896d37b5eebb09cbc7
[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                 if (!p) {
1383                         r = -EINVAL;
1384                         goto fail;
1385                 }
1386
1387                 align = sz = 4;
1388
1389                 assert_cc(sizeof(int) == sizeof(uint32_t));
1390                 memcpy(&k, p, 4);
1391                 k = !!k;
1392                 p = &k;
1393                 break;
1394
1395         case SD_BUS_TYPE_UNIX_FD: {
1396                 int z, *f;
1397
1398                 if (!p) {
1399                         r = -EINVAL;
1400                         goto fail;
1401                 }
1402
1403                 if (!m->allow_fds) {
1404                         r = -ENOTSUP;
1405                         goto fail;
1406                 }
1407
1408                 align = sz = 4;
1409
1410                 z = *(int*) p;
1411                 if (z < 0) {
1412                         r = -EINVAL;
1413                         goto fail;
1414                 }
1415
1416                 fd = fcntl(z, F_DUPFD_CLOEXEC, 3);
1417                 if (fd < 0) {
1418                         r = -errno;
1419                         goto fail;
1420                 }
1421
1422                 f = realloc(m->fds, sizeof(int) * (m->n_fds + 1));
1423                 if (!f) {
1424                         m->poisoned = true;
1425                         r = -ENOMEM;
1426                         goto fail;
1427                 }
1428
1429                 fdi = m->n_fds;
1430                 f[fdi] = fd;
1431                 m->fds = f;
1432                 m->free_fds = true;
1433                 break;
1434         }
1435
1436         default:
1437                 align = bus_type_get_alignment(type);
1438                 sz = bus_type_get_size(type);
1439                 break;
1440         }
1441
1442         assert(align > 0);
1443         assert(sz > 0);
1444
1445         a = message_extend_body(m, align, sz);
1446         if (!a) {
1447                 r = -ENOMEM;
1448                 goto fail;
1449         }
1450
1451         if (type == SD_BUS_TYPE_STRING || type == SD_BUS_TYPE_OBJECT_PATH) {
1452                 *(uint32_t*) a = sz - 5;
1453                 memcpy((uint8_t*) a + 4, p, sz - 4);
1454
1455                 if (stored)
1456                         *stored = (const uint8_t*) a + 4;
1457
1458         } else if (type == SD_BUS_TYPE_SIGNATURE) {
1459                 *(uint8_t*) a = sz - 1;
1460                 memcpy((uint8_t*) a + 1, p, sz - 1);
1461
1462                 if (stored)
1463                         *stored = (const uint8_t*) a + 1;
1464         } else if (type == SD_BUS_TYPE_UNIX_FD) {
1465                 *(uint32_t*) a = fdi;
1466
1467                 if (stored)
1468                         *stored = a;
1469
1470                 m->n_fds ++;
1471
1472         } else {
1473                 memcpy(a, p, sz);
1474
1475                 if (stored)
1476                         *stored = a;
1477         }
1478
1479         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1480                 c->index++;
1481
1482         return 0;
1483
1484 fail:
1485         if (fd >= 0)
1486                 close_nointr_nofail(fd);
1487
1488         return r;
1489 }
1490
1491 _public_ int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) {
1492         return message_append_basic(m, type, p, NULL);
1493 }
1494
1495 _public_ int sd_bus_message_append_string_space(sd_bus_message *m, size_t size, char **s) {
1496         struct bus_container *c;
1497         void *a;
1498
1499         assert_return(m, -EINVAL);
1500         assert_return(s, -EINVAL);
1501         assert_return(!m->sealed, -EPERM);
1502         assert_return(!m->poisoned, -ESTALE);
1503
1504         c = message_get_container(m);
1505
1506         if (c->signature && c->signature[c->index]) {
1507                 /* Container signature is already set */
1508
1509                 if (c->signature[c->index] != SD_BUS_TYPE_STRING)
1510                         return -ENXIO;
1511         } else {
1512                 char *e;
1513
1514                 /* Maybe we can append to the signature? But only if this is the top-level container*/
1515                 if (c->enclosing != 0)
1516                         return -ENXIO;
1517
1518                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
1519                 if (!e) {
1520                         m->poisoned = true;
1521                         return -ENOMEM;
1522                 }
1523         }
1524
1525         a = message_extend_body(m, 4, 4 + size + 1);
1526         if (!a)
1527                 return -ENOMEM;
1528
1529         *(uint32_t*) a = size;
1530         *s = (char*) a + 4;
1531
1532         (*s)[size] = 0;
1533
1534         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1535                 c->index++;
1536
1537         return 0;
1538 }
1539
1540 static int bus_message_open_array(
1541                 sd_bus_message *m,
1542                 struct bus_container *c,
1543                 const char *contents,
1544                 uint32_t **array_size) {
1545
1546         unsigned nindex;
1547         void *a, *op;
1548         int alignment;
1549         size_t os;
1550         struct bus_body_part *o;
1551
1552         assert(m);
1553         assert(c);
1554         assert(contents);
1555         assert(array_size);
1556
1557         if (!signature_is_single(contents, true))
1558                 return -EINVAL;
1559
1560         alignment = bus_type_get_alignment(contents[0]);
1561         if (alignment < 0)
1562                 return alignment;
1563
1564         if (c->signature && c->signature[c->index]) {
1565
1566                 /* Verify the existing signature */
1567
1568                 if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
1569                         return -ENXIO;
1570
1571                 if (!startswith(c->signature + c->index + 1, contents))
1572                         return -ENXIO;
1573
1574                 nindex = c->index + 1 + strlen(contents);
1575         } else {
1576                 char *e;
1577
1578                 if (c->enclosing != 0)
1579                         return -ENXIO;
1580
1581                 /* Extend the existing signature */
1582
1583                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents, NULL);
1584                 if (!e) {
1585                         m->poisoned = true;
1586                         return -ENOMEM;
1587                 }
1588
1589                 nindex = e - c->signature;
1590         }
1591
1592         a = message_extend_body(m, 4, 4);
1593         if (!a)
1594                 return -ENOMEM;
1595
1596         o = m->body_end;
1597         op = m->body_end->data;
1598         os = m->body_end->size;
1599
1600         /* Add alignment between size and first element */
1601         if (!message_extend_body(m, alignment, 0))
1602                 return -ENOMEM;
1603
1604         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1605                 c->index = nindex;
1606
1607         /* location of array size might have changed so let's readjust a */
1608         if (o == m->body_end)
1609                 a = adjust_pointer(a, op, os, m->body_end->data);
1610
1611         *(uint32_t*) a = 0;
1612         *array_size = a;
1613         return 0;
1614 }
1615
1616 static int bus_message_open_variant(
1617                 sd_bus_message *m,
1618                 struct bus_container *c,
1619                 const char *contents) {
1620
1621         size_t l;
1622         void *a;
1623
1624         assert(m);
1625         assert(c);
1626         assert(contents);
1627
1628         if (!signature_is_single(contents, false))
1629                 return -EINVAL;
1630
1631         if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
1632                 return -EINVAL;
1633
1634         if (c->signature && c->signature[c->index]) {
1635
1636                 if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
1637                         return -ENXIO;
1638
1639         } else {
1640                 char *e;
1641
1642                 if (c->enclosing != 0)
1643                         return -ENXIO;
1644
1645                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT), NULL);
1646                 if (!e) {
1647                         m->poisoned = true;
1648                         return -ENOMEM;
1649                 }
1650         }
1651
1652         l = strlen(contents);
1653         a = message_extend_body(m, 1, 1 + l + 1);
1654         if (!a)
1655                 return -ENOMEM;
1656
1657         *(uint8_t*) a = l;
1658         memcpy((uint8_t*) a + 1, contents, l + 1);
1659
1660         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1661                 c->index++;
1662
1663         return 0;
1664 }
1665
1666 static int bus_message_open_struct(
1667                 sd_bus_message *m,
1668                 struct bus_container *c,
1669                 const char *contents) {
1670
1671         size_t nindex;
1672
1673         assert(m);
1674         assert(c);
1675         assert(contents);
1676
1677         if (!signature_is_valid(contents, false))
1678                 return -EINVAL;
1679
1680         if (c->signature && c->signature[c->index]) {
1681                 size_t l;
1682
1683                 l = strlen(contents);
1684
1685                 if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
1686                     !startswith(c->signature + c->index + 1, contents) ||
1687                     c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
1688                         return -ENXIO;
1689
1690                 nindex = c->index + 1 + l + 1;
1691         } else {
1692                 char *e;
1693
1694                 if (c->enclosing != 0)
1695                         return -ENXIO;
1696
1697                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL);
1698                 if (!e) {
1699                         m->poisoned = true;
1700                         return -ENOMEM;
1701                 }
1702
1703                 nindex = e - c->signature;
1704         }
1705
1706         /* Align contents to 8 byte boundary */
1707         if (!message_extend_body(m, 8, 0))
1708                 return -ENOMEM;
1709
1710         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1711                 c->index = nindex;
1712
1713         return 0;
1714 }
1715
1716 static int bus_message_open_dict_entry(
1717                 sd_bus_message *m,
1718                 struct bus_container *c,
1719                 const char *contents) {
1720
1721         size_t nindex;
1722
1723         assert(m);
1724         assert(c);
1725         assert(contents);
1726
1727         if (!signature_is_pair(contents))
1728                 return -EINVAL;
1729
1730         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1731                 return -ENXIO;
1732
1733         if (c->signature && c->signature[c->index]) {
1734                 size_t l;
1735
1736                 l = strlen(contents);
1737
1738                 if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
1739                     !startswith(c->signature + c->index + 1, contents) ||
1740                     c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
1741                         return -ENXIO;
1742
1743                 nindex = c->index + 1 + l + 1;
1744         } else
1745                 return -ENXIO;
1746
1747         /* Align contents to 8 byte boundary */
1748         if (!message_extend_body(m, 8, 0))
1749                 return -ENOMEM;
1750
1751         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1752                 c->index = nindex;
1753
1754         return 0;
1755 }
1756
1757 _public_ int sd_bus_message_open_container(
1758                 sd_bus_message *m,
1759                 char type,
1760                 const char *contents) {
1761
1762         struct bus_container *c, *w;
1763         uint32_t *array_size = NULL;
1764         char *signature;
1765         size_t before;
1766         int r;
1767
1768         assert_return(m, -EINVAL);
1769         assert_return(!m->sealed, -EPERM);
1770         assert_return(contents, -EINVAL);
1771         assert_return(!m->poisoned, -ESTALE);
1772
1773         /* Make sure we have space for one more container */
1774         w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
1775         if (!w) {
1776                 m->poisoned = true;
1777                 return -ENOMEM;
1778         }
1779
1780         m->containers = w;
1781
1782         c = message_get_container(m);
1783
1784         signature = strdup(contents);
1785         if (!signature) {
1786                 m->poisoned = true;
1787                 return -ENOMEM;
1788         }
1789
1790         /* Save old index in the parent container, in case we have to
1791          * abort this container */
1792         c->saved_index = c->index;
1793         before = m->header->body_size;
1794
1795         if (type == SD_BUS_TYPE_ARRAY)
1796                 r = bus_message_open_array(m, c, contents, &array_size);
1797         else if (type == SD_BUS_TYPE_VARIANT)
1798                 r = bus_message_open_variant(m, c, contents);
1799         else if (type == SD_BUS_TYPE_STRUCT)
1800                 r = bus_message_open_struct(m, c, contents);
1801         else if (type == SD_BUS_TYPE_DICT_ENTRY)
1802                 r = bus_message_open_dict_entry(m, c, contents);
1803         else
1804                 r = -EINVAL;
1805
1806         if (r < 0) {
1807                 free(signature);
1808                 return r;
1809         }
1810
1811         /* OK, let's fill it in */
1812         w += m->n_containers++;
1813         w->enclosing = type;
1814         w->signature = signature;
1815         w->index = 0;
1816         w->array_size = array_size;
1817         w->before = before;
1818         w->begin = m->rindex;
1819
1820         return 0;
1821 }
1822
1823 _public_ int sd_bus_message_close_container(sd_bus_message *m) {
1824         struct bus_container *c;
1825
1826         assert_return(m, -EINVAL);
1827         assert_return(!m->sealed, -EPERM);
1828         assert_return(m->n_containers > 0, -EINVAL);
1829         assert_return(!m->poisoned, -ESTALE);
1830
1831         c = message_get_container(m);
1832         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1833                 if (c->signature && c->signature[c->index] != 0)
1834                         return -EINVAL;
1835
1836         free(c->signature);
1837         m->n_containers--;
1838
1839         return 0;
1840 }
1841
1842 typedef struct {
1843         const char *types;
1844         unsigned n_struct;
1845         unsigned n_array;
1846 } TypeStack;
1847
1848 static int type_stack_push(TypeStack *stack, unsigned max, unsigned *i, const char *types, unsigned n_struct, unsigned n_array) {
1849         assert(stack);
1850         assert(max > 0);
1851
1852         if (*i >= max)
1853                 return -EINVAL;
1854
1855         stack[*i].types = types;
1856         stack[*i].n_struct = n_struct;
1857         stack[*i].n_array = n_array;
1858         (*i)++;
1859
1860         return 0;
1861 }
1862
1863 static int type_stack_pop(TypeStack *stack, unsigned max, unsigned *i, const char **types, unsigned *n_struct, unsigned *n_array) {
1864         assert(stack);
1865         assert(max > 0);
1866         assert(types);
1867         assert(n_struct);
1868         assert(n_array);
1869
1870         if (*i <= 0)
1871                 return 0;
1872
1873         (*i)--;
1874         *types = stack[*i].types;
1875         *n_struct = stack[*i].n_struct;
1876         *n_array = stack[*i].n_array;
1877
1878         return 1;
1879 }
1880
1881 int bus_message_append_ap(
1882                 sd_bus_message *m,
1883                 const char *types,
1884                 va_list ap) {
1885
1886         unsigned n_array, n_struct;
1887         TypeStack stack[BUS_CONTAINER_DEPTH];
1888         unsigned stack_ptr = 0;
1889         int r;
1890
1891         assert(m);
1892
1893         if (!types)
1894                 return 0;
1895
1896         n_array = (unsigned) -1;
1897         n_struct = strlen(types);
1898
1899         for (;;) {
1900                 const char *t;
1901
1902                 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
1903                         r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
1904                         if (r < 0)
1905                                 return r;
1906                         if (r == 0)
1907                                 break;
1908
1909                         r = sd_bus_message_close_container(m);
1910                         if (r < 0)
1911                                 return r;
1912
1913                         continue;
1914                 }
1915
1916                 t = types;
1917                 if (n_array != (unsigned) -1)
1918                         n_array --;
1919                 else {
1920                         types ++;
1921                         n_struct--;
1922                 }
1923
1924                 switch (*t) {
1925
1926                 case SD_BUS_TYPE_BYTE: {
1927                         uint8_t x;
1928
1929                         x = (uint8_t) va_arg(ap, int);
1930                         r = sd_bus_message_append_basic(m, *t, &x);
1931                         break;
1932                 }
1933
1934                 case SD_BUS_TYPE_BOOLEAN:
1935                 case SD_BUS_TYPE_INT32:
1936                 case SD_BUS_TYPE_UINT32:
1937                 case SD_BUS_TYPE_UNIX_FD: {
1938                         uint32_t x;
1939
1940                         /* We assume a boolean is the same as int32_t */
1941                         assert_cc(sizeof(int32_t) == sizeof(int));
1942
1943                         x = va_arg(ap, uint32_t);
1944                         r = sd_bus_message_append_basic(m, *t, &x);
1945                         break;
1946                 }
1947
1948                 case SD_BUS_TYPE_INT16:
1949                 case SD_BUS_TYPE_UINT16: {
1950                         uint16_t x;
1951
1952                         x = (uint16_t) va_arg(ap, int);
1953                         r = sd_bus_message_append_basic(m, *t, &x);
1954                         break;
1955                 }
1956
1957                 case SD_BUS_TYPE_INT64:
1958                 case SD_BUS_TYPE_UINT64:
1959                 case SD_BUS_TYPE_DOUBLE: {
1960                         uint64_t x;
1961
1962                         x = va_arg(ap, uint64_t);
1963                         r = sd_bus_message_append_basic(m, *t, &x);
1964                         break;
1965                 }
1966
1967                 case SD_BUS_TYPE_STRING:
1968                 case SD_BUS_TYPE_OBJECT_PATH:
1969                 case SD_BUS_TYPE_SIGNATURE: {
1970                         const char *x;
1971
1972                         x = va_arg(ap, const char*);
1973                         r = sd_bus_message_append_basic(m, *t, x);
1974                         break;
1975                 }
1976
1977                 case SD_BUS_TYPE_ARRAY: {
1978                         size_t k;
1979
1980                         r = signature_element_length(t + 1, &k);
1981                         if (r < 0)
1982                                 return r;
1983
1984                         {
1985                                 char s[k + 1];
1986                                 memcpy(s, t + 1, k);
1987                                 s[k] = 0;
1988
1989                                 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
1990                                 if (r < 0)
1991                                         return r;
1992                         }
1993
1994                         if (n_array == (unsigned) -1) {
1995                                 types += k;
1996                                 n_struct -= k;
1997                         }
1998
1999                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2000                         if (r < 0)
2001                                 return r;
2002
2003                         types = t + 1;
2004                         n_struct = k;
2005                         n_array = va_arg(ap, unsigned);
2006
2007                         break;
2008                 }
2009
2010                 case SD_BUS_TYPE_VARIANT: {
2011                         const char *s;
2012
2013                         s = va_arg(ap, const char*);
2014                         if (!s)
2015                                 return -EINVAL;
2016
2017                         r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, s);
2018                         if (r < 0)
2019                                 return r;
2020
2021                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2022                         if (r < 0)
2023                                 return r;
2024
2025                         types = s;
2026                         n_struct = strlen(s);
2027                         n_array = (unsigned) -1;
2028
2029                         break;
2030                 }
2031
2032                 case SD_BUS_TYPE_STRUCT_BEGIN:
2033                 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
2034                         size_t k;
2035
2036                         r = signature_element_length(t, &k);
2037                         if (r < 0)
2038                                 return r;
2039
2040                         {
2041                                 char s[k - 1];
2042
2043                                 memcpy(s, t + 1, k - 2);
2044                                 s[k - 2] = 0;
2045
2046                                 r = sd_bus_message_open_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
2047                                 if (r < 0)
2048                                         return r;
2049                         }
2050
2051                         if (n_array == (unsigned) -1) {
2052                                 types += k - 1;
2053                                 n_struct -= k - 1;
2054                         }
2055
2056                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2057                         if (r < 0)
2058                                 return r;
2059
2060                         types = t + 1;
2061                         n_struct = k - 2;
2062                         n_array = (unsigned) -1;
2063
2064                         break;
2065                 }
2066
2067                 default:
2068                         r = -EINVAL;
2069                 }
2070
2071                 if (r < 0)
2072                         return r;
2073         }
2074
2075         return 0;
2076 }
2077
2078 _public_ int sd_bus_message_append(sd_bus_message *m, const char *types, ...) {
2079         va_list ap;
2080         int r;
2081
2082         assert_return(m, -EINVAL);
2083         assert_return(types, -EINVAL);
2084         assert_return(!m->sealed, -EPERM);
2085         assert_return(!m->poisoned, -ESTALE);
2086
2087         va_start(ap, types);
2088         r = bus_message_append_ap(m, types, ap);
2089         va_end(ap);
2090
2091         return r;
2092 }
2093
2094 _public_ int sd_bus_message_append_array_space(sd_bus_message *m,
2095                                                char type,
2096                                                size_t size,
2097                                                void **ptr) {
2098         ssize_t align, sz;
2099         void *a;
2100         int r;
2101
2102         assert_return(m, -EINVAL);
2103         assert_return(!m->sealed, -EPERM);
2104         assert_return(bus_type_is_trivial(type), -EINVAL);
2105         assert_return(ptr || size == 0, -EINVAL);
2106         assert_return(!m->poisoned, -ESTALE);
2107
2108         align = bus_type_get_alignment(type);
2109         sz = bus_type_get_size(type);
2110
2111         assert_se(align > 0);
2112         assert_se(sz > 0);
2113
2114         if (size % sz != 0)
2115                 return -EINVAL;
2116
2117         r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
2118         if (r < 0)
2119                 return r;
2120
2121         a = message_extend_body(m, align, size);
2122         if (!a)
2123                 return -ENOMEM;
2124
2125         r = sd_bus_message_close_container(m);
2126         if (r < 0)
2127                 return r;
2128
2129         *ptr = a;
2130         return 0;
2131 }
2132
2133 _public_ int sd_bus_message_append_array(sd_bus_message *m,
2134                                          char type,
2135                                          const void *ptr,
2136                                          size_t size) {
2137         int r;
2138         void *p;
2139
2140         assert_return(m, -EINVAL);
2141         assert_return(!m->sealed, -EPERM);
2142         assert_return(bus_type_is_trivial(type), -EINVAL);
2143         assert_return(ptr || size == 0, -EINVAL);
2144         assert_return(!m->poisoned, -ESTALE);
2145
2146         r = sd_bus_message_append_array_space(m, type, size, &p);
2147         if (r < 0)
2148                 return r;
2149
2150         if (size > 0)
2151                 memcpy(p, ptr, size);
2152
2153         return 0;
2154 }
2155
2156 _public_ int sd_bus_message_append_array_memfd(sd_bus_message *m,
2157                                                char type,
2158                                                sd_memfd *memfd) {
2159         _cleanup_close_ int copy_fd = -1;
2160         struct bus_body_part *part;
2161         ssize_t align, sz;
2162         uint64_t size;
2163         void *a;
2164         int r;
2165
2166         if (!m)
2167                 return -EINVAL;
2168         if (!memfd)
2169                 return -EINVAL;
2170         if (m->sealed)
2171                 return -EPERM;
2172         if (!bus_type_is_trivial(type))
2173                 return -EINVAL;
2174         if (m->poisoned)
2175                 return -ESTALE;
2176
2177         r = sd_memfd_set_sealed(memfd, true);
2178         if (r < 0)
2179                 return r;
2180
2181         copy_fd = sd_memfd_dup_fd(memfd);
2182         if (copy_fd < 0)
2183                 return copy_fd;
2184
2185         r = sd_memfd_get_size(memfd, &size);
2186         if (r < 0)
2187                 return r;
2188
2189         align = bus_type_get_alignment(type);
2190         sz = bus_type_get_size(type);
2191
2192         assert_se(align > 0);
2193         assert_se(sz > 0);
2194
2195         if (size % sz != 0)
2196                 return -EINVAL;
2197
2198         if (size > (uint64_t) (uint32_t) -1)
2199                 return -EINVAL;
2200
2201         r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
2202         if (r < 0)
2203                 return r;
2204
2205         a = message_extend_body(m, align, 0);
2206         if (!a)
2207                 return -ENOMEM;
2208
2209         part = message_append_part(m);
2210         if (!part)
2211                 return -ENOMEM;
2212
2213         part->memfd = copy_fd;
2214         part->sealed = true;
2215         part->size = size;
2216         copy_fd = -1;
2217
2218         message_extend_containers(m, size);
2219         m->header->body_size += size;
2220
2221         return sd_bus_message_close_container(m);
2222 }
2223
2224 _public_ int sd_bus_message_append_string_memfd(sd_bus_message *m, sd_memfd *memfd) {
2225         _cleanup_close_ int copy_fd = -1;
2226         struct bus_body_part *part;
2227         struct bus_container *c;
2228         uint64_t size;
2229         void *a;
2230         int r;
2231
2232         assert_return(m, -EINVAL);
2233         assert_return(memfd, -EINVAL);
2234         assert_return(!m->sealed, -EPERM);
2235         assert_return(!m->poisoned, -ESTALE);
2236
2237         r = sd_memfd_set_sealed(memfd, true);
2238         if (r < 0)
2239                 return r;
2240
2241         copy_fd = sd_memfd_dup_fd(memfd);
2242         if (copy_fd < 0)
2243                 return copy_fd;
2244
2245         r = sd_memfd_get_size(memfd, &size);
2246         if (r < 0)
2247                 return r;
2248
2249         /* We require this to be NUL terminated */
2250         if (size == 0)
2251                 return -EINVAL;
2252
2253         if (size > (uint64_t) (uint32_t) -1)
2254                 return -EINVAL;
2255
2256         c = message_get_container(m);
2257         if (c->signature && c->signature[c->index]) {
2258                 /* Container signature is already set */
2259
2260                 if (c->signature[c->index] != SD_BUS_TYPE_STRING)
2261                         return -ENXIO;
2262         } else {
2263                 char *e;
2264
2265                 /* Maybe we can append to the signature? But only if this is the top-level container*/
2266                 if (c->enclosing != 0)
2267                         return -ENXIO;
2268
2269                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRING), NULL);
2270                 if (!e) {
2271                         m->poisoned = true;
2272                         return -ENOMEM;
2273                 }
2274         }
2275
2276         a = message_extend_body(m, 4, 4);
2277         if (!a)
2278                 return -ENOMEM;
2279
2280         *(uint32_t*) a = size - 1;
2281
2282         part = message_append_part(m);
2283         if (!part)
2284                 return -ENOMEM;
2285
2286         part->memfd = copy_fd;
2287         part->sealed = true;
2288         part->size = size;
2289         copy_fd = -1;
2290
2291         message_extend_containers(m, size);
2292         m->header->body_size += size;
2293
2294         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2295                 c->index++;
2296
2297         return 0;
2298 }
2299
2300 _public_ int sd_bus_message_append_strv(sd_bus_message *m, char **l) {
2301         char **i;
2302         int r;
2303
2304         assert_return(m, -EINVAL);
2305         assert_return(!m->sealed, -EPERM);
2306         assert_return(!m->poisoned, -ESTALE);
2307
2308         r = sd_bus_message_open_container(m, 'a', "s");
2309         if (r < 0)
2310                 return r;
2311
2312         STRV_FOREACH(i, l) {
2313                 r = sd_bus_message_append_basic(m, 's', *i);
2314                 if (r < 0)
2315                         return r;
2316         }
2317
2318         return sd_bus_message_close_container(m);
2319 }
2320
2321 int bus_body_part_map(struct bus_body_part *part) {
2322         void *p;
2323         size_t psz;
2324
2325         assert_se(part);
2326
2327         if (part->data)
2328                 return 0;
2329
2330         if (part->size <= 0)
2331                 return 0;
2332
2333         /* For smaller zero parts (as used for padding) we don't need to map anything... */
2334         if (part->memfd < 0 && part->is_zero && part->size < 8) {
2335                 static const uint8_t zeroes[7] = { };
2336                 part->data = (void*) zeroes;
2337                 return 0;
2338         }
2339
2340         psz = PAGE_ALIGN(part->size);
2341
2342         if (part->memfd >= 0)
2343                 p = mmap(NULL, psz, PROT_READ, MAP_SHARED, part->memfd, 0);
2344         else if (part->is_zero)
2345                 p = mmap(NULL, psz, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
2346         else
2347                 return -EINVAL;
2348
2349         if (p == MAP_FAILED)
2350                 return -errno;
2351
2352         part->mapped = psz;
2353         part->data = p;
2354         part->munmap_this = true;
2355
2356         return 0;
2357 }
2358
2359 void bus_body_part_unmap(struct bus_body_part *part) {
2360
2361         assert_se(part);
2362
2363         if (part->memfd < 0)
2364                 return;
2365
2366         if (!part->data)
2367                 return;
2368
2369         if (!part->munmap_this)
2370                 return;
2371
2372         assert_se(munmap(part->data, part->mapped) == 0);
2373
2374         part->data = NULL;
2375         part->mapped = 0;
2376         part->munmap_this = false;
2377
2378         return;
2379 }
2380
2381 static int buffer_peek(const void *p, uint32_t sz, size_t *rindex, size_t align, size_t nbytes, void **r) {
2382         size_t k, start, end;
2383
2384         assert(rindex);
2385         assert(align > 0);
2386
2387         start = ALIGN_TO((size_t) *rindex, align);
2388         end = start + nbytes;
2389
2390         if (end > sz)
2391                 return -EBADMSG;
2392
2393         /* Verify that padding is 0 */
2394         for (k = *rindex; k < start; k++)
2395                 if (((const uint8_t*) p)[k] != 0)
2396                         return -EBADMSG;
2397
2398         if (r)
2399                 *r = (uint8_t*) p + start;
2400
2401         *rindex = end;
2402
2403         return 1;
2404 }
2405
2406 static bool message_end_of_signature(sd_bus_message *m) {
2407         struct bus_container *c;
2408
2409         assert(m);
2410
2411         c = message_get_container(m);
2412         return !c->signature || c->signature[c->index] == 0;
2413 }
2414
2415 static bool message_end_of_array(sd_bus_message *m, size_t index) {
2416         struct bus_container *c;
2417
2418         assert(m);
2419
2420         c = message_get_container(m);
2421         if (!c->array_size)
2422                 return false;
2423
2424         return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size);
2425 }
2426
2427 int sd_bus_message_at_end(sd_bus_message *m, int complete) {
2428         assert_return(m, -EINVAL);
2429         assert_return(m->sealed, -EPERM);
2430
2431         if (complete && m->n_containers > 0)
2432                 return false;
2433
2434         if (message_end_of_signature(m))
2435                 return true;
2436
2437         if (message_end_of_array(m, m->rindex))
2438                 return true;
2439
2440         return false;
2441 }
2442
2443 static struct bus_body_part* find_part(sd_bus_message *m, size_t index, size_t sz, void **p) {
2444         struct bus_body_part *part;
2445         size_t begin;
2446         int r;
2447
2448         assert(m);
2449
2450         if (m->cached_rindex_part && index >= m->cached_rindex_part_begin) {
2451                 part = m->cached_rindex_part;
2452                 begin = m->cached_rindex_part_begin;
2453         } else {
2454                 part = &m->body;
2455                 begin = 0;
2456         }
2457
2458         while (part) {
2459                 if (index < begin)
2460                         return NULL;
2461
2462                 if (index + sz <= begin + part->size) {
2463
2464                         r = bus_body_part_map(part);
2465                         if (r < 0)
2466                                 return NULL;
2467
2468                         if (p)
2469                                 *p = (uint8_t*) part->data + index - begin;
2470
2471                         m->cached_rindex_part = part;
2472                         m->cached_rindex_part_begin = begin;
2473
2474                         return part;
2475                 }
2476
2477                 begin += part->size;
2478                 part = part->next;
2479         }
2480
2481         return NULL;
2482 }
2483
2484 static int message_peek_body(
2485                 sd_bus_message *m,
2486                 size_t *rindex,
2487                 size_t align,
2488                 size_t nbytes,
2489                 void **ret) {
2490
2491         size_t k, start, end, padding;
2492         struct bus_body_part *part;
2493         uint8_t *q;
2494
2495         assert(m);
2496         assert(rindex);
2497         assert(align > 0);
2498
2499         if (message_end_of_array(m, *rindex))
2500                 return 0;
2501
2502         start = ALIGN_TO((size_t) *rindex, align);
2503         padding = start - *rindex;
2504         end = start + nbytes;
2505
2506         if (end > BUS_MESSAGE_BODY_SIZE(m))
2507                 return -EBADMSG;
2508
2509         part = find_part(m, *rindex, padding, (void**) &q);
2510         if (!part)
2511                 return -EBADMSG;
2512
2513         if (q) {
2514                 /* Verify padding */
2515                 for (k = 0; k < padding; k++)
2516                         if (q[k] != 0)
2517                                 return -EBADMSG;
2518         }
2519
2520         part = find_part(m, start, nbytes, (void**) &q);
2521         if (!part || !q)
2522                 return -EBADMSG;
2523
2524         *rindex = end;
2525
2526         if (ret)
2527                 *ret = q;
2528
2529         return 1;
2530 }
2531
2532 static bool validate_nul(const char *s, size_t l) {
2533
2534         /* Check for NUL chars in the string */
2535         if (memchr(s, 0, l))
2536                 return false;
2537
2538         /* Check for NUL termination */
2539         if (s[l] != 0)
2540                 return false;
2541
2542         return true;
2543 }
2544
2545 static bool validate_string(const char *s, size_t l) {
2546
2547         if (!validate_nul(s, l))
2548                 return false;
2549
2550         /* Check if valid UTF8 */
2551         if (!utf8_is_valid(s))
2552                 return false;
2553
2554         return true;
2555 }
2556
2557 static bool validate_signature(const char *s, size_t l) {
2558
2559         if (!validate_nul(s, l))
2560                 return false;
2561
2562         /* Check if valid signature */
2563         if (!signature_is_valid(s, true))
2564                 return false;
2565
2566         return true;
2567 }
2568
2569 static bool validate_object_path(const char *s, size_t l) {
2570
2571         if (!validate_nul(s, l))
2572                 return false;
2573
2574         if (!object_path_is_valid(s))
2575                 return false;
2576
2577         return true;
2578 }
2579
2580 _public_ int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
2581         struct bus_container *c;
2582         void *q;
2583         int r;
2584
2585         assert_return(m, -EINVAL);
2586         assert_return(m->sealed, -EPERM);
2587         assert_return(bus_type_is_basic(type), -EINVAL);
2588
2589         if (message_end_of_signature(m))
2590                 return -ENXIO;
2591
2592         if (message_end_of_array(m, m->rindex))
2593                 return 0;
2594
2595         c = message_get_container(m);
2596         if (c->signature[c->index] != type)
2597                 return -ENXIO;
2598
2599         switch (type) {
2600
2601         case SD_BUS_TYPE_STRING:
2602         case SD_BUS_TYPE_OBJECT_PATH: {
2603                 uint32_t l;
2604                 size_t rindex;
2605
2606                 rindex = m->rindex;
2607                 r = message_peek_body(m, &rindex, 4, 4, &q);
2608                 if (r <= 0)
2609                         return r;
2610
2611                 l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2612                 r = message_peek_body(m, &rindex, 1, l+1, &q);
2613                 if (r < 0)
2614                         return r;
2615                 if (r == 0)
2616                         return -EBADMSG;
2617
2618                 if (type == SD_BUS_TYPE_OBJECT_PATH) {
2619                         if (!validate_object_path(q, l))
2620                                 return -EBADMSG;
2621                 } else {
2622                         if (!validate_string(q, l))
2623                                 return -EBADMSG;
2624                 }
2625
2626                 m->rindex = rindex;
2627                 if (p)
2628                         *(const char**) p = q;
2629
2630                 break;
2631         }
2632
2633         case SD_BUS_TYPE_SIGNATURE: {
2634                 uint8_t l;
2635                 size_t rindex;
2636
2637                 rindex = m->rindex;
2638                 r = message_peek_body(m, &rindex, 1, 1, &q);
2639                 if (r <= 0)
2640                         return r;
2641
2642                 l = *(uint8_t*) q;
2643                 r = message_peek_body(m, &rindex, 1, l+1, &q);
2644                 if (r < 0)
2645                         return r;
2646                 if (r == 0)
2647                         return -EBADMSG;
2648
2649                 if (!validate_signature(q, l))
2650                         return -EBADMSG;
2651
2652                 m->rindex = rindex;
2653
2654                 if (p)
2655                         *(const char**) p = q;
2656                 break;
2657         }
2658
2659         default: {
2660                 ssize_t sz, align;
2661                 size_t rindex;
2662
2663                 align = bus_type_get_alignment(type);
2664                 sz = bus_type_get_size(type);
2665                 assert(align > 0 && sz > 0);
2666
2667                 rindex = m->rindex;
2668                 r = message_peek_body(m, &rindex, align, sz, &q);
2669                 if (r <= 0)
2670                         return r;
2671
2672                 switch (type) {
2673
2674                 case SD_BUS_TYPE_BYTE:
2675                         if (p)
2676                                 *(uint8_t*) p = *(uint8_t*) q;
2677                         break;
2678
2679                 case SD_BUS_TYPE_BOOLEAN:
2680                         if (p)
2681                                 *(int*) p = !!*(uint32_t*) q;
2682                         break;
2683
2684                 case SD_BUS_TYPE_INT16:
2685                 case SD_BUS_TYPE_UINT16:
2686                         if (p)
2687                                 *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
2688                         break;
2689
2690                 case SD_BUS_TYPE_INT32:
2691                 case SD_BUS_TYPE_UINT32:
2692                         if (p)
2693                                 *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2694                         break;
2695
2696                 case SD_BUS_TYPE_INT64:
2697                 case SD_BUS_TYPE_UINT64:
2698                 case SD_BUS_TYPE_DOUBLE:
2699                         if (p)
2700                                 *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
2701                         break;
2702
2703                 case SD_BUS_TYPE_UNIX_FD: {
2704                         uint32_t j;
2705
2706                         j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2707                         if (j >= m->n_fds)
2708                                 return -EBADMSG;
2709
2710                         if (p)
2711                                 *(int*) p = m->fds[j];
2712                         break;
2713                 }
2714
2715                 default:
2716                         assert_not_reached("Unknown basic type...");
2717                 }
2718
2719                 m->rindex = rindex;
2720
2721                 break;
2722         }
2723         }
2724
2725         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2726                 c->index++;
2727
2728         return 1;
2729 }
2730
2731 static int bus_message_enter_array(
2732                 sd_bus_message *m,
2733                 struct bus_container *c,
2734                 const char *contents,
2735                 uint32_t **array_size) {
2736
2737         size_t rindex;
2738         void *q;
2739         int r, alignment;
2740
2741         assert(m);
2742         assert(c);
2743         assert(contents);
2744         assert(array_size);
2745
2746         if (!signature_is_single(contents, true))
2747                 return -EINVAL;
2748
2749         alignment = bus_type_get_alignment(contents[0]);
2750         if (alignment < 0)
2751                 return alignment;
2752
2753         if (!c->signature || c->signature[c->index] == 0)
2754                 return 0;
2755
2756         if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
2757                 return -ENXIO;
2758
2759         if (!startswith(c->signature + c->index + 1, contents))
2760                 return -ENXIO;
2761
2762         rindex = m->rindex;
2763         r = message_peek_body(m, &rindex, 4, 4, &q);
2764         if (r <= 0)
2765                 return r;
2766
2767         if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > BUS_ARRAY_MAX_SIZE)
2768                 return -EBADMSG;
2769
2770         r = message_peek_body(m, &rindex, alignment, 0, NULL);
2771         if (r < 0)
2772                 return r;
2773         if (r == 0)
2774                 return -EBADMSG;
2775
2776         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2777                 c->index += 1 + strlen(contents);
2778
2779         m->rindex = rindex;
2780
2781         *array_size = (uint32_t*) q;
2782
2783         return 1;
2784 }
2785
2786 static int bus_message_enter_variant(
2787                 sd_bus_message *m,
2788                 struct bus_container *c,
2789                 const char *contents) {
2790
2791         size_t rindex;
2792         uint8_t l;
2793         void *q;
2794         int r;
2795
2796         assert(m);
2797         assert(c);
2798         assert(contents);
2799
2800         if (!signature_is_single(contents, false))
2801                 return -EINVAL;
2802
2803         if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
2804                 return -EINVAL;
2805
2806         if (!c->signature || c->signature[c->index] == 0)
2807                 return 0;
2808
2809         if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
2810                 return -ENXIO;
2811
2812         rindex = m->rindex;
2813         r = message_peek_body(m, &rindex, 1, 1, &q);
2814         if (r <= 0)
2815                 return r;
2816
2817         l = *(uint8_t*) q;
2818         r = message_peek_body(m, &rindex, 1, l+1, &q);
2819         if (r < 0)
2820                 return r;
2821         if (r == 0)
2822                 return -EBADMSG;
2823
2824         if (!validate_signature(q, l))
2825                 return -EBADMSG;
2826
2827         if (!streq(q, contents))
2828                 return -ENXIO;
2829
2830         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2831                 c->index++;
2832
2833         m->rindex = rindex;
2834
2835         return 1;
2836 }
2837
2838 static int bus_message_enter_struct(
2839                 sd_bus_message *m,
2840                 struct bus_container *c,
2841                 const char *contents) {
2842
2843         size_t l;
2844         int r;
2845
2846         assert(m);
2847         assert(c);
2848         assert(contents);
2849
2850         if (!signature_is_valid(contents, false))
2851                 return -EINVAL;
2852
2853         if (!c->signature || c->signature[c->index] == 0)
2854                 return 0;
2855
2856         l = strlen(contents);
2857
2858         if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
2859             !startswith(c->signature + c->index + 1, contents) ||
2860             c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
2861                 return -ENXIO;
2862
2863         r = message_peek_body(m, &m->rindex, 8, 0, NULL);
2864         if (r <= 0)
2865                 return r;
2866
2867         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2868                 c->index += 1 + l + 1;
2869
2870         return 1;
2871 }
2872
2873 static int bus_message_enter_dict_entry(
2874                 sd_bus_message *m,
2875                 struct bus_container *c,
2876                 const char *contents) {
2877
2878         size_t l;
2879         int r;
2880
2881         assert(m);
2882         assert(c);
2883         assert(contents);
2884
2885         if (!signature_is_pair(contents))
2886                 return -EINVAL;
2887
2888         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2889                 return -ENXIO;
2890
2891         if (!c->signature || c->signature[c->index] == 0)
2892                 return 0;
2893
2894         l = strlen(contents);
2895
2896         if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
2897             !startswith(c->signature + c->index + 1, contents) ||
2898             c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
2899                 return -ENXIO;
2900
2901         r = message_peek_body(m, &m->rindex, 8, 0, NULL);
2902         if (r <= 0)
2903                 return r;
2904
2905         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2906                 c->index += 1 + l + 1;
2907
2908         return 1;
2909 }
2910
2911 _public_ int sd_bus_message_enter_container(sd_bus_message *m,
2912                                             char type,
2913                                             const char *contents) {
2914         struct bus_container *c, *w;
2915         uint32_t *array_size = NULL;
2916         char *signature;
2917         size_t before;
2918         int r;
2919
2920         assert_return(m, -EINVAL);
2921         assert_return(m->sealed, -EPERM);
2922         assert_return(type != 0 || !contents, -EINVAL);
2923
2924         if (type == 0 || !contents) {
2925                 const char *cc;
2926                 char tt;
2927
2928                 /* Allow entering into anonymous containers */
2929                 r = sd_bus_message_peek_type(m, &tt, &cc);
2930                 if (r <= 0)
2931                         return r;
2932
2933                 if (type != 0 && type != tt)
2934                         return -ENXIO;
2935
2936                 if (contents && !streq(contents, cc))
2937                         return -ENXIO;
2938
2939                 type = tt;
2940                 contents = cc;
2941         }
2942
2943         /*
2944          * We enforce a global limit on container depth, that is much
2945          * higher than the 32 structs and 32 arrays the specification
2946          * mandates. This is simpler to implement for us, and we need
2947          * this only to ensure our container array doesn't grow
2948          * without bounds. We are happy to return any data from a
2949          * message as long as the data itself is valid, even if the
2950          * overall message might be not.
2951          *
2952          * Note that the message signature is validated when
2953          * parsing the headers, and that validation does check the
2954          * 32/32 limit.
2955          *
2956          * Note that the specification defines no limits on the depth
2957          * of stacked variants, but we do.
2958          */
2959         if (m->n_containers >= BUS_CONTAINER_DEPTH)
2960                 return -EBADMSG;
2961
2962         w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
2963         if (!w)
2964                 return -ENOMEM;
2965         m->containers = w;
2966
2967         if (message_end_of_signature(m))
2968                 return -ENXIO;
2969
2970         if (message_end_of_array(m, m->rindex))
2971                 return 0;
2972
2973         c = message_get_container(m);
2974
2975         signature = strdup(contents);
2976         if (!signature)
2977                 return -ENOMEM;
2978
2979         c->saved_index = c->index;
2980         before = m->rindex;
2981
2982         if (type == SD_BUS_TYPE_ARRAY)
2983                 r = bus_message_enter_array(m, c, contents, &array_size);
2984         else if (type == SD_BUS_TYPE_VARIANT)
2985                 r = bus_message_enter_variant(m, c, contents);
2986         else if (type == SD_BUS_TYPE_STRUCT)
2987                 r = bus_message_enter_struct(m, c, contents);
2988         else if (type == SD_BUS_TYPE_DICT_ENTRY)
2989                 r = bus_message_enter_dict_entry(m, c, contents);
2990         else
2991                 r = -EINVAL;
2992
2993         if (r <= 0) {
2994                 free(signature);
2995                 return r;
2996         }
2997
2998         /* OK, let's fill it in */
2999         w += m->n_containers++;
3000         w->enclosing = type;
3001         w->signature = signature;
3002         w->index = 0;
3003         w->array_size = array_size;
3004         w->before = before;
3005         w->begin = m->rindex;
3006
3007         return 1;
3008 }
3009
3010 _public_ int sd_bus_message_exit_container(sd_bus_message *m) {
3011         struct bus_container *c;
3012
3013         assert_return(m, -EINVAL);
3014         assert_return(m->sealed, -EPERM);
3015         assert_return(m->n_containers > 0, -EINVAL);
3016
3017         c = message_get_container(m);
3018         if (c->enclosing == SD_BUS_TYPE_ARRAY) {
3019                 uint32_t l;
3020
3021                 l = BUS_MESSAGE_BSWAP32(m, *c->array_size);
3022                 if (c->begin + l != m->rindex)
3023                         return -EBUSY;
3024
3025         } else {
3026                 if (c->signature && c->signature[c->index] != 0)
3027                         return -EINVAL;
3028         }
3029
3030         free(c->signature);
3031         m->n_containers--;
3032
3033         return 1;
3034 }
3035
3036 static void message_quit_container(sd_bus_message *m) {
3037         struct bus_container *c;
3038
3039         assert(m);
3040         assert(m->sealed);
3041         assert(m->n_containers > 0);
3042
3043         c = message_get_container(m);
3044
3045         /* Undo seeks */
3046         assert(m->rindex >= c->before);
3047         m->rindex = c->before;
3048
3049         /* Free container */
3050         free(c->signature);
3051         m->n_containers--;
3052
3053         /* Correct index of new top-level container */
3054         c = message_get_container(m);
3055         c->index = c->saved_index;
3056 }
3057
3058 _public_ int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents) {
3059         struct bus_container *c;
3060         int r;
3061
3062         assert_return(m, -EINVAL);
3063         assert_return(m->sealed, -EPERM);
3064
3065         if (message_end_of_signature(m))
3066                 goto eof;
3067
3068         if (message_end_of_array(m, m->rindex))
3069                 goto eof;
3070
3071         c = message_get_container(m);
3072
3073         if (bus_type_is_basic(c->signature[c->index])) {
3074                 if (contents)
3075                         *contents = NULL;
3076                 if (type)
3077                         *type = c->signature[c->index];
3078                 return 1;
3079         }
3080
3081         if (c->signature[c->index] == SD_BUS_TYPE_ARRAY) {
3082
3083                 if (contents) {
3084                         size_t l;
3085                         char *sig;
3086
3087                         r = signature_element_length(c->signature+c->index+1, &l);
3088                         if (r < 0)
3089                                 return r;
3090
3091                         assert(l >= 1);
3092
3093                         sig = strndup(c->signature + c->index + 1, l);
3094                         if (!sig)
3095                                 return -ENOMEM;
3096
3097                         free(m->peeked_signature);
3098                         m->peeked_signature = sig;
3099
3100                         *contents = sig;
3101                 }
3102
3103                 if (type)
3104                         *type = SD_BUS_TYPE_ARRAY;
3105
3106                 return 1;
3107         }
3108
3109         if (c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ||
3110             c->signature[c->index] == SD_BUS_TYPE_DICT_ENTRY_BEGIN) {
3111
3112                 if (contents) {
3113                         size_t l;
3114                         char *sig;
3115
3116                         r = signature_element_length(c->signature+c->index, &l);
3117                         if (r < 0)
3118                                 return r;
3119
3120                         assert(l >= 2);
3121                         sig = strndup(c->signature + c->index + 1, l - 2);
3122                         if (!sig)
3123                                 return -ENOMEM;
3124
3125                         free(m->peeked_signature);
3126                         m->peeked_signature = sig;
3127
3128                         *contents = sig;
3129                 }
3130
3131                 if (type)
3132                         *type = c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY;
3133
3134                 return 1;
3135         }
3136
3137         if (c->signature[c->index] == SD_BUS_TYPE_VARIANT) {
3138                 if (contents) {
3139                         size_t rindex, l;
3140                         void *q;
3141
3142                         rindex = m->rindex;
3143                         r = message_peek_body(m, &rindex, 1, 1, &q);
3144                         if (r < 0)
3145                                 return r;
3146                         if (r == 0)
3147                                 goto eof;
3148
3149                         l = *(uint8_t*) q;
3150                         r = message_peek_body(m, &rindex, 1, l+1, &q);
3151                         if (r < 0)
3152                                 return r;
3153                         if (r == 0)
3154                                 return -EBADMSG;
3155
3156                         if (!validate_signature(q, l))
3157                                 return -EBADMSG;
3158
3159                         *contents = q;
3160                 }
3161
3162                 if (type)
3163                         *type = SD_BUS_TYPE_VARIANT;
3164
3165                 return 1;
3166         }
3167
3168         return -EINVAL;
3169
3170 eof:
3171         if (type)
3172                 *type = 0;
3173         if (contents)
3174                 *contents = NULL;
3175         return 0;
3176 }
3177
3178 _public_ int sd_bus_message_rewind(sd_bus_message *m, int complete) {
3179         struct bus_container *c;
3180
3181         assert_return(m, -EINVAL);
3182         assert_return(m->sealed, -EPERM);
3183
3184         if (complete) {
3185                 message_reset_containers(m);
3186                 m->rindex = 0;
3187                 m->root_container.index = 0;
3188
3189                 c = message_get_container(m);
3190         } else {
3191                 c = message_get_container(m);
3192
3193                 c->index = 0;
3194                 m->rindex = c->begin;
3195         }
3196
3197         return !isempty(c->signature);
3198 }
3199 static int message_read_ap(
3200                 sd_bus_message *m,
3201                 const char *types,
3202                 va_list ap) {
3203
3204         unsigned n_array, n_struct;
3205         TypeStack stack[BUS_CONTAINER_DEPTH];
3206         unsigned stack_ptr = 0;
3207         unsigned n_loop = 0;
3208         int r;
3209
3210         assert(m);
3211
3212         if (isempty(types))
3213                 return 0;
3214
3215         /* Ideally, we'd just call ourselves recursively on every
3216          * complex type. However, the state of a va_list that is
3217          * passed to a function is undefined after that function
3218          * returns. This means we need to docode the va_list linearly
3219          * in a single stackframe. We hence implement our own
3220          * home-grown stack in an array. */
3221
3222         n_array = (unsigned) -1; /* lenght of current array entries */
3223         n_struct = strlen(types); /* length of current struct contents signature */
3224
3225         for (;;) {
3226                 const char *t;
3227
3228                 n_loop++;
3229
3230                 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
3231                         r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
3232                         if (r < 0)
3233                                 return r;
3234                         if (r == 0)
3235                                 break;
3236
3237                         r = sd_bus_message_exit_container(m);
3238                         if (r < 0)
3239                                 return r;
3240
3241                         continue;
3242                 }
3243
3244                 t = types;
3245                 if (n_array != (unsigned) -1)
3246                         n_array --;
3247                 else {
3248                         types ++;
3249                         n_struct--;
3250                 }
3251
3252                 switch (*t) {
3253
3254                 case SD_BUS_TYPE_BYTE:
3255                 case SD_BUS_TYPE_BOOLEAN:
3256                 case SD_BUS_TYPE_INT16:
3257                 case SD_BUS_TYPE_UINT16:
3258                 case SD_BUS_TYPE_INT32:
3259                 case SD_BUS_TYPE_UINT32:
3260                 case SD_BUS_TYPE_INT64:
3261                 case SD_BUS_TYPE_UINT64:
3262                 case SD_BUS_TYPE_DOUBLE:
3263                 case SD_BUS_TYPE_STRING:
3264                 case SD_BUS_TYPE_OBJECT_PATH:
3265                 case SD_BUS_TYPE_SIGNATURE:
3266                 case SD_BUS_TYPE_UNIX_FD: {
3267                         void *p;
3268
3269                         p = va_arg(ap, void*);
3270                         r = sd_bus_message_read_basic(m, *t, p);
3271                         if (r < 0)
3272                                 return r;
3273                         if (r == 0) {
3274                                 if (n_loop <= 1)
3275                                         return 0;
3276
3277                                 return -ENXIO;
3278                         }
3279
3280                         break;
3281                 }
3282
3283                 case SD_BUS_TYPE_ARRAY: {
3284                         size_t k;
3285
3286                         r = signature_element_length(t + 1, &k);
3287                         if (r < 0)
3288                                 return r;
3289
3290                         {
3291                                 char s[k + 1];
3292                                 memcpy(s, t + 1, k);
3293                                 s[k] = 0;
3294
3295                                 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
3296                                 if (r < 0)
3297                                         return r;
3298                                 if (r == 0) {
3299                                         if (n_loop <= 1)
3300                                                 return 0;
3301
3302                                         return -ENXIO;
3303                                 }
3304                         }
3305
3306                         if (n_array == (unsigned) -1) {
3307                                 types += k;
3308                                 n_struct -= k;
3309                         }
3310
3311                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
3312                         if (r < 0)
3313                                 return r;
3314
3315                         types = t + 1;
3316                         n_struct = k;
3317                         n_array = va_arg(ap, unsigned);
3318
3319                         break;
3320                 }
3321
3322                 case SD_BUS_TYPE_VARIANT: {
3323                         const char *s;
3324
3325                         s = va_arg(ap, const char *);
3326                         if (!s)
3327                                 return -EINVAL;
3328
3329                         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, s);
3330                         if (r < 0)
3331                                 return r;
3332                         if (r == 0) {
3333                                 if (n_loop <= 1)
3334                                         return 0;
3335
3336                                 return -ENXIO;
3337                         }
3338
3339                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
3340                         if (r < 0)
3341                                 return r;
3342
3343                         types = s;
3344                         n_struct = strlen(s);
3345                         n_array = (unsigned) -1;
3346
3347                         break;
3348                 }
3349
3350                 case SD_BUS_TYPE_STRUCT_BEGIN:
3351                 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
3352                         size_t k;
3353
3354                         r = signature_element_length(t, &k);
3355                         if (r < 0)
3356                                 return r;
3357
3358                         {
3359                                 char s[k - 1];
3360                                 memcpy(s, t + 1, k - 2);
3361                                 s[k - 2] = 0;
3362
3363                                 r = sd_bus_message_enter_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
3364                                 if (r < 0)
3365                                         return r;
3366                                 if (r == 0) {
3367                                         if (n_loop <= 1)
3368                                                 return 0;
3369                                         return -ENXIO;
3370                                 }
3371                         }
3372
3373                         if (n_array == (unsigned) -1) {
3374                                 types += k - 1;
3375                                 n_struct -= k - 1;
3376                         }
3377
3378                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
3379                         if (r < 0)
3380                                 return r;
3381
3382                         types = t + 1;
3383                         n_struct = k - 2;
3384                         n_array = (unsigned) -1;
3385
3386                         break;
3387                 }
3388
3389                 default:
3390                         return -EINVAL;
3391                 }
3392         }
3393
3394         return 1;
3395 }
3396
3397 _public_ int sd_bus_message_read(sd_bus_message *m, const char *types, ...) {
3398         va_list ap;
3399         int r;
3400
3401         assert_return(m, -EINVAL);
3402         assert_return(m->sealed, -EPERM);
3403         assert_return(types, -EINVAL);
3404
3405         va_start(ap, types);
3406         r = message_read_ap(m, types, ap);
3407         va_end(ap);
3408
3409         return r;
3410 }
3411
3412 _public_ int sd_bus_message_skip(sd_bus_message *m, const char *types) {
3413         int r;
3414
3415         assert_return(m, -EINVAL);
3416         assert_return(m->sealed, -EPERM);
3417         assert_return(types, -EINVAL);
3418
3419         if (isempty(types))
3420                 return 0;
3421
3422         switch (*types) {
3423
3424         case SD_BUS_TYPE_BYTE:
3425         case SD_BUS_TYPE_BOOLEAN:
3426         case SD_BUS_TYPE_INT16:
3427         case SD_BUS_TYPE_UINT16:
3428         case SD_BUS_TYPE_INT32:
3429         case SD_BUS_TYPE_UINT32:
3430         case SD_BUS_TYPE_INT64:
3431         case SD_BUS_TYPE_UINT64:
3432         case SD_BUS_TYPE_DOUBLE:
3433         case SD_BUS_TYPE_STRING:
3434         case SD_BUS_TYPE_OBJECT_PATH:
3435         case SD_BUS_TYPE_SIGNATURE:
3436         case SD_BUS_TYPE_UNIX_FD:
3437
3438                 r = sd_bus_message_read_basic(m, *types, NULL);
3439                 if (r <= 0)
3440                         return r;
3441
3442                 r = sd_bus_message_skip(m, types + 1);
3443                 if (r < 0)
3444                         return r;
3445
3446                 return 1;
3447
3448         case SD_BUS_TYPE_ARRAY: {
3449                 size_t k;
3450
3451                 r = signature_element_length(types + 1, &k);
3452                 if (r < 0)
3453                         return r;
3454
3455                 {
3456                         char s[k+1];
3457                         memcpy(s, types+1, k);
3458                         s[k] = 0;
3459
3460                         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
3461                         if (r <= 0)
3462                                 return r;
3463
3464                         for (;;) {
3465                                 r = sd_bus_message_skip(m, s);
3466                                 if (r < 0)
3467                                         return r;
3468                                 if (r == 0)
3469                                         break;
3470                         }
3471
3472                         r = sd_bus_message_exit_container(m);
3473                         if (r < 0)
3474                                 return r;
3475                 }
3476
3477                 r = sd_bus_message_skip(m, types + 1 + k);
3478                 if (r < 0)
3479                         return r;
3480
3481                 return 1;
3482         }
3483
3484         case SD_BUS_TYPE_VARIANT: {
3485                 const char *contents;
3486                 char x;
3487
3488                 r = sd_bus_message_peek_type(m, &x, &contents);
3489                 if (r <= 0)
3490                         return r;
3491
3492                 if (x != SD_BUS_TYPE_VARIANT)
3493                         return -ENXIO;
3494
3495                 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
3496                 if (r <= 0)
3497                         return r;
3498
3499                 r = sd_bus_message_skip(m, contents);
3500                 if (r < 0)
3501                         return r;
3502                 assert(r != 0);
3503
3504                 r = sd_bus_message_exit_container(m);
3505                 if (r < 0)
3506                         return r;
3507
3508                 r = sd_bus_message_skip(m, types + 1);
3509                 if (r < 0)
3510                         return r;
3511
3512                 return 1;
3513         }
3514
3515         case SD_BUS_TYPE_STRUCT_BEGIN:
3516         case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
3517                 size_t k;
3518
3519                 r = signature_element_length(types, &k);
3520                 if (r < 0)
3521                         return r;
3522
3523                 {
3524                         char s[k-1];
3525                         memcpy(s, types+1, k-2);
3526                         s[k-2] = 0;
3527
3528                         r = sd_bus_message_enter_container(m, *types == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
3529                         if (r <= 0)
3530                                 return r;
3531
3532                         r = sd_bus_message_skip(m, s);
3533                         if (r < 0)
3534                                 return r;
3535                         assert(r != 0);
3536
3537                         r = sd_bus_message_exit_container(m);
3538                         if (r < 0)
3539                                 return r;
3540                 }
3541
3542                 r = sd_bus_message_skip(m, types + k);
3543                 if (r < 0)
3544                         return r;
3545
3546                 return 1;
3547         }
3548
3549         default:
3550                 return -EINVAL;
3551         }
3552 }
3553
3554 _public_ int sd_bus_message_read_array(sd_bus_message *m,
3555                                        char type,
3556                                        const void **ptr,
3557                                        size_t *size) {
3558         struct bus_container *c;
3559         void *p;
3560         size_t sz;
3561         ssize_t align;
3562         int r;
3563
3564         assert_return(m, -EINVAL);
3565         assert_return(m->sealed, -EPERM);
3566         assert_return(bus_type_is_trivial(type), -EINVAL);
3567         assert_return(ptr, -EINVAL);
3568         assert_return(size, -EINVAL);
3569         assert_return(!BUS_MESSAGE_NEED_BSWAP(m), -ENOTSUP);
3570
3571         align = bus_type_get_alignment(type);
3572         if (align < 0)
3573                 return align;
3574
3575         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, CHAR_TO_STR(type));
3576         if (r <= 0)
3577                 return r;
3578
3579         c = message_get_container(m);
3580         sz = BUS_MESSAGE_BSWAP32(m, *c->array_size);
3581
3582         r = message_peek_body(m, &m->rindex, align, sz, &p);
3583         if (r < 0)
3584                 goto fail;
3585         if (r == 0) {
3586                 r = -EBADMSG;
3587                 goto fail;
3588         }
3589
3590         r = sd_bus_message_exit_container(m);
3591         if (r < 0)
3592                 goto fail;
3593
3594         *ptr = (const void*) p;
3595         *size = sz;
3596
3597         return 1;
3598
3599 fail:
3600         message_quit_container(m);
3601         return r;
3602 }
3603
3604 static int message_peek_fields(
3605                 sd_bus_message *m,
3606                 size_t *rindex,
3607                 size_t align,
3608                 size_t nbytes,
3609                 void **ret) {
3610
3611         assert(m);
3612         assert(rindex);
3613         assert(align > 0);
3614
3615         return buffer_peek(BUS_MESSAGE_FIELDS(m), BUS_MESSAGE_FIELDS_SIZE(m), rindex, align, nbytes, ret);
3616 }
3617
3618 static int message_peek_field_uint32(
3619                 sd_bus_message *m,
3620                 size_t *ri,
3621                 uint32_t *ret) {
3622
3623         int r;
3624         void *q;
3625
3626         assert(m);
3627         assert(ri);
3628
3629         r = message_peek_fields(m, ri, 4, 4, &q);
3630         if (r < 0)
3631                 return r;
3632
3633         if (ret)
3634                 *ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
3635
3636         return 0;
3637 }
3638
3639 static int message_peek_field_string(
3640                 sd_bus_message *m,
3641                 bool (*validate)(const char *p),
3642                 size_t *ri,
3643                 const char **ret) {
3644
3645         uint32_t l;
3646         int r;
3647         void *q;
3648
3649         assert(m);
3650         assert(ri);
3651
3652         r = message_peek_field_uint32(m, ri, &l);
3653         if (r < 0)
3654                 return r;
3655
3656         r = message_peek_fields(m, ri, 1, l+1, &q);
3657         if (r < 0)
3658                 return r;
3659
3660         if (validate) {
3661                 if (!validate_nul(q, l))
3662                         return -EBADMSG;
3663
3664                 if (!validate(q))
3665                         return -EBADMSG;
3666         } else {
3667                 if (!validate_string(q, l))
3668                         return -EBADMSG;
3669         }
3670
3671         if (ret)
3672                 *ret = q;
3673
3674         return 0;
3675 }
3676
3677 static int message_peek_field_signature(
3678                 sd_bus_message *m,
3679                 size_t *ri,
3680                 const char **ret) {
3681
3682         size_t l;
3683         int r;
3684         void *q;
3685
3686         assert(m);
3687         assert(ri);
3688
3689         r = message_peek_fields(m, ri, 1, 1, &q);
3690         if (r < 0)
3691                 return r;
3692
3693         l = *(uint8_t*) q;
3694         r = message_peek_fields(m, ri, 1, l+1, &q);
3695         if (r < 0)
3696                 return r;
3697
3698         if (!validate_signature(q, l))
3699                 return -EBADMSG;
3700
3701         if (ret)
3702                 *ret = q;
3703
3704         return 0;
3705 }
3706
3707 static int message_skip_fields(
3708                 sd_bus_message *m,
3709                 size_t *ri,
3710                 uint32_t array_size,
3711                 const char **signature) {
3712
3713         size_t original_index;
3714         int r;
3715
3716         assert(m);
3717         assert(ri);
3718         assert(signature);
3719
3720         original_index = *ri;
3721
3722         for (;;) {
3723                 char t;
3724                 size_t l;
3725
3726                 if (array_size != (uint32_t) -1 &&
3727                     array_size <= *ri - original_index)
3728                         return 0;
3729
3730                 t = **signature;
3731                 if (!t)
3732                         return 0;
3733
3734                 if (t == SD_BUS_TYPE_STRING) {
3735
3736                         r = message_peek_field_string(m, NULL, ri, NULL);
3737                         if (r < 0)
3738                                 return r;
3739
3740                         (*signature)++;
3741
3742                 } else if (t == SD_BUS_TYPE_OBJECT_PATH) {
3743
3744                         r = message_peek_field_string(m, object_path_is_valid, ri, NULL);
3745                         if (r < 0)
3746                                 return r;
3747
3748                         (*signature)++;
3749
3750                 } else if (t == SD_BUS_TYPE_SIGNATURE) {
3751
3752                         r = message_peek_field_signature(m, ri, NULL);
3753                         if (r < 0)
3754                                 return r;
3755
3756                         (*signature)++;
3757
3758                 } else if (bus_type_is_basic(t)) {
3759                         ssize_t align, k;
3760
3761                         align = bus_type_get_alignment(t);
3762                         k = bus_type_get_size(t);
3763                         assert(align > 0 && k > 0);
3764
3765                         r = message_peek_fields(m, ri, align, k, NULL);
3766                         if (r < 0)
3767                                 return r;
3768
3769                         (*signature)++;
3770
3771                 } else if (t == SD_BUS_TYPE_ARRAY) {
3772
3773                         r = signature_element_length(*signature+1, &l);
3774                         if (r < 0)
3775                                 return r;
3776
3777                         assert(l >= 1);
3778                         {
3779                                 char sig[l-1], *s;
3780                                 uint32_t nas;
3781                                 int alignment;
3782
3783                                 strncpy(sig, *signature + 1, l-1);
3784                                 s = sig;
3785
3786                                 alignment = bus_type_get_alignment(sig[0]);
3787                                 if (alignment < 0)
3788                                         return alignment;
3789
3790                                 r = message_peek_field_uint32(m, ri, &nas);
3791                                 if (r < 0)
3792                                         return r;
3793                                 if (nas > BUS_ARRAY_MAX_SIZE)
3794                                         return -EBADMSG;
3795
3796                                 r = message_peek_fields(m, ri, alignment, 0, NULL);
3797                                 if (r < 0)
3798                                         return r;
3799
3800                                 r = message_skip_fields(m, ri, nas, (const char**) &s);
3801                                 if (r < 0)
3802                                         return r;
3803                         }
3804
3805                         (*signature) += 1 + l;
3806
3807                 } else if (t == SD_BUS_TYPE_VARIANT) {
3808                         const char *s;
3809
3810                         r = message_peek_field_signature(m, ri, &s);
3811                         if (r < 0)
3812                                 return r;
3813
3814                         r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
3815                         if (r < 0)
3816                                 return r;
3817
3818                         (*signature)++;
3819
3820                 } else if (t == SD_BUS_TYPE_STRUCT ||
3821                            t == SD_BUS_TYPE_DICT_ENTRY) {
3822
3823                         r = signature_element_length(*signature, &l);
3824                         if (r < 0)
3825                                 return r;
3826
3827                         assert(l >= 2);
3828                         {
3829                                 char sig[l-1], *s;
3830                                 strncpy(sig, *signature + 1, l-1);
3831                                 s = sig;
3832
3833                                 r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
3834                                 if (r < 0)
3835                                         return r;
3836                         }
3837
3838                         *signature += l;
3839                 } else
3840                         return -EINVAL;
3841         }
3842 }
3843
3844 int bus_message_parse_fields(sd_bus_message *m) {
3845         size_t ri;
3846         int r;
3847         uint32_t unix_fds = 0;
3848
3849         assert(m);
3850
3851         for (ri = 0; ri < BUS_MESSAGE_FIELDS_SIZE(m); ) {
3852                 const char *signature;
3853                 uint8_t *header;
3854
3855                 r = message_peek_fields(m, &ri, 8, 1, (void**) &header);
3856                 if (r < 0)
3857                         return r;
3858
3859                 r = message_peek_field_signature(m, &ri, &signature);
3860                 if (r < 0)
3861                         return r;
3862
3863                 switch (*header) {
3864                 case _SD_BUS_MESSAGE_HEADER_INVALID:
3865                         return -EBADMSG;
3866
3867                 case SD_BUS_MESSAGE_HEADER_PATH:
3868
3869                         if (m->path)
3870                                 return -EBADMSG;
3871
3872                         if (!streq(signature, "o"))
3873                                 return -EBADMSG;
3874
3875                         r = message_peek_field_string(m, object_path_is_valid, &ri, &m->path);
3876                         break;
3877
3878                 case SD_BUS_MESSAGE_HEADER_INTERFACE:
3879
3880                         if (m->interface)
3881                                 return -EBADMSG;
3882
3883                         if (!streq(signature, "s"))
3884                                 return -EBADMSG;
3885
3886                         r = message_peek_field_string(m, interface_name_is_valid, &ri, &m->interface);
3887                         break;
3888
3889                 case SD_BUS_MESSAGE_HEADER_MEMBER:
3890
3891                         if (m->member)
3892                                 return -EBADMSG;
3893
3894                         if (!streq(signature, "s"))
3895                                 return -EBADMSG;
3896
3897                         r = message_peek_field_string(m, member_name_is_valid, &ri, &m->member);
3898                         break;
3899
3900                 case SD_BUS_MESSAGE_HEADER_ERROR_NAME:
3901
3902                         if (m->error.name)
3903                                 return -EBADMSG;
3904
3905                         if (!streq(signature, "s"))
3906                                 return -EBADMSG;
3907
3908                         r = message_peek_field_string(m, error_name_is_valid, &ri, &m->error.name);
3909                         break;
3910
3911                 case SD_BUS_MESSAGE_HEADER_DESTINATION:
3912
3913                         if (m->destination)
3914                                 return -EBADMSG;
3915
3916                         if (!streq(signature, "s"))
3917                                 return -EBADMSG;
3918
3919                         r = message_peek_field_string(m, service_name_is_valid, &ri, &m->destination);
3920                         break;
3921
3922                 case SD_BUS_MESSAGE_HEADER_SENDER:
3923
3924                         if (m->sender)
3925                                 return -EBADMSG;
3926
3927                         if (!streq(signature, "s"))
3928                                 return -EBADMSG;
3929
3930                         r = message_peek_field_string(m, service_name_is_valid, &ri, &m->sender);
3931                         break;
3932
3933
3934                 case SD_BUS_MESSAGE_HEADER_SIGNATURE: {
3935                         const char *s;
3936                         char *c;
3937
3938                         if (m->root_container.signature)
3939                                 return -EBADMSG;
3940
3941                         if (!streq(signature, "g"))
3942                                 return -EBADMSG;
3943
3944                         r = message_peek_field_signature(m, &ri, &s);
3945                         if (r < 0)
3946                                 return r;
3947
3948                         c = strdup(s);
3949                         if (!c)
3950                                 return -ENOMEM;
3951
3952                         free(m->root_container.signature);
3953                         m->root_container.signature = c;
3954                         break;
3955                 }
3956
3957                 case SD_BUS_MESSAGE_HEADER_REPLY_SERIAL:
3958                         if (m->reply_serial != 0)
3959                                 return -EBADMSG;
3960
3961                         if (!streq(signature, "u"))
3962                                 return -EBADMSG;
3963
3964                         r = message_peek_field_uint32(m, &ri, &m->reply_serial);
3965                         if (r < 0)
3966                                 return r;
3967
3968                         if (m->reply_serial == 0)
3969                                 return -EBADMSG;
3970
3971                         break;
3972
3973                 case SD_BUS_MESSAGE_HEADER_UNIX_FDS:
3974                         if (unix_fds != 0)
3975                                 return -EBADMSG;
3976
3977                         if (!streq(signature, "u"))
3978                                 return -EBADMSG;
3979
3980                         r = message_peek_field_uint32(m, &ri, &unix_fds);
3981                         if (r < 0)
3982                                 return -EBADMSG;
3983
3984                         if (unix_fds == 0)
3985                                 return -EBADMSG;
3986
3987                         break;
3988
3989                 default:
3990                         r = message_skip_fields(m, &ri, (uint32_t) -1, (const char **) &signature);
3991                 }
3992
3993                 if (r < 0)
3994                         return r;
3995         }
3996
3997         if (m->n_fds != unix_fds)
3998                 return -EBADMSG;
3999
4000         if (isempty(m->root_container.signature) != (BUS_MESSAGE_BODY_SIZE(m) == 0))
4001                 return -EBADMSG;
4002
4003         switch (m->header->type) {
4004
4005         case SD_BUS_MESSAGE_SIGNAL:
4006                 if (!m->path || !m->interface || !m->member)
4007                         return -EBADMSG;
4008                 break;
4009
4010         case SD_BUS_MESSAGE_METHOD_CALL:
4011
4012                 if (!m->path || !m->member)
4013                         return -EBADMSG;
4014
4015                 break;
4016
4017         case SD_BUS_MESSAGE_METHOD_RETURN:
4018
4019                 if (m->reply_serial == 0)
4020                         return -EBADMSG;
4021                 break;
4022
4023         case SD_BUS_MESSAGE_METHOD_ERROR:
4024
4025                 if (m->reply_serial == 0 || !m->error.name)
4026                         return -EBADMSG;
4027                 break;
4028         }
4029
4030         /* Try to read the error message, but if we can't it's a non-issue */
4031         if (m->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
4032                 sd_bus_message_read(m, "s", &m->error.message);
4033
4034         return 0;
4035 }
4036
4037 int bus_message_seal(sd_bus_message *m, uint64_t serial) {
4038         struct bus_body_part *part;
4039         size_t l, a;
4040         unsigned i;
4041         int r;
4042
4043         assert(m);
4044
4045         if (m->sealed)
4046                 return -EPERM;
4047
4048         if (m->n_containers > 0)
4049                 return -EBADMSG;
4050
4051         if (m->poisoned)
4052                 return -ESTALE;
4053
4054         /* If there's a non-trivial signature set, then add it in here */
4055         if (!isempty(m->root_container.signature)) {
4056                 r = message_append_field_signature(m, SD_BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, NULL);
4057                 if (r < 0)
4058                         return r;
4059         }
4060
4061         if (m->n_fds > 0) {
4062                 r = message_append_field_uint32(m, SD_BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds);
4063                 if (r < 0)
4064                         return r;
4065         }
4066
4067         /* Add padding at the end of the fields part, since we know
4068          * the body needs to start at an 8 byte alignment. We made
4069          * sure we allocated enough space for this, so all we need to
4070          * do here is to zero it out. */
4071         l = BUS_MESSAGE_FIELDS_SIZE(m);
4072         a = ALIGN8(l) - l;
4073         if (a > 0)
4074                 memset((uint8_t*) BUS_MESSAGE_FIELDS(m) + l, 0, a);
4075
4076         /* If this is something we can send as memfd, then let's seal
4077         the memfd now. Note that we can send memfds as payload only
4078         for directed messages, and not for broadcasts. */
4079         if (m->destination && m->bus && m->bus->use_memfd) {
4080                 MESSAGE_FOREACH_PART(part, i, m)
4081                         if (part->memfd >= 0 && !part->sealed && (part->size > MEMFD_MIN_SIZE || m->bus->use_memfd < 0)) {
4082                                 bus_body_part_unmap(part);
4083
4084                                 if (ioctl(part->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1) >= 0)
4085                                         part->sealed = true;
4086                         }
4087         }
4088
4089         m->header->serial = serial;
4090         m->sealed = true;
4091
4092         return 0;
4093 }
4094
4095 _public_ int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) {
4096         assert_return(m, -EINVAL);
4097         assert_return(destination, -EINVAL);
4098         assert_return(!m->sealed, -EPERM);
4099         assert_return(!m->destination, -EEXIST);
4100
4101         return message_append_field_string(m, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
4102 }
4103
4104 int bus_message_dump(sd_bus_message *m, FILE *f, bool with_header) {
4105         const char *u = NULL, *uu = NULL, *s = NULL;
4106         char **cmdline = NULL;
4107         unsigned level = 1;
4108         int r;
4109         uid_t owner, audit_loginuid;
4110         uint32_t audit_sessionid;
4111
4112         assert(m);
4113
4114         if (!f)
4115                 f = stdout;
4116
4117         if (with_header) {
4118                 fprintf(f,
4119                         "Message %p\n"
4120                         "\tn_ref=%u\n"
4121                         "\tendian=%c\n"
4122                         "\ttype=%i\n"
4123                         "\tflags=%u\n"
4124                         "\tversion=%u\n"
4125                         "\tserial=%u\n"
4126                         "\tfields_size=%u\n"
4127                         "\tbody_size=%u\n"
4128                         "\tpath=%s\n"
4129                         "\tinterface=%s\n"
4130                         "\tmember=%s\n"
4131                         "\tdestination=%s\n"
4132                         "\tsender=%s\n"
4133                         "\tsignature=%s\n"
4134                         "\treply_serial=%u\n"
4135                         "\tsealed=%s\n"
4136                         "\tn_body_parts=%u\n",
4137                         m,
4138                         m->n_ref,
4139                         m->header->endian,
4140                         m->header->type,
4141                         m->header->flags,
4142                         m->header->version,
4143                         BUS_MESSAGE_SERIAL(m),
4144                         BUS_MESSAGE_FIELDS_SIZE(m),
4145                         BUS_MESSAGE_BODY_SIZE(m),
4146                         strna(m->path),
4147                         strna(m->interface),
4148                         strna(m->member),
4149                         strna(m->destination),
4150                         strna(m->sender),
4151                         strna(m->root_container.signature),
4152                         m->reply_serial,
4153                         yes_no(m->sealed),
4154                         m->n_body_parts);
4155
4156                 if (sd_bus_error_is_set(&m->error))
4157                         fprintf(f,
4158                                 "\terror.name=%s\n"
4159                                 "\terror.message=%s\n",
4160                                 strna(m->error.name),
4161                                 strna(m->error.message));
4162
4163                 if (m->pid != 0)
4164                         fprintf(f, "\tpid=%lu\n", (unsigned long) m->pid);
4165                 if (m->tid != 0)
4166                         fprintf(f, "\ttid=%lu\n", (unsigned long) m->tid);
4167                 if (m->uid_valid)
4168                         fprintf(f, "\tuid=%lu\n", (unsigned long) m->uid);
4169                 if (m->gid_valid)
4170                         fprintf(f, "\tgid=%lu\n", (unsigned long) m->gid);
4171                 if (m->pid_starttime != 0)
4172                         fprintf(f, "\tpid_starttime=%llu\n", (unsigned long long) m->pid_starttime);
4173                 if (m->monotonic != 0)
4174                         fprintf(f, "\tmonotonic=%llu\n", (unsigned long long) m->monotonic);
4175                 if (m->realtime != 0)
4176                         fprintf(f, "\trealtime=%llu\n", (unsigned long long) m->realtime);
4177                 if (m->exe)
4178                         fprintf(f, "\texe=[%s]\n", m->exe);
4179                 if (m->comm)
4180                         fprintf(f, "\tcomm=[%s]\n", m->comm);
4181                 if (m->tid_comm)
4182                         fprintf(f, "\ttid_comm=[%s]\n", m->tid_comm);
4183                 if (m->label)
4184                         fprintf(f, "\tlabel=[%s]\n", m->label);
4185                 if (m->cgroup)
4186                         fprintf(f, "\tcgroup=[%s]\n", m->cgroup);
4187
4188                 sd_bus_message_get_unit(m, &u);
4189                 if (u)
4190                         fprintf(f, "\tunit=[%s]\n", u);
4191                 sd_bus_message_get_user_unit(m, &uu);
4192                 if (uu)
4193                         fprintf(f, "\tuser_unit=[%s]\n", uu);
4194                 sd_bus_message_get_session(m, &s);
4195                 if (s)
4196                         fprintf(f, "\tsession=[%s]\n", s);
4197                 if (sd_bus_message_get_owner_uid(m, &owner) >= 0)
4198                         fprintf(f, "\towner_uid=%lu\n", (unsigned long) owner);
4199                 if (sd_bus_message_get_audit_loginuid(m, &audit_loginuid) >= 0)
4200                         fprintf(f, "\taudit_loginuid=%lu\n", (unsigned long) audit_loginuid);
4201                 if (sd_bus_message_get_audit_sessionid(m, &audit_sessionid) >= 0)
4202                         fprintf(f, "\taudit_sessionid=%lu\n", (unsigned long) audit_sessionid);
4203
4204                 r = sd_bus_message_has_effective_cap(m, 5);
4205                 if (r >= 0)
4206                         fprintf(f, "\tCAP_KILL=%s\n", yes_no(r));
4207
4208                 if (sd_bus_message_get_cmdline(m, &cmdline) >= 0) {
4209                         char **c;
4210
4211                         fputs("\tcmdline=[", f);
4212                         STRV_FOREACH(c, cmdline) {
4213                                 if (c != cmdline)
4214                                         fputc(' ', f);
4215
4216                                 fputs(*c, f);
4217                         }
4218
4219                         fputs("]\n", f);
4220                 }
4221         }
4222
4223         r = sd_bus_message_rewind(m, true);
4224         if (r < 0) {
4225                 log_error("Failed to rewind: %s", strerror(-r));
4226                 return r;
4227         }
4228
4229         fprintf(f, "BEGIN_MESSAGE \"%s\" {\n", strempty(m->root_container.signature));
4230
4231         for(;;) {
4232                 _cleanup_free_ char *prefix = NULL;
4233                 const char *contents = NULL;
4234                 char type;
4235                 union {
4236                         uint8_t u8;
4237                         uint16_t u16;
4238                         int16_t s16;
4239                         uint32_t u32;
4240                         int32_t s32;
4241                         uint64_t u64;
4242                         int64_t s64;
4243                         double d64;
4244                         const char *string;
4245                         int i;
4246                 } basic;
4247
4248                 r = sd_bus_message_peek_type(m, &type, &contents);
4249                 if (r < 0) {
4250                         log_error("Failed to peek type: %s", strerror(-r));
4251                         return r;
4252                 }
4253                 if (r == 0) {
4254                         if (level <= 1)
4255                                 break;
4256
4257                         r = sd_bus_message_exit_container(m);
4258                         if (r < 0) {
4259                                 log_error("Failed to exit container: %s", strerror(-r));
4260                                 return r;
4261                         }
4262
4263                         level--;
4264
4265                         prefix = strrep("\t", level);
4266                         if (!prefix)
4267                                 return log_oom();
4268
4269                         if (type == SD_BUS_TYPE_ARRAY)
4270                                 fprintf(f, "%s} END_ARRAY \n", prefix);
4271                         else if (type == SD_BUS_TYPE_VARIANT)
4272                                 fprintf(f, "%s} END_VARIANT\n", prefix);
4273                         else if (type == SD_BUS_TYPE_STRUCT)
4274                                 fprintf(f, "%s} END_STRUCT\n", prefix);
4275                         else if (type == SD_BUS_TYPE_DICT_ENTRY)
4276                                 fprintf(f, "%s} END_DICT_ENTRY\n", prefix);
4277
4278                         continue;
4279                 }
4280
4281                 prefix = strrep("\t", level);
4282                 if (!prefix)
4283                         return log_oom();
4284
4285                 if (bus_type_is_container(type) > 0) {
4286                         r = sd_bus_message_enter_container(m, type, contents);
4287                         if (r < 0) {
4288                                 log_error("Failed to enter container: %s", strerror(-r));
4289                                 return r;
4290                         }
4291
4292                         if (type == SD_BUS_TYPE_ARRAY)
4293                                 fprintf(f, "%sBEGIN_ARRAY \"%s\" {\n", prefix, contents);
4294                         else if (type == SD_BUS_TYPE_VARIANT)
4295                                 fprintf(f, "%sBEGIN_VARIANT \"%s\" {\n", prefix, contents);
4296                         else if (type == SD_BUS_TYPE_STRUCT)
4297                                 fprintf(f, "%sBEGIN_STRUCT \"%s\" {\n", prefix, contents);
4298                         else if (type == SD_BUS_TYPE_DICT_ENTRY)
4299                                 fprintf(f, "%sBEGIN_DICT_ENTRY \"%s\" {\n", prefix, contents);
4300
4301                         level ++;
4302
4303                         continue;
4304                 }
4305
4306                 r = sd_bus_message_read_basic(m, type, &basic);
4307                 if (r < 0) {
4308                         log_error("Failed to get basic: %s", strerror(-r));
4309                         return r;
4310                 }
4311
4312                 assert(r > 0);
4313
4314                 switch (type) {
4315
4316                 case SD_BUS_TYPE_BYTE:
4317                         fprintf(f, "%sBYTE: %u\n", prefix, basic.u8);
4318                         break;
4319
4320                 case SD_BUS_TYPE_BOOLEAN:
4321                         fprintf(f, "%sBOOLEAN: %s\n", prefix, yes_no(basic.i));
4322                         break;
4323
4324                 case SD_BUS_TYPE_INT16:
4325                         fprintf(f, "%sINT16: %i\n", prefix, basic.s16);
4326                         break;
4327
4328                 case SD_BUS_TYPE_UINT16:
4329                         fprintf(f, "%sUINT16: %u\n", prefix, basic.u16);
4330                         break;
4331
4332                 case SD_BUS_TYPE_INT32:
4333                         fprintf(f, "%sINT32: %i\n", prefix, basic.s32);
4334                         break;
4335
4336                 case SD_BUS_TYPE_UINT32:
4337                         fprintf(f, "%sUINT32: %u\n", prefix, basic.u32);
4338                         break;
4339
4340                 case SD_BUS_TYPE_INT64:
4341                         fprintf(f, "%sINT64: %lli\n", prefix, (long long) basic.s64);
4342                         break;
4343
4344                 case SD_BUS_TYPE_UINT64:
4345                         fprintf(f, "%sUINT64: %llu\n", prefix, (unsigned long long) basic.u64);
4346                         break;
4347
4348                 case SD_BUS_TYPE_DOUBLE:
4349                         fprintf(f, "%sDOUBLE: %g\n", prefix, basic.d64);
4350                         break;
4351
4352                 case SD_BUS_TYPE_STRING:
4353                         fprintf(f, "%sSTRING: \"%s\"\n", prefix, basic.string);
4354                         break;
4355
4356                 case SD_BUS_TYPE_OBJECT_PATH:
4357                         fprintf(f, "%sOBJECT_PATH: \"%s\"\n", prefix, basic.string);
4358                         break;
4359
4360                 case SD_BUS_TYPE_SIGNATURE:
4361                         fprintf(f, "%sSIGNATURE: \"%s\"\n", prefix, basic.string);
4362                         break;
4363
4364                 case SD_BUS_TYPE_UNIX_FD:
4365                         fprintf(f, "%sUNIX_FD: %i\n", prefix, basic.i);
4366                         break;
4367
4368                 default:
4369                         assert_not_reached("Unknown basic type.");
4370                 }
4371         }
4372
4373         fprintf(f, "} END_MESSAGE\n");
4374         return 0;
4375 }
4376
4377 int bus_message_get_blob(sd_bus_message *m, void **buffer, size_t *sz) {
4378         size_t total;
4379         void *p, *e;
4380         unsigned i;
4381         struct bus_body_part *part;
4382
4383         assert(m);
4384         assert(buffer);
4385         assert(sz);
4386
4387         total = BUS_MESSAGE_SIZE(m);
4388
4389         p = malloc(total);
4390         if (!p)
4391                 return -ENOMEM;
4392
4393         e = mempcpy(p, m->header, BUS_MESSAGE_BODY_BEGIN(m));
4394         MESSAGE_FOREACH_PART(part, i, m)
4395                 e = mempcpy(e, part->data, part->size);
4396
4397         assert(total == (size_t) ((uint8_t*) e - (uint8_t*) p));
4398
4399         *buffer = p;
4400         *sz = total;
4401
4402         return 0;
4403 }
4404
4405 int bus_message_read_strv_extend(sd_bus_message *m, char ***l) {
4406         int r;
4407
4408         assert(m);
4409         assert(l);
4410
4411         r = sd_bus_message_enter_container(m, 'a', "s");
4412         if (r <= 0)
4413                 return r;
4414
4415         for (;;) {
4416                 const char *s;
4417
4418                 r = sd_bus_message_read_basic(m, 's', &s);
4419                 if (r < 0)
4420                         return r;
4421                 if (r == 0)
4422                         break;
4423
4424                 r = strv_extend(l, s);
4425                 if (r < 0)
4426                         return r;
4427         }
4428
4429         r = sd_bus_message_exit_container(m);
4430         if (r < 0)
4431                 return r;
4432
4433         return 0;
4434 }
4435
4436 _public_ int sd_bus_message_read_strv(sd_bus_message *m, char ***l) {
4437         char **strv = NULL;
4438         int r;
4439
4440         assert_return(m, -EINVAL);
4441         assert_return(m->sealed, -EPERM);
4442         assert_return(l, -EINVAL);
4443
4444         r = bus_message_read_strv_extend(m, &strv);
4445         if (r <= 0) {
4446                 strv_free(strv);
4447                 return r;
4448         }
4449
4450         *l = strv;
4451         return 1;
4452 }
4453
4454 const char* bus_message_get_arg(sd_bus_message *m, unsigned i) {
4455         int r;
4456         const char *t = NULL;
4457         unsigned j;
4458
4459         assert(m);
4460
4461         r = sd_bus_message_rewind(m, true);
4462         if (r < 0)
4463                 return NULL;
4464
4465         for (j = 0; j <= i; j++) {
4466                 char type;
4467
4468                 r = sd_bus_message_peek_type(m, &type, NULL);
4469                 if (r < 0)
4470                         return NULL;
4471
4472                 if (type != SD_BUS_TYPE_STRING &&
4473                     type != SD_BUS_TYPE_OBJECT_PATH &&
4474                     type != SD_BUS_TYPE_SIGNATURE)
4475                         return NULL;
4476
4477                 r = sd_bus_message_read_basic(m, type, &t);
4478                 if (r < 0)
4479                         return NULL;
4480         }
4481
4482         return t;
4483 }
4484
4485 bool bus_header_is_complete(struct bus_header *h, size_t size) {
4486         size_t full;
4487
4488         assert(h);
4489         assert(size);
4490
4491         if (size < sizeof(struct bus_header))
4492                 return false;
4493
4494         full = sizeof(struct bus_header) +
4495                 (h->endian == SD_BUS_NATIVE_ENDIAN ? h->fields_size : bswap_32(h->fields_size));
4496
4497         return size >= full;
4498 }
4499
4500 int bus_header_message_size(struct bus_header *h, size_t *sum) {
4501         size_t fs, bs;
4502
4503         assert(h);
4504         assert(sum);
4505
4506         if (h->endian == SD_BUS_NATIVE_ENDIAN) {
4507                 fs = h->fields_size;
4508                 bs = h->body_size;
4509         } else if (h->endian == SD_BUS_REVERSE_ENDIAN) {
4510                 fs = bswap_32(h->fields_size);
4511                 bs = bswap_32(h->body_size);
4512         } else
4513                 return -EBADMSG;
4514
4515         *sum = sizeof(struct bus_header) + ALIGN8(fs) + bs;
4516         return 0;
4517 }
4518
4519 _public_ int sd_bus_message_get_errno(sd_bus_message *m) {
4520         assert_return(m, -EINVAL);
4521
4522         if (m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
4523                 return 0;
4524
4525         return sd_bus_error_get_errno(&m->error);
4526 }
4527
4528 _public_ const char* sd_bus_message_get_signature(sd_bus_message *m, int complete) {
4529         struct bus_container *c;
4530
4531         assert_return(m, NULL);
4532
4533         c = complete ? &m->root_container : message_get_container(m);
4534         return c->signature ?: "";
4535 }
4536
4537 _public_ int sd_bus_message_copy(sd_bus_message *m, sd_bus_message *source, int all) {
4538         bool done_something = false;
4539         int r;
4540
4541         assert_return(m, -EINVAL);
4542         assert_return(source, -EINVAL);
4543         assert_return(!m->sealed, -EPERM);
4544         assert_return(source->sealed, -EPERM);
4545
4546         do {
4547                 const char *contents;
4548                 char type;
4549                 union {
4550                         uint8_t u8;
4551                         uint16_t u16;
4552                         int16_t s16;
4553                         uint32_t u32;
4554                         int32_t s32;
4555                         uint64_t u64;
4556                         int64_t s64;
4557                         double d64;
4558                         const char *string;
4559                         int i;
4560                 } basic;
4561
4562                 r = sd_bus_message_peek_type(source, &type, &contents);
4563                 if (r < 0)
4564                         return r;
4565                 if (r == 0)
4566                         break;
4567
4568                 done_something = true;
4569
4570                 if (bus_type_is_container(type) > 0) {
4571
4572                         r = sd_bus_message_enter_container(source, type, contents);
4573                         if (r < 0)
4574                                 return r;
4575
4576                         r = sd_bus_message_open_container(m, type, contents);
4577                         if (r < 0)
4578                                 return r;
4579
4580                         r = sd_bus_message_copy(m, source, true);
4581                         if (r < 0)
4582                                 return r;
4583
4584                         r = sd_bus_message_close_container(m);
4585                         if (r < 0)
4586                                 return r;
4587
4588                         r = sd_bus_message_exit_container(source);
4589                         if (r < 0)
4590                                 return r;
4591
4592                         continue;
4593                 }
4594
4595                 r = sd_bus_message_read_basic(source, type, &basic);
4596                 if (r < 0)
4597                         return r;
4598
4599                 assert(r > 0);
4600
4601                 if (type == SD_BUS_TYPE_OBJECT_PATH ||
4602                     type == SD_BUS_TYPE_SIGNATURE ||
4603                     type == SD_BUS_TYPE_STRING)
4604                         r = sd_bus_message_append_basic(m, type, basic.string);
4605                 else
4606                         r = sd_bus_message_append_basic(m, type, &basic);
4607
4608                 if (r < 0)
4609                         return r;
4610
4611         } while (all);
4612
4613         return done_something;
4614 }
4615
4616 _public_ int sd_bus_message_verify_type(sd_bus_message *m, char type, const char *contents) {
4617         const char *c;
4618         char t;
4619         int r;
4620
4621         assert_return(m, -EINVAL);
4622         assert_return(m->sealed, -EPERM);
4623         assert_return(!type || bus_type_is_valid(type), -EINVAL);
4624         assert_return(!contents || signature_is_valid(contents, true), -EINVAL);
4625         assert_return(type || contents, -EINVAL);
4626         assert_return(!contents || !type || bus_type_is_container(type), -EINVAL);
4627
4628         r = sd_bus_message_peek_type(m, &t, &c);
4629         if (r <= 0)
4630                 return r;
4631
4632         if (type != 0 && type != t)
4633                 return 0;
4634
4635         if (contents && !streq_ptr(contents, c))
4636                 return 0;
4637
4638         return 1;
4639 }