s4:torture/smb2/session: Fix expire tests
[Samba.git] / source3 / winbindd / winbindd_cred_cache.c
blob85ad426446ae820beea9e3ecf7c419a4c387ad59
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 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/>.
24 #include "includes.h"
25 #include "winbindd.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "smb_krb5.h"
28 #include "libads/kerberos_proto.h"
29 #include "libads/krb5_errs.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_WINBIND
34 /* uncomment this to do fast debugging on the krb5 ticket renewal event */
35 #ifdef DEBUG_KRB5_TKT_RENEWAL
36 #undef DEBUG_KRB5_TKT_RENEWAL
37 #endif
39 #define MAX_CCACHES 100
41 static struct WINBINDD_CCACHE_ENTRY *ccache_list;
42 static void krb5_ticket_gain_handler(struct tevent_context *,
43 struct tevent_timer *,
44 struct timeval,
45 void *);
46 static void add_krb5_ticket_gain_handler_event(struct WINBINDD_CCACHE_ENTRY *,
47 struct timeval);
49 /* The Krb5 ticket refresh handler should be scheduled
50 at one-half of the period from now till the tkt
51 expiration */
53 static time_t krb5_event_refresh_time(time_t end_time)
55 time_t rest = end_time - time(NULL);
56 return end_time - rest/2;
59 /****************************************************************
60 Find an entry by name.
61 ****************************************************************/
63 static struct WINBINDD_CCACHE_ENTRY *get_ccache_by_username(const char *username)
65 struct WINBINDD_CCACHE_ENTRY *entry;
67 for (entry = ccache_list; entry; entry = entry->next) {
68 if (strequal(entry->username, username)) {
69 return entry;
72 return NULL;
75 /****************************************************************
76 How many do we have ?
77 ****************************************************************/
79 static int ccache_entry_count(void)
81 struct WINBINDD_CCACHE_ENTRY *entry;
82 int i = 0;
84 for (entry = ccache_list; entry; entry = entry->next) {
85 i++;
87 return i;
90 void ccache_remove_all_after_fork(void)
92 struct WINBINDD_CCACHE_ENTRY *cur, *next;
94 for (cur = ccache_list; cur; cur = next) {
95 next = cur->next;
96 DLIST_REMOVE(ccache_list, cur);
97 TALLOC_FREE(cur->event);
98 TALLOC_FREE(cur);
101 return;
104 /****************************************************************
105 Do the work of refreshing the ticket.
106 ****************************************************************/
108 static void krb5_ticket_refresh_handler(struct tevent_context *event_ctx,
109 struct tevent_timer *te,
110 struct timeval now,
111 void *private_data)
113 struct WINBINDD_CCACHE_ENTRY *entry =
114 talloc_get_type_abort(private_data, struct WINBINDD_CCACHE_ENTRY);
115 #ifdef HAVE_KRB5
116 int ret;
117 time_t new_start;
118 time_t expire_time = 0;
119 struct WINBINDD_MEMORY_CREDS *cred_ptr = entry->cred_ptr;
120 #endif
122 DBG_DEBUG("event called for: %s, %s\n",
123 entry->ccname, entry->username);
125 TALLOC_FREE(entry->event);
127 #ifdef HAVE_KRB5
129 /* Kinit again if we have the user password and we can't renew the old
130 * tgt anymore
131 * NB
132 * This happens when machine are put to sleep for a very long time. */
134 if (entry->renew_until < time(NULL)) {
135 rekinit:
136 if (cred_ptr && cred_ptr->pass) {
138 set_effective_uid(entry->uid);
140 ret = kerberos_kinit_password_ext(entry->principal_name,
141 cred_ptr->pass,
142 0, /* hm, can we do time correction here ? */
143 &entry->refresh_time,
144 &entry->renew_until,
145 entry->ccname,
146 False, /* no PAC required anymore */
147 True,
148 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
149 NULL);
150 gain_root_privilege();
152 if (ret) {
153 DEBUG(3,("krb5_ticket_refresh_handler: "
154 "could not re-kinit: %s\n",
155 error_message(ret)));
156 /* destroy the ticket because we cannot rekinit
157 * it, ignore error here */
158 ads_kdestroy(entry->ccname);
160 /* Don't break the ticket refresh chain: retry
161 * refreshing ticket sometime later when KDC is
162 * unreachable -- BoYang. More error code handling
163 * here?
164 * */
166 if ((ret == KRB5_KDC_UNREACH)
167 || (ret == KRB5_REALM_CANT_RESOLVE)) {
168 #if defined(DEBUG_KRB5_TKT_RENEWAL)
169 new_start = time(NULL) + 30;
170 #else
171 new_start = time(NULL) +
172 MAX(30, lp_winbind_cache_time());
173 #endif
174 add_krb5_ticket_gain_handler_event(entry,
175 timeval_set(new_start, 0));
176 return;
178 TALLOC_FREE(entry->event);
179 return;
182 DEBUG(10,("krb5_ticket_refresh_handler: successful re-kinit "
183 "for: %s in ccache: %s\n",
184 entry->principal_name, entry->ccname));
186 #if defined(DEBUG_KRB5_TKT_RENEWAL)
187 new_start = time(NULL) + 30;
188 #else
189 /* The tkt should be refreshed at one-half the period
190 from now to the expiration time */
191 expire_time = entry->refresh_time;
192 new_start = krb5_event_refresh_time(entry->refresh_time);
193 #endif
194 goto done;
195 } else {
196 /* can this happen?
197 * No cached credentials
198 * destroy ticket and refresh chain
199 * */
200 ads_kdestroy(entry->ccname);
201 TALLOC_FREE(entry->event);
202 return;
206 set_effective_uid(entry->uid);
208 ret = smb_krb5_renew_ticket(entry->ccname,
209 entry->principal_name,
210 entry->service,
211 &new_start);
212 #if defined(DEBUG_KRB5_TKT_RENEWAL)
213 new_start = time(NULL) + 30;
214 #else
215 expire_time = new_start;
216 new_start = krb5_event_refresh_time(new_start);
217 #endif
219 gain_root_privilege();
221 if (ret) {
222 DEBUG(3,("krb5_ticket_refresh_handler: "
223 "could not renew tickets: %s\n",
224 error_message(ret)));
225 /* maybe we are beyond the renewing window */
227 /* evil rises here, we refresh ticket failed,
228 * but the ticket might be expired. Therefore,
229 * When we refresh ticket failed, destory the
230 * ticket */
232 ads_kdestroy(entry->ccname);
234 /* avoid breaking the renewal chain: retry in
235 * lp_winbind_cache_time() seconds when the KDC was not
236 * available right now.
237 * the return code can be KRB5_REALM_CANT_RESOLVE.
238 * More error code handling here? */
240 if ((ret == KRB5_KDC_UNREACH)
241 || (ret == KRB5_REALM_CANT_RESOLVE)) {
242 #if defined(DEBUG_KRB5_TKT_RENEWAL)
243 new_start = time(NULL) + 30;
244 #else
245 new_start = time(NULL) +
246 MAX(30, lp_winbind_cache_time());
247 #endif
248 /* ticket is destroyed here, we have to regain it
249 * if it is possible */
250 add_krb5_ticket_gain_handler_event(entry,
251 timeval_set(new_start, 0));
252 return;
255 /* This is evil, if the ticket was already expired.
256 * renew ticket function returns KRB5KRB_AP_ERR_TKT_EXPIRED.
257 * But there is still a chance that we can rekinit it.
259 * This happens when user login in online mode, and then network
260 * down or something cause winbind goes offline for a very long time,
261 * and then goes online again. ticket expired, renew failed.
262 * This happens when machine are put to sleep for a long time,
263 * but shorter than entry->renew_util.
264 * NB
265 * Looks like the KDC is reachable, we want to rekinit as soon as
266 * possible instead of waiting some time later. */
267 if ((ret == KRB5KRB_AP_ERR_TKT_EXPIRED)
268 || (ret == KRB5_FCC_NOFILE)) goto rekinit;
270 return;
273 done:
274 /* in cases that ticket will be unrenewable soon, we don't try to renew ticket
275 * but try to regain ticket if it is possible */
276 if (entry->renew_until && expire_time
277 && (entry->renew_until <= expire_time)) {
278 /* try to regain ticket 10 seconds before expiration */
279 expire_time -= 10;
280 add_krb5_ticket_gain_handler_event(entry,
281 timeval_set(expire_time, 0));
282 return;
285 if (entry->refresh_time == 0) {
286 entry->refresh_time = new_start;
288 entry->event = tevent_add_timer(global_event_context(), entry,
289 timeval_set(new_start, 0),
290 krb5_ticket_refresh_handler,
291 entry);
293 #endif
296 /****************************************************************
297 Do the work of regaining a ticket when coming from offline auth.
298 ****************************************************************/
300 static void krb5_ticket_gain_handler(struct tevent_context *event_ctx,
301 struct tevent_timer *te,
302 struct timeval now,
303 void *private_data)
305 struct WINBINDD_CCACHE_ENTRY *entry =
306 talloc_get_type_abort(private_data, struct WINBINDD_CCACHE_ENTRY);
307 #ifdef HAVE_KRB5
308 int ret;
309 struct timeval t;
310 struct WINBINDD_MEMORY_CREDS *cred_ptr = entry->cred_ptr;
311 struct winbindd_domain *domain = NULL;
312 #endif
314 DBG_DEBUG("event called for: %s, %s\n",
315 entry->ccname, entry->username);
317 TALLOC_FREE(entry->event);
319 #ifdef HAVE_KRB5
321 if (!cred_ptr || !cred_ptr->pass) {
322 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
323 return;
326 if ((domain = find_domain_from_name(entry->realm)) == NULL) {
327 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
328 return;
331 if (!domain->online) {
332 goto retry_later;
335 set_effective_uid(entry->uid);
337 ret = kerberos_kinit_password_ext(entry->principal_name,
338 cred_ptr->pass,
339 0, /* hm, can we do time correction here ? */
340 &entry->refresh_time,
341 &entry->renew_until,
342 entry->ccname,
343 False, /* no PAC required anymore */
344 True,
345 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
346 NULL);
347 gain_root_privilege();
349 if (ret) {
350 DEBUG(3,("krb5_ticket_gain_handler: "
351 "could not kinit: %s\n",
352 error_message(ret)));
353 /* evil. If we cannot do it, destroy any the __maybe__
354 * __existing__ ticket */
355 ads_kdestroy(entry->ccname);
356 goto retry_later;
359 DEBUG(10,("krb5_ticket_gain_handler: "
360 "successful kinit for: %s in ccache: %s\n",
361 entry->principal_name, entry->ccname));
363 goto got_ticket;
365 retry_later:
367 #if defined(DEBUG_KRB5_TKT_RENEWAL)
368 t = timeval_set(time(NULL) + 30, 0);
369 #else
370 t = timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
371 #endif
373 add_krb5_ticket_gain_handler_event(entry, t);
374 return;
376 got_ticket:
378 #if defined(DEBUG_KRB5_TKT_RENEWAL)
379 t = timeval_set(time(NULL) + 30, 0);
380 #else
381 t = timeval_set(krb5_event_refresh_time(entry->refresh_time), 0);
382 #endif
384 if (entry->refresh_time == 0) {
385 entry->refresh_time = t.tv_sec;
387 entry->event = tevent_add_timer(global_event_context(),
388 entry,
390 krb5_ticket_refresh_handler,
391 entry);
393 return;
394 #endif
397 /**************************************************************
398 The gain initial ticket case is recognised as entry->refresh_time
399 is always zero.
400 **************************************************************/
402 static void add_krb5_ticket_gain_handler_event(struct WINBINDD_CCACHE_ENTRY *entry,
403 struct timeval t)
405 entry->refresh_time = 0;
406 entry->event = tevent_add_timer(global_event_context(),
407 entry,
409 krb5_ticket_gain_handler,
410 entry);
413 void ccache_regain_all_now(void)
415 struct WINBINDD_CCACHE_ENTRY *cur;
416 struct timeval t = timeval_current();
418 for (cur = ccache_list; cur; cur = cur->next) {
419 struct tevent_timer *new_event;
422 * if refresh_time is 0, we know that the
423 * the event has the krb5_ticket_gain_handler
425 if (cur->refresh_time == 0) {
426 new_event = tevent_add_timer(global_event_context(),
427 cur,
429 krb5_ticket_gain_handler,
430 cur);
431 } else {
432 new_event = tevent_add_timer(global_event_context(),
433 cur,
435 krb5_ticket_refresh_handler,
436 cur);
439 if (!new_event) {
440 continue;
443 TALLOC_FREE(cur->event);
444 cur->event = new_event;
447 return;
450 /****************************************************************
451 Check if an ccache entry exists.
452 ****************************************************************/
454 bool ccache_entry_exists(const char *username)
456 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
457 return (entry != NULL);
460 /****************************************************************
461 Ensure we're changing the correct entry.
462 ****************************************************************/
464 bool ccache_entry_identical(const char *username,
465 uid_t uid,
466 const char *ccname)
468 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
470 if (!entry) {
471 return False;
474 if (entry->uid != uid) {
475 DEBUG(0,("cache_entry_identical: uid's differ: %u != %u\n",
476 (unsigned int)entry->uid, (unsigned int)uid));
477 return False;
479 if (!strcsequal(entry->ccname, ccname)) {
480 DEBUG(0,("cache_entry_identical: "
481 "ccnames differ: (cache) %s != (client) %s\n",
482 entry->ccname, ccname));
483 return False;
485 return True;
488 NTSTATUS add_ccache_to_list(const char *princ_name,
489 const char *ccname,
490 const char *service,
491 const char *username,
492 const char *pass,
493 const char *realm,
494 uid_t uid,
495 time_t create_time,
496 time_t ticket_end,
497 time_t renew_until,
498 bool postponed_request)
500 struct WINBINDD_CCACHE_ENTRY *entry = NULL;
501 struct timeval t;
502 NTSTATUS ntret;
504 if ((username == NULL && princ_name == NULL) ||
505 ccname == NULL || uid == (uid_t)-1) {
506 return NT_STATUS_INVALID_PARAMETER;
509 if (ccache_entry_count() + 1 > MAX_CCACHES) {
510 DEBUG(10,("add_ccache_to_list: "
511 "max number of ccaches reached\n"));
512 return NT_STATUS_NO_MORE_ENTRIES;
515 /* Reference count old entries */
516 entry = get_ccache_by_username(username);
517 if (entry) {
518 /* Check cached entries are identical. */
519 if (!ccache_entry_identical(username, uid, ccname)) {
520 return NT_STATUS_INVALID_PARAMETER;
522 entry->ref_count++;
523 DEBUG(10,("add_ccache_to_list: "
524 "ref count on entry %s is now %d\n",
525 username, entry->ref_count));
526 /* FIXME: in this case we still might want to have a krb5 cred
527 * event handler created - gd
528 * Add ticket refresh handler here */
530 if (!lp_winbind_refresh_tickets() || renew_until <= 0) {
531 return NT_STATUS_OK;
534 if (!entry->event) {
535 if (postponed_request) {
536 t = timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
537 add_krb5_ticket_gain_handler_event(entry, t);
538 } else {
539 /* Renew at 1/2 the ticket expiration time */
540 #if defined(DEBUG_KRB5_TKT_RENEWAL)
541 t = timeval_set(time(NULL)+30, 0);
542 #else
543 t = timeval_set(krb5_event_refresh_time(ticket_end),
545 #endif
546 if (!entry->refresh_time) {
547 entry->refresh_time = t.tv_sec;
549 entry->event = tevent_add_timer(global_event_context(),
550 entry,
552 krb5_ticket_refresh_handler,
553 entry);
556 if (!entry->event) {
557 ntret = remove_ccache(username);
558 if (!NT_STATUS_IS_OK(ntret)) {
559 DEBUG(0, ("add_ccache_to_list: Failed to remove krb5 "
560 "ccache %s for user %s\n", entry->ccname,
561 entry->username));
562 DEBUG(0, ("add_ccache_to_list: error is %s\n",
563 nt_errstr(ntret)));
564 return ntret;
566 return NT_STATUS_NO_MEMORY;
569 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
574 * If we're set up to renew our krb5 tickets, we must
575 * cache the credentials in memory for the ticket
576 * renew function (or increase the reference count
577 * if we're logging in more than once). Fix inspired
578 * by patch from Ian Gordon <ian.gordon@strath.ac.uk>
579 * for bugid #9098.
582 ntret = winbindd_add_memory_creds(username, uid, pass);
583 DEBUG(10, ("winbindd_add_memory_creds returned: %s\n",
584 nt_errstr(ntret)));
586 return NT_STATUS_OK;
589 entry = talloc(NULL, struct WINBINDD_CCACHE_ENTRY);
590 if (!entry) {
591 return NT_STATUS_NO_MEMORY;
594 ZERO_STRUCTP(entry);
596 if (username) {
597 entry->username = talloc_strdup(entry, username);
598 if (!entry->username) {
599 goto no_mem;
602 if (princ_name) {
603 entry->principal_name = talloc_strdup(entry, princ_name);
604 if (!entry->principal_name) {
605 goto no_mem;
608 if (service) {
609 entry->service = talloc_strdup(entry, service);
610 if (!entry->service) {
611 goto no_mem;
615 entry->ccname = talloc_strdup(entry, ccname);
616 if (!entry->ccname) {
617 goto no_mem;
620 entry->realm = talloc_strdup(entry, realm);
621 if (!entry->realm) {
622 goto no_mem;
625 entry->create_time = create_time;
626 entry->renew_until = renew_until;
627 entry->uid = uid;
628 entry->ref_count = 1;
630 if (!lp_winbind_refresh_tickets() || renew_until <= 0) {
631 goto add_entry;
634 if (postponed_request) {
635 t = timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
636 add_krb5_ticket_gain_handler_event(entry, t);
637 } else {
638 /* Renew at 1/2 the ticket expiration time */
639 #if defined(DEBUG_KRB5_TKT_RENEWAL)
640 t = timeval_set(time(NULL)+30, 0);
641 #else
642 t = timeval_set(krb5_event_refresh_time(ticket_end), 0);
643 #endif
644 if (entry->refresh_time == 0) {
645 entry->refresh_time = t.tv_sec;
647 entry->event = tevent_add_timer(global_event_context(),
648 entry,
650 krb5_ticket_refresh_handler,
651 entry);
654 if (!entry->event) {
655 goto no_mem;
658 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
660 add_entry:
662 DLIST_ADD(ccache_list, entry);
664 DEBUG(10,("add_ccache_to_list: "
665 "added ccache [%s] for user [%s] to the list\n",
666 ccname, username));
668 if (entry->event) {
670 * If we're set up to renew our krb5 tickets, we must
671 * cache the credentials in memory for the ticket
672 * renew function. Fix inspired by patch from
673 * Ian Gordon <ian.gordon@strath.ac.uk> for
674 * bugid #9098.
677 ntret = winbindd_add_memory_creds(username, uid, pass);
678 DEBUG(10, ("winbindd_add_memory_creds returned: %s\n",
679 nt_errstr(ntret)));
682 return NT_STATUS_OK;
684 no_mem:
686 TALLOC_FREE(entry);
687 return NT_STATUS_NO_MEMORY;
690 /*******************************************************************
691 Remove a WINBINDD_CCACHE_ENTRY entry and the krb5 ccache if no longer
692 referenced.
693 *******************************************************************/
695 NTSTATUS remove_ccache(const char *username)
697 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
698 NTSTATUS status = NT_STATUS_OK;
699 #ifdef HAVE_KRB5
700 krb5_error_code ret;
701 #endif
703 if (!entry) {
704 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
707 if (entry->ref_count <= 0) {
708 DEBUG(0,("remove_ccache: logic error. "
709 "ref count for user %s = %d\n",
710 username, entry->ref_count));
711 return NT_STATUS_INTERNAL_DB_CORRUPTION;
714 entry->ref_count--;
716 if (entry->ref_count > 0) {
717 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
718 username, entry->ref_count));
719 return NT_STATUS_OK;
722 /* no references any more */
724 DLIST_REMOVE(ccache_list, entry);
725 TALLOC_FREE(entry->event); /* unregisters events */
727 #ifdef HAVE_KRB5
728 ret = ads_kdestroy(entry->ccname);
730 /* we ignore the error when there has been no credential cache */
731 if (ret == KRB5_FCC_NOFILE) {
732 ret = 0;
733 } else if (ret) {
734 DEBUG(0,("remove_ccache: "
735 "failed to destroy user krb5 ccache %s with: %s\n",
736 entry->ccname, error_message(ret)));
737 } else {
738 DEBUG(10,("remove_ccache: "
739 "successfully destroyed krb5 ccache %s for user %s\n",
740 entry->ccname, username));
742 status = krb5_to_nt_status(ret);
743 #endif
745 TALLOC_FREE(entry);
746 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username));
748 return status;
751 /*******************************************************************
752 In memory credentials cache code.
753 *******************************************************************/
755 static struct WINBINDD_MEMORY_CREDS *memory_creds_list;
757 /***********************************************************
758 Find an entry on the list by name.
759 ***********************************************************/
761 struct WINBINDD_MEMORY_CREDS *find_memory_creds_by_name(const char *username)
763 struct WINBINDD_MEMORY_CREDS *p;
765 for (p = memory_creds_list; p; p = p->next) {
766 if (strequal(p->username, username)) {
767 return p;
770 return NULL;
773 /***********************************************************
774 Store the required creds and mlock them.
775 ***********************************************************/
777 static NTSTATUS store_memory_creds(struct WINBINDD_MEMORY_CREDS *memcredp,
778 const char *pass)
780 #if !defined(HAVE_MLOCK)
781 return NT_STATUS_OK;
782 #else
783 /* new_entry->nt_hash is the base pointer for the block
784 of memory pointed into by new_entry->lm_hash and
785 new_entry->pass (if we're storing plaintext). */
787 memcredp->len = NT_HASH_LEN + LM_HASH_LEN;
788 if (pass) {
789 memcredp->len += strlen(pass)+1;
793 #if defined(LINUX)
794 /* aligning the memory on on x86_64 and compiling
795 with gcc 4.1 using -O2 causes a segv in the
796 next memset() --jerry */
797 memcredp->nt_hash = SMB_MALLOC_ARRAY(unsigned char, memcredp->len);
798 #else
799 /* On non-linux platforms, mlock()'d memory must be aligned */
800 memcredp->nt_hash = SMB_MEMALIGN_ARRAY(unsigned char,
801 getpagesize(), memcredp->len);
802 #endif
803 if (!memcredp->nt_hash) {
804 return NT_STATUS_NO_MEMORY;
806 memset(memcredp->nt_hash, 0x0, memcredp->len);
808 memcredp->lm_hash = memcredp->nt_hash + NT_HASH_LEN;
810 #ifdef DEBUG_PASSWORD
811 DEBUG(10,("mlocking memory: %p\n", memcredp->nt_hash));
812 #endif
813 if ((mlock(memcredp->nt_hash, memcredp->len)) == -1) {
814 DEBUG(0,("failed to mlock memory: %s (%d)\n",
815 strerror(errno), errno));
816 SAFE_FREE(memcredp->nt_hash);
817 return map_nt_error_from_unix(errno);
820 #ifdef DEBUG_PASSWORD
821 DEBUG(10,("mlocked memory: %p\n", memcredp->nt_hash));
822 #endif
824 if (pass) {
825 /* Create and store the password hashes. */
826 E_md4hash(pass, memcredp->nt_hash);
827 E_deshash(pass, memcredp->lm_hash);
829 memcredp->pass = (char *)memcredp->lm_hash + LM_HASH_LEN;
830 memcpy(memcredp->pass, pass,
831 memcredp->len - NT_HASH_LEN - LM_HASH_LEN);
834 return NT_STATUS_OK;
835 #endif
838 /***********************************************************
839 Destroy existing creds.
840 ***********************************************************/
842 static NTSTATUS delete_memory_creds(struct WINBINDD_MEMORY_CREDS *memcredp)
844 #if !defined(HAVE_MUNLOCK)
845 return NT_STATUS_OK;
846 #else
847 if (munlock(memcredp->nt_hash, memcredp->len) == -1) {
848 DEBUG(0,("failed to munlock memory: %s (%d)\n",
849 strerror(errno), errno));
850 return map_nt_error_from_unix(errno);
852 memset(memcredp->nt_hash, '\0', memcredp->len);
853 SAFE_FREE(memcredp->nt_hash);
854 memcredp->nt_hash = NULL;
855 memcredp->lm_hash = NULL;
856 memcredp->pass = NULL;
857 memcredp->len = 0;
858 return NT_STATUS_OK;
859 #endif
862 /***********************************************************
863 Replace the required creds with new ones (password change).
864 ***********************************************************/
866 static NTSTATUS winbindd_replace_memory_creds_internal(struct WINBINDD_MEMORY_CREDS *memcredp,
867 const char *pass)
869 NTSTATUS status = delete_memory_creds(memcredp);
870 if (!NT_STATUS_IS_OK(status)) {
871 return status;
873 return store_memory_creds(memcredp, pass);
876 /*************************************************************
877 Store credentials in memory in a list.
878 *************************************************************/
880 static NTSTATUS winbindd_add_memory_creds_internal(const char *username,
881 uid_t uid,
882 const char *pass)
884 /* Shortcut to ensure we don't store if no mlock. */
885 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
886 return NT_STATUS_OK;
887 #else
888 NTSTATUS status;
889 struct WINBINDD_MEMORY_CREDS *memcredp = NULL;
891 memcredp = find_memory_creds_by_name(username);
892 if (uid == (uid_t)-1) {
893 DEBUG(0,("winbindd_add_memory_creds_internal: "
894 "invalid uid for user %s.\n", username));
895 return NT_STATUS_INVALID_PARAMETER;
898 if (memcredp) {
899 /* Already exists. Increment the reference count and replace stored creds. */
900 if (uid != memcredp->uid) {
901 DEBUG(0,("winbindd_add_memory_creds_internal: "
902 "uid %u for user %s doesn't "
903 "match stored uid %u. Replacing.\n",
904 (unsigned int)uid, username,
905 (unsigned int)memcredp->uid));
906 memcredp->uid = uid;
908 memcredp->ref_count++;
909 DEBUG(10,("winbindd_add_memory_creds_internal: "
910 "ref count for user %s is now %d\n",
911 username, memcredp->ref_count));
912 return winbindd_replace_memory_creds_internal(memcredp, pass);
915 memcredp = talloc_zero(NULL, struct WINBINDD_MEMORY_CREDS);
916 if (!memcredp) {
917 return NT_STATUS_NO_MEMORY;
919 memcredp->username = talloc_strdup(memcredp, username);
920 if (!memcredp->username) {
921 talloc_destroy(memcredp);
922 return NT_STATUS_NO_MEMORY;
925 status = store_memory_creds(memcredp, pass);
926 if (!NT_STATUS_IS_OK(status)) {
927 talloc_destroy(memcredp);
928 return status;
931 memcredp->uid = uid;
932 memcredp->ref_count = 1;
933 DLIST_ADD(memory_creds_list, memcredp);
935 DEBUG(10,("winbindd_add_memory_creds_internal: "
936 "added entry for user %s\n", username));
938 return NT_STATUS_OK;
939 #endif
942 /*************************************************************
943 Store users credentials in memory. If we also have a
944 struct WINBINDD_CCACHE_ENTRY for this username with a
945 refresh timer, then store the plaintext of the password
946 and associate the new credentials with the struct WINBINDD_CCACHE_ENTRY.
947 *************************************************************/
949 NTSTATUS winbindd_add_memory_creds(const char *username,
950 uid_t uid,
951 const char *pass)
953 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
954 NTSTATUS status;
956 status = winbindd_add_memory_creds_internal(username, uid, pass);
957 if (!NT_STATUS_IS_OK(status)) {
958 return status;
961 if (entry) {
962 struct WINBINDD_MEMORY_CREDS *memcredp = NULL;
963 memcredp = find_memory_creds_by_name(username);
964 if (memcredp) {
965 entry->cred_ptr = memcredp;
969 return status;
972 /*************************************************************
973 Decrement the in-memory ref count - delete if zero.
974 *************************************************************/
976 NTSTATUS winbindd_delete_memory_creds(const char *username)
978 struct WINBINDD_MEMORY_CREDS *memcredp = NULL;
979 struct WINBINDD_CCACHE_ENTRY *entry = NULL;
980 NTSTATUS status = NT_STATUS_OK;
982 memcredp = find_memory_creds_by_name(username);
983 entry = get_ccache_by_username(username);
985 if (!memcredp) {
986 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
987 username));
988 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
991 if (memcredp->ref_count <= 0) {
992 DEBUG(0,("winbindd_delete_memory_creds: logic error. "
993 "ref count for user %s = %d\n",
994 username, memcredp->ref_count));
995 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
998 memcredp->ref_count--;
999 if (memcredp->ref_count <= 0) {
1000 delete_memory_creds(memcredp);
1001 DLIST_REMOVE(memory_creds_list, memcredp);
1002 talloc_destroy(memcredp);
1003 DEBUG(10,("winbindd_delete_memory_creds: "
1004 "deleted entry for user %s\n",
1005 username));
1006 } else {
1007 DEBUG(10,("winbindd_delete_memory_creds: "
1008 "entry for user %s ref_count now %d\n",
1009 username, memcredp->ref_count));
1012 if (entry) {
1013 /* Ensure we have no dangling references to this. */
1014 entry->cred_ptr = NULL;
1017 return status;
1020 /***********************************************************
1021 Replace the required creds with new ones (password change).
1022 ***********************************************************/
1024 NTSTATUS winbindd_replace_memory_creds(const char *username,
1025 const char *pass)
1027 struct WINBINDD_MEMORY_CREDS *memcredp = NULL;
1029 memcredp = find_memory_creds_by_name(username);
1030 if (!memcredp) {
1031 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
1032 username));
1033 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1036 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
1037 username));
1039 return winbindd_replace_memory_creds_internal(memcredp, pass);