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 3 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, see <http://www.gnu.org/licenses/>.
27 #define DBGC_CLASS DBGC_WINBIND
29 /* uncomment this to do fast debugging on the krb5 ticket renewal event */
30 #ifdef DEBUG_KRB5_TKT_RENEWAL
31 #undef DEBUG_KRB5_TKT_RENEWAL
34 #define MAX_CCACHES 100
36 static struct WINBINDD_CCACHE_ENTRY
*ccache_list
;
38 /* The Krb5 ticket refresh handler should be scheduled
39 at one-half of the period from now till the tkt
41 #define KRB5_EVENT_REFRESH_TIME(x) ((x) - (((x) - time(NULL))/2))
43 /****************************************************************
44 Find an entry by name.
45 ****************************************************************/
47 static struct WINBINDD_CCACHE_ENTRY
*get_ccache_by_username(const char *username
)
49 struct WINBINDD_CCACHE_ENTRY
*entry
;
51 for (entry
= ccache_list
; entry
; entry
= entry
->next
) {
52 if (strequal(entry
->username
, username
)) {
59 /****************************************************************
61 ****************************************************************/
63 static int ccache_entry_count(void)
65 struct WINBINDD_CCACHE_ENTRY
*entry
;
68 for (entry
= ccache_list
; entry
; entry
= entry
->next
) {
74 /****************************************************************
75 Do the work of refreshing the ticket.
76 ****************************************************************/
78 static void krb5_ticket_refresh_handler(struct event_context
*event_ctx
,
79 struct timed_event
*te
,
80 const struct timeval
*now
,
83 struct WINBINDD_CCACHE_ENTRY
*entry
=
84 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
88 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
91 DEBUG(10,("krb5_ticket_refresh_handler called\n"));
92 DEBUGADD(10,("event called for: %s, %s\n",
93 entry
->ccname
, entry
->username
));
95 TALLOC_FREE(entry
->event
);
99 /* Kinit again if we have the user password and we can't renew the old
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
,
108 0, /* hm, can we do time correction here ? */
109 &entry
->refresh_time
,
112 False
, /* no PAC required anymore */
114 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
116 gain_root_privilege();
119 DEBUG(3,("krb5_ticket_refresh_handler: "
120 "could not re-kinit: %s\n",
121 error_message(ret
)));
122 TALLOC_FREE(entry
->event
);
126 DEBUG(10,("krb5_ticket_refresh_handler: successful re-kinit "
127 "for: %s in ccache: %s\n",
128 entry
->principal_name
, entry
->ccname
));
130 #if defined(DEBUG_KRB5_TKT_RENEWAL)
131 new_start
= time(NULL
) + 30;
133 /* The tkt should be refreshed at one-half the period
134 from now to the expiration time */
135 new_start
= KRB5_EVENT_REFRESH_TIME(entry
->refresh_time
);
140 set_effective_uid(entry
->uid
);
142 ret
= smb_krb5_renew_ticket(entry
->ccname
,
143 entry
->principal_name
,
146 #if defined(DEBUG_KRB5_TKT_RENEWAL)
147 new_start
= time(NULL
) + 30;
149 new_start
= KRB5_EVENT_REFRESH_TIME(new_start
);
152 gain_root_privilege();
155 DEBUG(3,("krb5_ticket_refresh_handler: "
156 "could not renew tickets: %s\n",
157 error_message(ret
)));
158 /* maybe we are beyond the renewing window */
160 /* avoid breaking the renewal chain: retry in
161 * lp_winbind_cache_time() seconds when the KDC was not
162 * available right now. */
164 if (ret
== KRB5_KDC_UNREACH
) {
165 new_start
= time(NULL
) +
166 MAX(30, lp_winbind_cache_time());
175 entry
->event
= event_add_timed(winbind_event_context(), entry
,
176 timeval_set(new_start
, 0),
177 "krb5_ticket_refresh_handler",
178 krb5_ticket_refresh_handler
,
184 /****************************************************************
185 Do the work of regaining a ticket when coming from offline auth.
186 ****************************************************************/
188 static void krb5_ticket_gain_handler(struct event_context
*event_ctx
,
189 struct timed_event
*te
,
190 const struct timeval
*now
,
193 struct WINBINDD_CCACHE_ENTRY
*entry
=
194 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
198 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
199 struct winbindd_domain
*domain
= NULL
;
202 DEBUG(10,("krb5_ticket_gain_handler called\n"));
203 DEBUGADD(10,("event called for: %s, %s\n",
204 entry
->ccname
, entry
->username
));
206 TALLOC_FREE(entry
->event
);
210 if (!cred_ptr
|| !cred_ptr
->pass
) {
211 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
215 if ((domain
= find_domain_from_name(entry
->realm
)) == NULL
) {
216 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
220 if (!domain
->online
) {
224 set_effective_uid(entry
->uid
);
226 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
228 0, /* hm, can we do time correction here ? */
229 &entry
->refresh_time
,
232 False
, /* no PAC required anymore */
234 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
236 gain_root_privilege();
239 DEBUG(3,("krb5_ticket_gain_handler: "
240 "could not kinit: %s\n",
241 error_message(ret
)));
245 DEBUG(10,("krb5_ticket_gain_handler: "
246 "successful kinit for: %s in ccache: %s\n",
247 entry
->principal_name
, entry
->ccname
));
253 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
255 entry
->event
= event_add_timed(winbind_event_context(),
258 "krb5_ticket_gain_handler",
259 krb5_ticket_gain_handler
,
266 #if defined(DEBUG_KRB5_TKT_RENEWAL)
267 t
= timeval_set(time(NULL
) + 30, 0);
269 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(entry
->refresh_time
), 0);
272 entry
->event
= event_add_timed(winbind_event_context(),
275 "krb5_ticket_refresh_handler",
276 krb5_ticket_refresh_handler
,
283 /****************************************************************
284 Check if an ccache entry exists.
285 ****************************************************************/
287 bool ccache_entry_exists(const char *username
)
289 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
290 return (entry
!= NULL
);
293 /****************************************************************
294 Ensure we're changing the correct entry.
295 ****************************************************************/
297 bool ccache_entry_identical(const char *username
,
301 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
307 if (entry
->uid
!= uid
) {
308 DEBUG(0,("cache_entry_identical: uid's differ: %u != %u\n",
309 (unsigned int)entry
->uid
, (unsigned int)uid
));
312 if (!strcsequal(entry
->ccname
, ccname
)) {
313 DEBUG(0,("cache_entry_identical: "
314 "ccnames differ: (cache) %s != (client) %s\n",
315 entry
->ccname
, ccname
));
321 NTSTATUS
add_ccache_to_list(const char *princ_name
,
324 const char *username
,
330 bool postponed_request
)
332 struct WINBINDD_CCACHE_ENTRY
*entry
= NULL
;
335 if ((username
== NULL
&& princ_name
== NULL
) ||
336 ccname
== NULL
|| uid
< 0) {
337 return NT_STATUS_INVALID_PARAMETER
;
340 if (ccache_entry_count() + 1 > MAX_CCACHES
) {
341 DEBUG(10,("add_ccache_to_list: "
342 "max number of ccaches reached\n"));
343 return NT_STATUS_NO_MORE_ENTRIES
;
346 /* Reference count old entries */
347 entry
= get_ccache_by_username(username
);
349 /* Check cached entries are identical. */
350 if (!ccache_entry_identical(username
, uid
, ccname
)) {
351 return NT_STATUS_INVALID_PARAMETER
;
354 DEBUG(10,("add_ccache_to_list: "
355 "ref count on entry %s is now %d\n",
356 username
, entry
->ref_count
));
357 /* FIXME: in this case we still might want to have a krb5 cred
358 * event handler created - gd*/
362 entry
= TALLOC_P(NULL
, struct WINBINDD_CCACHE_ENTRY
);
364 return NT_STATUS_NO_MEMORY
;
370 entry
->username
= talloc_strdup(entry
, username
);
371 if (!entry
->username
) {
376 entry
->principal_name
= talloc_strdup(entry
, princ_name
);
377 if (!entry
->principal_name
) {
382 entry
->service
= talloc_strdup(entry
, service
);
383 if (!entry
->service
) {
388 entry
->ccname
= talloc_strdup(entry
, ccname
);
389 if (!entry
->ccname
) {
393 entry
->realm
= talloc_strdup(entry
, realm
);
398 entry
->create_time
= create_time
;
399 entry
->renew_until
= renew_until
;
401 entry
->ref_count
= 1;
403 if (!lp_winbind_refresh_tickets() || renew_until
<= 0) {
407 if (postponed_request
) {
408 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
409 entry
->event
= event_add_timed(winbind_event_context(),
412 "krb5_ticket_gain_handler",
413 krb5_ticket_gain_handler
,
416 /* Renew at 1/2 the ticket expiration time */
417 #if defined(DEBUG_KRB5_TKT_RENEWAL)
418 t
= timeval_set(time(NULL
)+30, 0);
420 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end
), 0);
422 entry
->event
= event_add_timed(winbind_event_context(),
425 "krb5_ticket_refresh_handler",
426 krb5_ticket_refresh_handler
,
434 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
438 DLIST_ADD(ccache_list
, entry
);
440 DEBUG(10,("add_ccache_to_list: "
441 "added ccache [%s] for user [%s] to the list\n",
449 return NT_STATUS_NO_MEMORY
;
452 /*******************************************************************
453 Remove a WINBINDD_CCACHE_ENTRY entry and the krb5 ccache if no longer
455 *******************************************************************/
457 NTSTATUS
remove_ccache(const char *username
)
459 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
460 NTSTATUS status
= NT_STATUS_OK
;
466 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
469 if (entry
->ref_count
<= 0) {
470 DEBUG(0,("remove_ccache: logic error. "
471 "ref count for user %s = %d\n",
472 username
, entry
->ref_count
));
473 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
478 if (entry
->ref_count
> 0) {
479 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
480 username
, entry
->ref_count
));
484 /* no references any more */
486 DLIST_REMOVE(ccache_list
, entry
);
487 TALLOC_FREE(entry
->event
); /* unregisters events */
490 ret
= ads_kdestroy(entry
->ccname
);
492 /* we ignore the error when there has been no credential cache */
493 if (ret
== KRB5_FCC_NOFILE
) {
496 DEBUG(0,("remove_ccache: "
497 "failed to destroy user krb5 ccache %s with: %s\n",
498 entry
->ccname
, error_message(ret
)));
500 DEBUG(10,("remove_ccache: "
501 "successfully destroyed krb5 ccache %s for user %s\n",
502 entry
->ccname
, username
));
504 status
= krb5_to_nt_status(ret
);
508 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username
));
513 /*******************************************************************
514 In memory credentials cache code.
515 *******************************************************************/
517 static struct WINBINDD_MEMORY_CREDS
*memory_creds_list
;
519 /***********************************************************
520 Find an entry on the list by name.
521 ***********************************************************/
523 struct WINBINDD_MEMORY_CREDS
*find_memory_creds_by_name(const char *username
)
525 struct WINBINDD_MEMORY_CREDS
*p
;
527 for (p
= memory_creds_list
; p
; p
= p
->next
) {
528 if (strequal(p
->username
, username
)) {
535 /***********************************************************
536 Store the required creds and mlock them.
537 ***********************************************************/
539 static NTSTATUS
store_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
,
542 #if !defined(HAVE_MLOCK)
545 /* new_entry->nt_hash is the base pointer for the block
546 of memory pointed into by new_entry->lm_hash and
547 new_entry->pass (if we're storing plaintext). */
549 memcredp
->len
= NT_HASH_LEN
+ LM_HASH_LEN
;
551 memcredp
->len
+= strlen(pass
)+1;
556 /* aligning the memory on on x86_64 and compiling
557 with gcc 4.1 using -O2 causes a segv in the
558 next memset() --jerry */
559 memcredp
->nt_hash
= SMB_MALLOC_ARRAY(unsigned char, memcredp
->len
);
561 /* On non-linux platforms, mlock()'d memory must be aligned */
562 memcredp
->nt_hash
= SMB_MEMALIGN_ARRAY(unsigned char,
563 getpagesize(), memcredp
->len
);
565 if (!memcredp
->nt_hash
) {
566 return NT_STATUS_NO_MEMORY
;
568 memset(memcredp
->nt_hash
, 0x0, memcredp
->len
);
570 memcredp
->lm_hash
= memcredp
->nt_hash
+ NT_HASH_LEN
;
572 #ifdef DEBUG_PASSWORD
573 DEBUG(10,("mlocking memory: %p\n", memcredp
->nt_hash
));
575 if ((mlock(memcredp
->nt_hash
, memcredp
->len
)) == -1) {
576 DEBUG(0,("failed to mlock memory: %s (%d)\n",
577 strerror(errno
), errno
));
578 SAFE_FREE(memcredp
->nt_hash
);
579 return map_nt_error_from_unix(errno
);
582 #ifdef DEBUG_PASSWORD
583 DEBUG(10,("mlocked memory: %p\n", memcredp
->nt_hash
));
586 /* Create and store the password hashes. */
587 E_md4hash(pass
, memcredp
->nt_hash
);
588 E_deshash(pass
, memcredp
->lm_hash
);
591 memcredp
->pass
= (char *)memcredp
->lm_hash
+ LM_HASH_LEN
;
592 memcpy(memcredp
->pass
, pass
,
593 memcredp
->len
- NT_HASH_LEN
- LM_HASH_LEN
);
600 /***********************************************************
601 Destroy existing creds.
602 ***********************************************************/
604 static NTSTATUS
delete_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
)
606 #if !defined(HAVE_MUNLOCK)
609 if (munlock(memcredp
->nt_hash
, memcredp
->len
) == -1) {
610 DEBUG(0,("failed to munlock memory: %s (%d)\n",
611 strerror(errno
), errno
));
612 return map_nt_error_from_unix(errno
);
614 memset(memcredp
->nt_hash
, '\0', memcredp
->len
);
615 SAFE_FREE(memcredp
->nt_hash
);
616 memcredp
->nt_hash
= NULL
;
617 memcredp
->lm_hash
= NULL
;
618 memcredp
->pass
= NULL
;
624 /***********************************************************
625 Replace the required creds with new ones (password change).
626 ***********************************************************/
628 static NTSTATUS
winbindd_replace_memory_creds_internal(struct WINBINDD_MEMORY_CREDS
*memcredp
,
631 NTSTATUS status
= delete_memory_creds(memcredp
);
632 if (!NT_STATUS_IS_OK(status
)) {
635 return store_memory_creds(memcredp
, pass
);
638 /*************************************************************
639 Store credentials in memory in a list.
640 *************************************************************/
642 static NTSTATUS
winbindd_add_memory_creds_internal(const char *username
,
646 /* Shortcut to ensure we don't store if no mlock. */
647 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
651 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
653 memcredp
= find_memory_creds_by_name(username
);
654 if (uid
== (uid_t
)-1) {
655 DEBUG(0,("winbindd_add_memory_creds_internal: "
656 "invalid uid for user %s.\n", username
));
657 return NT_STATUS_INVALID_PARAMETER
;
661 /* Already exists. Increment the reference count and replace stored creds. */
662 if (uid
!= memcredp
->uid
) {
663 DEBUG(0,("winbindd_add_memory_creds_internal: "
664 "uid %u for user %s doesn't "
665 "match stored uid %u. Replacing.\n",
666 (unsigned int)uid
, username
,
667 (unsigned int)memcredp
->uid
));
670 memcredp
->ref_count
++;
671 DEBUG(10,("winbindd_add_memory_creds_internal: "
672 "ref count for user %s is now %d\n",
673 username
, memcredp
->ref_count
));
674 return winbindd_replace_memory_creds_internal(memcredp
, pass
);
677 memcredp
= TALLOC_ZERO_P(NULL
, struct WINBINDD_MEMORY_CREDS
);
679 return NT_STATUS_NO_MEMORY
;
681 memcredp
->username
= talloc_strdup(memcredp
, username
);
682 if (!memcredp
->username
) {
683 talloc_destroy(memcredp
);
684 return NT_STATUS_NO_MEMORY
;
687 status
= store_memory_creds(memcredp
, pass
);
688 if (!NT_STATUS_IS_OK(status
)) {
689 talloc_destroy(memcredp
);
694 memcredp
->ref_count
= 1;
695 DLIST_ADD(memory_creds_list
, memcredp
);
697 DEBUG(10,("winbindd_add_memory_creds_internal: "
698 "added entry for user %s\n", username
));
704 /*************************************************************
705 Store users credentials in memory. If we also have a
706 struct WINBINDD_CCACHE_ENTRY for this username with a
707 refresh timer, then store the plaintext of the password
708 and associate the new credentials with the struct WINBINDD_CCACHE_ENTRY.
709 *************************************************************/
711 NTSTATUS
winbindd_add_memory_creds(const char *username
,
715 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
718 status
= winbindd_add_memory_creds_internal(username
, uid
, pass
);
719 if (!NT_STATUS_IS_OK(status
)) {
724 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
725 memcredp
= find_memory_creds_by_name(username
);
727 entry
->cred_ptr
= memcredp
;
734 /*************************************************************
735 Decrement the in-memory ref count - delete if zero.
736 *************************************************************/
738 NTSTATUS
winbindd_delete_memory_creds(const char *username
)
740 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
741 struct WINBINDD_CCACHE_ENTRY
*entry
= NULL
;
742 NTSTATUS status
= NT_STATUS_OK
;
744 memcredp
= find_memory_creds_by_name(username
);
745 entry
= get_ccache_by_username(username
);
748 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
750 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
753 if (memcredp
->ref_count
<= 0) {
754 DEBUG(0,("winbindd_delete_memory_creds: logic error. "
755 "ref count for user %s = %d\n",
756 username
, memcredp
->ref_count
));
757 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
760 memcredp
->ref_count
--;
761 if (memcredp
->ref_count
<= 0) {
762 delete_memory_creds(memcredp
);
763 DLIST_REMOVE(memory_creds_list
, memcredp
);
764 talloc_destroy(memcredp
);
765 DEBUG(10,("winbindd_delete_memory_creds: "
766 "deleted entry for user %s\n",
769 DEBUG(10,("winbindd_delete_memory_creds: "
770 "entry for user %s ref_count now %d\n",
771 username
, memcredp
->ref_count
));
775 /* Ensure we have no dangling references to this. */
776 entry
->cred_ptr
= NULL
;
782 /***********************************************************
783 Replace the required creds with new ones (password change).
784 ***********************************************************/
786 NTSTATUS
winbindd_replace_memory_creds(const char *username
,
789 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
791 memcredp
= find_memory_creds_by_name(username
);
793 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
795 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
798 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
801 return winbindd_replace_memory_creds_internal(memcredp
, pass
);