613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * CDDL HEADER START
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * The contents of this file are subject to the terms of the
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Common Development and Distribution License (the "License").
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * You may not use this file except in compliance with the License.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * See the License for the specific language governing permissions
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * and limitations under the License.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * When distributing Covered Code, include this CDDL HEADER in each
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * If applicable, add the following below this CDDL HEADER, with the
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * fields enclosed by brackets "[]" replaced with your own identifying
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * information: Portions Copyright [yyyy] [name of copyright owner]
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * CDDL HEADER END
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Use is subject to license terms.
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Functions to setup connections (TCP and/or NetBIOS)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * This has the fall-back logic for IP6, IP4, NBT
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * SMB messages are up to 64K.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Let's leave room for two.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Rossstatic int smb_connect_timeout = 30; /* seconds */
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Rossint conn_tcp6(struct smb_ctx *, const struct sockaddr *, int);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Rossint conn_tcp4(struct smb_ctx *, const struct sockaddr *, int);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Rossint conn_nbt(struct smb_ctx *, const struct sockaddr *, char *);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Internal set sockopt for int-sized options.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Borrowed from: libnsl/rpc/ti_opts.c
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Rosssmb_setopt_int(int fd, int level, int name, int val)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross /* opt header */
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Set various socket/TCP options.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Failures here are not fatal -
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * just log a complaint.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * We don't need these two:
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * SO_RCVTIMEO, SO_SNDTIMEO
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross err = smb_setopt_int(fd, SOL_SOCKET, SO_SNDBUF, smb_tcpsndbuf);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross err = smb_setopt_int(fd, SOL_SOCKET, SO_RCVBUF, smb_tcprcvbuf);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross err = smb_setopt_int(fd, SOL_SOCKET, SO_KEEPALIVE, 1);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross err = smb_setopt_int(fd, IPPROTO_TCP, TCP_NODELAY, 1);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross /* Set the connect timeout (in milliseconds). */
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Rossconn_tcp6(struct smb_ctx *ctx, const struct sockaddr *sa, int port)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross /* Assume t_errno = TSYSERR */
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * This is used for both SMB over TCP (port 445)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * and NetBIOS - see conn_nbt().
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Rossconn_tcp4(struct smb_ctx *ctx, const struct sockaddr *sa, int port)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross /* Assume t_errno = TSYSERR */
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Open a NetBIOS connection (session, port 139)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * The optional name parameter, if passed, means
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * we found the sockaddr via NetBIOS name lookup,
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * and can just use that for our session request.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Otherwise (if name is NULL), we're connecting
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * by IP address, and need to come up with the
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * NetBIOS name by other means.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Rossconn_nbt(struct smb_ctx *ctx, const struct sockaddr *saarg, char *name)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * If we have a NetBIOS name, just use it.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * This is the path taken when we've done a
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * NetBIOS name lookup on this name to get
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * the IP address in the passed sa. Otherwise,
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * we're connecting by IP address, and need to
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * figure out what NetBIOS name to use.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Try a NetBIOS node status query,
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * which searches for a type=[20] name.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * If that doesn't work, just use the
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * (fake) "*SMBSERVER" name.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross /* Found the name. Save for reconnect. */
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Establish the TCP connection.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Careful to close it on errors.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross /* Connected. Do NetBIOS session request. */
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Make a new connection, or reconnect.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * This may be a reconnect, so
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * cleanup if necessary.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Get local machine name.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Full name - not a NetBIOS name.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * We're called with each IP address
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * already copied into ct_srvaddr.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * If port 445 was not listening, try port 139.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Note: Not doing NetBIOS name lookup here.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * We already have the IP address.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross /* Like AF_INET, but use NetBIOS ssn. */
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * Do SMB Negotiate Protocol.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Empty user name means an explicit request for
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * NULL session setup, which is a special case.
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * If negotiate determined that we want to do
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * SMB signing, we have to turn that off for a
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * NULL session. [MS-SMB 3.3.5.3].
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross /* Null user should have null domain too. */
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * Do SMB Session Setup (authenticate)
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * If the server negotiated extended security,
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * run the SPNEGO state machine, otherwise do
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * one of the old-style variants.
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * Server did NOT negotiate extended security.
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * Try NTLMv2, NTLMv1, or ANON (if enabled).
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * Don't return EAUTH, because a new
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * password prompt will not help.
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross /* Tell library code we have a session. */