1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <syslog.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <time.h>
35 #include <synch.h>
36 #include <netdb.h>
37 #include <sys/socket.h>
38 #include <arpa/inet.h>
39 
40 #include <smbsrv/libsmbns.h>
41 
42 #include <smbsrv/cifs.h>
43 #include <smbsrv/mailslot.h>
44 
45 #include <smbns_browser.h>
46 #include <smbns_netbios.h>
47 
48 #define	SMB_SERVER_SIGNATURE		0xaa550415
49 
50 /*
51  * Macro definitions:
52  */
53 static char	*lanman = MAILSLOT_LANMAN;
54 static char	*browse	= MAILSLOT_BROWSE;
55 
56 typedef struct server_info {
57 	uint32_t type;
58 	uint32_t signature;
59 	char major;
60 	char minor;
61 	char hostname[NETBIOS_NAME_SZ];
62 	char comment[SMB_PI_MAX_COMMENT];
63 	char update_count;
64 	struct name_entry name;
65 } server_info_t;
66 
67 #define	BROWSER_NF_INVALID 0x00
68 #define	BROWSER_NF_VALID   0x01
69 
70 typedef struct browser_netinfo {
71 	uint32_t flags;
72 	int	next_announce;
73 	int	reps;
74 	int	interval;
75 	server_info_t server;
76 	mutex_t mtx;
77 } browser_netinfo_t;
78 
79 /*
80  * Local Data Definitions:
81  */
82 static struct browser_netinfo smb_browser_info[SMB_PI_MAX_NETWORKS];
83 
84 static void smb_browser_init(void);
85 
86 static inline browser_netinfo_t *
87 smb_browser_getnet(int net)
88 {
89 	browser_netinfo_t *subnet;
90 
91 	if (net < smb_nic_get_num()) {
92 		subnet = &smb_browser_info[net];
93 		(void) mutex_lock(&subnet->mtx);
94 		if (subnet->flags & BROWSER_NF_VALID)
95 			return (subnet);
96 	}
97 
98 	return (0);
99 }
100 
101 static inline void
102 smb_browser_putnet(browser_netinfo_t *netinfo)
103 {
104 	if (netinfo)
105 		(void) mutex_unlock(&netinfo->mtx);
106 }
107 
108 /*
109  * 3. Browser Overview
110  *
111  * Hosts involved in the browsing process can be separated into two
112  * distinct groups, browser clients and browser servers (often referred to
113  * simply as "browsers").
114  *
115  * A browser is a server which maintains information about servers -
116  * primarily the domain they are in and the services that they are running
117  * -- and about domains. Browsers may assume several different roles in
118  * their lifetimes, and dynamically switch between them.
119  *
120  *  Browser clients are of two types: workstations and (non-browser)
121  * servers. In the context of browsing, workstations query browsers for the
122  * information they contain; servers supply browsers the information by
123  * registering with them. Note that, at times, browsers may themselves
124  * behave as browser clients and query other browsers.
125  *
126  * For the purposes of this specification, a domain is simply a name with
127  * which to associate a group of resources such as computers, servers and
128  * users. Domains allow a convenient means for browser clients to restrict
129  * the scope of a search when they query browser servers. Every domain has
130  * a "master" server called the Primary Domain Controller (PDC) that
131  * manages various  activities within the domain.
132  *
133  * One browser for each domain on a subnet is designated the Local Master
134  * Browser for that domain. Servers in its domain on the subnet register
135  * with it, as do the Local Master Browsers for other domains on the
136  * subnet. It uses these registrations to maintain authoritative
137  * information about its domain on its subnet. If there are other subnets
138  * in the network, it also knows the name of the server running the
139  * domain's Domain Master Browser; it registers with it, and uses it to
140  * obtain information about the rest of the network (see below).
141  *
142  * Clients on a subnet query browsers designated as the Backup Browsers for
143  * the subnet (not the Master Browser). Backup Browsers maintain a copy of
144  * the information on the Local Master Browser; they get it by periodically
145  * querying the Local Master Browser for all of its information. Clients
146  * find the Backup Browsers by asking the Local Master Browser. Clients are
147  * expected to spread their queries evenly across Backup Browsers to
148  * balance the load.
149  *
150  * The Local Master Browser is dynamically elected automatically. Multiple
151  * Backup Browser Servers may exist per subnet; they are selected from
152  * among the potential browser servers by the Local Master Browser, which
153  * is configured to select enough to handle the expected query load.
154  *
155  * When there are multiple subnets, a Domain Master Browser is assigned
156  * the task of keeping the multiple subnets in synchronization. The Primary
157  * Domain Controller (PDC) always acts as the Domain Master Browser. The
158  * Domain Master Browser periodically acts as a client and queries all the
159  * Local Master Browsers for its domain, asking them for a list containing
160  * all the domains and all the servers in their domain known within their
161  * subnets; it merges all the replies into a single master list. This
162  * allows a Domain Master Browser server to act as a collection point for
163  * inter-subnet browsing information. Local Master Browsers periodically
164  * query the Domain Master Browser to retrieve the network-wide information
165  * it maintains.
166  *
167  * When a domain spans only a single subnet, there will not be any distinct
168  * Local Master Browser; this role will be handled by the Domain Master
169  * Browser. Similarly, the Domain Master Browser is always the Local Master
170  * Browser for the subnet it is on.
171  *
172  * When a browser client suspects that the Local Master Browser has failed,
173  * the client will instigate an election in which the browser servers
174  * participate, and some browser servers may change roles.
175  *
176  * Some characteristics of a good browsing mechanism include:
177  * . minimal network traffic
178  * . minimum server discovery time
179  * . minimum change discovery latency
180  * . immunity to machine failures
181  *
182  * Historically, Browser implementations had been very closely tied to
183  * NETBIOS and datagrams. The early implementations caused a lot of
184  * broadcast traffic. See Appendix D for an overview that presents how the
185  * Browser specification evolved.
186  *
187  * 4. Browsing Protocol Architecture
188  *
189  * This section first describes the how the browsing protocol is layered,
190  * then describes the roles of clients, servers, and browsers in the
191  * browsing subsystem.
192  *
193  * 4.1 Layering of Browsing Protocol Requests
194  *
195  * Most of the browser functionality is implemented using mailslots.
196  * Mailslots provide a mechanism for fast, unreliable unidirectional data
197  * transfer; they are named via ASCII "mailslot (path) name". Mailslots are
198  * implemented using the CIFS Transact SMB which is encapsulated in a
199  * NETBIOS datagram. Browser protocol requests are sent to browser specific
200  * mailslots using some browser-specific NETBIOS names. These datagrams can
201  * either be unicast or broadcast, depending on whether the NETBIOS name is
202  * a "unique name" or a "group name". Various data structures, which are
203  * detailed subsequently within this document, flow as the data portion of
204  * the Transact SMB.
205  *
206  * Here is an example of a generic browser SMB, showing how a browser
207  * request is encapsulated in a TRANSACT SMB request. Note that the PID,
208  * TID, MID, UID, and Flags are all 0 in mailslot requests.
209  *
210  * SMB: C transact, File = \MAILSLOT\BROWSE
211  *   SMB: SMB Status = Error Success
212  *     SMB: Error class = No Error
213  *     SMB: Error code = No Error
214  *   SMB: Header: PID = 0x0000 TID = 0x0000 MID = 0x0000 UID = 0x0000
215  *     SMB: Tree ID   (TID) = 0 (0x0)
216  *     SMB: Process ID  (PID) = 0 (0x0)
217  *     SMB: User ID   (UID) = 0 (0x0)
218  *     SMB: Multiplex ID (MID) = 0 (0x0)
219  *     SMB: Flags Summary = 0 (0x0)
220  *   SMB: Command = C transact
221  *     SMB: Word count = 17
222  *     SMB: Word parameters
223  *     SMB: Total parm bytes = 0
224  *     SMB: Total data bytes = 33
225  *     SMB: Max parm bytes = 0
226  *     SMB: Max data bytes = 0
227  *     SMB: Max setup words = 0
228  *     SMB: Transact Flags Summary = 0 (0x0)
229  *       SMB: ...............0 = Leave session intact
230  *       SMB: ..............0. = Response required
231  *     SMB: Transact timeout = 0 (0x0)
232  *     SMB: Parameter bytes = 0 (0x0)
233  *     SMB: Parameter offset = 0 (0x0)
234  *     SMB: Data bytes = 33 (0x21)
235  *     SMB: Data offset = 86 (0x56)
236  *     SMB: Setup word count = 3
237  *     SMB: Setup words
238  *     SMB: Mailslot opcode = Write mailslot
239  *     SMB: Transaction priority = 1
240  *     SMB: Mailslot class = Unreliable (broadcast)
241  *     SMB: Byte count = 50
242  *     SMB: Byte parameters
243  *     SMB: Path name = \MAILSLOT\BROWSE
244  *     SMB: Transaction data
245  *   SMB: Data: Number of data bytes remaining = 33 (0x0021)
246  *
247  * Note the SMB command is Transact, the opcode within the Transact SMB is
248  * Mailslot Write, and the browser data structure is carried as the
249  * Transact data.
250  * The Transaction data begins with an opcode, that signifies the operation
251  * and determines the size and structure of data that follows. This opcode
252  * is named as per one of the below:
253  *
254  * HostAnnouncement         1
255  * AnnouncementRequest      2
256  * RequestElection          8
257  * GetBackupListReq         9
258  * GetBackupListResp        10
259  * BecomeBackup             11
260  * DomainAnnouncment        12
261  * MasterAnnouncement       13
262  * LocalMasterAnnouncement  15
263  *
264  * Browser datagrams are often referred to as simply browser frames. The
265  * frames are in particular, referred to by the name of the opcode within
266  * the Transaction data e.g. a GetBackupListReq browser frame, a
267  * RequestElection browser frame, etc.
268  *
269  * The structures that are sent as the data portion of the Transact SMB are
270  * described in section(s) 6.2 through 6.12 in this document. These
271  * structures are tightly packed, i.e. there are no intervening pad bytes
272  * in the structure, unless they are explicitly described as being there.
273  * All quantities are sent in native Intel format and multi-byte values are
274  * transmitted least significant byte first.
275  *
276  * Besides mailslots and Transaction SMBs, the other important piece of the
277  * browser architecture is the NetServerEnum2 request. This request that
278  * allows an application to interrogate a Browser Server and obtain a
279  * complete list of resources (servers, domains, etc) known to that Browser
280  * server. Details of the NetServerEnum2 request are presented in section
281  * 6.4. Some examples of the NetServerEnum2 request being used are when a
282  * Local Master Browser sends a NetServerEnum2 request to the Domain Master
283  * Browser and vice versa. Another example is when a browser client sends a
284  * NetServerEnum2 request to a Backup Browser server.
285  *
286  * 4.3 Non-Browser Server
287  *
288  * A non-browser server is a server that has some resource(s) or service(s)
289  * it wishes to advertise as being available using the browsing protocol.
290  * Examples of non-browser servers would be an SQL server, print server,
291  * etc.
292  *
293  * A non-browser server MUST periodically send a HostAnnouncement browser
294  * frame, specifying the type of resources or services it is advertising.
295  * Details are in section 6.5.
296  *
297  * A non-browser server SHOULD announce itself relatively frequently when
298  * it first starts up in order to make its presence quickly known to the
299  * browsers and thence to potential clients. The frequency of the
300  * announcements SHOULD then be gradually stretched, so as to minimize
301  * network traffic. Typically,  non-browser servers announce themselves
302  * once every minute upon start up and then gradually adjust the frequency
303  * of the announcements to once every 12 minutes.
304  *
305  * A non-browser server SHOULD send a HostAnnouncement browser frame
306  * specifying a type of  0 just prior to shutting down, to allow it to
307  * quickly be removed from the list of available servers.
308  *
309  * A non-browser server MUST receive and process AnnouncementRequest frames
310  * from the Local Master Browser, and MUST respond with a HostAnnouncement
311  * frame, after a delay chosen randomly from the interval [0,30] seconds.
312  * AnnouncementRequests typically happen when a Local Master Browser starts
313  * up with an empty list of servers for the domain, and wants to fill it
314  * quickly. The 30 second range for responses prevents the Master Browser
315  * from becoming overloaded and losing replies, as well as preventing the
316  * network from being flooded with responses.
317  *
318  * 4.4  Browser Servers
319  *
320  * The following sections describe the roles of the various types of
321  * browser servers.
322  *
323  * 4.4.1  Potential Browser Server
324  *
325  * A Potential Browser server is a browser server that is capable of being
326  * a Backup Browser server or Master Browser server, but is not currently
327  * fulfilling either of those roles.
328  *
329  * A Potential Browser MUST set type SV_TYPE_POTENTIAL_BROWSER (see section
330  * 6.4.1) in its HostAnnouncement until it is ready to shut down. In its
331  * last HostAnnouncement frame before it shuts down, it SHOULD specify a
332  * type of  0.
333  *
334  * A Potential Browser server MUST receive and process BecomeBackup frames
335  * (see section 6.9) and become a backup browser upon their receipt.
336  *
337  * A Potential Browser MUST participate in browser elections (see section
338  * 6.8).
339  *
340  * 4.4.2  Backup Browser
341  *
342  * Backup Browser servers are a subset of the Potential Browsers that have
343  * been chosen by the Master Browser on their subnet to be the Backup
344  * Browsers for the subnet.
345  *
346  * A Backup Browser MUST set type SV_TYPE_BACKUP_BROWSER (see section
347  * 6.4.1) in its HostAnnouncement until it is ready to shut down. In its
348  * last HostAnnouncement frame before it shuts down, it SHOULD specify a
349  * type of  0.
350  *
351  * A Backup Browser MUST listen for a LocalMasterAnnouncement frame (see
352  * section 6.10) from the Local Master Browser, and use it to set the name
353  * of the Master Browser it queries for the server and domain lists.
354  *
355  * A  Backup Browsers MUST periodically make a NetServerEnum2 request of
356  * the Master Browser on its subnet for its domain to get a list of servers
357  * in that domain, as well as a list of domains. The period is a
358  * configuration option balancing currency of the information with network
359  * traffic costs - a typical value is 15 minutes.
360  *
361  * A Backup Browser SHOULD force an election by sending a RequestElection
362  * frame (see section 6.7) if it does not get a response to its periodic
363  * NetServeEnum2 request to the Master Browser.
364  *
365  * A Backup Browser MUST receive and process NetServerEnum2 requests from
366  * browser clients, for its own domain and others. If the request is for a
367  * list of servers in its domain, or for a list of domains, it can answer
368  * from its internal lists. If the request is for a list of servers in a
369  * domain different than the one it serves, it sends a NetServerEnum2
370  * request to the Domain Master Browser for that domain (which it can in
371  * find in its list of domains and their Domain Master Browsers).
372  *
373  * A Backup Browser MUST participate in browser elections (see section
374  * 6.8).
375  *
376  * 4.4.3 Master Browser
377  *
378  * Master Browsers are responsible for:
379  * . indicating it is a Master Browser
380  * . receiving server announcements and building a list of such servers
381  *   and keeping it reasonably up-to-date.
382  * . returning lists of Backup Browsers to browser clients.
383  * . ensuring an appropriate number of Backup Browsers are available.
384  * . announcing their existence to other Master Browsers on their subnet,
385  *   to the Domain Master Browser for their domain, and to all browsers in
386  *   their domain on their subnet
387  * . forwarding requests for lists of servers on other domains to the
388  *   Master Browser for that domain
389  * . keeping a list of domains in its subnet
390  * . synchronizing with the Domain Master Browser (if any) for its domain
391  * . participating in browser elections
392  * . ensuring that there is only one Master Browser on its subnet
393  *
394  * A Master Browser MUST set type SV_TYPE_MASTER_BROWSER (see section
395  * 6.4.1) in its HostAnnouncement until it is ready to shut down. In its
396  * last HostAnnouncement frame before it shuts down, it SHOULD specify a
397  * type of  0.
398  *
399  * A Master Browser MUST receive and process HostAnnouncement frames from
400  * servers, adding the server name and other information to its servers
401  * list; it must mark them as "local" entries. Periodically, it MUST check
402  * all local server entries to see if a server's HostAnnouncement has timed
403  * out (no HostAnnouncement received for three times the periodicity the
404  * server gave in the last received HostAnnouncement) and remove timed-out
405  * servers from its list.
406  *
407  * A Master Browser MUST receive and process DomainAnnouncement frames (see
408  * section 6.12) and maintain the domain names and their associated (Local)
409  * Master Browsers in its internal domain list until they time out; it must
410  * mark these as "local" entries. Periodically, it MUST check all local
411  * domain entries to see if a server's DomainAnnouncement has timed out (no
412  * DomainAnnouncement received for three times the periodicity the server
413  * gave in the last received DomainAnnouncement) and remove timed-out
414  * servers from its list.
415  *
416  * A Master Browser MUST receive and process GetBackupListRequest frames
417  * from clients, returning GetBackupListResponse frames containing a list
418  * of the Backup Servers for its domain.
419  *
420  * A Master Browser MUST eventually send BecomeBackup frames (see section
421  * 6.9) to one or more Potential Browser servers to increase the number of
422  * Backup Browsers if there are not enough Backup Browsers to handle the
423  * anticipated query load. Note: possible good times for checking for
424  * sufficient backup browsers are after being elected, when timing out
425  * server HostAnnouncements, and when receiving a server's HostAnnouncement
426  * for the first time.
427  *
428  * A Master Browser MUST periodically announce itself and the domain it
429  * serves to other (Local) Master Browsers on its subnet, by sending a
430  * DomainAnnouncement frame (see section 6.12) to its subnet.
431  *
432  * A Master Browser MUST send a MasterAnnouncement frame (see section 6.11)
433  * to the Domain Master Browser after it is first elected, and periodically
434  * thereafter. This informs the Domain Master Browser of the presence of
435  * all the Master Browsers.
436  *
437  * A Master Browser MUST periodically announce itself to all browsers for
438  * its domain on its subnet by sending a LocalMasterAnnouncement frame (see
439  * section 6.10).
440  *
441  * A Master Browser MUST receive and process NetServerEnum2 requests from
442  * browser clients, for its own domain and others. If the request is for a
443  * list of servers in its domain, or for a list of domains, it can answer
444  * from its internal lists. Entries in its list marked "local" MUST have
445  * the SV_TYPE_LOCAL_LIST_ONLY bit set in the returned results; it must be
446  * clear for all other entries. If the request is for a list of servers in
447  * a domain different than the one it serves, it sends a NetServerEnum2
448  * request to the Domain Master Browser for that domain (which it can in
449  * find in its list of domains and their Domain Master Browsers).
450  *
451  *     Note: The list of servers that the Master Browser maintains and
452  *     returns to the Backup Browsers, is limited in size to 64K of
453  *     data. This will limit the number of systems that can be in a
454  *     browse list in a single workgroup or domain to approximately two
455  *     thousand systems.
456  *
457  * A Master Browser SHOULD request all servers to register with it by
458  * sending an AnnouncementRequest frame, if, on becoming the Master Browser
459  * by winning an election, its server list is empty. Otherwise, clients
460  * might get an incomplete list of servers until the servers' periodic
461  * registrations fill the server list.
462  *
463  * If the Master Browser on a subnet is not the Primary Domain Controller
464  * (PDC), then it is a Local Master Browser.
465  *
466  * A Local Master Browser MUST periodically synchronize with the Domain
467  * Master Browser (which is the PDC). This synchronization is performed by
468  * making a NetServerEnum2 request to the Domain Master Browser and merging
469  * the results with its list of servers and domains. An entry from the
470  * Domain Master Browser should be marked "non-local", and must not
471  * overwrite an entry with the same name marked "local". The Domain Master
472  * Browser is located as specified in Appendix B.
473  *
474  * A Master Browser MUST participate in browser elections (see section
475  * 6.8).
476  *
477  * A Master Browser MUST, if it receives a HostAnnouncement,
478  * DomainAnnouncement, or LocalMasterAnnouncement frame another system that
479  * claims to be the Master Browser for its domain, demote itself from
480  * Master Browser and force an election. This ensures that there is only
481  * ever one Master Browser in each workgroup or domain.
482  *
483  * A Master Browser SHOULD, if it loses an election, become a Backup
484  * Browser (without being told to do so by the new Master Browser). Since
485  * it has more up-to-date information in its lists than a Potential
486  * Browser, it is more efficient to have it be a Backup Browser than to
487  * promote a Potential Browser.
488  *
489  * 4.4.3.1 Preferred Master Browser
490  *
491  * A Preferred Master Browser supports exactly the same protocol elements
492  * as a Potential Browser, except as follows.
493  *
494  * A Preferred Master Browser MUST always force an election when it starts
495  * up.
496  *
497  * A Preferred Master Browser MUST participate in browser elections (see
498  * section 6.8).
499  *
500  * A Preferred Master Browser MUST set the Preferred Master bit in the
501  * RequestElection frame (see section 6.7) to bias the election in its
502  * favor.
503  *
504  * A Preferred Master Browser SHOULD, if it loses an election,
505  * automatically become a Backup Browser, without being told to do so by
506  * the Master Browser.
507  *
508  * 4.4.4 Domain Master Browser
509  *
510  * Since the Domain Master Browser always runs on the PDC, it must
511  * implement all the protocols required of a PDC in addition to the
512  * browsing protocol, and that is way beyond the scope of this
513  * specification.
514  *
515  * 5. Mailslot Protocol Specification
516  *
517  * The only transaction allowed to a mailslot is a mailslot write. Mailslot
518  * writes requests are encapsulated in TRANSACT SMBs. The following table
519  * shows the interpretation of the TRANSACT SMB parameters for a mailslot
520  * transaction:
521  *
522  *  Name            Value               Description
523  *  Command         SMB_COM_TRANSACTION
524  *  Name            <name>              STRING name of mail slot to write;
525  *                                      must start with "\\MAILSLOT\\"
526  *  SetupCount      3                   Always 3 for mailslot writes
527  *  Setup[0]        1                   Command code == write mailslot
528  *  Setup[1]        Ignored
529  *  Setup[2]        Ignored
530  *  TotalDataCount  n                   Size of data in bytes to write to
531  *                                      the mailslot
532  *  Data[ n ]                           The data to write to the mailslot
533  *
534  */
535 
536 /*
537  * SMB: C transact, File = \MAILSLOT\BROWSE
538  *   SMB: SMB Status = Error Success
539  *     SMB: Error class = No Error
540  *     SMB: Error code = No Error
541  *   SMB: Header: PID = 0x0000 TID = 0x0000 MID = 0x0000 UID = 0x0000
542  *     SMB: Tree ID   (TID) = 0 (0x0)
543  *     SMB: Process ID  (PID) = 0 (0x0)
544  *     SMB: User ID   (UID) = 0 (0x0)
545  *     SMB: Multiplex ID (MID) = 0 (0x0)
546  *     SMB: Flags Summary = 0 (0x0)
547  *   SMB: Command = C transact
548  *     SMB: Word count = 17
549  *     SMB: Word parameters
550  *     SMB: Total parm bytes = 0
551  *     SMB: Total data bytes = 33
552  *     SMB: Max parm bytes = 0
553  *     SMB: Max data bytes = 0
554  *     SMB: Max setup words = 0
555  *     SMB: Transact Flags Summary = 0 (0x0)
556  *       SMB: ...............0 = Leave session intact
557  *       SMB: ..............0. = Response required
558  *     SMB: Transact timeout = 0 (0x0)
559  *     SMB: Parameter bytes = 0 (0x0)
560  *     SMB: Parameter offset = 0 (0x0)
561  *     SMB: Data bytes = 33 (0x21)
562  *     SMB: Data offset = 86 (0x56)
563  *     SMB: Setup word count = 3
564  *     SMB: Setup words
565  *     SMB: Mailslot opcode = Write mailslot
566  *     SMB: Transaction priority = 1
567  *     SMB: Mailslot class = Unreliable (broadcast)
568  *     SMB: Byte count = 50
569  *     SMB: Byte parameters
570  *     SMB: Path name = \MAILSLOT\BROWSE
571  *     SMB: Transaction data
572  *   SMB: Data: Number of data bytes remaining = 33 (0x0021)
573  *
574  * 5. Mailslot Protocol Specification
575  *
576  * The only transaction allowed to a mailslot is a mailslot write. Mailslot
577  * writes requests are encapsulated in TRANSACT SMBs. The following table
578  * shows the interpretation of the TRANSACT SMB parameters for a mailslot
579  * transaction:
580  *
581  *  Name            Value               Description
582  *  Command         SMB_COM_TRANSACTION
583  *  Name            <name>              STRING name of mail slot to write;
584  *                                      must start with "\MAILSLOT\"
585  *  SetupCount      3                   Always 3 for mailslot writes
586  *  Setup[0]        1                   Command code == write mailslot
587  *  Setup[1]        Ignored
588  *  Setup[2]        Ignored
589  *  TotalDataCount  n                   Size of data in bytes to write to
590  *                                      the mailslot
591  *  Data[ n ]                           The data to write to the mailslot
592  *
593  *	Magic		0xFF 'S' 'M' 'B'
594  *	smb_com 	a byte, the "first" command
595  *	Error		a 4-byte union, ignored in a request
596  *	smb_flg		a one byte set of eight flags
597  *	smb_flg2	a two byte set of 16 flags
598  *	.		twelve reserved bytes, have a role
599  *			in connectionless transports (IPX, UDP?)
600  *	smb_tid		a 16-bit tree ID, a mount point sorta,
601  *			0xFFFF is this command does not have
602  *			or require a tree context
603  *	smb_pid		a 16-bit process ID
604  *	smb_uid		a 16-bit user ID, specific to this "session"
605  *			and mapped to a system (bona-fide) UID
606  *	smb_mid		a 16-bit multiplex ID, used to differentiate
607  *			multiple simultaneous requests from the same
608  *			process (pid) (ref RPC "xid")
609  */
610 
611 int
612 smb_browser_load_transact_header(unsigned char *buffer, int maxcnt,
613 	int data_count, int reply, char *mailbox)
614 {
615 	smb_msgbuf_t mb;
616 	int	mailboxlen;
617 	char *fmt;
618 	int result;
619 	short	class = (reply == ONE_WAY_TRANSACTION) ? 2 : 0;
620 
621 	/*
622 	 * If the mailboxlen is an even number we need to pad the
623 	 * header so that the data starts on a word boundary.
624 	 */
625 	fmt = "Mb4.bw20.bwwwwb.wl2.wwwwb.wwwws";
626 	mailboxlen = strlen(mailbox) + 1;
627 
628 	if ((mailboxlen & 0x01) == 0) {
629 		++mailboxlen;
630 		fmt = "Mb4.bw20.bwwwwb.wl2.wwwwb.wwwws.";
631 	}
632 
633 	bzero(buffer, maxcnt);
634 	smb_msgbuf_init(&mb, buffer, maxcnt, 0);
635 
636 	result = smb_msgbuf_encode(&mb, fmt,
637 	    SMB_COM_TRANSACTION,	/* Command */
638 	    0x18,
639 	    0x3,
640 	    17,				/* Count of parameter words */
641 	    0,				/* Total Parameter words sent */
642 	    data_count,			/* Total Data bytes sent */
643 	    2,				/* Max Parameters to return */
644 	    0,				/* Max data bytes to return */
645 	    0,				/* Max setup bytes to return */
646 	    reply,			/* No reply */
647 	    0xffffffff,			/* Timeout */
648 	    0,				/* Parameter bytes sent */
649 	    0,				/* Parameter offset */
650 	    data_count,			/* Data bytes sent */
651 	    69 + mailboxlen,		/* Data offset */
652 	    3,				/* Setup word count */
653 	    1,				/* Setup word[0] */
654 	    0,				/* Setup word[1] */
655 	    class,			/* Setup word[2] */
656 	    mailboxlen + data_count,	/* Total request bytes */
657 	    mailbox);			/* Mailbox address */
658 
659 	smb_msgbuf_term(&mb);
660 	return (result);
661 }
662 
663 /*
664  * smb_net_id
665  *
666  * Lookup for the given IP in the NICs info table.
667  * If it finds a matching entry it'll return the index,
668  * otherwise returns -1.
669  *
670  * SMB network table and SMB browser info table share
671  * the same index.
672  */
673 int
674 smb_net_id(uint32_t ipaddr)
675 {
676 	uint32_t myaddr, mask;
677 	int net, smb_nc_cnt;
678 
679 	smb_nc_cnt = smb_nic_get_num();
680 	for (net = 0; net < smb_nc_cnt; net++) {
681 		net_cfg_t cfg;
682 		if (smb_nic_get_byind(net, &cfg) == NULL)
683 			break;
684 		mask = cfg.mask;
685 		myaddr = cfg.ip;
686 		if ((ipaddr & mask) == (myaddr & mask))
687 			return (net);
688 	}
689 
690 	return (-1);
691 }
692 
693 /*
694  * smb_browser_get_srvname
695  *
696  */
697 struct name_entry *
698 smb_browser_get_srvname(unsigned short netid)
699 {
700 	if (netid < smb_nic_get_num())
701 		return (&(smb_browser_info[netid].server.name));
702 
703 	return (NULL);
704 }
705 
706 static int
707 smb_browser_addr_of_subnet(struct name_entry *name, int subnet,
708     struct name_entry *result)
709 {
710 	uint32_t ipaddr, mask, saddr;
711 	struct addr_entry *addr;
712 	int smb_nc_cnt;
713 	net_cfg_t cfg;
714 
715 	smb_nc_cnt = smb_nic_get_num();
716 	if ((name == 0) || subnet >= smb_nc_cnt)
717 		return (-1);
718 
719 	if (smb_nic_get_byind(subnet, &cfg) == NULL)
720 		return (-1);
721 	ipaddr = cfg.ip;
722 	mask = cfg.mask;
723 
724 	*result = *name;
725 	addr = &name->addr_list;
726 	do {
727 		saddr = addr->sin.sin_addr.s_addr;
728 		if ((saddr & mask) == (ipaddr & mask)) {
729 			*result = *name;
730 			result->addr_list = *addr;
731 			result->addr_list.forw = result->addr_list.back =
732 			    &result->addr_list;
733 			return (0);
734 		}
735 		addr = addr->forw;
736 	} while (addr != &name->addr_list);
737 
738 	return (-1);
739 }
740 
741 
742 static int
743 smb_browser_bcast_addr_of_subnet(struct name_entry *name, int net,
744     struct name_entry *result)
745 {
746 	uint32_t broadcast;
747 	int smb_nc_cnt;
748 	net_cfg_t cfg;
749 
750 	smb_nc_cnt = smb_nic_get_num();
751 	if (net >= smb_nc_cnt)
752 		return (-1);
753 
754 	if (name != 0 && name != result)
755 		*result = *name;
756 
757 	if (smb_nic_get_byind(net, &cfg) == NULL)
758 		return (-1);
759 
760 	broadcast = cfg.broadcast;
761 	result->addr_list.sin.sin_family = AF_INET;
762 	result->addr_list.sinlen = sizeof (result->addr_list.sin);
763 	result->addr_list.sin.sin_addr.s_addr = broadcast;
764 	result->addr_list.sin.sin_port = htons(DGM_SRVC_UDP_PORT);
765 	result->addr_list.forw = result->addr_list.back = &result->addr_list;
766 	return (0);
767 }
768 
769 /*
770  * 6.5 HostAnnouncement Browser Frame
771  *
772  * To advertise its presence, i.e. to publish itself as being available, a
773  * non-browser server sends a HostAnnouncement browser frame. If the server
774  * is a member of domain "D", this frame is sent to the NETBIOS unique name
775  * D(1d) and mailslot "\\MAILSLOT\\BROWSE". The definition of  the
776  * HostAnnouncement frame is:
777  *
778  *     struct {
779  *         unsigned short  Opcode;
780  *         unsigned char   UpdateCount;
781  *         uint32_t   Periodicity;
782  *         unsigned char   ServerName[];
783  *         unsigned char   VersionMajor;
784  *         unsigned char   VersionMinor;
785  *         uint32_t   Type;
786  *         uint32_t   Signature;
787  *         unsigned char   Comment[];
788  *     }
789  *
790  * where:
791  *      Opcode - Identifies this structure as a browser server
792  *          announcement and is defined as HostAnnouncement with a
793  *          value of decimal 1.
794  *
795  *      UpdateCount - must be sent as zero and ignored on receipt.
796  *
797  *      Periodicity - The announcement frequency of the server (in
798  *          seconds). The server will be removed from the browse list
799  *          if it has not been heard from in 3X its announcement
800  *          frequency. In no case will the server be removed from the
801  *          browse list before the period 3X has elapsed. Actual
802  *          implementations may take more than 3X to actually remove
803  *          the server from the browse list.
804  *
805  *      ServerName - Null terminated ASCII server name (up to 16 bytes
806  *          in length).
807  *
808  *      VersionMajor - The major version number of the OS the server
809  *          is running. it will be returned by NetServerEnum2.
810  *
811  *      VersionMinor - The minor version number of the OS the server
812  *          is running. This is entirely informational and does not
813  *          have any significance for the browsing protocol.
814  *
815  *      Type - Specifies the type of the server. The server type bits
816  *          are specified in the NetServerEnum2 section.
817  *
818  *      Signature -  The browser protocol minor version number in the
819  *          low 8 bits, the browser protocol major version number in
820  *          the next higher 8 bits and the signature 0xaa55 in the
821  *          high 16 bits of this field. Thus, for this version of the
822  *          browser protocol (1.15) this field has the value
823  *          0xaa55010f. This may used to isolate browser servers that
824  *          are running out of revision browser software; otherwise,
825  *          it is ignored.
826  *
827  *      Comment - Null terminated ASCII comment for the server.
828  *          Limited to 43 bytes.
829  *
830  * When a non-browser server starts up, it announces itself in the manner
831  * described once every minute. The frequency of these statements is
832  * gradually stretched to once every 12 minutes.
833  *
834  * Note: older non-browser servers in a domain "D" sent HostAnnouncement
835  * frames to the NETBIOS group name D(00). Non-Browser servers supporting
836  * version 1.15 of the browsing protocol SHOULD NOT use this NETBIOS name,
837  * but for backwards compatibility Master Browsers MAY receive and process
838  * HostAnnouncement frames on this name as described above for D(1d).
839  */
840 
841 void
842 smb_browser_send_HostAnnouncement(int net, int32_t next_announcement,
843     struct addr_entry *addr, char suffix)
844 {
845 	smb_msgbuf_t mb;
846 	int offset, announce_len, data_length;
847 	struct name_entry dest_name;
848 	struct name_entry server_name;
849 	struct browser_netinfo *subnet;
850 	server_info_t *server;
851 	unsigned char *buffer;
852 	uint32_t type;
853 	char resource_domain[SMB_PI_MAX_DOMAIN];
854 
855 	syslog(LOG_DEBUG, "smb_browse: send_HostAnnouncement(%d)", net);
856 
857 	smb_config_rdlock();
858 	(void) strlcpy(resource_domain,
859 	    smb_config_getstr(SMB_CI_DOMAIN_NAME), SMB_PI_MAX_DOMAIN);
860 	(void) utf8_strupr(resource_domain);
861 	smb_config_unlock();
862 
863 	if (addr == 0) {
864 		/* Local master Browser */
865 		smb_init_name_struct(
866 		    (unsigned char *)resource_domain, suffix,
867 		    0, 0, 0, 0, 0, &dest_name);
868 		if (smb_browser_bcast_addr_of_subnet(0, net, &dest_name) < 0)
869 			return;
870 	} else {
871 		smb_init_name_struct(
872 		    (unsigned char *)resource_domain, suffix,
873 		    0, 0, 0, 0, 0, &dest_name);
874 		dest_name.addr_list = *addr;
875 		dest_name.addr_list.forw = dest_name.addr_list.back =
876 		    &dest_name.addr_list;
877 	}
878 
879 	/* give some extra room */
880 	buffer = (unsigned char *)malloc(MAX_DATAGRAM_LENGTH * 2);
881 	if (buffer == 0) {
882 		syslog(LOG_ERR, "HostAnnouncement: resource shortage");
883 		return;
884 	}
885 
886 	subnet = smb_browser_getnet(net);
887 	if (subnet == 0) {
888 		free(buffer);
889 		return;
890 	}
891 
892 	server = &subnet->server;
893 
894 	data_length = 1 + 1 + 4 + 16 + 1 + 1 + 4 + 4 +
895 	    strlen(server->comment) + 1;
896 
897 	if ((offset = smb_browser_load_transact_header(buffer,
898 	    MAX_DATAGRAM_LENGTH, data_length, ONE_WAY_TRANSACTION,
899 	    browse)) < 0) {
900 
901 		smb_browser_putnet(subnet);
902 		free(buffer);
903 		return;
904 	}
905 
906 	/*
907 	 * A non-browser server SHOULD send a HostAnnouncement browser frame
908 	 * specifying a type of 0 just prior to shutting down, to allow it to
909 	 * quickly be removed from the list of available servers.
910 	 */
911 	type = (nb_status.state & NETBIOS_SHUTTING_DOWN) ? 0 : server->type;
912 
913 	smb_msgbuf_init(&mb, buffer + offset, MAX_DATAGRAM_LENGTH - offset, 0);
914 	announce_len = smb_msgbuf_encode(&mb, "bbl16cbblls",
915 	    (char)HOST_ANNOUNCEMENT,	/* Announcement opcode */
916 	    (char)++subnet->server.update_count,
917 	    next_announcement * 60000,	/* Periodicity in MilliSeconds */
918 	    server->hostname, /* Server name */
919 	    server->major,	/* our major version */
920 	    server->minor,	/* our minor version */
921 	    type,		/* server type  */
922 	    server->signature,	/* Signature */
923 	    server->comment);	/* Let 'em know */
924 
925 	server_name = server->name;
926 	smb_browser_putnet(subnet);
927 
928 	if (announce_len > 0)
929 		(void) smb_netbios_datagram_send(&server_name, &dest_name,
930 		    buffer, offset + announce_len);
931 
932 	free(buffer);
933 	smb_msgbuf_term(&mb);
934 }
935 
936 void
937 smb_browser_process_AnnouncementRequest(struct datagram *datagram,
938     char *mailbox)
939 {
940 	struct browser_netinfo *subnet;
941 	unsigned int next_announcement;
942 	uint32_t delay = random() % 29; /* in seconds */
943 	int	net;
944 
945 	if (strcmp(mailbox, lanman) != 0) {
946 		syslog(LOG_DEBUG, "smb_browse: Wrong Mailbox (%s)", mailbox);
947 		return;
948 	}
949 
950 	net = smb_net_id(datagram->src.addr_list.sin.sin_addr.s_addr);
951 	if (net < 0) {
952 		/* We don't know who this is so ignore it... */
953 		return;
954 	}
955 
956 	(void) sleep(delay);
957 
958 	subnet = smb_browser_getnet(net);
959 	if (subnet) {
960 		next_announcement = subnet->next_announce * 60 * 1000;
961 		smb_browser_putnet(subnet);
962 		smb_browser_send_HostAnnouncement(net, next_announcement,
963 		    &datagram->src.addr_list, 0x1D);
964 	}
965 }
966 
967 void *
968 smb_browser_dispatch(void *arg)
969 {
970 	struct datagram *datagram = (struct datagram *)arg;
971 	smb_msgbuf_t 	mb;
972 	int		rc;
973 	unsigned char	command;
974 	unsigned char	parameter_words;
975 	unsigned short	total_parameter_words;
976 	unsigned short	total_data_count;
977 	unsigned short	max_parameters_to_return;
978 	unsigned short	max_data_to_return;
979 	unsigned char	max_setup_bytes_to_return;
980 	unsigned short	reply;
981 	unsigned short	parameter_bytes_sent;
982 	unsigned short	parameter_offset;
983 	unsigned short	data_bytes_sent;
984 	unsigned short	data_offset;
985 	unsigned char	setup_word_count;
986 	unsigned short	setup_word_0;
987 	unsigned short	setup_word_1;
988 	unsigned short	setup_word_2;
989 	unsigned short	total_request_bytes;
990 	char 		*mailbox;
991 	unsigned char	message_type;
992 	unsigned char 	*data;
993 	int		datalen;
994 
995 	syslog(LOG_DEBUG, "smb_browse: packet_received");
996 
997 	smb_msgbuf_init(&mb, datagram->data, datagram->data_length, 0);
998 	rc = smb_msgbuf_decode(&mb, "Mb27.bwwwwb.w6.wwwwb.wwwws",
999 	    &command,			/* Command */
1000 	    &parameter_words,		/* Count of parameter words */
1001 	    &total_parameter_words,	/* Total Parameter words sent */
1002 	    &total_data_count,		/* Total Data bytes sent */
1003 	    &max_parameters_to_return,	/* Max Parameters to return */
1004 	    &max_data_to_return,	/* Max data bytes to return */
1005 	    &max_setup_bytes_to_return,	/* Max setup bytes to return */
1006 	    &reply,			/* No reply */
1007 	    &parameter_bytes_sent,	/* Parameter bytes sent */
1008 	    &parameter_offset,		/* Parameter offset */
1009 	    &data_bytes_sent,		/* Data bytes sent */
1010 	    &data_offset,		/* Data offset */
1011 	    &setup_word_count,		/* Setup word count */
1012 	    &setup_word_0,		/* Setup word[0] */
1013 	    &setup_word_1,		/* Setup word[1] */
1014 	    &setup_word_2,		/* Setup word[2] */
1015 	    &total_request_bytes,	/* Total request bytes */
1016 	    &mailbox);			/* Mailbox address */
1017 
1018 	if (rc < 0) {
1019 		syslog(LOG_ERR, "smb_browser_dispatch: decode error");
1020 		smb_msgbuf_term(&mb);
1021 		free(datagram);
1022 		return (0);
1023 	}
1024 
1025 	data = &datagram->data[data_offset];
1026 	datalen = datagram->data_length - data_offset;
1027 
1028 	/*
1029 	 * The PDC location protocol, i.e. anything on the \\NET
1030 	 * mailslot, is handled by the smb_netlogon module.
1031 	 */
1032 	if (strncasecmp("\\MAILSLOT\\NET\\", mailbox, 14) == 0) {
1033 		smb_netlogon_receive(datagram, mailbox, data, datalen);
1034 		smb_msgbuf_term(&mb);
1035 		free(datagram);
1036 		return (0);
1037 	}
1038 
1039 	/*
1040 	 * If it's not a netlogon message, assume it's a browser request.
1041 	 * This is not the most elegant way to extract the command byte
1042 	 * but at least we no longer use it to get the netlogon opcode.
1043 	 */
1044 	message_type = datagram->data[data_offset];
1045 
1046 	switch (message_type) {
1047 	case ANNOUNCEMENT_REQUEST :
1048 		smb_browser_process_AnnouncementRequest(datagram, mailbox);
1049 		break;
1050 
1051 	default:
1052 		syslog(LOG_DEBUG, "smb_browse: invalid message_type(%d, %x)",
1053 		    message_type, message_type);
1054 		break;
1055 	}
1056 
1057 	smb_msgbuf_term(&mb);
1058 	free(datagram);
1059 	return (0);
1060 }
1061 
1062 
1063 /*
1064  * 11.1 Registered unique names
1065  *
1066  *  <COMPUTER>(00)
1067  *     This name is used by all servers and clients to receive second
1068  *     class mailslot messages. A system must add this name in order to
1069  *     receive mailslot messages. The only browser requests that should
1070  *     appear on this name are BecomeBackup, GetBackupListResp,
1071  *     MasterAnnouncement, and LocalMasterAnnouncement frames. All other
1072  *     datagrams (other than the expected non-browser datagrams) may be
1073  *     ignored and an error logged.
1074  *
1075  *   <DOMAIN>(1d)
1076  *     This name is used to identify a master browser server for domain
1077  *     "DOMAIN" on a subnet.  A master browser server adds this name as a
1078  *     unique NETBIOS name when it becomes master browser. If the attempt
1079  *     to add the name fails, the master browser server assumes that there
1080  *     is another master in the domain and will fail to come up. It may
1081  *     log an error if the failure occurs more than 3 times in a row (this
1082  *     either indicates some form of network misconfiguration or a
1083  *     software error). The only requests that should appear on this name
1084  *     are GetBackupListRequest and HostAnnouncement requests. All other
1085  *     datagrams on this name may be ignored (and an error logged). If
1086  *     running a NETBIOS name service (NBNS, such as WINS), this name
1087  *     should not be registered with the NBNS.
1088  *
1089  *   <DOMAIN>(1b)
1090  *     This name is used to identify the Domain Master Browser for domain
1091  *     "DOMAIN" (which is also the primary domain controller). It is a
1092  *     unique name added only by the primary domain controller. The
1093  *     primary domain controller will respond to GetBackupListRequest on
1094  *     this name just as it responds to these requests on the <DOMAIN>(1d)
1095  *     name.
1096  *
1097  * 11.2 Registered group names
1098  *
1099  *   (01)(02)__MSBROWSE__(02)(01)
1100  *     This name is used by Master Browsers to announce themselves to the
1101  *     other Master Browsers on a subnet. It is added as a group name by
1102  *     all Master Browser servers. The only broadcasts that should appear
1103  *     on this name is DomainAnnouncement requests. All other datagrams
1104  *     can be ignored.
1105  *
1106  *   <DOMAIN>(00)
1107  *     This name is used by clients and servers in domain "DOMAIN" to
1108  *     process server announcements. The only requests that should appear
1109  *     on this name that the browser is interested in are
1110  *     AnnouncementRequest and NETLOGON_QUERY (to locate the PDC) packets.
1111  *     All other unidentifiable requests may be ignored (and an error
1112  *     logged).
1113  *
1114  *   <DOMAIN>(1E)
1115  *     This name is used for announcements to browsers for domain "DOMAIN"
1116  *     on a subnet. This name is registered by all the browser servers in
1117  *     the domain. The only requests that should appear on this name are
1118  *     RequestElection and AnnouncementRequest packets. All other
1119  *     datagrams may be ignored (and an error logged).
1120  *
1121  *   <DOMAIN>(1C)
1122  *     This name is registered by Primary Domain Controllers.
1123  */
1124 
1125 void
1126 smb_browser_config(void)
1127 {
1128 	struct name_entry	name;
1129 	struct name_entry	master;
1130 	struct name_entry	dest;
1131 	struct name_entry	*entry;
1132 	int smb_nc_cnt;
1133 	net_cfg_t cfg;
1134 	int net;
1135 	char resource_domain[SMB_PI_MAX_DOMAIN];
1136 
1137 	syslog(LOG_DEBUG, "smb_browse: reconfigure");
1138 
1139 	smb_browser_init();
1140 
1141 	smb_config_rdlock();
1142 	(void) strlcpy(resource_domain,
1143 	    smb_config_getstr(SMB_CI_DOMAIN_NAME), SMB_PI_MAX_DOMAIN);
1144 	(void) utf8_strupr(resource_domain);
1145 	smb_config_unlock();
1146 
1147 	/* domain<00> */
1148 	smb_init_name_struct((unsigned char *)resource_domain, 0x00,
1149 	    0, 0, 0, 0, 0, &name);
1150 	entry = smb_name_find_name(&name);
1151 	smb_name_unlock_name(entry);
1152 
1153 	smb_nc_cnt = smb_nic_get_num();
1154 	for (net = 0; net < smb_nc_cnt; net++) {
1155 		if (smb_nic_get_byind(net, &cfg) == NULL)
1156 			break;
1157 		if (cfg.exclude)
1158 			continue;
1159 		smb_init_name_struct(
1160 		    (unsigned char *)resource_domain, 0x00, 0,
1161 		    cfg.ip, htons(DGM_SRVC_UDP_PORT),
1162 		    NAME_ATTR_GROUP, NAME_ATTR_LOCAL, &name);
1163 		(void) smb_name_add_name(&name);
1164 	}
1165 
1166 	/* All our local master browsers */
1167 	smb_init_name_struct((unsigned char *)resource_domain, 0x1D,
1168 	    0, 0, 0, 0, 0, &dest);
1169 	entry = smb_name_find_name(&dest);
1170 
1171 	if (entry) {
1172 		for (net = 0; net < smb_nc_cnt; net++) {
1173 			if (smb_browser_addr_of_subnet(entry, net, &master)
1174 			    == 0) {
1175 				syslog(LOG_DEBUG,
1176 				    "smbd: Master browser found at %s",
1177 				    inet_ntoa(master.addr_list.sin.sin_addr));
1178 			}
1179 		}
1180 		smb_name_unlock_name(entry);
1181 	}
1182 	smb_init_name_struct((unsigned char *)resource_domain,
1183 	    0x1B, 0, 0, 0, 0, 0, &dest);
1184 
1185 	if ((entry = smb_name_find_name(&dest)) != 0) {
1186 		syslog(LOG_DEBUG, "smbd: Domain Master browser for %s is %s",
1187 		    resource_domain,
1188 		    inet_ntoa(entry->addr_list.sin.sin_addr));
1189 		smb_name_unlock_name(entry);
1190 	}
1191 }
1192 
1193 static void
1194 smb_browser_init()
1195 {
1196 	struct browser_netinfo *subnet;
1197 	struct server_info *server;
1198 	char cmnt[SMB_PI_MAX_COMMENT], hostname[MAXHOSTNAMELEN];
1199 	int i, j;
1200 	int smb_nc_cnt;
1201 	net_cfg_t cfg;
1202 
1203 	(void) smb_gethostname(hostname, MAXHOSTNAMELEN, 1);
1204 
1205 	smb_config_rdlock();
1206 	(void) strlcpy(cmnt, smb_config_getstr(SMB_CI_SYS_CMNT),
1207 	    sizeof (cmnt));
1208 	smb_config_unlock();
1209 
1210 	smb_nc_cnt = smb_nic_get_num();
1211 	for (i = 0; i < smb_nc_cnt; i++) {
1212 		if (smb_nic_get_byind(i, &cfg) == NULL)
1213 			break;
1214 		if (cfg.exclude)
1215 			continue;
1216 
1217 		subnet = &smb_browser_info[i];
1218 		(void) mutex_lock(&subnet->mtx);
1219 
1220 		/* One Minute announcements for first five */
1221 		subnet->flags = BROWSER_NF_VALID;
1222 		subnet->next_announce = 1;
1223 		subnet->interval = 1;
1224 		subnet->reps = 5;
1225 
1226 		server = &subnet->server;
1227 		bzero(server, sizeof (struct server_info));
1228 
1229 		server->type = MY_SERVER_TYPE;
1230 		server->major = SMB_VERSION_MAJOR;
1231 		server->minor = SMB_VERSION_MINOR;
1232 		server->signature = SMB_SERVER_SIGNATURE;
1233 		(void) strlcpy(server->comment, cmnt, SMB_PI_MAX_COMMENT);
1234 
1235 		(void) snprintf(server->hostname, NETBIOS_NAME_SZ, "%.15s",
1236 		    hostname);
1237 
1238 		/*
1239 		 * 00 is workstation service.
1240 		 * 20 is file server service.
1241 		 */
1242 		smb_init_name_struct((unsigned char *)server->hostname, 0x20, 0,
1243 		    cfg.ip, htons(DGM_SRVC_UDP_PORT),
1244 		    NAME_ATTR_UNIQUE, NAME_ATTR_LOCAL, &server->name);
1245 
1246 		(void) mutex_unlock(&subnet->mtx);
1247 	}
1248 
1249 	/* Invalidate unconfigured NICs */
1250 	for (j = i; j < SMB_PI_MAX_NETWORKS; j++) {
1251 		subnet = &smb_browser_info[j];
1252 		(void) mutex_lock(&subnet->mtx);
1253 		subnet->flags = BROWSER_NF_INVALID;
1254 		(void) mutex_unlock(&subnet->mtx);
1255 	}
1256 }
1257 
1258 /*
1259  * smb_browser_non_master_duties
1260  *
1261  * To advertise its presence, i.e. to publish itself as being available, a
1262  * non-browser server sends a HostAnnouncement browser frame. If the server
1263  * is a member of domain "D", this frame is sent to the NETBIOS unique name
1264  * D(1d) and mailslot "\\MAILSLOT\\BROWSE".
1265  */
1266 void
1267 smb_browser_non_master_duties(int net)
1268 {
1269 	struct browser_netinfo *subnet;
1270 	struct name_entry name;
1271 	struct name_entry *dest;
1272 	struct addr_entry addr;
1273 	int interval;
1274 	char resource_domain[SMB_PI_MAX_DOMAIN];
1275 
1276 	subnet = smb_browser_getnet(net);
1277 	if (subnet == 0)
1278 		return;
1279 
1280 	interval = subnet->interval;
1281 	smb_browser_putnet(subnet);
1282 
1283 	smb_browser_send_HostAnnouncement(net, interval, 0, 0x1D);
1284 
1285 	smb_config_rdlock();
1286 	(void) strlcpy(resource_domain,
1287 	    smb_config_getstr(SMB_CI_DOMAIN_NAME), SMB_PI_MAX_DOMAIN);
1288 	(void) utf8_strupr(resource_domain);
1289 	smb_config_unlock();
1290 
1291 	smb_init_name_struct((unsigned char *)resource_domain, 0x1D,
1292 	    0, 0, 0, 0, 0, &name);
1293 
1294 	if ((dest = smb_name_find_name(&name))) {
1295 		addr = dest->addr_list;
1296 		addr.forw = addr.back = &addr;
1297 		smb_name_unlock_name(dest);
1298 		smb_browser_send_HostAnnouncement(net, interval, &addr, 0x1D);
1299 	} else {
1300 		smb_init_name_struct(
1301 		    (unsigned char *)resource_domain, 0x1B,
1302 		    0, 0, 0, 0, 0, &name);
1303 		if ((dest = smb_name_find_name(&name))) {
1304 			addr = dest->addr_list;
1305 			addr.forw = addr.back = &addr;
1306 			smb_name_unlock_name(dest);
1307 			smb_browser_send_HostAnnouncement(net, interval,
1308 			    &addr, 0x1B);
1309 		}
1310 	}
1311 
1312 	subnet = smb_browser_getnet(net);
1313 	/*
1314 	 * One Minute announcements for first five
1315 	 * minutes, one munute longer each round
1316 	 * until 12 minutes and every 12 minutes
1317 	 * thereafter.
1318 	 */
1319 	if (--subnet->reps == 0) {
1320 		if (subnet->interval < 12)
1321 			subnet->interval++;
1322 
1323 		subnet->reps = 1;
1324 	}
1325 
1326 	subnet->next_announce = subnet->interval;
1327 	smb_browser_putnet(subnet);
1328 }
1329 
1330 
1331 /*
1332  * browser_sleep
1333  *
1334  * Put browser in 1 minute sleep if netbios services are not
1335  * shutting down and both name and datagram services are still
1336  * running. It'll wake up after 1 minute or if one of the above
1337  * conditions go false. It checks the conditions again and return
1338  * 1 if everything is ok or 0 if browser shouldn't continue
1339  * running.
1340  */
1341 static int
1342 browser_sleep()
1343 {
1344 	int slept = 0;
1345 	timestruc_t to;
1346 
1347 	(void) mutex_lock(&nb_status.mtx);
1348 	while (((nb_status.state & NETBIOS_SHUTTING_DOWN) == 0) &&
1349 	    (nb_status.state & NETBIOS_NAME_SVC_RUNNING) &&
1350 	    (nb_status.state & NETBIOS_DATAGRAM_SVC_RUNNING)) {
1351 
1352 		if (slept) {
1353 			(void) mutex_unlock(&nb_status.mtx);
1354 			return (1);
1355 		}
1356 
1357 		to.tv_sec = 60;  /* 1 minute */
1358 		to.tv_nsec = 0;
1359 		(void) cond_reltimedwait(&nb_status.cv, &nb_status.mtx, &to);
1360 		slept = 1;
1361 	}
1362 	(void) mutex_unlock(&nb_status.mtx);
1363 
1364 	return (0);
1365 }
1366 
1367 /*
1368  * smb_browser_start
1369  *
1370  * Smb Netbios browser daemon.
1371  */
1372 /*ARGSUSED*/
1373 void *
1374 smb_browser_daemon(void *arg)
1375 {
1376 	int	net;
1377 	int next_announce;
1378 	struct browser_netinfo *subnet;
1379 	int run = 1;
1380 	int smb_nc_cnt;
1381 	net_cfg_t cfg;
1382 
1383 	smb_browser_config();
1384 
1385 	nb_status.state |= NETBIOS_BROWSER_RUNNING;
1386 
1387 	while (run) {
1388 		smb_nc_cnt = smb_nic_get_num();
1389 		for (net = 0; net < smb_nc_cnt; net++) {
1390 			if (smb_nic_get_byind(net, &cfg) == NULL)
1391 				break;
1392 			if (cfg.exclude)
1393 				continue;
1394 
1395 			subnet = smb_browser_getnet(net);
1396 			next_announce = --subnet->next_announce;
1397 			smb_browser_putnet(subnet);
1398 
1399 			if (next_announce > 0 || cfg.broadcast == 0)
1400 				continue;
1401 
1402 			smb_browser_non_master_duties(net);
1403 		}
1404 
1405 		run = browser_sleep();
1406 	}
1407 
1408 	smb_netbios_chg_status(NETBIOS_BROWSER_RUNNING, 0);
1409 	return (0);
1410 }
1411