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