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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #define DBGC_CLASS DBGC_WINBIND
30 /* uncomment this to to fast debugging on the krb5 ticket renewal event */
31 #ifdef DEBUG_KRB5_TKT_RENEWAL
32 #undef DEBUG_KRB5_TKT_RENEWAL
35 #define MAX_CCACHES 100
37 static struct WINBINDD_CCACHE_ENTRY
*ccache_list
;
38 static void add_krb5_ticket_gain_handler_event(struct WINBINDD_CCACHE_ENTRY
*entry
,
41 /* The Krb5 ticket refresh handler should be scheduled
42 at one-half of the period from now till the tkt
44 #define KRB5_EVENT_REFRESH_TIME(x) ((x) - (((x) - time(NULL))/2))
46 /****************************************************************
47 Find an entry by name.
48 ****************************************************************/
50 static struct WINBINDD_CCACHE_ENTRY
*get_ccache_by_username(const char *username
)
52 struct WINBINDD_CCACHE_ENTRY
*entry
;
54 for (entry
= ccache_list
; entry
; entry
= entry
->next
) {
55 if (strequal(entry
->username
, username
)) {
62 /****************************************************************
64 ****************************************************************/
66 static int ccache_entry_count(void)
68 struct WINBINDD_CCACHE_ENTRY
*entry
;
71 for (entry
= ccache_list
; entry
; entry
= entry
->next
) {
77 void ccache_remove_all_after_fork(void)
79 struct WINBINDD_CCACHE_ENTRY
*cur
;
82 DLIST_REMOVE(ccache_list
, cur
);
83 TALLOC_FREE(cur
->event
);
89 static void krb5_ticket_gain_handler(struct event_context
*event_ctx
,
90 struct timed_event
*te
,
91 const struct timeval
*now
,
93 static void krb5_ticket_refresh_handler(struct event_context
*event_ctx
,
94 struct timed_event
*te
,
95 const struct timeval
*now
,
98 void ccache_regain_all_now(void)
100 struct WINBINDD_CCACHE_ENTRY
*cur
;
101 struct timeval t
= timeval_current();
103 for (cur
= ccache_list
; cur
; cur
= cur
->next
) {
104 struct timed_event
*new_event
;
107 * if refresh_time is 0, we know that the
108 * the event has the krb5_ticket_gain_handler
110 if (cur
->refresh_time
== 0) {
111 new_event
= event_add_timed(winbind_event_context(),
113 "krb5_ticket_gain_handler",
114 krb5_ticket_gain_handler
,
117 new_event
= event_add_timed(winbind_event_context(),
119 "krb5_ticket_refresh_handler",
120 krb5_ticket_refresh_handler
,
127 TALLOC_FREE(cur
->event
);
128 cur
->event
= new_event
;
133 /****************************************************************
134 The gain initial ticket is recognized as entry->refresh_time is
136 ****************************************************************/
138 static void add_krb5_ticket_gain_handler_event(struct WINBINDD_CCACHE_ENTRY
*entry
,
141 entry
->refresh_time
= 0;
142 entry
->event
= event_add_timed(winbind_event_context(), entry
,
144 "krb5_ticket_gain_handler",
145 krb5_ticket_gain_handler
,
149 /****************************************************************
150 Do the work of refreshing the ticket.
151 ****************************************************************/
153 static void krb5_ticket_refresh_handler(struct event_context
*event_ctx
,
154 struct timed_event
*te
,
155 const struct timeval
*now
,
158 struct WINBINDD_CCACHE_ENTRY
*entry
=
159 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
163 time_t expire_time
= 0;
164 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
167 DEBUG(10,("krb5_ticket_refresh_handler called\n"));
168 DEBUGADD(10,("event called for: %s, %s\n", entry
->ccname
, entry
->username
));
170 TALLOC_FREE(entry
->event
);
174 /* Kinit again if we have the user password and we can't renew the old
177 * This happens when machine are put to sleep for a very long time. */
179 if (entry
->renew_until
< time(NULL
)) {
181 if (cred_ptr
&& cred_ptr
->pass
) {
183 set_effective_uid(entry
->uid
);
185 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
187 0, /* hm, can we do time correction here ? */
188 &entry
->refresh_time
,
191 False
, /* no PAC required anymore */
193 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
);
194 gain_root_privilege();
197 DEBUG(3,("krb5_ticket_refresh_handler: "
198 "could not re-kinit: %s\n",
199 error_message(ret
)));
200 /* destroy the ticket because we cannot rekinit
201 * it, ignore error here */
202 ads_kdestroy(entry
->ccname
);
204 /* Don't break the ticket refresh chain: retry
205 * refreshing ticket sometime later when KDC is
206 * unreachable -- BoYang.
207 * More error handling here? KRB5_CC_IO,
208 * KRB5KRB_AP_ERR_SKEW.
211 if ((ret
== KRB5_KDC_UNREACH
)
212 || (ret
== KRB5_REALM_CANT_RESOLVE
)) {
213 #if defined(DEBUG_KRB5_TKT_RENEWAL)
214 new_start
= time(NULL
) + 30;
216 new_start
= time(NULL
) +
217 MAX(30, lp_winbind_cache_time());
219 /* try to regain ticket here */
220 add_krb5_ticket_gain_handler_event(entry
,
221 timeval_set(new_start
, 0));
224 TALLOC_FREE(entry
->event
);
228 DEBUG(10,("krb5_ticket_refresh_handler: successful re-kinit "
229 "for: %s in ccache: %s\n",
230 entry
->principal_name
, entry
->ccname
));
232 #if defined(DEBUG_KRB5_TKT_RENEWAL)
233 new_start
= time(NULL
) + 30;
235 /* The tkt should be refreshed at one-half the period
236 from now to the expiration time */
237 expire_time
= entry
->refresh_time
;
238 new_start
= KRB5_EVENT_REFRESH_TIME(entry
->refresh_time
);
243 * No cached credentials
244 * destroy ticket and refresh chain
246 ads_kdestroy(entry
->ccname
);
247 TALLOC_FREE(entry
->event
);
252 set_effective_uid(entry
->uid
);
254 ret
= smb_krb5_renew_ticket(entry
->ccname
,
255 entry
->principal_name
,
258 #if defined(DEBUG_KRB5_TKT_RENEWAL)
259 new_start
= time(NULL
) + 30;
261 expire_time
= new_start
;
262 new_start
= KRB5_EVENT_REFRESH_TIME(new_start
);
265 gain_root_privilege();
268 DEBUG(3,("krb5_ticket_refresh_handler: could not renew tickets: %s\n",
269 error_message(ret
)));
270 /* maybe we are beyond the renewing window */
272 /* evil rises here, we refresh ticket failed,
273 * but the ticket might be expired. Therefore,
274 * When we refresh ticket failed, destory the
277 ads_kdestroy(entry
->ccname
);
279 /* avoid breaking the renewal chain: retry in lp_winbind_cache_time()
280 * seconds when the KDC was not available right now.
281 * the return code can be KRB5_REALM_CANT_RESOLVE
282 * More error handling here? KRB5_CC_IO, KRB5KRB_AP_ERR_SKEW. */
284 if ((ret
== KRB5_KDC_UNREACH
)
285 || (ret
== KRB5_REALM_CANT_RESOLVE
)) {
286 #if defined(DEBUG_KRB5_TKT_RENEWAL)
287 new_start
= time(NULL
) + 30;
289 new_start
= time(NULL
) +
290 MAX(30, lp_winbind_cache_time());
292 /* ticket is destroyed here, we have to regain it
293 * if it is possible */
294 add_krb5_ticket_gain_handler_event(entry
, timeval_set(new_start
, 0));
297 /* This is evil, if the ticket was already expired.
298 * renew ticket function returns KRB5KRB_AP_ERR_TKT_EXPIRED.
299 * But there is still a chance that we can rekinit it.
301 * This happens when user login in online mode, and then network
302 * down or something cause winbind goes offline for a very long time,
303 * and then goes online again. ticket expired, renew failed.
304 * This happens when machine are put to sleep for a long time,
305 * but shorter than entry->renew_util.
307 * Looks like the KDC is reachable, we want to rekinit as soon as
308 * possible instead of waiting some time later. */
309 if ((ret
== KRB5KRB_AP_ERR_TKT_EXPIRED
)
310 || (ret
== KRB5_FCC_NOFILE
)) goto rekinit
;
317 /* in cases that ticket will be unrenewable soon, we don't try to renew ticket
318 * but try to regain ticket if it is possible */
319 if (entry
->renew_until
&& expire_time
320 && (entry
->renew_until
<= expire_time
)) {
321 /* try to regain ticket 10 seconds beforre expiration */
323 add_krb5_ticket_gain_handler_event(entry
, timeval_set(expire_time
, 0));
327 if (!entry
->refresh_time
) {
328 entry
->refresh_time
= new_start
;
330 entry
->event
= event_add_timed(winbind_event_context(), entry
,
331 timeval_set(new_start
, 0),
332 "krb5_ticket_refresh_handler",
333 krb5_ticket_refresh_handler
,
339 /****************************************************************
340 Do the work of regaining a ticket when coming from offline auth.
341 ****************************************************************/
343 static void krb5_ticket_gain_handler(struct event_context
*event_ctx
,
344 struct timed_event
*te
,
345 const struct timeval
*now
,
348 struct WINBINDD_CCACHE_ENTRY
*entry
=
349 talloc_get_type_abort(private_data
, struct WINBINDD_CCACHE_ENTRY
);
353 struct WINBINDD_MEMORY_CREDS
*cred_ptr
= entry
->cred_ptr
;
354 struct winbindd_domain
*domain
= NULL
;
357 DEBUG(10,("krb5_ticket_gain_handler called\n"));
358 DEBUGADD(10,("event called for: %s, %s\n", entry
->ccname
, entry
->username
));
360 TALLOC_FREE(entry
->event
);
364 if (!cred_ptr
|| !cred_ptr
->pass
) {
365 DEBUG(10,("krb5_ticket_gain_handler: no memory creds\n"));
369 if ((domain
= find_domain_from_name(entry
->realm
)) == NULL
) {
370 DEBUG(0,("krb5_ticket_gain_handler: unknown domain\n"));
374 if (domain
->online
) {
376 set_effective_uid(entry
->uid
);
378 ret
= kerberos_kinit_password_ext(entry
->principal_name
,
380 0, /* hm, can we do time correction here ? */
381 &entry
->refresh_time
,
384 False
, /* no PAC required anymore */
386 WINBINDD_PAM_AUTH_KRB5_RENEW_TIME
);
387 gain_root_privilege();
390 DEBUG(3,("krb5_ticket_gain_handler: could not kinit: %s\n",
391 error_message(ret
)));
392 /* evil. If we cannot do it, destroy any the __maybe__
393 * __existing__ ticket */
394 ads_kdestroy(entry
->ccname
);
398 DEBUG(10,("krb5_ticket_gain_handler: successful kinit for: %s in ccache: %s\n",
399 entry
->principal_name
, entry
->ccname
));
405 #if defined(DEBUG_KRB5_TKT_REGAIN)
406 t
= timeval_set(time(NULL
) + 30, 0);
408 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
410 add_krb5_ticket_gain_handler_event(entry
, t
);
416 #if defined(DEBUG_KRB5_TKT_RENEWAL)
417 t
= timeval_set(time(NULL
) + 30, 0);
419 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(entry
->refresh_time
), 0);
422 if (!entry
->refresh_time
) {
423 entry
->refresh_time
= t
.tv_sec
;
425 entry
->event
= event_add_timed(winbind_event_context(), entry
,
427 "krb5_ticket_refresh_handler",
428 krb5_ticket_refresh_handler
,
435 /****************************************************************
436 Check if an ccache entry exists.
437 ****************************************************************/
439 BOOL
ccache_entry_exists(const char *username
)
441 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
442 return (entry
!= NULL
);
445 /****************************************************************
446 Ensure we're changing the correct entry.
447 ****************************************************************/
449 BOOL
ccache_entry_identical(const char *username
, uid_t uid
, const char *ccname
)
451 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
457 if (entry
->uid
!= uid
) {
458 DEBUG(0,("cache_entry_identical: uid's differ: %u != %u\n",
459 (unsigned int)entry
->uid
, (unsigned int)uid
));
462 if (!strcsequal(entry
->ccname
, ccname
)) {
463 DEBUG(0,("cache_entry_identical: ccnames differ: (cache) %s != (client) %s\n",
464 entry
->ccname
, ccname
));
470 NTSTATUS
add_ccache_to_list(const char *princ_name
,
473 const char *username
,
479 BOOL postponed_request
)
481 struct WINBINDD_CCACHE_ENTRY
*entry
= NULL
;
487 if ((username
== NULL
&& princ_name
== NULL
) || ccname
== NULL
|| uid
< 0) {
488 return NT_STATUS_INVALID_PARAMETER
;
491 if (ccache_entry_count() + 1 > MAX_CCACHES
) {
492 DEBUG(10,("add_ccache_to_list: max number of ccaches reached\n"));
493 return NT_STATUS_NO_MORE_ENTRIES
;
496 /* If it is cached login, destroy krb5 ticket
497 * to avoid surprise. */
499 if (postponed_request
) {
500 /* ignore KRB5_FCC_NOFILE error here */
501 ret
= ads_kdestroy(ccname
);
502 if (ret
== KRB5_FCC_NOFILE
) {
506 DEBUG(0, ("add_ccache_to_list: failed to destroy "
507 "user krb5 ccache %s with %s\n", ccname
,
508 error_message(ret
)));
509 return krb5_to_nt_status(ret
);
511 DEBUG(10, ("add_ccache_to_list: successfully destroyed "
512 "krb5 ccache %s for user %s\n", ccname
,
518 /* Reference count old entries */
519 entry
= get_ccache_by_username(username
);
521 /* Check cached entries are identical. */
522 if (!ccache_entry_identical(username
, uid
, ccname
)) {
523 return NT_STATUS_INVALID_PARAMETER
;
526 DEBUG(10,("add_ccache_to_list: ref count on entry %s is now %d\n",
527 username
, entry
->ref_count
));
528 /* FIXME: in this case we still might want to have a krb5 cred
529 * event handler created - gd
530 * Add ticket refresh handler here */
532 if (!lp_winbind_refresh_tickets() || renew_until
<= 0) {
538 if (postponed_request
) {
539 t
= timeval_current_ofs(MAX(30, lp_winbind_cache_time()), 0);
540 add_krb5_ticket_gain_handler_event(entry
, t
);
542 /* Renew at 1/2 the ticket expiration time */
543 #if defined(DEBUG_KRB5_TKT_RENEWAL)
544 t
= timeval_set(time(NULL
)+30, 0);
546 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end
), 0);
548 if (!entry
->refresh_time
) {
549 entry
->refresh_time
= t
.tv_sec
;
551 entry
->event
= event_add_timed(winbind_event_context(),
554 "krb5_ticket_refresh_handler",
555 krb5_ticket_refresh_handler
,
560 ntret
= remove_ccache(username
);
561 if (!NT_STATUS_IS_OK(ntret
)) {
562 DEBUG(0, ("add_ccache_to_list: Failed to remove krb5 "
563 "ccache %s for user %s\n", entry
->ccname
,
565 DEBUG(0, ("add_ccache_to_list: error is %s\n",
569 return NT_STATUS_NO_MEMORY
;
572 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
578 entry
= TALLOC_P(NULL
, struct WINBINDD_CCACHE_ENTRY
);
580 return NT_STATUS_NO_MEMORY
;
586 entry
->username
= talloc_strdup(entry
, username
);
587 if (!entry
->username
) {
592 entry
->principal_name
= talloc_strdup(entry
, princ_name
);
593 if (!entry
->principal_name
) {
598 entry
->service
= talloc_strdup(entry
, service
);
599 if (!entry
->service
) {
604 entry
->ccname
= talloc_strdup(entry
, ccname
);
605 if (!entry
->ccname
) {
609 entry
->realm
= talloc_strdup(entry
, realm
);
614 entry
->create_time
= create_time
;
615 entry
->renew_until
= renew_until
;
617 entry
->ref_count
= 1;
619 if (lp_winbind_refresh_tickets() && renew_until
> 0) {
621 if (postponed_request
) {
622 add_krb5_ticket_gain_handler_event(entry
, t
);
624 /* Renew at 1/2 the ticket expiration time */
625 #if defined(DEBUG_KRB5_TKT_RENEWAL)
626 t
= timeval_set(time(NULL
)+30, 0);
628 t
= timeval_set(KRB5_EVENT_REFRESH_TIME(ticket_end
), 0);
630 if (!entry
->refresh_time
) {
631 entry
->refresh_time
= t
.tv_sec
;
633 entry
->event
= event_add_timed(winbind_event_context(), entry
,
635 "krb5_ticket_refresh_handler",
636 krb5_ticket_refresh_handler
,
644 DEBUG(10,("add_ccache_to_list: added krb5_ticket handler\n"));
647 DLIST_ADD(ccache_list
, entry
);
649 DEBUG(10,("add_ccache_to_list: added ccache [%s] for user [%s] to the list\n", ccname
, username
));
656 return NT_STATUS_NO_MEMORY
;
659 /*******************************************************************
660 Remove a WINBINDD_CCACHE_ENTRY entry and the krb5 ccache if no longer referenced.
661 *******************************************************************/
663 NTSTATUS
remove_ccache(const char *username
)
665 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
666 NTSTATUS status
= NT_STATUS_OK
;
672 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
675 if (entry
->ref_count
<= 0) {
676 DEBUG(0,("remove_ccache: logic error. ref count for user %s = %d\n",
677 username
, entry
->ref_count
));
678 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
683 if (entry
->ref_count
> 0) {
684 DEBUG(10,("remove_ccache: entry %s ref count now %d\n",
685 username
, entry
->ref_count
));
689 /* no references any more */
691 DLIST_REMOVE(ccache_list
, entry
);
692 TALLOC_FREE(entry
->event
); /* unregisters events */
695 ret
= ads_kdestroy(entry
->ccname
);
697 /* we ignore the error when there has been no credential cache */
698 if (ret
== KRB5_FCC_NOFILE
) {
701 DEBUG(0,("remove_ccache: failed to destroy user krb5 ccache %s with: %s\n",
702 entry
->ccname
, error_message(ret
)));
704 DEBUG(10,("remove_ccache: successfully destroyed krb5 ccache %s for user %s\n",
705 entry
->ccname
, username
));
707 status
= krb5_to_nt_status(ret
);
711 DEBUG(10,("remove_ccache: removed ccache for user %s\n", username
));
716 /*******************************************************************
717 In memory credentials cache code.
718 *******************************************************************/
720 static struct WINBINDD_MEMORY_CREDS
*memory_creds_list
;
722 /***********************************************************
723 Find an entry on the list by name.
724 ***********************************************************/
726 struct WINBINDD_MEMORY_CREDS
*find_memory_creds_by_name(const char *username
)
728 struct WINBINDD_MEMORY_CREDS
*p
;
730 for (p
= memory_creds_list
; p
; p
= p
->next
) {
731 if (strequal(p
->username
, username
)) {
738 /***********************************************************
739 Store the required creds and mlock them.
740 ***********************************************************/
742 static NTSTATUS
store_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
, const char *pass
)
744 #if !defined(HAVE_MLOCK)
747 /* new_entry->nt_hash is the base pointer for the block
748 of memory pointed into by new_entry->lm_hash and
749 new_entry->pass (if we're storing plaintext). */
751 memcredp
->len
= NT_HASH_LEN
+ LM_HASH_LEN
;
753 memcredp
->len
+= strlen(pass
)+1;
758 /* aligning the memory on on x86_64 and compiling
759 with gcc 4.1 using -O2 causes a segv in the
760 next memset() --jerry */
761 memcredp
->nt_hash
= SMB_MALLOC_ARRAY(unsigned char, memcredp
->len
);
763 /* On non-linux platforms, mlock()'d memory must be aligned */
764 memcredp
->nt_hash
= SMB_MEMALIGN_ARRAY(unsigned char,
765 getpagesize(), memcredp
->len
);
767 if (!memcredp
->nt_hash
) {
768 return NT_STATUS_NO_MEMORY
;
770 memset( memcredp
->nt_hash
, 0x0, memcredp
->len
);
772 memcredp
->lm_hash
= memcredp
->nt_hash
+ NT_HASH_LEN
;
774 #ifdef DEBUG_PASSWORD
775 DEBUG(10,("mlocking memory: %p\n", memcredp
->nt_hash
));
777 if ((mlock(memcredp
->nt_hash
, memcredp
->len
)) == -1) {
778 DEBUG(0,("failed to mlock memory: %s (%d)\n",
779 strerror(errno
), errno
));
780 SAFE_FREE(memcredp
->nt_hash
);
781 return map_nt_error_from_unix(errno
);
784 #ifdef DEBUG_PASSWORD
785 DEBUG(10,("mlocked memory: %p\n", memcredp
->nt_hash
));
788 /* Create and store the password hashes. */
789 E_md4hash(pass
, memcredp
->nt_hash
);
790 E_deshash(pass
, memcredp
->lm_hash
);
793 memcredp
->pass
= (char *)memcredp
->lm_hash
+ LM_HASH_LEN
;
794 memcpy(memcredp
->pass
, pass
, memcredp
->len
- NT_HASH_LEN
- LM_HASH_LEN
);
801 /***********************************************************
802 Destroy existing creds.
803 ***********************************************************/
805 static NTSTATUS
delete_memory_creds(struct WINBINDD_MEMORY_CREDS
*memcredp
)
807 #if !defined(HAVE_MUNLOCK)
810 if (munlock(memcredp
->nt_hash
, memcredp
->len
) == -1) {
811 DEBUG(0,("failed to munlock memory: %s (%d)\n",
812 strerror(errno
), errno
));
813 return map_nt_error_from_unix(errno
);
815 memset(memcredp
->nt_hash
, '\0', memcredp
->len
);
816 SAFE_FREE(memcredp
->nt_hash
);
817 memcredp
->nt_hash
= NULL
;
818 memcredp
->lm_hash
= NULL
;
819 memcredp
->pass
= NULL
;
825 /***********************************************************
826 Replace the required creds with new ones (password change).
827 ***********************************************************/
829 static NTSTATUS
winbindd_replace_memory_creds_internal(struct WINBINDD_MEMORY_CREDS
*memcredp
,
832 NTSTATUS status
= delete_memory_creds(memcredp
);
833 if (!NT_STATUS_IS_OK(status
)) {
836 return store_memory_creds(memcredp
, pass
);
839 /*************************************************************
840 Store credentials in memory in a list.
841 *************************************************************/
843 static NTSTATUS
winbindd_add_memory_creds_internal(const char *username
, uid_t uid
, const char *pass
)
845 /* Shortcut to ensure we don't store if no mlock. */
846 #if !defined(HAVE_MLOCK) || !defined(HAVE_MUNLOCK)
850 struct WINBINDD_MEMORY_CREDS
*memcredp
= find_memory_creds_by_name(username
);
852 if (uid
== (uid_t
)-1) {
853 DEBUG(0,("winbindd_add_memory_creds_internal: invalid uid for user %s.\n",
855 return NT_STATUS_INVALID_PARAMETER
;
859 /* Already exists. Increment the reference count and replace stored creds. */
860 if (uid
!= memcredp
->uid
) {
861 DEBUG(0,("winbindd_add_memory_creds_internal: uid %u for user %s doesn't "
862 "match stored uid %u. Replacing.\n",
863 (unsigned int)uid
, username
, (unsigned int)memcredp
->uid
));
866 memcredp
->ref_count
++;
867 DEBUG(10,("winbindd_add_memory_creds_internal: ref count for user %s is now %d\n",
868 username
, memcredp
->ref_count
));
869 return winbindd_replace_memory_creds_internal(memcredp
, pass
);
872 memcredp
= TALLOC_ZERO_P(NULL
, struct WINBINDD_MEMORY_CREDS
);
874 return NT_STATUS_NO_MEMORY
;
876 memcredp
->username
= talloc_strdup(memcredp
, username
);
877 if (!memcredp
->username
) {
878 talloc_destroy(memcredp
);
879 return NT_STATUS_NO_MEMORY
;
882 status
= store_memory_creds(memcredp
, pass
);
883 if (!NT_STATUS_IS_OK(status
)) {
884 talloc_destroy(memcredp
);
889 memcredp
->ref_count
= 1;
890 DLIST_ADD(memory_creds_list
, memcredp
);
892 DEBUG(10,("winbindd_add_memory_creds_internal: added entry for user %s\n",
899 /*************************************************************
900 Store users credentials in memory. If we also have a
901 struct WINBINDD_CCACHE_ENTRY for this username with a
902 refresh timer, then store the plaintext of the password
903 and associate the new credentials with the struct WINBINDD_CCACHE_ENTRY.
904 *************************************************************/
906 NTSTATUS
winbindd_add_memory_creds(const char *username
, uid_t uid
, const char *pass
)
908 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
911 status
= winbindd_add_memory_creds_internal(username
, uid
, pass
);
912 if (!NT_STATUS_IS_OK(status
)) {
917 struct WINBINDD_MEMORY_CREDS
*memcredp
= find_memory_creds_by_name(username
);
919 entry
->cred_ptr
= memcredp
;
926 /*************************************************************
927 Decrement the in-memory ref count - delete if zero.
928 *************************************************************/
930 NTSTATUS
winbindd_delete_memory_creds(const char *username
)
932 struct WINBINDD_MEMORY_CREDS
*memcredp
= find_memory_creds_by_name(username
);
933 struct WINBINDD_CCACHE_ENTRY
*entry
= get_ccache_by_username(username
);
934 NTSTATUS status
= NT_STATUS_OK
;
937 DEBUG(10,("winbindd_delete_memory_creds: unknown user %s\n",
939 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
942 if (memcredp
->ref_count
<= 0) {
943 DEBUG(0,("winbindd_delete_memory_creds: logic error. ref count for user %s = %d\n",
944 username
, memcredp
->ref_count
));
945 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
948 memcredp
->ref_count
--;
949 if (memcredp
->ref_count
<= 0) {
950 delete_memory_creds(memcredp
);
951 DLIST_REMOVE(memory_creds_list
, memcredp
);
952 talloc_destroy(memcredp
);
953 DEBUG(10,("winbindd_delete_memory_creds: deleted entry for user %s\n",
956 DEBUG(10,("winbindd_delete_memory_creds: entry for user %s ref_count now %d\n",
957 username
, memcredp
->ref_count
));
961 entry
->cred_ptr
= NULL
; /* Ensure we have no dangling references to this. */
966 /***********************************************************
967 Replace the required creds with new ones (password change).
968 ***********************************************************/
970 NTSTATUS
winbindd_replace_memory_creds(const char *username
, const char *pass
)
972 struct WINBINDD_MEMORY_CREDS
*memcredp
= find_memory_creds_by_name(username
);
975 DEBUG(10,("winbindd_replace_memory_creds: unknown user %s\n",
977 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
980 DEBUG(10,("winbindd_replace_memory_creds: replaced creds for user %s\n",
983 return winbindd_replace_memory_creds_internal(memcredp
, pass
);