2 Unix SMB/CIFS implementation.
4 Winbind daemon - krb5 credential cache functions
5 and in-memory cache functions.
7 Copyright (C) Guenther Deschner 2005-2006
8 Copyright (C) Jeremy Allison 2006
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #define DBGC_CLASS DBGC_WINBIND
30 #define MAX_CCACHES 100
32 static struct WINBINDD_CCACHE_ENTRY *ccache_list;
34 /****************************************************************
35 Find an entry by name.
36 ****************************************************************/
38 static struct WINBINDD_CCACHE_ENTRY *get_ccache_by_username(const char *username)
40 struct WINBINDD_CCACHE_ENTRY *entry;
42 for (entry = ccache_list; entry; entry = entry->next) {
43 if (strequal(entry->username, username)) {
50 /****************************************************************
52 ****************************************************************/
54 static int ccache_entry_count(void)
56 struct WINBINDD_CCACHE_ENTRY *entry;
59 for (entry = ccache_list; entry; entry = entry->next) {
65 /****************************************************************
66 Do the work of refreshing the ticket.
67 ****************************************************************/
69 static void krb5_ticket_refresh_handler(struct timed_event *te,
70 const struct timeval *now,
73 struct WINBINDD_CCACHE_ENTRY *entry =
74 talloc_get_type_abort(private_data, struct WINBINDD_CCACHE_ENTRY);
78 struct WINBINDD_MEMORY_CREDS *cred_ptr = entry->cred_ptr;
81 DEBUG(10,("krb5_ticket_refresh_handler called\n"));
82 DEBUGADD(10,("event called for: %s, %s\n", entry->ccname, entry->username));
84 TALLOC_FREE(entry->event);
88 /* Kinit again if we have the user password and we can't renew the old
91 if ((entry->renew_until < time(NULL)) && cred_ptr && cred_ptr->pass) {
93 set_effective_uid(entry->uid);
95 ret = kerberos_kinit_password_ext(entry->principal_name,
97 0, /* hm, can we do time correction here ? */
101 False, /* no PAC required anymore */
103 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME);
104 gain_root_privilege();
107 DEBUG(3,("krb5_ticket_refresh_handler: could not re-kinit: %s\n",
108 error_message(ret)));
109 TALLOC_FREE(entry->event);
113 DEBUG(10,("krb5_ticket_refresh_handler: successful re-kinit "
114 "for: %s in ccache: %s\n",
115 entry->principal_name, entry->ccname));
117 new_start = entry->refresh_time;
122 set_effective_uid(entry->uid);
124 ret = smb_krb5_renew_ticket(entry->ccname,
125 entry->principal_name,
128 gain_root_privilege();
131 DEBUG(3,("krb5_ticket_refresh_handler: could not renew tickets: %s\n",
132 error_message(ret)));
133 /* maybe we are beyond the renewing window */
135 /* avoid breaking the renewal chain: retry in lp_winbind_cache_time()
136 * seconds when the KDC was not available right now. */
138 if (ret == KRB5_KDC_UNREACH) {
139 new_start = time(NULL) + MAX(30, lp_winbind_cache_time());
148 entry->event = add_timed_event(entry,
149 timeval_set(new_start, 0),
150 "krb5_ticket_refresh_handler",
151 krb5_ticket_refresh_handler,
157 /****************************************************************
158 Do the work of regaining a ticket when coming from offline auth.
159 ****************************************************************/
161 static void krb5_ticket_gain_handler(struct timed_event *te,
162 const struct timeval *now,
165 struct WINBINDD_CCACHE_ENTRY *entry =
166 talloc_get_type_abort(private_data, struct WINBINDD_CCACHE_ENTRY);
171 struct WINBINDD_MEMORY_CREDS *cred_ptr = entry->cred_ptr;
172 struct winbindd_domain *domain = NULL;
175 DEBUG(10,("krb5_ticket_gain_handler called\n"));
176 DEBUGADD(10,("event called for: %s, %s\n", entry->ccname, entry->username));
178 TALLOC_FREE(entry->event);
182 if (!cred_ptr || !cred_ptr->pass) {
183 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
187 if ((domain = find_domain_from_name(entry->realm)) == NULL) {
188 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
192 if (domain->online) {
194 set_effective_uid(entry->uid);
196 ret = kerberos_kinit_password_ext(entry->principal_name,
198 0, /* hm, can we do time correction here ? */
199 &entry->refresh_time,
202 False, /* no PAC required anymore */
204 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME);
205 gain_root_privilege();
208 DEBUG(3,("krb5_ticket_gain_handler: could not kinit: %s\n",
209 error_message(ret)));
213 DEBUG(10,("krb5_ticket_gain_handler: successful kinit for: %s in ccache: %s\n",
214 entry->principal_name, entry->ccname));
216 new_start = entry->refresh_time;
223 entry->event = add_timed_event(entry,
224 timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0),
225 "krb5_ticket_gain_handler",
226 krb5_ticket_gain_handler,
234 t = timeval_set(time(NULL) + 30, 0);
236 t = timeval_set(new_start, 0);
239 entry->event = add_timed_event(entry,
241 "krb5_ticket_refresh_handler",
242 krb5_ticket_refresh_handler,
249 /****************************************************************
250 Ensure we're changing the correct entry.
251 ****************************************************************/
253 BOOL ccache_entry_identical(const char *username, uid_t uid, const char *ccname)
255 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
261 if (entry->uid != uid) {
262 DEBUG(0,("cache_entry_identical: uid's differ: %u != %u\n",
263 (unsigned int)entry->uid, (unsigned int)uid ));
266 if (!strcsequal(entry->ccname, ccname)) {
267 DEBUG(0,("cache_entry_identical: ccnames differ: (cache) %s != (client) %s\n",
268 entry->ccname, ccname));
274 NTSTATUS add_ccache_to_list(const char *princ_name,
277 const char *username,
283 BOOL schedule_refresh_event,
284 BOOL postponed_request)
286 struct WINBINDD_CCACHE_ENTRY *entry = NULL;
288 if ((username == NULL && princ_name == NULL) || ccname == NULL || uid < 0) {
289 return NT_STATUS_INVALID_PARAMETER;
292 if (ccache_entry_count() + 1 > MAX_CCACHES) {
293 DEBUG(10,("add_ccache_to_list: max number of ccaches reached\n"));
294 return NT_STATUS_NO_MORE_ENTRIES;
297 /* Reference count old entries */
298 entry = get_ccache_by_username(username);
300 /* Check cached entries are identical. */
301 if (!ccache_entry_identical(username, uid, ccname)) {
302 return NT_STATUS_INVALID_PARAMETER;
305 DEBUG(10,("add_ccache_to_list: ref count on entry %s is now %d\n",
306 username, entry->ref_count));
310 entry = TALLOC_P(NULL, struct WINBINDD_CCACHE_ENTRY);
312 return NT_STATUS_NO_MEMORY;
318 entry->username = talloc_strdup(entry, username);
319 if (!entry->username) {
324 entry->principal_name = talloc_strdup(entry, princ_name);
325 if (!entry->principal_name) {
330 entry->service = talloc_strdup(entry, service);
331 if (!entry->service) {
336 entry->ccname = talloc_strdup(entry, ccname);
337 if (!entry->ccname) {
341 entry->realm = talloc_strdup(entry, realm);
346 entry->create_time = create_time;
347 entry->renew_until = renew_until;
349 entry->ref_count = 1;
351 if (schedule_refresh_event && renew_until > 0) {
352 if (postponed_request) {
353 entry->event = add_timed_event(entry,
354 timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0),
355 "krb5_ticket_gain_handler",
356 krb5_ticket_gain_handler,
359 entry->event = add_timed_event(entry,
360 timeval_set((ticket_end - 1), 0),
361 "krb5_ticket_refresh_handler",
362 krb5_ticket_refresh_handler,
370 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
373 DLIST_ADD(ccache_list, entry);
375 DEBUG(10,("add_ccache_to_list: added ccache [%s] for user [%s] to the list\n", ccname, username));
382 return NT_STATUS_NO_MEMORY;
385 /*******************************************************************
386 Remove a WINBINDD_CCACHE_ENTRY entry and the krb5 ccache if no longer referenced.
387 *******************************************************************/
389 NTSTATUS remove_ccache(const char *username)
391 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
398 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
401 if (entry->ref_count <= 0) {
402 DEBUG(0,("remove_ccache: logic error. ref count for user %s = %d\n",
403 username, entry->ref_count));
404 return NT_STATUS_INTERNAL_DB_CORRUPTION;
409 if (entry->ref_count > 0) {
410 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
411 username, entry->ref_count ));
415 /* no references any more */
417 DLIST_REMOVE(ccache_list, entry);
418 TALLOC_FREE(entry->event); /* unregisters events */
421 ret = ads_kdestroy(entry->ccname);
423 /* we ignore the error when there has been no credential cache */
424 if (ret == KRB5_FCC_NOFILE) {
427 DEBUG(0,("remove_ccache: failed to destroy user krb5 ccache %s with: %s\n",
428 entry->ccname, error_message(ret)));
430 DEBUG(10,("remove_ccache: successfully destroyed krb5 ccache %s for user %s\n",
431 entry->ccname, username));
433 status = krb5_to_nt_status(ret);
437 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username));
442 /*******************************************************************
443 In memory credentials cache code.
444 *******************************************************************/
446 static struct WINBINDD_MEMORY_CREDS *memory_creds_list;
448 /***********************************************************
449 Find an entry on the list by name.
450 ***********************************************************/
452 struct WINBINDD_MEMORY_CREDS *find_memory_creds_by_name(const char *username)
454 struct WINBINDD_MEMORY_CREDS *p;
456 for (p = memory_creds_list; p; p = p->next) {
457 if (strequal(p->username, username)) {
464 /***********************************************************
465 Store the required creds and mlock them.
466 ***********************************************************/
468 static NTSTATUS store_memory_creds(struct WINBINDD_MEMORY_CREDS *memcredp, const char *pass)
470 #if !defined(HAVE_MLOCK)
473 /* new_entry->nt_hash is the base pointer for the block
474 of memory pointed into by new_entry->lm_hash and
475 new_entry->pass (if we're storing plaintext). */
477 memcredp->len = NT_HASH_LEN + LM_HASH_LEN;
479 memcredp->len += strlen(pass)+1;
482 memcredp->nt_hash = (unsigned char *)TALLOC_ZERO(memcredp, memcredp->len);
483 if (!memcredp->nt_hash) {
484 return NT_STATUS_NO_MEMORY;
487 memcredp->lm_hash = memcredp->nt_hash + NT_HASH_LEN;
488 #ifdef DEBUG_PASSWORD
489 DEBUG(10,("mlocking memory: %p\n", memcredp->nt_hash));
492 if ((mlock(memcredp->nt_hash, memcredp->len)) == -1) {
493 DEBUG(0,("failed to mlock memory: %s (%d)\n",
494 strerror(errno), errno));
495 return map_nt_error_from_unix(errno);
498 #ifdef DEBUG_PASSWORD
499 DEBUG(10,("mlocked memory: %p\n", memcredp->nt_hash));
502 /* Create and store the password hashes. */
503 E_md4hash(pass, memcredp->nt_hash);
504 E_deshash(pass, memcredp->lm_hash);
507 memcredp->pass = (char *)memcredp->lm_hash + LM_HASH_LEN;
508 memcpy(memcredp->pass, pass, memcredp->len - NT_HASH_LEN - LM_HASH_LEN);
515 /***********************************************************
516 Destroy existing creds.
517 ***********************************************************/
519 static NTSTATUS delete_memory_creds(struct WINBINDD_MEMORY_CREDS *memcredp)
521 #if !defined(HAVE_MUNLOCK)
524 if (munlock(memcredp->nt_hash, memcredp->len) == -1) {
525 DEBUG(0,("failed to munlock memory: %s (%d)\n",
526 strerror(errno), errno));
527 return map_nt_error_from_unix(errno);
529 memset(memcredp->nt_hash, '\0', memcredp->len);
530 TALLOC_FREE(memcredp->nt_hash);
531 memcredp->lm_hash = NULL;
532 memcredp->pass = NULL;
538 /***********************************************************
539 Replace the required creds with new ones (password change).
540 ***********************************************************/
542 static NTSTATUS winbindd_replace_memory_creds_internal(struct WINBINDD_MEMORY_CREDS *memcredp,
545 NTSTATUS status = delete_memory_creds(memcredp);
546 if (!NT_STATUS_IS_OK(status)) {
549 return store_memory_creds(memcredp, pass);
552 /*************************************************************
553 Store credentials in memory in a list.
554 *************************************************************/
556 static NTSTATUS winbindd_add_memory_creds_internal(const char *username, uid_t uid, const char *pass)
558 /* Shortcut to ensure we don't store if no mlock. */
559 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
563 struct WINBINDD_MEMORY_CREDS *memcredp = find_memory_creds_by_name(username);
565 if (uid == (uid_t)-1) {
566 DEBUG(0,("winbindd_add_memory_creds_internal: invalid uid for user %s.\n",
568 return NT_STATUS_INVALID_PARAMETER;
572 /* Already exists. Increment the reference count and replace stored creds. */
573 if (uid != memcredp->uid) {
574 DEBUG(0,("winbindd_add_memory_creds_internal: uid %u for user %s doesn't "
575 "match stored uid %u. Replacing.\n",
576 (unsigned int)uid, username, (unsigned int)memcredp->uid ));
579 memcredp->ref_count++;
580 DEBUG(10,("winbindd_add_memory_creds_internal: ref count for user %s is now %d\n",
581 username, memcredp->ref_count ));
582 return winbindd_replace_memory_creds_internal(memcredp, pass);
585 memcredp = TALLOC_ZERO_P(NULL, struct WINBINDD_MEMORY_CREDS);
587 return NT_STATUS_NO_MEMORY;
589 memcredp->username = talloc_strdup(memcredp, username);
590 if (!memcredp->username) {
591 talloc_destroy(memcredp);
592 return NT_STATUS_NO_MEMORY;
595 status = store_memory_creds(memcredp, pass);
596 if (!NT_STATUS_IS_OK(status)) {
597 talloc_destroy(memcredp);
602 memcredp->ref_count = 1;
603 DLIST_ADD(memory_creds_list, memcredp);
605 DEBUG(10,("winbindd_add_memory_creds_internal: added entry for user %s\n",
612 /*************************************************************
613 Store users credentials in memory. If we also have a
614 struct WINBINDD_CCACHE_ENTRY for this username with a
615 refresh timer, then store the plaintext of the password
616 and associate the new credentials with the struct WINBINDD_CCACHE_ENTRY.
617 *************************************************************/
619 NTSTATUS winbindd_add_memory_creds(const char *username, uid_t uid, const char *pass)
621 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
624 status = winbindd_add_memory_creds_internal(username, uid, pass);
625 if (!NT_STATUS_IS_OK(status)) {
630 struct WINBINDD_MEMORY_CREDS *memcredp = find_memory_creds_by_name(username);
632 entry->cred_ptr = memcredp;
639 /*************************************************************
640 Decrement the in-memory ref count - delete if zero.
641 *************************************************************/
643 NTSTATUS winbindd_delete_memory_creds(const char *username)
645 struct WINBINDD_MEMORY_CREDS *memcredp = find_memory_creds_by_name(username);
646 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
647 NTSTATUS status = NT_STATUS_OK;
650 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
652 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
655 if (memcredp->ref_count <= 0) {
656 DEBUG(0,("winbindd_delete_memory_creds: logic error. ref count for user %s = %d\n",
657 username, memcredp->ref_count));
658 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
661 memcredp->ref_count--;
662 if (memcredp->ref_count <= 0) {
663 delete_memory_creds(memcredp);
664 DLIST_REMOVE(memory_creds_list, memcredp);
665 talloc_destroy(memcredp);
666 DEBUG(10,("winbindd_delete_memory_creds: deleted entry for user %s\n",
669 DEBUG(10,("winbindd_delete_memory_creds: entry for user %s ref_count now %d\n",
670 username, memcredp->ref_count));
674 entry->cred_ptr = NULL; /* Ensure we have no dangling references to this. */
679 /***********************************************************
680 Replace the required creds with new ones (password change).
681 ***********************************************************/
683 NTSTATUS winbindd_replace_memory_creds(const char *username, const char *pass)
685 struct WINBINDD_MEMORY_CREDS *memcredp = find_memory_creds_by_name(username);
688 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
690 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
693 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
696 return winbindd_replace_memory_creds_internal(memcredp, pass);