08fda5fdb5287edb91b67256f41a4ccefaef272a
[obnox/samba/samba-obnox.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, 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 additional_flags2 = 0;
63         uint16_t clear_flags2 = 0;
64
65         state = talloc_zero(mem_ctx, struct cli_trans_state);
66         if (state == NULL) {
67                 return NULL;
68         }
69         state->cli = cli;
70         state->ptr = talloc(state, struct cli_trans_state *);
71         if (state->ptr == NULL) {
72                 talloc_free(state);
73                 return NULL;
74         }
75         *state->ptr = state;
76         if (cli->case_sensitive) {
77                 clear_flags |= FLAG_CASELESS_PATHNAMES;
78         } else {
79                 /* Default setting, case insensitive. */
80                 additional_flags |= FLAG_CASELESS_PATHNAMES;
81         }
82
83         if ((smb1cli_conn_capabilities(cli->conn) & CAP_DFS) && cli->dfsroot) {
84                 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
85         }
86
87         state->req = smb1cli_trans_send(state, ev,
88                                         cli->conn, cmd,
89                                         additional_flags, clear_flags,
90                                         additional_flags2, clear_flags2,
91                                         cli->timeout,
92                                         cli->smb1.pid,
93                                         cli->smb1.tcon,
94                                         cli->smb1.session,
95                                         pipe_name, fid, function, flags,
96                                         setup, num_setup, max_setup,
97                                         param, num_param, max_param,
98                                         data, num_data, max_data);
99         if (state->req == NULL) {
100                 talloc_free(state);
101                 return NULL;
102         }
103
104         talloc_reparent(state, state->req, state->ptr);
105         talloc_set_destructor(state, cli_trans_state_destructor);
106         talloc_set_destructor(state->ptr, cli_trans_state_ptr_destructor);
107
108         return state->req;
109 }
110
111 NTSTATUS cli_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
112                         uint16_t *recv_flags2,
113                         uint16_t **setup, uint8_t min_setup,
114                         uint8_t *num_setup,
115                         uint8_t **param, uint32_t min_param,
116                         uint32_t *num_param,
117                         uint8_t **data, uint32_t min_data,
118                         uint32_t *num_data)
119 {
120         NTSTATUS status;
121         void *parent = talloc_parent(req);
122         struct cli_trans_state *state =
123                 talloc_get_type(parent,
124                 struct cli_trans_state);
125         bool map_dos_errors = true;
126
127         status = smb1cli_trans_recv(req, mem_ctx, recv_flags2,
128                                     setup, min_setup, num_setup,
129                                     param, min_param, num_param,
130                                     data, min_data, num_data);
131
132         if (state) {
133                 map_dos_errors = state->cli->map_dos_errors;
134                 state->cli->raw_status = status;
135                 talloc_free(state->ptr);
136                 state = NULL;
137         }
138
139         if (NT_STATUS_IS_DOS(status) && map_dos_errors) {
140                 uint8_t eclass = NT_STATUS_DOS_CLASS(status);
141                 uint16_t ecode = NT_STATUS_DOS_CODE(status);
142                 /*
143                  * TODO: is it really a good idea to do a mapping here?
144                  *
145                  * The old cli_pull_error() also does it, so I do not change
146                  * the behavior yet.
147                  */
148                 status = dos_to_ntstatus(eclass, ecode);
149         }
150
151         return status;
152 }
153
154 NTSTATUS cli_trans(TALLOC_CTX *mem_ctx, struct cli_state *cli,
155                    uint8_t trans_cmd,
156                    const char *pipe_name, uint16_t fid, uint16_t function,
157                    int flags,
158                    uint16_t *setup, uint8_t num_setup, uint8_t max_setup,
159                    uint8_t *param, uint32_t num_param, uint32_t max_param,
160                    uint8_t *data, uint32_t num_data, uint32_t max_data,
161                    uint16_t *recv_flags2,
162                    uint16_t **rsetup, uint8_t min_rsetup, uint8_t *num_rsetup,
163                    uint8_t **rparam, uint32_t min_rparam, uint32_t *num_rparam,
164                    uint8_t **rdata, uint32_t min_rdata, uint32_t *num_rdata)
165 {
166         NTSTATUS status;
167         uint8_t additional_flags = 0;
168         uint8_t clear_flags = 0;
169         uint16_t additional_flags2 = 0;
170         uint16_t clear_flags2 = 0;
171
172         if (cli->case_sensitive) {
173                 clear_flags |= FLAG_CASELESS_PATHNAMES;
174         } else {
175                 /* Default setting, case insensitive. */
176                 additional_flags |= FLAG_CASELESS_PATHNAMES;
177         }
178
179         if ((smb1cli_conn_capabilities(cli->conn) & CAP_DFS) && cli->dfsroot) {
180                 additional_flags2 |= FLAGS2_DFS_PATHNAMES;
181         }
182
183         status = smb1cli_trans(mem_ctx,
184                                cli->conn, trans_cmd,
185                                additional_flags, clear_flags,
186                                additional_flags2, clear_flags2,
187                                cli->timeout,
188                                cli->smb1.pid,
189                                cli->smb1.tcon,
190                                cli->smb1.session,
191                                pipe_name, fid, function, flags,
192                                setup, num_setup, max_setup,
193                                param, num_param, max_param,
194                                data, num_data, max_data,
195                                recv_flags2,
196                                rsetup, min_rsetup, num_rsetup,
197                                rparam, min_rparam, num_rparam,
198                                rdata, min_rdata, num_rdata);
199
200         cli->raw_status = status;
201
202         if (NT_STATUS_IS_DOS(status) && cli->map_dos_errors) {
203                 uint8_t eclass = NT_STATUS_DOS_CLASS(status);
204                 uint16_t ecode = NT_STATUS_DOS_CODE(status);
205                 /*
206                  * TODO: is it really a good idea to do a mapping here?
207                  *
208                  * The old cli_pull_error() also does it, so I do not change
209                  * the behavior yet.
210                  */
211                 status = dos_to_ntstatus(eclass, ecode);
212         }
213
214         return status;
215 }