[daemon-fix] Changed the way of make bus address when the address is not given
[platform/upstream/dbus.git] / dbus / dbus-sysdeps-unix.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-sysdeps-unix.c Wrappers around UNIX system/libc features (internal to D-Bus implementation)
3  *
4  * Copyright (C) 2002, 2003, 2006  Red Hat, Inc.
5  * Copyright (C) 2003 CodeFactory AB
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #include <config.h>
26
27 #include "dbus-internals.h"
28 #include "dbus-sysdeps.h"
29 #include "dbus-sysdeps-unix.h"
30 #include "dbus-threads.h"
31 #include "dbus-protocol.h"
32 #include "dbus-transport.h"
33 #include "dbus-string.h"
34 #include "dbus-userdb.h"
35 #include "dbus-list.h"
36 #include "dbus-credentials.h"
37 #include "dbus-nonce.h"
38
39 #include <sys/types.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <signal.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <fcntl.h>
46 #include <sys/socket.h>
47 #include <dirent.h>
48 #include <sys/un.h>
49 #include <pwd.h>
50 #include <time.h>
51 #include <locale.h>
52 #include <sys/time.h>
53 #include <sys/stat.h>
54 #include <sys/wait.h>
55 #include <netinet/in.h>
56 #include <netdb.h>
57 #include <grp.h>
58 #include <arpa/inet.h>
59
60 #ifdef HAVE_ERRNO_H
61 #include <errno.h>
62 #endif
63 #ifdef HAVE_WRITEV
64 #include <sys/uio.h>
65 #endif
66 #ifdef HAVE_POLL
67 #include <sys/poll.h>
68 #endif
69 #ifdef HAVE_BACKTRACE
70 #include <execinfo.h>
71 #endif
72 #ifdef HAVE_GETPEERUCRED
73 #include <ucred.h>
74 #endif
75 #ifdef HAVE_ALLOCA_H
76 #include <alloca.h>
77 #endif
78
79 #ifdef HAVE_ADT
80 #include <bsm/adt.h>
81 #endif
82
83 #include "sd-daemon.h"
84
85 #if !DBUS_USE_SYNC
86 #include <pthread.h>
87 #endif
88
89 #ifndef O_BINARY
90 #define O_BINARY 0
91 #endif
92
93 #ifndef AI_ADDRCONFIG
94 #define AI_ADDRCONFIG 0
95 #endif
96
97 #ifndef HAVE_SOCKLEN_T
98 #define socklen_t int
99 #endif
100
101 #if defined (__sun) || defined (__sun__)
102 /*
103  * CMS_SPACE etc. definitions for Solaris < 10, based on
104  *   http://mailman.videolan.org/pipermail/vlc-devel/2006-May/024402.html
105  * via
106  *   http://wiki.opencsw.org/porting-faq#toc10
107  *
108  * These are only redefined for Solaris, for now: if your OS needs these too,
109  * please file a bug. (Or preferably, improve your OS so they're not needed.)
110  */
111
112 # ifndef CMSG_ALIGN
113 #   ifdef __sun__
114 #     define CMSG_ALIGN(len) _CMSG_DATA_ALIGN (len)
115 #   else
116       /* aligning to sizeof (long) is assumed to be portable (fd.o#40235) */
117 #     define CMSG_ALIGN(len) (((len) + sizeof (long) - 1) & \
118                               ~(sizeof (long) - 1))
119 #   endif
120 # endif
121
122 # ifndef CMSG_SPACE
123 #   define CMSG_SPACE(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + \
124                             CMSG_ALIGN (len))
125 # endif
126
127 # ifndef CMSG_LEN
128 #   define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
129 # endif
130
131 #endif /* Solaris */
132
133 static dbus_bool_t
134 _dbus_open_socket (int              *fd_p,
135                    int               domain,
136                    int               type,
137                    int               protocol,
138                    DBusError        *error)
139 {
140 #ifdef SOCK_CLOEXEC
141   dbus_bool_t cloexec_done;
142
143   *fd_p = socket (domain, type | SOCK_CLOEXEC, protocol);
144   cloexec_done = *fd_p >= 0;
145
146   /* Check if kernel seems to be too old to know SOCK_CLOEXEC */
147   if (*fd_p < 0 && errno == EINVAL)
148 #endif
149     {
150       *fd_p = socket (domain, type, protocol);
151     }
152
153   if (*fd_p >= 0)
154     {
155 #ifdef SOCK_CLOEXEC
156       if (!cloexec_done)
157 #endif
158         {
159           _dbus_fd_set_close_on_exec(*fd_p);
160         }
161
162       _dbus_verbose ("socket fd %d opened\n", *fd_p);
163       return TRUE;
164     }
165   else
166     {
167       dbus_set_error(error,
168                      _dbus_error_from_errno (errno),
169                      "Failed to open socket: %s",
170                      _dbus_strerror (errno));
171       return FALSE;
172     }
173 }
174
175 /**
176  * Opens a UNIX domain socket (as in the socket() call).
177  * Does not bind the socket.
178  *
179  * This will set FD_CLOEXEC for the socket returned
180  *
181  * @param fd return location for socket descriptor
182  * @param error return location for an error
183  * @returns #FALSE if error is set
184  */
185 static dbus_bool_t
186 _dbus_open_unix_socket (int              *fd,
187                         DBusError        *error)
188 {
189   return _dbus_open_socket(fd, PF_UNIX, SOCK_STREAM, 0, error);
190 }
191
192 /**
193  * Closes a socket. Should not be used on non-socket
194  * file descriptors or handles.
195  *
196  * @param fd the socket
197  * @param error return location for an error
198  * @returns #FALSE if error is set
199  */
200 dbus_bool_t
201 _dbus_close_socket (int               fd,
202                     DBusError        *error)
203 {
204   return _dbus_close (fd, error);
205 }
206
207 /**
208  * Like _dbus_read(), but only works on sockets so is
209  * available on Windows.
210  *
211  * @param fd the socket
212  * @param buffer string to append data to
213  * @param count max amount of data to read
214  * @returns number of bytes appended to the string
215  */
216 int
217 _dbus_read_socket (int               fd,
218                    DBusString       *buffer,
219                    int               count)
220 {
221   return _dbus_read (fd, buffer, count);
222 }
223
224 /**
225  * Like _dbus_write(), but only supports sockets
226  * and is thus available on Windows.
227  *
228  * @param fd the file descriptor to write
229  * @param buffer the buffer to write data from
230  * @param start the first byte in the buffer to write
231  * @param len the number of bytes to try to write
232  * @returns the number of bytes written or -1 on error
233  */
234 int
235 _dbus_write_socket (int               fd,
236                     const DBusString *buffer,
237                     int               start,
238                     int               len)
239 {
240 #if HAVE_DECL_MSG_NOSIGNAL
241   const char *data;
242   int bytes_written;
243
244   data = _dbus_string_get_const_data_len (buffer, start, len);
245
246  again:
247
248   bytes_written = send (fd, data, len, MSG_NOSIGNAL);
249
250   if (bytes_written < 0 && errno == EINTR)
251     goto again;
252
253   return bytes_written;
254
255 #else
256   return _dbus_write (fd, buffer, start, len);
257 #endif
258 }
259
260 /**
261  * Like _dbus_read_socket() but also tries to read unix fds from the
262  * socket. When there are more fds to read than space in the array
263  * passed this function will fail with ENOSPC.
264  *
265  * @param fd the socket
266  * @param buffer string to append data to
267  * @param count max amount of data to read
268  * @param fds array to place read file descriptors in
269  * @param n_fds on input space in fds array, on output how many fds actually got read
270  * @returns number of bytes appended to string
271  */
272 int
273 _dbus_read_socket_with_unix_fds (int               fd,
274                                  DBusString       *buffer,
275                                  int               count,
276                                  int              *fds,
277                                  int              *n_fds) {
278 #ifndef HAVE_UNIX_FD_PASSING
279   int r;
280
281   if ((r = _dbus_read_socket(fd, buffer, count)) < 0)
282     return r;
283
284   *n_fds = 0;
285   return r;
286
287 #else
288   int bytes_read;
289   int start;
290   struct msghdr m;
291   struct iovec iov;
292
293   _dbus_assert (count >= 0);
294   _dbus_assert (*n_fds >= 0);
295
296   start = _dbus_string_get_length (buffer);
297
298   if (!_dbus_string_lengthen (buffer, count))
299     {
300       errno = ENOMEM;
301       return -1;
302     }
303
304   _DBUS_ZERO(iov);
305   iov.iov_base = _dbus_string_get_data_len (buffer, start, count);
306   iov.iov_len = count;
307
308   _DBUS_ZERO(m);
309   m.msg_iov = &iov;
310   m.msg_iovlen = 1;
311
312   /* Hmm, we have no clue how long the control data will actually be
313      that is queued for us. The least we can do is assume that the
314      caller knows. Hence let's make space for the number of fds that
315      we shall read at max plus the cmsg header. */
316   m.msg_controllen = CMSG_SPACE(*n_fds * sizeof(int));
317
318   /* It's probably safe to assume that systems with SCM_RIGHTS also
319      know alloca() */
320   m.msg_control = alloca(m.msg_controllen);
321   memset(m.msg_control, 0, m.msg_controllen);
322
323  again:
324
325   bytes_read = recvmsg(fd, &m, 0
326 #ifdef MSG_CMSG_CLOEXEC
327                        |MSG_CMSG_CLOEXEC
328 #endif
329                        );
330
331   if (bytes_read < 0)
332     {
333       if (errno == EINTR)
334         goto again;
335       else
336         {
337           /* put length back (note that this doesn't actually realloc anything) */
338           _dbus_string_set_length (buffer, start);
339           return -1;
340         }
341     }
342   else
343     {
344       struct cmsghdr *cm;
345       dbus_bool_t found = FALSE;
346
347       if (m.msg_flags & MSG_CTRUNC)
348         {
349           /* Hmm, apparently the control data was truncated. The bad
350              thing is that we might have completely lost a couple of fds
351              without chance to recover them. Hence let's treat this as a
352              serious error. */
353
354           errno = ENOSPC;
355           _dbus_string_set_length (buffer, start);
356           return -1;
357         }
358
359       for (cm = CMSG_FIRSTHDR(&m); cm; cm = CMSG_NXTHDR(&m, cm))
360         if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SCM_RIGHTS)
361           {
362             unsigned i;
363
364             _dbus_assert(cm->cmsg_len <= CMSG_LEN(*n_fds * sizeof(int)));
365             *n_fds = (cm->cmsg_len - CMSG_LEN(0)) / sizeof(int);
366
367             memcpy(fds, CMSG_DATA(cm), *n_fds * sizeof(int));
368             found = TRUE;
369
370             /* Linux doesn't tell us whether MSG_CMSG_CLOEXEC actually
371                worked, hence we need to go through this list and set
372                CLOEXEC everywhere in any case */
373             for (i = 0; i < *n_fds; i++)
374               _dbus_fd_set_close_on_exec(fds[i]);
375
376             break;
377           }
378
379       if (!found)
380         *n_fds = 0;
381
382       /* put length back (doesn't actually realloc) */
383       _dbus_string_set_length (buffer, start + bytes_read);
384
385 #if 0
386       if (bytes_read > 0)
387         _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
388 #endif
389
390       return bytes_read;
391     }
392 #endif
393 }
394
395 int
396 _dbus_write_socket_with_unix_fds(int               fd,
397                                  const DBusString *buffer,
398                                  int               start,
399                                  int               len,
400                                  const int        *fds,
401                                  int               n_fds) {
402
403 #ifndef HAVE_UNIX_FD_PASSING
404
405   if (n_fds > 0) {
406     errno = ENOTSUP;
407     return -1;
408   }
409
410   return _dbus_write_socket(fd, buffer, start, len);
411 #else
412   return _dbus_write_socket_with_unix_fds_two(fd, buffer, start, len, NULL, 0, 0, fds, n_fds);
413 #endif
414 }
415
416 int
417 _dbus_write_socket_with_unix_fds_two(int               fd,
418                                      const DBusString *buffer1,
419                                      int               start1,
420                                      int               len1,
421                                      const DBusString *buffer2,
422                                      int               start2,
423                                      int               len2,
424                                      const int        *fds,
425                                      int               n_fds) {
426
427 #ifndef HAVE_UNIX_FD_PASSING
428
429   if (n_fds > 0) {
430     errno = ENOTSUP;
431     return -1;
432   }
433
434   return _dbus_write_socket_two(fd,
435                                 buffer1, start1, len1,
436                                 buffer2, start2, len2);
437 #else
438
439   struct msghdr m;
440   struct cmsghdr *cm;
441   struct iovec iov[2];
442   int bytes_written;
443
444   _dbus_assert (len1 >= 0);
445   _dbus_assert (len2 >= 0);
446   _dbus_assert (n_fds >= 0);
447
448   _DBUS_ZERO(iov);
449   iov[0].iov_base = (char*) _dbus_string_get_const_data_len (buffer1, start1, len1);
450   iov[0].iov_len = len1;
451
452   if (buffer2)
453     {
454       iov[1].iov_base = (char*) _dbus_string_get_const_data_len (buffer2, start2, len2);
455       iov[1].iov_len = len2;
456     }
457
458   _DBUS_ZERO(m);
459   m.msg_iov = iov;
460   m.msg_iovlen = buffer2 ? 2 : 1;
461
462   if (n_fds > 0)
463     {
464       m.msg_controllen = CMSG_SPACE(n_fds * sizeof(int));
465       m.msg_control = alloca(m.msg_controllen);
466       memset(m.msg_control, 0, m.msg_controllen);
467
468       cm = CMSG_FIRSTHDR(&m);
469       cm->cmsg_level = SOL_SOCKET;
470       cm->cmsg_type = SCM_RIGHTS;
471       cm->cmsg_len = CMSG_LEN(n_fds * sizeof(int));
472       memcpy(CMSG_DATA(cm), fds, n_fds * sizeof(int));
473     }
474
475  again:
476
477   bytes_written = sendmsg (fd, &m, 0
478 #if HAVE_DECL_MSG_NOSIGNAL
479                            |MSG_NOSIGNAL
480 #endif
481                            );
482
483   if (bytes_written < 0 && errno == EINTR)
484     goto again;
485
486 #if 0
487   if (bytes_written > 0)
488     _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
489 #endif
490
491   return bytes_written;
492 #endif
493 }
494
495 /**
496  * Like _dbus_write_two() but only works on sockets and is thus
497  * available on Windows.
498  *
499  * @param fd the file descriptor
500  * @param buffer1 first buffer
501  * @param start1 first byte to write in first buffer
502  * @param len1 number of bytes to write from first buffer
503  * @param buffer2 second buffer, or #NULL
504  * @param start2 first byte to write in second buffer
505  * @param len2 number of bytes to write in second buffer
506  * @returns total bytes written from both buffers, or -1 on error
507  */
508 int
509 _dbus_write_socket_two (int               fd,
510                         const DBusString *buffer1,
511                         int               start1,
512                         int               len1,
513                         const DBusString *buffer2,
514                         int               start2,
515                         int               len2)
516 {
517 #if HAVE_DECL_MSG_NOSIGNAL
518   struct iovec vectors[2];
519   const char *data1;
520   const char *data2;
521   int bytes_written;
522   struct msghdr m;
523
524   _dbus_assert (buffer1 != NULL);
525   _dbus_assert (start1 >= 0);
526   _dbus_assert (start2 >= 0);
527   _dbus_assert (len1 >= 0);
528   _dbus_assert (len2 >= 0);
529
530   data1 = _dbus_string_get_const_data_len (buffer1, start1, len1);
531
532   if (buffer2 != NULL)
533     data2 = _dbus_string_get_const_data_len (buffer2, start2, len2);
534   else
535     {
536       data2 = NULL;
537       start2 = 0;
538       len2 = 0;
539     }
540
541   vectors[0].iov_base = (char*) data1;
542   vectors[0].iov_len = len1;
543   vectors[1].iov_base = (char*) data2;
544   vectors[1].iov_len = len2;
545
546   _DBUS_ZERO(m);
547   m.msg_iov = vectors;
548   m.msg_iovlen = data2 ? 2 : 1;
549
550  again:
551
552   bytes_written = sendmsg (fd, &m, MSG_NOSIGNAL);
553
554   if (bytes_written < 0 && errno == EINTR)
555     goto again;
556
557   return bytes_written;
558
559 #else
560   return _dbus_write_two (fd, buffer1, start1, len1,
561                           buffer2, start2, len2);
562 #endif
563 }
564
565 dbus_bool_t
566 _dbus_socket_is_invalid (int fd)
567 {
568     return fd < 0 ? TRUE : FALSE;
569 }
570
571 /**
572  * Thin wrapper around the read() system call that appends
573  * the data it reads to the DBusString buffer. It appends
574  * up to the given count, and returns the same value
575  * and same errno as read(). The only exception is that
576  * _dbus_read() handles EINTR for you. Also, _dbus_read() can
577  * return ENOMEM, even though regular UNIX read doesn't.
578  *
579  * Unlike _dbus_read_socket(), _dbus_read() is not available
580  * on Windows.
581  *
582  * @param fd the file descriptor to read from
583  * @param buffer the buffer to append data to
584  * @param count the amount of data to read
585  * @returns the number of bytes read or -1
586  */
587 int
588 _dbus_read (int               fd,
589             DBusString       *buffer,
590             int               count)
591 {
592   int bytes_read;
593   int start;
594   char *data;
595
596   _dbus_assert (count >= 0);
597
598   start = _dbus_string_get_length (buffer);
599
600   if (!_dbus_string_lengthen (buffer, count))
601     {
602       errno = ENOMEM;
603       return -1;
604     }
605
606   data = _dbus_string_get_data_len (buffer, start, count);
607
608  again:
609
610   bytes_read = read (fd, data, count);
611
612   if (bytes_read < 0)
613     {
614       if (errno == EINTR)
615         goto again;
616       else
617         {
618           /* put length back (note that this doesn't actually realloc anything) */
619           _dbus_string_set_length (buffer, start);
620           return -1;
621         }
622     }
623   else
624     {
625       /* put length back (doesn't actually realloc) */
626       _dbus_string_set_length (buffer, start + bytes_read);
627
628 #if 0
629       if (bytes_read > 0)
630         _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
631 #endif
632
633       return bytes_read;
634     }
635 }
636
637 /**
638  * Thin wrapper around the write() system call that writes a part of a
639  * DBusString and handles EINTR for you.
640  *
641  * @param fd the file descriptor to write
642  * @param buffer the buffer to write data from
643  * @param start the first byte in the buffer to write
644  * @param len the number of bytes to try to write
645  * @returns the number of bytes written or -1 on error
646  */
647 int
648 _dbus_write (int               fd,
649              const DBusString *buffer,
650              int               start,
651              int               len)
652 {
653   const char *data;
654   int bytes_written;
655
656   data = _dbus_string_get_const_data_len (buffer, start, len);
657
658  again:
659
660   bytes_written = write (fd, data, len);
661
662   if (bytes_written < 0 && errno == EINTR)
663     goto again;
664
665 #if 0
666   if (bytes_written > 0)
667     _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
668 #endif
669
670   return bytes_written;
671 }
672
673 /**
674  * Like _dbus_write() but will use writev() if possible
675  * to write both buffers in sequence. The return value
676  * is the number of bytes written in the first buffer,
677  * plus the number written in the second. If the first
678  * buffer is written successfully and an error occurs
679  * writing the second, the number of bytes in the first
680  * is returned (i.e. the error is ignored), on systems that
681  * don't have writev. Handles EINTR for you.
682  * The second buffer may be #NULL.
683  *
684  * @param fd the file descriptor
685  * @param buffer1 first buffer
686  * @param start1 first byte to write in first buffer
687  * @param len1 number of bytes to write from first buffer
688  * @param buffer2 second buffer, or #NULL
689  * @param start2 first byte to write in second buffer
690  * @param len2 number of bytes to write in second buffer
691  * @returns total bytes written from both buffers, or -1 on error
692  */
693 int
694 _dbus_write_two (int               fd,
695                  const DBusString *buffer1,
696                  int               start1,
697                  int               len1,
698                  const DBusString *buffer2,
699                  int               start2,
700                  int               len2)
701 {
702   _dbus_assert (buffer1 != NULL);
703   _dbus_assert (start1 >= 0);
704   _dbus_assert (start2 >= 0);
705   _dbus_assert (len1 >= 0);
706   _dbus_assert (len2 >= 0);
707
708 #ifdef HAVE_WRITEV
709   {
710     struct iovec vectors[2];
711     const char *data1;
712     const char *data2;
713     int bytes_written;
714
715     data1 = _dbus_string_get_const_data_len (buffer1, start1, len1);
716
717     if (buffer2 != NULL)
718       data2 = _dbus_string_get_const_data_len (buffer2, start2, len2);
719     else
720       {
721         data2 = NULL;
722         start2 = 0;
723         len2 = 0;
724       }
725
726     vectors[0].iov_base = (char*) data1;
727     vectors[0].iov_len = len1;
728     vectors[1].iov_base = (char*) data2;
729     vectors[1].iov_len = len2;
730
731   again:
732
733     bytes_written = writev (fd,
734                             vectors,
735                             data2 ? 2 : 1);
736
737     if (bytes_written < 0 && errno == EINTR)
738       goto again;
739
740     return bytes_written;
741   }
742 #else /* HAVE_WRITEV */
743   {
744     int ret1;
745
746     ret1 = _dbus_write (fd, buffer1, start1, len1);
747     if (ret1 == len1 && buffer2 != NULL)
748       {
749         ret2 = _dbus_write (fd, buffer2, start2, len2);
750         if (ret2 < 0)
751           ret2 = 0; /* we can't report an error as the first write was OK */
752
753         return ret1 + ret2;
754       }
755     else
756       return ret1;
757   }
758 #endif /* !HAVE_WRITEV */
759 }
760
761 #define _DBUS_MAX_SUN_PATH_LENGTH 99
762
763 /**
764  * @def _DBUS_MAX_SUN_PATH_LENGTH
765  *
766  * Maximum length of the path to a UNIX domain socket,
767  * sockaddr_un::sun_path member. POSIX requires that all systems
768  * support at least 100 bytes here, including the nul termination.
769  * We use 99 for the max value to allow for the nul.
770  *
771  * We could probably also do sizeof (addr.sun_path)
772  * but this way we are the same on all platforms
773  * which is probably a good idea.
774  */
775
776 /**
777  * Creates a socket and connects it to the UNIX domain socket at the
778  * given path.  The connection fd is returned, and is set up as
779  * nonblocking.
780  *
781  * Uses abstract sockets instead of filesystem-linked sockets if
782  * requested (it's possible only on Linux; see "man 7 unix" on Linux).
783  * On non-Linux abstract socket usage always fails.
784  *
785  * This will set FD_CLOEXEC for the socket returned.
786  *
787  * @param path the path to UNIX domain socket
788  * @param abstract #TRUE to use abstract namespace
789  * @param error return location for error code
790  * @returns connection file descriptor or -1 on error
791  */
792 int
793 _dbus_connect_unix_socket (const char     *path,
794                            dbus_bool_t     abstract,
795                            DBusError      *error)
796 {
797   int fd;
798   size_t path_len;
799   struct sockaddr_un addr;
800
801   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
802
803   _dbus_verbose ("connecting to unix socket %s abstract=%d\n",
804                  path, abstract);
805
806
807   if (!_dbus_open_unix_socket (&fd, error))
808     {
809       _DBUS_ASSERT_ERROR_IS_SET(error);
810       return -1;
811     }
812   _DBUS_ASSERT_ERROR_IS_CLEAR(error);
813
814   _DBUS_ZERO (addr);
815   addr.sun_family = AF_UNIX;
816   path_len = strlen (path);
817
818   if (abstract)
819     {
820 #ifdef HAVE_ABSTRACT_SOCKETS
821       addr.sun_path[0] = '\0'; /* this is what says "use abstract" */
822       path_len++; /* Account for the extra nul byte added to the start of sun_path */
823
824       if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
825         {
826           dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
827                       "Abstract socket name too long\n");
828           _dbus_close (fd, NULL);
829           return -1;
830         }
831
832       strncpy (&addr.sun_path[1], path, path_len);
833       /* _dbus_verbose_bytes (addr.sun_path, sizeof (addr.sun_path)); */
834 #else /* HAVE_ABSTRACT_SOCKETS */
835       dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
836                       "Operating system does not support abstract socket namespace\n");
837       _dbus_close (fd, NULL);
838       return -1;
839 #endif /* ! HAVE_ABSTRACT_SOCKETS */
840     }
841   else
842     {
843       if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
844         {
845           dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
846                       "Socket name too long\n");
847           _dbus_close (fd, NULL);
848           return -1;
849         }
850
851       strncpy (addr.sun_path, path, path_len);
852     }
853
854   if (connect (fd, (struct sockaddr*) &addr, _DBUS_STRUCT_OFFSET (struct sockaddr_un, sun_path) + path_len) < 0)
855     {
856       dbus_set_error (error,
857                       _dbus_error_from_errno (errno),
858                       "Failed to connect to socket %s: %s",
859                       path, _dbus_strerror (errno));
860
861       _dbus_close (fd, NULL);
862       return -1;
863     }
864
865   if (!_dbus_set_fd_nonblocking (fd, error))
866     {
867       _DBUS_ASSERT_ERROR_IS_SET (error);
868
869       _dbus_close (fd, NULL);
870       return -1;
871     }
872
873   return fd;
874 }
875
876 /**
877  * Creates a UNIX domain socket and connects it to the specified
878  * process to execute.
879  *
880  * This will set FD_CLOEXEC for the socket returned.
881  *
882  * @param path the path to the executable
883  * @param argv the argument list for the process to execute.
884  * argv[0] typically is identical to the path of the executable
885  * @param error return location for error code
886  * @returns connection file descriptor or -1 on error
887  */
888 int
889 _dbus_connect_exec (const char     *path,
890                     char *const    argv[],
891                     DBusError      *error)
892 {
893   int fds[2];
894   pid_t pid;
895
896   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
897
898   _dbus_verbose ("connecting to process %s\n", path);
899
900   if (socketpair (AF_UNIX, SOCK_STREAM
901 #ifdef SOCK_CLOEXEC
902                   |SOCK_CLOEXEC
903 #endif
904                   , 0, fds) < 0)
905     {
906       dbus_set_error (error,
907                       _dbus_error_from_errno (errno),
908                       "Failed to create socket pair: %s",
909                       _dbus_strerror (errno));
910       return -1;
911     }
912
913   _dbus_fd_set_close_on_exec (fds[0]);
914   _dbus_fd_set_close_on_exec (fds[1]);
915
916   pid = fork ();
917   if (pid < 0)
918     {
919       dbus_set_error (error,
920                       _dbus_error_from_errno (errno),
921                       "Failed to fork() to call %s: %s",
922                       path, _dbus_strerror (errno));
923       close (fds[0]);
924       close (fds[1]);
925       return -1;
926     }
927
928   if (pid == 0)
929     {
930       /* child */
931       close (fds[0]);
932
933       dup2 (fds[1], STDIN_FILENO);
934       dup2 (fds[1], STDOUT_FILENO);
935
936       if (fds[1] != STDIN_FILENO &&
937           fds[1] != STDOUT_FILENO)
938         close (fds[1]);
939
940       /* Inherit STDERR and the controlling terminal from the
941          parent */
942
943       _dbus_close_all ();
944
945       execvp (path, argv);
946
947       fprintf (stderr, "Failed to execute process %s: %s\n", path, _dbus_strerror (errno));
948
949       _exit(1);
950     }
951
952   /* parent */
953   close (fds[1]);
954
955   if (!_dbus_set_fd_nonblocking (fds[0], error))
956     {
957       _DBUS_ASSERT_ERROR_IS_SET (error);
958
959       close (fds[0]);
960       return -1;
961     }
962
963   return fds[0];
964 }
965
966 /**
967  * Enables or disables the reception of credentials on the given socket during
968  * the next message transmission.  This is only effective if the #LOCAL_CREDS
969  * system feature exists, in which case the other side of the connection does
970  * not have to do anything special to send the credentials.
971  *
972  * @param fd socket on which to change the #LOCAL_CREDS flag.
973  * @param on whether to enable or disable the #LOCAL_CREDS flag.
974  */
975 static dbus_bool_t
976 _dbus_set_local_creds (int fd, dbus_bool_t on)
977 {
978   dbus_bool_t retval = TRUE;
979
980 #if defined(HAVE_CMSGCRED)
981   /* NOOP just to make sure only one codepath is used
982    *      and to prefer CMSGCRED
983    */
984 #elif defined(LOCAL_CREDS)
985   int val = on ? 1 : 0;
986   if (setsockopt (fd, 0, LOCAL_CREDS, &val, sizeof (val)) < 0)
987     {
988       _dbus_verbose ("Unable to set LOCAL_CREDS socket option on fd %d\n", fd);
989       retval = FALSE;
990     }
991   else
992     _dbus_verbose ("LOCAL_CREDS %s for further messages on fd %d\n",
993                    on ? "enabled" : "disabled", fd);
994 #endif
995
996   return retval;
997 }
998
999 /**
1000  * Creates a socket and binds it to the given path,
1001  * then listens on the socket. The socket is
1002  * set to be nonblocking.
1003  *
1004  * Uses abstract sockets instead of filesystem-linked
1005  * sockets if requested (it's possible only on Linux;
1006  * see "man 7 unix" on Linux).
1007  * On non-Linux abstract socket usage always fails.
1008  *
1009  * This will set FD_CLOEXEC for the socket returned
1010  *
1011  * @param path the socket name
1012  * @param abstract #TRUE to use abstract namespace
1013  * @param error return location for errors
1014  * @returns the listening file descriptor or -1 on error
1015  */
1016 int
1017 _dbus_listen_unix_socket (const char     *path,
1018                           dbus_bool_t     abstract,
1019                           DBusError      *error)
1020 {
1021   int listen_fd;
1022   struct sockaddr_un addr;
1023   size_t path_len;
1024   unsigned int reuseaddr;
1025
1026   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1027
1028   _dbus_verbose ("listening on unix socket %s abstract=%d\n",
1029                  path, abstract);
1030
1031   if (!_dbus_open_unix_socket (&listen_fd, error))
1032     {
1033       _DBUS_ASSERT_ERROR_IS_SET(error);
1034       return -1;
1035     }
1036   _DBUS_ASSERT_ERROR_IS_CLEAR(error);
1037
1038   _DBUS_ZERO (addr);
1039   addr.sun_family = AF_UNIX;
1040   path_len = strlen (path);
1041
1042   if (abstract)
1043     {
1044 #ifdef HAVE_ABSTRACT_SOCKETS
1045       /* remember that abstract names aren't nul-terminated so we rely
1046        * on sun_path being filled in with zeroes above.
1047        */
1048       addr.sun_path[0] = '\0'; /* this is what says "use abstract" */
1049       path_len++; /* Account for the extra nul byte added to the start of sun_path */
1050
1051       if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
1052         {
1053           dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
1054                       "Abstract socket name too long\n");
1055           _dbus_close (listen_fd, NULL);
1056           return -1;
1057         }
1058
1059       strncpy (&addr.sun_path[1], path, path_len);
1060       /* _dbus_verbose_bytes (addr.sun_path, sizeof (addr.sun_path)); */
1061 #else /* HAVE_ABSTRACT_SOCKETS */
1062       dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
1063                       "Operating system does not support abstract socket namespace\n");
1064       _dbus_close (listen_fd, NULL);
1065       return -1;
1066 #endif /* ! HAVE_ABSTRACT_SOCKETS */
1067     }
1068   else
1069     {
1070       /* Discussed security implications of this with Nalin,
1071        * and we couldn't think of where it would kick our ass, but
1072        * it still seems a bit sucky. It also has non-security suckage;
1073        * really we'd prefer to exit if the socket is already in use.
1074        * But there doesn't seem to be a good way to do this.
1075        *
1076        * Just to be extra careful, I threw in the stat() - clearly
1077        * the stat() can't *fix* any security issue, but it at least
1078        * avoids inadvertent/accidental data loss.
1079        */
1080       {
1081         struct stat sb;
1082
1083         if (stat (path, &sb) == 0 &&
1084             S_ISSOCK (sb.st_mode))
1085           unlink (path);
1086       }
1087
1088       if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
1089         {
1090           dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
1091                       "Abstract socket name too long\n");
1092           _dbus_close (listen_fd, NULL);
1093           return -1;
1094         }
1095
1096       strncpy (addr.sun_path, path, path_len);
1097     }
1098
1099   reuseaddr = 1;
1100   if (setsockopt  (listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr))==-1)
1101     {
1102       _dbus_warn ("Failed to set socket option\"%s\": %s",
1103                   path, _dbus_strerror (errno));
1104     }
1105
1106   if (bind (listen_fd, (struct sockaddr*) &addr, _DBUS_STRUCT_OFFSET (struct sockaddr_un, sun_path) + path_len) < 0)
1107     {
1108       dbus_set_error (error, _dbus_error_from_errno (errno),
1109                       "Failed to bind socket \"%s\": %s",
1110                       path, _dbus_strerror (errno));
1111       _dbus_close (listen_fd, NULL);
1112       return -1;
1113     }
1114
1115   if (listen (listen_fd, 30 /* backlog */) < 0)
1116     {
1117       dbus_set_error (error, _dbus_error_from_errno (errno),
1118                       "Failed to listen on socket \"%s\": %s",
1119                       path, _dbus_strerror (errno));
1120       _dbus_close (listen_fd, NULL);
1121       return -1;
1122     }
1123
1124   if (!_dbus_set_local_creds (listen_fd, TRUE))
1125     {
1126       dbus_set_error (error, _dbus_error_from_errno (errno),
1127                       "Failed to enable LOCAL_CREDS on socket \"%s\": %s",
1128                       path, _dbus_strerror (errno));
1129       close (listen_fd);
1130       return -1;
1131     }
1132
1133   if (!_dbus_set_fd_nonblocking (listen_fd, error))
1134     {
1135       _DBUS_ASSERT_ERROR_IS_SET (error);
1136       _dbus_close (listen_fd, NULL);
1137       return -1;
1138     }
1139
1140   /* Try opening up the permissions, but if we can't, just go ahead
1141    * and continue, maybe it will be good enough.
1142    */
1143   if (!abstract && chmod (path, 0777) < 0)
1144     _dbus_warn ("Could not set mode 0777 on socket %s\n",
1145                 path);
1146
1147   return listen_fd;
1148 }
1149
1150 /**
1151  * Acquires one or more sockets passed in from systemd. The sockets
1152  * are set to be nonblocking.
1153  *
1154  * This will set FD_CLOEXEC for the sockets returned.
1155  *
1156  * @param fds the file descriptors
1157  * @param error return location for errors
1158  * @returns the number of file descriptors
1159  */
1160 int
1161 _dbus_listen_systemd_sockets (int       **fds,
1162                               DBusError *error)
1163 {
1164   int r, n;
1165   unsigned fd;
1166   int *new_fds;
1167
1168   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1169
1170   n = sd_listen_fds (TRUE);
1171   if (n < 0)
1172     {
1173       dbus_set_error (error, _dbus_error_from_errno (-n),
1174                       "Failed to acquire systemd socket: %s",
1175                       _dbus_strerror (-n));
1176       return -1;
1177     }
1178
1179   if (n <= 0)
1180     {
1181       dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
1182                       "No socket received.");
1183       return -1;
1184     }
1185
1186   for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++)
1187     {
1188       r = sd_is_socket (fd, AF_UNSPEC, SOCK_STREAM, 1);
1189       if (r < 0)
1190         {
1191           dbus_set_error (error, _dbus_error_from_errno (-r),
1192                           "Failed to verify systemd socket type: %s",
1193                           _dbus_strerror (-r));
1194           return -1;
1195         }
1196
1197       if (!r)
1198         {
1199           dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
1200                           "Passed socket has wrong type.");
1201           return -1;
1202         }
1203     }
1204
1205   /* OK, the file descriptors are all good, so let's take posession of
1206      them then. */
1207
1208   new_fds = dbus_new (int, n);
1209   if (!new_fds)
1210     {
1211       dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1212                       "Failed to allocate file handle array.");
1213       goto fail;
1214     }
1215
1216   for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++)
1217     {
1218       if (!_dbus_set_local_creds (fd, TRUE))
1219         {
1220           dbus_set_error (error, _dbus_error_from_errno (errno),
1221                           "Failed to enable LOCAL_CREDS on systemd socket: %s",
1222                           _dbus_strerror (errno));
1223           goto fail;
1224         }
1225
1226       if (!_dbus_set_fd_nonblocking (fd, error))
1227         {
1228           _DBUS_ASSERT_ERROR_IS_SET (error);
1229           goto fail;
1230         }
1231
1232       new_fds[fd - SD_LISTEN_FDS_START] = fd;
1233     }
1234
1235   *fds = new_fds;
1236   return n;
1237
1238  fail:
1239
1240   for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++)
1241     {
1242       _dbus_close (fd, NULL);
1243     }
1244
1245   dbus_free (new_fds);
1246   return -1;
1247 }
1248
1249 /**
1250  * Creates a socket and connects to a socket at the given host
1251  * and port. The connection fd is returned, and is set up as
1252  * nonblocking.
1253  *
1254  * This will set FD_CLOEXEC for the socket returned
1255  *
1256  * @param host the host name to connect to
1257  * @param port the port to connect to
1258  * @param family the address family to listen on, NULL for all
1259  * @param error return location for error code
1260  * @returns connection file descriptor or -1 on error
1261  */
1262 int
1263 _dbus_connect_tcp_socket (const char     *host,
1264                           const char     *port,
1265                           const char     *family,
1266                           DBusError      *error)
1267 {
1268     return _dbus_connect_tcp_socket_with_nonce (host, port, family, (const char*)NULL, error);
1269 }
1270
1271 int
1272 _dbus_connect_tcp_socket_with_nonce (const char     *host,
1273                                      const char     *port,
1274                                      const char     *family,
1275                                      const char     *noncefile,
1276                                      DBusError      *error)
1277 {
1278   int saved_errno = 0;
1279   int fd = -1, res;
1280   struct addrinfo hints;
1281   struct addrinfo *ai, *tmp;
1282
1283   _DBUS_ASSERT_ERROR_IS_CLEAR(error);
1284
1285   _DBUS_ZERO (hints);
1286
1287   if (!family)
1288     hints.ai_family = AF_UNSPEC;
1289   else if (!strcmp(family, "ipv4"))
1290     hints.ai_family = AF_INET;
1291   else if (!strcmp(family, "ipv6"))
1292     hints.ai_family = AF_INET6;
1293   else
1294     {
1295       dbus_set_error (error,
1296                       DBUS_ERROR_BAD_ADDRESS,
1297                       "Unknown address family %s", family);
1298       return -1;
1299     }
1300   hints.ai_protocol = IPPROTO_TCP;
1301   hints.ai_socktype = SOCK_STREAM;
1302   hints.ai_flags = AI_ADDRCONFIG;
1303
1304   if ((res = getaddrinfo(host, port, &hints, &ai)) != 0)
1305     {
1306       dbus_set_error (error,
1307                       _dbus_error_from_errno (errno),
1308                       "Failed to lookup host/port: \"%s:%s\": %s (%d)",
1309                       host, port, gai_strerror(res), res);
1310       return -1;
1311     }
1312
1313   tmp = ai;
1314   while (tmp)
1315     {
1316       if (!_dbus_open_socket (&fd, tmp->ai_family, SOCK_STREAM, 0, error))
1317         {
1318           freeaddrinfo(ai);
1319           _DBUS_ASSERT_ERROR_IS_SET(error);
1320           return -1;
1321         }
1322       _DBUS_ASSERT_ERROR_IS_CLEAR(error);
1323
1324       if (connect (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) < 0)
1325         {
1326           saved_errno = errno;
1327           _dbus_close(fd, NULL);
1328           fd = -1;
1329           tmp = tmp->ai_next;
1330           continue;
1331         }
1332
1333       break;
1334     }
1335   freeaddrinfo(ai);
1336
1337   if (fd == -1)
1338     {
1339       dbus_set_error (error,
1340                       _dbus_error_from_errno (saved_errno),
1341                       "Failed to connect to socket \"%s:%s\" %s",
1342                       host, port, _dbus_strerror(saved_errno));
1343       return -1;
1344     }
1345
1346   if (noncefile != NULL)
1347     {
1348       DBusString noncefileStr;
1349       dbus_bool_t ret;
1350       _dbus_string_init_const (&noncefileStr, noncefile);
1351       ret = _dbus_send_nonce (fd, &noncefileStr, error);
1352       _dbus_string_free (&noncefileStr);
1353
1354       if (!ret)
1355     {
1356       _dbus_close (fd, NULL);
1357           return -1;
1358         }
1359     }
1360
1361   if (!_dbus_set_fd_nonblocking (fd, error))
1362     {
1363       _dbus_close (fd, NULL);
1364       return -1;
1365     }
1366
1367   return fd;
1368 }
1369
1370 /**
1371  * Creates a socket and binds it to the given path, then listens on
1372  * the socket. The socket is set to be nonblocking.  In case of port=0
1373  * a random free port is used and returned in the port parameter.
1374  * If inaddr_any is specified, the hostname is ignored.
1375  *
1376  * This will set FD_CLOEXEC for the socket returned
1377  *
1378  * @param host the host name to listen on
1379  * @param port the port to listen on, if zero a free port will be used
1380  * @param family the address family to listen on, NULL for all
1381  * @param retport string to return the actual port listened on
1382  * @param fds_p location to store returned file descriptors
1383  * @param error return location for errors
1384  * @returns the number of listening file descriptors or -1 on error
1385  */
1386 int
1387 _dbus_listen_tcp_socket (const char     *host,
1388                          const char     *port,
1389                          const char     *family,
1390                          DBusString     *retport,
1391                          int           **fds_p,
1392                          DBusError      *error)
1393 {
1394   int saved_errno;
1395   int nlisten_fd = 0, *listen_fd = NULL, res, i;
1396   struct addrinfo hints;
1397   struct addrinfo *ai, *tmp;
1398   unsigned int reuseaddr;
1399
1400   *fds_p = NULL;
1401   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1402
1403   _DBUS_ZERO (hints);
1404
1405   if (!family)
1406     hints.ai_family = AF_UNSPEC;
1407   else if (!strcmp(family, "ipv4"))
1408     hints.ai_family = AF_INET;
1409   else if (!strcmp(family, "ipv6"))
1410     hints.ai_family = AF_INET6;
1411   else
1412     {
1413       dbus_set_error (error,
1414                       DBUS_ERROR_BAD_ADDRESS,
1415                       "Unknown address family %s", family);
1416       return -1;
1417     }
1418
1419   hints.ai_protocol = IPPROTO_TCP;
1420   hints.ai_socktype = SOCK_STREAM;
1421   hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
1422
1423  redo_lookup_with_port:
1424   ai = NULL;
1425   if ((res = getaddrinfo(host, port, &hints, &ai)) != 0 || !ai)
1426     {
1427       dbus_set_error (error,
1428                       _dbus_error_from_errno (errno),
1429                       "Failed to lookup host/port: \"%s:%s\": %s (%d)",
1430                       host ? host : "*", port, gai_strerror(res), res);
1431       goto failed;
1432     }
1433
1434   tmp = ai;
1435   while (tmp)
1436     {
1437       int fd = -1, *newlisten_fd;
1438       if (!_dbus_open_socket (&fd, tmp->ai_family, SOCK_STREAM, 0, error))
1439         {
1440           _DBUS_ASSERT_ERROR_IS_SET(error);
1441           goto failed;
1442         }
1443       _DBUS_ASSERT_ERROR_IS_CLEAR(error);
1444
1445       reuseaddr = 1;
1446       if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr))==-1)
1447         {
1448           _dbus_warn ("Failed to set socket option \"%s:%s\": %s",
1449                       host ? host : "*", port, _dbus_strerror (errno));
1450         }
1451
1452       if (bind (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) < 0)
1453         {
1454           saved_errno = errno;
1455           _dbus_close(fd, NULL);
1456           if (saved_errno == EADDRINUSE)
1457             {
1458               /* Depending on kernel policy, it may or may not
1459                  be neccessary to bind to both IPv4 & 6 addresses
1460                  so ignore EADDRINUSE here */
1461               tmp = tmp->ai_next;
1462               continue;
1463             }
1464           dbus_set_error (error, _dbus_error_from_errno (saved_errno),
1465                           "Failed to bind socket \"%s:%s\": %s",
1466                           host ? host : "*", port, _dbus_strerror (saved_errno));
1467           goto failed;
1468         }
1469
1470       if (listen (fd, 30 /* backlog */) < 0)
1471         {
1472           saved_errno = errno;
1473           _dbus_close (fd, NULL);
1474           dbus_set_error (error, _dbus_error_from_errno (saved_errno),
1475                           "Failed to listen on socket \"%s:%s\": %s",
1476                           host ? host : "*", port, _dbus_strerror (saved_errno));
1477           goto failed;
1478         }
1479
1480       newlisten_fd = dbus_realloc(listen_fd, sizeof(int)*(nlisten_fd+1));
1481       if (!newlisten_fd)
1482         {
1483           saved_errno = errno;
1484           _dbus_close (fd, NULL);
1485           dbus_set_error (error, _dbus_error_from_errno (saved_errno),
1486                           "Failed to allocate file handle array: %s",
1487                           _dbus_strerror (saved_errno));
1488           goto failed;
1489         }
1490       listen_fd = newlisten_fd;
1491       listen_fd[nlisten_fd] = fd;
1492       nlisten_fd++;
1493
1494       if (!_dbus_string_get_length(retport))
1495         {
1496           /* If the user didn't specify a port, or used 0, then
1497              the kernel chooses a port. After the first address
1498              is bound to, we need to force all remaining addresses
1499              to use the same port */
1500           if (!port || !strcmp(port, "0"))
1501             {
1502               int result;
1503               struct sockaddr_storage addr;
1504               socklen_t addrlen;
1505               char portbuf[50];
1506
1507               addrlen = sizeof(addr);
1508               result = getsockname(fd, (struct sockaddr*) &addr, &addrlen);
1509
1510               if (result == -1 ||
1511                   (res = getnameinfo ((struct sockaddr*)&addr, addrlen, NULL, 0,
1512                                       portbuf, sizeof(portbuf),
1513                                       NI_NUMERICHOST)) != 0)
1514                 {
1515                   dbus_set_error (error, _dbus_error_from_errno (errno),
1516                                   "Failed to resolve port \"%s:%s\": %s (%s)",
1517                                   host ? host : "*", port, gai_strerror(res), res);
1518                   goto failed;
1519                 }
1520               if (!_dbus_string_append(retport, portbuf))
1521                 {
1522                   dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1523                   goto failed;
1524                 }
1525
1526               /* Release current address list & redo lookup */
1527               port = _dbus_string_get_const_data(retport);
1528               freeaddrinfo(ai);
1529               goto redo_lookup_with_port;
1530             }
1531           else
1532             {
1533               if (!_dbus_string_append(retport, port))
1534                 {
1535                     dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1536                     goto failed;
1537                 }
1538             }
1539         }
1540
1541       tmp = tmp->ai_next;
1542     }
1543   freeaddrinfo(ai);
1544   ai = NULL;
1545
1546   if (!nlisten_fd)
1547     {
1548       errno = EADDRINUSE;
1549       dbus_set_error (error, _dbus_error_from_errno (errno),
1550                       "Failed to bind socket \"%s:%s\": %s",
1551                       host ? host : "*", port, _dbus_strerror (errno));
1552       goto failed;
1553     }
1554
1555   for (i = 0 ; i < nlisten_fd ; i++)
1556     {
1557       if (!_dbus_set_fd_nonblocking (listen_fd[i], error))
1558         {
1559           goto failed;
1560         }
1561     }
1562
1563   *fds_p = listen_fd;
1564
1565   return nlisten_fd;
1566
1567  failed:
1568   if (ai)
1569     freeaddrinfo(ai);
1570   for (i = 0 ; i < nlisten_fd ; i++)
1571     _dbus_close(listen_fd[i], NULL);
1572   dbus_free(listen_fd);
1573   return -1;
1574 }
1575
1576 static dbus_bool_t
1577 write_credentials_byte (int             server_fd,
1578                         DBusError      *error)
1579 {
1580   int bytes_written;
1581   char buf[1] = { '\0' };
1582 #if defined(HAVE_CMSGCRED)
1583   union {
1584           struct cmsghdr hdr;
1585           char cred[CMSG_SPACE (sizeof (struct cmsgcred))];
1586   } cmsg;
1587   struct iovec iov;
1588   struct msghdr msg;
1589   iov.iov_base = buf;
1590   iov.iov_len = 1;
1591
1592   _DBUS_ZERO(msg);
1593   msg.msg_iov = &iov;
1594   msg.msg_iovlen = 1;
1595
1596   msg.msg_control = (caddr_t) &cmsg;
1597   msg.msg_controllen = CMSG_SPACE (sizeof (struct cmsgcred));
1598   _DBUS_ZERO(cmsg);
1599   cmsg.hdr.cmsg_len = CMSG_LEN (sizeof (struct cmsgcred));
1600   cmsg.hdr.cmsg_level = SOL_SOCKET;
1601   cmsg.hdr.cmsg_type = SCM_CREDS;
1602 #endif
1603
1604   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1605
1606  again:
1607
1608 #if defined(HAVE_CMSGCRED)
1609   bytes_written = sendmsg (server_fd, &msg, 0
1610 #if HAVE_DECL_MSG_NOSIGNAL
1611                            |MSG_NOSIGNAL
1612 #endif
1613                            );
1614 #else
1615   bytes_written = send (server_fd, buf, 1, 0
1616 #if HAVE_DECL_MSG_NOSIGNAL
1617                         |MSG_NOSIGNAL
1618 #endif
1619                         );
1620 #endif
1621
1622   if (bytes_written < 0 && errno == EINTR)
1623     goto again;
1624
1625   if (bytes_written < 0)
1626     {
1627       dbus_set_error (error, _dbus_error_from_errno (errno),
1628                       "Failed to write credentials byte: %s",
1629                      _dbus_strerror (errno));
1630       return FALSE;
1631     }
1632   else if (bytes_written == 0)
1633     {
1634       dbus_set_error (error, DBUS_ERROR_IO_ERROR,
1635                       "wrote zero bytes writing credentials byte");
1636       return FALSE;
1637     }
1638   else
1639     {
1640       _dbus_assert (bytes_written == 1);
1641       _dbus_verbose ("wrote credentials byte\n");
1642       return TRUE;
1643     }
1644 }
1645
1646 /**
1647  * Reads a single byte which must be nul (an error occurs otherwise),
1648  * and reads unix credentials if available. Clears the credentials
1649  * object, then adds pid/uid if available, so any previous credentials
1650  * stored in the object are lost.
1651  *
1652  * Return value indicates whether a byte was read, not whether
1653  * we got valid credentials. On some systems, such as Linux,
1654  * reading/writing the byte isn't actually required, but we do it
1655  * anyway just to avoid multiple codepaths.
1656  *
1657  * Fails if no byte is available, so you must select() first.
1658  *
1659  * The point of the byte is that on some systems we have to
1660  * use sendmsg()/recvmsg() to transmit credentials.
1661  *
1662  * @param client_fd the client file descriptor
1663  * @param credentials object to add client credentials to
1664  * @param error location to store error code
1665  * @returns #TRUE on success
1666  */
1667 dbus_bool_t
1668 _dbus_read_credentials_socket  (int              client_fd,
1669                                 DBusCredentials *credentials,
1670                                 DBusError       *error)
1671 {
1672   struct msghdr msg;
1673   struct iovec iov;
1674   char buf;
1675   dbus_uid_t uid_read;
1676   dbus_pid_t pid_read;
1677   int bytes_read;
1678
1679 #ifdef HAVE_CMSGCRED
1680   union {
1681     struct cmsghdr hdr;
1682     char cred[CMSG_SPACE (sizeof (struct cmsgcred))];
1683   } cmsg;
1684
1685 #elif defined(LOCAL_CREDS)
1686   struct {
1687     struct cmsghdr hdr;
1688     struct sockcred cred;
1689   } cmsg;
1690 #endif
1691
1692   uid_read = DBUS_UID_UNSET;
1693   pid_read = DBUS_PID_UNSET;
1694
1695   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1696
1697   /* The POSIX spec certainly doesn't promise this, but
1698    * we need these assertions to fail as soon as we're wrong about
1699    * it so we can do the porting fixups
1700    */
1701   _dbus_assert (sizeof (pid_t) <= sizeof (dbus_pid_t));
1702   _dbus_assert (sizeof (uid_t) <= sizeof (dbus_uid_t));
1703   _dbus_assert (sizeof (gid_t) <= sizeof (dbus_gid_t));
1704
1705   _dbus_credentials_clear (credentials);
1706
1707   /* Systems supporting LOCAL_CREDS are configured to have this feature
1708    * enabled (if it does not conflict with HAVE_CMSGCRED) prior accepting
1709    * the connection.  Therefore, the received message must carry the
1710    * credentials information without doing anything special.
1711    */
1712
1713   iov.iov_base = &buf;
1714   iov.iov_len = 1;
1715
1716   _DBUS_ZERO(msg);
1717   msg.msg_iov = &iov;
1718   msg.msg_iovlen = 1;
1719
1720 #if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
1721   _DBUS_ZERO(cmsg);
1722   msg.msg_control = (caddr_t) &cmsg;
1723   msg.msg_controllen = CMSG_SPACE (sizeof (struct cmsgcred));
1724 #endif
1725
1726  again:
1727   bytes_read = recvmsg (client_fd, &msg, 0);
1728
1729   if (bytes_read < 0)
1730     {
1731       if (errno == EINTR)
1732         goto again;
1733
1734       /* EAGAIN or EWOULDBLOCK would be unexpected here since we would
1735        * normally only call read_credentials if the socket was ready
1736        * for reading
1737        */
1738
1739       dbus_set_error (error, _dbus_error_from_errno (errno),
1740                       "Failed to read credentials byte: %s",
1741                       _dbus_strerror (errno));
1742       return FALSE;
1743     }
1744   else if (bytes_read == 0)
1745     {
1746       /* this should not happen unless we are using recvmsg wrong,
1747        * so is essentially here for paranoia
1748        */
1749       dbus_set_error (error, DBUS_ERROR_FAILED,
1750                       "Failed to read credentials byte (zero-length read)");
1751       return FALSE;
1752     }
1753   else if (buf != '\0')
1754     {
1755       dbus_set_error (error, DBUS_ERROR_FAILED,
1756                       "Credentials byte was not nul");
1757       return FALSE;
1758     }
1759
1760 #if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
1761   if (cmsg.hdr.cmsg_len < CMSG_LEN (sizeof (struct cmsgcred))
1762                   || cmsg.hdr.cmsg_type != SCM_CREDS)
1763     {
1764       dbus_set_error (error, DBUS_ERROR_FAILED,
1765                       "Message from recvmsg() was not SCM_CREDS");
1766       return FALSE;
1767     }
1768 #endif
1769
1770   _dbus_verbose ("read credentials byte\n");
1771
1772   {
1773 #ifdef SO_PEERCRED
1774 #ifdef __OpenBSD__
1775     struct sockpeercred cr;
1776 #else
1777     struct ucred cr;
1778 #endif
1779     int cr_len = sizeof (cr);
1780
1781     if (getsockopt (client_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) == 0 &&
1782         cr_len == sizeof (cr))
1783       {
1784         pid_read = cr.pid;
1785         uid_read = cr.uid;
1786       }
1787     else
1788       {
1789         _dbus_verbose ("Failed to getsockopt() credentials, returned len %d/%d: %s\n",
1790                        cr_len, (int) sizeof (cr), _dbus_strerror (errno));
1791       }
1792 #elif defined(HAVE_CMSGCRED)
1793     struct cmsgcred *cred;
1794
1795     cred = (struct cmsgcred *) CMSG_DATA (&cmsg.hdr);
1796     pid_read = cred->cmcred_pid;
1797     uid_read = cred->cmcred_euid;
1798 #elif defined(LOCAL_CREDS)
1799     pid_read = DBUS_PID_UNSET;
1800     uid_read = cmsg.cred.sc_uid;
1801     /* Since we have already got the credentials from this socket, we can
1802      * disable its LOCAL_CREDS flag if it was ever set. */
1803     _dbus_set_local_creds (client_fd, FALSE);
1804 #elif defined(HAVE_GETPEEREID)
1805     uid_t euid;
1806     gid_t egid;
1807     if (getpeereid (client_fd, &euid, &egid) == 0)
1808       {
1809         uid_read = euid;
1810       }
1811     else
1812       {
1813         _dbus_verbose ("Failed to getpeereid() credentials: %s\n", _dbus_strerror (errno));
1814       }
1815 #elif defined(HAVE_GETPEERUCRED)
1816     ucred_t * ucred = NULL;
1817     if (getpeerucred (client_fd, &ucred) == 0)
1818       {
1819         pid_read = ucred_getpid (ucred);
1820         uid_read = ucred_geteuid (ucred);
1821 #ifdef HAVE_ADT
1822         /* generate audit session data based on socket ucred */
1823         adt_session_data_t *adth = NULL;
1824         adt_export_data_t *data = NULL;
1825         size_t size = 0;
1826         if (adt_start_session (&adth, NULL, 0) || (adth == NULL))
1827           {
1828             _dbus_verbose ("Failed to adt_start_session(): %s\n", _dbus_strerror (errno));
1829           }
1830         else
1831           {
1832             if (adt_set_from_ucred (adth, ucred, ADT_NEW))
1833               {
1834                 _dbus_verbose ("Failed to adt_set_from_ucred(): %s\n", _dbus_strerror (errno));
1835               }
1836             else
1837               {
1838                 size = adt_export_session_data (adth, &data);
1839                 if (size <= 0)
1840                   {
1841                     _dbus_verbose ("Failed to adt_export_session_data(): %s\n", _dbus_strerror (errno));
1842                   }
1843                 else
1844                   {
1845                     _dbus_credentials_add_adt_audit_data (credentials, data, size);
1846                     free (data);
1847                   }
1848               }
1849             (void) adt_end_session (adth);
1850           }
1851 #endif /* HAVE_ADT */
1852       }
1853     else
1854       {
1855         _dbus_verbose ("Failed to getpeerucred() credentials: %s\n", _dbus_strerror (errno));
1856       }
1857     if (ucred != NULL)
1858       ucred_free (ucred);
1859 #else /* !SO_PEERCRED && !HAVE_CMSGCRED && !HAVE_GETPEEREID && !HAVE_GETPEERUCRED */
1860     _dbus_verbose ("Socket credentials not supported on this OS\n");
1861 #endif
1862   }
1863
1864   _dbus_verbose ("Credentials:"
1865                  "  pid "DBUS_PID_FORMAT
1866                  "  uid "DBUS_UID_FORMAT
1867                  "\n",
1868                  pid_read,
1869                  uid_read);
1870
1871   if (pid_read != DBUS_PID_UNSET)
1872     {
1873       if (!_dbus_credentials_add_pid (credentials, pid_read))
1874         {
1875           _DBUS_SET_OOM (error);
1876           return FALSE;
1877         }
1878     }
1879
1880   if (uid_read != DBUS_UID_UNSET)
1881     {
1882       if (!_dbus_credentials_add_unix_uid (credentials, uid_read))
1883         {
1884           _DBUS_SET_OOM (error);
1885           return FALSE;
1886         }
1887     }
1888
1889   return TRUE;
1890 }
1891
1892 /**
1893  * Sends a single nul byte with our UNIX credentials as ancillary
1894  * data.  Returns #TRUE if the data was successfully written.  On
1895  * systems that don't support sending credentials, just writes a byte,
1896  * doesn't send any credentials.  On some systems, such as Linux,
1897  * reading/writing the byte isn't actually required, but we do it
1898  * anyway just to avoid multiple codepaths.
1899  *
1900  * Fails if no byte can be written, so you must select() first.
1901  *
1902  * The point of the byte is that on some systems we have to
1903  * use sendmsg()/recvmsg() to transmit credentials.
1904  *
1905  * @param server_fd file descriptor for connection to server
1906  * @param error return location for error code
1907  * @returns #TRUE if the byte was sent
1908  */
1909 dbus_bool_t
1910 _dbus_send_credentials_socket  (int              server_fd,
1911                                 DBusError       *error)
1912 {
1913   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1914
1915   if (write_credentials_byte (server_fd, error))
1916     return TRUE;
1917   else
1918     return FALSE;
1919 }
1920
1921 /**
1922  * Accepts a connection on a listening socket.
1923  * Handles EINTR for you.
1924  *
1925  * This will enable FD_CLOEXEC for the returned socket.
1926  *
1927  * @param listen_fd the listen file descriptor
1928  * @returns the connection fd of the client, or -1 on error
1929  */
1930 int
1931 _dbus_accept  (int listen_fd)
1932 {
1933   int client_fd;
1934   struct sockaddr addr;
1935   socklen_t addrlen;
1936 #ifdef HAVE_ACCEPT4
1937   dbus_bool_t cloexec_done;
1938 #endif
1939
1940   addrlen = sizeof (addr);
1941
1942  retry:
1943
1944 #ifdef HAVE_ACCEPT4
1945   /* We assume that if accept4 is available SOCK_CLOEXEC is too */
1946   client_fd = accept4 (listen_fd, &addr, &addrlen, SOCK_CLOEXEC);
1947   cloexec_done = client_fd >= 0;
1948
1949   if (client_fd < 0 && errno == ENOSYS)
1950 #endif
1951     {
1952       client_fd = accept (listen_fd, &addr, &addrlen);
1953     }
1954
1955   if (client_fd < 0)
1956     {
1957       if (errno == EINTR)
1958         goto retry;
1959     }
1960
1961   _dbus_verbose ("client fd %d accepted\n", client_fd);
1962
1963 #ifdef HAVE_ACCEPT4
1964   if (!cloexec_done)
1965 #endif
1966     {
1967       _dbus_fd_set_close_on_exec(client_fd);
1968     }
1969
1970   return client_fd;
1971 }
1972
1973 /**
1974  * Checks to make sure the given directory is
1975  * private to the user
1976  *
1977  * @param dir the name of the directory
1978  * @param error error return
1979  * @returns #FALSE on failure
1980  **/
1981 dbus_bool_t
1982 _dbus_check_dir_is_private_to_user (DBusString *dir, DBusError *error)
1983 {
1984   const char *directory;
1985   struct stat sb;
1986
1987   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1988
1989   directory = _dbus_string_get_const_data (dir);
1990
1991   if (stat (directory, &sb) < 0)
1992     {
1993       dbus_set_error (error, _dbus_error_from_errno (errno),
1994                       "%s", _dbus_strerror (errno));
1995
1996       return FALSE;
1997     }
1998
1999   if ((S_IROTH & sb.st_mode) || (S_IWOTH & sb.st_mode) ||
2000       (S_IRGRP & sb.st_mode) || (S_IWGRP & sb.st_mode))
2001     {
2002       dbus_set_error (error, DBUS_ERROR_FAILED,
2003                      "%s directory is not private to the user", directory);
2004       return FALSE;
2005     }
2006
2007   return TRUE;
2008 }
2009
2010 static dbus_bool_t
2011 fill_user_info_from_passwd (struct passwd *p,
2012                             DBusUserInfo  *info,
2013                             DBusError     *error)
2014 {
2015   _dbus_assert (p->pw_name != NULL);
2016   _dbus_assert (p->pw_dir != NULL);
2017
2018   info->uid = p->pw_uid;
2019   info->primary_gid = p->pw_gid;
2020   info->username = _dbus_strdup (p->pw_name);
2021   info->homedir = _dbus_strdup (p->pw_dir);
2022
2023   if (info->username == NULL ||
2024       info->homedir == NULL)
2025     {
2026       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2027       return FALSE;
2028     }
2029
2030   return TRUE;
2031 }
2032
2033 static dbus_bool_t
2034 fill_user_info (DBusUserInfo       *info,
2035                 dbus_uid_t          uid,
2036                 const DBusString   *username,
2037                 DBusError          *error)
2038 {
2039   const char *username_c;
2040
2041   /* exactly one of username/uid provided */
2042   _dbus_assert (username != NULL || uid != DBUS_UID_UNSET);
2043   _dbus_assert (username == NULL || uid == DBUS_UID_UNSET);
2044
2045   info->uid = DBUS_UID_UNSET;
2046   info->primary_gid = DBUS_GID_UNSET;
2047   info->group_ids = NULL;
2048   info->n_group_ids = 0;
2049   info->username = NULL;
2050   info->homedir = NULL;
2051
2052   if (username != NULL)
2053     username_c = _dbus_string_get_const_data (username);
2054   else
2055     username_c = NULL;
2056
2057   /* For now assuming that the getpwnam() and getpwuid() flavors
2058    * are always symmetrical, if not we have to add more configure
2059    * checks
2060    */
2061
2062 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
2063   {
2064     struct passwd *p;
2065     int result;
2066     size_t buflen;
2067     char *buf;
2068     struct passwd p_str;
2069
2070     /* retrieve maximum needed size for buf */
2071     buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
2072
2073     /* sysconf actually returns a long, but everything else expects size_t,
2074      * so just recast here.
2075      * https://bugs.freedesktop.org/show_bug.cgi?id=17061
2076      */
2077     if ((long) buflen <= 0)
2078       buflen = 1024;
2079
2080     result = -1;
2081     while (1)
2082       {
2083         buf = dbus_malloc (buflen);
2084         if (buf == NULL)
2085           {
2086             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2087             return FALSE;
2088           }
2089
2090         p = NULL;
2091 #ifdef HAVE_POSIX_GETPWNAM_R
2092         if (uid != DBUS_UID_UNSET)
2093           result = getpwuid_r (uid, &p_str, buf, buflen,
2094                                &p);
2095         else
2096           result = getpwnam_r (username_c, &p_str, buf, buflen,
2097                                &p);
2098 #else
2099         if (uid != DBUS_UID_UNSET)
2100           p = getpwuid_r (uid, &p_str, buf, buflen);
2101         else
2102           p = getpwnam_r (username_c, &p_str, buf, buflen);
2103         result = 0;
2104 #endif /* !HAVE_POSIX_GETPWNAM_R */
2105         //Try a bigger buffer if ERANGE was returned
2106         if (result == ERANGE && buflen < 512 * 1024)
2107           {
2108             dbus_free (buf);
2109             buflen *= 2;
2110           }
2111         else
2112           {
2113             break;
2114           }
2115       }
2116     if (result == 0 && p == &p_str)
2117       {
2118         if (!fill_user_info_from_passwd (p, info, error))
2119           {
2120             dbus_free (buf);
2121             return FALSE;
2122           }
2123         dbus_free (buf);
2124       }
2125     else
2126       {
2127         dbus_set_error (error, _dbus_error_from_errno (errno),
2128                         "User \"%s\" unknown or no memory to allocate password entry\n",
2129                         username_c ? username_c : "???");
2130         _dbus_verbose ("User %s unknown\n", username_c ? username_c : "???");
2131         dbus_free (buf);
2132         return FALSE;
2133       }
2134   }
2135 #else /* ! HAVE_GETPWNAM_R */
2136   {
2137     /* I guess we're screwed on thread safety here */
2138     struct passwd *p;
2139
2140     if (uid != DBUS_UID_UNSET)
2141       p = getpwuid (uid);
2142     else
2143       p = getpwnam (username_c);
2144
2145     if (p != NULL)
2146       {
2147         if (!fill_user_info_from_passwd (p, info, error))
2148           {
2149             return FALSE;
2150           }
2151       }
2152     else
2153       {
2154         dbus_set_error (error, _dbus_error_from_errno (errno),
2155                         "User \"%s\" unknown or no memory to allocate password entry\n",
2156                         username_c ? username_c : "???");
2157         _dbus_verbose ("User %s unknown\n", username_c ? username_c : "???");
2158         return FALSE;
2159       }
2160   }
2161 #endif  /* ! HAVE_GETPWNAM_R */
2162
2163   /* Fill this in so we can use it to get groups */
2164   username_c = info->username;
2165
2166 #ifdef HAVE_GETGROUPLIST
2167   {
2168     gid_t *buf;
2169     int buf_count;
2170     int i;
2171     int initial_buf_count;
2172
2173     initial_buf_count = 17;
2174     buf_count = initial_buf_count;
2175     buf = dbus_new (gid_t, buf_count);
2176     if (buf == NULL)
2177       {
2178         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2179         goto failed;
2180       }
2181
2182     if (getgrouplist (username_c,
2183                       info->primary_gid,
2184                       buf, &buf_count) < 0)
2185       {
2186         gid_t *new;
2187         /* Presumed cause of negative return code: buf has insufficient
2188            entries to hold the entire group list. The Linux behavior in this
2189            case is to pass back the actual number of groups in buf_count, but
2190            on Mac OS X 10.5, buf_count is unhelpfully left alone.
2191            So as a hack, try to help out a bit by guessing a larger
2192            number of groups, within reason.. might still fail, of course,
2193            but we can at least print a more informative message.  I looked up
2194            the "right way" to do this by downloading Apple's own source code
2195            for the "id" command, and it turns out that they use an
2196            undocumented library function getgrouplist_2 (!) which is not
2197            declared in any header in /usr/include (!!). That did not seem
2198            like the way to go here.
2199         */
2200         if (buf_count == initial_buf_count)
2201           {
2202             buf_count *= 16; /* Retry with an arbitrarily scaled-up array */
2203           }
2204         new = dbus_realloc (buf, buf_count * sizeof (buf[0]));
2205         if (new == NULL)
2206           {
2207             dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2208             dbus_free (buf);
2209             goto failed;
2210           }
2211
2212         buf = new;
2213
2214         errno = 0;
2215         if (getgrouplist (username_c, info->primary_gid, buf, &buf_count) < 0)
2216           {
2217             if (errno == 0)
2218               {
2219                 _dbus_warn ("It appears that username \"%s\" is in more than %d groups.\nProceeding with just the first %d groups.",
2220                             username_c, buf_count, buf_count);
2221               }
2222             else
2223               {
2224                 dbus_set_error (error,
2225                                 _dbus_error_from_errno (errno),
2226                                 "Failed to get groups for username \"%s\" primary GID "
2227                                 DBUS_GID_FORMAT ": %s\n",
2228                                 username_c, info->primary_gid,
2229                                 _dbus_strerror (errno));
2230                 dbus_free (buf);
2231                 goto failed;
2232               }
2233           }
2234       }
2235
2236     info->group_ids = dbus_new (dbus_gid_t, buf_count);
2237     if (info->group_ids == NULL)
2238       {
2239         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2240         dbus_free (buf);
2241         goto failed;
2242       }
2243
2244     for (i = 0; i < buf_count; ++i)
2245       info->group_ids[i] = buf[i];
2246
2247     info->n_group_ids = buf_count;
2248
2249     dbus_free (buf);
2250   }
2251 #else  /* HAVE_GETGROUPLIST */
2252   {
2253     /* We just get the one group ID */
2254     info->group_ids = dbus_new (dbus_gid_t, 1);
2255     if (info->group_ids == NULL)
2256       {
2257         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2258         goto failed;
2259       }
2260
2261     info->n_group_ids = 1;
2262
2263     (info->group_ids)[0] = info->primary_gid;
2264   }
2265 #endif /* HAVE_GETGROUPLIST */
2266
2267   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2268
2269   return TRUE;
2270
2271  failed:
2272   _DBUS_ASSERT_ERROR_IS_SET (error);
2273   return FALSE;
2274 }
2275
2276 /**
2277  * Gets user info for the given username.
2278  *
2279  * @param info user info object to initialize
2280  * @param username the username
2281  * @param error error return
2282  * @returns #TRUE on success
2283  */
2284 dbus_bool_t
2285 _dbus_user_info_fill (DBusUserInfo     *info,
2286                       const DBusString *username,
2287                       DBusError        *error)
2288 {
2289   return fill_user_info (info, DBUS_UID_UNSET,
2290                          username, error);
2291 }
2292
2293 /**
2294  * Gets user info for the given user ID.
2295  *
2296  * @param info user info object to initialize
2297  * @param uid the user ID
2298  * @param error error return
2299  * @returns #TRUE on success
2300  */
2301 dbus_bool_t
2302 _dbus_user_info_fill_uid (DBusUserInfo *info,
2303                           dbus_uid_t    uid,
2304                           DBusError    *error)
2305 {
2306   return fill_user_info (info, uid,
2307                          NULL, error);
2308 }
2309
2310 /**
2311  * Adds the credentials of the current process to the
2312  * passed-in credentials object.
2313  *
2314  * @param credentials credentials to add to
2315  * @returns #FALSE if no memory; does not properly roll back on failure, so only some credentials may have been added
2316  */
2317 dbus_bool_t
2318 _dbus_credentials_add_from_current_process (DBusCredentials *credentials)
2319 {
2320   /* The POSIX spec certainly doesn't promise this, but
2321    * we need these assertions to fail as soon as we're wrong about
2322    * it so we can do the porting fixups
2323    */
2324   _dbus_assert (sizeof (pid_t) <= sizeof (dbus_pid_t));
2325   _dbus_assert (sizeof (uid_t) <= sizeof (dbus_uid_t));
2326   _dbus_assert (sizeof (gid_t) <= sizeof (dbus_gid_t));
2327
2328   if (!_dbus_credentials_add_pid(credentials, _dbus_getpid()))
2329     return FALSE;
2330   if (!_dbus_credentials_add_unix_uid(credentials, _dbus_geteuid()))
2331     return FALSE;
2332
2333   return TRUE;
2334 }
2335
2336 /**
2337  * Append to the string the identity we would like to have when we
2338  * authenticate, on UNIX this is the current process UID and on
2339  * Windows something else, probably a Windows SID string.  No escaping
2340  * is required, that is done in dbus-auth.c. The username here
2341  * need not be anything human-readable, it can be the machine-readable
2342  * form i.e. a user id.
2343  *
2344  * @param str the string to append to
2345  * @returns #FALSE on no memory
2346  */
2347 dbus_bool_t
2348 _dbus_append_user_from_current_process (DBusString *str)
2349 {
2350   return _dbus_string_append_uint (str,
2351                                    _dbus_geteuid ());
2352 }
2353
2354 /**
2355  * Gets our process ID
2356  * @returns process ID
2357  */
2358 dbus_pid_t
2359 _dbus_getpid (void)
2360 {
2361   return getpid ();
2362 }
2363
2364 /** Gets our UID
2365  * @returns process UID
2366  */
2367 dbus_uid_t
2368 _dbus_getuid (void)
2369 {
2370   return getuid ();
2371 }
2372
2373 /** Gets our effective UID
2374  * @returns process effective UID
2375  */
2376 dbus_uid_t
2377 _dbus_geteuid (void)
2378 {
2379   return geteuid ();
2380 }
2381
2382 /**
2383  * The only reason this is separate from _dbus_getpid() is to allow it
2384  * on Windows for logging but not for other purposes.
2385  *
2386  * @returns process ID to put in log messages
2387  */
2388 unsigned long
2389 _dbus_pid_for_log (void)
2390 {
2391   return getpid ();
2392 }
2393
2394 /**
2395  * Gets a UID from a UID string.
2396  *
2397  * @param uid_str the UID in string form
2398  * @param uid UID to fill in
2399  * @returns #TRUE if successfully filled in UID
2400  */
2401 dbus_bool_t
2402 _dbus_parse_uid (const DBusString      *uid_str,
2403                  dbus_uid_t            *uid)
2404 {
2405   int end;
2406   long val;
2407
2408   if (_dbus_string_get_length (uid_str) == 0)
2409     {
2410       _dbus_verbose ("UID string was zero length\n");
2411       return FALSE;
2412     }
2413
2414   val = -1;
2415   end = 0;
2416   if (!_dbus_string_parse_int (uid_str, 0, &val,
2417                                &end))
2418     {
2419       _dbus_verbose ("could not parse string as a UID\n");
2420       return FALSE;
2421     }
2422
2423   if (end != _dbus_string_get_length (uid_str))
2424     {
2425       _dbus_verbose ("string contained trailing stuff after UID\n");
2426       return FALSE;
2427     }
2428
2429   *uid = val;
2430
2431   return TRUE;
2432 }
2433
2434 #if !DBUS_USE_SYNC
2435 /* To be thread-safe by default on platforms that don't necessarily have
2436  * atomic operations (notably Debian armel, which is armv4t), we must
2437  * use a mutex that can be initialized statically, like this.
2438  * GLib >= 2.32 uses a similar system.
2439  */
2440 static pthread_mutex_t atomic_mutex = PTHREAD_MUTEX_INITIALIZER;
2441 #endif
2442
2443 /**
2444  * Atomically increments an integer
2445  *
2446  * @param atomic pointer to the integer to increment
2447  * @returns the value before incrementing
2448  */
2449 dbus_int32_t
2450 _dbus_atomic_inc (DBusAtomic *atomic)
2451 {
2452 #if DBUS_USE_SYNC
2453   return __sync_add_and_fetch(&atomic->value, 1)-1;
2454 #else
2455   dbus_int32_t res;
2456
2457   pthread_mutex_lock (&atomic_mutex);
2458   res = atomic->value;
2459   atomic->value += 1;
2460   pthread_mutex_unlock (&atomic_mutex);
2461
2462   return res;
2463 #endif
2464 }
2465
2466 /**
2467  * Atomically decrement an integer
2468  *
2469  * @param atomic pointer to the integer to decrement
2470  * @returns the value before decrementing
2471  */
2472 dbus_int32_t
2473 _dbus_atomic_dec (DBusAtomic *atomic)
2474 {
2475 #if DBUS_USE_SYNC
2476   return __sync_sub_and_fetch(&atomic->value, 1)+1;
2477 #else
2478   dbus_int32_t res;
2479
2480   pthread_mutex_lock (&atomic_mutex);
2481   res = atomic->value;
2482   atomic->value -= 1;
2483   pthread_mutex_unlock (&atomic_mutex);
2484
2485   return res;
2486 #endif
2487 }
2488
2489 /**
2490  * Atomically get the value of an integer. It may change at any time
2491  * thereafter, so this is mostly only useful for assertions.
2492  *
2493  * @param atomic pointer to the integer to get
2494  * @returns the value at this moment
2495  */
2496 dbus_int32_t
2497 _dbus_atomic_get (DBusAtomic *atomic)
2498 {
2499 #if DBUS_USE_SYNC
2500   __sync_synchronize ();
2501   return atomic->value;
2502 #else
2503   dbus_int32_t res;
2504
2505   pthread_mutex_lock (&atomic_mutex);
2506   res = atomic->value;
2507   pthread_mutex_unlock (&atomic_mutex);
2508
2509   return res;
2510 #endif
2511 }
2512
2513 /**
2514  * Wrapper for poll().
2515  *
2516  * @param fds the file descriptors to poll
2517  * @param n_fds number of descriptors in the array
2518  * @param timeout_milliseconds timeout or -1 for infinite
2519  * @returns numbers of fds with revents, or <0 on error
2520  */
2521 int
2522 _dbus_poll (DBusPollFD *fds,
2523             int         n_fds,
2524             int         timeout_milliseconds)
2525 {
2526 #if defined(HAVE_POLL) && !defined(BROKEN_POLL)
2527   /* This big thing is a constant expression and should get optimized
2528    * out of existence. So it's more robust than a configure check at
2529    * no cost.
2530    */
2531   if (_DBUS_POLLIN == POLLIN &&
2532       _DBUS_POLLPRI == POLLPRI &&
2533       _DBUS_POLLOUT == POLLOUT &&
2534       _DBUS_POLLERR == POLLERR &&
2535       _DBUS_POLLHUP == POLLHUP &&
2536       _DBUS_POLLNVAL == POLLNVAL &&
2537       sizeof (DBusPollFD) == sizeof (struct pollfd) &&
2538       _DBUS_STRUCT_OFFSET (DBusPollFD, fd) ==
2539       _DBUS_STRUCT_OFFSET (struct pollfd, fd) &&
2540       _DBUS_STRUCT_OFFSET (DBusPollFD, events) ==
2541       _DBUS_STRUCT_OFFSET (struct pollfd, events) &&
2542       _DBUS_STRUCT_OFFSET (DBusPollFD, revents) ==
2543       _DBUS_STRUCT_OFFSET (struct pollfd, revents))
2544     {
2545       return poll ((struct pollfd*) fds,
2546                    n_fds,
2547                    timeout_milliseconds);
2548     }
2549   else
2550     {
2551       /* We have to convert the DBusPollFD to an array of
2552        * struct pollfd, poll, and convert back.
2553        */
2554       _dbus_warn ("didn't implement poll() properly for this system yet\n");
2555       return -1;
2556     }
2557 #else /* ! HAVE_POLL */
2558
2559   fd_set read_set, write_set, err_set;
2560   int max_fd = 0;
2561   int i;
2562   struct timeval tv;
2563   int ready;
2564
2565   FD_ZERO (&read_set);
2566   FD_ZERO (&write_set);
2567   FD_ZERO (&err_set);
2568
2569   for (i = 0; i < n_fds; i++)
2570     {
2571       DBusPollFD *fdp = &fds[i];
2572
2573       if (fdp->events & _DBUS_POLLIN)
2574         FD_SET (fdp->fd, &read_set);
2575
2576       if (fdp->events & _DBUS_POLLOUT)
2577         FD_SET (fdp->fd, &write_set);
2578
2579       FD_SET (fdp->fd, &err_set);
2580
2581       max_fd = MAX (max_fd, fdp->fd);
2582     }
2583
2584   tv.tv_sec = timeout_milliseconds / 1000;
2585   tv.tv_usec = (timeout_milliseconds % 1000) * 1000;
2586
2587   ready = select (max_fd + 1, &read_set, &write_set, &err_set,
2588                   timeout_milliseconds < 0 ? NULL : &tv);
2589
2590   if (ready > 0)
2591     {
2592       for (i = 0; i < n_fds; i++)
2593         {
2594           DBusPollFD *fdp = &fds[i];
2595
2596           fdp->revents = 0;
2597
2598           if (FD_ISSET (fdp->fd, &read_set))
2599             fdp->revents |= _DBUS_POLLIN;
2600
2601           if (FD_ISSET (fdp->fd, &write_set))
2602             fdp->revents |= _DBUS_POLLOUT;
2603
2604           if (FD_ISSET (fdp->fd, &err_set))
2605             fdp->revents |= _DBUS_POLLERR;
2606         }
2607     }
2608
2609   return ready;
2610 #endif
2611 }
2612
2613 /**
2614  * Get current time, as in gettimeofday(). Use the monotonic clock if
2615  * available, to avoid problems when the system time changes.
2616  *
2617  * @param tv_sec return location for number of seconds
2618  * @param tv_usec return location for number of microseconds
2619  */
2620 void
2621 _dbus_get_monotonic_time (long *tv_sec,
2622                           long *tv_usec)
2623 {
2624 #ifdef HAVE_MONOTONIC_CLOCK
2625   struct timespec ts;
2626   clock_gettime (CLOCK_MONOTONIC, &ts);
2627
2628   if (tv_sec)
2629     *tv_sec = ts.tv_sec;
2630   if (tv_usec)
2631     *tv_usec = ts.tv_nsec / 1000;
2632 #else
2633   struct timeval t;
2634
2635   gettimeofday (&t, NULL);
2636
2637   if (tv_sec)
2638     *tv_sec = t.tv_sec;
2639   if (tv_usec)
2640     *tv_usec = t.tv_usec;
2641 #endif
2642 }
2643
2644 /**
2645  * Get current time, as in gettimeofday(). Never uses the monotonic
2646  * clock.
2647  *
2648  * @param tv_sec return location for number of seconds
2649  * @param tv_usec return location for number of microseconds
2650  */
2651 void
2652 _dbus_get_real_time (long *tv_sec,
2653                      long *tv_usec)
2654 {
2655   struct timeval t;
2656
2657   gettimeofday (&t, NULL);
2658
2659   if (tv_sec)
2660     *tv_sec = t.tv_sec;
2661   if (tv_usec)
2662     *tv_usec = t.tv_usec;
2663 }
2664
2665 /**
2666  * Creates a directory; succeeds if the directory
2667  * is created or already existed.
2668  *
2669  * @param filename directory filename
2670  * @param error initialized error object
2671  * @returns #TRUE on success
2672  */
2673 dbus_bool_t
2674 _dbus_create_directory (const DBusString *filename,
2675                         DBusError        *error)
2676 {
2677   const char *filename_c;
2678
2679   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2680
2681   filename_c = _dbus_string_get_const_data (filename);
2682
2683   if (mkdir (filename_c, 0700) < 0)
2684     {
2685       if (errno == EEXIST)
2686         return TRUE;
2687
2688       dbus_set_error (error, DBUS_ERROR_FAILED,
2689                       "Failed to create directory %s: %s\n",
2690                       filename_c, _dbus_strerror (errno));
2691       return FALSE;
2692     }
2693   else
2694     return TRUE;
2695 }
2696
2697 /**
2698  * Appends the given filename to the given directory.
2699  *
2700  * @todo it might be cute to collapse multiple '/' such as "foo//"
2701  * concat "//bar"
2702  *
2703  * @param dir the directory name
2704  * @param next_component the filename
2705  * @returns #TRUE on success
2706  */
2707 dbus_bool_t
2708 _dbus_concat_dir_and_file (DBusString       *dir,
2709                            const DBusString *next_component)
2710 {
2711   dbus_bool_t dir_ends_in_slash;
2712   dbus_bool_t file_starts_with_slash;
2713
2714   if (_dbus_string_get_length (dir) == 0 ||
2715       _dbus_string_get_length (next_component) == 0)
2716     return TRUE;
2717
2718   dir_ends_in_slash = '/' == _dbus_string_get_byte (dir,
2719                                                     _dbus_string_get_length (dir) - 1);
2720
2721   file_starts_with_slash = '/' == _dbus_string_get_byte (next_component, 0);
2722
2723   if (dir_ends_in_slash && file_starts_with_slash)
2724     {
2725       _dbus_string_shorten (dir, 1);
2726     }
2727   else if (!(dir_ends_in_slash || file_starts_with_slash))
2728     {
2729       if (!_dbus_string_append_byte (dir, '/'))
2730         return FALSE;
2731     }
2732
2733   return _dbus_string_copy (next_component, 0, dir,
2734                             _dbus_string_get_length (dir));
2735 }
2736
2737 /** nanoseconds in a second */
2738 #define NANOSECONDS_PER_SECOND       1000000000
2739 /** microseconds in a second */
2740 #define MICROSECONDS_PER_SECOND      1000000
2741 /** milliseconds in a second */
2742 #define MILLISECONDS_PER_SECOND      1000
2743 /** nanoseconds in a millisecond */
2744 #define NANOSECONDS_PER_MILLISECOND  1000000
2745 /** microseconds in a millisecond */
2746 #define MICROSECONDS_PER_MILLISECOND 1000
2747
2748 /**
2749  * Sleeps the given number of milliseconds.
2750  * @param milliseconds number of milliseconds
2751  */
2752 void
2753 _dbus_sleep_milliseconds (int milliseconds)
2754 {
2755 #ifdef HAVE_NANOSLEEP
2756   struct timespec req;
2757   struct timespec rem;
2758
2759   req.tv_sec = milliseconds / MILLISECONDS_PER_SECOND;
2760   req.tv_nsec = (milliseconds % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND;
2761   rem.tv_sec = 0;
2762   rem.tv_nsec = 0;
2763
2764   while (nanosleep (&req, &rem) < 0 && errno == EINTR)
2765     req = rem;
2766 #elif defined (HAVE_USLEEP)
2767   usleep (milliseconds * MICROSECONDS_PER_MILLISECOND);
2768 #else /* ! HAVE_USLEEP */
2769   sleep (MAX (milliseconds / 1000, 1));
2770 #endif
2771 }
2772
2773 static dbus_bool_t
2774 _dbus_generate_pseudorandom_bytes (DBusString *str,
2775                                    int         n_bytes)
2776 {
2777   int old_len;
2778   char *p;
2779
2780   old_len = _dbus_string_get_length (str);
2781
2782   if (!_dbus_string_lengthen (str, n_bytes))
2783     return FALSE;
2784
2785   p = _dbus_string_get_data_len (str, old_len, n_bytes);
2786
2787   _dbus_generate_pseudorandom_bytes_buffer (p, n_bytes);
2788
2789   return TRUE;
2790 }
2791
2792 /**
2793  * Generates the given number of random bytes,
2794  * using the best mechanism we can come up with.
2795  *
2796  * @param str the string
2797  * @param n_bytes the number of random bytes to append to string
2798  * @returns #TRUE on success, #FALSE if no memory
2799  */
2800 dbus_bool_t
2801 _dbus_generate_random_bytes (DBusString *str,
2802                              int         n_bytes)
2803 {
2804   int old_len;
2805   int fd;
2806
2807   /* FALSE return means "no memory", if it could
2808    * mean something else then we'd need to return
2809    * a DBusError. So we always fall back to pseudorandom
2810    * if the I/O fails.
2811    */
2812
2813   old_len = _dbus_string_get_length (str);
2814   fd = -1;
2815
2816   /* note, urandom on linux will fall back to pseudorandom */
2817   fd = open ("/dev/urandom", O_RDONLY);
2818   if (fd < 0)
2819     return _dbus_generate_pseudorandom_bytes (str, n_bytes);
2820
2821   _dbus_verbose ("/dev/urandom fd %d opened\n", fd);
2822
2823   if (_dbus_read (fd, str, n_bytes) != n_bytes)
2824     {
2825       _dbus_close (fd, NULL);
2826       _dbus_string_set_length (str, old_len);
2827       return _dbus_generate_pseudorandom_bytes (str, n_bytes);
2828     }
2829
2830   _dbus_verbose ("Read %d bytes from /dev/urandom\n",
2831                  n_bytes);
2832
2833   _dbus_close (fd, NULL);
2834
2835   return TRUE;
2836 }
2837
2838 /**
2839  * Exit the process, returning the given value.
2840  *
2841  * @param code the exit code
2842  */
2843 void
2844 _dbus_exit (int code)
2845 {
2846   _exit (code);
2847 }
2848
2849 /**
2850  * A wrapper around strerror() because some platforms
2851  * may be lame and not have strerror(). Also, never
2852  * returns NULL.
2853  *
2854  * @param error_number errno.
2855  * @returns error description.
2856  */
2857 const char*
2858 _dbus_strerror (int error_number)
2859 {
2860   const char *msg;
2861
2862   msg = strerror (error_number);
2863   if (msg == NULL)
2864     msg = "unknown";
2865
2866   return msg;
2867 }
2868
2869 /**
2870  * signal (SIGPIPE, SIG_IGN);
2871  */
2872 void
2873 _dbus_disable_sigpipe (void)
2874 {
2875   signal (SIGPIPE, SIG_IGN);
2876 }
2877
2878 /**
2879  * Sets the file descriptor to be close
2880  * on exec. Should be called for all file
2881  * descriptors in D-Bus code.
2882  *
2883  * @param fd the file descriptor
2884  */
2885 void
2886 _dbus_fd_set_close_on_exec (intptr_t fd)
2887 {
2888   int val;
2889
2890   val = fcntl (fd, F_GETFD, 0);
2891
2892   if (val < 0)
2893     return;
2894
2895   val |= FD_CLOEXEC;
2896
2897   fcntl (fd, F_SETFD, val);
2898 }
2899
2900 /**
2901  * Closes a file descriptor.
2902  *
2903  * @param fd the file descriptor
2904  * @param error error object
2905  * @returns #FALSE if error set
2906  */
2907 dbus_bool_t
2908 _dbus_close (int        fd,
2909              DBusError *error)
2910 {
2911   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2912
2913  again:
2914   if (close (fd) < 0)
2915     {
2916       if (errno == EINTR)
2917         goto again;
2918
2919       dbus_set_error (error, _dbus_error_from_errno (errno),
2920                       "Could not close fd %d", fd);
2921       return FALSE;
2922     }
2923
2924   return TRUE;
2925 }
2926
2927 /**
2928  * Duplicates a file descriptor. Makes sure the fd returned is >= 3
2929  * (i.e. avoids stdin/stdout/stderr). Sets O_CLOEXEC.
2930  *
2931  * @param fd the file descriptor to duplicate
2932  * @param error address of error location.
2933  * @returns duplicated file descriptor
2934  * */
2935 int
2936 _dbus_dup(int        fd,
2937           DBusError *error)
2938 {
2939   int new_fd;
2940
2941 #ifdef F_DUPFD_CLOEXEC
2942   dbus_bool_t cloexec_done;
2943
2944   new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
2945   cloexec_done = new_fd >= 0;
2946
2947   if (new_fd < 0 && errno == EINVAL)
2948 #endif
2949     {
2950       new_fd = fcntl(fd, F_DUPFD, 3);
2951     }
2952
2953   if (new_fd < 0) {
2954
2955     dbus_set_error (error, _dbus_error_from_errno (errno),
2956                     "Could not duplicate fd %d", fd);
2957     return -1;
2958   }
2959
2960 #ifdef F_DUPFD_CLOEXEC
2961   if (!cloexec_done)
2962 #endif
2963     {
2964       _dbus_fd_set_close_on_exec(new_fd);
2965     }
2966
2967   return new_fd;
2968 }
2969
2970 /**
2971  * Sets a file descriptor to be nonblocking.
2972  *
2973  * @param fd the file descriptor.
2974  * @param error address of error location.
2975  * @returns #TRUE on success.
2976  */
2977 dbus_bool_t
2978 _dbus_set_fd_nonblocking (int             fd,
2979                           DBusError      *error)
2980 {
2981   int val;
2982
2983   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2984
2985   val = fcntl (fd, F_GETFL, 0);
2986   if (val < 0)
2987     {
2988       dbus_set_error (error, _dbus_error_from_errno (errno),
2989                       "Failed to get flags from file descriptor %d: %s",
2990                       fd, _dbus_strerror (errno));
2991       _dbus_verbose ("Failed to get flags for fd %d: %s\n", fd,
2992                      _dbus_strerror (errno));
2993       return FALSE;
2994     }
2995
2996   if (fcntl (fd, F_SETFL, val | O_NONBLOCK) < 0)
2997     {
2998       dbus_set_error (error, _dbus_error_from_errno (errno),
2999                       "Failed to set nonblocking flag of file descriptor %d: %s",
3000                       fd, _dbus_strerror (errno));
3001       _dbus_verbose ("Failed to set fd %d nonblocking: %s\n",
3002                      fd, _dbus_strerror (errno));
3003
3004       return FALSE;
3005     }
3006
3007   return TRUE;
3008 }
3009
3010 /**
3011  * On GNU libc systems, print a crude backtrace to stderr.  On other
3012  * systems, print "no backtrace support" and block for possible gdb
3013  * attachment if an appropriate environment variable is set.
3014  */
3015 void
3016 _dbus_print_backtrace (void)
3017 {
3018 #if defined (HAVE_BACKTRACE) && defined (DBUS_BUILT_R_DYNAMIC)
3019   void *bt[500];
3020   int bt_size;
3021   int i;
3022   char **syms;
3023
3024   bt_size = backtrace (bt, 500);
3025
3026   syms = backtrace_symbols (bt, bt_size);
3027
3028   i = 0;
3029   while (i < bt_size)
3030     {
3031       /* don't use dbus_warn since it can _dbus_abort() */
3032       fprintf (stderr, "  %s\n", syms[i]);
3033       ++i;
3034     }
3035   fflush (stderr);
3036
3037   free (syms);
3038 #elif defined (HAVE_BACKTRACE) && ! defined (DBUS_BUILT_R_DYNAMIC)
3039   fprintf (stderr, "  D-Bus not built with -rdynamic so unable to print a backtrace\n");
3040 #else
3041   fprintf (stderr, "  D-Bus not compiled with backtrace support so unable to print a backtrace\n");
3042 #endif
3043 }
3044
3045 /**
3046  * Creates a full-duplex pipe (as in socketpair()).
3047  * Sets both ends of the pipe nonblocking.
3048  *
3049  * Marks both file descriptors as close-on-exec
3050  *
3051  * @param fd1 return location for one end
3052  * @param fd2 return location for the other end
3053  * @param blocking #TRUE if pipe should be blocking
3054  * @param error error return
3055  * @returns #FALSE on failure (if error is set)
3056  */
3057 dbus_bool_t
3058 _dbus_full_duplex_pipe (int        *fd1,
3059                         int        *fd2,
3060                         dbus_bool_t blocking,
3061                         DBusError  *error)
3062 {
3063 #ifdef HAVE_SOCKETPAIR
3064   int fds[2];
3065   int retval;
3066
3067 #ifdef SOCK_CLOEXEC
3068   dbus_bool_t cloexec_done;
3069
3070   retval = socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, fds);
3071   cloexec_done = retval >= 0;
3072
3073   if (retval < 0 && errno == EINVAL)
3074 #endif
3075     {
3076       retval = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
3077     }
3078
3079   if (retval < 0)
3080     {
3081       dbus_set_error (error, _dbus_error_from_errno (errno),
3082                       "Could not create full-duplex pipe");
3083       return FALSE;
3084     }
3085
3086   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3087
3088 #ifdef SOCK_CLOEXEC
3089   if (!cloexec_done)
3090 #endif
3091     {
3092       _dbus_fd_set_close_on_exec (fds[0]);
3093       _dbus_fd_set_close_on_exec (fds[1]);
3094     }
3095
3096   if (!blocking &&
3097       (!_dbus_set_fd_nonblocking (fds[0], NULL) ||
3098        !_dbus_set_fd_nonblocking (fds[1], NULL)))
3099     {
3100       dbus_set_error (error, _dbus_error_from_errno (errno),
3101                       "Could not set full-duplex pipe nonblocking");
3102
3103       _dbus_close (fds[0], NULL);
3104       _dbus_close (fds[1], NULL);
3105
3106       return FALSE;
3107     }
3108
3109   *fd1 = fds[0];
3110   *fd2 = fds[1];
3111
3112   _dbus_verbose ("full-duplex pipe %d <-> %d\n",
3113                  *fd1, *fd2);
3114
3115   return TRUE;
3116 #else
3117   _dbus_warn ("_dbus_full_duplex_pipe() not implemented on this OS\n");
3118   dbus_set_error (error, DBUS_ERROR_FAILED,
3119                   "_dbus_full_duplex_pipe() not implemented on this OS");
3120   return FALSE;
3121 #endif
3122 }
3123
3124 /**
3125  * Measure the length of the given format string and arguments,
3126  * not including the terminating nul.
3127  *
3128  * @param format a printf-style format string
3129  * @param args arguments for the format string
3130  * @returns length of the given format string and args, or -1 if no memory
3131  */
3132 int
3133 _dbus_printf_string_upper_bound (const char *format,
3134                                  va_list     args)
3135 {
3136   char static_buf[1024];
3137   int bufsize = sizeof (static_buf);
3138   int len;
3139   va_list args_copy;
3140
3141   DBUS_VA_COPY (args_copy, args);
3142   len = vsnprintf (static_buf, bufsize, format, args_copy);
3143   va_end (args_copy);
3144
3145   /* If vsnprintf() returned non-negative, then either the string fits in
3146    * static_buf, or this OS has the POSIX and C99 behaviour where vsnprintf
3147    * returns the number of characters that were needed, or this OS returns the
3148    * truncated length.
3149    *
3150    * We ignore the possibility that snprintf might just ignore the length and
3151    * overrun the buffer (64-bit Solaris 7), because that's pathological.
3152    * If your libc is really that bad, come back when you have a better one. */
3153   if (len == bufsize)
3154     {
3155       /* This could be the truncated length (Tru64 and IRIX have this bug),
3156        * or the real length could be coincidentally the same. Which is it?
3157        * If vsnprintf returns the truncated length, we'll go to the slow
3158        * path. */
3159       DBUS_VA_COPY (args_copy, args);
3160
3161       if (vsnprintf (static_buf, 1, format, args_copy) == 1)
3162         len = -1;
3163
3164       va_end (args_copy);
3165     }
3166
3167   /* If vsnprintf() returned negative, we have to do more work.
3168    * HP-UX returns negative. */
3169   while (len < 0)
3170     {
3171       char *buf;
3172
3173       bufsize *= 2;
3174
3175       buf = dbus_malloc (bufsize);
3176
3177       if (buf == NULL)
3178         return -1;
3179
3180       DBUS_VA_COPY (args_copy, args);
3181       len = vsnprintf (buf, bufsize, format, args_copy);
3182       va_end (args_copy);
3183
3184       dbus_free (buf);
3185
3186       /* If the reported length is exactly the buffer size, round up to the
3187        * next size, in case vsnprintf has been returning the truncated
3188        * length */
3189       if (len == bufsize)
3190         len = -1;
3191     }
3192
3193   return len;
3194 }
3195
3196 /**
3197  * Gets the temporary files directory by inspecting the environment variables
3198  * TMPDIR, TMP, and TEMP in that order. If none of those are set "/tmp" is returned
3199  *
3200  * @returns location of temp directory, or #NULL if no memory for locking
3201  */
3202 const char*
3203 _dbus_get_tmpdir(void)
3204 {
3205   /* Protected by _DBUS_LOCK_sysdeps */
3206   static const char* tmpdir = NULL;
3207
3208   if (!_DBUS_LOCK (sysdeps))
3209     return NULL;
3210
3211   if (tmpdir == NULL)
3212     {
3213       /* TMPDIR is what glibc uses, then
3214        * glibc falls back to the P_tmpdir macro which
3215        * just expands to "/tmp"
3216        */
3217       if (tmpdir == NULL)
3218         tmpdir = getenv("TMPDIR");
3219
3220       /* These two env variables are probably
3221        * broken, but maybe some OS uses them?
3222        */
3223       if (tmpdir == NULL)
3224         tmpdir = getenv("TMP");
3225       if (tmpdir == NULL)
3226         tmpdir = getenv("TEMP");
3227
3228       /* And this is the sane fallback. */
3229       if (tmpdir == NULL)
3230         tmpdir = "/tmp";
3231     }
3232
3233   _DBUS_UNLOCK (sysdeps);
3234
3235   _dbus_assert(tmpdir != NULL);
3236
3237   return tmpdir;
3238 }
3239
3240 #if defined(DBUS_ENABLE_X11_AUTOLAUNCH) || defined(DBUS_ENABLE_LAUNCHD)
3241 /**
3242  * Execute a subprocess, returning up to 1024 bytes of output
3243  * into @p result.
3244  *
3245  * If successful, returns #TRUE and appends the output to @p
3246  * result. If a failure happens, returns #FALSE and
3247  * sets an error in @p error.
3248  *
3249  * @note It's not an error if the subprocess terminates normally
3250  * without writing any data to stdout. Verify the @p result length
3251  * before and after this function call to cover this case.
3252  *
3253  * @param progname initial path to exec (may or may not be absolute)
3254  * @param path_fallback if %TRUE, search PATH for executable
3255  * @param argv NULL-terminated list of arguments
3256  * @param result a DBusString where the output can be append
3257  * @param error a DBusError to store the error in case of failure
3258  * @returns #TRUE on success, #FALSE if an error happened
3259  */
3260 static dbus_bool_t
3261 _read_subprocess_line_argv (const char *progpath,
3262                             dbus_bool_t path_fallback,
3263                             char       * const *argv,
3264                             DBusString *result,
3265                             DBusError  *error)
3266 {
3267   int result_pipe[2] = { -1, -1 };
3268   int errors_pipe[2] = { -1, -1 };
3269   pid_t pid;
3270   int ret;
3271   int status;
3272   int orig_len;
3273
3274   dbus_bool_t retval;
3275   sigset_t new_set, old_set;
3276
3277   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3278   retval = FALSE;
3279
3280   /* We need to block any existing handlers for SIGCHLD temporarily; they
3281    * will cause waitpid() below to fail.
3282    * https://bugs.freedesktop.org/show_bug.cgi?id=21347
3283    */
3284   sigemptyset (&new_set);
3285   sigaddset (&new_set, SIGCHLD);
3286   sigprocmask (SIG_BLOCK, &new_set, &old_set);
3287
3288   orig_len = _dbus_string_get_length (result);
3289
3290 #define READ_END        0
3291 #define WRITE_END       1
3292   if (pipe (result_pipe) < 0)
3293     {
3294       dbus_set_error (error, _dbus_error_from_errno (errno),
3295                       "Failed to create a pipe to call %s: %s",
3296                       progpath, _dbus_strerror (errno));
3297       _dbus_verbose ("Failed to create a pipe to call %s: %s\n",
3298                      progpath, _dbus_strerror (errno));
3299       goto out;
3300     }
3301   if (pipe (errors_pipe) < 0)
3302     {
3303       dbus_set_error (error, _dbus_error_from_errno (errno),
3304                       "Failed to create a pipe to call %s: %s",
3305                       progpath, _dbus_strerror (errno));
3306       _dbus_verbose ("Failed to create a pipe to call %s: %s\n",
3307                      progpath, _dbus_strerror (errno));
3308       goto out;
3309     }
3310
3311   pid = fork ();
3312   if (pid < 0)
3313     {
3314       dbus_set_error (error, _dbus_error_from_errno (errno),
3315                       "Failed to fork() to call %s: %s",
3316                       progpath, _dbus_strerror (errno));
3317       _dbus_verbose ("Failed to fork() to call %s: %s\n",
3318                      progpath, _dbus_strerror (errno));
3319       goto out;
3320     }
3321
3322   if (pid == 0)
3323     {
3324       /* child process */
3325       int fd;
3326
3327       fd = open ("/dev/null", O_RDWR);
3328       if (fd == -1)
3329         /* huh?! can't open /dev/null? */
3330         _exit (1);
3331
3332       _dbus_verbose ("/dev/null fd %d opened\n", fd);
3333
3334       /* set-up stdXXX */
3335       close (result_pipe[READ_END]);
3336       close (errors_pipe[READ_END]);
3337
3338       if (dup2 (fd, 0) == -1) /* setup stdin */
3339         _exit (1);
3340       if (dup2 (result_pipe[WRITE_END], 1) == -1) /* setup stdout */
3341         _exit (1);
3342       if (dup2 (errors_pipe[WRITE_END], 2) == -1) /* setup stderr */
3343         _exit (1);
3344
3345       _dbus_close_all ();
3346
3347       sigprocmask (SIG_SETMASK, &old_set, NULL);
3348
3349       /* If it looks fully-qualified, try execv first */
3350       if (progpath[0] == '/')
3351         {
3352           execv (progpath, argv);
3353           /* Ok, that failed.  Now if path_fallback is given, let's
3354            * try unqualified.  This is mostly a hack to work
3355            * around systems which ship dbus-launch in /usr/bin
3356            * but everything else in /bin (because dbus-launch
3357            * depends on X11).
3358            */
3359           if (path_fallback)
3360             /* We must have a slash, because we checked above */
3361             execvp (strrchr (progpath, '/')+1, argv);
3362         }
3363       else
3364         execvp (progpath, argv);
3365
3366       /* still nothing, we failed */
3367       _exit (1);
3368     }
3369
3370   /* parent process */
3371   close (result_pipe[WRITE_END]);
3372   close (errors_pipe[WRITE_END]);
3373   result_pipe[WRITE_END] = -1;
3374   errors_pipe[WRITE_END] = -1;
3375
3376   ret = 0;
3377   do
3378     {
3379       ret = _dbus_read (result_pipe[READ_END], result, 1024);
3380     }
3381   while (ret > 0);
3382
3383   /* reap the child process to avoid it lingering as zombie */
3384   do
3385     {
3386       ret = waitpid (pid, &status, 0);
3387     }
3388   while (ret == -1 && errno == EINTR);
3389
3390   /* We succeeded if the process exited with status 0 and
3391      anything was read */
3392   if (!WIFEXITED (status) || WEXITSTATUS (status) != 0 )
3393     {
3394       /* The process ended with error */
3395       DBusString error_message;
3396       if (!_dbus_string_init (&error_message))
3397         {
3398           _DBUS_SET_OOM (error);
3399           goto out;
3400         }
3401
3402       ret = 0;
3403       do
3404         {
3405           ret = _dbus_read (errors_pipe[READ_END], &error_message, 1024);
3406         }
3407       while (ret > 0);
3408
3409       _dbus_string_set_length (result, orig_len);
3410       if (_dbus_string_get_length (&error_message) > 0)
3411         dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
3412                         "%s terminated abnormally with the following error: %s",
3413                         progpath, _dbus_string_get_data (&error_message));
3414       else
3415         dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
3416                         "%s terminated abnormally without any error message",
3417                         progpath);
3418       goto out;
3419     }
3420
3421   retval = TRUE;
3422
3423  out:
3424   sigprocmask (SIG_SETMASK, &old_set, NULL);
3425
3426   if (retval)
3427     _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3428   else
3429     _DBUS_ASSERT_ERROR_IS_SET (error);
3430
3431   if (result_pipe[0] != -1)
3432     close (result_pipe[0]);
3433   if (result_pipe[1] != -1)
3434     close (result_pipe[1]);
3435   if (errors_pipe[0] != -1)
3436     close (errors_pipe[0]);
3437   if (errors_pipe[1] != -1)
3438     close (errors_pipe[1]);
3439
3440   return retval;
3441 }
3442 #endif
3443
3444 /**
3445  * Returns the address of a new session bus.
3446  *
3447  * If successful, returns #TRUE and appends the address to @p
3448  * address. If a failure happens, returns #FALSE and
3449  * sets an error in @p error.
3450  *
3451  * @param scope scope of autolaunch (Windows only)
3452  * @param address a DBusString where the address can be stored
3453  * @param error a DBusError to store the error in case of failure
3454  * @returns #TRUE on success, #FALSE if an error happened
3455  */
3456 dbus_bool_t
3457 _dbus_get_autolaunch_address (const char *scope,
3458                               DBusString *address,
3459                               DBusError  *error)
3460 {
3461 #ifdef DBUS_ENABLE_X11_AUTOLAUNCH
3462   /* Perform X11-based autolaunch. (We also support launchd-based autolaunch,
3463    * but that's done elsewhere, and if it worked, this function wouldn't
3464    * be called.) */
3465   const char *display;
3466   char *argv[6];
3467   int i;
3468   DBusString uuid;
3469   dbus_bool_t retval;
3470
3471   if (_dbus_check_setuid ())
3472     {
3473       dbus_set_error_const (error, DBUS_ERROR_NOT_SUPPORTED,
3474                             "Unable to autolaunch when setuid");
3475       return FALSE;
3476     }
3477
3478   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3479   retval = FALSE;
3480
3481   /* fd.o #19997: if $DISPLAY isn't set to something useful, then
3482    * dbus-launch-x11 is just going to fail. Rather than trying to
3483    * run it, we might as well bail out early with a nice error. */
3484   display = _dbus_getenv ("DISPLAY");
3485
3486   if (display == NULL || display[0] == '\0')
3487     {
3488       dbus_set_error_const (error, DBUS_ERROR_NOT_SUPPORTED,
3489           "Unable to autolaunch a dbus-daemon without a $DISPLAY for X11");
3490       return FALSE;
3491     }
3492
3493   if (!_dbus_string_init (&uuid))
3494     {
3495       _DBUS_SET_OOM (error);
3496       return FALSE;
3497     }
3498
3499   if (!_dbus_get_local_machine_uuid_encoded (&uuid))
3500     {
3501       _DBUS_SET_OOM (error);
3502       goto out;
3503     }
3504
3505   i = 0;
3506   argv[i] = "dbus-launch";
3507   ++i;
3508   argv[i] = "--autolaunch";
3509   ++i;
3510   argv[i] = _dbus_string_get_data (&uuid);
3511   ++i;
3512   argv[i] = "--binary-syntax";
3513   ++i;
3514   argv[i] = "--close-stderr";
3515   ++i;
3516   argv[i] = NULL;
3517   ++i;
3518
3519   _dbus_assert (i == _DBUS_N_ELEMENTS (argv));
3520
3521   retval = _read_subprocess_line_argv (DBUS_BINDIR "/dbus-launch",
3522                                        TRUE,
3523                                        argv, address, error);
3524
3525  out:
3526   _dbus_string_free (&uuid);
3527   return retval;
3528 #else
3529   dbus_set_error_const (error, DBUS_ERROR_NOT_SUPPORTED,
3530       "Using X11 for dbus-daemon autolaunch was disabled at compile time, "
3531       "set your DBUS_SESSION_BUS_ADDRESS instead");
3532   return FALSE;
3533 #endif
3534 }
3535
3536 /**
3537  * Reads the uuid of the machine we're running on from
3538  * the dbus configuration. Optionally try to create it
3539  * (only root can do this usually).
3540  *
3541  * On UNIX, reads a file that gets created by dbus-uuidgen
3542  * in a post-install script. On Windows, if there's a standard
3543  * machine uuid we could just use that, but I can't find one
3544  * with the right properties (the hardware profile guid can change
3545  * without rebooting I believe). If there's no standard one
3546  * we might want to use the registry instead of a file for
3547  * this, and I'm not sure how we'd ensure the uuid gets created.
3548  *
3549  * @param machine_id guid to init with the machine's uuid
3550  * @param create_if_not_found try to create the uuid if it doesn't exist
3551  * @param error the error return
3552  * @returns #FALSE if the error is set
3553  */
3554 dbus_bool_t
3555 _dbus_read_local_machine_uuid (DBusGUID   *machine_id,
3556                                dbus_bool_t create_if_not_found,
3557                                DBusError  *error)
3558 {
3559   DBusString filename;
3560   dbus_bool_t b;
3561
3562   _dbus_string_init_const (&filename, DBUS_MACHINE_UUID_FILE);
3563
3564   b = _dbus_read_uuid_file (&filename, machine_id, create_if_not_found, error);
3565   if (b)
3566     return TRUE;
3567
3568   dbus_error_free (error);
3569
3570   /* Fallback to the system machine ID */
3571   _dbus_string_init_const (&filename, "/etc/machine-id");
3572   return _dbus_read_uuid_file (&filename, machine_id, FALSE, error);
3573 }
3574
3575 /**
3576  * quries launchd for a specific env var which holds the socket path.
3577  * @param socket_path append the socket path to this DBusString
3578  * @param launchd_env_var the env var to look up
3579  * @param error a DBusError to store the error in case of failure
3580  * @return the value of the env var
3581  */
3582 dbus_bool_t
3583 _dbus_lookup_launchd_socket (DBusString *socket_path,
3584                              const char *launchd_env_var,
3585                              DBusError  *error)
3586 {
3587 #ifdef DBUS_ENABLE_LAUNCHD
3588   char *argv[4];
3589   int i;
3590
3591   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3592
3593   if (_dbus_check_setuid ())
3594     {
3595       dbus_set_error_const (error, DBUS_ERROR_NOT_SUPPORTED,
3596                             "Unable to find launchd socket when setuid");
3597       return FALSE;
3598     }
3599
3600   i = 0;
3601   argv[i] = "launchctl";
3602   ++i;
3603   argv[i] = "getenv";
3604   ++i;
3605   argv[i] = (char*)launchd_env_var;
3606   ++i;
3607   argv[i] = NULL;
3608   ++i;
3609
3610   _dbus_assert (i == _DBUS_N_ELEMENTS (argv));
3611
3612   if (!_read_subprocess_line_argv(argv[0], TRUE, argv, socket_path, error))
3613     {
3614       return FALSE;
3615     }
3616
3617   /* no error, but no result either */
3618   if (_dbus_string_get_length(socket_path) == 0)
3619     {
3620       return FALSE;
3621     }
3622
3623   /* strip the carriage-return */
3624   _dbus_string_shorten(socket_path, 1);
3625   return TRUE;
3626 #else /* DBUS_ENABLE_LAUNCHD */
3627   dbus_set_error(error, DBUS_ERROR_NOT_SUPPORTED,
3628                 "can't lookup socket from launchd; launchd support not compiled in");
3629   return FALSE;
3630 #endif
3631 }
3632
3633 #ifdef DBUS_ENABLE_LAUNCHD
3634 static dbus_bool_t
3635 _dbus_lookup_session_address_launchd (DBusString *address, DBusError  *error)
3636 {
3637   dbus_bool_t valid_socket;
3638   DBusString socket_path;
3639
3640   if (_dbus_check_setuid ())
3641     {
3642       dbus_set_error_const (error, DBUS_ERROR_NOT_SUPPORTED,
3643                             "Unable to find launchd socket when setuid");
3644       return FALSE;
3645     }
3646
3647   if (!_dbus_string_init (&socket_path))
3648     {
3649       _DBUS_SET_OOM (error);
3650       return FALSE;
3651     }
3652
3653   valid_socket = _dbus_lookup_launchd_socket (&socket_path, "DBUS_LAUNCHD_SESSION_BUS_SOCKET", error);
3654
3655   if (dbus_error_is_set(error))
3656     {
3657       _dbus_string_free(&socket_path);
3658       return FALSE;
3659     }
3660
3661   if (!valid_socket)
3662     {
3663       dbus_set_error(error, "no socket path",
3664                 "launchd did not provide a socket path, "
3665                 "verify that org.freedesktop.dbus-session.plist is loaded!");
3666       _dbus_string_free(&socket_path);
3667       return FALSE;
3668     }
3669   if (!_dbus_string_append (address, "unix:path="))
3670     {
3671       _DBUS_SET_OOM (error);
3672       _dbus_string_free(&socket_path);
3673       return FALSE;
3674     }
3675   if (!_dbus_string_copy (&socket_path, 0, address,
3676                           _dbus_string_get_length (address)))
3677     {
3678       _DBUS_SET_OOM (error);
3679       _dbus_string_free(&socket_path);
3680       return FALSE;
3681     }
3682
3683   _dbus_string_free(&socket_path);
3684   return TRUE;
3685 }
3686 #endif
3687
3688 /**
3689  * Determines the address of the session bus by querying a
3690  * platform-specific method.
3691  *
3692  * The first parameter will be a boolean specifying whether
3693  * or not a dynamic session lookup is supported on this platform.
3694  *
3695  * If supported is TRUE and the return value is #TRUE, the
3696  * address will be  appended to @p address.
3697  * If a failure happens, returns #FALSE and sets an error in
3698  * @p error.
3699  *
3700  * If supported is FALSE, ignore the return value.
3701  *
3702  * @param supported returns whether this method is supported
3703  * @param address a DBusString where the address can be stored
3704  * @param error a DBusError to store the error in case of failure
3705  * @returns #TRUE on success, #FALSE if an error happened
3706  */
3707 dbus_bool_t
3708 _dbus_lookup_session_address (dbus_bool_t *supported,
3709                               DBusString  *address,
3710                               DBusError   *error)
3711 {
3712 #ifdef DBUS_ENABLE_LAUNCHD
3713   *supported = TRUE;
3714   return _dbus_lookup_session_address_launchd (address, error);
3715 #else
3716   /* On non-Mac Unix platforms, if the session address isn't already
3717    * set in DBUS_SESSION_BUS_ADDRESS environment variable, we punt and
3718    * fall back to the autolaunch: global default; see
3719    * init_session_address in dbus/dbus-bus.c. */
3720   *supported = FALSE;
3721   return TRUE;
3722 #endif
3723 }
3724
3725 /**
3726  * Called when the bus daemon is signaled to reload its configuration; any
3727  * caches should be nuked. Of course any caches that need explicit reload
3728  * are probably broken, but c'est la vie.
3729  *
3730  *
3731  */
3732 void
3733 _dbus_flush_caches (void)
3734 {
3735   _dbus_user_database_flush_system ();
3736 }
3737
3738 /**
3739  * Appends the directory in which a keyring for the given credentials
3740  * should be stored.  The credentials should have either a Windows or
3741  * UNIX user in them.  The directory should be an absolute path.
3742  *
3743  * On UNIX the directory is ~/.dbus-keyrings while on Windows it should probably
3744  * be something else, since the dotfile convention is not normal on Windows.
3745  *
3746  * @param directory string to append directory to
3747  * @param credentials credentials the directory should be for
3748  *
3749  * @returns #FALSE on no memory
3750  */
3751 dbus_bool_t
3752 _dbus_append_keyring_directory_for_credentials (DBusString      *directory,
3753                                                 DBusCredentials *credentials)
3754 {
3755   DBusString homedir;
3756   DBusString dotdir;
3757   dbus_uid_t uid;
3758
3759   _dbus_assert (credentials != NULL);
3760   _dbus_assert (!_dbus_credentials_are_anonymous (credentials));
3761
3762   if (!_dbus_string_init (&homedir))
3763     return FALSE;
3764
3765   uid = _dbus_credentials_get_unix_uid (credentials);
3766   _dbus_assert (uid != DBUS_UID_UNSET);
3767
3768   if (!_dbus_homedir_from_uid (uid, &homedir))
3769     goto failed;
3770
3771 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
3772   {
3773     const char *override;
3774
3775     override = _dbus_getenv ("DBUS_TEST_HOMEDIR");
3776     if (override != NULL && *override != '\0')
3777       {
3778         _dbus_string_set_length (&homedir, 0);
3779         if (!_dbus_string_append (&homedir, override))
3780           goto failed;
3781
3782         _dbus_verbose ("Using fake homedir for testing: %s\n",
3783                        _dbus_string_get_const_data (&homedir));
3784       }
3785     else
3786       {
3787         /* Not strictly thread-safe, but if we fail at thread-safety here,
3788          * the worst that will happen is some extra warnings. */
3789         static dbus_bool_t already_warned = FALSE;
3790         if (!already_warned)
3791           {
3792             _dbus_warn ("Using your real home directory for testing, set DBUS_TEST_HOMEDIR to avoid\n");
3793             already_warned = TRUE;
3794           }
3795       }
3796   }
3797 #endif
3798
3799   _dbus_string_init_const (&dotdir, ".dbus-keyrings");
3800   if (!_dbus_concat_dir_and_file (&homedir,
3801                                   &dotdir))
3802     goto failed;
3803
3804   if (!_dbus_string_copy (&homedir, 0,
3805                           directory, _dbus_string_get_length (directory))) {
3806     goto failed;
3807   }
3808
3809   _dbus_string_free (&homedir);
3810   return TRUE;
3811
3812  failed:
3813   _dbus_string_free (&homedir);
3814   return FALSE;
3815 }
3816
3817 //PENDING(kdab) docs
3818 dbus_bool_t
3819 _dbus_daemon_publish_session_bus_address (const char* addr,
3820                                           const char *scope)
3821 {
3822   return TRUE;
3823 }
3824
3825 //PENDING(kdab) docs
3826 void
3827 _dbus_daemon_unpublish_session_bus_address (void)
3828 {
3829
3830 }
3831
3832 /**
3833  * See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently
3834  * for Winsock so is abstracted)
3835  *
3836  * @returns #TRUE if errno == EAGAIN or errno == EWOULDBLOCK
3837  */
3838 dbus_bool_t
3839 _dbus_get_is_errno_eagain_or_ewouldblock (void)
3840 {
3841   return errno == EAGAIN || errno == EWOULDBLOCK;
3842 }
3843
3844 /**
3845  * Removes a directory; Directory must be empty
3846  *
3847  * @param filename directory filename
3848  * @param error initialized error object
3849  * @returns #TRUE on success
3850  */
3851 dbus_bool_t
3852 _dbus_delete_directory (const DBusString *filename,
3853                         DBusError        *error)
3854 {
3855   const char *filename_c;
3856
3857   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3858
3859   filename_c = _dbus_string_get_const_data (filename);
3860
3861   if (rmdir (filename_c) != 0)
3862     {
3863       dbus_set_error (error, DBUS_ERROR_FAILED,
3864                       "Failed to remove directory %s: %s\n",
3865                       filename_c, _dbus_strerror (errno));
3866       return FALSE;
3867     }
3868
3869   return TRUE;
3870 }
3871
3872 /**
3873  *  Checks whether file descriptors may be passed via the socket
3874  *
3875  *  @param fd the socket
3876  *  @return TRUE when fd passing over this socket is supported
3877  *
3878  */
3879 dbus_bool_t
3880 _dbus_socket_can_pass_unix_fd(int fd) {
3881
3882 #ifdef SCM_RIGHTS
3883   union {
3884     struct sockaddr sa;
3885     struct sockaddr_storage storage;
3886     struct sockaddr_un un;
3887   } sa_buf;
3888
3889   socklen_t sa_len = sizeof(sa_buf);
3890
3891   _DBUS_ZERO(sa_buf);
3892
3893   if (getsockname(fd, &sa_buf.sa, &sa_len) < 0)
3894     return FALSE;
3895
3896   return sa_buf.sa.sa_family == AF_UNIX;
3897
3898 #else
3899   return FALSE;
3900
3901 #endif
3902 }
3903
3904 /**
3905  * Closes all file descriptors except the first three (i.e. stdin,
3906  * stdout, stderr).
3907  */
3908 void
3909 _dbus_close_all (void)
3910 {
3911   int maxfds, i;
3912
3913 #ifdef __linux__
3914   DIR *d;
3915
3916   /* On Linux we can optimize this a bit if /proc is available. If it
3917      isn't available, fall back to the brute force way. */
3918
3919   d = opendir ("/proc/self/fd");
3920   if (d)
3921     {
3922       for (;;)
3923         {
3924           struct dirent buf, *de;
3925           int k, fd;
3926           long l;
3927           char *e = NULL;
3928
3929           k = readdir_r (d, &buf, &de);
3930           if (k != 0 || !de)
3931             break;
3932
3933           if (de->d_name[0] == '.')
3934             continue;
3935
3936           errno = 0;
3937           l = strtol (de->d_name, &e, 10);
3938           if (errno != 0 || e == NULL || *e != '\0')
3939             continue;
3940
3941           fd = (int) l;
3942           if (fd < 3)
3943             continue;
3944
3945           if (fd == dirfd (d))
3946             continue;
3947
3948           close (fd);
3949         }
3950
3951       closedir (d);
3952       return;
3953     }
3954 #endif
3955
3956   maxfds = sysconf (_SC_OPEN_MAX);
3957
3958   /* Pick something reasonable if for some reason sysconf says
3959    * unlimited.
3960    */
3961   if (maxfds < 0)
3962     maxfds = 1024;
3963
3964   /* close all inherited fds */
3965   for (i = 3; i < maxfds; i++)
3966     close (i);
3967 }
3968
3969 /**
3970  * **NOTE**: If you modify this function, please also consider making
3971  * the corresponding change in GLib.  See
3972  * glib/gutils.c:g_check_setuid().
3973  *
3974  * Returns TRUE if the current process was executed as setuid (or an
3975  * equivalent __libc_enable_secure is available).  See:
3976  * http://osdir.com/ml/linux.lfs.hardened/2007-04/msg00032.html
3977  */
3978 dbus_bool_t
3979 _dbus_check_setuid (void)
3980 {
3981   /* TODO: get __libc_enable_secure exported from glibc.
3982    * See http://www.openwall.com/lists/owl-dev/2012/08/14/1
3983    */
3984 #if 0 && defined(HAVE_LIBC_ENABLE_SECURE)
3985   {
3986     /* See glibc/include/unistd.h */
3987     extern int __libc_enable_secure;
3988     return __libc_enable_secure;
3989   }
3990 #elif defined(HAVE_ISSETUGID)
3991   /* BSD: http://www.freebsd.org/cgi/man.cgi?query=issetugid&sektion=2 */
3992   return issetugid ();
3993 #else
3994   uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
3995   gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
3996
3997   /* We call into this function from _dbus_threads_init_platform_specific()
3998    * to make sure these are initialized before we start threading. */
3999   static dbus_bool_t check_setuid_initialised;
4000   static dbus_bool_t is_setuid;
4001
4002   if (_DBUS_UNLIKELY (!check_setuid_initialised))
4003     {
4004 #ifdef HAVE_GETRESUID
4005       if (getresuid (&ruid, &euid, &suid) != 0 ||
4006           getresgid (&rgid, &egid, &sgid) != 0)
4007 #endif /* HAVE_GETRESUID */
4008         {
4009           suid = ruid = getuid ();
4010           sgid = rgid = getgid ();
4011           euid = geteuid ();
4012           egid = getegid ();
4013         }
4014
4015       check_setuid_initialised = TRUE;
4016       is_setuid = (ruid != euid || ruid != suid ||
4017                    rgid != egid || rgid != sgid);
4018
4019     }
4020   return is_setuid;
4021 #endif
4022 }
4023
4024 /**
4025  * Read the address from the socket and append it to the string
4026  *
4027  * @param fd the socket
4028  * @param address
4029  * @param error return location for error code
4030  */
4031 dbus_bool_t
4032 _dbus_append_address_from_socket (int         fd,
4033                                   DBusString *address,
4034                                   DBusError  *error)
4035 {
4036   union {
4037       struct sockaddr sa;
4038       struct sockaddr_storage storage;
4039       struct sockaddr_un un;
4040       struct sockaddr_in ipv4;
4041       struct sockaddr_in6 ipv6;
4042   } socket;
4043   char hostip[INET6_ADDRSTRLEN];
4044   int size = sizeof (socket);
4045
4046   if (getsockname (fd, &socket.sa, &size))
4047     goto err;
4048
4049   switch (socket.sa.sa_family)
4050     {
4051     case AF_UNIX:
4052       if (socket.un.sun_path[0]=='\0')
4053         {
4054           if (_dbus_string_append_printf (address, "unix:abstract=%s", &(socket.un.sun_path[1])))
4055             return TRUE;
4056         }
4057       else
4058         {
4059           if (_dbus_string_append_printf (address, "unix:path=%s", socket.un.sun_path))
4060             return TRUE;
4061         }
4062       break;
4063     case AF_INET:
4064       if (inet_ntop (AF_INET, &socket.ipv4.sin_addr, hostip, sizeof (hostip)))
4065         if (_dbus_string_append_printf (address, "tcp:family=ipv4,host=%s,port=%u",
4066                    hostip, ntohs (socket.ipv4.sin_port)))
4067           return TRUE;
4068       break;
4069 #ifdef AF_INET6
4070     case AF_INET6:
4071       if (inet_ntop (AF_INET6, &socket.ipv6.sin6_addr, hostip, sizeof (hostip)))
4072         if (_dbus_string_append_printf (address, "tcp:family=ipv6,host=%s,port=%u",
4073                    hostip, ntohs (socket.ipv6.sin6_port)))
4074           return TRUE;
4075       break;
4076 #endif
4077     default:
4078       dbus_set_error (error,
4079                       _dbus_error_from_errno (EINVAL),
4080                       "Failed to read address from socket: Unknown socket type.");
4081       return FALSE;
4082     }
4083  err:
4084   dbus_set_error (error,
4085                   _dbus_error_from_errno (errno),
4086                   "Failed to open socket: %s",
4087                   _dbus_strerror (errno));
4088   return FALSE;
4089 }
4090
4091 /* tests in dbus-sysdeps-util.c */