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
*,
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
,
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. More error code handling
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;
160 new_start
= time(NULL
) +
161 MAX(30, lp_winbind_cache_time());
163 entry
->refresh_time
= 0;
164 /* try to regain ticket here */
165 entry
->event
= event_add_timed(winbind_event_context(),
167 timeval_set(new_start
, 0),
168 krb5_ticket_gain_handler
,
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 entry
->refresh_time
= 0;
245 entry
->event
= event_add_timed(winbind_event_context(),
247 timeval_set(new_start
, 0),
248 krb5_ticket_gain_handler
,
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.
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
;
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 */
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
,
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
,
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
,
306 struct WINBINDD_CCACHE_ENTRY
*entry
=
307 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
311 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
312 struct winbindd_domain
*domain
= NULL
;
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
);
323 if (!cred_ptr
|| !cred_ptr
->pass
) {
324 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
328 if ((domain
= find_domain_from_name(entry
->realm
)) == NULL
) {
329 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
333 if (!domain
->online
) {
337 set_effective_uid(entry
->uid
);
339 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
341 0, /* hm, can we do time correction here ? */
342 &entry
->refresh_time
,
345 False
, /* no PAC required anymore */
347 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
349 gain_root_privilege();
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
);
361 DEBUG(10,("krb5_ticket_gain_handler: "
362 "successful kinit for: %s in ccache: %s\n",
363 entry
->principal_name
, entry
->ccname
));
369 #if defined(DEBUG_KRB5_TKT_REGAIN)
370 t
= timeval_set(time(NULL
) + 30, 0);
372 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
375 entry
->refresh_time
= 0;
376 entry
->event
= event_add_timed(winbind_event_context(),
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
,
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(),
421 krb5_ticket_gain_handler
,
424 new_event
= event_add_timed(winbind_event_context(),
427 krb5_ticket_refresh_handler
,
435 TALLOC_FREE(cur
->event
);
436 cur
->event
= new_event
;
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
,
460 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
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
));
471 if (!strcsequal(entry
->ccname
, ccname
)) {
472 DEBUG(0,("cache_entry_identical: "
473 "ccnames differ: (cache) %s != (client) %s\n",
474 entry
->ccname
, ccname
));
480 NTSTATUS
add_ccache_to_list(const char *princ_name
,
483 const char *username
,
489 bool postponed_request
)
491 struct WINBINDD_CCACHE_ENTRY
*entry
= NULL
;
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. */
512 if (postponed_request
) {
513 /* ignore KRB5_FCC_NOFILE error here */
514 ret
= ads_kdestroy(ccname
);
515 if (ret
== KRB5_FCC_NOFILE
) {
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
);
524 DEBUG(10, ("add_ccache_to_list: successfully destroyed "
525 "krb5 ccache %s for user %s\n", ccname
,
531 /* Reference count old entries */
532 entry
= get_ccache_by_username(username
);
534 /* Check cached entries are identical. */
535 if (!ccache_entry_identical(username
, uid
, ccname
)) {
536 return NT_STATUS_INVALID_PARAMETER
;
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) {
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(),
557 krb5_ticket_gain_handler
,
560 /* Renew at 1/2 the ticket expiration time */
561 #if defined(DEBUG_KRB5_TKT_RENEWAL)
562 t
= timeval_set(time(NULL
)+30, 0);
564 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end
), 0);
566 if (!entry
->refresh_time
) {
567 entry
->refresh_time
= t
.tv_sec
;
569 entry
->event
= event_add_timed(winbind_event_context(),
572 krb5_ticket_refresh_handler
,
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
,
582 DEBUG(0, ("add_ccache_to_list: error is %s\n",
586 return NT_STATUS_NO_MEMORY
;
589 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
595 entry
= TALLOC_P(NULL
, struct WINBINDD_CCACHE_ENTRY
);
597 return NT_STATUS_NO_MEMORY
;
603 entry
->username
= talloc_strdup(entry
, username
);
604 if (!entry
->username
) {
609 entry
->principal_name
= talloc_strdup(entry
, princ_name
);
610 if (!entry
->principal_name
) {
615 entry
->service
= talloc_strdup(entry
, service
);
616 if (!entry
->service
) {
621 entry
->ccname
= talloc_strdup(entry
, ccname
);
622 if (!entry
->ccname
) {
626 entry
->realm
= talloc_strdup(entry
, realm
);
631 entry
->create_time
= create_time
;
632 entry
->renew_until
= renew_until
;
634 entry
->ref_count
= 1;
636 if (!lp_winbind_refresh_tickets() || renew_until
<= 0) {
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(),
646 krb5_ticket_gain_handler
,
649 /* Renew at 1/2 the ticket expiration time */
650 #if defined(DEBUG_KRB5_TKT_RENEWAL)
651 t
= timeval_set(time(NULL
)+30, 0);
653 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end
), 0);
655 if (entry
->refresh_time
== 0) {
656 entry
->refresh_time
= t
.tv_sec
;
658 entry
->event
= event_add_timed(winbind_event_context(),
661 krb5_ticket_refresh_handler
,
669 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
673 DLIST_ADD(ccache_list
, entry
);
675 DEBUG(10,("add_ccache_to_list: "
676 "added ccache [%s] for user [%s] to the list\n",
684 return NT_STATUS_NO_MEMORY
;
687 /*******************************************************************
688 Remove a WINBINDD_CCACHE_ENTRY entry and the krb5 ccache if no longer
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
;
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
;
713 if (entry
->ref_count
> 0) {
714 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
715 username
, entry
->ref_count
));
719 /* no references any more */
721 DLIST_REMOVE(ccache_list
, entry
);
722 TALLOC_FREE(entry
->event
); /* unregisters events */
725 ret
= ads_kdestroy(entry
->ccname
);
727 /* we ignore the error when there has been no credential cache */
728 if (ret
== KRB5_FCC_NOFILE
) {
731 DEBUG(0,("remove_ccache: "
732 "failed to destroy user krb5 ccache %s with: %s\n",
733 entry
->ccname
, error_message(ret
)));
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
);
743 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username
));
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
)) {
770 /***********************************************************
771 Store the required creds and mlock them.
772 ***********************************************************/
774 static NTSTATUS
store_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
,
777 #if !defined(HAVE_MLOCK)
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
;
786 memcredp
->len
+= strlen(pass
)+1;
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
);
796 /* On non-linux platforms, mlock()'d memory must be aligned */
797 memcredp
->nt_hash
= SMB_MEMALIGN_ARRAY(unsigned char,
798 getpagesize(), memcredp
->len
);
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
));
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
));
821 /* Create and store the password hashes. */
822 E_md4hash(pass
, memcredp
->nt_hash
);
823 E_deshash(pass
, memcredp
->lm_hash
);
826 memcredp
->pass
= (char *)memcredp
->lm_hash
+ LM_HASH_LEN
;
827 memcpy(memcredp
->pass
, pass
,
828 memcredp
->len
- NT_HASH_LEN
- LM_HASH_LEN
);
835 /***********************************************************
836 Destroy existing creds.
837 ***********************************************************/
839 static NTSTATUS
delete_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
)
841 #if !defined(HAVE_MUNLOCK)
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
;
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
,
866 NTSTATUS status
= delete_memory_creds(memcredp
);
867 if (!NT_STATUS_IS_OK(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
,
881 /* Shortcut to ensure we don't store if no mlock. */
882 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
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
;
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
));
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
);
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
);
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
));
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
,
950 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
953 status
= winbindd_add_memory_creds_internal(username
, uid
, pass
);
954 if (!NT_STATUS_IS_OK(status
)) {
959 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
960 memcredp
= find_memory_creds_by_name(username
);
962 entry
->cred_ptr
= memcredp
;
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
);
983 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
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",
1004 DEBUG(10,("winbindd_delete_memory_creds: "
1005 "entry for user %s ref_count now %d\n",
1006 username
, memcredp
->ref_count
));
1010 /* Ensure we have no dangling references to this. */
1011 entry
->cred_ptr
= NULL
;
1017 /***********************************************************
1018 Replace the required creds with new ones (password change).
1019 ***********************************************************/
1021 NTSTATUS
winbindd_replace_memory_creds(const char *username
,
1024 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
1026 memcredp
= find_memory_creds_by_name(username
);
1028 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
1030 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1033 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
1036 return winbindd_replace_memory_creds_internal(memcredp
, pass
);