f4190f21b46e72c87425b74f577588dbd286d796
[abartlet/samba.git/.git] / source4 / rpc_server / netlogon / schannel_state.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    module to store/fetch session keys for the schannel server
5
6    Copyright (C) Andrew Tridgell 2004
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24
25 /* a reasonable amount of time to keep credentials live */
26 #define SCHANNEL_CREDENTIALS_EXPIRY 600
27
28 /*
29   connect to the schannel ldb
30 */
31 static struct ldb_context *schannel_db_connect(TALLOC_CTX *mem_ctx)
32 {
33         char *path;
34         struct ldb_context *ldb;
35
36         path = lock_path(mem_ctx, "schannel.ldb");
37         if (!path) {
38                 return NULL;
39         }
40         
41         ldb = ldb_connect(path, 0, NULL);
42         if (!ldb) {
43                 return NULL;
44         }
45
46         ldb_set_alloc(ldb, talloc_ldb_alloc, mem_ctx);
47         
48         return ldb;
49 }
50
51 /*
52   remember an established session key for a netr server authentication
53   use a simple ldb structure
54 */
55 NTSTATUS schannel_store_session_key(TALLOC_CTX *mem_ctx,
56                                     const char *computer_name, struct creds_CredentialState *creds)
57 {
58         struct ldb_context *ldb;
59         struct ldb_message msg;
60         struct ldb_val val;
61         char *s = NULL;
62         time_t expiry = time(NULL) + SCHANNEL_CREDENTIALS_EXPIRY;
63         int ret;
64
65         ldb = schannel_db_connect(mem_ctx);
66         if (ldb == NULL) {
67                 return NT_STATUS_NO_MEMORY;
68         }
69
70         asprintf(&s, "%u", (unsigned int)expiry);
71
72         if (s == NULL) {
73                 ldb_close(ldb);
74                 return NT_STATUS_NO_MEMORY;
75         }
76
77
78         ZERO_STRUCT(msg);
79         msg.dn = talloc_strdup(mem_ctx, computer_name);
80         if (msg.dn == NULL) {
81                 ldb_close(ldb);
82                 return NT_STATUS_NO_MEMORY;
83         }
84
85         val.data = creds->session_key;
86         val.length = sizeof(creds->session_key);
87
88         ldb_msg_add_value(ldb, &msg, "sessionKey", &val);
89         ldb_msg_add_string(ldb, &msg, "expiry", s);
90
91         ret = ldb_add(ldb, &msg);
92         ldb_close(ldb);
93
94         if (ret != 0) {
95                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
96         }
97
98         return NT_STATUS_OK;
99 }
100
101
102 /*
103   read back a session key for a computer
104 */
105 NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
106                                     const char *computer_name, uint8_t session_key[16])
107 {
108         struct ldb_context *ldb;
109         time_t expiry;
110         struct ldb_message **res;
111         int ret;
112         const struct ldb_val *val;
113
114         ldb = schannel_db_connect(mem_ctx);
115         if (ldb == NULL) {
116                 return NT_STATUS_NO_MEMORY;
117         }
118
119         ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, "(dn=%s)", NULL, &res);
120         if (ret != 1) {
121                 ldb_close(ldb);
122                 return NT_STATUS_INVALID_HANDLE;
123         }
124
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));
128                 ldb_close(ldb);
129                 return NT_STATUS_INVALID_HANDLE;
130         }
131
132         val = ldb_msg_find_ldb_val(res[0], "sessionKey");
133         if (val == NULL || val->length != 16) {
134                 ldb_close(ldb);
135                 return NT_STATUS_INVALID_HANDLE;
136         }
137
138         memcpy(session_key, val->data, 16);
139
140         ldb_close(ldb);
141
142         return NT_STATUS_OK;
143 }