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 /* uncomment this to to fast debugging on the krb5 ticket renewal event */
31 #ifdef DEBUG_KRB5_TKT_RENEWAL
32 #undef DEBUG_KRB5_TKT_RENEWAL
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
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
)) {
60 /****************************************************************
62 ****************************************************************/
64 static int ccache_entry_count(void)
66 struct WINBINDD_CCACHE_ENTRY
*entry
;
69 for (entry
= ccache_list
; entry
; entry
= entry
->next
) {
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
,
84 struct WINBINDD_CCACHE_ENTRY
*entry
=
85 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
89 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
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
);
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
);
115 gain_root_privilege();
118 DEBUG(3,("krb5_ticket_refresh_handler: could not re-kinit: %s\n",
119 error_message(ret
)));
120 TALLOC_FREE(entry
->event
);
124 DEBUG(10,("krb5_ticket_refresh_handler: successful re-kinit "
125 "for: %s in ccache: %s\n",
126 entry
->principal_name
, entry
->ccname
));
128 #if defined(DEBUG_KRB5_TKT_RENEWAL)
129 new_start
= time(NULL
) + 30;
131 /* The tkt should be refreshed at one-half the period
132 from now to the expiration time */
133 new_start
= KRB5_EVENT_REFRESH_TIME(entry
->refresh_time
);
139 set_effective_uid(entry
->uid
);
141 ret
= smb_krb5_renew_ticket(entry
->ccname
,
142 entry
->principal_name
,
145 #if defined(DEBUG_KRB5_TKT_RENEWAL)
146 new_start
= time(NULL
) + 30;
148 new_start
= KRB5_EVENT_REFRESH_TIME(new_start
);
151 gain_root_privilege();
154 DEBUG(3,("krb5_ticket_refresh_handler: could not renew tickets: %s\n",
155 error_message(ret
)));
156 /* maybe we are beyond the renewing window */
158 /* avoid breaking the renewal chain: retry in lp_winbind_cache_time()
159 * seconds when the KDC was not available right now. */
161 if (ret
== KRB5_KDC_UNREACH
) {
162 new_start
= time(NULL
) + MAX(30, lp_winbind_cache_time());
171 entry
->event
= event_add_timed(winbind_event_context(), entry
,
172 timeval_set(new_start
, 0),
173 "krb5_ticket_refresh_handler",
174 krb5_ticket_refresh_handler
,
180 /****************************************************************
181 Do the work of regaining a ticket when coming from offline auth.
182 ****************************************************************/
184 static void krb5_ticket_gain_handler(struct event_context
*event_ctx
,
185 struct timed_event
*te
,
186 const struct timeval
*now
,
189 struct WINBINDD_CCACHE_ENTRY
*entry
=
190 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
194 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
195 struct winbindd_domain
*domain
= NULL
;
198 DEBUG(10,("krb5_ticket_gain_handler called\n"));
199 DEBUGADD(10,("event called for: %s, %s\n", entry
->ccname
, entry
->username
));
201 TALLOC_FREE(entry
->event
);
205 if (!cred_ptr
|| !cred_ptr
->pass
) {
206 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
210 if ((domain
= find_domain_from_name(entry
->realm
)) == NULL
) {
211 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
215 if (domain
->online
) {
217 set_effective_uid(entry
->uid
);
219 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
221 0, /* hm, can we do time correction here ? */
222 &entry
->refresh_time
,
225 False
, /* no PAC required anymore */
227 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
);
228 gain_root_privilege();
231 DEBUG(3,("krb5_ticket_gain_handler: could not kinit: %s\n",
232 error_message(ret
)));
236 DEBUG(10,("krb5_ticket_gain_handler: successful kinit for: %s in ccache: %s\n",
237 entry
->principal_name
, entry
->ccname
));
244 entry
->event
= event_add_timed(winbind_event_context(), entry
,
245 timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0),
246 "krb5_ticket_gain_handler",
247 krb5_ticket_gain_handler
,
254 #if defined(DEBUG_KRB5_TKT_RENEWAL)
255 t
= timeval_set(time(NULL
) + 30, 0);
257 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(entry
->refresh_time
), 0);
260 entry
->event
= event_add_timed(winbind_event_context(), entry
,
262 "krb5_ticket_refresh_handler",
263 krb5_ticket_refresh_handler
,
270 /****************************************************************
271 Check if an ccache entry exists.
272 ****************************************************************/
274 BOOL
ccache_entry_exists(const char *username
)
276 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
277 return (entry
!= NULL
);
280 /****************************************************************
281 Ensure we're changing the correct entry.
282 ****************************************************************/
284 BOOL
ccache_entry_identical(const char *username
, uid_t uid
, const char *ccname
)
286 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
292 if (entry
->uid
!= uid
) {
293 DEBUG(0,("cache_entry_identical: uid's differ: %u != %u\n",
294 (unsigned int)entry
->uid
, (unsigned int)uid
));
297 if (!strcsequal(entry
->ccname
, ccname
)) {
298 DEBUG(0,("cache_entry_identical: ccnames differ: (cache) %s != (client) %s\n",
299 entry
->ccname
, ccname
));
305 NTSTATUS
add_ccache_to_list(const char *princ_name
,
308 const char *username
,
314 BOOL postponed_request
)
316 struct WINBINDD_CCACHE_ENTRY
*entry
= NULL
;
318 if ((username
== NULL
&& princ_name
== NULL
) || ccname
== NULL
|| uid
< 0) {
319 return NT_STATUS_INVALID_PARAMETER
;
322 if (ccache_entry_count() + 1 > MAX_CCACHES
) {
323 DEBUG(10,("add_ccache_to_list: max number of ccaches reached\n"));
324 return NT_STATUS_NO_MORE_ENTRIES
;
327 /* Reference count old entries */
328 entry
= get_ccache_by_username(username
);
330 /* Check cached entries are identical. */
331 if (!ccache_entry_identical(username
, uid
, ccname
)) {
332 return NT_STATUS_INVALID_PARAMETER
;
335 DEBUG(10,("add_ccache_to_list: ref count on entry %s is now %d\n",
336 username
, entry
->ref_count
));
337 /* FIXME: in this case we still might want to have a krb5 cred
338 * event handler created - gd*/
342 entry
= TALLOC_P(NULL
, struct WINBINDD_CCACHE_ENTRY
);
344 return NT_STATUS_NO_MEMORY
;
350 entry
->username
= talloc_strdup(entry
, username
);
351 if (!entry
->username
) {
356 entry
->principal_name
= talloc_strdup(entry
, princ_name
);
357 if (!entry
->principal_name
) {
362 entry
->service
= talloc_strdup(entry
, service
);
363 if (!entry
->service
) {
368 entry
->ccname
= talloc_strdup(entry
, ccname
);
369 if (!entry
->ccname
) {
373 entry
->realm
= talloc_strdup(entry
, realm
);
378 entry
->create_time
= create_time
;
379 entry
->renew_until
= renew_until
;
381 entry
->ref_count
= 1;
383 if (lp_winbind_refresh_tickets() && renew_until
> 0) {
384 if (postponed_request
) {
385 entry
->event
= event_add_timed(winbind_event_context(), entry
,
386 timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0),
387 "krb5_ticket_gain_handler",
388 krb5_ticket_gain_handler
,
391 /* Renew at 1/2 the ticket expiration time */
392 entry
->event
= event_add_timed(winbind_event_context(), entry
,
393 #if defined(DEBUG_KRB5_TKT_RENEWAL)
394 timeval_set(time(NULL
)+30, 0),
396 timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end
), 0),
398 "krb5_ticket_refresh_handler",
399 krb5_ticket_refresh_handler
,
407 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
410 DLIST_ADD(ccache_list
, entry
);
412 DEBUG(10,("add_ccache_to_list: added ccache [%s] for user [%s] to the list\n", ccname
, username
));
419 return NT_STATUS_NO_MEMORY
;
422 /*******************************************************************
423 Remove a WINBINDD_CCACHE_ENTRY entry and the krb5 ccache if no longer referenced.
424 *******************************************************************/
426 NTSTATUS
remove_ccache(const char *username
)
428 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
429 NTSTATUS status
= NT_STATUS_OK
;
435 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
438 if (entry
->ref_count
<= 0) {
439 DEBUG(0,("remove_ccache: logic error. ref count for user %s = %d\n",
440 username
, entry
->ref_count
));
441 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
446 if (entry
->ref_count
> 0) {
447 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
448 username
, entry
->ref_count
));
452 /* no references any more */
454 DLIST_REMOVE(ccache_list
, entry
);
455 TALLOC_FREE(entry
->event
); /* unregisters events */
458 ret
= ads_kdestroy(entry
->ccname
);
460 /* we ignore the error when there has been no credential cache */
461 if (ret
== KRB5_FCC_NOFILE
) {
464 DEBUG(0,("remove_ccache: failed to destroy user krb5 ccache %s with: %s\n",
465 entry
->ccname
, error_message(ret
)));
467 DEBUG(10,("remove_ccache: successfully destroyed krb5 ccache %s for user %s\n",
468 entry
->ccname
, username
));
470 status
= krb5_to_nt_status(ret
);
474 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username
));
479 /*******************************************************************
480 In memory credentials cache code.
481 *******************************************************************/
483 static struct WINBINDD_MEMORY_CREDS
*memory_creds_list
;
485 /***********************************************************
486 Find an entry on the list by name.
487 ***********************************************************/
489 struct WINBINDD_MEMORY_CREDS
*find_memory_creds_by_name(const char *username
)
491 struct WINBINDD_MEMORY_CREDS
*p
;
493 for (p
= memory_creds_list
; p
; p
= p
->next
) {
494 if (strequal(p
->username
, username
)) {
501 /***********************************************************
502 Store the required creds and mlock them.
503 ***********************************************************/
505 static NTSTATUS
store_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
, const char *pass
)
507 #if !defined(HAVE_MLOCK)
510 /* new_entry->nt_hash is the base pointer for the block
511 of memory pointed into by new_entry->lm_hash and
512 new_entry->pass (if we're storing plaintext). */
514 memcredp
->len
= NT_HASH_LEN
+ LM_HASH_LEN
;
516 memcredp
->len
+= strlen(pass
)+1;
521 /* aligning the memory on on x86_64 and compiling
522 with gcc 4.1 using -O2 causes a segv in the
523 next memset() --jerry */
524 memcredp
->nt_hash
= SMB_MALLOC_ARRAY(unsigned char, memcredp
->len
);
526 /* On non-linux platforms, mlock()'d memory must be aligned */
527 memcredp
->nt_hash
= SMB_MEMALIGN_ARRAY(unsigned char,
528 getpagesize(), memcredp
->len
);
530 if (!memcredp
->nt_hash
) {
531 return NT_STATUS_NO_MEMORY
;
533 memset( memcredp
->nt_hash
, 0x0, memcredp
->len
);
535 memcredp
->lm_hash
= memcredp
->nt_hash
+ NT_HASH_LEN
;
537 #ifdef DEBUG_PASSWORD
538 DEBUG(10,("mlocking memory: %p\n", memcredp
->nt_hash
));
540 if ((mlock(memcredp
->nt_hash
, memcredp
->len
)) == -1) {
541 DEBUG(0,("failed to mlock memory: %s (%d)\n",
542 strerror(errno
), errno
));
543 SAFE_FREE(memcredp
->nt_hash
);
544 return map_nt_error_from_unix(errno
);
547 #ifdef DEBUG_PASSWORD
548 DEBUG(10,("mlocked memory: %p\n", memcredp
->nt_hash
));
551 /* Create and store the password hashes. */
552 E_md4hash(pass
, memcredp
->nt_hash
);
553 E_deshash(pass
, memcredp
->lm_hash
);
556 memcredp
->pass
= (char *)memcredp
->lm_hash
+ LM_HASH_LEN
;
557 memcpy(memcredp
->pass
, pass
, memcredp
->len
- NT_HASH_LEN
- LM_HASH_LEN
);
564 /***********************************************************
565 Destroy existing creds.
566 ***********************************************************/
568 static NTSTATUS
delete_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
)
570 #if !defined(HAVE_MUNLOCK)
573 if (munlock(memcredp
->nt_hash
, memcredp
->len
) == -1) {
574 DEBUG(0,("failed to munlock memory: %s (%d)\n",
575 strerror(errno
), errno
));
576 return map_nt_error_from_unix(errno
);
578 memset(memcredp
->nt_hash
, '\0', memcredp
->len
);
579 SAFE_FREE(memcredp
->nt_hash
);
580 memcredp
->nt_hash
= NULL
;
581 memcredp
->lm_hash
= NULL
;
582 memcredp
->pass
= NULL
;
588 /***********************************************************
589 Replace the required creds with new ones (password change).
590 ***********************************************************/
592 static NTSTATUS
winbindd_replace_memory_creds_internal(struct WINBINDD_MEMORY_CREDS
*memcredp
,
595 NTSTATUS status
= delete_memory_creds(memcredp
);
596 if (!NT_STATUS_IS_OK(status
)) {
599 return store_memory_creds(memcredp
, pass
);
602 /*************************************************************
603 Store credentials in memory in a list.
604 *************************************************************/
606 static NTSTATUS
winbindd_add_memory_creds_internal(const char *username
, uid_t uid
, const char *pass
)
608 /* Shortcut to ensure we don't store if no mlock. */
609 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
613 struct WINBINDD_MEMORY_CREDS
*memcredp
= find_memory_creds_by_name(username
);
615 if (uid
== (uid_t
)-1) {
616 DEBUG(0,("winbindd_add_memory_creds_internal: invalid uid for user %s.\n",
618 return NT_STATUS_INVALID_PARAMETER
;
622 /* Already exists. Increment the reference count and replace stored creds. */
623 if (uid
!= memcredp
->uid
) {
624 DEBUG(0,("winbindd_add_memory_creds_internal: uid %u for user %s doesn't "
625 "match stored uid %u. Replacing.\n",
626 (unsigned int)uid
, username
, (unsigned int)memcredp
->uid
));
629 memcredp
->ref_count
++;
630 DEBUG(10,("winbindd_add_memory_creds_internal: ref count for user %s is now %d\n",
631 username
, memcredp
->ref_count
));
632 return winbindd_replace_memory_creds_internal(memcredp
, pass
);
635 memcredp
= TALLOC_ZERO_P(NULL
, struct WINBINDD_MEMORY_CREDS
);
637 return NT_STATUS_NO_MEMORY
;
639 memcredp
->username
= talloc_strdup(memcredp
, username
);
640 if (!memcredp
->username
) {
641 talloc_destroy(memcredp
);
642 return NT_STATUS_NO_MEMORY
;
645 status
= store_memory_creds(memcredp
, pass
);
646 if (!NT_STATUS_IS_OK(status
)) {
647 talloc_destroy(memcredp
);
652 memcredp
->ref_count
= 1;
653 DLIST_ADD(memory_creds_list
, memcredp
);
655 DEBUG(10,("winbindd_add_memory_creds_internal: added entry for user %s\n",
662 /*************************************************************
663 Store users credentials in memory. If we also have a
664 struct WINBINDD_CCACHE_ENTRY for this username with a
665 refresh timer, then store the plaintext of the password
666 and associate the new credentials with the struct WINBINDD_CCACHE_ENTRY.
667 *************************************************************/
669 NTSTATUS
winbindd_add_memory_creds(const char *username
, uid_t uid
, const char *pass
)
671 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
674 status
= winbindd_add_memory_creds_internal(username
, uid
, pass
);
675 if (!NT_STATUS_IS_OK(status
)) {
680 struct WINBINDD_MEMORY_CREDS
*memcredp
= find_memory_creds_by_name(username
);
682 entry
->cred_ptr
= memcredp
;
689 /*************************************************************
690 Decrement the in-memory ref count - delete if zero.
691 *************************************************************/
693 NTSTATUS
winbindd_delete_memory_creds(const char *username
)
695 struct WINBINDD_MEMORY_CREDS
*memcredp
= find_memory_creds_by_name(username
);
696 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
697 NTSTATUS status
= NT_STATUS_OK
;
700 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
702 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
705 if (memcredp
->ref_count
<= 0) {
706 DEBUG(0,("winbindd_delete_memory_creds: logic error. ref count for user %s = %d\n",
707 username
, memcredp
->ref_count
));
708 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
711 memcredp
->ref_count
--;
712 if (memcredp
->ref_count
<= 0) {
713 delete_memory_creds(memcredp
);
714 DLIST_REMOVE(memory_creds_list
, memcredp
);
715 talloc_destroy(memcredp
);
716 DEBUG(10,("winbindd_delete_memory_creds: deleted entry for user %s\n",
719 DEBUG(10,("winbindd_delete_memory_creds: entry for user %s ref_count now %d\n",
720 username
, memcredp
->ref_count
));
724 entry
->cred_ptr
= NULL
; /* Ensure we have no dangling references to this. */
729 /***********************************************************
730 Replace the required creds with new ones (password change).
731 ***********************************************************/
733 NTSTATUS
winbindd_replace_memory_creds(const char *username
, const char *pass
)
735 struct WINBINDD_MEMORY_CREDS
*memcredp
= find_memory_creds_by_name(username
);
738 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
740 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
743 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
746 return winbindd_replace_memory_creds_internal(memcredp
, pass
);