r12268: Use transactions to ensure that the schannel db is consistant.
[ira/wip.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 "system/time.h"
25 #include "auth/auth.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "db_wrap.h"
29
30 /*
31   connect to the schannel ldb
32 */
33 static 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_exists(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(TALLOC_CTX *mem_ctx,
69                                     struct creds_CredentialState *creds)
70 {
71         struct ldb_context *ldb;
72         struct ldb_message *msg;
73         struct ldb_val val, seed;
74         char *f;
75         char *sct;
76         int ret;
77
78         ldb = schannel_db_connect(mem_ctx);
79         if (ldb == NULL) {
80                 return NT_STATUS_NO_MEMORY;
81         }
82
83         f = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->negotiate_flags);
84
85         if (f == NULL) {
86                 talloc_free(ldb);
87                 return NT_STATUS_NO_MEMORY;
88         }
89
90         sct = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->secure_channel_type);
91
92         if (sct == NULL) {
93                 talloc_free(ldb);
94                 return NT_STATUS_NO_MEMORY;
95         }
96
97         msg = ldb_msg_new(ldb);
98         if (msg == NULL) {
99                 talloc_free(ldb);
100                 return NT_STATUS_NO_MEMORY;
101         }
102
103         msg->dn = ldb_dn_build_child(msg, "computerName", creds->computer_name, NULL);
104         if (msg->dn == NULL) {
105                 talloc_free(ldb);
106                 return NT_STATUS_NO_MEMORY;
107         }
108
109         val.data = creds->session_key;
110         val.length = sizeof(creds->session_key);
111
112         seed.data = creds->seed.data;
113         seed.length = sizeof(creds->seed.data);
114
115         ldb_msg_add_string(msg, "objectClass", "schannelState");
116         ldb_msg_add_value(msg, "sessionKey", &val);
117         ldb_msg_add_value(msg, "seed", &seed);
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         ret = ldb_transaction_start(ldb);
126         if (ret != 0) {
127                 DEBUG(0,("Unable to start transaction to add %s to session key db - %s\n", 
128                          ldb_dn_linearize(msg, msg->dn), ldb_errstring(ldb)));
129                 talloc_free(ldb);
130                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
131         }
132
133         ldb_delete(ldb, msg->dn);
134
135         ret = ldb_add(ldb, msg);
136
137         if (ret != 0) {
138                 DEBUG(0,("Unable to add %s to session key db - %s\n", 
139                          ldb_dn_linearize(msg, msg->dn), ldb_errstring(ldb)));
140                 talloc_free(ldb);
141                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
142         }
143
144         ret = ldb_transaction_commit(ldb);
145
146         if (ret != 0) {
147                 DEBUG(0,("Unable to commit adding %s to session key db - %s\n", 
148                          ldb_dn_linearize(msg, msg->dn), ldb_errstring(ldb)));
149                 talloc_free(ldb);
150                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
151         }
152
153         talloc_free(ldb);
154
155         return NT_STATUS_OK;
156 }
157
158
159 /*
160   read back a credentials back for a computer
161 */
162 NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
163                                     const char *computer_name, 
164                                     const char *domain,
165                                     struct creds_CredentialState **creds)
166 {
167         struct ldb_context *ldb;
168         struct ldb_result *res;
169         int ret;
170         const struct ldb_val *val;
171         char *expr=NULL;
172
173         *creds = talloc_zero(mem_ctx, struct creds_CredentialState);
174         if (!*creds) {
175                 return NT_STATUS_NO_MEMORY;
176         }
177
178         ldb = schannel_db_connect(mem_ctx);
179         if (ldb == NULL) {
180                 return NT_STATUS_NO_MEMORY;
181         }
182
183         expr = talloc_asprintf(mem_ctx, "(&(computerName=%s)(flatname=%s))", computer_name, domain);
184         if (expr == NULL) {
185                 talloc_free(ldb);
186                 return NT_STATUS_NO_MEMORY;
187         }
188
189         ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
190         if (ret != LDB_SUCCESS || res->count != 1) {
191                 talloc_free(ldb);
192                 return NT_STATUS_INVALID_HANDLE;
193         }
194
195         val = ldb_msg_find_ldb_val(res->msgs[0], "sessionKey");
196         if (val == NULL || val->length != 16) {
197                 DEBUG(1,("schannel: record in schannel DB must contain a sessionKey of length 16, when searching for client: %s\n", computer_name));
198                 talloc_free(ldb);
199                 return NT_STATUS_INTERNAL_ERROR;
200         }
201
202         memcpy((*creds)->session_key, val->data, 16);
203
204         val = ldb_msg_find_ldb_val(res->msgs[0], "seed");
205         if (val == NULL || val->length != 8) {
206                 DEBUG(1,("schannel: record in schannel DB must contain a vaid seed of length 8, when searching for client: %s\n", computer_name));
207                 talloc_free(ldb);
208                 return NT_STATUS_INTERNAL_ERROR;
209         }
210
211         memcpy((*creds)->seed.data, val->data, 8);
212
213         (*creds)->negotiate_flags = ldb_msg_find_int(res->msgs[0], "negotiateFlags", 0);
214
215         (*creds)->secure_channel_type = ldb_msg_find_int(res->msgs[0], "secureChannelType", 0);
216
217         (*creds)->account_name = talloc_reference(*creds, ldb_msg_find_string(res->msgs[0], "accountName", NULL));
218
219         (*creds)->computer_name = talloc_reference(*creds, ldb_msg_find_string(res->msgs[0], "computerName", NULL));
220
221         (*creds)->domain = talloc_reference(*creds, ldb_msg_find_string(res->msgs[0], "flatname", NULL));
222
223         (*creds)->sid = samdb_result_dom_sid(*creds, res->msgs[0], "objectSid");
224
225         talloc_free(ldb);
226
227         return NT_STATUS_OK;
228 }