afbbd618eb8efa1685a5eb0f3727b2e4d57d709b
[jelmer/samba4-debian.git] / source / auth / gensec / schannel_state.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    module to store/fetch session keys for the schannel server
5
6    Copyright (C) Andrew Tridgell 2004
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 "lib/ldb/include/ldb.h"
24 #include "lib/ldb/include/ldb_errors.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "db_wrap.h"
27 #include "libcli/auth/libcli_auth.h"
28 #include "auth/auth.h"
29 #include "param/param.h"
30
31 /**
32   connect to the schannel ldb
33 */
34 struct ldb_context *schannel_db_connect(TALLOC_CTX *mem_ctx)
35 {
36         char *path;
37         struct ldb_context *ldb;
38         BOOL existed;
39         const char *init_ldif = 
40                 "dn: @ATTRIBUTES\n" \
41                 "computerName: CASE_INSENSITIVE\n" \
42                 "flatname: CASE_INSENSITIVE\n";
43
44         path = smbd_tmp_path(mem_ctx, "schannel.ldb");
45         if (!path) {
46                 return NULL;
47         }
48
49         existed = file_exist(path);
50         
51         ldb = ldb_wrap_connect(mem_ctx, path, system_session(mem_ctx), 
52                                NULL, LDB_FLG_NOSYNC, NULL);
53         talloc_free(path);
54         if (!ldb) {
55                 return NULL;
56         }
57         
58         if (!existed) {
59                 gendb_add_ldif(ldb, init_ldif);
60         }
61
62         return ldb;
63 }
64
65 /*
66   remember an established session key for a netr server authentication
67   use a simple ldb structure
68 */
69 NTSTATUS schannel_store_session_key_ldb(TALLOC_CTX *mem_ctx,
70                                         struct ldb_context *ldb,
71                                         struct creds_CredentialState *creds)
72 {
73         struct ldb_message *msg;
74         struct ldb_val val, seed, client_state, server_state;
75         char *f;
76         char *sct;
77         int ret;
78
79         f = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->negotiate_flags);
80
81         if (f == NULL) {
82                 return NT_STATUS_NO_MEMORY;
83         }
84
85         sct = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->secure_channel_type);
86
87         if (sct == NULL) {
88                 return NT_STATUS_NO_MEMORY;
89         }
90
91         msg = ldb_msg_new(ldb);
92         if (msg == NULL) {
93                 return NT_STATUS_NO_MEMORY;
94         }
95
96         msg->dn = ldb_dn_new_fmt(msg, ldb, "computerName=%s", creds->computer_name);
97         if ( ! msg->dn) {
98                 return NT_STATUS_NO_MEMORY;
99         }
100
101         val.data = creds->session_key;
102         val.length = sizeof(creds->session_key);
103
104         seed.data = creds->seed.data;
105         seed.length = sizeof(creds->seed.data);
106
107         client_state.data = creds->client.data;
108         client_state.length = sizeof(creds->client.data);
109         server_state.data = creds->server.data;
110         server_state.length = sizeof(creds->server.data);
111
112         ldb_msg_add_string(msg, "objectClass", "schannelState");
113         ldb_msg_add_value(msg, "sessionKey", &val, NULL);
114         ldb_msg_add_value(msg, "seed", &seed, NULL);
115         ldb_msg_add_value(msg, "clientState", &client_state, NULL);
116         ldb_msg_add_value(msg, "serverState", &server_state, NULL);
117         ldb_msg_add_string(msg, "negotiateFlags", f);
118         ldb_msg_add_string(msg, "secureChannelType", sct);
119         ldb_msg_add_string(msg, "accountName", creds->account_name);
120         ldb_msg_add_string(msg, "computerName", creds->computer_name);
121         ldb_msg_add_string(msg, "flatname", creds->domain);
122         samdb_msg_add_dom_sid(ldb, mem_ctx, msg, "objectSid", creds->sid);
123
124         ldb_delete(ldb, msg->dn);
125
126         ret = ldb_add(ldb, msg);
127
128         if (ret != 0) {
129                 DEBUG(0,("Unable to add %s to session key db - %s\n", 
130                          ldb_dn_get_linearized(msg->dn), ldb_errstring(ldb)));
131                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
132         }
133
134         return NT_STATUS_OK;
135 }
136
137 NTSTATUS schannel_store_session_key(TALLOC_CTX *mem_ctx,
138                                     struct creds_CredentialState *creds)
139 {
140         struct ldb_context *ldb;
141         NTSTATUS nt_status;
142         int ret;
143                 
144         ldb = schannel_db_connect(mem_ctx);
145         if (!ldb) {
146                 return NT_STATUS_ACCESS_DENIED;
147         }
148
149         ret = ldb_transaction_start(ldb);
150         if (ret != 0) {
151                 talloc_free(ldb);
152                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
153         }
154
155         nt_status = schannel_store_session_key_ldb(mem_ctx, ldb, creds);
156
157         if (NT_STATUS_IS_OK(nt_status)) {
158                 ret = ldb_transaction_commit(ldb);
159         } else {
160                 ret = ldb_transaction_cancel(ldb);
161         }
162
163         if (ret != 0) {
164                 DEBUG(0,("Unable to commit adding credentials for %s to schannel key db - %s\n", 
165                          creds->computer_name, ldb_errstring(ldb)));
166                 talloc_free(ldb);
167                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
168         }
169
170         talloc_free(ldb);
171         return nt_status;
172 }
173
174 /*
175   read back a credentials back for a computer
176 */
177 NTSTATUS schannel_fetch_session_key_ldb(TALLOC_CTX *mem_ctx,
178                                         struct ldb_context *ldb,
179                                         const char *computer_name, 
180                                         const char *domain,
181                                         struct creds_CredentialState **creds)
182 {
183         struct ldb_result *res;
184         int ret;
185         const struct ldb_val *val;
186
187         *creds = talloc_zero(mem_ctx, struct creds_CredentialState);
188         if (!*creds) {
189                 return NT_STATUS_NO_MEMORY;
190         }
191
192         ret = ldb_search_exp_fmt(ldb, mem_ctx, &res,
193                                  NULL, LDB_SCOPE_SUBTREE, NULL,
194                                 "(&(computerName=%s)(flatname=%s))", computer_name, domain);
195         if (ret != LDB_SUCCESS) {
196                 DEBUG(3,("schannel: Failed to find a record for client %s: %s\n", computer_name, ldb_errstring(ldb)));
197                 return NT_STATUS_INVALID_HANDLE;
198         }
199         if (res->count != 1) {
200                 DEBUG(3,("schannel: Failed to find a record for client: %s (found %d records)\n", computer_name, res->count));
201                 talloc_free(res);
202                 return NT_STATUS_INVALID_HANDLE;
203         }
204
205         val = ldb_msg_find_ldb_val(res->msgs[0], "sessionKey");
206         if (val == NULL || val->length != 16) {
207                 DEBUG(1,("schannel: record in schannel DB must contain a sessionKey of length 16, when searching for client: %s\n", computer_name));
208                 talloc_free(res);
209                 return NT_STATUS_INTERNAL_ERROR;
210         }
211
212         memcpy((*creds)->session_key, val->data, 16);
213
214         val = ldb_msg_find_ldb_val(res->msgs[0], "seed");
215         if (val == NULL || val->length != 8) {
216                 DEBUG(1,("schannel: record in schannel DB must contain a vaid seed of length 8, when searching for client: %s\n", computer_name));
217                 talloc_free(res);
218                 return NT_STATUS_INTERNAL_ERROR;
219         }
220
221         memcpy((*creds)->seed.data, val->data, 8);
222
223         val = ldb_msg_find_ldb_val(res->msgs[0], "clientState");
224         if (val == NULL || val->length != 8) {
225                 DEBUG(1,("schannel: record in schannel DB must contain a vaid clientState of length 8, when searching for client: %s\n", computer_name));
226                 talloc_free(res);
227                 return NT_STATUS_INTERNAL_ERROR;
228         }
229         memcpy((*creds)->client.data, val->data, 8);
230
231         val = ldb_msg_find_ldb_val(res->msgs[0], "serverState");
232         if (val == NULL || val->length != 8) {
233                 DEBUG(1,("schannel: record in schannel DB must contain a vaid serverState of length 8, when searching for client: %s\n", computer_name));
234                 talloc_free(res);
235                 return NT_STATUS_INTERNAL_ERROR;
236         }
237         memcpy((*creds)->server.data, val->data, 8);
238
239         (*creds)->negotiate_flags = ldb_msg_find_attr_as_int(res->msgs[0], "negotiateFlags", 0);
240
241         (*creds)->secure_channel_type = ldb_msg_find_attr_as_int(res->msgs[0], "secureChannelType", 0);
242
243         (*creds)->account_name = talloc_strdup(*creds, ldb_msg_find_attr_as_string(res->msgs[0], "accountName", NULL));
244         if ((*creds)->account_name == NULL) {
245                 talloc_free(res);
246                 return NT_STATUS_NO_MEMORY;
247         }
248
249         (*creds)->computer_name = talloc_strdup(*creds, ldb_msg_find_attr_as_string(res->msgs[0], "computerName", NULL));
250         if ((*creds)->computer_name == NULL) {
251                 talloc_free(res);
252                 return NT_STATUS_NO_MEMORY;
253         }
254
255         (*creds)->domain = talloc_strdup(*creds, ldb_msg_find_attr_as_string(res->msgs[0], "flatname", NULL));
256         if ((*creds)->domain == NULL) {
257                 talloc_free(res);
258                 return NT_STATUS_NO_MEMORY;
259         }
260
261         (*creds)->sid = samdb_result_dom_sid(*creds, res->msgs[0], "objectSid");
262
263         talloc_free(res);
264         return NT_STATUS_OK;
265 }
266
267 NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
268                                         const char *computer_name, 
269                                         const char *domain, 
270                                         struct creds_CredentialState **creds)
271 {
272         NTSTATUS nt_status;
273         struct ldb_context *ldb;
274
275         ldb = schannel_db_connect(mem_ctx);
276         if (!ldb) {
277                 return NT_STATUS_ACCESS_DENIED;
278         }
279
280         nt_status = schannel_fetch_session_key_ldb(mem_ctx, ldb,
281                                                    computer_name, domain, 
282                                                    creds);
283         talloc_free(ldb);
284         return nt_status;
285 }