lib/util: new merged debug system
[Samba.git] / source3 / winbindd / winbindd_creds.c
blob6bbd0ffd9d134bd7dc6206d6d3bea2d97ad13a13
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 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
29 #define MAX_CACHED_LOGINS 10
31 NTSTATUS winbindd_get_creds(struct winbindd_domain *domain,
32 TALLOC_CTX *mem_ctx,
33 const struct dom_sid *sid,
34 struct netr_SamInfo3 **info3,
35 const uint8 *cached_nt_pass[NT_HASH_LEN],
36 const uint8 *cred_salt[NT_HASH_LEN])
38 struct netr_SamInfo3 *info;
39 NTSTATUS status;
41 status = wcache_get_creds(domain, mem_ctx, sid, cached_nt_pass, cred_salt);
42 if (!NT_STATUS_IS_OK(status)) {
43 return status;
46 info = netsamlogon_cache_get(mem_ctx, sid);
47 if (info == NULL) {
48 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
51 *info3 = info;
53 return NT_STATUS_OK;
57 NTSTATUS winbindd_store_creds(struct winbindd_domain *domain,
58 const char *user,
59 const char *pass,
60 struct netr_SamInfo3 *info3)
62 NTSTATUS status;
63 uchar nt_pass[NT_HASH_LEN];
64 struct dom_sid cred_sid;
66 if (info3 != NULL) {
68 sid_compose(&cred_sid, info3->base.domain_sid,
69 info3->base.rid);
70 info3->base.user_flags |= NETLOGON_CACHED_ACCOUNT;
72 } else if (user != NULL) {
74 /* do lookup ourself */
76 enum lsa_SidType type;
78 if (!lookup_cached_name(domain->name,
79 user,
80 &cred_sid,
81 &type)) {
82 return NT_STATUS_NO_SUCH_USER;
84 } else {
85 return NT_STATUS_INVALID_PARAMETER;
88 if (pass) {
90 int count = 0;
92 status = wcache_count_cached_creds(domain, &count);
93 if (!NT_STATUS_IS_OK(status)) {
94 return status;
97 DEBUG(11,("we have %d cached creds\n", count));
99 if (count + 1 > MAX_CACHED_LOGINS) {
101 DEBUG(10,("need to delete the oldest cached login\n"));
103 status = wcache_remove_oldest_cached_creds(domain, &cred_sid);
104 if (!NT_STATUS_IS_OK(status)) {
105 DEBUG(10,("failed to remove oldest cached cred: %s\n",
106 nt_errstr(status)));
107 return status;
111 E_md4hash(pass, nt_pass);
113 dump_data_pw("nt_pass", nt_pass, NT_HASH_LEN);
115 status = wcache_save_creds(domain, &cred_sid, nt_pass);
116 if (!NT_STATUS_IS_OK(status)) {
117 return status;
121 if (info3 != NULL && user != NULL) {
122 if (!netsamlogon_cache_store(user, info3)) {
123 return NT_STATUS_ACCESS_DENIED;
127 return NT_STATUS_OK;
130 NTSTATUS winbindd_update_creds_by_info3(struct winbindd_domain *domain,
131 const char *user,
132 const char *pass,
133 struct netr_SamInfo3 *info3)
135 return winbindd_store_creds(domain, user, pass, info3);
138 NTSTATUS winbindd_update_creds_by_name(struct winbindd_domain *domain,
139 const char *user,
140 const char *pass)
142 return winbindd_store_creds(domain, user, pass, NULL);