auth/spnego: call gensec_spnego_create_negTokenInit() directly in gensec_spnego_updat...
[Samba.git] / source3 / winbindd / winbindd_cred_cache.c
blob20b4d55fb80f7a9aab26a05d2b66861bb156d7ac
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"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_WINBIND
33 /* uncomment this to do fast debugging on the krb5 ticket renewal event */
34 #ifdef DEBUG_KRB5_TKT_RENEWAL
35 #undef DEBUG_KRB5_TKT_RENEWAL
36 #endif
38 #define MAX_CCACHES 100
40 static struct WINBINDD_CCACHE_ENTRY *ccache_list;
41 static void krb5_ticket_gain_handler(struct tevent_context *,
42 struct tevent_timer *,
43 struct timeval,
44 void *);
45 static void add_krb5_ticket_gain_handler_event(struct WINBINDD_CCACHE_ENTRY *,
46 struct timeval);
48 /* The Krb5 ticket refresh handler should be scheduled
49 at one-half of the period from now till the tkt
50 expiration */
52 static time_t krb5_event_refresh_time(time_t end_time)
54 time_t rest = end_time - time(NULL);
55 return end_time - rest/2;
58 /****************************************************************
59 Find an entry by name.
60 ****************************************************************/
62 static struct WINBINDD_CCACHE_ENTRY *get_ccache_by_username(const char *username)
64 struct WINBINDD_CCACHE_ENTRY *entry;
66 for (entry = ccache_list; entry; entry = entry->next) {
67 if (strequal(entry->username, username)) {
68 return entry;
71 return NULL;
74 /****************************************************************
75 How many do we have ?
76 ****************************************************************/
78 static int ccache_entry_count(void)
80 struct WINBINDD_CCACHE_ENTRY *entry;
81 int i = 0;
83 for (entry = ccache_list; entry; entry = entry->next) {
84 i++;
86 return i;
89 void ccache_remove_all_after_fork(void)
91 struct WINBINDD_CCACHE_ENTRY *cur, *next;
93 for (cur = ccache_list; cur; cur = next) {
94 next = cur->next;
95 DLIST_REMOVE(ccache_list, cur);
96 TALLOC_FREE(cur->event);
97 TALLOC_FREE(cur);
100 return;
103 /****************************************************************
104 Do the work of refreshing the ticket.
105 ****************************************************************/
107 static void krb5_ticket_refresh_handler(struct tevent_context *event_ctx,
108 struct tevent_timer *te,
109 struct timeval now,
110 void *private_data)
112 struct WINBINDD_CCACHE_ENTRY *entry =
113 talloc_get_type_abort(private_data, struct WINBINDD_CCACHE_ENTRY);
114 #ifdef HAVE_KRB5
115 int ret;
116 time_t new_start;
117 time_t expire_time = 0;
118 struct WINBINDD_MEMORY_CREDS *cred_ptr = entry->cred_ptr;
119 #endif
121 DEBUG(10,("krb5_ticket_refresh_handler called\n"));
122 DEBUGADD(10,("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(winbind_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 DEBUG(10,("krb5_ticket_gain_handler called\n"));
315 DEBUGADD(10,("event called for: %s, %s\n",
316 entry->ccname, entry->username));
318 TALLOC_FREE(entry->event);
320 #ifdef HAVE_KRB5
322 if (!cred_ptr || !cred_ptr->pass) {
323 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
324 return;
327 if ((domain = find_domain_from_name(entry->realm)) == NULL) {
328 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
329 return;
332 if (!domain->online) {
333 goto retry_later;
336 set_effective_uid(entry->uid);
338 ret = kerberos_kinit_password_ext(entry->principal_name,
339 cred_ptr->pass,
340 0, /* hm, can we do time correction here ? */
341 &entry->refresh_time,
342 &entry->renew_until,
343 entry->ccname,
344 False, /* no PAC required anymore */
345 True,
346 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
347 NULL);
348 gain_root_privilege();
350 if (ret) {
351 DEBUG(3,("krb5_ticket_gain_handler: "
352 "could not kinit: %s\n",
353 error_message(ret)));
354 /* evil. If we cannot do it, destroy any the __maybe__
355 * __existing__ ticket */
356 ads_kdestroy(entry->ccname);
357 goto retry_later;
360 DEBUG(10,("krb5_ticket_gain_handler: "
361 "successful kinit for: %s in ccache: %s\n",
362 entry->principal_name, entry->ccname));
364 goto got_ticket;
366 retry_later:
368 #if defined(DEBUG_KRB5_TKT_RENEWAL)
369 t = timeval_set(time(NULL) + 30, 0);
370 #else
371 t = timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
372 #endif
374 add_krb5_ticket_gain_handler_event(entry, t);
375 return;
377 got_ticket:
379 #if defined(DEBUG_KRB5_TKT_RENEWAL)
380 t = timeval_set(time(NULL) + 30, 0);
381 #else
382 t = timeval_set(krb5_event_refresh_time(entry->refresh_time), 0);
383 #endif
385 if (entry->refresh_time == 0) {
386 entry->refresh_time = t.tv_sec;
388 entry->event = tevent_add_timer(winbind_event_context(),
389 entry,
391 krb5_ticket_refresh_handler,
392 entry);
394 return;
395 #endif
398 /**************************************************************
399 The gain initial ticket case is recognised as entry->refresh_time
400 is always zero.
401 **************************************************************/
403 static void add_krb5_ticket_gain_handler_event(struct WINBINDD_CCACHE_ENTRY *entry,
404 struct timeval t)
406 entry->refresh_time = 0;
407 entry->event = tevent_add_timer(winbind_event_context(),
408 entry,
410 krb5_ticket_gain_handler,
411 entry);
414 void ccache_regain_all_now(void)
416 struct WINBINDD_CCACHE_ENTRY *cur;
417 struct timeval t = timeval_current();
419 for (cur = ccache_list; cur; cur = cur->next) {
420 struct tevent_timer *new_event;
423 * if refresh_time is 0, we know that the
424 * the event has the krb5_ticket_gain_handler
426 if (cur->refresh_time == 0) {
427 new_event = tevent_add_timer(winbind_event_context(),
428 cur,
430 krb5_ticket_gain_handler,
431 cur);
432 } else {
433 new_event = tevent_add_timer(winbind_event_context(),
434 cur,
436 krb5_ticket_refresh_handler,
437 cur);
440 if (!new_event) {
441 continue;
444 TALLOC_FREE(cur->event);
445 cur->event = new_event;
448 return;
451 /****************************************************************
452 Check if an ccache entry exists.
453 ****************************************************************/
455 bool ccache_entry_exists(const char *username)
457 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
458 return (entry != NULL);
461 /****************************************************************
462 Ensure we're changing the correct entry.
463 ****************************************************************/
465 bool ccache_entry_identical(const char *username,
466 uid_t uid,
467 const char *ccname)
469 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
471 if (!entry) {
472 return False;
475 if (entry->uid != uid) {
476 DEBUG(0,("cache_entry_identical: uid's differ: %u != %u\n",
477 (unsigned int)entry->uid, (unsigned int)uid));
478 return False;
480 if (!strcsequal(entry->ccname, ccname)) {
481 DEBUG(0,("cache_entry_identical: "
482 "ccnames differ: (cache) %s != (client) %s\n",
483 entry->ccname, ccname));
484 return False;
486 return True;
489 NTSTATUS add_ccache_to_list(const char *princ_name,
490 const char *ccname,
491 const char *service,
492 const char *username,
493 const char *pass,
494 const char *realm,
495 uid_t uid,
496 time_t create_time,
497 time_t ticket_end,
498 time_t renew_until,
499 bool postponed_request)
501 struct WINBINDD_CCACHE_ENTRY *entry = NULL;
502 struct timeval t;
503 NTSTATUS ntret;
505 if ((username == NULL && princ_name == NULL) ||
506 ccname == NULL || uid == (uid_t)-1) {
507 return NT_STATUS_INVALID_PARAMETER;
510 if (ccache_entry_count() + 1 > MAX_CCACHES) {
511 DEBUG(10,("add_ccache_to_list: "
512 "max number of ccaches reached\n"));
513 return NT_STATUS_NO_MORE_ENTRIES;
516 /* Reference count old entries */
517 entry = get_ccache_by_username(username);
518 if (entry) {
519 /* Check cached entries are identical. */
520 if (!ccache_entry_identical(username, uid, ccname)) {
521 return NT_STATUS_INVALID_PARAMETER;
523 entry->ref_count++;
524 DEBUG(10,("add_ccache_to_list: "
525 "ref count on entry %s is now %d\n",
526 username, entry->ref_count));
527 /* FIXME: in this case we still might want to have a krb5 cred
528 * event handler created - gd
529 * Add ticket refresh handler here */
531 if (!lp_winbind_refresh_tickets() || renew_until <= 0) {
532 return NT_STATUS_OK;
535 if (!entry->event) {
536 if (postponed_request) {
537 t = timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
538 add_krb5_ticket_gain_handler_event(entry, t);
539 } else {
540 /* Renew at 1/2 the ticket expiration time */
541 #if defined(DEBUG_KRB5_TKT_RENEWAL)
542 t = timeval_set(time(NULL)+30, 0);
543 #else
544 t = timeval_set(krb5_event_refresh_time(ticket_end),
546 #endif
547 if (!entry->refresh_time) {
548 entry->refresh_time = t.tv_sec;
550 entry->event = tevent_add_timer(winbind_event_context(),
551 entry,
553 krb5_ticket_refresh_handler,
554 entry);
557 if (!entry->event) {
558 ntret = remove_ccache(username);
559 if (!NT_STATUS_IS_OK(ntret)) {
560 DEBUG(0, ("add_ccache_to_list: Failed to remove krb5 "
561 "ccache %s for user %s\n", entry->ccname,
562 entry->username));
563 DEBUG(0, ("add_ccache_to_list: error is %s\n",
564 nt_errstr(ntret)));
565 return ntret;
567 return NT_STATUS_NO_MEMORY;
570 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
575 * If we're set up to renew our krb5 tickets, we must
576 * cache the credentials in memory for the ticket
577 * renew function (or increase the reference count
578 * if we're logging in more than once). Fix inspired
579 * by patch from Ian Gordon <ian.gordon@strath.ac.uk>
580 * for bugid #9098.
583 ntret = winbindd_add_memory_creds(username, uid, pass);
584 DEBUG(10, ("winbindd_add_memory_creds returned: %s\n",
585 nt_errstr(ntret)));
587 return NT_STATUS_OK;
590 entry = talloc(NULL, struct WINBINDD_CCACHE_ENTRY);
591 if (!entry) {
592 return NT_STATUS_NO_MEMORY;
595 ZERO_STRUCTP(entry);
597 if (username) {
598 entry->username = talloc_strdup(entry, username);
599 if (!entry->username) {
600 goto no_mem;
603 if (princ_name) {
604 entry->principal_name = talloc_strdup(entry, princ_name);
605 if (!entry->principal_name) {
606 goto no_mem;
609 if (service) {
610 entry->service = talloc_strdup(entry, service);
611 if (!entry->service) {
612 goto no_mem;
616 entry->ccname = talloc_strdup(entry, ccname);
617 if (!entry->ccname) {
618 goto no_mem;
621 entry->realm = talloc_strdup(entry, realm);
622 if (!entry->realm) {
623 goto no_mem;
626 entry->create_time = create_time;
627 entry->renew_until = renew_until;
628 entry->uid = uid;
629 entry->ref_count = 1;
631 if (!lp_winbind_refresh_tickets() || renew_until <= 0) {
632 goto add_entry;
635 if (postponed_request) {
636 t = timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
637 add_krb5_ticket_gain_handler_event(entry, t);
638 } else {
639 /* Renew at 1/2 the ticket expiration time */
640 #if defined(DEBUG_KRB5_TKT_RENEWAL)
641 t = timeval_set(time(NULL)+30, 0);
642 #else
643 t = timeval_set(krb5_event_refresh_time(ticket_end), 0);
644 #endif
645 if (entry->refresh_time == 0) {
646 entry->refresh_time = t.tv_sec;
648 entry->event = tevent_add_timer(winbind_event_context(),
649 entry,
651 krb5_ticket_refresh_handler,
652 entry);
655 if (!entry->event) {
656 goto no_mem;
659 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
661 add_entry:
663 DLIST_ADD(ccache_list, entry);
665 DEBUG(10,("add_ccache_to_list: "
666 "added ccache [%s] for user [%s] to the list\n",
667 ccname, username));
669 if (entry->event) {
671 * If we're set up to renew our krb5 tickets, we must
672 * cache the credentials in memory for the ticket
673 * renew function. Fix inspired by patch from
674 * Ian Gordon <ian.gordon@strath.ac.uk> for
675 * bugid #9098.
678 ntret = winbindd_add_memory_creds(username, uid, pass);
679 DEBUG(10, ("winbindd_add_memory_creds returned: %s\n",
680 nt_errstr(ntret)));
683 return NT_STATUS_OK;
685 no_mem:
687 TALLOC_FREE(entry);
688 return NT_STATUS_NO_MEMORY;
691 /*******************************************************************
692 Remove a WINBINDD_CCACHE_ENTRY entry and the krb5 ccache if no longer
693 referenced.
694 *******************************************************************/
696 NTSTATUS remove_ccache(const char *username)
698 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
699 NTSTATUS status = NT_STATUS_OK;
700 #ifdef HAVE_KRB5
701 krb5_error_code ret;
702 #endif
704 if (!entry) {
705 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
708 if (entry->ref_count <= 0) {
709 DEBUG(0,("remove_ccache: logic error. "
710 "ref count for user %s = %d\n",
711 username, entry->ref_count));
712 return NT_STATUS_INTERNAL_DB_CORRUPTION;
715 entry->ref_count--;
717 if (entry->ref_count > 0) {
718 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
719 username, entry->ref_count));
720 return NT_STATUS_OK;
723 /* no references any more */
725 DLIST_REMOVE(ccache_list, entry);
726 TALLOC_FREE(entry->event); /* unregisters events */
728 #ifdef HAVE_KRB5
729 ret = ads_kdestroy(entry->ccname);
731 /* we ignore the error when there has been no credential cache */
732 if (ret == KRB5_FCC_NOFILE) {
733 ret = 0;
734 } else if (ret) {
735 DEBUG(0,("remove_ccache: "
736 "failed to destroy user krb5 ccache %s with: %s\n",
737 entry->ccname, error_message(ret)));
738 } else {
739 DEBUG(10,("remove_ccache: "
740 "successfully destroyed krb5 ccache %s for user %s\n",
741 entry->ccname, username));
743 status = krb5_to_nt_status(ret);
744 #endif
746 TALLOC_FREE(entry);
747 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username));
749 return status;
752 /*******************************************************************
753 In memory credentials cache code.
754 *******************************************************************/
756 static struct WINBINDD_MEMORY_CREDS *memory_creds_list;
758 /***********************************************************
759 Find an entry on the list by name.
760 ***********************************************************/
762 struct WINBINDD_MEMORY_CREDS *find_memory_creds_by_name(const char *username)
764 struct WINBINDD_MEMORY_CREDS *p;
766 for (p = memory_creds_list; p; p = p->next) {
767 if (strequal(p->username, username)) {
768 return p;
771 return NULL;
774 /***********************************************************
775 Store the required creds and mlock them.
776 ***********************************************************/
778 static NTSTATUS store_memory_creds(struct WINBINDD_MEMORY_CREDS *memcredp,
779 const char *pass)
781 #if !defined(HAVE_MLOCK)
782 return NT_STATUS_OK;
783 #else
784 /* new_entry->nt_hash is the base pointer for the block
785 of memory pointed into by new_entry->lm_hash and
786 new_entry->pass (if we're storing plaintext). */
788 memcredp->len = NT_HASH_LEN + LM_HASH_LEN;
789 if (pass) {
790 memcredp->len += strlen(pass)+1;
794 #if defined(LINUX)
795 /* aligning the memory on on x86_64 and compiling
796 with gcc 4.1 using -O2 causes a segv in the
797 next memset() --jerry */
798 memcredp->nt_hash = SMB_MALLOC_ARRAY(unsigned char, memcredp->len);
799 #else
800 /* On non-linux platforms, mlock()'d memory must be aligned */
801 memcredp->nt_hash = SMB_MEMALIGN_ARRAY(unsigned char,
802 getpagesize(), memcredp->len);
803 #endif
804 if (!memcredp->nt_hash) {
805 return NT_STATUS_NO_MEMORY;
807 memset(memcredp->nt_hash, 0x0, memcredp->len);
809 memcredp->lm_hash = memcredp->nt_hash + NT_HASH_LEN;
811 #ifdef DEBUG_PASSWORD
812 DEBUG(10,("mlocking memory: %p\n", memcredp->nt_hash));
813 #endif
814 if ((mlock(memcredp->nt_hash, memcredp->len)) == -1) {
815 DEBUG(0,("failed to mlock memory: %s (%d)\n",
816 strerror(errno), errno));
817 SAFE_FREE(memcredp->nt_hash);
818 return map_nt_error_from_unix(errno);
821 #ifdef DEBUG_PASSWORD
822 DEBUG(10,("mlocked memory: %p\n", memcredp->nt_hash));
823 #endif
825 if (pass) {
826 /* Create and store the password hashes. */
827 E_md4hash(pass, memcredp->nt_hash);
828 E_deshash(pass, memcredp->lm_hash);
830 memcredp->pass = (char *)memcredp->lm_hash + LM_HASH_LEN;
831 memcpy(memcredp->pass, pass,
832 memcredp->len - NT_HASH_LEN - LM_HASH_LEN);
835 return NT_STATUS_OK;
836 #endif
839 /***********************************************************
840 Destroy existing creds.
841 ***********************************************************/
843 static NTSTATUS delete_memory_creds(struct WINBINDD_MEMORY_CREDS *memcredp)
845 #if !defined(HAVE_MUNLOCK)
846 return NT_STATUS_OK;
847 #else
848 if (munlock(memcredp->nt_hash, memcredp->len) == -1) {
849 DEBUG(0,("failed to munlock memory: %s (%d)\n",
850 strerror(errno), errno));
851 return map_nt_error_from_unix(errno);
853 memset(memcredp->nt_hash, '\0', memcredp->len);
854 SAFE_FREE(memcredp->nt_hash);
855 memcredp->nt_hash = NULL;
856 memcredp->lm_hash = NULL;
857 memcredp->pass = NULL;
858 memcredp->len = 0;
859 return NT_STATUS_OK;
860 #endif
863 /***********************************************************
864 Replace the required creds with new ones (password change).
865 ***********************************************************/
867 static NTSTATUS winbindd_replace_memory_creds_internal(struct WINBINDD_MEMORY_CREDS *memcredp,
868 const char *pass)
870 NTSTATUS status = delete_memory_creds(memcredp);
871 if (!NT_STATUS_IS_OK(status)) {
872 return status;
874 return store_memory_creds(memcredp, pass);
877 /*************************************************************
878 Store credentials in memory in a list.
879 *************************************************************/
881 static NTSTATUS winbindd_add_memory_creds_internal(const char *username,
882 uid_t uid,
883 const char *pass)
885 /* Shortcut to ensure we don't store if no mlock. */
886 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
887 return NT_STATUS_OK;
888 #else
889 NTSTATUS status;
890 struct WINBINDD_MEMORY_CREDS *memcredp = NULL;
892 memcredp = find_memory_creds_by_name(username);
893 if (uid == (uid_t)-1) {
894 DEBUG(0,("winbindd_add_memory_creds_internal: "
895 "invalid uid for user %s.\n", username));
896 return NT_STATUS_INVALID_PARAMETER;
899 if (memcredp) {
900 /* Already exists. Increment the reference count and replace stored creds. */
901 if (uid != memcredp->uid) {
902 DEBUG(0,("winbindd_add_memory_creds_internal: "
903 "uid %u for user %s doesn't "
904 "match stored uid %u. Replacing.\n",
905 (unsigned int)uid, username,
906 (unsigned int)memcredp->uid));
907 memcredp->uid = uid;
909 memcredp->ref_count++;
910 DEBUG(10,("winbindd_add_memory_creds_internal: "
911 "ref count for user %s is now %d\n",
912 username, memcredp->ref_count));
913 return winbindd_replace_memory_creds_internal(memcredp, pass);
916 memcredp = talloc_zero(NULL, struct WINBINDD_MEMORY_CREDS);
917 if (!memcredp) {
918 return NT_STATUS_NO_MEMORY;
920 memcredp->username = talloc_strdup(memcredp, username);
921 if (!memcredp->username) {
922 talloc_destroy(memcredp);
923 return NT_STATUS_NO_MEMORY;
926 status = store_memory_creds(memcredp, pass);
927 if (!NT_STATUS_IS_OK(status)) {
928 talloc_destroy(memcredp);
929 return status;
932 memcredp->uid = uid;
933 memcredp->ref_count = 1;
934 DLIST_ADD(memory_creds_list, memcredp);
936 DEBUG(10,("winbindd_add_memory_creds_internal: "
937 "added entry for user %s\n", username));
939 return NT_STATUS_OK;
940 #endif
943 /*************************************************************
944 Store users credentials in memory. If we also have a
945 struct WINBINDD_CCACHE_ENTRY for this username with a
946 refresh timer, then store the plaintext of the password
947 and associate the new credentials with the struct WINBINDD_CCACHE_ENTRY.
948 *************************************************************/
950 NTSTATUS winbindd_add_memory_creds(const char *username,
951 uid_t uid,
952 const char *pass)
954 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
955 NTSTATUS status;
957 status = winbindd_add_memory_creds_internal(username, uid, pass);
958 if (!NT_STATUS_IS_OK(status)) {
959 return status;
962 if (entry) {
963 struct WINBINDD_MEMORY_CREDS *memcredp = NULL;
964 memcredp = find_memory_creds_by_name(username);
965 if (memcredp) {
966 entry->cred_ptr = memcredp;
970 return status;
973 /*************************************************************
974 Decrement the in-memory ref count - delete if zero.
975 *************************************************************/
977 NTSTATUS winbindd_delete_memory_creds(const char *username)
979 struct WINBINDD_MEMORY_CREDS *memcredp = NULL;
980 struct WINBINDD_CCACHE_ENTRY *entry = NULL;
981 NTSTATUS status = NT_STATUS_OK;
983 memcredp = find_memory_creds_by_name(username);
984 entry = get_ccache_by_username(username);
986 if (!memcredp) {
987 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
988 username));
989 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
992 if (memcredp->ref_count <= 0) {
993 DEBUG(0,("winbindd_delete_memory_creds: logic error. "
994 "ref count for user %s = %d\n",
995 username, memcredp->ref_count));
996 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
999 memcredp->ref_count--;
1000 if (memcredp->ref_count <= 0) {
1001 delete_memory_creds(memcredp);
1002 DLIST_REMOVE(memory_creds_list, memcredp);
1003 talloc_destroy(memcredp);
1004 DEBUG(10,("winbindd_delete_memory_creds: "
1005 "deleted entry for user %s\n",
1006 username));
1007 } else {
1008 DEBUG(10,("winbindd_delete_memory_creds: "
1009 "entry for user %s ref_count now %d\n",
1010 username, memcredp->ref_count));
1013 if (entry) {
1014 /* Ensure we have no dangling references to this. */
1015 entry->cred_ptr = NULL;
1018 return status;
1021 /***********************************************************
1022 Replace the required creds with new ones (password change).
1023 ***********************************************************/
1025 NTSTATUS winbindd_replace_memory_creds(const char *username,
1026 const char *pass)
1028 struct WINBINDD_MEMORY_CREDS *memcredp = NULL;
1030 memcredp = find_memory_creds_by_name(username);
1031 if (!memcredp) {
1032 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
1033 username));
1034 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1037 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
1038 username));
1040 return winbindd_replace_memory_creds_internal(memcredp, pass);