r26264: pass name resolve order explicitly, use torture context for settings in dssyn...
[jelmer/samba4-debian.git] / source / libcli / resolve / resolve.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    general name resolution interface
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/events/events.h"
24 #include "libcli/composite/composite.h"
25 #include "libcli/resolve/resolve.h"
26 #include "librpc/gen_ndr/ndr_nbt.h"
27 #include "param/param.h"
28 #include "system/network.h"
29
30 struct resolve_state {
31         struct nbt_name name;
32         const char **methods;
33         struct composite_context *creq;
34         const char *reply_addr;
35 };
36
37 static struct composite_context *setup_next_method(struct composite_context *c);
38
39 /* pointers to the resolver backends */
40 static const struct resolve_method {
41         const char *name;
42         struct composite_context *(*send_fn)(TALLOC_CTX *mem_ctx, struct event_context *, struct nbt_name *);
43         NTSTATUS (*recv_fn)(struct composite_context *, TALLOC_CTX *, const char **);
44
45 } resolve_methods[] = {
46         { "bcast", resolve_name_bcast_send,  resolve_name_bcast_recv },
47         { "wins",  resolve_name_wins_send,   resolve_name_wins_recv },
48         { "host",  resolve_name_host_send,   resolve_name_host_recv }
49 };
50
51
52 /* 
53    find a matching backend
54 */
55 static const struct resolve_method *find_method(const char *name)
56 {
57         int i;
58         if (name == NULL) return NULL;
59         for (i=0;i<ARRAY_SIZE(resolve_methods);i++) {
60                 if (strcasecmp(name, resolve_methods[i].name) == 0) {
61                         return &resolve_methods[i];
62                 }
63         }
64         return NULL;
65 }
66
67 /*
68   handle completion of one name resolve method
69 */
70 static void resolve_handler(struct composite_context *creq)
71 {
72         struct composite_context *c = (struct composite_context *)creq->async.private_data;
73         struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
74         const struct resolve_method *method = find_method(state->methods[0]);
75
76         c->status = method->recv_fn(creq, state, &state->reply_addr);
77         
78         if (!NT_STATUS_IS_OK(c->status)) {
79                 state->methods++;
80                 state->creq = setup_next_method(c);
81                 if (state->creq != NULL) {
82                         return;
83                 }
84         }
85
86         if (!NT_STATUS_IS_OK(c->status)) {
87                 c->state = COMPOSITE_STATE_ERROR;
88         } else {
89                 c->state = COMPOSITE_STATE_DONE;
90         }
91         if (c->async.fn) {
92                 c->async.fn(c);
93         }
94 }
95
96
97 static struct composite_context *setup_next_method(struct composite_context *c)
98 {
99         struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
100         struct composite_context *creq = NULL;
101
102         do {
103                 const struct resolve_method *method = find_method(state->methods[0]);
104                 if (method) {
105                         creq = method->send_fn(c, c->event_ctx, &state->name);
106                 }
107                 if (creq == NULL && state->methods[0]) state->methods++;
108
109         } while (!creq && state->methods[0]);
110
111         if (creq) {
112                 creq->async.fn = resolve_handler;
113                 creq->async.private_data = c;
114         }
115
116         return creq;
117 }
118
119 /*
120   general name resolution - async send
121  */
122 struct composite_context *resolve_name_send(struct nbt_name *name, struct event_context *event_ctx,
123                                             const char **methods)
124 {
125         struct composite_context *c;
126         struct resolve_state *state;
127
128         c = composite_create(event_ctx, event_ctx);
129         if (c == NULL) return NULL;
130
131         if (methods == NULL) {
132                 composite_error(c, NT_STATUS_INVALID_PARAMETER);
133                 return c;
134         }
135
136         if (event_ctx == NULL) {
137                 c->event_ctx = event_context_init(c);
138         } else {
139                 c->event_ctx = talloc_reference(c, event_ctx);
140         }
141         if (composite_nomem(c->event_ctx, c)) return c;
142
143         state = talloc(c, struct resolve_state);
144         if (composite_nomem(state, c)) return c;
145         c->private_data = state;
146
147         c->status = nbt_name_dup(state, name, &state->name);
148         if (!composite_is_ok(c)) return c;
149         
150         state->methods = str_list_copy(state, methods);
151         if (composite_nomem(state->methods, c)) return c;
152
153         if (is_ipaddress(state->name.name) || 
154             strcasecmp(state->name.name, "localhost") == 0) {
155                 struct in_addr ip = interpret_addr2(state->name.name);
156                 state->reply_addr = talloc_strdup(state, inet_ntoa(ip));
157                 if (composite_nomem(state->reply_addr, c)) return c;
158                 composite_done(c);
159                 return c;
160         }
161
162         state->creq = setup_next_method(c);
163         if (composite_nomem(state->creq, c)) return c;
164         
165         return c;
166 }
167
168 /*
169   general name resolution method - recv side
170  */
171 NTSTATUS resolve_name_recv(struct composite_context *c, 
172                            TALLOC_CTX *mem_ctx, const char **reply_addr)
173 {
174         NTSTATUS status;
175
176         status = composite_wait(c);
177
178         if (NT_STATUS_IS_OK(status)) {
179                 struct resolve_state *state = talloc_get_type(c->private_data, struct resolve_state);
180                 *reply_addr = talloc_steal(mem_ctx, state->reply_addr);
181         }
182
183         talloc_free(c);
184         return status;
185 }
186
187 /*
188   general name resolution - sync call
189  */
190 NTSTATUS resolve_name(struct nbt_name *name, TALLOC_CTX *mem_ctx, const char **reply_addr, struct event_context *ev, const char **name_resolve_order)
191 {
192         struct composite_context *c = resolve_name_send(name, ev, name_resolve_order); 
193         return resolve_name_recv(c, mem_ctx, reply_addr);
194 }
195
196 /* Initialise a struct nbt_name with a NULL scope */
197
198 void make_nbt_name(struct nbt_name *nbt, const char *name, int type)
199 {
200         nbt->name = name;
201         nbt->scope = NULL;
202         nbt->type = type;
203 }
204
205 /* Initialise a struct nbt_name with a NBT_NAME_CLIENT (0x00) name */
206
207 void make_nbt_name_client(struct nbt_name *nbt, const char *name)
208 {
209         make_nbt_name(nbt, name, NBT_NAME_CLIENT);
210 }
211
212 /* Initialise a struct nbt_name with a NBT_NAME_SERVER (0x20) name */
213
214 void make_nbt_name_server(struct nbt_name *nbt, const char *name)
215 {
216         make_nbt_name(nbt, name, NBT_NAME_SERVER);
217 }