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>
33 #include <glib/gstdio.h>
35 #include "gdbusauthmechanismsha1.h"
36 #include "gcredentials.h"
37 #include "gdbuserror.h"
38 #include "gioenumtypes.h"
40 #include "gdbusprivate.h"
45 struct _GDBusAuthMechanismSha1Private
49 GDBusAuthMechanismState state;
51 /* used on the client side */
54 /* used on the server side */
56 gchar *server_challenge;
59 static gint mechanism_get_priority (void);
60 static const gchar *mechanism_get_name (void);
62 static gboolean mechanism_is_supported (GDBusAuthMechanism *mechanism);
63 static gchar *mechanism_encode_data (GDBusAuthMechanism *mechanism,
67 static gchar *mechanism_decode_data (GDBusAuthMechanism *mechanism,
71 static GDBusAuthMechanismState mechanism_server_get_state (GDBusAuthMechanism *mechanism);
72 static void mechanism_server_initiate (GDBusAuthMechanism *mechanism,
73 const gchar *initial_response,
74 gsize initial_response_len);
75 static void mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
78 static gchar *mechanism_server_data_send (GDBusAuthMechanism *mechanism,
80 static gchar *mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism);
81 static void mechanism_server_shutdown (GDBusAuthMechanism *mechanism);
82 static GDBusAuthMechanismState mechanism_client_get_state (GDBusAuthMechanism *mechanism);
83 static gchar *mechanism_client_initiate (GDBusAuthMechanism *mechanism,
84 gsize *out_initial_response_len);
85 static void mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
88 static gchar *mechanism_client_data_send (GDBusAuthMechanism *mechanism,
90 static void mechanism_client_shutdown (GDBusAuthMechanism *mechanism);
92 /* ---------------------------------------------------------------------------------------------------- */
94 G_DEFINE_TYPE (GDBusAuthMechanismSha1, _g_dbus_auth_mechanism_sha1, G_TYPE_DBUS_AUTH_MECHANISM);
96 /* ---------------------------------------------------------------------------------------------------- */
99 _g_dbus_auth_mechanism_sha1_finalize (GObject *object)
101 GDBusAuthMechanismSha1 *mechanism = G_DBUS_AUTH_MECHANISM_SHA1 (object);
103 g_free (mechanism->priv->to_send);
105 g_free (mechanism->priv->cookie);
106 g_free (mechanism->priv->server_challenge);
108 if (G_OBJECT_CLASS (_g_dbus_auth_mechanism_sha1_parent_class)->finalize != NULL)
109 G_OBJECT_CLASS (_g_dbus_auth_mechanism_sha1_parent_class)->finalize (object);
113 _g_dbus_auth_mechanism_sha1_class_init (GDBusAuthMechanismSha1Class *klass)
115 GObjectClass *gobject_class;
116 GDBusAuthMechanismClass *mechanism_class;
118 g_type_class_add_private (klass, sizeof (GDBusAuthMechanismSha1Private));
120 gobject_class = G_OBJECT_CLASS (klass);
121 gobject_class->finalize = _g_dbus_auth_mechanism_sha1_finalize;
123 mechanism_class = G_DBUS_AUTH_MECHANISM_CLASS (klass);
124 mechanism_class->get_priority = mechanism_get_priority;
125 mechanism_class->get_name = mechanism_get_name;
126 mechanism_class->is_supported = mechanism_is_supported;
127 mechanism_class->encode_data = mechanism_encode_data;
128 mechanism_class->decode_data = mechanism_decode_data;
129 mechanism_class->server_get_state = mechanism_server_get_state;
130 mechanism_class->server_initiate = mechanism_server_initiate;
131 mechanism_class->server_data_receive = mechanism_server_data_receive;
132 mechanism_class->server_data_send = mechanism_server_data_send;
133 mechanism_class->server_get_reject_reason = mechanism_server_get_reject_reason;
134 mechanism_class->server_shutdown = mechanism_server_shutdown;
135 mechanism_class->client_get_state = mechanism_client_get_state;
136 mechanism_class->client_initiate = mechanism_client_initiate;
137 mechanism_class->client_data_receive = mechanism_client_data_receive;
138 mechanism_class->client_data_send = mechanism_client_data_send;
139 mechanism_class->client_shutdown = mechanism_client_shutdown;
143 _g_dbus_auth_mechanism_sha1_init (GDBusAuthMechanismSha1 *mechanism)
145 mechanism->priv = G_TYPE_INSTANCE_GET_PRIVATE (mechanism,
146 G_TYPE_DBUS_AUTH_MECHANISM_SHA1,
147 GDBusAuthMechanismSha1Private);
150 /* ---------------------------------------------------------------------------------------------------- */
153 mechanism_get_priority (void)
159 mechanism_get_name (void)
161 return "DBUS_COOKIE_SHA1";
165 mechanism_is_supported (GDBusAuthMechanism *mechanism)
167 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), FALSE);
172 mechanism_encode_data (GDBusAuthMechanism *mechanism,
182 mechanism_decode_data (GDBusAuthMechanism *mechanism,
190 /* ---------------------------------------------------------------------------------------------------- */
196 ret = g_random_int_range (0, 60);
207 random_ascii_string (guint len)
212 challenge = g_string_new (NULL);
213 for (n = 0; n < len; n++)
214 g_string_append_c (challenge, random_ascii ());
215 return g_string_free (challenge, FALSE);
219 random_blob (guint len)
224 challenge = g_string_new (NULL);
225 for (n = 0; n < len; n++)
226 g_string_append_c (challenge, g_random_int_range (0, 256));
227 return g_string_free (challenge, FALSE);
230 /* ---------------------------------------------------------------------------------------------------- */
232 /* ensure keyring dir exists and permissions are correct */
234 ensure_keyring_directory (GError **error)
239 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
241 e = g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR");
248 path = g_build_filename (g_get_home_dir (),
253 if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
255 if (g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION") == NULL)
259 if (stat (path, &statbuf) != 0)
263 g_io_error_from_errno (errno),
264 _("Error statting directory `%s': %s"),
271 if ((statbuf.st_mode & 0777) != 0700)
276 _("Permissions on directory `%s' are malformed. Expected mode 0700, got 0%o"),
278 statbuf.st_mode & 0777);
284 #warning Please implement permission checking on this non-UNIX platform
290 if (g_mkdir (path, 0700) != 0)
294 g_io_error_from_errno (errno),
295 _("Error creating directory `%s': %s"),
307 /* ---------------------------------------------------------------------------------------------------- */
310 append_nibble (GString *s, gint val)
312 g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
316 hexencode (const gchar *str,
325 s = g_string_new (NULL);
326 for (n = 0; n < len; n++)
332 val = ((const guchar *) str)[n];
333 upper_nibble = val >> 4;
334 lower_nibble = val & 0x0f;
336 append_nibble (s, upper_nibble);
337 append_nibble (s, lower_nibble);
340 return g_string_free (s, FALSE);
343 /* ---------------------------------------------------------------------------------------------------- */
345 /* looks up an entry in the keyring */
347 keyring_lookup_entry (const gchar *cookie_context,
358 g_return_val_if_fail (cookie_context != NULL, FALSE);
359 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
366 keyring_dir = ensure_keyring_directory (error);
367 if (keyring_dir == NULL)
370 path = g_build_filename (keyring_dir, cookie_context, NULL);
372 if (!g_file_get_contents (path,
377 g_prefix_error (error,
378 _("Error opening keyring `%s' for reading: "),
382 g_assert (contents != NULL);
384 lines = g_strsplit (contents, "\n", 0);
385 for (n = 0; lines[n] != NULL; n++)
387 const gchar *line = lines[n];
396 tokens = g_strsplit (line, " ", 0);
397 if (g_strv_length (tokens) != 3)
402 _("Line %d of the keyring at `%s' with content `%s' is malformed"),
410 line_id = g_ascii_strtoll (tokens[0], &endp, 10);
416 _("First token of line %d of the keyring at `%s' with content `%s' is malformed"),
424 line_when = g_ascii_strtoll (tokens[1], &endp, 10);
430 _("Second token of line %d of the keyring at `%s' with content `%s' is malformed"),
438 if (line_id == cookie_id)
441 ret = tokens[2]; /* steal pointer */
450 /* BOOH, didn't find the cookie */
454 _("Didn't find cookie with id %d in the keyring at `%s'"),
459 g_free (keyring_dir);
466 /* function for logging important events that the system administrator should take notice of */
468 _log (const gchar *message,
474 va_start (var_args, message);
475 s = g_strdup_vprintf (message, var_args);
478 /* TODO: might want to send this to syslog instead */
479 g_printerr ("GDBus-DBUS_COOKIE_SHA1: %s\n", s);
484 keyring_acquire_lock (const gchar *path,
490 guint num_create_tries;
492 g_return_val_if_fail (path != NULL, FALSE);
493 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
496 lock = g_strdup_printf ("%s.lock", path);
498 /* This is what the D-Bus spec says
500 * Create a lockfile name by appending ".lock" to the name of the
501 * cookie file. The server should attempt to create this file using
502 * O_CREAT | O_EXCL. If file creation fails, the lock
503 * fails. Servers should retry for a reasonable period of time,
504 * then they may choose to delete an existing lock to keep users
505 * from having to manually delete a stale lock. [1]
507 * [1] : Lockfiles are used instead of real file locking fcntl() because
508 * real locking implementations are still flaky on network filesystems
511 num_create_tries = 0;
516 while (g_file_test (lock, G_FILE_TEST_EXISTS))
518 /* sleep 10ms, then try again */
523 /* ok, we slept 50*10ms = 0.5 seconds.. Conclude that the lock-file must be
524 * stale (nuke the it from orbit)
526 if (g_unlink (lock) != 0)
530 g_io_error_from_errno (errno),
531 _("Error deleting stale lock-file `%s': %s"),
536 _log ("Deleted stale lock-file `%s'", lock);
541 ret = g_open (lock, O_CREAT |
551 /* EEXIST: pathname already exists and O_CREAT and O_EXCL were used. */
552 if (errno == EEXISTS)
555 if (num_create_tries < 5)
561 g_io_error_from_errno (errno),
562 _("Error creating lock-file `%s': %s"),
574 keyring_release_lock (const gchar *path,
581 g_return_val_if_fail (path != NULL, FALSE);
582 g_return_val_if_fail (lock_fd != -1, FALSE);
583 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
586 lock = g_strdup_printf ("%s.lock", path);
587 if (close (lock_fd) != 0)
591 g_io_error_from_errno (errno),
592 _("Error closing (unlinked) lock-file `%s': %s"),
597 if (g_unlink (lock) != 0)
601 g_io_error_from_errno (errno),
602 _("Error unlinking lock-file `%s': %s"),
616 /* adds an entry to the keyring, taking care of locking and deleting stale/future entries */
618 keyring_generate_entry (const gchar *cookie_context,
630 GString *new_contents;
635 gboolean changed_file;
638 g_return_val_if_fail (cookie_context != NULL, FALSE);
639 g_return_val_if_fail (out_id != NULL, FALSE);
640 g_return_val_if_fail (out_cookie != NULL, FALSE);
641 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
652 keyring_dir = ensure_keyring_directory (error);
653 if (keyring_dir == NULL)
656 path = g_build_filename (keyring_dir, cookie_context, NULL);
658 lock_fd = keyring_acquire_lock (path, error);
664 if (!g_file_get_contents (path,
669 if (local_error->domain == G_FILE_ERROR && local_error->code == G_FILE_ERROR_NOENT)
671 /* file doesn't have to exist */
672 g_error_free (local_error);
676 g_propagate_prefixed_error (error,
678 _("Error opening keyring `%s' for writing: "),
684 new_contents = g_string_new (NULL);
686 changed_file = FALSE;
689 if (contents != NULL)
692 lines = g_strsplit (contents, "\n", 0);
693 for (n = 0; lines[n] != NULL; n++)
695 const gchar *line = lines[n];
705 tokens = g_strsplit (line, " ", 0);
706 if (g_strv_length (tokens) != 3)
711 _("Line %d of the keyring at `%s' with content `%s' is malformed"),
719 line_id = g_ascii_strtoll (tokens[0], &endp, 10);
725 _("First token of line %d of the keyring at `%s' with content `%s' is malformed"),
733 line_when = g_ascii_strtoll (tokens[1], &endp, 10);
739 _("Second token of line %d of the keyring at `%s' with content `%s' is malformed"),
749 * Once the lockfile has been created, the server loads the
750 * cookie file. It should then delete any cookies that are
751 * old (the timeout can be fairly short), or more than a
752 * reasonable time in the future (so that cookies never
753 * accidentally become permanent, if the clock was set far
754 * into the future at some point). If no recent keys remain,
755 * the server may generate a new key.
761 /* Oddball case: entry is more recent than our current wall-clock time..
762 * This is OK, it means that another server on another machine but with
763 * same $HOME wrote the entry.
765 * So discard the entry if it's more than 1 day in the future ("reasonable
766 * time in the future").
768 if (line_when - now > 24*60*60)
771 _log ("Deleted SHA1 cookie from %" G_GUINT64_FORMAT " seconds in the future", line_when - now);
776 /* Discard entry if it's older than 15 minutes ("can be fairly short") */
777 if (now - line_when > 15*60)
785 changed_file = FALSE;
789 g_string_append_printf (new_contents,
790 "%d %" G_GUINT64_FORMAT " %s\n",
794 max_line_id = MAX (line_id, max_line_id);
795 /* Only reuse entry if not older than 10 minutes.
797 * (We need a bit of grace time compared to 15 minutes above.. otherwise
798 * there's a race where we reuse the 14min59.9 secs old entry and a
799 * split-second later another server purges the now 15 minute old entry.)
801 if (now - line_when < 10 * 60)
806 use_cookie = tokens[2]; /* steal memory */
814 } /* for each line */
821 *out_cookie = use_cookie;
827 *out_id = max_line_id + 1;
828 raw_cookie = random_blob (32);
829 *out_cookie = hexencode (raw_cookie, 32);
832 g_string_append_printf (new_contents,
833 "%d %" G_GUINT64_FORMAT " %s\n",
835 (guint64) time (NULL),
840 /* and now actually write the cookie file if there are changes (this is atomic) */
843 if (!g_file_set_contents (path,
850 g_free (*out_cookie);
862 if (!keyring_release_lock (path, lock_fd, &local_error))
868 *error = local_error;
872 g_prefix_error (error,
873 _("(Additionally, releasing the lock for `%s' also failed: %s) "),
875 local_error->message);
880 g_error_free (local_error);
885 g_free (keyring_dir);
889 if (new_contents != NULL)
890 g_string_free (new_contents, TRUE);
895 /* ---------------------------------------------------------------------------------------------------- */
898 generate_sha1 (const gchar *server_challenge,
899 const gchar *client_challenge,
905 str = g_string_new (server_challenge);
906 g_string_append_c (str, ':');
907 g_string_append (str, client_challenge);
908 g_string_append_c (str, ':');
909 g_string_append (str, cookie);
910 sha1 = g_compute_checksum_for_string (G_CHECKSUM_SHA1, str->str, -1);
911 g_string_free (str, TRUE);
916 /* ---------------------------------------------------------------------------------------------------- */
918 static GDBusAuthMechanismState
919 mechanism_server_get_state (GDBusAuthMechanism *mechanism)
921 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
923 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
924 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
926 return m->priv->state;
930 mechanism_server_initiate (GDBusAuthMechanism *mechanism,
931 const gchar *initial_response,
932 gsize initial_response_len)
934 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
936 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
937 g_return_if_fail (!m->priv->is_server && !m->priv->is_client);
939 m->priv->is_server = TRUE;
940 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
942 if (initial_response != NULL && strlen (initial_response) > 0)
948 uid = g_ascii_strtoll (initial_response, &endp, 10);
951 if (uid == getuid ())
953 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
956 #elif defined(G_OS_WIN32)
958 sid = _g_dbus_win32_get_user_sid ();
959 if (g_strcmp0 (initial_response, sid) == 0)
960 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
963 #error Please implement for your OS
969 mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
973 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
975 const gchar *client_challenge;
976 const gchar *alleged_sha1;
979 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
980 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
981 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
986 tokens = g_strsplit (data, " ", 0);
987 if (g_strv_length (tokens) != 2)
989 g_warning ("Malformed data `%s'", data);
990 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
994 client_challenge = tokens[0];
995 alleged_sha1 = tokens[1];
997 sha1 = generate_sha1 (m->priv->server_challenge, client_challenge, m->priv->cookie);
999 if (g_strcmp0 (sha1, alleged_sha1) == 0)
1001 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1005 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1009 g_strfreev (tokens);
1014 mechanism_server_data_send (GDBusAuthMechanism *mechanism,
1015 gsize *out_data_len)
1017 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1020 const gchar *cookie_context;
1023 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1024 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1025 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1029 /* TODO: use GDBusAuthObserver here to get the cookie context to use? */
1030 cookie_context = "org_gtk_gdbus_general";
1033 if (!keyring_generate_entry (cookie_context,
1038 g_warning ("Error adding entry to keyring: %s", error->message);
1039 g_error_free (error);
1040 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1044 m->priv->server_challenge = random_ascii_string (16);
1045 s = g_strdup_printf ("%s %d %s",
1048 m->priv->server_challenge);
1050 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1057 mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
1059 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1061 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1062 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1063 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_REJECTED, NULL);
1065 /* can never end up here because we are never in the REJECTED state */
1066 g_assert_not_reached ();
1072 mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
1074 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1076 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1077 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
1079 m->priv->is_server = FALSE;
1082 /* ---------------------------------------------------------------------------------------------------- */
1084 static GDBusAuthMechanismState
1085 mechanism_client_get_state (GDBusAuthMechanism *mechanism)
1087 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1089 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1090 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1092 return m->priv->state;
1096 mechanism_client_initiate (GDBusAuthMechanism *mechanism,
1097 gsize *out_initial_response_len)
1099 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1100 gchar *initial_response;
1102 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1103 g_return_val_if_fail (!m->priv->is_server && !m->priv->is_client, NULL);
1105 m->priv->is_client = TRUE;
1106 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1108 *out_initial_response_len = -1;
1111 initial_response = g_strdup_printf ("%" G_GINT64_FORMAT, (gint64) getuid ());
1112 #elif defined (G_OS_WIN32)
1113 initial_response = _g_dbus_win32_get_user_sid ();
1115 #error Please implement for your OS
1117 g_assert (initial_response != NULL);
1119 return initial_response;
1123 mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
1127 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1129 const gchar *cookie_context;
1131 const gchar *server_challenge;
1132 gchar *client_challenge;
1138 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1139 g_return_if_fail (m->priv->is_client && !m->priv->is_server);
1140 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
1144 client_challenge = NULL;
1146 tokens = g_strsplit (data, " ", 0);
1147 if (g_strv_length (tokens) != 3)
1149 g_warning ("Malformed data `%s'", data);
1150 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1154 cookie_context = tokens[0];
1155 cookie_id = g_ascii_strtoll (tokens[1], &endp, 10);
1158 g_warning ("Malformed cookie_id `%s'", tokens[1]);
1159 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1162 server_challenge = tokens[2];
1165 cookie = keyring_lookup_entry (cookie_context, cookie_id, &error);
1168 g_warning ("Problems looking up entry in keyring: %s", error->message);
1169 g_error_free (error);
1170 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1174 client_challenge = random_ascii_string (16);
1175 sha1 = generate_sha1 (server_challenge, client_challenge, cookie);
1176 m->priv->to_send = g_strdup_printf ("%s %s", client_challenge, sha1);
1178 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
1181 g_strfreev (tokens);
1183 g_free (client_challenge);
1187 mechanism_client_data_send (GDBusAuthMechanism *mechanism,
1188 gsize *out_data_len)
1190 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1192 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1193 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, NULL);
1194 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1196 g_assert (m->priv->to_send != NULL);
1198 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1200 return g_strdup (m->priv->to_send);
1204 mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
1206 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1208 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1209 g_return_if_fail (m->priv->is_client && !m->priv->is_server);
1211 m->priv->is_client = FALSE;
1214 /* ---------------------------------------------------------------------------------------------------- */
1216 #define __G_DBUS_AUTH_MECHANISM_SHA1_C__
1217 #include "gioaliasdef.c"