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