1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-BUS implementation)
4 * Copyright (C) 2002 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-sysdeps.h"
31 #include <sys/types.h>
34 #include <sys/socket.h>
43 * @addtogroup DBusInternalsUtils
47 * Aborts the program with SIGABRT (dumping core).
53 _exit (1); /* in case someone manages to ignore SIGABRT */
57 * Wrapper for getenv().
59 * @param varname name of environment variable
60 * @returns value of environment variable or #NULL if unset
63 _dbus_getenv (const char *varname)
65 return getenv (varname);
69 * Thin wrapper around the read() system call that appends
70 * the data it reads to the DBusString buffer. It appends
71 * up to the given count, and returns the same value
72 * and same errno as read(). The only exception is that
73 * _dbus_read() handles EINTR for you.
75 * @param fd the file descriptor to read from
76 * @param buffer the buffer to append data to
77 * @param count the amount of data to read
78 * @returns the number of bytes read or -1
89 _dbus_assert (count >= 0);
91 start = _dbus_string_get_length (buffer);
93 if (!_dbus_string_lengthen (buffer, count))
99 _dbus_string_get_data_len (buffer, &data, start, count);
103 bytes_read = read (fd, data, count);
111 /* put length back (note that this doesn't actually realloc anything) */
112 _dbus_string_set_length (buffer, start);
118 /* put length back (doesn't actually realloc) */
119 _dbus_string_set_length (buffer, start + bytes_read);
123 _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
131 * Thin wrapper around the write() system call that writes a part of a
132 * DBusString and handles EINTR for you.
134 * @param fd the file descriptor to write
135 * @param buffer the buffer to write data from
136 * @param start the first byte in the buffer to write
137 * @param len the number of bytes to try to write
138 * @returns the number of bytes written or -1 on error
142 const DBusString *buffer,
149 _dbus_string_get_const_data_len (buffer, &data, start, len);
153 bytes_written = write (fd, data, len);
155 if (bytes_written < 0 && errno == EINTR)
159 if (bytes_written > 0)
160 _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
163 return bytes_written;
167 * Like _dbus_write() but will use writev() if possible
168 * to write both buffers in sequence. The return value
169 * is the number of bytes written in the first buffer,
170 * plus the number written in the second. If the first
171 * buffer is written successfully and an error occurs
172 * writing the second, the number of bytes in the first
173 * is returned (i.e. the error is ignored), on systems that
174 * don't have writev. Handles EINTR for you.
175 * The second buffer may be #NULL.
177 * @param fd the file descriptor
178 * @param buffer1 first buffer
179 * @param start1 first byte to write in first buffer
180 * @param len1 number of bytes to write from first buffer
181 * @param buffer2 second buffer, or #NULL
182 * @param start2 first byte to write in second buffer
183 * @param len2 number of bytes to write in second buffer
184 * @returns total bytes written from both buffers, or -1 on error
187 _dbus_write_two (int fd,
188 const DBusString *buffer1,
191 const DBusString *buffer2,
195 _dbus_assert (buffer1 != NULL);
196 _dbus_assert (start1 >= 0);
197 _dbus_assert (start2 >= 0);
198 _dbus_assert (len1 >= 0);
199 _dbus_assert (len2 >= 0);
203 struct iovec vectors[2];
208 _dbus_string_get_const_data_len (buffer1, &data1, start1, len1);
211 _dbus_string_get_const_data_len (buffer2, &data2, start2, len2);
219 vectors[0].iov_base = (char*) data1;
220 vectors[0].iov_len = len1;
221 vectors[1].iov_base = (char*) data2;
222 vectors[1].iov_len = len2;
226 bytes_written = writev (fd,
230 if (bytes_written < 0 && errno == EINTR)
233 return bytes_written;
235 #else /* HAVE_WRITEV */
239 ret1 = _dbus_write (fd, buffer1, start1, len1);
240 if (ret1 == len1 && buffer2 != NULL)
242 ret2 = _dbus_write (fd, buffer2, start2, len2);
244 ret2 = 0; /* we can't report an error as the first write was OK */
251 #endif /* !HAVE_WRITEV */
255 * Creates a socket and connects it to the UNIX domain socket at the
256 * given path. The connection fd is returned, and is set up as
259 * @param path the path to UNIX domain socket
260 * @param result return location for error code
261 * @returns connection file descriptor or -1 on error
264 _dbus_connect_unix_socket (const char *path,
265 DBusResultCode *result)
268 struct sockaddr_un addr;
270 fd = socket (AF_LOCAL, SOCK_STREAM, 0);
274 dbus_set_result (result,
275 _dbus_result_from_errno (errno));
277 _dbus_verbose ("Failed to create socket: %s\n",
278 _dbus_strerror (errno));
284 addr.sun_family = AF_LOCAL;
285 strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
286 addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
288 if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0)
290 dbus_set_result (result,
291 _dbus_result_from_errno (errno));
293 _dbus_verbose ("Failed to connect to socket %s: %s\n",
294 path, _dbus_strerror (errno));
302 if (!_dbus_set_fd_nonblocking (fd, result))
314 * Creates a socket and binds it to the given path,
315 * then listens on the socket. The socket is
316 * set to be nonblocking.
318 * @param path the socket name
319 * @param result return location for errors
320 * @returns the listening file descriptor or -1 on error
323 _dbus_listen_unix_socket (const char *path,
324 DBusResultCode *result)
327 struct sockaddr_un addr;
329 listen_fd = socket (AF_LOCAL, SOCK_STREAM, 0);
333 dbus_set_result (result, _dbus_result_from_errno (errno));
334 _dbus_verbose ("Failed to create socket \"%s\": %s\n",
335 path, _dbus_strerror (errno));
340 addr.sun_family = AF_LOCAL;
341 strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
342 addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
344 if (bind (listen_fd, (struct sockaddr*) &addr, SUN_LEN (&addr)) < 0)
346 dbus_set_result (result, _dbus_result_from_errno (errno));
347 _dbus_verbose ("Failed to bind socket \"%s\": %s\n",
348 path, _dbus_strerror (errno));
353 if (listen (listen_fd, 30 /* backlog */) < 0)
355 dbus_set_result (result, _dbus_result_from_errno (errno));
356 _dbus_verbose ("Failed to listen on socket \"%s\": %s\n",
357 path, _dbus_strerror (errno));
362 if (!_dbus_set_fd_nonblocking (listen_fd, result))
371 /* try to read a single byte and return #TRUE if we read it
372 * and it's equal to nul.
375 read_credentials_byte (int client_fd,
376 DBusResultCode *result)
382 bytes_read = read (client_fd, buf, 1);
389 dbus_set_result (result, _dbus_result_from_errno (errno));
390 _dbus_verbose ("Failed to read credentials byte: %s\n",
391 _dbus_strerror (errno));
395 else if (bytes_read == 0)
397 dbus_set_result (result, DBUS_RESULT_IO_ERROR);
398 _dbus_verbose ("EOF reading credentials byte\n");
403 _dbus_assert (bytes_read == 1);
407 dbus_set_result (result, DBUS_RESULT_FAILED);
408 _dbus_verbose ("Credentials byte was not nul\n");
412 _dbus_verbose ("read credentials byte\n");
419 write_credentials_byte (int server_fd,
420 DBusResultCode *result)
423 char buf[1] = { '\0' };
427 bytes_written = write (server_fd, buf, 1);
429 if (bytes_written < 0 && errno == EINTR)
432 if (bytes_written < 0)
434 dbus_set_result (result, _dbus_result_from_errno (errno));
435 _dbus_verbose ("Failed to write credentials byte: %s\n",
436 _dbus_strerror (errno));
439 else if (bytes_written == 0)
441 dbus_set_result (result, DBUS_RESULT_IO_ERROR);
442 _dbus_verbose ("wrote zero bytes writing credentials byte\n");
447 _dbus_assert (bytes_written == 1);
448 _dbus_verbose ("wrote credentials byte\n");
454 * Reads a single byte which must be nul (an error occurs otherwise),
455 * and reads unix credentials if available. Fills in pid/uid/gid with
456 * -1 if no credentials are available. Return value indicates whether
457 * a byte was read, not whether we got valid credentials. On some
458 * systems, such as Linux, reading/writing the byte isn't actually
459 * required, but we do it anyway just to avoid multiple codepaths.
461 * Fails if no byte is available, so you must select() first.
463 * The point of the byte is that on some systems we have to
464 * use sendmsg()/recvmsg() to transmit credentials.
466 * @param client_fd the client file descriptor
467 * @param credentials struct to fill with credentials of client
468 * @param result location to store result code
469 * @returns #TRUE on success
472 _dbus_read_credentials_unix_socket (int client_fd,
473 DBusCredentials *credentials,
474 DBusResultCode *result)
476 credentials->pid = -1;
477 credentials->uid = -1;
478 credentials->gid = -1;
481 if (read_credentials_byte (client_fd, result))
484 int cr_len = sizeof (cr);
486 if (getsockopt (client_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) == 0 &&
487 cr_len == sizeof (cr))
489 credentials->pid = cr.pid;
490 credentials->uid = cr.uid;
491 credentials->gid = cr.gid;
492 _dbus_verbose ("Got credentials pid %d uid %d gid %d\n",
499 _dbus_verbose ("Failed to getsockopt() credentials, returned len %d/%d: %s\n",
500 cr_len, (int) sizeof (cr), _dbus_strerror (errno));
507 #else /* !SO_PEERCRED */
508 _dbus_verbose ("Socket credentials not supported on this OS\n");
514 * Sends a single nul byte with our UNIX credentials as ancillary
515 * data. Returns #TRUE if the data was successfully written. On
516 * systems that don't support sending credentials, just writes a byte,
517 * doesn't send any credentials. On some systems, such as Linux,
518 * reading/writing the byte isn't actually required, but we do it
519 * anyway just to avoid multiple codepaths.
521 * Fails if no byte can be written, so you must select() first.
523 * The point of the byte is that on some systems we have to
524 * use sendmsg()/recvmsg() to transmit credentials.
526 * @param server_fd file descriptor for connection to server
527 * @param result return location for error code
528 * @returns #TRUE if the byte was sent
531 _dbus_send_credentials_unix_socket (int server_fd,
532 DBusResultCode *result)
534 if (write_credentials_byte (server_fd, result))
541 * Accepts a connection on a listening socket.
542 * Handles EINTR for you.
544 * @param listen_fd the listen file descriptor
545 * @returns the connection fd of the client, or -1 on error
548 _dbus_accept (int listen_fd)
553 client_fd = accept (listen_fd, NULL, NULL);
567 * @addtogroup DBusString
572 * Appends an integer to a DBusString.
574 * @param str the string
575 * @param value the integer value
576 * @returns #FALSE if not enough memory or other failure.
579 _dbus_string_append_int (DBusString *str,
582 /* this calculation is from comp.lang.c faq */
583 #define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1) /* +1 for '-' */
588 orig_len = _dbus_string_get_length (str);
590 if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
593 _dbus_string_get_data_len (str, &buf, orig_len, MAX_LONG_LEN);
595 snprintf (buf, MAX_LONG_LEN, "%ld", value);
604 _dbus_string_shorten (str, MAX_LONG_LEN - i);
610 * Appends a double to a DBusString.
612 * @param str the string
613 * @param value the floating point value
614 * @returns #FALSE if not enough memory or other failure.
617 _dbus_string_append_double (DBusString *str,
620 #define MAX_DOUBLE_LEN 64 /* this is completely made up :-/ */
625 orig_len = _dbus_string_get_length (str);
627 if (!_dbus_string_lengthen (str, MAX_DOUBLE_LEN))
630 _dbus_string_get_data_len (str, &buf, orig_len, MAX_DOUBLE_LEN);
632 snprintf (buf, MAX_LONG_LEN, "%g", value);
641 _dbus_string_shorten (str, MAX_DOUBLE_LEN - i);
647 * Parses an integer contained in a DBusString. Either return parameter
648 * may be #NULL if you aren't interested in it. The integer is parsed
649 * and stored in value_return. Return parameters are not initialized
650 * if the function returns #FALSE.
652 * @param str the string
653 * @param start the byte index of the start of the integer
654 * @param value_return return location of the integer value or #NULL
655 * @param end_return return location of the end of the integer, or #NULL
656 * @returns #TRUE on success
659 _dbus_string_parse_int (const DBusString *str,
668 _dbus_string_get_const_data_len (str, &p, start,
669 _dbus_string_get_length (str) - start);
673 v = strtol (p, &end, 0);
674 if (end == NULL || end == p || errno != 0)
680 *end_return = (end - p);
686 * Parses a floating point number contained in a DBusString. Either
687 * return parameter may be #NULL if you aren't interested in it. The
688 * integer is parsed and stored in value_return. Return parameters are
689 * not initialized if the function returns #FALSE.
691 * @todo this function is currently locale-dependent. Should
692 * ask alexl to relicense g_ascii_strtod() code and put that in
693 * here instead, so it's locale-independent.
695 * @param str the string
696 * @param start the byte index of the start of the float
697 * @param value_return return location of the float value or #NULL
698 * @param end_return return location of the end of the float, or #NULL
699 * @returns #TRUE on success
702 _dbus_string_parse_double (const DBusString *str,
704 double *value_return,
711 _dbus_warn ("_dbus_string_parse_double() needs to be made locale-independent\n");
713 _dbus_string_get_const_data_len (str, &p, start,
714 _dbus_string_get_length (str) - start);
718 v = strtod (p, &end);
719 if (end == NULL || end == p || errno != 0)
725 *end_return = (end - p);
731 * Gets the credentials corresponding to the given username.
733 * @param username the username
734 * @param credentials credentials to fill in
735 * @returns #TRUE if the username existed and we got some credentials
738 _dbus_credentials_from_username (const DBusString *username,
739 DBusCredentials *credentials)
741 const char *username_c_str;
743 credentials->pid = -1;
744 credentials->uid = -1;
745 credentials->gid = -1;
747 _dbus_string_get_const_data (username, &username_c_str);
749 #ifdef HAVE_GETPWNAM_R
757 result = getpwnam_r (username_c_str, &p_str, buf, sizeof (buf),
760 if (result == 0 && p == &p_str)
762 credentials->uid = p->pw_uid;
763 credentials->gid = p->pw_gid;
765 _dbus_verbose ("Username %s has uid %d gid %d\n",
766 username_c_str, credentials->uid, credentials->gid);
771 _dbus_verbose ("User %s unknown\n", username_c_str);
775 #else /* ! HAVE_GETPWNAM_R */
777 /* I guess we're screwed on thread safety here */
780 p = getpwnam (username_c_str);
784 credentials->uid = p->pw_uid;
785 credentials->gid = p->pw_gid;
787 _dbus_verbose ("Username %s has uid %d gid %d\n",
788 username_c_str, credentials->uid, credentials->gid);
793 _dbus_verbose ("User %s unknown\n", username_c_str);
801 * Gets credentials from a UID string. (Parses a string to a UID
802 * and converts to a DBusCredentials.)
804 * @param uid_str the UID in string form
805 * @param credentials credentials to fill in
806 * @returns #TRUE if successfully filled in some credentials
809 _dbus_credentials_from_uid_string (const DBusString *uid_str,
810 DBusCredentials *credentials)
815 credentials->pid = -1;
816 credentials->uid = -1;
817 credentials->gid = -1;
819 if (_dbus_string_get_length (uid_str) == 0)
821 _dbus_verbose ("UID string was zero length\n");
827 if (!_dbus_string_parse_int (uid_str, 0, &uid,
830 _dbus_verbose ("could not parse string as a UID\n");
834 if (end != _dbus_string_get_length (uid_str))
836 _dbus_verbose ("string contained trailing stuff after UID\n");
840 credentials->uid = uid;
846 * Gets the credentials of the current process.
848 * @param credentials credentials to fill in.
851 _dbus_credentials_from_current_process (DBusCredentials *credentials)
853 credentials->pid = getpid ();
854 credentials->uid = getuid ();
855 credentials->gid = getgid ();
859 * Checks whether the provided_credentials are allowed to log in
860 * as the expected_credentials.
862 * @param expected_credentials credentials we're trying to log in as
863 * @param provided_credentials credentials we have
864 * @returns #TRUE if we can log in
867 _dbus_credentials_match (const DBusCredentials *expected_credentials,
868 const DBusCredentials *provided_credentials)
870 if (provided_credentials->uid < 0)
872 else if (expected_credentials->uid < 0)
874 else if (provided_credentials->uid == 0)
876 else if (provided_credentials->uid == expected_credentials->uid)
883 * Appends the uid of the current process to the given string.
885 * @param str the string to append to
886 * @returns #TRUE on success
889 _dbus_string_append_our_uid (DBusString *str)
891 return _dbus_string_append_int (str, getuid ());
894 /** @} end of sysdeps */