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"
44 struct _GDBusAuthMechanismSha1Private
48 GDBusAuthMechanismState state;
50 /* used on the client side */
53 /* used on the server side */
55 gchar *server_challenge;
58 static gint mechanism_get_priority (void);
59 static const gchar *mechanism_get_name (void);
61 static gboolean mechanism_is_supported (GDBusAuthMechanism *mechanism);
62 static gchar *mechanism_encode_data (GDBusAuthMechanism *mechanism,
66 static gchar *mechanism_decode_data (GDBusAuthMechanism *mechanism,
70 static GDBusAuthMechanismState mechanism_server_get_state (GDBusAuthMechanism *mechanism);
71 static void mechanism_server_initiate (GDBusAuthMechanism *mechanism,
72 const gchar *initial_response,
73 gsize initial_response_len);
74 static void mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
77 static gchar *mechanism_server_data_send (GDBusAuthMechanism *mechanism,
79 static gchar *mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism);
80 static void mechanism_server_shutdown (GDBusAuthMechanism *mechanism);
81 static GDBusAuthMechanismState mechanism_client_get_state (GDBusAuthMechanism *mechanism);
82 static gchar *mechanism_client_initiate (GDBusAuthMechanism *mechanism,
83 gsize *out_initial_response_len);
84 static void mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
87 static gchar *mechanism_client_data_send (GDBusAuthMechanism *mechanism,
89 static void mechanism_client_shutdown (GDBusAuthMechanism *mechanism);
91 /* ---------------------------------------------------------------------------------------------------- */
93 G_DEFINE_TYPE (GDBusAuthMechanismSha1, _g_dbus_auth_mechanism_sha1, G_TYPE_DBUS_AUTH_MECHANISM);
95 /* ---------------------------------------------------------------------------------------------------- */
98 _g_dbus_auth_mechanism_sha1_finalize (GObject *object)
100 GDBusAuthMechanismSha1 *mechanism = G_DBUS_AUTH_MECHANISM_SHA1 (object);
102 g_free (mechanism->priv->to_send);
104 g_free (mechanism->priv->cookie);
105 g_free (mechanism->priv->server_challenge);
107 if (G_OBJECT_CLASS (_g_dbus_auth_mechanism_sha1_parent_class)->finalize != NULL)
108 G_OBJECT_CLASS (_g_dbus_auth_mechanism_sha1_parent_class)->finalize (object);
112 _g_dbus_auth_mechanism_sha1_class_init (GDBusAuthMechanismSha1Class *klass)
114 GObjectClass *gobject_class;
115 GDBusAuthMechanismClass *mechanism_class;
117 g_type_class_add_private (klass, sizeof (GDBusAuthMechanismSha1Private));
119 gobject_class = G_OBJECT_CLASS (klass);
120 gobject_class->finalize = _g_dbus_auth_mechanism_sha1_finalize;
122 mechanism_class = G_DBUS_AUTH_MECHANISM_CLASS (klass);
123 mechanism_class->get_priority = mechanism_get_priority;
124 mechanism_class->get_name = mechanism_get_name;
125 mechanism_class->is_supported = mechanism_is_supported;
126 mechanism_class->encode_data = mechanism_encode_data;
127 mechanism_class->decode_data = mechanism_decode_data;
128 mechanism_class->server_get_state = mechanism_server_get_state;
129 mechanism_class->server_initiate = mechanism_server_initiate;
130 mechanism_class->server_data_receive = mechanism_server_data_receive;
131 mechanism_class->server_data_send = mechanism_server_data_send;
132 mechanism_class->server_get_reject_reason = mechanism_server_get_reject_reason;
133 mechanism_class->server_shutdown = mechanism_server_shutdown;
134 mechanism_class->client_get_state = mechanism_client_get_state;
135 mechanism_class->client_initiate = mechanism_client_initiate;
136 mechanism_class->client_data_receive = mechanism_client_data_receive;
137 mechanism_class->client_data_send = mechanism_client_data_send;
138 mechanism_class->client_shutdown = mechanism_client_shutdown;
142 _g_dbus_auth_mechanism_sha1_init (GDBusAuthMechanismSha1 *mechanism)
144 mechanism->priv = G_TYPE_INSTANCE_GET_PRIVATE (mechanism,
145 G_TYPE_DBUS_AUTH_MECHANISM_SHA1,
146 GDBusAuthMechanismSha1Private);
149 /* ---------------------------------------------------------------------------------------------------- */
152 mechanism_get_priority (void)
158 mechanism_get_name (void)
160 return "DBUS_COOKIE_SHA1";
164 mechanism_is_supported (GDBusAuthMechanism *mechanism)
166 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), FALSE);
171 mechanism_encode_data (GDBusAuthMechanism *mechanism,
181 mechanism_decode_data (GDBusAuthMechanism *mechanism,
189 /* ---------------------------------------------------------------------------------------------------- */
195 ret = g_random_int_range (0, 60);
206 random_ascii_string (guint len)
211 challenge = g_string_new (NULL);
212 for (n = 0; n < len; n++)
213 g_string_append_c (challenge, random_ascii ());
214 return g_string_free (challenge, FALSE);
218 random_blob (guint len)
223 challenge = g_string_new (NULL);
224 for (n = 0; n < len; n++)
225 g_string_append_c (challenge, g_random_int_range (0, 256));
226 return g_string_free (challenge, FALSE);
229 /* ---------------------------------------------------------------------------------------------------- */
231 /* ensure keyring dir exists and permissions are correct */
233 ensure_keyring_directory (GError **error)
238 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
240 e = g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR");
247 path = g_build_filename (g_get_home_dir (),
252 if (g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
254 if (g_getenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION") == NULL)
258 if (stat (path, &statbuf) != 0)
262 g_io_error_from_errno (errno),
263 _("Error statting directory `%s': %s"),
270 if ((statbuf.st_mode & 0777) != 0700)
275 _("Permissions on directory `%s' are malformed. Expected mode 0700, got 0%o"),
277 statbuf.st_mode & 0777);
283 #error Please implement permission checking on non-UNIX platforms
289 if (g_mkdir (path, 0700) != 0)
293 g_io_error_from_errno (errno),
294 _("Error creating directory `%s': %s"),
306 /* ---------------------------------------------------------------------------------------------------- */
309 append_nibble (GString *s, gint val)
311 g_string_append_c (s, val >= 10 ? ('a' + val - 10) : ('0' + val));
315 hexencode (const gchar *str,
324 s = g_string_new (NULL);
325 for (n = 0; n < len; n++)
331 val = ((const guchar *) str)[n];
332 upper_nibble = val >> 4;
333 lower_nibble = val & 0x0f;
335 append_nibble (s, upper_nibble);
336 append_nibble (s, lower_nibble);
339 return g_string_free (s, FALSE);
342 /* ---------------------------------------------------------------------------------------------------- */
344 /* looks up an entry in the keyring */
346 keyring_lookup_entry (const gchar *cookie_context,
357 g_return_val_if_fail (cookie_context != NULL, FALSE);
358 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
365 keyring_dir = ensure_keyring_directory (error);
366 if (keyring_dir == NULL)
369 path = g_build_filename (keyring_dir, cookie_context, NULL);
371 if (!g_file_get_contents (path,
376 g_prefix_error (error,
377 _("Error opening keyring `%s' for reading: "),
381 g_assert (contents != NULL);
383 lines = g_strsplit (contents, "\n", 0);
384 for (n = 0; lines[n] != NULL; n++)
386 const gchar *line = lines[n];
395 tokens = g_strsplit (line, " ", 0);
396 if (g_strv_length (tokens) != 3)
401 _("Line %d of the keyring at `%s' with content `%s' is malformed"),
409 line_id = g_ascii_strtoll (tokens[0], &endp, 10);
415 _("First token of line %d of the keyring at `%s' with content `%s' is malformed"),
423 line_when = g_ascii_strtoll (tokens[1], &endp, 10);
429 _("Second token of line %d of the keyring at `%s' with content `%s' is malformed"),
437 if (line_id == cookie_id)
440 ret = tokens[2]; /* steal pointer */
449 /* BOOH, didn't find the cookie */
453 _("Didn't find cookie with id %d in the keyring at `%s'"),
458 g_free (keyring_dir);
465 /* function for logging important events that the system administrator should take notice of */
467 _log (const gchar *message,
473 va_start (var_args, message);
474 s = g_strdup_vprintf (message, var_args);
477 /* TODO: might want to send this to syslog instead */
478 g_printerr ("GDBus-DBUS_COOKIE_SHA1: %s\n", s);
483 keyring_acquire_lock (const gchar *path,
489 guint num_create_tries;
491 g_return_val_if_fail (path != NULL, FALSE);
492 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
495 lock = g_strdup_printf ("%s.lock", path);
497 /* This is what the D-Bus spec says
499 * Create a lockfile name by appending ".lock" to the name of the
500 * cookie file. The server should attempt to create this file using
501 * O_CREAT | O_EXCL. If file creation fails, the lock
502 * fails. Servers should retry for a reasonable period of time,
503 * then they may choose to delete an existing lock to keep users
504 * from having to manually delete a stale lock. [1]
506 * [1] : Lockfiles are used instead of real file locking fcntl() because
507 * real locking implementations are still flaky on network filesystems
510 num_create_tries = 0;
515 while (g_file_test (lock, G_FILE_TEST_EXISTS))
517 /* sleep 10ms, then try again */
522 /* ok, we slept 50*10ms = 0.5 seconds.. Conclude that the lock-file must be
523 * stale (nuke the it from orbit)
525 if (g_unlink (lock) != 0)
529 g_io_error_from_errno (errno),
530 _("Error deleting stale lock-file `%s': %s"),
535 _log ("Deleted stale lock-file `%s'", lock);
540 ret = g_open (lock, O_CREAT |
550 /* EEXIST: pathname already exists and O_CREAT and O_EXCL were used. */
551 if (errno == EEXISTS)
554 if (num_create_tries < 5)
560 g_io_error_from_errno (errno),
561 _("Error creating lock-file `%s': %s"),
573 keyring_release_lock (const gchar *path,
580 g_return_val_if_fail (path != NULL, FALSE);
581 g_return_val_if_fail (lock_fd != -1, FALSE);
582 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
585 lock = g_strdup_printf ("%s.lock", path);
586 if (g_unlink (lock) != 0)
590 g_io_error_from_errno (errno),
591 _("Error unlinking lock-file `%s': %s"),
596 if (close (lock_fd) != 0)
600 g_io_error_from_errno (errno),
601 _("Error closing (unlinked) lock-file `%s': %s"),
615 /* adds an entry to the keyring, taking care of locking and deleting stale/future entries */
617 keyring_generate_entry (const gchar *cookie_context,
629 GString *new_contents;
634 gboolean changed_file;
637 g_return_val_if_fail (cookie_context != NULL, FALSE);
638 g_return_val_if_fail (out_id != NULL, FALSE);
639 g_return_val_if_fail (out_cookie != NULL, FALSE);
640 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
651 keyring_dir = ensure_keyring_directory (error);
652 if (keyring_dir == NULL)
655 path = g_build_filename (keyring_dir, cookie_context, NULL);
657 lock_fd = keyring_acquire_lock (path, error);
663 if (!g_file_get_contents (path,
668 if (local_error->domain == G_FILE_ERROR && local_error->code == G_FILE_ERROR_NOENT)
670 /* file doesn't have to exist */
671 g_error_free (local_error);
675 g_propagate_prefixed_error (error,
677 _("Error opening keyring `%s' for writing: "),
683 new_contents = g_string_new (NULL);
685 changed_file = FALSE;
688 if (contents != NULL)
691 lines = g_strsplit (contents, "\n", 0);
692 for (n = 0; lines[n] != NULL; n++)
694 const gchar *line = lines[n];
704 tokens = g_strsplit (line, " ", 0);
705 if (g_strv_length (tokens) != 3)
710 _("Line %d of the keyring at `%s' with content `%s' is malformed"),
718 line_id = g_ascii_strtoll (tokens[0], &endp, 10);
724 _("First token of line %d of the keyring at `%s' with content `%s' is malformed"),
732 line_when = g_ascii_strtoll (tokens[1], &endp, 10);
738 _("Second token of line %d of the keyring at `%s' with content `%s' is malformed"),
748 * Once the lockfile has been created, the server loads the
749 * cookie file. It should then delete any cookies that are
750 * old (the timeout can be fairly short), or more than a
751 * reasonable time in the future (so that cookies never
752 * accidentally become permanent, if the clock was set far
753 * into the future at some point). If no recent keys remain,
754 * the server may generate a new key.
760 /* Oddball case: entry is more recent than our current wall-clock time..
761 * This is OK, it means that another server on another machine but with
762 * same $HOME wrote the entry.
764 * So discard the entry if it's more than 1 day in the future ("reasonable
765 * time in the future").
767 if (line_when - now > 24*60*60)
770 _log ("Deleted SHA1 cookie from %" G_GUINT64_FORMAT " seconds in the future", line_when - now);
775 /* Discard entry if it's older than 15 minutes ("can be fairly short") */
776 if (now - line_when > 15*60)
784 changed_file = FALSE;
788 g_string_append_printf (new_contents,
789 "%d %" G_GUINT64_FORMAT " %s\n",
793 max_line_id = MAX (line_id, max_line_id);
794 /* Only reuse entry if not older than 10 minutes.
796 * (We need a bit of grace time compared to 15 minutes above.. otherwise
797 * there's a race where we reuse the 14min59.9 secs old entry and a
798 * split-second later another server purges the now 15 minute old entry.)
800 if (now - line_when < 10 * 60)
805 use_cookie = tokens[2]; /* steal memory */
813 } /* for each line */
820 *out_cookie = use_cookie;
826 *out_id = max_line_id + 1;
827 raw_cookie = random_blob (32);
828 *out_cookie = hexencode (raw_cookie, 32);
831 g_string_append_printf (new_contents,
832 "%d %" G_GUINT64_FORMAT " %s\n",
834 (guint64) time (NULL),
839 /* and now actually write the cookie file if there are changes (this is atomic) */
842 if (!g_file_set_contents (path,
849 g_free (*out_cookie);
861 if (!keyring_release_lock (path, lock_fd, &local_error))
867 *error = local_error;
871 g_prefix_error (error,
872 _("(Additionally, releasing the lock for `%s' also failed: %s) "),
874 local_error->message);
879 g_error_free (local_error);
884 g_free (keyring_dir);
888 if (new_contents != NULL)
889 g_string_free (new_contents, TRUE);
894 /* ---------------------------------------------------------------------------------------------------- */
897 generate_sha1 (const gchar *server_challenge,
898 const gchar *client_challenge,
904 str = g_string_new (server_challenge);
905 g_string_append_c (str, ':');
906 g_string_append (str, client_challenge);
907 g_string_append_c (str, ':');
908 g_string_append (str, cookie);
909 sha1 = g_compute_checksum_for_string (G_CHECKSUM_SHA1, str->str, -1);
910 g_string_free (str, TRUE);
915 /* ---------------------------------------------------------------------------------------------------- */
917 static GDBusAuthMechanismState
918 mechanism_server_get_state (GDBusAuthMechanism *mechanism)
920 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
922 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
923 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
925 return m->priv->state;
929 mechanism_server_initiate (GDBusAuthMechanism *mechanism,
930 const gchar *initial_response,
931 gsize initial_response_len)
933 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
935 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
936 g_return_if_fail (!m->priv->is_server && !m->priv->is_client);
938 m->priv->is_server = TRUE;
939 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
941 if (initial_response != NULL && strlen (initial_response) > 0)
947 uid = g_ascii_strtoll (initial_response, &endp, 10);
950 if (uid == getuid ())
952 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
955 #elif defined(G_OS_WIN32)
956 GCredentials *credentials;
957 credentials = g_credentials_new_for_process ();
958 if (g_strcmp0 (g_credentials_get_windows_user (credentials), initial_response) == 0)
959 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
960 g_object_unref (credentials);
962 #error Please implement for your OS
968 mechanism_server_data_receive (GDBusAuthMechanism *mechanism,
972 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
974 const gchar *client_challenge;
975 const gchar *alleged_sha1;
978 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
979 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
980 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
985 tokens = g_strsplit (data, " ", 0);
986 if (g_strv_length (tokens) != 2)
988 g_warning ("Malformed data `%s'", data);
989 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
993 client_challenge = tokens[0];
994 alleged_sha1 = tokens[1];
996 sha1 = generate_sha1 (m->priv->server_challenge, client_challenge, m->priv->cookie);
998 if (g_strcmp0 (sha1, alleged_sha1) == 0)
1000 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1004 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1008 g_strfreev (tokens);
1013 mechanism_server_data_send (GDBusAuthMechanism *mechanism,
1014 gsize *out_data_len)
1016 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1019 const gchar *cookie_context;
1022 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1023 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1024 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1028 /* TODO: use GDBusAuthObserver here to get the cookie context to use? */
1029 cookie_context = "org_gtk_gdbus_general";
1032 if (!keyring_generate_entry (cookie_context,
1037 g_warning ("Error adding entry to keyring: %s", error->message);
1038 g_error_free (error);
1039 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1043 m->priv->server_challenge = random_ascii_string (16);
1044 s = g_strdup_printf ("%s %d %s",
1047 m->priv->server_challenge);
1049 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1056 mechanism_server_get_reject_reason (GDBusAuthMechanism *mechanism)
1058 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1060 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1061 g_return_val_if_fail (m->priv->is_server && !m->priv->is_client, NULL);
1062 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_REJECTED, NULL);
1064 /* can never end up here because we are never in the REJECTED state */
1065 g_assert_not_reached ();
1071 mechanism_server_shutdown (GDBusAuthMechanism *mechanism)
1073 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1075 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1076 g_return_if_fail (m->priv->is_server && !m->priv->is_client);
1078 m->priv->is_server = FALSE;
1081 /* ---------------------------------------------------------------------------------------------------- */
1083 static GDBusAuthMechanismState
1084 mechanism_client_get_state (GDBusAuthMechanism *mechanism)
1086 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1088 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1089 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, G_DBUS_AUTH_MECHANISM_STATE_INVALID);
1091 return m->priv->state;
1095 mechanism_client_initiate (GDBusAuthMechanism *mechanism,
1096 gsize *out_initial_response_len)
1098 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1099 gchar *initial_response;
1101 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1102 g_return_val_if_fail (!m->priv->is_server && !m->priv->is_client, NULL);
1104 m->priv->is_client = TRUE;
1105 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA;
1107 *out_initial_response_len = -1;
1110 initial_response = g_strdup_printf ("%" G_GINT64_FORMAT, (gint64) getuid ());
1111 #elif defined (G_OS_WIN32)
1113 GCredentials *credentials;
1114 credentials = g_credentials_new_for_process ();
1115 initial_response = g_strdup (g_credentials_get_windows_user (credentials));
1116 g_object_unref (credentials);
1120 g_assert (initial_response != NULL);
1122 return initial_response;
1126 mechanism_client_data_receive (GDBusAuthMechanism *mechanism,
1130 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1132 const gchar *cookie_context;
1134 const gchar *server_challenge;
1135 gchar *client_challenge;
1141 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1142 g_return_if_fail (m->priv->is_client && !m->priv->is_server);
1143 g_return_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_WAITING_FOR_DATA);
1147 client_challenge = NULL;
1149 tokens = g_strsplit (data, " ", 0);
1150 if (g_strv_length (tokens) != 3)
1152 g_warning ("Malformed data `%s'", data);
1153 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1157 cookie_context = tokens[0];
1158 cookie_id = g_ascii_strtoll (tokens[1], &endp, 10);
1161 g_warning ("Malformed cookie_id `%s'", tokens[1]);
1162 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1165 server_challenge = tokens[2];
1168 cookie = keyring_lookup_entry (cookie_context, cookie_id, &error);
1171 g_warning ("Problems looking up entry in keyring: %s", error->message);
1172 g_error_free (error);
1173 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
1177 client_challenge = random_ascii_string (16);
1178 sha1 = generate_sha1 (server_challenge, client_challenge, cookie);
1179 m->priv->to_send = g_strdup_printf ("%s %s", client_challenge, sha1);
1181 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
1184 g_strfreev (tokens);
1186 g_free (client_challenge);
1190 mechanism_client_data_send (GDBusAuthMechanism *mechanism,
1191 gsize *out_data_len)
1193 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1195 g_return_val_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism), NULL);
1196 g_return_val_if_fail (m->priv->is_client && !m->priv->is_server, NULL);
1197 g_return_val_if_fail (m->priv->state == G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND, NULL);
1199 g_assert (m->priv->to_send != NULL);
1201 m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_ACCEPTED;
1203 return g_strdup (m->priv->to_send);
1207 mechanism_client_shutdown (GDBusAuthMechanism *mechanism)
1209 GDBusAuthMechanismSha1 *m = G_DBUS_AUTH_MECHANISM_SHA1 (mechanism);
1211 g_return_if_fail (G_IS_DBUS_AUTH_MECHANISM_SHA1 (mechanism));
1212 g_return_if_fail (m->priv->is_client && !m->priv->is_server);
1214 m->priv->is_client = FALSE;
1217 /* ---------------------------------------------------------------------------------------------------- */
1219 #define __G_DBUS_AUTH_MECHANISM_SHA1_C__
1220 #include "gioaliasdef.c"