r11199: Push an objectSid into the schannel state database, to match the new header.
[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 "db_wrap.h"
28
29 /*
30   connect to the schannel ldb
31 */
32 static struct ldb_context *schannel_db_connect(TALLOC_CTX *mem_ctx)
33 {
34         char *path;
35         struct ldb_context *ldb;
36         BOOL existed;
37         const char *init_ldif = 
38                 "dn: @ATTRIBUTES\n" \
39                 "computerName: CASE_INSENSITIVE\n" \
40                 "flatname: CASE_INSENSITIVE\n";
41
42         path = smbd_tmp_path(mem_ctx, "schannel.ldb");
43         if (!path) {
44                 return NULL;
45         }
46
47         existed = file_exists(path);
48         
49         ldb = ldb_wrap_connect(mem_ctx, path, LDB_FLG_NOSYNC, NULL);
50         talloc_free(path);
51         if (!ldb) {
52                 return NULL;
53         }
54         
55         if (!existed) {
56                 gendb_add_ldif(ldb, init_ldif);
57         }
58
59         return ldb;
60 }
61
62 /*
63   remember an established session key for a netr server authentication
64   use a simple ldb structure
65 */
66 NTSTATUS schannel_store_session_key(TALLOC_CTX *mem_ctx,
67                                     struct creds_CredentialState *creds)
68 {
69         struct ldb_context *ldb;
70         struct ldb_message *msg;
71         struct ldb_val val, seed;
72         char *f;
73         char *sct;
74         int ret;
75
76         ldb = schannel_db_connect(mem_ctx);
77         if (ldb == NULL) {
78                 return NT_STATUS_NO_MEMORY;
79         }
80
81         f = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->negotiate_flags);
82
83         if (f == NULL) {
84                 talloc_free(ldb);
85                 return NT_STATUS_NO_MEMORY;
86         }
87
88         sct = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->secure_channel_type);
89
90         if (sct == NULL) {
91                 talloc_free(ldb);
92                 return NT_STATUS_NO_MEMORY;
93         }
94
95         msg = ldb_msg_new(ldb);
96         if (msg == NULL) {
97                 talloc_free(ldb);
98                 return NT_STATUS_NO_MEMORY;
99         }
100
101         msg->dn = ldb_dn_build_child(msg, "computerName", creds->computer_name, NULL);
102         if (msg->dn == NULL) {
103                 talloc_free(ldb);
104                 return NT_STATUS_NO_MEMORY;
105         }
106
107         val.data = creds->session_key;
108         val.length = sizeof(creds->session_key);
109
110         seed.data = creds->seed.data;
111         seed.length = sizeof(creds->seed.data);
112
113         ldb_msg_add_string(msg, "objectClass", "schannelState");
114         ldb_msg_add_value(msg, "sessionKey", &val);
115         ldb_msg_add_value(msg, "seed", &seed);
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                 talloc_free(ldb);
131                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
132         }
133
134         talloc_free(ldb);
135
136         return NT_STATUS_OK;
137 }
138
139
140 /*
141   read back a credentials back for a computer
142 */
143 NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
144                                     const char *computer_name, 
145                                     const char *domain,
146                                     struct creds_CredentialState **creds)
147 {
148         struct ldb_context *ldb;
149         struct ldb_message **res;
150         int ret;
151         const struct ldb_val *val;
152         char *expr=NULL;
153
154         *creds = talloc_zero(mem_ctx, struct creds_CredentialState);
155         if (!*creds) {
156                 return NT_STATUS_NO_MEMORY;
157         }
158
159         ldb = schannel_db_connect(mem_ctx);
160         if (ldb == NULL) {
161                 return NT_STATUS_NO_MEMORY;
162         }
163
164         expr = talloc_asprintf(mem_ctx, "(&(computerName=%s)(flatname=%s))", computer_name, domain);
165         if (expr == NULL) {
166                 talloc_free(ldb);
167                 return NT_STATUS_NO_MEMORY;
168         }
169
170         ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
171         if (ret != 1) {
172                 talloc_free(ldb);
173                 return NT_STATUS_INVALID_HANDLE;
174         }
175
176         val = ldb_msg_find_ldb_val(res[0], "sessionKey");
177         if (val == NULL || val->length != 16) {
178                 DEBUG(1,("schannel: record in schannel DB must contain a sessionKey of length 16, when searching for client: %s\n", computer_name));
179                 talloc_free(ldb);
180                 return NT_STATUS_INTERNAL_ERROR;
181         }
182
183         memcpy((*creds)->session_key, val->data, 16);
184
185         val = ldb_msg_find_ldb_val(res[0], "seed");
186         if (val == NULL || val->length != 8) {
187                 DEBUG(1,("schannel: record in schannel DB must contain a vaid seed of length 8, when searching for client: %s\n", computer_name));
188                 talloc_free(ldb);
189                 return NT_STATUS_INTERNAL_ERROR;
190         }
191
192         memcpy((*creds)->seed.data, val->data, 8);
193
194         (*creds)->negotiate_flags = ldb_msg_find_int(res[0], "negotiateFlags", 0);
195
196         (*creds)->secure_channel_type = ldb_msg_find_int(res[0], "secureChannelType", 0);
197
198         (*creds)->account_name = talloc_reference(*creds, ldb_msg_find_string(res[0], "accountName", NULL));
199
200         (*creds)->computer_name = talloc_reference(*creds, ldb_msg_find_string(res[0], "computerName", NULL));
201
202         (*creds)->domain = talloc_reference(*creds, ldb_msg_find_string(res[0], "flatname", NULL));
203
204         (*creds)->sid = samdb_result_dom_sid(*creds, res[0], "objectSid");
205
206         talloc_free(ldb);
207
208         return NT_STATUS_OK;
209 }