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"
31 #define DBGC_CLASS DBGC_WINBIND
33 /* uncomment this to do fast debugging on the krb5 ticket renewal event */
34 #ifdef DEBUG_KRB5_TKT_RENEWAL
35 #undef DEBUG_KRB5_TKT_RENEWAL
38 #define MAX_CCACHES 100
40 static struct WINBINDD_CCACHE_ENTRY
*ccache_list
;
41 static void krb5_ticket_gain_handler(struct tevent_context
*,
42 struct tevent_timer
*,
45 static void add_krb5_ticket_gain_handler_event(struct WINBINDD_CCACHE_ENTRY
*,
48 /* The Krb5 ticket refresh handler should be scheduled
49 at one-half of the period from now till the tkt
52 static time_t krb5_event_refresh_time(time_t end_time
)
54 time_t rest
= end_time
- time(NULL
);
55 return end_time
- rest
/2;
58 /****************************************************************
59 Find an entry by name.
60 ****************************************************************/
62 static struct WINBINDD_CCACHE_ENTRY
*get_ccache_by_username(const char *username
)
64 struct WINBINDD_CCACHE_ENTRY
*entry
;
66 for (entry
= ccache_list
; entry
; entry
= entry
->next
) {
67 if (strequal(entry
->username
, username
)) {
74 /****************************************************************
76 ****************************************************************/
78 static int ccache_entry_count(void)
80 struct WINBINDD_CCACHE_ENTRY
*entry
;
83 for (entry
= ccache_list
; entry
; entry
= entry
->next
) {
89 void ccache_remove_all_after_fork(void)
91 struct WINBINDD_CCACHE_ENTRY
*cur
, *next
;
93 for (cur
= ccache_list
; cur
; cur
= next
) {
95 DLIST_REMOVE(ccache_list
, cur
);
96 TALLOC_FREE(cur
->event
);
103 /****************************************************************
104 Do the work of refreshing the ticket.
105 ****************************************************************/
107 static void krb5_ticket_refresh_handler(struct tevent_context
*event_ctx
,
108 struct tevent_timer
*te
,
112 struct WINBINDD_CCACHE_ENTRY
*entry
=
113 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
117 time_t expire_time
= 0;
118 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
121 DBG_DEBUG("event called for: %s, %s\n",
122 entry
->ccname
, entry
->username
);
124 TALLOC_FREE(entry
->event
);
128 /* Kinit again if we have the user password and we can't renew the old
131 * This happens when machine are put to sleep for a very long time. */
133 if (entry
->renew_until
< time(NULL
)) {
135 if (cred_ptr
&& cred_ptr
->pass
) {
137 set_effective_uid(entry
->uid
);
139 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
141 0, /* hm, can we do time correction here ? */
142 &entry
->refresh_time
,
145 False
, /* no PAC required anymore */
147 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
149 gain_root_privilege();
152 DEBUG(3,("krb5_ticket_refresh_handler: "
153 "could not re-kinit: %s\n",
154 error_message(ret
)));
155 /* destroy the ticket because we cannot rekinit
156 * it, ignore error here */
157 ads_kdestroy(entry
->ccname
);
159 /* Don't break the ticket refresh chain: retry
160 * refreshing ticket sometime later when KDC is
161 * unreachable -- BoYang. More error code handling
165 if ((ret
== KRB5_KDC_UNREACH
)
166 || (ret
== KRB5_REALM_CANT_RESOLVE
)) {
167 #if defined(DEBUG_KRB5_TKT_RENEWAL)
168 new_start
= time(NULL
) + 30;
170 new_start
= time(NULL
) +
171 MAX(30, lp_winbind_cache_time());
173 add_krb5_ticket_gain_handler_event(entry
,
174 timeval_set(new_start
, 0));
177 TALLOC_FREE(entry
->event
);
181 DEBUG(10,("krb5_ticket_refresh_handler: successful re-kinit "
182 "for: %s in ccache: %s\n",
183 entry
->principal_name
, entry
->ccname
));
185 #if defined(DEBUG_KRB5_TKT_RENEWAL)
186 new_start
= time(NULL
) + 30;
188 /* The tkt should be refreshed at one-half the period
189 from now to the expiration time */
190 expire_time
= entry
->refresh_time
;
191 new_start
= krb5_event_refresh_time(entry
->refresh_time
);
196 * No cached credentials
197 * destroy ticket and refresh chain
199 ads_kdestroy(entry
->ccname
);
200 TALLOC_FREE(entry
->event
);
205 set_effective_uid(entry
->uid
);
207 ret
= smb_krb5_renew_ticket(entry
->ccname
,
208 entry
->principal_name
,
211 #if defined(DEBUG_KRB5_TKT_RENEWAL)
212 new_start
= time(NULL
) + 30;
214 expire_time
= new_start
;
215 new_start
= krb5_event_refresh_time(new_start
);
218 gain_root_privilege();
221 DEBUG(3,("krb5_ticket_refresh_handler: "
222 "could not renew tickets: %s\n",
223 error_message(ret
)));
224 /* maybe we are beyond the renewing window */
226 /* evil rises here, we refresh ticket failed,
227 * but the ticket might be expired. Therefore,
228 * When we refresh ticket failed, destory the
231 ads_kdestroy(entry
->ccname
);
233 /* avoid breaking the renewal chain: retry in
234 * lp_winbind_cache_time() seconds when the KDC was not
235 * available right now.
236 * the return code can be KRB5_REALM_CANT_RESOLVE.
237 * More error code handling here? */
239 if ((ret
== KRB5_KDC_UNREACH
)
240 || (ret
== KRB5_REALM_CANT_RESOLVE
)) {
241 #if defined(DEBUG_KRB5_TKT_RENEWAL)
242 new_start
= time(NULL
) + 30;
244 new_start
= time(NULL
) +
245 MAX(30, lp_winbind_cache_time());
247 /* ticket is destroyed here, we have to regain it
248 * if it is possible */
249 add_krb5_ticket_gain_handler_event(entry
,
250 timeval_set(new_start
, 0));
254 /* This is evil, if the ticket was already expired.
255 * renew ticket function returns KRB5KRB_AP_ERR_TKT_EXPIRED.
256 * But there is still a chance that we can rekinit it.
258 * This happens when user login in online mode, and then network
259 * down or something cause winbind goes offline for a very long time,
260 * and then goes online again. ticket expired, renew failed.
261 * This happens when machine are put to sleep for a long time,
262 * but shorter than entry->renew_util.
264 * Looks like the KDC is reachable, we want to rekinit as soon as
265 * possible instead of waiting some time later. */
266 if ((ret
== KRB5KRB_AP_ERR_TKT_EXPIRED
)
267 || (ret
== KRB5_FCC_NOFILE
)) goto rekinit
;
273 /* in cases that ticket will be unrenewable soon, we don't try to renew ticket
274 * but try to regain ticket if it is possible */
275 if (entry
->renew_until
&& expire_time
276 && (entry
->renew_until
<= expire_time
)) {
277 /* try to regain ticket 10 seconds before expiration */
279 add_krb5_ticket_gain_handler_event(entry
,
280 timeval_set(expire_time
, 0));
284 if (entry
->refresh_time
== 0) {
285 entry
->refresh_time
= new_start
;
287 entry
->event
= tevent_add_timer(server_event_context(), entry
,
288 timeval_set(new_start
, 0),
289 krb5_ticket_refresh_handler
,
295 /****************************************************************
296 Do the work of regaining a ticket when coming from offline auth.
297 ****************************************************************/
299 static void krb5_ticket_gain_handler(struct tevent_context
*event_ctx
,
300 struct tevent_timer
*te
,
304 struct WINBINDD_CCACHE_ENTRY
*entry
=
305 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
309 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
310 struct winbindd_domain
*domain
= NULL
;
313 DBG_DEBUG("event called for: %s, %s\n",
314 entry
->ccname
, entry
->username
);
316 TALLOC_FREE(entry
->event
);
320 if (!cred_ptr
|| !cred_ptr
->pass
) {
321 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
325 if ((domain
= find_domain_from_name(entry
->realm
)) == NULL
) {
326 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
330 if (!domain
->online
) {
334 set_effective_uid(entry
->uid
);
336 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
338 0, /* hm, can we do time correction here ? */
339 &entry
->refresh_time
,
342 False
, /* no PAC required anymore */
344 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
,
346 gain_root_privilege();
349 DEBUG(3,("krb5_ticket_gain_handler: "
350 "could not kinit: %s\n",
351 error_message(ret
)));
352 /* evil. If we cannot do it, destroy any the __maybe__
353 * __existing__ ticket */
354 ads_kdestroy(entry
->ccname
);
358 DEBUG(10,("krb5_ticket_gain_handler: "
359 "successful kinit for: %s in ccache: %s\n",
360 entry
->principal_name
, entry
->ccname
));
366 #if defined(DEBUG_KRB5_TKT_RENEWAL)
367 t
= timeval_set(time(NULL
) + 30, 0);
369 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
372 add_krb5_ticket_gain_handler_event(entry
, t
);
377 #if defined(DEBUG_KRB5_TKT_RENEWAL)
378 t
= timeval_set(time(NULL
) + 30, 0);
380 t
= timeval_set(krb5_event_refresh_time(entry
->refresh_time
), 0);
383 if (entry
->refresh_time
== 0) {
384 entry
->refresh_time
= t
.tv_sec
;
386 entry
->event
= tevent_add_timer(server_event_context(),
389 krb5_ticket_refresh_handler
,
396 /**************************************************************
397 The gain initial ticket case is recognised as entry->refresh_time
399 **************************************************************/
401 static void add_krb5_ticket_gain_handler_event(struct WINBINDD_CCACHE_ENTRY
*entry
,
404 entry
->refresh_time
= 0;
405 entry
->event
= tevent_add_timer(server_event_context(),
408 krb5_ticket_gain_handler
,
412 void ccache_regain_all_now(void)
414 struct WINBINDD_CCACHE_ENTRY
*cur
;
415 struct timeval t
= timeval_current();
417 for (cur
= ccache_list
; cur
; cur
= cur
->next
) {
418 struct tevent_timer
*new_event
;
421 * if refresh_time is 0, we know that the
422 * the event has the krb5_ticket_gain_handler
424 if (cur
->refresh_time
== 0) {
425 new_event
= tevent_add_timer(server_event_context(),
428 krb5_ticket_gain_handler
,
431 new_event
= tevent_add_timer(server_event_context(),
434 krb5_ticket_refresh_handler
,
442 TALLOC_FREE(cur
->event
);
443 cur
->event
= new_event
;
449 /****************************************************************
450 Check if an ccache entry exists.
451 ****************************************************************/
453 bool ccache_entry_exists(const char *username
)
455 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
456 return (entry
!= NULL
);
459 /****************************************************************
460 Ensure we're changing the correct entry.
461 ****************************************************************/
463 bool ccache_entry_identical(const char *username
,
467 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
473 if (entry
->uid
!= uid
) {
474 DEBUG(0,("cache_entry_identical: uid's differ: %u != %u\n",
475 (unsigned int)entry
->uid
, (unsigned int)uid
));
478 if (!strcsequal(entry
->ccname
, ccname
)) {
479 DEBUG(0,("cache_entry_identical: "
480 "ccnames differ: (cache) %s != (client) %s\n",
481 entry
->ccname
, ccname
));
487 NTSTATUS
add_ccache_to_list(const char *princ_name
,
490 const char *username
,
497 bool postponed_request
)
499 struct WINBINDD_CCACHE_ENTRY
*entry
= NULL
;
503 if ((username
== NULL
&& princ_name
== NULL
) ||
504 ccname
== NULL
|| uid
== (uid_t
)-1) {
505 return NT_STATUS_INVALID_PARAMETER
;
508 if (ccache_entry_count() + 1 > MAX_CCACHES
) {
509 DEBUG(10,("add_ccache_to_list: "
510 "max number of ccaches reached\n"));
511 return NT_STATUS_NO_MORE_ENTRIES
;
514 /* Reference count old entries */
515 entry
= get_ccache_by_username(username
);
517 /* Check cached entries are identical. */
518 if (!ccache_entry_identical(username
, uid
, ccname
)) {
519 return NT_STATUS_INVALID_PARAMETER
;
522 DEBUG(10,("add_ccache_to_list: "
523 "ref count on entry %s is now %d\n",
524 username
, entry
->ref_count
));
525 /* FIXME: in this case we still might want to have a krb5 cred
526 * event handler created - gd
527 * Add ticket refresh handler here */
529 if (!lp_winbind_refresh_tickets() || renew_until
<= 0) {
534 if (postponed_request
) {
535 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
536 add_krb5_ticket_gain_handler_event(entry
, t
);
538 /* Renew at 1/2 the ticket expiration time */
539 #if defined(DEBUG_KRB5_TKT_RENEWAL)
540 t
= timeval_set(time(NULL
)+30, 0);
542 t
= timeval_set(krb5_event_refresh_time(ticket_end
),
545 if (!entry
->refresh_time
) {
546 entry
->refresh_time
= t
.tv_sec
;
548 entry
->event
= tevent_add_timer(server_event_context(),
551 krb5_ticket_refresh_handler
,
556 ntret
= remove_ccache(username
);
557 if (!NT_STATUS_IS_OK(ntret
)) {
558 DEBUG(0, ("add_ccache_to_list: Failed to remove krb5 "
559 "ccache %s for user %s\n", entry
->ccname
,
561 DEBUG(0, ("add_ccache_to_list: error is %s\n",
565 return NT_STATUS_NO_MEMORY
;
568 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
573 * If we're set up to renew our krb5 tickets, we must
574 * cache the credentials in memory for the ticket
575 * renew function (or increase the reference count
576 * if we're logging in more than once). Fix inspired
577 * by patch from Ian Gordon <ian.gordon@strath.ac.uk>
581 ntret
= winbindd_add_memory_creds(username
, uid
, pass
);
582 DEBUG(10, ("winbindd_add_memory_creds returned: %s\n",
588 entry
= talloc(NULL
, struct WINBINDD_CCACHE_ENTRY
);
590 return NT_STATUS_NO_MEMORY
;
596 entry
->username
= talloc_strdup(entry
, username
);
597 if (!entry
->username
) {
602 entry
->principal_name
= talloc_strdup(entry
, princ_name
);
603 if (!entry
->principal_name
) {
608 entry
->service
= talloc_strdup(entry
, service
);
609 if (!entry
->service
) {
614 entry
->ccname
= talloc_strdup(entry
, ccname
);
615 if (!entry
->ccname
) {
619 entry
->realm
= talloc_strdup(entry
, realm
);
624 entry
->create_time
= create_time
;
625 entry
->renew_until
= renew_until
;
627 entry
->ref_count
= 1;
629 if (!lp_winbind_refresh_tickets() || renew_until
<= 0) {
633 if (postponed_request
) {
634 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
635 add_krb5_ticket_gain_handler_event(entry
, t
);
637 /* Renew at 1/2 the ticket expiration time */
638 #if defined(DEBUG_KRB5_TKT_RENEWAL)
639 t
= timeval_set(time(NULL
)+30, 0);
641 t
= timeval_set(krb5_event_refresh_time(ticket_end
), 0);
643 if (entry
->refresh_time
== 0) {
644 entry
->refresh_time
= t
.tv_sec
;
646 entry
->event
= tevent_add_timer(server_event_context(),
649 krb5_ticket_refresh_handler
,
657 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
661 DLIST_ADD(ccache_list
, entry
);
663 DEBUG(10,("add_ccache_to_list: "
664 "added ccache [%s] for user [%s] to the list\n",
669 * If we're set up to renew our krb5 tickets, we must
670 * cache the credentials in memory for the ticket
671 * renew function. Fix inspired by patch from
672 * Ian Gordon <ian.gordon@strath.ac.uk> for
676 ntret
= winbindd_add_memory_creds(username
, uid
, pass
);
677 DEBUG(10, ("winbindd_add_memory_creds returned: %s\n",
686 return NT_STATUS_NO_MEMORY
;
689 /*******************************************************************
690 Remove a WINBINDD_CCACHE_ENTRY entry and the krb5 ccache if no longer
692 *******************************************************************/
694 NTSTATUS
remove_ccache(const char *username
)
696 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
697 NTSTATUS status
= NT_STATUS_OK
;
703 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
706 if (entry
->ref_count
<= 0) {
707 DEBUG(0,("remove_ccache: logic error. "
708 "ref count for user %s = %d\n",
709 username
, entry
->ref_count
));
710 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
715 if (entry
->ref_count
> 0) {
716 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
717 username
, entry
->ref_count
));
721 /* no references any more */
723 DLIST_REMOVE(ccache_list
, entry
);
724 TALLOC_FREE(entry
->event
); /* unregisters events */
727 ret
= ads_kdestroy(entry
->ccname
);
729 /* we ignore the error when there has been no credential cache */
730 if (ret
== KRB5_FCC_NOFILE
) {
733 DEBUG(0,("remove_ccache: "
734 "failed to destroy user krb5 ccache %s with: %s\n",
735 entry
->ccname
, error_message(ret
)));
737 DEBUG(10,("remove_ccache: "
738 "successfully destroyed krb5 ccache %s for user %s\n",
739 entry
->ccname
, username
));
741 status
= krb5_to_nt_status(ret
);
745 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username
));
750 /*******************************************************************
751 In memory credentials cache code.
752 *******************************************************************/
754 static struct WINBINDD_MEMORY_CREDS
*memory_creds_list
;
756 /***********************************************************
757 Find an entry on the list by name.
758 ***********************************************************/
760 struct WINBINDD_MEMORY_CREDS
*find_memory_creds_by_name(const char *username
)
762 struct WINBINDD_MEMORY_CREDS
*p
;
764 for (p
= memory_creds_list
; p
; p
= p
->next
) {
765 if (strequal(p
->username
, username
)) {
772 /***********************************************************
773 Store the required creds and mlock them.
774 ***********************************************************/
776 static NTSTATUS
store_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
,
779 #if !defined(HAVE_MLOCK)
782 /* new_entry->nt_hash is the base pointer for the block
783 of memory pointed into by new_entry->lm_hash and
784 new_entry->pass (if we're storing plaintext). */
786 memcredp
->len
= NT_HASH_LEN
+ LM_HASH_LEN
;
788 memcredp
->len
+= strlen(pass
)+1;
793 /* aligning the memory on on x86_64 and compiling
794 with gcc 4.1 using -O2 causes a segv in the
795 next memset() --jerry */
796 memcredp
->nt_hash
= SMB_MALLOC_ARRAY(unsigned char, memcredp
->len
);
798 /* On non-linux platforms, mlock()'d memory must be aligned */
799 memcredp
->nt_hash
= SMB_MEMALIGN_ARRAY(unsigned char,
800 getpagesize(), memcredp
->len
);
802 if (!memcredp
->nt_hash
) {
803 return NT_STATUS_NO_MEMORY
;
805 memset(memcredp
->nt_hash
, 0x0, memcredp
->len
);
807 memcredp
->lm_hash
= memcredp
->nt_hash
+ NT_HASH_LEN
;
809 #ifdef DEBUG_PASSWORD
810 DEBUG(10,("mlocking memory: %p\n", memcredp
->nt_hash
));
812 if ((mlock(memcredp
->nt_hash
, memcredp
->len
)) == -1) {
813 DEBUG(0,("failed to mlock memory: %s (%d)\n",
814 strerror(errno
), errno
));
815 SAFE_FREE(memcredp
->nt_hash
);
816 return map_nt_error_from_unix(errno
);
819 #ifdef DEBUG_PASSWORD
820 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
);
828 memcredp
->pass
= (char *)memcredp
->lm_hash
+ LM_HASH_LEN
;
829 memcpy(memcredp
->pass
, pass
,
830 memcredp
->len
- NT_HASH_LEN
- LM_HASH_LEN
);
837 /***********************************************************
838 Destroy existing creds.
839 ***********************************************************/
841 static NTSTATUS
delete_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
)
843 #if !defined(HAVE_MUNLOCK)
846 if (munlock(memcredp
->nt_hash
, memcredp
->len
) == -1) {
847 DEBUG(0,("failed to munlock memory: %s (%d)\n",
848 strerror(errno
), errno
));
849 return map_nt_error_from_unix(errno
);
851 memset(memcredp
->nt_hash
, '\0', memcredp
->len
);
852 SAFE_FREE(memcredp
->nt_hash
);
853 memcredp
->nt_hash
= NULL
;
854 memcredp
->lm_hash
= NULL
;
855 memcredp
->pass
= NULL
;
861 /***********************************************************
862 Replace the required creds with new ones (password change).
863 ***********************************************************/
865 static NTSTATUS
winbindd_replace_memory_creds_internal(struct WINBINDD_MEMORY_CREDS
*memcredp
,
868 NTSTATUS status
= delete_memory_creds(memcredp
);
869 if (!NT_STATUS_IS_OK(status
)) {
872 return store_memory_creds(memcredp
, pass
);
875 /*************************************************************
876 Store credentials in memory in a list.
877 *************************************************************/
879 static NTSTATUS
winbindd_add_memory_creds_internal(const char *username
,
883 /* Shortcut to ensure we don't store if no mlock. */
884 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
888 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
890 memcredp
= find_memory_creds_by_name(username
);
891 if (uid
== (uid_t
)-1) {
892 DEBUG(0,("winbindd_add_memory_creds_internal: "
893 "invalid uid for user %s.\n", username
));
894 return NT_STATUS_INVALID_PARAMETER
;
898 /* Already exists. Increment the reference count and replace stored creds. */
899 if (uid
!= memcredp
->uid
) {
900 DEBUG(0,("winbindd_add_memory_creds_internal: "
901 "uid %u for user %s doesn't "
902 "match stored uid %u. Replacing.\n",
903 (unsigned int)uid
, username
,
904 (unsigned int)memcredp
->uid
));
907 memcredp
->ref_count
++;
908 DEBUG(10,("winbindd_add_memory_creds_internal: "
909 "ref count for user %s is now %d\n",
910 username
, memcredp
->ref_count
));
911 return winbindd_replace_memory_creds_internal(memcredp
, pass
);
914 memcredp
= talloc_zero(NULL
, struct WINBINDD_MEMORY_CREDS
);
916 return NT_STATUS_NO_MEMORY
;
918 memcredp
->username
= talloc_strdup(memcredp
, username
);
919 if (!memcredp
->username
) {
920 talloc_destroy(memcredp
);
921 return NT_STATUS_NO_MEMORY
;
924 status
= store_memory_creds(memcredp
, pass
);
925 if (!NT_STATUS_IS_OK(status
)) {
926 talloc_destroy(memcredp
);
931 memcredp
->ref_count
= 1;
932 DLIST_ADD(memory_creds_list
, memcredp
);
934 DEBUG(10,("winbindd_add_memory_creds_internal: "
935 "added entry for user %s\n", username
));
941 /*************************************************************
942 Store users credentials in memory. If we also have a
943 struct WINBINDD_CCACHE_ENTRY for this username with a
944 refresh timer, then store the plaintext of the password
945 and associate the new credentials with the struct WINBINDD_CCACHE_ENTRY.
946 *************************************************************/
948 NTSTATUS
winbindd_add_memory_creds(const char *username
,
952 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
955 status
= winbindd_add_memory_creds_internal(username
, uid
, pass
);
956 if (!NT_STATUS_IS_OK(status
)) {
961 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
962 memcredp
= find_memory_creds_by_name(username
);
964 entry
->cred_ptr
= memcredp
;
971 /*************************************************************
972 Decrement the in-memory ref count - delete if zero.
973 *************************************************************/
975 NTSTATUS
winbindd_delete_memory_creds(const char *username
)
977 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
978 struct WINBINDD_CCACHE_ENTRY
*entry
= NULL
;
979 NTSTATUS status
= NT_STATUS_OK
;
981 memcredp
= find_memory_creds_by_name(username
);
982 entry
= get_ccache_by_username(username
);
985 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
987 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
990 if (memcredp
->ref_count
<= 0) {
991 DEBUG(0,("winbindd_delete_memory_creds: logic error. "
992 "ref count for user %s = %d\n",
993 username
, memcredp
->ref_count
));
994 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
997 memcredp
->ref_count
--;
998 if (memcredp
->ref_count
<= 0) {
999 delete_memory_creds(memcredp
);
1000 DLIST_REMOVE(memory_creds_list
, memcredp
);
1001 talloc_destroy(memcredp
);
1002 DEBUG(10,("winbindd_delete_memory_creds: "
1003 "deleted entry for user %s\n",
1006 DEBUG(10,("winbindd_delete_memory_creds: "
1007 "entry for user %s ref_count now %d\n",
1008 username
, memcredp
->ref_count
));
1012 /* Ensure we have no dangling references to this. */
1013 entry
->cred_ptr
= NULL
;
1019 /***********************************************************
1020 Replace the required creds with new ones (password change).
1021 ***********************************************************/
1023 NTSTATUS
winbindd_replace_memory_creds(const char *username
,
1026 struct WINBINDD_MEMORY_CREDS
*memcredp
= NULL
;
1028 memcredp
= find_memory_creds_by_name(username
);
1030 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
1032 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1035 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
1038 return winbindd_replace_memory_creds_internal(memcredp
, pass
);