summaryrefslogtreecommitdiffstats
path: root/source4/libcli/raw/clisocket.c
blob: 688ee8a78b294e006354246f68d0a4356b1b012f (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* 
   Unix SMB/CIFS implementation.

   SMB client socket 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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "lib/events/events.h"
#include "libcli/raw/libcliraw.h"
#include "libcli/composite/composite.h"
#include "lib/socket/socket.h"

/*
  this private structure is used during async connection handling
*/
struct clisocket_connect {
	int port_num;
	int *iports;
	struct smbcli_socket *sock;
	const char *dest_host_addr;
	const char *dest_hostname;
};


/*
  create a smbcli_socket context
  The event_ctx is optional - if not supplied one will be created
*/
struct smbcli_socket *smbcli_sock_init(TALLOC_CTX *mem_ctx, 
				       struct event_context *event_ctx)
{
	struct smbcli_socket *sock;

	sock = talloc_zero(mem_ctx, struct smbcli_socket);
	if (!sock) {
		return NULL;
	}

	if (event_ctx) {
		sock->event.ctx = talloc_reference(sock, event_ctx);
	} else {
		sock->event.ctx = event_context_init(sock);
	}
	if (sock->event.ctx == NULL) {
		talloc_free(sock);
		return NULL;
	}

	/* ensure we don't get SIGPIPE */
	BlockSignals(True,SIGPIPE);

	return sock;
}

static NTSTATUS smbcli_sock_connect_one(struct smbcli_socket *sock, 
					const char *hostaddr, int port, 
					struct composite_context *c);

/*
  handle socket write events during an async connect. These happen when the OS
  has either completed the connect() or has returned an error
*/
static void smbcli_sock_connect_handler(struct event_context *ev, struct fd_event *fde, 
					uint16_t flags, void *private_data)
{
	struct composite_context *c = talloc_get_type(private_data, struct composite_context);
	struct clisocket_connect *conn = talloc_get_type(c->private_data, struct clisocket_connect);
	int i;
	
	c->status = socket_connect_complete(conn->sock->sock, 0);
	if (NT_STATUS_IS_OK(c->status)) {
		socket_set_option(conn->sock->sock, lp_socket_options(), NULL);
		conn->sock->hostname = talloc_strdup(conn->sock, conn->dest_hostname);
		c->state = COMPOSITE_STATE_DONE;
		if (c->async.fn) {
			c->async.fn(c);
		}
		return;
	}

	/* that port failed - try the next port */
	for (i=conn->port_num+1;conn->iports[i];i++) {
		conn->port_num = i;
		c->status = smbcli_sock_connect_one(conn->sock, 
						    conn->dest_host_addr, 
						    conn->iports[i], c);
		if (NT_STATUS_IS_OK(c->status) ||
		    NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
			return;
		}
	}

	c->state = COMPOSITE_STATE_ERROR;
	if (c->async.fn) {
		c->async.fn(c);
	}
}


/*
  try to connect to the given address/port
*/
static NTSTATUS smbcli_sock_connect_one(struct smbcli_socket *sock, 
					const char *hostaddr, int port,
					struct composite_context *c)
{
	NTSTATUS status;

	if (sock->sock) {
		talloc_free(sock->sock);
		sock->sock = NULL;
	}
	talloc_free(sock->event.fde);

	status = socket_create("ip", SOCKET_TYPE_STREAM, &sock->sock, 0);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	talloc_steal(sock, sock->sock);

	/* we initially look for write - see the man page on
	   non-blocking connect */
	sock->event.fde = event_add_fd(sock->event.ctx, sock, socket_get_fd(sock->sock), 
				       EVENT_FD_WRITE, smbcli_sock_connect_handler, c);

	sock->port = port;
	set_blocking(socket_get_fd(sock->sock), False);

	return socket_connect(sock->sock, NULL, 0, hostaddr, port, 0);
}
					

/*
  connect a smbcli_socket context to an IP/port pair
  if port is 0 then choose 445 then 139

  this is the async send side of the interface
*/
struct composite_context *smbcli_sock_connect_send(struct smbcli_socket *sock, 
						   const char *host_addr, int port,
						   const char *host_name)
{
	struct composite_context *c;
	struct clisocket_connect *conn;
	int i;

	c = talloc_zero(sock, struct composite_context);
	if (c == NULL) return NULL;

	c->event_ctx = sock->event.ctx;

	conn = talloc(c, struct clisocket_connect);
	if (conn == NULL) goto failed;

	conn->sock = sock;

	/* work out what ports we will try */
	if (port == 0) {
		const char **ports = lp_smb_ports();
		for (i=0;ports[i];i++) /* noop */ ;
		conn->iports = talloc_array(c, int, i+1);
		if (conn->iports == NULL) goto failed;
		for (i=0;ports[i];i++) {
			conn->iports[i] = atoi(ports[i]);
		}
		conn->iports[i] = 0;
	} else {
		conn->iports = talloc_array(c, int, 2);
		if (conn->iports == NULL) goto failed;
		conn->iports[0] = port;
		conn->iports[1] = 0;
	}

	conn->dest_host_addr = talloc_strdup(c, host_addr);
	if (conn->dest_host_addr == NULL) goto failed;

	conn->dest_hostname = talloc_strdup(c, host_name);
	if (conn->dest_hostname == NULL) goto failed;

	c->private_data	= conn;
	c->state	= COMPOSITE_STATE_IN_PROGRESS;

	/* startup the connect process for each port in turn until one
	   succeeds or tells us that it is pending */
	for (i=0;conn->iports[i];i++) {
		conn->port_num = i;
		conn->sock->port = conn->iports[i];
		c->status = smbcli_sock_connect_one(sock, 
						    conn->dest_host_addr, 
						    conn->iports[i], c);
		if (NT_STATUS_IS_OK(c->status) ||
		    NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
			return c;
		}
	}

	c->state = COMPOSITE_STATE_ERROR;
	return c;
	
failed:
	talloc_free(c);
	return NULL;
}

/*
  finish a smbcli_sock_connect_send() operation
*/
NTSTATUS smbcli_sock_connect_recv(struct composite_context *c)
{
	NTSTATUS status;
	status = composite_wait(c);
	talloc_free(c);
	return status;
}

/*
  connect a smbcli_socket context to an IP/port pair
  if port is 0 then choose the ports listed in smb.conf (normally 445 then 139)

  sync version of the function
*/
NTSTATUS smbcli_sock_connect(struct smbcli_socket *sock, const char *host_addr, int port,
			     const char *host_name)
{
	struct composite_context *c;

	c = smbcli_sock_connect_send(sock, host_addr, port, host_name);
	if (c == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	return smbcli_sock_connect_recv(c);
}


/****************************************************************************
 mark the socket as dead
****************************************************************************/
void smbcli_sock_dead(struct smbcli_socket *sock)
{
	if (sock->sock != NULL) {
		talloc_free(sock->sock);
		sock->sock = NULL;
	}
}

/****************************************************************************
 Set socket options on a open connection.
****************************************************************************/
void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
{
	socket_set_option(sock->sock, options, NULL);
}

/****************************************************************************
 Write to socket. Return amount written.
****************************************************************************/
NTSTATUS smbcli_sock_write(struct smbcli_socket *sock, const uint8_t *data, 
			   size_t len, size_t *nsent)
{
	DATA_BLOB blob;

	if (sock->sock == NULL) {
		return NT_STATUS_CONNECTION_DISCONNECTED;
	}

	blob.data = discard_const(data);
	blob.length = len;

	return socket_send(sock->sock, &blob, nsent, 0);
}


/****************************************************************************
 Read from socket. return amount read
****************************************************************************/
NTSTATUS smbcli_sock_read(struct smbcli_socket *sock, uint8_t *data, 
			  size_t len, size_t *nread)
{
	if (sock->sock == NULL) {
		return NT_STATUS_CONNECTION_DISCONNECTED;
	}

	return socket_recv(sock->sock, data, len, nread, 0);
}


/****************************************************************************
resolve a hostname and connect 
****************************************************************************/
BOOL smbcli_sock_connect_byname(struct smbcli_socket *sock, const char *host, int port)
{
	int name_type = NBT_NAME_SERVER;
	const char *address;
	NTSTATUS status;
	struct nbt_name nbt_name;
	char *name, *p;

	name = talloc_strdup(sock, host);

	/* allow hostnames of the form NAME#xx and do a netbios lookup */
	if ((p = strchr(name, '#'))) {
		name_type = strtol(p+1, NULL, 16);
		*p = 0;
	}

	nbt_name.name = name;
	nbt_name.type = name_type;
	nbt_name.scope = NULL;
	
	status = resolve_name(&nbt_name, sock, &address, sock->event.ctx);
	if (!NT_STATUS_IS_OK(status)) {
		return False;
	}

	status = smbcli_sock_connect(sock, address, port, name);

	return NT_STATUS_IS_OK(status);
}