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