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