Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[tprouty/samba.git] / source / nmbd / nmbd_lmhosts.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NBT netbios routines and daemon - version 2
5    Copyright (C) Jeremy Allison 1994-1998
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21    Revision History:
22
23    Handle lmhosts file reading.
24
25 */
26
27 #include "includes.h"
28
29 /****************************************************************************
30 Load a lmhosts file.
31 ****************************************************************************/
32 void 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   {
47     struct subnet_record *subrec = NULL;
48     enum name_source source = LMHOSTS_NAME;
49
50     /* We find a relevent subnet to put this entry on, then add it. */
51     /* Go through all the broadcast subnets and see if the mask matches. */
52     for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
53     {
54       if(same_net(ipaddr, subrec->bcast_ip, subrec->mask_ip))
55         break;
56     }
57   
58     /* If none match add the name to the remote_broadcast_subnet. */
59     if(subrec == NULL)
60       subrec = remote_broadcast_subnet;
61
62     if(name_type == -1)
63     {
64       /* Add the (0) and (0x20) names directly into the namelist for this subnet. */
65       (void)add_name_to_subnet(subrec,name,0x00,(uint16)NB_ACTIVE,PERMANENT_TTL,source,1,&ipaddr);
66       (void)add_name_to_subnet(subrec,name,0x20,(uint16)NB_ACTIVE,PERMANENT_TTL,source,1,&ipaddr);
67     }
68     else
69     {
70       /* Add the given name type to the subnet namelist. */
71       (void)add_name_to_subnet(subrec,name,name_type,(uint16)NB_ACTIVE,PERMANENT_TTL,source,1,&ipaddr);
72     }
73   }
74    
75   endlmhosts(fp);
76 }
77
78 /****************************************************************************
79   Find a name read from the lmhosts file. We secretly check the names on
80   the remote_broadcast_subnet as if the name was added to a regular broadcast
81   subnet it will be found by normal name query processing.
82 ****************************************************************************/
83
84 BOOL find_name_in_lmhosts(struct nmb_name *nmbname, struct name_record **namerecp)
85 {
86   struct name_record *namerec;
87
88   *namerecp = NULL;
89
90   if((namerec = find_name_on_subnet(remote_broadcast_subnet, nmbname, 
91                                  FIND_ANY_NAME))==NULL)
92     return False;
93
94   if(!NAME_IS_ACTIVE(namerec) || (namerec->data.source != LMHOSTS_NAME))
95     return False;
96
97   *namerecp = namerec;
98   return True;
99 }