110023SGordon.Ross@Sun.COM /*
210023SGordon.Ross@Sun.COM * CDDL HEADER START
310023SGordon.Ross@Sun.COM *
410023SGordon.Ross@Sun.COM * The contents of this file are subject to the terms of the
510023SGordon.Ross@Sun.COM * Common Development and Distribution License (the "License").
610023SGordon.Ross@Sun.COM * You may not use this file except in compliance with the License.
710023SGordon.Ross@Sun.COM *
810023SGordon.Ross@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
910023SGordon.Ross@Sun.COM * or http://www.opensolaris.org/os/licensing.
1010023SGordon.Ross@Sun.COM * See the License for the specific language governing permissions
1110023SGordon.Ross@Sun.COM * and limitations under the License.
1210023SGordon.Ross@Sun.COM *
1310023SGordon.Ross@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
1410023SGordon.Ross@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1510023SGordon.Ross@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
1610023SGordon.Ross@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
1710023SGordon.Ross@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
1810023SGordon.Ross@Sun.COM *
1910023SGordon.Ross@Sun.COM * CDDL HEADER END
2010023SGordon.Ross@Sun.COM */
2110023SGordon.Ross@Sun.COM
2210023SGordon.Ross@Sun.COM /*
23*12925SGordon.Ross@Sun.COM * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2410023SGordon.Ross@Sun.COM */
2510023SGordon.Ross@Sun.COM
2610023SGordon.Ross@Sun.COM /*
2710023SGordon.Ross@Sun.COM * Functions to get list of addresses (TCP and/or NetBIOS)
2810023SGordon.Ross@Sun.COM */
2910023SGordon.Ross@Sun.COM
3010023SGordon.Ross@Sun.COM #include <errno.h>
3110023SGordon.Ross@Sun.COM #include <stdio.h>
3210023SGordon.Ross@Sun.COM #include <stdlib.h>
3310023SGordon.Ross@Sun.COM #include <string.h>
3410023SGordon.Ross@Sun.COM #include <strings.h>
3510023SGordon.Ross@Sun.COM #include <unistd.h>
3610023SGordon.Ross@Sun.COM #include <netdb.h>
3710023SGordon.Ross@Sun.COM #include <libintl.h>
3810023SGordon.Ross@Sun.COM #include <xti.h>
3910023SGordon.Ross@Sun.COM #include <assert.h>
4010023SGordon.Ross@Sun.COM
4110023SGordon.Ross@Sun.COM #include <sys/types.h>
4210023SGordon.Ross@Sun.COM #include <sys/time.h>
4310023SGordon.Ross@Sun.COM #include <sys/byteorder.h>
4410023SGordon.Ross@Sun.COM #include <sys/socket.h>
4510023SGordon.Ross@Sun.COM #include <sys/fcntl.h>
4610023SGordon.Ross@Sun.COM
4710023SGordon.Ross@Sun.COM #include <netinet/in.h>
4810023SGordon.Ross@Sun.COM #include <netinet/tcp.h>
4910023SGordon.Ross@Sun.COM #include <arpa/inet.h>
5010023SGordon.Ross@Sun.COM
5110023SGordon.Ross@Sun.COM #include <netsmb/smb.h>
5210023SGordon.Ross@Sun.COM #include <netsmb/smb_lib.h>
5310023SGordon.Ross@Sun.COM #include <netsmb/netbios.h>
5410023SGordon.Ross@Sun.COM #include <netsmb/nb_lib.h>
5510023SGordon.Ross@Sun.COM #include <netsmb/smb_dev.h>
5610023SGordon.Ross@Sun.COM
5710023SGordon.Ross@Sun.COM #include "charsets.h"
5810023SGordon.Ross@Sun.COM #include "private.h"
5910023SGordon.Ross@Sun.COM
6010023SGordon.Ross@Sun.COM void
dump_addrinfo(struct addrinfo * ai)6110023SGordon.Ross@Sun.COM dump_addrinfo(struct addrinfo *ai)
6210023SGordon.Ross@Sun.COM {
6310023SGordon.Ross@Sun.COM int i;
6410023SGordon.Ross@Sun.COM
6510023SGordon.Ross@Sun.COM if (ai == NULL) {
6610023SGordon.Ross@Sun.COM printf("ai==NULL\n");
6710023SGordon.Ross@Sun.COM return;
6810023SGordon.Ross@Sun.COM }
6910023SGordon.Ross@Sun.COM
7010023SGordon.Ross@Sun.COM for (i = 0; ai; i++, ai = ai->ai_next) {
7110023SGordon.Ross@Sun.COM printf("ai[%d]: af=%d, len=%d", i,
7210023SGordon.Ross@Sun.COM ai->ai_family, ai->ai_addrlen);
7310023SGordon.Ross@Sun.COM dump_sockaddr(ai->ai_addr);
7410023SGordon.Ross@Sun.COM if (ai->ai_canonname) {
7510023SGordon.Ross@Sun.COM printf("ai[%d]: cname=\"%s\"\n",
7610023SGordon.Ross@Sun.COM i, ai->ai_canonname);
7710023SGordon.Ross@Sun.COM }
7810023SGordon.Ross@Sun.COM }
7910023SGordon.Ross@Sun.COM }
8010023SGordon.Ross@Sun.COM
8110023SGordon.Ross@Sun.COM void
dump_sockaddr(struct sockaddr * sa)8210023SGordon.Ross@Sun.COM dump_sockaddr(struct sockaddr *sa)
8310023SGordon.Ross@Sun.COM {
8410023SGordon.Ross@Sun.COM char paddrbuf[INET6_ADDRSTRLEN];
8510023SGordon.Ross@Sun.COM struct sockaddr_in *sin;
8610023SGordon.Ross@Sun.COM struct sockaddr_in6 *sin6;
8710023SGordon.Ross@Sun.COM int af = sa->sa_family;
8810023SGordon.Ross@Sun.COM const char *ip;
8910023SGordon.Ross@Sun.COM
9010023SGordon.Ross@Sun.COM printf(" saf=%d,", af);
9110023SGordon.Ross@Sun.COM switch (af) {
9210023SGordon.Ross@Sun.COM case AF_NETBIOS: /* see nbns_rq.c */
9310023SGordon.Ross@Sun.COM case AF_INET:
9410023SGordon.Ross@Sun.COM sin = (void *)sa;
9510023SGordon.Ross@Sun.COM ip = inet_ntop(AF_INET, &sin->sin_addr,
9610023SGordon.Ross@Sun.COM paddrbuf, sizeof (paddrbuf));
9710023SGordon.Ross@Sun.COM break;
9810023SGordon.Ross@Sun.COM case AF_INET6:
9910023SGordon.Ross@Sun.COM sin6 = (void *)sa;
10010023SGordon.Ross@Sun.COM ip = inet_ntop(AF_INET6, &sin6->sin6_addr,
10110023SGordon.Ross@Sun.COM paddrbuf, sizeof (paddrbuf));
10210023SGordon.Ross@Sun.COM break;
10310023SGordon.Ross@Sun.COM default:
10410023SGordon.Ross@Sun.COM ip = "?";
10510023SGordon.Ross@Sun.COM break;
10610023SGordon.Ross@Sun.COM }
10710023SGordon.Ross@Sun.COM printf(" IP=%s\n", ip);
10810023SGordon.Ross@Sun.COM }
10910023SGordon.Ross@Sun.COM
11010023SGordon.Ross@Sun.COM
11110023SGordon.Ross@Sun.COM /*
11210023SGordon.Ross@Sun.COM * SMB client name resolution - normal, and/or NetBIOS.
11310023SGordon.Ross@Sun.COM * Returns an EAI_xxx error number like getaddrinfo(3)
11410023SGordon.Ross@Sun.COM */
11510023SGordon.Ross@Sun.COM int
smb_ctx_getaddr(struct smb_ctx * ctx)11610023SGordon.Ross@Sun.COM smb_ctx_getaddr(struct smb_ctx *ctx)
11710023SGordon.Ross@Sun.COM {
11810023SGordon.Ross@Sun.COM struct nb_ctx *nbc = ctx->ct_nb;
11910023SGordon.Ross@Sun.COM struct addrinfo hints, *res;
12010023SGordon.Ross@Sun.COM char *srvaddr_str;
12110023SGordon.Ross@Sun.COM int gaierr, gaierr2;
12210023SGordon.Ross@Sun.COM
12310023SGordon.Ross@Sun.COM if (ctx->ct_fullserver == NULL || ctx->ct_fullserver[0] == '\0')
12410023SGordon.Ross@Sun.COM return (EAI_NONAME);
12510023SGordon.Ross@Sun.COM
12610023SGordon.Ross@Sun.COM if (ctx->ct_addrinfo != NULL) {
12710023SGordon.Ross@Sun.COM freeaddrinfo(ctx->ct_addrinfo);
12810023SGordon.Ross@Sun.COM ctx->ct_addrinfo = NULL;
12910023SGordon.Ross@Sun.COM }
13010023SGordon.Ross@Sun.COM
13110023SGordon.Ross@Sun.COM /*
13210023SGordon.Ross@Sun.COM * If the user specified an address, use it,
13310023SGordon.Ross@Sun.COM * and don't do NetBIOS lookup.
13410023SGordon.Ross@Sun.COM */
13510023SGordon.Ross@Sun.COM if (ctx->ct_srvaddr_s) {
13610023SGordon.Ross@Sun.COM srvaddr_str = ctx->ct_srvaddr_s;
13710023SGordon.Ross@Sun.COM nbc->nb_flags &= ~NBCF_NS_ENABLE;
13810023SGordon.Ross@Sun.COM } else
13910023SGordon.Ross@Sun.COM srvaddr_str = ctx->ct_fullserver;
14010023SGordon.Ross@Sun.COM
14110023SGordon.Ross@Sun.COM /*
14210023SGordon.Ross@Sun.COM * Default the server name we'll use in the
14310023SGordon.Ross@Sun.COM * protocol (i.e. NTLM, tree connect).
14410023SGordon.Ross@Sun.COM */
14510023SGordon.Ross@Sun.COM strlcpy(ctx->ct_srvname, ctx->ct_fullserver,
14610023SGordon.Ross@Sun.COM sizeof (ctx->ct_srvname));
14710023SGordon.Ross@Sun.COM
14810023SGordon.Ross@Sun.COM /*
14910023SGordon.Ross@Sun.COM * Try to lookup the host address using the
15010023SGordon.Ross@Sun.COM * normal name-to-IP address mechanisms.
15110023SGordon.Ross@Sun.COM * If that fails, we MAY try NetBIOS.
15210023SGordon.Ross@Sun.COM */
15310023SGordon.Ross@Sun.COM memset(&hints, 0, sizeof (hints));
15410023SGordon.Ross@Sun.COM hints.ai_flags = AI_CANONNAME;
15510023SGordon.Ross@Sun.COM hints.ai_family = PF_UNSPEC;
15610023SGordon.Ross@Sun.COM hints.ai_socktype = SOCK_STREAM;
15710023SGordon.Ross@Sun.COM gaierr = getaddrinfo(srvaddr_str, NULL, &hints, &res);
15810023SGordon.Ross@Sun.COM if (gaierr == 0) {
15910023SGordon.Ross@Sun.COM ctx->ct_addrinfo = res;
16010023SGordon.Ross@Sun.COM return (0);
16110023SGordon.Ross@Sun.COM }
16210023SGordon.Ross@Sun.COM
16310023SGordon.Ross@Sun.COM /*
16410023SGordon.Ross@Sun.COM * If regular IP name lookup failed, try NetBIOS,
16510023SGordon.Ross@Sun.COM * but only if given a valid NetBIOS name and if
16610023SGordon.Ross@Sun.COM * NetBIOS name lookup is enabled.
16710023SGordon.Ross@Sun.COM */
16810023SGordon.Ross@Sun.COM if (nbc->nb_flags & NBCF_NS_ENABLE) {
16910023SGordon.Ross@Sun.COM gaierr2 = nbns_getaddrinfo(ctx->ct_fullserver, nbc, &res);
17010023SGordon.Ross@Sun.COM if (gaierr2 == 0) {
17110023SGordon.Ross@Sun.COM if (res->ai_canonname)
17210023SGordon.Ross@Sun.COM strlcpy(ctx->ct_srvname,
17310023SGordon.Ross@Sun.COM res->ai_canonname,
17410023SGordon.Ross@Sun.COM sizeof (ctx->ct_srvname));
17510023SGordon.Ross@Sun.COM ctx->ct_addrinfo = res;
17610023SGordon.Ross@Sun.COM return (0);
17710023SGordon.Ross@Sun.COM }
17810023SGordon.Ross@Sun.COM }
17910023SGordon.Ross@Sun.COM
18010023SGordon.Ross@Sun.COM /*
18110023SGordon.Ross@Sun.COM * Return the original error from getaddrinfo
18210023SGordon.Ross@Sun.COM */
18310023SGordon.Ross@Sun.COM if (smb_verbose) {
18410023SGordon.Ross@Sun.COM smb_error(dgettext(TEXT_DOMAIN,
18510023SGordon.Ross@Sun.COM "getaddrinfo: %s: %s"), 0,
18610023SGordon.Ross@Sun.COM ctx->ct_fullserver,
18710023SGordon.Ross@Sun.COM gai_strerror(gaierr));
18810023SGordon.Ross@Sun.COM }
18910023SGordon.Ross@Sun.COM return (gaierr);
19010023SGordon.Ross@Sun.COM }
191