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"
29 /* a reasonable amount of time to keep credentials live */
30 #define SCHANNEL_CREDENTIALS_EXPIRY 600
33 connect to the schannel ldb
35 static struct ldb_context *schannel_db_connect(TALLOC_CTX *mem_ctx)
38 struct ldb_context *ldb;
40 path = smbd_tmp_path(mem_ctx, "schannel.ldb");
45 ldb = ldb_wrap_connect(mem_ctx, path, 0, NULL);
55 remember an established session key for a netr server authentication
56 use a simple ldb structure
58 NTSTATUS schannel_store_session_key(TALLOC_CTX *mem_ctx,
59 struct creds_CredentialState *creds)
61 struct ldb_context *ldb;
62 struct ldb_message *msg;
63 struct ldb_val val, seed;
68 time_t expiry = time(NULL) + SCHANNEL_CREDENTIALS_EXPIRY;
71 ldb = schannel_db_connect(mem_ctx);
73 return NT_STATUS_NO_MEMORY;
76 s = talloc_asprintf(mem_ctx, "%u", (unsigned int)expiry);
80 return NT_STATUS_NO_MEMORY;
83 f = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->negotiate_flags);
87 return NT_STATUS_NO_MEMORY;
90 sct = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->secure_channel_type);
94 return NT_STATUS_NO_MEMORY;
97 rid = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->rid);
101 return NT_STATUS_NO_MEMORY;
104 msg = ldb_msg_new(mem_ctx);
107 return NT_STATUS_NO_MEMORY;
110 msg->dn = talloc_asprintf(msg, "computerName=%s", creds->computer_name);
111 if (msg->dn == NULL) {
114 return NT_STATUS_NO_MEMORY;
117 val.data = creds->session_key;
118 val.length = sizeof(creds->session_key);
120 seed.data = creds->seed.data;
121 seed.length = sizeof(creds->seed.data);
123 ldb_msg_add_value(ldb, msg, "sessionKey", &val);
124 ldb_msg_add_value(ldb, msg, "seed", &seed);
125 ldb_msg_add_string(ldb, msg, "expiry", s);
126 ldb_msg_add_string(ldb, msg, "negotiateFlags", f);
127 ldb_msg_add_string(ldb, msg, "secureChannelType", sct);
128 ldb_msg_add_string(ldb, msg, "accountName", creds->account_name);
129 ldb_msg_add_string(ldb, msg, "computerName", creds->computer_name);
130 ldb_msg_add_string(ldb, msg, "flatname", creds->domain);
131 ldb_msg_add_string(ldb, msg, "rid", rid);
133 ldb_delete(ldb, msg->dn);
135 ret = ldb_add(ldb, msg);
140 DEBUG(0,("Unable to add %s to session key db - %s\n",
141 msg->dn, ldb_errstring(ldb)));
144 return NT_STATUS_INTERNAL_DB_CORRUPTION;
155 read back a credentials back for a computer
157 NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
158 const char *computer_name,
160 struct creds_CredentialState **creds)
162 struct ldb_context *ldb;
164 struct ldb_message **res;
166 const struct ldb_val *val;
169 *creds = talloc_zero(mem_ctx, struct creds_CredentialState);
171 return NT_STATUS_NO_MEMORY;
174 ldb = schannel_db_connect(mem_ctx);
176 return NT_STATUS_NO_MEMORY;
179 expr = talloc_asprintf(mem_ctx, "(&(computerName=%s)(flatname=%s))", computer_name, domain);
182 return NT_STATUS_NO_MEMORY;
185 ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
188 return NT_STATUS_INVALID_HANDLE;
191 expiry = ldb_msg_find_uint(res[0], "expiry", 0);
192 if (expiry < time(NULL)) {
193 DEBUG(1,("schannel: attempt to use expired session key for %s\n", computer_name));
195 return NT_STATUS_INVALID_HANDLE;
198 val = ldb_msg_find_ldb_val(res[0], "sessionKey");
199 if (val == NULL || val->length != 16) {
201 return NT_STATUS_INVALID_HANDLE;
204 memcpy((*creds)->session_key, val->data, 16);
206 val = ldb_msg_find_ldb_val(res[0], "seed");
207 if (val == NULL || val->length != 8) {
209 return NT_STATUS_INVALID_HANDLE;
212 memcpy((*creds)->seed.data, val->data, 8);
214 (*creds)->negotiate_flags = ldb_msg_find_int(res[0], "negotiateFlags", 0);
216 (*creds)->secure_channel_type = ldb_msg_find_int(res[0], "secureChannelType", 0);
218 (*creds)->account_name = talloc_reference(*creds, ldb_msg_find_string(res[0], "accountName", NULL));
220 (*creds)->computer_name = talloc_reference(*creds, ldb_msg_find_string(res[0], "computerName", NULL));
222 (*creds)->domain = talloc_reference(*creds, ldb_msg_find_string(res[0], "flatname", NULL));
224 (*creds)->rid = ldb_msg_find_uint(res[0], "rid", 0);