Debian Codegraph @

src/goaidentity/goakerberosidentitymanager.c

raw symbols SCIP
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /*
3 * Copyright © 2012 – 2017 Red Hat, Inc.
4 *
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.
9 *
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.
14 *
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "config.h"
20
21 #define _GNU_SOURCE 1
22
23 #include "goakerberosidentitymanager.h"
24 #include "goaidentitymanager.h"
25 #include "goaidentitymanagererror.h"
26 #include "goaidentitymanagerprivate.h"
27 #include "goakerberosidentityinquiry.h"
28
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 #include <glib/gi18n.h>
38 #include <glib/gstdio.h>
39 #include <glib-unix.h>
40 #include <gio/gio.h>
41
42 #include <krb5.h>
43
44 #ifdef __linux__
45 #include <keyutils.h>
46 #include "goalinuxnotificationstream.h"
47 #include "goalinuxwatchqueue-priv.h"
48 #endif /* __linux__ */
49
50 struct _GoaKerberosIdentityManager
51 {
52 GObject parent_instance;
53
54 GHashTable *identities;
55 GHashTable *expired_identities;
56 GHashTable *identities_by_realm;
57 GCancellable *scheduler_cancellable;
58 GThreadPool *thread_pool;
59
60 krb5_context kerberos_context;
61 GFileMonitor *credentials_cache_file_monitor;
62 char *credentials_cache_type;
63
64 GMutex scheduler_job_lock;
65 GCond scheduler_job_unblocked;
66 gboolean is_blocking_scheduler_job;
67
68 volatile int pending_refresh_count;
69
70 guint credentials_cache_keyring_notification_id;
71 guint credentials_cache_polling_timeout_id;
72 };
73
74 typedef enum
75 {
76 OPERATION_TYPE_REFRESH,
77 OPERATION_TYPE_GET_IDENTITY,
78 OPERATION_TYPE_LIST,
79 OPERATION_TYPE_RENEW,
80 OPERATION_TYPE_SIGN_IN,
81 OPERATION_TYPE_SIGN_OUT
82 } OperationType;
83
84 typedef struct
85 {
86 GCancellable *cancellable;
87 GCancellable *scheduler_cancellable;
88 GoaKerberosIdentityManager *manager;
89 OperationType type;
90 GMainContext *context;
91 GTask *task;
92
93 union
94 {
95 GoaIdentity *identity;
96 struct
97 {
98 char *identifier;
99 gconstpointer initial_password;
100 char *preauth_source;
101 GoaIdentitySignInFlags sign_in_flags;
102 GoaIdentityInquiry *inquiry;
103 GoaIdentityInquiryFunc inquiry_func;
104 gpointer inquiry_data;
105 GMutex inquiry_lock;
106 GCond inquiry_finished_condition;
107 volatile gboolean is_inquiring;
108 };
109 };
110 } Operation;
111
112 typedef struct
113 {
114 GoaKerberosIdentityManager *manager;
115 GoaIdentity *identity;
116 } IdentitySignalWork;
117
118 static GoaIdentityManager *goa_kerberos_identity_manager_singleton;
119
120 static void identity_manager_interface_init (GoaIdentityManagerInterface *
121 interface);
122 static void initable_interface_init (GInitableIface *interface);
123
124 static void on_identity_expired (GoaIdentity *identity,
125 GoaKerberosIdentityManager *self);
126
127 G_DEFINE_TYPE_WITH_CODE (GoaKerberosIdentityManager,
128 goa_kerberos_identity_manager,
129 G_TYPE_OBJECT,
130 G_IMPLEMENT_INTERFACE (GOA_TYPE_IDENTITY_MANAGER,
131 identity_manager_interface_init)
132 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
133 initable_interface_init));
134 #define FALLBACK_POLLING_INTERVAL 5
135
136 static Operation *
137 operation_new (GoaKerberosIdentityManager *self,
138 GCancellable *cancellable,
139 OperationType type,
140 GTask *task)
141 {
142 Operation *operation;
143
144 operation = g_slice_new0 (Operation);
145
146 operation->manager = self;
147 operation->scheduler_cancellable = g_object_ref (self->scheduler_cancellable);
148
149 operation->type = type;
150
151 if (cancellable == NULL)
152 cancellable = g_cancellable_new ();
153 else
154 g_object_ref (cancellable);
155 operation->cancellable = cancellable;
156
157 operation->context = g_main_context_ref_thread_default ();
158
159 if (task != NULL)
160 g_object_ref (task);
161 operation->task = task;
162
163 operation->identity = NULL;
164
165 return operation;
166 }
167
168 static void
169 operation_free (Operation *operation)
170 {
171 g_clear_object (&operation->cancellable);
172 g_clear_object (&operation->scheduler_cancellable);
173 g_clear_pointer (&operation->context, g_main_context_unref);
174
175 if (operation->type != OPERATION_TYPE_SIGN_IN &&
176 operation->type != OPERATION_TYPE_GET_IDENTITY)
177 {
178 g_clear_object (&operation->identity);
179 }
180 else
181 {
182 g_clear_pointer (&operation->identifier, g_free);
183 g_clear_pointer (&operation->preauth_source, g_free);
184 }
185 g_clear_object (&operation->task);
186
187 g_slice_free (Operation, operation);
188 }
189
190 static GSource *
191 goa_kerberos_identity_manager_keyring_source_new (GError **error)
192 {
193 GSource *ret = NULL;
194 gint fds[2] = { -1, -1 };
195
196 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
197
198 #if !defined(__linux__)
199 {
200 g_set_error_literal (error, G_UNIX_ERROR, 0, "KEYRING only supported on Linux");
201 return NULL;
202 }
203 #elif !defined(HAVE_PIPE2)
204 {
205 g_set_error_literal (error, G_UNIX_ERROR, 0, "pipe2(2) not supported");
206 return NULL;
207 }
208 #else
209 {
210 g_autoptr (GInputStream) stream = NULL;
211 gint error_code;
212
213 error_code = pipe2 (fds, O_NOTIFICATION_PIPE);
214 if (error_code == -1)
215 {
216 const gchar *error_str;
217
218 error_str = g_strerror (errno);
219 g_set_error_literal (error, G_UNIX_ERROR, 0, error_str);
220 goto out;
221 }
222
223 error_code = ioctl (fds[0], IOC_WATCH_QUEUE_SET_SIZE, WATCH_QUEUE_BUFFER_SIZE);
224 if (error_code == -1)
225 {
226 const gchar *error_str;
227
228 error_str = g_strerror (errno);
229 g_set_error_literal (error, G_UNIX_ERROR, 0, error_str);
230 goto out;
231 }
232
233 error_code = ioctl(fds[0], IOC_WATCH_QUEUE_SET_FILTER, &watch_queue_notification_filter);
234 if (error_code == -1)
235 {
236 const gchar *error_str;
237
238 error_str = g_strerror (errno);
239 g_set_error_literal (error, G_UNIX_ERROR, 0, error_str);
240 goto out;
241 }
242
243 error_code = keyctl_watch_key (KEY_SPEC_SESSION_KEYRING, fds[0], 0x01);
244 if (error_code == -1)
245 {
246 const gchar *error_str;
247
248 error_str = g_strerror (errno);
249 g_set_error_literal (error, G_UNIX_ERROR, 0, error_str);
250 goto out;
251 }
252
253 error_code = keyctl_watch_key (KEY_SPEC_USER_KEYRING, fds[0], 0x02);
254 if (error_code == -1)
255 {
256 const gchar *error_str;
257
258 error_str = g_strerror (errno);
259 g_set_error_literal (error, G_UNIX_ERROR, 0, error_str);
260 goto out;
261 }
262
263 stream = goa_linux_notification_stream_new (fds[0], fds[1]);
264 ret = g_pollable_input_stream_create_source (G_POLLABLE_INPUT_STREAM (stream), NULL);
265 }
266 #endif
267
268 out:
269 if (ret == NULL)
270 {
271 g_close (fds[0], NULL);
272 g_close (fds[1], NULL);
273 }
274
275 return ret;
276 }
277
278 typedef struct {
279 GSourceFunc func;
280 gboolean ret_val;
281 gpointer data;
282 GDestroyNotify notify;
283
284 GMutex ack_lock;
285 GCond ack_condition;
286 gboolean ack;
287 } MainLoopProxy;
288
289 static gboolean
290 mainloop_proxy_func (gpointer data)
291 {
292 MainLoopProxy *proxy = data;
293
294 proxy->ret_val = proxy->func (proxy->data);
295
296 if (proxy->notify)
297 proxy->notify (proxy->data);
298
299 g_mutex_lock (&proxy->ack_lock);
300 proxy->ack = TRUE;
301 g_cond_signal (&proxy->ack_condition);
302 g_mutex_unlock (&proxy->ack_lock);
303
304 return FALSE;
305 }
306
307 static void
308 mainloop_proxy_free (MainLoopProxy *proxy)
309 {
310 g_mutex_clear (&proxy->ack_lock);
311 g_cond_clear (&proxy->ack_condition);
312 g_free (proxy);
313 }
314
315 static gboolean
316 goa_kerberos_identify_manager_send_to_context (GMainContext *context,
317 GSourceFunc func,
318 gpointer user_data,
319 GDestroyNotify notify)
320 {
321 GSource *source;
322 MainLoopProxy *proxy;
323 gboolean ret_val;
324
325 g_return_val_if_fail (context != NULL, FALSE);
326 g_return_val_if_fail (func != NULL, FALSE);
327
328 proxy = g_new0 (MainLoopProxy, 1);
329 proxy->func = func;
330 proxy->data = user_data;
331 proxy->notify = notify;
332 g_mutex_init (&proxy->ack_lock);
333 g_cond_init (&proxy->ack_condition);
334 g_mutex_lock (&proxy->ack_lock);
335
336 source = g_idle_source_new ();
337 g_source_set_priority (source, G_PRIORITY_DEFAULT);
338 g_source_set_callback (source, mainloop_proxy_func, proxy, NULL);
339 g_source_set_name (source, "[goa] mainloop_proxy_func");
340
341 g_source_attach (source, context);
342 g_source_unref (source);
343
344 while (!proxy->ack)
345 g_cond_wait (&proxy->ack_condition, &proxy->ack_lock);
346 g_mutex_unlock (&proxy->ack_lock);
347
348 ret_val = proxy->ret_val;
349 mainloop_proxy_free (proxy);
350
351 return ret_val;
352 }
353
354 static void
355 schedule_refresh (GoaKerberosIdentityManager *self)
356 {
357 Operation *operation;
358
359 g_atomic_int_inc (&self->pending_refresh_count);
360
361 operation = operation_new (self, NULL, OPERATION_TYPE_REFRESH, NULL);
362 g_thread_pool_push (self->thread_pool, operation, NULL);
363 }
364
365 static IdentitySignalWork *
366 identity_signal_work_new (GoaKerberosIdentityManager *self,
367 GoaIdentity *identity)
368 {
369 IdentitySignalWork *work;
370
371 work = g_slice_new (IdentitySignalWork);
372 work->manager = self;
373 work->identity = g_object_ref (identity);
374
375 return work;
376 }
377
378 static void
379 identity_signal_work_free (IdentitySignalWork *work)
380 {
381 g_object_unref (work->identity);
382 g_slice_free (IdentitySignalWork, work);
383 }
384
385 static void
386 do_identity_signal_expired_work (IdentitySignalWork *work)
387 {
388 GoaKerberosIdentityManager *self = work->manager;
389 GoaIdentity *identity = work->identity;
390
391 g_debug ("GoaKerberosIdentityManager: identity expired");
392 _goa_identity_manager_emit_identity_expired (GOA_IDENTITY_MANAGER (self), identity);
393 }
394
395 static void
396 on_identity_expired (GoaIdentity *identity,
397 GoaKerberosIdentityManager *self)
398 {
399 IdentitySignalWork *work;
400
401 work = identity_signal_work_new (self, identity);
402 goa_kerberos_identify_manager_send_to_context (g_main_context_default (),
403 (GSourceFunc)
404 do_identity_signal_expired_work,
405 work,
406 (GDestroyNotify)
407 identity_signal_work_free);
408 }
409
410 static void
411 on_identity_unexpired (GoaIdentity *identity,
412 GoaKerberosIdentityManager *self)
413 {
414 g_debug ("GoaKerberosIdentityManager: identity unexpired");
415 /* If an identity is now unexpired, that means some sort of weird
416 * clock skew happened and we should just do a full refresh, since it's
417 * probably affected more than one identity
418 */
419 schedule_refresh (self);
420 }
421
422 static void
423 on_identity_expiring (GoaIdentity *identity,
424 GoaKerberosIdentityManager *self)
425 {
426 g_debug ("GoaKerberosIdentityManager: identity about to expire");
427 _goa_identity_manager_emit_identity_expiring (GOA_IDENTITY_MANAGER (self),
428 identity);
429 }
430
431 static void
432 on_identity_needs_renewal (GoaIdentity *identity,
433 GoaKerberosIdentityManager *self)
434 {
435 g_debug ("GoaKerberosIdentityManager: identity needs renewal");
436 _goa_identity_manager_emit_identity_needs_renewal (GOA_IDENTITY_MANAGER (self),
437 identity);
438 }
439
440 static void
441 on_identity_needs_refresh (GoaIdentity *identity,
442 GoaKerberosIdentityManager *self)
443 {
444 g_debug ("GoaKerberosIdentityManager: needs refresh");
445 schedule_refresh (self);
446 }
447
448 static void
449 watch_for_identity_expiration (GoaKerberosIdentityManager *self,
450 GoaIdentity *identity)
451 {
452 g_signal_handlers_disconnect_by_func (G_OBJECT (identity),
453 G_CALLBACK (on_identity_expired),
454 self);
455 g_signal_connect (G_OBJECT (identity),
456 "expired",
457 G_CALLBACK (on_identity_expired),
458 self);
459
460 g_signal_handlers_disconnect_by_func (G_OBJECT (identity),
461 G_CALLBACK (on_identity_unexpired),
462 self);
463 g_signal_connect (G_OBJECT (identity),
464 "unexpired",
465 G_CALLBACK (on_identity_unexpired),
466 self);
467
468 g_signal_handlers_disconnect_by_func (G_OBJECT (identity),
469 G_CALLBACK (on_identity_expiring),
470 self);
471 g_signal_connect (G_OBJECT (identity),
472 "expiring",
473 G_CALLBACK (on_identity_expiring),
474 self);
475
476 g_signal_handlers_disconnect_by_func (G_OBJECT (identity),
477 G_CALLBACK (on_identity_needs_renewal),
478 self);
479 g_signal_connect (G_OBJECT (identity),
480 "needs-renewal",
481 G_CALLBACK (on_identity_needs_renewal),
482 self);
483
484 g_signal_handlers_disconnect_by_func (G_OBJECT (identity),
485 G_CALLBACK (on_identity_needs_refresh),
486 self);
487 g_signal_connect (G_OBJECT (identity),
488 "needs-refresh",
489 G_CALLBACK (on_identity_needs_refresh),
490 self);
491 }
492
493 static void
494 do_identity_signal_added_work (IdentitySignalWork *work)
495 {
496 GoaKerberosIdentityManager *self = work->manager;
497 GoaIdentity *identity = work->identity;
498
499 watch_for_identity_expiration (self, identity);
500 _goa_identity_manager_emit_identity_added (GOA_IDENTITY_MANAGER (self), identity);
501 }
502
503 static void
504 do_identity_signal_removed_work (IdentitySignalWork *work)
505 {
506 GoaKerberosIdentityManager *self = work->manager;
507 GoaIdentity *identity = work->identity;
508
509 _goa_identity_manager_emit_identity_removed (GOA_IDENTITY_MANAGER (self),
510 identity);
511 }
512
513 static void
514 do_identity_signal_renamed_work (IdentitySignalWork *work)
515 {
516 GoaKerberosIdentityManager *self = work->manager;
517 GoaIdentity *identity = work->identity;
518
519 _goa_identity_manager_emit_identity_renamed (GOA_IDENTITY_MANAGER (self),
520 identity);
521 }
522
523 static void
524 do_identity_signal_refreshed_work (IdentitySignalWork *work)
525 {
526 GoaKerberosIdentityManager *self = work->manager;
527 GoaIdentity *identity = work->identity;
528
529 watch_for_identity_expiration (self, identity);
530 _goa_identity_manager_emit_identity_refreshed (GOA_IDENTITY_MANAGER (self),
531 identity);
532 }
533
534 static void
535 remove_identity (GoaKerberosIdentityManager *self,
536 Operation *operation,
537 GoaIdentity *identity)
538 {
539 IdentitySignalWork *work;
540 const char *identifier;
541 char *name;
542 GList *other_identities = NULL;
543
544 identifier = goa_identity_get_identifier (identity);
545 name = goa_kerberos_identity_get_realm_name (GOA_KERBEROS_IDENTITY (identity));
546
547 if (name != NULL)
548 {
549 other_identities = g_hash_table_lookup (self->identities_by_realm, name);
550 g_hash_table_remove (self->identities_by_realm, name);
551
552 other_identities = g_list_remove (other_identities, identity);
553 }
554
555
556 if (other_identities != NULL)
557 g_hash_table_replace (self->identities_by_realm, g_strdup (name), other_identities);
558
559 g_free (name);
560
561 work = identity_signal_work_new (self, identity);
562 g_hash_table_remove (self->expired_identities, identifier);
563 g_hash_table_remove (self->identities, identifier);
564
565 goa_kerberos_identify_manager_send_to_context (operation->context,
566 (GSourceFunc)
567 do_identity_signal_removed_work,
568 work,
569 (GDestroyNotify) identity_signal_work_free);
570 /* If there's only one identity for this realm now, then we can
571 * rename that identity to just the realm name
572 */
573 if (other_identities != NULL && other_identities->next == NULL)
574 {
575 GoaIdentity *other_identity = other_identities->data;
576
577 work = identity_signal_work_new (self, other_identity);
578
579 goa_kerberos_identify_manager_send_to_context (operation->context,
580 (GSourceFunc)
581 do_identity_signal_renamed_work,
582 work,
583 (GDestroyNotify)
584 identity_signal_work_free);
585 }
586 }
587
588 static void
589 drop_stale_identities (GoaKerberosIdentityManager *self,
590 Operation *operation,
591 GHashTable *known_identities)
592 {
593 GList *stale_identity_ids;
594 GList *node;
595
596 stale_identity_ids = g_hash_table_get_keys (self->identities);
597
598 node = stale_identity_ids;
599 while (node != NULL)
600 {
601 GoaIdentity *identity;
602 const char *identifier = node->data;
603
604 identity = g_hash_table_lookup (known_identities, identifier);
605 if (identity == NULL)
606 {
607 identity = g_hash_table_lookup (self->identities, identifier);
608
609 if (identity != NULL)
610 {
611 remove_identity (self, operation, identity);
612 }
613 }
614 node = node->next;
615 }
616 g_list_free (stale_identity_ids);
617 }
618
619 static void
620 add_identity (GoaKerberosIdentityManager *self,
621 Operation *operation,
622 GoaIdentity *identity,
623 const char *identifier)
624 {
625 IdentitySignalWork *work;
626
627 g_hash_table_replace (self->identities, g_strdup (identifier), g_object_ref (identity));
628
629 if (!goa_identity_is_signed_in (identity))
630 g_hash_table_replace (self->expired_identities, g_strdup (identifier), identity);
631
632 work = identity_signal_work_new (self, identity);
633 goa_kerberos_identify_manager_send_to_context (operation->context,
634 (GSourceFunc)
635 do_identity_signal_added_work,
636 work,
637 (GDestroyNotify) identity_signal_work_free);
638 }
639
640 static char *
641 get_principal_from_cache (GoaKerberosIdentityManager *self,
642 krb5_ccache cache)
643 {
644 int error_code;
645 krb5_principal principal;
646 char *unparsed_name;
647 char *principal_name;
648
649 error_code = krb5_cc_get_principal (self->kerberos_context, cache, &principal);
650
651 error_code = krb5_unparse_name_flags (self->kerberos_context, principal, 0, &unparsed_name);
652 krb5_free_principal (self->kerberos_context, principal);
653
654 if (error_code != 0)
655 return NULL;
656
657 principal_name = g_strdup (unparsed_name);
658
659 krb5_free_unparsed_name (self->kerberos_context, unparsed_name);
660
661 return principal_name;
662 }
663
664 static void
665 import_credentials_cache (GoaKerberosIdentityManager *self,
666 Operation *operation,
667 GHashTable *refreshed_identities,
668 krb5_ccache cache)
669 {
670 g_autofree char *identifier = NULL;
671 GoaIdentity *identity = NULL;
672
673 identifier = get_principal_from_cache (self, cache);
674
675 if (identifier == NULL)
676 return;
677
678 identity = g_hash_table_lookup (self->identities, identifier);
679
680 if (identity == NULL)
681 {
682 g_autoptr(GError) error = NULL;
683
684 g_debug ("GoaKerberosIdentityManager: Adding new identity '%s'", identifier);
685 identity = goa_kerberos_identity_new (self->kerberos_context, cache, &error);
686
687 if (error != NULL)
688 {
689 g_debug ("GoaKerberosIdentityManager: Could not track identity %s: %s",
690 identifier, error->message);
691 return;
692 }
693
694 add_identity (self, operation, identity, identifier);
695 }
696 else
697 {
698 if (!goa_kerberos_identity_has_credentials_cache (GOA_KERBEROS_IDENTITY (identity), cache))
699 goa_kerberos_identity_add_credentials_cache (GOA_KERBEROS_IDENTITY (identity), cache);
700 }
701
702 /* Track refreshed identities so we can emit refreshes and removals when we're done fully
703 * enumerating the collection of credential caches
704 */
705 g_hash_table_replace (refreshed_identities,
706 g_strdup (identifier),
707 g_object_ref (identity));
708 }
709
710 static gboolean
711 refresh_identities (GoaKerberosIdentityManager *self,
712 Operation *operation)
713 {
714 krb5_error_code error_code;
715 krb5_ccache cache;
716 krb5_cccol_cursor cursor;
717 const char *error_message;
718 GHashTable *refreshed_identities;
719 GHashTableIter iter;
720 const char *name;
721 GoaIdentity *identity;
722
723 /* If we have more refreshes queued up, don't bother doing this one
724 */
725 if (!g_atomic_int_dec_and_test (&self->pending_refresh_count))
726 {
727 return FALSE;
728 }
729
730 g_debug ("GoaKerberosIdentityManager: Refreshing identities");
731 refreshed_identities = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
732 error_code = krb5_cccol_cursor_new (self->kerberos_context, &cursor);
733
734 if (error_code != 0)
735 {
736 error_message = krb5_get_error_message (self->kerberos_context, error_code);
737 g_debug ("GoaKerberosIdentityManager: Error looking up available credential caches: %s",
738 error_message);
739 krb5_free_error_message (self->kerberos_context, error_message);
740 goto done;
741 }
742
743 error_code = krb5_cccol_cursor_next (self->kerberos_context, cursor, &cache);
744
745 while (error_code == 0 && cache != NULL)
746 {
747 import_credentials_cache (self, operation, refreshed_identities, cache);
748
749 krb5_cc_close (self->kerberos_context, cache);
750 error_code = krb5_cccol_cursor_next (self->kerberos_context, cursor, &cache);
751 }
752
753 if (error_code != 0)
754 {
755 error_message = krb5_get_error_message (self->kerberos_context, error_code);
756 g_debug ("GoaKerberosIdentityManager: Error iterating over available credential caches: %s",
757 error_message);
758 krb5_free_error_message (self->kerberos_context, error_message);
759 }
760
761 krb5_cccol_cursor_free (self->kerberos_context, &cursor);
762
763 g_hash_table_iter_init (&iter, self->identities);
764 while (g_hash_table_iter_next (&iter, (gpointer *) &name, (gpointer*) &identity))
765 {
766 goa_kerberos_identity_refresh (GOA_KERBEROS_IDENTITY (identity));
767
768 if (goa_identity_is_signed_in (identity))
769 {
770 IdentitySignalWork *work;
771
772 /* if it's not expired, send out a refresh signal */
773 g_debug ("GoaKerberosIdentityManager: identity '%s' refreshed",
774 goa_identity_get_identifier (identity));
775
776 work = identity_signal_work_new (self, identity);
777 goa_kerberos_identify_manager_send_to_context (operation->context,
778 (GSourceFunc)
779 do_identity_signal_refreshed_work,
780 work,
781 (GDestroyNotify)
782 identity_signal_work_free);
783 }
784 }
785
786 done:
787 drop_stale_identities (self, operation, refreshed_identities);
788 g_hash_table_unref (refreshed_identities);
789
790 return TRUE;
791 }
792
793 static int
794 identity_sort_func (GoaIdentity *a,
795 GoaIdentity *b)
796 {
797 return g_strcmp0 (goa_identity_get_identifier (a),
798 goa_identity_get_identifier (b));
799 }
800
801 static void
802 free_identity_list (GList *list)
803 {
804 g_list_free_full (list, g_object_unref);
805 }
806
807 static void
808 list_identities (GoaKerberosIdentityManager *self,
809 Operation *operation)
810 {
811 GList *identities;
812
813 g_debug ("GoaKerberosIdentityManager: Listing identities");
814 identities = g_hash_table_get_values (self->identities);
815
816 identities = g_list_sort (identities, (GCompareFunc) identity_sort_func);
817
818 g_list_foreach (identities, (GFunc) g_object_ref, NULL);
819 g_task_return_pointer (operation->task, identities, (GDestroyNotify) free_identity_list);
820 }
821
822 static void
823 renew_identity (GoaKerberosIdentityManager *self,
824 Operation *operation)
825 {
826 GError *error;
827 gboolean was_renewed;
828 char *identity_name;
829
830 identity_name =
831 goa_kerberos_identity_get_principal_name (GOA_KERBEROS_IDENTITY
832 (operation->identity));
833 g_debug ("GoaKerberosIdentityManager: renewing identity %s", identity_name);
834 g_free (identity_name);
835
836 error = NULL;
837 was_renewed =
838 goa_kerberos_identity_renew (GOA_KERBEROS_IDENTITY (operation->identity),
839 &error);
840
841 if (!was_renewed)
842 {
843 g_debug ("GoaKerberosIdentityManager: could not renew identity: %s",
844 error->message);
845
846 g_task_return_error (operation->task, error);
847 return;
848 }
849
850 g_task_return_boolean (operation->task, was_renewed);
851 }
852
853 static void
854 do_identity_inquiry (Operation *operation)
855 {
856 if (operation->inquiry_func == NULL)
857 {
858 return;
859 }
860
861 operation->inquiry_func (operation->inquiry,
862 operation->cancellable,
863 operation->inquiry_data);
864 }
865
866 static void
867 stop_waiting_on_inquiry (Operation *operation)
868 {
869 g_mutex_lock (&operation->inquiry_lock);
870 if (operation->is_inquiring)
871 {
872 operation->is_inquiring = FALSE;
873 g_cond_signal (&operation->inquiry_finished_condition);
874 }
875 g_mutex_unlock (&operation->inquiry_lock);
876 }
877
878 static void
879 on_kerberos_identity_inquiry_complete (GoaIdentityInquiry *inquiry,
880 Operation *operation)
881 {
882 stop_waiting_on_inquiry (operation);
883 }
884
885 static void
886 start_inquiry (Operation *operation,
887 GoaIdentityInquiry *inquiry)
888 {
889 operation->is_inquiring = TRUE;
890
891 g_signal_connect (G_OBJECT (inquiry),
892 "complete",
893 G_CALLBACK (on_kerberos_identity_inquiry_complete),
894 operation);
895
896 operation->inquiry = inquiry;
897 goa_kerberos_identify_manager_send_to_context (operation->context,
898 (GSourceFunc)
899 do_identity_inquiry,
900 operation, NULL);
901 }
902
903 static void
904 wait_for_inquiry_to_complete (Operation *operation,
905 GoaKerberosIdentityInquiry *inquiry)
906 {
907 g_mutex_lock (&operation->inquiry_lock);
908 while (operation->is_inquiring)
909 g_cond_wait (&operation->inquiry_finished_condition,
910 &operation->inquiry_lock);
911 g_mutex_unlock (&operation->inquiry_lock);
912 }
913
914 static void
915 on_sign_in_operation_cancelled (GCancellable *cancellable,
916 Operation *operation)
917 {
918 stop_waiting_on_inquiry (operation);
919 }
920
921 static void
922 on_kerberos_identity_inquiry (GoaKerberosIdentityInquiry *inquiry,
923 GCancellable *cancellable,
924 Operation *operation)
925 {
926 gulong handler_id;
927
928 start_inquiry (operation, GOA_IDENTITY_INQUIRY (inquiry));
929
930 handler_id = g_cancellable_connect (cancellable,
931 G_CALLBACK (on_sign_in_operation_cancelled),
932 operation, NULL);
933
934 if ((operation->cancellable == NULL) ||
935 !g_cancellable_is_cancelled (operation->cancellable))
936 wait_for_inquiry_to_complete (operation, inquiry);
937
938 g_cancellable_disconnect (cancellable, handler_id);
939 }
940
941 static void
942 get_identity (GoaKerberosIdentityManager *self,
943 Operation *operation)
944 {
945 GoaIdentity *identity;
946
947 g_debug ("GoaKerberosIdentityManager: get identity %s", operation->identifier);
948 identity = g_hash_table_lookup (self->identities, operation->identifier);
949
950 if (identity == NULL)
951 {
952 g_task_return_new_error (operation->task,
953 GOA_IDENTITY_MANAGER_ERROR,
954 GOA_IDENTITY_MANAGER_ERROR_IDENTITY_NOT_FOUND,
955 _("Could not find identity"));
956 return;
957 }
958
959 g_task_return_pointer (operation->task, g_object_ref (identity), g_object_unref);
960 }
961
962 static krb5_error_code
963 get_new_credentials_cache (GoaKerberosIdentityManager *self,
964 krb5_ccache *credentials_cache)
965 {
966 krb5_error_code error_code;
967 gboolean supports_multiple_identities;
968
969 if (g_strcmp0 (self->credentials_cache_type, "FILE") == 0)
970 {
971 g_debug ("GoaKerberosIdentityManager: credential cache type %s doesn't supports cache collections",
972 self->credentials_cache_type);
973 supports_multiple_identities = FALSE;
974 }
975 else if (g_strcmp0 (self->credentials_cache_type, "DIR") == 0
976 || g_strcmp0 (self->credentials_cache_type, "KCM") == 0
977 || g_strcmp0 (self->credentials_cache_type, "KEYRING") == 0)
978 {
979 g_debug ("GoaKerberosIdentityManager: credential cache type %s supports cache collections", self->credentials_cache_type);
980 supports_multiple_identities = TRUE;
981 }
982 else
983 {
984 g_debug ("GoaKerberosIdentityManager: don't know if credential cache type %s supports cache collections, "
985 "assuming yes",
986 self->credentials_cache_type);
987 supports_multiple_identities = TRUE;
988 }
989
990 /* If we're configured for FILE based credentials, then we only
991 * have one ccache, and we need to use it always.
992 *
993 * If we're configured for DIR or KEYRING based credentials, then we
994 * can have multiple ccache's so we should use the default one first
995 * (so it gets selected automatically) and then fallback to unique
996 * ccache names for subsequent tickets.
997 *
998 */
999 if (!supports_multiple_identities || g_hash_table_size (self->identities) == 0)
1000 error_code = krb5_cc_default (self->kerberos_context, credentials_cache);
1001 else
1002 error_code = krb5_cc_new_unique (self->kerberos_context, self->credentials_cache_type, NULL, credentials_cache);
1003
1004 return error_code;
1005 }
1006
1007 static void
1008 sign_in_identity (GoaKerberosIdentityManager *self,
1009 Operation *operation)
1010 {
1011 GoaIdentity *identity;
1012 GError *error;
1013 krb5_error_code error_code;
1014 gboolean is_new_identity = FALSE;
1015
1016 g_debug ("GoaKerberosIdentityManager: signing in identity %s",
1017 operation->identifier);
1018 error = NULL;
1019 identity = g_hash_table_lookup (self->identities, operation->identifier);
1020 if (identity == NULL)
1021 {
1022 krb5_ccache credentials_cache;
1023
1024 error_code = get_new_credentials_cache (self, &credentials_cache);
1025
1026 if (error_code != 0)
1027 {
1028 const char *error_message;
1029
1030 error_message = krb5_get_error_message (self->kerberos_context, error_code);
1031 g_debug ("GoaKerberosIdentityManager: Error creating new cache for identity credentials: %s",
1032 error_message);
1033 krb5_free_error_message (self->kerberos_context, error_message);
1034
1035 g_task_return_new_error (operation->task,
1036 GOA_IDENTITY_MANAGER_ERROR,
1037 GOA_IDENTITY_MANAGER_ERROR_CREATING_IDENTITY,
1038 _("Could not create credential cache for identity"));
1039 return;
1040 }
1041
1042 identity = goa_kerberos_identity_new (self->kerberos_context, credentials_cache, &error);
1043 if (identity == NULL)
1044 {
1045 krb5_cc_destroy (self->kerberos_context, credentials_cache);
1046 g_task_return_error (operation->task, error);
1047 return;
1048 }
1049 krb5_cc_close (self->kerberos_context, credentials_cache);
1050 is_new_identity = TRUE;
1051 }
1052 else
1053 {
1054 g_object_ref (identity);
1055 }
1056
1057 if (!goa_kerberos_identity_sign_in (GOA_KERBEROS_IDENTITY (identity),
1058 operation->identifier,
1059 operation->initial_password,
1060 operation->preauth_source,
1061 operation->sign_in_flags,
1062 (GoaIdentityInquiryFunc)
1063 on_kerberos_identity_inquiry,
1064 operation,
1065 NULL,
1066 operation->cancellable,
1067 &error))
1068 {
1069 if (is_new_identity)
1070 goa_kerberos_identity_erase (GOA_KERBEROS_IDENTITY (identity), NULL);
1071
1072 g_task_return_error (operation->task, error);
1073 }
1074 else
1075 {
1076 g_task_return_pointer (operation->task, g_object_ref (identity), g_object_unref);
1077 g_hash_table_replace (self->identities, g_strdup (operation->identifier), g_object_ref (identity));
1078 }
1079
1080 g_object_unref (identity);
1081 }
1082
1083 static void
1084 sign_out_identity (GoaKerberosIdentityManager *self,
1085 Operation *operation)
1086 {
1087 GError *error;
1088 gboolean was_signed_out;
1089 char *identity_name;
1090
1091 identity_name =
1092 goa_kerberos_identity_get_principal_name (GOA_KERBEROS_IDENTITY
1093 (operation->identity));
1094 g_debug ("GoaKerberosIdentityManager: signing out identity %s", identity_name);
1095 g_free (identity_name);
1096
1097 error = NULL;
1098 was_signed_out =
1099 goa_kerberos_identity_erase (GOA_KERBEROS_IDENTITY (operation->identity),
1100 &error);
1101
1102 if (!was_signed_out)
1103 {
1104 g_debug ("GoaKerberosIdentityManager: could not sign out identity: %s",
1105 error->message);
1106 g_error_free (error);
1107 }
1108
1109 g_task_return_boolean (operation->task, TRUE);
1110 }
1111
1112 static void
1113 block_scheduler_job (GoaKerberosIdentityManager *self)
1114 {
1115 g_mutex_lock (&self->scheduler_job_lock);
1116 while (self->is_blocking_scheduler_job)
1117 g_cond_wait (&self->scheduler_job_unblocked, &self->scheduler_job_lock);
1118 self->is_blocking_scheduler_job = TRUE;
1119 g_mutex_unlock (&self->scheduler_job_lock);
1120 }
1121
1122 static void
1123 stop_blocking_scheduler_job (GoaKerberosIdentityManager *self)
1124 {
1125 g_mutex_lock (&self->scheduler_job_lock);
1126 self->is_blocking_scheduler_job = FALSE;
1127 g_cond_signal (&self->scheduler_job_unblocked);
1128 g_mutex_unlock (&self->scheduler_job_lock);
1129 }
1130
1131 static void
1132 wait_for_scheduler_job_to_become_unblocked (GoaKerberosIdentityManager *self)
1133 {
1134 g_mutex_lock (&self->scheduler_job_lock);
1135 while (self->is_blocking_scheduler_job)
1136 g_cond_wait (&self->scheduler_job_unblocked, &self->scheduler_job_lock);
1137 g_mutex_unlock (&self->scheduler_job_lock);
1138 }
1139
1140 static void
1141 on_scheduler_cancellable_cancelled (GCancellable *cancellable,
1142 GoaKerberosIdentityManager *self)
1143 {
1144 stop_blocking_scheduler_job (self);
1145 }
1146
1147 static void
1148 goa_kerberos_identity_manager_thread_pool_func (gpointer data, gpointer user_data)
1149 {
1150 Operation *operation = (Operation *) data;
1151 GoaKerberosIdentityManager *self = GOA_KERBEROS_IDENTITY_MANAGER (operation->manager);
1152 gboolean processed_operation = FALSE;
1153
1154 if (operation->task != NULL && g_task_return_error_if_cancelled (operation->task))
1155 {
1156 g_clear_object (&operation->task);
1157 goto out;
1158 }
1159
1160 if (g_cancellable_is_cancelled (operation->scheduler_cancellable))
1161 goto out;
1162
1163 switch (operation->type)
1164 {
1165 case OPERATION_TYPE_REFRESH:
1166 processed_operation = refresh_identities (operation->manager, operation);
1167 break;
1168
1169 case OPERATION_TYPE_GET_IDENTITY:
1170 get_identity (operation->manager, operation);
1171 processed_operation = TRUE;
1172 break;
1173
1174 case OPERATION_TYPE_LIST:
1175 list_identities (operation->manager, operation);
1176 processed_operation = TRUE;
1177
1178 /* We want to block refreshes (and their associated "added"
1179 * and "removed" signals) until the caller has had
1180 * a chance to look at the batch of
1181 * results we already processed
1182 */
1183 g_assert (operation->task != NULL);
1184
1185 g_debug ("GoaKerberosIdentityManager: Blocking until identities list processed");
1186 block_scheduler_job (self);
1187 g_object_weak_ref (G_OBJECT (operation->task), (GWeakNotify) stop_blocking_scheduler_job, self);
1188 g_debug ("GoaKerberosIdentityManager: Continuing");
1189 break;
1190
1191 case OPERATION_TYPE_SIGN_IN:
1192 sign_in_identity (operation->manager, operation);
1193 processed_operation = TRUE;
1194 break;
1195
1196 case OPERATION_TYPE_SIGN_OUT:
1197 sign_out_identity (operation->manager, operation);
1198 processed_operation = TRUE;
1199 break;
1200
1201 case OPERATION_TYPE_RENEW:
1202 renew_identity (operation->manager, operation);
1203 processed_operation = TRUE;
1204 break;
1205
1206 default:
1207 break;
1208 }
1209
1210 g_clear_object (&operation->task);
1211 wait_for_scheduler_job_to_become_unblocked (self);
1212
1213 /* Don't bother saying "Waiting for next operation" if this operation
1214 * was a no-op, since the debug spew probably already says the message
1215 */
1216 if (processed_operation)
1217 g_debug ("GoaKerberosIdentityManager: Waiting for next operation");
1218
1219 out:
1220 operation_free (operation);
1221 return;
1222 }
1223
1224 static void
1225 goa_kerberos_identity_manager_get_identity (GoaIdentityManager *manager,
1226 const char *identifier,
1227 GCancellable *cancellable,
1228 GAsyncReadyCallback callback,
1229 gpointer user_data)
1230 {
1231 GoaKerberosIdentityManager *self = GOA_KERBEROS_IDENTITY_MANAGER (manager);
1232 GTask *task;
1233 Operation *operation;
1234
1235 task = g_task_new (self, cancellable, callback, user_data);
1236 g_task_set_source_tag (task, goa_kerberos_identity_manager_get_identity);
1237
1238 operation = operation_new (self, cancellable, OPERATION_TYPE_GET_IDENTITY, task);
1239 g_object_unref (task);
1240
1241 operation->identifier = g_strdup (identifier);
1242
1243 g_thread_pool_push (self->thread_pool, operation, NULL);
1244 }
1245
1246 static GoaIdentity *
1247 goa_kerberos_identity_manager_get_identity_finish (GoaIdentityManager *self,
1248 GAsyncResult *result,
1249 GError **error)
1250 {
1251 GTask *task = G_TASK (result);
1252 return g_task_propagate_pointer (task, error);
1253 }
1254
1255 static void
1256 goa_kerberos_identity_manager_list_identities (GoaIdentityManager *manager,
1257 GCancellable *cancellable,
1258 GAsyncReadyCallback callback,
1259 gpointer user_data)
1260 {
1261 GoaKerberosIdentityManager *self = GOA_KERBEROS_IDENTITY_MANAGER (manager);
1262 GTask *task;
1263 Operation *operation;
1264
1265 task = g_task_new (self, cancellable, callback, user_data);
1266 g_task_set_source_tag (task, goa_kerberos_identity_manager_list_identities);
1267
1268 operation = operation_new (self, cancellable, OPERATION_TYPE_LIST, task);
1269 g_object_unref (task);
1270
1271 g_thread_pool_push (self->thread_pool, operation, NULL);
1272 }
1273
1274 static GList *
1275 goa_kerberos_identity_manager_list_identities_finish (GoaIdentityManager *manager,
1276 GAsyncResult *result,
1277 GError **error)
1278 {
1279 GTask *task = G_TASK (result);
1280 return g_task_propagate_pointer (task, error);
1281 }
1282
1283 static void
1284 goa_kerberos_identity_manager_renew_identity (GoaIdentityManager *manager,
1285 GoaIdentity *identity,
1286 GCancellable *cancellable,
1287 GAsyncReadyCallback callback,
1288 gpointer user_data)
1289 {
1290 GoaKerberosIdentityManager *self = GOA_KERBEROS_IDENTITY_MANAGER (manager);
1291 GTask *task;
1292 Operation *operation;
1293
1294 task = g_task_new (self, cancellable, callback, user_data);
1295 g_task_set_source_tag (task, goa_kerberos_identity_manager_renew_identity);
1296
1297 operation = operation_new (self, cancellable, OPERATION_TYPE_RENEW, task);
1298 g_object_unref (task);
1299
1300 operation->identity = g_object_ref (identity);
1301
1302 g_thread_pool_push (self->thread_pool, operation, NULL);
1303 }
1304
1305 static void
1306 goa_kerberos_identity_manager_renew_identity_finish (GoaIdentityManager *self,
1307 GAsyncResult *result,
1308 GError **error)
1309 {
1310 GTask *task = G_TASK (result);
1311 g_task_propagate_pointer (task, error);
1312 }
1313
1314 static void
1315 goa_kerberos_identity_manager_sign_identity_in (GoaIdentityManager *manager,
1316 const char *identifier,
1317 gconstpointer initial_password,
1318 const char *preauth_source,
1319 GoaIdentitySignInFlags flags,
1320 GoaIdentityInquiryFunc inquiry_func,
1321 gpointer inquiry_data,
1322 GCancellable *cancellable,
1323 GAsyncReadyCallback callback,
1324 gpointer user_data)
1325 {
1326 GoaKerberosIdentityManager *self = GOA_KERBEROS_IDENTITY_MANAGER (manager);
1327 GTask *task;
1328 Operation *operation;
1329
1330 task = g_task_new (self, cancellable, callback, user_data);
1331 g_task_set_source_tag (task, goa_kerberos_identity_manager_sign_identity_in);
1332
1333 operation = operation_new (self, cancellable, OPERATION_TYPE_SIGN_IN, task);
1334 g_object_unref (task);
1335
1336 operation->identifier = g_strdup (identifier);
1337 /* Not duped. Caller is responsible for ensuring it stays alive
1338 * for duration of operation
1339 */
1340 operation->initial_password = initial_password;
1341 operation->preauth_source = g_strdup (preauth_source);
1342 operation->sign_in_flags = flags;
1343 operation->inquiry_func = inquiry_func;
1344 operation->inquiry_data = inquiry_data;
1345 g_mutex_init (&operation->inquiry_lock);
1346 g_cond_init (&operation->inquiry_finished_condition);
1347 operation->is_inquiring = FALSE;
1348
1349 g_thread_pool_push (self->thread_pool, operation, NULL);
1350 }
1351
1352 static GoaIdentity *
1353 goa_kerberos_identity_manager_sign_identity_in_finish (GoaIdentityManager *self,
1354 GAsyncResult *result,
1355 GError **error)
1356 {
1357 GTask *task = G_TASK (result);
1358 return g_task_propagate_pointer (task, error);
1359 }
1360
1361 static void
1362 goa_kerberos_identity_manager_sign_identity_out (GoaIdentityManager *manager,
1363 GoaIdentity *identity,
1364 GCancellable *cancellable,
1365 GAsyncReadyCallback callback,
1366 gpointer user_data)
1367 {
1368 GoaKerberosIdentityManager *self = GOA_KERBEROS_IDENTITY_MANAGER (manager);
1369 GTask *task;
1370 Operation *operation;
1371
1372 task = g_task_new (self, cancellable, callback, user_data);
1373 g_task_set_source_tag (task, goa_kerberos_identity_manager_sign_identity_out);
1374
1375 operation = operation_new (self, cancellable, OPERATION_TYPE_SIGN_OUT, task);
1376 g_object_unref (task);
1377
1378 operation->identity = g_object_ref (identity);
1379
1380 g_thread_pool_push (self->thread_pool, operation, NULL);
1381 }
1382
1383 static void
1384 goa_kerberos_identity_manager_sign_identity_out_finish (GoaIdentityManager *self,
1385 GAsyncResult *result,
1386 GError **error)
1387 {
1388 GTask *task = G_TASK (result);
1389 g_task_propagate_pointer (task, error);
1390 }
1391
1392 static char *
1393 goa_kerberos_identity_manager_name_identity (GoaIdentityManager *manager,
1394 GoaIdentity *identity)
1395 {
1396 GoaKerberosIdentityManager *self = GOA_KERBEROS_IDENTITY_MANAGER (manager);
1397 char *name;
1398 GList *other_identities;
1399 gboolean other_identity_needs_rename = FALSE;
1400
1401 name = goa_kerberos_identity_get_realm_name (GOA_KERBEROS_IDENTITY (identity));
1402
1403 if (name == NULL)
1404 return NULL;
1405
1406 other_identities = g_hash_table_lookup (self->identities_by_realm, name);
1407
1408 /* If there was already exactly one identity for this realm before,
1409 * then it was going by just the realm name, so we need to rename it
1410 * to use the full principle name
1411 */
1412 if (other_identities != NULL &&
1413 other_identities->next == NULL && other_identities->data != identity)
1414 other_identity_needs_rename = TRUE;
1415
1416 other_identities = g_list_remove (other_identities, identity);
1417 other_identities = g_list_prepend (other_identities, identity);
1418
1419 g_hash_table_replace (self->identities_by_realm, g_strdup (name), other_identities);
1420
1421 if (other_identities->next != NULL)
1422 {
1423 g_free (name);
1424 name = goa_kerberos_identity_get_principal_name (GOA_KERBEROS_IDENTITY (identity));
1425
1426 if (other_identity_needs_rename)
1427 {
1428 GoaIdentity *other_identity = other_identities->next->data;
1429
1430 _goa_identity_manager_emit_identity_renamed (GOA_IDENTITY_MANAGER (self),
1431 other_identity);
1432 }
1433 }
1434
1435 return name;
1436 }
1437
1438 static void
1439 identity_manager_interface_init (GoaIdentityManagerInterface *interface)
1440 {
1441 interface->get_identity = goa_kerberos_identity_manager_get_identity;
1442 interface->get_identity_finish = goa_kerberos_identity_manager_get_identity_finish;
1443 interface->list_identities = goa_kerberos_identity_manager_list_identities;
1444 interface->list_identities_finish = goa_kerberos_identity_manager_list_identities_finish;
1445 interface->sign_identity_in = goa_kerberos_identity_manager_sign_identity_in;
1446 interface->sign_identity_in_finish = goa_kerberos_identity_manager_sign_identity_in_finish;
1447 interface->sign_identity_out = goa_kerberos_identity_manager_sign_identity_out;
1448 interface->sign_identity_out_finish = goa_kerberos_identity_manager_sign_identity_out_finish;
1449 interface->renew_identity = goa_kerberos_identity_manager_renew_identity;
1450 interface->renew_identity_finish = goa_kerberos_identity_manager_renew_identity_finish;
1451 interface->name_identity = goa_kerberos_identity_manager_name_identity;
1452 }
1453
1454 static void
1455 credentials_cache_file_monitor_changed (GFileMonitor *monitor,
1456 GFile *file,
1457 GFile *other_file,
1458 GFileMonitorEvent *event_type,
1459 GoaKerberosIdentityManager *self)
1460 {
1461 schedule_refresh (self);
1462 }
1463
1464 static gboolean
1465 credentials_cache_keyring_notification (GPollableInputStream *stream, GoaKerberosIdentityManager *self)
1466 {
1467 gssize bytes_read;
1468 guchar buffer[433];
1469
1470 {
1471 g_autoptr (GError) error = NULL;
1472
1473 bytes_read = g_pollable_input_stream_read_nonblocking (stream, buffer, sizeof (buffer), NULL, &error);
1474 if (error != NULL)
1475 {
1476 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1477 g_debug ("GoaKerberosIdentityManager: Keyring notification source not yet ready");
1478 else
1479 g_warning ("GoaKerberosIdentityManager: Could not read keyring notification source: %s", error->message);
1480
1481 goto out;
1482 }
1483 }
1484
1485 if (bytes_read != (gssize) sizeof (buffer))
1486 {
1487 g_warning ("GoaKerberosIdentityManager: Expected to read %" G_GSIZE_FORMAT " bytes, "
1488 "but received only %" G_GSSIZE_FORMAT " bytes",
1489 sizeof (buffer),
1490 bytes_read);
1491 }
1492
1493 schedule_refresh (self);
1494
1495 out:
1496 return G_SOURCE_CONTINUE;
1497 }
1498
1499 static gboolean
1500 credentials_cache_polling_timeout (GoaKerberosIdentityManager *self)
1501 {
1502 schedule_refresh (self);
1503
1504 return G_SOURCE_CONTINUE;
1505 }
1506
1507 static gboolean
1508 monitor_credentials_cache (GoaKerberosIdentityManager *self,
1509 GError **error)
1510 {
1511 krb5_ccache default_cache;
1512 const char *cache_type;
1513 const char *cache_path;
1514 krb5_error_code error_code;
1515 GError *monitoring_error = NULL;
1516 gboolean can_monitor = TRUE;
1517
1518 error_code = krb5_cc_default (self->kerberos_context, &default_cache);
1519
1520 if (error_code != 0)
1521 {
1522 const char *error_message;
1523 error_message = krb5_get_error_message (self->kerberos_context, error_code);
1524
1525 g_set_error_literal (error,
1526 GOA_IDENTITY_MANAGER_ERROR,
1527 GOA_IDENTITY_MANAGER_ERROR_ACCESSING_CREDENTIALS,
1528 error_message);
1529 krb5_free_error_message (self->kerberos_context, error_message);
1530
1531 return FALSE;
1532 }
1533
1534 cache_type = krb5_cc_get_type (self->kerberos_context, default_cache);
1535 g_assert (cache_type != NULL);
1536
1537 g_free (self->credentials_cache_type);
1538 self->credentials_cache_type = g_strdup (cache_type);
1539
1540 /* If we're using a FILE type credential cache, then the
1541 * default cache file is the only cache we care about,
1542 * and its path is what we want to monitor.
1543 *
1544 * If we're using a DIR type credential cache, then the default
1545 * cache file is one of many possible cache files, all in the
1546 * same directory. We want to monitor that directory.
1547 */
1548 cache_path = krb5_cc_get_name (self->kerberos_context, default_cache);
1549
1550 /* The cache name might have a : in front of it.
1551 * See goakerberosidentity.c (fetch_raw_credentials) for similar code
1552 * FIXME: figure out if that behavior is by design, or some
1553 * odd bug.
1554 */
1555 if (cache_path[0] == ':')
1556 cache_path++;
1557
1558 if (strcmp (cache_type, "FILE") == 0 || strcmp (cache_type, "DIR") == 0)
1559 {
1560 GFile *file = NULL;
1561 GFileMonitor *monitor = NULL;
1562
1563 file = g_file_new_for_path (cache_path);
1564
1565 if (strcmp (cache_type, "FILE") == 0)
1566 {
1567 monitoring_error = NULL;
1568 monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &monitoring_error);
1569 if (monitoring_error != NULL)
1570 {
1571 g_warning ("GoaKerberosIdentityManager: Could not monitor credentials for %s (type %s), reverting to "
1572 "polling: %s",
1573 cache_path,
1574 cache_type,
1575 monitoring_error->message);
1576
1577 can_monitor = FALSE;
1578 g_error_free (monitoring_error);
1579 }
1580 }
1581 else if (strcmp (cache_type, "DIR") == 0)
1582 {
1583 GFile *directory;
1584
1585 directory = g_file_get_parent (file);
1586
1587 monitoring_error = NULL;
1588 monitor = g_file_monitor_directory (directory, G_FILE_MONITOR_NONE, NULL, &monitoring_error);
1589 if (monitoring_error != NULL)
1590 {
1591 g_warning ("GoaKerberosIdentityManager: Could not monitor credentials for %s (type %s), reverting to "
1592 "polling: %s",
1593 cache_path,
1594 cache_type,
1595 monitoring_error->message);
1596
1597 can_monitor = FALSE;
1598 g_error_free (monitoring_error);
1599 }
1600
1601 g_object_unref (directory);
1602 }
1603
1604 if (monitor != NULL)
1605 {
1606 g_signal_connect (G_OBJECT (monitor), "changed", G_CALLBACK (credentials_cache_file_monitor_changed), self);
1607 self->credentials_cache_file_monitor = monitor;
1608 }
1609
1610 g_object_unref (file);
1611 }
1612 else if (strcmp (cache_type, "KEYRING") == 0)
1613 {
1614 GSource *keyring_source = NULL;
1615
1616 monitoring_error = NULL;
1617 keyring_source = goa_kerberos_identity_manager_keyring_source_new (&monitoring_error);
1618 if (monitoring_error != NULL)
1619 {
1620 g_warning ("GoaKerberosIdentityManager: Could not monitor credentials for %s (type %s), reverting to polling: %s",
1621 cache_path,
1622 cache_type,
1623 monitoring_error->message);
1624
1625 can_monitor = FALSE;
1626 g_error_free (monitoring_error);
1627 }
1628
1629 if (keyring_source != NULL)
1630 {
1631 g_source_set_callback (keyring_source, (GSourceFunc) credentials_cache_keyring_notification, self, NULL);
1632 self->credentials_cache_keyring_notification_id = g_source_attach (keyring_source, NULL);
1633 }
1634
1635 g_clear_pointer (&keyring_source, g_source_unref);
1636 }
1637 else
1638 {
1639 can_monitor = FALSE;
1640 }
1641
1642 if (!can_monitor)
1643 {
1644 g_warning ("GoaKerberosIdentityManager: Using polling for change notification for credential cache type '%s'",
1645 cache_type);
1646
1647 self->credentials_cache_polling_timeout_id = g_timeout_add_seconds (FALLBACK_POLLING_INTERVAL,
1648 (GSourceFunc) credentials_cache_polling_timeout,
1649 self);
1650 }
1651
1652 krb5_cc_close (self->kerberos_context, default_cache);
1653
1654 return TRUE;
1655 }
1656
1657 static void
1658 stop_watching_credentials_cache (GoaKerberosIdentityManager *self)
1659 {
1660 if (self->credentials_cache_file_monitor != NULL)
1661 {
1662 if (!g_file_monitor_is_cancelled (self->credentials_cache_file_monitor))
1663 g_file_monitor_cancel (self->credentials_cache_file_monitor);
1664
1665 g_clear_object (&self->credentials_cache_file_monitor);
1666 }
1667
1668 if (self->credentials_cache_keyring_notification_id != 0)
1669 {
1670 g_source_remove (self->credentials_cache_keyring_notification_id);
1671 self->credentials_cache_keyring_notification_id = 0;
1672 }
1673
1674 if (self->credentials_cache_polling_timeout_id != 0)
1675 {
1676 g_source_remove (self->credentials_cache_polling_timeout_id);
1677 self->credentials_cache_polling_timeout_id = 0;
1678 }
1679 }
1680
1681 static gboolean
1682 goa_kerberos_identity_manager_initable_init (GInitable *initable,
1683 GCancellable *cancellable,
1684 GError **error)
1685 {
1686 GoaKerberosIdentityManager *self = GOA_KERBEROS_IDENTITY_MANAGER (initable);
1687 krb5_error_code error_code;
1688 GError *monitoring_error;
1689
1690 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1691 return FALSE;
1692
1693 error_code = krb5_init_context (&self->kerberos_context);
1694
1695 if (error_code != 0)
1696 {
1697 const char *error_message;
1698 error_message = krb5_get_error_message (self->kerberos_context, error_code);
1699
1700 g_set_error_literal (error,
1701 GOA_IDENTITY_MANAGER_ERROR,
1702 GOA_IDENTITY_MANAGER_ERROR_INITIALIZING, error_message);
1703 krb5_free_error_message (self->kerberos_context, error_message);
1704
1705 return FALSE;
1706 }
1707
1708 monitoring_error = NULL;
1709 if (!monitor_credentials_cache (self, &monitoring_error))
1710 {
1711 g_warning ("GoaKerberosIdentityManager: Could not monitor credentials: %s",
1712 monitoring_error->message);
1713 g_error_free (monitoring_error);
1714 }
1715
1716 schedule_refresh (self);
1717
1718 return TRUE;
1719 }
1720
1721 static void
1722 initable_interface_init (GInitableIface *interface)
1723 {
1724 interface->init = goa_kerberos_identity_manager_initable_init;
1725 }
1726
1727 static void
1728 goa_kerberos_identity_manager_init (GoaKerberosIdentityManager *self)
1729 {
1730 GError *error;
1731
1732 self->identities = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
1733 self->expired_identities = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1734 self->identities_by_realm = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1735
1736 error = NULL;
1737 self->thread_pool = g_thread_pool_new (goa_kerberos_identity_manager_thread_pool_func, NULL, 1, FALSE, &error);
1738 g_assert_no_error (error);
1739
1740 g_mutex_init (&self->scheduler_job_lock);
1741 g_cond_init (&self->scheduler_job_unblocked);
1742
1743 self->scheduler_cancellable = g_cancellable_new ();
1744 g_cancellable_connect (self->scheduler_cancellable, G_CALLBACK (on_scheduler_cancellable_cancelled), self, NULL);
1745 }
1746
1747 static void
1748 goa_kerberos_identity_manager_dispose (GObject *object)
1749 {
1750 GoaKerberosIdentityManager *self = GOA_KERBEROS_IDENTITY_MANAGER (object);
1751
1752 if (self->scheduler_cancellable != NULL)
1753 {
1754 if (!g_cancellable_is_cancelled (self->scheduler_cancellable))
1755 {
1756 g_cancellable_cancel (self->scheduler_cancellable);
1757 }
1758
1759 g_clear_object (&self->scheduler_cancellable);
1760 }
1761
1762 if (self->thread_pool != NULL)
1763 {
1764 g_thread_pool_free (self->thread_pool, FALSE, TRUE);
1765 self->thread_pool = NULL;
1766 }
1767
1768 if (self->identities_by_realm != NULL)
1769 {
1770 g_hash_table_unref (self->identities_by_realm);
1771 self->identities_by_realm = NULL;
1772 }
1773
1774 if (self->expired_identities != NULL)
1775 {
1776 g_hash_table_unref (self->expired_identities);
1777 self->expired_identities = NULL;
1778 }
1779
1780 if (self->identities != NULL)
1781 {
1782 g_hash_table_unref (self->identities);
1783 self->identities = NULL;
1784 }
1785
1786 stop_watching_credentials_cache (self);
1787
1788 G_OBJECT_CLASS (goa_kerberos_identity_manager_parent_class)->dispose (object);
1789 }
1790
1791 static void
1792 goa_kerberos_identity_manager_finalize (GObject *object)
1793 {
1794 GoaKerberosIdentityManager *self = GOA_KERBEROS_IDENTITY_MANAGER (object);
1795
1796 g_free (self->credentials_cache_type);
1797
1798 g_cond_clear (&self->scheduler_job_unblocked);
1799 krb5_free_context (self->kerberos_context);
1800
1801 G_OBJECT_CLASS (goa_kerberos_identity_manager_parent_class)->finalize (object);
1802 }
1803
1804 static void
1805 goa_kerberos_identity_manager_class_init (GoaKerberosIdentityManagerClass *klass)
1806 {
1807 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1808
1809 object_class->dispose = goa_kerberos_identity_manager_dispose;
1810 object_class->finalize = goa_kerberos_identity_manager_finalize;
1811 }
1812
1813 GoaIdentityManager *
1814 goa_kerberos_identity_manager_new (GCancellable * cancellable, GError ** error)
1815 {
1816 if (goa_kerberos_identity_manager_singleton == NULL)
1817 {
1818 GObject *object;
1819
1820 object = g_object_new (GOA_TYPE_KERBEROS_IDENTITY_MANAGER, NULL);
1821
1822 goa_kerberos_identity_manager_singleton = GOA_IDENTITY_MANAGER (object);
1823 g_object_add_weak_pointer (object,
1824 (gpointer *) &
1825 goa_kerberos_identity_manager_singleton);
1826
1827 if (!g_initable_init (G_INITABLE (object), cancellable, error))
1828 {
1829 g_object_unref (object);
1830 return NULL;
1831 }
1832
1833 }
1834 else
1835 {
1836 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1837 return NULL;
1838 g_object_ref (goa_kerberos_identity_manager_singleton);
1839 }
1840
1841 return goa_kerberos_identity_manager_singleton;
1842 }