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 NTSTATUS
remove_ccache(const char *username
)
387 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
390 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
393 if (entry
->ref_count
<= 0) {
394 DEBUG(0,("remove_ccache: logic error. ref count for user %s = %d\n",
395 username
, entry
->ref_count
));
396 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
400 if (entry
->ref_count
<= 0) {
401 DLIST_REMOVE(ccache_list
, entry
);
402 TALLOC_FREE(entry
->event
); /* unregisters events */
404 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username
));
406 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
407 username
, entry
->ref_count
));
413 /*******************************************************************
414 In memory credentials cache code.
415 *******************************************************************/
417 static struct WINBINDD_MEMORY_CREDS
*memory_creds_list
;
419 /***********************************************************
420 Find an entry on the list by name.
421 ***********************************************************/
423 struct WINBINDD_MEMORY_CREDS
*find_memory_creds_by_name(const char *username
)
425 struct WINBINDD_MEMORY_CREDS
*p
;
427 for (p
= memory_creds_list
; p
; p
= p
->next
) {
428 if (strequal(p
->username
, username
)) {
435 /***********************************************************
436 Store the required creds and mlock them.
437 ***********************************************************/
439 static NTSTATUS
store_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
, const char *pass
)
441 #if !defined(HAVE_MLOCK)
444 /* new_entry->nt_hash is the base pointer for the block
445 of memory pointed into by new_entry->lm_hash and
446 new_entry->pass (if we're storing plaintext). */
448 memcredp
->len
= NT_HASH_LEN
+ LM_HASH_LEN
;
450 memcredp
->len
+= strlen(pass
)+1;
453 memcredp
->nt_hash
= (unsigned char *)TALLOC_ZERO(memcredp
, memcredp
->len
);
454 if (!memcredp
->nt_hash
) {
455 return NT_STATUS_NO_MEMORY
;
458 memcredp
->lm_hash
= memcredp
->nt_hash
+ NT_HASH_LEN
;
459 #ifdef DEBUG_PASSWORD
460 DEBUG(10,("mlocking memory: %p\n", memcredp
->nt_hash
));
463 if ((mlock(memcredp
->nt_hash
, memcredp
->len
)) == -1) {
464 DEBUG(0,("failed to mlock memory: %s (%d)\n",
465 strerror(errno
), errno
));
466 return map_nt_error_from_unix(errno
);
469 #ifdef DEBUG_PASSWORD
470 DEBUG(10,("mlocked memory: %p\n", memcredp
->nt_hash
));
473 /* Create and store the password hashes. */
474 E_md4hash(pass
, memcredp
->nt_hash
);
475 E_deshash(pass
, memcredp
->lm_hash
);
478 memcredp
->pass
= (char *)memcredp
->lm_hash
+ LM_HASH_LEN
;
479 memcpy(memcredp
->pass
, pass
, memcredp
->len
- NT_HASH_LEN
- LM_HASH_LEN
);
486 /***********************************************************
487 Destroy existing creds.
488 ***********************************************************/
490 static NTSTATUS
delete_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
)
492 #if !defined(HAVE_MUNLOCK)
495 if (munlock(memcredp
->nt_hash
, memcredp
->len
) == -1) {
496 DEBUG(0,("failed to munlock memory: %s (%d)\n",
497 strerror(errno
), errno
));
498 return map_nt_error_from_unix(errno
);
500 memset(memcredp
->nt_hash
, '\0', memcredp
->len
);
501 TALLOC_FREE(memcredp
->nt_hash
);
502 memcredp
->lm_hash
= NULL
;
503 memcredp
->pass
= NULL
;
509 /***********************************************************
510 Replace the required creds with new ones (password change).
511 ***********************************************************/
513 static NTSTATUS
winbindd_replace_memory_creds_internal(struct WINBINDD_MEMORY_CREDS
*memcredp
,
516 NTSTATUS status
= delete_memory_creds(memcredp
);
517 if (!NT_STATUS_IS_OK(status
)) {
520 return store_memory_creds(memcredp
, pass
);
523 /*************************************************************
524 Store credentials in memory in a list.
525 *************************************************************/
527 static NTSTATUS
winbindd_add_memory_creds_internal(const char *username
, uid_t uid
, const char *pass
)
529 /* Shortcut to ensure we don't store if no mlock. */
530 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
534 struct WINBINDD_MEMORY_CREDS
*memcredp
= find_memory_creds_by_name(username
);
536 if (uid
== (uid_t
)-1) {
537 DEBUG(0,("winbindd_add_memory_creds_internal: invalid uid for user %s.\n",
539 return NT_STATUS_INVALID_PARAMETER
;
543 /* Already exists. Increment the reference count and replace stored creds. */
544 if (uid
!= memcredp
->uid
) {
545 DEBUG(0,("winbindd_add_memory_creds_internal: uid %u for user %s doesn't "
546 "match stored uid %u. Replacing.\n",
547 (unsigned int)uid
, username
, (unsigned int)memcredp
->uid
));
550 memcredp
->ref_count
++;
551 DEBUG(10,("winbindd_add_memory_creds_internal: ref count for user %s is now %d\n",
552 username
, memcredp
->ref_count
));
553 return winbindd_replace_memory_creds_internal(memcredp
, pass
);
556 memcredp
= TALLOC_ZERO_P(NULL
, struct WINBINDD_MEMORY_CREDS
);
558 return NT_STATUS_NO_MEMORY
;
560 memcredp
->username
= talloc_strdup(memcredp
, username
);
561 if (!memcredp
->username
) {
562 talloc_destroy(memcredp
);
563 return NT_STATUS_NO_MEMORY
;
566 status
= store_memory_creds(memcredp
, pass
);
567 if (!NT_STATUS_IS_OK(status
)) {
568 talloc_destroy(memcredp
);
573 memcredp
->ref_count
= 1;
574 DLIST_ADD(memory_creds_list
, memcredp
);
576 DEBUG(10,("winbindd_add_memory_creds_internal: added entry for user %s\n",
583 /*************************************************************
584 Store users credentials in memory. If we also have a
585 struct WINBINDD_CCACHE_ENTRY for this username with a
586 refresh timer, then store the plaintext of the password
587 and associate the new credentials with the struct WINBINDD_CCACHE_ENTRY.
588 *************************************************************/
590 NTSTATUS
winbindd_add_memory_creds(const char *username
, uid_t uid
, const char *pass
)
592 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
595 status
= winbindd_add_memory_creds_internal(username
, uid
, pass
);
596 if (!NT_STATUS_IS_OK(status
)) {
601 struct WINBINDD_MEMORY_CREDS
*memcredp
= find_memory_creds_by_name(username
);
603 entry
->cred_ptr
= memcredp
;
610 /*************************************************************
611 Decrement the in-memory ref count - delete if zero.
612 *************************************************************/
614 NTSTATUS
winbindd_delete_memory_creds(const char *username
)
616 struct WINBINDD_MEMORY_CREDS
*memcredp
= find_memory_creds_by_name(username
);
617 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
618 NTSTATUS status
= NT_STATUS_OK
;
621 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
623 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
626 if (memcredp
->ref_count
<= 0) {
627 DEBUG(0,("winbindd_delete_memory_creds: logic error. ref count for user %s = %d\n",
628 username
, memcredp
->ref_count
));
629 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
632 memcredp
->ref_count
--;
633 if (memcredp
->ref_count
<= 0) {
634 delete_memory_creds(memcredp
);
635 DLIST_REMOVE(memory_creds_list
, memcredp
);
636 talloc_destroy(memcredp
);
637 DEBUG(10,("winbindd_delete_memory_creds: deleted entry for user %s\n",
640 DEBUG(10,("winbindd_delete_memory_creds: entry for user %s ref_count now %d\n",
641 username
, memcredp
->ref_count
));
645 entry
->cred_ptr
= NULL
; /* Ensure we have no dangling references to this. */
650 /***********************************************************
651 Replace the required creds with new ones (password change).
652 ***********************************************************/
654 NTSTATUS
winbindd_replace_memory_creds(const char *username
, const char *pass
)
656 struct WINBINDD_MEMORY_CREDS
*memcredp
= find_memory_creds_by_name(username
);
659 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
661 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
664 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
667 return winbindd_replace_memory_creds_internal(memcredp
, pass
);