s3-net: add command rpc registry import
[Samba.git] / source3 / nmbd / nmbd_namelistdb.c
blob9c5d0efde2a983ebb8f02ba2b261950333fdf5d7
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
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.
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.
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/>.
23 #include "includes.h"
24 #include "nmbd/nmbd.h"
26 uint16 samba_nb_type = 0; /* samba's NetBIOS name type */
29 /**************************************************************************
30 Set Samba's NetBIOS name type.
31 ***************************************************************************/
33 void set_samba_nb_type(void)
35 if( lp_wins_support() || wins_srv_count() ) {
36 samba_nb_type = NB_HFLAG; /* samba is a 'hybrid' node type. */
37 } else {
38 samba_nb_type = NB_BFLAG; /* samba is broadcast-only node type. */
42 /***************************************************************************
43 Convert a NetBIOS name to upper case.
44 ***************************************************************************/
46 static void upcase_name( struct nmb_name *target, const struct nmb_name *source )
48 int i;
49 unstring targ;
50 fstring scope;
52 if( NULL != source ) {
53 memcpy( target, source, sizeof( struct nmb_name ) );
56 pull_ascii_nstring(targ, sizeof(targ), target->name);
57 strupper_m( targ );
58 push_ascii_nstring( target->name, targ);
60 pull_ascii(scope, target->scope, 64, -1, STR_TERMINATE);
61 strupper_m( scope );
62 push_ascii(target->scope, scope, 64, STR_TERMINATE);
64 /* fudge... We're using a byte-by-byte compare, so we must be sure that
65 * unused space doesn't have garbage in it.
68 for( i = strlen( target->name ); i < sizeof( target->name ); i++ ) {
69 target->name[i] = '\0';
71 for( i = strlen( target->scope ); i < sizeof( target->scope ); i++ ) {
72 target->scope[i] = '\0';
76 /**************************************************************************
77 Remove a name from the namelist.
78 ***************************************************************************/
80 void remove_name_from_namelist(struct subnet_record *subrec,
81 struct name_record *namerec )
83 if (subrec == wins_server_subnet)
84 remove_name_from_wins_namelist(namerec);
85 else {
86 subrec->namelist_changed = True;
87 DLIST_REMOVE(subrec->namelist, namerec);
90 SAFE_FREE(namerec->data.ip);
91 ZERO_STRUCTP(namerec);
92 SAFE_FREE(namerec);
95 /**************************************************************************
96 Find a name in a subnet.
97 **************************************************************************/
99 struct name_record *find_name_on_subnet(struct subnet_record *subrec,
100 const struct nmb_name *nmbname,
101 bool self_only)
103 struct nmb_name uc_name;
104 struct name_record *name_ret;
106 upcase_name( &uc_name, nmbname );
108 if (subrec == wins_server_subnet) {
109 return find_name_on_wins_subnet(&uc_name, self_only);
112 for( name_ret = subrec->namelist; name_ret; name_ret = name_ret->next) {
113 if (memcmp(&uc_name, &name_ret->name, sizeof(struct nmb_name)) == 0) {
114 break;
118 if( name_ret ) {
119 /* Self names only - these include permanent names. */
120 if( self_only && (name_ret->data.source != SELF_NAME) && (name_ret->data.source != PERMANENT_NAME) ) {
121 DEBUG( 9, ( "find_name_on_subnet: on subnet %s - self name %s NOT FOUND\n",
122 subrec->subnet_name, nmb_namestr(nmbname) ) );
123 return NULL;
126 DEBUG( 9, ("find_name_on_subnet: on subnet %s - found name %s source=%d\n",
127 subrec->subnet_name, nmb_namestr(nmbname), name_ret->data.source) );
129 return name_ret;
132 DEBUG( 9, ( "find_name_on_subnet: on subnet %s - name %s NOT FOUND\n",
133 subrec->subnet_name, nmb_namestr(nmbname) ) );
135 return NULL;
138 /**************************************************************************
139 Find a name over all known broadcast subnets.
140 ************************************************************************/
142 struct name_record *find_name_for_remote_broadcast_subnet(struct nmb_name *nmbname,
143 bool self_only)
145 struct subnet_record *subrec;
146 struct name_record *namerec;
148 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec) ) {
149 namerec = find_name_on_subnet(subrec, nmbname, self_only);
150 if (namerec) {
151 return namerec;
155 return NULL;
158 /**************************************************************************
159 Update the ttl of an entry in a subnet name list.
160 ***************************************************************************/
162 void update_name_ttl( struct name_record *namerec, int ttl )
164 time_t time_now = time(NULL);
166 if( namerec->data.death_time != PERMANENT_TTL) {
167 namerec->data.death_time = time_now + ttl;
170 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
172 if (namerec->subnet == wins_server_subnet) {
173 wins_store_changed_namerec(namerec);
174 } else {
175 namerec->subnet->namelist_changed = True;
179 /**************************************************************************
180 Add an entry to a subnet name list.
181 ***********************************************************************/
183 bool add_name_to_subnet( struct subnet_record *subrec,
184 const char *name,
185 int type,
186 uint16 nb_flags,
187 int ttl,
188 enum name_source source,
189 int num_ips,
190 struct in_addr *iplist)
192 bool ret = False;
193 struct name_record *namerec;
194 time_t time_now = time(NULL);
196 if (num_ips == 0) {
197 return false;
200 namerec = SMB_MALLOC_P(struct name_record);
201 if( NULL == namerec ) {
202 DEBUG( 0, ( "add_name_to_subnet: malloc fail.\n" ) );
203 return False;
206 memset( (char *)namerec, '\0', sizeof(*namerec) );
207 namerec->data.ip = SMB_MALLOC_ARRAY( struct in_addr, num_ips );
208 if( NULL == namerec->data.ip ) {
209 DEBUG( 0, ( "add_name_to_subnet: malloc fail when creating ip_flgs.\n" ) );
210 ZERO_STRUCTP(namerec);
211 SAFE_FREE(namerec);
212 return False;
215 namerec->subnet = subrec;
217 make_nmb_name(&namerec->name, name, type);
218 upcase_name(&namerec->name, NULL );
220 /* Enter the name as active. */
221 namerec->data.nb_flags = nb_flags | NB_ACTIVE;
222 namerec->data.wins_flags = WINS_ACTIVE;
224 /* If it's our primary name, flag it as so. */
225 if (strequal( my_netbios_names(0), name )) {
226 namerec->data.nb_flags |= NB_PERM;
229 /* Copy the IPs. */
230 namerec->data.num_ips = num_ips;
231 memcpy( (namerec->data.ip), iplist, num_ips * sizeof(struct in_addr) );
233 /* Data source. */
234 namerec->data.source = source;
236 /* Setup the death_time and refresh_time. */
237 if (ttl == PERMANENT_TTL) {
238 namerec->data.death_time = PERMANENT_TTL;
239 } else {
240 namerec->data.death_time = time_now + ttl;
243 namerec->data.refresh_time = time_now + MIN((ttl/2), MAX_REFRESH_TIME);
245 DEBUG( 3, ( "add_name_to_subnet: Added netbios name %s with first IP %s \
246 ttl=%d nb_flags=%2x to subnet %s\n",
247 nmb_namestr( &namerec->name ),
248 inet_ntoa( *iplist ),
249 ttl,
250 (unsigned int)nb_flags,
251 subrec->subnet_name ) );
253 /* Now add the record to the name list. */
255 if (subrec == wins_server_subnet) {
256 ret = add_name_to_wins_subnet(namerec);
257 /* Free namerec - it's stored in the tdb. */
258 SAFE_FREE(namerec->data.ip);
259 SAFE_FREE(namerec);
260 } else {
261 DLIST_ADD(subrec->namelist, namerec);
262 subrec->namelist_changed = True;
263 ret = True;
266 return ret;
269 /*******************************************************************
270 Utility function automatically called when a name refresh or register
271 succeeds. By definition this is a SELF_NAME (or we wouldn't be registering
272 it).
273 ******************************************************************/
275 void standard_success_register(struct subnet_record *subrec,
276 struct userdata_struct *userdata,
277 struct nmb_name *nmbname, uint16 nb_flags, int ttl,
278 struct in_addr registered_ip)
280 struct name_record *namerec;
282 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME);
283 if (namerec == NULL) {
284 unstring name;
285 pull_ascii_nstring(name, sizeof(name), nmbname->name);
286 add_name_to_subnet( subrec, name, nmbname->name_type,
287 nb_flags, ttl, SELF_NAME, 1, &registered_ip );
288 } else {
289 update_name_ttl( namerec, ttl );
293 /*******************************************************************
294 Utility function automatically called when a name refresh or register
295 fails. Note that this is only ever called on a broadcast subnet with
296 one IP address per name. This is why it can just delete the name
297 without enumerating the IP adresses. JRA.
298 ******************************************************************/
300 void standard_fail_register( struct subnet_record *subrec,
301 struct nmb_name *nmbname )
303 struct name_record *namerec;
305 namerec = find_name_on_subnet( subrec, nmbname, FIND_SELF_NAME);
307 DEBUG( 0, ( "standard_fail_register: Failed to register/refresh name %s \
308 on subnet %s\n", nmb_namestr(nmbname), subrec->subnet_name) );
310 /* Remove the name from the subnet. */
311 if( namerec ) {
312 remove_name_from_namelist(subrec, namerec);
316 /*******************************************************************
317 Utility function to remove an IP address from a name record.
318 ******************************************************************/
320 static void remove_nth_ip_in_record( struct name_record *namerec, int ind)
322 if( ind != namerec->data.num_ips ) {
323 memmove( (char *)(&namerec->data.ip[ind]),
324 (char *)(&namerec->data.ip[ind+1]),
325 ( namerec->data.num_ips - ind - 1) * sizeof(struct in_addr) );
328 namerec->data.num_ips--;
329 if (namerec->subnet == wins_server_subnet) {
330 wins_store_changed_namerec(namerec);
331 } else {
332 namerec->subnet->namelist_changed = True;
336 /*******************************************************************
337 Utility function to check if an IP address exists in a name record.
338 ******************************************************************/
340 bool find_ip_in_name_record( struct name_record *namerec, struct in_addr ip )
342 int i;
344 for(i = 0; i < namerec->data.num_ips; i++) {
345 if(ip_equal_v4( namerec->data.ip[i], ip)) {
346 return True;
350 return False;
353 /*******************************************************************
354 Utility function to add an IP address to a name record.
355 ******************************************************************/
357 void add_ip_to_name_record( struct name_record *namerec, struct in_addr new_ip )
359 struct in_addr *new_list;
361 /* Don't add one we already have. */
362 if( find_ip_in_name_record( namerec, new_ip )) {
363 return;
366 new_list = SMB_MALLOC_ARRAY( struct in_addr, namerec->data.num_ips + 1);
367 if( NULL == new_list ) {
368 DEBUG(0,("add_ip_to_name_record: Malloc fail !\n"));
369 return;
372 memcpy( (char *)new_list, (char *)namerec->data.ip, namerec->data.num_ips * sizeof(struct in_addr) );
373 new_list[namerec->data.num_ips] = new_ip;
375 SAFE_FREE(namerec->data.ip);
376 namerec->data.ip = new_list;
377 namerec->data.num_ips += 1;
379 if (namerec->subnet == wins_server_subnet) {
380 wins_store_changed_namerec(namerec);
381 } else {
382 namerec->subnet->namelist_changed = True;
386 /*******************************************************************
387 Utility function to remove an IP address from a name record.
388 ******************************************************************/
390 void remove_ip_from_name_record( struct name_record *namerec,
391 struct in_addr remove_ip )
393 /* Try and find the requested ip address - remove it. */
394 int i;
395 int orig_num = namerec->data.num_ips;
397 for(i = 0; i < orig_num; i++) {
398 if( ip_equal_v4( remove_ip, namerec->data.ip[i]) ) {
399 remove_nth_ip_in_record( namerec, i);
400 break;
405 /*******************************************************************
406 Utility function that release_name callers can plug into as the
407 success function when a name release is successful. Used to save
408 duplication of success_function code.
409 ******************************************************************/
411 void standard_success_release( struct subnet_record *subrec,
412 struct userdata_struct *userdata,
413 struct nmb_name *nmbname,
414 struct in_addr released_ip )
416 struct name_record *namerec;
418 namerec = find_name_on_subnet( subrec, nmbname, FIND_ANY_NAME );
419 if( namerec == NULL ) {
420 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
421 on subnet %s. Name was not found on subnet.\n", nmb_namestr(nmbname), inet_ntoa(released_ip),
422 subrec->subnet_name) );
423 return;
424 } else {
425 int orig_num = namerec->data.num_ips;
427 remove_ip_from_name_record( namerec, released_ip );
429 if( namerec->data.num_ips == orig_num ) {
430 DEBUG( 0, ( "standard_success_release: Name release for name %s IP %s \
431 on subnet %s. This ip is not known for this name.\n", nmb_namestr(nmbname), inet_ntoa(released_ip), subrec->subnet_name ) );
435 if( namerec->data.num_ips == 0 ) {
436 remove_name_from_namelist( subrec, namerec );
440 /*******************************************************************
441 Expires old names in a subnet namelist.
442 NB. Does not touch the wins_subnet - no wins specific processing here.
443 ******************************************************************/
445 static void expire_names_on_subnet(struct subnet_record *subrec, time_t t)
447 struct name_record *namerec;
448 struct name_record *next_namerec;
450 for( namerec = subrec->namelist; namerec; namerec = next_namerec ) {
451 next_namerec = namerec->next;
452 if( (namerec->data.death_time != PERMANENT_TTL) && (namerec->data.death_time < t) ) {
453 if( namerec->data.source == SELF_NAME ) {
454 DEBUG( 3, ( "expire_names_on_subnet: Subnet %s not expiring SELF \
455 name %s\n", subrec->subnet_name, nmb_namestr(&namerec->name) ) );
456 namerec->data.death_time += 300;
457 namerec->subnet->namelist_changed = True;
458 continue;
461 DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
462 subrec->subnet_name, nmb_namestr(&namerec->name)));
464 remove_name_from_namelist(subrec, namerec );
469 /*******************************************************************
470 Expires old names in all subnet namelists.
471 NB. Does not touch the wins_subnet.
472 ******************************************************************/
474 void expire_names(time_t t)
476 struct subnet_record *subrec;
478 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec) ) {
479 expire_names_on_subnet( subrec, t );
483 /****************************************************************************
484 Add the magic samba names, useful for finding samba servers.
485 These go directly into the name list for a particular subnet,
486 without going through the normal registration process.
487 When adding them to the unicast subnet, add them as a list of
488 all broadcast subnet IP addresses.
489 **************************************************************************/
491 void add_samba_names_to_subnet( struct subnet_record *subrec )
493 struct in_addr *iplist = &subrec->myip;
494 int num_ips = 1;
496 /* These names are added permanently (ttl of zero) and will NOT be refreshed. */
498 if( (subrec == unicast_subnet) || (subrec == wins_server_subnet) || (subrec == remote_broadcast_subnet) ) {
499 struct subnet_record *bcast_subrecs;
500 int i;
502 /* Create an IP list containing all our known subnets. */
504 num_ips = iface_count();
505 iplist = SMB_MALLOC_ARRAY( struct in_addr, num_ips);
506 if( NULL == iplist ) {
507 DEBUG(0,("add_samba_names_to_subnet: Malloc fail !\n"));
508 return;
511 for( bcast_subrecs = FIRST_SUBNET, i = 0; bcast_subrecs &&
512 i < num_ips;
513 bcast_subrecs = NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs), i++ ) {
514 iplist[i] = bcast_subrecs->myip;
516 num_ips = i;
519 add_name_to_subnet(subrec,"*",0x0,samba_nb_type, PERMANENT_TTL,
520 PERMANENT_NAME, num_ips, iplist);
521 add_name_to_subnet(subrec,"*",0x20,samba_nb_type,PERMANENT_TTL,
522 PERMANENT_NAME, num_ips, iplist);
523 add_name_to_subnet(subrec,"__SAMBA__",0x20,samba_nb_type,PERMANENT_TTL,
524 PERMANENT_NAME, num_ips, iplist);
525 add_name_to_subnet(subrec,"__SAMBA__",0x00,samba_nb_type,PERMANENT_TTL,
526 PERMANENT_NAME, num_ips, iplist);
528 if(iplist != &subrec->myip) {
529 SAFE_FREE(iplist);
533 /****************************************************************************
534 Dump a name_record struct.
535 **************************************************************************/
537 void dump_name_record( struct name_record *namerec, XFILE *fp)
539 const char *src_type;
540 struct tm *tm;
541 int i;
543 x_fprintf(fp,"\tName = %s\t", nmb_namestr(&namerec->name));
544 switch(namerec->data.source) {
545 case LMHOSTS_NAME:
546 src_type = "LMHOSTS_NAME";
547 break;
548 case WINS_PROXY_NAME:
549 src_type = "WINS_PROXY_NAME";
550 break;
551 case REGISTER_NAME:
552 src_type = "REGISTER_NAME";
553 break;
554 case SELF_NAME:
555 src_type = "SELF_NAME";
556 break;
557 case DNS_NAME:
558 src_type = "DNS_NAME";
559 break;
560 case DNSFAIL_NAME:
561 src_type = "DNSFAIL_NAME";
562 break;
563 case PERMANENT_NAME:
564 src_type = "PERMANENT_NAME";
565 break;
566 default:
567 src_type = "unknown!";
568 break;
571 x_fprintf(fp,"Source = %s\nb_flags = %x\t", src_type, namerec->data.nb_flags);
573 if(namerec->data.death_time != PERMANENT_TTL) {
574 const char *asct;
575 tm = localtime(&namerec->data.death_time);
576 if (!tm) {
577 return;
579 asct = asctime(tm);
580 if (!asct) {
581 return;
583 x_fprintf(fp, "death_time = %s\t", asct);
584 } else {
585 x_fprintf(fp, "death_time = PERMANENT\t");
588 if(namerec->data.refresh_time != PERMANENT_TTL) {
589 const char *asct;
590 tm = localtime(&namerec->data.refresh_time);
591 if (!tm) {
592 return;
594 asct = asctime(tm);
595 if (!asct) {
596 return;
598 x_fprintf(fp, "refresh_time = %s\n", asct);
599 } else {
600 x_fprintf(fp, "refresh_time = PERMANENT\n");
603 x_fprintf(fp, "\t\tnumber of IPS = %d", namerec->data.num_ips);
604 for(i = 0; i < namerec->data.num_ips; i++) {
605 x_fprintf(fp, "\t%s", inet_ntoa(namerec->data.ip[i]));
608 x_fprintf(fp, "\n\n");
612 /****************************************************************************
613 Dump the contents of the namelists on all the subnets (including unicast)
614 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
615 **************************************************************************/
617 static void dump_subnet_namelist( struct subnet_record *subrec, XFILE *fp)
619 struct name_record *namerec;
620 x_fprintf(fp, "Subnet %s\n----------------------\n", subrec->subnet_name);
621 for( namerec = subrec->namelist; namerec; namerec = namerec->next) {
622 dump_name_record(namerec, fp);
626 /****************************************************************************
627 Dump the contents of the namelists on all the subnets (including unicast)
628 into a file. Initiated by SIGHUP - used to debug the state of the namelists.
629 **************************************************************************/
631 void dump_all_namelists(void)
633 XFILE *fp;
634 struct subnet_record *subrec;
636 fp = x_fopen(lock_path("namelist.debug"),O_WRONLY|O_CREAT|O_TRUNC, 0644);
638 if (!fp) {
639 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
640 "namelist.debug",strerror(errno)));
641 return;
644 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
645 dump_subnet_namelist( subrec, fp );
648 if (!we_are_a_wins_client()) {
649 dump_subnet_namelist( unicast_subnet, fp );
652 if (remote_broadcast_subnet->namelist != NULL) {
653 dump_subnet_namelist( remote_broadcast_subnet, fp );
656 if (wins_server_subnet != NULL) {
657 dump_wins_subnet_namelist(fp );
660 x_fclose( fp );