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