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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
28 #include <sys/types.h>
30 #include <glib/gstdio.h>
39 #include "gdbusauthmechanismsha1.h"
40 #include "gcredentials.h"
41 #include "gdbuserror.h"
42 #include "gioenumtypes.h"
44 #include "gdbusprivate.h"
48 struct _GDBusAuthMechanismSha1Private
52 GDBusAuthMechanismState state;
54 /* used on the client side */
57 /* used on the server side */
59 gchar *server_challenge;
62 static gint mechanism_get_priority (void);
63 static const gchar *mechanism_get_name (void);
65 static gboolean mechanism_is_supported (GDBusAuthMechanism *mechanism);
66 static gchar *mechanism_encode_data (GDBusAuthMechanism *mechanism,
70 static gchar *mechanism_decode_data (GDBusAuthMechanism *mechanism,
74 static GDBusAuthMechanismState mechanism_server_get_state (GDBusAuthMechanism *mechanism);
75 static void mechanism_server_initiate (GDBusAuthMechanism *mechanism,
76 const gchar *initial_response,
77 gsize initial_response_len);
78 static void mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
81 static gchar *mechanism_server_data_send (GDBusAuthMechanism *mechanism,
83 static gchar *mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism);
84 static void mechanism_server_shutdown (GDBusAuthMechanism *mechanism);
85 static GDBusAuthMechanismState mechanism_client_get_state (GDBusAuthMechanism *mechanism);
86 static gchar *mechanism_client_initiate (GDBusAuthMechanism *mechanism,
87 gsize *out_initial_response_len);
88 static void mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
91 static gchar *mechanism_client_data_send (GDBusAuthMechanism *mechanism,
93 static void mechanism_client_shutdown (GDBusAuthMechanism *mechanism);
95 /* ---------------------------------------------------------------------------------------------------- */
97 G_DEFINE_TYPE_WITH_PRIVATE (GDBusAuthMechanismSha1, _g_dbus_auth_mechanism_sha1, G_TYPE_DBUS_AUTH_MECHANISM)
99 /* ---------------------------------------------------------------------------------------------------- */
102 _g_dbus_auth_mechanism_sha1_finalize (GObject *object)
104 GDBusAuthMechanismSha1 *mechanism = G_DBUS_AUTH_MECHANISM_SHA1 (object);
106 g_free (mechanism->priv->to_send);
108 g_free (mechanism->priv->cookie);
109 g_free (mechanism->priv->server_challenge);
111 if (G_OBJECT_CLASS (_g_dbus_auth_mechanism_sha1_parent_class)->finalize != NULL)
112 G_OBJECT_CLASS (_g_dbus_auth_mechanism_sha1_parent_class)->finalize (object);
116 _g_dbus_auth_mechanism_sha1_class_init (GDBusAuthMechanismSha1Class *klass)
118 GObjectClass *gobject_class;
119 GDBusAuthMechanismClass *mechanism_class;
121 gobject_class = G_OBJECT_CLASS (klass);
122 gobject_class->finalize = _g_dbus_auth_mechanism_sha1_finalize;
124 mechanism_class = G_DBUS_AUTH_MECHANISM_CLASS (klass);
125 mechanism_class->get_priority = mechanism_get_priority;
126 mechanism_class->get_name = mechanism_get_name;
127 mechanism_class->is_supported = mechanism_is_supported;
128 mechanism_class->encode_data = mechanism_encode_data;
129 mechanism_class->decode_data = mechanism_decode_data;
130 mechanism_class->server_get_state = mechanism_server_get_state;
131 mechanism_class->server_initiate = mechanism_server_initiate;
132 mechanism_class->server_data_receive = mechanism_server_data_receive;
133 mechanism_class->server_data_send = mechanism_server_data_send;
134 mechanism_class->server_get_reject_reason = mechanism_server_get_reject_reason;
135 mechanism_class->server_shutdown = mechanism_server_shutdown;
136 mechanism_class->client_get_state = mechanism_client_get_state;
137 mechanism_class->client_initiate = mechanism_client_initiate;
138 mechanism_class->client_data_receive = mechanism_client_data_receive;
139 mechanism_class->client_data_send = mechanism_client_data_send;
140 mechanism_class->client_shutdown = mechanism_client_shutdown;
144 _g_dbus_auth_mechanism_sha1_init (GDBusAuthMechanismSha1 *mechanism)
146 mechanism->priv = _g_dbus_auth_mechanism_sha1_get_instance_private (mechanism);
149 /* ---------------------------------------------------------------------------------------------------- */
152 mechanism_get_priority (void)
158 mechanism_get_name (void)
160 return "DBUS_COOKIE_SHA1";
164 mechanism_is_supported (GDBusAuthMechanism *mechanism)
166 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), FALSE);
171 mechanism_encode_data (GDBusAuthMechanism *mechanism,
181 mechanism_decode_data (GDBusAuthMechanism *mechanism,
189 /* ---------------------------------------------------------------------------------------------------- */
195 ret = g_random_int_range (0, 60);
206 random_ascii_string (guint len)
211 challenge = g_string_new (NULL);
212 for (n = 0; n < len; n++)
213 g_string_append_c (challenge, random_ascii ());
214 return g_string_free (challenge, FALSE);
218 random_blob (guint len)
223 challenge = g_string_new (NULL);
224 for (n = 0; n < len; n++)
225 g_string_append_c (challenge, g_random_int_range (0, 256));
226 return g_string_free (challenge, FALSE);
229 /* ---------------------------------------------------------------------------------------------------- */
231 /* ensure keyring dir exists and permissions are correct */
233 ensure_keyring_directory (GError **error)
238 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
240 e = g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR");
247 path = g_build_filename (g_get_home_dir (),
252 if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
254 if (g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION") == NULL)
258 if (stat (path, &statbuf) != 0)
262 g_io_error_from_errno (errno),
263 _("Error when getting information for directory '%s': %s"),
270 if ((statbuf.st_mode & 0777) != 0700)
275 _("Permissions on directory '%s' are malformed. Expected mode 0700, got 0%o"),
277 statbuf.st_mode & 0777);
284 #warning Please implement permission checking on this non-UNIX platform
291 if (g_mkdir (path, 0700) != 0)
295 g_io_error_from_errno (errno),
296 _("Error creating directory '%s': %s"),
308 /* ---------------------------------------------------------------------------------------------------- */
311 append_nibble (GString *s, gint val)
313 g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
317 hexencode (const gchar *str,
326 s = g_string_new (NULL);
327 for (n = 0; n < len; n++)
333 val = ((const guchar *) str)[n];
334 upper_nibble = val >> 4;
335 lower_nibble = val & 0x0f;
337 append_nibble (s, upper_nibble);
338 append_nibble (s, lower_nibble);
341 return g_string_free (s, FALSE);
344 /* ---------------------------------------------------------------------------------------------------- */
346 /* looks up an entry in the keyring */
348 keyring_lookup_entry (const gchar *cookie_context,
359 g_return_val_if_fail (cookie_context != NULL, NULL);
360 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
367 keyring_dir = ensure_keyring_directory (error);
368 if (keyring_dir == NULL)
371 path = g_build_filename (keyring_dir, cookie_context, NULL);
373 if (!g_file_get_contents (path,
378 g_prefix_error (error,
379 _("Error opening keyring '%s' for reading: "),
383 g_assert (contents != NULL);
385 lines = g_strsplit (contents, "\n", 0);
386 for (n = 0; lines[n] != NULL; n++)
388 const gchar *line = lines[n];
397 tokens = g_strsplit (line, " ", 0);
398 if (g_strv_length (tokens) != 3)
403 _("Line %d of the keyring at '%s' with content '%s' is malformed"),
411 line_id = g_ascii_strtoll (tokens[0], &endp, 10);
417 _("First token of line %d of the keyring at '%s' with content '%s' is malformed"),
425 line_when = g_ascii_strtoll (tokens[1], &endp, 10);
426 line_when = line_when; /* To avoid -Wunused-but-set-variable */
432 _("Second token of line %d of the keyring at '%s' with content '%s' is malformed"),
440 if (line_id == cookie_id)
443 ret = tokens[2]; /* steal pointer */
452 /* BOOH, didn't find the cookie */
456 _("Didn't find cookie with id %d in the keyring at '%s'"),
461 g_free (keyring_dir);
468 /* function for logging important events that the system administrator should take notice of */
471 _log (const gchar *message,
477 va_start (var_args, message);
478 s = g_strdup_vprintf (message, var_args);
481 /* TODO: might want to send this to syslog instead */
482 g_printerr ("GDBus-DBUS_COOKIE_SHA1: %s\n", s);
487 keyring_acquire_lock (const gchar *path,
493 guint num_create_tries;
495 g_return_val_if_fail (path != NULL, FALSE);
496 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
499 lock = g_strdup_printf ("%s.lock", path);
501 /* This is what the D-Bus spec says
503 * Create a lockfile name by appending ".lock" to the name of the
504 * cookie file. The server should attempt to create this file using
505 * O_CREAT | O_EXCL. If file creation fails, the lock
506 * fails. Servers should retry for a reasonable period of time,
507 * then they may choose to delete an existing lock to keep users
508 * from having to manually delete a stale lock. [1]
510 * [1] : Lockfiles are used instead of real file locking fcntl() because
511 * real locking implementations are still flaky on network filesystems
514 num_create_tries = 0;
519 while (g_file_test (lock, G_FILE_TEST_EXISTS))
521 /* sleep 10ms, then try again */
526 /* ok, we slept 50*10ms = 0.5 seconds. Conclude that the lock file must be
527 * stale (nuke the it from orbit)
529 if (g_unlink (lock) != 0)
533 g_io_error_from_errno (errno),
534 _("Error deleting stale lock file '%s': %s"),
539 _log ("Deleted stale lock file '%s'", lock);
544 ret = g_open (lock, O_CREAT |
554 /* EEXIST: pathname already exists and O_CREAT and O_EXCL were used. */
555 if (errno == EEXISTS)
558 if (num_create_tries < 5)
562 num_create_tries = num_create_tries; /* To avoid -Wunused-but-set-variable */
565 g_io_error_from_errno (errno),
566 _("Error creating lock file '%s': %s"),
578 keyring_release_lock (const gchar *path,
585 g_return_val_if_fail (path != NULL, FALSE);
586 g_return_val_if_fail (lock_fd != -1, FALSE);
587 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
590 lock = g_strdup_printf ("%s.lock", path);
591 if (close (lock_fd) != 0)
595 g_io_error_from_errno (errno),
596 _("Error closing (unlinked) lock file '%s': %s"),
601 if (g_unlink (lock) != 0)
605 g_io_error_from_errno (errno),
606 _("Error unlinking lock file '%s': %s"),
620 /* adds an entry to the keyring, taking care of locking and deleting stale/future entries */
622 keyring_generate_entry (const gchar *cookie_context,
634 GString *new_contents;
639 gboolean changed_file;
642 g_return_val_if_fail (cookie_context != NULL, FALSE);
643 g_return_val_if_fail (out_id != NULL, FALSE);
644 g_return_val_if_fail (out_cookie != NULL, FALSE);
645 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
657 keyring_dir = ensure_keyring_directory (error);
658 if (keyring_dir == NULL)
661 path = g_build_filename (keyring_dir, cookie_context, NULL);
663 lock_fd = keyring_acquire_lock (path, error);
669 if (!g_file_get_contents (path,
674 if (local_error->domain == G_FILE_ERROR && local_error->code == G_FILE_ERROR_NOENT)
676 /* file doesn't have to exist */
677 g_error_free (local_error);
681 g_propagate_prefixed_error (error,
683 _("Error opening keyring '%s' for writing: "),
689 new_contents = g_string_new (NULL);
691 changed_file = FALSE;
694 if (contents != NULL)
697 lines = g_strsplit (contents, "\n", 0);
698 for (n = 0; lines[n] != NULL; n++)
700 const gchar *line = lines[n];
710 tokens = g_strsplit (line, " ", 0);
711 if (g_strv_length (tokens) != 3)
716 _("Line %d of the keyring at '%s' with content '%s' is malformed"),
724 line_id = g_ascii_strtoll (tokens[0], &endp, 10);
730 _("First token of line %d of the keyring at '%s' with content '%s' is malformed"),
738 line_when = g_ascii_strtoll (tokens[1], &endp, 10);
744 _("Second token of line %d of the keyring at '%s' with content '%s' is malformed"),
751 line_when = line_when; /* To avoid -Wunused-but-set-variable */
756 * Once the lockfile has been created, the server loads the
757 * cookie file. It should then delete any cookies that are
758 * old (the timeout can be fairly short), or more than a
759 * reasonable time in the future (so that cookies never
760 * accidentally become permanent, if the clock was set far
761 * into the future at some point). If no recent keys remain,
762 * the server may generate a new key.
768 /* Oddball case: entry is more recent than our current wall-clock time..
769 * This is OK, it means that another server on another machine but with
770 * same $HOME wrote the entry.
772 * So discard the entry if it's more than 1 day in the future ("reasonable
773 * time in the future").
775 if (line_when - now > 24*60*60)
778 _log ("Deleted SHA1 cookie from %" G_GUINT64_FORMAT " seconds in the future", line_when - now);
783 /* Discard entry if it's older than 15 minutes ("can be fairly short") */
784 if (now - line_when > 15*60)
792 changed_file = FALSE;
796 g_string_append_printf (new_contents,
797 "%d %" G_GUINT64_FORMAT " %s\n",
801 max_line_id = MAX (line_id, max_line_id);
802 /* Only reuse entry if not older than 10 minutes.
804 * (We need a bit of grace time compared to 15 minutes above.. otherwise
805 * there's a race where we reuse the 14min59.9 secs old entry and a
806 * split-second later another server purges the now 15 minute old entry.)
808 if (now - line_when < 10 * 60)
813 use_cookie = tokens[2]; /* steal memory */
821 } /* for each line */
828 *out_cookie = use_cookie;
834 *out_id = max_line_id + 1;
835 raw_cookie = random_blob (32);
836 *out_cookie = hexencode (raw_cookie, 32);
839 g_string_append_printf (new_contents,
840 "%d %" G_GUINT64_FORMAT " %s\n",
842 (guint64) time (NULL),
847 /* and now actually write the cookie file if there are changes (this is atomic) */
850 if (!g_file_set_contents (path,
857 g_free (*out_cookie);
869 if (!keyring_release_lock (path, lock_fd, &local_error))
875 *error = local_error;
879 g_prefix_error (error,
880 _("(Additionally, releasing the lock for '%s' also failed: %s) "),
882 local_error->message);
887 g_error_free (local_error);
892 g_free (keyring_dir);
896 if (new_contents != NULL)
897 g_string_free (new_contents, TRUE);
902 /* ---------------------------------------------------------------------------------------------------- */
905 generate_sha1 (const gchar *server_challenge,
906 const gchar *client_challenge,
912 str = g_string_new (server_challenge);
913 g_string_append_c (str, ':');
914 g_string_append (str, client_challenge);
915 g_string_append_c (str, ':');
916 g_string_append (str, cookie);
917 sha1 = g_compute_checksum_for_string (G_CHECKSUM_SHA1, str->str, -1);
918 g_string_free (str, TRUE);
923 /* ---------------------------------------------------------------------------------------------------- */
925 static GDBusAuthMechanismState
926 mechanism_server_get_state (GDBusAuthMechanism *mechanism)
928 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
930 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
931 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
933 return m->priv->state;
937 mechanism_server_initiate (GDBusAuthMechanism *mechanism,
938 const gchar *initial_response,
939 gsize initial_response_len)
941 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
943 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
944 g_return_if_fail (!m->priv->is_server && !m->priv->is_client);
946 m->priv->is_server = TRUE;
947 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
949 if (initial_response != NULL && strlen (initial_response) > 0)
955 uid = g_ascii_strtoll (initial_response, &endp, 10);
958 if (uid == getuid ())
960 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
963 #elif defined(G_OS_WIN32)
965 sid = _g_dbus_win32_get_user_sid ();
966 if (g_strcmp0 (initial_response, sid) == 0)
967 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
970 #error Please implement for your OS
976 mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
980 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
982 const gchar *client_challenge;
983 const gchar *alleged_sha1;
986 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
987 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
988 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
993 tokens = g_strsplit (data, " ", 0);
994 if (g_strv_length (tokens) != 2)
996 g_warning ("Malformed data '%s'", data);
997 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1001 client_challenge = tokens[0];
1002 alleged_sha1 = tokens[1];
1004 sha1 = generate_sha1 (m->priv->server_challenge, client_challenge, m->priv->cookie);
1006 if (g_strcmp0 (sha1, alleged_sha1) == 0)
1008 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1012 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1016 g_strfreev (tokens);
1021 mechanism_server_data_send (GDBusAuthMechanism *mechanism,
1022 gsize *out_data_len)
1024 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1027 const gchar *cookie_context;
1030 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1031 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1032 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1036 /* TODO: use GDBusAuthObserver here to get the cookie context to use? */
1037 cookie_context = "org_gtk_gdbus_general";
1041 if (!keyring_generate_entry (cookie_context,
1046 g_warning ("Error adding entry to keyring: %s", error->message);
1047 g_error_free (error);
1048 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1052 m->priv->server_challenge = random_ascii_string (16);
1053 s = g_strdup_printf ("%s %d %s",
1056 m->priv->server_challenge);
1058 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1065 mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
1067 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1069 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1070 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1071 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_REJECTED, NULL);
1073 /* can never end up here because we are never in the REJECTED state */
1074 g_assert_not_reached ();
1080 mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
1082 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1084 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1085 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
1087 m->priv->is_server = FALSE;
1090 /* ---------------------------------------------------------------------------------------------------- */
1092 static GDBusAuthMechanismState
1093 mechanism_client_get_state (GDBusAuthMechanism *mechanism)
1095 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1097 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1098 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1100 return m->priv->state;
1104 mechanism_client_initiate (GDBusAuthMechanism *mechanism,
1105 gsize *out_initial_response_len)
1107 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1108 gchar *initial_response;
1110 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1111 g_return_val_if_fail (!m->priv->is_server && !m->priv->is_client, NULL);
1113 m->priv->is_client = TRUE;
1114 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1116 *out_initial_response_len = -1;
1119 initial_response = g_strdup_printf ("%" G_GINT64_FORMAT, (gint64) getuid ());
1120 #elif defined (G_OS_WIN32)
1121 initial_response = _g_dbus_win32_get_user_sid ();
1123 #error Please implement for your OS
1125 g_assert (initial_response != NULL);
1127 return initial_response;
1131 mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
1135 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1137 const gchar *cookie_context;
1139 const gchar *server_challenge;
1140 gchar *client_challenge;
1146 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1147 g_return_if_fail (m->priv->is_client && !m->priv->is_server);
1148 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
1152 client_challenge = NULL;
1154 tokens = g_strsplit (data, " ", 0);
1155 if (g_strv_length (tokens) != 3)
1157 g_warning ("Malformed data '%s'", data);
1158 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1162 cookie_context = tokens[0];
1163 cookie_id = g_ascii_strtoll (tokens[1], &endp, 10);
1166 g_warning ("Malformed cookie_id '%s'", tokens[1]);
1167 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1170 server_challenge = tokens[2];
1173 cookie = keyring_lookup_entry (cookie_context, cookie_id, &error);
1176 g_warning ("Problems looking up entry in keyring: %s", error->message);
1177 g_error_free (error);
1178 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1182 client_challenge = random_ascii_string (16);
1183 sha1 = generate_sha1 (server_challenge, client_challenge, cookie);
1184 m->priv->to_send = g_strdup_printf ("%s %s", client_challenge, sha1);
1186 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
1189 g_strfreev (tokens);
1191 g_free (client_challenge);
1195 mechanism_client_data_send (GDBusAuthMechanism *mechanism,
1196 gsize *out_data_len)
1198 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1200 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1201 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, NULL);
1202 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1204 g_assert (m->priv->to_send != NULL);
1206 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1208 return g_strdup (m->priv->to_send);
1212 mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
1214 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1216 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1217 g_return_if_fail (m->priv->is_client && !m->priv->is_server);
1219 m->priv->is_client = FALSE;
1222 /* ---------------------------------------------------------------------------------------------------- */