first public release of samba4 code
[samba.git] / source / include / nameserv.h
1 #ifndef _NAMESERV_H_
2 #define _NAMESERV_H_
3 /* 
4    Unix SMB/CIFS implementation.
5    NBT netbios header - version 2
6    Copyright (C) Andrew Tridgell 1994-1998
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21    
22 */
23
24 #define INFO_VERSION    "INFO/version"
25 #define INFO_COUNT      "INFO/num_entries"
26 #define INFO_ID_HIGH    "INFO/id_high"
27 #define INFO_ID_LOW     "INFO/id_low"
28 #define ENTRY_PREFIX    "ENTRY/"
29
30 #define PERMANENT_TTL 0
31
32 /* NTAS uses 2, NT uses 1, WfWg uses 0 */
33 #define MAINTAIN_LIST    2
34 #define ELECTION_VERSION 1
35
36 #define MAX_DGRAM_SIZE (576) /* tcp/ip datagram limit is 576 bytes */
37 #define MIN_DGRAM_SIZE 12
38
39 /*********************************************************
40  Types of reply packet.
41 **********************************************************/
42
43 enum netbios_reply_type_code { NMB_QUERY, NMB_STATUS, NMB_REG, NMB_REG_REFRESH,
44                                NMB_REL, NMB_WAIT_ACK, NMB_MULTIHOMED_REG,
45                                WINS_REG, WINS_QUERY };
46
47 /* From rfc1002, 4.2.1.2 */
48 /* Question types. */
49 #define QUESTION_TYPE_NB_QUERY  0x20
50 #define QUESTION_TYPE_NB_STATUS 0x21
51
52 /* Question class */
53 #define QUESTION_CLASS_IN  0x1
54
55 /* Opcode definitions */
56 #define NMB_NAME_QUERY_OPCODE       0x0
57 #define NMB_NAME_REG_OPCODE         0x05 /* see rfc1002.txt 4.2.2,3,5,6,7,8 */
58 #define NMB_NAME_RELEASE_OPCODE     0x06 /* see rfc1002.txt 4.2.9,10,11 */
59 #define NMB_WACK_OPCODE             0x07 /* see rfc1002.txt 4.2.16 */
60 /* Ambiguity in rfc1002 about which of these is correct. */
61 /* WinNT uses 8 by default but can be made to use 9. */
62 #define NMB_NAME_REFRESH_OPCODE_8   0x08 /* see rfc1002.txt 4.2.4 */
63 #define NMB_NAME_REFRESH_OPCODE_9   0x09 /* see rfc1002.txt 4.2.4 */
64 #define NMB_NAME_MULTIHOMED_REG_OPCODE 0x0F /* Invented by Microsoft. */
65
66 /* XXXX what about all the other types?? 0x1, 0x2, 0x3, 0x4, 0x8? */
67
68 /* Resource record types. rfc1002 4.2.1.3 */
69 #define RR_TYPE_A                  0x1
70 #define RR_TYPE_NS                 0x2
71 #define RR_TYPE_NULL               0xA
72 #define RR_TYPE_NB                0x20
73 #define RR_TYPE_NBSTAT            0x21
74
75 /* Resource record class. */
76 #define RR_CLASS_IN                0x1
77
78 /* NetBIOS flags */
79 #define NB_GROUP  0x80
80 #define NB_PERM   0x02
81 #define NB_ACTIVE 0x04
82 #define NB_CONFL  0x08
83 #define NB_DEREG  0x10
84 #define NB_BFLAG  0x00 /* Broadcast node type. */
85 #define NB_PFLAG  0x20 /* Point-to-point node type. */
86 #define NB_MFLAG  0x40 /* Mixed bcast & p-p node type. */
87 #define NB_HFLAG  0x60 /* Microsoft 'hybrid' node type. */
88 #define NB_NODETYPEMASK 0x60
89 /* Mask applied to outgoing NetBIOS flags. */
90 #define NB_FLGMSK 0xE0
91
92 /* The wins flags. Looks like the nbflags ! */
93 #define WINS_UNIQUE     0x00 /* Unique record */
94 #define WINS_NGROUP     0x01 /* Normal Group eg: 1B */
95 #define WINS_SGROUP     0x02 /* Special Group eg: 1C */
96 #define WINS_MHOMED     0x03 /* MultiHomed */
97
98 #define WINS_ACTIVE     0x00 /* active record */
99 #define WINS_RELEASED   0x04 /* released record */
100 #define WINS_TOMBSTONED 0x08 /* tombstoned record */
101 #define WINS_DELETED    0x0C /* deleted record */
102
103 #define WINS_STATE_MASK 0x0C
104
105 #define WINS_LOCAL      0x00 /* local record */
106 #define WINS_REMOTE     0x10 /* remote record */
107
108 #define WINS_BNODE      0x00 /* Broadcast node */
109 #define WINS_PNODE      0x20 /* PtP node */
110 #define WINS_MNODE      0x40 /* Mixed node */
111 #define WINS_HNODE      0x60 /* Hybrid node */
112
113 #define WINS_NONSTATIC  0x00 /* dynamic record */
114 #define WINS_STATIC     0x80 /* static record */
115
116 #define WINS_STATE_ACTIVE(p) (((p)->data.wins_flags & WINS_STATE_MASK) == WINS_ACTIVE)
117
118
119 /* NetBIOS flag identifier. */
120 #define NAME_GROUP(p)  ((p)->data.nb_flags & NB_GROUP)
121 #define NAME_BFLAG(p) (((p)->data.nb_flags & NB_NODETYPEMASK) == NB_BFLAG)
122 #define NAME_PFLAG(p) (((p)->data.nb_flags & NB_NODETYPEMASK) == NB_PFLAG)
123 #define NAME_MFLAG(p) (((p)->data.nb_flags & NB_NODETYPEMASK) == NB_MFLAG)
124 #define NAME_HFLAG(p) (((p)->data.nb_flags & NB_NODETYPEMASK) == NB_HFLAG)
125
126 /* Samba name state for a name in a namelist. */
127 #define NAME_IS_ACTIVE(p)        ((p)->data.nb_flags & NB_ACTIVE)
128 #define NAME_IN_CONFLICT(p)      ((p)->data.nb_flags & NB_CONFL)
129 #define NAME_IS_DEREGISTERING(p) ((p)->data.nb_flags & NB_DEREG)
130
131 /* Error codes for NetBIOS requests. */
132 #define FMT_ERR   0x1       /* Packet format error. */
133 #define SRV_ERR   0x2       /* Internal server error. */
134 #define NAM_ERR   0x3       /* Name does not exist. */
135 #define IMP_ERR   0x4       /* Request not implemented. */
136 #define RFS_ERR   0x5       /* Request refused. */
137 #define ACT_ERR   0x6       /* Active error - name owned by another host. */
138 #define CFT_ERR   0x7       /* Name in conflict error. */
139
140 #define REFRESH_TIME (15*60)
141 #define NAME_POLL_REFRESH_TIME (5*60)
142 #define NAME_POLL_INTERVAL 15
143
144 /* Workgroup state identifiers. */
145 #define AM_POTENTIAL_MASTER_BROWSER(work) ((work)->mst_state == MST_POTENTIAL)
146 #define AM_LOCAL_MASTER_BROWSER(work) ((work)->mst_state == MST_BROWSER)
147 #define AM_DOMAIN_MASTER_BROWSER(work) ((work)->dom_state == DOMAIN_MST)
148 #define AM_DOMAIN_MEMBER(work) ((work)->log_state == LOGON_SRV)
149
150 /* Microsoft browser NetBIOS name. */
151 #define MSBROWSE "\001\002__MSBROWSE__\002"
152
153 /* Mail slots. */
154 #define BROWSE_MAILSLOT    "\\MAILSLOT\\BROWSE"
155 #define NET_LOGON_MAILSLOT "\\MAILSLOT\\NET\\NETLOGON"
156 #define NT_LOGON_MAILSLOT  "\\MAILSLOT\\NET\\NTLOGON"
157 #define LANMAN_MAILSLOT    "\\MAILSLOT\\LANMAN"
158
159 /* Samba definitions for find_name_on_subnet(). */
160 #define FIND_ANY_NAME   0
161 #define FIND_SELF_NAME  1
162
163 /*
164  * The different name types that can be in namelists.
165  *
166  * SELF_NAME should only be on the broadcast and unicast subnets.
167  * LMHOSTS_NAME should only be in the remote_broadcast_subnet.
168  * REGISTER_NAME, DNS_NAME, DNSFAIL_NAME should only be in the wins_server_subnet.
169  * WINS_PROXY_NAME should only be on the broadcast subnets.
170  * PERMANENT_NAME can be on all subnets except remote_broadcast_subnet.
171  *
172  */
173
174 enum name_source {LMHOSTS_NAME, REGISTER_NAME, SELF_NAME, DNS_NAME, 
175                   DNSFAIL_NAME, PERMANENT_NAME, WINS_PROXY_NAME};
176 enum node_type {B_NODE=0, P_NODE=1, M_NODE=2, NBDD_NODE=3};
177 enum packet_type {NMB_PACKET, DGRAM_PACKET};
178
179 enum master_state
180 {
181   MST_NONE,
182   MST_POTENTIAL,
183   MST_BACKUP,
184   MST_MSB,
185   MST_BROWSER,
186   MST_UNBECOMING_MASTER
187 };
188
189 enum domain_state
190 {
191   DOMAIN_NONE,
192   DOMAIN_WAIT,
193   DOMAIN_MST
194 };
195
196 enum logon_state
197 {
198   LOGON_NONE,
199   LOGON_WAIT,
200   LOGON_SRV
201 };
202
203 struct subnet_record;
204
205 struct nmb_data
206 {
207   uint16 nb_flags;         /* Netbios flags. */
208   int num_ips;             /* Number of ip entries. */
209   struct in_addr *ip;      /* The ip list for this name. */
210
211   enum name_source source; /* Where the name came from. */
212
213   time_t death_time; /* The time the record must be removed (do not remove if 0). */
214   time_t refresh_time; /* The time the record should be refreshed. */
215   
216   SMB_BIG_UINT id;              /* unique id */
217   struct in_addr wins_ip;       /* the adress of the wins server this record comes from */
218
219   int wins_flags;               /* similar to the netbios flags but different ! */
220 };
221
222 /* This structure represents an entry in a local netbios name list. */
223 struct name_record
224   {
225 #if 0
226   ubi_trNode            node[1];
227 #endif
228   struct subnet_record *subnet;
229   struct nmb_name       name;    /* The netbios name. */
230   struct nmb_data       data;    /* The netbios data. */
231   };
232
233 /* Browser cache for synchronising browse lists. */
234 struct browse_cache_record
235   {
236 #if 0
237   ubi_dlNode     node[1];
238 #endif
239   pstring        lmb_name;
240   pstring        work_group;
241   struct in_addr ip;
242   time_t         sync_time;
243   time_t         death_time; /* The time the record must be removed. */
244   };
245
246 /* This is used to hold the list of servers in my domain, and is
247    contained within lists of domains. */
248
249 struct server_record
250 {
251   struct server_record *next;
252   struct server_record *prev;
253
254   struct subnet_record *subnet;
255
256   struct server_info_struct serv;
257   time_t death_time;  
258 };
259
260 /* A workgroup structure. It contains a list of servers. */
261 struct work_record
262 {
263   struct work_record *next;
264   struct work_record *prev;
265
266   struct subnet_record *subnet;
267
268   struct server_record *serverlist;
269
270   /* Stage of development from non-local-master up to local-master browser. */
271   enum master_state mst_state;
272
273   /* Stage of development from non-domain-master to domain-master browser. */
274   enum domain_state dom_state;
275
276   /* Stage of development from non-logon-server to logon server. */
277   enum logon_state log_state;
278
279   /* Work group info. */
280   fstring work_group;
281   int     token;        /* Used when communicating with backup browsers. */
282   fstring local_master_browser_name;      /* Current local master browser. */
283
284   /* Announce info. */
285   time_t lastannounce_time;
286   int announce_interval;
287   BOOL    needannounce;
288
289   /* Timeout time for this workgroup. 0 means permanent. */
290   time_t death_time;  
291
292   /* Election info */
293   BOOL    RunningElection;
294   BOOL    needelection;
295   int     ElectionCount;
296   uint32  ElectionCriterion;
297
298   /* Domain master browser info. Used for efficient syncs. */
299   struct nmb_name dmb_name;
300   struct in_addr dmb_addr;
301 };
302
303 /* typedefs needed to define copy & free functions for userdata. */
304 struct userdata_struct;
305
306 typedef struct userdata_struct * (*userdata_copy_fn)(struct userdata_struct *);
307 typedef void (*userdata_free_fn)(struct userdata_struct *);
308
309 /* Structure to define any userdata passed around. */
310
311 struct userdata_struct {
312   userdata_copy_fn copy_fn;
313   userdata_free_fn free_fn;
314   unsigned int userdata_len;
315   char data[16]; /* 16 is to ensure alignment/padding on all systems */
316 };
317
318 struct response_record;
319 struct packet_struct;
320 struct res_rec;
321
322 /* typedef to define the function called when this response packet comes in. */
323 typedef void (*response_function)(struct subnet_record *, struct response_record *,
324                                   struct packet_struct *);
325
326 /* typedef to define the function called when this response record times out. */
327 typedef void (*timeout_response_function)(struct subnet_record *,
328                                           struct response_record *);
329
330 /* typedef to define the function called when the request that caused this
331    response record to be created is successful. */
332 typedef void (*success_function)(struct subnet_record *, struct userdata_struct *, ...);
333
334 /* typedef to define the function called when the request that caused this
335    response record to be created is unsuccessful. */
336 typedef void (*fail_function)(struct subnet_record *, struct response_record *, ...);
337
338 /* List of typedefs for success and fail functions of the different query
339    types. Used to catch any compile time prototype errors. */
340
341 typedef void (*register_name_success_function)( struct subnet_record *,
342                                                 struct userdata_struct *,
343                                                 struct nmb_name *,
344                                                 uint16,
345                                                 int,
346                                                 struct in_addr);
347 typedef void (*register_name_fail_function)( struct subnet_record *,
348                                              struct response_record *,
349                                              struct nmb_name *);
350
351 typedef void (*release_name_success_function)( struct subnet_record *,
352                                                struct userdata_struct *, 
353                                                struct nmb_name *,
354                                                struct in_addr);
355 typedef void (*release_name_fail_function)( struct subnet_record *,
356                                             struct response_record *, 
357                                             struct nmb_name *);
358
359 typedef void (*refresh_name_success_function)( struct subnet_record *,
360                                                struct userdata_struct *, 
361                                                struct nmb_name *,
362                                                uint16,
363                                                int,
364                                                struct in_addr);
365 typedef void (*refresh_name_fail_function)( struct subnet_record *,
366                                             struct response_record *,
367                                             struct nmb_name *);
368
369 typedef void (*query_name_success_function)( struct subnet_record *,
370                                              struct userdata_struct *,
371                                              struct nmb_name *,
372                                              struct in_addr,
373                                              struct res_rec *answers);
374
375 typedef void (*query_name_fail_function)( struct subnet_record *,
376                                           struct response_record *,    
377                                           struct nmb_name *,
378                                           int);  
379
380 typedef void (*node_status_success_function)( struct subnet_record *,
381                                               struct userdata_struct *,
382                                               struct res_rec *,
383                                               struct in_addr);
384 typedef void (*node_status_fail_function)( struct subnet_record *,
385                                            struct response_record *);
386
387 /* Initiated name queries are recorded in this list to track any responses. */
388
389 struct response_record
390 {
391   struct response_record *next;
392   struct response_record *prev;
393
394   uint16 response_id;
395
396   /* Callbacks for packets received or not. */ 
397   response_function resp_fn;
398   timeout_response_function timeout_fn;
399
400   /* Callbacks for the request succeeding or not. */
401   success_function success_fn;
402   fail_function fail_fn;
403  
404   struct packet_struct *packet;
405
406   struct userdata_struct *userdata;
407
408   int num_msgs;
409
410   time_t repeat_time;
411   time_t repeat_interval;
412   int    repeat_count;
413
414   /* Recursion protection. */
415   BOOL in_expiration_processing;
416 };
417
418 /* A subnet structure. It contains a list of workgroups and netbios names. */
419
420 /*
421    B nodes will have their own, totally separate subnet record, with their
422    own netbios name set. These do NOT interact with other subnet records'
423    netbios names.
424 */
425
426 enum subnet_type {
427   NORMAL_SUBNET              = 0,  /* Subnet listed in interfaces list. */
428   UNICAST_SUBNET             = 1,  /* Subnet for unicast packets. */
429   REMOTE_BROADCAST_SUBNET    = 2,  /* Subnet for remote broadcasts. */
430   WINS_SERVER_SUBNET         = 3   /* Only created if we are a WINS server. */
431 };
432
433 struct subnet_record
434 {
435   struct subnet_record *next;
436   struct subnet_record *prev;
437
438   char  *subnet_name;      /* For Debug identification. */
439   enum subnet_type type;   /* To catagorize the subnet. */
440
441   struct work_record     *workgrouplist; /* List of workgroups. */
442 #if 0
443   ubi_trRoot              namelist[1];   /* List of netbios names. */
444 #endif
445   struct response_record *responselist;  /* List of responses expected. */
446
447   BOOL namelist_changed;
448   BOOL work_changed;
449
450   struct in_addr bcast_ip;
451   struct in_addr mask_ip;
452   struct in_addr myip;
453   int nmb_sock;               /* socket to listen for unicast 137. */
454   int dgram_sock;             /* socket to listen for unicast 138. */
455 };
456
457 /* A resource record. */
458 struct res_rec {
459   struct nmb_name rr_name;
460   int rr_type;
461   int rr_class;
462   int ttl;
463   int rdlength;
464   char rdata[MAX_DGRAM_SIZE];
465 };
466
467 /* Define these so we can pass info back to caller of name_query */
468 #define NM_FLAGS_RS 0x80 /* Response. Cheat     */
469 #define NM_FLAGS_AA 0x40 /* Authoritative       */
470 #define NM_FLAGS_TC 0x20 /* Truncated           */
471 #define NM_FLAGS_RD 0x10 /* Recursion Desired   */
472 #define NM_FLAGS_RA 0x08 /* Recursion Available */
473 #define NM_FLAGS_B  0x01 /* Broadcast           */
474
475 /* An nmb packet. */
476 struct nmb_packet
477 {
478   struct {
479     int name_trn_id;
480     int opcode;
481     BOOL response;
482     struct {
483       BOOL bcast;
484       BOOL recursion_available;
485       BOOL recursion_desired;
486       BOOL trunc;
487       BOOL authoritative;
488     } nm_flags;
489     int rcode;
490     int qdcount;
491     int ancount;
492     int nscount;
493     int arcount;
494   } header;
495
496   struct {
497     struct nmb_name question_name;
498     int question_type;
499     int question_class;
500   } question;
501
502   struct res_rec *answers;
503   struct res_rec *nsrecs;
504   struct res_rec *additional;
505 };
506
507 /* msg_type field options - from rfc1002. */
508
509 #define DGRAM_UNIQUE 0x10
510 #define DGRAM_GROUP 0x11
511 #define DGRAM_BROADCAST 0x12
512 #define DGRAM_ERROR 0x13
513 #define DGRAM_QUERY_REQUEST 0x14
514 #define DGRAM_POSITIVE_QUERY_RESPONSE 0x15
515 #define DGRAM_NEGATIVE_QUERT_RESPONSE 0x16
516
517 /* A datagram - this normally contains SMB data in the data[] array. */
518
519 struct dgram_packet {
520   struct {
521     int msg_type;
522     struct {
523       enum node_type node_type;
524       BOOL first;
525       BOOL more;
526     } flags;
527     int dgm_id;
528     struct in_addr source_ip;
529     int source_port;
530     int dgm_length;
531     int packet_offset;
532   } header;
533   struct nmb_name source_name;
534   struct nmb_name dest_name;
535   int datasize;
536   char data[MAX_DGRAM_SIZE];
537 };
538
539 /* Define a structure used to queue packets. This will be a linked
540  list of nmb packets. */
541
542 struct packet_struct
543 {
544   struct packet_struct *next;
545   struct packet_struct *prev;
546   BOOL locked;
547   struct in_addr ip;
548   int port;
549   int fd;
550   time_t timestamp;
551   enum packet_type packet_type;
552   union {
553     struct nmb_packet nmb;
554     struct dgram_packet dgram;
555   } packet;
556 };
557
558 /* NETLOGON opcodes */
559
560 #define QUERYFORPDC      7 /* Query for PDC. */
561 #define SAM_UAS_CHANGE  10 /* Announce change to UAS or SAM. */
562 #define QUERYFORPDC_R   12 /* Response to Query for PDC. */
563 #define SAMLOGON        18
564 #define SAMLOGON_R      19
565 #define SAMLOGON_UNK_R  21
566 #define SAMLOGON_AD_UNK_R 23
567 #define SAMLOGON_AD_R   25
568
569 /* Ids for netbios packet types. */
570
571 #define ANN_HostAnnouncement         1
572 #define ANN_AnnouncementRequest      2
573 #define ANN_Election                 8
574 #define ANN_GetBackupListReq         9
575 #define ANN_GetBackupListResp       10
576 #define ANN_BecomeBackup            11
577 #define ANN_DomainAnnouncement      12
578 #define ANN_MasterAnnouncement      13
579 #define ANN_ResetBrowserState       14
580 #define ANN_LocalMasterAnnouncement 15
581
582
583 /* Broadcast packet announcement intervals, in minutes. */
584
585 /* Attempt to add domain logon and domain master names. */
586 #define CHECK_TIME_ADD_DOM_NAMES 5 
587
588 /* Search for master browsers of workgroups samba knows about, 
589    except default. */
590 #define CHECK_TIME_MST_BROWSE       5 
591
592 /* Request backup browser announcements from other servers. */
593 #define CHECK_TIME_ANNOUNCE_BACKUP 15
594
595 /* Request host announcements from other servers: min and max of interval. */
596 #define CHECK_TIME_MIN_HOST_ANNCE   3
597 #define CHECK_TIME_MAX_HOST_ANNCE  12
598
599 /* Announce as master to WINS server and any Primary Domain Controllers. */
600 #define CHECK_TIME_MST_ANNOUNCE    15
601
602 /* Time between syncs from domain master browser to local master browsers. */
603 #define CHECK_TIME_DMB_TO_LMB_SYNC    15
604
605 /* Do all remote announcements this often. */
606 #define REMOTE_ANNOUNCE_INTERVAL 180
607
608 /* what is the maximum period between name refreshes. Note that this only
609    affects non-permanent self names (in seconds) */
610 #define MAX_REFRESH_TIME (60*20)
611
612 /* The Extinction interval: 4 days, time a node will stay in released state  */
613 #define EXTINCTION_INTERVAL (4*24*60*60)
614
615 /* The Extinction time-out: 1 day, time a node will stay in deleted state */
616 #define EXTINCTION_TIMEOUT (24*60*60)
617
618 /* Macro's to enumerate subnets either with or without
619    the UNICAST subnet. */
620
621 extern struct subnet_record *subnetlist;
622 extern struct subnet_record *unicast_subnet;
623 extern struct subnet_record *wins_server_subnet;
624 extern struct subnet_record *remote_broadcast_subnet;
625
626 #define FIRST_SUBNET subnetlist
627 #define NEXT_SUBNET_EXCLUDING_UNICAST(x) ((x)->next)
628 #define NEXT_SUBNET_INCLUDING_UNICAST(x) (get_next_subnet_maybe_unicast((x)))
629
630 /* wins replication record used between nmbd and wrepld */
631 typedef struct _WINS_RECORD {
632         char name[17];
633         char type;
634         int nb_flags;
635         int wins_flags;
636         SMB_BIG_UINT id;
637         int num_ips;
638         struct in_addr ip[25];
639         struct in_addr wins_ip;
640 } WINS_RECORD;
641
642 /* To be removed. */
643 enum state_type { TEST };
644 #endif /* _NAMESERV_H_ */