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