Coverage Report

Created: 2026-06-07 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/source4/libcli/smb_composite/fetchfile.c
Line
Count
Source
1
/* 
2
   Unix SMB/CIFS implementation.
3
4
   Copyright (C) Volker Lendecke 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 loading a whole file into memory
21
*/
22
23
#include "includes.h"
24
#include "libcli/composite/composite.h"
25
#include "libcli/smb_composite/smb_composite.h"
26
#include "libcli/resolve/resolve.h"
27
28
enum fetchfile_stage {FETCHFILE_CONNECT,
29
          FETCHFILE_READ};
30
31
struct fetchfile_state {
32
  enum fetchfile_stage stage;
33
  struct smb_composite_fetchfile *io;
34
  struct composite_context *creq;
35
  struct smb_composite_connect *connect;
36
  struct smb_composite_loadfile *loadfile;
37
};
38
39
static void fetchfile_composite_handler(struct composite_context *req);
40
41
static NTSTATUS fetchfile_connect(struct composite_context *c,
42
          struct smb_composite_fetchfile *io)
43
0
{
44
0
  NTSTATUS status;
45
0
  struct fetchfile_state *state;
46
0
  state = talloc_get_type(c->private_data, struct fetchfile_state);
47
48
0
  status = smb_composite_connect_recv(state->creq, c);
49
0
  NT_STATUS_NOT_OK_RETURN(status);
50
51
0
  state->loadfile = talloc(state, struct smb_composite_loadfile);
52
0
  NT_STATUS_HAVE_NO_MEMORY(state->loadfile);
53
54
0
  state->loadfile->in.fname = io->in.filename;
55
56
0
  state->creq = smb_composite_loadfile_send(state->connect->out.tree,
57
0
             state->loadfile);
58
0
  NT_STATUS_HAVE_NO_MEMORY(state->creq);
59
60
0
  state->creq->async.private_data = c;
61
0
  state->creq->async.fn = fetchfile_composite_handler;
62
63
0
  state->stage = FETCHFILE_READ;
64
65
0
  return NT_STATUS_OK;
66
0
}
67
68
static NTSTATUS fetchfile_read(struct composite_context *c,
69
             struct smb_composite_fetchfile *io)
70
0
{
71
0
  NTSTATUS status;
72
0
  struct fetchfile_state *state;
73
0
  state = talloc_get_type(c->private_data, struct fetchfile_state);
74
75
0
  status = smb_composite_loadfile_recv(state->creq, NULL);
76
0
  NT_STATUS_NOT_OK_RETURN(status);
77
78
0
  io->out.data = state->loadfile->out.data;
79
0
  io->out.size = state->loadfile->out.size;
80
81
0
  c->state = COMPOSITE_STATE_DONE;
82
0
  if (c->async.fn)
83
0
    c->async.fn(c);
84
85
0
  return NT_STATUS_OK;
86
0
}
87
88
static void fetchfile_state_handler(struct composite_context *c)
89
0
{
90
0
  struct fetchfile_state *state;
91
0
  NTSTATUS status;
92
  
93
0
  state = talloc_get_type(c->private_data, struct fetchfile_state);
94
95
  /* when this handler is called, the stage indicates what
96
     call has just finished */
97
0
  switch (state->stage) {
98
0
  case FETCHFILE_CONNECT:
99
0
    status = fetchfile_connect(c, state->io);
100
0
    break;
101
0
  case FETCHFILE_READ:
102
0
    status = fetchfile_read(c, state->io);
103
0
    break;
104
0
  default:
105
0
    status = NT_STATUS_UNSUCCESSFUL;
106
0
    break;
107
0
  }
108
109
0
  if (!NT_STATUS_IS_OK(status)) {
110
0
    c->status = status;
111
0
    c->state = COMPOSITE_STATE_ERROR;
112
0
    if (c->async.fn) {
113
0
      c->async.fn(c);
114
0
    }
115
0
  }
116
0
}
117
118
static void fetchfile_composite_handler(struct composite_context *creq)
119
0
{
120
0
  struct composite_context *c = talloc_get_type(creq->async.private_data, 
121
0
                  struct composite_context);
122
0
  fetchfile_state_handler(c);
123
0
}
124
125
struct composite_context *smb_composite_fetchfile_send(struct smb_composite_fetchfile *io,
126
                   struct loadparm_context *lp_ctx,
127
                   struct tevent_context *event_ctx)
128
0
{
129
0
  struct composite_context *c;
130
0
  struct fetchfile_state *state;
131
132
0
  c = talloc_zero(NULL, struct composite_context);
133
0
  if (c == NULL) goto failed;
134
135
0
  state = talloc(c, struct fetchfile_state);
136
0
  if (state == NULL) goto failed;
137
138
0
  state->connect = talloc_zero(state, struct smb_composite_connect);
139
0
  if (state->connect == NULL) goto failed;
140
141
0
  state->io = io;
142
143
0
  state->connect->in.dest_host    = io->in.dest_host;
144
0
  state->connect->in.socket_options = io->in.socket_options;
145
0
  state->connect->in.called_name  = io->in.called_name;
146
0
  state->connect->in.service      = io->in.service;
147
0
  state->connect->in.service_type = io->in.service_type;
148
0
  state->connect->in.credentials  = io->in.credentials;
149
0
  state->connect->in.fallback_to_anonymous = false;
150
0
  state->connect->in.workgroup    = io->in.workgroup;
151
0
  state->connect->in.gensec_settings = io->in.gensec_settings;
152
153
0
  state->connect->in.options  = io->in.options;
154
0
  state->connect->in.session_options = io->in.session_options;
155
156
0
  state->creq = smb_composite_connect_send(state->connect, state, lp_ctx,
157
0
             io->in.resolve_ctx, event_ctx);
158
0
  if (state->creq == NULL) goto failed;
159
160
0
  state->creq->async.private_data = c;
161
0
  state->creq->async.fn = fetchfile_composite_handler;
162
163
0
  c->state = COMPOSITE_STATE_IN_PROGRESS;
164
0
  state->stage = FETCHFILE_CONNECT;
165
0
  c->private_data = state;
166
167
0
  return c;
168
0
 failed:
169
0
  talloc_free(c);
170
0
  return NULL;
171
0
}
172
173
NTSTATUS smb_composite_fetchfile_recv(struct composite_context *c,
174
              TALLOC_CTX *mem_ctx)
175
0
{
176
0
  NTSTATUS status;
177
178
0
  status = composite_wait(c);
179
180
0
  if (NT_STATUS_IS_OK(status)) {
181
0
    struct fetchfile_state *state = talloc_get_type(c->private_data, struct fetchfile_state);
182
0
    talloc_steal(mem_ctx, state->io->out.data);
183
0
  }
184
185
  talloc_free(c);
186
0
  return status;
187
0
}