ctdb-scripts: Ignore shellcheck SC2181 warning (use of $?)
[metze/samba/wip.git] / source3 / libsmb / clitrans.c
1 /*
2    Unix SMB/CIFS implementation.
3    client transaction calls
4    Copyright (C) Andrew Tridgell 1994-1998
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 "libsmb/libsmb.h"
22 #include "../lib/util/tevent_ntstatus.h"
23 #include "async_smb.h"
24 #include "../libcli/smb/smbXcli_base.h"
25
26 struct cli_trans_state {
27         struct cli_state *cli;
28         struct tevent_req *req;
29         struct cli_trans_state **ptr;
30 };
31
32 static int cli_trans_state_destructor(struct cli_trans_state *state)
33 {
34         talloc_set_destructor(state->ptr, NULL);
35         talloc_free(state->ptr);
36         return 0;
37 }
38
39 static int cli_trans_state_ptr_destructor(struct cli_trans_state **ptr)
40 {
41         struct cli_trans_state *state = *ptr;
42         void *parent = talloc_parent(state);
43
44         talloc_set_destructor(state, NULL);
45
46         talloc_reparent(state, parent, state->req);
47         talloc_free(state);
48         return 0;
49 }
50
51 struct tevent_req *cli_trans_send(
52         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
53         struct cli_state *cli, uint16_t additional_flags2, uint8_t cmd,
54         const char *pipe_name, uint16_t fid, uint16_t function, int flags,
55         uint16_t *setup, uint8_t num_setup, uint8_t max_setup,
56         uint8_t *param, uint32_t num_param, uint32_t max_param,
57         uint8_t *data, uint32_t num_data, uint32_t max_data)
58 {
59         struct cli_trans_state *state;
60         uint8_t additional_flags = 0;
61         uint8_t clear_flags = 0;
62         uint16_t clear_flags2 = 0;
63
64         state = talloc_zero(mem_ctx, struct cli_trans_state);
65         if (state == NULL) {
66                 return NULL;
67         }
68         state->cli = cli;
69         state->ptr = talloc(state, struct cli_trans_state *);
70         if (state->ptr == NULL) {
71                 talloc_free(state);
72                 return NULL;
73         }
74         *state->ptr = state;
75
76         state->req = smb1cli_trans_send(state, ev,
77                                         cli->conn, cmd,
78                                         additional_flags, clear_flags,
79                                         additional_flags2, clear_flags2,
80                                         cli->timeout,
81                                         cli->smb1.pid,
82                                         cli->smb1.tcon,
83                                         cli->smb1.session,
84                                         pipe_name, fid, function, flags,
85                                         setup, num_setup, max_setup,
86                                         param, num_param, max_param,
87                                         data, num_data, max_data);
88         if (state->req == NULL) {
89                 talloc_free(state);
90                 return NULL;
91         }
92
93         talloc_reparent(state, state->req, state->ptr);
94         talloc_set_destructor(state, cli_trans_state_destructor);
95         talloc_set_destructor(state->ptr, cli_trans_state_ptr_destructor);
96
97         return state->req;
98 }
99
100 NTSTATUS cli_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
101                         uint16_t *recv_flags2,
102                         uint16_t **setup, uint8_t min_setup,
103                         uint8_t *num_setup,
104                         uint8_t **param, uint32_t min_param,
105                         uint32_t *num_param,
106                         uint8_t **data, uint32_t min_data,
107                         uint32_t *num_data)
108 {
109         NTSTATUS status;
110         void *parent = talloc_parent(req);
111         struct cli_trans_state *state =
112                 talloc_get_type(parent,
113                 struct cli_trans_state);
114         bool map_dos_errors = true;
115
116         status = smb1cli_trans_recv(req, mem_ctx, recv_flags2,
117                                     setup, min_setup, num_setup,
118                                     param, min_param, num_param,
119                                     data, min_data, num_data);
120
121         if (state) {
122                 map_dos_errors = state->cli->map_dos_errors;
123                 state->cli->raw_status = status;
124                 talloc_free(state->ptr);
125                 state = NULL;
126         }
127
128         if (NT_STATUS_IS_DOS(status) && map_dos_errors) {
129                 uint8_t eclass = NT_STATUS_DOS_CLASS(status);
130                 uint16_t ecode = NT_STATUS_DOS_CODE(status);
131                 /*
132                  * TODO: is it really a good idea to do a mapping here?
133                  *
134                  * The old cli_pull_error() also does it, so I do not change
135                  * the behavior yet.
136                  */
137                 status = dos_to_ntstatus(eclass, ecode);
138         }
139
140         return status;
141 }
142
143 NTSTATUS cli_trans(TALLOC_CTX *mem_ctx, struct cli_state *cli,
144                    uint8_t trans_cmd,
145                    const char *pipe_name, uint16_t fid, uint16_t function,
146                    int flags,
147                    uint16_t *setup, uint8_t num_setup, uint8_t max_setup,
148                    uint8_t *param, uint32_t num_param, uint32_t max_param,
149                    uint8_t *data, uint32_t num_data, uint32_t max_data,
150                    uint16_t *recv_flags2,
151                    uint16_t **rsetup, uint8_t min_rsetup, uint8_t *num_rsetup,
152                    uint8_t **rparam, uint32_t min_rparam, uint32_t *num_rparam,
153                    uint8_t **rdata, uint32_t min_rdata, uint32_t *num_rdata)
154 {
155         NTSTATUS status;
156         uint8_t additional_flags = 0;
157         uint8_t clear_flags = 0;
158         uint16_t additional_flags2 = 0;
159         uint16_t clear_flags2 = 0;
160
161         status = smb1cli_trans(mem_ctx,
162                                cli->conn, trans_cmd,
163                                additional_flags, clear_flags,
164                                additional_flags2, clear_flags2,
165                                cli->timeout,
166                                cli->smb1.pid,
167                                cli->smb1.tcon,
168                                cli->smb1.session,
169                                pipe_name, fid, function, flags,
170                                setup, num_setup, max_setup,
171                                param, num_param, max_param,
172                                data, num_data, max_data,
173                                recv_flags2,
174                                rsetup, min_rsetup, num_rsetup,
175                                rparam, min_rparam, num_rparam,
176                                rdata, min_rdata, num_rdata);
177
178         cli->raw_status = status;
179
180         if (NT_STATUS_IS_DOS(status) && cli->map_dos_errors) {
181                 uint8_t eclass = NT_STATUS_DOS_CLASS(status);
182                 uint16_t ecode = NT_STATUS_DOS_CODE(status);
183                 /*
184                  * TODO: is it really a good idea to do a mapping here?
185                  *
186                  * The old cli_pull_error() also does it, so I do not change
187                  * the behavior yet.
188                  */
189                 status = dos_to_ntstatus(eclass, ecode);
190         }
191
192         return status;
193 }