This is it ! The mega-merge of the JRA_NMBD_REWRITE branch
[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-1997
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 extern int DEBUGLEVEL;
30
31 /****************************************************************************
32 Load a lmhosts file.
33 ****************************************************************************/
34 void load_lmhosts_file(char *fname)
35 {  
36   FILE *fp = fopen(fname,"r");
37   pstring line;
38   if (!fp) {
39     DEBUG(2,("load_lmhosts_file: Can't open lmhosts file %s. Error was %s\n",
40              fname, strerror(errno)));
41     return;
42   }
43    
44   while (!feof(fp))
45   {
46     pstring ip,name,flags,extra;
47     struct subnet_record *subrec = NULL;
48     char *ptr;
49     int count = 0;  
50     struct in_addr ipaddr;
51     enum name_source source = LMHOSTS_NAME;
52     int name_type = -1;
53
54     if (!fgets_slash(line,sizeof(pstring),fp))
55       continue;
56
57     if (*line == '#')
58       continue;
59
60     strcpy(ip,"");     
61     strcpy(name,"");
62     strcpy(flags,"");
63      
64     ptr = line;
65      
66     if (next_token(&ptr,ip   ,NULL))
67       ++count;
68     if (next_token(&ptr,name ,NULL))
69       ++count;
70     if (next_token(&ptr,flags,NULL))
71       ++count;
72     if (next_token(&ptr,extra,NULL))
73       ++count;
74    
75     if (count <= 0)
76       continue;
77      
78     if (count > 0 && count < 2)
79     {
80       DEBUG(0,("load_lmhosts_file: Ill formed hosts line [%s]\n",line));
81       continue;
82     }
83       
84     if (count >= 4)
85     {
86       DEBUG(0,("load_lmhosts_file: too many columns in lmhosts file %s (obsolete syntax)\n",
87              fname));
88       continue;
89     }
90       
91     DEBUG(4, ("load_lmhosts_file: lmhost entry: %s %s %s\n", ip, name, flags));
92     
93     if (strchr(flags,'G') || strchr(flags,'S'))
94     {
95       DEBUG(0,("load_lmhosts_file: group flag in %s ignored (obsolete)\n",fname));
96       continue;
97     }
98
99     ipaddr = *interpret_addr2(ip);
100
101     /* Extra feature. If the name ends in '#XX', where XX is a hex number,
102        then only add that name type. */
103     if((ptr = strchr(name, '#')) != NULL)
104     {
105       char *endptr;
106
107       ptr++;
108       name_type = (int)strtol(ptr, &endptr,0);
109
110       if(!*ptr || (endptr == ptr))
111       {
112         DEBUG(0,("load_lmhosts_file: invalid name %s containing '#'.\n", name));
113         continue;
114       }
115
116       *(--ptr) = '\0'; /* Truncate at the '#' */
117     }
118
119     /* We find a relevent subnet to put this entry on, then add it. */
120     /* Go through all the broadcast subnets and see if the mask matches. */
121     for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
122     {
123       if(same_net(ipaddr, subrec->bcast_ip, subrec->mask_ip))
124         break;
125     }
126   
127     /* If none match add the name to the remote_broadcast_subnet. */
128     if(subrec == NULL)
129       subrec = remote_broadcast_subnet;
130
131     if(name_type == -1)
132     {
133       /* Add the (0) and (0x20) names directly into the namelist for this subnet. */
134       add_name_to_subnet(subrec,name,0x00,(uint16)NB_ACTIVE,PERMANENT_TTL,source,1,&ipaddr);
135       add_name_to_subnet(subrec,name,0x20,(uint16)NB_ACTIVE,PERMANENT_TTL,source,1,&ipaddr);
136     }
137     else
138     {
139       /* Add the given name type to the subnet namelist. */
140       add_name_to_subnet(subrec,name,name_type,(uint16)NB_ACTIVE,PERMANENT_TTL,source,1,&ipaddr);
141     }
142   }
143    
144   fclose(fp);
145 }
146
147 /****************************************************************************
148   Find a name read from the lmhosts file. We secretly check the names on
149   the remote_broadcast_subnet as if the name was added to a regular broadcast
150   subnet it will be found by normal name query processing.
151 ****************************************************************************/
152
153 BOOL find_name_in_lmhosts(struct nmb_name *nmbname, struct name_record **namerecp)
154 {
155   struct name_record *namerec;
156
157   *namerecp = NULL;
158
159   if((namerec = find_name_on_subnet(remote_broadcast_subnet, nmbname, 
160                                  FIND_ANY_NAME))==NULL)
161     return False;
162
163   if(!NAME_IS_ACTIVE(namerec) || (namerec->source != LMHOSTS_NAME))
164     return False;
165
166   *namerecp = namerec;
167   return True;
168 }