/src/samba/source4/libcli/resolve/lmhosts.c
Line | Count | Source |
1 | | /* |
2 | | Unix SMB/CIFS implementation. |
3 | | |
4 | | lmhosts 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-2014 |
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 | | #include "dynconfig.h" |
34 | | |
35 | | struct resolve_lmhosts_state { |
36 | | struct socket_address **addrs; |
37 | | char **names; |
38 | | }; |
39 | | |
40 | | /** |
41 | | lmhosts name resolution method - async send |
42 | | */ |
43 | | /* |
44 | | general name resolution - async send |
45 | | */ |
46 | | static struct composite_context *resolve_name_lmhosts_send( |
47 | | TALLOC_CTX *mem_ctx, |
48 | | struct tevent_context *event_ctx, |
49 | | void *userdata, uint32_t flags, |
50 | | uint16_t port, |
51 | | struct nbt_name *name) |
52 | 0 | { |
53 | 0 | struct composite_context *c; |
54 | 0 | struct resolve_lmhosts_state *state; |
55 | 0 | struct sockaddr_storage *resolved_iplist; |
56 | 0 | size_t resolved_count = 0, i; |
57 | |
|
58 | 0 | if (event_ctx == NULL) { |
59 | 0 | return NULL; |
60 | 0 | } |
61 | | |
62 | 0 | c = composite_create(mem_ctx, event_ctx); |
63 | 0 | if (c == NULL) return NULL; |
64 | | |
65 | 0 | if (composite_nomem(c->event_ctx, c)) return c; |
66 | | |
67 | 0 | state = talloc_zero(c, struct resolve_lmhosts_state); |
68 | 0 | if (composite_nomem(state, c)) return c; |
69 | 0 | c->private_data = state; |
70 | |
|
71 | 0 | c->status = resolve_lmhosts_file_as_sockaddr(state, |
72 | 0 | dyn_LMHOSTSFILE, |
73 | 0 | name->name, |
74 | 0 | name->type, |
75 | 0 | &resolved_iplist, |
76 | 0 | &resolved_count); |
77 | 0 | if (!composite_is_ok(c)) return c; |
78 | | |
79 | 0 | for (i=0; i < resolved_count; i += 2) { |
80 | 0 | state->addrs = talloc_realloc(state, state->addrs, struct socket_address *, i+2); |
81 | 0 | if (composite_nomem(state->addrs, c)) return c; |
82 | | |
83 | 0 | set_sockaddr_port((struct sockaddr *)&resolved_iplist[i], port); |
84 | |
|
85 | 0 | state->addrs[i] = socket_address_from_sockaddr(state->addrs, (struct sockaddr *)&resolved_iplist[i], sizeof(resolved_iplist[i])); |
86 | 0 | if (composite_nomem(state->addrs[i], c)) return c; |
87 | | |
88 | 0 | state->addrs[i+1] = NULL; |
89 | | |
90 | |
|
91 | 0 | state->names = talloc_realloc(state, state->names, char *, i+2); |
92 | 0 | if (composite_nomem(state->addrs, c)) return c; |
93 | | |
94 | 0 | state->names[i] = talloc_strdup(state->names, name->name); |
95 | 0 | if (composite_nomem(state->names[i], c)) return c; |
96 | | |
97 | 0 | state->names[i+1] = NULL; |
98 | |
|
99 | 0 | } |
100 | | |
101 | 0 | composite_done(c); |
102 | 0 | return c; |
103 | 0 | } |
104 | | |
105 | | /* |
106 | | general name resolution method - recv side |
107 | | */ |
108 | | static NTSTATUS resolve_name_lmhosts_recv(struct composite_context *c, |
109 | | TALLOC_CTX *mem_ctx, |
110 | | struct socket_address ***addrs, |
111 | | char ***names) |
112 | 0 | { |
113 | 0 | NTSTATUS status; |
114 | |
|
115 | 0 | status = composite_wait(c); |
116 | |
|
117 | 0 | if (NT_STATUS_IS_OK(status)) { |
118 | 0 | struct resolve_lmhosts_state *state = talloc_get_type(c->private_data, struct resolve_lmhosts_state); |
119 | 0 | *addrs = talloc_steal(mem_ctx, state->addrs); |
120 | 0 | if (names) { |
121 | 0 | *names = talloc_steal(mem_ctx, state->names); |
122 | 0 | } |
123 | 0 | } |
124 | |
|
125 | 0 | talloc_free(c); |
126 | 0 | return status; |
127 | 0 | } |
128 | | |
129 | | |
130 | | bool resolve_context_add_lmhosts_method(struct resolve_context *ctx) |
131 | 0 | { |
132 | | return resolve_context_add_method(ctx, resolve_name_lmhosts_send, resolve_name_lmhosts_recv, NULL); |
133 | 0 | } |