r2792: got rid of talloc_ldb_alloc() and instead created talloc_realloc_fn(),
[bbaumbach/samba-autobuild/.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_realloc_fn, 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, 
57                                     struct creds_CredentialState *creds)
58 {
59         struct ldb_context *ldb;
60         struct ldb_message msg;
61         struct ldb_val val, seed;
62         char *s = NULL;
63         time_t expiry = time(NULL) + SCHANNEL_CREDENTIALS_EXPIRY;
64         int ret;
65
66         ldb = schannel_db_connect(mem_ctx);
67         if (ldb == NULL) {
68                 return NT_STATUS_NO_MEMORY;
69         }
70
71         asprintf(&s, "%u", (unsigned int)expiry);
72
73         if (s == NULL) {
74                 ldb_close(ldb);
75                 return NT_STATUS_NO_MEMORY;
76         }
77
78
79         ZERO_STRUCT(msg);
80         msg.dn = talloc_strdup(mem_ctx, computer_name);
81         if (msg.dn == NULL) {
82                 ldb_close(ldb);
83                 return NT_STATUS_NO_MEMORY;
84         }
85
86         val.data = creds->session_key;
87         val.length = sizeof(creds->session_key);
88
89         seed.data = creds->seed.data;
90         seed.length = sizeof(creds->seed.data);
91
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);
95
96         ldb_delete(ldb, msg.dn);
97
98         ret = ldb_add(ldb, &msg);
99
100         if (ret != 0) {
101                 DEBUG(0,("Unable to add %s to session key db - %s\n", msg.dn, ldb_errstring(ldb)));
102                 ldb_close(ldb);
103                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
104         }
105
106         ldb_close(ldb);
107         return NT_STATUS_OK;
108 }
109
110
111 /*
112   read back a credentials back for a computer
113 */
114 NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
115                                     const char *computer_name, 
116                                     struct creds_CredentialState *creds)
117 {
118         struct ldb_context *ldb;
119         time_t expiry;
120         struct ldb_message **res;
121         int ret;
122         const struct ldb_val *val;
123         char *expr=NULL;
124
125         ZERO_STRUCTP(creds);
126
127         ldb = schannel_db_connect(mem_ctx);
128         if (ldb == NULL) {
129                 return NT_STATUS_NO_MEMORY;
130         }
131
132         expr = talloc_asprintf(mem_ctx, "(dn=%s)", computer_name);
133         if (expr == NULL) {
134                 ldb_close(ldb);
135                 return NT_STATUS_NO_MEMORY;
136         }
137
138         ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
139         if (ret != 1) {
140                 ldb_close(ldb);
141                 return NT_STATUS_INVALID_HANDLE;
142         }
143
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));
147                 ldb_close(ldb);
148                 return NT_STATUS_INVALID_HANDLE;
149         }
150
151         val = ldb_msg_find_ldb_val(res[0], "sessionKey");
152         if (val == NULL || val->length != 16) {
153                 ldb_close(ldb);
154                 return NT_STATUS_INVALID_HANDLE;
155         }
156
157         memcpy(creds->session_key, val->data, 16);
158
159         val = ldb_msg_find_ldb_val(res[0], "seed");
160         if (val == NULL || val->length != 8) {
161                 ldb_close(ldb);
162                 return NT_STATUS_INVALID_HANDLE;
163         }
164
165         memcpy(creds->seed.data, val->data, 8);
166
167         ldb_close(ldb);
168
169         return NT_STATUS_OK;
170 }