r22712: Inform the user when logging in via pam_winbind
[Samba/gbeck.git] / source3 / nsswitch / winbindd_cred_cache.c
blob2c9572d7f86cfabdb669030045f2e843f77d7080
1 /*
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.
25 #include "includes.h"
26 #include "winbindd.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
30 /* uncomment this to to fast debugging on the krb5 ticket renewal event */
31 #ifdef DEBUG_KRB5_TKT_RENEWAL
32 #undef DEBUG_KRB5_TKT_RENEWAL
33 #endif
35 #define MAX_CCACHES 100
37 static struct WINBINDD_CCACHE_ENTRY *ccache_list;
39 /* The Krb5 ticket refresh handler should be scheduled
40 at one-half of the period from now till the tkt
41 expiration */
42 #define KRB5_EVENT_REFRESH_TIME(x) ((x) - (((x) - time(NULL))/2))
44 /****************************************************************
45 Find an entry by name.
46 ****************************************************************/
48 static struct WINBINDD_CCACHE_ENTRY *get_ccache_by_username(const char *username)
50 struct WINBINDD_CCACHE_ENTRY *entry;
52 for (entry = ccache_list; entry; entry = entry->next) {
53 if (strequal(entry->username, username)) {
54 return entry;
57 return NULL;
60 /****************************************************************
61 How many do we have ?
62 ****************************************************************/
64 static int ccache_entry_count(void)
66 struct WINBINDD_CCACHE_ENTRY *entry;
67 int i = 0;
69 for (entry = ccache_list; entry; entry = entry->next) {
70 i++;
72 return i;
75 /****************************************************************
76 Do the work of refreshing the ticket.
77 ****************************************************************/
79 static void krb5_ticket_refresh_handler(struct event_context *event_ctx,
80 struct timed_event *te,
81 const struct timeval *now,
82 void *private_data)
84 struct WINBINDD_CCACHE_ENTRY *entry =
85 talloc_get_type_abort(private_data, struct WINBINDD_CCACHE_ENTRY);
86 #ifdef HAVE_KRB5
87 int ret;
88 time_t new_start;
89 struct WINBINDD_MEMORY_CREDS *cred_ptr = entry->cred_ptr;
90 #endif
92 DEBUG(10,("krb5_ticket_refresh_handler called\n"));
93 DEBUGADD(10,("event called for: %s, %s\n", entry->ccname, entry->username));
95 TALLOC_FREE(entry->event);
97 #ifdef HAVE_KRB5
99 /* Kinit again if we have the user password and we can't renew the old
100 * tgt anymore */
102 if ((entry->renew_until < time(NULL)) && cred_ptr && cred_ptr->pass) {
104 set_effective_uid(entry->uid);
106 ret = kerberos_kinit_password_ext(entry->principal_name,
107 cred_ptr->pass,
108 0, /* hm, can we do time correction here ? */
109 &entry->refresh_time,
110 &entry->renew_until,
111 entry->ccname,
112 False, /* no PAC required anymore */
113 True,
114 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
115 NULL);
116 gain_root_privilege();
118 if (ret) {
119 DEBUG(3,("krb5_ticket_refresh_handler: could not re-kinit: %s\n",
120 error_message(ret)));
121 TALLOC_FREE(entry->event);
122 return;
125 DEBUG(10,("krb5_ticket_refresh_handler: successful re-kinit "
126 "for: %s in ccache: %s\n",
127 entry->principal_name, entry->ccname));
129 #if defined(DEBUG_KRB5_TKT_RENEWAL)
130 new_start = time(NULL) + 30;
131 #else
132 /* The tkt should be refreshed at one-half the period
133 from now to the expiration time */
134 new_start = KRB5_EVENT_REFRESH_TIME(entry->refresh_time);
135 #endif
137 goto done;
140 set_effective_uid(entry->uid);
142 ret = smb_krb5_renew_ticket(entry->ccname,
143 entry->principal_name,
144 entry->service,
145 &new_start);
146 #if defined(DEBUG_KRB5_TKT_RENEWAL)
147 new_start = time(NULL) + 30;
148 #else
149 new_start = KRB5_EVENT_REFRESH_TIME(new_start);
150 #endif
152 gain_root_privilege();
154 if (ret) {
155 DEBUG(3,("krb5_ticket_refresh_handler: could not renew tickets: %s\n",
156 error_message(ret)));
157 /* maybe we are beyond the renewing window */
159 /* avoid breaking the renewal chain: retry in lp_winbind_cache_time()
160 * seconds when the KDC was not available right now. */
162 if (ret == KRB5_KDC_UNREACH) {
163 new_start = time(NULL) + MAX(30, lp_winbind_cache_time());
164 goto done;
167 return;
170 done:
172 entry->event = event_add_timed(winbind_event_context(), entry,
173 timeval_set(new_start, 0),
174 "krb5_ticket_refresh_handler",
175 krb5_ticket_refresh_handler,
176 entry);
178 #endif
181 /****************************************************************
182 Do the work of regaining a ticket when coming from offline auth.
183 ****************************************************************/
185 static void krb5_ticket_gain_handler(struct event_context *event_ctx,
186 struct timed_event *te,
187 const struct timeval *now,
188 void *private_data)
190 struct WINBINDD_CCACHE_ENTRY *entry =
191 talloc_get_type_abort(private_data, struct WINBINDD_CCACHE_ENTRY);
192 #ifdef HAVE_KRB5
193 int ret;
194 struct timeval t;
195 struct WINBINDD_MEMORY_CREDS *cred_ptr = entry->cred_ptr;
196 struct winbindd_domain *domain = NULL;
197 #endif
199 DEBUG(10,("krb5_ticket_gain_handler called\n"));
200 DEBUGADD(10,("event called for: %s, %s\n", entry->ccname, entry->username));
202 TALLOC_FREE(entry->event);
204 #ifdef HAVE_KRB5
206 if (!cred_ptr || !cred_ptr->pass) {
207 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
208 return;
211 if ((domain = find_domain_from_name(entry->realm)) == NULL) {
212 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
213 return;
216 if (domain->online) {
218 set_effective_uid(entry->uid);
220 ret = kerberos_kinit_password_ext(entry->principal_name,
221 cred_ptr->pass,
222 0, /* hm, can we do time correction here ? */
223 &entry->refresh_time,
224 &entry->renew_until,
225 entry->ccname,
226 False, /* no PAC required anymore */
227 True,
228 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
229 NULL);
230 gain_root_privilege();
232 if (ret) {
233 DEBUG(3,("krb5_ticket_gain_handler: could not kinit: %s\n",
234 error_message(ret)));
235 goto retry_later;
238 DEBUG(10,("krb5_ticket_gain_handler: successful kinit for: %s in ccache: %s\n",
239 entry->principal_name, entry->ccname));
241 goto got_ticket;
244 retry_later:
246 entry->event = event_add_timed(winbind_event_context(), entry,
247 timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0),
248 "krb5_ticket_gain_handler",
249 krb5_ticket_gain_handler,
250 entry);
252 return;
254 got_ticket:
256 #if defined(DEBUG_KRB5_TKT_RENEWAL)
257 t = timeval_set(time(NULL) + 30, 0);
258 #else
259 t = timeval_set(KRB5_EVENT_REFRESH_TIME(entry->refresh_time), 0);
260 #endif
262 entry->event = event_add_timed(winbind_event_context(), entry,
264 "krb5_ticket_refresh_handler",
265 krb5_ticket_refresh_handler,
266 entry);
268 return;
269 #endif
272 /****************************************************************
273 Check if an ccache entry exists.
274 ****************************************************************/
276 BOOL ccache_entry_exists(const char *username)
278 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
279 return (entry != NULL);
282 /****************************************************************
283 Ensure we're changing the correct entry.
284 ****************************************************************/
286 BOOL ccache_entry_identical(const char *username, uid_t uid, const char *ccname)
288 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
290 if (!entry) {
291 return False;
294 if (entry->uid != uid) {
295 DEBUG(0,("cache_entry_identical: uid's differ: %u != %u\n",
296 (unsigned int)entry->uid, (unsigned int)uid ));
297 return False;
299 if (!strcsequal(entry->ccname, ccname)) {
300 DEBUG(0,("cache_entry_identical: ccnames differ: (cache) %s != (client) %s\n",
301 entry->ccname, ccname));
302 return False;
304 return True;
307 NTSTATUS add_ccache_to_list(const char *princ_name,
308 const char *ccname,
309 const char *service,
310 const char *username,
311 const char *realm,
312 uid_t uid,
313 time_t create_time,
314 time_t ticket_end,
315 time_t renew_until,
316 BOOL postponed_request)
318 struct WINBINDD_CCACHE_ENTRY *entry = NULL;
320 if ((username == NULL && princ_name == NULL) || ccname == NULL || uid < 0) {
321 return NT_STATUS_INVALID_PARAMETER;
324 if (ccache_entry_count() + 1 > MAX_CCACHES) {
325 DEBUG(10,("add_ccache_to_list: max number of ccaches reached\n"));
326 return NT_STATUS_NO_MORE_ENTRIES;
329 /* Reference count old entries */
330 entry = get_ccache_by_username(username);
331 if (entry) {
332 /* Check cached entries are identical. */
333 if (!ccache_entry_identical(username, uid, ccname)) {
334 return NT_STATUS_INVALID_PARAMETER;
336 entry->ref_count++;
337 DEBUG(10,("add_ccache_to_list: ref count on entry %s is now %d\n",
338 username, entry->ref_count));
339 /* FIXME: in this case we still might want to have a krb5 cred
340 * event handler created - gd*/
341 return NT_STATUS_OK;
344 entry = TALLOC_P(NULL, struct WINBINDD_CCACHE_ENTRY);
345 if (!entry) {
346 return NT_STATUS_NO_MEMORY;
349 ZERO_STRUCTP(entry);
351 if (username) {
352 entry->username = talloc_strdup(entry, username);
353 if (!entry->username) {
354 goto no_mem;
357 if (princ_name) {
358 entry->principal_name = talloc_strdup(entry, princ_name);
359 if (!entry->principal_name) {
360 goto no_mem;
363 if (service) {
364 entry->service = talloc_strdup(entry, service);
365 if (!entry->service) {
366 goto no_mem;
370 entry->ccname = talloc_strdup(entry, ccname);
371 if (!entry->ccname) {
372 goto no_mem;
375 entry->realm = talloc_strdup(entry, realm);
376 if (!entry->realm) {
377 goto no_mem;
380 entry->create_time = create_time;
381 entry->renew_until = renew_until;
382 entry->uid = uid;
383 entry->ref_count = 1;
385 if (lp_winbind_refresh_tickets() && renew_until > 0) {
386 if (postponed_request) {
387 entry->event = event_add_timed(winbind_event_context(), entry,
388 timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0),
389 "krb5_ticket_gain_handler",
390 krb5_ticket_gain_handler,
391 entry);
392 } else {
393 /* Renew at 1/2 the ticket expiration time */
394 entry->event = event_add_timed(winbind_event_context(), entry,
395 #if defined(DEBUG_KRB5_TKT_RENEWAL)
396 timeval_set(time(NULL)+30, 0),
397 #else
398 timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end), 0),
399 #endif
400 "krb5_ticket_refresh_handler",
401 krb5_ticket_refresh_handler,
402 entry);
405 if (!entry->event) {
406 goto no_mem;
409 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
412 DLIST_ADD(ccache_list, entry);
414 DEBUG(10,("add_ccache_to_list: added ccache [%s] for user [%s] to the list\n", ccname, username));
416 return NT_STATUS_OK;
418 no_mem:
420 TALLOC_FREE(entry);
421 return NT_STATUS_NO_MEMORY;
424 /*******************************************************************
425 Remove a WINBINDD_CCACHE_ENTRY entry and the krb5 ccache if no longer referenced.
426 *******************************************************************/
428 NTSTATUS remove_ccache(const char *username)
430 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
431 NTSTATUS status;
432 #ifdef HAVE_KRB5
433 krb5_error_code ret;
434 #endif
436 if (!entry) {
437 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
440 if (entry->ref_count <= 0) {
441 DEBUG(0,("remove_ccache: logic error. ref count for user %s = %d\n",
442 username, entry->ref_count));
443 return NT_STATUS_INTERNAL_DB_CORRUPTION;
446 entry->ref_count--;
448 if (entry->ref_count > 0) {
449 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
450 username, entry->ref_count ));
451 return NT_STATUS_OK;
454 /* no references any more */
456 DLIST_REMOVE(ccache_list, entry);
457 TALLOC_FREE(entry->event); /* unregisters events */
459 #ifdef HAVE_KRB5
460 ret = ads_kdestroy(entry->ccname);
462 /* we ignore the error when there has been no credential cache */
463 if (ret == KRB5_FCC_NOFILE) {
464 ret = 0;
465 } else if (ret) {
466 DEBUG(0,("remove_ccache: failed to destroy user krb5 ccache %s with: %s\n",
467 entry->ccname, error_message(ret)));
468 } else {
469 DEBUG(10,("remove_ccache: successfully destroyed krb5 ccache %s for user %s\n",
470 entry->ccname, username));
472 status = krb5_to_nt_status(ret);
473 #endif
475 TALLOC_FREE(entry);
476 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username));
478 return status;
481 /*******************************************************************
482 In memory credentials cache code.
483 *******************************************************************/
485 static struct WINBINDD_MEMORY_CREDS *memory_creds_list;
487 /***********************************************************
488 Find an entry on the list by name.
489 ***********************************************************/
491 struct WINBINDD_MEMORY_CREDS *find_memory_creds_by_name(const char *username)
493 struct WINBINDD_MEMORY_CREDS *p;
495 for (p = memory_creds_list; p; p = p->next) {
496 if (strequal(p->username, username)) {
497 return p;
500 return NULL;
503 /***********************************************************
504 Store the required creds and mlock them.
505 ***********************************************************/
507 static NTSTATUS store_memory_creds(struct WINBINDD_MEMORY_CREDS *memcredp, const char *pass)
509 #if !defined(HAVE_MLOCK)
510 return NT_STATUS_OK;
511 #else
512 /* new_entry->nt_hash is the base pointer for the block
513 of memory pointed into by new_entry->lm_hash and
514 new_entry->pass (if we're storing plaintext). */
516 memcredp->len = NT_HASH_LEN + LM_HASH_LEN;
517 if (pass) {
518 memcredp->len += strlen(pass)+1;
522 #if defined(LINUX)
523 /* aligning the memory on on x86_64 and compiling
524 with gcc 4.1 using -O2 causes a segv in the
525 next memset() --jerry */
526 memcredp->nt_hash = SMB_MALLOC_ARRAY(unsigned char, memcredp->len);
527 #else
528 /* On non-linux platforms, mlock()'d memory must be aligned */
529 memcredp->nt_hash = SMB_MEMALIGN_ARRAY(unsigned char,
530 getpagesize(), memcredp->len);
531 #endif
532 if (!memcredp->nt_hash) {
533 return NT_STATUS_NO_MEMORY;
535 memset( memcredp->nt_hash, 0x0, memcredp->len );
537 memcredp->lm_hash = memcredp->nt_hash + NT_HASH_LEN;
539 #ifdef DEBUG_PASSWORD
540 DEBUG(10,("mlocking memory: %p\n", memcredp->nt_hash));
541 #endif
542 if ((mlock(memcredp->nt_hash, memcredp->len)) == -1) {
543 DEBUG(0,("failed to mlock memory: %s (%d)\n",
544 strerror(errno), errno));
545 SAFE_FREE(memcredp->nt_hash);
546 return map_nt_error_from_unix(errno);
549 #ifdef DEBUG_PASSWORD
550 DEBUG(10,("mlocked memory: %p\n", memcredp->nt_hash));
551 #endif
553 /* Create and store the password hashes. */
554 E_md4hash(pass, memcredp->nt_hash);
555 E_deshash(pass, memcredp->lm_hash);
557 if (pass) {
558 memcredp->pass = (char *)memcredp->lm_hash + LM_HASH_LEN;
559 memcpy(memcredp->pass, pass, memcredp->len - NT_HASH_LEN - LM_HASH_LEN);
562 return NT_STATUS_OK;
563 #endif
566 /***********************************************************
567 Destroy existing creds.
568 ***********************************************************/
570 static NTSTATUS delete_memory_creds(struct WINBINDD_MEMORY_CREDS *memcredp)
572 #if !defined(HAVE_MUNLOCK)
573 return NT_STATUS_OK;
574 #else
575 if (munlock(memcredp->nt_hash, memcredp->len) == -1) {
576 DEBUG(0,("failed to munlock memory: %s (%d)\n",
577 strerror(errno), errno));
578 return map_nt_error_from_unix(errno);
580 memset(memcredp->nt_hash, '\0', memcredp->len);
581 SAFE_FREE(memcredp->nt_hash);
582 memcredp->nt_hash = NULL;
583 memcredp->lm_hash = NULL;
584 memcredp->pass = NULL;
585 memcredp->len = 0;
586 return NT_STATUS_OK;
587 #endif
590 /***********************************************************
591 Replace the required creds with new ones (password change).
592 ***********************************************************/
594 static NTSTATUS winbindd_replace_memory_creds_internal(struct WINBINDD_MEMORY_CREDS *memcredp,
595 const char *pass)
597 NTSTATUS status = delete_memory_creds(memcredp);
598 if (!NT_STATUS_IS_OK(status)) {
599 return status;
601 return store_memory_creds(memcredp, pass);
604 /*************************************************************
605 Store credentials in memory in a list.
606 *************************************************************/
608 static NTSTATUS winbindd_add_memory_creds_internal(const char *username, uid_t uid, const char *pass)
610 /* Shortcut to ensure we don't store if no mlock. */
611 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
612 return NT_STATUS_OK;
613 #else
614 NTSTATUS status;
615 struct WINBINDD_MEMORY_CREDS *memcredp = find_memory_creds_by_name(username);
617 if (uid == (uid_t)-1) {
618 DEBUG(0,("winbindd_add_memory_creds_internal: invalid uid for user %s.\n",
619 username ));
620 return NT_STATUS_INVALID_PARAMETER;
623 if (memcredp) {
624 /* Already exists. Increment the reference count and replace stored creds. */
625 if (uid != memcredp->uid) {
626 DEBUG(0,("winbindd_add_memory_creds_internal: uid %u for user %s doesn't "
627 "match stored uid %u. Replacing.\n",
628 (unsigned int)uid, username, (unsigned int)memcredp->uid ));
629 memcredp->uid = uid;
631 memcredp->ref_count++;
632 DEBUG(10,("winbindd_add_memory_creds_internal: ref count for user %s is now %d\n",
633 username, memcredp->ref_count ));
634 return winbindd_replace_memory_creds_internal(memcredp, pass);
637 memcredp = TALLOC_ZERO_P(NULL, struct WINBINDD_MEMORY_CREDS);
638 if (!memcredp) {
639 return NT_STATUS_NO_MEMORY;
641 memcredp->username = talloc_strdup(memcredp, username);
642 if (!memcredp->username) {
643 talloc_destroy(memcredp);
644 return NT_STATUS_NO_MEMORY;
647 status = store_memory_creds(memcredp, pass);
648 if (!NT_STATUS_IS_OK(status)) {
649 talloc_destroy(memcredp);
650 return status;
653 memcredp->uid = uid;
654 memcredp->ref_count = 1;
655 DLIST_ADD(memory_creds_list, memcredp);
657 DEBUG(10,("winbindd_add_memory_creds_internal: added entry for user %s\n",
658 username ));
660 return NT_STATUS_OK;
661 #endif
664 /*************************************************************
665 Store users credentials in memory. If we also have a
666 struct WINBINDD_CCACHE_ENTRY for this username with a
667 refresh timer, then store the plaintext of the password
668 and associate the new credentials with the struct WINBINDD_CCACHE_ENTRY.
669 *************************************************************/
671 NTSTATUS winbindd_add_memory_creds(const char *username, uid_t uid, const char *pass)
673 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
674 NTSTATUS status;
676 status = winbindd_add_memory_creds_internal(username, uid, pass);
677 if (!NT_STATUS_IS_OK(status)) {
678 return status;
681 if (entry) {
682 struct WINBINDD_MEMORY_CREDS *memcredp = find_memory_creds_by_name(username);
683 if (memcredp) {
684 entry->cred_ptr = memcredp;
688 return status;
691 /*************************************************************
692 Decrement the in-memory ref count - delete if zero.
693 *************************************************************/
695 NTSTATUS winbindd_delete_memory_creds(const char *username)
697 struct WINBINDD_MEMORY_CREDS *memcredp = find_memory_creds_by_name(username);
698 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
699 NTSTATUS status = NT_STATUS_OK;
701 if (!memcredp) {
702 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
703 username ));
704 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
707 if (memcredp->ref_count <= 0) {
708 DEBUG(0,("winbindd_delete_memory_creds: logic error. ref count for user %s = %d\n",
709 username, memcredp->ref_count));
710 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
713 memcredp->ref_count--;
714 if (memcredp->ref_count <= 0) {
715 delete_memory_creds(memcredp);
716 DLIST_REMOVE(memory_creds_list, memcredp);
717 talloc_destroy(memcredp);
718 DEBUG(10,("winbindd_delete_memory_creds: deleted entry for user %s\n",
719 username));
720 } else {
721 DEBUG(10,("winbindd_delete_memory_creds: entry for user %s ref_count now %d\n",
722 username, memcredp->ref_count));
725 if (entry) {
726 entry->cred_ptr = NULL; /* Ensure we have no dangling references to this. */
728 return status;
731 /***********************************************************
732 Replace the required creds with new ones (password change).
733 ***********************************************************/
735 NTSTATUS winbindd_replace_memory_creds(const char *username, const char *pass)
737 struct WINBINDD_MEMORY_CREDS *memcredp = find_memory_creds_by_name(username);
739 if (!memcredp) {
740 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
741 username ));
742 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
745 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
746 username ));
748 return winbindd_replace_memory_creds_internal(memcredp, pass);