s3-rpc_server: run minimal_includes.pl.
[samba.git] / source3 / nmbd / nmbd_namelistdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NBT netbios routines and daemon - version 2
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6    Copyright (C) Jeremy Allison 1994-2003
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 #include "includes.h"
24 #include "system/filesys.h"
25 #include "nmbd/nmbd.h"
26
27 uint16 samba_nb_type = 0; /* samba's NetBIOS name type */
28
29
30 /**************************************************************************
31  Set Samba's NetBIOS name type.
32 ***************************************************************************/
33
34 void set_samba_nb_type(void)
35 {
36         if( lp_wins_support() || wins_srv_count() ) {
37                 samba_nb_type = NB_HFLAG;               /* samba is a 'hybrid' node type. */
38         } else {
39                 samba_nb_type = NB_BFLAG;           /* samba is broadcast-only node type. */
40         }
41 }
42
43 /***************************************************************************
44  Convert a NetBIOS name to upper case.
45 ***************************************************************************/
46
47 static void upcase_name( struct nmb_name *target, const struct nmb_name *source )
48 {
49         int i;
50         unstring targ;
51         fstring scope;
52
53         if( NULL != source ) {
54                 memcpy( target, source, sizeof( struct nmb_name ) );
55         }
56
57         pull_ascii_nstring(targ, sizeof(targ), target->name);
58         strupper_m( targ );
59         push_ascii_nstring( target->name, targ);
60
61         pull_ascii(scope, target->scope, 64, -1, STR_TERMINATE);
62         strupper_m( scope );
63         push_ascii(target->scope, scope, 64, STR_TERMINATE);
64
65         /* fudge... We're using a byte-by-byte compare, so we must be sure that
66          * unused space doesn't have garbage in it.
67          */
68
69         for( i = strlen( target->name ); i < sizeof( target->name ); i++ ) {
70                 target->name[i] = '\0';
71         }
72         for( i = strlen( target->scope ); i < sizeof( target->scope ); i++ ) {
73                 target->scope[i] = '\0';
74         }
75 }
76
77 /**************************************************************************
78  Remove a name from the namelist.
79 ***************************************************************************/
80
81 void remove_name_from_namelist(struct subnet_record *subrec, 
82                                 struct name_record *namerec )
83 {
84         if (subrec == wins_server_subnet) 
85                 remove_name_from_wins_namelist(namerec);
86         else {
87                 subrec->namelist_changed = True;
88                 DLIST_REMOVE(subrec->namelist, namerec);
89         }
90
91         SAFE_FREE(namerec->data.ip);
92         ZERO_STRUCTP(namerec);
93         SAFE_FREE(namerec);
94 }
95
96 /**************************************************************************
97  Find a name in a subnet.
98 **************************************************************************/
99
100 struct name_record *find_name_on_subnet(struct subnet_record *subrec,
101                                 const struct nmb_name *nmbname,
102                                 bool self_only)
103 {
104         struct nmb_name uc_name;
105         struct name_record *name_ret;
106
107         upcase_name( &uc_name, nmbname );
108         
109         if (subrec == wins_server_subnet) {
110                 return find_name_on_wins_subnet(&uc_name, self_only);
111         }
112
113         for( name_ret = subrec->namelist; name_ret; name_ret = name_ret->next) {
114                 if (memcmp(&uc_name, &name_ret->name, sizeof(struct nmb_name)) == 0) {
115                         break;
116                 }
117         }
118
119         if( name_ret ) {
120                 /* Self names only - these include permanent names. */
121                 if( self_only && (name_ret->data.source != SELF_NAME) && (name_ret->data.source != PERMANENT_NAME) ) {
122                         DEBUG( 9, ( "find_name_on_subnet: on subnet %s - self name %s NOT FOUND\n",
123                                                 subrec->subnet_name, nmb_namestr(nmbname) ) );
124                         return NULL;
125                 }
126
127                 DEBUG( 9, ("find_name_on_subnet: on subnet %s - found name %s source=%d\n",
128                         subrec->subnet_name, nmb_namestr(nmbname), name_ret->data.source) );
129
130                 return name_ret;
131         }
132
133         DEBUG( 9, ( "find_name_on_subnet: on subnet %s - name %s NOT FOUND\n",
134                 subrec->subnet_name, nmb_namestr(nmbname) ) );
135
136         return NULL;
137 }
138
139 /**************************************************************************
140  Find a name over all known broadcast subnets.
141 ************************************************************************/
142
143 struct name_record *find_name_for_remote_broadcast_subnet(struct nmb_name *nmbname,
144                                                 bool self_only)
145 {
146         struct subnet_record *subrec;
147         struct name_record *namerec;
148
149         for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec) ) {
150                 namerec = find_name_on_subnet(subrec, nmbname, self_only);
151                 if (namerec) {
152                         return namerec;
153                 }
154         }
155
156         return NULL;
157 }
158   
159 /**************************************************************************
160  Update the ttl of an entry in a subnet name list.
161 ***************************************************************************/
162
163 void update_name_ttl( struct name_record *namerec, int ttl )
164 {
165         time_t time_now = time(NULL);
166
167         if( namerec->data.death_time != PERMANENT_TTL) {
168                 namerec->data.death_time = time_now + ttl;
169         }
170
171         namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
172
173         if (namerec->subnet == wins_server_subnet) {
174                 wins_store_changed_namerec(namerec);
175         } else {
176                 namerec->subnet->namelist_changed = True;
177         }
178 }
179
180 /**************************************************************************
181  Add an entry to a subnet name list.
182 ***********************************************************************/
183
184 bool add_name_to_subnet( struct subnet_record *subrec,
185                         const char *name,
186                         int type,
187                         uint16 nb_flags,
188                         int ttl,
189                         enum name_source source,
190                         int num_ips,
191                         struct in_addr *iplist)
192 {
193         bool ret = False;
194         struct name_record *namerec;
195         time_t time_now = time(NULL);
196
197         if (num_ips == 0) {
198                 return false;
199         }
200
201         namerec = SMB_MALLOC_P(struct name_record);
202         if( NULL == namerec ) {
203                 DEBUG( 0, ( "add_name_to_subnet: malloc fail.\n" ) );
204                 return False;
205         }
206
207         memset( (char *)namerec, '\0', sizeof(*namerec) );
208         namerec->data.ip = SMB_MALLOC_ARRAY( struct in_addr, num_ips );
209         if( NULL == namerec->data.ip ) {
210                 DEBUG( 0, ( "add_name_to_subnet: malloc fail when creating ip_flgs.\n" ) );
211                 ZERO_STRUCTP(namerec);
212                 SAFE_FREE(namerec);
213                 return False;
214         }
215
216         namerec->subnet = subrec;
217
218         make_nmb_name(&namerec->name, name, type);
219         upcase_name(&namerec->name, NULL );
220
221         /* Enter the name as active. */
222         namerec->data.nb_flags = nb_flags | NB_ACTIVE;
223         namerec->data.wins_flags = WINS_ACTIVE;
224
225         /* If it's our primary name, flag it as so. */
226         if (strequal( my_netbios_names(0), name )) {
227                 namerec->data.nb_flags |= NB_PERM;
228         }
229
230         /* Copy the IPs. */
231         namerec->data.num_ips = num_ips;
232         memcpy( (namerec->data.ip), iplist, num_ips * sizeof(struct in_addr) );
233
234         /* Data source. */
235         namerec->data.source = source;
236
237         /* Setup the death_time and refresh_time. */
238         if (ttl == PERMANENT_TTL) {
239                 namerec->data.death_time = PERMANENT_TTL;
240         } else {
241                 namerec->data.death_time = time_now + ttl;
242         }
243
244         namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
245
246         DEBUG( 3, ( "add_name_to_subnet: Added netbios name %s with first IP %s \
247 ttl=%d nb_flags=%2x to subnet %s\n",
248                 nmb_namestr( &namerec->name ),
249                 inet_ntoa( *iplist ),
250                 ttl,
251                 (unsigned int)nb_flags,
252                 subrec->subnet_name ) );
253
254         /* Now add the record to the name list. */    
255
256         if (subrec == wins_server_subnet) {
257                 ret = add_name_to_wins_subnet(namerec);
258                 /* Free namerec - it's stored in the tdb. */
259                 SAFE_FREE(namerec->data.ip);
260                 SAFE_FREE(namerec);
261         } else {
262                 DLIST_ADD(subrec->namelist, namerec);
263                 subrec->namelist_changed = True;
264                 ret = True;
265         }
266
267         return ret;
268 }
269
270 /*******************************************************************
271  Utility function automatically called when a name refresh or register 
272  succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
273  it).
274  ******************************************************************/
275
276 void standard_success_register(struct subnet_record *subrec, 
277                              struct userdata_struct *userdata,
278                              struct nmb_name *nmbname, uint16 nb_flags, int ttl,
279                              struct in_addr registered_ip)
280 {
281         struct name_record *namerec;
282
283         namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME);
284         if (namerec == NULL) {
285                 unstring name;
286                 pull_ascii_nstring(name, sizeof(name), nmbname->name);
287                 add_name_to_subnet( subrec, name, nmbname->name_type,
288                         nb_flags, ttl, SELF_NAME, 1, &registered_ip );
289         } else {
290                 update_name_ttl( namerec, ttl );
291         }
292 }
293
294 /*******************************************************************
295  Utility function automatically called when a name refresh or register 
296  fails. Note that this is only ever called on a broadcast subnet with
297  one IP address per name. This is why it can just delete the name 
298  without enumerating the IP adresses. JRA.
299  ******************************************************************/
300
301 void standard_fail_register( struct subnet_record   *subrec,
302                              struct nmb_name        *nmbname )
303 {
304         struct name_record *namerec;
305
306         namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME);
307
308         DEBUG( 0, ( "standard_fail_register: Failed to register/refresh name %s \
309 on subnet %s\n", nmb_namestr(nmbname), subrec->subnet_name) );
310
311         /* Remove the name from the subnet. */
312         if( namerec ) {
313                 remove_name_from_namelist(subrec, namerec);
314         }
315 }
316
317 /*******************************************************************
318  Utility function to remove an IP address from a name record.
319  ******************************************************************/
320
321 static void remove_nth_ip_in_record( struct name_record *namerec, int ind)
322 {
323         if( ind != namerec->data.num_ips ) {
324                 memmove( (char *)(&namerec->data.ip[ind]),
325                                 (char *)(&namerec->data.ip[ind+1]), 
326                                 ( namerec->data.num_ips - ind - 1) * sizeof(struct in_addr) );
327         }
328
329         namerec->data.num_ips--;
330         if (namerec->subnet == wins_server_subnet) {
331                 wins_store_changed_namerec(namerec);
332         } else {
333                 namerec->subnet->namelist_changed = True;
334         }
335 }
336
337 /*******************************************************************
338  Utility function to check if an IP address exists in a name record.
339  ******************************************************************/
340
341 bool find_ip_in_name_record( struct name_record *namerec, struct in_addr ip )
342 {
343         int i;
344
345         for(i = 0; i < namerec->data.num_ips; i++) {
346                 if(ip_equal_v4( namerec->data.ip[i], ip)) {
347                         return True;
348                 }
349         }
350
351         return False;
352 }
353
354 /*******************************************************************
355  Utility function to add an IP address to a name record.
356  ******************************************************************/
357
358 void add_ip_to_name_record( struct name_record *namerec, struct in_addr new_ip )
359 {
360         struct in_addr *new_list;
361
362         /* Don't add one we already have. */
363         if( find_ip_in_name_record( namerec, new_ip )) {
364                 return;
365         }
366   
367         new_list = SMB_MALLOC_ARRAY( struct in_addr, namerec->data.num_ips + 1);
368         if( NULL == new_list ) {
369                 DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
370                 return;
371         }
372
373         memcpy( (char *)new_list, (char *)namerec->data.ip, namerec->data.num_ips * sizeof(struct in_addr) );
374         new_list[namerec->data.num_ips] = new_ip;
375
376         SAFE_FREE(namerec->data.ip);
377         namerec->data.ip = new_list;
378         namerec->data.num_ips += 1;
379
380         if (namerec->subnet == wins_server_subnet) {
381                 wins_store_changed_namerec(namerec);
382         } else {
383                 namerec->subnet->namelist_changed = True;
384         }
385 }
386
387 /*******************************************************************
388  Utility function to remove an IP address from a name record.
389  ******************************************************************/
390
391 void remove_ip_from_name_record( struct name_record *namerec,
392                                  struct in_addr      remove_ip )
393 {
394         /* Try and find the requested ip address - remove it. */
395         int i;
396         int orig_num = namerec->data.num_ips;
397
398         for(i = 0; i < orig_num; i++) {
399                 if( ip_equal_v4( remove_ip, namerec->data.ip[i]) ) {
400                         remove_nth_ip_in_record( namerec, i);
401                         break;
402                 }
403         }
404 }
405
406 /*******************************************************************
407  Utility function that release_name callers can plug into as the
408  success function when a name release is successful. Used to save
409  duplication of success_function code.
410  ******************************************************************/
411
412 void standard_success_release( struct subnet_record   *subrec,
413                                struct userdata_struct *userdata,
414                                struct nmb_name        *nmbname,
415                                struct in_addr          released_ip )
416 {
417         struct name_record *namerec;
418
419         namerec = find_name_on_subnet( subrec, nmbname, FIND_ANY_NAME );
420         if( namerec == NULL ) {
421                 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
422 on subnet %s. Name was not found on subnet.\n", nmb_namestr(nmbname), inet_ntoa(released_ip),
423                                 subrec->subnet_name) );
424                 return;
425         } else {
426                 int orig_num = namerec->data.num_ips;
427
428                 remove_ip_from_name_record( namerec, released_ip );
429
430                 if( namerec->data.num_ips == orig_num ) {
431                         DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
432 on subnet %s. This ip is not known for this name.\n", nmb_namestr(nmbname), inet_ntoa(released_ip), subrec->subnet_name ) );
433                 }
434         }
435
436         if( namerec->data.num_ips == 0 ) {
437                 remove_name_from_namelist( subrec, namerec );
438         }
439 }
440
441 /*******************************************************************
442  Expires old names in a subnet namelist.
443  NB. Does not touch the wins_subnet - no wins specific processing here.
444 ******************************************************************/
445
446 static void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
447 {
448         struct name_record *namerec;
449         struct name_record *next_namerec;
450
451         for( namerec = subrec->namelist; namerec; namerec = next_namerec ) {
452                 next_namerec = namerec->next;
453                 if( (namerec->data.death_time != PERMANENT_TTL) && (namerec->data.death_time < t) ) {
454                         if( namerec->data.source == SELF_NAME ) {
455                                 DEBUG( 3, ( "expire_names_on_subnet: Subnet %s not expiring SELF \
456 name %s\n", subrec->subnet_name, nmb_namestr(&namerec->name) ) );
457                                 namerec->data.death_time += 300;
458                                 namerec->subnet->namelist_changed = True;
459                                 continue;
460                         }
461
462                         DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
463                                 subrec->subnet_name, nmb_namestr(&namerec->name)));
464   
465                         remove_name_from_namelist(subrec, namerec );
466                 }
467         }
468 }
469
470 /*******************************************************************
471  Expires old names in all subnet namelists.
472  NB. Does not touch the wins_subnet.
473 ******************************************************************/
474
475 void expire_names(time_t t)
476 {
477         struct subnet_record *subrec;
478
479         for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) ) {
480                 expire_names_on_subnet( subrec, t );
481         }
482 }
483
484 /****************************************************************************
485   Add the magic samba names, useful for finding samba servers.
486   These go directly into the name list for a particular subnet,
487   without going through the normal registration process.
488   When adding them to the unicast subnet, add them as a list of
489   all broadcast subnet IP addresses.
490 **************************************************************************/
491
492 void add_samba_names_to_subnet( struct subnet_record *subrec )
493 {
494         struct in_addr *iplist = &subrec->myip;
495         int num_ips = 1;
496
497         /* These names are added permanently (ttl of zero) and will NOT be refreshed.  */
498
499         if( (subrec == unicast_subnet) || (subrec == wins_server_subnet) || (subrec == remote_broadcast_subnet) ) {
500                 struct subnet_record *bcast_subrecs;
501                 int i;
502
503                 /* Create an IP list containing all our known subnets. */
504
505                 num_ips = iface_count();
506                 iplist = SMB_MALLOC_ARRAY( struct in_addr, num_ips);
507                 if( NULL == iplist ) {
508                         DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
509                         return;
510                 }
511
512                 for( bcast_subrecs = FIRST_SUBNET, i = 0; bcast_subrecs &&
513                                 i < num_ips;
514                                 bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++ ) {
515                         iplist[i] = bcast_subrecs->myip;
516                 }
517                 num_ips = i;
518         }
519
520         add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
521                                 PERMANENT_NAME, num_ips, iplist);
522         add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
523                                 PERMANENT_NAME, num_ips, iplist);
524         add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
525                                 PERMANENT_NAME, num_ips, iplist);
526         add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
527                                 PERMANENT_NAME, num_ips, iplist);
528
529         if(iplist != &subrec->myip) {
530                 SAFE_FREE(iplist);
531         }
532 }
533
534 /****************************************************************************
535  Dump a name_record struct.
536 **************************************************************************/
537
538 void dump_name_record( struct name_record *namerec, XFILE *fp)
539 {
540         const char *src_type;
541         struct tm *tm;
542         int i;
543
544         x_fprintf(fp,"\tName = %s\t", nmb_namestr(&namerec->name));
545         switch(namerec->data.source) {
546                 case LMHOSTS_NAME:
547                         src_type = "LMHOSTS_NAME";
548                         break;
549                 case WINS_PROXY_NAME:
550                         src_type = "WINS_PROXY_NAME";
551                         break;
552                 case REGISTER_NAME:
553                         src_type = "REGISTER_NAME";
554                         break;
555                 case SELF_NAME:
556                         src_type = "SELF_NAME";
557                         break;
558                 case DNS_NAME:
559                         src_type = "DNS_NAME";
560                         break;
561                 case DNSFAIL_NAME:
562                         src_type = "DNSFAIL_NAME";
563                         break;
564                 case PERMANENT_NAME:
565                         src_type = "PERMANENT_NAME";
566                         break;
567                 default:
568                         src_type = "unknown!";
569                         break;
570         }
571
572         x_fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags);
573
574         if(namerec->data.death_time != PERMANENT_TTL) {
575                 const char *asct;
576                 tm = localtime(&namerec->data.death_time);
577                 if (!tm) {
578                         return;
579                 }
580                 asct = asctime(tm);
581                 if (!asct) {
582                         return;
583                 }
584                 x_fprintf(fp, "death_time = %s\t", asct);
585         } else {
586                 x_fprintf(fp, "death_time = PERMANENT\t");
587         }
588
589         if(namerec->data.refresh_time != PERMANENT_TTL) {
590                 const char *asct;
591                 tm = localtime(&namerec->data.refresh_time);
592                 if (!tm) {
593                         return;
594                 }
595                 asct = asctime(tm);
596                 if (!asct) {
597                         return;
598                 }
599                 x_fprintf(fp, "refresh_time = %s\n", asct);
600         } else {
601                 x_fprintf(fp, "refresh_time = PERMANENT\n");
602         }
603
604         x_fprintf(fp, "\t\tnumber of IPS = %d", namerec->data.num_ips);
605         for(i = 0; i < namerec->data.num_ips; i++) {
606                 x_fprintf(fp, "\t%s", inet_ntoa(namerec->data.ip[i]));
607         }
608
609         x_fprintf(fp, "\n\n");
610         
611 }
612
613 /****************************************************************************
614  Dump the contents of the namelists on all the subnets (including unicast)
615  into a file. Initiated by SIGHUP - used to debug the state of the namelists.
616 **************************************************************************/
617
618 static void dump_subnet_namelist( struct subnet_record *subrec, XFILE *fp)
619 {
620         struct name_record *namerec;
621         x_fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
622         for( namerec = subrec->namelist; namerec; namerec = namerec->next) {
623                 dump_name_record(namerec, fp);
624         }
625 }
626
627 /****************************************************************************
628  Dump the contents of the namelists on all the subnets (including unicast)
629  into a file. Initiated by SIGHUP - used to debug the state of the namelists.
630 **************************************************************************/
631
632 void dump_all_namelists(void)
633 {
634         XFILE *fp; 
635         struct subnet_record *subrec;
636
637         fp = x_fopen(lock_path("namelist.debug"),O_WRONLY|O_CREAT|O_TRUNC, 0644);
638      
639         if (!fp) { 
640                 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
641                         "namelist.debug",strerror(errno)));
642                 return;
643         }
644       
645         for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
646                 dump_subnet_namelist( subrec, fp );
647         }
648
649         if (!we_are_a_wins_client()) {
650                 dump_subnet_namelist( unicast_subnet, fp );
651         }
652
653         if (remote_broadcast_subnet->namelist != NULL) {
654                 dump_subnet_namelist( remote_broadcast_subnet, fp );
655         }
656
657         if (wins_server_subnet != NULL) {
658                 dump_wins_subnet_namelist(fp );
659         }
660
661         x_fclose( fp );
662 }