1/*
2   Unix SMB/CIFS implementation.
3   name query routines
4   Copyright (C) Andrew Tridgell 1994-1998
5
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.
10
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.
15
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.
19
20*/
21
22#include "includes.h"
23
24/* nmbd.c sets this to True. */
25BOOL global_in_nmbd = False;
26
27/****************************************************************************
28 Generate a random trn_id.
29****************************************************************************/
30
31static int generate_trn_id(void)
32{
33	static int trn_id;
34
35	if (trn_id == 0) {
36		sys_srandom(sys_getpid());
37	}
38
39	trn_id = sys_random();
40
41	return trn_id % (unsigned)0x7FFF;
42}
43
44/****************************************************************************
45 Parse a node status response into an array of structures.
46****************************************************************************/
47
48static struct node_status *parse_node_status(char *p, int *num_names, struct node_status_extra *extra)
49{
50	struct node_status *ret;
51	int i;
52
53	*num_names = CVAL(p,0);
54
55	if (*num_names == 0)
56		return NULL;
57
58	ret = SMB_MALLOC_ARRAY(struct node_status,*num_names);
59	if (!ret)
60		return NULL;
61
62	p++;
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);
67		ret[i].flags = p[16];
68		p += 18;
69		DEBUG(10, ("%s#%02x: flags = 0x%02x\n", ret[i].name,
70			   ret[i].type, ret[i].flags));
71	}
72	/*
73	 * Also, pick up the MAC address ...
74	 */
75	if (extra) {
76		memcpy(&extra->mac_addr, p, 6); /* Fill in the mac addr */
77	}
78	return ret;
79}
80
81
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**************************************************************************/
86
87struct 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)
90{
91	BOOL found=False;
92	int retries = 2;
93	int retry_time = 2000;
94	struct timeval tval;
95	struct packet_struct p;
96	struct packet_struct *p2;
97	struct nmb_packet *nmb = &p.packet.nmb;
98	struct node_status *ret;
99
100	ZERO_STRUCT(p);
101
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;
118
119	p.ip = to_ip;
120	p.port = NMB_PORT;
121	p.fd = fd;
122	p.timestamp = time(NULL);
123	p.packet_type = NMB_PACKET;
124
125	GetTimeOfDay(&tval);
126
127	if (!send_packet(&p))
128		return NULL;
129
130	retries--;
131
132	while (1) {
133		struct timeval tval2;
134		GetTimeOfDay(&tval2);
135		if (TvalDiff(&tval,&tval2) > retry_time) {
136			if (!retries)
137				break;
138			if (!found && !send_packet(&p))
139				return NULL;
140			GetTimeOfDay(&tval);
141			retries--;
142		}
143
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);
147
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
155				   moment */
156				free_packet(p2);
157				continue;
158			}
159
160			ret = parse_node_status(&nmb2->answers->rdata[0], num_names, extra);
161			free_packet(p2);
162			return ret;
163		}
164	}
165
166	return NULL;
167}
168
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**************************************************************************/
173
174BOOL name_status_find(const char *q_name, int q_type, int type, struct in_addr to_ip, fstring name)
175{
176	struct node_status *status = NULL;
177	struct nmb_name nname;
178	int count, i;
179	int sock;
180	BOOL result = False;
181
182	if (lp_disable_netbios()) {
183		DEBUG(5,("name_status_find(%s#%02x): netbios is disabled\n", q_name, q_type));
184		return False;
185	}
186
187	DEBUG(10, ("name_status_find: looking up %s#%02x at %s\n", q_name,
188		   q_type, inet_ntoa(to_ip)));
189
190	/* Check the cache first. */
191
192	if (namecache_status_fetch(q_name, q_type, type, to_ip, name))
193		return True;
194
195	sock = open_socket_in(SOCK_DGRAM, 0, 3, interpret_addr(lp_socket_address()), True);
196	if (sock == -1)
197		goto done;
198
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);
202	close(sock);
203	if (!status)
204		goto done;
205
206	for (i=0;i<count;i++) {
207		if (status[i].type == type)
208			break;
209	}
210	if (i == count)
211		goto done;
212
213	pull_ascii_nstring(name, sizeof(fstring), status[i].name);
214
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 */
218
219	if ( q_type != 0x1c )
220		namecache_status_store(q_name, q_type, type, to_ip, name);
221
222	result = True;
223
224 done:
225	SAFE_FREE(status);
226
227	DEBUG(10, ("name_status_find: name %sfound", result ? "" : "not "));
228
229	if (result)
230		DEBUGADD(10, (", name %s ip address is %s", name, inet_ntoa(to_ip)));
231
232	DEBUG(10, ("\n"));
233
234	return result;
235}
236
237/*
238  comparison function used by sort_ip_list
239*/
240
241static int ip_compare(struct in_addr *ip1, struct in_addr *ip2)
242{
243	int max_bits1=0, max_bits2=0;
244	int num_interfaces = iface_count();
245	int i;
246
247	for (i=0;i<num_interfaces;i++) {
248		struct in_addr ip;
249		int bits1, bits2;
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);
255	}
256
257	/* bias towards directly reachable IPs */
258	if (iface_local(*ip1)) {
259		max_bits1 += 32;
260	}
261	if (iface_local(*ip2)) {
262		max_bits2 += 32;
263	}
264
265	return max_bits2 - max_bits1;
266}
267
268/*******************************************************************
269 compare 2 ldap IPs by nearness to our interfaces - used in qsort
270*******************************************************************/
271
272static int ip_service_compare(struct ip_service *ip1, struct ip_service *ip2)
273{
274	int result;
275
276	if ( (result = ip_compare(&ip1->ip, &ip2->ip)) != 0 )
277		return result;
278
279	if ( ip1->port > ip2->port )
280		return 1;
281
282	if ( ip1->port < ip2->port )
283		return -1;
284
285	return 0;
286}
287
288/*
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
292*/
293
294static void sort_ip_list(struct in_addr *iplist, int count)
295{
296	if (count <= 1) {
297		return;
298	}
299
300	qsort(iplist, count, sizeof(struct in_addr), QSORT_CAST ip_compare);
301}
302
303static void sort_ip_list2(struct ip_service *iplist, int count)
304{
305	if (count <= 1) {
306		return;
307	}
308
309	qsort(iplist, count, sizeof(struct ip_service), QSORT_CAST ip_service_compare);
310}
311
312/**********************************************************************
313 Remove any duplicate address/port pairs in the list
314 *********************************************************************/
315
316static int remove_duplicate_addrs2( struct ip_service *iplist, int count )
317{
318	int i, j;
319
320	DEBUG(10,("remove_duplicate_addrs2: looking for duplicate address/port pairs\n"));
321
322	/* one loop to remove duplicates */
323	for ( i=0; i<count; i++ ) {
324		if ( is_zero_ip(iplist[i].ip) )
325			continue;
326
327		for ( j=i+1; j<count; j++ ) {
328			if ( ip_service_equal(iplist[i], iplist[j]) )
329				zero_ip(&iplist[j].ip);
330		}
331	}
332
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) ) {
337			if (i != count-1 )
338				memmove(&iplist[i], &iplist[i+1], (count - i - 1)*sizeof(iplist[i]));
339			count--;
340			continue;
341		}
342		i++;
343	}
344
345	return count;
346}
347
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****************************************************************************/
354
355struct 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,
358			   BOOL *timed_out)
359{
360	BOOL found=False;
361	int i, retries = 3;
362	int retry_time = bcast?250:2000;
363	struct timeval tval;
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;
368
369	if (lp_disable_netbios()) {
370		DEBUG(5,("name_query(%s#%02x): netbios is disabled\n", name, name_type));
371		return NULL;
372	}
373
374	if (timed_out) {
375		*timed_out = False;
376	}
377
378	memset((char *)&p,'\0',sizeof(p));
379	(*count) = 0;
380	(*flags) = 0;
381
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;
395
396	make_nmb_name(&nmb->question.question_name,name,name_type);
397
398	nmb->question.question_type = 0x20;
399	nmb->question.question_class = 0x1;
400
401	p.ip = to_ip;
402	p.port = NMB_PORT;
403	p.fd = fd;
404	p.timestamp = time(NULL);
405	p.packet_type = NMB_PACKET;
406
407	GetTimeOfDay(&tval);
408
409	if (!send_packet(&p))
410		return NULL;
411
412	retries--;
413
414	while (1) {
415		struct timeval tval2;
416		struct in_addr *tmp_ip_list;
417
418		GetTimeOfDay(&tval2);
419		if (TvalDiff(&tval,&tval2) > retry_time) {
420			if (!retries)
421				break;
422			if (!found && !send_packet(&p))
423				return NULL;
424			GetTimeOfDay(&tval);
425			retries--;
426		}
427
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);
431
432			/* If we get a Negative Name Query Response from a WINS
433			 * server, we should report it and give up.
434			 */
435			if( 0 == nmb2->header.opcode		/* A query response   */
436			    && !(bcast)			/* from a WINS server */
437			    && nmb2->header.rcode		/* Error returned     */
438				) {
439
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 ) {
444					case 0x01:
445						dbgtext( "Request was invalidly formatted.\n" );
446						break;
447					case 0x02:
448						dbgtext( "Problem with NBNS, cannot process name.\n");
449						break;
450					case 0x03:
451						dbgtext( "The name requested does not exist.\n" );
452						break;
453					case 0x04:
454						dbgtext( "Unsupported request error.\n" );
455						break;
456					case 0x05:
457						dbgtext( "Query refused error.\n" );
458						break;
459					default:
460						dbgtext( "Unrecognized error code.\n" );
461						break;
462					}
463				}
464				free_packet(p2);
465				return( NULL );
466			}
467
468			if (nmb2->header.opcode != 0 ||
469			    nmb2->header.nm_flags.bcast ||
470			    nmb2->header.rcode ||
471			    !nmb2->header.ancount) {
472				/*
473				 * XXXX what do we do with this? Could be a
474				 * redirect, but we'll discard it for the
475				 * moment.
476				 */
477				free_packet(p2);
478				continue;
479			}
480
481			tmp_ip_list = SMB_REALLOC_ARRAY( ip_list, struct in_addr,
482						(*count) + nmb2->answers->rdlength/6 );
483
484			if (!tmp_ip_list) {
485				DEBUG(0,("name_query: Realloc failed.\n"));
486				SAFE_FREE(ip_list);
487			}
488
489			ip_list = tmp_ip_list;
490
491			if (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)])));
496					(*count)++;
497				}
498				DEBUGADD(2,(")\n"));
499			}
500
501			found=True;
502			retries=0;
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;
516			free_packet(p2);
517			/*
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.
521			 */
522			if(!bcast && found)
523				break;
524		}
525	}
526
527	/* only set timed_out if we didn't fund what we where looking for*/
528
529	if ( !found && timed_out ) {
530		*timed_out = True;
531	}
532
533	/* sort the ip list so we choose close servers first if possible */
534	sort_ip_list(ip_list, *count);
535
536	return ip_list;
537}
538
539/********************************************************
540 Start parsing the lmhosts file.
541*********************************************************/
542
543XFILE *startlmhosts(char *fname)
544{
545	XFILE *fp = x_fopen(fname,O_RDONLY, 0);
546	if (!fp) {
547		DEBUG(4,("startlmhosts: Can't open lmhosts file %s. Error was %s\n",
548			 fname, strerror(errno)));
549		return NULL;
550	}
551	return fp;
552}
553
554/********************************************************
555 Parse the next line in the lmhosts file.
556*********************************************************/
557
558BOOL getlmhostsent( XFILE *fp, pstring name, int *name_type, struct in_addr *ipaddr)
559{
560	pstring line;
561
562	while(!x_feof(fp) && !x_ferror(fp)) {
563		pstring ip,flags,extra;
564		const char *ptr;
565		char *ptr1;
566		int count = 0;
567
568		*name_type = -1;
569
570		if (!fgets_slash(line,sizeof(pstring),fp)) {
571			continue;
572		}
573
574		if (*line == '#') {
575			continue;
576		}
577
578		pstrcpy(ip,"");
579		pstrcpy(name,"");
580		pstrcpy(flags,"");
581
582		ptr = line;
583
584		if (next_token(&ptr,ip   ,NULL,sizeof(ip)))
585			++count;
586		if (next_token(&ptr,name ,NULL, sizeof(pstring)))
587			++count;
588		if (next_token(&ptr,flags,NULL, sizeof(flags)))
589			++count;
590		if (next_token(&ptr,extra,NULL, sizeof(extra)))
591			++count;
592
593		if (count <= 0)
594			continue;
595
596		if (count > 0 && count < 2) {
597			DEBUG(0,("getlmhostsent: Ill formed hosts line [%s]\n",line));
598			continue;
599		}
600
601		if (count >= 4) {
602			DEBUG(0,("getlmhostsent: too many columns in lmhosts file (obsolete syntax)\n"));
603			continue;
604		}
605
606		DEBUG(4, ("getlmhostsent: lmhost entry: %s %s %s\n", ip, name, flags));
607
608		if (strchr_m(flags,'G') || strchr_m(flags,'S')) {
609			DEBUG(0,("getlmhostsent: group flag in lmhosts ignored (obsolete)\n"));
610			continue;
611		}
612
613		*ipaddr = *interpret_addr2(ip);
614
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) {
618			char *endptr;
619      			ptr1++;
620
621			*name_type = (int)strtol(ptr1, &endptr, 16);
622			if(!*ptr1 || (endptr == ptr1)) {
623				DEBUG(0,("getlmhostsent: invalid name %s containing '#'.\n", name));
624				continue;
625			}
626
627			*(--ptr1) = '\0'; /* Truncate at the '#' */
628		}
629
630		return True;
631	}
632
633	return False;
634}
635
636/********************************************************
637 Finish parsing the lmhosts file.
638*********************************************************/
639
640void endlmhosts(XFILE *fp)
641{
642	x_fclose(fp);
643}
644
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*********************************************************/
649
650static BOOL convert_ip2service( struct ip_service **return_iplist, struct in_addr *ip_list, int count )
651{
652	int i;
653
654	if ( count==0 || !ip_list )
655		return False;
656
657	/* copy the ip address; port will be PORT_NONE */
658	if ( (*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, count)) == NULL ) {
659		DEBUG(0,("convert_ip2service: malloc failed for %d enetries!\n", count ));
660		return False;
661	}
662
663	for ( i=0; i<count; i++ ) {
664		(*return_iplist)[i].ip   = ip_list[i];
665		(*return_iplist)[i].port = PORT_NONE;
666	}
667
668	return True;
669}
670/********************************************************
671 Resolve via "bcast" method.
672*********************************************************/
673
674BOOL name_resolve_bcast(const char *name, int name_type,
675			struct ip_service **return_iplist, int *return_count)
676{
677	int sock, i;
678	int num_interfaces = iface_count();
679	struct in_addr *ip_list;
680	BOOL ret;
681
682	if (lp_disable_netbios()) {
683		DEBUG(5,("name_resolve_bcast(%s#%02x): netbios is disabled\n", name, name_type));
684		return False;
685	}
686
687	*return_iplist = NULL;
688	*return_count = 0;
689
690	/*
691	 * "bcast" means do a broadcast lookup on all the local interfaces.
692	 */
693
694	DEBUG(3,("name_resolve_bcast: Attempting broadcast lookup for name %s<0x%x>\n", name, name_type));
695
696	sock = open_socket_in( SOCK_DGRAM, 0, 3,
697			       interpret_addr(lp_socket_address()), True );
698
699	if (sock == -1) return False;
700
701	set_socket_options(sock,"SO_BROADCAST");
702	/*
703	 * Lookup the name on all the interfaces, return on
704	 * the first successful match.
705	 */
706	for( i = num_interfaces-1; i >= 0; i--) {
707		struct in_addr sendto_ip;
708		int flags;
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);
713		if( ip_list )
714			goto success;
715	}
716
717	/* failed - no response */
718
719	close(sock);
720	return False;
721
722success:
723	ret = True;
724	if ( !convert_ip2service(return_iplist, ip_list, *return_count) )
725		ret = False;
726
727	SAFE_FREE( ip_list );
728	close(sock);
729	return ret;
730}
731
732/********************************************************
733 Resolve via "wins" method.
734*********************************************************/
735
736BOOL resolve_wins(const char *name, int name_type,
737		  struct ip_service **return_iplist, int *return_count)
738{
739	int sock, t, i;
740	char **wins_tags;
741	struct in_addr src_ip, *ip_list = NULL;
742	BOOL ret;
743
744	if (lp_disable_netbios()) {
745		DEBUG(5,("resolve_wins(%s#%02x): netbios is disabled\n", name, name_type));
746		return False;
747	}
748
749	*return_iplist = NULL;
750	*return_count = 0;
751
752	DEBUG(3,("resolve_wins: Attempting wins lookup for name %s<0x%x>\n", name, name_type));
753
754	if (wins_srv_count() < 1) {
755		DEBUG(3,("resolve_wins: WINS server resolution selected and no WINS servers listed.\n"));
756		return False;
757	}
758
759	/* we try a lookup on each of the WINS tags in turn */
760	wins_tags = wins_srv_tags();
761
762	if (!wins_tags) {
763		/* huh? no tags?? give up in disgust */
764		return False;
765	}
766
767	/* the address we will be sending from */
768	src_ip = *interpret_addr2(lp_socket_address());
769
770	/* in the worst case we will try every wins server with every
771	   tag! */
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;
776			int flags;
777			BOOL timed_out;
778
779			wins_ip = wins_srv_ip_tag(wins_tags[t], src_ip);
780
781			if (global_in_nmbd && ismyip(wins_ip)) {
782				/* yikes! we'll loop forever */
783				continue;
784			}
785
786			/* skip any that have been unresponsive lately */
787			if (wins_srv_is_dead(wins_ip, src_ip)) {
788				continue;
789			}
790
791			DEBUG(3,("resolve_wins: using WINS server %s and tag '%s'\n", inet_ntoa(wins_ip), wins_tags[t]));
792
793			sock = open_socket_in(SOCK_DGRAM, 0, 3, src_ip.s_addr, True);
794			if (sock == -1) {
795				continue;
796			}
797
798			ip_list = name_query(sock,name,name_type, False,
799						    True, wins_ip, return_count, &flags,
800						    &timed_out);
801
802			/* exit loop if we got a list of addresses */
803
804			if ( ip_list )
805				goto success;
806
807			close(sock);
808
809			if (timed_out) {
810				/* Timed out wating for WINS server to respond.  Mark it dead. */
811				wins_srv_died(wins_ip, src_ip);
812			} else {
813				/* The name definately isn't in this
814				   group of WINS servers. goto the next group  */
815				break;
816			}
817		}
818	}
819
820	wins_srv_tags_free(wins_tags);
821	return False;
822
823success:
824	ret = True;
825	if ( !convert_ip2service( return_iplist, ip_list, *return_count ) )
826		ret = False;
827
828	SAFE_FREE( ip_list );
829	wins_srv_tags_free(wins_tags);
830	close(sock);
831
832	return ret;
833}
834
835/********************************************************
836 Resolve via "lmhosts" method.
837*********************************************************/
838
839static BOOL resolve_lmhosts(const char *name, int name_type,
840                         struct ip_service **return_iplist, int *return_count)
841{
842	/*
843	 * "lmhosts" means parse the local lmhosts file.
844	 */
845
846	XFILE *fp;
847	pstring lmhost_name;
848	int name_type2;
849	struct in_addr return_ip;
850	BOOL result = False;
851
852	*return_iplist = NULL;
853	*return_count = 0;
854
855	DEBUG(3,("resolve_lmhosts: Attempting lmhosts lookup for name %s<0x%x>\n", name, name_type));
856
857	fp = startlmhosts(dyn_LMHOSTSFILE);
858
859	if ( fp == NULL )
860		return False;
861
862	while (getlmhostsent(fp, lmhost_name, &name_type2, &return_ip))
863	{
864
865		if (!strequal(name, lmhost_name))
866			continue;
867
868		if ((name_type2 != -1) && (name_type != name_type2))
869			continue;
870
871		*return_iplist = SMB_REALLOC_ARRAY((*return_iplist), struct ip_service,
872					(*return_count)+1);
873
874		if ((*return_iplist) == NULL) {
875			DEBUG(3,("resolve_lmhosts: malloc fail !\n"));
876			return False;
877		}
878
879		(*return_iplist)[*return_count].ip   = return_ip;
880		(*return_iplist)[*return_count].port = PORT_NONE;
881		*return_count += 1;
882
883		/* we found something */
884		result = True;
885
886		/* Multiple names only for DC lookup */
887		if (name_type != 0x1c)
888			break;
889	}
890
891	endlmhosts(fp);
892
893	return result;
894}
895
896
897/********************************************************
898 Resolve via "hosts" method.
899*********************************************************/
900
901static BOOL resolve_hosts(const char *name, int name_type,
902                         struct ip_service **return_iplist, int *return_count)
903{
904	/*
905	 * "host" means do a localhost, or dns lookup.
906	 */
907	struct hostent *hp;
908
909	if ( name_type != 0x20 && name_type != 0x0) {
910		DEBUG(5, ("resolve_hosts: not appropriate for name type <0x%x>\n", name_type));
911		return False;
912	}
913
914	*return_iplist = NULL;
915	*return_count = 0;
916
917	DEBUG(3,("resolve_hosts: Attempting host lookup for name %s<0x%x>\n", name, name_type));
918
919	if (((hp = sys_gethostbyname(name)) != NULL) && (hp->h_addr != NULL)) {
920		struct in_addr return_ip;
921		putip((char *)&return_ip,(char *)hp->h_addr);
922		*return_iplist = SMB_MALLOC_P(struct ip_service);
923		if(*return_iplist == NULL) {
924			DEBUG(3,("resolve_hosts: malloc fail !\n"));
925			return False;
926		}
927		(*return_iplist)->ip   = return_ip;
928		(*return_iplist)->port = PORT_NONE;
929		*return_count = 1;
930		return True;
931	}
932	return False;
933}
934
935/********************************************************
936 Resolve via "ADS" method.
937*********************************************************/
938
939static BOOL resolve_ads(const char *name, int name_type,
940                         struct ip_service **return_iplist, int *return_count)
941{
942
943#ifdef HAVE_ADS
944	if ( name_type == 0x1c ) {
945		int 			count, i = 0;
946		char 			*list = NULL;
947		const char		*ptr;
948		pstring			tok;
949
950		/* try to lookup the _ldap._tcp.<domain> if we are using ADS */
951		if ( lp_security() != SEC_ADS )
952			return False;
953
954		DEBUG(5,("resolve_hosts: Attempting to resolve DC's for %s using DNS\n",
955			name));
956
957		if (ldap_domain2hostlist(name, &list) != LDAP_SUCCESS)
958			return False;
959
960		count = count_chars(list, ' ') + 1;
961		if ( (*return_iplist = SMB_MALLOC_ARRAY(struct ip_service, count)) == NULL ) {
962			DEBUG(0,("resolve_hosts: malloc failed for %d entries\n", count ));
963			return False;
964		}
965
966		ptr = list;
967		while (next_token(&ptr, tok, " ", sizeof(tok))) {
968			unsigned port = LDAP_PORT;
969			char *p = strchr(tok, ':');
970			if (p) {
971				*p = 0;
972				port = atoi(p+1);
973			}
974			(*return_iplist)[i].ip   = *interpret_addr2(tok);
975			(*return_iplist)[i].port = port;
976
977			/* make sure it is a valid IP.  I considered checking the negative
978			   connection cache, but this is the wrong place for it.  Maybe only
979			   as a hac.  After think about it, if all of the IP addresses retuend
980			   from DNS are dead, what hope does a netbios name lookup have?
981			   The standard reason for falling back to netbios lookups is that
982			   our DNS server doesn't know anything about the DC's   -- jerry */
983
984			if ( is_zero_ip((*return_iplist)[i].ip) )
985				continue;
986
987			i++;
988		}
989		SAFE_FREE(list);
990
991		*return_count = i;
992
993		return True;
994	} else
995#endif 	/* HAVE_ADS */
996	{
997		return False;
998	}
999}
1000
1001/*******************************************************************
1002 Internal interface to resolve a name into an IP address.
1003 Use this function if the string is either an IP address, DNS
1004 or host name or NetBIOS name. This uses the name switch in the
1005 smb.conf to determine the order of name resolution.
1006
1007 Added support for ip addr/port to support ADS ldap servers.
1008 the only place we currently care about the port is in the
1009 resolve_hosts() when looking up DC's via SRV RR entries in DNS
1010**********************************************************************/
1011
1012BOOL internal_resolve_name(const char *name, int name_type,
1013			   struct ip_service **return_iplist,
1014			   int *return_count, const char *resolve_order)
1015{
1016	pstring name_resolve_list;
1017	fstring tok;
1018	const char *ptr;
1019	BOOL allones = (strcmp(name,"255.255.255.255") == 0);
1020	BOOL allzeros = (strcmp(name,"0.0.0.0") == 0);
1021	BOOL is_address = is_ipaddress(name);
1022	BOOL result = False;
1023	int i;
1024
1025	*return_iplist = NULL;
1026	*return_count = 0;
1027
1028	DEBUG(10, ("internal_resolve_name: looking up %s#%x\n", name, name_type));
1029
1030	if (allzeros || allones || is_address) {
1031
1032		if ( (*return_iplist = SMB_MALLOC_P(struct ip_service)) == NULL ) {
1033			DEBUG(0,("internal_resolve_name: malloc fail !\n"));
1034			return False;
1035		}
1036
1037		if(is_address) {
1038			/* ignore the port here */
1039			(*return_iplist)->port = PORT_NONE;
1040
1041			/* if it's in the form of an IP address then get the lib to interpret it */
1042			if (((*return_iplist)->ip.s_addr = inet_addr(name)) == 0xFFFFFFFF ){
1043				DEBUG(1,("internal_resolve_name: inet_addr failed on %s\n", name));
1044				return False;
1045			}
1046		} else {
1047			(*return_iplist)->ip.s_addr = allones ? 0xFFFFFFFF : 0;
1048		}
1049		*return_count = 1;
1050		return True;
1051	}
1052
1053	/* Check name cache */
1054
1055	if (namecache_fetch(name, name_type, return_iplist, return_count)) {
1056		/* This could be a negative response */
1057		return (*return_count > 0);
1058	}
1059
1060	/* set the name resolution order */
1061
1062	if ( strcmp( resolve_order, "NULL") == 0 ) {
1063		DEBUG(8,("internal_resolve_name: all lookups disabled\n"));
1064		return False;
1065	}
1066
1067	if ( !resolve_order ) {
1068		pstrcpy(name_resolve_list, lp_name_resolve_order());
1069	} else {
1070		pstrcpy(name_resolve_list, resolve_order);
1071
1072		if ( !name_resolve_list[0] ) {
1073			ptr = "host";
1074		} else {
1075			ptr = name_resolve_list;
1076		}
1077
1078		/* iterate through the name resolution backends */
1079
1080		while (next_token(&ptr, tok, LIST_SEP, sizeof(tok))) {
1081			if((strequal(tok, "host") || strequal(tok, "hosts"))) {
1082				if (resolve_hosts(name, name_type, return_iplist, return_count)) {
1083					result = True;
1084					goto done;
1085				}
1086			} else if(strequal( tok, "ads")) {
1087				/* deal with 0x1c names here.  This will result in a
1088					SRV record lookup for _ldap._tcp.<domain> if we
1089					are using 'security = ads' */
1090				if (resolve_ads(name, name_type, return_iplist, return_count)) {
1091					result = True;
1092					goto done;
1093				}
1094			} else if(strequal( tok, "lmhosts")) {
1095				if (resolve_lmhosts(name, name_type, return_iplist, return_count)) {
1096					result = True;
1097					goto done;
1098				}
1099			} else if(strequal( tok, "wins")) {
1100				/* don't resolve 1D via WINS */
1101				if (name_type != 0x1D && resolve_wins(name, name_type, return_iplist, return_count)) {
1102					result = True;
1103					goto done;
1104				}
1105			} else if(strequal( tok, "bcast")) {
1106				if (name_resolve_bcast(name, name_type, return_iplist, return_count)) {
1107					result = True;
1108					goto done;
1109				}
1110			} else {
1111				DEBUG(0,("resolve_name: unknown name switch type %s\n", tok));
1112			}
1113		}
1114
1115		/* All of the resolve_* functions above have returned false. */
1116
1117		SAFE_FREE(*return_iplist);
1118		*return_count = 0;
1119
1120		return False;
1121
1122  done:
1123
1124		/* Remove duplicate entries.  Some queries, notably #1c (domain
1125			controllers) return the PDC in iplist[0] and then all domain
1126			controllers including the PDC in iplist[1..n].  Iterating over
1127			the iplist when the PDC is down will cause two sets of timeouts. */
1128
1129		if ( *return_count ) {
1130			*return_count = remove_duplicate_addrs2( *return_iplist, *return_count );
1131		}
1132
1133		/* Save in name cache */
1134		if ( DEBUGLEVEL >= 100 ) {
1135			for (i = 0; i < *return_count && DEBUGLEVEL == 100; i++)
1136				DEBUG(100, ("Storing name %s of type %d (%s:%d)\n", name,
1137					name_type, inet_ntoa((*return_iplist)[i].ip), (*return_iplist)[i].port));
1138		}
1139
1140		namecache_store(name, name_type, *return_count, *return_iplist);
1141
1142		/* Display some debugging info */
1143
1144		if ( DEBUGLEVEL >= 10 ) {
1145			DEBUG(10, ("internal_resolve_name: returning %d addresses: ", *return_count));
1146
1147			for (i = 0; i < *return_count; i++) {
1148				DEBUGADD(10, ("%s:%d ", inet_ntoa((*return_iplist)[i].ip), (*return_iplist)[i].port));
1149			}
1150			DEBUG(10, ("\n"));
1151		}
1152	}
1153
1154	return result;
1155}
1156
1157/********************************************************
1158 Internal interface to resolve a name into one IP address.
1159 Use this function if the string is either an IP address, DNS
1160 or host name or NetBIOS name. This uses the name switch in the
1161 smb.conf to determine the order of name resolution.
1162*********************************************************/
1163
1164BOOL resolve_name(const char *name, struct in_addr *return_ip, int name_type)
1165{
1166	struct ip_service *ip_list = NULL;
1167	int count = 0;
1168
1169	if (is_ipaddress(name)) {
1170		*return_ip = *interpret_addr2(name);
1171		return True;
1172	}
1173
1174	if (internal_resolve_name(name, name_type, &ip_list, &count, lp_name_resolve_order())) {
1175		int i;
1176
1177		/* only return valid addresses for TCP connections */
1178		for (i=0; i<count; i++) {
1179			char *ip_str = inet_ntoa(ip_list[i].ip);
1180			if (ip_str &&
1181			    strcmp(ip_str, "255.255.255.255") != 0 &&
1182			    strcmp(ip_str, "0.0.0.0") != 0)
1183			{
1184				*return_ip = ip_list[i].ip;
1185				SAFE_FREE(ip_list);
1186				return True;
1187			}
1188		}
1189	}
1190
1191	SAFE_FREE(ip_list);
1192	return False;
1193}
1194
1195/********************************************************
1196 Find the IP address of the master browser or DMB for a workgroup.
1197*********************************************************/
1198
1199BOOL find_master_ip(const char *group, struct in_addr *master_ip)
1200{
1201	struct ip_service *ip_list = NULL;
1202	int count = 0;
1203
1204	if (lp_disable_netbios()) {
1205		DEBUG(5,("find_master_ip(%s): netbios is disabled\n", group));
1206		return False;
1207	}
1208
1209	if (internal_resolve_name(group, 0x1D, &ip_list, &count, lp_name_resolve_order())) {
1210		*master_ip = ip_list[0].ip;
1211		SAFE_FREE(ip_list);
1212		return True;
1213	}
1214	if(internal_resolve_name(group, 0x1B, &ip_list, &count, lp_name_resolve_order())) {
1215		*master_ip = ip_list[0].ip;
1216		SAFE_FREE(ip_list);
1217		return True;
1218	}
1219
1220	SAFE_FREE(ip_list);
1221	return False;
1222}
1223
1224/********************************************************
1225 Get the IP address list of the primary domain controller
1226 for a domain.
1227*********************************************************/
1228
1229BOOL get_pdc_ip(const char *domain, struct in_addr *ip)
1230{
1231	struct ip_service *ip_list;
1232	int count;
1233
1234	/* Look up #1B name */
1235
1236	if (!internal_resolve_name(domain, 0x1b, &ip_list, &count, lp_name_resolve_order())) {
1237		return False;
1238	}
1239
1240	/* if we get more than 1 IP back we have to assume it is a
1241	   multi-homed PDC and not a mess up */
1242
1243	if ( count > 1 ) {
1244		DEBUG(6,("get_pdc_ip: PDC has %d IP addresses!\n", count));
1245		sort_ip_list2( ip_list, count );
1246	}
1247
1248	*ip = ip_list[0].ip;
1249
1250	SAFE_FREE(ip_list);
1251
1252	return True;
1253}
1254
1255/********************************************************
1256 Get the IP address list of the domain controllers for
1257 a domain.
1258*********************************************************/
1259
1260static BOOL get_dc_list(const char *domain, struct ip_service **ip_list,
1261                 int *count, BOOL ads_only, int *ordered)
1262{
1263	fstring resolve_order;
1264
1265	/* if we are restricted to solely using DNS for looking
1266	   up a domain controller, make sure that host lookups
1267	   are enabled for the 'name resolve order'.  If host lookups
1268	   are disabled and ads_only is True, then set the string to
1269	   NULL. */
1270
1271	fstrcpy( resolve_order, lp_name_resolve_order() );
1272	strlower_m( resolve_order );
1273	if ( ads_only )  {
1274		if ( strstr( resolve_order, "host" ) )
1275			fstrcpy( resolve_order, "ads" );
1276		else
1277			fstrcpy( resolve_order, "NULL" );
1278	}
1279
1280
1281	*ordered = False;
1282
1283	/* If it's our domain then use the 'password server' parameter. */
1284
1285	if ( strequal(domain, lp_workgroup()) || strequal(domain, lp_realm()) ) {
1286		const char *p;
1287		char *pserver = lp_passwordserver(); /* UNIX charset. */
1288		char *port_str;
1289		int port;
1290		fstring name;
1291		int num_addresses = 0;
1292		int  local_count, i, j;
1293		struct ip_service *return_iplist = NULL;
1294		struct ip_service *auto_ip_list = NULL;
1295		BOOL done_auto_lookup = False;
1296		int auto_count = 0;
1297
1298
1299		if (!*pserver)
1300			return internal_resolve_name(domain, 0x1C, ip_list, count, resolve_order);
1301
1302		p = pserver;
1303
1304		/*
1305		 * if '*' appears in the "password server" list then add
1306		 * an auto lookup to the list of manually configured
1307		 * DC's.  If any DC is listed by name, then the list should be
1308		 * considered to be ordered
1309		 */
1310
1311		while (next_token(&p,name,LIST_SEP,sizeof(name))) {
1312			if (strequal(name, "*")) {
1313				if ( internal_resolve_name(domain, 0x1C, &auto_ip_list, &auto_count, resolve_order) )
1314					num_addresses += auto_count;
1315				done_auto_lookup = True;
1316				DEBUG(8,("Adding %d DC's from auto lookup\n", auto_count));
1317			} else  {
1318				num_addresses++;
1319			}
1320		}
1321
1322		/* if we have no addresses and haven't done the auto lookup, then
1323		   just return the list of DC's */
1324
1325		if ( (num_addresses == 0) && !done_auto_lookup ) {
1326			return internal_resolve_name(domain, 0x1C, ip_list, count, resolve_order);
1327		}
1328
1329		/* maybe we just failed? */
1330
1331		if ( num_addresses == 0 ) {
1332			DEBUG(4,("get_dc_list: no servers found\n"));
1333			return False;
1334		}
1335
1336		if ( (return_iplist = SMB_MALLOC_ARRAY(struct ip_service, num_addresses)) == NULL ) {
1337			DEBUG(3,("get_dc_list: malloc fail !\n"));
1338			return False;
1339		}
1340
1341		p = pserver;
1342		local_count = 0;
1343
1344		/* fill in the return list now with real IP's */
1345
1346		while ( (local_count<num_addresses) && next_token(&p,name,LIST_SEP,sizeof(name)) ) {
1347			struct in_addr name_ip;
1348
1349			/* copy any addersses from the auto lookup */
1350
1351			if ( strequal(name, "*") ) {
1352				for ( j=0; j<auto_count; j++ ) {
1353					/* Check for and don't copy any known bad DC IP's. */
1354					if(!NT_STATUS_IS_OK(check_negative_conn_cache(domain,
1355							inet_ntoa(auto_ip_list[j].ip)))) {
1356						DEBUG(5,("get_dc_list: negative entry %s removed from DC list\n",
1357							inet_ntoa(auto_ip_list[j].ip) ));
1358						continue;
1359					}
1360					return_iplist[local_count].ip   = auto_ip_list[j].ip;
1361					return_iplist[local_count].port = auto_ip_list[j].port;
1362					local_count++;
1363				}
1364				continue;
1365			}
1366
1367
1368			/* added support for address:port syntax for ads (not that I think
1369			   anyone will ever run the LDAP server in an AD domain on something
1370			   other than port 389 */
1371
1372			port = (lp_security() == SEC_ADS) ? LDAP_PORT : PORT_NONE;
1373			if ( (port_str=strchr(name, ':')) != NULL ) {
1374				*port_str = '\0';
1375				port_str++;
1376				port = atoi( port_str );
1377			}
1378
1379			/* explicit lookup; resolve_name() will handle names & IP addresses */
1380			if ( resolve_name( name, &name_ip, 0x20 ) ) {
1381
1382				/* Check for and don't copy any known bad DC IP's. */
1383				if( !NT_STATUS_IS_OK(check_negative_conn_cache(domain, inet_ntoa(name_ip))) ) {
1384					DEBUG(5,("get_dc_list: negative entry %s removed from DC list\n",name ));
1385					continue;
1386				}
1387
1388				return_iplist[local_count].ip 	= name_ip;
1389				return_iplist[local_count].port = port;
1390				local_count++;
1391				*ordered = True;
1392			}
1393		}
1394
1395		SAFE_FREE(auto_ip_list);
1396
1397		/* need to remove duplicates in the list if we have any
1398		   explicit password servers */
1399
1400		if ( local_count ) {
1401			local_count = remove_duplicate_addrs2( return_iplist, local_count );
1402		}
1403
1404		if ( DEBUGLEVEL >= 4 ) {
1405			DEBUG(4,("get_dc_list: returning %d ip addresses in an %sordered list\n", local_count,
1406				*ordered ? "":"un"));
1407			DEBUG(4,("get_dc_list: "));
1408			for ( i=0; i<local_count; i++ )
1409				DEBUGADD(4,("%s:%d ", inet_ntoa(return_iplist[i].ip), return_iplist[i].port ));
1410			DEBUGADD(4,("\n"));
1411		}
1412
1413		*ip_list = return_iplist;
1414		*count = local_count;
1415
1416		return (*count != 0);
1417	}
1418
1419	DEBUG(10,("get_dc_list: defaulting to internal auto lookup for domain %s\n", domain));
1420
1421	return internal_resolve_name(domain, 0x1C, ip_list, count, resolve_order);
1422}
1423
1424/*********************************************************************
1425 Small wrapper function to get the DC list and sort it if neccessary.
1426*********************************************************************/
1427
1428BOOL get_sorted_dc_list( const char *domain, struct ip_service **ip_list, int *count, BOOL ads_only )
1429{
1430	BOOL ordered;
1431
1432	DEBUG(8,("get_sorted_dc_list: attempting lookup using [%s]\n",
1433		(ads_only ? "ads" : lp_name_resolve_order())));
1434
1435	if ( !get_dc_list(domain, ip_list, count, ads_only, &ordered) ) {
1436		return False;
1437	}
1438
1439	/* only sort if we don't already have an ordered list */
1440	if ( !ordered ) {
1441		sort_ip_list2( *ip_list, *count );
1442	}
1443
1444	return True;
1445}
1446