1 #ifndef _NAMESERV_H_
2 #define _NAMESERV_H_
3 /*
4    Unix SMB/Netbios implementation.
5    Version 1.9.
6    NBT netbios header - version 2
7  */
8 
9 #define PERMANENT_TTL 0
10 
11 /* NTAS uses 2, NT uses 1, WfWg uses 0 */
12 #define MAINTAIN_LIST    2
13 #define ELECTION_VERSION 1
14 
15 #define MAX_DGRAM_SIZE (576)    /* tcp/ip datagram limit is 576 bytes */
16 #define MIN_DGRAM_SIZE 12
17 
18 /*********************************************************
19  Types of reply packet.
20 **********************************************************/
21 
22 enum netbios_reply_type_code
23 { NMB_QUERY, NMB_STATUS, NMB_REG, NMB_REG_REFRESH,
24     NMB_REL, NMB_WAIT_ACK, NMB_MULTIHOMED_REG,
25     WINS_REG, WINS_QUERY
26 };
27 
28 /* From rfc1002, 4.2.1.2 */
29 /* Question types. */
30 #define QUESTION_TYPE_NB_QUERY  0x20
31 #define QUESTION_TYPE_NB_STATUS 0x21
32 
33 /* Question class */
34 #define QUESTION_CLASS_IN  0x1
35 
36 /* Opcode definitions */
37 #define NMB_NAME_QUERY_OPCODE       0x0
38 #define NMB_NAME_REG_OPCODE         0x05        /* see rfc1002.txt 4.2.2,3,5,6,7,8 */
39 #define NMB_NAME_RELEASE_OPCODE     0x06        /* see rfc1002.txt 4.2.9,10,11 */
40 #define NMB_WACK_OPCODE             0x07        /* see rfc1002.txt 4.2.16 */
41 /* Ambiguity in rfc1002 about which of these is correct. */
42 /* WinNT uses 8 by default but can be made to use 9. */
43 #define NMB_NAME_REFRESH_OPCODE_8   0x08        /* see rfc1002.txt 4.2.4 */
44 #define NMB_NAME_REFRESH_OPCODE_9   0x09        /* see rfc1002.txt 4.2.4 */
45 #define NMB_NAME_MULTIHOMED_REG_OPCODE 0x0F     /* Invented by Microsoft. */
46 
47 /* XXXX what about all the other types?? 0x1, 0x2, 0x3, 0x4, 0x8? */
48 
49 /* Resource record types. rfc1002 4.2.1.3 */
50 #define RR_TYPE_A                  0x1
51 #define RR_TYPE_NS                 0x2
52 #define RR_TYPE_NULL               0xA
53 #define RR_TYPE_NB                0x20
54 #define RR_TYPE_NBSTAT            0x21
55 
56 /* Resource record class. */
57 #define RR_CLASS_IN                0x1
58 
59 /* NetBIOS flags */
60 #define NB_GROUP  0x80
61 #define NB_PERM   0x02
62 #define NB_ACTIVE 0x04
63 #define NB_CONFL  0x08
64 #define NB_DEREG  0x10
65 #define NB_BFLAG  0x00          /* Broadcast node type. */
66 #define NB_PFLAG  0x20          /* Point-to-point node type. */
67 #define NB_MFLAG  0x40          /* Mixed bcast & p-p node type. */
68 #define NB_HFLAG  0x60          /* Microsoft 'hybrid' node type. */
69 #define NB_NODETYPEMASK 0x60
70 /* Mask applied to outgoing NetBIOS flags. */
71 #define NB_FLGMSK 0xE0
72 
73 /* NetBIOS flag identifier. */
74 #define NAME_GROUP(p)  ((p)->data.nb_flags & NB_GROUP)
75 #define NAME_BFLAG(p) (((p)->data.nb_flags & NB_NODETYPEMASK) == NB_BFLAG)
76 #define NAME_PFLAG(p) (((p)->data.nb_flags & NB_NODETYPEMASK) == NB_PFLAG)
77 #define NAME_MFLAG(p) (((p)->data.nb_flags & NB_NODETYPEMASK) == NB_MFLAG)
78 #define NAME_HFLAG(p) (((p)->data.nb_flags & NB_NODETYPEMASK) == NB_HFLAG)
79 
80 /* Samba name state for a name in a namelist. */
81 #define NAME_IS_ACTIVE(p)        ((p)->data.nb_flags & NB_ACTIVE)
82 #define NAME_IN_CONFLICT(p)      ((p)->data.nb_flags & NB_CONFL)
83 #define NAME_IS_DEREGISTERING(p) ((p)->data.nb_flags & NB_DEREG)
84 
85 /* Error codes for NetBIOS requests. */
86 #define FMT_ERR   0x1           /* Packet format error. */
87 #define SRV_ERR   0x2           /* Internal server error. */
88 #define NAM_ERR   0x3           /* Name does not exist. */
89 #define IMP_ERR   0x4           /* Request not implemented. */
90 #define RFS_ERR   0x5           /* Request refused. */
91 #define ACT_ERR   0x6           /* Active error - name owned by another host. */
92 #define CFT_ERR   0x7           /* Name in conflict error. */
93 
94 #define REFRESH_TIME (15*60)
95 #define NAME_POLL_REFRESH_TIME (5*60)
96 #define NAME_POLL_INTERVAL 15
97 
98 /* Workgroup state identifiers. */
99 #define AM_POTENTIAL_MASTER_BROWSER(work) ((work)->mst_state == MST_POTENTIAL)
100 #define AM_LOCAL_MASTER_BROWSER(work) ((work)->mst_state == MST_BROWSER)
101 #define AM_DOMAIN_MASTER_BROWSER(work) ((work)->dom_state == DOMAIN_MST)
102 #define AM_DOMAIN_MEMBER(work) ((work)->log_state == LOGON_SRV)
103 
104 /* Microsoft browser NetBIOS name. */
105 #define MSBROWSE "\001\002__MSBROWSE__\002"
106 
107 /* Mail slots. */
108 #define BROWSE_MAILSLOT    "\\MAILSLOT\\BROWSE"
109 #define NET_LOGON_MAILSLOT "\\MAILSLOT\\NET\\NETLOGON"
110 #define NT_LOGON_MAILSLOT  "\\MAILSLOT\\NET\\NTLOGON"
111 #define LANMAN_MAILSLOT    "\\MAILSLOT\\LANMAN"
112 
113 /* Samba definitions for find_name_on_subnet(). */
114 #define FIND_ANY_NAME   0
115 #define FIND_SELF_NAME  1
116 
117 /*
118  * The different name types that can be in namelists.
119  *
120  * SELF_NAME should only be on the broadcast and unicast subnets.
121  * LMHOSTS_NAME should only be in the remote_broadcast_subnet.
122  * REGISTER_NAME, DNS_NAME, DNSFAIL_NAME should only be in the wins_server_subnet.
123  * WINS_PROXY_NAME should only be on the broadcast subnets.
124  * PERMANENT_NAME can be on all subnets except remote_broadcast_subnet.
125  *
126  */
127 
128 enum name_source
129 { LMHOSTS_NAME, REGISTER_NAME, SELF_NAME, DNS_NAME,
130     DNSFAIL_NAME, PERMANENT_NAME, WINS_PROXY_NAME
131 };
132 enum node_type
133 { B_NODE = 0, P_NODE = 1, M_NODE = 2, NBDD_NODE = 3 };
134 enum packet_type
135 { NMB_PACKET, DGRAM_PACKET };
136 
137 enum master_state
138 {
139     MST_NONE,
140     MST_POTENTIAL,
141     MST_BACKUP,
142     MST_MSB,
143     MST_BROWSER,
144     MST_UNBECOMING_MASTER
145 };
146 
147 enum domain_state
148 {
149     DOMAIN_NONE,
150     DOMAIN_WAIT,
151     DOMAIN_MST
152 };
153 
154 enum logon_state
155 {
156     LOGON_NONE,
157     LOGON_WAIT,
158     LOGON_SRV
159 };
160 
161 struct subnet_record;
162 
163 struct nmb_data
164 {
165     uint16 nb_flags;            /* Netbios flags. */
166     int num_ips;                /* Number of ip entries. */
167     struct in_addr *ip;         /* The ip list for this name. */
168 
169     enum name_source source;    /* Where the name came from. */
170 
171     time_t death_time;          /* The time the record must be removed (do not remove if 0). */
172     time_t refresh_time;        /* The time the record should be refreshed. */
173 };
174 
175 /* This is used to hold the list of servers in my domain, and is
176    contained within lists of domains. */
177 
178 struct server_record
179 {
180     struct server_record *next;
181     struct server_record *prev;
182 
183     struct subnet_record *subnet;
184 
185     struct server_info_struct serv;
186     time_t death_time;
187 };
188 
189 /* A workgroup structure. It contains a list of servers. */
190 struct work_record
191 {
192     struct work_record *next;
193     struct work_record *prev;
194 
195     struct subnet_record *subnet;
196 
197     struct server_record *serverlist;
198 
199     /* Stage of development from non-local-master up to local-master browser. */
200     enum master_state mst_state;
201 
202     /* Stage of development from non-domain-master to domain-master browser. */
203     enum domain_state dom_state;
204 
205     /* Stage of development from non-logon-server to logon server. */
206     enum logon_state log_state;
207 
208     /* Work group info. */
209     fstring work_group;
210     int token;                  /* Used when communicating with backup browsers. */
211     fstring local_master_browser_name;  /* Current local master browser. */
212 
213     /* Announce info. */
214     time_t lastannounce_time;
215     int announce_interval;
216     BOOL needannounce;
217 
218     /* Timeout time for this workgroup. 0 means permanent. */
219     time_t death_time;
220 
221     /* Election info */
222     BOOL RunningElection;
223     BOOL needelection;
224     int ElectionCount;
225     uint32 ElectionCriterion;
226 
227     /* Domain master browser info. Used for efficient syncs. */
228     struct nmb_name dmb_name;
229     struct in_addr dmb_addr;
230 };
231 
232 /* typedefs needed to define copy & free functions for userdata. */
233 struct userdata_struct;
234 
235 typedef struct userdata_struct *(*userdata_copy_fn) (struct userdata_struct *);
236 typedef void (*userdata_free_fn) (struct userdata_struct *);
237 
238 /* Structure to define any userdata passed around. */
239 
240 struct userdata_struct
241 {
242     userdata_copy_fn copy_fn;
243     userdata_free_fn free_fn;
244     unsigned int userdata_len;
245     char data[16];              /* 16 is to ensure alignment/padding on all systems */
246 };
247 
248 struct response_record;
249 struct packet_struct;
250 struct res_rec;
251 
252 /* typedef to define the function called when this response packet comes in. */
253 typedef void (*response_function) (struct subnet_record *, struct response_record *,
254                                    struct packet_struct *);
255 
256 /* typedef to define the function called when this response record times out. */
257 typedef void (*timeout_response_function) (struct subnet_record *, struct response_record *);
258 
259 /* typedef to define the function called when the request that caused this
260    response record to be created is successful. */
261 typedef void (*success_function) (struct subnet_record *, struct userdata_struct *, ...);
262 
263 /* typedef to define the function called when the request that caused this
264    response record to be created is unsuccessful. */
265 typedef void (*fail_function) (struct subnet_record *, struct response_record *, ...);
266 
267 /* List of typedefs for success and fail functions of the different query
268    types. Used to catch any compile time prototype errors. */
269 
270 typedef void (*register_name_success_function) (struct subnet_record *,
271                                                 struct userdata_struct *,
272                                                 struct nmb_name *, uint16, int, struct in_addr);
273 typedef void (*register_name_fail_function) (struct subnet_record *,
274                                              struct response_record *, struct nmb_name *);
275 
276 typedef void (*release_name_success_function) (struct subnet_record *,
277                                                struct userdata_struct *,
278                                                struct nmb_name *, struct in_addr);
279 typedef void (*release_name_fail_function) (struct subnet_record *,
280                                             struct response_record *, struct nmb_name *);
281 
282 typedef void (*refresh_name_success_function) (struct subnet_record *,
283                                                struct userdata_struct *,
284                                                struct nmb_name *, uint16, int, struct in_addr);
285 typedef void (*refresh_name_fail_function) (struct subnet_record *,
286                                             struct response_record *, struct nmb_name *);
287 
288 typedef void (*query_name_success_function) (struct subnet_record *,
289                                              struct userdata_struct *,
290                                              struct nmb_name *,
291                                              struct in_addr, struct res_rec * answers);
292 
293 typedef void (*query_name_fail_function) (struct subnet_record *,
294                                           struct response_record *, struct nmb_name *, int);
295 
296 typedef void (*node_status_success_function) (struct subnet_record *,
297                                               struct userdata_struct *,
298                                               struct res_rec *, struct in_addr);
299 typedef void (*node_status_fail_function) (struct subnet_record *, struct response_record *);
300 
301 /* Initiated name queries are recorded in this list to track any responses. */
302 
303 struct response_record
304 {
305     struct response_record *next;
306     struct response_record *prev;
307 
308     uint16 response_id;
309 
310     /* Callbacks for packets received or not. */
311     response_function resp_fn;
312     timeout_response_function timeout_fn;
313 
314     /* Callbacks for the request succeeding or not. */
315     success_function success_fn;
316     fail_function fail_fn;
317 
318     struct packet_struct *packet;
319 
320     struct userdata_struct *userdata;
321 
322     int num_msgs;
323 
324     time_t repeat_time;
325     time_t repeat_interval;
326     int repeat_count;
327 
328     /* Recursion protection. */
329     BOOL in_expiration_processing;
330 };
331 
332 /* A subnet structure. It contains a list of workgroups and netbios names. */
333 
334 /*
335    B nodes will have their own, totally separate subnet record, with their
336    own netbios name set. These do NOT interact with other subnet records'
337    netbios names.
338  */
339 
340 enum subnet_type
341 {
342     NORMAL_SUBNET = 0,          /* Subnet listed in interfaces list. */
343     UNICAST_SUBNET = 1,         /* Subnet for unicast packets. */
344     REMOTE_BROADCAST_SUBNET = 2,        /* Subnet for remote broadcasts. */
345     WINS_SERVER_SUBNET = 3      /* Only created if we are a WINS server. */
346 };
347 
348 /* A resource record. */
349 struct res_rec
350 {
351     struct nmb_name rr_name;
352     int rr_type;
353     int rr_class;
354     int ttl;
355     int rdlength;
356     char rdata[MAX_DGRAM_SIZE];
357 };
358 
359 /* An nmb packet. */
360 struct nmb_packet
361 {
362     struct
363     {
364         int name_trn_id;
365         int opcode;
366         BOOL response;
367         struct
368         {
369             BOOL bcast;
370             BOOL recursion_available;
371             BOOL recursion_desired;
372             BOOL trunc;
373             BOOL authoritative;
374         } nm_flags;
375         int rcode;
376         int qdcount;
377         int ancount;
378         int nscount;
379         int arcount;
380     } header;
381 
382     struct
383     {
384         struct nmb_name question_name;
385         int question_type;
386         int question_class;
387     } question;
388 
389     struct res_rec *answers;
390     struct res_rec *nsrecs;
391     struct res_rec *additional;
392 };
393 
394 
395 /* A datagram - this normally contains SMB data in the data[] array. */
396 
397 struct dgram_packet
398 {
399     struct
400     {
401         int msg_type;
402         struct
403         {
404             enum node_type node_type;
405             BOOL first;
406             BOOL more;
407         } flags;
408         int dgm_id;
409         struct in_addr source_ip;
410         int source_port;
411         int dgm_length;
412         int packet_offset;
413     } header;
414     struct nmb_name source_name;
415     struct nmb_name dest_name;
416     int datasize;
417     char data[MAX_DGRAM_SIZE];
418 };
419 
420 /* Define a structure used to queue packets. This will be a linked
421    list of nmb packets. */
422 
423 struct packet_struct
424 {
425     struct packet_struct *next;
426     struct packet_struct *prev;
427     BOOL locked;
428     struct in_addr ip;
429     int port;
430     int fd;
431     time_t timestamp;
432     enum packet_type packet_type;
433     union
434     {
435         struct nmb_packet nmb;
436         struct dgram_packet dgram;
437     } packet;
438 };
439 
440 /* NETLOGON opcodes */
441 
442 #define QUERYFORPDC	 7      /* Query for PDC. */
443 #define QUERYFORPDC_R	12      /* Response to Query for PDC. */
444 #define SAMLOGON	18
445 #define SAMLOGON_R	19
446 
447 
448 /* Ids for netbios packet types. */
449 
450 #define ANN_HostAnnouncement         1
451 #define ANN_AnnouncementRequest      2
452 #define ANN_Election                 8
453 #define ANN_GetBackupListReq         9
454 #define ANN_GetBackupListResp       10
455 #define ANN_BecomeBackup            11
456 #define ANN_DomainAnnouncement      12
457 #define ANN_MasterAnnouncement      13
458 #define ANN_ResetBrowserState       14
459 #define ANN_LocalMasterAnnouncement 15
460 
461 
462 /* Broadcast packet announcement intervals, in minutes. */
463 
464 /* Attempt to add domain logon and domain master names. */
465 #define CHECK_TIME_ADD_DOM_NAMES 5
466 
467 /* Search for master browsers of workgroups samba knows about,
468    except default. */
469 #define CHECK_TIME_MST_BROWSE       5
470 
471 /* Request backup browser announcements from other servers. */
472 #define CHECK_TIME_ANNOUNCE_BACKUP 15
473 
474 /* Request host announcements from other servers: min and max of interval. */
475 #define CHECK_TIME_MIN_HOST_ANNCE   3
476 #define CHECK_TIME_MAX_HOST_ANNCE  12
477 
478 /* Announce as master to WINS server and any Primary Domain Controllers. */
479 #define CHECK_TIME_MST_ANNOUNCE    15
480 
481 /* Time between syncs from domain master browser to local master browsers. */
482 #define CHECK_TIME_DMB_TO_LMB_SYNC    15
483 
484 /* Do all remote announcements this often. */
485 #define REMOTE_ANNOUNCE_INTERVAL 180
486 
487 /* what is the maximum period between name refreshes. Note that this only
488    affects non-permanent self names (in seconds) */
489 #define MAX_REFRESH_TIME (60*20)
490 
491 /* Macro's to enumerate subnets either with or without
492    the UNICAST subnet. */
493 
494 extern struct subnet_record *subnetlist;
495 extern struct subnet_record *unicast_subnet;
496 extern struct subnet_record *wins_server_subnet;
497 extern struct subnet_record *remote_broadcast_subnet;
498 
499 #define FIRST_SUBNET subnetlist
500 #define NEXT_SUBNET_EXCLUDING_UNICAST(x) ((x)->next)
501 #define NEXT_SUBNET_INCLUDING_UNICAST(x) (get_next_subnet_maybe_unicast((x)))
502 
503 /* To be removed. */
504 enum state_type
505 { TEST };
506 #endif /* _NAMESERV_H_ */
507