1/*
2   Unix SMB/CIFS implementation.
3   NBT netbios routines and daemon - version 2
4   Copyright (C) Jeremy Allison 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   Revision History:
21
22   Handle lmhosts file reading.
23
24*/
25
26#include "includes.h"
27
28/****************************************************************************
29Load a lmhosts file.
30****************************************************************************/
31
32void load_lmhosts_file(char *fname)
33{
34	pstring name;
35	int name_type;
36	struct in_addr ipaddr;
37	XFILE *fp = startlmhosts( fname );
38
39	if (!fp) {
40		DEBUG(2,("load_lmhosts_file: Can't open lmhosts file %s. Error was %s\n",
41			fname, strerror(errno)));
42		return;
43	}
44
45	while (getlmhostsent(fp, name, &name_type, &ipaddr) ) {
46		struct subnet_record *subrec = NULL;
47		enum name_source source = LMHOSTS_NAME;
48
49		/* We find a relevent subnet to put this entry on, then add it. */
50		/* Go through all the broadcast subnets and see if the mask matches. */
51		for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
52			if(same_net(ipaddr, subrec->bcast_ip, subrec->mask_ip))
53				break;
54		}
55
56		/* If none match add the name to the remote_broadcast_subnet. */
57		if(subrec == NULL)
58			subrec = remote_broadcast_subnet;
59
60		if(name_type == -1) {
61			/* Add the (0) and (0x20) names directly into the namelist for this subnet. */
62			(void)add_name_to_subnet(subrec,name,0x00,(uint16)NB_ACTIVE,PERMANENT_TTL,source,1,&ipaddr);
63			(void)add_name_to_subnet(subrec,name,0x20,(uint16)NB_ACTIVE,PERMANENT_TTL,source,1,&ipaddr);
64		} else {
65			/* Add the given name type to the subnet namelist. */
66			(void)add_name_to_subnet(subrec,name,name_type,(uint16)NB_ACTIVE,PERMANENT_TTL,source,1,&ipaddr);
67		}
68	}
69
70	endlmhosts(fp);
71}
72
73/****************************************************************************
74  Find a name read from the lmhosts file. We secretly check the names on
75  the remote_broadcast_subnet as if the name was added to a regular broadcast
76  subnet it will be found by normal name query processing.
77****************************************************************************/
78
79BOOL find_name_in_lmhosts(struct nmb_name *nmbname, struct name_record **namerecp)
80{
81	struct name_record *namerec;
82
83	*namerecp = NULL;
84
85	if((namerec = find_name_on_subnet(remote_broadcast_subnet, nmbname, FIND_ANY_NAME))==NULL)
86		return False;
87
88	if(!NAME_IS_ACTIVE(namerec) || (namerec->data.source != LMHOSTS_NAME))
89		return False;
90
91	*namerecp = namerec;
92	return True;
93}
94