1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | Copyright (C) Stefan Metzmacher 2014
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "system/filesys.h"
|
---|
22 | #include <tevent.h>
|
---|
23 | #include "smbd/smbd.h"
|
---|
24 | #include "smbd/globals.h"
|
---|
25 | #include "dbwrap/dbwrap.h"
|
---|
26 | #include "dbwrap/dbwrap_rbt.h"
|
---|
27 | #include "dbwrap/dbwrap_open.h"
|
---|
28 | #include "dbwrap/dbwrap_watch.h"
|
---|
29 | #include "session.h"
|
---|
30 | #include "auth.h"
|
---|
31 | #include "auth/gensec/gensec.h"
|
---|
32 | #include "../lib/tsocket/tsocket.h"
|
---|
33 | #include "../libcli/security/security.h"
|
---|
34 | #include "messages.h"
|
---|
35 | #include "lib/util/util_tdb.h"
|
---|
36 | #include "librpc/gen_ndr/ndr_smbXsrv.h"
|
---|
37 | #include "serverid.h"
|
---|
38 | #include "lib/util/tevent_ntstatus.h"
|
---|
39 | #include "lib/util/iov_buf.h"
|
---|
40 |
|
---|
41 | struct smbXsrv_client_table {
|
---|
42 | struct {
|
---|
43 | uint32_t max_clients;
|
---|
44 | uint32_t num_clients;
|
---|
45 | } local;
|
---|
46 | struct {
|
---|
47 | struct db_context *db_ctx;
|
---|
48 | } global;
|
---|
49 | };
|
---|
50 |
|
---|
51 | static struct db_context *smbXsrv_client_global_db_ctx = NULL;
|
---|
52 |
|
---|
53 | NTSTATUS smbXsrv_client_global_init(void)
|
---|
54 | {
|
---|
55 | const char *global_path = NULL;
|
---|
56 | struct db_context *db_ctx = NULL;
|
---|
57 |
|
---|
58 | if (smbXsrv_client_global_db_ctx != NULL) {
|
---|
59 | return NT_STATUS_OK;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * This contains secret information like client keys!
|
---|
64 | */
|
---|
65 | global_path = lock_path("smbXsrv_client_global.tdb");
|
---|
66 | if (global_path == NULL) {
|
---|
67 | return NT_STATUS_NO_MEMORY;
|
---|
68 | }
|
---|
69 |
|
---|
70 | db_ctx = db_open(NULL, global_path,
|
---|
71 | 0, /* hash_size */
|
---|
72 | TDB_DEFAULT |
|
---|
73 | TDB_CLEAR_IF_FIRST |
|
---|
74 | TDB_INCOMPATIBLE_HASH,
|
---|
75 | O_RDWR | O_CREAT, 0600,
|
---|
76 | DBWRAP_LOCK_ORDER_1,
|
---|
77 | DBWRAP_FLAG_NONE);
|
---|
78 | if (db_ctx == NULL) {
|
---|
79 | NTSTATUS status;
|
---|
80 |
|
---|
81 | status = map_nt_error_from_unix_common(errno);
|
---|
82 |
|
---|
83 | return status;
|
---|
84 | }
|
---|
85 |
|
---|
86 | smbXsrv_client_global_db_ctx = db_ctx;
|
---|
87 |
|
---|
88 | return NT_STATUS_OK;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * NOTE:
|
---|
93 | * We need to store the keys in big endian so that dbwrap_rbt's memcmp
|
---|
94 | * has the same result as integer comparison between the uint32_t
|
---|
95 | * values.
|
---|
96 | *
|
---|
97 | * TODO: implement string based key
|
---|
98 | */
|
---|
99 |
|
---|
100 | #define SMBXSRV_CLIENT_GLOBAL_TDB_KEY_SIZE 16
|
---|
101 |
|
---|
102 | static TDB_DATA smbXsrv_client_global_id_to_key(const struct GUID *client_guid,
|
---|
103 | uint8_t *key_buf)
|
---|
104 | {
|
---|
105 | TDB_DATA key = { .dsize = 0, };
|
---|
106 | NTSTATUS status;
|
---|
107 | DATA_BLOB b;
|
---|
108 |
|
---|
109 | status = GUID_to_ndr_blob(client_guid, talloc_tos(), &b);
|
---|
110 | if (!NT_STATUS_IS_OK(status)) {
|
---|
111 | return key;
|
---|
112 | }
|
---|
113 | memcpy(key_buf, b.data, SMBXSRV_CLIENT_GLOBAL_TDB_KEY_SIZE);
|
---|
114 | data_blob_free(&b);
|
---|
115 |
|
---|
116 | key = make_tdb_data(key_buf, SMBXSRV_CLIENT_GLOBAL_TDB_KEY_SIZE);
|
---|
117 |
|
---|
118 | return key;
|
---|
119 | }
|
---|
120 |
|
---|
121 | static struct db_record *smbXsrv_client_global_fetch_locked(
|
---|
122 | struct db_context *db,
|
---|
123 | const struct GUID *client_guid,
|
---|
124 | TALLOC_CTX *mem_ctx)
|
---|
125 | {
|
---|
126 | TDB_DATA key;
|
---|
127 | uint8_t key_buf[SMBXSRV_CLIENT_GLOBAL_TDB_KEY_SIZE];
|
---|
128 | struct db_record *rec = NULL;
|
---|
129 |
|
---|
130 | key = smbXsrv_client_global_id_to_key(client_guid, key_buf);
|
---|
131 |
|
---|
132 | rec = dbwrap_fetch_locked(db, mem_ctx, key);
|
---|
133 |
|
---|
134 | if (rec == NULL) {
|
---|
135 | DBG_DEBUG("Failed to lock guid [%s], key '%s'\n",
|
---|
136 | GUID_string(talloc_tos(), client_guid),
|
---|
137 | hex_encode_talloc(talloc_tos(), key.dptr, key.dsize));
|
---|
138 | }
|
---|
139 |
|
---|
140 | return rec;
|
---|
141 | }
|
---|
142 |
|
---|
143 | static NTSTATUS smbXsrv_client_table_create(TALLOC_CTX *mem_ctx,
|
---|
144 | struct messaging_context *msg_ctx,
|
---|
145 | uint32_t max_clients,
|
---|
146 | struct smbXsrv_client_table **_table)
|
---|
147 | {
|
---|
148 | struct smbXsrv_client_table *table;
|
---|
149 | NTSTATUS status;
|
---|
150 |
|
---|
151 | if (max_clients > 1) {
|
---|
152 | return NT_STATUS_INTERNAL_ERROR;
|
---|
153 | }
|
---|
154 |
|
---|
155 | table = talloc_zero(mem_ctx, struct smbXsrv_client_table);
|
---|
156 | if (table == NULL) {
|
---|
157 | return NT_STATUS_NO_MEMORY;
|
---|
158 | }
|
---|
159 |
|
---|
160 | table->local.max_clients = max_clients;
|
---|
161 |
|
---|
162 | status = smbXsrv_client_global_init();
|
---|
163 | if (!NT_STATUS_IS_OK(status)) {
|
---|
164 | TALLOC_FREE(table);
|
---|
165 | return status;
|
---|
166 | }
|
---|
167 |
|
---|
168 | table->global.db_ctx = smbXsrv_client_global_db_ctx;
|
---|
169 |
|
---|
170 | dbwrap_watch_db(table->global.db_ctx, msg_ctx);
|
---|
171 |
|
---|
172 | *_table = table;
|
---|
173 | return NT_STATUS_OK;
|
---|
174 | }
|
---|
175 |
|
---|
176 | static int smbXsrv_client_global_destructor(struct smbXsrv_client_global0 *global)
|
---|
177 | {
|
---|
178 | return 0;
|
---|
179 | }
|
---|
180 |
|
---|
181 | static void smbXsrv_client_global_verify_record(struct db_record *db_rec,
|
---|
182 | bool *is_free,
|
---|
183 | bool *was_free,
|
---|
184 | TALLOC_CTX *mem_ctx,
|
---|
185 | struct smbXsrv_client_global0 **_g)
|
---|
186 | {
|
---|
187 | TDB_DATA key;
|
---|
188 | TDB_DATA val;
|
---|
189 | DATA_BLOB blob;
|
---|
190 | struct smbXsrv_client_globalB global_blob;
|
---|
191 | enum ndr_err_code ndr_err;
|
---|
192 | struct smbXsrv_client_global0 *global = NULL;
|
---|
193 | bool exists;
|
---|
194 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
195 |
|
---|
196 | *is_free = false;
|
---|
197 |
|
---|
198 | if (was_free) {
|
---|
199 | *was_free = false;
|
---|
200 | }
|
---|
201 | if (_g) {
|
---|
202 | *_g = NULL;
|
---|
203 | }
|
---|
204 |
|
---|
205 | key = dbwrap_record_get_key(db_rec);
|
---|
206 |
|
---|
207 | val = dbwrap_record_get_value(db_rec);
|
---|
208 | if (val.dsize == 0) {
|
---|
209 | TALLOC_FREE(frame);
|
---|
210 | *is_free = true;
|
---|
211 | if (was_free) {
|
---|
212 | *was_free = true;
|
---|
213 | }
|
---|
214 | return;
|
---|
215 | }
|
---|
216 |
|
---|
217 | blob = data_blob_const(val.dptr, val.dsize);
|
---|
218 |
|
---|
219 | ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
|
---|
220 | (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_client_globalB);
|
---|
221 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
222 | NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
|
---|
223 | DBG_WARNING("smbXsrv_client_global_verify_record: "
|
---|
224 | "key '%s' ndr_pull_struct_blob - %s\n",
|
---|
225 | hex_encode_talloc(frame, key.dptr, key.dsize),
|
---|
226 | nt_errstr(status));
|
---|
227 | TALLOC_FREE(frame);
|
---|
228 | return;
|
---|
229 | }
|
---|
230 |
|
---|
231 | DBG_DEBUG("client_global:\n");
|
---|
232 | if (DEBUGLVL(DBGLVL_DEBUG)) {
|
---|
233 | NDR_PRINT_DEBUG(smbXsrv_client_globalB, &global_blob);
|
---|
234 | }
|
---|
235 |
|
---|
236 | if (global_blob.version != SMBXSRV_VERSION_0) {
|
---|
237 | DBG_ERR("key '%s' use unsupported version %u\n",
|
---|
238 | hex_encode_talloc(frame, key.dptr, key.dsize),
|
---|
239 | global_blob.version);
|
---|
240 | NDR_PRINT_DEBUG(smbXsrv_client_globalB, &global_blob);
|
---|
241 | TALLOC_FREE(frame);
|
---|
242 | return;
|
---|
243 | }
|
---|
244 |
|
---|
245 | global = global_blob.info.info0;
|
---|
246 |
|
---|
247 | exists = serverid_exists(&global->server_id);
|
---|
248 | if (!exists) {
|
---|
249 | struct server_id_buf tmp;
|
---|
250 |
|
---|
251 | DBG_NOTICE("key '%s' server_id %s does not exist.\n",
|
---|
252 | hex_encode_talloc(frame, key.dptr, key.dsize),
|
---|
253 | server_id_str_buf(global->server_id, &tmp));
|
---|
254 | if (DEBUGLVL(DBGLVL_NOTICE)) {
|
---|
255 | NDR_PRINT_DEBUG(smbXsrv_client_globalB, &global_blob);
|
---|
256 | }
|
---|
257 | TALLOC_FREE(frame);
|
---|
258 | dbwrap_record_delete(db_rec);
|
---|
259 | *is_free = true;
|
---|
260 | return;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (_g) {
|
---|
264 | *_g = talloc_move(mem_ctx, &global);
|
---|
265 | }
|
---|
266 | TALLOC_FREE(frame);
|
---|
267 | }
|
---|
268 |
|
---|
269 | NTSTATUS smb2srv_client_lookup_global(struct smbXsrv_client *client,
|
---|
270 | struct GUID client_guid,
|
---|
271 | TALLOC_CTX *mem_ctx,
|
---|
272 | struct smbXsrv_client_global0 **_global)
|
---|
273 | {
|
---|
274 | struct smbXsrv_client_table *table = client->table;
|
---|
275 | struct smbXsrv_client_global0 *global = NULL;
|
---|
276 | bool is_free = false;
|
---|
277 | struct db_record *db_rec;
|
---|
278 |
|
---|
279 | db_rec = smbXsrv_client_global_fetch_locked(table->global.db_ctx,
|
---|
280 | &client_guid,
|
---|
281 | talloc_tos());
|
---|
282 | if (db_rec == NULL) {
|
---|
283 | return NT_STATUS_INTERNAL_DB_ERROR;
|
---|
284 | }
|
---|
285 |
|
---|
286 | smbXsrv_client_global_verify_record(db_rec,
|
---|
287 | &is_free,
|
---|
288 | NULL,
|
---|
289 | mem_ctx,
|
---|
290 | &global);
|
---|
291 | TALLOC_FREE(db_rec);
|
---|
292 |
|
---|
293 | if (is_free) {
|
---|
294 | return NT_STATUS_OBJECTID_NOT_FOUND;
|
---|
295 | }
|
---|
296 |
|
---|
297 | *_global = global;
|
---|
298 | return NT_STATUS_OK;
|
---|
299 | }
|
---|
300 |
|
---|
301 | NTSTATUS smb2srv_client_connection_pass(struct smbd_smb2_request *smb2req,
|
---|
302 | struct smbXsrv_client_global0 *global)
|
---|
303 | {
|
---|
304 | DATA_BLOB blob;
|
---|
305 | enum ndr_err_code ndr_err;
|
---|
306 | NTSTATUS status;
|
---|
307 | struct smbXsrv_connection_pass0 pass_info0;
|
---|
308 | struct smbXsrv_connection_passB pass_blob;
|
---|
309 | ssize_t reqlen;
|
---|
310 | struct iovec iov;
|
---|
311 |
|
---|
312 | pass_info0.initial_connect_time = global->initial_connect_time;
|
---|
313 | pass_info0.client_guid = global->client_guid;
|
---|
314 |
|
---|
315 | reqlen = iov_buflen(smb2req->in.vector, smb2req->in.vector_count);
|
---|
316 | if (reqlen == -1) {
|
---|
317 | return NT_STATUS_INVALID_BUFFER_SIZE;
|
---|
318 | }
|
---|
319 |
|
---|
320 | pass_info0.negotiate_request.length = reqlen;
|
---|
321 | pass_info0.negotiate_request.data = talloc_array(talloc_tos(), uint8_t,
|
---|
322 | reqlen);
|
---|
323 | if (pass_info0.negotiate_request.data == NULL) {
|
---|
324 | return NT_STATUS_NO_MEMORY;
|
---|
325 | }
|
---|
326 | iov_buf(smb2req->in.vector, smb2req->in.vector_count,
|
---|
327 | pass_info0.negotiate_request.data,
|
---|
328 | pass_info0.negotiate_request.length);
|
---|
329 |
|
---|
330 | ZERO_STRUCT(pass_blob);
|
---|
331 | pass_blob.version = smbXsrv_version_global_current();
|
---|
332 | pass_blob.info.info0 = &pass_info0;
|
---|
333 |
|
---|
334 | if (DEBUGLVL(DBGLVL_DEBUG)) {
|
---|
335 | NDR_PRINT_DEBUG(smbXsrv_connection_passB, &pass_blob);
|
---|
336 | }
|
---|
337 |
|
---|
338 | ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &pass_blob,
|
---|
339 | (ndr_push_flags_fn_t)ndr_push_smbXsrv_connection_passB);
|
---|
340 | data_blob_free(&pass_info0.negotiate_request);
|
---|
341 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
342 | status = ndr_map_error2ntstatus(ndr_err);
|
---|
343 | return status;
|
---|
344 | }
|
---|
345 |
|
---|
346 | iov.iov_base = blob.data;
|
---|
347 | iov.iov_len = blob.length;
|
---|
348 |
|
---|
349 | status = messaging_send_iov(smb2req->xconn->msg_ctx,
|
---|
350 | global->server_id,
|
---|
351 | MSG_SMBXSRV_CONNECTION_PASS,
|
---|
352 | &iov, 1,
|
---|
353 | &smb2req->xconn->transport.sock, 1);
|
---|
354 | data_blob_free(&blob);
|
---|
355 | if (!NT_STATUS_IS_OK(status)) {
|
---|
356 | return status;
|
---|
357 | }
|
---|
358 |
|
---|
359 | return NT_STATUS_OK;
|
---|
360 | }
|
---|
361 |
|
---|
362 | static NTSTATUS smbXsrv_client_global_store(struct smbXsrv_client_global0 *global)
|
---|
363 | {
|
---|
364 | struct smbXsrv_client_globalB global_blob;
|
---|
365 | DATA_BLOB blob = data_blob_null;
|
---|
366 | TDB_DATA key;
|
---|
367 | TDB_DATA val;
|
---|
368 | NTSTATUS status;
|
---|
369 | enum ndr_err_code ndr_err;
|
---|
370 | bool saved_stored = global->stored;
|
---|
371 |
|
---|
372 | /*
|
---|
373 | * TODO: if we use other versions than '0'
|
---|
374 | * we would add glue code here, that would be able to
|
---|
375 | * store the information in the old format.
|
---|
376 | */
|
---|
377 |
|
---|
378 | if (global->db_rec == NULL) {
|
---|
379 | return NT_STATUS_INTERNAL_ERROR;
|
---|
380 | }
|
---|
381 |
|
---|
382 | key = dbwrap_record_get_key(global->db_rec);
|
---|
383 | val = dbwrap_record_get_value(global->db_rec);
|
---|
384 |
|
---|
385 | ZERO_STRUCT(global_blob);
|
---|
386 | global_blob.version = smbXsrv_version_global_current();
|
---|
387 | if (val.dsize >= 8) {
|
---|
388 | global_blob.seqnum = IVAL(val.dptr, 4);
|
---|
389 | }
|
---|
390 | global_blob.seqnum += 1;
|
---|
391 | global_blob.info.info0 = global;
|
---|
392 |
|
---|
393 | global->stored = true;
|
---|
394 | ndr_err = ndr_push_struct_blob(&blob, global->db_rec, &global_blob,
|
---|
395 | (ndr_push_flags_fn_t)ndr_push_smbXsrv_client_globalB);
|
---|
396 | global->stored = saved_stored;
|
---|
397 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
398 | status = ndr_map_error2ntstatus(ndr_err);
|
---|
399 | DBG_WARNING("key '%s' ndr_push - %s\n",
|
---|
400 | hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
|
---|
401 | nt_errstr(status));
|
---|
402 | TALLOC_FREE(global->db_rec);
|
---|
403 | return status;
|
---|
404 | }
|
---|
405 |
|
---|
406 | val = make_tdb_data(blob.data, blob.length);
|
---|
407 | status = dbwrap_record_store(global->db_rec, val, TDB_REPLACE);
|
---|
408 | if (!NT_STATUS_IS_OK(status)) {
|
---|
409 | DBG_WARNING("key '%s' store - %s\n",
|
---|
410 | hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
|
---|
411 | nt_errstr(status));
|
---|
412 | TALLOC_FREE(global->db_rec);
|
---|
413 | return status;
|
---|
414 | }
|
---|
415 |
|
---|
416 | global->stored = true;
|
---|
417 |
|
---|
418 | if (DEBUGLVL(DBGLVL_DEBUG)) {
|
---|
419 | DBG_DEBUG("key '%s' stored\n",
|
---|
420 | hex_encode_talloc(global->db_rec, key.dptr, key.dsize));
|
---|
421 | NDR_PRINT_DEBUG(smbXsrv_client_globalB, &global_blob);
|
---|
422 | }
|
---|
423 |
|
---|
424 | TALLOC_FREE(global->db_rec);
|
---|
425 |
|
---|
426 | return NT_STATUS_OK;
|
---|
427 | }
|
---|
428 |
|
---|
429 | static NTSTATUS smbXsrv_client_global_remove(struct smbXsrv_client_global0 *global)
|
---|
430 | {
|
---|
431 | TDB_DATA key;
|
---|
432 | NTSTATUS status;
|
---|
433 |
|
---|
434 | /*
|
---|
435 | * TODO: if we use other versions than '0'
|
---|
436 | * we would add glue code here, that would be able to
|
---|
437 | * store the information in the old format.
|
---|
438 | */
|
---|
439 |
|
---|
440 | if (global->db_rec == NULL) {
|
---|
441 | return NT_STATUS_INTERNAL_ERROR;
|
---|
442 | }
|
---|
443 |
|
---|
444 | key = dbwrap_record_get_key(global->db_rec);
|
---|
445 |
|
---|
446 | status = dbwrap_record_delete(global->db_rec);
|
---|
447 | if (!NT_STATUS_IS_OK(status)) {
|
---|
448 | DBG_WARNING("key '%s' delete - %s\n",
|
---|
449 | hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
|
---|
450 | nt_errstr(status));
|
---|
451 | TALLOC_FREE(global->db_rec);
|
---|
452 | return status;
|
---|
453 | }
|
---|
454 | global->stored = false;
|
---|
455 | DBG_DEBUG("key '%s' delete\n",
|
---|
456 | hex_encode_talloc(global->db_rec, key.dptr, key.dsize));
|
---|
457 |
|
---|
458 | TALLOC_FREE(global->db_rec);
|
---|
459 |
|
---|
460 | return NT_STATUS_OK;
|
---|
461 | }
|
---|
462 |
|
---|
463 | static int smbXsrv_client_destructor(struct smbXsrv_client *client)
|
---|
464 | {
|
---|
465 | NTSTATUS status;
|
---|
466 |
|
---|
467 | status = smbXsrv_client_remove(client);
|
---|
468 | if (!NT_STATUS_IS_OK(status)) {
|
---|
469 | DBG_ERR("smbXsrv_client_remove() failed: %s\n",
|
---|
470 | nt_errstr(status));
|
---|
471 | }
|
---|
472 |
|
---|
473 | TALLOC_FREE(client->global);
|
---|
474 |
|
---|
475 | return 0;
|
---|
476 | }
|
---|
477 |
|
---|
478 | static bool smbXsrv_client_connection_pass_filter(struct messaging_rec *rec, void *private_data);
|
---|
479 | static void smbXsrv_client_connection_pass_loop(struct tevent_req *subreq);
|
---|
480 |
|
---|
481 | NTSTATUS smbXsrv_client_create(TALLOC_CTX *mem_ctx,
|
---|
482 | struct tevent_context *ev_ctx,
|
---|
483 | struct messaging_context *msg_ctx,
|
---|
484 | NTTIME now,
|
---|
485 | struct smbXsrv_client **_client)
|
---|
486 | {
|
---|
487 | struct smbXsrv_client_table *table;
|
---|
488 | struct smbXsrv_client *client = NULL;
|
---|
489 | struct smbXsrv_client_global0 *global = NULL;
|
---|
490 | NTSTATUS status;
|
---|
491 | struct tevent_req *subreq = NULL;
|
---|
492 |
|
---|
493 | status = smbXsrv_client_table_create(mem_ctx,
|
---|
494 | msg_ctx,
|
---|
495 | 1, /* max_clients */
|
---|
496 | &table);
|
---|
497 | if (!NT_STATUS_IS_OK(status)) {
|
---|
498 | return status;
|
---|
499 | }
|
---|
500 |
|
---|
501 | if (table->local.num_clients >= table->local.max_clients) {
|
---|
502 | TALLOC_FREE(table);
|
---|
503 | return NT_STATUS_INSUFFICIENT_RESOURCES;
|
---|
504 | }
|
---|
505 |
|
---|
506 | client = talloc_zero(mem_ctx, struct smbXsrv_client);
|
---|
507 | if (client == NULL) {
|
---|
508 | TALLOC_FREE(table);
|
---|
509 | return NT_STATUS_NO_MEMORY;
|
---|
510 | }
|
---|
511 | client->ev_ctx = ev_ctx;
|
---|
512 | client->msg_ctx = msg_ctx;
|
---|
513 |
|
---|
514 | client->server_multi_channel_enabled = lp_server_multi_channel_support();
|
---|
515 |
|
---|
516 | client->table = talloc_move(client, &table);
|
---|
517 | table = client->table;
|
---|
518 |
|
---|
519 | global = talloc_zero(client, struct smbXsrv_client_global0);
|
---|
520 | if (global == NULL) {
|
---|
521 | TALLOC_FREE(client);
|
---|
522 | return NT_STATUS_NO_MEMORY;
|
---|
523 | }
|
---|
524 | talloc_set_destructor(global, smbXsrv_client_global_destructor);
|
---|
525 | client->global = global;
|
---|
526 |
|
---|
527 | global->initial_connect_time = now;
|
---|
528 |
|
---|
529 | global->server_id = messaging_server_id(client->msg_ctx);
|
---|
530 |
|
---|
531 | table->local.num_clients += 1;
|
---|
532 |
|
---|
533 | talloc_set_destructor(client, smbXsrv_client_destructor);
|
---|
534 |
|
---|
535 | if (DEBUGLVL(DBGLVL_DEBUG)) {
|
---|
536 | struct smbXsrv_clientB client_blob;
|
---|
537 |
|
---|
538 | ZERO_STRUCT(client_blob);
|
---|
539 | client_blob.version = SMBXSRV_VERSION_0;
|
---|
540 | client_blob.info.info0 = client;
|
---|
541 |
|
---|
542 | DBG_DEBUG("client_guid[%s] stored\n",
|
---|
543 | GUID_string(talloc_tos(), &global->client_guid));
|
---|
544 | NDR_PRINT_DEBUG(smbXsrv_clientB, &client_blob);
|
---|
545 | }
|
---|
546 |
|
---|
547 | subreq = messaging_filtered_read_send(client, client->ev_ctx, client->msg_ctx,
|
---|
548 | smbXsrv_client_connection_pass_filter,
|
---|
549 | client);
|
---|
550 | if (subreq == NULL) {
|
---|
551 | TALLOC_FREE(client);
|
---|
552 | return NT_STATUS_NO_MEMORY;
|
---|
553 | }
|
---|
554 | tevent_req_set_callback(subreq, smbXsrv_client_connection_pass_loop, client);
|
---|
555 |
|
---|
556 | *_client = client;
|
---|
557 | return NT_STATUS_OK;
|
---|
558 | }
|
---|
559 |
|
---|
560 | static bool smbXsrv_client_connection_pass_filter(struct messaging_rec *rec, void *private_data)
|
---|
561 | {
|
---|
562 | if (rec->msg_type != MSG_SMBXSRV_CONNECTION_PASS) {
|
---|
563 | return false;
|
---|
564 | }
|
---|
565 |
|
---|
566 | if (rec->num_fds != 1) {
|
---|
567 | return false;
|
---|
568 | }
|
---|
569 |
|
---|
570 | if (rec->buf.length < SMB2_HDR_BODY) {
|
---|
571 | return false;
|
---|
572 | }
|
---|
573 |
|
---|
574 | /* TODO: verify client_guid...? */
|
---|
575 |
|
---|
576 | return true;
|
---|
577 | }
|
---|
578 |
|
---|
579 | static void smbXsrv_client_connection_pass_loop(struct tevent_req *subreq)
|
---|
580 | {
|
---|
581 | struct smbXsrv_client *client =
|
---|
582 | tevent_req_callback_data(subreq,
|
---|
583 | struct smbXsrv_client);
|
---|
584 | struct smbXsrv_connection *xconn = NULL;
|
---|
585 | int ret;
|
---|
586 | struct messaging_rec *rec = NULL;
|
---|
587 | struct smbXsrv_connection_passB pass_blob;
|
---|
588 | enum ndr_err_code ndr_err;
|
---|
589 | struct smbXsrv_connection_pass0 *pass_info0 = NULL;
|
---|
590 | NTSTATUS status;
|
---|
591 | int sock_fd = -1;
|
---|
592 | uint64_t seq_low;
|
---|
593 |
|
---|
594 | ret = messaging_filtered_read_recv(subreq, talloc_tos(), &rec);
|
---|
595 | TALLOC_FREE(subreq);
|
---|
596 | if (ret != 0) {
|
---|
597 | goto next;
|
---|
598 | }
|
---|
599 |
|
---|
600 | ndr_err = ndr_pull_struct_blob(&rec->buf, rec, &pass_blob,
|
---|
601 | (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_connection_passB);
|
---|
602 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
603 | status = ndr_map_error2ntstatus(ndr_err);
|
---|
604 | DBG_WARNING("ndr_pull_struct_blob - %s\n", nt_errstr(status));
|
---|
605 | goto next;
|
---|
606 | }
|
---|
607 |
|
---|
608 | DBG_DEBUG("MSG_SMBXSRV_CLIENT_CLOSE\n");
|
---|
609 | if (DEBUGLVL(DBGLVL_DEBUG)) {
|
---|
610 | NDR_PRINT_DEBUG(smbXsrv_connection_passB, &pass_blob);
|
---|
611 | }
|
---|
612 |
|
---|
613 | if (pass_blob.version != SMBXSRV_VERSION_0) {
|
---|
614 | DBG_ERR("ignore invalid version %u\n", pass_blob.version);
|
---|
615 | NDR_PRINT_DEBUG(smbXsrv_connection_passB, &pass_blob);
|
---|
616 | goto next;
|
---|
617 | }
|
---|
618 |
|
---|
619 | pass_info0 = pass_blob.info.info0;
|
---|
620 | if (pass_info0 == NULL) {
|
---|
621 | DBG_ERR("ignore NULL info %u\n", pass_blob.version);
|
---|
622 | NDR_PRINT_DEBUG(smbXsrv_connection_passB, &pass_blob);
|
---|
623 | goto next;
|
---|
624 | }
|
---|
625 |
|
---|
626 | if (!GUID_equal(&client->global->client_guid, &pass_info0->client_guid))
|
---|
627 | {
|
---|
628 | DBG_WARNING("client's client_guid [%s] != passed guid [%s]\n",
|
---|
629 | GUID_string(talloc_tos(), &client->global->client_guid),
|
---|
630 | GUID_string(talloc_tos(), &pass_info0->client_guid));
|
---|
631 | if (DEBUGLVL(DBGLVL_WARNING)) {
|
---|
632 | NDR_PRINT_DEBUG(smbXsrv_connection_passB, &pass_blob);
|
---|
633 | }
|
---|
634 | goto next;
|
---|
635 | }
|
---|
636 |
|
---|
637 | if (client->global->initial_connect_time !=
|
---|
638 | pass_info0->initial_connect_time)
|
---|
639 | {
|
---|
640 | DBG_WARNING("client's initial connect time [%s] (%llu) != "
|
---|
641 | "passed initial connect time [%s] (%llu)\n",
|
---|
642 | nt_time_string(talloc_tos(),
|
---|
643 | client->global->initial_connect_time),
|
---|
644 | (unsigned long long)client->global->initial_connect_time,
|
---|
645 | nt_time_string(talloc_tos(),
|
---|
646 | pass_info0->initial_connect_time),
|
---|
647 | (unsigned long long)pass_info0->initial_connect_time);
|
---|
648 | if (DEBUGLVL(DBGLVL_WARNING)) {
|
---|
649 | NDR_PRINT_DEBUG(smbXsrv_connection_passB, &pass_blob);
|
---|
650 | }
|
---|
651 | goto next;
|
---|
652 | }
|
---|
653 |
|
---|
654 | SMB_ASSERT(rec->num_fds == 1);
|
---|
655 | sock_fd = rec->fds[0];
|
---|
656 |
|
---|
657 | DBG_ERR("got connection sockfd[%d]\n", sock_fd);
|
---|
658 | NDR_PRINT_DEBUG(smbXsrv_connection_passB, &pass_blob);
|
---|
659 | status = smbd_add_connection(client, sock_fd, &xconn);
|
---|
660 | if (!NT_STATUS_IS_OK(status)) {
|
---|
661 | close(sock_fd);
|
---|
662 | sock_fd = -1;
|
---|
663 | DBG_ERR("smbd_add_connection => %s\n", nt_errstr(status));
|
---|
664 | NDR_PRINT_DEBUG(smbXsrv_connection_passB, &pass_blob);
|
---|
665 | goto next;
|
---|
666 | }
|
---|
667 |
|
---|
668 | /*
|
---|
669 | * Set seq_low to mid received in negprot
|
---|
670 | */
|
---|
671 | seq_low = BVAL(pass_info0->negotiate_request.data,
|
---|
672 | SMB2_HDR_MESSAGE_ID);
|
---|
673 |
|
---|
674 | xconn->smb2.client.guid_verified = true;
|
---|
675 | smbd_smb2_process_negprot(xconn, seq_low,
|
---|
676 | pass_info0->negotiate_request.data,
|
---|
677 | pass_info0->negotiate_request.length);
|
---|
678 |
|
---|
679 | next:
|
---|
680 | TALLOC_FREE(rec);
|
---|
681 |
|
---|
682 | subreq = messaging_filtered_read_send(client, client->ev_ctx, client->msg_ctx,
|
---|
683 | smbXsrv_client_connection_pass_filter,
|
---|
684 | client);
|
---|
685 | if (subreq == NULL) {
|
---|
686 | const char *r;
|
---|
687 | r = "messaging_read_send(MSG_SMBXSRV_CONNECTION_PASS failed";
|
---|
688 | exit_server_cleanly(r);
|
---|
689 | return;
|
---|
690 | }
|
---|
691 | tevent_req_set_callback(subreq, smbXsrv_client_connection_pass_loop, client);
|
---|
692 | }
|
---|
693 |
|
---|
694 | NTSTATUS smbXsrv_client_update(struct smbXsrv_client *client)
|
---|
695 | {
|
---|
696 | struct smbXsrv_client_table *table = client->table;
|
---|
697 | NTSTATUS status;
|
---|
698 |
|
---|
699 | if (client->global->db_rec != NULL) {
|
---|
700 | DBG_ERR("guid [%s]: Called with db_rec != NULL'\n",
|
---|
701 | GUID_string(talloc_tos(),
|
---|
702 | &client->global->client_guid));
|
---|
703 | return NT_STATUS_INTERNAL_ERROR;
|
---|
704 | }
|
---|
705 |
|
---|
706 | client->global->db_rec = smbXsrv_client_global_fetch_locked(
|
---|
707 | table->global.db_ctx,
|
---|
708 | &client->global->client_guid,
|
---|
709 | client->global /* TALLOC_CTX */);
|
---|
710 | if (client->global->db_rec == NULL) {
|
---|
711 | return NT_STATUS_INTERNAL_DB_ERROR;
|
---|
712 | }
|
---|
713 |
|
---|
714 | status = smbXsrv_client_global_store(client->global);
|
---|
715 | if (!NT_STATUS_IS_OK(status)) {
|
---|
716 | DBG_ERR("client_guid[%s] store failed - %s\n",
|
---|
717 | GUID_string(talloc_tos(), &client->global->client_guid),
|
---|
718 | nt_errstr(status));
|
---|
719 | return status;
|
---|
720 | }
|
---|
721 |
|
---|
722 | if (DEBUGLVL(DBGLVL_DEBUG)) {
|
---|
723 | struct smbXsrv_clientB client_blob;
|
---|
724 |
|
---|
725 | ZERO_STRUCT(client_blob);
|
---|
726 | client_blob.version = SMBXSRV_VERSION_0;
|
---|
727 | client_blob.info.info0 = client;
|
---|
728 |
|
---|
729 | DBG_DEBUG("client_guid[%s] stored\n",
|
---|
730 | GUID_string(talloc_tos(), &client->global->client_guid));
|
---|
731 | NDR_PRINT_DEBUG(smbXsrv_clientB, &client_blob);
|
---|
732 | }
|
---|
733 |
|
---|
734 | return NT_STATUS_OK;
|
---|
735 | }
|
---|
736 |
|
---|
737 | NTSTATUS smbXsrv_client_remove(struct smbXsrv_client *client)
|
---|
738 | {
|
---|
739 | struct smbXsrv_client_table *table = client->table;
|
---|
740 | NTSTATUS status;
|
---|
741 |
|
---|
742 | if (client->global->db_rec != NULL) {
|
---|
743 | DBG_ERR("client_guid[%s]: Called with db_rec != NULL'\n",
|
---|
744 | GUID_string(talloc_tos(), &client->global->client_guid));
|
---|
745 | return NT_STATUS_INTERNAL_ERROR;
|
---|
746 | }
|
---|
747 |
|
---|
748 | if (!client->global->stored) {
|
---|
749 | return NT_STATUS_OK;
|
---|
750 | }
|
---|
751 |
|
---|
752 | client->global->db_rec = smbXsrv_client_global_fetch_locked(
|
---|
753 | table->global.db_ctx,
|
---|
754 | &client->global->client_guid,
|
---|
755 | client->global /* TALLOC_CTX */);
|
---|
756 | if (client->global->db_rec == NULL) {
|
---|
757 | return NT_STATUS_INTERNAL_DB_ERROR;
|
---|
758 | }
|
---|
759 |
|
---|
760 | status = smbXsrv_client_global_remove(client->global);
|
---|
761 | if (!NT_STATUS_IS_OK(status)) {
|
---|
762 | DBG_ERR("client_guid[%s] store failed - %s\n",
|
---|
763 | GUID_string(talloc_tos(), &client->global->client_guid),
|
---|
764 | nt_errstr(status));
|
---|
765 | return status;
|
---|
766 | }
|
---|
767 |
|
---|
768 | if (DEBUGLVL(DBGLVL_DEBUG)) {
|
---|
769 | struct smbXsrv_clientB client_blob;
|
---|
770 |
|
---|
771 | ZERO_STRUCT(client_blob);
|
---|
772 | client_blob.version = SMBXSRV_VERSION_0;
|
---|
773 | client_blob.info.info0 = client;
|
---|
774 |
|
---|
775 | DBG_DEBUG("client_guid[%s] stored\n",
|
---|
776 | GUID_string(talloc_tos(), &client->global->client_guid));
|
---|
777 | NDR_PRINT_DEBUG(smbXsrv_clientB, &client_blob);
|
---|
778 | }
|
---|
779 |
|
---|
780 | return NT_STATUS_OK;
|
---|
781 | }
|
---|