s4-messaging: Add helpful comments
[Samba.git] / source3 / winbindd / winbindd_creds.c
blob15cca554d45e64d7fbf0b3600927e1e452b31511
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind daemon - cached credentials funcions
6 Copyright (C) Guenther Deschner 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "winbindd.h"
24 #include "../libcli/auth/libcli_auth.h"
25 #include "../libcli/security/security.h"
26 #include "libsmb/samlogon_cache.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
30 #define MAX_CACHED_LOGINS 10
32 NTSTATUS winbindd_get_creds(struct winbindd_domain *domain,
33 TALLOC_CTX *mem_ctx,
34 const struct dom_sid *sid,
35 struct netr_SamInfo3 **info3,
36 const uint8_t *cached_nt_pass[NT_HASH_LEN],
37 const uint8_t *cred_salt[NT_HASH_LEN])
39 struct netr_SamInfo3 *info;
40 NTSTATUS status;
42 status = wcache_get_creds(domain, mem_ctx, sid, cached_nt_pass, cred_salt);
43 if (!NT_STATUS_IS_OK(status)) {
44 return status;
47 info = netsamlogon_cache_get(mem_ctx, sid);
48 if (info == NULL) {
49 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
52 *info3 = info;
54 return NT_STATUS_OK;
58 NTSTATUS winbindd_store_creds(struct winbindd_domain *domain,
59 const char *user,
60 const char *pass,
61 struct netr_SamInfo3 *info3)
63 NTSTATUS status;
64 uchar nt_pass[NT_HASH_LEN];
65 struct dom_sid cred_sid;
67 if (info3 != NULL) {
69 sid_compose(&cred_sid, info3->base.domain_sid,
70 info3->base.rid);
71 info3->base.user_flags |= NETLOGON_CACHED_ACCOUNT;
73 } else if (user != NULL) {
75 /* do lookup ourself */
77 enum lsa_SidType type;
79 if (!lookup_cached_name(domain->name,
80 user,
81 &cred_sid,
82 &type)) {
83 return NT_STATUS_NO_SUCH_USER;
85 } else {
86 return NT_STATUS_INVALID_PARAMETER;
89 if (pass) {
91 int count = 0;
93 status = wcache_count_cached_creds(domain, &count);
94 if (!NT_STATUS_IS_OK(status)) {
95 return status;
98 DEBUG(11,("we have %d cached creds\n", count));
100 if (count + 1 > MAX_CACHED_LOGINS) {
102 DEBUG(10,("need to delete the oldest cached login\n"));
104 status = wcache_remove_oldest_cached_creds(domain, &cred_sid);
105 if (!NT_STATUS_IS_OK(status)) {
106 DEBUG(10,("failed to remove oldest cached cred: %s\n",
107 nt_errstr(status)));
108 return status;
112 E_md4hash(pass, nt_pass);
114 dump_data_pw("nt_pass", nt_pass, NT_HASH_LEN);
116 status = wcache_save_creds(domain, &cred_sid, nt_pass);
117 if (!NT_STATUS_IS_OK(status)) {
118 return status;
122 if (info3 != NULL && user != NULL) {
123 if (!netsamlogon_cache_store(user, info3)) {
124 return NT_STATUS_ACCESS_DENIED;
128 return NT_STATUS_OK;
131 NTSTATUS winbindd_update_creds_by_info3(struct winbindd_domain *domain,
132 const char *user,
133 const char *pass,
134 struct netr_SamInfo3 *info3)
136 return winbindd_store_creds(domain, user, pass, info3);
139 NTSTATUS winbindd_update_creds_by_name(struct winbindd_domain *domain,
140 const char *user,
141 const char *pass)
143 return winbindd_store_creds(domain, user, pass, NULL);