selftest: abstract Samba3 provision a bit
[metze/samba/wip.git] / source3 / nmbd / nmbd_lmhosts.c
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 3 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, see <http://www.gnu.org/licenses/>.
18
19    Revision History:
20
21    Handle lmhosts file reading.
22
23 */
24
25 #include "includes.h"
26
27 /****************************************************************************
28 Load a lmhosts file.
29 ****************************************************************************/
30
31 void load_lmhosts_file(const char *fname)
32 {
33         char *name = NULL;
34         int name_type;
35         struct sockaddr_storage ss;
36         TALLOC_CTX *ctx = talloc_init("load_lmhosts_file");
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                 TALLOC_FREE(ctx);
43                 return;
44         }
45
46         while (getlmhostsent(ctx, fp, &name, &name_type, &ss) ) {
47                 struct in_addr ipaddr;
48                 struct subnet_record *subrec = NULL;
49                 enum name_source source = LMHOSTS_NAME;
50
51                 if (ss.ss_family != AF_INET) {
52                         TALLOC_FREE(name);
53                         continue;
54                 }
55
56                 ipaddr = ((struct sockaddr_in *)&ss)->sin_addr;
57
58                 /* We find a relevent subnet to put this entry on, then add it. */
59                 /* Go through all the broadcast subnets and see if the mask matches. */
60                 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
61                         if(same_net_v4(ipaddr, subrec->bcast_ip, subrec->mask_ip))
62                                 break;
63                 }
64
65                 /* If none match add the name to the remote_broadcast_subnet. */
66                 if(subrec == NULL)
67                         subrec = remote_broadcast_subnet;
68
69                 if(name_type == -1) {
70                         /* Add the (0) and (0x20) names directly into the namelist for this subnet. */
71                         (void)add_name_to_subnet(subrec,name,0x00,(uint16)NB_ACTIVE,PERMANENT_TTL,source,1,&ipaddr);
72                         (void)add_name_to_subnet(subrec,name,0x20,(uint16)NB_ACTIVE,PERMANENT_TTL,source,1,&ipaddr);
73                 } else {
74                         /* Add the given name type to the subnet namelist. */
75                         (void)add_name_to_subnet(subrec,name,name_type,(uint16)NB_ACTIVE,PERMANENT_TTL,source,1,&ipaddr);
76                 }
77         }
78
79         TALLOC_FREE(ctx);
80         endlmhosts(fp);
81 }
82
83 /****************************************************************************
84   Find a name read from the lmhosts file. We secretly check the names on
85   the remote_broadcast_subnet as if the name was added to a regular broadcast
86   subnet it will be found by normal name query processing.
87 ****************************************************************************/
88
89 bool find_name_in_lmhosts(struct nmb_name *nmbname, struct name_record **namerecp)
90 {
91         struct name_record *namerec;
92
93         *namerecp = NULL;
94
95         if((namerec = find_name_on_subnet(remote_broadcast_subnet, nmbname, FIND_ANY_NAME))==NULL)
96                 return False;
97
98         if(!NAME_IS_ACTIVE(namerec) || (namerec->data.source != LMHOSTS_NAME))
99                 return False;
100
101         *namerecp = namerec;
102         return True;
103 }