2 Unix SMB/CIFS implementation.
3 SMB client session context management functions
5 Copyright (C) Andrew Tridgell 1994-2005
6 Copyright (C) James Myers 2003 <myersjj@samba.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/raw/raw_proto.h"
25 #include "system/filesys.h"
26 #include "../libcli/smb/smbXcli_base.h"
28 #define SETUP_REQUEST_SESSION(cmd, wct, buflen) do { \
29 req = smbcli_request_setup_session(session, cmd, wct, buflen); \
30 if (!req) return NULL; \
34 /****************************************************************************
35 Initialize the session context
36 ****************************************************************************/
37 struct smbcli_session
*smbcli_session_init(struct smbcli_transport
*transport
,
38 TALLOC_CTX
*parent_ctx
, bool primary
,
39 struct smbcli_session_options options
)
41 struct smbcli_session
*session
;
43 uint32_t capabilities
;
45 session
= talloc_zero(parent_ctx
, struct smbcli_session
);
51 session
->transport
= talloc_steal(session
, transport
);
53 session
->transport
= talloc_reference(session
, transport
);
55 session
->pid
= (uint16_t)getpid();
56 session
->vuid
= UID_FIELD_INVALID
;
57 session
->options
= options
;
59 capabilities
= transport
->negotiate
.capabilities
;
61 flags2
= FLAGS2_LONG_PATH_COMPONENTS
| FLAGS2_EXTENDED_ATTRIBUTES
;
63 if (capabilities
& CAP_UNICODE
) {
64 flags2
|= FLAGS2_UNICODE_STRINGS
;
66 if (capabilities
& CAP_STATUS32
) {
67 flags2
|= FLAGS2_32_BIT_ERROR_CODES
;
69 if (capabilities
& CAP_EXTENDED_SECURITY
) {
70 flags2
|= FLAGS2_EXTENDED_SECURITY
;
72 if (smb1cli_conn_signing_is_active(session
->transport
->conn
)) {
73 flags2
|= FLAGS2_SMB_SECURITY_SIGNATURES
;
76 session
->flags2
= flags2
;
81 /****************************************************************************
82 Perform a session setup (async send)
83 ****************************************************************************/
84 struct smbcli_request
*smb_raw_sesssetup_send(struct smbcli_session
*session
,
85 union smb_sesssetup
*parms
)
87 struct smbcli_request
*req
= NULL
;
89 switch (parms
->old
.level
) {
90 case RAW_SESSSETUP_OLD
:
91 SETUP_REQUEST_SESSION(SMBsesssetupX
, 10, 0);
92 SSVAL(req
->out
.vwv
, VWV(0), SMB_CHAIN_NONE
);
93 SSVAL(req
->out
.vwv
, VWV(1), 0);
94 SSVAL(req
->out
.vwv
,VWV(2),parms
->old
.in
.bufsize
);
95 SSVAL(req
->out
.vwv
,VWV(3),parms
->old
.in
.mpx_max
);
96 SSVAL(req
->out
.vwv
,VWV(4),parms
->old
.in
.vc_num
);
97 SIVAL(req
->out
.vwv
,VWV(5),parms
->old
.in
.sesskey
);
98 SSVAL(req
->out
.vwv
,VWV(7),parms
->old
.in
.password
.length
);
99 SIVAL(req
->out
.vwv
,VWV(8), 0); /* reserved */
100 smbcli_req_append_blob(req
, &parms
->old
.in
.password
);
101 smbcli_req_append_string(req
, parms
->old
.in
.user
, STR_TERMINATE
);
102 smbcli_req_append_string(req
, parms
->old
.in
.domain
, STR_TERMINATE
|STR_UPPER
);
103 smbcli_req_append_string(req
, parms
->old
.in
.os
, STR_TERMINATE
);
104 smbcli_req_append_string(req
, parms
->old
.in
.lanman
, STR_TERMINATE
);
107 case RAW_SESSSETUP_NT1
:
108 SETUP_REQUEST_SESSION(SMBsesssetupX
, 13, 0);
109 SSVAL(req
->out
.vwv
, VWV(0), SMB_CHAIN_NONE
);
110 SSVAL(req
->out
.vwv
, VWV(1), 0);
111 SSVAL(req
->out
.vwv
, VWV(2), parms
->nt1
.in
.bufsize
);
112 SSVAL(req
->out
.vwv
, VWV(3), parms
->nt1
.in
.mpx_max
);
113 SSVAL(req
->out
.vwv
, VWV(4), parms
->nt1
.in
.vc_num
);
114 SIVAL(req
->out
.vwv
, VWV(5), parms
->nt1
.in
.sesskey
);
115 SSVAL(req
->out
.vwv
, VWV(7), parms
->nt1
.in
.password1
.length
);
116 SSVAL(req
->out
.vwv
, VWV(8), parms
->nt1
.in
.password2
.length
);
117 SIVAL(req
->out
.vwv
, VWV(9), 0); /* reserved */
118 SIVAL(req
->out
.vwv
, VWV(11), parms
->nt1
.in
.capabilities
);
119 smbcli_req_append_blob(req
, &parms
->nt1
.in
.password1
);
120 smbcli_req_append_blob(req
, &parms
->nt1
.in
.password2
);
121 smbcli_req_append_string(req
, parms
->nt1
.in
.user
, STR_TERMINATE
);
122 smbcli_req_append_string(req
, parms
->nt1
.in
.domain
, STR_TERMINATE
|STR_UPPER
);
123 smbcli_req_append_string(req
, parms
->nt1
.in
.os
, STR_TERMINATE
);
124 smbcli_req_append_string(req
, parms
->nt1
.in
.lanman
, STR_TERMINATE
);
127 case RAW_SESSSETUP_SPNEGO
:
128 SETUP_REQUEST_SESSION(SMBsesssetupX
, 12, 0);
129 SSVAL(req
->out
.vwv
, VWV(0), SMB_CHAIN_NONE
);
130 SSVAL(req
->out
.vwv
, VWV(1), 0);
131 SSVAL(req
->out
.vwv
, VWV(2), parms
->spnego
.in
.bufsize
);
132 SSVAL(req
->out
.vwv
, VWV(3), parms
->spnego
.in
.mpx_max
);
133 SSVAL(req
->out
.vwv
, VWV(4), parms
->spnego
.in
.vc_num
);
134 SIVAL(req
->out
.vwv
, VWV(5), parms
->spnego
.in
.sesskey
);
135 SSVAL(req
->out
.vwv
, VWV(7), parms
->spnego
.in
.secblob
.length
);
136 SIVAL(req
->out
.vwv
, VWV(8), 0); /* reserved */
137 SIVAL(req
->out
.vwv
, VWV(10), parms
->spnego
.in
.capabilities
);
138 smbcli_req_append_blob(req
, &parms
->spnego
.in
.secblob
);
139 smbcli_req_append_string(req
, parms
->spnego
.in
.os
, STR_TERMINATE
);
140 smbcli_req_append_string(req
, parms
->spnego
.in
.lanman
, STR_TERMINATE
);
141 smbcli_req_append_string(req
, parms
->spnego
.in
.workgroup
, STR_TERMINATE
);
144 case RAW_SESSSETUP_SMB2
:
148 if (!smbcli_request_send(req
)) {
149 smbcli_request_destroy(req
);
157 /****************************************************************************
158 Perform a session setup (async recv)
159 ****************************************************************************/
160 NTSTATUS
smb_raw_sesssetup_recv(struct smbcli_request
*req
,
162 union smb_sesssetup
*parms
)
167 if (!smbcli_request_receive(req
)) {
168 return smbcli_request_destroy(req
);
171 if (!NT_STATUS_IS_OK(req
->status
) &&
172 !NT_STATUS_EQUAL(req
->status
,NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
173 return smbcli_request_destroy(req
);
176 switch (parms
->old
.level
) {
177 case RAW_SESSSETUP_OLD
:
178 SMBCLI_CHECK_WCT(req
, 3);
179 ZERO_STRUCT(parms
->old
.out
);
180 parms
->old
.out
.vuid
= SVAL(req
->in
.hdr
, HDR_UID
);
181 parms
->old
.out
.action
= SVAL(req
->in
.vwv
, VWV(2));
184 p
+= smbcli_req_pull_string(&req
->in
.bufinfo
, mem_ctx
, &parms
->old
.out
.os
, p
, -1, STR_TERMINATE
);
185 p
+= smbcli_req_pull_string(&req
->in
.bufinfo
, mem_ctx
, &parms
->old
.out
.lanman
, p
, -1, STR_TERMINATE
);
186 p
+= smbcli_req_pull_string(&req
->in
.bufinfo
, mem_ctx
, &parms
->old
.out
.domain
, p
, -1, STR_TERMINATE
);
190 case RAW_SESSSETUP_NT1
:
191 SMBCLI_CHECK_WCT(req
, 3);
192 ZERO_STRUCT(parms
->nt1
.out
);
193 parms
->nt1
.out
.vuid
= SVAL(req
->in
.hdr
, HDR_UID
);
194 parms
->nt1
.out
.action
= SVAL(req
->in
.vwv
, VWV(2));
197 p
+= smbcli_req_pull_string(&req
->in
.bufinfo
, mem_ctx
, &parms
->nt1
.out
.os
, p
, -1, STR_TERMINATE
);
198 p
+= smbcli_req_pull_string(&req
->in
.bufinfo
, mem_ctx
, &parms
->nt1
.out
.lanman
, p
, -1, STR_TERMINATE
);
199 if (p
< (req
->in
.data
+ req
->in
.data_size
)) {
200 p
+= smbcli_req_pull_string(&req
->in
.bufinfo
, mem_ctx
, &parms
->nt1
.out
.domain
, p
, -1, STR_TERMINATE
);
205 case RAW_SESSSETUP_SPNEGO
:
206 SMBCLI_CHECK_WCT(req
, 4);
207 ZERO_STRUCT(parms
->spnego
.out
);
208 parms
->spnego
.out
.vuid
= SVAL(req
->in
.hdr
, HDR_UID
);
209 parms
->spnego
.out
.action
= SVAL(req
->in
.vwv
, VWV(2));
210 len
= SVAL(req
->in
.vwv
, VWV(3));
216 parms
->spnego
.out
.secblob
= smbcli_req_pull_blob(&req
->in
.bufinfo
, mem_ctx
, p
, len
);
217 p
+= parms
->spnego
.out
.secblob
.length
;
218 p
+= smbcli_req_pull_string(&req
->in
.bufinfo
, mem_ctx
, &parms
->spnego
.out
.os
, p
, -1, STR_TERMINATE
);
219 p
+= smbcli_req_pull_string(&req
->in
.bufinfo
, mem_ctx
, &parms
->spnego
.out
.lanman
, p
, -1, STR_TERMINATE
);
220 p
+= smbcli_req_pull_string(&req
->in
.bufinfo
, mem_ctx
, &parms
->spnego
.out
.workgroup
, p
, -1, STR_TERMINATE
);
223 case RAW_SESSSETUP_SMB2
:
224 req
->status
= NT_STATUS_INTERNAL_ERROR
;
229 return smbcli_request_destroy(req
);
234 Perform a session setup (sync interface)
236 NTSTATUS
smb_raw_sesssetup(struct smbcli_session
*session
,
237 TALLOC_CTX
*mem_ctx
, union smb_sesssetup
*parms
)
239 struct smbcli_request
*req
= smb_raw_sesssetup_send(session
, parms
);
240 return smb_raw_sesssetup_recv(req
, mem_ctx
, parms
);
244 /****************************************************************************
245 Send a ulogoff (async send)
246 *****************************************************************************/
247 struct smbcli_request
*smb_raw_ulogoff_send(struct smbcli_session
*session
)
249 struct smbcli_request
*req
;
251 SETUP_REQUEST_SESSION(SMBulogoffX
, 2, 0);
253 SSVAL(req
->out
.vwv
, VWV(0), SMB_CHAIN_NONE
);
254 SSVAL(req
->out
.vwv
, VWV(1), 0);
256 if (!smbcli_request_send(req
)) {
257 smbcli_request_destroy(req
);
264 /****************************************************************************
265 Send a ulogoff (sync interface)
266 *****************************************************************************/
267 NTSTATUS
smb_raw_ulogoff(struct smbcli_session
*session
)
269 struct smbcli_request
*req
= smb_raw_ulogoff_send(session
);
270 return smbcli_request_simple_recv(req
);
274 /****************************************************************************
275 Send a exit (async send)
276 *****************************************************************************/
277 struct smbcli_request
*smb_raw_exit_send(struct smbcli_session
*session
)
279 struct smbcli_request
*req
;
281 SETUP_REQUEST_SESSION(SMBexit
, 0, 0);
283 if (!smbcli_request_send(req
)) {
284 smbcli_request_destroy(req
);
291 /****************************************************************************
292 Send a exit (sync interface)
293 *****************************************************************************/
294 _PUBLIC_ NTSTATUS
smb_raw_exit(struct smbcli_session
*session
)
296 struct smbcli_request
*req
= smb_raw_exit_send(session
);
297 return smbcli_request_simple_recv(req
);