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"
28 /* a reasonable amount of time to keep credentials live */
29 #define SCHANNEL_CREDENTIALS_EXPIRY 600
32 connect to the schannel ldb
34 static struct ldb_wrap *schannel_db_connect(TALLOC_CTX *mem_ctx)
39 path = smbd_tmp_path(mem_ctx, "schannel.ldb");
44 ldb = ldb_wrap_connect(mem_ctx, path, 0, NULL);
54 remember an established session key for a netr server authentication
55 use a simple ldb structure
57 NTSTATUS schannel_store_session_key(TALLOC_CTX *mem_ctx,
58 struct creds_CredentialState *creds)
61 struct ldb_message *msg;
62 struct ldb_val val, seed;
66 time_t expiry = time(NULL) + SCHANNEL_CREDENTIALS_EXPIRY;
69 ldb = schannel_db_connect(mem_ctx);
71 return NT_STATUS_NO_MEMORY;
74 s = talloc_asprintf(mem_ctx, "%u", (unsigned int)expiry);
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(mem_ctx);
98 return NT_STATUS_NO_MEMORY;
101 msg->dn = talloc_asprintf(msg, "computerName=%s", creds->computer_name);
102 if (msg->dn == NULL) {
105 return NT_STATUS_NO_MEMORY;
108 val.data = creds->session_key;
109 val.length = sizeof(creds->session_key);
111 seed.data = creds->seed.data;
112 seed.length = sizeof(creds->seed.data);
114 ldb_msg_add_value(ldb->ldb, msg, "sessionKey", &val);
115 ldb_msg_add_value(ldb->ldb, msg, "seed", &seed);
116 ldb_msg_add_string(ldb->ldb, msg, "expiry", s);
117 ldb_msg_add_string(ldb->ldb, msg, "negotiateFlags", f);
118 ldb_msg_add_string(ldb->ldb, msg, "secureChannelType", sct);
119 ldb_msg_add_string(ldb->ldb, msg, "accountName", creds->account_name);
120 ldb_msg_add_string(ldb->ldb, msg, "computerName", creds->computer_name);
122 ldb_delete(ldb->ldb, msg->dn);
124 ret = ldb_add(ldb->ldb, msg);
129 DEBUG(0,("Unable to add %s to session key db - %s\n",
130 msg->dn, ldb_errstring(ldb->ldb)));
133 return NT_STATUS_INTERNAL_DB_CORRUPTION;
144 read back a credentials back for a computer
146 NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
147 const char *computer_name,
148 struct creds_CredentialState **creds)
150 struct ldb_wrap *ldb;
152 struct ldb_message **res;
154 const struct ldb_val *val;
157 *creds = talloc_zero(mem_ctx, struct creds_CredentialState);
159 return NT_STATUS_NO_MEMORY;
162 ldb = schannel_db_connect(mem_ctx);
164 return NT_STATUS_NO_MEMORY;
167 expr = talloc_asprintf(mem_ctx, "(dn=computerName=%s)", computer_name);
170 return NT_STATUS_NO_MEMORY;
173 ret = ldb_search(ldb->ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
176 return NT_STATUS_INVALID_HANDLE;
179 expiry = ldb_msg_find_uint(res[0], "expiry", 0);
180 if (expiry < time(NULL)) {
181 DEBUG(1,("schannel: attempt to use expired session key for %s\n", computer_name));
183 return NT_STATUS_INVALID_HANDLE;
186 val = ldb_msg_find_ldb_val(res[0], "sessionKey");
187 if (val == NULL || val->length != 16) {
189 return NT_STATUS_INVALID_HANDLE;
192 memcpy((*creds)->session_key, val->data, 16);
194 val = ldb_msg_find_ldb_val(res[0], "seed");
195 if (val == NULL || val->length != 8) {
197 return NT_STATUS_INVALID_HANDLE;
200 memcpy((*creds)->seed.data, val->data, 8);
202 (*creds)->negotiate_flags = ldb_msg_find_int(res[0], "negotiateFlags", 0);
204 (*creds)->secure_channel_type = ldb_msg_find_int(res[0], "secureChannelType", 0);
206 (*creds)->account_name = talloc_reference(*creds, ldb_msg_find_string(res[0], "accountName", NULL));
208 (*creds)->computer_name = talloc_reference(*creds, ldb_msg_find_string(res[0], "computerName", NULL));