2 Unix SMB/CIFS implementation.
4 module to store/fetch session keys for the schannel server
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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.
24 #include "system/time.h"
25 #include "auth/auth.h"
26 #include "lib/ldb/include/ldb.h"
30 connect to the schannel ldb
32 static struct ldb_context *schannel_db_connect(TALLOC_CTX *mem_ctx)
35 struct ldb_context *ldb;
37 const char *init_ldif =
39 "computerName: CASE_INSENSITIVE\n" \
40 "flatname: CASE_INSENSITIVE\n";
42 path = smbd_tmp_path(mem_ctx, "schannel.ldb");
47 existed = file_exists(path);
49 ldb = ldb_wrap_connect(mem_ctx, path, LDB_FLG_NOSYNC, NULL);
56 gendb_add_ldif(ldb, init_ldif);
63 remember an established session key for a netr server authentication
64 use a simple ldb structure
66 NTSTATUS schannel_store_session_key(TALLOC_CTX *mem_ctx,
67 struct creds_CredentialState *creds)
69 struct ldb_context *ldb;
70 struct ldb_message *msg;
71 struct ldb_val val, seed;
76 ldb = schannel_db_connect(mem_ctx);
78 return NT_STATUS_NO_MEMORY;
81 f = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->negotiate_flags);
85 return NT_STATUS_NO_MEMORY;
88 sct = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->secure_channel_type);
92 return NT_STATUS_NO_MEMORY;
95 msg = ldb_msg_new(ldb);
98 return NT_STATUS_NO_MEMORY;
101 msg->dn = ldb_dn_build_child(msg, "computerName", creds->computer_name, NULL);
102 if (msg->dn == NULL) {
104 return NT_STATUS_NO_MEMORY;
107 val.data = creds->session_key;
108 val.length = sizeof(creds->session_key);
110 seed.data = creds->seed.data;
111 seed.length = sizeof(creds->seed.data);
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);
123 ldb_delete(ldb, msg->dn);
125 ret = ldb_add(ldb, msg);
128 DEBUG(0,("Unable to add %s to session key db - %s\n",
129 ldb_dn_linearize(msg, msg->dn), ldb_errstring(ldb)));
131 return NT_STATUS_INTERNAL_DB_CORRUPTION;
141 read back a credentials back for a computer
143 NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
144 const char *computer_name,
146 struct creds_CredentialState **creds)
148 struct ldb_context *ldb;
149 struct ldb_message **res;
151 const struct ldb_val *val;
154 *creds = talloc_zero(mem_ctx, struct creds_CredentialState);
156 return NT_STATUS_NO_MEMORY;
159 ldb = schannel_db_connect(mem_ctx);
161 return NT_STATUS_NO_MEMORY;
164 expr = talloc_asprintf(mem_ctx, "(&(computerName=%s)(flatname=%s))", computer_name, domain);
167 return NT_STATUS_NO_MEMORY;
170 ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
173 return NT_STATUS_INVALID_HANDLE;
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));
180 return NT_STATUS_INTERNAL_ERROR;
183 memcpy((*creds)->session_key, val->data, 16);
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));
189 return NT_STATUS_INTERNAL_ERROR;
192 memcpy((*creds)->seed.data, val->data, 8);
194 (*creds)->negotiate_flags = ldb_msg_find_int(res[0], "negotiateFlags", 0);
196 (*creds)->secure_channel_type = ldb_msg_find_int(res[0], "secureChannelType", 0);
198 (*creds)->account_name = talloc_reference(*creds, ldb_msg_find_string(res[0], "accountName", NULL));
200 (*creds)->computer_name = talloc_reference(*creds, ldb_msg_find_string(res[0], "computerName", NULL));
202 (*creds)->domain = talloc_reference(*creds, ldb_msg_find_string(res[0], "flatname", NULL));
204 (*creds)->sid = samdb_result_dom_sid(*creds, res[0], "objectSid");