1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25: #include "dbus-internals.h"
26: #include "dbus-sysdeps.h"
27: #include "dbus-sysdeps-unix.h"
28: #include "dbus-threads.h"
29: #include "dbus-protocol.h"
30: #include "dbus-transport.h"
31: #include "dbus-string.h"
32: #include "dbus-userdb.h"
33: #include "dbus-list.h"
34: #include <sys/types.h>
35: #include <stdlib.h>
36: #include <string.h>
37: #include <signal.h>
38: #include <unistd.h>
39: #include <stdio.h>
40: #include <fcntl.h>
41: #include <sys/socket.h>
42: #include <dirent.h>
43: #include <sys/un.h>
44: #include <pwd.h>
45: #include <time.h>
46: #include <locale.h>
47: #include <sys/time.h>
48: #include <sys/stat.h>
49: #include <sys/wait.h>
50: #include <netinet/in.h>
51: #include <netdb.h>
52: #include <grp.h>
53:
54: #ifdef HAVE_ERRNO_H
55: #include <errno.h>
56: #endif
57: #ifdef HAVE_WRITEV
58: #include <sys/uio.h>
59: #endif
60: #ifdef HAVE_POLL
61: #include <sys/poll.h>
62: #endif
63: #ifdef HAVE_BACKTRACE
64: #include <execinfo.h>
65: #endif
66: #ifdef HAVE_GETPEERUCRED
67: #include <ucred.h>
68: #endif
69:
70: #ifndef O_BINARY
71: #define O_BINARY 0
72: #endif
73:
74: #ifndef HAVE_SOCKLEN_T
75: #define socklen_t int
76: #endif
77:
78: static dbus_bool_t
79: _dbus_open_socket (int *fd,
80: int domain,
81: int type,
82: int protocol,
83: DBusError *error)
84: {
85: *fd = socket (domain, type, protocol);
86: if (fd >= 0)
87: {
88: return TRUE;
89: }
90: else
91: {
92: dbus_set_error(error,
93: _dbus_error_from_errno (errno),
94: "Failed to open socket: %s",
95: _dbus_strerror (errno));
96: return FALSE;
97: }
98: }
99:
100: dbus_bool_t
101: _dbus_open_tcp_socket (int *fd,
102: DBusError *error)
103: {
104: return _dbus_open_socket(fd, AF_INET, SOCK_STREAM, 0, error);
105: }
106:
107:
108:
109:
110:
111:
112:
113:
114: dbus_bool_t
115: _dbus_open_unix_socket (int *fd,
116: DBusError *error)
117: {
118: return _dbus_open_socket(fd, PF_UNIX, SOCK_STREAM, 0, error);
119: }
120:
121:
122:
123:
124:
125:
126:
127:
128:
129: dbus_bool_t
130: _dbus_close_socket (int fd,
131: DBusError *error)
132: {
133: return _dbus_close (fd, error);
134: }
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145: int
146: _dbus_read_socket (int fd,
147: DBusString *buffer,
148: int count)
149: {
150: return _dbus_read (fd, buffer, count);
151: }
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163: int
164: _dbus_write_socket (int fd,
165: const DBusString *buffer,
166: int start,
167: int len)
168: {
169: return _dbus_write (fd, buffer, start, len);
170: }
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185: int
186: _dbus_write_socket_two (int fd,
187: const DBusString *buffer1,
188: int start1,
189: int len1,
190: const DBusString *buffer2,
191: int start2,
192: int len2)
193: {
194: return _dbus_write_two (fd, buffer1, start1, len1,
195: buffer2, start2, len2);
196: }
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215: int
216: _dbus_read (int fd,
217: DBusString *buffer,
218: int count)
219: {
220: int bytes_read;
221: int start;
222: char *data;
223:
224: _dbus_assert (count >= 0);
225:
226: start = _dbus_string_get_length (buffer);
227:
228: if (!_dbus_string_lengthen (buffer, count))
229: {
230: errno = ENOMEM;
231: return -1;
232: }
233:
234: data = _dbus_string_get_data_len (buffer, start, count);
235:
236: again:
237:
238: bytes_read = read (fd, data, count);
239:
240: if (bytes_read < 0)
241: {
242: if (errno == EINTR)
243: goto again;
244: else
245: {
246:
247: _dbus_string_set_length (buffer, start);
248: return -1;
249: }
250: }
251: else
252: {
253:
254: _dbus_string_set_length (buffer, start + bytes_read);
255:
256: #if 0
257: if (bytes_read > 0)
258: _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
259: #endif
260:
261: return bytes_read;
262: }
263: }
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275: int
276: _dbus_write (int fd,
277: const DBusString *buffer,
278: int start,
279: int len)
280: {
281: const char *data;
282: int bytes_written;
283:
284: data = _dbus_string_get_const_data_len (buffer, start, len);
285:
286: again:
287:
288: bytes_written = write (fd, data, len);
289:
290: if (bytes_written < 0 && errno == EINTR)
291: goto again;
292:
293: #if 0
294: if (bytes_written > 0)
295: _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
296: #endif
297:
298: return bytes_written;
299: }
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321: int
322: _dbus_write_two (int fd,
323: const DBusString *buffer1,
324: int start1,
325: int len1,
326: const DBusString *buffer2,
327: int start2,
328: int len2)
329: {
330: _dbus_assert (buffer1 != NULL);
331: _dbus_assert (start1 >= 0);
332: _dbus_assert (start2 >= 0);
333: _dbus_assert (len1 >= 0);
334: _dbus_assert (len2 >= 0);
335:
336: #ifdef HAVE_WRITEV
337: {
338: struct iovec vectors[2];
339: const char *data1;
340: const char *data2;
341: int bytes_written;
342:
343: data1 = _dbus_string_get_const_data_len (buffer1, start1, len1);
344:
345: if (buffer2 != NULL)
346: data2 = _dbus_string_get_const_data_len (buffer2, start2, len2);
347: else
348: {
349: data2 = NULL;
350: start2 = 0;
351: len2 = 0;
352: }
353:
354: vectors[0].iov_base = (char*) data1;
355: vectors[0].iov_len = len1;
356: vectors[1].iov_base = (char*) data2;
357: vectors[1].iov_len = len2;
358:
359: again:
360:
361: bytes_written = writev (fd,
362: vectors,
363: data2 ? 2 : 1);
364:
365: if (bytes_written < 0 && errno == EINTR)
366: goto again;
367:
368: return bytes_written;
369: }
370: #else
371: {
372: int ret1;
373:
374: ret1 = _dbus_write (fd, buffer1, start1, len1);
375: if (ret1 == len1 && buffer2 != NULL)
376: {
377: ret2 = _dbus_write (fd, buffer2, start2, len2);
378: if (ret2 < 0)
379: ret2 = 0;
380:
381: return ret1 + ret2;
382: }
383: else
384: return ret1;
385: }
386: #endif
387: }
388:
389: #define _DBUS_MAX_SUN_PATH_LENGTH 99
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418: int
419: _dbus_connect_unix_socket (const char *path,
420: dbus_bool_t abstract,
421: DBusError *error)
422: {
423: int fd;
424: size_t path_len;
425: struct sockaddr_un addr;
426:
427: _DBUS_ASSERT_ERROR_IS_CLEAR (error);
428:
429: _dbus_verbose ("connecting to unix socket %s abstract=%d\n",
430: path, abstract);
431:
432:
433: if (!_dbus_open_unix_socket (&fd, error))
434: {
435: _DBUS_ASSERT_ERROR_IS_SET(error);
436: return -1;
437: }
438: _DBUS_ASSERT_ERROR_IS_CLEAR(error);
439:
440: _DBUS_ZERO (addr);
441: addr.sun_family = AF_UNIX;
442: path_len = strlen (path);
443:
444: if (abstract)
445: {
446: #ifdef HAVE_ABSTRACT_SOCKETS
447: addr.sun_path[0] = '\0';
448: path_len++;
449:
450: if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
451: {
452: dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
453: "Abstract socket name too long\n");
454: _dbus_close (fd, NULL);
455: return -1;
456: }
457:
458: strncpy (&addr.sun_path[1], path, path_len);
459:
460: #else
461: dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
462: "Operating system does not support abstract socket namespace\n");
463: _dbus_close (fd, NULL);
464: return -1;
465: #endif
466: }
467: else
468: {
469: if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
470: {
471: dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
472: "Socket name too long\n");
473: _dbus_close (fd, NULL);
474: return -1;
475: }
476:
477: strncpy (addr.sun_path, path, path_len);
478: }
479:
480: if (connect (fd, (struct sockaddr*) &addr, _DBUS_STRUCT_OFFSET (struct sockaddr_un, sun_path) + path_len) < 0)
481: {
482: dbus_set_error (error,
483: _dbus_error_from_errno (errno),
484: "Failed to connect to socket %s: %s",
485: path, _dbus_strerror (errno));
486:
487: _dbus_close (fd, NULL);
488: fd = -1;
489:
490: return -1;
491: }
492:
493: if (!_dbus_set_fd_nonblocking (fd, error))
494: {
495: _DBUS_ASSERT_ERROR_IS_SET (error);
496:
497: _dbus_close (fd, NULL);
498: fd = -1;
499:
500: return -1;
501: }
502:
503: return fd;
504: }
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515: static dbus_bool_t
516: _dbus_set_local_creds (int fd, dbus_bool_t on)
517: {
518: dbus_bool_t retval = TRUE;
519:
520: #if defined(HAVE_CMSGCRED)
521:
522:
523:
524: #elif defined(LOCAL_CREDS)
525: int val = on ? 1 : 0;
526: if (setsockopt (fd, 0, LOCAL_CREDS, &val, sizeof (val)) < 0)
527: {
528: _dbus_verbose ("Unable to set LOCAL_CREDS socket option on fd %d\n", fd);
529: retval = FALSE;
530: }
531: else
532: _dbus_verbose ("LOCAL_CREDS %s for further messages on fd %d\n",
533: on ? "enabled" : "disabled", fd);
534: #endif
535:
536: return retval;
537: }
538:
539:
540:
541:
542:
543:
544:
545:
546:
547:
548:
549:
550:
551:
552:
553:
554: int
555: _dbus_listen_unix_socket (const char *path,
556: dbus_bool_t abstract,
557: DBusError *error)
558: {
559: int listen_fd;
560: struct sockaddr_un addr;
561: size_t path_len;
562:
563: _DBUS_ASSERT_ERROR_IS_CLEAR (error);
564:
565: _dbus_verbose ("listening on unix socket %s abstract=%d\n",
566: path, abstract);
567:
568: if (!_dbus_open_unix_socket (&listen_fd, error))
569: {
570: _DBUS_ASSERT_ERROR_IS_SET(error);
571: return -1;
572: }
573: _DBUS_ASSERT_ERROR_IS_CLEAR(error);
574:
575: _DBUS_ZERO (addr);
576: addr.sun_family = AF_UNIX;
577: path_len = strlen (path);
578:
579: if (abstract)
580: {
581: #ifdef HAVE_ABSTRACT_SOCKETS
582:
583:
584:
585: addr.sun_path[0] = '\0';
586: path_len++;
587:
588: if (path_len > _DBUS_MAX_SUN_PATH_LENGTH)
589: {
590: dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
591: "Abs