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>
36 #include <glib/gstdio.h>
38 #include "gdbusauthmechanismsha1.h"
39 #include "gcredentials.h"
40 #include "gdbuserror.h"
41 #include "gioenumtypes.h"
43 #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 (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 g_type_class_add_private (klass, sizeof (GDBusAuthMechanismSha1Private));
123 gobject_class = G_OBJECT_CLASS (klass);
124 gobject_class->finalize = _g_dbus_auth_mechanism_sha1_finalize;
126 mechanism_class = G_DBUS_AUTH_MECHANISM_CLASS (klass);
127 mechanism_class->get_priority = mechanism_get_priority;
128 mechanism_class->get_name = mechanism_get_name;
129 mechanism_class->is_supported = mechanism_is_supported;
130 mechanism_class->encode_data = mechanism_encode_data;
131 mechanism_class->decode_data = mechanism_decode_data;
132 mechanism_class->server_get_state = mechanism_server_get_state;
133 mechanism_class->server_initiate = mechanism_server_initiate;
134 mechanism_class->server_data_receive = mechanism_server_data_receive;
135 mechanism_class->server_data_send = mechanism_server_data_send;
136 mechanism_class->server_get_reject_reason = mechanism_server_get_reject_reason;
137 mechanism_class->server_shutdown = mechanism_server_shutdown;
138 mechanism_class->client_get_state = mechanism_client_get_state;
139 mechanism_class->client_initiate = mechanism_client_initiate;
140 mechanism_class->client_data_receive = mechanism_client_data_receive;
141 mechanism_class->client_data_send = mechanism_client_data_send;
142 mechanism_class->client_shutdown = mechanism_client_shutdown;
146 _g_dbus_auth_mechanism_sha1_init (GDBusAuthMechanismSha1 *mechanism)
148 mechanism->priv = G_TYPE_INSTANCE_GET_PRIVATE (mechanism,
149 G_TYPE_DBUS_AUTH_MECHANISM_SHA1,
150 GDBusAuthMechanismSha1Private);
153 /* ---------------------------------------------------------------------------------------------------- */
156 mechanism_get_priority (void)
162 mechanism_get_name (void)
164 return "DBUS_COOKIE_SHA1";
168 mechanism_is_supported (GDBusAuthMechanism *mechanism)
170 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), FALSE);
175 mechanism_encode_data (GDBusAuthMechanism *mechanism,
185 mechanism_decode_data (GDBusAuthMechanism *mechanism,
193 /* ---------------------------------------------------------------------------------------------------- */
199 ret = g_random_int_range (0, 60);
210 random_ascii_string (guint len)
215 challenge = g_string_new (NULL);
216 for (n = 0; n < len; n++)
217 g_string_append_c (challenge, random_ascii ());
218 return g_string_free (challenge, FALSE);
222 random_blob (guint len)
227 challenge = g_string_new (NULL);
228 for (n = 0; n < len; n++)
229 g_string_append_c (challenge, g_random_int_range (0, 256));
230 return g_string_free (challenge, FALSE);
233 /* ---------------------------------------------------------------------------------------------------- */
235 /* ensure keyring dir exists and permissions are correct */
237 ensure_keyring_directory (GError **error)
242 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
244 e = g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR");
251 path = g_build_filename (g_get_home_dir (),
256 if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
258 if (g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION") == NULL)
262 if (stat (path, &statbuf) != 0)
266 g_io_error_from_errno (errno),
267 _("Error statting directory `%s': %s"),
274 if ((statbuf.st_mode & 0777) != 0700)
279 _("Permissions on directory `%s' are malformed. Expected mode 0700, got 0%o"),
281 statbuf.st_mode & 0777);
288 #warning Please implement permission checking on this non-UNIX platform
295 if (g_mkdir (path, 0700) != 0)
299 g_io_error_from_errno (errno),
300 _("Error creating directory `%s': %s"),
312 /* ---------------------------------------------------------------------------------------------------- */
315 append_nibble (GString *s, gint val)
317 g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
321 hexencode (const gchar *str,
330 s = g_string_new (NULL);
331 for (n = 0; n < len; n++)
337 val = ((const guchar *) str)[n];
338 upper_nibble = val >> 4;
339 lower_nibble = val & 0x0f;
341 append_nibble (s, upper_nibble);
342 append_nibble (s, lower_nibble);
345 return g_string_free (s, FALSE);
348 /* ---------------------------------------------------------------------------------------------------- */
350 /* looks up an entry in the keyring */
352 keyring_lookup_entry (const gchar *cookie_context,
363 g_return_val_if_fail (cookie_context != NULL, FALSE);
364 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
371 keyring_dir = ensure_keyring_directory (error);
372 if (keyring_dir == NULL)
375 path = g_build_filename (keyring_dir, cookie_context, NULL);
377 if (!g_file_get_contents (path,
382 g_prefix_error (error,
383 _("Error opening keyring `%s' for reading: "),
387 g_assert (contents != NULL);
389 lines = g_strsplit (contents, "\n", 0);
390 for (n = 0; lines[n] != NULL; n++)
392 const gchar *line = lines[n];
401 tokens = g_strsplit (line, " ", 0);
402 if (g_strv_length (tokens) != 3)
407 _("Line %d of the keyring at `%s' with content `%s' is malformed"),
415 line_id = g_ascii_strtoll (tokens[0], &endp, 10);
421 _("First token of line %d of the keyring at `%s' with content `%s' is malformed"),
429 line_when = g_ascii_strtoll (tokens[1], &endp, 10);
435 _("Second token of line %d of the keyring at `%s' with content `%s' is malformed"),
443 if (line_id == cookie_id)
446 ret = tokens[2]; /* steal pointer */
455 /* BOOH, didn't find the cookie */
459 _("Didn't find cookie with id %d in the keyring at `%s'"),
464 g_free (keyring_dir);
471 /* function for logging important events that the system administrator should take notice of */
473 _log (const gchar *message,
479 va_start (var_args, message);
480 s = g_strdup_vprintf (message, var_args);
483 /* TODO: might want to send this to syslog instead */
484 g_printerr ("GDBus-DBUS_COOKIE_SHA1: %s\n", s);
489 keyring_acquire_lock (const gchar *path,
495 guint num_create_tries;
497 g_return_val_if_fail (path != NULL, FALSE);
498 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
501 lock = g_strdup_printf ("%s.lock", path);
503 /* This is what the D-Bus spec says
505 * Create a lockfile name by appending ".lock" to the name of the
506 * cookie file. The server should attempt to create this file using
507 * O_CREAT | O_EXCL. If file creation fails, the lock
508 * fails. Servers should retry for a reasonable period of time,
509 * then they may choose to delete an existing lock to keep users
510 * from having to manually delete a stale lock. [1]
512 * [1] : Lockfiles are used instead of real file locking fcntl() because
513 * real locking implementations are still flaky on network filesystems
516 num_create_tries = 0;
521 while (g_file_test (lock, G_FILE_TEST_EXISTS))
523 /* sleep 10ms, then try again */
528 /* ok, we slept 50*10ms = 0.5 seconds.. Conclude that the lock-file must be
529 * stale (nuke the it from orbit)
531 if (g_unlink (lock) != 0)
535 g_io_error_from_errno (errno),
536 _("Error deleting stale lock-file `%s': %s"),
541 _log ("Deleted stale lock-file `%s'", lock);
546 ret = g_open (lock, O_CREAT |
556 /* EEXIST: pathname already exists and O_CREAT and O_EXCL were used. */
557 if (errno == EEXISTS)
560 if (num_create_tries < 5)
566 g_io_error_from_errno (errno),
567 _("Error creating lock-file `%s': %s"),
579 keyring_release_lock (const gchar *path,
586 g_return_val_if_fail (path != NULL, FALSE);
587 g_return_val_if_fail (lock_fd != -1, FALSE);
588 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
591 lock = g_strdup_printf ("%s.lock", path);
592 if (close (lock_fd) != 0)
596 g_io_error_from_errno (errno),
597 _("Error closing (unlinked) lock-file `%s': %s"),
602 if (g_unlink (lock) != 0)
606 g_io_error_from_errno (errno),
607 _("Error unlinking lock-file `%s': %s"),
621 /* adds an entry to the keyring, taking care of locking and deleting stale/future entries */
623 keyring_generate_entry (const gchar *cookie_context,
635 GString *new_contents;
640 gboolean changed_file;
643 g_return_val_if_fail (cookie_context != NULL, FALSE);
644 g_return_val_if_fail (out_id != NULL, FALSE);
645 g_return_val_if_fail (out_cookie != NULL, FALSE);
646 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"),
754 * Once the lockfile has been created, the server loads the
755 * cookie file. It should then delete any cookies that are
756 * old (the timeout can be fairly short), or more than a
757 * reasonable time in the future (so that cookies never
758 * accidentally become permanent, if the clock was set far
759 * into the future at some point). If no recent keys remain,
760 * the server may generate a new key.
766 /* Oddball case: entry is more recent than our current wall-clock time..
767 * This is OK, it means that another server on another machine but with
768 * same $HOME wrote the entry.
770 * So discard the entry if it's more than 1 day in the future ("reasonable
771 * time in the future").
773 if (line_when - now > 24*60*60)
776 _log ("Deleted SHA1 cookie from %" G_GUINT64_FORMAT " seconds in the future", line_when - now);
781 /* Discard entry if it's older than 15 minutes ("can be fairly short") */
782 if (now - line_when > 15*60)
790 changed_file = FALSE;
794 g_string_append_printf (new_contents,
795 "%d %" G_GUINT64_FORMAT " %s\n",
799 max_line_id = MAX (line_id, max_line_id);
800 /* Only reuse entry if not older than 10 minutes.
802 * (We need a bit of grace time compared to 15 minutes above.. otherwise
803 * there's a race where we reuse the 14min59.9 secs old entry and a
804 * split-second later another server purges the now 15 minute old entry.)
806 if (now - line_when < 10 * 60)
811 use_cookie = tokens[2]; /* steal memory */
819 } /* for each line */
826 *out_cookie = use_cookie;
832 *out_id = max_line_id + 1;
833 raw_cookie = random_blob (32);
834 *out_cookie = hexencode (raw_cookie, 32);
837 g_string_append_printf (new_contents,
838 "%d %" G_GUINT64_FORMAT " %s\n",
840 (guint64) time (NULL),
845 /* and now actually write the cookie file if there are changes (this is atomic) */
848 if (!g_file_set_contents (path,
855 g_free (*out_cookie);
867 if (!keyring_release_lock (path, lock_fd, &local_error))
873 *error = local_error;
877 g_prefix_error (error,
878 _("(Additionally, releasing the lock for `%s' also failed: %s) "),
880 local_error->message);
885 g_error_free (local_error);
890 g_free (keyring_dir);
894 if (new_contents != NULL)
895 g_string_free (new_contents, TRUE);
900 /* ---------------------------------------------------------------------------------------------------- */
903 generate_sha1 (const gchar *server_challenge,
904 const gchar *client_challenge,
910 str = g_string_new (server_challenge);
911 g_string_append_c (str, ':');
912 g_string_append (str, client_challenge);
913 g_string_append_c (str, ':');
914 g_string_append (str, cookie);
915 sha1 = g_compute_checksum_for_string (G_CHECKSUM_SHA1, str->str, -1);
916 g_string_free (str, TRUE);
921 /* ---------------------------------------------------------------------------------------------------- */
923 static GDBusAuthMechanismState
924 mechanism_server_get_state (GDBusAuthMechanism *mechanism)
926 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
928 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
929 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
931 return m->priv->state;
935 mechanism_server_initiate (GDBusAuthMechanism *mechanism,
936 const gchar *initial_response,
937 gsize initial_response_len)
939 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
941 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
942 g_return_if_fail (!m->priv->is_server && !m->priv->is_client);
944 m->priv->is_server = TRUE;
945 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
947 if (initial_response != NULL && strlen (initial_response) > 0)
953 uid = g_ascii_strtoll (initial_response, &endp, 10);
956 if (uid == getuid ())
958 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
961 #elif defined(G_OS_WIN32)
963 sid = _g_dbus_win32_get_user_sid ();
964 if (g_strcmp0 (initial_response, sid) == 0)
965 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
968 #error Please implement for your OS
974 mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
978 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
980 const gchar *client_challenge;
981 const gchar *alleged_sha1;
984 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
985 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
986 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
991 tokens = g_strsplit (data, " ", 0);
992 if (g_strv_length (tokens) != 2)
994 g_warning ("Malformed data `%s'", data);
995 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
999 client_challenge = tokens[0];
1000 alleged_sha1 = tokens[1];
1002 sha1 = generate_sha1 (m->priv->server_challenge, client_challenge, m->priv->cookie);
1004 if (g_strcmp0 (sha1, alleged_sha1) == 0)
1006 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1010 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1014 g_strfreev (tokens);
1019 mechanism_server_data_send (GDBusAuthMechanism *mechanism,
1020 gsize *out_data_len)
1022 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1025 const gchar *cookie_context;
1028 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1029 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1030 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1034 /* TODO: use GDBusAuthObserver here to get the cookie context to use? */
1035 cookie_context = "org_gtk_gdbus_general";
1038 if (!keyring_generate_entry (cookie_context,
1043 g_warning ("Error adding entry to keyring: %s", error->message);
1044 g_error_free (error);
1045 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1049 m->priv->server_challenge = random_ascii_string (16);
1050 s = g_strdup_printf ("%s %d %s",
1053 m->priv->server_challenge);
1055 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1062 mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
1064 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1066 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1067 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1068 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_REJECTED, NULL);
1070 /* can never end up here because we are never in the REJECTED state */
1071 g_assert_not_reached ();
1077 mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
1079 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1081 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1082 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
1084 m->priv->is_server = FALSE;
1087 /* ---------------------------------------------------------------------------------------------------- */
1089 static GDBusAuthMechanismState
1090 mechanism_client_get_state (GDBusAuthMechanism *mechanism)
1092 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1094 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1095 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1097 return m->priv->state;
1101 mechanism_client_initiate (GDBusAuthMechanism *mechanism,
1102 gsize *out_initial_response_len)
1104 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1105 gchar *initial_response;
1107 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1108 g_return_val_if_fail (!m->priv->is_server && !m->priv->is_client, NULL);
1110 m->priv->is_client = TRUE;
1111 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1113 *out_initial_response_len = -1;
1116 initial_response = g_strdup_printf ("%" G_GINT64_FORMAT, (gint64) getuid ());
1117 #elif defined (G_OS_WIN32)
1118 initial_response = _g_dbus_win32_get_user_sid ();
1120 #error Please implement for your OS
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"