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_realloc_fn, 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,
57 struct creds_CredentialState *creds)
59 struct ldb_context *ldb;
60 struct ldb_message msg;
61 struct ldb_val val, seed;
63 time_t expiry = time(NULL) + SCHANNEL_CREDENTIALS_EXPIRY;
66 ldb = schannel_db_connect(mem_ctx);
68 return NT_STATUS_NO_MEMORY;
71 asprintf(&s, "%u", (unsigned int)expiry);
75 return NT_STATUS_NO_MEMORY;
80 msg.dn = talloc_strdup(mem_ctx, computer_name);
83 return NT_STATUS_NO_MEMORY;
86 val.data = creds->session_key;
87 val.length = sizeof(creds->session_key);
89 seed.data = creds->seed.data;
90 seed.length = sizeof(creds->seed.data);
92 ldb_msg_add_value(ldb, &msg, "sessionKey", &val);
93 ldb_msg_add_value(ldb, &msg, "seed", &seed);
94 ldb_msg_add_string(ldb, &msg, "expiry", s);
96 ldb_delete(ldb, msg.dn);
98 ret = ldb_add(ldb, &msg);
101 DEBUG(0,("Unable to add %s to session key db - %s\n", msg.dn, ldb_errstring(ldb)));
103 return NT_STATUS_INTERNAL_DB_CORRUPTION;
112 read back a credentials back for a computer
114 NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
115 const char *computer_name,
116 struct creds_CredentialState *creds)
118 struct ldb_context *ldb;
120 struct ldb_message **res;
122 const struct ldb_val *val;
127 ldb = schannel_db_connect(mem_ctx);
129 return NT_STATUS_NO_MEMORY;
132 expr = talloc_asprintf(mem_ctx, "(dn=%s)", computer_name);
135 return NT_STATUS_NO_MEMORY;
138 ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
141 return NT_STATUS_INVALID_HANDLE;
144 expiry = ldb_msg_find_uint(res[0], "expiry", 0);
145 if (expiry < time(NULL)) {
146 DEBUG(1,("schannel: attempt to use expired session key for %s\n", computer_name));
148 return NT_STATUS_INVALID_HANDLE;
151 val = ldb_msg_find_ldb_val(res[0], "sessionKey");
152 if (val == NULL || val->length != 16) {
154 return NT_STATUS_INVALID_HANDLE;
157 memcpy(creds->session_key, val->data, 16);
159 val = ldb_msg_find_ldb_val(res[0], "seed");
160 if (val == NULL || val->length != 8) {
162 return NT_STATUS_INVALID_HANDLE;
165 memcpy(creds->seed.data, val->data, 8);