1da6c28aamw/*
2da6c28aamw * CDDL HEADER START
3da6c28aamw *
4da6c28aamw * The contents of this file are subject to the terms of the
5da6c28aamw * Common Development and Distribution License (the "License").
6da6c28aamw * You may not use this file except in compliance with the License.
7da6c28aamw *
8da6c28aamw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aamw * or http://www.opensolaris.org/os/licensing.
10da6c28aamw * See the License for the specific language governing permissions
11da6c28aamw * and limitations under the License.
12da6c28aamw *
13da6c28aamw * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aamw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aamw * If applicable, add the following below this CDDL HEADER, with the
16da6c28aamw * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aamw * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aamw *
19da6c28aamw * CDDL HEADER END
20da6c28aamw */
21da6c28aamw/*
22fd9ee8bjoyce mcintosh * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23da6c28aamw */
24da6c28aamw
25a0aa776Alan Wright#include <sys/tzfile.h>
268d7e416jose borrego#include <errno.h>
27da6c28aamw#include <stdlib.h>
28da6c28aamw#include <stdio.h>
29da6c28aamw#include <unistd.h>
30da6c28aamw#include <syslog.h>
31da6c28aamw#include <string.h>
32da6c28aamw#include <strings.h>
33da6c28aamw#include <time.h>
34da6c28aamw#include <synch.h>
35da6c28aamw#include <netdb.h>
36da6c28aamw#include <sys/socket.h>
37da6c28aamw#include <arpa/inet.h>
38da6c28aamw
39dc20a30as#include <smbsrv/libsmb.h>
40da6c28aamw#include <smbsrv/libsmbns.h>
41bbf6f00Jordan Brown#include <smbsrv/smb.h>
42da6c28aamw#include <smbsrv/mailslot.h>
43da6c28aamw#include <smbns_browser.h>
44da6c28aamw#include <smbns_netbios.h>
45da6c28aamw
468d7e416jose borrego/*
478d7e416jose borrego * ntdomain_info
488d7e416jose borrego * Temporary. It should be removed once NBTD is integrated.
498d7e416jose borrego */
508d7e416jose borregosmb_ntdomain_t ntdomain_info;
518d7e416jose borregomutex_t ntdomain_mtx;
528d7e416jose borregocond_t ntdomain_cv;
538d7e416jose borrego
54da6c28aamw#define	SMB_SERVER_SIGNATURE		0xaa550415
55da6c28aamw
567b59d02jbtypedef struct smb_hostinfo {
577b59d02jb	list_node_t	hi_lnd;
587b59d02jb	smb_nic_t	hi_nic;
597b59d02jb	char		hi_nbname[NETBIOS_NAME_SZ];
607b59d02jb	name_entry_t	hi_netname;
617b59d02jb	uint32_t	hi_nextannouce;
627b59d02jb	int		hi_reps;
637b59d02jb	int		hi_interval;
647b59d02jb	uint8_t		hi_updatecnt;
657b59d02jb	uint32_t	hi_type;
669fb67eaafshin salek ardakani - Sun Microsystems - Irvine United States	smb_version_t	hi_version;
677b59d02jb} smb_hostinfo_t;
687b59d02jb
697b59d02jbtypedef struct smb_browserinfo {
707b59d02jb	list_t		bi_hlist;
717b59d02jb	int		bi_hcnt;
727b59d02jb	rwlock_t	bi_hlist_rwl;
737b59d02jb	boolean_t	bi_changed;
747b59d02jb	mutex_t		bi_mtx;
757b59d02jb} smb_browserinfo_t;
767b59d02jb
777b59d02jbstatic smb_browserinfo_t smb_binfo;
787b59d02jb
797b59d02jbstatic int smb_browser_init(void);
807b59d02jbstatic void smb_browser_infoinit(void);
817b59d02jbstatic void smb_browser_infoterm(void);
827b59d02jbstatic void smb_browser_infofree(void);
83da6c28aamw
848d7e416jose borrego
857b59d02jbvoid
867b59d02jbsmb_browser_reconfig(void)
87da6c28aamw{
887b59d02jb	(void) mutex_lock(&smb_binfo.bi_mtx);
897b59d02jb	smb_binfo.bi_changed = B_TRUE;
907b59d02jb	(void) mutex_unlock(&smb_binfo.bi_mtx);
91da6c28aamw}
92da6c28aamw
93da6c28aamw/*
94da6c28aamw * 3. Browser Overview
95da6c28aamw *
96da6c28aamw * Hosts involved in the browsing process can be separated into two
97da6c28aamw * distinct groups, browser clients and browser servers (often referred to
98da6c28aamw * simply as "browsers").
99da6c28aamw *
100da6c28aamw * A browser is a server which maintains information about servers -
101da6c28aamw * primarily the domain they are in and the services that they are running
102da6c28aamw * -- and about domains. Browsers may assume several different roles in
103da6c28aamw * their lifetimes, and dynamically switch between them.
104da6c28aamw *
105da6c28aamw *  Browser clients are of two types: workstations and (non-browser)
106da6c28aamw * servers. In the context of browsing, workstations query browsers for the
107da6c28aamw * information they contain; servers supply browsers the information by
108da6c28aamw * registering with them. Note that, at times, browsers may themselves
109da6c28aamw * behave as browser clients and query other browsers.
110da6c28aamw *
111da6c28aamw * For the purposes of this specification, a domain is simply a name with
112da6c28aamw * which to associate a group of resources such as computers, servers and
113da6c28aamw * users. Domains allow a convenient means for browser clients to restrict
114da6c28aamw * the scope of a search when they query browser servers. Every domain has
115da6c28aamw * a "master" server called the Primary Domain Controller (PDC) that
116da6c28aamw * manages various  activities within the domain.
117da6c28aamw *
118da6c28aamw * One browser for each domain on a subnet is designated the Local Master
119da6c28aamw * Browser for that domain. Servers in its domain on the subnet register
120da6c28aamw * with it, as do the Local Master Browsers for other domains on the
121da6c28aamw * subnet. It uses these registrations to maintain authoritative
122da6c28aamw * information about its domain on its subnet. If there are other subnets
123da6c28aamw * in the network, it also knows the name of the server running the
124da6c28aamw * domain's Domain Master Browser; it registers with it, and uses it to
125da6c28aamw * obtain information about the rest of the network (see below).
126da6c28aamw *
127da6c28aamw * Clients on a subnet query browsers designated as the Backup Browsers for
128da6c28aamw * the subnet (not the Master Browser). Backup Browsers maintain a copy of
129da6c28aamw * the information on the Local Master Browser; they get it by periodically
130da6c28aamw * querying the Local Master Browser for all of its information. Clients
131da6c28aamw * find the Backup Browsers by asking the Local Master Browser. Clients are
132da6c28aamw * expected to spread their queries evenly across Backup Browsers to
133da6c28aamw * balance the load.
134da6c28aamw *
135da6c28aamw * The Local Master Browser is dynamically elected automatically. Multiple
136da6c28aamw * Backup Browser Servers may exist per subnet; they are selected from
137da6c28aamw * among the potential browser servers by the Local Master Browser, which
138da6c28aamw * is configured to select enough to handle the expected query load.
139da6c28aamw *
140da6c28aamw * When there are multiple subnets, a Domain Master Browser is assigned
141da6c28aamw * the task of keeping the multiple subnets in synchronization. The Primary
142da6c28aamw * Domain Controller (PDC) always acts as the Domain Master Browser. The
143da6c28aamw * Domain Master Browser periodically acts as a client and queries all the
144da6c28aamw * Local Master Browsers for its domain, asking them for a list containing
145da6c28aamw * all the domains and all the servers in their domain known within their
146da6c28aamw * subnets; it merges all the replies into a single master list. This
147da6c28aamw * allows a Domain Master Browser server to act as a collection point for
148da6c28aamw * inter-subnet browsing information. Local Master Browsers periodically
149da6c28aamw * query the Domain Master Browser to retrieve the network-wide information
150da6c28aamw * it maintains.
151da6c28aamw *
152da6c28aamw * When a domain spans only a single subnet, there will not be any distinct
153da6c28aamw * Local Master Browser; this role will be handled by the Domain Master
154da6c28aamw * Browser. Similarly, the Domain Master Browser is always the Local Master
155da6c28aamw * Browser for the subnet it is on.
156da6c28aamw *
157da6c28aamw * When a browser client suspects that the Local Master Browser has failed,
158da6c28aamw * the client will instigate an election in which the browser servers
159da6c28aamw * participate, and some browser servers may change roles.
160da6c28aamw *
161da6c28aamw * Some characteristics of a good browsing mechanism include:
162da6c28aamw * . minimal network traffic
163da6c28aamw * . minimum server discovery time
164da6c28aamw * . minimum change discovery latency
165da6c28aamw * . immunity to machine failures
166da6c28aamw *
167da6c28aamw * Historically, Browser implementations had been very closely tied to
168da6c28aamw * NETBIOS and datagrams. The early implementations caused a lot of
169da6c28aamw * broadcast traffic. See Appendix D for an overview that presents how the
170da6c28aamw * Browser specification evolved.
171da6c28aamw *
172da6c28aamw * 4. Browsing Protocol Architecture
173da6c28aamw *
174da6c28aamw * This section first describes the how the browsing protocol is layered,
175da6c28aamw * then describes the roles of clients, servers, and browsers in the
176da6c28aamw * browsing subsystem.
177da6c28aamw *
178da6c28aamw * 4.1 Layering of Browsing Protocol Requests
179da6c28aamw *
180da6c28aamw * Most of the browser functionality is implemented using mailslots.
181da6c28aamw * Mailslots provide a mechanism for fast, unreliable unidirectional data
182da6c28aamw * transfer; they are named via ASCII "mailslot (path) name". Mailslots are
183da6c28aamw * implemented using the CIFS Transact SMB which is encapsulated in a
184da6c28aamw * NETBIOS datagram. Browser protocol requests are sent to browser specific
185da6c28aamw * mailslots using some browser-specific NETBIOS names. These datagrams can
186da6c28aamw * either be unicast or broadcast, depending on whether the NETBIOS name is
187da6c28aamw * a "unique name" or a "group name". Various data structures, which are
188