s4-loadparm: 2nd half of lp_ to lpcfg_ conversion
[nivanova/samba-autobuild/.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 };
37
38 struct resolve_file_state {
39         struct socket_address **addrs;
40         char **names;
41 };
42
43 /**
44   broadcast name resolution method - async send
45  */
46 /*
47   general name resolution - async send
48  */
49 struct composite_context *resolve_name_file_send(TALLOC_CTX *mem_ctx, 
50                                                   struct tevent_context *event_ctx,
51                                                   void *userdata, uint32_t flags,
52                                                   uint16_t port,
53                                                   struct nbt_name *name)
54 {
55         struct composite_context *c;
56         struct resolve_file_data *data = talloc_get_type_abort(userdata, struct resolve_file_data);
57         struct resolve_file_state *state;
58         struct sockaddr_storage *resolved_iplist;
59         int resolved_count, i;
60
61         bool srv_lookup = (flags & RESOLVE_NAME_FLAG_DNS_SRV);
62
63         if (event_ctx == NULL) {
64                 return NULL;
65         }
66
67         c = composite_create(mem_ctx, event_ctx);
68         if (c == NULL) return NULL;
69
70         /* This isn't an NBT layer resolver */
71         if (flags & RESOLVE_NAME_FLAG_FORCE_NBT || name->type != NBT_NAME_SERVER) {
72                 composite_error(c, NT_STATUS_OBJECT_NAME_NOT_FOUND);
73                 return c;
74         }
75
76         if (composite_nomem(c->event_ctx, c)) return c;
77
78         state = talloc_zero(c, struct resolve_file_state);
79         if (composite_nomem(state, c)) return c;
80         c->private_data = state;
81
82
83         c->status = resolve_dns_hosts_file_as_sockaddr(data->lookup_file, name->name, srv_lookup, state, &resolved_iplist, &resolved_count);
84         if (!composite_is_ok(c)) return c;
85
86         for (i=0; i < resolved_count; i++) {
87                 state->addrs = talloc_realloc(state, state->addrs, struct socket_address *, i+2);
88                 if (composite_nomem(state->addrs, c)) return c;
89                 
90                 if (!(flags & RESOLVE_NAME_FLAG_OVERWRITE_PORT)) {
91                         set_sockaddr_port((struct sockaddr *)&resolved_iplist[i], port);
92                 }
93
94                 state->addrs[i] = socket_address_from_sockaddr(state->addrs, (struct sockaddr *)&resolved_iplist[i], sizeof(resolved_iplist[i]));
95                 if (composite_nomem(state->addrs[i], c)) return c;
96
97                 state->addrs[i+1] = NULL;
98
99
100                 state->names = talloc_realloc(state, state->names, char *, i+2);
101                 if (composite_nomem(state->addrs, c)) return c;
102
103                 state->names[i] = talloc_strdup(state->names, name->name);
104                 if (composite_nomem(state->names[i], c)) return c;
105
106                 state->names[i+1] = NULL;
107                 
108                 i++;
109         }
110
111         composite_done(c);
112         return c;
113 }
114
115 /*
116   general name resolution method - recv side
117  */
118 NTSTATUS resolve_name_file_recv(struct composite_context *c, 
119                                  TALLOC_CTX *mem_ctx,
120                                  struct socket_address ***addrs,
121                                  char ***names)
122 {
123         NTSTATUS status;
124
125         status = composite_wait(c);
126
127         if (NT_STATUS_IS_OK(status)) {
128                 struct resolve_file_state *state = talloc_get_type(c->private_data, struct resolve_file_state);
129                 *addrs = talloc_steal(mem_ctx, state->addrs);
130                 if (names) {
131                         *names = talloc_steal(mem_ctx, state->names);
132                 }
133         }
134
135         talloc_free(c);
136         return status;
137 }
138
139
140 bool resolve_context_add_file_method(struct resolve_context *ctx, const char *lookup_file)
141 {
142         struct resolve_file_data *data = talloc(ctx, struct resolve_file_data);
143         data->lookup_file = lookup_file;
144         return resolve_context_add_method(ctx, resolve_name_file_send, resolve_name_file_recv, data);
145 }
146
147 bool resolve_context_add_file_method_lp(struct resolve_context *ctx, struct loadparm_context *lp_ctx)
148 {
149         return resolve_context_add_file_method(ctx, lpcfg_parm_string(lp_ctx, NULL, "resolv", "host file"));
150 }