]> git.samba.org - kamenim/samba.git/blob - source4/auth/gensec/schannel_state.c
r10913: This patch isn't as big as it looks ...
[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 "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         char *rid;
75         int ret;
76
77         ldb = schannel_db_connect(mem_ctx);
78         if (ldb == NULL) {
79                 return NT_STATUS_NO_MEMORY;
80         }
81
82         f = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->negotiate_flags);
83
84         if (f == NULL) {
85                 talloc_free(ldb);
86                 return NT_STATUS_NO_MEMORY;
87         }
88
89         sct = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->secure_channel_type);
90
91         if (sct == NULL) {
92                 talloc_free(ldb);
93                 return NT_STATUS_NO_MEMORY;
94         }
95
96         rid = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->rid);
97
98         if (rid == NULL) {
99                 talloc_free(ldb);
100                 return NT_STATUS_NO_MEMORY;
101         }
102
103         msg = ldb_msg_new(ldb);
104         if (msg == NULL) {
105                 talloc_free(ldb);
106                 return NT_STATUS_NO_MEMORY;
107         }
108
109         msg->dn = ldb_dn_build_child(msg, "computerName", creds->computer_name, NULL);
110         if (msg->dn == NULL) {
111                 talloc_free(ldb);
112                 return NT_STATUS_NO_MEMORY;
113         }
114
115         val.data = creds->session_key;
116         val.length = sizeof(creds->session_key);
117
118         seed.data = creds->seed.data;
119         seed.length = sizeof(creds->seed.data);
120
121         ldb_msg_add_string(msg, "objectClass", "schannelState");
122         ldb_msg_add_value(msg, "sessionKey", &val);
123         ldb_msg_add_value(msg, "seed", &seed);
124         ldb_msg_add_string(msg, "negotiateFlags", f);
125         ldb_msg_add_string(msg, "secureChannelType", sct);
126         ldb_msg_add_string(msg, "accountName", creds->account_name);
127         ldb_msg_add_string(msg, "computerName", creds->computer_name);
128         ldb_msg_add_string(msg, "flatname", creds->domain);
129         ldb_msg_add_string(msg, "rid", rid);
130
131         ldb_delete(ldb, msg->dn);
132
133         ret = ldb_add(ldb, msg);
134
135         if (ret != 0) {
136                 DEBUG(0,("Unable to add %s to session key db - %s\n", 
137                          ldb_dn_linearize(msg, msg->dn), ldb_errstring(ldb)));
138                 talloc_free(ldb);
139                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
140         }
141
142         talloc_free(ldb);
143
144         return NT_STATUS_OK;
145 }
146
147
148 /*
149   read back a credentials back for a computer
150 */
151 NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
152                                     const char *computer_name, 
153                                     const char *domain,
154                                     struct creds_CredentialState **creds)
155 {
156         struct ldb_context *ldb;
157         struct ldb_message **res;
158         int ret;
159         const struct ldb_val *val;
160         char *expr=NULL;
161
162         *creds = talloc_zero(mem_ctx, struct creds_CredentialState);
163         if (!*creds) {
164                 return NT_STATUS_NO_MEMORY;
165         }
166
167         ldb = schannel_db_connect(mem_ctx);
168         if (ldb == NULL) {
169                 return NT_STATUS_NO_MEMORY;
170         }
171
172         expr = talloc_asprintf(mem_ctx, "(&(computerName=%s)(flatname=%s))", computer_name, domain);
173         if (expr == NULL) {
174                 talloc_free(ldb);
175                 return NT_STATUS_NO_MEMORY;
176         }
177
178         ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
179         if (ret != 1) {
180                 talloc_free(ldb);
181                 return NT_STATUS_INVALID_HANDLE;
182         }
183
184         val = ldb_msg_find_ldb_val(res[0], "sessionKey");
185         if (val == NULL || val->length != 16) {
186                 DEBUG(1,("schannel: record in schannel DB must contain a sessionKey of length 16, when searching for client: %s\n", computer_name));
187                 talloc_free(ldb);
188                 return NT_STATUS_INTERNAL_ERROR;
189         }
190
191         memcpy((*creds)->session_key, val->data, 16);
192
193         val = ldb_msg_find_ldb_val(res[0], "seed");
194         if (val == NULL || val->length != 8) {
195                 DEBUG(1,("schannel: record in schannel DB must contain a vaid seed of length 8, when searching for client: %s\n", computer_name));
196                 talloc_free(ldb);
197                 return NT_STATUS_INTERNAL_ERROR;
198         }
199
200         memcpy((*creds)->seed.data, val->data, 8);
201
202         (*creds)->negotiate_flags = ldb_msg_find_int(res[0], "negotiateFlags", 0);
203
204         (*creds)->secure_channel_type = ldb_msg_find_int(res[0], "secureChannelType", 0);
205
206         (*creds)->account_name = talloc_reference(*creds, ldb_msg_find_string(res[0], "accountName", NULL));
207
208         (*creds)->computer_name = talloc_reference(*creds, ldb_msg_find_string(res[0], "computerName", NULL));
209
210         (*creds)->domain = talloc_reference(*creds, ldb_msg_find_string(res[0], "flatname", NULL));
211
212         (*creds)->rid = ldb_msg_find_uint(res[0], "rid", 0);
213
214         talloc_free(ldb);
215
216         return NT_STATUS_OK;
217 }