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>
27 #include "gdbusauthmechanismsha1.h"
28 #include "gcredentials.h"
29 #include "gdbuserror.h"
30 #include "gioenumtypes.h"
35 #include <sys/types.h>
41 #include <glib/gstdio.h>
46 struct _GDBusAuthMechanismSha1Private
50 GDBusAuthMechanismState state;
52 /* used on the client side */
55 /* used on the server side */
57 gchar *server_challenge;
60 static gint mechanism_get_priority (void);
61 static const gchar *mechanism_get_name (void);
63 static gboolean mechanism_is_supported (GDBusAuthMechanism *mechanism);
64 static gchar *mechanism_encode_data (GDBusAuthMechanism *mechanism,
68 static gchar *mechanism_decode_data (GDBusAuthMechanism *mechanism,
72 static GDBusAuthMechanismState mechanism_server_get_state (GDBusAuthMechanism *mechanism);
73 static void mechanism_server_initiate (GDBusAuthMechanism *mechanism,
74 const gchar *initial_response,
75 gsize initial_response_len);
76 static void mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
79 static gchar *mechanism_server_data_send (GDBusAuthMechanism *mechanism,
81 static gchar *mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism);
82 static void mechanism_server_shutdown (GDBusAuthMechanism *mechanism);
83 static GDBusAuthMechanismState mechanism_client_get_state (GDBusAuthMechanism *mechanism);
84 static gchar *mechanism_client_initiate (GDBusAuthMechanism *mechanism,
85 gsize *out_initial_response_len);
86 static void mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
89 static gchar *mechanism_client_data_send (GDBusAuthMechanism *mechanism,
91 static void mechanism_client_shutdown (GDBusAuthMechanism *mechanism);
93 /* ---------------------------------------------------------------------------------------------------- */
95 G_DEFINE_TYPE (GDBusAuthMechanismSha1, _g_dbus_auth_mechanism_sha1, G_TYPE_DBUS_AUTH_MECHANISM);
97 /* ---------------------------------------------------------------------------------------------------- */
100 _g_dbus_auth_mechanism_sha1_finalize (GObject *object)
102 GDBusAuthMechanismSha1 *mechanism = G_DBUS_AUTH_MECHANISM_SHA1 (object);
104 g_free (mechanism->priv->to_send);
106 g_free (mechanism->priv->cookie);
107 g_free (mechanism->priv->server_challenge);
109 if (G_OBJECT_CLASS (_g_dbus_auth_mechanism_sha1_parent_class)->finalize != NULL)
110 G_OBJECT_CLASS (_g_dbus_auth_mechanism_sha1_parent_class)->finalize (object);
114 _g_dbus_auth_mechanism_sha1_class_init (GDBusAuthMechanismSha1Class *klass)
116 GObjectClass *gobject_class;
117 GDBusAuthMechanismClass *mechanism_class;
119 g_type_class_add_private (klass, sizeof (GDBusAuthMechanismSha1Private));
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_TYPE_INSTANCE_GET_PRIVATE (mechanism,
147 G_TYPE_DBUS_AUTH_MECHANISM_SHA1,
148 GDBusAuthMechanismSha1Private);
151 /* ---------------------------------------------------------------------------------------------------- */
154 mechanism_get_priority (void)
160 mechanism_get_name (void)
162 return "DBUS_COOKIE_SHA1";
166 mechanism_is_supported (GDBusAuthMechanism *mechanism)
168 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), FALSE);
173 mechanism_encode_data (GDBusAuthMechanism *mechanism,
183 mechanism_decode_data (GDBusAuthMechanism *mechanism,
191 /* ---------------------------------------------------------------------------------------------------- */
197 ret = g_random_int_range (0, 60);
208 random_ascii_string (guint len)
213 challenge = g_string_new (NULL);
214 for (n = 0; n < len; n++)
215 g_string_append_c (challenge, random_ascii ());
216 return g_string_free (challenge, FALSE);
220 random_blob (guint len)
225 challenge = g_string_new (NULL);
226 for (n = 0; n < len; n++)
227 g_string_append_c (challenge, g_random_int_range (0, 256));
228 return g_string_free (challenge, FALSE);
231 /* ---------------------------------------------------------------------------------------------------- */
233 /* ensure keyring dir exists and permissions are correct */
235 ensure_keyring_directory (GError **error)
240 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
242 e = g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR");
249 path = g_build_filename (g_get_home_dir (),
254 if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
256 if (g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION") == NULL)
260 if (stat (path, &statbuf) != 0)
264 g_io_error_from_errno (errno),
265 _("Error statting directory `%s': %s"),
272 if ((statbuf.st_mode & 0777) != 0700)
277 _("Permissions on directory `%s' are malformed. Expected mode 0700, got 0%o"),
279 statbuf.st_mode & 0777);
285 #error Please implement permission checking on non-UNIX platforms
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, FALSE);
360 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
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);
431 _("Second token of line %d of the keyring at `%s' with content `%s' is malformed"),
439 if (line_id == cookie_id)
442 ret = tokens[2]; /* steal pointer */
451 /* BOOH, didn't find the cookie */
455 _("Didn't find cookie with id %d in the keyring at `%s'"),
460 g_free (keyring_dir);
467 /* function for logging important events that the system administrator should take notice of */
469 _log (const gchar *message,
475 va_start (var_args, message);
476 s = g_strdup_vprintf (message, var_args);
479 /* TODO: might want to send this to syslog instead */
480 g_printerr ("GDBus-DBUS_COOKIE_SHA1: %s\n", s);
485 keyring_acquire_lock (const gchar *path,
491 guint num_create_tries;
493 g_return_val_if_fail (path != NULL, FALSE);
494 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
497 lock = g_strdup_printf ("%s.lock", path);
499 /* This is what the D-Bus spec says
501 * Create a lockfile name by appending ".lock" to the name of the
502 * cookie file. The server should attempt to create this file using
503 * O_CREAT | O_EXCL. If file creation fails, the lock
504 * fails. Servers should retry for a reasonable period of time,
505 * then they may choose to delete an existing lock to keep users
506 * from having to manually delete a stale lock. [1]
508 * [1] : Lockfiles are used instead of real file locking fcntl() because
509 * real locking implementations are still flaky on network filesystems
512 num_create_tries = 0;
517 while (g_file_test (lock, G_FILE_TEST_EXISTS))
519 /* sleep 10ms, then try again */
524 /* ok, we slept 50*10ms = 0.5 seconds.. Conclude that the lock-file must be
525 * stale (nuke the it from orbit)
527 if (g_unlink (lock) != 0)
531 g_io_error_from_errno (errno),
532 _("Error deleting stale lock-file `%s': %s"),
537 _log ("Deleted stale lock-file `%s'", lock);
542 ret = g_open (lock, O_CREAT |
552 /* EEXIST: pathname already exists and O_CREAT and O_EXCL were used. */
553 if (errno == EEXISTS)
556 if (num_create_tries < 5)
562 g_io_error_from_errno (errno),
563 _("Error creating lock-file `%s': %s"),
575 keyring_release_lock (const gchar *path,
582 g_return_val_if_fail (path != NULL, FALSE);
583 g_return_val_if_fail (lock_fd != -1, FALSE);
584 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
587 lock = g_strdup_printf ("%s.lock", path);
588 if (g_unlink (lock) != 0)
592 g_io_error_from_errno (errno),
593 _("Error unlinking lock-file `%s': %s"),
598 if (close (lock_fd) != 0)
602 g_io_error_from_errno (errno),
603 _("Error closing (unlinked) lock-file `%s': %s"),
617 /* adds an entry to the keyring, taking care of locking and deleting stale/future entries */
619 keyring_generate_entry (const gchar *cookie_context,
631 GString *new_contents;
636 gboolean changed_file;
639 g_return_val_if_fail (cookie_context != NULL, FALSE);
640 g_return_val_if_fail (out_id != NULL, FALSE);
641 g_return_val_if_fail (out_cookie != NULL, FALSE);
642 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
653 keyring_dir = ensure_keyring_directory (error);
654 if (keyring_dir == NULL)
657 path = g_build_filename (keyring_dir, cookie_context, NULL);
659 lock_fd = keyring_acquire_lock (path, error);
665 if (!g_file_get_contents (path,
670 if (local_error->domain == G_FILE_ERROR && local_error->code == G_FILE_ERROR_NOENT)
672 /* file doesn't have to exist */
673 g_error_free (local_error);
677 g_propagate_prefixed_error (error,
679 _("Error opening keyring `%s' for writing: "),
685 new_contents = g_string_new (NULL);
687 changed_file = FALSE;
690 if (contents != NULL)
693 lines = g_strsplit (contents, "\n", 0);
694 for (n = 0; lines[n] != NULL; n++)
696 const gchar *line = lines[n];
706 tokens = g_strsplit (line, " ", 0);
707 if (g_strv_length (tokens) != 3)
712 _("Line %d of the keyring at `%s' with content `%s' is malformed"),
720 line_id = g_ascii_strtoll (tokens[0], &endp, 10);
726 _("First token of line %d of the keyring at `%s' with content `%s' is malformed"),
734 line_when = g_ascii_strtoll (tokens[1], &endp, 10);
740 _("Second token of line %d of the keyring at `%s' with content `%s' is malformed"),
750 * Once the lockfile has been created, the server loads the
751 * cookie file. It should then delete any cookies that are
752 * old (the timeout can be fairly short), or more than a
753 * reasonable time in the future (so that cookies never
754 * accidentally become permanent, if the clock was set far
755 * into the future at some point). If no recent keys remain,
756 * the server may generate a new key.
762 /* Oddball case: entry is more recent than our current wall-clock time..
763 * This is OK, it means that another server on another machine but with
764 * same $HOME wrote the entry.
766 * So discard the entry if it's more than 1 day in the future ("reasonable
767 * time in the future").
769 if (line_when - now > 24*60*60)
772 _log ("Deleted SHA1 cookie from %" G_GUINT64_FORMAT " seconds in the future", line_when - now);
777 /* Discard entry if it's older than 15 minutes ("can be fairly short") */
778 if (now - line_when > 15*60)
786 changed_file = FALSE;
790 g_string_append_printf (new_contents,
791 "%d %" G_GUINT64_FORMAT " %s\n",
795 max_line_id = MAX (line_id, max_line_id);
796 /* Only reuse entry if not older than 10 minutes.
798 * (We need a bit of grace time compared to 15 minutes above.. otherwise
799 * there's a race where we reuse the 14min59.9 secs old entry and a
800 * split-second later another server purges the now 15 minute old entry.)
802 if (now - line_when < 10 * 60)
807 use_cookie = tokens[2]; /* steal memory */
815 } /* for each line */
822 *out_cookie = use_cookie;
828 *out_id = max_line_id + 1;
829 raw_cookie = random_blob (32);
830 *out_cookie = hexencode (raw_cookie, 32);
833 g_string_append_printf (new_contents,
834 "%d %" G_GUINT64_FORMAT " %s\n",
836 (guint64) time (NULL),
841 /* and now actually write the cookie file if there are changes (this is atomic) */
844 if (!g_file_set_contents (path,
851 g_free (*out_cookie);
863 if (!keyring_release_lock (path, lock_fd, &local_error))
869 *error = local_error;
873 g_prefix_error (error,
874 _("(Additionally, releasing the lock for `%s' also failed: %s) "),
876 local_error->message);
881 g_error_free (local_error);
886 g_free (keyring_dir);
890 if (new_contents != NULL)
891 g_string_free (new_contents, TRUE);
896 /* ---------------------------------------------------------------------------------------------------- */
899 generate_sha1 (const gchar *server_challenge,
900 const gchar *client_challenge,
906 str = g_string_new (server_challenge);
907 g_string_append_c (str, ':');
908 g_string_append (str, client_challenge);
909 g_string_append_c (str, ':');
910 g_string_append (str, cookie);
911 sha1 = g_compute_checksum_for_string (G_CHECKSUM_SHA1, str->str, -1);
912 g_string_free (str, TRUE);
917 /* ---------------------------------------------------------------------------------------------------- */
919 static GDBusAuthMechanismState
920 mechanism_server_get_state (GDBusAuthMechanism *mechanism)
922 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
924 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
925 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
927 return m->priv->state;
931 mechanism_server_initiate (GDBusAuthMechanism *mechanism,
932 const gchar *initial_response,
933 gsize initial_response_len)
935 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
937 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
938 g_return_if_fail (!m->priv->is_server && !m->priv->is_client);
940 m->priv->is_server = TRUE;
941 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
943 if (initial_response != NULL && strlen (initial_response) > 0)
949 uid = g_ascii_strtoll (initial_response, &endp, 10);
952 if (uid == getuid ())
954 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
957 #elif defined(G_OS_WIN32)
958 GCredentials *credentials;
959 credentials = g_credentials_new_for_process ();
960 if (g_strcmp0 (g_credentials_get_windows_user (credentials), initial_response) == 0)
961 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
962 g_object_unref (credentials);
964 #error Please implement for your OS
970 mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
974 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
976 const gchar *client_challenge;
977 const gchar *alleged_sha1;
980 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
981 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
982 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
987 tokens = g_strsplit (data, " ", 0);
988 if (g_strv_length (tokens) != 2)
990 g_warning ("Malformed data `%s'", data);
991 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
995 client_challenge = tokens[0];
996 alleged_sha1 = tokens[1];
998 sha1 = generate_sha1 (m->priv->server_challenge, client_challenge, m->priv->cookie);
1000 if (g_strcmp0 (sha1, alleged_sha1) == 0)
1002 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1006 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1010 g_strfreev (tokens);
1015 mechanism_server_data_send (GDBusAuthMechanism *mechanism,
1016 gsize *out_data_len)
1018 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1021 const gchar *cookie_context;
1024 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1025 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1026 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1030 /* TODO: use GDBusAuthObserver here to get the cookie context to use? */
1031 cookie_context = "org_gtk_gdbus_general";
1034 if (!keyring_generate_entry (cookie_context,
1039 g_warning ("Error adding entry to keyring: %s", error->message);
1040 g_error_free (error);
1041 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1045 m->priv->server_challenge = random_ascii_string (16);
1046 s = g_strdup_printf ("%s %d %s",
1049 m->priv->server_challenge);
1051 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1058 mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
1060 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1062 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1063 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1064 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_REJECTED, NULL);
1066 /* can never end up here because we are never in the REJECTED state */
1067 g_assert_not_reached ();
1073 mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
1075 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1077 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1078 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
1080 m->priv->is_server = FALSE;
1083 /* ---------------------------------------------------------------------------------------------------- */
1085 static GDBusAuthMechanismState
1086 mechanism_client_get_state (GDBusAuthMechanism *mechanism)
1088 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1090 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1091 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1093 return m->priv->state;
1097 mechanism_client_initiate (GDBusAuthMechanism *mechanism,
1098 gsize *out_initial_response_len)
1100 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1101 gchar *initial_response;
1103 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1104 g_return_val_if_fail (!m->priv->is_server && !m->priv->is_client, NULL);
1106 m->priv->is_client = TRUE;
1107 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1109 *out_initial_response_len = -1;
1112 initial_response = g_strdup_printf ("%" G_GINT64_FORMAT, (gint64) getuid ());
1113 #elif defined (G_OS_WIN32)
1115 GCredentials *credentials;
1116 credentials = g_credentials_new_for_process ();
1117 initial_response = g_strdup (g_credentials_get_windows_user (credentials));
1118 g_object_unref (credentials);
1122 g_assert (initial_response != NULL);
1124 return initial_response;
1128 mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
1132 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1134 const gchar *cookie_context;
1136 const gchar *server_challenge;
1137 gchar *client_challenge;
1143 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1144 g_return_if_fail (m->priv->is_client && !m->priv->is_server);
1145 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
1149 client_challenge = NULL;
1151 tokens = g_strsplit (data, " ", 0);
1152 if (g_strv_length (tokens) != 3)
1154 g_warning ("Malformed data `%s'", data);
1155 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1159 cookie_context = tokens[0];
1160 cookie_id = g_ascii_strtoll (tokens[1], &endp, 10);
1163 g_warning ("Malformed cookie_id `%s'", tokens[1]);
1164 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1167 server_challenge = tokens[2];
1170 cookie = keyring_lookup_entry (cookie_context, cookie_id, &error);
1173 g_warning ("Problems looking up entry in keyring: %s", error->message);
1174 g_error_free (error);
1175 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1179 client_challenge = random_ascii_string (16);
1180 sha1 = generate_sha1 (server_challenge, client_challenge, cookie);
1181 m->priv->to_send = g_strdup_printf ("%s %s", client_challenge, sha1);
1183 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
1186 g_strfreev (tokens);
1188 g_free (client_challenge);
1192 mechanism_client_data_send (GDBusAuthMechanism *mechanism,
1193 gsize *out_data_len)
1195 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1197 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1198 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, NULL);
1199 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1201 g_assert (m->priv->to_send != NULL);
1203 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1205 return g_strdup (m->priv->to_send);
1209 mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
1211 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1213 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1214 g_return_if_fail (m->priv->is_client && !m->priv->is_server);
1216 m->priv->is_client = FALSE;
1219 /* ---------------------------------------------------------------------------------------------------- */
1221 #define __G_DBUS_AUTH_MECHANISM_SHA1_C__
1222 #include "gioaliasdef.c"