2N/A/*
2N/A * Copyright (c) 2000, 2001 Boris Popov
2N/A * All rights reserved.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in the
2N/A * documentation and/or other materials provided with the distribution.
2N/A * 3. All advertising materials mentioning features or use of this software
2N/A * must display the following acknowledgement:
2N/A * This product includes software developed by Boris Popov.
2N/A * 4. Neither the name of the author nor the names of any co-contributors
2N/A * may be used to endorse or promote products derived from this software
2N/A * without specific prior written permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2N/A * SUCH DAMAGE.
2N/A *
2N/A * $Id: nb.c,v 1.1.1.2 2001/07/06 22:38:42 conrad Exp $
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <sys/param.h>
2N/A#include <sys/socket.h>
2N/A
2N/A#include <errno.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <strings.h>
2N/A#include <unistd.h>
2N/A#include <libintl.h>
2N/A#include <netdb.h>
2N/A#include <nss_dbdefs.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A#include <err.h>
2N/A#include <netsmb/netbios.h>
2N/A#include "smbfs_lib.h"
2N/A#include "smbfs_nb_lib.h"
2N/A#include "smbfs_private.h"
2N/A#include <smbsrv/libsmb.h>
2N/A
2N/Aint
2N/Asmbfs_nb_ctx_create(struct nb_ctx **ctxpp)
2N/A{
2N/A struct nb_ctx *ctx;
2N/A
2N/A ctx = malloc(sizeof (struct nb_ctx));
2N/A if (ctx == NULL)
2N/A return (ENOMEM);
2N/A bzero(ctx, sizeof (struct nb_ctx));
2N/A *ctxpp = ctx;
2N/A return (0);
2N/A}
2N/A
2N/Avoid
2N/Asmbfs_nb_ctx_done(struct nb_ctx *ctx)
2N/A{
2N/A if (ctx == NULL)
2N/A return;
2N/A if (ctx->nb_scope)
2N/A free(ctx->nb_scope);
2N/A if (ctx)
2N/A free(ctx);
2N/A}
2N/A
2N/Avoid
2N/Asmbfs_nb_ctx_setnbflags(struct nb_ctx *nb, int ns_ena, int bc_ena)
2N/A{
2N/A nb->nb_flags &= ~(NBCF_NS_ENABLE | NBCF_BC_ENABLE);
2N/A if (ns_ena) {
2N/A nb->nb_flags |= NBCF_NS_ENABLE;
2N/A if (bc_ena)
2N/A nb->nb_flags |= NBCF_BC_ENABLE;
2N/A }
2N/A}
2N/A
2N/Aint
2N/Asmbfs_nb_ctx_setwins(struct nb_ctx *ctx, const char *wins1, const char *wins2)
2N/A{
2N/A struct in_addr ina;
2N/A int error;
2N/A
2N/A if (wins1 == NULL) {
2N/A ctx->nb_wins1 = 0;
2N/A ctx->nb_wins2 = 0;
2N/A return (0);
2N/A }
2N/A
2N/A error = smbfs_nb_resolvehost_in(wins1, &ina);
2N/A if (error) {
2N/A smbfs_error(dgettext(TEXT_DOMAIN, "can't resolve %s"),
2N/A error, wins1);
2N/A return (error);
2N/A }
2N/A ctx->nb_wins1 = ina.s_addr;
2N/A
2N/A if (wins2 == NULL)
2N/A ctx->nb_wins2 = 0;
2N/A else {
2N/A error = smbfs_nb_resolvehost_in(wins2, &ina);
2N/A if (error) {
2N/A smbfs_error(dgettext(TEXT_DOMAIN, "can't resolve %s"),
2N/A error, wins2);
2N/A return (error);
2N/A }
2N/A ctx->nb_wins2 = ina.s_addr;
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * This is called by "smbutil lookup" to handle the
2N/A * "-w wins_server" option. Let the semantics of
2N/A * this option be: Use specified WINS server only.
2N/A * If specified server is the broadcast address,
2N/A * set broadcast mode (and no WINS servers).
2N/A */
2N/Aint
2N/Asmbfs_nb_ctx_setns(struct nb_ctx *ctx, const char *addr)
2N/A{
2N/A int error;
2N/A
2N/A error = smbfs_nb_ctx_setwins(ctx, addr, NULL);
2N/A if (error)
2N/A return (error);
2N/A
2N/A /* Deal with explicit request for broadcast. */
2N/A if (ctx->nb_wins1 == INADDR_BROADCAST) {
2N/A ctx->nb_wins1 = 0;
2N/A ctx->nb_flags |= NBCF_BC_ENABLE;
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Asmbfs_nb_ctx_setscope(struct nb_ctx *ctx, const char *scope)
2N/A{
2N/A size_t slen = strlen(scope);
2N/A
2N/A if (slen >= 128) {
2N/A smbfs_error(dgettext(TEXT_DOMAIN,
2N/A "scope '%s' is too long"), 0, scope);
2N/A return (ENAMETOOLONG);
2N/A }
2N/A if (ctx->nb_scope)
2N/A free(ctx->nb_scope);
2N/A ctx->nb_scope = malloc(slen + 1);
2N/A if (ctx->nb_scope == NULL)
2N/A return (ENOMEM);
2N/A (void) smbfs_nls_str_upper(ctx->nb_scope, scope);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Now get the WINS server IP addresses directly
2N/A * when reading the RC files, so no longer need to
2N/A * lookup any names here.
2N/A */
2N/Aint
2N/Asmbfs_nb_ctx_resolve(struct nb_ctx *ctx)
2N/A{
2N/A ctx->nb_flags |= NBCF_RESOLVED;
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * For defaults, enable NetBIOS name lookup and NetBIOS lookup via broadcast.
2N/A * These settings are not configurable.
2N/A */
2N/Aint
2N/Asmbfs_nb_ctx_getconfigs(struct nb_ctx *ctx)
2N/A{
2N/A char ipstr1[16], ipstr2[16];
2N/A char *wins1 = NULL, *wins2 = NULL;
2N/A
2N/A smbfs_nb_ctx_setnbflags(ctx, TRUE, TRUE);
2N/A
2N/A if (smb_config_getstr(SMB_CI_WINS_SRV1, ipstr1, sizeof (ipstr1)) ==
2N/A SMBD_SMF_OK)
2N/A if (*ipstr1 != '\0')
2N/A wins1 = ipstr1;
2N/A
2N/A if (smb_config_getstr(SMB_CI_WINS_SRV2, ipstr2, sizeof (ipstr2)) ==
2N/A SMBD_SMF_OK)
2N/A if (*ipstr2 != '\0')
2N/A wins2 = ipstr2;
2N/A
2N/A return (smbfs_nb_ctx_setwins(ctx, wins1, wins2));
2N/A}
2N/A
2N/A/*
2N/A * General networking stuff, in spite of the names
2N/A * that imply they're specific to NetBIOS.
2N/A */
2N/Aint
2N/Asmbfs_nb_resolvehost_in(const char *name, struct in_addr *ia)
2N/A{
2N/A char he_buf[NSS_BUFLEN_HOSTS];
2N/A struct hostent he, *h;
2N/A int err;
2N/A
2N/A h = gethostbyname_r(name, &he, he_buf, sizeof (he_buf), &err);
2N/A if (h == NULL) {
2N/A#ifdef DEBUG
2N/A warnx("can't get server address `%s': ", name);
2N/A#endif
2N/A return (ENETDOWN);
2N/A }
2N/A if (h->h_addrtype != AF_INET) {
2N/A#ifdef DEBUG
2N/A warnx("address for `%s' is not in the AF_INET family", name);
2N/A#endif
2N/A return (EAFNOSUPPORT);
2N/A }
2N/A if (h->h_length != 4) {
2N/A#ifdef DEBUG
2N/A warnx("address for `%s' has invalid length", name);
2N/A#endif
2N/A return (EAFNOSUPPORT);
2N/A }
2N/A
2N/A (void) memcpy(ia, h->h_addr, sizeof (*ia));
2N/A return (0);
2N/A}
2N/A
2N/Astatic const char *nb_err_rcode[] = {
2N/A "bad request/response format",
2N/A "NBNS server failure",
2N/A "no such name",
2N/A "unsupported request",
2N/A "request rejected",
2N/A "name already registered"
2N/A};
2N/A
2N/Astatic const char *nb_err[] = {
2N/A "host not found",
2N/A "too many redirects",
2N/A "invalid response",
2N/A "NETBIOS name too long",
2N/A "no interface to broadcast on and no NBNS server specified"
2N/A};
2N/A
2N/Aconst char *
2N/Asmbfs_nb_strerror(int error)
2N/A{
2N/A if (error == 0)
2N/A return (NULL);
2N/A if (error <= NBERR_ACTIVE)
2N/A return (nb_err_rcode[error - 1]);
2N/A else if (error >= NBERR_HOSTNOTFOUND && error < NBERR_MAX)
2N/A return (nb_err[error - NBERR_HOSTNOTFOUND]);
2N/A else
2N/A return (NULL);
2N/A}
2N/A
2N/A/* Netbios name encoding functions. Moved from smbfs_nb_name.c */
2N/Aint
2N/Asmbfs_nb_name_enc_len(const uchar_t *str)
2N/A{
2N/A const uchar_t *cp = str;
2N/A int len, blen;
2N/A
2N/A if ((cp[0] & 0xc0) == 0xc0)
2N/A return (-1); /* first two bytes are offset to name */
2N/A
2N/A len = 1;
2N/A for (;;) {
2N/A blen = *cp;
2N/A if (blen++ == 0)
2N/A break;
2N/A len += blen;
2N/A cp += blen;
2N/A }
2N/A return (len);
2N/A}
2N/A
2N/Aint
2N/Asmbfs_nb_name_encode(struct mbdata *mbp, struct nb_name *nn)
2N/A{
2N/A char *plen;
2N/A uchar_t ch;
2N/A char *p, namebuf[NB_NAMELEN+1];
2N/A int i, lblen;
2N/A
2N/A bcopy(nn->nn_name, namebuf, NB_NAMELEN);
2N/A namebuf[NB_NAMELEN-1] = (char)nn->nn_type;
2N/A namebuf[NB_NAMELEN] = '\0'; /* for debug */
2N/A
2N/A /*
2N/A * Do the NetBIOS "first-level encoding" here.
2N/A * (RFC1002 explains this weirdness...)
2N/A *
2N/A * Here is what we marshall:
2N/A * uint8_t NAME_LENGTH (always 32)
2N/A * uint8_t ENCODED_NAME[32]
2N/A * uint8_t SCOPE_LENGTH
2N/A * Scope follows here, then another null.
2N/A */
2N/A
2N/A /* NAME_LENGTH */
2N/A smbfs_mb_put_uint8(mbp, (2 * NB_NAMELEN));
2N/A
2N/A /* ENCODED_NAME */
2N/A for (i = 0; i < NB_NAMELEN; i++) {
2N/A ch = namebuf[i];
2N/A smbfs_mb_put_uint8(mbp, 'A' + ((ch >> 4) & 0xF));
2N/A smbfs_mb_put_uint8(mbp, 'A' + ((ch) & 0xF));
2N/A }
2N/A
2N/A /*
2N/A * NetBIOS "scope" sting encoding,
2N/A * a.k.a second-level encoding.
2N/A * See RFC1002 for the details.
2N/A *
2N/A * Note: plen points to the length byte at the
2N/A * start of each string. This keeps a pointer
2N/A * to the location and fills it in after the
2N/A * length of the string is determined.
2N/A *
2N/A * One string of length zero terminates.
2N/A * With no scope string, the zero-length
2N/A * string is the only thing there.
2N/A */
2N/A if (nn->nn_scope == NULL) {
2N/A smbfs_mb_put_uint8(mbp, 0);
2N/A return (0);
2N/A }
2N/A
2N/A (void) smbfs_mb_fit(mbp, 1, &plen);
2N/A *plen = 0; /* will update below */
2N/A lblen = 0;
2N/A for (p = nn->nn_scope; ; p++) {
2N/A if (*p == '\0') {
2N/A *plen = lblen;
2N/A if (lblen)
2N/A smbfs_mb_put_uint8(mbp, 0);
2N/A break;
2N/A }
2N/A if (*p == '.') {
2N/A *plen = lblen;
2N/A (void) smbfs_mb_fit(mbp, 1, &plen);
2N/A *plen = 0;
2N/A lblen = 0;
2N/A } else {
2N/A if (lblen < NB_MAXLABLEN) {
2N/A smbfs_mb_put_uint8(mbp, *p);
2N/A lblen++;
2N/A }
2N/A }
2N/A }
2N/A
2N/A return (0);
2N/A}