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/>.
26 #include "../libcli/auth/libcli_auth.h"
28 #include "libads/kerberos_proto.h"
29 #include "libads/krb5_errs.h"
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
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
*,
46 static void add_krb5_ticket_gain_handler_event(struct WINBINDD_CCACHE_ENTRY
*,
49 /* The Krb5 ticket refresh handler should be scheduled
50 at one-half of the period from now till the tkt
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
)) {
75 /****************************************************************
77 ****************************************************************/
79 static int ccache_entry_count(void)
81 struct WINBINDD_CCACHE_ENTRY
*entry
;
84 for (entry
= ccache_list
; entry
; entry
= entry
->next
) {
90 void ccache_remove_all_after_fork(void)
92 struct WINBINDD_CCACHE_ENTRY
*cur
, *next
;
94 for (cur
= ccache_list
; cur
; cur
= next
) {
96 DLIST_REMOVE(ccache_list
, cur
);
97 TALLOC_FREE(cur
->event
);
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
,
113 struct WINBINDD_CCACHE_ENTRY
*entry
=
114 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
118 time_t expire_time
= 0;
119 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
122 DBG_DEBUG("event called for: %s, %s\n",
123 entry
->ccname
, entry
->username
);
125 TALLOC_FREE(entry
->event
);
129 /* Kinit again if we have the user password and we can't renew the old
132 * This happens when machine are put to sleep for a very long time. */
134 if (entry
->renew_until
< time(NULL
)) {
136 if (cred_ptr
&& cred_ptr
->pass
) {
138 set_effective_uid(entry
->uid
);
140 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
142 0, /* hm, can we do time correction here ? */
143 &entry
->refresh_time
,
146 False
, /* no PAC required anymore */
148 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
150 gain_root_privilege();
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
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;
171 new_start
= time(NULL
) +
172 MAX(30, lp_winbind_cache_time());
174 add_krb5_ticket_gain_handler_event(entry
,
175 timeval_set(new_start
, 0));
178 TALLOC_FREE(entry
->event
);
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;
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
);
197 * No cached credentials
198 * destroy ticket and refresh chain
200 ads_kdestroy(entry
->ccname
);
201 TALLOC_FREE(entry
->event
);
206 set_effective_uid(entry
->uid
);
208 ret
= smb_krb5_renew_ticket(entry
->ccname
,
209 entry
->principal_name
,
212 #if defined(DEBUG_KRB5_TKT_RENEWAL)
213 new_start
= time(NULL
) + 30;
215 expire_time
= new_start
;
216 new_start
= krb5_event_refresh_time(new_start
);
219 gain_root_privilege();
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
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;
245 new_start
= time(NULL
) +
246 MAX(30, lp_winbind_cache_time());
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));
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.
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
;
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 */
280 add_krb5_ticket_gain_handler_event(entry
,
281 timeval_set(expire_time
, 0));
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
,
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
,
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 DBG_DEBUG("event called for: %s, %s\n",
315 entry
->ccname
, entry
->username
);
317 TALLOC_FREE(entry
->event
);
321 if (!cred_ptr
|| !cred_ptr
->pass
) {
322 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
326 if ((domain
= find_domain_from_name(entry
->realm
)) == NULL
) {
327 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
331 if (!domain
->online
) {
335 set_effective_uid(entry
->uid
);
337 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
339 0, /* hm, can we do time correction here ? */
340 &entry
->refresh_time
,
343 False
, /* no PAC required anymore */
345 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
347 gain_root_privilege();
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
);
359 DEBUG(10,("krb5_ticket_gain_handler: "
360 "successful kinit for: %s in ccache: %s\n",
361 entry
->principal_name
, entry
->ccname
));
367 #if defined(DEBUG_KRB5_TKT_RENEWAL)
368 t
= timeval_set(time(NULL
) + 30, 0);
370 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
373 add_krb5_ticket_gain_handler_event(entry
, t
);
378 #if defined(DEBUG_KRB5_TKT_RENEWAL)
379 t
= timeval_set(time(NULL
) + 30, 0);
381 t
= timeval_set(krb5_event_refresh_time(entry
->refresh_time
), 0);
384 if (entry
->refresh_time
== 0) {
385 entry
->refresh_time
= t
.tv_sec
;
387 entry
->event
= tevent_add_timer(global_event_context(),
390 krb5_ticket_refresh_handler
,
397 /**************************************************************
398 The gain initial ticket case is recognised as entry->refresh_time
400 **************************************************************/
402 static void add_krb5_ticket_gain_handler_event(struct WINBINDD_CCACHE_ENTRY
*entry
,
405 entry
->refresh_time
= 0;
406 entry
->event
= tevent_add_timer(global_event_context(),
409 krb5_ticket_gain_handler
,
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(),
429 krb5_ticket_gain_handler
,
432 new_event
= tevent_add_timer(global_event_context(),
435 krb5_ticket_refresh_handler
,
443 TALLOC_FREE(cur
->event
);
444 cur
->event
= new_event
;
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
,
468 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
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
));
479 if (!strcsequal(entry
->ccname
, ccname
)) {
480 DEBUG(0,("cache_entry_identical: "
481 "ccnames differ: (cache) %s != (client) %s\n",
482 entry
->ccname
, ccname
));
488 NTSTATUS
add_ccache_to_list(const char *princ_name
,
491 const char *username
,
498 bool postponed_request
)
500 struct WINBINDD_CCACHE_ENTRY
*entry
= NULL
;
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
);
518 /* Check cached entries are identical. */
519 if (!ccache_entry_identical(username
, uid
, ccname
)) {
520 return NT_STATUS_INVALID_PARAMETER
;
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) {
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
);
539 /* Renew at 1/2 the ticket expiration time */
540 #if defined(DEBUG_KRB5_TKT_RENEWAL)
541 t
= timeval_set(time(NULL
)+30, 0);
543 t
= timeval_set(krb5_event_refresh_time(ticket_end
),
546 if (!entry
->refresh_time
) {
547 entry
->refresh_time
= t
.tv_sec
;
549 entry
->event
= tevent_add_timer(global_event_context(),
552 krb5_ticket_refresh_handler
,
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
,
562 DEBUG(0, ("add_ccache_to_list: error is %s\n",
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>
582 ntret
= winbindd_add_memory_creds(username
, uid
, pass
);
583 DEBUG(10, ("winbindd_add_memory_creds returned: %s\n",
589 entry
= talloc(NULL
, struct WINBINDD_CCACHE_ENTRY
);
591 return NT_STATUS_NO_MEMORY
;
597 entry
->username
= talloc_strdup(entry
, username
);
598 if (!entry
->username
) {
603 entry
->principal_name
= talloc_strdup(entry
, princ_name
);
604 if (!entry
->principal_name
) {
609 entry
->service
= talloc_strdup(entry
, service
);
610 if (!entry
->service
) {
615 entry
->ccname
= talloc_strdup(entry
, ccname
);
616 if (!entry
->ccname
) {
620 entry
->realm
= talloc_strdup(entry
, realm
);
625 entry
->create_time
= create_time
;
626 entry
->renew_until
= renew_until
;
628 entry
->ref_count
= 1;
630 if (!lp_winbind_refresh_tickets() || renew_until
<= 0) {
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
);
638 /* Renew at 1/2 the ticket expiration time */
639 #if defined(DEBUG_KRB5_TKT_RENEWAL)
640 t
= timeval_set(time(NULL
)+30, 0);
642 t
= timeval_set(krb5_event_refresh_time(ticket_end
), 0);
644 if (entry
->refresh_time
== 0) {
645 entry
->refresh_time
= t
.tv_sec
;
647 entry
->event
= tevent_add_timer(global_event_context(),
650 krb5_ticket_refresh_handler
,
658 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
662 DLIST_ADD(ccache_list
, entry
);
664 DEBUG(10,("add_ccache_to_list: "
665 "added ccache [%s] for user [%s] to the list\n",
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
677 ntret
= winbindd_add_memory_creds(username
, uid
, pass
);
678 DEBUG(10, ("winbindd_add_memory_creds returned: %s\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
));
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
);
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(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
);