s4-resolve: the file backend should not look at the name type
[kai/samba.git] / source4 / libcli / resolve / file.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    broadcast name resolution module
5
6    Copyright (C) Andrew Tridgell 1994-1998,2005
7    Copyright (C) Jeremy Allison 2007
8    Copyright (C) Jelmer Vernooij 2007
9    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009-2010
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "libcli/composite/composite.h"
26 #include "libcli/resolve/resolve.h"
27 #include "lib/socket/socket.h"
28 #include "system/network.h"
29 #include "lib/socket/netif.h"
30 #include "param/param.h"
31 #include "lib/util/util_net.h"
32 #include "libcli/nbt/libnbt.h"
33
34 struct resolve_file_data {
35         const char *lookup_file;
36         const char *default_domain;
37 };
38
39 struct resolve_file_state {
40         struct socket_address **addrs;
41         char **names;
42 };
43
44 /**
45   broadcast name resolution method - async send
46  */
47 /*
48   general name resolution - async send
49  */
50 struct composite_context *resolve_name_file_send(TALLOC_CTX *mem_ctx, 
51                                                   struct tevent_context *event_ctx,
52                                                   void *userdata, uint32_t flags,
53                                                   uint16_t port,
54                                                   struct nbt_name *name)
55 {
56         struct composite_context *c;
57         struct resolve_file_data *data = talloc_get_type_abort(userdata, struct resolve_file_data);
58         struct resolve_file_state *state;
59         struct sockaddr_storage *resolved_iplist;
60         int resolved_count, i;
61         char *dns_name;
62
63         bool srv_lookup = (flags & RESOLVE_NAME_FLAG_DNS_SRV);
64
65         if (event_ctx == NULL) {
66                 return NULL;
67         }
68
69         c = composite_create(mem_ctx, event_ctx);
70         if (c == NULL) return NULL;
71
72         /* This isn't an NBT layer resolver */
73         if (flags & RESOLVE_NAME_FLAG_FORCE_NBT) {
74                 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
75                 return c;
76         }
77
78         if (composite_nomem(c->event_ctx, c)) return c;
79
80         state = talloc_zero(c, struct resolve_file_state);
81         if (composite_nomem(state, c)) return c;
82         c->private_data = state;
83
84         dns_name = name->name;
85         if (strchr(dns_name, '.') == NULL) {
86                 dns_name = talloc_asprintf(state, "%s.%s", dns_name, data->default_domain);
87         }
88
89         c->status = resolve_dns_hosts_file_as_sockaddr(data->lookup_file, dns_name,
90                                                        srv_lookup, state, &resolved_iplist, &resolved_count);
91         if (!composite_is_ok(c)) return c;
92
93         for (i=0; i < resolved_count; i++) {
94                 state->addrs = talloc_realloc(state, state->addrs, struct socket_address *, i+2);
95                 if (composite_nomem(state->addrs, c)) return c;
96                 
97                 if (!(flags & RESOLVE_NAME_FLAG_OVERWRITE_PORT)) {
98                         set_sockaddr_port((struct sockaddr *)&resolved_iplist[i], port);
99                 }
100
101                 state->addrs[i] = socket_address_from_sockaddr(state->addrs, (struct sockaddr *)&resolved_iplist[i], sizeof(resolved_iplist[i]));
102                 if (composite_nomem(state->addrs[i], c)) return c;
103
104                 state->addrs[i+1] = NULL;
105
106
107                 state->names = talloc_realloc(state, state->names, char *, i+2);
108                 if (composite_nomem(state->addrs, c)) return c;
109
110                 state->names[i] = talloc_strdup(state->names, dns_name);
111                 if (composite_nomem(state->names[i], c)) return c;
112
113                 state->names[i+1] = NULL;
114                 
115                 i++;
116         }
117
118         composite_done(c);
119         return c;
120 }
121
122 /*
123   general name resolution method - recv side
124  */
125 NTSTATUS resolve_name_file_recv(struct composite_context *c, 
126                                  TALLOC_CTX *mem_ctx,
127                                  struct socket_address ***addrs,
128                                  char ***names)
129 {
130         NTSTATUS status;
131
132         status = composite_wait(c);
133
134         if (NT_STATUS_IS_OK(status)) {
135                 struct resolve_file_state *state = talloc_get_type(c->private_data, struct resolve_file_state);
136                 *addrs = talloc_steal(mem_ctx, state->addrs);
137                 if (names) {
138                         *names = talloc_steal(mem_ctx, state->names);
139                 }
140         }
141
142         talloc_free(c);
143         return status;
144 }
145
146
147 bool resolve_context_add_file_method(struct resolve_context *ctx, const char *lookup_file, const char *default_domain)
148 {
149         struct resolve_file_data *data = talloc(ctx, struct resolve_file_data);
150         data->lookup_file    = talloc_strdup(data, lookup_file);
151         data->default_domain = talloc_strdup(data, default_domain);
152         return resolve_context_add_method(ctx, resolve_name_file_send, resolve_name_file_recv, data);
153 }
154
155 bool resolve_context_add_file_method_lp(struct resolve_context *ctx, struct loadparm_context *lp_ctx)
156 {
157         return resolve_context_add_file_method(ctx,
158                                                lpcfg_parm_string(lp_ctx, NULL, "resolv", "host file"),
159                                                lpcfg_dnsdomain(lp_ctx));
160 }