summaryrefslogtreecommitdiff
path: root/usr/src/lib/libsmbfs/smb/connect.c
blob: c2ccc3361d778aa9907417efaf862fc30fa12301 (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
 */

/*
 * Functions to setup connections (TCP and/or NetBIOS)
 * This has the fall-back logic for IP6, IP4, NBT
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <libintl.h>
#include <xti.h>
#include <assert.h>

#include <sys/types.h>
#include <sys/time.h>
#include <sys/byteorder.h>
#include <sys/socket.h>
#include <sys/fcntl.h>

#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>

#include <netsmb/smb.h>
#include <netsmb/smb_lib.h>
#include <netsmb/netbios.h>
#include <netsmb/nb_lib.h>
#include <netsmb/smb_dev.h>

#include "charsets.h"
#include "private.h"

/*
 * SMB messages are up to 64K.
 * Let's leave room for two.
 */
static int smb_tcpsndbuf = 0x20000;
static int smb_tcprcvbuf = 0x20000;
static int smb_connect_timeout = 30; /* seconds */
int smb_recv_timeout = 30; /* seconds */

int conn_tcp6(struct smb_ctx *, const struct sockaddr *, int);
int conn_tcp4(struct smb_ctx *, const struct sockaddr *, int);
int conn_nbt(struct smb_ctx *, const struct sockaddr *, char *);

/*
 * Internal set sockopt for int-sized options.
 * Borrowed from: libnsl/rpc/ti_opts.c
 */
static int
smb_setopt_int(int fd, int level, int name, int val)
{
	struct t_optmgmt oreq, ores;
	struct {
		struct t_opthdr oh;
		int ival;
	} opts;

	/* opt header */
	opts.oh.len = sizeof (opts);
	opts.oh.level = level;
	opts.oh.name = name;
	opts.oh.status = 0;
	opts.ival = val;

	oreq.flags = T_NEGOTIATE;
	oreq.opt.buf = (void *)&opts;
	oreq.opt.len = sizeof (opts);

	ores.flags = 0;
	ores.opt.buf = NULL;
	ores.opt.maxlen = 0;

	if (t_optmgmt(fd, &oreq, &ores) < 0) {
		DPRINT("t_opgmgnt, t_errno = %d", t_errno);
		if (t_errno == TSYSERR)
			return (errno);
		return (EPROTO);
	}
	if (ores.flags != T_SUCCESS) {
		DPRINT("flags 0x%x, status 0x%x",
		    (int)ores.flags, (int)opts.oh.status);
		return (EPROTO);
	}

	return (0);
}

static int
smb_setopts(int fd)
{
	int err;

	/*
	 * Set various socket/TCP options.
	 * Failures here are not fatal -
	 * just log a complaint.
	 *
	 * We don't need these two:
	 *   SO_RCVTIMEO, SO_SNDTIMEO
	 */

	err = smb_setopt_int(fd, SOL_SOCKET, SO_SNDBUF, smb_tcpsndbuf);
	if (err) {
		DPRINT("set SO_SNDBUF, err %d", err);
	}

	err = smb_setopt_int(fd, SOL_SOCKET, SO_RCVBUF, smb_tcprcvbuf);
	if (err) {
		DPRINT("set SO_RCVBUF, err %d", err);
	}

	err = smb_setopt_int(fd, SOL_SOCKET, SO_KEEPALIVE, 1);
	if (err) {
		DPRINT("set SO_KEEPALIVE, err %d", err);
	}

	err = smb_setopt_int(fd, IPPROTO_TCP, TCP_NODELAY, 1);
	if (err) {
		DPRINT("set TCP_NODELAY, err %d", err);
	}

	/* Set the connect timeout (in milliseconds). */
	err = smb_setopt_int(fd, IPPROTO_TCP,
	    TCP_CONN_ABORT_THRESHOLD,
	    smb_connect_timeout * 1000);
	if (err) {
		DPRINT("set connect timeout, err %d", err);
	}
	return (0);
}


int
conn_tcp6(struct smb_ctx *ctx, const struct sockaddr *sa, int port)
{
	struct sockaddr_in6 sin6;
	char *dev = "/dev/tcp6";
	char paddrbuf[INET6_ADDRSTRLEN];
	struct t_call sndcall;
	int fd, err;

	if (sa->sa_family != AF_INET6) {
		DPRINT("bad af %d", sa->sa_family);
		return (EINVAL);
	}
	bcopy(sa, &sin6, sizeof (sin6));
	sin6.sin6_port = htons(port);

	DPRINT("tcp6: %s (%d)",
	    inet_ntop(AF_INET6, &sin6.sin6_addr,
	    paddrbuf, sizeof (paddrbuf)), port);

	fd = t_open(dev, O_RDWR, NULL);
	if (fd < 0) {
		/* Assume t_errno = TSYSERR */
		err = errno;
		perror(dev);
		return (err);
	}
	if ((err = smb_setopts(fd)) != 0)
		goto errout;
	if (t_bind(fd, NULL, NULL) < 0) {
		DPRINT("t_bind t_errno %d", t_errno);
		if (t_errno == TSYSERR)
			err = errno;
		else
			err = EPROTO;
		goto errout;
	}
	sndcall.addr.maxlen = sizeof (sin6);
	sndcall.addr.len = sizeof (sin6);
	sndcall.addr.buf = (void *) &sin6;
	sndcall.opt.len = 0;
	sndcall.udata.len = 0;
	if (t_connect(fd, &sndcall, NULL) < 0) {
		err = get_xti_err(fd);
		DPRINT("connect, err %d", err);
		goto errout;
	}

	DPRINT("tcp6: connected, fd=%d", fd);
	ctx->ct_tran_fd = fd;
	return (0);

errout:
	close(fd);
	return (err);
}

/*
 * This is used for both SMB over TCP (port 445)
 * and NetBIOS - see conn_nbt().
 */
int
conn_tcp4(struct smb_ctx *ctx, const struct sockaddr *sa, int port)
{
	struct sockaddr_in sin;
	char *dev = "/dev/tcp";
	char paddrbuf[INET_ADDRSTRLEN];
	struct t_call sndcall;
	int fd, err;

	if (sa->sa_family != AF_INET) {
		DPRINT("bad af %d", sa->sa_family);
		return (EINVAL);
	}
	bcopy(sa, &sin, sizeof (sin));
	sin.sin_port = htons(port);

	DPRINT("tcp4: %s (%d)",
	    inet_ntop(AF_INET, &sin.sin_addr,
	    paddrbuf, sizeof (paddrbuf)), port);

	fd = t_open(dev, O_RDWR, NULL);
	if (fd < 0) {
		/* Assume t_errno = TSYSERR */
		err = errno;
		perror(dev);
		return (err);
	}
	if ((err = smb_setopts(fd)) != 0)
		goto errout;
	if (t_bind(fd, NULL, NULL) < 0) {
		DPRINT("t_bind t_errno %d", t_errno);
		if (t_errno == TSYSERR)
			err = errno;
		else
			err = EPROTO;
		goto errout;
	}
	sndcall.addr.maxlen = sizeof (sin);
	sndcall.addr.len = sizeof (sin);
	sndcall.addr.buf = (void *) &sin;
	sndcall.opt.len = 0;
	sndcall.udata.len = 0;
	if (t_connect(fd, &sndcall, NULL) < 0) {
		err = get_xti_err(fd);
		DPRINT("connect, err %d", err);
		goto errout;
	}

	DPRINT("tcp4: connected, fd=%d", fd);
	ctx->ct_tran_fd = fd;
	return (0);

errout:
	close(fd);
	return (err);
}

/*
 * Open a NetBIOS connection (session, port 139)
 *
 * The optional name parameter, if passed, means
 * we found the sockaddr via NetBIOS name lookup,
 * and can just use that for our session request.
 * Otherwise (if name is NULL), we're connecting
 * by IP address, and need to come up with the
 * NetBIOS name by other means.
 */
int
conn_nbt(struct smb_ctx *ctx, const struct sockaddr *saarg, char *name)
{
	struct sockaddr_in sin;
	struct sockaddr *sa;
	char server[NB_NAMELEN];
	char workgroup[NB_NAMELEN];
	int err, nberr, port;

	bcopy(saarg, &sin, sizeof (sin));
	sa = (struct sockaddr *)&sin;

	switch (sin.sin_family) {
	case AF_NETBIOS:	/* our fake AF */
		sin.sin_family = AF_INET;
		break;
	case AF_INET:
		break;
	default:
		DPRINT("bad af %d", sin.sin_family);
		return (EINVAL);
	}
	port = IPPORT_NETBIOS_SSN;

	/*
	 * If we have a NetBIOS name, just use it.
	 * This is the path taken when we've done a
	 * NetBIOS name lookup on this name to get
	 * the IP address in the passed sa. Otherwise,
	 * we're connecting by IP address, and need to
	 * figure out what NetBIOS name to use.
	 */
	if (name) {
		strlcpy(server, name, sizeof (server));
		DPRINT("given name: %s", server);
	} else {
		/*
		 *
		 * Try a NetBIOS node status query,
		 * which searches for a type=[20] name.
		 * If that doesn't work, just use the
		 * (fake) "*SMBSERVER" name.
		 */
		DPRINT("try node status");
		server[0] = '\0';
		nberr = nbns_getnodestatus(ctx->ct_nb,
		    &sin.sin_addr, server, workgroup);
		if (nberr == 0 && server[0] != '\0') {
			/* Found the name.  Save for reconnect. */
			DPRINT("found name: %s", server);
			strlcpy(ctx->ct_srvname, server,
			    sizeof (ctx->ct_srvname));
		} else {
			DPRINT("getnodestatus, nberr %d", nberr);
			strlcpy(server, "*SMBSERVER", sizeof (server));
		}
	}

	/*
	 * Establish the TCP connection.
	 * Careful to close it on errors.
	 */
	if ((err = conn_tcp4(ctx, sa, port)) != 0) {
		DPRINT("TCP connect: err=%d", err);
		goto out;
	}

	/* Connected.  Do NetBIOS session request. */
	err = nb_ssn_request(ctx, server);
	if (err)
		DPRINT("ssn_rq, err %d", err);

out:
	if (err) {
		if (ctx->ct_tran_fd != -1) {
			close(ctx->ct_tran_fd);
			ctx->ct_tran_fd = -1;
		}
	}
	return (err);
}

/*
 * Make a new connection, or reconnect.
 */
int
smb_iod_connect(smb_ctx_t *ctx)
{
	struct sockaddr *sa;
	int err, err2;
	struct mbdata blob;

	memset(&blob, 0, sizeof (blob));

	if (ctx->ct_srvname[0] == '\0') {
		DPRINT("sername not set!");
		return (EINVAL);
	}
	DPRINT("server: %s", ctx->ct_srvname);

	if (smb_debug)
		dump_ctx("smb_iod_connect", ctx);

	/*
	 * This may be a reconnect, so
	 * cleanup if necessary.
	 */
	if (ctx->ct_tran_fd != -1) {
		close(ctx->ct_tran_fd);
		ctx->ct_tran_fd = -1;
	}

	/*
	 * Get local machine name.
	 * Full name - not a NetBIOS name.
	 */
	if (ctx->ct_locname == NULL) {
		err = smb_getlocalname(&ctx->ct_locname);
		if (err) {
			smb_error(dgettext(TEXT_DOMAIN,
			    "can't get local name"), err);
			return (err);
		}
	}

	/*
	 * We're called with each IP address
	 * already copied into ct_srvaddr.
	 */
	ctx->ct_flags |= SMBCF_RESOLVED;

	sa = &ctx->ct_srvaddr.sa;
	switch (sa->sa_family) {

	case AF_INET6:
		err = conn_tcp6(ctx, sa, IPPORT_SMB);
		break;

	case AF_INET:
		err = conn_tcp4(ctx, sa, IPPORT_SMB);
		/*
		 * If port 445 was not listening, try port 139.
		 * Note: Not doing NetBIOS name lookup here.
		 * We already have the IP address.
		 */
		switch (err) {
		case ECONNRESET:
		case ECONNREFUSED:
			err2 = conn_nbt(ctx, sa, NULL);
			if (err2 == 0)
				err = 0;
		}
		break;

	case AF_NETBIOS:
		/* Like AF_INET, but use NetBIOS ssn. */
		err = conn_nbt(ctx, sa, ctx->ct_srvname);
		break;

	default:
		DPRINT("skipped family %d", sa->sa_family);
		err = EPROTONOSUPPORT;
		break;
	}


	if (err) {
		DPRINT("connect, err=%d", err);
		return (err);
	}

	/*
	 * Do SMB Negotiate Protocol.
	 */
	err = smb_negprot(ctx, &blob);
	if (err)
		goto out;

	/*
	 * Empty user name means an explicit request for
	 * NULL session setup, which is a special case.
	 * If negotiate determined that we want to do
	 * SMB signing, we have to turn that off for a
	 * NULL session. [MS-SMB 3.3.5.3].
	 */
	if (ctx->ct_user[0] == '\0') {
		/* Null user should have null domain too. */
		ctx->ct_domain[0] = '\0';
		ctx->ct_authflags = SMB_AT_ANON;
		ctx->ct_clnt_caps &= ~SMB_CAP_EXT_SECURITY;
		ctx->ct_vcflags &= ~SMBV_WILL_SIGN;
	}

	/*
	 * Do SMB Session Setup (authenticate)
	 *
	 * If the server negotiated extended security,
	 * run the SPNEGO state machine, otherwise do
	 * one of the old-style variants.
	 */
	if (ctx->ct_clnt_caps & SMB_CAP_EXT_SECURITY) {
		err = smb_ssnsetup_spnego(ctx, &blob);
	} else {
		/*
		 * Server did NOT negotiate extended security.
		 * Try NTLMv2, NTLMv1, or ANON (if enabled).
		 */
		if (ctx->ct_authflags & SMB_AT_NTLM2) {
			err = smb_ssnsetup_ntlm2(ctx);
		} else if (ctx->ct_authflags & SMB_AT_NTLM1) {
			err = smb_ssnsetup_ntlm1(ctx);
		} else if (ctx->ct_authflags & SMB_AT_ANON) {
			err = smb_ssnsetup_null(ctx);
		} else {
			/*
			 * Don't return EAUTH, because a new
			 * password prompt will not help.
			 */
			DPRINT("No NTLM authflags");
			err = ENOTSUP;
		}
	}

out:
	mb_done(&blob);

	if (err) {
		close(ctx->ct_tran_fd);
		ctx->ct_tran_fd = -1;
	} else {
		/* Tell library code we have a session. */
		ctx->ct_flags |= SMBCF_SSNACTIVE;
		DPRINT("tran_fd = %d", ctx->ct_tran_fd);
	}

	return (err);
}