chiark / gitweb /
kdbus: parse cgroup meta data, too
[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
25 #include "util.h"
26 #include "utf8.h"
27 #include "strv.h"
28 #include "time-util.h"
29
30 #include "sd-bus.h"
31 #include "bus-message.h"
32 #include "bus-internal.h"
33 #include "bus-type.h"
34 #include "bus-signature.h"
35
36 static int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored);
37
38 static void reset_containers(sd_bus_message *m) {
39         unsigned i;
40
41         assert(m);
42
43         for (i = 0; i < m->n_containers; i++)
44                 free(m->containers[i].signature);
45
46         free(m->containers);
47         m->containers = NULL;
48
49         m->n_containers = 0;
50         m->root_container.index = 0;
51 }
52
53 static void message_free(sd_bus_message *m) {
54         assert(m);
55
56         if (m->free_header)
57                 free(m->header);
58
59         if (m->free_fields)
60                 free(m->fields);
61
62         if (m->free_body)
63                 free(m->body);
64
65         if (m->free_kdbus)
66                 free(m->kdbus);
67
68         if (m->free_fds) {
69                 close_many(m->fds, m->n_fds);
70                 free(m->fds);
71         }
72
73         free(m->cmdline_array);
74
75         reset_containers(m);
76         free(m->root_container.signature);
77
78         free(m->peeked_signature);
79         free(m);
80 }
81
82 static void* buffer_extend(void **p, uint32_t *sz, size_t align, size_t extend) {
83         size_t start, n;
84         void *k;
85
86         assert(p);
87         assert(sz);
88         assert(align > 0);
89
90         start = ALIGN_TO((size_t) *sz, align);
91         n = start + extend;
92
93         if (n == *sz)
94                 return (uint8_t*) *p + start;
95
96         if (n > (size_t) ((uint32_t) -1))
97                 return NULL;
98
99         k = realloc(*p, n);
100         if (!k)
101                 return NULL;
102
103         /* Zero out padding */
104         if (start > *sz)
105                 memset((uint8_t*) k + *sz, 0, start - *sz);
106
107         *p = k;
108         *sz = n;
109
110         return (uint8_t*) k + start;
111 }
112
113 static void *message_extend_fields(sd_bus_message *m, size_t align, size_t sz) {
114         void *p, *o;
115
116         assert(m);
117
118         o = m->fields;
119         p = buffer_extend(&m->fields, &m->header->fields_size, align, sz);
120         if (!p)
121                 return NULL;
122
123         if (o != m->fields) {
124                 /* Adjust quick access pointers */
125
126                 if (m->path)
127                         m->path = (const char*) m->fields + (m->path - (const char*) o);
128                 if (m->interface)
129                         m->interface = (const char*) m->fields + (m->interface - (const char*) o);
130                 if (m->member)
131                         m->member = (const char*) m->fields + (m->member - (const char*) o);
132                 if (m->destination)
133                         m->destination = (const char*) m->fields + (m->destination - (const char*) o);
134                 if (m->sender)
135                         m->sender = (const char*) m->fields + (m->sender - (const char*) o);
136                 if (m->error.name)
137                         m->error.name = (const char*) m->fields + (m->error.name - (const char*) o);
138         }
139
140         m->free_fields = true;
141
142         return p;
143 }
144
145 static int message_append_field_string(
146                 sd_bus_message *m,
147                 uint8_t h,
148                 char type,
149                 const char *s,
150                 const char **ret) {
151
152         size_t l;
153         uint8_t *p;
154
155         assert(m);
156
157         l = strlen(s);
158         if (l > (size_t) (uint32_t) -1)
159                 return -EINVAL;
160
161         /* field id byte + signature length + signature 's' + NUL + string length + string + NUL */
162         p = message_extend_fields(m, 8, 4 + 4 + l + 1);
163         if (!p)
164                 return -ENOMEM;
165
166         p[0] = h;
167         p[1] = 1;
168         p[2] = type;
169         p[3] = 0;
170
171         ((uint32_t*) p)[1] = l;
172         memcpy(p + 8, s, l + 1);
173
174         if (ret)
175                 *ret = (const char*) p + 8;
176
177         return 0;
178 }
179
180 static int message_append_field_signature(
181                 sd_bus_message *m,
182                 uint8_t h,
183                 const char *s,
184                 const char **ret) {
185
186         size_t l;
187         uint8_t *p;
188
189         assert(m);
190
191         l = strlen(s);
192         if (l > 255)
193                 return -EINVAL;
194
195         /* field id byte + signature length + signature 'g' + NUL + string length + string + NUL */
196         p = message_extend_fields(m, 8, 4 + 1 + l + 1);
197         if (!p)
198                 return -ENOMEM;
199
200         p[0] = h;
201         p[1] = 1;
202         p[2] = SD_BUS_TYPE_SIGNATURE;
203         p[3] = 0;
204         p[4] = l;
205         memcpy(p + 5, s, l + 1);
206
207         if (ret)
208                 *ret = (const char*) p + 5;
209
210         return 0;
211 }
212
213 static int message_append_field_uint32(sd_bus_message *m, uint8_t h, uint32_t x) {
214         uint8_t *p;
215
216         assert(m);
217
218         /* field id byte + signature length + signature 'u' + NUL + value */
219         p = message_extend_fields(m, 8, 4 + 4);
220         if (!p)
221                 return -ENOMEM;
222
223         p[0] = h;
224         p[1] = 1;
225         p[2] = SD_BUS_TYPE_UINT32;
226         p[3] = 0;
227
228         ((uint32_t*) p)[1] = x;
229
230         return 0;
231 }
232
233 int bus_message_from_header(
234                 void *buffer,
235                 size_t length,
236                 int *fds,
237                 unsigned n_fds,
238                 const struct ucred *ucred,
239                 const char *label,
240                 size_t extra,
241                 sd_bus_message **ret) {
242
243         sd_bus_message *m;
244         struct bus_header *h;
245         size_t a, label_sz;
246
247         assert(buffer || length <= 0);
248         assert(fds || n_fds <= 0);
249         assert(ret);
250
251         if (length < sizeof(struct bus_header))
252                 return -EBADMSG;
253
254         h = buffer;
255         if (h->version != 1)
256                 return -EBADMSG;
257
258         if (h->serial == 0)
259                 return -EBADMSG;
260
261         if (h->type == _SD_BUS_MESSAGE_TYPE_INVALID)
262                 return -EBADMSG;
263
264         if (h->endian != SD_BUS_LITTLE_ENDIAN &&
265             h->endian != SD_BUS_BIG_ENDIAN)
266                 return -EBADMSG;
267
268         a = ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
269
270         if (label) {
271                 label_sz = strlen(label);
272                 a += label_sz + 1;
273         }
274
275         m = malloc0(a);
276         if (!m)
277                 return -ENOMEM;
278
279         m->n_ref = 1;
280         m->sealed = true;
281         m->header = h;
282         m->fds = fds;
283         m->n_fds = n_fds;
284
285         if (ucred) {
286                 m->uid = ucred->uid;
287                 m->pid = ucred->pid;
288                 m->gid = ucred->gid;
289                 m->uid_valid = m->gid_valid = true;
290         }
291
292         if (label) {
293                 m->label = (char*) m + ALIGN(sizeof(sd_bus_message)) + ALIGN(extra);
294                 memcpy(m->label, label, label_sz + 1);
295         }
296
297         *ret = m;
298         return 0;
299 }
300
301 int bus_message_from_malloc(
302                 void *buffer,
303                 size_t length,
304                 int *fds,
305                 unsigned n_fds,
306                 const struct ucred *ucred,
307                 const char *label,
308                 sd_bus_message **ret) {
309
310         sd_bus_message *m;
311         int r;
312
313         r = bus_message_from_header(buffer, length, fds, n_fds, ucred, label, 0, &m);
314         if (r < 0)
315                 return r;
316
317         if (length != BUS_MESSAGE_SIZE(m)) {
318                 r = -EBADMSG;
319                 goto fail;
320         }
321
322         m->fields = (uint8_t*) buffer + sizeof(struct bus_header);
323         m->body = (uint8_t*) buffer + sizeof(struct bus_header) + ALIGN8(BUS_MESSAGE_FIELDS_SIZE(m));
324
325         m->n_iovec = 1;
326         m->iovec[0].iov_base = buffer;
327         m->iovec[0].iov_len = length;
328
329         r = bus_message_parse_fields(m);
330         if (r < 0)
331                 goto fail;
332
333         /* We take possession of the memory and fds now */
334         m->free_header = true;
335         m->free_fds = true;
336
337         *ret = m;
338         return 0;
339
340 fail:
341         message_free(m);
342         return r;
343 }
344
345 static sd_bus_message *message_new(sd_bus *bus, uint8_t type) {
346         sd_bus_message *m;
347
348         m = malloc0(ALIGN(sizeof(sd_bus_message)) + sizeof(struct bus_header));
349         if (!m)
350                 return NULL;
351
352         m->n_ref = 1;
353         m->header = (struct bus_header*) ((uint8_t*) m + ALIGN(sizeof(struct sd_bus_message)));
354         m->header->endian = SD_BUS_NATIVE_ENDIAN;
355         m->header->type = type;
356         m->header->version = bus ? bus->message_version : 1;
357         m->allow_fds = !bus || bus->can_fds || (bus->state != BUS_HELLO && bus->state != BUS_RUNNING);
358
359         return m;
360 }
361
362 int sd_bus_message_new_signal(
363                 sd_bus *bus,
364                 const char *path,
365                 const char *interface,
366                 const char *member,
367                 sd_bus_message **m) {
368
369         sd_bus_message *t;
370         int r;
371
372         if (!path)
373                 return -EINVAL;
374         if (!interface)
375                 return -EINVAL;
376         if (!member)
377                 return -EINVAL;
378         if (!m)
379                 return -EINVAL;
380         if (bus && bus->state == BUS_UNSET)
381                 return -ENOTCONN;
382
383         t = message_new(bus, SD_BUS_MESSAGE_TYPE_SIGNAL);
384         if (!t)
385                 return -ENOMEM;
386
387         t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
388
389         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
390         if (r < 0)
391                 goto fail;
392         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
393         if (r < 0)
394                 goto fail;
395         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
396         if (r < 0)
397                 goto fail;
398
399         *m = t;
400         return 0;
401
402 fail:
403         sd_bus_message_unref(t);
404         return r;
405 }
406
407 int sd_bus_message_new_method_call(
408                 sd_bus *bus,
409                 const char *destination,
410                 const char *path,
411                 const char *interface,
412                 const char *member,
413                 sd_bus_message **m) {
414
415         sd_bus_message *t;
416         int r;
417
418         if (!path)
419                 return -EINVAL;
420         if (!member)
421                 return -EINVAL;
422         if (!m)
423                 return -EINVAL;
424         if (bus && bus->state == BUS_UNSET)
425                 return -ENOTCONN;
426
427         t = message_new(bus, SD_BUS_MESSAGE_TYPE_METHOD_CALL);
428         if (!t)
429                 return -ENOMEM;
430
431         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_PATH, SD_BUS_TYPE_OBJECT_PATH, path, &t->path);
432         if (r < 0)
433                 goto fail;
434         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_MEMBER, SD_BUS_TYPE_STRING, member, &t->member);
435         if (r < 0)
436                 goto fail;
437
438         if (interface) {
439                 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_INTERFACE, SD_BUS_TYPE_STRING, interface, &t->interface);
440                 if (r < 0)
441                         goto fail;
442         }
443
444         if (destination) {
445                 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &t->destination);
446                 if (r < 0)
447                         goto fail;
448         }
449
450         *m = t;
451         return 0;
452
453 fail:
454         message_free(t);
455         return r;
456 }
457
458 static int message_new_reply(
459                 sd_bus *bus,
460                 sd_bus_message *call,
461                 uint8_t type,
462                 sd_bus_message **m) {
463
464         sd_bus_message *t;
465         int r;
466
467         if (!call)
468                 return -EINVAL;
469         if (!call->sealed)
470                 return -EPERM;
471         if (call->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
472                 return -EINVAL;
473         if (!m)
474                 return -EINVAL;
475         if (bus && bus->state == BUS_UNSET)
476                 return -ENOTCONN;
477
478         t = message_new(bus, type);
479         if (!t)
480                 return -ENOMEM;
481
482         t->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
483         t->reply_serial = BUS_MESSAGE_SERIAL(call);
484
485         r = message_append_field_uint32(t, SD_BUS_MESSAGE_HEADER_REPLY_SERIAL, t->reply_serial);
486         if (r < 0)
487                 goto fail;
488
489         if (call->sender) {
490                 r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, call->sender, &t->sender);
491                 if (r < 0)
492                         goto fail;
493         }
494
495         t->dont_send = !!(call->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED);
496
497         *m = t;
498         return 0;
499
500 fail:
501         message_free(t);
502         return r;
503 }
504
505 int sd_bus_message_new_method_return(
506                 sd_bus *bus,
507                 sd_bus_message *call,
508                 sd_bus_message **m) {
509
510         return message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_RETURN, m);
511 }
512
513 int sd_bus_message_new_method_error(
514                 sd_bus *bus,
515                 sd_bus_message *call,
516                 const sd_bus_error *e,
517                 sd_bus_message **m) {
518
519         sd_bus_message *t;
520         int r;
521
522         if (!sd_bus_error_is_set(e))
523                 return -EINVAL;
524         if (!m)
525                 return -EINVAL;
526
527         r = message_new_reply(bus, call, SD_BUS_MESSAGE_TYPE_METHOD_ERROR, &t);
528         if (r < 0)
529                 return r;
530
531         r = message_append_field_string(t, SD_BUS_MESSAGE_HEADER_ERROR_NAME, SD_BUS_TYPE_STRING, e->name, &t->error.name);
532         if (r < 0)
533                 goto fail;
534
535         if (e->message) {
536                 r = message_append_basic(t, SD_BUS_TYPE_STRING, e->message, (const void**) &t->error.message);
537                 if (r < 0)
538                         goto fail;
539         }
540
541         *m = t;
542         return 0;
543
544 fail:
545         message_free(t);
546         return r;
547 }
548
549 sd_bus_message* sd_bus_message_ref(sd_bus_message *m) {
550         if (!m)
551                 return NULL;
552
553         assert(m->n_ref > 0);
554         m->n_ref++;
555
556         return m;
557 }
558
559 sd_bus_message* sd_bus_message_unref(sd_bus_message *m) {
560         if (!m)
561                 return NULL;
562
563         assert(m->n_ref > 0);
564         m->n_ref--;
565
566         if (m->n_ref <= 0)
567                 message_free(m);
568
569         return NULL;
570 }
571
572 int sd_bus_message_get_type(sd_bus_message *m, uint8_t *type) {
573         if (!m)
574                 return -EINVAL;
575         if (!type)
576                 return -EINVAL;
577
578         *type = m->header->type;
579         return 0;
580 }
581
582 int sd_bus_message_get_serial(sd_bus_message *m, uint64_t *serial) {
583         if (!m)
584                 return -EINVAL;
585         if (!serial)
586                 return -EINVAL;
587         if (m->header->serial == 0)
588                 return -ENOENT;
589
590         *serial = BUS_MESSAGE_SERIAL(m);
591         return 0;
592 }
593
594 int sd_bus_message_get_reply_serial(sd_bus_message *m, uint64_t *serial) {
595         if (!m)
596                 return -EINVAL;
597         if (!serial)
598                 return -EINVAL;
599         if (m->reply_serial == 0)
600                 return -ENOENT;
601
602         *serial = m->reply_serial;
603         return 0;
604 }
605
606 int sd_bus_message_get_no_reply(sd_bus_message *m) {
607         if (!m)
608                 return -EINVAL;
609
610         return m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_CALL ? !!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED) : 0;
611 }
612
613 const char *sd_bus_message_get_path(sd_bus_message *m) {
614         if (!m)
615                 return NULL;
616
617         return m->path;
618 }
619
620 const char *sd_bus_message_get_interface(sd_bus_message *m) {
621         if (!m)
622                 return NULL;
623
624         return m->interface;
625 }
626
627 const char *sd_bus_message_get_member(sd_bus_message *m) {
628         if (!m)
629                 return NULL;
630
631         return m->member;
632 }
633 const char *sd_bus_message_get_destination(sd_bus_message *m) {
634         if (!m)
635                 return NULL;
636
637         return m->destination;
638 }
639
640 const char *sd_bus_message_get_sender(sd_bus_message *m) {
641         if (!m)
642                 return NULL;
643
644         return m->sender;
645 }
646
647 const sd_bus_error *sd_bus_message_get_error(sd_bus_message *m) {
648         if (!m)
649                 return NULL;
650
651         if (!sd_bus_error_is_set(&m->error))
652                 return NULL;
653
654         return &m->error;
655 }
656
657 int sd_bus_message_get_uid(sd_bus_message *m, uid_t *uid) {
658         if (!m)
659                 return -EINVAL;
660         if (!uid)
661                 return -EINVAL;
662         if (!m->uid_valid)
663                 return -ESRCH;
664
665         *uid = m->uid;
666         return 0;
667 }
668
669 int sd_bus_message_get_gid(sd_bus_message *m, gid_t *gid) {
670         if (!m)
671                 return -EINVAL;
672         if (!gid)
673                 return -EINVAL;
674         if (!m->gid_valid)
675                 return -ESRCH;
676
677         *gid = m->gid;
678         return 0;
679 }
680
681 int sd_bus_message_get_pid(sd_bus_message *m, pid_t *pid) {
682         if (!m)
683                 return -EINVAL;
684         if (!pid)
685                 return -EINVAL;
686         if (m->pid <= 0)
687                 return -ESRCH;
688
689         *pid = m->pid;
690         return 0;
691 }
692
693 int sd_bus_message_get_tid(sd_bus_message *m, pid_t *tid) {
694         if (!m)
695                 return -EINVAL;
696         if (!tid)
697                 return -EINVAL;
698         if (m->tid <= 0)
699                 return -ESRCH;
700
701         *tid = m->tid;
702         return 0;
703 }
704
705 int sd_bus_message_get_pid_starttime(sd_bus_message *m, uint64_t *usec) {
706         if (!m)
707                 return -EINVAL;
708         if (!usec)
709                 return -EINVAL;
710         if (m->pid_starttime <= 0)
711                 return -ESRCH;
712
713         *usec = m->pid_starttime;
714         return 0;
715 }
716
717 int sd_bus_message_get_selinux_context(sd_bus_message *m, const char **ret) {
718         if (!m)
719                 return -EINVAL;
720         if (!m->label)
721                 return -ESRCH;
722
723         *ret = m->label;
724         return 0;
725 }
726
727 int sd_bus_message_get_monotonic_timestamp(sd_bus_message *m, uint64_t *usec) {
728         if (!m)
729                 return -EINVAL;
730         if (!usec)
731                 return -EINVAL;
732         if (m->monotonic <= 0)
733                 return -ESRCH;
734
735         *usec = m->monotonic;
736         return 0;
737 }
738
739 int sd_bus_message_get_realtime_timestamp(sd_bus_message *m, uint64_t *usec) {
740         if (!m)
741                 return -EINVAL;
742         if (!usec)
743                 return -EINVAL;
744         if (m->realtime <= 0)
745                 return -ESRCH;
746
747         *usec = m->realtime;
748         return 0;
749 }
750
751 int sd_bus_message_get_comm(sd_bus_message *m, const char **ret) {
752         if (!m)
753                 return -EINVAL;
754         if (!ret)
755                 return -EINVAL;
756         if (!m->comm)
757                 return -ESRCH;
758
759         *ret = m->comm;
760         return 0;
761 }
762
763 int sd_bus_message_get_tid_comm(sd_bus_message *m, const char **ret) {
764         if (!m)
765                 return -EINVAL;
766         if (!ret)
767                 return -EINVAL;
768         if (!m->tid_comm)
769                 return -ESRCH;
770
771         *ret = m->tid_comm;
772         return 0;
773 }
774
775 int sd_bus_message_get_exe(sd_bus_message *m, const char **ret) {
776         if (!m)
777                 return -EINVAL;
778         if (!ret)
779                 return -EINVAL;
780         if (!m->exe)
781                 return -ESRCH;
782
783         *ret = m->exe;
784         return 0;
785 }
786
787 int sd_bus_message_get_cgroup(sd_bus_message *m, const char **ret) {
788         if (!m)
789                 return -EINVAL;
790         if (!ret)
791                 return -EINVAL;
792         if (!m->cgroup)
793                 return -ESRCH;
794
795         *ret = m->cgroup;
796         return 0;
797 }
798
799 int sd_bus_message_get_cmdline(sd_bus_message *m, char ***cmdline) {
800         size_t n, i;
801         const char *p;
802         bool first;
803
804         if (!m)
805                 return -EINVAL;
806
807         if (!m->cmdline)
808                 return -ENOENT;
809
810         for (p = m->cmdline, n = 0; p < m->cmdline + m->cmdline_length; p++)
811                 if (*p == 0)
812                         n++;
813
814         m->cmdline_array = new(char*, n + 1);
815         if (!m->cmdline_array)
816                 return -ENOMEM;
817
818         for (p = m->cmdline, i = 0, first = true; p < m->cmdline + m->cmdline_length; p++) {
819                 if (first)
820                         m->cmdline_array[i++] = (char*) p;
821
822                 first = *p == 0;
823         }
824
825         m->cmdline_array[i] = NULL;
826         *cmdline = m->cmdline_array;
827
828         return 0;
829 }
830
831 int sd_bus_message_is_signal(sd_bus_message *m, const char *interface, const char *member) {
832         if (!m)
833                 return -EINVAL;
834
835         if (m->header->type != SD_BUS_MESSAGE_TYPE_SIGNAL)
836                 return 0;
837
838         if (interface && (!m->interface || !streq(m->interface, interface)))
839                 return 0;
840
841         if (member &&  (!m->member || !streq(m->member, member)))
842                 return 0;
843
844         return 1;
845 }
846
847 int sd_bus_message_is_method_call(sd_bus_message *m, const char *interface, const char *member) {
848         if (!m)
849                 return -EINVAL;
850
851         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
852                 return 0;
853
854         if (interface && (!m->interface || !streq(m->interface, interface)))
855                 return 0;
856
857         if (member &&  (!m->member || !streq(m->member, member)))
858                 return 0;
859
860         return 1;
861 }
862
863 int sd_bus_message_is_method_error(sd_bus_message *m, const char *name) {
864         if (!m)
865                 return -EINVAL;
866
867         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
868                 return 0;
869
870         if (name && (!m->error.name || !streq(m->error.name, name)))
871                 return 0;
872
873         return 1;
874 }
875
876 int sd_bus_message_set_no_reply(sd_bus_message *m, int b) {
877         if (!m)
878                 return -EINVAL;
879         if (m->sealed)
880                 return -EPERM;
881         if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_CALL)
882                 return -EPERM;
883
884         if (b)
885                 m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
886         else
887                 m->header->flags &= ~SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
888
889         return 0;
890 }
891
892 static struct bus_container *message_get_container(sd_bus_message *m) {
893         assert(m);
894
895         if (m->n_containers == 0)
896                 return &m->root_container;
897
898         assert(m->containers);
899         return m->containers + m->n_containers - 1;
900 }
901
902 static void *message_extend_body(sd_bus_message *m, size_t align, size_t sz) {
903         void *p, *o;
904         size_t added;
905         struct bus_container *c;
906
907         assert(m);
908         assert(align > 0);
909
910         o = m->body;
911         added = m->header->body_size;
912
913         p = buffer_extend(&m->body, &m->header->body_size, align, sz);
914         if (!p)
915                 return NULL;
916
917         added = m->header->body_size - added;
918
919         for (c = m->containers; c < m->containers + m->n_containers; c++)
920                 if (c->array_size) {
921                         c->array_size = (uint32_t*) ((uint8_t*) m->body + ((uint8_t*) c->array_size - (uint8_t*) o));
922                         *c->array_size += added;
923                 }
924
925         if (o != m->body) {
926                 if (m->error.message)
927                         m->error.message = (const char*) m->body + (m->error.message - (const char*) o);
928         }
929
930         m->free_body = true;
931
932         return p;
933 }
934
935 int message_append_basic(sd_bus_message *m, char type, const void *p, const void **stored) {
936         struct bus_container *c;
937         ssize_t align, sz;
938         uint32_t k;
939         void *a;
940         char *e = NULL;
941         int fd = -1;
942         uint32_t fdi = 0;
943         int r;
944
945         if (!m)
946                 return -EINVAL;
947         if (!p)
948                 return -EINVAL;
949         if (m->sealed)
950                 return -EPERM;
951         if (!bus_type_is_basic(type))
952                 return -EINVAL;
953
954         c = message_get_container(m);
955
956         if (c->signature && c->signature[c->index]) {
957                 /* Container signature is already set */
958
959                 if (c->signature[c->index] != type)
960                         return -ENXIO;
961         } else {
962                 /* Maybe we can append to the signature? But only if this is the top-level container*/
963                 if (c->enclosing != 0)
964                         return -ENXIO;
965
966                 e = strextend(&c->signature, CHAR_TO_STR(type), NULL);
967                 if (!e)
968                         return -ENOMEM;
969         }
970
971         switch (type) {
972
973         case SD_BUS_TYPE_STRING:
974         case SD_BUS_TYPE_OBJECT_PATH:
975
976                 align = 4;
977                 sz = 4 + strlen(p) + 1;
978                 break;
979
980         case SD_BUS_TYPE_SIGNATURE:
981
982                 align = 1;
983                 sz = 1 + strlen(p) + 1;
984                 break;
985
986         case SD_BUS_TYPE_BOOLEAN:
987                 align = sz = 4;
988
989                 assert_cc(sizeof(int) == sizeof(uint32_t));
990                 memcpy(&k, p, 4);
991                 k = !!k;
992                 p = &k;
993                 break;
994
995         case SD_BUS_TYPE_UNIX_FD: {
996                 int z, *f;
997
998                 if (!m->allow_fds) {
999                         r = -ENOTSUP;
1000                         goto fail;
1001                 }
1002
1003                 align = sz = 4;
1004
1005                 z = *(int*) p;
1006                 if (z < 0) {
1007                         r = -EINVAL;
1008                         goto fail;
1009                 }
1010
1011                 fd = fcntl(z, F_DUPFD_CLOEXEC, 3);
1012                 if (fd < 0) {
1013                         r = -errno;
1014                         goto fail;
1015                 }
1016
1017                 f = realloc(m->fds, sizeof(int) * (m->n_fds + 1));
1018                 if (!f) {
1019                         r = -ENOMEM;
1020                         goto fail;
1021                 }
1022
1023                 fdi = m->n_fds;
1024                 f[fdi] = fd;
1025                 m->fds = f;
1026                 m->free_fds = true;
1027                 break;
1028         }
1029
1030         default:
1031                 align = bus_type_get_alignment(type);
1032                 sz = bus_type_get_size(type);
1033                 break;
1034         }
1035
1036         assert(align > 0);
1037         assert(sz > 0);
1038
1039         a = message_extend_body(m, align, sz);
1040         if (!a) {
1041                 r = -ENOMEM;
1042                 goto fail;
1043         }
1044
1045         if (type == SD_BUS_TYPE_STRING || type == SD_BUS_TYPE_OBJECT_PATH) {
1046                 *(uint32_t*) a = sz - 5;
1047                 memcpy((uint8_t*) a + 4, p, sz - 4);
1048
1049                 if (stored)
1050                         *stored = (const uint8_t*) a + 4;
1051
1052         } else if (type == SD_BUS_TYPE_SIGNATURE) {
1053                 *(uint8_t*) a = sz - 1;
1054                 memcpy((uint8_t*) a + 1, p, sz - 1);
1055
1056                 if (stored)
1057                         *stored = (const uint8_t*) a + 1;
1058         } else if (type == SD_BUS_TYPE_UNIX_FD) {
1059                 *(uint32_t*) a = fdi;
1060
1061                 if (stored)
1062                         *stored = a;
1063
1064                 m->n_fds ++;
1065
1066         } else {
1067                 memcpy(a, p, sz);
1068
1069                 if (stored)
1070                         *stored = a;
1071         }
1072
1073         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1074                 c->index++;
1075
1076         return 0;
1077
1078 fail:
1079         /* Truncate extended signature again */
1080         if (e)
1081                 c->signature[c->index] = 0;
1082
1083         if (fd >= 0)
1084                 close_nointr_nofail(fd);
1085
1086         return r;
1087 }
1088
1089 int sd_bus_message_append_basic(sd_bus_message *m, char type, const void *p) {
1090         return message_append_basic(m, type, p, NULL);
1091 }
1092
1093 static int bus_message_open_array(
1094                 sd_bus_message *m,
1095                 struct bus_container *c,
1096                 const char *contents,
1097                 uint32_t **array_size) {
1098
1099         unsigned nindex;
1100         char *e = NULL;
1101         void *a, *b;
1102         int alignment;
1103         size_t saved;
1104
1105         assert(m);
1106         assert(c);
1107         assert(contents);
1108         assert(array_size);
1109
1110         if (!signature_is_single(contents))
1111                 return -EINVAL;
1112
1113         alignment = bus_type_get_alignment(contents[0]);
1114         if (alignment < 0)
1115                 return alignment;
1116
1117         if (c->signature && c->signature[c->index]) {
1118
1119                 /* Verify the existing signature */
1120
1121                 if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
1122                         return -ENXIO;
1123
1124                 if (!startswith(c->signature + c->index + 1, contents))
1125                         return -ENXIO;
1126
1127                 nindex = c->index + 1 + strlen(contents);
1128         } else {
1129                 if (c->enclosing != 0)
1130                         return -ENXIO;
1131
1132                 /* Extend the existing signature */
1133
1134                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_ARRAY), contents, NULL);
1135                 if (!e)
1136                         return -ENOMEM;
1137
1138                 nindex = e - c->signature;
1139         }
1140
1141         saved = m->header->body_size;
1142         a = message_extend_body(m, 4, 4);
1143         if (!a) {
1144                 /* Truncate extended signature again */
1145                 if (e)
1146                         c->signature[c->index] = 0;
1147
1148                 return -ENOMEM;
1149         }
1150         b = m->body;
1151
1152         if (!message_extend_body(m, alignment, 0)) {
1153                 /* Add alignment between size and first element */
1154                 if (e)
1155                         c->signature[c->index] = 0;
1156
1157                 m->header->body_size = saved;
1158                 return -ENOMEM;
1159         }
1160
1161         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1162                 c->index = nindex;
1163
1164         /* m->body might have changed so let's readjust a */
1165         a = (uint8_t*) m->body + ((uint8_t*) a - (uint8_t*) b);
1166         *(uint32_t*) a = 0;
1167
1168         *array_size = a;
1169         return 0;
1170 }
1171
1172 static int bus_message_open_variant(
1173                 sd_bus_message *m,
1174                 struct bus_container *c,
1175                 const char *contents) {
1176
1177         char *e = NULL;
1178         size_t l;
1179         void *a;
1180
1181         assert(m);
1182         assert(c);
1183         assert(contents);
1184
1185         if (!signature_is_single(contents))
1186                 return -EINVAL;
1187
1188         if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
1189                 return -EINVAL;
1190
1191         if (c->signature && c->signature[c->index]) {
1192
1193                 if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
1194                         return -ENXIO;
1195
1196         } else {
1197                 if (c->enclosing != 0)
1198                         return -ENXIO;
1199
1200                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_VARIANT), NULL);
1201                 if (!e)
1202                         return -ENOMEM;
1203         }
1204
1205         l = strlen(contents);
1206         a = message_extend_body(m, 1, 1 + l + 1);
1207         if (!a) {
1208                 /* Truncate extended signature again */
1209                 if (e)
1210                         c->signature[c->index] = 0;
1211
1212                 return -ENOMEM;
1213         }
1214
1215         *(uint8_t*) a = l;
1216         memcpy((uint8_t*) a + 1, contents, l + 1);
1217
1218         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1219                 c->index++;
1220
1221         return 0;
1222 }
1223
1224 static int bus_message_open_struct(
1225                 sd_bus_message *m,
1226                 struct bus_container *c,
1227                 const char *contents) {
1228
1229         size_t nindex;
1230         char *e = NULL;
1231
1232         assert(m);
1233         assert(c);
1234         assert(contents);
1235
1236         if (!signature_is_valid(contents, false))
1237                 return -EINVAL;
1238
1239         if (c->signature && c->signature[c->index]) {
1240                 size_t l;
1241
1242                 l = strlen(contents);
1243
1244                 if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
1245                     !startswith(c->signature + c->index + 1, contents) ||
1246                     c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
1247                         return -ENXIO;
1248
1249                 nindex = c->index + 1 + l + 1;
1250         } else {
1251                 if (c->enclosing != 0)
1252                         return -ENXIO;
1253
1254                 e = strextend(&c->signature, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_BEGIN), contents, CHAR_TO_STR(SD_BUS_TYPE_STRUCT_END), NULL);
1255                 if (!e)
1256                         return -ENOMEM;
1257
1258                 nindex = e - c->signature;
1259         }
1260
1261         /* Align contents to 8 byte boundary */
1262         if (!message_extend_body(m, 8, 0)) {
1263                 if (e)
1264                         c->signature[c->index] = 0;
1265
1266                 return -ENOMEM;
1267         }
1268
1269         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1270                 c->index = nindex;
1271
1272         return 0;
1273 }
1274
1275 static int bus_message_open_dict_entry(
1276                 sd_bus_message *m,
1277                 struct bus_container *c,
1278                 const char *contents) {
1279
1280         size_t nindex;
1281
1282         assert(m);
1283         assert(c);
1284         assert(contents);
1285
1286         if (!signature_is_pair(contents))
1287                 return -EINVAL;
1288
1289         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1290                 return -ENXIO;
1291
1292         if (c->signature && c->signature[c->index]) {
1293                 size_t l;
1294
1295                 l = strlen(contents);
1296
1297                 if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
1298                     !startswith(c->signature + c->index + 1, contents) ||
1299                     c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
1300                         return -ENXIO;
1301
1302                 nindex = c->index + 1 + l + 1;
1303         } else
1304                 return -ENXIO;
1305
1306         /* Align contents to 8 byte boundary */
1307         if (!message_extend_body(m, 8, 0))
1308                 return -ENOMEM;
1309
1310         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1311                 c->index = nindex;
1312
1313         return 0;
1314 }
1315
1316 int sd_bus_message_open_container(
1317                 sd_bus_message *m,
1318                 char type,
1319                 const char *contents) {
1320
1321         struct bus_container *c, *w;
1322         uint32_t *array_size = NULL;
1323         char *signature;
1324         int r;
1325
1326         if (!m)
1327                 return -EINVAL;
1328         if (m->sealed)
1329                 return -EPERM;
1330         if (!contents)
1331                 return -EINVAL;
1332
1333         /* Make sure we have space for one more container */
1334         w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
1335         if (!w)
1336                 return -ENOMEM;
1337         m->containers = w;
1338
1339         c = message_get_container(m);
1340
1341         signature = strdup(contents);
1342         if (!signature)
1343                 return -ENOMEM;
1344
1345         if (type == SD_BUS_TYPE_ARRAY)
1346                 r = bus_message_open_array(m, c, contents, &array_size);
1347         else if (type == SD_BUS_TYPE_VARIANT)
1348                 r = bus_message_open_variant(m, c, contents);
1349         else if (type == SD_BUS_TYPE_STRUCT)
1350                 r = bus_message_open_struct(m, c, contents);
1351         else if (type == SD_BUS_TYPE_DICT_ENTRY)
1352                 r = bus_message_open_dict_entry(m, c, contents);
1353         else
1354                 r = -EINVAL;
1355
1356         if (r < 0) {
1357                 free(signature);
1358                 return r;
1359         }
1360
1361         /* OK, let's fill it in */
1362         w += m->n_containers++;
1363         w->enclosing = type;
1364         w->signature = signature;
1365         w->index = 0;
1366         w->array_size = array_size;
1367         w->begin = 0;
1368
1369         return 0;
1370 }
1371
1372 int sd_bus_message_close_container(sd_bus_message *m) {
1373         struct bus_container *c;
1374
1375         if (!m)
1376                 return -EINVAL;
1377         if (m->sealed)
1378                 return -EPERM;
1379         if (m->n_containers <= 0)
1380                 return -EINVAL;
1381
1382         c = message_get_container(m);
1383         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1384                 if (c->signature && c->signature[c->index] != 0)
1385                         return -EINVAL;
1386
1387         free(c->signature);
1388         m->n_containers--;
1389
1390         return 0;
1391 }
1392
1393
1394 typedef struct {
1395         const char *types;
1396         unsigned n_struct;
1397         unsigned n_array;
1398 } TypeStack;
1399
1400 static int type_stack_push(TypeStack *stack, unsigned max, unsigned *i, const char *types, unsigned n_struct, unsigned n_array) {
1401         assert(stack);
1402         assert(max > 0);
1403
1404         if (*i >= max)
1405                 return -EINVAL;
1406
1407         stack[*i].types = types;
1408         stack[*i].n_struct = n_struct;
1409         stack[*i].n_array = n_array;
1410         (*i)++;
1411
1412         return 0;
1413 }
1414
1415 static int type_stack_pop(TypeStack *stack, unsigned max, unsigned *i, const char **types, unsigned *n_struct, unsigned *n_array) {
1416         assert(stack);
1417         assert(max > 0);
1418         assert(types);
1419         assert(n_struct);
1420         assert(n_array);
1421
1422         if (*i <= 0)
1423                 return 0;
1424
1425         (*i)--;
1426         *types = stack[*i].types;
1427         *n_struct = stack[*i].n_struct;
1428         *n_array = stack[*i].n_array;
1429
1430         return 1;
1431 }
1432
1433 int bus_message_append_ap(
1434                 sd_bus_message *m,
1435                 const char *types,
1436                 va_list ap) {
1437
1438         unsigned n_array, n_struct;
1439         TypeStack stack[BUS_CONTAINER_DEPTH];
1440         unsigned stack_ptr = 0;
1441         int r;
1442
1443         assert(m);
1444
1445         if (!types)
1446                 return 0;
1447
1448         n_array = (unsigned) -1;
1449         n_struct = strlen(types);
1450
1451         for (;;) {
1452                 const char *t;
1453
1454                 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
1455                         r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
1456                         if (r < 0)
1457                                 return r;
1458                         if (r == 0)
1459                                 break;
1460
1461                         r = sd_bus_message_close_container(m);
1462                         if (r < 0)
1463                                 return r;
1464
1465                         continue;
1466                 }
1467
1468                 t = types;
1469                 if (n_array != (unsigned) -1)
1470                         n_array --;
1471                 else {
1472                         types ++;
1473                         n_struct--;
1474                 }
1475
1476                 switch (*t) {
1477
1478                 case SD_BUS_TYPE_BYTE: {
1479                         uint8_t x;
1480
1481                         x = (uint8_t) va_arg(ap, int);
1482                         r = sd_bus_message_append_basic(m, *t, &x);
1483                         break;
1484                 }
1485
1486                 case SD_BUS_TYPE_BOOLEAN:
1487                 case SD_BUS_TYPE_INT32:
1488                 case SD_BUS_TYPE_UINT32:
1489                 case SD_BUS_TYPE_UNIX_FD: {
1490                         uint32_t x;
1491
1492                         /* We assume a boolean is the same as int32_t */
1493                         assert_cc(sizeof(int32_t) == sizeof(int));
1494
1495                         x = va_arg(ap, uint32_t);
1496                         r = sd_bus_message_append_basic(m, *t, &x);
1497                         break;
1498                 }
1499
1500                 case SD_BUS_TYPE_INT16:
1501                 case SD_BUS_TYPE_UINT16: {
1502                         uint16_t x;
1503
1504                         x = (uint16_t) va_arg(ap, int);
1505                         r = sd_bus_message_append_basic(m, *t, &x);
1506                         break;
1507                 }
1508
1509                 case SD_BUS_TYPE_INT64:
1510                 case SD_BUS_TYPE_UINT64:
1511                 case SD_BUS_TYPE_DOUBLE: {
1512                         uint64_t x;
1513
1514                         x = va_arg(ap, uint64_t);
1515                         r = sd_bus_message_append_basic(m, *t, &x);
1516                         break;
1517                 }
1518
1519                 case SD_BUS_TYPE_STRING:
1520                 case SD_BUS_TYPE_OBJECT_PATH:
1521                 case SD_BUS_TYPE_SIGNATURE: {
1522                         const char *x;
1523
1524                         x = va_arg(ap, const char*);
1525                         r = sd_bus_message_append_basic(m, *t, x);
1526                         break;
1527                 }
1528
1529                 case SD_BUS_TYPE_ARRAY: {
1530                         size_t k;
1531
1532                         r = signature_element_length(t + 1, &k);
1533                         if (r < 0)
1534                                 return r;
1535
1536                         {
1537                                 char s[k + 1];
1538                                 memcpy(s, t + 1, k);
1539                                 s[k] = 0;
1540
1541                                 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
1542                                 if (r < 0)
1543                                         return r;
1544                         }
1545
1546                         if (n_array == (unsigned) -1) {
1547                                 types += k;
1548                                 n_struct -= k;
1549                         }
1550
1551                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
1552                         if (r < 0)
1553                                 return r;
1554
1555                         types = t + 1;
1556                         n_struct = k;
1557                         n_array = va_arg(ap, unsigned);
1558
1559                         break;
1560                 }
1561
1562                 case SD_BUS_TYPE_VARIANT: {
1563                         const char *s;
1564
1565                         s = va_arg(ap, const char*);
1566                         if (!s)
1567                                 return -EINVAL;
1568
1569                         r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, s);
1570                         if (r < 0)
1571                                 return r;
1572
1573                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
1574                         if (r < 0)
1575                                 return r;
1576
1577                         types = s;
1578                         n_struct = strlen(s);
1579                         n_array = (unsigned) -1;
1580
1581                         break;
1582                 }
1583
1584                 case SD_BUS_TYPE_STRUCT_BEGIN:
1585                 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
1586                         size_t k;
1587
1588                         r = signature_element_length(t, &k);
1589                         if (r < 0)
1590                                 return r;
1591
1592                         {
1593                                 char s[k - 1];
1594
1595                                 memcpy(s, t + 1, k - 2);
1596                                 s[k - 2] = 0;
1597
1598                                 r = sd_bus_message_open_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
1599                                 if (r < 0)
1600                                         return r;
1601                         }
1602
1603                         if (n_array == (unsigned) -1) {
1604                                 types += k - 1;
1605                                 n_struct -= k - 1;
1606                         }
1607
1608                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
1609                         if (r < 0)
1610                                 return r;
1611
1612                         types = t + 1;
1613                         n_struct = k - 2;
1614                         n_array = (unsigned) -1;
1615
1616                         break;
1617                 }
1618
1619                 default:
1620                         r = -EINVAL;
1621                 }
1622
1623                 if (r < 0)
1624                         return r;
1625         }
1626
1627         return 0;
1628 }
1629
1630 int sd_bus_message_append(sd_bus_message *m, const char *types, ...) {
1631         va_list ap;
1632         int r;
1633
1634         if (!m)
1635                 return -EINVAL;
1636         if (m->sealed)
1637                 return -EPERM;
1638         if (!types)
1639                 return 0;
1640
1641         va_start(ap, types);
1642         r = bus_message_append_ap(m, types, ap);
1643         va_end(ap);
1644
1645         return r;
1646 }
1647
1648 static int buffer_peek(const void *p, uint32_t sz, size_t *rindex, size_t align, size_t nbytes, void **r) {
1649         size_t k, start, n;
1650
1651         assert(rindex);
1652         assert(align > 0);
1653
1654         start = ALIGN_TO((size_t) *rindex, align);
1655         n = start + nbytes;
1656
1657         if (n > sz)
1658                 return -EBADMSG;
1659
1660         /* Verify that padding is 0 */
1661         for (k = *rindex; k < start; k++)
1662                 if (((const uint8_t*) p)[k] != 0)
1663                         return -EBADMSG;
1664
1665         if (r)
1666                 *r = (uint8_t*) p + start;
1667
1668         *rindex = n;
1669
1670         return 1;
1671 }
1672
1673 static bool message_end_of_array(sd_bus_message *m, size_t index) {
1674         struct bus_container *c;
1675
1676         assert(m);
1677
1678         c = message_get_container(m);
1679         if (!c->array_size)
1680                 return false;
1681
1682         return index >= c->begin + BUS_MESSAGE_BSWAP32(m, *c->array_size);
1683 }
1684
1685 static int message_peek_body(sd_bus_message *m, size_t *rindex, size_t align, size_t nbytes, void **ret) {
1686         assert(m);
1687         assert(rindex);
1688         assert(align > 0);
1689
1690         if (message_end_of_array(m, *rindex))
1691                 return 0;
1692
1693         return buffer_peek(m->body, BUS_MESSAGE_BODY_SIZE(m), rindex, align, nbytes, ret);
1694 }
1695
1696 static bool validate_nul(const char *s, size_t l) {
1697
1698         /* Check for NUL chars in the string */
1699         if (memchr(s, 0, l))
1700                 return false;
1701
1702         /* Check for NUL termination */
1703         if (s[l] != 0)
1704                 return false;
1705
1706         return true;
1707 }
1708
1709 static bool validate_string(const char *s, size_t l) {
1710
1711         if (!validate_nul(s, l))
1712                 return false;
1713
1714         /* Check if valid UTF8 */
1715         if (!utf8_is_valid(s))
1716                 return false;
1717
1718         return true;
1719 }
1720
1721 static bool validate_signature(const char *s, size_t l) {
1722
1723         if (!validate_nul(s, l))
1724                 return false;
1725
1726         /* Check if valid signature */
1727         if (!signature_is_valid(s, true))
1728                 return false;
1729
1730         return true;
1731 }
1732
1733 static bool validate_object_path(const char *s, size_t l) {
1734
1735         if (!validate_nul(s, l))
1736                 return false;
1737
1738         if (!object_path_is_valid(s))
1739                 return false;
1740
1741         return true;
1742 }
1743
1744 int sd_bus_message_read_basic(sd_bus_message *m, char type, void *p) {
1745         struct bus_container *c;
1746         int r;
1747         void *q;
1748
1749         if (!m)
1750                 return -EINVAL;
1751         if (!m->sealed)
1752                 return -EPERM;
1753         if (!bus_type_is_basic(type))
1754                 return -EINVAL;
1755         if (!p)
1756                 return -EINVAL;
1757
1758         c = message_get_container(m);
1759
1760         if (!c->signature || c->signature[c->index] == 0)
1761                 return 0;
1762
1763         if (c->signature[c->index] != type)
1764                 return -ENXIO;
1765
1766         switch (type) {
1767
1768         case SD_BUS_TYPE_STRING:
1769         case SD_BUS_TYPE_OBJECT_PATH: {
1770                 uint32_t l;
1771                 size_t rindex;
1772
1773                 rindex = m->rindex;
1774                 r = message_peek_body(m, &rindex, 4, 4, &q);
1775                 if (r <= 0)
1776                         return r;
1777
1778                 l = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
1779                 r = message_peek_body(m, &rindex, 1, l+1, &q);
1780                 if (r < 0)
1781                         return r;
1782                 if (r == 0)
1783                         return -EBADMSG;
1784
1785                 if (type == SD_BUS_TYPE_OBJECT_PATH) {
1786                         if (!validate_object_path(q, l))
1787                                 return -EBADMSG;
1788                 } else {
1789                         if (!validate_string(q, l))
1790                                 return -EBADMSG;
1791                 }
1792
1793                 m->rindex = rindex;
1794                 *(const char**) p = q;
1795                 break;
1796         }
1797
1798         case SD_BUS_TYPE_SIGNATURE: {
1799                 uint8_t l;
1800                 size_t rindex;
1801
1802                 rindex = m->rindex;
1803                 r = message_peek_body(m, &rindex, 1, 1, &q);
1804                 if (r <= 0)
1805                         return r;
1806
1807                 l = *(uint8_t*) q;
1808                 r = message_peek_body(m, &rindex, 1, l+1, &q);
1809                 if (r < 0)
1810                         return r;
1811                 if (r == 0)
1812                         return -EBADMSG;
1813
1814                 if (!validate_signature(q, l))
1815                         return -EBADMSG;
1816
1817                 m->rindex = rindex;
1818                 *(const char**) p = q;
1819                 break;
1820         }
1821
1822         default: {
1823                 ssize_t sz, align;
1824                 size_t rindex;
1825
1826                 align = bus_type_get_alignment(type);
1827                 sz = bus_type_get_size(type);
1828                 assert(align > 0 && sz > 0);
1829
1830                 rindex = m->rindex;
1831                 r = message_peek_body(m, &rindex, align, sz, &q);
1832                 if (r <= 0)
1833                         return r;
1834
1835                 switch (type) {
1836
1837                 case SD_BUS_TYPE_BYTE:
1838                         *(uint8_t*) p = *(uint8_t*) q;
1839                         break;
1840
1841                 case SD_BUS_TYPE_BOOLEAN:
1842                         *(int*) p = !!*(uint32_t*) q;
1843                         break;
1844
1845                 case SD_BUS_TYPE_INT16:
1846                 case SD_BUS_TYPE_UINT16:
1847                         *(uint16_t*) p = BUS_MESSAGE_BSWAP16(m, *(uint16_t*) q);
1848                         break;
1849
1850                 case SD_BUS_TYPE_INT32:
1851                 case SD_BUS_TYPE_UINT32:
1852                         *(uint32_t*) p = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
1853                         break;
1854
1855                 case SD_BUS_TYPE_INT64:
1856                 case SD_BUS_TYPE_UINT64:
1857                 case SD_BUS_TYPE_DOUBLE:
1858                         *(uint64_t*) p = BUS_MESSAGE_BSWAP64(m, *(uint64_t*) q);
1859                         break;
1860
1861                 case SD_BUS_TYPE_UNIX_FD: {
1862                         uint32_t j;
1863
1864                         j = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
1865                         if (j >= m->n_fds)
1866                                 return -EBADMSG;
1867
1868                         *(int*) p = m->fds[j];
1869                         break;
1870                 }
1871
1872                 default:
1873                         assert_not_reached("Unknown basic type...");
1874                 }
1875
1876                         m->rindex = rindex;
1877
1878                 break;
1879         }
1880         }
1881
1882         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1883                 c->index++;
1884
1885         return 1;
1886 }
1887
1888 static int bus_message_enter_array(
1889                 sd_bus_message *m,
1890                 struct bus_container *c,
1891                 const char *contents,
1892                 uint32_t **array_size) {
1893
1894         size_t rindex;
1895         void *q;
1896         int r, alignment;
1897
1898         assert(m);
1899         assert(c);
1900         assert(contents);
1901         assert(array_size);
1902
1903         if (!signature_is_single(contents))
1904                 return -EINVAL;
1905
1906         alignment = bus_type_get_alignment(contents[0]);
1907         if (alignment < 0)
1908                 return alignment;
1909
1910         if (!c->signature || c->signature[c->index] == 0)
1911                 return 0;
1912
1913         if (c->signature[c->index] != SD_BUS_TYPE_ARRAY)
1914                 return -ENXIO;
1915
1916         if (!startswith(c->signature + c->index + 1, contents))
1917                 return -ENXIO;
1918
1919         rindex = m->rindex;
1920         r = message_peek_body(m, &rindex, 4, 4, &q);
1921         if (r <= 0)
1922                 return r;
1923
1924         if (BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q) > BUS_ARRAY_MAX_SIZE)
1925                 return -EBADMSG;
1926
1927         r = message_peek_body(m, &rindex, alignment, 0, NULL);
1928         if (r < 0)
1929                 return r;
1930         if (r == 0)
1931                 return -EBADMSG;
1932
1933         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1934                 c->index += 1 + strlen(contents);
1935
1936         m->rindex = rindex;
1937
1938         *array_size = (uint32_t*) q;
1939
1940         return 1;
1941 }
1942
1943 static int bus_message_enter_variant(
1944                 sd_bus_message *m,
1945                 struct bus_container *c,
1946                 const char *contents) {
1947
1948         size_t rindex;
1949         uint8_t l;
1950         void *q;
1951         int r;
1952
1953         assert(m);
1954         assert(c);
1955         assert(contents);
1956
1957         if (!signature_is_single(contents))
1958                 return -EINVAL;
1959
1960         if (*contents == SD_BUS_TYPE_DICT_ENTRY_BEGIN)
1961                 return -EINVAL;
1962
1963         if (!c->signature || c->signature[c->index] == 0)
1964                 return 0;
1965
1966         if (c->signature[c->index] != SD_BUS_TYPE_VARIANT)
1967                 return -ENXIO;
1968
1969         rindex = m->rindex;
1970         r = message_peek_body(m, &rindex, 1, 1, &q);
1971         if (r <= 0)
1972                 return r;
1973
1974         l = *(uint8_t*) q;
1975         r = message_peek_body(m, &rindex, 1, l+1, &q);
1976         if (r < 0)
1977                 return r;
1978         if (r == 0)
1979                 return -EBADMSG;
1980
1981         if (!validate_signature(q, l))
1982                 return -EBADMSG;
1983
1984         if (!streq(q, contents))
1985                 return -ENXIO;
1986
1987         if (c->enclosing != SD_BUS_TYPE_ARRAY)
1988                 c->index++;
1989
1990         m->rindex = rindex;
1991
1992         return 1;
1993 }
1994
1995 static int bus_message_enter_struct(
1996                 sd_bus_message *m,
1997                 struct bus_container *c,
1998                 const char *contents) {
1999
2000         size_t l;
2001         int r;
2002
2003         assert(m);
2004         assert(c);
2005         assert(contents);
2006
2007         if (!signature_is_valid(contents, false))
2008                 return -EINVAL;
2009
2010         if (!c->signature || c->signature[c->index] == 0)
2011                 return 0;
2012
2013         l = strlen(contents);
2014
2015         if (c->signature[c->index] != SD_BUS_TYPE_STRUCT_BEGIN ||
2016             !startswith(c->signature + c->index + 1, contents) ||
2017             c->signature[c->index + 1 + l] != SD_BUS_TYPE_STRUCT_END)
2018                 return -ENXIO;
2019
2020         r = message_peek_body(m, &m->rindex, 8, 0, NULL);
2021         if (r <= 0)
2022                 return r;
2023
2024         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2025                 c->index += 1 + l + 1;
2026
2027         return 1;
2028 }
2029
2030 static int bus_message_enter_dict_entry(
2031                 sd_bus_message *m,
2032                 struct bus_container *c,
2033                 const char *contents) {
2034
2035         size_t l;
2036         int r;
2037
2038         assert(m);
2039         assert(c);
2040         assert(contents);
2041
2042         if (!signature_is_pair(contents))
2043                 return -EINVAL;
2044
2045         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2046                 return -ENXIO;
2047
2048         if (!c->signature || c->signature[c->index] == 0)
2049                 return 0;
2050
2051         l = strlen(contents);
2052
2053         if (c->signature[c->index] != SD_BUS_TYPE_DICT_ENTRY_BEGIN ||
2054             !startswith(c->signature + c->index + 1, contents) ||
2055             c->signature[c->index + 1 + l] != SD_BUS_TYPE_DICT_ENTRY_END)
2056                 return -ENXIO;
2057
2058         r = message_peek_body(m, &m->rindex, 8, 0, NULL);
2059         if (r <= 0)
2060                 return r;
2061
2062         if (c->enclosing != SD_BUS_TYPE_ARRAY)
2063                 c->index += 1 + l + 1;
2064
2065         return 1;
2066 }
2067
2068 int sd_bus_message_enter_container(sd_bus_message *m, char type, const char *contents) {
2069         struct bus_container *c, *w;
2070         uint32_t *array_size = NULL;
2071         char *signature;
2072         int r;
2073
2074         if (!m)
2075                 return -EINVAL;
2076         if (!m->sealed)
2077                 return -EPERM;
2078         if (!contents)
2079                 return -EINVAL;
2080
2081         /*
2082          * We enforce a global limit on container depth, that is much
2083          * higher than the 32 structs and 32 arrays the specification
2084          * mandates. This is simpler to implement for us, and we need
2085          * this only to ensure our container array doesn't grow
2086          * without bounds. We are happy to return any data from a
2087          * message as long as the data itself is valid, even if the
2088          * overall message might be not.
2089          *
2090          * Note that the message signature is validated when
2091          * parsing the headers, and that validation does check the
2092          * 32/32 limit.
2093          *
2094          * Note that the specification defines no limits on the depth
2095          * of stacked variants, but we do.
2096          */
2097         if (m->n_containers >= BUS_CONTAINER_DEPTH)
2098                 return -EBADMSG;
2099
2100         w = realloc(m->containers, sizeof(struct bus_container) * (m->n_containers + 1));
2101         if (!w)
2102                 return -ENOMEM;
2103         m->containers = w;
2104
2105         c = message_get_container(m);
2106
2107         if (!c->signature || c->signature[c->index] == 0)
2108                 return 0;
2109
2110         signature = strdup(contents);
2111         if (!signature)
2112                 return -ENOMEM;
2113
2114         if (type == SD_BUS_TYPE_ARRAY)
2115                 r = bus_message_enter_array(m, c, contents, &array_size);
2116         else if (type == SD_BUS_TYPE_VARIANT)
2117                 r = bus_message_enter_variant(m, c, contents);
2118         else if (type == SD_BUS_TYPE_STRUCT)
2119                 r = bus_message_enter_struct(m, c, contents);
2120         else if (type == SD_BUS_TYPE_DICT_ENTRY)
2121                 r = bus_message_enter_dict_entry(m, c, contents);
2122         else
2123                 r = -EINVAL;
2124
2125         if (r <= 0) {
2126                 free(signature);
2127                 return r;
2128         }
2129
2130         /* OK, let's fill it in */
2131         w += m->n_containers++;
2132         w->enclosing = type;
2133         w->signature = signature;
2134         w->index = 0;
2135         w->array_size = array_size;
2136         w->begin = m->rindex;
2137
2138         return 1;
2139 }
2140
2141 int sd_bus_message_exit_container(sd_bus_message *m) {
2142         struct bus_container *c;
2143
2144         if (!m)
2145                 return -EINVAL;
2146         if (!m->sealed)
2147                 return -EPERM;
2148         if (m->n_containers <= 0)
2149                 return -EINVAL;
2150
2151         c = message_get_container(m);
2152         if (c->enclosing == SD_BUS_TYPE_ARRAY) {
2153                 uint32_t l;
2154
2155                 l = BUS_MESSAGE_BSWAP32(m, *c->array_size);
2156                 if (c->begin + l != m->rindex)
2157                         return -EBUSY;
2158
2159         } else {
2160                 if (c->signature && c->signature[c->index] != 0)
2161                         return -EINVAL;
2162         }
2163
2164         free(c->signature);
2165         m->n_containers--;
2166
2167         return 1;
2168 }
2169
2170 int sd_bus_message_peek_type(sd_bus_message *m, char *type, const char **contents) {
2171         struct bus_container *c;
2172         int r;
2173
2174         if (!m)
2175                 return -EINVAL;
2176         if (!m->sealed)
2177                 return -EPERM;
2178
2179         c = message_get_container(m);
2180
2181         if (!c->signature || c->signature[c->index] == 0)
2182                 goto eof;
2183
2184         if (message_end_of_array(m, m->rindex))
2185                 goto eof;
2186
2187         if (bus_type_is_basic(c->signature[c->index])) {
2188                 if (contents)
2189                         *contents = NULL;
2190                 if (type)
2191                         *type = c->signature[c->index];
2192                 return 1;
2193         }
2194
2195         if (c->signature[c->index] == SD_BUS_TYPE_ARRAY) {
2196
2197                 if (contents) {
2198                         size_t l;
2199                         char *sig;
2200
2201                         r = signature_element_length(c->signature+c->index+1, &l);
2202                         if (r < 0)
2203                                 return r;
2204
2205                         assert(l >= 1);
2206
2207                         sig = strndup(c->signature + c->index + 1, l);
2208                         if (!sig)
2209                                 return -ENOMEM;
2210
2211                         free(m->peeked_signature);
2212                         m->peeked_signature = sig;
2213
2214                         *contents = sig;
2215                 }
2216
2217                 if (type)
2218                         *type = SD_BUS_TYPE_ARRAY;
2219
2220                 return 1;
2221         }
2222
2223         if (c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ||
2224             c->signature[c->index] == SD_BUS_TYPE_DICT_ENTRY_BEGIN) {
2225
2226                 if (contents) {
2227                         size_t l;
2228                         char *sig;
2229
2230                         r = signature_element_length(c->signature+c->index, &l);
2231                         if (r < 0)
2232                                 return r;
2233
2234                         assert(l >= 2);
2235                         sig = strndup(c->signature + c->index + 1, l - 2);
2236                         if (!sig)
2237                                 return -ENOMEM;
2238
2239                         free(m->peeked_signature);
2240                         m->peeked_signature = sig;
2241
2242                         *contents = sig;
2243                 }
2244
2245                 if (type)
2246                         *type = c->signature[c->index] == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY;
2247
2248                 return 1;
2249         }
2250
2251         if (c->signature[c->index] == SD_BUS_TYPE_VARIANT) {
2252                 if (contents) {
2253                         size_t rindex, l;
2254                         void *q;
2255
2256                         rindex = m->rindex;
2257                         r = message_peek_body(m, &rindex, 1, 1, &q);
2258                         if (r < 0)
2259                                 return r;
2260                         if (r == 0)
2261                                 goto eof;
2262
2263                         l = *(uint8_t*) q;
2264                         r = message_peek_body(m, &rindex, 1, l+1, &q);
2265                         if (r < 0)
2266                                 return r;
2267                         if (r == 0)
2268                                 return -EBADMSG;
2269
2270                         if (!validate_signature(q, l))
2271                                 return -EBADMSG;
2272
2273                         *contents = q;
2274                 }
2275
2276                 if (type)
2277                         *type = SD_BUS_TYPE_VARIANT;
2278
2279                 return 1;
2280         }
2281
2282         return -EINVAL;
2283
2284 eof:
2285         if (type)
2286                 *type = c->enclosing;
2287         if (contents)
2288                 *contents = NULL;
2289         return 0;
2290 }
2291
2292 int sd_bus_message_rewind(sd_bus_message *m, int complete) {
2293         struct bus_container *c;
2294
2295         if (!m)
2296                 return -EINVAL;
2297         if (!m->sealed)
2298                 return -EPERM;
2299
2300         if (complete) {
2301                 reset_containers(m);
2302                 m->rindex = 0;
2303                 m->root_container.index = 0;
2304
2305                 c = message_get_container(m);
2306         } else {
2307                 c = message_get_container(m);
2308
2309                 c->index = 0;
2310                 m->rindex = c->begin;
2311         }
2312
2313         return !isempty(c->signature);
2314 }
2315 static int message_read_ap(
2316                 sd_bus_message *m,
2317                 const char *types,
2318                 va_list ap) {
2319
2320         unsigned n_array, n_struct;
2321         TypeStack stack[BUS_CONTAINER_DEPTH];
2322         unsigned stack_ptr = 0;
2323         int r;
2324
2325         assert(m);
2326
2327         if (!types)
2328                 return 0;
2329
2330         /* Ideally, we'd just call ourselves recursively on every
2331          * complex type. However, the state of a va_list that is
2332          * passed to a function is undefined after that function
2333          * returns. This means we need to docode the va_list linearly
2334          * in a single stackframe. We hence implement our own
2335          * home-grown stack in an array. */
2336
2337         n_array = (unsigned) -1;
2338         n_struct = strlen(types);
2339
2340         for (;;) {
2341                 const char *t;
2342
2343                 if (n_array == 0 || (n_array == (unsigned) -1 && n_struct == 0)) {
2344                         r = type_stack_pop(stack, ELEMENTSOF(stack), &stack_ptr, &types, &n_struct, &n_array);
2345                         if (r < 0)
2346                                 return r;
2347                         if (r == 0)
2348                                 break;
2349
2350                         r = sd_bus_message_exit_container(m);
2351                         if (r < 0)
2352                                 return r;
2353
2354                         continue;
2355                 }
2356
2357                 t = types;
2358                 if (n_array != (unsigned) -1)
2359                         n_array --;
2360                 else {
2361                         types ++;
2362                         n_struct--;
2363                 }
2364
2365                 switch (*t) {
2366
2367                 case SD_BUS_TYPE_BYTE:
2368                 case SD_BUS_TYPE_BOOLEAN:
2369                 case SD_BUS_TYPE_INT16:
2370                 case SD_BUS_TYPE_UINT16:
2371                 case SD_BUS_TYPE_INT32:
2372                 case SD_BUS_TYPE_UINT32:
2373                 case SD_BUS_TYPE_INT64:
2374                 case SD_BUS_TYPE_UINT64:
2375                 case SD_BUS_TYPE_DOUBLE:
2376                 case SD_BUS_TYPE_STRING:
2377                 case SD_BUS_TYPE_OBJECT_PATH:
2378                 case SD_BUS_TYPE_SIGNATURE:
2379                 case SD_BUS_TYPE_UNIX_FD: {
2380                         void *p;
2381
2382                         p = va_arg(ap, void*);
2383                         r = sd_bus_message_read_basic(m, *t, p);
2384                         if (r < 0)
2385                                 return r;
2386                         if (r == 0)
2387                                 return -ENXIO;
2388
2389                         break;
2390                 }
2391
2392                 case SD_BUS_TYPE_ARRAY: {
2393                         size_t k;
2394
2395                         r = signature_element_length(t + 1, &k);
2396                         if (r < 0)
2397                                 return r;
2398
2399                         {
2400                                 char s[k + 1];
2401                                 memcpy(s, t + 1, k);
2402                                 s[k] = 0;
2403
2404                                 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, s);
2405                                 if (r < 0)
2406                                         return r;
2407                                 if (r == 0)
2408                                         return -ENXIO;
2409                         }
2410
2411                         if (n_array == (unsigned) -1) {
2412                                 types += k;
2413                                 n_struct -= k;
2414                         }
2415
2416                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2417                         if (r < 0)
2418                                 return r;
2419
2420                         types = t + 1;
2421                         n_struct = k;
2422                         n_array = va_arg(ap, unsigned);
2423
2424                         break;
2425                 }
2426
2427                 case SD_BUS_TYPE_VARIANT: {
2428                         const char *s;
2429
2430                         s = va_arg(ap, const char *);
2431                         if (!s)
2432                                 return -EINVAL;
2433
2434                         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, s);
2435                         if (r < 0)
2436                                 return r;
2437                         if (r == 0)
2438                                 return -ENXIO;
2439
2440                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2441                         if (r < 0)
2442                                 return r;
2443
2444                         types = s;
2445                         n_struct = strlen(s);
2446                         n_array = (unsigned) -1;
2447
2448                         break;
2449                 }
2450
2451                 case SD_BUS_TYPE_STRUCT_BEGIN:
2452                 case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
2453                         size_t k;
2454
2455                         r = signature_element_length(t, &k);
2456                         if (r < 0)
2457                                 return r;
2458
2459                         {
2460                                 char s[k - 1];
2461                                 memcpy(s, t + 1, k - 2);
2462                                 s[k - 2] = 0;
2463
2464                                 r = sd_bus_message_enter_container(m, *t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
2465                                 if (r < 0)
2466                                         return r;
2467                                 if (r == 0)
2468                                         return -ENXIO;
2469                         }
2470
2471                         if (n_array == (unsigned) -1) {
2472                                 types += k - 1;
2473                                 n_struct -= k - 1;
2474                         }
2475
2476                         r = type_stack_push(stack, ELEMENTSOF(stack), &stack_ptr, types, n_struct, n_array);
2477                         if (r < 0)
2478                                 return r;
2479
2480                         types = t + 1;
2481                         n_struct = k - 2;
2482                         n_array = (unsigned) -1;
2483
2484                         break;
2485                 }
2486
2487                 default:
2488                         return -EINVAL;
2489                 }
2490         }
2491
2492         return 1;
2493 }
2494
2495 int sd_bus_message_read(sd_bus_message *m, const char *types, ...) {
2496         va_list ap;
2497         int r;
2498
2499         if (!m)
2500                 return -EINVAL;
2501         if (!m->sealed)
2502                 return -EPERM;
2503         if (!types)
2504                 return -EINVAL;
2505
2506         va_start(ap, types);
2507         r = message_read_ap(m, types, ap);
2508         va_end(ap);
2509
2510         return r;
2511 }
2512
2513 static int message_peek_fields(
2514                 sd_bus_message *m,
2515                 size_t *rindex,
2516                 size_t align,
2517                 size_t nbytes,
2518                 void **ret) {
2519
2520         assert(m);
2521         assert(rindex);
2522         assert(align > 0);
2523
2524         return buffer_peek(m->fields, BUS_MESSAGE_FIELDS_SIZE(m), rindex, align, nbytes, ret);
2525 }
2526
2527 static int message_peek_field_uint32(
2528                 sd_bus_message *m,
2529                 size_t *ri,
2530                 uint32_t *ret) {
2531
2532         int r;
2533         void *q;
2534
2535         assert(m);
2536         assert(ri);
2537
2538         r = message_peek_fields(m, ri, 4, 4, &q);
2539         if (r < 0)
2540                 return r;
2541
2542         if (ret)
2543                 *ret = BUS_MESSAGE_BSWAP32(m, *(uint32_t*) q);
2544
2545         return 0;
2546 }
2547
2548 static int message_peek_field_string(
2549                 sd_bus_message *m,
2550                 bool (*validate)(const char *p),
2551                 size_t *ri,
2552                 const char **ret) {
2553
2554         uint32_t l;
2555         int r;
2556         void *q;
2557
2558         assert(m);
2559         assert(ri);
2560
2561         r = message_peek_field_uint32(m, ri, &l);
2562         if (r < 0)
2563                 return r;
2564
2565         r = message_peek_fields(m, ri, 1, l+1, &q);
2566         if (r < 0)
2567                 return r;
2568
2569         if (validate) {
2570                 if (!validate_nul(q, l))
2571                         return -EBADMSG;
2572
2573                 if (!validate(q))
2574                         return -EBADMSG;
2575         } else {
2576                 if (!validate_string(q, l))
2577                         return -EBADMSG;
2578         }
2579
2580         if (ret)
2581                 *ret = q;
2582
2583         return 0;
2584 }
2585
2586 static int message_peek_field_signature(
2587                 sd_bus_message *m,
2588                 size_t *ri,
2589                 const char **ret) {
2590
2591         size_t l;
2592         int r;
2593         void *q;
2594
2595         assert(m);
2596         assert(ri);
2597
2598         r = message_peek_fields(m, ri, 1, 1, &q);
2599         if (r < 0)
2600                 return r;
2601
2602         l = *(uint8_t*) q;
2603         r = message_peek_fields(m, ri, 1, l+1, &q);
2604         if (r < 0)
2605                 return r;
2606
2607         if (!validate_signature(q, l))
2608                 return -EBADMSG;
2609
2610         if (ret)
2611                 *ret = q;
2612
2613         return 0;
2614 }
2615
2616 static int message_skip_fields(
2617                 sd_bus_message *m,
2618                 size_t *ri,
2619                 uint32_t array_size,
2620                 const char **signature) {
2621
2622         size_t original_index;
2623         int r;
2624
2625         assert(m);
2626         assert(ri);
2627         assert(signature);
2628
2629         original_index = *ri;
2630
2631         for (;;) {
2632                 char t;
2633                 size_t l;
2634
2635                 if (array_size != (uint32_t) -1 &&
2636                     array_size <= *ri - original_index)
2637                         return 0;
2638
2639                 t = **signature;
2640                 if (!t)
2641                         return 0;
2642
2643                 if (t == SD_BUS_TYPE_STRING) {
2644
2645                         r = message_peek_field_string(m, NULL, ri, NULL);
2646                         if (r < 0)
2647                                 return r;
2648
2649                         (*signature)++;
2650
2651                 } else if (t == SD_BUS_TYPE_OBJECT_PATH) {
2652
2653                         r = message_peek_field_string(m, object_path_is_valid, ri, NULL);
2654                         if (r < 0)
2655                                 return r;
2656
2657                         (*signature)++;
2658
2659                 } else if (t == SD_BUS_TYPE_SIGNATURE) {
2660
2661                         r = message_peek_field_signature(m, ri, NULL);
2662                         if (r < 0)
2663                                 return r;
2664
2665                         (*signature)++;
2666
2667                 } else if (bus_type_is_basic(t)) {
2668                         ssize_t align, k;
2669
2670                         align = bus_type_get_alignment(t);
2671                         k = bus_type_get_size(t);
2672                         assert(align > 0 && k > 0);
2673
2674                         r = message_peek_fields(m, ri, align, k, NULL);
2675                         if (r < 0)
2676                                 return r;
2677
2678                         (*signature)++;
2679
2680                 } else if (t == SD_BUS_TYPE_ARRAY) {
2681
2682                         r = signature_element_length(*signature+1, &l);
2683                         if (r < 0)
2684                                 return r;
2685
2686                         assert(l >= 1);
2687                         {
2688                                 char sig[l-1], *s;
2689                                 uint32_t nas;
2690                                 int alignment;
2691
2692                                 strncpy(sig, *signature + 1, l-1);
2693                                 s = sig;
2694
2695                                 alignment = bus_type_get_alignment(sig[0]);
2696                                 if (alignment < 0)
2697                                         return alignment;
2698
2699                                 r = message_peek_field_uint32(m, ri, &nas);
2700                                 if (r < 0)
2701                                         return r;
2702                                 if (nas > BUS_ARRAY_MAX_SIZE)
2703                                         return -EBADMSG;
2704
2705                                 r = message_peek_fields(m, ri, alignment, 0, NULL);
2706                                 if (r < 0)
2707                                         return r;
2708
2709                                 r = message_skip_fields(m, ri, nas, (const char**) &s);
2710                                 if (r < 0)
2711                                         return r;
2712                         }
2713
2714                         (*signature) += 1 + l;
2715
2716                 } else if (t == SD_BUS_TYPE_VARIANT) {
2717                         const char *s;
2718
2719                         r = message_peek_field_signature(m, ri, &s);
2720                         if (r < 0)
2721                                 return r;
2722
2723                         r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
2724                         if (r < 0)
2725                                 return r;
2726
2727                         (*signature)++;
2728
2729                 } else if (t == SD_BUS_TYPE_STRUCT ||
2730                            t == SD_BUS_TYPE_DICT_ENTRY) {
2731
2732                         r = signature_element_length(*signature, &l);
2733                         if (r < 0)
2734                                 return r;
2735
2736                         assert(l >= 2);
2737                         {
2738                                 char sig[l-1], *s;
2739                                 strncpy(sig, *signature + 1, l-1);
2740                                 s = sig;
2741
2742                                 r = message_skip_fields(m, ri, (uint32_t) -1, (const char**) &s);
2743                                 if (r < 0)
2744                                         return r;
2745                         }
2746
2747                         *signature += l;
2748                 } else
2749                         return -EINVAL;
2750         }
2751 }
2752
2753 int bus_message_parse_fields(sd_bus_message *m) {
2754         size_t ri;
2755         int r;
2756         uint32_t unix_fds = 0;
2757
2758         assert(m);
2759
2760         for (ri = 0; ri < BUS_MESSAGE_FIELDS_SIZE(m); ) {
2761                 const char *signature;
2762                 uint8_t *header;
2763
2764                 r = message_peek_fields(m, &ri, 8, 1, (void**) &header);
2765                 if (r < 0)
2766                         return r;
2767
2768                 r = message_peek_field_signature(m, &ri, &signature);
2769                 if (r < 0)
2770                         return r;
2771
2772                 switch (*header) {
2773                 case _SD_BUS_MESSAGE_HEADER_INVALID:
2774                         return -EBADMSG;
2775
2776                 case SD_BUS_MESSAGE_HEADER_PATH:
2777
2778                         if (m->path)
2779                                 return -EBADMSG;
2780
2781                         if (!streq(signature, "o"))
2782                                 return -EBADMSG;
2783
2784                         r = message_peek_field_string(m, object_path_is_valid, &ri, &m->path);
2785                         break;
2786
2787                 case SD_BUS_MESSAGE_HEADER_INTERFACE:
2788
2789                         if (m->interface)
2790                                 return -EBADMSG;
2791
2792                         if (!streq(signature, "s"))
2793                                 return -EBADMSG;
2794
2795                         r = message_peek_field_string(m, interface_name_is_valid, &ri, &m->interface);
2796                         break;
2797
2798                 case SD_BUS_MESSAGE_HEADER_MEMBER:
2799
2800                         if (m->member)
2801                                 return -EBADMSG;
2802
2803                         if (!streq(signature, "s"))
2804                                 return -EBADMSG;
2805
2806                         r = message_peek_field_string(m, member_name_is_valid, &ri, &m->member);
2807                         break;
2808
2809                 case SD_BUS_MESSAGE_HEADER_ERROR_NAME:
2810
2811                         if (m->error.name)
2812                                 return -EBADMSG;
2813
2814                         if (!streq(signature, "s"))
2815                                 return -EBADMSG;
2816
2817                         r = message_peek_field_string(m, error_name_is_valid, &ri, &m->error.name);
2818                         break;
2819
2820                 case SD_BUS_MESSAGE_HEADER_DESTINATION:
2821
2822                         if (m->destination)
2823                                 return -EBADMSG;
2824
2825                         if (!streq(signature, "s"))
2826                                 return -EBADMSG;
2827
2828                         r = message_peek_field_string(m, service_name_is_valid, &ri, &m->destination);
2829                         break;
2830
2831                 case SD_BUS_MESSAGE_HEADER_SENDER:
2832
2833                         if (m->sender)
2834                                 return -EBADMSG;
2835
2836                         if (!streq(signature, "s"))
2837                                 return -EBADMSG;
2838
2839                         r = message_peek_field_string(m, service_name_is_valid, &ri, &m->sender);
2840                         break;
2841
2842
2843                 case SD_BUS_MESSAGE_HEADER_SIGNATURE: {
2844                         const char *s;
2845                         char *c;
2846
2847                         if (m->root_container.signature)
2848                                 return -EBADMSG;
2849
2850                         if (!streq(signature, "g"))
2851                                 return -EBADMSG;
2852
2853                         r = message_peek_field_signature(m, &ri, &s);
2854                         if (r < 0)
2855                                 return r;
2856
2857                         c = strdup(s);
2858                         if (!c)
2859                                 return -ENOMEM;
2860
2861                         free(m->root_container.signature);
2862                         m->root_container.signature = c;
2863                         break;
2864                 }
2865
2866                 case SD_BUS_MESSAGE_HEADER_REPLY_SERIAL:
2867                         if (m->reply_serial != 0)
2868                                 return -EBADMSG;
2869
2870                         if (!streq(signature, "u"))
2871                                 return -EBADMSG;
2872
2873                         r = message_peek_field_uint32(m, &ri, &m->reply_serial);
2874                         if (r < 0)
2875                                 return r;
2876
2877                         if (m->reply_serial == 0)
2878                                 return -EBADMSG;
2879
2880                         break;
2881
2882                 case SD_BUS_MESSAGE_HEADER_UNIX_FDS:
2883                         if (unix_fds != 0)
2884                                 return -EBADMSG;
2885
2886                         if (!streq(signature, "u"))
2887                                 return -EBADMSG;
2888
2889                         r = message_peek_field_uint32(m, &ri, &unix_fds);
2890                         if (r < 0)
2891                                 return -EBADMSG;
2892
2893                         if (unix_fds == 0)
2894                                 return -EBADMSG;
2895
2896                         break;
2897
2898                 default:
2899                         r = message_skip_fields(m, &ri, (uint32_t) -1, (const char **) &signature);
2900                 }
2901
2902                 if (r < 0)
2903                         return r;
2904         }
2905
2906         if (m->n_fds != unix_fds)
2907                 return -EBADMSG;
2908
2909         if (isempty(m->root_container.signature) != (BUS_MESSAGE_BODY_SIZE(m) == 0))
2910                 return -EBADMSG;
2911
2912         switch (m->header->type) {
2913
2914         case SD_BUS_MESSAGE_TYPE_SIGNAL:
2915                 if (!m->path || !m->interface || !m->member)
2916                         return -EBADMSG;
2917                 break;
2918
2919         case SD_BUS_MESSAGE_TYPE_METHOD_CALL:
2920
2921                 if (!m->path || !m->member)
2922                         return -EBADMSG;
2923
2924                 break;
2925
2926         case SD_BUS_MESSAGE_TYPE_METHOD_RETURN:
2927
2928                 if (m->reply_serial == 0)
2929                         return -EBADMSG;
2930                 break;
2931
2932         case SD_BUS_MESSAGE_TYPE_METHOD_ERROR:
2933
2934                 if (m->reply_serial == 0 || !m->error.name)
2935                         return -EBADMSG;
2936                 break;
2937         }
2938
2939         /* Try to read the error message, but if we can't it's a non-issue */
2940         if (m->header->type == SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
2941                 sd_bus_message_read(m, "s", &m->error.message);
2942
2943         return 0;
2944 }
2945
2946 int bus_message_seal(sd_bus_message *m, uint64_t serial) {
2947         int r;
2948         size_t l, a;
2949
2950         assert(m);
2951
2952         if (m->sealed)
2953                 return -EPERM;
2954
2955         if (m->n_containers > 0)
2956                 return -EBADMSG;
2957
2958         /* If there's a non-trivial signature set, then add it in here */
2959         if (!isempty(m->root_container.signature)) {
2960                 r = message_append_field_signature(m, SD_BUS_MESSAGE_HEADER_SIGNATURE, m->root_container.signature, NULL);
2961                 if (r < 0)
2962                         return r;
2963         }
2964
2965         if (m->n_fds > 0) {
2966                 r = message_append_field_uint32(m, SD_BUS_MESSAGE_HEADER_UNIX_FDS, m->n_fds);
2967                 if (r < 0)
2968                         return r;
2969         }
2970
2971         l = BUS_MESSAGE_FIELDS_SIZE(m);
2972         a = ALIGN8(l) - l;
2973
2974         if (a > 0) {
2975                 /* Add padding at the end, since we know the body
2976                  * needs to start at an 8 byte alignment. */
2977                 void *p;
2978
2979                 p = message_extend_fields(m, 1, a);
2980                 if (!p)
2981                         return -ENOMEM;
2982
2983                 memset(p, 0, a);
2984                 m->header->fields_size -= a;
2985         }
2986
2987         m->header->serial = serial;
2988         m->sealed = true;
2989
2990         return 0;
2991 }
2992
2993 int sd_bus_message_set_destination(sd_bus_message *m, const char *destination) {
2994         if (!m)
2995                 return -EINVAL;
2996         if (!destination)
2997                 return -EINVAL;
2998         if (m->sealed)
2999                 return -EPERM;
3000         if (m->destination)
3001                 return -EEXIST;
3002
3003         return message_append_field_string(m, SD_BUS_MESSAGE_HEADER_DESTINATION, SD_BUS_TYPE_STRING, destination, &m->destination);
3004 }
3005
3006 int bus_message_dump(sd_bus_message *m) {
3007         char **cmdline = NULL;
3008         unsigned level = 1;
3009         int r;
3010
3011         assert(m);
3012
3013         printf("Message %p\n"
3014                "\tn_ref=%u\n"
3015                "\tendian=%c\n"
3016                "\ttype=%i\n"
3017                "\tflags=%u\n"
3018                "\tversion=%u\n"
3019                "\tserial=%u\n"
3020                "\tfields_size=%u\n"
3021                "\tbody_size=%u\n"
3022                "\tpath=%s\n"
3023                "\tinterface=%s\n"
3024                "\tmember=%s\n"
3025                "\tdestination=%s\n"
3026                "\tsender=%s\n"
3027                "\tsignature=%s\n"
3028                "\treply_serial=%u\n"
3029                "\terror.name=%s\n"
3030                "\terror.message=%s\n"
3031                "\tsealed=%s\n",
3032                m,
3033                m->n_ref,
3034                m->header->endian,
3035                m->header->type,
3036                m->header->flags,
3037                m->header->version,
3038                BUS_MESSAGE_SERIAL(m),
3039                BUS_MESSAGE_FIELDS_SIZE(m),
3040                BUS_MESSAGE_BODY_SIZE(m),
3041                strna(m->path),
3042                strna(m->interface),
3043                strna(m->member),
3044                strna(m->destination),
3045                strna(m->sender),
3046                strna(m->root_container.signature),
3047                m->reply_serial,
3048                strna(m->error.name),
3049                strna(m->error.message),
3050                yes_no(m->sealed));
3051
3052         if (m->pid != 0)
3053                 printf("\tpid=%lu\n", (unsigned long) m->pid);
3054         if (m->tid != 0)
3055                 printf("\ttid=%lu\n", (unsigned long) m->tid);
3056         if (m->uid_valid)
3057                 printf("\tuid=%lu\n", (unsigned long) m->uid);
3058         if (m->gid_valid)
3059                 printf("\tgid=%lu\n", (unsigned long) m->gid);
3060         if (m->pid_starttime != 0)
3061                 printf("\tpid_starttime=%llu\n", (unsigned long long) m->pid_starttime);
3062         if (m->monotonic != 0)
3063                 printf("\tmonotonic=%llu\n", (unsigned long long) m->monotonic);
3064         if (m->realtime != 0)
3065                 printf("\trealtime=%llu\n", (unsigned long long) m->realtime);
3066         if (m->exe)
3067                 printf("\texe=[%s]\n", m->exe);
3068         if (m->comm)
3069                 printf("\tcomm=[%s]\n", m->comm);
3070         if (m->tid_comm)
3071                 printf("\ttid_comm=[%s]\n", m->tid_comm);
3072         if (m->label)
3073                 printf("\tlabel=[%s]\n", m->label);
3074         if (m->cgroup)
3075                 printf("\tcgroup=[%s]\n", m->cgroup);
3076
3077         if (sd_bus_message_get_cmdline(m, &cmdline) >= 0) {
3078                 char **c;
3079
3080                 fputs("\tcmdline=[", stdout);
3081                 STRV_FOREACH(c, cmdline) {
3082                         if (c != cmdline)
3083                                 putchar(' ');
3084
3085                         fputs(*c, stdout);
3086                 }
3087
3088                 fputs("]\n", stdout);
3089         }
3090
3091         r = sd_bus_message_rewind(m, true);
3092         if (r < 0) {
3093                 log_error("Failed to rewind: %s", strerror(-r));
3094                 return r;
3095         }
3096
3097         printf("BEGIN_MESSAGE \"%s\" {\n", strempty(m->root_container.signature));
3098
3099         for(;;) {
3100                 _cleanup_free_ char *prefix = NULL;
3101                 const char *contents = NULL;
3102                 char type;
3103                 union {
3104                         uint8_t u8;
3105                         uint16_t u16;
3106                         int16_t s16;
3107                         uint32_t u32;
3108                         int32_t s32;
3109                         uint64_t u64;
3110                         int64_t s64;
3111                         double d64;
3112                         const char *string;
3113                         int i;
3114                 } basic;
3115
3116                 r = sd_bus_message_peek_type(m, &type, &contents);
3117                 if (r < 0) {
3118                         log_error("Failed to peek type: %s", strerror(-r));
3119                         return r;
3120                 }
3121                 if (r == 0) {
3122                         if (level <= 1)
3123                                 break;
3124
3125                         r = sd_bus_message_exit_container(m);
3126                         if (r < 0) {
3127                                 log_error("Failed to exit container: %s", strerror(-r));
3128                                 return r;
3129                         }
3130
3131                         level--;
3132
3133                         prefix = strrep("\t", level);
3134                         if (!prefix)
3135                                 return log_oom();
3136
3137                         if (type == SD_BUS_TYPE_ARRAY)
3138                                 printf("%s} END_ARRAY \n", prefix);
3139                         else if (type == SD_BUS_TYPE_VARIANT)
3140                                 printf("%s} END_VARIANT\n", prefix);
3141                         else if (type == SD_BUS_TYPE_STRUCT)
3142                                 printf("%s} END_STRUCT\n", prefix);
3143                         else if (type == SD_BUS_TYPE_DICT_ENTRY)
3144                                 printf("%s} END_DICT_ENTRY\n", prefix);
3145
3146                         continue;
3147                 }
3148
3149                 prefix = strrep("\t", level);
3150                 if (!prefix)
3151                         return log_oom();
3152
3153                 if (bus_type_is_container(type) > 0) {
3154                         r = sd_bus_message_enter_container(m, type, contents);
3155                         if (r < 0) {
3156                                 log_error("Failed to enter container: %s", strerror(-r));
3157                                 return r;
3158                         }
3159
3160                         if (type == SD_BUS_TYPE_ARRAY)
3161                                 printf("%sBEGIN_ARRAY \"%s\" {\n", prefix, contents);
3162                         else if (type == SD_BUS_TYPE_VARIANT)
3163                                 printf("%sBEGIN_VARIANT \"%s\" {\n", prefix, contents);
3164                         else if (type == SD_BUS_TYPE_STRUCT)
3165                                 printf("%sBEGIN_STRUCT \"%s\" {\n", prefix, contents);
3166                         else if (type == SD_BUS_TYPE_DICT_ENTRY)
3167                                 printf("%sBEGIN_DICT_ENTRY \"%s\" {\n", prefix, contents);
3168
3169                         level ++;
3170
3171                         continue;
3172                 }
3173
3174                 r = sd_bus_message_read_basic(m, type, &basic);
3175                 if (r < 0) {
3176                         log_error("Failed to get basic: %s", strerror(-r));
3177                         return r;
3178                 }
3179
3180                 switch (type) {
3181
3182                 case SD_BUS_TYPE_BYTE:
3183                         printf("%sBYTE: %u\n", prefix, basic.u8);
3184                         break;
3185
3186                 case SD_BUS_TYPE_BOOLEAN:
3187                         printf("%sBOOLEAN: %s\n", prefix, yes_no(basic.i));
3188                         break;
3189
3190                 case SD_BUS_TYPE_INT16:
3191                         printf("%sINT16: %i\n", prefix, basic.s16);
3192                         break;
3193
3194                 case SD_BUS_TYPE_UINT16:
3195                         printf("%sUINT16: %u\n", prefix, basic.u16);
3196                         break;
3197
3198                 case SD_BUS_TYPE_INT32:
319