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"
30 #define DBGC_CLASS DBGC_WINBIND
32 /* uncomment this to do fast debugging on the krb5 ticket renewal event */
33 #ifdef DEBUG_KRB5_TKT_RENEWAL
34 #undef DEBUG_KRB5_TKT_RENEWAL
37 #define MAX_CCACHES 100
39 static struct WINBINDD_CCACHE_ENTRY
*ccache_list
;
40 static void krb5_ticket_gain_handler(struct event_context
*,
44 static void add_krb5_ticket_gain_handler_event(struct WINBINDD_CCACHE_ENTRY
*,
47 /* The Krb5 ticket refresh handler should be scheduled
48 at one-half of the period from now till the tkt
50 #define KRB5_EVENT_REFRESH_TIME(x) ((x) - (((x) - time(NULL))/2))
52 /****************************************************************
53 Find an entry by name.
54 ****************************************************************/
56 static struct WINBINDD_CCACHE_ENTRY
*get_ccache_by_username(const char *username
)
58 struct WINBINDD_CCACHE_ENTRY
*entry
;
60 for (entry
= ccache_list
; entry
; entry
= entry
->next
) {
61 if (strequal(entry
->username
, username
)) {
68 /****************************************************************
70 ****************************************************************/
72 static int ccache_entry_count(void)
74 struct WINBINDD_CCACHE_ENTRY
*entry
;
77 for (entry
= ccache_list
; entry
; entry
= entry
->next
) {
83 void ccache_remove_all_after_fork(void)
85 struct WINBINDD_CCACHE_ENTRY
*cur
, *next
;
87 for (cur
= ccache_list
; cur
; cur
= next
) {
89 DLIST_REMOVE(ccache_list
, cur
);
90 TALLOC_FREE(cur
->event
);
97 /****************************************************************
98 Do the work of refreshing the ticket.
99 ****************************************************************/
101 static void krb5_ticket_refresh_handler(struct event_context
*event_ctx
,
102 struct timed_event
*te
,
106 struct WINBINDD_CCACHE_ENTRY
*entry
=
107 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
111 time_t expire_time
= 0;
112 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
115 DEBUG(10,("krb5_ticket_refresh_handler called\n"));
116 DEBUGADD(10,("event called for: %s, %s\n",
117 entry
->ccname
, entry
->username
));
119 TALLOC_FREE(entry
->event
);
123 /* Kinit again if we have the user password and we can't renew the old
126 * This happens when machine are put to sleep for a very long time. */
128 if (entry
->renew_until
< time(NULL
)) {
130 if (cred_ptr
&& cred_ptr
->pass
) {
132 set_effective_uid(entry
->uid
);
134 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
136 0, /* hm, can we do time correction here ? */
137 &entry
->refresh_time
,
140 False
, /* no PAC required anymore */
142 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
144 gain_root_privilege();
147 DEBUG(3,("krb5_ticket_refresh_handler: "
148 "could not re-kinit: %s\n",
149 error_message(ret
)));
150 /* destroy the ticket because we cannot rekinit
151 * it, ignore error here */
152 ads_kdestroy(entry
->ccname
);
154 /* Don't break the ticket refresh chain: retry
155 * refreshing ticket sometime later when KDC is
156 * unreachable -- BoYang. More error code handling
160 if ((ret
== KRB5_KDC_UNREACH
)
161 || (ret
== KRB5_REALM_CANT_RESOLVE
)) {
162 #if defined(DEBUG_KRB5_TKT_RENEWAL)
163 new_start
= time(NULL
) + 30;
165 new_start
= time(NULL
) +
166 MAX(30, lp_winbind_cache_time());
168 add_krb5_ticket_gain_handler_event(entry
,
169 timeval_set(new_start
, 0));
172 TALLOC_FREE(entry
->event
);
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;
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
);
191 * No cached credentials
192 * destroy ticket and refresh chain
194 ads_kdestroy(entry
->ccname
);
195 TALLOC_FREE(entry
->event
);
200 set_effective_uid(entry
->uid
);
202 ret
= smb_krb5_renew_ticket(entry
->ccname
,
203 entry
->principal_name
,
206 #if defined(DEBUG_KRB5_TKT_RENEWAL)
207 new_start
= time(NULL
) + 30;
209 expire_time
= new_start
;
210 new_start
= KRB5_EVENT_REFRESH_TIME(new_start
);
213 gain_root_privilege();
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
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;
239 new_start
= time(NULL
) +
240 MAX(30, lp_winbind_cache_time());
242 /* ticket is destroyed here, we have to regain it
243 * if it is possible */
244 add_krb5_ticket_gain_handler_event(entry
,
245 timeval_set(new_start
, 0));
249 /* This is evil, if the ticket was already expired.
250 * renew ticket function returns KRB5KRB_AP_ERR_TKT_EXPIRED.
251 * But there is still a chance that we can rekinit it.
253 * This happens when user login in online mode, and then network
254 * down or something cause winbind goes offline for a very long time,
255 * and then goes online again. ticket expired, renew failed.
256 * This happens when machine are put to sleep for a long time,
257 * but shorter than entry->renew_util.
259 * Looks like the KDC is reachable, we want to rekinit as soon as
260 * possible instead of waiting some time later. */
261 if ((ret
== KRB5KRB_AP_ERR_TKT_EXPIRED
)
262 || (ret
== KRB5_FCC_NOFILE
)) goto rekinit
;
268 /* in cases that ticket will be unrenewable soon, we don't try to renew ticket
269 * but try to regain ticket if it is possible */
270 if (entry
->renew_until
&& expire_time
271 && (entry
->renew_until
<= expire_time
)) {
272 /* try to regain ticket 10 seconds beforre expiration */
274 add_krb5_ticket_gain_handler_event(entry
,
275 timeval_set(expire_time
, 0));
279 if (entry
->refresh_time
== 0) {
280 entry
->refresh_time
= new_start
;
282 entry
->event
= event_add_timed(winbind_event_context(), entry
,
283 timeval_set(new_start
, 0),
284 krb5_ticket_refresh_handler
,
290 /****************************************************************
291 Do the work of regaining a ticket when coming from offline auth.
292 ****************************************************************/
294 static void krb5_ticket_gain_handler(struct event_context
*event_ctx
,
295 struct timed_event
*te
,
299 struct WINBINDD_CCACHE_ENTRY
*entry
=
300 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
304 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
305 struct winbindd_domain
*domain
= NULL
;
308 DEBUG(10,("krb5_ticket_gain_handler called\n"));
309 DEBUGADD(10,("event called for: %s, %s\n",
310 entry
->ccname
, entry
->username
));
312 TALLOC_FREE(entry
->event
);
316 if (!cred_ptr
|| !cred_ptr
->pass
) {
317 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
321 if ((domain
= find_domain_from_name(entry
->realm
)) == NULL
) {
322 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
326 if (!domain
->online
) {
330 set_effective_uid(entry
->uid
);
332 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
334 0, /* hm, can we do time correction here ? */
335 &entry
->refresh_time
,
338 False
, /* no PAC required anymore */
340 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
342 gain_root_privilege();
345 DEBUG(3,("krb5_ticket_gain_handler: "
346 "could not kinit: %s\n",
347 error_message(ret
)));
348 /* evil. If we cannot do it, destroy any the __maybe__
349 * __existing__ ticket */
350 ads_kdestroy(entry
->ccname
);
354 DEBUG(10,("krb5_ticket_gain_handler: "
355 "successful kinit for: %s in ccache: %s\n",
356 entry
->principal_name
, entry
->ccname
));
362 #if defined(DEBUG_KRB5_TKT_REGAIN)
363 t
= timeval_set(time(NULL
) + 30, 0);
365 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
368 add_krb5_ticket_gain_handler_event(entry
, t
);
373 #if defined(DEBUG_KRB5_TKT_RENEWAL)
374 t
= timeval_set(time(NULL
) + 30, 0);
376 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(entry
->refresh_time
), 0);
379 if (entry
->refresh_time
== 0) {
380 entry
->refresh_time
= t
.tv_sec
;
382 entry
->event
= event_add_timed(winbind_event_context(),
385 krb5_ticket_refresh_handler
,
392 /**************************************************************
393 The gain initial ticket case is recognised as entry->refresh_time
395 **************************************************************/
397 static void add_krb5_ticket_gain_handler_event(struct WINBINDD_CCACHE_ENTRY
*entry
,
400 entry
->refresh_time
= 0;
401 entry
->event
= event_add_timed(winbind_event_context(),
404 krb5_ticket_gain_handler
,
408 void ccache_regain_all_now(void)
410 struct WINBINDD_CCACHE_ENTRY
*cur
;
411 struct timeval t
= timeval_current();
413 for (cur
= ccache_list
; cur
; cur
= cur
->next
) {
414 struct timed_event
*new_event
;
417 * if refresh_time is 0, we know that the
418 * the event has the krb5_ticket_gain_handler
420 if (cur
->refresh_time
== 0) {
421 new_event
= event_add_timed(winbind_event_context(),
424 krb5_ticket_gain_handler
,
427 new_event
= event_add_timed(winbind_event_context(),
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
,
533 /* Reference count old entries */
534 entry
= get_ccache_by_username(username
);
536 /* Check cached entries are identical. */
537 if (!ccache_entry_identical(username
, uid
, ccname
)) {
538 return NT_STATUS_INVALID_PARAMETER
;
541 DEBUG(10,("add_ccache_to_list: "
542 "ref count on entry %s is now %d\n",
543 username
, entry
->ref_count
));
544 /* FIXME: in this case we still might want to have a krb5 cred
545 * event handler created - gd
546 * Add ticket refresh handler here */
548 if (!lp_winbind_refresh_tickets() || renew_until
<= 0) {
553 if (postponed_request
) {
554 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
555 add_krb5_ticket_gain_handler_event(entry
, t
);
557 /* Renew at 1/2 the ticket expiration time */
558 #if defined(DEBUG_KRB5_TKT_RENEWAL)
559 t
= timeval_set(time(NULL
)+30, 0);
561 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end
), 0);
563 if (!entry
->refresh_time
) {
564 entry
->refresh_time
= t
.tv_sec
;
566 entry
->event
= event_add_timed(winbind_event_context(),
569 krb5_ticket_refresh_handler
,
574 ntret
= remove_ccache(username
);
575 if (!NT_STATUS_IS_OK(ntret
)) {
576 DEBUG(0, ("add_ccache_to_list: Failed to remove krb5 "
577 "ccache %s for user %s\n", entry
->ccname
,
579 DEBUG(0, ("add_ccache_to_list: error is %s\n",
583 return NT_STATUS_NO_MEMORY
;
586 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
592 entry
= TALLOC_P(NULL
, struct WINBINDD_CCACHE_ENTRY
);
594 return NT_STATUS_NO_MEMORY
;
600 entry
->username
= talloc_strdup(entry
, username
);
601 if (!entry
->username
) {
606 entry
->principal_name
= talloc_strdup(entry
, princ_name
);
607 if (!entry
->principal_name
) {
612 entry
->service
= talloc_strdup(entry
, service
);
613 if (!entry
->service
) {
618 entry
->ccname
= talloc_strdup(entry
, ccname
);
619 if (!entry
->ccname
) {
623 entry
->realm
= talloc_strdup(entry
, realm
);
628 entry
->create_time
= create_time
;
629 entry
->renew_until
= renew_until
;
631 entry
->ref_count
= 1;
633 if (!lp_winbind_refresh_tickets() || renew_until
<= 0) {
637 if (postponed_request
) {
638 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
639 add_krb5_ticket_gain_handler_event(entry
, t
);
641 /* Renew at 1/2 the ticket expiration time */
642 #if defined(DEBUG_KRB5_TKT_RENEWAL)
643 t
= timeval_set(time(NULL
)+30, 0);
645 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end
), 0);
647 if (entry
->refresh_time
== 0) {
648 entry
->refresh_time
= t
.tv_sec
;
650 entry
->event
= event_add_timed(winbind_event_context(),
653 krb5_ticket_refresh_handler
,
661 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
665 DLIST_ADD(ccache_list
, entry
);
667 DEBUG(10,("add_ccache_to_list: "
668 "added ccache [%s] for user [%s] to the list\n",
676 return NT_STATUS_NO_MEMORY
;
679 /*******************************************************************
680 Remove a WINBINDD_CCACHE_ENTRY entry and the krb5 ccache if no longer
682 *******************************************************************/
684 NTSTATUS
remove_ccache(const char *username
)
686 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
687 NTSTATUS status
= NT_STATUS_OK
;
693 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
696 if (entry
->ref_count
<= 0) {
697 DEBUG(0,("remove_ccache: logic error. "
698 "ref count for user %s = %d\n",
699 username
, entry
->ref_count
));
700 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
705 if (entry
->ref_count
> 0) {
706 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
707 username
, entry
->ref_count
));
711 /* no references any more */
713 DLIST_REMOVE(ccache_list
, entry
);
714 TALLOC_FREE(entry
->event
); /* unregisters events */
717 ret
= ads_kdestroy(entry
->ccname
);
719 /* we ignore the error when there has been no credential cache */
720 if (ret
== KRB5_FCC_NOFILE
) {
723 DEBUG(0,("remove_ccache: "
724 "failed to destroy user krb5 ccache %s with: %s\n",
725 entry
->ccname
, error_message(ret
)));
727 DEBUG(10,("remove_ccache: "
728 "successfully destroyed krb5 ccache %s for user %s\n",
729 entry
->ccname
, username
));
731 status
= krb5_to_nt_status(ret
);
735 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username
));
740 /*******************************************************************
741 In memory credentials cache code.
742 *******************************************************************/
744 static struct WINBINDD_MEMORY_CREDS
*memory_creds_list
;
746 /***********************************************************
747 Find an entry on the list by name.
748 ***********************************************************/
750 struct WINBINDD_MEMORY_CREDS
*find_memory_creds_by_name(const char *username
)
752 struct WINBINDD_MEMORY_CREDS
*p
;
754 for (p
= memory_creds_list
; p
; p
= p
->next
) {
755 if (strequal(p
->username
, username
)) {
762 /***********************************************************
763 Store the required creds and mlock them.
764 ***********************************************************/
766 static NTSTATUS
store_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
,
769 #if !defined(HAVE_MLOCK)
772 /* new_entry->nt_hash is the base pointer for the block
773 of memory pointed into by new_entry->lm_hash and
774 new_entry->pass (if we're storing plaintext). */
776 memcredp
->len
= NT_HASH_LEN
+ LM_HASH_LEN
;
778 memcredp
->len
+= strlen(pass
)+1;
783 /* aligning the memory on on x86_64 and compiling
784 with gcc 4.1 using -O2 causes a segv in the
785 next memset() --jerry */
786 memcredp
->nt_hash
= SMB_MALLOC_ARRAY(unsigned char, memcredp
->len
);
788 /* On non-linux platforms, mlock()'d memory must be aligned */
789 memcredp
->nt_hash
= SMB_MEMALIGN_ARRAY(unsigned char,
790 getpagesize(), memcredp
->len
);
792 if (!memcredp
->nt_hash
) {
793 return NT_STATUS_NO_MEMORY
;
795 memset(memcredp
->nt_hash
, 0x0, memcredp
->len
);
797 memcredp
->lm_hash
= memcredp
->nt_hash
+ NT_HASH_LEN
;
799 #ifdef DEBUG_PASSWORD
800 DEBUG(10,("mlocking memory: %p\n", memcredp
->nt_hash
));
802 if ((mlock(memcredp
->nt_hash
, memcredp
->len
)) == -1) {
803 DEBUG(0,("failed to mlock memory: %s (%d)\n",
804 strerror(errno
), errno
));
805 SAFE_FREE(memcredp
->nt_hash
);
806 return map_nt_error_from_unix(errno
);
809 #ifdef DEBUG_PASSWORD
810 DEBUG(10,("mlocked memory: %p\n", memcredp
->nt_hash
));
813 /* Create and store the password hashes. */
814 E_md4hash(pass
, memcredp
->nt_hash
);
815 E_deshash(pass
, memcredp
->lm_hash
);
818 memcredp
->pass
= (char *)memcredp
->lm_hash
+ LM_HASH_LEN
;
819 memcpy(memcredp
->pass
, pass
,
820 memcredp
->len
- NT_HASH_LEN
- LM_HASH_LEN
);
827 /***********************************************************
828 Destroy existing creds.
829 ***********************************************************/
831 static NTSTATUS
delete_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
)
833 #if !defined(HAVE_MUNLOCK)
836 if (munlock(memcredp
->nt_hash
, memcredp
->len
) == -1) {
837 DEBUG(0,("failed to munlock memory: %s (%d)\n",
838 strerror(errno
), errno
));
839 return map_nt_error_from_unix(errno
);
841 memset(memcredp
->nt_hash
, '\0', memcredp
->len
);
842 SAFE_FREE(memcredp
->nt_hash
);
843 memcredp
->nt_hash
= NULL
;
844 memcredp
->lm_hash
= NULL
;
845 memcredp
->pass
= NULL
;
851 /***********************************************************
852 Replace the required creds with new ones (password change).
853 ***********************************************************/
855 static NTSTATUS
winbindd_replace_memory_creds_internal(struct WINBINDD_MEMORY_CREDS
*memcredp
,
858 NTSTATUS status
= delete_memory_creds(memcredp
);
859 if (!NT_STATUS_IS_OK(status
)) {
862 return store_memory_creds(memcredp
, pass
);
865 /*************************************************************
866 Store credentials in memory in a list.
867 *************************************************************/
869 static NTSTATUS
winbindd_add_memory_creds_internal(const char *username
,
873 /* Shortcut to ensure we don't store if no mlock. */
874 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
878 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
880 memcredp
= find_memory_creds_by_name(username
);
881 if (uid
== (uid_t
)-1) {
882 DEBUG(0,("winbindd_add_memory_creds_internal: "
883 "invalid uid for user %s.\n", username
));
884 return NT_STATUS_INVALID_PARAMETER
;
888 /* Already exists. Increment the reference count and replace stored creds. */
889 if (uid
!= memcredp
->uid
) {
890 DEBUG(0,("winbindd_add_memory_creds_internal: "
891 "uid %u for user %s doesn't "
892 "match stored uid %u. Replacing.\n",
893 (unsigned int)uid
, username
,
894 (unsigned int)memcredp
->uid
));
897 memcredp
->ref_count
++;
898 DEBUG(10,("winbindd_add_memory_creds_internal: "
899 "ref count for user %s is now %d\n",
900 username
, memcredp
->ref_count
));
901 return winbindd_replace_memory_creds_internal(memcredp
, pass
);
904 memcredp
= TALLOC_ZERO_P(NULL
, struct WINBINDD_MEMORY_CREDS
);
906 return NT_STATUS_NO_MEMORY
;
908 memcredp
->username
= talloc_strdup(memcredp
, username
);
909 if (!memcredp
->username
) {
910 talloc_destroy(memcredp
);
911 return NT_STATUS_NO_MEMORY
;
914 status
= store_memory_creds(memcredp
, pass
);
915 if (!NT_STATUS_IS_OK(status
)) {
916 talloc_destroy(memcredp
);
921 memcredp
->ref_count
= 1;
922 DLIST_ADD(memory_creds_list
, memcredp
);
924 DEBUG(10,("winbindd_add_memory_creds_internal: "
925 "added entry for user %s\n", username
));
931 /*************************************************************
932 Store users credentials in memory. If we also have a
933 struct WINBINDD_CCACHE_ENTRY for this username with a
934 refresh timer, then store the plaintext of the password
935 and associate the new credentials with the struct WINBINDD_CCACHE_ENTRY.
936 *************************************************************/
938 NTSTATUS
winbindd_add_memory_creds(const char *username
,
942 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
945 status
= winbindd_add_memory_creds_internal(username
, uid
, pass
);
946 if (!NT_STATUS_IS_OK(status
)) {
951 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
952 memcredp
= find_memory_creds_by_name(username
);
954 entry
->cred_ptr
= memcredp
;
961 /*************************************************************
962 Decrement the in-memory ref count - delete if zero.
963 *************************************************************/
965 NTSTATUS
winbindd_delete_memory_creds(const char *username
)
967 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
968 struct WINBINDD_CCACHE_ENTRY
*entry
= NULL
;
969 NTSTATUS status
= NT_STATUS_OK
;
971 memcredp
= find_memory_creds_by_name(username
);
972 entry
= get_ccache_by_username(username
);
975 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
977 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
980 if (memcredp
->ref_count
<= 0) {
981 DEBUG(0,("winbindd_delete_memory_creds: logic error. "
982 "ref count for user %s = %d\n",
983 username
, memcredp
->ref_count
));
984 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
987 memcredp
->ref_count
--;
988 if (memcredp
->ref_count
<= 0) {
989 delete_memory_creds(memcredp
);
990 DLIST_REMOVE(memory_creds_list
, memcredp
);
991 talloc_destroy(memcredp
);
992 DEBUG(10,("winbindd_delete_memory_creds: "
993 "deleted entry for user %s\n",
996 DEBUG(10,("winbindd_delete_memory_creds: "
997 "entry for user %s ref_count now %d\n",
998 username
, memcredp
->ref_count
));
1002 /* Ensure we have no dangling references to this. */
1003 entry
->cred_ptr
= NULL
;
1009 /***********************************************************
1010 Replace the required creds with new ones (password change).
1011 ***********************************************************/
1013 NTSTATUS
winbindd_replace_memory_creds(const char *username
,
1016 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
1018 memcredp
= find_memory_creds_by_name(username
);
1020 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
1022 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1025 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
1028 return winbindd_replace_memory_creds_internal(memcredp
, pass
);