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.
25 /* a reasonable amount of time to keep credentials live */
26 #define SCHANNEL_CREDENTIALS_EXPIRY 600
29 connect to the schannel ldb
31 static struct ldb_context *schannel_db_connect(TALLOC_CTX *mem_ctx)
34 struct ldb_context *ldb;
36 path = lock_path(mem_ctx, "schannel.ldb");
41 ldb = ldb_connect(path, 0, NULL);
46 ldb_set_alloc(ldb, talloc_ldb_alloc, mem_ctx);
52 remember an established session key for a netr server authentication
53 use a simple ldb structure
55 NTSTATUS schannel_store_session_key(TALLOC_CTX *mem_ctx,
56 const char *computer_name, struct creds_CredentialState *creds)
58 struct ldb_context *ldb;
59 struct ldb_message msg;
62 time_t expiry = time(NULL) + SCHANNEL_CREDENTIALS_EXPIRY;
65 ldb = schannel_db_connect(mem_ctx);
67 return NT_STATUS_NO_MEMORY;
70 asprintf(&s, "%u", (unsigned int)expiry);
74 return NT_STATUS_NO_MEMORY;
79 msg.dn = talloc_strdup(mem_ctx, computer_name);
82 return NT_STATUS_NO_MEMORY;
85 val.data = creds->session_key;
86 val.length = sizeof(creds->session_key);
88 ldb_msg_add_value(ldb, &msg, "sessionKey", &val);
89 ldb_msg_add_string(ldb, &msg, "expiry", s);
91 ret = ldb_add(ldb, &msg);
95 return NT_STATUS_INTERNAL_DB_CORRUPTION;
103 read back a session key for a computer
105 NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
106 const char *computer_name, uint8_t session_key[16])
108 struct ldb_context *ldb;
110 struct ldb_message **res;
112 const struct ldb_val *val;
114 ldb = schannel_db_connect(mem_ctx);
116 return NT_STATUS_NO_MEMORY;
119 ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, "(dn=%s)", NULL, &res);
122 return NT_STATUS_INVALID_HANDLE;
125 expiry = ldb_msg_find_uint(res[0], "expiry", 0);
126 if (expiry < time(NULL)) {
127 DEBUG(1,("schannel: attempt to use expired session key for %s\n", computer_name));
129 return NT_STATUS_INVALID_HANDLE;
132 val = ldb_msg_find_ldb_val(res[0], "sessionKey");
133 if (val == NULL || val->length != 16) {
135 return NT_STATUS_INVALID_HANDLE;
138 memcpy(session_key, val->data, 16);