1/* 2 Unix SMB/CIFS implementation. 3 4 SMB client tree context management functions 5 6 Copyright (C) Andrew Tridgell 1994-2005 7 Copyright (C) James Myers 2003 <myersjj@samba.org> 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program. If not, see <http://www.gnu.org/licenses/>. 21*/ 22 23#include "includes.h" 24#include "libcli/raw/libcliraw.h" 25#include "libcli/raw/raw_proto.h" 26#include "libcli/smb_composite/smb_composite.h" 27 28#define SETUP_REQUEST_TREE(cmd, wct, buflen) do { \ 29 req = smbcli_request_setup(tree, cmd, wct, buflen); \ 30 if (!req) return NULL; \ 31} while (0) 32 33/**************************************************************************** 34 Initialize the tree context 35****************************************************************************/ 36_PUBLIC_ struct smbcli_tree *smbcli_tree_init(struct smbcli_session *session, 37 TALLOC_CTX *parent_ctx, bool primary) 38{ 39 struct smbcli_tree *tree; 40 41 tree = talloc_zero(parent_ctx, struct smbcli_tree); 42 if (!tree) { 43 return NULL; 44 } 45 46 if (primary) { 47 tree->session = talloc_steal(tree, session); 48 } else { 49 tree->session = talloc_reference(tree, session); 50 } 51 52 53 return tree; 54} 55 56/**************************************************************************** 57 Send a tconX (async send) 58****************************************************************************/ 59struct smbcli_request *smb_raw_tcon_send(struct smbcli_tree *tree, 60 union smb_tcon *parms) 61{ 62 struct smbcli_request *req = NULL; 63 64 switch (parms->tcon.level) { 65 case RAW_TCON_TCON: 66 SETUP_REQUEST_TREE(SMBtcon, 0, 0); 67 smbcli_req_append_ascii4(req, parms->tcon.in.service, STR_ASCII); 68 smbcli_req_append_ascii4(req, parms->tcon.in.password,STR_ASCII); 69 smbcli_req_append_ascii4(req, parms->tcon.in.dev, STR_ASCII); 70 break; 71 72 case RAW_TCON_TCONX: 73 SETUP_REQUEST_TREE(SMBtconX, 4, 0); 74 SSVAL(req->out.vwv, VWV(0), 0xFF); 75 SSVAL(req->out.vwv, VWV(1), 0); 76 SSVAL(req->out.vwv, VWV(2), parms->tconx.in.flags); 77 SSVAL(req->out.vwv, VWV(3), parms->tconx.in.password.length); 78 smbcli_req_append_blob(req, &parms->tconx.in.password); 79 smbcli_req_append_string(req, parms->tconx.in.path, STR_TERMINATE | STR_UPPER); 80 smbcli_req_append_string(req, parms->tconx.in.device, STR_TERMINATE | STR_ASCII); 81 break; 82 83 case RAW_TCON_SMB2: 84 return NULL; 85 } 86 87 if (!smbcli_request_send(req)) { 88 smbcli_request_destroy(req); 89 return NULL; 90 } 91 92 return req; 93} 94 95/**************************************************************************** 96 Send a tconX (async recv) 97****************************************************************************/ 98NTSTATUS smb_raw_tcon_recv(struct smbcli_request *req, TALLOC_CTX *mem_ctx, 99 union smb_tcon *parms) 100{ 101 uint8_t *p; 102 103 if (!smbcli_request_receive(req) || 104 smbcli_request_is_error(req)) { 105 goto failed; 106 } 107 108 switch (parms->tcon.level) { 109 case RAW_TCON_TCON: 110 SMBCLI_CHECK_WCT(req, 2); 111 parms->tcon.out.max_xmit = SVAL(req->in.vwv, VWV(0)); 112 parms->tcon.out.tid = SVAL(req->in.vwv, VWV(1)); 113 break; 114 115 case RAW_TCON_TCONX: 116 ZERO_STRUCT(parms->tconx.out); 117 parms->tconx.out.tid = SVAL(req->in.hdr, HDR_TID); 118 if (req->in.wct >= 4) { 119 parms->tconx.out.options = SVAL(req->in.vwv, VWV(3)); 120 } 121 122 /* output is actual service name */ 123 p = req->in.data; 124 if (!p) break; 125 126 p += smbcli_req_pull_string(&req->in.bufinfo, mem_ctx, &parms->tconx.out.dev_type, 127 p, -1, STR_ASCII | STR_TERMINATE); 128 p += smbcli_req_pull_string(&req->in.bufinfo, mem_ctx, &parms->tconx.out.fs_type, 129 p, -1, STR_TERMINATE); 130 break; 131 132 case RAW_TCON_SMB2: 133 req->status = NT_STATUS_INTERNAL_ERROR; 134 break; 135 } 136 137failed: 138 return smbcli_request_destroy(req); 139} 140 141/**************************************************************************** 142 Send a tconX (sync interface) 143****************************************************************************/ 144_PUBLIC_ NTSTATUS smb_raw_tcon(struct smbcli_tree *tree, TALLOC_CTX *mem_ctx, 145 union smb_tcon *parms) 146{ 147 struct smbcli_request *req = smb_raw_tcon_send(tree, parms); 148 return smb_raw_tcon_recv(req, mem_ctx, parms); 149} 150 151 152/**************************************************************************** 153 Send a tree disconnect. 154****************************************************************************/ 155_PUBLIC_ NTSTATUS smb_tree_disconnect(struct smbcli_tree *tree) 156{ 157 struct smbcli_request *req; 158 159 if (!tree) return NT_STATUS_OK; 160 req = smbcli_request_setup(tree, SMBtdis, 0, 0); 161 162 if (smbcli_request_send(req)) { 163 (void) smbcli_request_receive(req); 164 } 165 return smbcli_request_destroy(req); 166} 167 168 169/* 170 a convenient function to establish a smbcli_tree from scratch 171*/ 172NTSTATUS smbcli_tree_full_connection(TALLOC_CTX *parent_ctx, 173 struct smbcli_tree **ret_tree, 174 const char *dest_host, const char **dest_ports, 175 const char *service, const char *service_type, 176 const char *socket_options, 177 struct cli_credentials *credentials, 178 struct resolve_context *resolve_ctx, 179 struct tevent_context *ev, 180 struct smbcli_options *options, 181 struct smbcli_session_options *session_options, 182 struct smb_iconv_convenience *iconv_convenience, 183 struct gensec_settings *gensec_settings) 184{ 185 struct smb_composite_connect io; 186 NTSTATUS status; 187 TALLOC_CTX *tmp_ctx = talloc_new(parent_ctx); 188 if (!tmp_ctx) { 189 return NT_STATUS_NO_MEMORY; 190 } 191 192 io.in.dest_host = dest_host; 193 io.in.dest_ports = dest_ports; 194 io.in.socket_options = socket_options; 195 io.in.called_name = strupper_talloc(tmp_ctx, dest_host); 196 io.in.service = service; 197 io.in.service_type = service_type; 198 io.in.credentials = credentials; 199 io.in.gensec_settings = gensec_settings; 200 io.in.fallback_to_anonymous = false; 201 202 /* This workgroup gets sent out by the SPNEGO session setup. 203 * I don't know of any servers that look at it, so we 204 * hardcode it to "". */ 205 io.in.workgroup = ""; 206 io.in.options = *options; 207 io.in.session_options = *session_options; 208 io.in.iconv_convenience = iconv_convenience; 209 210 status = smb_composite_connect(&io, parent_ctx, resolve_ctx, ev); 211 if (NT_STATUS_IS_OK(status)) { 212 *ret_tree = io.out.tree; 213 } 214 talloc_free(tmp_ctx); 215 return status; 216} 217