summaryrefslogtreecommitdiff
path: root/source4/libcli/raw/rawnegotiate.c
blob: cec081c364a41454489f98dd1df148ec0fc073dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* 
   Unix SMB/CIFS implementation.

   SMB client negotiate context management functions

   Copyright (C) Andrew Tridgell 1994-2005
   Copyright (C) James Myers 2003 <myersjj@samba.org>
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include <tevent.h>
#include "system/time.h"
#include "libcli/raw/libcliraw.h"
#include "libcli/raw/raw_proto.h"
#include "../libcli/smb/smbXcli_base.h"
#include "../lib/util/tevent_ntstatus.h"

NTSTATUS smb_raw_negotiate_fill_transport(struct smbcli_transport *transport)
{
	struct smbcli_negotiate *n = &transport->negotiate;
	struct smbXcli_conn *c = transport->conn;
	NTTIME ntt;

	n->protocol = smbXcli_conn_protocol(c);
	if (n->protocol > PROTOCOL_NT1) {
		return NT_STATUS_REVISION_MISMATCH;
	}

	n->sec_mode = smb1cli_conn_server_security_mode(c);
	n->max_mux  = smbXcli_conn_max_requests(c);
	n->max_xmit = smb1cli_conn_max_xmit(c);
	n->sesskey  = smb1cli_conn_server_session_key(c);
	n->capabilities = smb1cli_conn_capabilities(c);;

	/* this time arrives in real GMT */
	ntt = smbXcli_conn_server_system_time(c);
	n->server_time = nt_time_to_unix(ntt);
	n->server_zone = smb1cli_conn_server_time_zone(c);

	if (n->capabilities & CAP_EXTENDED_SECURITY) {
		const DATA_BLOB *b = smbXcli_conn_server_gss_blob(c);
		if (b) {
			n->secblob = *b;
		}
	} else {
		const uint8_t *p = smb1cli_conn_server_challenge(c);
		if (p) {
			n->secblob = data_blob_const(p, 8);
		}
	}

	n->readbraw_supported = smb1cli_conn_server_readbraw(c);
	n->readbraw_supported = smb1cli_conn_server_writebraw(c);
	n->lockread_supported = smb1cli_conn_server_lockread(c);

	return NT_STATUS_OK;
}

struct smb_raw_negotiate_state {
	struct smbcli_transport *transport;
};

static void smb_raw_negotiate_done(struct tevent_req *subreq);

struct tevent_req *smb_raw_negotiate_send(TALLOC_CTX *mem_ctx,
					  struct tevent_context *ev,
					  struct smbcli_transport *transport,
					  int minprotocol,
					  int maxprotocol)
{
	struct tevent_req *req;
	struct smb_raw_negotiate_state *state;
	struct tevent_req *subreq;
	uint32_t timeout_msec = transport->options.request_timeout * 1000;

	req = tevent_req_create(mem_ctx, &state,
				struct smb_raw_negotiate_state);;
	if (req == NULL) {
		return NULL;
	}
	state->transport = transport;

	if (maxprotocol > PROTOCOL_NT1) {
		maxprotocol = PROTOCOL_NT1;
	}

	if (minprotocol > maxprotocol) {
		minprotocol = maxprotocol;
	}

	subreq = smbXcli_negprot_send(state, ev,
				      transport->conn,
				      timeout_msec,
				      minprotocol,
				      maxprotocol,
				      transport->options.max_credits);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, smb_raw_negotiate_done, req);

	return req;
}

static void smb_raw_negotiate_done(struct tevent_req *subreq)
{
	struct tevent_req *req =
		tevent_req_callback_data(subreq,
		struct tevent_req);
	struct smb_raw_negotiate_state *state =
		tevent_req_data(req,
		struct smb_raw_negotiate_state);
	NTSTATUS status;

	status = smbXcli_negprot_recv(subreq);
	TALLOC_FREE(subreq);
	if (tevent_req_nterror(req, status)) {
		return;
	}

	status = smb_raw_negotiate_fill_transport(state->transport);
	if (tevent_req_nterror(req, status)) {
		return;
	}

	tevent_req_done(req);
}

/*
 Send a negprot command.
*/
NTSTATUS smb_raw_negotiate_recv(struct tevent_req *req)
{
	return tevent_req_simple_recv_ntstatus(req);
}


/*
 Send a negprot command (sync interface)
*/
NTSTATUS smb_raw_negotiate(struct smbcli_transport *transport, bool unicode,
			   int minprotocol, int maxprotocol)
{
	NTSTATUS status = NT_STATUS_INTERNAL_ERROR;
	struct tevent_req *subreq = NULL;
	bool ok;

	subreq = smb_raw_negotiate_send(transport,
					transport->ev,
					transport,
					minprotocol,
					maxprotocol);
	if (subreq == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	ok = tevent_req_poll(subreq, transport->ev);
	if (!ok) {
		status = map_nt_error_from_unix_common(errno);
		goto failed;
	}

	status = smb_raw_negotiate_recv(subreq);

failed:
	TALLOC_FREE(subreq);
	return status;
}