s3:events: change event_add_timed() prototype to match samba4
[Samba/bb.git] / source / winbindd / winbindd_cred_cache.c
bloba922a04894f3a2c1ed6c7e8b34d73319b4a80f1c
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 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
29 /* uncomment this to do fast debugging on the krb5 ticket renewal event */
30 #ifdef DEBUG_KRB5_TKT_RENEWAL
31 #undef DEBUG_KRB5_TKT_RENEWAL
32 #endif
34 #define MAX_CCACHES 100
36 static struct WINBINDD_CCACHE_ENTRY *ccache_list;
37 static void krb5_ticket_gain_handler(struct event_context *,
38 struct timed_event *,
39 struct timeval,
40 void *);
42 /* The Krb5 ticket refresh handler should be scheduled
43 at one-half of the period from now till the tkt
44 expiration */
45 #define KRB5_EVENT_REFRESH_TIME(x) ((x) - (((x) - time(NULL))/2))
47 /****************************************************************
48 Find an entry by name.
49 ****************************************************************/
51 static struct WINBINDD_CCACHE_ENTRY *get_ccache_by_username(const char *username)
53 struct WINBINDD_CCACHE_ENTRY *entry;
55 for (entry = ccache_list; entry; entry = entry->next) {
56 if (strequal(entry->username, username)) {
57 return entry;
60 return NULL;
63 /****************************************************************
64 How many do we have ?
65 ****************************************************************/
67 static int ccache_entry_count(void)
69 struct WINBINDD_CCACHE_ENTRY *entry;
70 int i = 0;
72 for (entry = ccache_list; entry; entry = entry->next) {
73 i++;
75 return i;
78 void ccache_remove_all_after_fork(void)
80 struct WINBINDD_CCACHE_ENTRY *cur, *next;
82 for (cur = ccache_list; cur; cur = next) {
83 next = cur->next;
84 DLIST_REMOVE(ccache_list, cur);
85 TALLOC_FREE(cur->event);
86 TALLOC_FREE(cur);
89 return;
92 /****************************************************************
93 Do the work of refreshing the ticket.
94 ****************************************************************/
96 static void krb5_ticket_refresh_handler(struct event_context *event_ctx,
97 struct timed_event *te,
98 struct timeval now,
99 void *private_data)
101 struct WINBINDD_CCACHE_ENTRY *entry =
102 talloc_get_type_abort(private_data, struct WINBINDD_CCACHE_ENTRY);
103 #ifdef HAVE_KRB5
104 int ret;
105 time_t new_start;
106 time_t expire_time = 0;
107 struct WINBINDD_MEMORY_CREDS *cred_ptr = entry->cred_ptr;
108 #endif
110 DEBUG(10,("krb5_ticket_refresh_handler called\n"));
111 DEBUGADD(10,("event called for: %s, %s\n",
112 entry->ccname, entry->username));
114 TALLOC_FREE(entry->event);
116 #ifdef HAVE_KRB5
118 /* Kinit again if we have the user password and we can't renew the old
119 * tgt anymore
120 * NB
121 * This happens when machine are put to sleep for a very long time. */
123 if (entry->renew_until < time(NULL)) {
124 rekinit:
125 if (cred_ptr && cred_ptr->pass) {
127 set_effective_uid(entry->uid);
129 ret = kerberos_kinit_password_ext(entry->principal_name,
130 cred_ptr->pass,
131 0, /* hm, can we do time correction here ? */
132 &entry->refresh_time,
133 &entry->renew_until,
134 entry->ccname,
135 False, /* no PAC required anymore */
136 True,
137 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
138 NULL);
139 gain_root_privilege();
141 if (ret) {
142 DEBUG(3,("krb5_ticket_refresh_handler: "
143 "could not re-kinit: %s\n",
144 error_message(ret)));
145 /* destroy the ticket because we cannot rekinit
146 * it, ignore error here */
147 ads_kdestroy(entry->ccname);
149 /* Don't break the ticket refresh chain: retry
150 * refreshing ticket sometime later when KDC is
151 * unreachable -- BoYang. More error code handling
152 * here?
153 * */
155 if ((ret == KRB5_KDC_UNREACH)
156 || (ret == KRB5_REALM_CANT_RESOLVE)) {
157 #if defined(DEBUG_KRB5_TKT_RENEWAL)
158 new_start = time(NULL) + 30;
159 #else
160 new_start = time(NULL) +
161 MAX(30, lp_winbind_cache_time());
162 #endif
163 entry->refresh_time = 0;
164 /* try to regain ticket here */
165 entry->event = event_add_timed(winbind_event_context(),
166 entry,
167 timeval_set(new_start, 0),
168 krb5_ticket_gain_handler,
169 entry);
170 return;
172 TALLOC_FREE(entry->event);
173 return;
176 DEBUG(10,("krb5_ticket_refresh_handler: successful re-kinit "
177 "for: %s in ccache: %s\n",
178 entry->principal_name, entry->ccname));
180 #if defined(DEBUG_KRB5_TKT_RENEWAL)
181 new_start = time(NULL) + 30;
182 #else
183 /* The tkt should be refreshed at one-half the period
184 from now to the expiration time */
185 expire_time = entry->refresh_time;
186 new_start = KRB5_EVENT_REFRESH_TIME(entry->refresh_time);
187 #endif
188 goto done;
189 } else {
190 /* can this happen?
191 * No cached credentials
192 * destroy ticket and refresh chain
193 * */
194 ads_kdestroy(entry->ccname);
195 TALLOC_FREE(entry->event);
196 return;
200 set_effective_uid(entry->uid);
202 ret = smb_krb5_renew_ticket(entry->ccname,
203 entry->principal_name,
204 entry->service,
205 &new_start);
206 #if defined(DEBUG_KRB5_TKT_RENEWAL)
207 new_start = time(NULL) + 30;
208 #else
209 expire_time = new_start;
210 new_start = KRB5_EVENT_REFRESH_TIME(new_start);
211 #endif
213 gain_root_privilege();
215 if (ret) {
216 DEBUG(3,("krb5_ticket_refresh_handler: "
217 "could not renew tickets: %s\n",
218 error_message(ret)));
219 /* maybe we are beyond the renewing window */
221 /* evil rises here, we refresh ticket failed,
222 * but the ticket might be expired. Therefore,
223 * When we refresh ticket failed, destory the
224 * ticket */
226 ads_kdestroy(entry->ccname);
228 /* avoid breaking the renewal chain: retry in
229 * lp_winbind_cache_time() seconds when the KDC was not
230 * available right now.
231 * the return code can be KRB5_REALM_CANT_RESOLVE.
232 * More error code handling here? */
234 if ((ret == KRB5_KDC_UNREACH)
235 || (ret == KRB5_REALM_CANT_RESOLVE)) {
236 #if defined(DEBUG_KRB5_TKT_RENEWAL)
237 new_start = time(NULL) + 30;
238 #else
239 new_start = time(NULL) +
240 MAX(30, lp_winbind_cache_time());
241 #endif
242 /* ticket is destroyed here, we have to regain it
243 * if it is possible */
244 entry->refresh_time = 0;
245 entry->event = event_add_timed(winbind_event_context(),
246 entry,
247 timeval_set(new_start, 0),
248 krb5_ticket_gain_handler,
249 entry);
250 return;
253 /* This is evil, if the ticket was already expired.
254 * renew ticket function returns KRB5KRB_AP_ERR_TKT_EXPIRED.
255 * But there is still a chance that we can rekinit it.
257 * This happens when user login in online mode, and then network
258 * down or something cause winbind goes offline for a very long time,
259 * and then goes online again. ticket expired, renew failed.
260 * This happens when machine are put to sleep for a long time,
261 * but shorter than entry->renew_util.
262 * NB
263 * Looks like the KDC is reachable, we want to rekinit as soon as
264 * possible instead of waiting some time later. */
265 if ((ret == KRB5KRB_AP_ERR_TKT_EXPIRED)
266 || (ret == KRB5_FCC_NOFILE)) goto rekinit;
268 return;
271 done:
272 /* in cases that ticket will be unrenewable soon, we don't try to renew ticket
273 * but try to regain ticket if it is possible */
274 if (entry->renew_until && expire_time
275 && (entry->renew_until <= expire_time)) {
276 /* try to regain ticket 10 seconds beforre expiration */
277 expire_time -= 10;
278 entry->refresh_time = 0;
279 entry->event = event_add_timed(winbind_event_context(), entry,
280 timeval_set(expire_time, 0),
281 krb5_ticket_gain_handler,
282 entry);
283 return;
286 if (entry->refresh_time == 0) {
287 entry->refresh_time = new_start;
289 entry->event = event_add_timed(winbind_event_context(), entry,
290 timeval_set(new_start, 0),
291 krb5_ticket_refresh_handler,
292 entry);
294 #endif
297 /****************************************************************
298 Do the work of regaining a ticket when coming from offline auth.
299 ****************************************************************/
301 static void krb5_ticket_gain_handler(struct event_context *event_ctx,
302 struct timed_event *te,
303 struct timeval now,
304 void *private_data)
306 struct WINBINDD_CCACHE_ENTRY *entry =
307 talloc_get_type_abort(private_data, struct WINBINDD_CCACHE_ENTRY);
308 #ifdef HAVE_KRB5
309 int ret;
310 struct timeval t;
311 struct WINBINDD_MEMORY_CREDS *cred_ptr = entry->cred_ptr;
312 struct winbindd_domain *domain = NULL;
313 #endif
315 DEBUG(10,("krb5_ticket_gain_handler called\n"));
316 DEBUGADD(10,("event called for: %s, %s\n",
317 entry->ccname, entry->username));
319 TALLOC_FREE(entry->event);
321 #ifdef HAVE_KRB5
323 if (!cred_ptr || !cred_ptr->pass) {
324 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
325 return;
328 if ((domain = find_domain_from_name(entry->realm)) == NULL) {
329 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
330 return;
333 if (!domain->online) {
334 goto retry_later;
337 set_effective_uid(entry->uid);
339 ret = kerberos_kinit_password_ext(entry->principal_name,
340 cred_ptr->pass,
341 0, /* hm, can we do time correction here ? */
342 &entry->refresh_time,
343 &entry->renew_until,
344 entry->ccname,
345 False, /* no PAC required anymore */
346 True,
347 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME,
348 NULL);
349 gain_root_privilege();
351 if (ret) {
352 DEBUG(3,("krb5_ticket_gain_handler: "
353 "could not kinit: %s\n",
354 error_message(ret)));
355 /* evil. If we cannot do it, destroy any the __maybe__
356 * __existing__ ticket */
357 ads_kdestroy(entry->ccname);
358 goto retry_later;
361 DEBUG(10,("krb5_ticket_gain_handler: "
362 "successful kinit for: %s in ccache: %s\n",
363 entry->principal_name, entry->ccname));
365 goto got_ticket;
367 retry_later:
369 #if defined(DEBUG_KRB5_TKT_REGAIN)
370 t = timeval_set(time(NULL) + 30, 0);
371 #else
372 t = timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
373 #endif
375 entry->refresh_time = 0;
376 entry->event = event_add_timed(winbind_event_context(),
377 entry,
379 krb5_ticket_gain_handler,
380 entry);
382 return;
384 got_ticket:
386 #if defined(DEBUG_KRB5_TKT_RENEWAL)
387 t = timeval_set(time(NULL) + 30, 0);
388 #else
389 t = timeval_set(KRB5_EVENT_REFRESH_TIME(entry->refresh_time), 0);
390 #endif
392 if (entry->refresh_time == 0) {
393 entry->refresh_time = t.tv_sec;
395 entry->event = event_add_timed(winbind_event_context(),
396 entry,
398 krb5_ticket_refresh_handler,
399 entry);
401 return;
402 #endif
405 void ccache_regain_all_now(void)
407 struct WINBINDD_CCACHE_ENTRY *cur;
408 struct timeval t = timeval_current();
410 for (cur = ccache_list; cur; cur = cur->next) {
411 struct timed_event *new_event;
414 * if refresh_time is 0, we know that the
415 * the event has the krb5_ticket_gain_handler
417 if (cur->refresh_time == 0) {
418 new_event = event_add_timed(winbind_event_context(),
419 cur,
421 krb5_ticket_gain_handler,
422 cur);
423 } else {
424 new_event = event_add_timed(winbind_event_context(),
425 cur,
427 krb5_ticket_refresh_handler,
428 cur);
431 if (!new_event) {
432 continue;
435 TALLOC_FREE(cur->event);
436 cur->event = new_event;
439 return;
442 /****************************************************************
443 Check if an ccache entry exists.
444 ****************************************************************/
446 bool ccache_entry_exists(const char *username)
448 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
449 return (entry != NULL);
452 /****************************************************************
453 Ensure we're changing the correct entry.
454 ****************************************************************/
456 bool ccache_entry_identical(const char *username,
457 uid_t uid,
458 const char *ccname)
460 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
462 if (!entry) {
463 return False;
466 if (entry->uid != uid) {
467 DEBUG(0,("cache_entry_identical: uid's differ: %u != %u\n",
468 (unsigned int)entry->uid, (unsigned int)uid));
469 return False;
471 if (!strcsequal(entry->ccname, ccname)) {
472 DEBUG(0,("cache_entry_identical: "
473 "ccnames differ: (cache) %s != (client) %s\n",
474 entry->ccname, ccname));
475 return False;
477 return True;
480 NTSTATUS add_ccache_to_list(const char *princ_name,
481 const char *ccname,
482 const char *service,
483 const char *username,
484 const char *realm,
485 uid_t uid,
486 time_t create_time,
487 time_t ticket_end,
488 time_t renew_until,
489 bool postponed_request)
491 struct WINBINDD_CCACHE_ENTRY *entry = NULL;
492 struct timeval t;
493 NTSTATUS ntret;
494 #ifdef HAVE_KRB5
495 int ret;
496 #endif
498 if ((username == NULL && princ_name == NULL) ||
499 ccname == NULL || uid < 0) {
500 return NT_STATUS_INVALID_PARAMETER;
503 if (ccache_entry_count() + 1 > MAX_CCACHES) {
504 DEBUG(10,("add_ccache_to_list: "
505 "max number of ccaches reached\n"));
506 return NT_STATUS_NO_MORE_ENTRIES;
509 /* If it is cached login, destroy krb5 ticket
510 * to avoid surprise. */
511 #ifdef HAVE_KRB5
512 if (postponed_request) {
513 /* ignore KRB5_FCC_NOFILE error here */
514 ret = ads_kdestroy(ccname);
515 if (ret == KRB5_FCC_NOFILE) {
516 ret = 0;
518 if (ret) {
519 DEBUG(0, ("add_ccache_to_list: failed to destroy "
520 "user krb5 ccache %s with %s\n", ccname,
521 error_message(ret)));
522 return krb5_to_nt_status(ret);
523 } else {
524 DEBUG(10, ("add_ccache_to_list: successfully destroyed "
525 "krb5 ccache %s for user %s\n", ccname,
526 username));
529 #endif
531 /* Reference count old entries */
532 entry = get_ccache_by_username(username);
533 if (entry) {
534 /* Check cached entries are identical. */
535 if (!ccache_entry_identical(username, uid, ccname)) {
536 return NT_STATUS_INVALID_PARAMETER;
538 entry->ref_count++;
539 DEBUG(10,("add_ccache_to_list: "
540 "ref count on entry %s is now %d\n",
541 username, entry->ref_count));
542 /* FIXME: in this case we still might want to have a krb5 cred
543 * event handler created - gd
544 * Add ticket refresh handler here */
546 if (!lp_winbind_refresh_tickets() || renew_until <= 0) {
547 return NT_STATUS_OK;
550 if (!entry->event) {
551 if (postponed_request) {
552 t = timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
553 entry->refresh_time = 0;
554 entry->event = event_add_timed(winbind_event_context(),
555 entry,
557 krb5_ticket_gain_handler,
558 entry);
559 } else {
560 /* Renew at 1/2 the ticket expiration time */
561 #if defined(DEBUG_KRB5_TKT_RENEWAL)
562 t = timeval_set(time(NULL)+30, 0);
563 #else
564 t = timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end), 0);
565 #endif
566 if (!entry->refresh_time) {
567 entry->refresh_time = t.tv_sec;
569 entry->event = event_add_timed(winbind_event_context(),
570 entry,
572 krb5_ticket_refresh_handler,
573 entry);
576 if (!entry->event) {
577 ntret = remove_ccache(username);
578 if (!NT_STATUS_IS_OK(ntret)) {
579 DEBUG(0, ("add_ccache_to_list: Failed to remove krb5 "
580 "ccache %s for user %s\n", entry->ccname,
581 entry->username));
582 DEBUG(0, ("add_ccache_to_list: error is %s\n",
583 nt_errstr(ntret)));
584 return ntret;
586 return NT_STATUS_NO_MEMORY;
589 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
592 return NT_STATUS_OK;
595 entry = TALLOC_P(NULL, struct WINBINDD_CCACHE_ENTRY);
596 if (!entry) {
597 return NT_STATUS_NO_MEMORY;
600 ZERO_STRUCTP(entry);
602 if (username) {
603 entry->username = talloc_strdup(entry, username);
604 if (!entry->username) {
605 goto no_mem;
608 if (princ_name) {
609 entry->principal_name = talloc_strdup(entry, princ_name);
610 if (!entry->principal_name) {
611 goto no_mem;
614 if (service) {
615 entry->service = talloc_strdup(entry, service);
616 if (!entry->service) {
617 goto no_mem;
621 entry->ccname = talloc_strdup(entry, ccname);
622 if (!entry->ccname) {
623 goto no_mem;
626 entry->realm = talloc_strdup(entry, realm);
627 if (!entry->realm) {
628 goto no_mem;
631 entry->create_time = create_time;
632 entry->renew_until = renew_until;
633 entry->uid = uid;
634 entry->ref_count = 1;
636 if (!lp_winbind_refresh_tickets() || renew_until <= 0) {
637 goto add_entry;
640 if (postponed_request) {
641 t = timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
642 entry->refresh_time = 0;
643 entry->event = event_add_timed(winbind_event_context(),
644 entry,
646 krb5_ticket_gain_handler,
647 entry);
648 } else {
649 /* Renew at 1/2 the ticket expiration time */
650 #if defined(DEBUG_KRB5_TKT_RENEWAL)
651 t = timeval_set(time(NULL)+30, 0);
652 #else
653 t = timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end), 0);
654 #endif
655 if (entry->refresh_time == 0) {
656 entry->refresh_time = t.tv_sec;
658 entry->event = event_add_timed(winbind_event_context(),
659 entry,
661 krb5_ticket_refresh_handler,
662 entry);
665 if (!entry->event) {
666 goto no_mem;
669 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
671 add_entry:
673 DLIST_ADD(ccache_list, entry);
675 DEBUG(10,("add_ccache_to_list: "
676 "added ccache [%s] for user [%s] to the list\n",
677 ccname, username));
679 return NT_STATUS_OK;
681 no_mem:
683 TALLOC_FREE(entry);
684 return NT_STATUS_NO_MEMORY;
687 /*******************************************************************
688 Remove a WINBINDD_CCACHE_ENTRY entry and the krb5 ccache if no longer
689 referenced.
690 *******************************************************************/
692 NTSTATUS remove_ccache(const char *username)
694 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
695 NTSTATUS status = NT_STATUS_OK;
696 #ifdef HAVE_KRB5
697 krb5_error_code ret;
698 #endif
700 if (!entry) {
701 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
704 if (entry->ref_count <= 0) {
705 DEBUG(0,("remove_ccache: logic error. "
706 "ref count for user %s = %d\n",
707 username, entry->ref_count));
708 return NT_STATUS_INTERNAL_DB_CORRUPTION;
711 entry->ref_count--;
713 if (entry->ref_count > 0) {
714 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
715 username, entry->ref_count));
716 return NT_STATUS_OK;
719 /* no references any more */
721 DLIST_REMOVE(ccache_list, entry);
722 TALLOC_FREE(entry->event); /* unregisters events */
724 #ifdef HAVE_KRB5
725 ret = ads_kdestroy(entry->ccname);
727 /* we ignore the error when there has been no credential cache */
728 if (ret == KRB5_FCC_NOFILE) {
729 ret = 0;
730 } else if (ret) {
731 DEBUG(0,("remove_ccache: "
732 "failed to destroy user krb5 ccache %s with: %s\n",
733 entry->ccname, error_message(ret)));
734 } else {
735 DEBUG(10,("remove_ccache: "
736 "successfully destroyed krb5 ccache %s for user %s\n",
737 entry->ccname, username));
739 status = krb5_to_nt_status(ret);
740 #endif
742 TALLOC_FREE(entry);
743 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username));
745 return status;
748 /*******************************************************************
749 In memory credentials cache code.
750 *******************************************************************/
752 static struct WINBINDD_MEMORY_CREDS *memory_creds_list;
754 /***********************************************************
755 Find an entry on the list by name.
756 ***********************************************************/
758 struct WINBINDD_MEMORY_CREDS *find_memory_creds_by_name(const char *username)
760 struct WINBINDD_MEMORY_CREDS *p;
762 for (p = memory_creds_list; p; p = p->next) {
763 if (strequal(p->username, username)) {
764 return p;
767 return NULL;
770 /***********************************************************
771 Store the required creds and mlock them.
772 ***********************************************************/
774 static NTSTATUS store_memory_creds(struct WINBINDD_MEMORY_CREDS *memcredp,
775 const char *pass)
777 #if !defined(HAVE_MLOCK)
778 return NT_STATUS_OK;
779 #else
780 /* new_entry->nt_hash is the base pointer for the block
781 of memory pointed into by new_entry->lm_hash and
782 new_entry->pass (if we're storing plaintext). */
784 memcredp->len = NT_HASH_LEN + LM_HASH_LEN;
785 if (pass) {
786 memcredp->len += strlen(pass)+1;
790 #if defined(LINUX)
791 /* aligning the memory on on x86_64 and compiling
792 with gcc 4.1 using -O2 causes a segv in the
793 next memset() --jerry */
794 memcredp->nt_hash = SMB_MALLOC_ARRAY(unsigned char, memcredp->len);
795 #else
796 /* On non-linux platforms, mlock()'d memory must be aligned */
797 memcredp->nt_hash = SMB_MEMALIGN_ARRAY(unsigned char,
798 getpagesize(), memcredp->len);
799 #endif
800 if (!memcredp->nt_hash) {
801 return NT_STATUS_NO_MEMORY;
803 memset(memcredp->nt_hash, 0x0, memcredp->len);
805 memcredp->lm_hash = memcredp->nt_hash + NT_HASH_LEN;
807 #ifdef DEBUG_PASSWORD
808 DEBUG(10,("mlocking memory: %p\n", memcredp->nt_hash));
809 #endif
810 if ((mlock(memcredp->nt_hash, memcredp->len)) == -1) {
811 DEBUG(0,("failed to mlock memory: %s (%d)\n",
812 strerror(errno), errno));
813 SAFE_FREE(memcredp->nt_hash);
814 return map_nt_error_from_unix(errno);
817 #ifdef DEBUG_PASSWORD
818 DEBUG(10,("mlocked memory: %p\n", memcredp->nt_hash));
819 #endif
821 /* Create and store the password hashes. */
822 E_md4hash(pass, memcredp->nt_hash);
823 E_deshash(pass, memcredp->lm_hash);
825 if (pass) {
826 memcredp->pass = (char *)memcredp->lm_hash + LM_HASH_LEN;
827 memcpy(memcredp->pass, pass,
828 memcredp->len - NT_HASH_LEN - LM_HASH_LEN);
831 return NT_STATUS_OK;
832 #endif
835 /***********************************************************
836 Destroy existing creds.
837 ***********************************************************/
839 static NTSTATUS delete_memory_creds(struct WINBINDD_MEMORY_CREDS *memcredp)
841 #if !defined(HAVE_MUNLOCK)
842 return NT_STATUS_OK;
843 #else
844 if (munlock(memcredp->nt_hash, memcredp->len) == -1) {
845 DEBUG(0,("failed to munlock memory: %s (%d)\n",
846 strerror(errno), errno));
847 return map_nt_error_from_unix(errno);
849 memset(memcredp->nt_hash, '\0', memcredp->len);
850 SAFE_FREE(memcredp->nt_hash);
851 memcredp->nt_hash = NULL;
852 memcredp->lm_hash = NULL;
853 memcredp->pass = NULL;
854 memcredp->len = 0;
855 return NT_STATUS_OK;
856 #endif
859 /***********************************************************
860 Replace the required creds with new ones (password change).
861 ***********************************************************/
863 static NTSTATUS winbindd_replace_memory_creds_internal(struct WINBINDD_MEMORY_CREDS *memcredp,
864 const char *pass)
866 NTSTATUS status = delete_memory_creds(memcredp);
867 if (!NT_STATUS_IS_OK(status)) {
868 return status;
870 return store_memory_creds(memcredp, pass);
873 /*************************************************************
874 Store credentials in memory in a list.
875 *************************************************************/
877 static NTSTATUS winbindd_add_memory_creds_internal(const char *username,
878 uid_t uid,
879 const char *pass)
881 /* Shortcut to ensure we don't store if no mlock. */
882 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
883 return NT_STATUS_OK;
884 #else
885 NTSTATUS status;
886 struct WINBINDD_MEMORY_CREDS *memcredp = NULL;
888 memcredp = find_memory_creds_by_name(username);
889 if (uid == (uid_t)-1) {
890 DEBUG(0,("winbindd_add_memory_creds_internal: "
891 "invalid uid for user %s.\n", username));
892 return NT_STATUS_INVALID_PARAMETER;
895 if (memcredp) {
896 /* Already exists. Increment the reference count and replace stored creds. */
897 if (uid != memcredp->uid) {
898 DEBUG(0,("winbindd_add_memory_creds_internal: "
899 "uid %u for user %s doesn't "
900 "match stored uid %u. Replacing.\n",
901 (unsigned int)uid, username,
902 (unsigned int)memcredp->uid));
903 memcredp->uid = uid;
905 memcredp->ref_count++;
906 DEBUG(10,("winbindd_add_memory_creds_internal: "
907 "ref count for user %s is now %d\n",
908 username, memcredp->ref_count));
909 return winbindd_replace_memory_creds_internal(memcredp, pass);
912 memcredp = TALLOC_ZERO_P(NULL, struct WINBINDD_MEMORY_CREDS);
913 if (!memcredp) {
914 return NT_STATUS_NO_MEMORY;
916 memcredp->username = talloc_strdup(memcredp, username);
917 if (!memcredp->username) {
918 talloc_destroy(memcredp);
919 return NT_STATUS_NO_MEMORY;
922 status = store_memory_creds(memcredp, pass);
923 if (!NT_STATUS_IS_OK(status)) {
924 talloc_destroy(memcredp);
925 return status;
928 memcredp->uid = uid;
929 memcredp->ref_count = 1;
930 DLIST_ADD(memory_creds_list, memcredp);
932 DEBUG(10,("winbindd_add_memory_creds_internal: "
933 "added entry for user %s\n", username));
935 return NT_STATUS_OK;
936 #endif
939 /*************************************************************
940 Store users credentials in memory. If we also have a
941 struct WINBINDD_CCACHE_ENTRY for this username with a
942 refresh timer, then store the plaintext of the password
943 and associate the new credentials with the struct WINBINDD_CCACHE_ENTRY.
944 *************************************************************/
946 NTSTATUS winbindd_add_memory_creds(const char *username,
947 uid_t uid,
948 const char *pass)
950 struct WINBINDD_CCACHE_ENTRY *entry = get_ccache_by_username(username);
951 NTSTATUS status;
953 status = winbindd_add_memory_creds_internal(username, uid, pass);
954 if (!NT_STATUS_IS_OK(status)) {
955 return status;
958 if (entry) {
959 struct WINBINDD_MEMORY_CREDS *memcredp = NULL;
960 memcredp = find_memory_creds_by_name(username);
961 if (memcredp) {
962 entry->cred_ptr = memcredp;
966 return status;
969 /*************************************************************
970 Decrement the in-memory ref count - delete if zero.
971 *************************************************************/
973 NTSTATUS winbindd_delete_memory_creds(const char *username)
975 struct WINBINDD_MEMORY_CREDS *memcredp = NULL;
976 struct WINBINDD_CCACHE_ENTRY *entry = NULL;
977 NTSTATUS status = NT_STATUS_OK;
979 memcredp = find_memory_creds_by_name(username);
980 entry = get_ccache_by_username(username);
982 if (!memcredp) {
983 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
984 username));
985 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
988 if (memcredp->ref_count <= 0) {
989 DEBUG(0,("winbindd_delete_memory_creds: logic error. "
990 "ref count for user %s = %d\n",
991 username, memcredp->ref_count));
992 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
995 memcredp->ref_count--;
996 if (memcredp->ref_count <= 0) {
997 delete_memory_creds(memcredp);
998 DLIST_REMOVE(memory_creds_list, memcredp);
999 talloc_destroy(memcredp);
1000 DEBUG(10,("winbindd_delete_memory_creds: "
1001 "deleted entry for user %s\n",
1002 username));
1003 } else {
1004 DEBUG(10,("winbindd_delete_memory_creds: "
1005 "entry for user %s ref_count now %d\n",
1006 username, memcredp->ref_count));
1009 if (entry) {
1010 /* Ensure we have no dangling references to this. */
1011 entry->cred_ptr = NULL;
1014 return status;
1017 /***********************************************************
1018 Replace the required creds with new ones (password change).
1019 ***********************************************************/
1021 NTSTATUS winbindd_replace_memory_creds(const char *username,
1022 const char *pass)
1024 struct WINBINDD_MEMORY_CREDS *memcredp = NULL;
1026 memcredp = find_memory_creds_by_name(username);
1027 if (!memcredp) {
1028 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
1029 username));
1030 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1033 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
1034 username));
1036 return winbindd_replace_memory_creds_internal(memcredp, pass);