Implemented asynchronous DNS lookups in nmbd.
[samba.git] / source3 / include / nameserv.h
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios header - version 2
5    Copyright (C) Andrew Tridgell 1994-1997
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21 */
22
23 #define GET_TTL(ttl) ((ttl)?MIN(ttl,lp_max_ttl()):lp_max_ttl())
24
25 /* NTAS uses 2, NT uses 1, WfWg uses 0 */
26 #define MAINTAIN_LIST    2
27 #define ELECTION_VERSION 1
28
29 #define MAX_DGRAM_SIZE (576) /* tcp/ip datagram limit is 576 bytes */
30 #define MIN_DGRAM_SIZE 12
31
32 #define NMB_QUERY  0x20
33 #define NMB_STATUS 0x21
34
35 #define NMB_REG         0x05 /* see rfc1002.txt 4.2.2,3,5,6,7,8 */
36 #define NMB_REG_REFRESH 0x09 /* see rfc1002.txt 4.2.4 */
37 #define NMB_REL         0x06 /* see rfc1002.txt 4.2.9,10,11 */
38 #define NMB_WAIT_ACK    0x07 /* see rfc1002.txt 4.2.16 */
39 /* XXXX what about all the other types?? 0x1, 0x2, 0x3, 0x4, 0x8? */
40
41 #define FIND_SELF  0x01
42 #define FIND_WINS  0x02
43 #define FIND_LOCAL 0x04
44
45 /* NetBIOS flags */
46 #define NB_GROUP  0x80
47 #define NB_PERM   0x02
48 #define NB_ACTIVE 0x04
49 #define NB_CONFL  0x08
50 #define NB_DEREG  0x10
51 #define NB_BFLAG  0x00 /* broadcast node type */
52 #define NB_PFLAG  0x20 /* point-to-point node type */
53 #define NB_MFLAG  0x40 /* mixed bcast & p-p node type */
54 #define NB_HFLAG  0x60 /* microsoft 'hybrid' node type */
55 #define NB_FLGMSK 0x60
56
57 #define REFRESH_TIME (15*60)
58 #define NAME_POLL_REFRESH_TIME (5*60)
59 #define NAME_POLL_INTERVAL 15
60
61 /* NetBIOS flag identifier */
62 #define NAME_PERMANENT(p) ((p) & NB_PERM)
63 #define NAME_ACTIVE(p)    ((p) & NB_ACTIVE)
64 #define NAME_CONFLICT(p)  ((p) & NB_CONFL)
65 #define NAME_DEREG(p)     ((p) & NB_DEREG)
66 #define NAME_GROUP(p)     ((p) & NB_GROUP)
67
68 #define NAME_BFLAG(p)     (((p) & NB_FLGMSK) == NB_BFLAG)
69 #define NAME_PFLAG(p)     (((p) & NB_FLGMSK) == NB_PFLAG)
70 #define NAME_MFLAG(p)     (((p) & NB_FLGMSK) == NB_MFLAG)
71 #define NAME_HFLAG(p)     (((p) & NB_FLGMSK) == NB_HFLAG)
72
73 /* server type identifiers */
74 #define AM_MASTER(work) (work->ServerType & SV_TYPE_MASTER_BROWSER)
75 #define AM_BACKUP(work) (work->ServerType & SV_TYPE_BACKUP_BROWSER)
76 #define AM_DOMMST(work) (work->ServerType & SV_TYPE_DOMAIN_MASTER)
77 #define AM_DOMMEM(work) (work->ServerType & SV_TYPE_DOMAIN_MEMBER)
78
79 /* microsoft browser NetBIOS name */
80 #define MSBROWSE "\001\002__MSBROWSE__\002"
81
82 /* mail slots */
83 #define BROWSE_MAILSLOT    "\\MAILSLOT\\BROWSE"
84 #define NET_LOGON_MAILSLOT "\\MAILSLOT\\NET\\NETLOGON"
85 #define NT_LOGON_MAILSLOT  "\\MAILSLOT\\NET\\NTLOGON"
86
87 enum name_source {STATUS_QUERY, LMHOSTS, REGISTER, SELF, DNS, DNSFAIL};
88 enum node_type {B_NODE=0, P_NODE=1, M_NODE=2, NBDD_NODE=3};
89 enum packet_type {NMB_PACKET, DGRAM_PACKET};
90
91 enum master_state
92 {
93    MST_POTENTIAL,
94    MST_BACK,
95    MST_MSB,
96    MST_BROWSER
97 };
98
99 enum domain_state
100 {
101    DOMAIN_NONE,
102    DOMAIN_WAIT,
103    DOMAIN_MST
104 };
105
106 enum logon_state
107 {
108    LOGON_NONE,
109    LOGON_WAIT,
110    LOGON_SRV
111 };
112
113 enum state_type
114 {
115         NAME_STATUS_DOM_SRV_CHK,
116         NAME_STATUS_SRV_CHK,
117         NAME_REGISTER_CHALLENGE,
118         NAME_REGISTER,
119         NAME_RELEASE,
120         NAME_QUERY_CONFIRM,
121         NAME_QUERY_SYNC_LOCAL,
122         NAME_QUERY_SYNC_REMOTE,
123         NAME_QUERY_DOM_SRV_CHK,
124         NAME_QUERY_SRV_CHK,
125         NAME_QUERY_FIND_MST,
126         NAME_QUERY_MST_CHK,
127         NAME_QUERY_DOMAIN
128 };
129
130 /* a netbios name structure */
131 struct nmb_name {
132   char name[17];
133   char scope[64];
134   int name_type;
135 };
136
137 /* a netbios flags + ip address structure */
138 /* this is used for multi-homed systems and for internet group names */
139 struct nmb_ip
140 {
141   struct in_addr ip; /* ip address of host that owns this name */
142   uint16 nb_flags;      /* netbios flags */
143 };
144
145 /* this is the structure used for the local netbios name list */
146 struct name_record
147 {
148   struct name_record *next;
149   struct name_record *prev;
150
151   struct nmb_name name;    /* the netbios name */
152   struct nmb_ip *ip_flgs;  /* the ip + flags */
153   int num_ips;             /* number of ip+flags entries */
154
155   enum name_source source; /* where the name came from */
156
157   time_t death_time; /* time record must be removed (do not remove if 0) */
158   time_t refresh_time; /* time record should be refreshed */
159 };
160
161 struct subnet_record;
162
163 /* browse and backup server cache for synchronising browse list */
164 struct browse_cache_record
165 {
166         struct browse_cache_record *next;
167         struct browse_cache_record *prev;
168
169         pstring name;
170         int type;
171         pstring group;
172         struct in_addr ip;
173         time_t sync_time;
174         BOOL synced;
175         BOOL local;
176         struct subnet_record *subnet;
177 };
178
179 /* this is used to hold the list of servers in my domain, and is */
180 /* contained within lists of domains */
181 struct server_record
182 {
183   struct server_record *next;
184   struct server_record *prev;
185
186   struct server_info_struct serv;
187   time_t death_time;  
188 };
189
190 /* a workgroup structure. it contains a list of servers */
191 struct work_record
192 {
193   struct work_record *next;
194   struct work_record *prev;
195
196   struct server_record *serverlist;
197
198   /* stage of development from non-local-master up to local-master browser */
199   enum master_state mst_state;
200
201   /* stage of development from non-domain-master to domain master browser */
202   enum domain_state dom_state;
203
204   /* stage of development from non-logon-server to logon server */
205   enum logon_state log_state;
206
207   /* work group info */
208   fstring work_group;
209   int     token;        /* used when communicating with backup browsers */
210   int     ServerType;
211
212   /* announce info */
213   time_t lastannounce_time;
214   int announce_interval;
215   BOOL    needannounce;
216
217
218   /* election info */
219   BOOL    RunningElection;
220   BOOL    needelection;
221   int     ElectionCount;
222   uint32  ElectionCriterion;
223 };
224
225 /* initiated name queries recorded in this list to track any responses... */
226 /* sadly, we need to group everything together. i suppose that if this
227    gets unwieldy, then a union ought to be considered. oh for c++... */
228 struct response_record
229 {
230   struct response_record *next;
231   struct response_record *prev;
232
233   uint16 response_id;
234   enum state_type state;
235
236   int fd;
237   int quest_type;
238   struct nmb_name name;
239   int nb_flags;
240   time_t ttl;
241
242   int server_type;
243   fstring my_name;
244   fstring my_comment;
245
246   BOOL bcast;
247   BOOL recurse;
248   struct in_addr send_ip;
249   struct in_addr reply_to_ip;
250   int reply_id;
251
252   int num_msgs;
253
254   time_t repeat_time;
255   time_t repeat_interval;
256   int    repeat_count;
257 };
258
259 /* a subnet structure. it contains a list of workgroups and netbios names*/
260
261 /* note that a subnet of 255.255.255.255 contains all the WINS netbios names.
262    all communication from such nodes are on a non-broadcast basis: they
263    are point-to-point (P nodes) or mixed point-to-point and broadcast
264    (M nodes). M nodes use point-to-point as a preference, and will use
265    broadcasting for certain activities, or will resort to broadcasting as a
266    last resort, if the WINS server fails (users of wfwg will notice that their
267    machine often freezes for 30 seconds at a time intermittently, if the WINS
268    server is down).
269
270    B nodes will have their own, totally separate subnet record, with their
271    own netbios name set. these do NOT interact with other subnet records'
272    netbios names, INCLUDING the WINS one (with an ip "address", so called,
273    of 255.255.255.255)
274
275    there is a separate response list for each subnet record. in the case of
276    the 255.255.255.255 subnet record (WINS), the WINS server will be able to
277    use this to poll (infrequently!) each of its entries, to ensure that the
278    names are still in use.
279    XXXX this polling is a planned feature for a really over-cautious WINS server 
280 */
281
282 struct subnet_record
283 {
284   struct subnet_record *next;
285   struct subnet_record *prev;
286
287   struct work_record *workgrouplist; /* list of workgroups */
288   struct name_record *namelist;      /* list of netbios names */
289   struct response_record *responselist; /* list of responses expected */
290
291   struct in_addr bcast_ip;
292   struct in_addr mask_ip;
293   struct in_addr myip;
294   int nmb_sock;               /* socket to listen for unicast 137. */
295   int dgram_sock;             /* socket to listen for unicast 138. */
296 };
297
298 /* a resource record */
299 struct res_rec {
300   struct nmb_name rr_name;
301   int rr_type;
302   int rr_class;
303   int ttl;
304   int rdlength;
305   char rdata[MAX_DGRAM_SIZE];
306 };
307
308 /* define a nmb packet. */
309 struct nmb_packet
310 {
311   struct {
312     int name_trn_id;
313     int opcode;
314     BOOL response;
315     struct {
316       BOOL bcast;
317       BOOL recursion_available;
318       BOOL recursion_desired;
319       BOOL trunc;
320       BOOL authoritative;
321     } nm_flags;
322     int rcode;
323     int qdcount;
324     int ancount;
325     int nscount;
326     int arcount;
327   } header;
328
329   struct {
330     struct nmb_name question_name;
331     int question_type;
332     int question_class;
333   } question;
334
335   struct res_rec *answers;
336   struct res_rec *nsrecs;
337   struct res_rec *additional;
338 };
339
340
341 /* a datagram - this normally contains SMB data in the data[] array */
342 struct dgram_packet {
343   struct {
344     int msg_type;
345     struct {
346       enum node_type node_type;
347       BOOL first;
348       BOOL more;
349     } flags;
350     int dgm_id;
351     struct in_addr source_ip;
352     int source_port;
353     int dgm_length;
354     int packet_offset;
355   } header;
356   struct nmb_name source_name;
357   struct nmb_name dest_name;
358   int datasize;
359   char data[MAX_DGRAM_SIZE];
360 };
361
362 /* define a structure used to queue packets. this will be a linked
363  list of nmb packets */
364 struct packet_struct
365 {
366         struct packet_struct *next;
367         struct packet_struct *prev;
368         BOOL locked;
369         struct in_addr ip;
370         int port;
371         int fd;
372         time_t timestamp;
373         enum packet_type packet_type;
374         union {
375                 struct nmb_packet nmb;
376                 struct dgram_packet dgram;
377         } packet;
378 };
379
380 /* NETLOGON opcodes */
381 #define QUERYFORPDC     7 /* Query for PDC */
382 #define QUERYFORPDC_R   12 /* Response to Query for PDC */
383 #define SAMLOGON        18
384 #define SAMLOGON_R      19
385
386
387 /* ids for netbios packet types */
388 #define ANN_HostAnnouncement         1
389 #define ANN_AnnouncementRequest      2
390 #define ANN_Election                 8
391 #define ANN_GetBackupListReq         9
392 #define ANN_GetBackupListResp       10
393 #define ANN_BecomeBackup            11
394 #define ANN_DomainAnnouncement      12
395 #define ANN_MasterAnnouncement      13
396 #define ANN_ResetBrowserState       14
397 #define ANN_LocalMasterAnnouncement 15
398
399
400 /* broadcast packet announcement intervals, in minutes */
401
402 /* attempt to add domain logon and domain master names */
403 #define CHECK_TIME_ADD_DOM_NAMES 5 
404
405 /* search for master browsers of workgroups samba knows about, 
406    except default */
407 #define CHECK_TIME_MST_BROWSE       5 
408
409 /* request backup browser announcements from other servers */
410 #define CHECK_TIME_ANNOUNCE_BACKUP 15
411
412 /* request host announcements from other servers: min and max of interval */
413 #define CHECK_TIME_MIN_HOST_ANNCE   3
414 #define CHECK_TIME_MAX_HOST_ANNCE  12
415
416 /* announce as master to WINS server and any Primary Domain Controllers */
417 #define CHECK_TIME_MST_ANNOUNCE    15
418
419 /* do all remote announcements this often */
420 #define REMOTE_ANNOUNCE_INTERVAL 180
421
422 /* Types of machine we can announce as */
423 #define ANNOUNCE_AS_NT 1
424 #define ANNOUNCE_AS_WIN95 2
425 #define ANNOUNCE_AS_WFW 3
426
427 /* Macro's to enumerate subnets either with or without
428    the WINS subnet. */
429
430 extern struct subnet_record *subnetlist;
431 extern struct subnet_record *wins_subnet;
432
433 #define FIRST_SUBNET subnetlist
434 #define NEXT_SUBNET_EXCLUDING_WINS(x) ((x)->next)
435 #define NEXT_SUBNET_INCLUDING_WINS(x) ( ((x) == wins_subnet) ? NULL : \
436                                         (((x)->next == NULL) ? wins_subnet : \
437                                          (x)->next))
438