2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <sys/tzfile.h>
2N/A#include <errno.h>
2N/A#include <stdlib.h>
2N/A#include <stdio.h>
2N/A#include <unistd.h>
2N/A#include <syslog.h>
2N/A#include <string.h>
2N/A#include <strings.h>
2N/A#include <time.h>
2N/A#include <synch.h>
2N/A#include <netdb.h>
2N/A#include <sys/socket.h>
2N/A#include <arpa/inet.h>
2N/A
2N/A#include <smbsrv/libsmb.h>
2N/A#include <smbsrv/libsmbns.h>
2N/A#include <smb/smb.h>
2N/A#include <smb/mailslot.h>
2N/A#include <smbns_browser.h>
2N/A#include <smbns_netbios.h>
2N/A
2N/A/*
2N/A * ntdomain_info
2N/A * Temporary. It should be removed once NBTD is integrated.
2N/A */
2N/Asmb_ntdomain_t ntdomain_info;
2N/Amutex_t ntdomain_mtx;
2N/Acond_t ntdomain_cv;
2N/A
2N/A#define SMB_SERVER_SIGNATURE 0xaa550415
2N/A
2N/Atypedef struct smb_hostinfo {
2N/A list_node_t hi_lnd;
2N/A smb_nic_t hi_nic;
2N/A char hi_nbname[NETBIOS_NAME_SZ];
2N/A name_entry_t hi_netname;
2N/A uint32_t hi_nextannouce;
2N/A int hi_reps;
2N/A int hi_interval;
2N/A uint8_t hi_updatecnt;
2N/A uint32_t hi_type;
2N/A smb_version_t hi_version;
2N/A} smb_hostinfo_t;
2N/A
2N/Atypedef struct smb_browserinfo {
2N/A list_t bi_hlist;
2N/A int bi_hcnt;
2N/A rwlock_t bi_hlist_rwl;
2N/A boolean_t bi_changed;
2N/A mutex_t bi_mtx;
2N/A} smb_browserinfo_t;
2N/A
2N/Astatic smb_browserinfo_t smb_binfo;
2N/A
2N/Astatic int smb_browser_init(void);
2N/Astatic void smb_browser_infoinit(void);
2N/Astatic void smb_browser_infoterm(void);
2N/Astatic void smb_browser_infofree(void);
2N/A
2N/A
2N/Avoid
2N/Asmb_browser_reconfig(void)
2N/A{
2N/A (void) mutex_lock(&smb_binfo.bi_mtx);
2N/A smb_binfo.bi_changed = B_TRUE;
2N/A (void) mutex_unlock(&smb_binfo.bi_mtx);
2N/A}
2N/A
2N/A/*
2N/A * 3. Browser Overview
2N/A *
2N/A * Hosts involved in the browsing process can be separated into two
2N/A * distinct groups, browser clients and browser servers (often referred to
2N/A * simply as "browsers").
2N/A *
2N/A * A browser is a server which maintains information about servers -
2N/A * primarily the domain they are in and the services that they are running
2N/A * -- and about domains. Browsers may assume several different roles in
2N/A * their lifetimes, and dynamically switch between them.
2N/A *
2N/A * Browser clients are of two types: workstations and (non-browser)
2N/A * servers. In the context of browsing, workstations query browsers for the
2N/A * information they contain; servers supply browsers the information by
2N/A * registering with them. Note that, at times, browsers may themselves
2N/A * behave as browser clients and query other browsers.
2N/A *
2N/A * For the purposes of this specification, a domain is simply a name with
2N/A * which to associate a group of resources such as computers, servers and
2N/A * users. Domains allow a convenient means for browser clients to restrict
2N/A * the scope of a search when they query browser servers. Every domain has
2N/A * a "master" server called the Primary Domain Controller (PDC) that
2N/A * manages various activities within the domain.
2N/A *
2N/A * One browser for each domain on a subnet is designated the Local Master
2N/A * Browser for that domain. Servers in its domain on the subnet register
2N/A * with it, as do the Local Master Browsers for other domains on the
2N/A * subnet. It uses these registrations to maintain authoritative
2N/A * information about its domain on its subnet. If there are other subnets
2N/A * in the network, it also knows the name of the server running the
2N/A * domain's Domain Master Browser; it registers with it, and uses it to
2N/A * obtain information about the rest of the network (see below).
2N/A *
2N/A * Clients on a subnet query browsers designated as the Backup Browsers for
2N/A * the subnet (not the Master Browser). Backup Browsers maintain a copy of
2N/A * the information on the Local Master Browser; they get it by periodically
2N/A * querying the Local Master Browser for all of its information. Clients
2N/A * find the Backup Browsers by asking the Local Master Browser. Clients are
2N/A * expected to spread their queries evenly across Backup Browsers to
2N/A * balance the load.
2N/A *
2N/A * The Local Master Browser is dynamically elected automatically. Multiple
2N/A * Backup Browser Servers may exist per subnet; they are selected from
2N/A * among the potential browser servers by the Local Master Browser, which
2N/A * is configured to select enough to handle the expected query load.
2N/A *
2N/A * When there are multiple subnets, a Domain Master Browser is assigned
2N/A * the task of keeping the multiple subnets in synchronization. The Primary
2N/A * Domain Controller (PDC) always acts as the Domain Master Browser. The
2N/A * Domain Master Browser periodically acts as a client and queries all the
2N/A * Local Master Browsers for its domain, asking them for a list containing
2N/A * all the domains and all the servers in their domain known within their
2N/A * subnets; it merges all the replies into a single master list. This
2N/A * allows a Domain Master Browser server to act as a collection point for
2N/A * inter-subnet browsing information. Local Master Browsers periodically
2N/A * query the Domain Master Browser to retrieve the network-wide information
2N/A * it maintains.
2N/A *
2N/A * When a domain spans only a single subnet, there will not be any distinct
2N/A * Local Master Browser; this role will be handled by the Domain Master
2N/A * Browser. Similarly, the Domain Master Browser is always the Local Master
2N/A * Browser for the subnet it is on.
2N/A *
2N/A * When a browser client suspects that the Local Master Browser has failed,
2N/A * the client will instigate an election in which the browser servers
2N/A * participate, and some browser servers may change roles.
2N/A *
2N/A * Some characteristics of a good browsing mechanism include:
2N/A * . minimal network traffic
2N/A * . minimum server discovery time
2N/A * . minimum change discovery latency
2N/A * . immunity to machine failures
2N/A *
2N/A * Historically, Browser implementations had been very closely tied to
2N/A * NETBIOS and datagrams. The early implementations caused a lot of
2N/A * broadcast traffic. See Appendix D for an overview that presents how the
2N/A * Browser specification evolved.
2N/A *
2N/A * 4. Browsing Protocol Architecture
2N/A *
2N/A * This section first describes the how the browsing protocol is layered,
2N/A * then describes the roles of clients, servers, and browsers in the
2N/A * browsing subsystem.
2N/A *
2N/A * 4.1 Layering of Browsing Protocol Requests
2N/A *
2N/A * Most of the browser functionality is implemented using mailslots.
2N/A * Mailslots provide a mechanism for fast, unreliable unidirectional data
2N/A * transfer; they are named via ASCII "mailslot (path) name". Mailslots are
2N/A * implemented using the CIFS Transact SMB which is encapsulated in a
2N/A * NETBIOS datagram. Browser protocol requests are sent to browser specific
2N/A * mailslots using some browser-specific NETBIOS names. These datagrams can
2N/A * either be unicast or broadcast, depending on whether the NETBIOS name is
2N/A * a "unique name" or a "group name". Various data structures, which are
2N/A * detailed subsequently within this document, flow as the data portion of
2N/A * the Transact SMB.
2N/A *
2N/A * Here is an example of a generic browser SMB, showing how a browser
2N/A * request is encapsulated in a TRANSACT SMB request. Note that the PID,
2N/A * TID, MID, UID, and Flags are all 0 in mailslot requests.
2N/A *
2N/A * SMB: C transact, File = \MAILSLOT\BROWSE
2N/A * SMB: SMB Status = Error Success
2N/A * SMB: Error class = No Error
2N/A * SMB: Error code = No Error
2N/A * SMB: Header: PID = 0x0000 TID = 0x0000 MID = 0x0000 UID = 0x0000
2N/A * SMB: Tree ID (TID) = 0 (0x0)
2N/A * SMB: Process ID (PID) = 0 (0x0)
2N/A * SMB: User ID (UID) = 0 (0x0)
2N/A * SMB: Multiplex ID (MID) = 0 (0x0)
2N/A * SMB: Flags Summary = 0 (0x0)
2N/A * SMB: Command = C transact
2N/A * SMB: Word count = 17
2N/A * SMB: Word parameters
2N/A * SMB: Total parm bytes = 0
2N/A * SMB: Total data bytes = 33
2N/A * SMB: Max parm bytes = 0
2N/A * SMB: Max data bytes = 0
2N/A * SMB: Max setup words = 0
2N/A * SMB: Transact Flags Summary = 0 (0x0)
2N/A * SMB: ...............0 = Leave session intact
2N/A * SMB: ..............0. = Response required
2N/A * SMB: Transact timeout = 0 (0x0)
2N/A * SMB: Parameter bytes = 0 (0x0)
2N/A * SMB: Parameter offset = 0 (0x0)
2N/A * SMB: Data bytes = 33 (0x21)
2N/A * SMB: Data offset = 86 (0x56)
2N/A * SMB: Setup word count = 3
2N/A * SMB: Setup words
2N/A * SMB: Mailslot opcode = Write mailslot
2N/A * SMB: Transaction priority = 1
2N/A * SMB: Mailslot class = Unreliable (broadcast)
2N/A * SMB: Byte count = 50
2N/A * SMB: Byte parameters
2N/A * SMB: Path name = \MAILSLOT\BROWSE
2N/A * SMB: Transaction data
2N/A * SMB: Data: Number of data bytes remaining = 33 (0x0021)
2N/A *
2N/A * Note the SMB command is Transact, the opcode within the Transact SMB is
2N/A * Mailslot Write, and the browser data structure is carried as the
2N/A * Transact data.
2N/A * The Transaction data begins with an opcode, that signifies the operation
2N/A * and determines the size and structure of data that follows. This opcode
2N/A * is named as per one of the below:
2N/A *
2N/A * HostAnnouncement 1
2N/A * AnnouncementRequest 2
2N/A * RequestElection 8
2N/A * GetBackupListReq 9
2N/A * GetBackupListResp 10
2N/A * BecomeBackup 11
2N/A * DomainAnnouncment 12
2N/A * MasterAnnouncement 13
2N/A * LocalMasterAnnouncement 15
2N/A *
2N/A * Browser datagrams are often referred to as simply browser frames. The
2N/A * frames are in particular, referred to by the name of the opcode within
2N/A * the Transaction data e.g. a GetBackupListReq browser frame, a
2N/A * RequestElection browser frame, etc.
2N/A *
2N/A * The structures that are sent as the data portion of the Transact SMB are
2N/A * described in section(s) 6.2 through 6.12 in this document. These
2N/A * structures are tightly packed, i.e. there are no intervening pad bytes
2N/A * in the structure, unless they are explicitly described as being there.
2N/A * All quantities are sent in native Intel format and multi-byte values are
2N/A * transmitted least significant byte first.
2N/A *
2N/A * Besides mailslots and Transaction SMBs, the other important piece of the
2N/A * browser architecture is the NetServerEnum2 request. This request that
2N/A * allows an application to interrogate a Browser Server and obtain a
2N/A * complete list of resources (servers, domains, etc) known to that Browser
2N/A * server. Details of the NetServerEnum2 request are presented in section
2N/A * 6.4. Some examples of the NetServerEnum2 request being used are when a
2N/A * Local Master Browser sends a NetServerEnum2 request to the Domain Master
2N/A * Browser and vice versa. Another example is when a browser client sends a
2N/A * NetServerEnum2 request to a Backup Browser server.
2N/A *
2N/A * 4.3 Non-Browser Server
2N/A *
2N/A * A non-browser server is a server that has some resource(s) or service(s)
2N/A * it wishes to advertise as being available using the browsing protocol.
2N/A * Examples of non-browser servers would be an SQL server, print server,
2N/A * etc.
2N/A *
2N/A * A non-browser server MUST periodically send a HostAnnouncement browser
2N/A * frame, specifying the type of resources or services it is advertising.
2N/A * Details are in section 6.5.
2N/A *
2N/A * A non-browser server SHOULD announce itself relatively frequently when
2N/A * it first starts up in order to make its presence quickly known to the
2N/A * browsers and thence to potential clients. The frequency of the
2N/A * announcements SHOULD then be gradually stretched, so as to minimize
2N/A * network traffic. Typically, non-browser servers announce themselves
2N/A * once every minute upon start up and then gradually adjust the frequency
2N/A * of the announcements to once every 12 minutes.
2N/A *
2N/A * A non-browser server SHOULD send a HostAnnouncement browser frame
2N/A * specifying a type of 0 just prior to shutting down, to allow it to
2N/A * quickly be removed from the list of available servers.
2N/A *
2N/A * A non-browser server MUST receive and process AnnouncementRequest frames
2N/A * from the Local Master Browser, and MUST respond with a HostAnnouncement
2N/A * frame, after a delay chosen randomly from the interval [0,30] seconds.
2N/A * AnnouncementRequests typically happen when a Local Master Browser starts
2N/A * up with an empty list of servers for the domain, and wants to fill it
2N/A * quickly. The 30 second range for responses prevents the Master Browser
2N/A * from becoming overloaded and losing replies, as well as preventing the
2N/A * network from being flooded with responses.
2N/A *
2N/A * 4.4 Browser Servers
2N/A *
2N/A * The following sections describe the roles of the various types of
2N/A * browser servers.
2N/A *
2N/A * 4.4.1 Potential Browser Server
2N/A *
2N/A * A Potential Browser server is a browser server that is capable of being
2N/A * a Backup Browser server or Master Browser server, but is not currently
2N/A * fulfilling either of those roles.
2N/A *
2N/A * A Potential Browser MUST set type SV_TYPE_POTENTIAL_BROWSER (see section
2N/A * 6.4.1) in its HostAnnouncement until it is ready to shut down. In its
2N/A * last HostAnnouncement frame before it shuts down, it SHOULD specify a
2N/A * type of 0.
2N/A *
2N/A * A Potential Browser server MUST receive and process BecomeBackup frames
2N/A * (see section 6.9) and become a backup browser upon their receipt.
2N/A *
2N/A * A Potential Browser MUST participate in browser elections (see section
2N/A * 6.8).
2N/A *
2N/A * 4.4.2 Backup Browser
2N/A *
2N/A * Backup Browser servers are a subset of the Potential Browsers that have
2N/A * been chosen by the Master Browser on their subnet to be the Backup
2N/A * Browsers for the subnet.
2N/A *
2N/A * A Backup Browser MUST set type SV_TYPE_BACKUP_BROWSER (see section
2N/A * 6.4.1) in its HostAnnouncement until it is ready to shut down. In its
2N/A * last HostAnnouncement frame before it shuts down, it SHOULD specify a
2N/A * type of 0.
2N/A *
2N/A * A Backup Browser MUST listen for a LocalMasterAnnouncement frame (see
2N/A * section 6.10) from the Local Master Browser, and use it to set the name
2N/A * of the Master Browser it queries for the server and domain lists.
2N/A *
2N/A * A Backup Browsers MUST periodically make a NetServerEnum2 request of
2N/A * the Master Browser on its subnet for its domain to get a list of servers
2N/A * in that domain, as well as a list of domains. The period is a
2N/A * configuration option balancing currency of the information with network
2N/A * traffic costs - a typical value is 15 minutes.
2N/A *
2N/A * A Backup Browser SHOULD force an election by sending a RequestElection
2N/A * frame (see section 6.7) if it does not get a response to its periodic
2N/A * NetServeEnum2 request to the Master Browser.
2N/A *
2N/A * A Backup Browser MUST receive and process NetServerEnum2 requests from
2N/A * browser clients, for its own domain and others. If the request is for a
2N/A * list of servers in its domain, or for a list of domains, it can answer
2N/A * from its internal lists. If the request is for a list of servers in a
2N/A * domain different than the one it serves, it sends a NetServerEnum2
2N/A * request to the Domain Master Browser for that domain (which it can in
2N/A * find in its list of domains and their Domain Master Browsers).
2N/A *
2N/A * A Backup Browser MUST participate in browser elections (see section
2N/A * 6.8).
2N/A *
2N/A * 4.4.3 Master Browser
2N/A *
2N/A * Master Browsers are responsible for:
2N/A * . indicating it is a Master Browser
2N/A * . receiving server announcements and building a list of such servers
2N/A * and keeping it reasonably up-to-date.
2N/A * . returning lists of Backup Browsers to browser clients.
2N/A * . ensuring an appropriate number of Backup Browsers are available.
2N/A * . announcing their existence to other Master Browsers on their subnet,
2N/A * to the Domain Master Browser for their domain, and to all browsers in
2N/A * their domain on their subnet
2N/A * . forwarding requests for lists of servers on other domains to the
2N/A * Master Browser for that domain
2N/A * . keeping a list of domains in its subnet
2N/A * . synchronizing with the Domain Master Browser (if any) for its domain
2N/A * . participating in browser elections
2N/A * . ensuring that there is only one Master Browser on its subnet
2N/A *
2N/A * A Master Browser MUST set type SV_TYPE_MASTER_BROWSER (see section
2N/A * 6.4.1) in its HostAnnouncement until it is ready to shut down. In its
2N/A * last HostAnnouncement frame before it shuts down, it SHOULD specify a
2N/A * type of 0.
2N/A *
2N/A * A Master Browser MUST receive and process HostAnnouncement frames from
2N/A * servers, adding the server name and other information to its servers
2N/A * list; it must mark them as "local" entries. Periodically, it MUST check
2N/A * all local server entries to see if a server's HostAnnouncement has timed
2N/A * out (no HostAnnouncement received for three times the periodicity the
2N/A * server gave in the last received HostAnnouncement) and remove timed-out
2N/A * servers from its list.
2N/A *
2N/A * A Master Browser MUST receive and process DomainAnnouncement frames (see
2N/A * section 6.12) and maintain the domain names and their associated (Local)
2N/A * Master Browsers in its internal domain list until they time out; it must
2N/A * mark these as "local" entries. Periodically, it MUST check all local
2N/A * domain entries to see if a server's DomainAnnouncement has timed out (no
2N/A * DomainAnnouncement received for three times the periodicity the server
2N/A * gave in the last received DomainAnnouncement) and remove timed-out
2N/A * servers from its list.
2N/A *
2N/A * A Master Browser MUST receive and process GetBackupListRequest frames
2N/A * from clients, returning GetBackupListResponse frames containing a list
2N/A * of the Backup Servers for its domain.
2N/A *
2N/A * A Master Browser MUST eventually send BecomeBackup frames (see section
2N/A * 6.9) to one or more Potential Browser servers to increase the number of
2N/A * Backup Browsers if there are not enough Backup Browsers to handle the
2N/A * anticipated query load. Note: possible good times for checking for
2N/A * sufficient backup browsers are after being elected, when timing out
2N/A * server HostAnnouncements, and when receiving a server's HostAnnouncement
2N/A * for the first time.
2N/A *
2N/A * A Master Browser MUST periodically announce itself and the domain it
2N/A * serves to other (Local) Master Browsers on its subnet, by sending a
2N/A * DomainAnnouncement frame (see section 6.12) to its subnet.
2N/A *
2N/A * A Master Browser MUST send a MasterAnnouncement frame (see section 6.11)
2N/A * to the Domain Master Browser after it is first elected, and periodically
2N/A * thereafter. This informs the Domain Master Browser of the presence of
2N/A * all the Master Browsers.
2N/A *
2N/A * A Master Browser MUST periodically announce itself to all browsers for
2N/A * its domain on its subnet by sending a LocalMasterAnnouncement frame (see
2N/A * section 6.10).
2N/A *
2N/A * A Master Browser MUST receive and process NetServerEnum2 requests from
2N/A * browser clients, for its own domain and others. If the request is for a
2N/A * list of servers in its domain, or for a list of domains, it can answer
2N/A * from its internal lists. Entries in its list marked "local" MUST have
2N/A * the SV_TYPE_LOCAL_LIST_ONLY bit set in the returned results; it must be
2N/A * clear for all other entries. If the request is for a list of servers in
2N/A * a domain different than the one it serves, it sends a NetServerEnum2
2N/A * request to the Domain Master Browser for that domain (which it can in
2N/A * find in its list of domains and their Domain Master Browsers).
2N/A *
2N/A * Note: The list of servers that the Master Browser maintains and
2N/A * returns to the Backup Browsers, is limited in size to 64K of
2N/A * data. This will limit the number of systems that can be in a
2N/A * browse list in a single workgroup or domain to approximately two
2N/A * thousand systems.
2N/A *
2N/A * A Master Browser SHOULD request all servers to register with it by
2N/A * sending an AnnouncementRequest frame, if, on becoming the Master Browser
2N/A * by winning an election, its server list is empty. Otherwise, clients
2N/A * might get an incomplete list of servers until the servers' periodic
2N/A * registrations fill the server list.
2N/A *
2N/A * If the Master Browser on a subnet is not the Primary Domain Controller
2N/A * (PDC), then it is a Local Master Browser.
2N/A *
2N/A * A Local Master Browser MUST periodically synchronize with the Domain
2N/A * Master Browser (which is the PDC). This synchronization is performed by
2N/A * making a NetServerEnum2 request to the Domain Master Browser and merging
2N/A * the results with its list of servers and domains. An entry from the
2N/A * Domain Master Browser should be marked "non-local", and must not
2N/A * overwrite an entry with the same name marked "local". The Domain Master
2N/A * Browser is located as specified in Appendix B.
2N/A *
2N/A * A Master Browser MUST participate in browser elections (see section
2N/A * 6.8).
2N/A *
2N/A * A Master Browser MUST, if it receives a HostAnnouncement,
2N/A * DomainAnnouncement, or LocalMasterAnnouncement frame another system that
2N/A * claims to be the Master Browser for its domain, demote itself from
2N/A * Master Browser and force an election. This ensures that there is only
2N/A * ever one Master Browser in each workgroup or domain.
2N/A *
2N/A * A Master Browser SHOULD, if it loses an election, become a Backup
2N/A * Browser (without being told to do so by the new Master Browser). Since
2N/A * it has more up-to-date information in its lists than a Potential
2N/A * Browser, it is more efficient to have it be a Backup Browser than to
2N/A * promote a Potential Browser.
2N/A *
2N/A * 4.4.3.1 Preferred Master Browser
2N/A *
2N/A * A Preferred Master Browser supports exactly the same protocol elements
2N/A * as a Potential Browser, except as follows.
2N/A *
2N/A * A Preferred Master Browser MUST always force an election when it starts
2N/A * up.
2N/A *
2N/A * A Preferred Master Browser MUST participate in browser elections (see
2N/A * section 6.8).
2N/A *
2N/A * A Preferred Master Browser MUST set the Preferred Master bit in the
2N/A * RequestElection frame (see section 6.7) to bias the election in its
2N/A * favor.
2N/A *
2N/A * A Preferred Master Browser SHOULD, if it loses an election,
2N/A * automatically become a Backup Browser, without being told to do so by
2N/A * the Master Browser.
2N/A *
2N/A * 4.4.4 Domain Master Browser
2N/A *
2N/A * Since the Domain Master Browser always runs on the PDC, it must
2N/A * implement all the protocols required of a PDC in addition to the
2N/A * browsing protocol, and that is way beyond the scope of this
2N/A * specification.
2N/A *
2N/A * 5. Mailslot Protocol Specification
2N/A *
2N/A * The only transaction allowed to a mailslot is a mailslot write. Mailslot
2N/A * writes requests are encapsulated in TRANSACT SMBs. The following table
2N/A * shows the interpretation of the TRANSACT SMB parameters for a mailslot
2N/A * transaction:
2N/A *
2N/A * Name Value Description
2N/A * Command SMB_COM_TRANSACTION
2N/A * Name <name> STRING name of mail slot to write;
2N/A * must start with "\\MAILSLOT\\"
2N/A * SetupCount 3 Always 3 for mailslot writes
2N/A * Setup[0] 1 Command code == write mailslot
2N/A * Setup[1] Ignored
2N/A * Setup[2] Ignored
2N/A * TotalDataCount n Size of data in bytes to write to
2N/A * the mailslot
2N/A * Data[ n ] The data to write to the mailslot
2N/A *
2N/A */
2N/A
2N/A/*
2N/A * SMB: C transact, File = \MAILSLOT\BROWSE
2N/A * SMB: SMB Status = Error Success
2N/A * SMB: Error class = No Error
2N/A * SMB: Error code = No Error
2N/A * SMB: Header: PID = 0x0000 TID = 0x0000 MID = 0x0000 UID = 0x0000
2N/A * SMB: Tree ID (TID) = 0 (0x0)
2N/A * SMB: Process ID (PID) = 0 (0x0)
2N/A * SMB: User ID (UID) = 0 (0x0)
2N/A * SMB: Multiplex ID (MID) = 0 (0x0)
2N/A * SMB: Flags Summary = 0 (0x0)
2N/A * SMB: Command = C transact
2N/A * SMB: Word count = 17
2N/A * SMB: Word parameters
2N/A * SMB: Total parm bytes = 0
2N/A * SMB: Total data bytes = 33
2N/A * SMB: Max parm bytes = 0
2N/A * SMB: Max data bytes = 0
2N/A * SMB: Max setup words = 0
2N/A * SMB: Transact Flags Summary = 0 (0x0)
2N/A * SMB: ...............0 = Leave session intact
2N/A * SMB: ..............0. = Response required
2N/A * SMB: Transact timeout = 0 (0x0)
2N/A * SMB: Parameter bytes = 0 (0x0)
2N/A * SMB: Parameter offset = 0 (0x0)
2N/A * SMB: Data bytes = 33 (0x21)
2N/A * SMB: Data offset = 86 (0x56)
2N/A * SMB: Setup word count = 3
2N/A * SMB: Setup words
2N/A * SMB: Mailslot opcode = Write mailslot
2N/A * SMB: Transaction priority = 1
2N/A * SMB: Mailslot class = Unreliable (broadcast)
2N/A * SMB: Byte count = 50
2N/A * SMB: Byte parameters
2N/A * SMB: Path name = \MAILSLOT\BROWSE
2N/A * SMB: Transaction data
2N/A * SMB: Data: Number of data bytes remaining = 33 (0x0021)
2N/A *
2N/A * 5. Mailslot Protocol Specification
2N/A *
2N/A * The only transaction allowed to a mailslot is a mailslot write. Mailslot
2N/A * writes requests are encapsulated in TRANSACT SMBs. The following table
2N/A * shows the interpretation of the TRANSACT SMB parameters for a mailslot
2N/A * transaction:
2N/A *
2N/A * Name Value Description
2N/A * Command SMB_COM_TRANSACTION
2N/A * Name <name> STRING name of mail slot to write;
2N/A * must start with "\MAILSLOT\"
2N/A * SetupCount 3 Always 3 for mailslot writes
2N/A * Setup[0] 1 Command code == write mailslot
2N/A * Setup[1] Ignored
2N/A * Setup[2] Ignored
2N/A * TotalDataCount n Size of data in bytes to write to
2N/A * the mailslot
2N/A * Data[ n ] The data to write to the mailslot
2N/A *
2N/A * Magic 0xFF 'S' 'M' 'B'
2N/A * smb_com a byte, the "first" command
2N/A * Error a 4-byte union, ignored in a request
2N/A * smb_flg a one byte set of eight flags
2N/A * smb_flg2 a two byte set of 16 flags
2N/A * . twelve reserved bytes, have a role
2N/A * in connectionless transports (IPX, UDP?)
2N/A * smb_tid a 16-bit tree ID, a mount point sorta,
2N/A * 0xFFFF is this command does not have
2N/A * or require a tree context
2N/A * smb_pid a 16-bit process ID
2N/A * smb_uid a 16-bit user ID, specific to this "session"
2N/A * and mapped to a system (bona-fide) UID
2N/A * smb_mid a 16-bit multiplex ID, used to differentiate
2N/A * multiple simultaneous requests from the same
2N/A * process (pid) (ref RPC "xid")
2N/A */
2N/A
2N/Aint
2N/Asmb_browser_load_transact_header(unsigned char *buffer, int maxcnt,
2N/A int data_count, int reply, char *mailbox)
2N/A{
2N/A smb_msgbuf_t mb;
2N/A int mailboxlen;
2N/A char *fmt;
2N/A int result;
2N/A short class = (reply == ONE_WAY_TRANSACTION) ? 2 : 0;
2N/A
2N/A /*
2N/A * If the mailboxlen is an even number we need to pad the
2N/A * header so that the data starts on a word boundary.
2N/A */
2N/A fmt = "Mb4.bw20.bwwwwb.wl2.wwwwb.wwwws";
2N/A mailboxlen = strlen(mailbox) + 1;
2N/A
2N/A if ((mailboxlen & 0x01) == 0) {
2N/A ++mailboxlen;
2N/A fmt = "Mb4.bw20.bwwwwb.wl2.wwwwb.wwwws.";
2N/A }
2N/A
2N/A bzero(buffer, maxcnt);
2N/A smb_msgbuf_init(&mb, buffer, maxcnt, 0);
2N/A
2N/A result = smb_msgbuf_encode(&mb, fmt,
2N/A SMB_COM_TRANSACTION, /* Command */
2N/A 0x18,
2N/A 0x3,
2N/A 17, /* Count of parameter words */
2N/A 0, /* Total Parameter words sent */
2N/A data_count, /* Total Data bytes sent */
2N/A 2, /* Max Parameters to return */
2N/A 0, /* Max data bytes to return */
2N/A 0, /* Max setup bytes to return */
2N/A reply, /* No reply */
2N/A 0xffffffff, /* Timeout */
2N/A 0, /* Parameter bytes sent */
2N/A 0, /* Parameter offset */
2N/A data_count, /* Data bytes sent */
2N/A 69 + mailboxlen, /* Data offset */
2N/A 3, /* Setup word count */
2N/A 1, /* Setup word[0] */
2N/A 0, /* Setup word[1] */
2N/A class, /* Setup word[2] */
2N/A mailboxlen + data_count, /* Total request bytes */
2N/A mailbox); /* Mailbox address */
2N/A
2N/A smb_msgbuf_term(&mb);
2N/A return (result);
2N/A}
2N/A
2N/Astatic int
2N/Asmb_browser_addr_of_subnet(struct name_entry *name, smb_hostinfo_t *hinfo,
2N/A struct name_entry *result)
2N/A{
2N/A uint32_t ipaddr, mask, saddr;
2N/A addr_entry_t *addr;
2N/A
2N/A if (name == NULL)
2N/A return (-1);
2N/A
2N/A if (hinfo->hi_nic.nic_smbflags & SMB_NICF_ALIAS)
2N/A return (-1);
2N/A
2N/A ipaddr = hinfo->hi_nic.nic_ip.a_ipv4;
2N/A mask = hinfo->hi_nic.nic_mask;
2N/A
2N/A *result = *name;
2N/A addr = &name->addr_list;
2N/A do {
2N/A saddr = addr->sin.sin_addr.s_addr;
2N/A if ((saddr & mask) == (ipaddr & mask)) {
2N/A *result = *name;
2N/A result->addr_list = *addr;
2N/A result->addr_list.forw = result->addr_list.back =
2N/A &result->addr_list;
2N/A return (0);
2N/A }
2N/A addr = addr->forw;
2N/A } while (addr != &name->addr_list);
2N/A
2N/A return (-1);
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Asmb_browser_bcast_addr_of_subnet(struct name_entry *name, uint32_t bcast,
2N/A struct name_entry *result)
2N/A{
2N/A if (name != NULL && name != result)
2N/A *result = *name;
2N/A
2N/A result->addr_list.sin.sin_family = AF_INET;
2N/A result->addr_list.sinlen = sizeof (result->addr_list.sin);
2N/A result->addr_list.sin.sin_addr.s_addr = bcast;
2N/A result->addr_list.sin.sin_port = htons(IPPORT_NETBIOS_DGM);
2N/A result->addr_list.forw = result->addr_list.back = &result->addr_list;
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * 6.5 HostAnnouncement Browser Frame
2N/A *
2N/A * To advertise its presence, i.e. to publish itself as being available, a
2N/A * non-browser server sends a HostAnnouncement browser frame. If the server
2N/A * is a member of domain "D", this frame is sent to the NETBIOS unique name
2N/A * D(1d) and mailslot "\\MAILSLOT\\BROWSE". The definition of the
2N/A * HostAnnouncement frame is:
2N/A *
2N/A * struct {
2N/A * unsigned short Opcode;
2N/A * unsigned char UpdateCount;
2N/A * uint32_t Periodicity;
2N/A * unsigned char ServerName[];
2N/A * unsigned char VersionMajor;
2N/A * unsigned char VersionMinor;
2N/A * uint32_t Type;
2N/A * uint32_t Signature;
2N/A * unsigned char Comment[];
2N/A * }
2N/A *
2N/A * where:
2N/A * Opcode - Identifies this structure as a browser server
2N/A * announcement and is defined as HostAnnouncement with a
2N/A * value of decimal 1.
2N/A *
2N/A * UpdateCount - must be sent as zero and ignored on receipt.
2N/A *
2N/A * Periodicity - The announcement frequency of the server (in
2N/A * seconds). The server will be removed from the browse list
2N/A * if it has not been heard from in 3X its announcement
2N/A * frequency. In no case will the server be removed from the
2N/A * browse list before the period 3X has elapsed. Actual
2N/A * implementations may take more than 3X to actually remove
2N/A * the server from the browse list.
2N/A *
2N/A * ServerName - Null terminated ASCII server name (up to 16 bytes
2N/A * in length).
2N/A *
2N/A * VersionMajor - The major version number of the OS the server
2N/A * is running. it will be returned by NetServerEnum2.
2N/A *
2N/A * VersionMinor - The minor version number of the OS the server
2N/A * is running. This is entirely informational and does not
2N/A * have any significance for the browsing protocol.
2N/A *
2N/A * Type - Specifies the type of the server. The server type bits
2N/A * are specified in the NetServerEnum2 section.
2N/A *
2N/A * Signature - The browser protocol minor version number in the
2N/A * low 8 bits, the browser protocol major version number in
2N/A * the next higher 8 bits and the signature 0xaa55 in the
2N/A * high 16 bits of this field. Thus, for this version of the
2N/A * browser protocol (1.15) this field has the value
2N/A * 0xaa55010f. This may used to isolate browser servers that
2N/A * are running out of revision browser software; otherwise,
2N/A * it is ignored.
2N/A *
2N/A * Comment - Null terminated ASCII comment for the server.
2N/A * Limited to 43 bytes.
2N/A *
2N/A * When a non-browser server starts up, it announces itself in the manner
2N/A * described once every minute. The frequency of these statements is
2N/A * gradually stretched to once every 12 minutes.
2N/A *
2N/A * Note: older non-browser servers in a domain "D" sent HostAnnouncement
2N/A * frames to the NETBIOS group name D(00). Non-Browser servers supporting
2N/A * version 1.15 of the browsing protocol SHOULD NOT use this NETBIOS name,
2N/A * but for backwards compatibility Master Browsers MAY receive and process
2N/A * HostAnnouncement frames on this name as described above for D(1d).
2N/A */
2N/A
2N/Astatic void
2N/Asmb_browser_send_HostAnnouncement(smb_hostinfo_t *hinfo,
2N/A uint32_t next_announcement, boolean_t remove,
2N/A addr_entry_t *addr, char suffix)
2N/A{
2N/A smb_msgbuf_t mb;
2N/A int offset, announce_len, data_length;
2N/A struct name_entry dest_name;
2N/A unsigned char *buffer;
2N/A uint32_t type;
2N/A char resource_domain[SMB_PI_MAX_DOMAIN];
2N/A
2N/A if (smb_getdomainname_nb(resource_domain, SMB_PI_MAX_DOMAIN) != 0)
2N/A return;
2N/A (void) smb_strupr(resource_domain);
2N/A
2N/A if (addr == NULL) {
2N/A /* Local master Browser */
2N/A smb_init_name_struct((unsigned char *)resource_domain, suffix,
2N/A 0, 0, 0, 0, 0, &dest_name);
2N/A if (smb_browser_bcast_addr_of_subnet(0, hinfo->hi_nic.nic_bcast,
2N/A &dest_name) < 0)
2N/A return;
2N/A } else {
2N/A smb_init_name_struct((unsigned char *)resource_domain, suffix,
2N/A 0, 0, 0, 0, 0, &dest_name);
2N/A dest_name.addr_list = *addr;
2N/A dest_name.addr_list.forw = dest_name.addr_list.back =
2N/A &dest_name.addr_list;
2N/A }
2N/A
2N/A /* give some extra room */
2N/A buffer = calloc(1, MAX_DATAGRAM_LENGTH * 2);
2N/A if (buffer == NULL) {
2N/A syslog(LOG_DEBUG, "smb browser: HostAnnouncement: %m");
2N/A return;
2N/A }
2N/A
2N/A data_length = 1 + 1 + 4 + 16 + 1 + 1 + 4 + 4 +
2N/A strlen(hinfo->hi_nic.nic_cmnt) + 1;
2N/A
2N/A offset = smb_browser_load_transact_header(buffer,
2N/A MAX_DATAGRAM_LENGTH, data_length, ONE_WAY_TRANSACTION,
2N/A MAILSLOT_BROWSE);
2N/A
2N/A if (offset < 0) {
2N/A free(buffer);
2N/A return;
2N/A }
2N/A
2N/A /*
2N/A * A non-browser server SHOULD send a HostAnnouncement browser frame
2N/A * specifying a type of 0 just prior to shutting down, to allow it to
2N/A * quickly be removed from the list of available servers.
2N/A */
2N/A if (remove || (!smb_netbios_running()))
2N/A type = 0;
2N/A else
2N/A type = hinfo->hi_type;
2N/A
2N/A smb_msgbuf_init(&mb, buffer + offset, MAX_DATAGRAM_LENGTH - offset, 0);
2N/A
2N/A announce_len = smb_msgbuf_encode(&mb, "bbl16cbblls",
2N/A HOST_ANNOUNCEMENT,
2N/A ++hinfo->hi_updatecnt,
2N/A next_announcement * 60000, /* Periodicity in MilliSeconds */
2N/A hinfo->hi_nbname,
2N/A (uint8_t)hinfo->hi_version.sv_major,
2N/A (uint8_t)hinfo->hi_version.sv_minor,
2N/A type,
2N/A SMB_SERVER_SIGNATURE,
2N/A hinfo->hi_nic.nic_cmnt);
2N/A
2N/A if (announce_len > 0)
2N/A (void) smb_netbios_datagram_send(&hinfo->hi_netname, &dest_name,
2N/A buffer, offset + announce_len);
2N/A
2N/A free(buffer);
2N/A smb_msgbuf_term(&mb);
2N/A}
2N/A
2N/Astatic void
2N/Asmb_browser_process_AnnouncementRequest(struct datagram *datagram,
2N/A char *mailbox)
2N/A{
2N/A smb_hostinfo_t *hinfo;
2N/A uint32_t next_announcement;
2N/A uint32_t delay = random() % 29; /* in seconds */
2N/A boolean_t h_found = B_FALSE;
2N/A
2N/A if (strcmp(mailbox, MAILSLOT_LANMAN) != 0) {
2N/A syslog(LOG_DEBUG, "smb browser: wrong mailbox (%s)", mailbox);
2N/A return;
2N/A }
2N/A
2N/A smb_netbios_sleep(delay);
2N/A
2N/A (void) rw_rdlock(&smb_binfo.bi_hlist_rwl);
2N/A hinfo = list_head(&smb_binfo.bi_hlist);
2N/A while (hinfo) {
2N/A if ((hinfo->hi_nic.nic_ip.a_ipv4 &
2N/A hinfo->hi_nic.nic_mask) ==
2N/A (datagram->src.addr_list.sin.sin_addr.s_addr &
2N/A hinfo->hi_nic.nic_mask)) {
2N/A h_found = B_TRUE;
2N/A break;
2N/A }
2N/A hinfo = list_next(&smb_binfo.bi_hlist, hinfo);
2N/A }
2N/A
2N/A if (h_found) {
2N/A next_announcement = hinfo->hi_nextannouce * 60 * 1000;
2N/A smb_browser_send_HostAnnouncement(hinfo, next_announcement,
2N/A B_FALSE, &datagram->src.addr_list, NBT_MB);
2N/A }
2N/A (void) rw_unlock(&smb_binfo.bi_hlist_rwl);
2N/A}
2N/A
2N/Avoid *
2N/Asmb_browser_dispatch(void *arg)
2N/A{
2N/A struct datagram *datagram = (struct datagram *)arg;
2N/A smb_msgbuf_t mb;
2N/A int rc;
2N/A unsigned char command;
2N/A unsigned char parameter_words;
2N/A unsigned short total_parameter_words;
2N/A unsigned short total_data_count;
2N/A unsigned short max_parameters_to_return;
2N/A unsigned short max_data_to_return;
2N/A unsigned char max_setup_bytes_to_return;
2N/A unsigned short reply;
2N/A unsigned short parameter_bytes_sent;
2N/A unsigned short parameter_offset;
2N/A unsigned short data_bytes_sent;
2N/A unsigned short data_offset;
2N/A unsigned char setup_word_count;
2N/A unsigned short setup_word_0;
2N/A unsigned short setup_word_1;
2N/A unsigned short setup_word_2;
2N/A unsigned short total_request_bytes;
2N/A char *mailbox;
2N/A unsigned char message_type;
2N/A unsigned char *data;
2N/A int datalen;
2N/A
2N/A syslog(LOG_DEBUG, "smb browser: packet received");
2N/A
2N/A smb_msgbuf_init(&mb, datagram->data, datagram->data_length, 0);
2N/A rc = smb_msgbuf_decode(&mb, "Mb27.bwwwwb.w6.wwwwb.wwwws",
2N/A &command, /* Command */
2N/A &parameter_words, /* Count of parameter words */
2N/A &total_parameter_words, /* Total Parameter words sent */
2N/A &total_data_count, /* Total Data bytes sent */
2N/A &max_parameters_to_return, /* Max Parameters to return */
2N/A &max_data_to_return, /* Max data bytes to return */
2N/A &max_setup_bytes_to_return, /* Max setup bytes to return */
2N/A &reply, /* No reply */
2N/A &parameter_bytes_sent, /* Parameter bytes sent */
2N/A &parameter_offset, /* Parameter offset */
2N/A &data_bytes_sent, /* Data bytes sent */
2N/A &data_offset, /* Data offset */
2N/A &setup_word_count, /* Setup word count */
2N/A &setup_word_0, /* Setup word[0] */
2N/A &setup_word_1, /* Setup word[1] */
2N/A &setup_word_2, /* Setup word[2] */
2N/A &total_request_bytes, /* Total request bytes */
2N/A &mailbox); /* Mailbox address */
2N/A
2N/A if (rc < 0) {
2N/A syslog(LOG_ERR, "smb browser: decode error");
2N/A smb_msgbuf_term(&mb);
2N/A free(datagram);
2N/A return (0);
2N/A }
2N/A
2N/A data = &datagram->data[data_offset];
2N/A datalen = datagram->data_length - data_offset;
2N/A
2N/A /*
2N/A * The PDC location protocol, i.e. anything on the \\NET
2N/A * mailslot, is handled by the smb_netlogon module.
2N/A */
2N/A if (strncasecmp("\\MAILSLOT\\NET\\", mailbox, 14) == 0) {
2N/A smb_netlogon_receive(datagram, mailbox, data, datalen);
2N/A smb_msgbuf_term(&mb);
2N/A free(datagram);
2N/A return (0);
2N/A }
2N/A
2N/A /*
2N/A * If it's not a netlogon message, assume it's a browser request.
2N/A * This is not the most elegant way to extract the command byte
2N/A * but at least we no longer use it to get the netlogon opcode.
2N/A */
2N/A message_type = datagram->data[data_offset];
2N/A
2N/A switch (message_type) {
2N/A case ANNOUNCEMENT_REQUEST :
2N/A smb_browser_process_AnnouncementRequest(datagram, mailbox);
2N/A break;
2N/A
2N/A default:
2N/A syslog(LOG_DEBUG, "smb browser: invalid message type(%d, %x)",
2N/A message_type, message_type);
2N/A break;
2N/A }
2N/A
2N/A smb_msgbuf_term(&mb);
2N/A free(datagram);
2N/A return (0);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * 11.1 Registered unique names
2N/A *
2N/A * <COMPUTER>(00)
2N/A * This name is used by all servers and clients to receive second
2N/A * class mailslot messages. A system must add this name in order to
2N/A * receive mailslot messages. The only browser requests that should
2N/A * appear on this name are BecomeBackup, GetBackupListResp,
2N/A * MasterAnnouncement, and LocalMasterAnnouncement frames. All other
2N/A * datagrams (other than the expected non-browser datagrams) may be
2N/A * ignored and an error logged.
2N/A *
2N/A * <DOMAIN>(1d)
2N/A * This name is used to identify a master browser server for domain
2N/A * "DOMAIN" on a subnet. A master browser server adds this name as a
2N/A * unique NETBIOS name when it becomes master browser. If the attempt
2N/A * to add the name fails, the master browser server assumes that there
2N/A * is another master in the domain and will fail to come up. It may
2N/A * log an error if the failure occurs more than 3 times in a row (this
2N/A * either indicates some form of network misconfiguration or a
2N/A * software error). The only requests that should appear on this name
2N/A * are GetBackupListRequest and HostAnnouncement requests. All other
2N/A * datagrams on this name may be ignored (and an error logged). If
2N/A * running a NETBIOS name service (NBNS, such as WINS), this name
2N/A * should not be registered with the NBNS.
2N/A *
2N/A * <DOMAIN>(1b)
2N/A * This name is used to identify the Domain Master Browser for domain
2N/A * "DOMAIN" (which is also the primary domain controller). It is a
2N/A * unique name added only by the primary domain controller. The
2N/A * primary domain controller will respond to GetBackupListRequest on
2N/A * this name just as it responds to these requests on the <DOMAIN>(1d)
2N/A * name.
2N/A *
2N/A * 11.2 Registered group names
2N/A *
2N/A * (01)(02)__MSBROWSE__(02)(01)
2N/A * This name is used by Master Browsers to announce themselves to the
2N/A * other Master Browsers on a subnet. It is added as a group name by
2N/A * all Master Browser servers. The only broadcasts that should appear
2N/A * on this name is DomainAnnouncement requests. All other datagrams
2N/A * can be ignored.
2N/A *
2N/A * <DOMAIN>(00)
2N/A * This name is used by clients and servers in domain "DOMAIN" to
2N/A * process server announcements. The only requests that should appear
2N/A * on this name that the browser is interested in are
2N/A * AnnouncementRequest and NETLOGON_QUERY (to locate the PDC) packets.
2N/A * All other unidentifiable requests may be ignored (and an error
2N/A * logged).
2N/A *
2N/A * <DOMAIN>(1E)
2N/A * This name is used for announcements to browsers for domain "DOMAIN"
2N/A * on a subnet. This name is registered by all the browser servers in
2N/A * the domain. The only requests that should appear on this name are
2N/A * RequestElection and AnnouncementRequest packets. All other
2N/A * datagrams may be ignored (and an error logged).
2N/A *
2N/A * <DOMAIN>(1C)
2N/A * This name is registered by Primary Domain Controllers.
2N/A */
2N/A
2N/Astatic void
2N/Asmb_browser_config(void)
2N/A{
2N/A smb_hostinfo_t *hinfo;
2N/A struct name_entry name;
2N/A struct name_entry master;
2N/A struct name_entry dest;
2N/A struct name_entry *entry;
2N/A char resource_domain[SMB_PI_MAX_DOMAIN];
2N/A int rc;
2N/A
2N/A if (smb_browser_init() != 0)
2N/A return;
2N/A
2N/A if (smb_getdomainname_nb(resource_domain, SMB_PI_MAX_DOMAIN) != 0)
2N/A return;
2N/A (void) smb_strupr(resource_domain);
2N/A
2N/A /* domain<00> */
2N/A smb_init_name_struct((unsigned char *)resource_domain, NBT_WKSTA,
2N/A 0, 0, 0, 0, 0, &name);
2N/A entry = smb_name_find_name(&name);
2N/A smb_name_unlock_name(entry);
2N/A
2N/A (void) rw_rdlock(&smb_binfo.bi_hlist_rwl);
2N/A hinfo = list_head(&smb_binfo.bi_hlist);
2N/A while (hinfo) {
2N/A smb_init_name_struct((unsigned char *)resource_domain,
2N/A NBT_WKSTA, 0, hinfo->hi_nic.nic_ip.a_ipv4,
2N/A htons(IPPORT_NETBIOS_DGM), NAME_ATTR_GROUP,
2N/A NAME_ATTR_LOCAL, &name);
2N/A (void) smb_name_add_name(&name);
2N/A
2N/A hinfo = list_next(&smb_binfo.bi_hlist, hinfo);
2N/A }
2N/A (void) rw_unlock(&smb_binfo.bi_hlist_rwl);
2N/A
2N/A /* All our local master browsers */
2N/A smb_init_name_struct((unsigned char *)resource_domain, NBT_MB,
2N/A 0, 0, 0, 0, 0, &dest);
2N/A entry = smb_name_find_name(&dest);
2N/A
2N/A if (entry) {
2N/A (void) rw_rdlock(&smb_binfo.bi_hlist_rwl);
2N/A hinfo = list_head(&smb_binfo.bi_hlist);
2N/A while (hinfo) {
2N/A rc = smb_browser_addr_of_subnet(entry, hinfo, &master);
2N/A if (rc == 0) {
2N/A syslog(LOG_DEBUG,
2N/A "smb browser: master browser found at %s",
2N/A inet_ntoa(master.addr_list.sin.sin_addr));
2N/A }
2N/A hinfo = list_next(&smb_binfo.bi_hlist, hinfo);
2N/A }
2N/A (void) rw_unlock(&smb_binfo.bi_hlist_rwl);
2N/A
2N/A smb_name_unlock_name(entry);
2N/A }
2N/A
2N/A /* Domain master browser */
2N/A smb_init_name_struct((unsigned char *)resource_domain,
2N/A NBT_DMB, 0, 0, 0, 0, 0, &dest);
2N/A
2N/A if ((entry = smb_name_find_name(&dest)) != 0) {
2N/A syslog(LOG_DEBUG,
2N/A "smb browser: domain master browser for %s is %s",
2N/A resource_domain,
2N/A inet_ntoa(entry->addr_list.sin.sin_addr));
2N/A smb_name_unlock_name(entry);
2N/A }
2N/A}
2N/A
2N/Astatic int
2N/Asmb_browser_init(void)
2N/A{
2N/A smb_hostinfo_t *hinfo;
2N/A smb_niciter_t ni;
2N/A uint32_t type;
2N/A smb_version_t version;
2N/A
2N/A smb_config_get_version(&version);
2N/A
2N/A (void) rw_wrlock(&smb_binfo.bi_hlist_rwl);
2N/A smb_browser_infofree();
2N/A
2N/A if (smb_nic_getfirst(&ni) != SMB_NIC_SUCCESS) {
2N/A (void) rw_unlock(&smb_binfo.bi_hlist_rwl);
2N/A return (-1);
2N/A }
2N/A
2N/A type = MY_SERVER_TYPE;
2N/A if (smb_config_get_secmode() == SMB_SECMODE_DOMAIN)
2N/A type |= SV_DOMAIN_MEMBER;
2N/A
2N/A do {
2N/A if ((ni.ni_nic.nic_smbflags & SMB_NICF_NBEXCL) ||
2N/A (ni.ni_nic.nic_smbflags & SMB_NICF_ALIAS))
2N/A continue;
2N/A
2N/A hinfo = calloc(1, sizeof (smb_hostinfo_t));
2N/A if (hinfo == NULL) {
2N/A smb_browser_infofree();
2N/A (void) rw_unlock(&smb_binfo.bi_hlist_rwl);
2N/A return (-1);
2N/A }
2N/A
2N/A hinfo->hi_nic = ni.ni_nic;
2N/A /* One Minute announcements for first five */
2N/A hinfo->hi_nextannouce = 1;
2N/A hinfo->hi_interval = 1;
2N/A hinfo->hi_reps = 5;
2N/A hinfo->hi_updatecnt = 0;
2N/A hinfo->hi_type = type;
2N/A hinfo->hi_version = version;
2N/A
2N/A /* This is the name used for HostAnnouncement */
2N/A (void) strlcpy(hinfo->hi_nbname, hinfo->hi_nic.nic_host,
2N/A NETBIOS_NAME_SZ);
2N/A (void) smb_strupr(hinfo->hi_nbname);
2N/A /* 0x20: file server service */
2N/A smb_init_name_struct((unsigned char *)hinfo->hi_nbname,
2N/A NBT_SERVER, 0, hinfo->hi_nic.nic_ip.a_ipv4,
2N/A htons(IPPORT_NETBIOS_DGM),
2N/A NAME_ATTR_UNIQUE, NAME_ATTR_LOCAL,
2N/A &hinfo->hi_netname);
2N/A
2N/A list_insert_tail(&smb_binfo.bi_hlist, hinfo);
2N/A smb_binfo.bi_hcnt++;
2N/A } while (smb_nic_getnext(&ni) == SMB_NIC_SUCCESS);
2N/A
2N/A (void) rw_unlock(&smb_binfo.bi_hlist_rwl);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * smb_browser_non_master_duties
2N/A *
2N/A * To advertise its presence, i.e. to publish itself as being available, a
2N/A * non-browser server sends a HostAnnouncement browser frame. If the server
2N/A * is a member of domain "D", this frame is sent to the NETBIOS unique name
2N/A * D(1d) and mailslot "\\MAILSLOT\\BROWSE".
2N/A */
2N/Astatic void
2N/Asmb_browser_non_master_duties(smb_hostinfo_t *hinfo, boolean_t remove)
2N/A{
2N/A struct name_entry name;
2N/A struct name_entry *dest;
2N/A addr_entry_t addr;
2N/A char resource_domain[SMB_PI_MAX_DOMAIN];
2N/A
2N/A smb_browser_send_HostAnnouncement(hinfo, hinfo->hi_interval,
2N/A remove, 0, NBT_MB);
2N/A if (smb_getdomainname_nb(resource_domain, SMB_PI_MAX_DOMAIN) != 0)
2N/A return;
2N/A
2N/A (void) smb_strupr(resource_domain);
2N/A
2N/A smb_init_name_struct((unsigned char *)resource_domain, NBT_MB,
2N/A 0, 0, 0, 0, 0, &name);
2N/A
2N/A if ((dest = smb_name_find_name(&name))) {
2N/A addr = dest->addr_list;
2N/A addr.forw = addr.back = &addr;
2N/A smb_name_unlock_name(dest);
2N/A smb_browser_send_HostAnnouncement(hinfo, hinfo->hi_interval,
2N/A remove, &addr, NBT_MB);
2N/A } else {
2N/A smb_init_name_struct((unsigned char *)resource_domain,
2N/A NBT_DMB, 0, 0, 0, 0, 0, &name);
2N/A if ((dest = smb_name_find_name(&name))) {
2N/A addr = dest->addr_list;
2N/A addr.forw = addr.back = &addr;
2N/A smb_name_unlock_name(dest);
2N/A smb_browser_send_HostAnnouncement(hinfo,
2N/A remove, hinfo->hi_interval, &addr, NBT_DMB);
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * One Minute announcements for first five
2N/A * minutes, one minute longer each round
2N/A * until 12 minutes and every 12 minutes
2N/A * thereafter.
2N/A */
2N/A if (--hinfo->hi_reps == 0) {
2N/A if (hinfo->hi_interval < 12)
2N/A hinfo->hi_interval++;
2N/A
2N/A hinfo->hi_reps = 1;
2N/A }
2N/A
2N/A hinfo->hi_nextannouce = hinfo->hi_interval;
2N/A}
2N/A
2N/A
2N/A/*
2N/A * SMB NetBIOS Browser Service
2N/A */
2N/A/*ARGSUSED*/
2N/Avoid *
2N/Asmb_browser_service(void *arg)
2N/A{
2N/A smb_hostinfo_t *hinfo;
2N/A
2N/A smb_browser_infoinit();
2N/A smb_browser_config();
2N/A
2N/A smb_netbios_event(NETBIOS_EVENT_BROWSER_START);
2N/A
2N/Arestart:
2N/A do {
2N/A (void) rw_rdlock(&smb_binfo.bi_hlist_rwl);
2N/A hinfo = list_head(&smb_binfo.bi_hlist);
2N/A
2N/A while (hinfo) {
2N/A if (--hinfo->hi_nextannouce > 0 ||
2N/A hinfo->hi_nic.nic_bcast == 0) {
2N/A hinfo = list_next(&smb_binfo.bi_hlist, hinfo);
2N/A continue;
2N/A }
2N/A
2N/A smb_browser_non_master_duties(hinfo, B_FALSE);
2N/A
2N/A /* Check to see whether reconfig is needed */
2N/A (void) mutex_lock(&smb_binfo.bi_mtx);
2N/A if (smb_binfo.bi_changed) {
2N/A smb_binfo.bi_changed = B_FALSE;
2N/A (void) mutex_unlock(&smb_binfo.bi_mtx);
2N/A (void) rw_unlock(&smb_binfo.bi_hlist_rwl);
2N/A smb_browser_config();
2N/A goto restart;
2N/A }
2N/A (void) mutex_unlock(&smb_binfo.bi_mtx);
2N/A
2N/A hinfo = list_next(&smb_binfo.bi_hlist, hinfo);
2N/A }
2N/A
2N/A (void) rw_unlock(&smb_binfo.bi_hlist_rwl);
2N/A smb_netbios_sleep(SECSPERMIN); /* 1 minute */
2N/A } while (smb_netbios_running());
2N/A
2N/A smb_browser_infoterm();
2N/A smb_netbios_event(NETBIOS_EVENT_BROWSER_STOP);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * smb_browser_netlogon
2N/A *
2N/A * Sends SAMLOGON/NETLOGON request for all host/ips, except
2N/A * aliases, to find a domain controller.
2N/A *
2N/A * The dc argument will be set if a DC is found.
2N/A */
2N/Aboolean_t
2N/Asmb_browser_netlogon(char *domain, char *dc, uint32_t dc_len, uint32_t *ipaddr)
2N/A{
2N/A smb_hostinfo_t *hinfo;
2N/A boolean_t found = B_FALSE;
2N/A timestruc_t to;
2N/A int err;
2N/A
2N/A (void) rw_rdlock(&smb_binfo.bi_hlist_rwl);
2N/A hinfo = list_head(&smb_binfo.bi_hlist);
2N/A while (hinfo) {
2N/A if ((hinfo->hi_nic.nic_smbflags & SMB_NICF_ALIAS) == 0)
2N/A smb_netlogon_request(&hinfo->hi_netname, domain);
2N/A hinfo = list_next(&smb_binfo.bi_hlist, hinfo);
2N/A }
2N/A (void) rw_unlock(&smb_binfo.bi_hlist_rwl);
2N/A
2N/A bzero(dc, dc_len);
2N/A to.tv_sec = 30;
2N/A to.tv_nsec = 0;
2N/A (void) mutex_lock(&ntdomain_mtx);
2N/A while (ntdomain_info.n_ipaddr == 0) {
2N/A err = cond_reltimedwait(&ntdomain_cv, &ntdomain_mtx, &to);
2N/A if (err == ETIME)
2N/A break;
2N/A }
2N/A
2N/A if (ntdomain_info.n_ipaddr != 0) {
2N/A (void) strlcpy(dc, ntdomain_info.n_name, dc_len);
2N/A *ipaddr = ntdomain_info.n_ipaddr;
2N/A found = B_TRUE;
2N/A }
2N/A (void) mutex_unlock(&ntdomain_mtx);
2N/A
2N/A return (found);
2N/A}
2N/A
2N/A/*
2N/A * smb_browser_infoinit
2N/A *
2N/A * This function is called only once when the browser starts
2N/A * to initialize the global smb_binfo structure.
2N/A */
2N/Astatic void
2N/Asmb_browser_infoinit(void)
2N/A{
2N/A (void) mutex_lock(&ntdomain_mtx);
2N/A bzero(&ntdomain_info, sizeof (ntdomain_info));
2N/A (void) mutex_unlock(&ntdomain_mtx);
2N/A
2N/A (void) rw_wrlock(&smb_binfo.bi_hlist_rwl);
2N/A list_create(&smb_binfo.bi_hlist, sizeof (smb_hostinfo_t),
2N/A offsetof(smb_hostinfo_t, hi_lnd));
2N/A smb_binfo.bi_hcnt = 0;
2N/A (void) rw_unlock(&smb_binfo.bi_hlist_rwl);
2N/A
2N/A (void) mutex_lock(&smb_binfo.bi_mtx);
2N/A smb_binfo.bi_changed = B_FALSE;
2N/A (void) mutex_unlock(&smb_binfo.bi_mtx);
2N/A}
2N/A
2N/A/*
2N/A * smb_browser_infoterm
2N/A *
2N/A * This function is called only once when the browser stops
2N/A * to destroy the smb_binfo structure.
2N/A */
2N/Astatic void
2N/Asmb_browser_infoterm(void)
2N/A{
2N/A (void) rw_wrlock(&smb_binfo.bi_hlist_rwl);
2N/A smb_browser_infofree();
2N/A list_destroy(&smb_binfo.bi_hlist);
2N/A (void) rw_unlock(&smb_binfo.bi_hlist_rwl);
2N/A}
2N/A
2N/A/*
2N/A * smb_browser_infofree
2N/A *
2N/A * Removes all the hostinfo structures from the browser list
2N/A * and frees the allocated memory
2N/A */
2N/Astatic void
2N/Asmb_browser_infofree(void)
2N/A{
2N/A smb_hostinfo_t *hinfo;
2N/A
2N/A while ((hinfo = list_head(&smb_binfo.bi_hlist)) != NULL) {
2N/A list_remove(&smb_binfo.bi_hlist, hinfo);
2N/A free(hinfo);
2N/A }
2N/A
2N/A smb_binfo.bi_hcnt = 0;
2N/A}