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"
47 struct _GDBusAuthMechanismSha1Private
51 GDBusAuthMechanismState state;
53 /* used on the client side */
56 /* used on the server side */
58 gchar *server_challenge;
61 static gint mechanism_get_priority (void);
62 static const gchar *mechanism_get_name (void);
64 static gboolean mechanism_is_supported (GDBusAuthMechanism *mechanism);
65 static gchar *mechanism_encode_data (GDBusAuthMechanism *mechanism,
69 static gchar *mechanism_decode_data (GDBusAuthMechanism *mechanism,
73 static GDBusAuthMechanismState mechanism_server_get_state (GDBusAuthMechanism *mechanism);
74 static void mechanism_server_initiate (GDBusAuthMechanism *mechanism,
75 const gchar *initial_response,
76 gsize initial_response_len);
77 static void mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
80 static gchar *mechanism_server_data_send (GDBusAuthMechanism *mechanism,
82 static gchar *mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism);
83 static void mechanism_server_shutdown (GDBusAuthMechanism *mechanism);
84 static GDBusAuthMechanismState mechanism_client_get_state (GDBusAuthMechanism *mechanism);
85 static gchar *mechanism_client_initiate (GDBusAuthMechanism *mechanism,
86 gsize *out_initial_response_len);
87 static void mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
90 static gchar *mechanism_client_data_send (GDBusAuthMechanism *mechanism,
92 static void mechanism_client_shutdown (GDBusAuthMechanism *mechanism);
94 /* ---------------------------------------------------------------------------------------------------- */
96 G_DEFINE_TYPE (GDBusAuthMechanismSha1, _g_dbus_auth_mechanism_sha1, G_TYPE_DBUS_AUTH_MECHANISM);
98 /* ---------------------------------------------------------------------------------------------------- */
101 _g_dbus_auth_mechanism_sha1_finalize (GObject *object)
103 GDBusAuthMechanismSha1 *mechanism = G_DBUS_AUTH_MECHANISM_SHA1 (object);
105 g_free (mechanism->priv->to_send);
107 g_free (mechanism->priv->cookie);
108 g_free (mechanism->priv->server_challenge);
110 if (G_OBJECT_CLASS (_g_dbus_auth_mechanism_sha1_parent_class)->finalize != NULL)
111 G_OBJECT_CLASS (_g_dbus_auth_mechanism_sha1_parent_class)->finalize (object);
115 _g_dbus_auth_mechanism_sha1_class_init (GDBusAuthMechanismSha1Class *klass)
117 GObjectClass *gobject_class;
118 GDBusAuthMechanismClass *mechanism_class;
120 g_type_class_add_private (klass, sizeof (GDBusAuthMechanismSha1Private));
122 gobject_class = G_OBJECT_CLASS (klass);
123 gobject_class->finalize = _g_dbus_auth_mechanism_sha1_finalize;
125 mechanism_class = G_DBUS_AUTH_MECHANISM_CLASS (klass);
126 mechanism_class->get_priority = mechanism_get_priority;
127 mechanism_class->get_name = mechanism_get_name;
128 mechanism_class->is_supported = mechanism_is_supported;
129 mechanism_class->encode_data = mechanism_encode_data;
130 mechanism_class->decode_data = mechanism_decode_data;
131 mechanism_class->server_get_state = mechanism_server_get_state;
132 mechanism_class->server_initiate = mechanism_server_initiate;
133 mechanism_class->server_data_receive = mechanism_server_data_receive;
134 mechanism_class->server_data_send = mechanism_server_data_send;
135 mechanism_class->server_get_reject_reason = mechanism_server_get_reject_reason;
136 mechanism_class->server_shutdown = mechanism_server_shutdown;
137 mechanism_class->client_get_state = mechanism_client_get_state;
138 mechanism_class->client_initiate = mechanism_client_initiate;
139 mechanism_class->client_data_receive = mechanism_client_data_receive;
140 mechanism_class->client_data_send = mechanism_client_data_send;
141 mechanism_class->client_shutdown = mechanism_client_shutdown;
145 _g_dbus_auth_mechanism_sha1_init (GDBusAuthMechanismSha1 *mechanism)
147 mechanism->priv = G_TYPE_INSTANCE_GET_PRIVATE (mechanism,
148 G_TYPE_DBUS_AUTH_MECHANISM_SHA1,
149 GDBusAuthMechanismSha1Private);
152 /* ---------------------------------------------------------------------------------------------------- */
155 mechanism_get_priority (void)
161 mechanism_get_name (void)
163 return "DBUS_COOKIE_SHA1";
167 mechanism_is_supported (GDBusAuthMechanism *mechanism)
169 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), FALSE);
174 mechanism_encode_data (GDBusAuthMechanism *mechanism,
184 mechanism_decode_data (GDBusAuthMechanism *mechanism,
192 /* ---------------------------------------------------------------------------------------------------- */
198 ret = g_random_int_range (0, 60);
209 random_ascii_string (guint len)
214 challenge = g_string_new (NULL);
215 for (n = 0; n < len; n++)
216 g_string_append_c (challenge, random_ascii ());
217 return g_string_free (challenge, FALSE);
221 random_blob (guint len)
226 challenge = g_string_new (NULL);
227 for (n = 0; n < len; n++)
228 g_string_append_c (challenge, g_random_int_range (0, 256));
229 return g_string_free (challenge, FALSE);
232 /* ---------------------------------------------------------------------------------------------------- */
234 /* ensure keyring dir exists and permissions are correct */
236 ensure_keyring_directory (GError **error)
241 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
243 e = g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR");
250 path = g_build_filename (g_get_home_dir (),
255 if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
257 if (g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION") == NULL)
261 if (stat (path, &statbuf) != 0)
265 g_io_error_from_errno (errno),
266 _("Error when getting information for directory `%s': %s"),
273 if ((statbuf.st_mode & 0777) != 0700)
278 _("Permissions on directory `%s' are malformed. Expected mode 0700, got 0%o"),
280 statbuf.st_mode & 0777);
287 #warning Please implement permission checking on this non-UNIX platform
294 if (g_mkdir (path, 0700) != 0)
298 g_io_error_from_errno (errno),
299 _("Error creating directory `%s': %s"),
311 /* ---------------------------------------------------------------------------------------------------- */
314 append_nibble (GString *s, gint val)
316 g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
320 hexencode (const gchar *str,
329 s = g_string_new (NULL);
330 for (n = 0; n < len; n++)
336 val = ((const guchar *) str)[n];
337 upper_nibble = val >> 4;
338 lower_nibble = val & 0x0f;
340 append_nibble (s, upper_nibble);
341 append_nibble (s, lower_nibble);
344 return g_string_free (s, FALSE);
347 /* ---------------------------------------------------------------------------------------------------- */
349 /* looks up an entry in the keyring */
351 keyring_lookup_entry (const gchar *cookie_context,
362 g_return_val_if_fail (cookie_context != NULL, NULL);
363 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
370 keyring_dir = ensure_keyring_directory (error);
371 if (keyring_dir == NULL)
374 path = g_build_filename (keyring_dir, cookie_context, NULL);
376 if (!g_file_get_contents (path,
381 g_prefix_error (error,
382 _("Error opening keyring `%s' for reading: "),
386 g_assert (contents != NULL);
388 lines = g_strsplit (contents, "\n", 0);
389 for (n = 0; lines[n] != NULL; n++)
391 const gchar *line = lines[n];
400 tokens = g_strsplit (line, " ", 0);
401 if (g_strv_length (tokens) != 3)
406 _("Line %d of the keyring at `%s' with content `%s' is malformed"),
414 line_id = g_ascii_strtoll (tokens[0], &endp, 10);
420 _("First token of line %d of the keyring at `%s' with content `%s' is malformed"),
428 line_when = g_ascii_strtoll (tokens[1], &endp, 10);
429 line_when = line_when; /* To avoid -Wunused-but-set-variable */
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 */
474 _log (const gchar *message,
480 va_start (var_args, message);
481 s = g_strdup_vprintf (message, var_args);
484 /* TODO: might want to send this to syslog instead */
485 g_printerr ("GDBus-DBUS_COOKIE_SHA1: %s\n", s);
490 keyring_acquire_lock (const gchar *path,
496 guint num_create_tries;
498 g_return_val_if_fail (path != NULL, FALSE);
499 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
502 lock = g_strdup_printf ("%s.lock", path);
504 /* This is what the D-Bus spec says
506 * Create a lockfile name by appending ".lock" to the name of the
507 * cookie file. The server should attempt to create this file using
508 * O_CREAT | O_EXCL. If file creation fails, the lock
509 * fails. Servers should retry for a reasonable period of time,
510 * then they may choose to delete an existing lock to keep users
511 * from having to manually delete a stale lock. [1]
513 * [1] : Lockfiles are used instead of real file locking fcntl() because
514 * real locking implementations are still flaky on network filesystems
517 num_create_tries = 0;
522 while (g_file_test (lock, G_FILE_TEST_EXISTS))
524 /* sleep 10ms, then try again */
529 /* ok, we slept 50*10ms = 0.5 seconds. Conclude that the lock file must be
530 * stale (nuke the it from orbit)
532 if (g_unlink (lock) != 0)
536 g_io_error_from_errno (errno),
537 _("Error deleting stale lock file `%s': %s"),
542 _log ("Deleted stale lock file `%s'", lock);
547 ret = g_open (lock, O_CREAT |
557 /* EEXIST: pathname already exists and O_CREAT and O_EXCL were used. */
558 if (errno == EEXISTS)
561 if (num_create_tries < 5)
565 num_create_tries = num_create_tries; /* To avoid -Wunused-but-set-variable */
568 g_io_error_from_errno (errno),
569 _("Error creating lock file `%s': %s"),
581 keyring_release_lock (const gchar *path,
588 g_return_val_if_fail (path != NULL, FALSE);
589 g_return_val_if_fail (lock_fd != -1, FALSE);
590 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
593 lock = g_strdup_printf ("%s.lock", path);
594 if (close (lock_fd) != 0)
598 g_io_error_from_errno (errno),
599 _("Error closing (unlinked) lock file `%s': %s"),
604 if (g_unlink (lock) != 0)
608 g_io_error_from_errno (errno),
609 _("Error unlinking lock file `%s': %s"),
623 /* adds an entry to the keyring, taking care of locking and deleting stale/future entries */
625 keyring_generate_entry (const gchar *cookie_context,
637 GString *new_contents;
642 gboolean changed_file;
645 g_return_val_if_fail (cookie_context != NULL, FALSE);
646 g_return_val_if_fail (out_id != NULL, FALSE);
647 g_return_val_if_fail (out_cookie != NULL, FALSE);
648 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
660 keyring_dir = ensure_keyring_directory (error);
661 if (keyring_dir == NULL)
664 path = g_build_filename (keyring_dir, cookie_context, NULL);
666 lock_fd = keyring_acquire_lock (path, error);
672 if (!g_file_get_contents (path,
677 if (local_error->domain == G_FILE_ERROR && local_error->code == G_FILE_ERROR_NOENT)
679 /* file doesn't have to exist */
680 g_error_free (local_error);
684 g_propagate_prefixed_error (error,
686 _("Error opening keyring `%s' for writing: "),
692 new_contents = g_string_new (NULL);
694 changed_file = FALSE;
697 if (contents != NULL)
700 lines = g_strsplit (contents, "\n", 0);
701 for (n = 0; lines[n] != NULL; n++)
703 const gchar *line = lines[n];
713 tokens = g_strsplit (line, " ", 0);
714 if (g_strv_length (tokens) != 3)
719 _("Line %d of the keyring at `%s' with content `%s' is malformed"),
727 line_id = g_ascii_strtoll (tokens[0], &endp, 10);
733 _("First token of line %d of the keyring at `%s' with content `%s' is malformed"),
741 line_when = g_ascii_strtoll (tokens[1], &endp, 10);
747 _("Second token of line %d of the keyring at `%s' with content `%s' is malformed"),
754 line_when = line_when; /* To avoid -Wunused-but-set-variable */
759 * Once the lockfile has been created, the server loads the
760 * cookie file. It should then delete any cookies that are
761 * old (the timeout can be fairly short), or more than a
762 * reasonable time in the future (so that cookies never
763 * accidentally become permanent, if the clock was set far
764 * into the future at some point). If no recent keys remain,
765 * the server may generate a new key.
771 /* Oddball case: entry is more recent than our current wall-clock time..
772 * This is OK, it means that another server on another machine but with
773 * same $HOME wrote the entry.
775 * So discard the entry if it's more than 1 day in the future ("reasonable
776 * time in the future").
778 if (line_when - now > 24*60*60)
781 _log ("Deleted SHA1 cookie from %" G_GUINT64_FORMAT " seconds in the future", line_when - now);
786 /* Discard entry if it's older than 15 minutes ("can be fairly short") */
787 if (now - line_when > 15*60)
795 changed_file = FALSE;
799 g_string_append_printf (new_contents,
800 "%d %" G_GUINT64_FORMAT " %s\n",
804 max_line_id = MAX (line_id, max_line_id);
805 /* Only reuse entry if not older than 10 minutes.
807 * (We need a bit of grace time compared to 15 minutes above.. otherwise
808 * there's a race where we reuse the 14min59.9 secs old entry and a
809 * split-second later another server purges the now 15 minute old entry.)
811 if (now - line_when < 10 * 60)
816 use_cookie = tokens[2]; /* steal memory */
824 } /* for each line */
831 *out_cookie = use_cookie;
837 *out_id = max_line_id + 1;
838 raw_cookie = random_blob (32);
839 *out_cookie = hexencode (raw_cookie, 32);
842 g_string_append_printf (new_contents,
843 "%d %" G_GUINT64_FORMAT " %s\n",
845 (guint64) time (NULL),
850 /* and now actually write the cookie file if there are changes (this is atomic) */
853 if (!g_file_set_contents (path,
860 g_free (*out_cookie);
872 if (!keyring_release_lock (path, lock_fd, &local_error))
878 *error = local_error;
882 g_prefix_error (error,
883 _("(Additionally, releasing the lock for `%s' also failed: %s) "),
885 local_error->message);
890 g_error_free (local_error);
895 g_free (keyring_dir);
899 if (new_contents != NULL)
900 g_string_free (new_contents, TRUE);
905 /* ---------------------------------------------------------------------------------------------------- */
908 generate_sha1 (const gchar *server_challenge,
909 const gchar *client_challenge,
915 str = g_string_new (server_challenge);
916 g_string_append_c (str, ':');
917 g_string_append (str, client_challenge);
918 g_string_append_c (str, ':');
919 g_string_append (str, cookie);
920 sha1 = g_compute_checksum_for_string (G_CHECKSUM_SHA1, str->str, -1);
921 g_string_free (str, TRUE);
926 /* ---------------------------------------------------------------------------------------------------- */
928 static GDBusAuthMechanismState
929 mechanism_server_get_state (GDBusAuthMechanism *mechanism)
931 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
933 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
934 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
936 return m->priv->state;
940 mechanism_server_initiate (GDBusAuthMechanism *mechanism,
941 const gchar *initial_response,
942 gsize initial_response_len)
944 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
946 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
947 g_return_if_fail (!m->priv->is_server && !m->priv->is_client);
949 m->priv->is_server = TRUE;
950 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
952 if (initial_response != NULL && strlen (initial_response) > 0)
958 uid = g_ascii_strtoll (initial_response, &endp, 10);
961 if (uid == getuid ())
963 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
966 #elif defined(G_OS_WIN32)
968 sid = _g_dbus_win32_get_user_sid ();
969 if (g_strcmp0 (initial_response, sid) == 0)
970 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
973 #error Please implement for your OS
979 mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
983 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
985 const gchar *client_challenge;
986 const gchar *alleged_sha1;
989 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
990 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
991 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
996 tokens = g_strsplit (data, " ", 0);
997 if (g_strv_length (tokens) != 2)
999 g_warning ("Malformed data `%s'", data);
1000 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1004 client_challenge = tokens[0];
1005 alleged_sha1 = tokens[1];
1007 sha1 = generate_sha1 (m->priv->server_challenge, client_challenge, m->priv->cookie);
1009 if (g_strcmp0 (sha1, alleged_sha1) == 0)
1011 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1015 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1019 g_strfreev (tokens);
1024 mechanism_server_data_send (GDBusAuthMechanism *mechanism,
1025 gsize *out_data_len)
1027 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1030 const gchar *cookie_context;
1033 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1034 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1035 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1039 /* TODO: use GDBusAuthObserver here to get the cookie context to use? */
1040 cookie_context = "org_gtk_gdbus_general";
1044 if (!keyring_generate_entry (cookie_context,
1049 g_warning ("Error adding entry to keyring: %s", error->message);
1050 g_error_free (error);
1051 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1055 m->priv->server_challenge = random_ascii_string (16);
1056 s = g_strdup_printf ("%s %d %s",
1059 m->priv->server_challenge);
1061 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1068 mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
1070 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1072 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1073 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1074 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_REJECTED, NULL);
1076 /* can never end up here because we are never in the REJECTED state */
1077 g_assert_not_reached ();
1083 mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
1085 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1087 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1088 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
1090 m->priv->is_server = FALSE;
1093 /* ---------------------------------------------------------------------------------------------------- */
1095 static GDBusAuthMechanismState
1096 mechanism_client_get_state (GDBusAuthMechanism *mechanism)
1098 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1100 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1101 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1103 return m->priv->state;
1107 mechanism_client_initiate (GDBusAuthMechanism *mechanism,
1108 gsize *out_initial_response_len)
1110 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1111 gchar *initial_response;
1113 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1114 g_return_val_if_fail (!m->priv->is_server && !m->priv->is_client, NULL);
1116 m->priv->is_client = TRUE;
1117 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1119 *out_initial_response_len = -1;
1122 initial_response = g_strdup_printf ("%" G_GINT64_FORMAT, (gint64) getuid ());
1123 #elif defined (G_OS_WIN32)
1124 initial_response = _g_dbus_win32_get_user_sid ();
1126 #error Please implement for your OS
1128 g_assert (initial_response != NULL);
1130 return initial_response;
1134 mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
1138 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1140 const gchar *cookie_context;
1142 const gchar *server_challenge;
1143 gchar *client_challenge;
1149 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1150 g_return_if_fail (m->priv->is_client && !m->priv->is_server);
1151 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
1155 client_challenge = NULL;
1157 tokens = g_strsplit (data, " ", 0);
1158 if (g_strv_length (tokens) != 3)
1160 g_warning ("Malformed data `%s'", data);
1161 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1165 cookie_context = tokens[0];
1166 cookie_id = g_ascii_strtoll (tokens[1], &endp, 10);
1169 g_warning ("Malformed cookie_id `%s'", tokens[1]);
1170 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1173 server_challenge = tokens[2];
1176 cookie = keyring_lookup_entry (cookie_context, cookie_id, &error);
1179 g_warning ("Problems looking up entry in keyring: %s", error->message);
1180 g_error_free (error);
1181 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1185 client_challenge = random_ascii_string (16);
1186 sha1 = generate_sha1 (server_challenge, client_challenge, cookie);
1187 m->priv->to_send = g_strdup_printf ("%s %s", client_challenge, sha1);
1189 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
1192 g_strfreev (tokens);
1194 g_free (client_challenge);
1198 mechanism_client_data_send (GDBusAuthMechanism *mechanism,
1199 gsize *out_data_len)
1201 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1203 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1204 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, NULL);
1205 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1207 g_assert (m->priv->to_send != NULL);
1209 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1211 return g_strdup (m->priv->to_send);
1215 mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
1217 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1219 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1220 g_return_if_fail (m->priv->is_client && !m->priv->is_server);
1222 m->priv->is_client = FALSE;
1225 /* ---------------------------------------------------------------------------------------------------- */