Coverage Report

Created: 2025-11-16 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/source4/libcli/smb_composite/connect.c
Line
Count
Source
1
/* 
2
   Unix SMB/CIFS implementation.
3
4
   Copyright (C) Andrew Tridgell 2005
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
  a composite API for making a full SMB connection
21
*/
22
23
#include "includes.h"
24
#include "libcli/raw/libcliraw.h"
25
#include "libcli/raw/raw_proto.h"
26
#include "libcli/composite/composite.h"
27
#include "libcli/smb_composite/smb_composite.h"
28
#include "lib/events/events.h"
29
#include "libcli/resolve/resolve.h"
30
#include "auth/credentials/credentials.h"
31
#include "librpc/gen_ndr/ndr_nbt.h"
32
#include "param/param.h"
33
#include "lib/util/util_net.h"
34
#include "libcli/smb/smbXcli_base.h"
35
36
/* the stages of this call */
37
enum connect_stage {CONNECT_SOCKET, 
38
        CONNECT_NEGPROT,
39
        CONNECT_SESSION_SETUP,
40
        CONNECT_SESSION_SETUP_ANON,
41
        CONNECT_TCON,
42
        CONNECT_DONE
43
};
44
45
struct connect_state {
46
  enum connect_stage stage;
47
  struct smbcli_socket *sock;
48
  struct smbcli_transport *transport;
49
  struct smbcli_session *session;
50
  struct smb_composite_connect *io;
51
  union smb_tcon *io_tcon;
52
  struct smb_composite_sesssetup *io_setup;
53
  struct smbcli_request *req;
54
  struct composite_context *creq;
55
  struct tevent_req *subreq;
56
  struct nbt_name calling, called;
57
};
58
59
60
static void request_handler(struct smbcli_request *);
61
static void composite_handler(struct composite_context *);
62
static void subreq_handler(struct tevent_req *subreq);
63
64
/*
65
  a tree connect request has completed
66
*/
67
static NTSTATUS connect_tcon(struct composite_context *c, 
68
           struct smb_composite_connect *io)
69
0
{
70
0
  struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
71
0
  NTSTATUS status;
72
73
0
  status = smb_raw_tcon_recv(state->req, c, state->io_tcon);
74
0
  NT_STATUS_NOT_OK_RETURN(status);
75
76
0
  if (state->io_tcon->tconx.out.options & SMB_EXTENDED_SIGNATURES) {
77
0
    smb1cli_session_protect_session_key(io->out.tree->session->smbXcli);
78
0
  }
79
80
0
  io->out.tree->tid = state->io_tcon->tconx.out.tid;
81
0
  if (state->io_tcon->tconx.out.dev_type) {
82
0
    io->out.tree->device = talloc_strdup(io->out.tree, 
83
0
                 state->io_tcon->tconx.out.dev_type);
84
0
  }
85
0
  if (state->io_tcon->tconx.out.fs_type) {
86
0
    io->out.tree->fs_type = talloc_strdup(io->out.tree, 
87
0
                  state->io_tcon->tconx.out.fs_type);
88
0
  }
89
90
0
  state->stage = CONNECT_DONE;
91
92
0
  return NT_STATUS_OK;
93
0
}
94
95
96
/*
97
  a session setup request with anonymous fallback has completed
98
*/
99
static NTSTATUS connect_session_setup_anon(struct composite_context *c, 
100
             struct smb_composite_connect *io)
101
0
{
102
0
  struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
103
0
  NTSTATUS status;
104
105
0
  status = smb_composite_sesssetup_recv(state->creq);
106
0
  NT_STATUS_NOT_OK_RETURN(status);
107
108
0
  io->out.anonymous_fallback_done = true;
109
  
110
0
  state->session->vuid = state->io_setup->out.vuid;
111
  
112
  /* setup for a tconx */
113
0
  state->io_tcon = talloc(c, union smb_tcon);
114
0
  NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
115
116
  /* connect to a share using a tree connect */
117
0
  state->io_tcon->generic.level = RAW_TCON_TCONX;
118
0
  state->io_tcon->tconx.in.flags = TCONX_FLAG_EXTENDED_RESPONSE;
119
0
  state->io_tcon->tconx.in.password = data_blob(NULL, 0);  
120
  
121
0
  state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon, 
122
0
             "\\\\%s\\%s", 
123
0
             io->in.called_name, 
124
0
             io->in.service);
125
0
  NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
126
0
  if (!io->in.service_type) {
127
0
    state->io_tcon->tconx.in.device = "?????";
128
0
  } else {
129
0
    state->io_tcon->tconx.in.device = io->in.service_type;
130
0
  }
131
132
0
  state->req = smb_raw_tcon_send(io->out.tree, state->io_tcon);
133
0
  NT_STATUS_HAVE_NO_MEMORY(state->req);
134
0
  if (state->req->state == SMBCLI_REQUEST_ERROR) {
135
0
    return state->req->status;
136
0
  }
137
138
0
  state->req->async.fn = request_handler;
139
0
  state->req->async.private_data = c;
140
0
  state->stage = CONNECT_TCON;
141
142
0
  return NT_STATUS_OK;
143
0
}
144
145
/*
146
  a session setup request has completed
147
*/
148
static NTSTATUS connect_session_setup(struct composite_context *c, 
149
              struct smb_composite_connect *io)
150
0
{
151
0
  struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
152
0
  NTSTATUS status;
153
154
0
  status = smb_composite_sesssetup_recv(state->creq);
155
156
0
  if (!NT_STATUS_IS_OK(status) &&
157
0
      !cli_credentials_is_anonymous(state->io->in.credentials) &&
158
0
      io->in.fallback_to_anonymous) {
159
160
0
    state->io_setup->in.credentials = cli_credentials_init(state);
161
0
    NT_STATUS_HAVE_NO_MEMORY(state->io_setup->in.credentials);
162
0
    cli_credentials_set_workstation(state->io_setup->in.credentials,
163
0
       cli_credentials_get_workstation(state->io->in.credentials), 
164
0
       CRED_SPECIFIED);
165
0
    cli_credentials_set_anonymous(state->io_setup->in.credentials);
166
167
    /* If the preceding attempt was with extended security, we
168
     * have been given a uid in the NTLMSSP_CHALLENGE reply. This
169
     * would lead to an invalid uid in the anonymous fallback */
170
0
    state->session->vuid = 0;
171
0
    talloc_free(state->session->gensec);
172
0
    state->session->gensec = NULL;
173
174
0
    state->creq = smb_composite_sesssetup_send(state->session,
175
0
                 state->io_setup);
176
0
    NT_STATUS_HAVE_NO_MEMORY(state->creq);
177
0
    if (state->creq->state == COMPOSITE_STATE_ERROR) {
178
0
      return state->creq->status;
179
0
    }
180
0
    state->creq->async.fn = composite_handler;
181
0
    state->creq->async.private_data = c;
182
0
    state->stage = CONNECT_SESSION_SETUP_ANON;
183
184
0
    return NT_STATUS_OK;
185
0
  }
186
187
0
  NT_STATUS_NOT_OK_RETURN(status);
188
  
189
0
  state->session->vuid = state->io_setup->out.vuid;
190
  
191
  /* If we don't have a remote share name then this indicates that
192
   * we don't want to do a tree connect */
193
0
  if (!io->in.service) {
194
0
    state->stage = CONNECT_DONE;
195
0
    return NT_STATUS_OK;
196
0
  }
197
198
0
  state->io_tcon = talloc(c, union smb_tcon);
199
0
  NT_STATUS_HAVE_NO_MEMORY(state->io_tcon);
200
201
  /* connect to a share using a tree connect */
202
0
  state->io_tcon->generic.level = RAW_TCON_TCONX;
203
0
  state->io_tcon->tconx.in.flags = TCONX_FLAG_EXTENDED_RESPONSE;
204
0
  state->io_tcon->tconx.in.flags |= TCONX_FLAG_EXTENDED_SIGNATURES;
205
0
  state->io_tcon->tconx.in.password = data_blob(NULL, 0);  
206
  
207
0
  state->io_tcon->tconx.in.path = talloc_asprintf(state->io_tcon, 
208
0
             "\\\\%s\\%s", 
209
0
             io->in.called_name, 
210
0
             io->in.service);
211
0
  NT_STATUS_HAVE_NO_MEMORY(state->io_tcon->tconx.in.path);
212
0
  if (!io->in.service_type) {
213
0
    state->io_tcon->tconx.in.device = "?????";
214
0
  } else {
215
0
    state->io_tcon->tconx.in.device = io->in.service_type;
216
0
  }
217
218
0
  state->req = smb_raw_tcon_send(io->out.tree, state->io_tcon);
219
0
  NT_STATUS_HAVE_NO_MEMORY(state->req);
220
0
  if (state->req->state == SMBCLI_REQUEST_ERROR) {
221
0
    return state->req->status;
222
0
  }
223
224
0
  state->req->async.fn = request_handler;
225
0
  state->req->async.private_data = c;
226
0
  state->stage = CONNECT_TCON;
227
228
0
  return NT_STATUS_OK;
229
0
}
230
231
static NTSTATUS connect_send_session(struct composite_context *c,
232
             struct smb_composite_connect *io)
233
0
{
234
0
  struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
235
236
  /* next step is a session setup */
237
0
  state->session = smbcli_session_init(state->transport, state, true, io->in.session_options);
238
0
  NT_STATUS_HAVE_NO_MEMORY(state->session);
239
  
240
  /* setup for a tconx (or at least have the structure ready to
241
   * return, if we won't go that far) */
242
0
  io->out.tree = smbcli_tree_init(state->session, state, true);
243
0
  NT_STATUS_HAVE_NO_MEMORY(io->out.tree);
244
245
  /* If we don't have any credentials then this indicates that
246
   * we don't want to do a session setup */
247
0
  if (!io->in.credentials) {
248
0
    state->stage = CONNECT_DONE;
249
0
    return NT_STATUS_OK;
250
0
  }
251
252
0
  state->io_setup = talloc(c, struct smb_composite_sesssetup);
253
0
  NT_STATUS_HAVE_NO_MEMORY(state->io_setup);
254
255
  /* prepare a session setup to establish a security context */
256
0
  state->io_setup->in.sesskey      = state->transport->negotiate.sesskey;
257
0
  state->io_setup->in.capabilities = state->transport->negotiate.capabilities;
258
0
  state->io_setup->in.credentials  = io->in.credentials;
259
0
  state->io_setup->in.workgroup    = io->in.workgroup;
260
0
  state->io_setup->in.gensec_settings = io->in.gensec_settings;
261
262
0
  state->creq = smb_composite_sesssetup_send(state->session, state->io_setup);
263
0
  NT_STATUS_HAVE_NO_MEMORY(state->creq);
264
0
  if (state->creq->state == COMPOSITE_STATE_ERROR) {
265
0
    return state->creq->status;
266
0
  }
267
268
0
  state->creq->async.fn = composite_handler;
269
0
  state->creq->async.private_data = c;
270
271
0
  state->stage = CONNECT_SESSION_SETUP;
272
  
273
0
  return NT_STATUS_OK;
274
0
}
275
276
/*
277
  a negprot request has completed
278
*/
279
static NTSTATUS connect_negprot(struct composite_context *c,
280
        struct smb_composite_connect *io)
281
0
{
282
0
  struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
283
0
  NTSTATUS status;
284
285
0
  status = smb_raw_negotiate_recv(state->subreq);
286
0
  TALLOC_FREE(state->subreq);
287
0
  NT_STATUS_NOT_OK_RETURN(status);
288
289
0
  return connect_send_session(c, io);
290
0
}
291
292
/*
293
  setup a negprot send 
294
*/
295
static NTSTATUS connect_send_negprot(struct composite_context *c, 
296
             struct smb_composite_connect *io)
297
0
{
298
0
  struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
299
300
  /* the socket is up - we can initialise the smbcli transport layer */
301
0
  state->transport = smbcli_transport_init(state->sock, state, true,
302
0
             &io->in.options);
303
0
  NT_STATUS_HAVE_NO_MEMORY(state->transport);
304
305
0
  state->subreq = smb_raw_negotiate_send(state,
306
0
                 state->transport->ev,
307
0
                 state->transport,
308
0
                 state->transport->options.min_protocol,
309
0
                 state->transport->options.max_protocol);
310
0
  NT_STATUS_HAVE_NO_MEMORY(state->subreq);
311
0
  tevent_req_set_callback(state->subreq, subreq_handler, c);
312
0
  state->stage = CONNECT_NEGPROT;
313
314
0
  return NT_STATUS_OK;
315
0
}
316
317
/*
318
  a socket connection operation has completed
319
*/
320
static NTSTATUS connect_socket(struct composite_context *c, 
321
             struct smb_composite_connect *io)
322
0
{
323
0
  struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
324
0
  NTSTATUS status;
325
326
0
  status = smbcli_sock_connect_recv(state->creq, state, &state->sock);
327
0
  NT_STATUS_NOT_OK_RETURN(status);
328
329
0
  if (is_ipaddress(state->sock->hostname) &&
330
0
      (state->io->in.called_name != NULL)) {
331
    /* If connecting to an IP address, we might want the real name
332
     * of the host for later kerberos. The called name is a better
333
     * approximation */
334
0
    state->sock->hostname =
335
0
      talloc_strdup(state->sock, io->in.called_name);
336
0
    NT_STATUS_HAVE_NO_MEMORY(state->sock->hostname);
337
0
  }
338
339
  /* next step is a negprot */
340
0
  return connect_send_negprot(c, io);
341
0
}
342
343
344
/*
345
  handle and dispatch state transitions
346
*/
347
static void state_handler(struct composite_context *c)
348
0
{
349
0
  struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
350
351
0
  switch (state->stage) {
352
0
  case CONNECT_SOCKET:
353
0
    c->status = connect_socket(c, state->io);
354
0
    break;
355
0
  case CONNECT_NEGPROT:
356
0
    c->status = connect_negprot(c, state->io);
357
0
    break;
358
0
  case CONNECT_SESSION_SETUP:
359
0
    c->status = connect_session_setup(c, state->io);
360
0
    break;
361
0
  case CONNECT_SESSION_SETUP_ANON:
362
0
    c->status = connect_session_setup_anon(c, state->io);
363
0
    break;
364
0
  case CONNECT_TCON:
365
0
    c->status = connect_tcon(c, state->io);
366
0
    break;
367
0
  case CONNECT_DONE:
368
0
    break;
369
0
  }
370
371
0
  if (state->stage == CONNECT_DONE) {
372
    /* all done! */
373
0
    composite_done(c);
374
0
  } else {
375
0
    composite_is_ok(c);
376
0
  }
377
0
}
378
379
380
/*
381
  handler for completion of a smbcli_request sub-request
382
*/
383
static void request_handler(struct smbcli_request *req)
384
0
{
385
0
  struct composite_context *c = talloc_get_type(req->async.private_data,
386
0
                 struct composite_context);
387
0
  state_handler(c);
388
0
}
389
390
/*
391
  handler for completion of a smbcli_composite sub-request
392
*/
393
static void composite_handler(struct composite_context *creq)
394
0
{
395
0
  struct composite_context *c = talloc_get_type(creq->async.private_data, 
396
0
                 struct composite_context);
397
0
  state_handler(c);
398
0
}
399
400
/*
401
  handler for completion of a tevent_req sub-request
402
*/
403
static void subreq_handler(struct tevent_req *subreq)
404
0
{
405
0
  struct composite_context *c =
406
0
    tevent_req_callback_data(subreq,
407
0
    struct composite_context);
408
0
  state_handler(c);
409
0
}
410
411
/*
412
  a function to establish a smbcli_tree from scratch
413
*/
414
struct composite_context *smb_composite_connect_send(struct smb_composite_connect *io,
415
                 TALLOC_CTX *mem_ctx,
416
                 struct loadparm_context *lp_ctx,
417
                 struct resolve_context *resolve_ctx,
418
                 struct tevent_context *event_ctx)
419
0
{
420
0
  struct composite_context *c;
421
0
  struct connect_state *state;
422
423
0
  c = talloc_zero(mem_ctx, struct composite_context);
424
0
  if (c == NULL) {
425
0
    goto nomem;
426
0
  }
427
428
0
  state = talloc_zero(c, struct connect_state);
429
0
  if (state == NULL) {
430
0
    goto nomem;
431
0
  }
432
433
0
  c->event_ctx = event_ctx;
434
0
  if (c->event_ctx == NULL) {
435
0
    composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
436
0
    return c;
437
0
  }
438
439
0
  if (io->in.gensec_settings == NULL) {
440
0
    composite_error(c, NT_STATUS_INVALID_PARAMETER_MIX);
441
0
    return c;
442
0
  }
443
0
  state->io = io;
444
445
0
  c->state = COMPOSITE_STATE_IN_PROGRESS;
446
0
  c->private_data = state;
447
448
0
  make_nbt_name_client(&state->calling,
449
0
           cli_credentials_get_workstation(io->in.credentials));
450
451
0
  nbt_choose_called_name(state, &state->called,
452
0
             io->in.called_name, NBT_NAME_SERVER);
453
454
0
  if (io->in.existing_conn != NULL) {
455
0
    NTSTATUS status;
456
457
0
    status = smbcli_transport_raw_init(state,
458
0
               c->event_ctx,
459
0
               &io->in.existing_conn,
460
0
               &io->in.options,
461
0
               &state->transport);
462
0
    if (!NT_STATUS_IS_OK(status)) {
463
0
      composite_error(c, status);
464
0
      return c;
465
0
    }
466
467
0
    status = connect_send_session(c, io);
468
0
    if (!NT_STATUS_IS_OK(status)) {
469
0
      composite_error(c, status);
470
0
      return c;
471
0
    }
472
473
0
    return c;
474
0
  }
475
476
0
  state->creq = smbcli_sock_connect_send(state, 
477
0
                 NULL,
478
0
                 &io->in.options,
479
0
                 io->in.dest_host, 
480
0
                 lp_ctx,
481
0
                 resolve_ctx, c->event_ctx, 
482
0
                 io->in.socket_options,
483
0
                 &state->calling,
484
0
                 &state->called);
485
0
  if (state->creq == NULL) {
486
0
    composite_error(c, NT_STATUS_NO_MEMORY);
487
0
    return c;
488
0
  }
489
490
0
  state->stage = CONNECT_SOCKET;
491
0
  state->creq->async.private_data = c;
492
0
  state->creq->async.fn = composite_handler;
493
494
0
  return c;
495
0
nomem:
496
0
  TALLOC_FREE(c);
497
0
  return NULL;
498
0
}
499
500
/*
501
  recv half of async composite connect code
502
*/
503
NTSTATUS smb_composite_connect_recv(struct composite_context *c, TALLOC_CTX *mem_ctx)
504
0
{
505
0
  NTSTATUS status;
506
507
0
  status = composite_wait(c);
508
509
0
  if (NT_STATUS_IS_OK(status)) {
510
0
    struct connect_state *state = talloc_get_type(c->private_data, struct connect_state);
511
0
    talloc_steal(mem_ctx, state->io->out.tree);
512
0
  }
513
514
0
  talloc_free(c);
515
0
  return status;
516
0
}
517
518
/*
519
  sync version of smb_composite_connect 
520
*/
521
NTSTATUS smb_composite_connect(struct smb_composite_connect *io, TALLOC_CTX *mem_ctx,
522
             struct loadparm_context *lp_ctx,
523
             struct resolve_context *resolve_ctx,
524
             struct tevent_context *ev)
525
0
{
526
0
  struct composite_context *c = smb_composite_connect_send(io, mem_ctx, lp_ctx, resolve_ctx, ev);
527
0
  if (c == NULL) {
528
0
    return NT_STATUS_NO_MEMORY;
529
0
  }
530
0
  return smb_composite_connect_recv(c, mem_ctx);
531
0
}