a0bffcce065cadc7371d8697a3d49a66bcc654e9
[jelmer/samba4-debian.git] / source / 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 #include "system/time.h"
25 #include "auth/auth.h"
26 #include "lib/ldb/include/ldb.h"
27
28 /* a reasonable amount of time to keep credentials live */
29 #define SCHANNEL_CREDENTIALS_EXPIRY 600
30
31 /*
32   connect to the schannel ldb
33 */
34 static struct ldb_wrap *schannel_db_connect(TALLOC_CTX *mem_ctx)
35 {
36         char *path;
37         struct ldb_wrap *ldb;
38
39         path = smbd_tmp_path(mem_ctx, "schannel.ldb");
40         if (!path) {
41                 return NULL;
42         }
43         
44         ldb = ldb_wrap_connect(mem_ctx, path, 0, NULL);
45         talloc_free(path);
46         if (!ldb) {
47                 return NULL;
48         }
49
50         return ldb;
51 }
52
53 /*
54   remember an established session key for a netr server authentication
55   use a simple ldb structure
56 */
57 NTSTATUS schannel_store_session_key(TALLOC_CTX *mem_ctx,
58                                     struct creds_CredentialState *creds)
59 {
60         struct ldb_wrap *ldb;
61         struct ldb_message *msg;
62         struct ldb_val val, seed;
63         char *s;
64         char *f;
65         char *sct;
66         time_t expiry = time(NULL) + SCHANNEL_CREDENTIALS_EXPIRY;
67         int ret;
68
69         ldb = schannel_db_connect(mem_ctx);
70         if (ldb == NULL) {
71                 return NT_STATUS_NO_MEMORY;
72         }
73
74         s = talloc_asprintf(mem_ctx, "%u", (unsigned int)expiry);
75
76         if (s == NULL) {
77                 talloc_free(ldb);
78                 return NT_STATUS_NO_MEMORY;
79         }
80
81         f = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->negotiate_flags);
82
83         if (f == NULL) {
84                 talloc_free(ldb);
85                 return NT_STATUS_NO_MEMORY;
86         }
87
88         sct = talloc_asprintf(mem_ctx, "%u", (unsigned int)creds->secure_channel_type);
89
90         if (sct == NULL) {
91                 talloc_free(ldb);
92                 return NT_STATUS_NO_MEMORY;
93         }
94
95         msg = ldb_msg_new(mem_ctx);
96         if (msg == NULL) {
97                 talloc_free(ldb);
98                 return NT_STATUS_NO_MEMORY;
99         }
100
101         msg->dn = talloc_asprintf(msg, "computerName=%s", creds->computer_name);
102         if (msg->dn == NULL) {
103                 talloc_free(ldb);
104                 talloc_free(msg);
105                 return NT_STATUS_NO_MEMORY;
106         }
107
108         val.data = creds->session_key;
109         val.length = sizeof(creds->session_key);
110
111         seed.data = creds->seed.data;
112         seed.length = sizeof(creds->seed.data);
113
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);
121
122         ldb_delete(ldb->ldb, msg->dn);
123
124         ret = ldb_add(ldb->ldb, msg);
125
126         talloc_free(s);
127
128         if (ret != 0) {
129                 DEBUG(0,("Unable to add %s to session key db - %s\n", 
130                          msg->dn, ldb_errstring(ldb->ldb)));
131                 talloc_free(ldb);
132                 talloc_free(msg);
133                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
134         }
135
136         talloc_free(msg);
137         talloc_free(ldb);
138
139         return NT_STATUS_OK;
140 }
141
142
143 /*
144   read back a credentials back for a computer
145 */
146 NTSTATUS schannel_fetch_session_key(TALLOC_CTX *mem_ctx,
147                                     const char *computer_name, 
148                                     struct creds_CredentialState **creds)
149 {
150         struct ldb_wrap *ldb;
151         time_t expiry;
152         struct ldb_message **res;
153         int ret;
154         const struct ldb_val *val;
155         char *expr=NULL;
156
157         *creds = talloc_zero(mem_ctx, struct creds_CredentialState);
158         if (!*creds) {
159                 return NT_STATUS_NO_MEMORY;
160         }
161
162         ldb = schannel_db_connect(mem_ctx);
163         if (ldb == NULL) {
164                 return NT_STATUS_NO_MEMORY;
165         }
166
167         expr = talloc_asprintf(mem_ctx, "(dn=computerName=%s)", computer_name);
168         if (expr == NULL) {
169                 talloc_free(ldb);
170                 return NT_STATUS_NO_MEMORY;
171         }
172
173         ret = ldb_search(ldb->ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res);
174         if (ret != 1) {
175                 talloc_free(ldb);
176                 return NT_STATUS_INVALID_HANDLE;
177         }
178
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));
182                 talloc_free(ldb);
183                 return NT_STATUS_INVALID_HANDLE;
184         }
185
186         val = ldb_msg_find_ldb_val(res[0], "sessionKey");
187         if (val == NULL || val->length != 16) {
188                 talloc_free(ldb);
189                 return NT_STATUS_INVALID_HANDLE;
190         }
191
192         memcpy((*creds)->session_key, val->data, 16);
193
194         val = ldb_msg_find_ldb_val(res[0], "seed");
195         if (val == NULL || val->length != 8) {
196                 talloc_free(ldb);
197                 return NT_STATUS_INVALID_HANDLE;
198         }
199
200         memcpy((*creds)->seed.data, val->data, 8);
201
202         (*creds)->negotiate_flags = ldb_msg_find_int(res[0], "negotiateFlags", 0);
203
204         (*creds)->secure_channel_type = ldb_msg_find_int(res[0], "secureChannelType", 0);
205
206         (*creds)->account_name = talloc_reference(*creds, ldb_msg_find_string(res[0], "accountName", NULL));
207
208         (*creds)->computer_name = talloc_reference(*creds, ldb_msg_find_string(res[0], "computerName", NULL));
209
210         talloc_free(ldb);
211
212         return NT_STATUS_OK;
213 }