1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
26 #include <sys/types.h>
28 #include <glib/gstdio.h>
37 #include "gdbusauthmechanismsha1.h"
38 #include "gcredentials.h"
39 #include "gdbuserror.h"
40 #include "gioenumtypes.h"
42 #include "gdbusprivate.h"
47 * Arbitrary timeouts for keys in the keyring.
48 * For interoperability, these match the reference implementation, libdbus.
49 * To make them easier to compare, their names also match libdbus
50 * (see dbus/dbus-keyring.c).
54 * Maximum age of a key before we create a new key to use in challenges:
57 #define NEW_KEY_TIMEOUT_SECONDS (60*5)
60 * Time before we drop a key from the keyring: 7 minutes.
61 * Authentication will succeed if it takes less than
62 * EXPIRE_KEYS_TIMEOUT_SECONDS - NEW_KEY_TIMEOUT_SECONDS (2 minutes)
64 * The spec says "delete any cookies that are old (the timeout can be
67 #define EXPIRE_KEYS_TIMEOUT_SECONDS (NEW_KEY_TIMEOUT_SECONDS + (60*2))
70 * Maximum amount of time a key can be in the future due to clock skew
71 * with a shared home directory: 5 minutes.
72 * The spec says "a reasonable time in the future".
74 #define MAX_TIME_TRAVEL_SECONDS (60*5)
77 struct _GDBusAuthMechanismSha1Private
81 GDBusAuthMechanismState state;
82 gchar *reject_reason; /* non-NULL iff (state == G_DBUS_AUTH_MECHANISM_STATE_REJECTED) */
84 /* used on the client side */
87 /* used on the server side */
89 gchar *server_challenge;
92 static gint mechanism_get_priority (void);
93 static const gchar *mechanism_get_name (void);
95 static gboolean mechanism_is_supported (GDBusAuthMechanism *mechanism);
96 static gchar *mechanism_encode_data (GDBusAuthMechanism *mechanism,
100 static gchar *mechanism_decode_data (GDBusAuthMechanism *mechanism,
103 gsize *out_data_len);
104 static GDBusAuthMechanismState mechanism_server_get_state (GDBusAuthMechanism *mechanism);
105 static void mechanism_server_initiate (GDBusAuthMechanism *mechanism,
106 const gchar *initial_response,
107 gsize initial_response_len);
108 static void mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
111 static gchar *mechanism_server_data_send (GDBusAuthMechanism *mechanism,
112 gsize *out_data_len);
113 static gchar *mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism);
114 static void mechanism_server_shutdown (GDBusAuthMechanism *mechanism);
115 static GDBusAuthMechanismState mechanism_client_get_state (GDBusAuthMechanism *mechanism);
116 static gchar *mechanism_client_initiate (GDBusAuthMechanism *mechanism,
117 gsize *out_initial_response_len);
118 static void mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
121 static gchar *mechanism_client_data_send (GDBusAuthMechanism *mechanism,
122 gsize *out_data_len);
123 static void mechanism_client_shutdown (GDBusAuthMechanism *mechanism);
125 /* ---------------------------------------------------------------------------------------------------- */
127 G_DEFINE_TYPE_WITH_PRIVATE (GDBusAuthMechanismSha1, _g_dbus_auth_mechanism_sha1, G_TYPE_DBUS_AUTH_MECHANISM)
129 /* ---------------------------------------------------------------------------------------------------- */
132 _g_dbus_auth_mechanism_sha1_finalize (GObject *object)
134 GDBusAuthMechanismSha1 *mechanism = G_DBUS_AUTH_MECHANISM_SHA1 (object);
136 g_free (mechanism->priv->reject_reason);
137 g_free (mechanism->priv->to_send);
139 g_free (mechanism->priv->cookie);
140 g_free (mechanism->priv->server_challenge);
142 if (G_OBJECT_CLASS (_g_dbus_auth_mechanism_sha1_parent_class)->finalize != NULL)
143 G_OBJECT_CLASS (_g_dbus_auth_mechanism_sha1_parent_class)->finalize (object);
147 _g_dbus_auth_mechanism_sha1_class_init (GDBusAuthMechanismSha1Class *klass)
149 GObjectClass *gobject_class;
150 GDBusAuthMechanismClass *mechanism_class;
152 gobject_class = G_OBJECT_CLASS (klass);
153 gobject_class->finalize = _g_dbus_auth_mechanism_sha1_finalize;
155 mechanism_class = G_DBUS_AUTH_MECHANISM_CLASS (klass);
156 mechanism_class->get_priority = mechanism_get_priority;
157 mechanism_class->get_name = mechanism_get_name;
158 mechanism_class->is_supported = mechanism_is_supported;
159 mechanism_class->encode_data = mechanism_encode_data;
160 mechanism_class->decode_data = mechanism_decode_data;
161 mechanism_class->server_get_state = mechanism_server_get_state;
162 mechanism_class->server_initiate = mechanism_server_initiate;
163 mechanism_class->server_data_receive = mechanism_server_data_receive;
164 mechanism_class->server_data_send = mechanism_server_data_send;
165 mechanism_class->server_get_reject_reason = mechanism_server_get_reject_reason;
166 mechanism_class->server_shutdown = mechanism_server_shutdown;
167 mechanism_class->client_get_state = mechanism_client_get_state;
168 mechanism_class->client_initiate = mechanism_client_initiate;
169 mechanism_class->client_data_receive = mechanism_client_data_receive;
170 mechanism_class->client_data_send = mechanism_client_data_send;
171 mechanism_class->client_shutdown = mechanism_client_shutdown;
175 _g_dbus_auth_mechanism_sha1_init (GDBusAuthMechanismSha1 *mechanism)
177 mechanism->priv = _g_dbus_auth_mechanism_sha1_get_instance_private (mechanism);
180 /* ---------------------------------------------------------------------------------------------------- */
183 mechanism_get_priority (void)
189 mechanism_get_name (void)
191 return "DBUS_COOKIE_SHA1";
195 mechanism_is_supported (GDBusAuthMechanism *mechanism)
197 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), FALSE);
202 mechanism_encode_data (GDBusAuthMechanism *mechanism,
212 mechanism_decode_data (GDBusAuthMechanism *mechanism,
220 /* ---------------------------------------------------------------------------------------------------- */
226 ret = g_random_int_range (0, 60);
237 random_ascii_string (guint len)
242 challenge = g_string_new (NULL);
243 for (n = 0; n < len; n++)
244 g_string_append_c (challenge, random_ascii ());
245 return g_string_free (challenge, FALSE);
249 random_blob (guint len)
254 challenge = g_string_new (NULL);
255 for (n = 0; n < len; n++)
256 g_string_append_c (challenge, g_random_int_range (0, 256));
257 return g_string_free (challenge, FALSE);
260 /* ---------------------------------------------------------------------------------------------------- */
262 /* ensure keyring dir exists and permissions are correct */
264 ensure_keyring_directory (GError **error)
272 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
274 e = g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR");
281 path = g_build_filename (g_get_home_dir (),
287 if (stat (path, &statbuf) != 0)
295 g_io_error_from_errno (errsv),
296 _("Error when getting information for directory “%s”: %s"),
299 g_clear_pointer (&path, g_free);
303 else if (S_ISDIR (statbuf.st_mode))
305 if (g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION") == NULL &&
306 (statbuf.st_mode & 0777) != 0700)
311 _("Permissions on directory “%s” are malformed. Expected mode 0700, got 0%o"),
313 (guint) (statbuf.st_mode & 0777));
314 g_clear_pointer (&path, g_free);
318 return g_steal_pointer (&path);
320 #else /* if !G_OS_UNIX */
321 /* On non-Unix platforms, check that it exists as a directory, but don’t do
322 * permissions checks at the moment. */
323 if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
326 #pragma GCC diagnostic push
327 #pragma GCC diagnostic warning "-Wcpp"
328 #warning Please implement permission checking on this non-UNIX platform
329 #pragma GCC diagnostic pop
330 #endif /* __GNUC__ */
331 return g_steal_pointer (&path);
333 #endif /* if !G_OS_UNIX */
335 if (g_mkdir_with_parents (path, 0700) != 0)
340 g_io_error_from_errno (errsv),
341 _("Error creating directory “%s”: %s"),
344 g_clear_pointer (&path, g_free);
348 return g_steal_pointer (&path);
351 /* ---------------------------------------------------------------------------------------------------- */
353 /* looks up an entry in the keyring */
355 keyring_lookup_entry (const gchar *cookie_context,
366 g_return_val_if_fail (cookie_context != NULL, NULL);
367 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
374 keyring_dir = ensure_keyring_directory (error);
375 if (keyring_dir == NULL)
378 path = g_build_filename (keyring_dir, cookie_context, NULL);
380 if (!g_file_get_contents (path,
385 g_prefix_error (error,
386 _("Error opening keyring “%s” for reading: "),
390 g_assert (contents != NULL);
392 lines = g_strsplit (contents, "\n", 0);
393 for (n = 0; lines[n] != NULL; n++)
395 const gchar *line = lines[n];
403 tokens = g_strsplit (line, " ", 0);
404 if (g_strv_length (tokens) != 3)
409 _("Line %d of the keyring at “%s” with content “%s” is malformed"),
417 line_id = g_ascii_strtoll (tokens[0], &endp, 10);
423 _("First token of line %d of the keyring at “%s” with content “%s” is malformed"),
431 (void)g_ascii_strtoll (tokens[1], &endp, 10); /* do not care what the timestamp is */
437 _("Second token of line %d of the keyring at “%s” with content “%s” is malformed"),
445 if (line_id == cookie_id)
448 ret = tokens[2]; /* steal pointer */
457 /* BOOH, didn't find the cookie */
461 _("Didn’t find cookie with id %d in the keyring at “%s”"),
466 g_free (keyring_dir);
473 /* function for logging important events that the system administrator should take notice of */
476 _log (const gchar *message,
482 va_start (var_args, message);
483 s = g_strdup_vprintf (message, var_args);
486 /* TODO: might want to send this to syslog instead */
487 g_printerr ("GDBus-DBUS_COOKIE_SHA1: %s\n", s);
491 /* Returns FD for lock file, if it was created exclusively (didn't exist already,
492 * and was created successfully) */
494 create_lock_exclusive (const gchar *lock_path,
500 ret = g_open (lock_path, O_CREAT | O_EXCL, 0600);
506 g_io_error_from_errno (errsv),
507 _("Error creating lock file “%s”: %s"),
517 keyring_acquire_lock (const gchar *path,
525 /* Total possible sleep period = max_tries * timeout_usec = 0.5s */
526 const guint max_tries = 50;
527 const guint timeout_usec = 1000 * 10;
529 g_return_val_if_fail (path != NULL, -1);
530 g_return_val_if_fail (error == NULL || *error == NULL, -1);
533 lock = g_strconcat (path, ".lock", NULL);
535 /* This is what the D-Bus spec says
536 * (https://dbus.freedesktop.org/doc/dbus-specification.html#auth-mechanisms-sha)
538 * Create a lockfile name by appending ".lock" to the name of the
539 * cookie file. The server should attempt to create this file using
540 * O_CREAT | O_EXCL. If file creation fails, the lock
541 * fails. Servers should retry for a reasonable period of time,
542 * then they may choose to delete an existing lock to keep users
543 * from having to manually delete a stale lock. [1]
545 * [1] : Lockfiles are used instead of real file locking fcntl() because
546 * real locking implementations are still flaky on network filesystems
549 for (num_tries = 0; num_tries < max_tries; num_tries++)
551 /* Ignore the error until the final call. */
552 ret = create_lock_exclusive (lock, NULL);
556 /* sleep 10ms, then try again */
557 g_usleep (timeout_usec);
560 if (num_tries == max_tries)
562 /* ok, we slept 50*10ms = 0.5 seconds. Conclude that the lock file must be
563 * stale (nuke it from orbit)
565 if (g_unlink (lock) != 0)
570 g_io_error_from_errno (errsv),
571 _("Error deleting stale lock file “%s”: %s"),
577 _log ("Deleted stale lock file '%s'", lock);
579 /* Try one last time to create it, now that we've deleted the stale one */
580 ret = create_lock_exclusive (lock, error);
591 keyring_release_lock (const gchar *path,
598 g_return_val_if_fail (path != NULL, FALSE);
599 g_return_val_if_fail (lock_fd != -1, FALSE);
600 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
603 lock = g_strdup_printf ("%s.lock", path);
604 if (close (lock_fd) != 0)
609 g_io_error_from_errno (errsv),
610 _("Error closing (unlinked) lock file “%s”: %s"),
615 if (g_unlink (lock) != 0)
620 g_io_error_from_errno (errsv),
621 _("Error unlinking lock file “%s”: %s"),
635 /* adds an entry to the keyring, taking care of locking and deleting stale/future entries */
637 keyring_generate_entry (const gchar *cookie_context,
649 GString *new_contents;
654 gboolean changed_file;
657 g_return_val_if_fail (cookie_context != NULL, FALSE);
658 g_return_val_if_fail (out_id != NULL, FALSE);
659 g_return_val_if_fail (out_cookie != NULL, FALSE);
660 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
672 keyring_dir = ensure_keyring_directory (error);
673 if (keyring_dir == NULL)
676 path = g_build_filename (keyring_dir, cookie_context, NULL);
678 lock_fd = keyring_acquire_lock (path, error);
684 if (!g_file_get_contents (path,
689 if (local_error->domain == G_FILE_ERROR && local_error->code == G_FILE_ERROR_NOENT)
691 /* file doesn't have to exist */
692 g_error_free (local_error);
696 g_propagate_prefixed_error (error,
698 _("Error opening keyring “%s” for writing: "),
704 new_contents = g_string_new (NULL);
705 now = g_get_real_time () / G_USEC_PER_SEC;
706 changed_file = FALSE;
709 if (contents != NULL)
712 lines = g_strsplit (contents, "\n", 0);
713 for (n = 0; lines[n] != NULL; n++)
715 const gchar *line = lines[n];
725 tokens = g_strsplit (line, " ", 0);
726 if (g_strv_length (tokens) != 3)
731 _("Line %d of the keyring at “%s” with content “%s” is malformed"),
739 line_id = g_ascii_strtoll (tokens[0], &endp, 10);
745 _("First token of line %d of the keyring at “%s” with content “%s” is malformed"),
753 line_when = g_ascii_strtoll (tokens[1], &endp, 10);
759 _("Second token of line %d of the keyring at “%s” with content “%s” is malformed"),
770 * Once the lockfile has been created, the server loads the
771 * cookie file. It should then delete any cookies that are
772 * old (the timeout can be fairly short), or more than a
773 * reasonable time in the future (so that cookies never
774 * accidentally become permanent, if the clock was set far
775 * into the future at some point). If no recent keys remain,
776 * the server may generate a new key.
782 /* Oddball case: entry is more recent than our current wall-clock time..
783 * This is OK, it means that another server on another machine but with
784 * same $HOME wrote the entry. */
785 if (line_when - now > MAX_TIME_TRAVEL_SECONDS)
788 _log ("Deleted SHA1 cookie from %" G_GUINT64_FORMAT " seconds in the future", line_when - now);
793 /* Discard entry if it's too old. */
794 if (now - line_when > EXPIRE_KEYS_TIMEOUT_SECONDS)
802 changed_file = FALSE;
806 g_string_append_printf (new_contents,
807 "%d %" G_GUINT64_FORMAT " %s\n",
811 max_line_id = MAX (line_id, max_line_id);
812 /* Only reuse entry if not older than 5 minutes.
814 * (We need a bit of grace time compared to 7 minutes above.. otherwise
815 * there's a race where we reuse the 6min59.9 secs old entry and a
816 * split-second later another server purges the now 7 minute old entry.)
818 if (now - line_when < NEW_KEY_TIMEOUT_SECONDS)
823 use_cookie = tokens[2]; /* steal memory */
831 } /* for each line */
838 *out_cookie = use_cookie;
844 *out_id = max_line_id + 1;
845 raw_cookie = random_blob (32);
846 *out_cookie = _g_dbus_hexencode (raw_cookie, 32);
849 g_string_append_printf (new_contents,
850 "%d %" G_GINT64_FORMAT " %s\n",
852 g_get_real_time () / G_USEC_PER_SEC,
857 /* and now actually write the cookie file if there are changes (this is atomic) */
860 if (!g_file_set_contents_full (path,
863 G_FILE_SET_CONTENTS_CONSISTENT,
869 g_free (*out_cookie);
881 if (!keyring_release_lock (path, lock_fd, &local_error))
887 *error = local_error;
891 g_prefix_error (error,
892 _("(Additionally, releasing the lock for “%s” also failed: %s) "),
894 local_error->message);
899 g_error_free (local_error);
904 g_free (keyring_dir);
908 if (new_contents != NULL)
909 g_string_free (new_contents, TRUE);
914 /* ---------------------------------------------------------------------------------------------------- */
917 generate_sha1 (const gchar *server_challenge,
918 const gchar *client_challenge,
924 str = g_string_new (server_challenge);
925 g_string_append_c (str, ':');
926 g_string_append (str, client_challenge);
927 g_string_append_c (str, ':');
928 g_string_append (str, cookie);
929 sha1 = g_compute_checksum_for_string (G_CHECKSUM_SHA1, str->str, -1);
930 g_string_free (str, TRUE);
935 /* ---------------------------------------------------------------------------------------------------- */
937 static GDBusAuthMechanismState
938 mechanism_server_get_state (GDBusAuthMechanism *mechanism)
940 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
942 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
943 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
945 return m->priv->state;
949 mechanism_server_initiate (GDBusAuthMechanism *mechanism,
950 const gchar *initial_response,
951 gsize initial_response_len)
953 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
955 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
956 g_return_if_fail (!m->priv->is_server && !m->priv->is_client);
958 m->priv->is_server = TRUE;
959 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
961 if (initial_response != NULL && initial_response_len > 0)
967 uid = g_ascii_strtoll (initial_response, &endp, 10);
970 if (uid == getuid ())
972 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
975 #elif defined(G_OS_WIN32)
977 sid = _g_dbus_win32_get_user_sid ();
978 if (g_strcmp0 (initial_response, sid) == 0)
979 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
982 #error Please implement for your OS
988 mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
992 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
994 const gchar *client_challenge;
995 const gchar *alleged_sha1;
998 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
999 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
1000 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
1005 tokens = g_strsplit (data, " ", 0);
1006 if (g_strv_length (tokens) != 2)
1008 g_free (m->priv->reject_reason);
1009 m->priv->reject_reason = g_strdup_printf ("Malformed data '%s'", data);
1010 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1014 client_challenge = tokens[0];
1015 alleged_sha1 = tokens[1];
1017 sha1 = generate_sha1 (m->priv->server_challenge, client_challenge, m->priv->cookie);
1019 if (g_strcmp0 (sha1, alleged_sha1) == 0)
1021 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1025 g_free (m->priv->reject_reason);
1026 m->priv->reject_reason = g_strdup_printf ("SHA-1 mismatch");
1027 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1031 g_strfreev (tokens);
1036 mechanism_server_data_send (GDBusAuthMechanism *mechanism,
1037 gsize *out_data_len)
1039 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1042 const gchar *cookie_context;
1045 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1046 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1047 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1052 /* TODO: use GDBusAuthObserver here to get the cookie context to use? */
1053 cookie_context = "org_gtk_gdbus_general";
1057 if (!keyring_generate_entry (cookie_context,
1062 g_free (m->priv->reject_reason);
1063 m->priv->reject_reason = g_strdup_printf ("Error adding entry to keyring: %s", error->message);
1064 g_error_free (error);
1065 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1069 m->priv->server_challenge = random_ascii_string (16);
1070 s = g_strdup_printf ("%s %d %s",
1073 m->priv->server_challenge);
1074 *out_data_len = strlen (s);
1076 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1083 mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
1085 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1087 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1088 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1089 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_REJECTED, NULL);
1091 return g_strdup (m->priv->reject_reason);
1095 mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
1097 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1099 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1100 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
1102 m->priv->is_server = FALSE;
1105 /* ---------------------------------------------------------------------------------------------------- */
1107 static GDBusAuthMechanismState
1108 mechanism_client_get_state (GDBusAuthMechanism *mechanism)
1110 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1112 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1113 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1115 return m->priv->state;
1119 mechanism_client_initiate (GDBusAuthMechanism *mechanism,
1120 gsize *out_initial_response_len)
1122 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1123 gchar *initial_response;
1125 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1126 g_return_val_if_fail (!m->priv->is_server && !m->priv->is_client, NULL);
1128 m->priv->is_client = TRUE;
1129 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1131 *out_initial_response_len = 0;
1134 initial_response = g_strdup_printf ("%" G_GINT64_FORMAT, (gint64) getuid ());
1135 *out_initial_response_len = strlen (initial_response);
1136 #elif defined (G_OS_WIN32)
1137 initial_response = _g_dbus_win32_get_user_sid ();
1138 *out_initial_response_len = strlen (initial_response);
1140 #error Please implement for your OS
1142 g_assert (initial_response != NULL);
1144 return initial_response;
1148 mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
1152 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1154 const gchar *cookie_context;
1156 const gchar *server_challenge;
1157 gchar *client_challenge;
1163 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1164 g_return_if_fail (m->priv->is_client && !m->priv->is_server);
1165 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
1169 client_challenge = NULL;
1171 tokens = g_strsplit (data, " ", 0);
1172 if (g_strv_length (tokens) != 3)
1174 g_free (m->priv->reject_reason);
1175 m->priv->reject_reason = g_strdup_printf ("Malformed data '%s'", data);
1176 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1180 cookie_context = tokens[0];
1181 cookie_id = g_ascii_strtoll (tokens[1], &endp, 10);
1184 g_free (m->priv->reject_reason);
1185 m->priv->reject_reason = g_strdup_printf ("Malformed cookie_id '%s'", tokens[1]);
1186 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1189 server_challenge = tokens[2];
1192 cookie = keyring_lookup_entry (cookie_context, cookie_id, &error);
1195 g_free (m->priv->reject_reason);
1196 m->priv->reject_reason = g_strdup_printf ("Problems looking up entry in keyring: %s", error->message);
1197 g_error_free (error);
1198 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1202 client_challenge = random_ascii_string (16);
1203 sha1 = generate_sha1 (server_challenge, client_challenge, cookie);
1204 m->priv->to_send = g_strdup_printf ("%s %s", client_challenge, sha1);
1206 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
1209 g_strfreev (tokens);
1211 g_free (client_challenge);
1215 mechanism_client_data_send (GDBusAuthMechanism *mechanism,
1216 gsize *out_data_len)
1218 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1220 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1221 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, NULL);
1222 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1224 g_assert (m->priv->to_send != NULL);
1226 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1228 *out_data_len = strlen (m->priv->to_send);
1229 return g_strdup (m->priv->to_send);
1233 mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
1235 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1237 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1238 g_return_if_fail (m->priv->is_client && !m->priv->is_server);
1240 m->priv->is_client = FALSE;
1243 /* ---------------------------------------------------------------------------------------------------- */