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/>.
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
34 #define MAX_CCACHES 100
36 static struct WINBINDD_CCACHE_ENTRY
*ccache_list
;
37 static void krb5_ticket_gain_handler(struct event_context
*,
39 const struct timeval
*,
42 /* The Krb5 ticket refresh handler should be scheduled
43 at one-half of the period from now till the tkt
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
)) {
63 /****************************************************************
65 ****************************************************************/
67 static int ccache_entry_count(void)
69 struct WINBINDD_CCACHE_ENTRY
*entry
;
72 for (entry
= ccache_list
; entry
; entry
= entry
->next
) {
78 void ccache_remove_all_after_fork(void)
80 struct WINBINDD_CCACHE_ENTRY
*cur
, *next
;
82 for (cur
= ccache_list
; cur
; cur
= next
) {
84 DLIST_REMOVE(ccache_list
, cur
);
85 TALLOC_FREE(cur
->event
);
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 const struct timeval
*now
,
101 struct WINBINDD_CCACHE_ENTRY
*entry
=
102 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
106 time_t expire_time
= 0;
107 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
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
);
118 /* Kinit again if we have the user password and we can't renew the old
121 * This happens when machine are put to sleep for a very long time. */
123 if (entry
->renew_until
< time(NULL
)) {
125 if (cred_ptr
&& cred_ptr
->pass
) {
127 set_effective_uid(entry
->uid
);
129 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
131 0, /* hm, can we do time correction here ? */
132 &entry
->refresh_time
,
135 False
, /* no PAC required anymore */
137 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
139 gain_root_privilege();
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
154 if ((ret
== KRB5_KDC_UNREACH
)
155 || (ret
== KRB5_REALM_CANT_RESOLVE
)) {
156 #if defined(DEBUG_KRB5_TKT_RENEWAL)
157 new_start
= time(NULL
) + 30;
159 new_start
= time(NULL
) +
160 MAX(30, lp_winbind_cache_time());
162 /* try to regain ticket here */
163 entry
->event
= event_add_timed(winbind_event_context(),
165 timeval_set(new_start
, 0),
166 "krb5_ticket_gain_handler",
167 krb5_ticket_gain_handler
,
171 TALLOC_FREE(entry
->event
);
175 DEBUG(10,("krb5_ticket_refresh_handler: successful re-kinit "
176 "for: %s in ccache: %s\n",
177 entry
->principal_name
, entry
->ccname
));
179 #if defined(DEBUG_KRB5_TKT_RENEWAL)
180 new_start
= time(NULL
) + 30;
182 /* The tkt should be refreshed at one-half the period
183 from now to the expiration time */
184 expire_time
= entry
->refresh_time
;
185 new_start
= KRB5_EVENT_REFRESH_TIME(entry
->refresh_time
);
190 * No cached credentials
191 * destroy ticket and refresh chain
193 ads_kdestroy(entry
->ccname
);
194 TALLOC_FREE(entry
->event
);
199 set_effective_uid(entry
->uid
);
201 ret
= smb_krb5_renew_ticket(entry
->ccname
,
202 entry
->principal_name
,
205 #if defined(DEBUG_KRB5_TKT_RENEWAL)
206 new_start
= time(NULL
) + 30;
208 expire_time
= new_start
;
209 new_start
= KRB5_EVENT_REFRESH_TIME(new_start
);
212 gain_root_privilege();
215 DEBUG(3,("krb5_ticket_refresh_handler: "
216 "could not renew tickets: %s\n",
217 error_message(ret
)));
218 /* maybe we are beyond the renewing window */
220 /* evil rises here, we refresh ticket failed,
221 * but the ticket might be expired. Therefore,
222 * When we refresh ticket failed, destory the
225 ads_kdestroy(entry
->ccname
);
227 /* avoid breaking the renewal chain: retry in
228 * lp_winbind_cache_time() seconds when the KDC was not
229 * available right now.
230 * the return code can be KRB5_REALM_CANT_RESOLVE*/
232 if ((ret
== KRB5_KDC_UNREACH
)
233 || (ret
== KRB5_REALM_CANT_RESOLVE
)) {
234 #if defined(DEBUG_KRB5_TKT_RENEWAL)
235 new_start
= time(NULL
) + 30;
237 new_start
= time(NULL
) +
238 MAX(30, lp_winbind_cache_time());
240 /* ticket is destroyed here, we have to regain it
241 * if it is possible */
242 entry
->event
= event_add_timed(winbind_event_context(),
244 timeval_set(new_start
, 0),
245 "krb5_ticket_gain_handler",
246 krb5_ticket_gain_handler
,
251 /* This is evil, if the ticket was already expired.
252 * renew ticket function returns KRB5KRB_AP_ERR_TKT_EXPIRED.
253 * But there is still a chance that we can rekinit it.
255 * This happens when user login in online mode, and then network
256 * down or something cause winbind goes offline for a very long time,
257 * and then goes online again. ticket expired, renew failed.
258 * This happens when machine are put to sleep for a long time,
259 * but shorter than entry->renew_util.
261 * Looks like the KDC is reachable, we want to rekinit as soon as
262 * possible instead of waiting some time later. */
263 if ((ret
== KRB5KRB_AP_ERR_TKT_EXPIRED
)
264 || (ret
== KRB5_FCC_NOFILE
)) goto rekinit
;
270 /* in cases that ticket will be unrenewable soon, we don't try to renew ticket
271 * but try to regain ticket if it is possible */
272 if (entry
->renew_until
&& expire_time
273 && (entry
->renew_until
<= expire_time
)) {
274 /* try to regain ticket 10 seconds beforre expiration */
276 entry
->event
= event_add_timed(winbind_event_context(), entry
,
277 timeval_set(expire_time
, 0),
278 "krb5_ticket_gain_handler",
279 krb5_ticket_gain_handler
,
284 if (entry
->refresh_time
== 0) {
285 entry
->refresh_time
= new_start
;
287 entry
->event
= event_add_timed(winbind_event_context(), entry
,
288 timeval_set(new_start
, 0),
289 "krb5_ticket_refresh_handler",
290 krb5_ticket_refresh_handler
,
296 /****************************************************************
297 Do the work of regaining a ticket when coming from offline auth.
298 ****************************************************************/
300 static void krb5_ticket_gain_handler(struct event_context
*event_ctx
,
301 struct timed_event
*te
,
302 const struct timeval
*now
,
305 struct WINBINDD_CCACHE_ENTRY
*entry
=
306 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
310 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
311 struct winbindd_domain
*domain
= NULL
;
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
);
322 if (!cred_ptr
|| !cred_ptr
->pass
) {
323 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
327 if ((domain
= find_domain_from_name(entry
->realm
)) == NULL
) {
328 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
332 if (!domain
->online
) {
336 set_effective_uid(entry
->uid
);
338 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
340 0, /* hm, can we do time correction here ? */
341 &entry
->refresh_time
,
344 False
, /* no PAC required anymore */
346 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
348 gain_root_privilege();
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
);
360 DEBUG(10,("krb5_ticket_gain_handler: "
361 "successful kinit for: %s in ccache: %s\n",
362 entry
->principal_name
, entry
->ccname
));
368 #if defined(DEBUG_KRB5_TKT_REGAIN)
369 t
= timeval_set(time(NULL
) + 30, 0);
371 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
374 entry
->refresh_time
= 0;
375 entry
->event
= event_add_timed(winbind_event_context(),
378 "krb5_ticket_gain_handler",
379 krb5_ticket_gain_handler
,
386 #if defined(DEBUG_KRB5_TKT_RENEWAL)
387 t
= timeval_set(time(NULL
) + 30, 0);
389 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(entry
->refresh_time
), 0);
392 if (entry
->refresh_time
== 0) {
393 entry
->refresh_time
= t
.tv_sec
;
395 entry
->event
= event_add_timed(winbind_event_context(),
398 "krb5_ticket_refresh_handler",
399 krb5_ticket_refresh_handler
,
406 void ccache_regain_all_now(void)
408 struct WINBINDD_CCACHE_ENTRY
*cur
;
409 struct timeval t
= timeval_current();
411 for (cur
= ccache_list
; cur
; cur
= cur
->next
) {
412 struct timed_event
*new_event
;
415 * if refresh_time is 0, we know that the
416 * the event has the krb5_ticket_gain_handler
418 if (cur
->refresh_time
== 0) {
419 new_event
= event_add_timed(winbind_event_context(),
422 "krb5_ticket_gain_handler",
423 krb5_ticket_gain_handler
,
426 new_event
= event_add_timed(winbind_event_context(),
429 "krb5_ticket_refresh_handler",
430 krb5_ticket_refresh_handler
,
438 TALLOC_FREE(cur
->event
);
439 cur
->event
= new_event
;
445 /****************************************************************
446 Check if an ccache entry exists.
447 ****************************************************************/
449 bool ccache_entry_exists(const char *username
)
451 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
452 return (entry
!= NULL
);
455 /****************************************************************
456 Ensure we're changing the correct entry.
457 ****************************************************************/
459 bool ccache_entry_identical(const char *username
,
463 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
469 if (entry
->uid
!= uid
) {
470 DEBUG(0,("cache_entry_identical: uid's differ: %u != %u\n",
471 (unsigned int)entry
->uid
, (unsigned int)uid
));
474 if (!strcsequal(entry
->ccname
, ccname
)) {
475 DEBUG(0,("cache_entry_identical: "
476 "ccnames differ: (cache) %s != (client) %s\n",
477 entry
->ccname
, ccname
));
483 NTSTATUS
add_ccache_to_list(const char *princ_name
,
486 const char *username
,
492 bool postponed_request
)
494 struct WINBINDD_CCACHE_ENTRY
*entry
= NULL
;
501 if ((username
== NULL
&& princ_name
== NULL
) ||
502 ccname
== NULL
|| uid
< 0) {
503 return NT_STATUS_INVALID_PARAMETER
;
506 if (ccache_entry_count() + 1 > MAX_CCACHES
) {
507 DEBUG(10,("add_ccache_to_list: "
508 "max number of ccaches reached\n"));
509 return NT_STATUS_NO_MORE_ENTRIES
;
512 /* If it is cached login, destroy krb5 ticket
513 * to avoid surprise. */
515 if (postponed_request
) {
516 /* ignore KRB5_FCC_NOFILE error here */
517 ret
= ads_kdestroy(ccname
);
518 if (ret
== KRB5_FCC_NOFILE
) {
522 DEBUG(0, ("add_ccache_to_list: failed to destroy "
523 "user krb5 ccache %s with %s\n", ccname
,
524 error_message(ret
)));
525 return krb5_to_nt_status(ret
);
527 DEBUG(10, ("add_ccache_to_list: successfully destroyed "
528 "krb5 ccache %s for user %s\n", ccname
,
534 /* Reference count old entries */
535 entry
= get_ccache_by_username(username
);
537 /* Check cached entries are identical. */
538 if (!ccache_entry_identical(username
, uid
, ccname
)) {
539 return NT_STATUS_INVALID_PARAMETER
;
542 DEBUG(10,("add_ccache_to_list: "
543 "ref count on entry %s is now %d\n",
544 username
, entry
->ref_count
));
545 /* FIXME: in this case we still might want to have a krb5 cred
546 * event handler created - gd
547 * Add ticket refresh handler here */
549 if (!lp_winbind_refresh_tickets() || renew_until
<= 0) {
554 if (postponed_request
) {
555 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
556 entry
->event
= event_add_timed(winbind_event_context(),
559 "krb5_ticket_gain_handler",
560 krb5_ticket_gain_handler
,
563 /* Renew at 1/2 the ticket expiration time */
564 #if defined(DEBUG_KRB5_TKT_RENEWAL)
565 t
= timeval_set(time(NULL
)+30, 0);
567 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end
), 0);
569 entry
->event
= event_add_timed(winbind_event_context(),
572 "krb5_ticket_refresh_handler",
573 krb5_ticket_refresh_handler
,
578 ntret
= remove_ccache(username
);
579 if (!NT_STATUS_IS_OK(ntret
)) {
580 DEBUG(0, ("add_ccache_to_list: Failed to remove krb5 "
581 "ccache %s for user %s\n", entry
->ccname
,
583 DEBUG(0, ("add_ccache_to_list: error is %s\n",
587 return NT_STATUS_NO_MEMORY
;
590 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
596 entry
= TALLOC_P(NULL
, struct WINBINDD_CCACHE_ENTRY
);
598 return NT_STATUS_NO_MEMORY
;
604 entry
->username
= talloc_strdup(entry
, username
);
605 if (!entry
->username
) {
610 entry
->principal_name
= talloc_strdup(entry
, princ_name
);
611 if (!entry
->principal_name
) {
616 entry
->service
= talloc_strdup(entry
, service
);
617 if (!entry
->service
) {
622 entry
->ccname
= talloc_strdup(entry
, ccname
);
623 if (!entry
->ccname
) {
627 entry
->realm
= talloc_strdup(entry
, realm
);
632 entry
->create_time
= create_time
;
633 entry
->renew_until
= renew_until
;
635 entry
->ref_count
= 1;
637 if (!lp_winbind_refresh_tickets() || renew_until
<= 0) {
641 if (postponed_request
) {
642 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
643 entry
->refresh_time
= 0;
644 entry
->event
= event_add_timed(winbind_event_context(),
647 "krb5_ticket_gain_handler",
648 krb5_ticket_gain_handler
,
651 /* Renew at 1/2 the ticket expiration time */
652 #if defined(DEBUG_KRB5_TKT_RENEWAL)
653 t
= timeval_set(time(NULL
)+30, 0);
655 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end
), 0);
657 if (entry
->refresh_time
== 0) {
658 entry
->refresh_time
= t
.tv_sec
;
660 entry
->event
= event_add_timed(winbind_event_context(),
663 "krb5_ticket_refresh_handler",
664 krb5_ticket_refresh_handler
,
672 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
676 DLIST_ADD(ccache_list
, entry
);
678 DEBUG(10,("add_ccache_to_list: "
679 "added ccache [%s] for user [%s] to the list\n",
687 return NT_STATUS_NO_MEMORY
;
690 /*******************************************************************
691 Remove a WINBINDD_CCACHE_ENTRY entry and the krb5 ccache if no longer
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
;
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
;
716 if (entry
->ref_count
> 0) {
717 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
718 username
, entry
->ref_count
));
722 /* no references any more */
724 DLIST_REMOVE(ccache_list
, entry
);
725 TALLOC_FREE(entry
->event
); /* unregisters events */
728 ret
= ads_kdestroy(entry
->ccname
);
730 /* we ignore the error when there has been no credential cache */
731 if (ret
== KRB5_FCC_NOFILE
) {
734 DEBUG(0,("remove_ccache: "
735 "failed to destroy user krb5 ccache %s with: %s\n",
736 entry
->ccname
, error_message(ret
)));
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
);
746 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username
));
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
)) {
773 /***********************************************************
774 Store the required creds and mlock them.
775 ***********************************************************/
777 static NTSTATUS
store_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
,
780 #if !defined(HAVE_MLOCK)
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
;
789 memcredp
->len
+= strlen(pass
)+1;
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
);
799 /* On non-linux platforms, mlock()'d memory must be aligned */
800 memcredp
->nt_hash
= SMB_MEMALIGN_ARRAY(unsigned char,
801 getpagesize(), memcredp
->len
);
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
));
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
));
824 /* Create and store the password hashes. */
825 E_md4hash(pass
, memcredp
->nt_hash
);
826 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
);
838 /***********************************************************
839 Destroy existing creds.
840 ***********************************************************/
842 static NTSTATUS
delete_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
)
844 #if !defined(HAVE_MUNLOCK)
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
;
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
,
869 NTSTATUS status
= delete_memory_creds(memcredp
);
870 if (!NT_STATUS_IS_OK(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
,
884 /* Shortcut to ensure we don't store if no mlock. */
885 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
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
;
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
));
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_P(NULL
, struct WINBINDD_MEMORY_CREDS
);
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
);
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
));
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
,
953 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
956 status
= winbindd_add_memory_creds_internal(username
, uid
, pass
);
957 if (!NT_STATUS_IS_OK(status
)) {
962 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
963 memcredp
= find_memory_creds_by_name(username
);
965 entry
->cred_ptr
= memcredp
;
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
);
986 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
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",
1007 DEBUG(10,("winbindd_delete_memory_creds: "
1008 "entry for user %s ref_count now %d\n",
1009 username
, memcredp
->ref_count
));
1013 /* Ensure we have no dangling references to this. */
1014 entry
->cred_ptr
= NULL
;
1020 /***********************************************************
1021 Replace the required creds with new ones (password change).
1022 ***********************************************************/
1024 NTSTATUS
winbindd_replace_memory_creds(const char *username
,
1027 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
1029 memcredp
= find_memory_creds_by_name(username
);
1031 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
1033 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1036 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
1039 return winbindd_replace_memory_creds_internal(memcredp
, pass
);