b1910b62c98240699db72e26faedd151f1262cb3
[kai/samba.git] / source3 / winbindd / winbindd_creds.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - cached credentials funcions
5
6    Copyright (C) Guenther Deschner 2005
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "winbindd.h"
24 #include "../libcli/auth/libcli_auth.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_WINBIND
27
28 #define MAX_CACHED_LOGINS 10
29
30 NTSTATUS winbindd_get_creds(struct winbindd_domain *domain,
31                             TALLOC_CTX *mem_ctx,
32                             const struct dom_sid *sid,
33                             struct netr_SamInfo3 **info3,
34                             const uint8 *cached_nt_pass[NT_HASH_LEN],
35                             const uint8 *cred_salt[NT_HASH_LEN])
36 {
37         struct netr_SamInfo3 *info;
38         NTSTATUS status;
39
40         status = wcache_get_creds(domain, mem_ctx, sid, cached_nt_pass, cred_salt);
41         if (!NT_STATUS_IS_OK(status)) {
42                 return status;
43         }
44
45         info = netsamlogon_cache_get(mem_ctx, sid);
46         if (info == NULL) {
47                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
48         }
49
50         *info3 = info;
51
52         return NT_STATUS_OK;
53 }
54
55
56 NTSTATUS winbindd_store_creds(struct winbindd_domain *domain,
57                               const char *user, 
58                               const char *pass, 
59                               struct netr_SamInfo3 *info3)
60 {
61         NTSTATUS status;
62         uchar nt_pass[NT_HASH_LEN];
63         struct dom_sid cred_sid;
64
65         if (info3 != NULL) {
66
67                 sid_compose(&cred_sid, info3->base.domain_sid,
68                             info3->base.rid);
69                 info3->base.user_flags |= NETLOGON_CACHED_ACCOUNT;
70
71         } else if (user != NULL) {
72
73                 /* do lookup ourself */
74
75                 enum lsa_SidType type;
76
77                 if (!lookup_cached_name(domain->name,
78                                         user,
79                                         &cred_sid,
80                                         &type)) {
81                         return NT_STATUS_NO_SUCH_USER;
82                 }
83         } else {
84                 return NT_STATUS_INVALID_PARAMETER;
85         }
86
87         if (pass) {
88
89                 int count = 0;
90
91                 status = wcache_count_cached_creds(domain, &count);
92                 if (!NT_STATUS_IS_OK(status)) {
93                         return status;
94                 }
95
96                 DEBUG(11,("we have %d cached creds\n", count));
97
98                 if (count + 1 > MAX_CACHED_LOGINS) {
99
100                         DEBUG(10,("need to delete the oldest cached login\n"));
101
102                         status = wcache_remove_oldest_cached_creds(domain, &cred_sid);
103                         if (!NT_STATUS_IS_OK(status)) {
104                                 DEBUG(10,("failed to remove oldest cached cred: %s\n", 
105                                         nt_errstr(status)));
106                                 return status;
107                         }
108                 }
109
110                 E_md4hash(pass, nt_pass);
111
112                 dump_data_pw("nt_pass", nt_pass, NT_HASH_LEN);
113
114                 status = wcache_save_creds(domain, &cred_sid, nt_pass);
115                 if (!NT_STATUS_IS_OK(status)) {
116                         return status;
117                 }
118         }
119
120         if (info3 != NULL && user != NULL) {
121                 if (!netsamlogon_cache_store(user, info3)) {
122                         return NT_STATUS_ACCESS_DENIED;
123                 }
124         }
125
126         return NT_STATUS_OK;
127 }
128
129 NTSTATUS winbindd_update_creds_by_info3(struct winbindd_domain *domain,
130                                         const char *user,
131                                         const char *pass,
132                                         struct netr_SamInfo3 *info3)
133 {
134         return winbindd_store_creds(domain, user, pass, info3);
135 }
136
137 NTSTATUS winbindd_update_creds_by_name(struct winbindd_domain *domain,
138                                        const char *user,
139                                        const char *pass)
140 {
141         return winbindd_store_creds(domain, user, pass, NULL);
142 }
143
144