r19747: Ensure to link with the required objects.
[Samba.git] / source / nsswitch / winbindd_cred_cache.c
blob37e3bb2f8cfec03786686da31cf8b834114a932f
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 #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)) {
44 return entry;
47 return NULL;
50 /****************************************************************
51 How many do we have ?
52 ****************************************************************/
54 static int ccache_entry_count(void)
56 struct WINBINDD_CCACHE_ENTRY *entry;
57 int i = 0;
59 for (entry = ccache_list; entry; entry = entry->next) {
60 i++;
62 return i;
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,
71 void *private_data)
73 struct WINBINDD_CCACHE_ENTRY *entry =
74 talloc_get_type_abort(private_data, struct WINBINDD_CCACHE_ENTRY);
75 #ifdef HAVE_KRB5
76 int ret;
77 time_t new_start;
78 struct WINBINDD_MEMORY_CREDS *cred_ptr = entry->cred_ptr;
79 #endif
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);
86 #ifdef HAVE_KRB5
88 /* Kinit again if we have the user password and we can't renew the old
89 * tgt anymore */
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,
96 cred_ptr->pass,
97 0, /* hm, can we do time correction here ? */
98 &entry->refresh_time,
99 &entry->renew_until,
100 entry->ccname,
101 False, /* no PAC required anymore */
102 True,
103 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME);
104 gain_root_privilege();
106 if (ret) {
107 DEBUG(3,("krb5_ticket_refresh_handler: could not re-kinit: %s\n",
108 error_message(ret)));
109 TALLOC_FREE(entry->event);
110 return;
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;
119 goto done;
122 set_effective_uid(entry->uid);
124 ret = smb_krb5_renew_ticket(entry->ccname,
125 entry->principal_name,
126 entry->service,
127 &new_start);
128 gain_root_privilege();
130 if (ret) {
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());
140 goto done;
143 return;
146 done:
148 entry->event = add_timed_event(entry,
149 timeval_set(new_start, 0),
150 "krb5_ticket_refresh_handler",
151 krb5_ticket_refresh_handler,
152 entry);
154 #endif
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,
163 void *private_data)
165 struct WINBINDD_CCACHE_ENTRY *entry =
166 talloc_get_type_abort(private_data, struct WINBINDD_CCACHE_ENTRY);
167 #ifdef HAVE_KRB5
168 int ret;
169 time_t new_start;
170 struct timeval t;
171 struct WINBINDD_MEMORY_CREDS *cred_ptr = entry->cred_ptr;
172 struct winbindd_domain *domain = NULL;
173 #endif
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);
180 #ifdef HAVE_KRB5
182 if (!cred_ptr || !cred_ptr->pass) {
183 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
184 return;
187 if ((domain = find_domain_from_name(entry->realm)) == NULL) {
188 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
189 return;
192 if (domain->online) {
194 set_effective_uid(entry->uid);
196 ret = kerberos_kinit_password_ext(entry->principal_name,
197 cred_ptr->pass,
198 0, /* hm, can we do time correction here ? */
199 &entry->refresh_time,
200 &entry->renew_until,
201 entry->ccname,
202 False, /* no PAC required anymore */
203 True,
204 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME);
205 gain_root_privilege();
207 if (ret) {
208 DEBUG(3,("krb5_ticket_gain_handler: could not kinit: %s\n",
209 error_message(ret)));
210 goto retry_later;
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;
218 goto got_ticket;
221 retry_later:
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,
227 entry);
229 return;
231 got_ticket:
233 #if 0 /* TESTING */
234 t = timeval_set(time(NULL) + 30, 0);
235 #else
236 t = timeval_set(new_start, 0);
237 #endif /* TESTING */
239 entry->event = add_timed_event(entry,
241 "krb5_ticket_refresh_handler",
242 krb5_ticket_refresh_handler,
243 entry);
245 return;
246 #endif
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);
257 if (!entry) {
258 return False;
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 ));
264 return False;
266 if (!strcsequal(entry->ccname, ccname)) {
267 DEBUG(0,("cache_entry_identical: ccnames differ: (cache) %s != (client) %s\n",
268 entry->ccname, ccname));
269 return False;
271 return True;
274 NTSTATUS add_ccache_to_list(const char *princ_name,
275 const char *ccname,
276 const char *service,
277 const char *username,
278 const char *realm,
279 uid_t uid,
280 time_t create_time,
281 time_t ticket_end,
282 time_t renew_until,
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);
299 if (entry) {
300 /* Check cached entries are identical. */
301 if (!ccache_entry_identical(username, uid, ccname)) {
302 return NT_STATUS_INVALID_PARAMETER;
304 entry->ref_count++;
305 DEBUG(10,("add_ccache_to_list: ref count on entry %s is now %d\n",
306 username, entry->ref_count));
307 return NT_STATUS_OK;
310 entry = TALLOC_P(NULL, struct WINBINDD_CCACHE_ENTRY);
311 if (!entry) {
312 return NT_STATUS_NO_MEMORY;
315 ZERO_STRUCTP(entry);
317 if (username) {
318 entry->username = talloc_strdup(entry, username);
319 if (!entry->username) {
320 goto no_mem;
323 if (princ_name) {
324 entry->principal_name = talloc_strdup(entry, princ_name);
325 if (!entry->principal_name) {
326 goto no_mem;
329 if (service) {
330 entry->service = talloc_strdup(entry, service);
331 if (!entry->service) {
332 goto no_mem;
336 entry->ccname = talloc_strdup(entry, ccname);
337 if (!entry->ccname) {
338 goto no_mem;
341 entry->realm = talloc_strdup(entry, realm);
342 if (!entry->realm) {
343 goto no_mem;
346 entry->create_time = create_time;
347 entry->renew_until = renew_until;
348 entry->uid = uid;
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,
357 entry);
358 } else {
359 entry->event = add_timed_event(entry,
360 timeval_set((ticket_end - 1), 0),
361 "krb5_ticket_refresh_handler",
362 krb5_ticket_refresh_handler,
363 entry);
366 if (!entry->event) {
367 goto no_mem;
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));
377 return NT_STATUS_OK;
379 no_mem:
381 TALLOC_FREE(entry);
382 return NT_STATUS_NO_MEMORY;
385 NTSTATUS remove_ccache(const char *username)
387 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
389 if (!entry) {
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;
399 entry->ref_count--;
400 if (entry->ref_count <= 0) {
401 DLIST_REMOVE(ccache_list, entry);
402 TALLOC_FREE(entry->event); /* unregisters events */
403 TALLOC_FREE(entry);
404 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username));
405 } else {
406 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
407 username, entry->ref_count ));
410 return NT_STATUS_OK;
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)) {
429 return p;
432 return NULL;
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)
442 return NT_STATUS_OK;
443 #else
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;
449 if (pass) {
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));
461 #endif
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));
471 #endif
473 /* Create and store the password hashes. */
474 E_md4hash(pass, memcredp->nt_hash);
475 E_deshash(pass, memcredp->lm_hash);
477 if (pass) {
478 memcredp->pass = (char *)memcredp->lm_hash + LM_HASH_LEN;
479 memcpy(memcredp->pass, pass, memcredp->len - NT_HASH_LEN - LM_HASH_LEN);
482 return NT_STATUS_OK;
483 #endif
486 /***********************************************************
487 Destroy existing creds.
488 ***********************************************************/
490 static NTSTATUS delete_memory_creds(struct WINBINDD_MEMORY_CREDS *memcredp)
492 #if !defined(HAVE_MUNLOCK)
493 return NT_STATUS_OK;
494 #else
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;
504 memcredp->len = 0;
505 return NT_STATUS_OK;
506 #endif
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,
514 const char *pass)
516 NTSTATUS status = delete_memory_creds(memcredp);
517 if (!NT_STATUS_IS_OK(status)) {
518 return 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)
531 return NT_STATUS_OK;
532 #else
533 NTSTATUS status;
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",
538 username ));
539 return NT_STATUS_INVALID_PARAMETER;
542 if (memcredp) {
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 ));
548 memcredp->uid = 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);
557 if (!memcredp) {
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);
569 return status;
572 memcredp->uid = uid;
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",
577 username ));
579 return NT_STATUS_OK;
580 #endif
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);
593 NTSTATUS status;
595 status = winbindd_add_memory_creds_internal(username, uid, pass);
596 if (!NT_STATUS_IS_OK(status)) {
597 return status;
600 if (entry) {
601 struct WINBINDD_MEMORY_CREDS *memcredp = find_memory_creds_by_name(username);
602 if (memcredp) {
603 entry->cred_ptr = memcredp;
607 return status;
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;
620 if (!memcredp) {
621 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
622 username ));
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",
638 username));
639 } else {
640 DEBUG(10,("winbindd_delete_memory_creds: entry for user %s ref_count now %d\n",
641 username, memcredp->ref_count));
644 if (entry) {
645 entry->cred_ptr = NULL; /* Ensure we have no dangling references to this. */
647 return status;
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);
658 if (!memcredp) {
659 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
660 username ));
661 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
664 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
665 username ));
667 return winbindd_replace_memory_creds_internal(memcredp, pass);