2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* nmbd.c sets this to True. */
25 BOOL global_in_nmbd = False;
27 /****************************************************************************
28 Generate a random trn_id.
29 ****************************************************************************/
31 static int generate_trn_id(void)
36 sys_srandom(sys_getpid());
39 trn_id = sys_random();
41 return trn_id % (unsigned)0x7FFF;
44 /****************************************************************************
45 Parse a node status response into an array of structures.
46 ****************************************************************************/
48 static struct node_status *parse_node_status(char *p, int *num_names, struct node_status_extra *extra)
50 struct node_status *ret;
53 *num_names = CVAL(p,0);
58 ret = (struct node_status *)malloc(sizeof(struct node_status)* (*num_names));
63 for (i=0;i< *num_names;i++) {
64 StrnCpy(ret[i].name,p,15);
65 trim_char(ret[i].name,'\0',' ');
66 ret[i].type = CVAL(p,15);
69 DEBUG(10, ("%s#%02x: flags = 0x%02x\n", ret[i].name,
70 ret[i].type, ret[i].flags));
73 * Also, pick up the MAC address ...
76 memcpy(&extra->mac_addr, p, 6); /* Fill in the mac addr */
82 /****************************************************************************
83 Do a NBT node status query on an open socket and return an array of
84 structures holding the returned names or NULL if the query failed.
85 **************************************************************************/
87 struct node_status *node_status_query(int fd,struct nmb_name *name,
88 struct in_addr to_ip, int *num_names,
89 struct node_status_extra *extra)
93 int retry_time = 2000;
95 struct packet_struct p;
96 struct packet_struct *p2;
97 struct nmb_packet *nmb = &p.packet.nmb;
98 struct node_status *ret;
102 nmb->header.name_trn_id = generate_trn_id();
103 nmb->header.opcode = 0;
104 nmb->header.response = False;
105 nmb->header.nm_flags.bcast = False;
106 nmb->header.nm_flags.recursion_available = False;
107 nmb->header.nm_flags.recursion_desired = False;
108 nmb->header.nm_flags.trunc = False;
109 nmb->header.nm_flags.authoritative = False;
110 nmb->header.rcode = 0;
111 nmb->header.qdcount = 1;
112 nmb->header.ancount = 0;
113 nmb->header.nscount = 0;
114 nmb->header.arcount = 0;
115 nmb->question.question_name = *name;
116 nmb->question.question_type = 0x21;
117 nmb->question.question_class = 0x1;
122 p.timestamp = time(NULL);
123 p.packet_type = NMB_PACKET;
127 if (!send_packet(&p))
133 struct timeval tval2;
134 GetTimeOfDay(&tval2);
135 if (TvalDiff(&tval,&tval2) > retry_time) {
138 if (!found && !send_packet(&p))
144 if ((p2=receive_nmb_packet(fd,90,nmb->header.name_trn_id))) {
145 struct nmb_packet *nmb2 = &p2->packet.nmb;
146 debug_nmb_packet(p2);
148 if (nmb2->header.opcode != 0 ||
149 nmb2->header.nm_flags.bcast ||
150 nmb2->header.rcode ||
151 !nmb2->header.ancount ||
152 nmb2->answers->rr_type != 0x21) {
153 /* XXXX what do we do with this? could be a
154 redirect, but we'll discard it for the
160 ret = parse_node_status(&nmb2->answers->rdata[0], num_names, extra);
169 /****************************************************************************
170 Find the first type XX name in a node status reply - used for finding
171 a servers name given its IP. Return the matched name in *name.
172 **************************************************************************/
174 BOOL name_status_find(const char *q_name, int q_type, int type, struct in_addr to_ip, fstring name)
176 struct node_status *status = NULL;
177 struct nmb_name nname;
182 if (lp_disable_netbios()) {
183 DEBUG(5,("name_status_find(%s#%02x): netbios is disabled\n", q_name, q_type));
187 DEBUG(10, ("name_status_find: looking up %s#%02x at %s\n", q_name,
188 q_type, inet_ntoa(to_ip)));
190 /* Check the cache first. */
192 if (namecache_status_fetch(q_name, q_type, type, to_ip, name))
195 sock = open_socket_in(SOCK_DGRAM, 0, 3, interpret_addr(lp_socket_address()), True);
199 /* W2K PDC's seem not to respond to '*'#0. JRA */
200 make_nmb_name(&nname, q_name, q_type);
201 status = node_status_query(sock, &nname, to_ip, &count, NULL);
206 for (i=0;i<count;i++) {
207 if (status[i].type == type)
213 pull_ascii_nstring(name, sizeof(fstring), status[i].name);
215 /* Store the result in the cache. */
216 /* but don't store an entry for 0x1c names here. Here we have
217 a single host and DOMAIN<0x1c> names should be a list of hosts */
219 if ( q_type != 0x1c )
220 namecache_status_store(q_name, q_type, type, to_ip, name);
227 DEBUG(10, ("name_status_find: name %sfound", result ? "" : "not "));
230 DEBUGADD(10, (", name %s ip address is %s", name, inet_ntoa(to_ip)));
238 comparison function used by sort_ip_list
241 static int ip_compare(struct in_addr *ip1, struct in_addr *ip2)
243 int max_bits1=0, max_bits2=0;
244 int num_interfaces = iface_count();
247 for (i=0;i<num_interfaces;i++) {
250 ip = *iface_n_bcast(i);
251 bits1 = matching_quad_bits((uchar *)&ip1->s_addr, (uchar *)&ip.s_addr);
252 bits2 = matching_quad_bits((uchar *)&ip2->s_addr, (uchar *)&ip.s_addr);
253 max_bits1 = MAX(bits1, max_bits1);
254 max_bits2 = MAX(bits2, max_bits2);
257 /* bias towards directly reachable IPs */
258 if (iface_local(*ip1)) {
261 if (iface_local(*ip2)) {
265 return max_bits2 - max_bits1;
268 /*******************************************************************
269 compare 2 ldap IPs by nearness to our interfaces - used in qsort
270 *******************************************************************/
272 static int ip_service_compare(struct ip_service *ip1, struct ip_service *ip2)
276 if ( (result = ip_compare(&ip1->ip, &ip2->ip)) != 0 )
279 if ( ip1->port > ip2->port )
282 if ( ip1->port < ip2->port )
289 sort an IP list so that names that are close to one of our interfaces
290 are at the top. This prevents the problem where a WINS server returns an IP that
291 is not reachable from our subnet as the first match
294 static void sort_ip_list(struct in_addr *iplist, int count)
300 qsort(iplist, count, sizeof(struct in_addr), QSORT_CAST ip_compare);
303 static void sort_ip_list2(struct ip_service *iplist, int count)
309 qsort(iplist, count, sizeof(struct ip_service), QSORT_CAST ip_service_compare);
312 /**********************************************************************
313 Remove any duplicate address/port pairs in the list
314 *********************************************************************/
316 static int remove_duplicate_addrs2( struct ip_service *iplist, int count )
320 DEBUG(10,("remove_duplicate_addrs2: looking for duplicate address/port pairs\n"));
322 /* one loop to remove duplicates */
323 for ( i=0; i<count; i++ ) {
324 if ( is_zero_ip(iplist[i].ip) )
327 for ( j=i+1; j<count; j++ ) {
328 if ( ip_service_equal(iplist[i], iplist[j]) )
329 zero_ip(&iplist[j].ip);
333 /* one loop to clean up any holes we left */
334 /* first ip should never be a zero_ip() */
335 for (i = 0; i<count; ) {
336 if ( is_zero_ip(iplist[i].ip) ) {
338 memmove(&iplist[i], &iplist[i+1], (count - i - 1)*sizeof(iplist[i]));
348 /****************************************************************************
349 Do a netbios name query to find someones IP.
350 Returns an array of IP addresses or NULL if none.
351 *count will be set to the number of addresses returned.
352 *timed_out is set if we failed by timing out
353 ****************************************************************************/
355 struct in_addr *name_query(int fd,const char *name,int name_type,
356 BOOL bcast,BOOL recurse,
357 struct in_addr to_ip, int *count, int *flags,
362 int retry_time = bcast?250:2000;
364 struct packet_struct p;
365 struct packet_struct *p2;
366 struct nmb_packet *nmb = &p.packet.nmb;
367 struct in_addr *ip_list = NULL;
369 if (lp_disable_netbios()) {
370 DEBUG(5,("name_query(%s#%02x): netbios is disabled\n", name, name_type));
378 memset((char *)&p,'\0',sizeof(p));
382 nmb->header.name_trn_id = generate_trn_id();
383 nmb->header.opcode = 0;
384 nmb->header.response = False;
385 nmb->header.nm_flags.bcast = bcast;
386 nmb->header.nm_flags.recursion_available = False;
387 nmb->header.nm_flags.recursion_desired = recurse;
388 nmb->header.nm_flags.trunc = False;
389 nmb->header.nm_flags.authoritative = False;
390 nmb->header.rcode = 0;
391 nmb->header.qdcount = 1;
392 nmb->header.ancount = 0;
393 nmb->header.nscount = 0;
394 nmb->header.arcount = 0;
396 make_nmb_name(&nmb->question.question_name,name,name_type);
398 nmb->question.question_type = 0x20;
399 nmb->question.question_class = 0x1;
404 p.timestamp = time(NULL);
405 p.packet_type = NMB_PACKET;
409 if (!send_packet(&p))
415 struct timeval tval2;
416 struct in_addr *tmp_ip_list;
418 GetTimeOfDay(&tval2);
419 if (TvalDiff(&tval,&tval2) > retry_time) {
422 if (!found && !send_packet(&p))
428 if ((p2=receive_nmb_packet(fd,90,nmb->header.name_trn_id))) {
429 struct nmb_packet *nmb2 = &p2->packet.nmb;
430 debug_nmb_packet(p2);
432 /* If we get a Negative Name Query Response from a WINS
433 * server, we should report it and give up.
435 if( 0 == nmb2->header.opcode /* A query response */
436 && !(bcast) /* from a WINS server */
437 && nmb2->header.rcode /* Error returned */
440 if( DEBUGLVL( 3 ) ) {
441 /* Only executed if DEBUGLEVEL >= 3 */
442 dbgtext( "Negative name query response, rcode 0x%02x: ", nmb2->header.rcode );
443 switch( nmb2->header.rcode ) {
445 dbgtext( "Request was invalidly formatted.\n" );
448 dbgtext( "Problem with NBNS, cannot process name.\n");
451 dbgtext( "The name requested does not exist.\n" );
454 dbgtext( "Unsupported request error.\n" );
457 dbgtext( "Query refused error.\n" );
460 dbgtext( "Unrecognized error code.\n" );
468 if (nmb2->header.opcode != 0 ||
469 nmb2->header.nm_flags.bcast ||
470 nmb2->header.rcode ||
471 !nmb2->header.ancount) {
473 * XXXX what do we do with this? Could be a
474 * redirect, but we'll discard it for the
481 tmp_ip_list = (struct in_addr *)Realloc( ip_list, sizeof( ip_list[0] )
482 * ( (*count) + nmb2->answers->rdlength/6 ) );
485 DEBUG(0,("name_query: Realloc failed.\n"));
489 ip_list = tmp_ip_list;
492 DEBUG(2,("Got a positive name query response from %s ( ", inet_ntoa(p2->ip)));
493 for (i=0;i<nmb2->answers->rdlength/6;i++) {
494 putip((char *)&ip_list[(*count)],&nmb2->answers->rdata[2+i*6]);
495 DEBUGADD(2,("%s ",inet_ntoa(ip_list[(*count)])));
503 /* We add the flags back ... */
504 if (nmb2->header.response)
505 (*flags) |= NM_FLAGS_RS;
506 if (nmb2->header.nm_flags.authoritative)
507 (*flags) |= NM_FLAGS_AA;
508 if (nmb2->header.nm_flags.trunc)
509 (*flags) |= NM_FLAGS_TC;
510 if (nmb2->header.nm_flags.recursion_desired)
511 (*flags) |= NM_FLAGS_RD;
512 if (nmb2->header.nm_flags.recursion_available)
513 (*flags) |= NM_FLAGS_RA;
514 if (nmb2->header.nm_flags.bcast)
515 (*flags) |= NM_FLAGS_B;
518 * If we're doing a unicast lookup we only
519 * expect one reply. Don't wait the full 2
520 * seconds if we got one. JRA.
527 /* only set timed_out if we didn't fund what we where looking for*/
529 if ( !found && timed_out ) {
533 /* sort the ip list so we choose close servers first if possible */
534 sort_ip_list(ip_list, *count);
539 /********************************************************
540 Start parsing the lmhosts file.
541 *********************************************************/
543 XFILE *startlmhosts(char *fname)
545 XFILE *fp = x_fopen(fname,O_RDONLY, 0);
547 DEBUG(4,("startlmhosts: Can't open lmhosts file %s. Error was %s\n",
548 fname, strerror(errno)));
554 /********************************************************
555 Parse the next line in the lmhosts file.
556 *********************************************************/
558 BOOL getlmhostsent( XFILE *fp, pstring name, int *name_type, struct in_addr *ipaddr)
562 while(!x_feof(fp) && !x_ferror(fp)) {
563 pstring ip,flags,extra;
570 if (!fgets_slash(line,sizeof(pstring),fp)) {
584 if (next_token(&ptr,ip ,NULL,sizeof(ip)))
586 if (next_token(&ptr,name ,NULL, sizeof(pstring)))
588 if (next_token(&ptr,flags,NULL, sizeof(flags)))
590 if (next_token(&ptr,extra,NULL, sizeof(extra)))
596 if (count > 0 && count < 2) {
597 DEBUG(0,("getlmhostsent: Ill formed hosts line [%s]\n",line));
602 DEBUG(0,("getlmhostsent: too many columns in lmhosts file (obsolete syntax)\n"));
606 DEBUG(4, ("getlmhostsent: lmhost entry: %s %s %s\n", ip, name, flags));
608 if (strchr_m(flags,'G') || strchr_m(flags,'S')) {
609 DEBUG(0,("getlmhostsent: group flag in lmhosts ignored (obsolete)\n"));
613 *ipaddr = *interpret_addr2(ip);
615 /* Extra feature. If the name ends in '#XX', where XX is a hex number,
616 then only add that name type. */
617 if((ptr1 = strchr_m(name, '#')) != NULL) {
621 *name_type = (int)strtol(ptr1, &endptr, 16);
622 if(!*ptr1 || (endptr == ptr1)) {
623 DEBUG(0,("getlmhostsent: invalid name %s containing '#'.\n", name));
627 *(--ptr1) = '\0'; /* Truncate at the '#' */
636 /********************************************************
637 Finish parsing the lmhosts file.
638 *********************************************************/
640 void endlmhosts(XFILE *fp)
645 /********************************************************
646 convert an array if struct in_addrs to struct ip_service
647 return False on failure. Port is set to PORT_NONE;
648 *********************************************************/
650 static BOOL convert_ip2service( struct ip_service **return_iplist, struct in_addr *ip_list, int count )
654 if ( count==0 || !ip_list )
657 /* copy the ip address; port will be PORT_NONE */
658 if ( (*return_iplist = (struct ip_service*)malloc(count*sizeof(struct ip_service))) == NULL ) {
659 DEBUG(0,("convert_ip2service: malloc failed for %d enetries!\n", count ));
663 for ( i=0; i<count; i++ ) {
664 (*return_iplist)[i].ip = ip_list[i];
665 (*return_iplist)[i].port = PORT_NONE;
670 /********************************************************
671 Resolve via "bcast" method.
672 *********************************************************/
674 BOOL name_resolve_bcast(const char *name, int name_type,
675 struct ip_service **return_iplist, int *return_count)
678 int num_interfaces = iface_count();
679 struct in_addr *ip_list;
682 if (lp_disable_netbios()) {
683 DEBUG(5,("name_resolve_bcast(%s#%02x): netbios is disabled\n", name, name_type));
687 *return_iplist = NULL;
691 * "bcast" means do a broadcast lookup on all the local interfaces.
694 DEBUG(3,("name_resolve_bcast: Attempting broadcast lookup for name %s<0x%x>\n", name, name_type));
696 sock = open_socket_in( SOCK_DGRAM, 0, 3,
697 interpret_addr(lp_socket_address()), True );
699 if (sock == -1) return False;
701 set_socket_options(sock,"SO_BROADCAST");
703 * Lookup the name on all the interfaces, return on
704 * the first successful match.
706 for( i = num_interfaces-1; i >= 0; i--) {
707 struct in_addr sendto_ip;
709 /* Done this way to fix compiler error on IRIX 5.x */
710 sendto_ip = *iface_n_bcast(i);
711 ip_list = name_query(sock, name, name_type, True,
712 True, sendto_ip, return_count, &flags, NULL);
717 /* failed - no response */
724 if ( !convert_ip2service(return_iplist, ip_list, *return_count) )
727 SAFE_FREE( ip_list );
732 /********************************************************
733 Resolve via "wins" method.
734 *********************************************************/
736 BOOL resolve_wins(const char *name, int name_type,
737 struct ip_service **return_iplist, int *return_count)
741 struct in_addr src_ip, *ip_list = NULL;
744 if (lp_disable_netbios()) {
745 DEBUG(5,("resolve_wins(%s#%02x): netbios is disabled\n", name, name_type));
749 *return_iplist = NULL;
752 DEBUG(3,("resolve_wins: Attempting wins lookup for name %s<0x%x>\n", name, name_type));
754 if (wins_srv_count() < 1) {
755 DEBUG(3,("resolve_wins: WINS server resolution selected and no WINS servers listed.\n"));
759 /* we try a lookup on each of the WINS tags in turn */
760 wins_tags = wins_srv_tags();
763 /* huh? no tags?? give up in disgust */
767 /* the address we will be sending from */
768 src_ip = *interpret_addr2(lp_socket_address());
770 /* in the worst case we will try every wins server with every
772 for (t=0; wins_tags && wins_tags[t]; t++) {
773 int srv_count = wins_srv_count_tag(wins_tags[t]);
774 for (i=0; i<srv_count; i++) {
775 struct in_addr wins_ip;
779 wins_ip = wins_srv_ip_tag(wins_tags[t], src_ip);
781 if (global_in_nmbd && ismyip(wins_ip)) {
782 /* yikes! we'll loop forever */
786 /* skip any that have been unresponsive lately */
787 if (wins_srv_is_dead(wins_ip, src_ip)) {
791 DEBUG(3,("resolve_wins: using WINS server %s and tag '%s'\n", inet_ntoa(wins_ip), wins_tags[t]));
793 sock = open_socket_in(SOCK_DGRAM, 0, 3, src_ip.s_addr, True);
798 ip_list = name_query(sock,name,name_type, False,
799 True, wins_ip, return_count, &flags,
802 /* exit loop if we got a list of addresses */
810 /* Timed out wating for WINS server to respond. Mark it dead. */
811 wins_srv_died(wins_ip, src_ip);
813 /* The name definately isn't in this
814 group of WINS servers. goto the next group */
820 wins_srv_tags_free(wins_tags);
825 if ( !convert_ip2service( return_iplist, ip_list, *return_count ) )
828 SAFE_FREE( ip_list );
829 wins_srv_tags_free(wins_tags);
835 /********************************************************
836 Resolve via "lmhosts" method.
837 *********************************************************/
839 static BOOL resolve_lmhosts(const char *name, int name_type,
840 struct ip_service **return_iplist, int *return_count)
843 * "lmhosts" means parse the local lmhosts file.
849 struct in_addr return_ip;
851 *return_iplist = NULL;
854 DEBUG(3,("resolve_lmhosts: Attempting lmhosts lookup for name %s<0x%x>\n", name, name_type));
856 fp = startlmhosts(dyn_LMHOSTSFILE);
858 while (getlmhostsent(fp, lmhost_name, &name_type2,
861 if (!strequal(name, lmhost_name))
864 if ((name_type2 != -1) && (name_type != name_type2))
867 *return_iplist = (struct ip_service *)
868 realloc((*return_iplist),
869 sizeof(struct ip_service) *
870 ((*return_count)+1));
872 if ((*return_iplist) == NULL) {
873 DEBUG(3,("resolve_lmhosts: malloc fail !\n"));
877 (*return_iplist)[*return_count].ip = return_ip;
878 (*return_iplist)[*return_count].port = PORT_NONE;
881 /* Multiple names only for DC lookup */
882 if (name_type != 0x1c)
892 /********************************************************
893 Resolve via "hosts" method.
894 *********************************************************/
896 static BOOL resolve_hosts(const char *name, int name_type,
897 struct ip_service **return_iplist, int *return_count)
900 * "host" means do a localhost, or dns lookup.
904 if ( name_type != 0x20 && name_type != 0x0) {
905 DEBUG(5, ("resolve_hosts: not appropriate for name type <0x%x>\n", name_type));
909 *return_iplist = NULL;
912 DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n", name, name_type));
914 if (((hp = sys_gethostbyname(name)) != NULL) && (hp->h_addr != NULL)) {
915 struct in_addr return_ip;
916 putip((char *)&return_ip,(char *)hp->h_addr);
917 *return_iplist = (struct ip_service *)malloc(sizeof(struct ip_service));
918 if(*return_iplist == NULL) {
919 DEBUG(3,("resolve_hosts: malloc fail !\n"));
922 (*return_iplist)->ip = return_ip;
923 (*return_iplist)->port = PORT_NONE;
930 /********************************************************
931 Resolve via "ADS" method.
932 *********************************************************/
934 static BOOL resolve_ads(const char *name, int name_type,
935 struct ip_service **return_iplist, int *return_count)
939 if ( name_type == 0x1c ) {
945 /* try to lookup the _ldap._tcp.<domain> if we are using ADS */
946 if ( lp_security() != SEC_ADS )
949 DEBUG(5,("resolve_hosts: Attempting to resolve DC's for %s using DNS\n",
952 if (ldap_domain2hostlist(name, &list) != LDAP_SUCCESS)
955 count = count_chars(list, ' ') + 1;
956 if ( (*return_iplist = malloc(count * sizeof(struct ip_service))) == NULL ) {
957 DEBUG(0,("resolve_hosts: malloc failed for %d entries\n", count ));
962 while (next_token(&ptr, tok, " ", sizeof(tok))) {
963 unsigned port = LDAP_PORT;
964 char *p = strchr(tok, ':');
969 (*return_iplist)[i].ip = *interpret_addr2(tok);
970 (*return_iplist)[i].port = port;
972 /* make sure it is a valid IP. I considered checking the negative
973 connection cache, but this is the wrong place for it. Maybe only
974 as a hac. After think about it, if all of the IP addresses retuend
975 from DNS are dead, what hope does a netbios name lookup have?
976 The standard reason for falling back to netbios lookups is that
977 our DNS server doesn't know anything about the DC's -- jerry */
979 if ( is_zero_ip((*return_iplist)[i].ip) )
990 #endif /* HAVE_ADS */
996 /*******************************************************************
997 Internal interface to resolve a name into an IP address.
998 Use this function if the string is either an IP address, DNS
999 or host name or NetBIOS name. This uses the name switch in the
1000 smb.conf to determine the order of name resolution.
1002 Added support for ip addr/port to support ADS ldap servers.
1003 the only place we currently care about the port is in the
1004 resolve_hosts() when looking up DC's via SRV RR entries in DNS
1005 **********************************************************************/
1007 static BOOL internal_resolve_name(const char *name, int name_type,
1008 struct ip_service **return_iplist,
1009 int *return_count, const char *resolve_order)
1011 pstring name_resolve_list;
1014 BOOL allones = (strcmp(name,"255.255.255.255") == 0);
1015 BOOL allzeros = (strcmp(name,"0.0.0.0") == 0);
1016 BOOL is_address = is_ipaddress(name);
1017 BOOL result = False;
1020 *return_iplist = NULL;
1023 DEBUG(10, ("internal_resolve_name: looking up %s#%x\n", name, name_type));
1025 if (allzeros || allones || is_address) {
1027 if ( (*return_iplist = (struct ip_service *)malloc(sizeof(struct ip_service))) == NULL ) {
1028 DEBUG(0,("internal_resolve_name: malloc fail !\n"));
1033 /* ignore the port here */
1034 (*return_iplist)->port = PORT_NONE;
1036 /* if it's in the form of an IP address then get the lib to interpret it */
1037 if (((*return_iplist)->ip.s_addr = inet_addr(name)) == 0xFFFFFFFF ){
1038 DEBUG(1,("internal_resolve_name: inet_addr failed on %s\n", name));
1042 (*return_iplist)->ip.s_addr = allones ? 0xFFFFFFFF : 0;
1048 /* Check name cache */
1050 if (namecache_fetch(name, name_type, return_iplist, return_count)) {
1051 /* This could be a negative response */
1052 return (*return_count > 0);
1055 /* set the name resolution order */
1057 if ( strcmp( resolve_order, "NULL") == 0 ) {
1058 DEBUG(8,("internal_resolve_name: all lookups disabled\n"));
1062 if ( !resolve_order ) {
1063 pstrcpy(name_resolve_list, lp_name_resolve_order());
1065 pstrcpy(name_resolve_list, resolve_order);
1067 if ( !name_resolve_list[0] ) {
1070 ptr = name_resolve_list;
1073 /* iterate through the name resolution backends */
1075 while (next_token(&ptr, tok, LIST_SEP, sizeof(tok))) {
1076 if((strequal(tok, "host") || strequal(tok, "hosts"))) {
1077 if (resolve_hosts(name, name_type, return_iplist, return_count)) {
1081 } else if(strequal( tok, "ads")) {
1082 /* deal with 0x1c names here. This will result in a
1083 SRV record lookup for _ldap._tcp.<domain> if we
1084 are using 'security = ads' */
1085 if (resolve_ads(name, name_type, return_iplist, return_count)) {
1089 } else if(strequal( tok, "lmhosts")) {
1090 if (resolve_lmhosts(name, name_type, return_iplist, return_count)) {
1094 } else if(strequal( tok, "wins")) {
1095 /* don't resolve 1D via WINS */
1096 if (name_type != 0x1D && resolve_wins(name, name_type, return_iplist, return_count)) {
1100 } else if(strequal( tok, "bcast")) {
1101 if (name_resolve_bcast(name, name_type, return_iplist, return_count)) {
1106 DEBUG(0,("resolve_name: unknown name switch type %s\n", tok));
1110 /* All of the resolve_* functions above have returned false. */
1112 SAFE_FREE(*return_iplist);
1119 /* Remove duplicate entries. Some queries, notably #1c (domain
1120 controllers) return the PDC in iplist[0] and then all domain
1121 controllers including the PDC in iplist[1..n]. Iterating over
1122 the iplist when the PDC is down will cause two sets of timeouts. */
1124 if ( *return_count ) {
1125 *return_count = remove_duplicate_addrs2( *return_iplist, *return_count );
1128 /* Save in name cache */
1129 if ( DEBUGLEVEL >= 100 ) {
1130 for (i = 0; i < *return_count && DEBUGLEVEL == 100; i++)
1131 DEBUG(100, ("Storing name %s of type %d (%s:%d)\n", name,
1132 name_type, inet_ntoa((*return_iplist)[i].ip), (*return_iplist)[i].port));
1135 namecache_store(name, name_type, *return_count, *return_iplist);
1137 /* Display some debugging info */
1139 if ( DEBUGLEVEL >= 10 ) {
1140 DEBUG(10, ("internal_resolve_name: returning %d addresses: ", *return_count));
1142 for (i = 0; i < *return_count; i++) {
1143 DEBUGADD(10, ("%s:%d ", inet_ntoa((*return_iplist)[i].ip), (*return_iplist)[i].port));
1152 /********************************************************
1153 Internal interface to resolve a name into one IP address.
1154 Use this function if the string is either an IP address, DNS
1155 or host name or NetBIOS name. This uses the name switch in the
1156 smb.conf to determine the order of name resolution.
1157 *********************************************************/
1159 BOOL resolve_name(const char *name, struct in_addr *return_ip, int name_type)
1161 struct ip_service *ip_list = NULL;
1164 if (is_ipaddress(name)) {
1165 *return_ip = *interpret_addr2(name);
1169 if (internal_resolve_name(name, name_type, &ip_list, &count, lp_name_resolve_order())) {
1172 /* only return valid addresses for TCP connections */
1173 for (i=0; i<count; i++) {
1174 char *ip_str = inet_ntoa(ip_list[i].ip);
1176 strcmp(ip_str, "255.255.255.255") != 0 &&
1177 strcmp(ip_str, "0.0.0.0") != 0)
1179 *return_ip = ip_list[i].ip;
1190 /********************************************************
1191 Find the IP address of the master browser or DMB for a workgroup.
1192 *********************************************************/
1194 BOOL find_master_ip(const char *group, struct in_addr *master_ip)
1196 struct ip_service *ip_list = NULL;
1199 if (lp_disable_netbios()) {
1200 DEBUG(5,("find_master_ip(%s): netbios is disabled\n", group));
1204 if (internal_resolve_name(group, 0x1D, &ip_list, &count, lp_name_resolve_order())) {
1205 *master_ip = ip_list[0].ip;
1209 if(internal_resolve_name(group, 0x1B, &ip_list, &count, lp_name_resolve_order())) {
1210 *master_ip = ip_list[0].ip;
1219 /********************************************************
1220 Get the IP address list of the primary domain controller
1222 *********************************************************/
1224 BOOL get_pdc_ip(const char *domain, struct in_addr *ip)
1226 struct ip_service *ip_list;
1229 /* Look up #1B name */
1231 if (!internal_resolve_name(domain, 0x1b, &ip_list, &count, lp_name_resolve_order())) {
1235 /* if we get more than 1 IP back we have to assume it is a
1236 multi-homed PDC and not a mess up */
1239 DEBUG(6,("get_pdc_ip: PDC has %d IP addresses!\n", count));
1240 sort_ip_list2( ip_list, count );
1243 *ip = ip_list[0].ip;
1250 /********************************************************
1251 Get the IP address list of the domain controllers for
1253 *********************************************************/
1255 static BOOL get_dc_list(const char *domain, struct ip_service **ip_list,
1256 int *count, BOOL ads_only, int *ordered)
1258 fstring resolve_order;
1260 /* if we are restricted to solely using DNS for looking
1261 up a domain controller, make sure that host lookups
1262 are enabled for the 'name resolve order'. If host lookups
1263 are disabled and ads_only is True, then set the string to
1266 fstrcpy( resolve_order, lp_name_resolve_order() );
1267 strlower_m( resolve_order );
1269 if ( strstr( resolve_order, "host" ) )
1270 fstrcpy( resolve_order, "ads" );
1272 fstrcpy( resolve_order, "NULL" );
1278 /* If it's our domain then use the 'password server' parameter. */
1280 if ( strequal(domain, lp_workgroup()) || strequal(domain, lp_realm()) ) {
1282 char *pserver = lp_passwordserver(); /* UNIX charset. */
1286 int num_addresses = 0;
1287 int local_count, i, j;
1288 struct ip_service *return_iplist = NULL;
1289 struct ip_service *auto_ip_list = NULL;
1290 BOOL done_auto_lookup = False;
1295 return internal_resolve_name(domain, 0x1C, ip_list, count, resolve_order);
1300 * if '*' appears in the "password server" list then add
1301 * an auto lookup to the list of manually configured
1302 * DC's. If any DC is listed by name, then the list should be
1303 * considered to be ordered
1306 while (next_token(&p,name,LIST_SEP,sizeof(name))) {
1307 if (strequal(name, "*")) {
1308 if ( internal_resolve_name(domain, 0x1C, &auto_ip_list, &auto_count, resolve_order) )
1309 num_addresses += auto_count;
1310 done_auto_lookup = True;
1311 DEBUG(8,("Adding %d DC's from auto lookup\n", auto_count));
1317 /* if we have no addresses and haven't done the auto lookup, then
1318 just return the list of DC's */
1320 if ( (num_addresses == 0) && !done_auto_lookup ) {
1321 return internal_resolve_name(domain, 0x1C, ip_list, count, resolve_order);
1324 /* maybe we just failed? */
1326 if ( num_addresses == 0 ) {
1327 DEBUG(4,("get_dc_list: no servers found\n"));
1331 if ( (return_iplist = (struct ip_service *)
1332 malloc(num_addresses * sizeof(struct ip_service))) == NULL ) {
1333 DEBUG(3,("get_dc_list: malloc fail !\n"));
1340 /* fill in the return list now with real IP's */
1342 while ( (local_count<num_addresses) && next_token(&p,name,LIST_SEP,sizeof(name)) ) {
1343 struct in_addr name_ip;
1345 /* copy any addersses from the auto lookup */
1347 if ( strequal(name, "*") ) {
1348 for ( j=0; j<auto_count; j++ ) {
1349 /* Check for and don't copy any known bad DC IP's. */
1350 if(!NT_STATUS_IS_OK(check_negative_conn_cache(domain,
1351 inet_ntoa(auto_ip_list[j].ip)))) {
1352 DEBUG(5,("get_dc_list: negative entry %s removed from DC list\n",
1353 inet_ntoa(auto_ip_list[j].ip) ));
1356 return_iplist[local_count].ip = auto_ip_list[j].ip;
1357 return_iplist[local_count].port = auto_ip_list[j].port;
1364 /* added support for address:port syntax for ads (not that I think
1365 anyone will ever run the LDAP server in an AD domain on something
1366 other than port 389 */
1368 port = (lp_security() == SEC_ADS) ? LDAP_PORT : PORT_NONE;
1369 if ( (port_str=strchr(name, ':')) != NULL ) {
1372 port = atoi( port_str );
1375 /* explicit lookup; resolve_name() will handle names & IP addresses */
1376 if ( resolve_name( name, &name_ip, 0x20 ) ) {
1378 /* Check for and don't copy any known bad DC IP's. */
1379 if( !NT_STATUS_IS_OK(check_negative_conn_cache(domain, inet_ntoa(name_ip))) ) {
1380 DEBUG(5,("get_dc_list: negative entry %s removed from DC list\n",name ));
1384 return_iplist[local_count].ip = name_ip;
1385 return_iplist[local_count].port = port;
1391 SAFE_FREE(auto_ip_list);
1393 /* need to remove duplicates in the list if we have any
1394 explicit password servers */
1396 if ( local_count ) {
1397 local_count = remove_duplicate_addrs2( return_iplist, local_count );
1400 if ( DEBUGLEVEL >= 4 ) {
1401 DEBUG(4,("get_dc_list: returning %d ip addresses in an %sordered list\n", local_count,
1402 *ordered ? "":"un"));
1403 DEBUG(4,("get_dc_list: "));
1404 for ( i=0; i<local_count; i++ )
1405 DEBUGADD(4,("%s:%d ", inet_ntoa(return_iplist[i].ip), return_iplist[i].port ));
1409 *ip_list = return_iplist;
1410 *count = local_count;
1412 return (*count != 0);
1415 DEBUG(10,("get_dc_list: defaulting to internal auto lookup for domain %s\n", domain));
1417 return internal_resolve_name(domain, 0x1C, ip_list, count, resolve_order);
1420 /*********************************************************************
1421 Small wrapper function to get the DC list and sort it if neccessary.
1422 *********************************************************************/
1424 BOOL get_sorted_dc_list( const char *domain, struct ip_service **ip_list, int *count, BOOL ads_only )
1428 DEBUG(8,("get_sorted_dc_list: attempting lookup using [%s]\n",
1429 (ads_only ? "ads" : lp_name_resolve_order())));
1431 if ( !get_dc_list(domain, ip_list, count, ads_only, &ordered) ) {
1435 /* only sort if we don't already have an ordered list */
1437 sort_ip_list2( *ip_list, *count );