r26644: Janitorial: Pass resolve_context explicitly to various SMB functions, should...
[gd/samba-autobuild/.git] / source4 / libcli / raw / clisocket.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB client socket context management functions
5
6    Copyright (C) Andrew Tridgell 1994-2005
7    Copyright (C) James Myers 2003 <myersjj@samba.org>
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27 #include "lib/socket/socket.h"
28 #include "libcli/resolve/resolve.h"
29 #include "param/param.h"
30
31 struct sock_connect_state {
32         struct composite_context *ctx;
33         const char *host_name;
34         int num_ports;
35         uint16_t *ports;
36         struct smbcli_socket *result;
37 };
38
39 /*
40   connect a smbcli_socket context to an IP/port pair
41   if port is 0 then choose 445 then 139
42 */
43
44 static void smbcli_sock_connect_recv_conn(struct composite_context *ctx);
45
46 struct composite_context *smbcli_sock_connect_send(TALLOC_CTX *mem_ctx,
47                                                    const char *host_addr,
48                                                    const char **ports,
49                                                    const char *host_name,
50                                                    struct resolve_context *resolve_ctx,
51                                                    struct event_context *event_ctx)
52 {
53         struct composite_context *result, *ctx;
54         struct sock_connect_state *state;
55         int i;
56
57         result = talloc_zero(mem_ctx, struct composite_context);
58         if (result == NULL) goto failed;
59         result->state = COMPOSITE_STATE_IN_PROGRESS;
60
61         if (event_ctx != NULL) {
62                 result->event_ctx = talloc_reference(result, event_ctx);
63         } else {
64                 result->event_ctx = event_context_init(result);
65         }
66
67         if (result->event_ctx == NULL) goto failed;
68
69         state = talloc(result, struct sock_connect_state);
70         if (state == NULL) goto failed;
71         state->ctx = result;
72         result->private_data = state;
73
74         state->host_name = talloc_strdup(state, host_name);
75         if (state->host_name == NULL) goto failed;
76
77         state->num_ports = str_list_length(ports);
78         state->ports = talloc_array(state, uint16_t, state->num_ports);
79         if (state->ports == NULL) goto failed;
80         for (i=0;ports[i];i++) {
81                 state->ports[i] = atoi(ports[i]);
82         }
83
84         ctx = socket_connect_multi_send(state, host_addr,
85                                         state->num_ports, state->ports,
86                                         lp_resolve_context(global_loadparm),
87                                         state->ctx->event_ctx);
88         if (ctx == NULL) goto failed;
89         ctx->async.fn = smbcli_sock_connect_recv_conn;
90         ctx->async.private_data = state;
91         return result;
92
93 failed:
94         talloc_free(result);
95         return NULL;
96 }
97
98 static void smbcli_sock_connect_recv_conn(struct composite_context *ctx)
99 {
100         struct sock_connect_state *state =
101                 talloc_get_type(ctx->async.private_data,
102                                 struct sock_connect_state);
103         struct socket_context *sock;
104         uint16_t port;
105
106         state->ctx->status = socket_connect_multi_recv(ctx, state, &sock,
107                                                        &port);
108         if (!composite_is_ok(state->ctx)) return;
109
110         state->ctx->status =
111                 socket_set_option(sock, lp_socket_options(global_loadparm), NULL);
112         if (!composite_is_ok(state->ctx)) return;
113
114
115         state->result = talloc_zero(state, struct smbcli_socket);
116         if (composite_nomem(state->result, state->ctx)) return;
117
118         state->result->sock = talloc_steal(state->result, sock);
119         state->result->port = port;
120         state->result->hostname = talloc_steal(sock, state->host_name);
121
122         state->result->event.ctx =
123                 talloc_reference(state->result, state->ctx->event_ctx);
124         if (composite_nomem(state->result->event.ctx, state->ctx)) return;
125
126         composite_done(state->ctx);
127 }
128
129 /*
130   finish a smbcli_sock_connect_send() operation
131 */
132 NTSTATUS smbcli_sock_connect_recv(struct composite_context *c,
133                                   TALLOC_CTX *mem_ctx,
134                                   struct smbcli_socket **result)
135 {
136         NTSTATUS status = composite_wait(c);
137         if (NT_STATUS_IS_OK(status)) {
138                 struct sock_connect_state *state =
139                         talloc_get_type(c->private_data,
140                                         struct sock_connect_state);
141                 *result = talloc_steal(mem_ctx, state->result);
142         }
143         talloc_free(c);
144         return status;
145 }
146
147 /*
148   connect a smbcli_socket context to an IP/port pair
149   if port is 0 then choose the ports listed in smb.conf (normally 445 then 139)
150
151   sync version of the function
152 */
153 NTSTATUS smbcli_sock_connect(TALLOC_CTX *mem_ctx,
154                              const char *host_addr, const char **ports,
155                              const char *host_name,
156                              struct resolve_context *resolve_ctx,
157                              struct event_context *event_ctx,
158                              struct smbcli_socket **result)
159 {
160         struct composite_context *c =
161                 smbcli_sock_connect_send(mem_ctx, host_addr, ports, host_name,
162                                          resolve_ctx,
163                                          event_ctx);
164         return smbcli_sock_connect_recv(c, mem_ctx, result);
165 }
166
167
168 /****************************************************************************
169  mark the socket as dead
170 ****************************************************************************/
171 void smbcli_sock_dead(struct smbcli_socket *sock)
172 {
173         talloc_free(sock->event.fde);
174         sock->event.fde = NULL;
175         talloc_free(sock->sock);
176         sock->sock = NULL;
177 }
178
179 /****************************************************************************
180  Set socket options on a open connection.
181 ****************************************************************************/
182 void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
183 {
184         socket_set_option(sock->sock, options, NULL);
185 }
186
187 /****************************************************************************
188 resolve a hostname and connect 
189 ****************************************************************************/
190 struct smbcli_socket *smbcli_sock_connect_byname(const char *host, const char **ports,
191                                                  TALLOC_CTX *mem_ctx,
192                                                  struct resolve_context *resolve_ctx,
193                                                  struct event_context *event_ctx)
194 {
195         int name_type = NBT_NAME_SERVER;
196         const char *address;
197         NTSTATUS status;
198         struct nbt_name nbt_name;
199         char *name, *p;
200         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
201         struct smbcli_socket *result;
202
203         if (tmp_ctx == NULL) {
204                 DEBUG(0, ("talloc_new failed\n"));
205                 return NULL;
206         }
207
208         name = talloc_strdup(tmp_ctx, host);
209         if (name == NULL) {
210                 DEBUG(0, ("talloc_strdup failed\n"));
211                 talloc_free(tmp_ctx);
212                 return NULL;
213         }
214
215         if (event_ctx == NULL) {
216                 event_ctx = event_context_init(mem_ctx);
217         }
218
219         if (event_ctx == NULL) {
220                 DEBUG(0, ("event_context_init failed\n"));
221                 talloc_free(tmp_ctx);
222                 return NULL;
223         }
224
225         /* allow hostnames of the form NAME#xx and do a netbios lookup */
226         if ((p = strchr(name, '#'))) {
227                 name_type = strtol(p+1, NULL, 16);
228                 *p = 0;
229         }
230
231         make_nbt_name(&nbt_name, host, name_type);
232         
233         status = resolve_name(resolve_ctx, &nbt_name, tmp_ctx, &address, event_ctx);
234         if (!NT_STATUS_IS_OK(status)) {
235                 talloc_free(tmp_ctx);
236                 return NULL;
237         }
238
239         status = smbcli_sock_connect(mem_ctx, address, ports, name, resolve_ctx,
240                                      event_ctx, &result);
241
242         if (!NT_STATUS_IS_OK(status)) {
243                 DEBUG(9, ("smbcli_sock_connect failed: %s\n",
244                           nt_errstr(status)));
245                 talloc_free(tmp_ctx);
246                 return NULL;
247         }
248
249         talloc_free(tmp_ctx);
250
251         return result;
252 }