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/>.
24 #include "system/filesys.h"
25 #include "nmbd/nmbd.h"
27 uint16 samba_nb_type
= 0; /* samba's NetBIOS name type */
30 /**************************************************************************
31 Set Samba's NetBIOS name type.
32 ***************************************************************************/
34 void set_samba_nb_type(void)
36 if( lp_we_are_a_wins_server() || wins_srv_count() ) {
37 samba_nb_type
= NB_HFLAG
; /* samba is a 'hybrid' node type. */
39 samba_nb_type
= NB_BFLAG
; /* samba is broadcast-only node type. */
43 /***************************************************************************
44 Convert a NetBIOS name to upper case.
45 ***************************************************************************/
47 static void upcase_name( struct nmb_name
*target
, const struct nmb_name
*source
)
53 if( NULL
!= source
) {
54 memcpy( target
, source
, sizeof( struct nmb_name
) );
57 pull_ascii_nstring(targ
, sizeof(targ
), target
->name
);
59 push_ascii_nstring( target
->name
, targ
);
61 pull_ascii(scope
, target
->scope
, 64, -1, STR_TERMINATE
);
63 push_ascii(target
->scope
, scope
, 64, STR_TERMINATE
);
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.
69 for( i
= strlen( target
->name
); i
< sizeof( target
->name
); i
++ ) {
70 target
->name
[i
] = '\0';
72 for( i
= strlen( target
->scope
); i
< sizeof( target
->scope
); i
++ ) {
73 target
->scope
[i
] = '\0';
77 /**************************************************************************
78 Remove a name from the namelist.
79 ***************************************************************************/
81 void remove_name_from_namelist(struct subnet_record
*subrec
,
82 struct name_record
*namerec
)
84 if (subrec
== wins_server_subnet
)
85 remove_name_from_wins_namelist(namerec
);
87 subrec
->namelist_changed
= True
;
88 DLIST_REMOVE(subrec
->namelist
, namerec
);
91 SAFE_FREE(namerec
->data
.ip
);
92 ZERO_STRUCTP(namerec
);
96 /**************************************************************************
97 Find a name in a subnet.
98 **************************************************************************/
100 struct name_record
*find_name_on_subnet(struct subnet_record
*subrec
,
101 const struct nmb_name
*nmbname
,
104 struct nmb_name uc_name
;
105 struct name_record
*name_ret
;
107 upcase_name( &uc_name
, nmbname
);
109 if (subrec
== wins_server_subnet
) {
110 return find_name_on_wins_subnet(&uc_name
, self_only
);
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) {
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
) ) );
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
) );
133 DEBUG( 9, ( "find_name_on_subnet: on subnet %s - name %s NOT FOUND\n",
134 subrec
->subnet_name
, nmb_namestr(nmbname
) ) );
139 /**************************************************************************
140 Find a name over all known broadcast subnets.
141 ************************************************************************/
143 struct name_record
*find_name_for_remote_broadcast_subnet(struct nmb_name
*nmbname
,
146 struct subnet_record
*subrec
;
147 struct name_record
*namerec
;
149 for( subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
) ) {
150 namerec
= find_name_on_subnet(subrec
, nmbname
, self_only
);
159 /**************************************************************************
160 Update the ttl of an entry in a subnet name list.
161 ***************************************************************************/
163 void update_name_ttl( struct name_record
*namerec
, int ttl
)
165 time_t time_now
= time(NULL
);
167 if( namerec
->data
.death_time
!= PERMANENT_TTL
) {
168 namerec
->data
.death_time
= time_now
+ ttl
;
171 namerec
->data
.refresh_time
= time_now
+ MIN((ttl
/2), MAX_REFRESH_TIME
);
173 if (namerec
->subnet
== wins_server_subnet
) {
174 wins_store_changed_namerec(namerec
);
176 namerec
->subnet
->namelist_changed
= True
;
180 /**************************************************************************
181 Add an entry to a subnet name list.
182 ***********************************************************************/
184 bool add_name_to_subnet( struct subnet_record
*subrec
,
189 enum name_source source
,
191 struct in_addr
*iplist
)
194 struct name_record
*namerec
;
195 time_t time_now
= time(NULL
);
201 namerec
= SMB_MALLOC_P(struct name_record
);
202 if( NULL
== namerec
) {
203 DEBUG( 0, ( "add_name_to_subnet: malloc fail.\n" ) );
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
);
216 namerec
->subnet
= subrec
;
218 make_nmb_name(&namerec
->name
, name
, type
);
219 upcase_name(&namerec
->name
, NULL
);
221 /* Enter the name as active. */
222 namerec
->data
.nb_flags
= nb_flags
| NB_ACTIVE
;
223 namerec
->data
.wins_flags
= WINS_ACTIVE
;
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
;
231 namerec
->data
.num_ips
= num_ips
;
232 memcpy( (namerec
->data
.ip
), iplist
, num_ips
* sizeof(struct in_addr
) );
235 namerec
->data
.source
= source
;
237 /* Setup the death_time and refresh_time. */
238 if (ttl
== PERMANENT_TTL
) {
239 namerec
->data
.death_time
= PERMANENT_TTL
;
241 namerec
->data
.death_time
= time_now
+ ttl
;
244 namerec
->data
.refresh_time
= time_now
+ MIN((ttl
/2), MAX_REFRESH_TIME
);
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
),
251 (unsigned int)nb_flags
,
252 subrec
->subnet_name
) );
254 /* Now add the record to the name list. */
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
);
262 DLIST_ADD(subrec
->namelist
, namerec
);
263 subrec
->namelist_changed
= True
;
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
274 ******************************************************************/
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
)
281 struct name_record
*namerec
;
283 namerec
= find_name_on_subnet( subrec
, nmbname
, FIND_SELF_NAME
);
284 if (namerec
== NULL
) {
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, ®istered_ip
);
290 update_name_ttl( namerec
, ttl
);
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 ******************************************************************/
301 void standard_fail_register( struct subnet_record
*subrec
,
302 struct nmb_name
*nmbname
)
304 struct name_record
*namerec
;
306 namerec
= find_name_on_subnet( subrec
, nmbname
, FIND_SELF_NAME
);
308 DEBUG( 0, ( "standard_fail_register: Failed to register/refresh name %s \
309 on subnet %s\n", nmb_namestr(nmbname
), subrec
->subnet_name
) );
311 /* Remove the name from the subnet. */
313 remove_name_from_namelist(subrec
, namerec
);
317 /*******************************************************************
318 Utility function to remove an IP address from a name record.
319 ******************************************************************/
321 static void remove_nth_ip_in_record( struct name_record
*namerec
, int ind
)
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
) );
329 namerec
->data
.num_ips
--;
330 if (namerec
->subnet
== wins_server_subnet
) {
331 wins_store_changed_namerec(namerec
);
333 namerec
->subnet
->namelist_changed
= True
;
337 /*******************************************************************
338 Utility function to check if an IP address exists in a name record.
339 ******************************************************************/
341 bool find_ip_in_name_record( struct name_record
*namerec
, struct in_addr ip
)
345 for(i
= 0; i
< namerec
->data
.num_ips
; i
++) {
346 if(ip_equal_v4( namerec
->data
.ip
[i
], ip
)) {
354 /*******************************************************************
355 Utility function to add an IP address to a name record.
356 ******************************************************************/
358 void add_ip_to_name_record( struct name_record
*namerec
, struct in_addr new_ip
)
360 struct in_addr
*new_list
;
362 /* Don't add one we already have. */
363 if( find_ip_in_name_record( namerec
, new_ip
)) {
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"));
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
;
376 SAFE_FREE(namerec
->data
.ip
);
377 namerec
->data
.ip
= new_list
;
378 namerec
->data
.num_ips
+= 1;
380 if (namerec
->subnet
== wins_server_subnet
) {
381 wins_store_changed_namerec(namerec
);
383 namerec
->subnet
->namelist_changed
= True
;
387 /*******************************************************************
388 Utility function to remove an IP address from a name record.
389 ******************************************************************/
391 void remove_ip_from_name_record( struct name_record
*namerec
,
392 struct in_addr remove_ip
)
394 /* Try and find the requested ip address - remove it. */
396 int orig_num
= namerec
->data
.num_ips
;
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
);
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 ******************************************************************/
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
)
417 struct name_record
*namerec
;
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
) );
426 int orig_num
= namerec
->data
.num_ips
;
428 remove_ip_from_name_record( namerec
, released_ip
);
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
) );
436 if( namerec
->data
.num_ips
== 0 ) {
437 remove_name_from_namelist( subrec
, namerec
);
441 /*******************************************************************
442 Expires old names in a subnet namelist.
443 NB. Does not touch the wins_subnet - no wins specific processing here.
444 ******************************************************************/
446 static void expire_names_on_subnet(struct subnet_record
*subrec
, time_t t
)
448 struct name_record
*namerec
;
449 struct name_record
*next_namerec
;
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
;
462 DEBUG(3,("expire_names_on_subnet: Subnet %s - removing expired name %s\n",
463 subrec
->subnet_name
, nmb_namestr(&namerec
->name
)));
465 remove_name_from_namelist(subrec
, namerec
);
470 /*******************************************************************
471 Expires old names in all subnet namelists.
472 NB. Does not touch the wins_subnet.
473 ******************************************************************/
475 void expire_names(time_t t
)
477 struct subnet_record
*subrec
;
479 for( subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
) ) {
480 expire_names_on_subnet( subrec
, t
);
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 **************************************************************************/
492 void add_samba_names_to_subnet( struct subnet_record
*subrec
)
494 struct in_addr
*iplist
= &subrec
->myip
;
497 /* These names are added permanently (ttl of zero) and will NOT be refreshed. */
499 if( (subrec
== unicast_subnet
) || (subrec
== wins_server_subnet
) || (subrec
== remote_broadcast_subnet
) ) {
500 struct subnet_record
*bcast_subrecs
;
503 /* Create an IP list containing all our known subnets. */
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"));
512 for( bcast_subrecs
= FIRST_SUBNET
, i
= 0; bcast_subrecs
&&
514 bcast_subrecs
= NEXT_SUBNET_EXCLUDING_UNICAST(bcast_subrecs
), i
++ ) {
515 iplist
[i
] = bcast_subrecs
->myip
;
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
);
529 if(iplist
!= &subrec
->myip
) {
534 /****************************************************************************
535 Dump a name_record struct.
536 **************************************************************************/
538 void dump_name_record( struct name_record
*namerec
, XFILE
*fp
)
540 const char *src_type
;
544 x_fprintf(fp
,"\tName = %s\t", nmb_namestr(&namerec
->name
));
545 switch(namerec
->data
.source
) {
547 src_type
= "LMHOSTS_NAME";
549 case WINS_PROXY_NAME
:
550 src_type
= "WINS_PROXY_NAME";
553 src_type
= "REGISTER_NAME";
556 src_type
= "SELF_NAME";
559 src_type
= "DNS_NAME";
562 src_type
= "DNSFAIL_NAME";
565 src_type
= "PERMANENT_NAME";
568 src_type
= "unknown!";
572 x_fprintf(fp
,"Source = %s\nb_flags = %x\t", src_type
, namerec
->data
.nb_flags
);
574 if(namerec
->data
.death_time
!= PERMANENT_TTL
) {
576 tm
= localtime(&namerec
->data
.death_time
);
584 x_fprintf(fp
, "death_time = %s\t", asct
);
586 x_fprintf(fp
, "death_time = PERMANENT\t");
589 if(namerec
->data
.refresh_time
!= PERMANENT_TTL
) {
591 tm
= localtime(&namerec
->data
.refresh_time
);
599 x_fprintf(fp
, "refresh_time = %s\n", asct
);
601 x_fprintf(fp
, "refresh_time = PERMANENT\n");
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
]));
609 x_fprintf(fp
, "\n\n");
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 **************************************************************************/
618 static void dump_subnet_namelist( struct subnet_record
*subrec
, XFILE
*fp
)
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
);
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 **************************************************************************/
632 void dump_all_namelists(void)
635 struct subnet_record
*subrec
;
637 fp
= x_fopen(lock_path("namelist.debug"),O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
640 DEBUG(0,("dump_all_namelists: Can't open file %s. Error was %s\n",
641 "namelist.debug",strerror(errno
)));
645 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_INCLUDING_UNICAST(subrec
)) {
646 dump_subnet_namelist( subrec
, fp
);
649 if (!we_are_a_wins_client()) {
650 dump_subnet_namelist( unicast_subnet
, fp
);
653 if (remote_broadcast_subnet
->namelist
!= NULL
) {
654 dump_subnet_namelist( remote_broadcast_subnet
, fp
);
657 if (wins_server_subnet
!= NULL
) {
658 dump_wins_subnet_namelist(fp
);