r18271: Big change:
[kai/samba.git] / source / nsswitch / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "winbindd.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 DOM_SID *sid,
33                             NET_USER_INFO_3 **info3,
34                             const uint8 *cached_nt_pass[NT_HASH_LEN],
35                             const uint8 *cred_salt[NT_HASH_LEN])
36 {
37         NET_USER_INFO_3 *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                               TALLOC_CTX *mem_ctx, 
58                               const char *user, 
59                               const char *pass, 
60                               NET_USER_INFO_3 *info3,
61                               const DOM_SID *user_sid)
62 {
63         NTSTATUS status;
64         uchar nt_pass[NT_HASH_LEN];
65         DOM_SID cred_sid;
66
67         if (info3 != NULL) {
68         
69                 DOM_SID sid;
70                 sid_copy(&sid, &(info3->dom_sid.sid));
71                 sid_append_rid(&sid, info3->user_rid);
72                 sid_copy(&cred_sid, &sid);
73                 info3->user_flgs |= LOGON_CACHED_ACCOUNT;
74                 
75         } else if (user_sid != NULL) {
76         
77                 sid_copy(&cred_sid, user_sid);
78                 
79         } else if (user != NULL) {
80         
81                 /* do lookup ourself */
82
83                 enum lsa_SidType type;
84                 
85                 if (!lookup_cached_name(mem_ctx,
86                                         domain->name,
87                                         user,
88                                         &cred_sid,
89                                         &type)) {
90                         return NT_STATUS_NO_SUCH_USER;
91                 }
92         } else {
93                 return NT_STATUS_INVALID_PARAMETER;
94         }
95                 
96         if (pass) {
97
98                 int count = 0;
99
100                 status = wcache_count_cached_creds(domain, &count);
101                 if (!NT_STATUS_IS_OK(status)) {
102                         return status;
103                 }
104
105                 DEBUG(11,("we have %d cached creds\n", count));
106
107                 if (count + 1 > MAX_CACHED_LOGINS) {
108
109                         DEBUG(10,("need to delete the oldest cached login\n"));
110
111                         status = wcache_remove_oldest_cached_creds(domain, &cred_sid);
112                         if (!NT_STATUS_IS_OK(status)) {
113                                 DEBUG(10,("failed to remove oldest cached cred: %s\n", 
114                                         nt_errstr(status)));
115                                 return status;
116                         }
117                 }
118
119                 E_md4hash(pass, nt_pass);
120
121 #if DEBUG_PASSWORD
122                 dump_data(100, (const char *)nt_pass, NT_HASH_LEN);
123 #endif
124
125                 status = wcache_save_creds(domain, mem_ctx, &cred_sid, nt_pass);
126                 if (!NT_STATUS_IS_OK(status)) {
127                         return status;
128                 }
129         }
130
131         if (info3 != NULL && user != NULL) {
132                 if (!netsamlogon_cache_store(user, info3)) {
133                         return NT_STATUS_ACCESS_DENIED;
134                 }
135         }
136
137         return NT_STATUS_OK;
138 }
139
140 NTSTATUS winbindd_update_creds_by_info3(struct winbindd_domain *domain,
141                                         TALLOC_CTX *mem_ctx,
142                                         const char *user,
143                                         const char *pass,
144                                         NET_USER_INFO_3 *info3)
145 {
146         return winbindd_store_creds(domain, mem_ctx, user, pass, info3, NULL);
147 }
148
149 NTSTATUS winbindd_update_creds_by_sid(struct winbindd_domain *domain,
150                                       TALLOC_CTX *mem_ctx,
151                                       const DOM_SID *sid,
152                                       const char *pass)
153 {
154         return winbindd_store_creds(domain, mem_ctx, NULL, pass, NULL, sid);
155 }
156
157 NTSTATUS winbindd_update_creds_by_name(struct winbindd_domain *domain,
158                                        TALLOC_CTX *mem_ctx,
159                                        const char *user,
160                                        const char *pass)
161 {
162         return winbindd_store_creds(domain, mem_ctx, user, pass, NULL, NULL);
163 }
164
165