Merge commit 'release-4-0-0alpha1' into v4-0-test
[ira/wip.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                                                    int port,
49                                                    const char *host_name,
50                                                    struct event_context *event_ctx)
51 {
52         struct composite_context *result, *ctx;
53         struct sock_connect_state *state;
54
55         result = talloc_zero(mem_ctx, struct composite_context);
56         if (result == NULL) goto failed;
57         result->state = COMPOSITE_STATE_IN_PROGRESS;
58
59         if (event_ctx != NULL) {
60                 result->event_ctx = talloc_reference(result, event_ctx);
61         } else {
62                 result->event_ctx = event_context_init(result);
63         }
64
65         if (result->event_ctx == NULL) goto failed;
66
67         state = talloc(result, struct sock_connect_state);
68         if (state == NULL) goto failed;
69         state->ctx = result;
70         result->private_data = state;
71
72         state->host_name = talloc_strdup(state, host_name);
73         if (state->host_name == NULL) goto failed;
74
75         if (port == 0) {
76                 const char **ports = lp_smb_ports(global_loadparm);
77                 int i;
78
79                 for (i=0;ports[i];i++) /* noop */ ;
80                 if (i == 0) {
81                         DEBUG(3, ("no smb ports defined\n"));
82                         goto failed;
83                 }
84                 state->num_ports = i;
85                 state->ports = talloc_array(state, uint16_t, i);
86                 if (state->ports == NULL) goto failed;
87                 for (i=0;ports[i];i++) {
88                         state->ports[i] = atoi(ports[i]);
89                 }
90         } else {
91                 state->ports = talloc_array(state, uint16_t, 1);
92                 if (state->ports == NULL) goto failed;
93                 state->num_ports = 1;
94                 state->ports[0] = port;
95         }
96
97         ctx = socket_connect_multi_send(state, host_addr,
98                                         state->num_ports, state->ports,
99                                         state->ctx->event_ctx);
100         if (ctx == NULL) goto failed;
101         ctx->async.fn = smbcli_sock_connect_recv_conn;
102         ctx->async.private_data = state;
103         return result;
104
105 failed:
106         talloc_free(result);
107         return NULL;
108 }
109
110 static void smbcli_sock_connect_recv_conn(struct composite_context *ctx)
111 {
112         struct sock_connect_state *state =
113                 talloc_get_type(ctx->async.private_data,
114                                 struct sock_connect_state);
115         struct socket_context *sock;
116         uint16_t port;
117
118         state->ctx->status = socket_connect_multi_recv(ctx, state, &sock,
119                                                        &port);
120         if (!composite_is_ok(state->ctx)) return;
121
122         state->ctx->status =
123                 socket_set_option(sock, lp_socket_options(global_loadparm), NULL);
124         if (!composite_is_ok(state->ctx)) return;
125
126
127         state->result = talloc_zero(state, struct smbcli_socket);
128         if (composite_nomem(state->result, state->ctx)) return;
129
130         state->result->sock = talloc_steal(state->result, sock);
131         state->result->port = port;
132         state->result->hostname = talloc_steal(sock, state->host_name);
133
134         state->result->event.ctx =
135                 talloc_reference(state->result, state->ctx->event_ctx);
136         if (composite_nomem(state->result->event.ctx, state->ctx)) return;
137
138         composite_done(state->ctx);
139 }
140
141 /*
142   finish a smbcli_sock_connect_send() operation
143 */
144 NTSTATUS smbcli_sock_connect_recv(struct composite_context *c,
145                                   TALLOC_CTX *mem_ctx,
146                                   struct smbcli_socket **result)
147 {
148         NTSTATUS status = composite_wait(c);
149         if (NT_STATUS_IS_OK(status)) {
150                 struct sock_connect_state *state =
151                         talloc_get_type(c->private_data,
152                                         struct sock_connect_state);
153                 *result = talloc_steal(mem_ctx, state->result);
154         }
155         talloc_free(c);
156         return status;
157 }
158
159 /*
160   connect a smbcli_socket context to an IP/port pair
161   if port is 0 then choose the ports listed in smb.conf (normally 445 then 139)
162
163   sync version of the function
164 */
165 NTSTATUS smbcli_sock_connect(TALLOC_CTX *mem_ctx,
166                              const char *host_addr, int port,
167                              const char *host_name,
168                              struct event_context *event_ctx,
169                              struct smbcli_socket **result)
170 {
171         struct composite_context *c =
172                 smbcli_sock_connect_send(mem_ctx, host_addr, port, host_name,
173                                          event_ctx);
174         return smbcli_sock_connect_recv(c, mem_ctx, result);
175 }
176
177
178 /****************************************************************************
179  mark the socket as dead
180 ****************************************************************************/
181 void smbcli_sock_dead(struct smbcli_socket *sock)
182 {
183         talloc_free(sock->event.fde);
184         sock->event.fde = NULL;
185         talloc_free(sock->sock);
186         sock->sock = NULL;
187 }
188
189 /****************************************************************************
190  Set socket options on a open connection.
191 ****************************************************************************/
192 void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
193 {
194         socket_set_option(sock->sock, options, NULL);
195 }
196
197 /****************************************************************************
198 resolve a hostname and connect 
199 ****************************************************************************/
200 struct smbcli_socket *smbcli_sock_connect_byname(const char *host, int port,
201                                                  TALLOC_CTX *mem_ctx,
202                                                  struct event_context *event_ctx)
203 {
204         int name_type = NBT_NAME_SERVER;
205         const char *address;
206         NTSTATUS status;
207         struct nbt_name nbt_name;
208         char *name, *p;
209         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
210         struct smbcli_socket *result;
211
212         if (tmp_ctx == NULL) {
213                 DEBUG(0, ("talloc_new failed\n"));
214                 return NULL;
215         }
216
217         name = talloc_strdup(tmp_ctx, host);
218         if (name == NULL) {
219                 DEBUG(0, ("talloc_strdup failed\n"));
220                 talloc_free(tmp_ctx);
221                 return NULL;
222         }
223
224         if (event_ctx == NULL) {
225                 event_ctx = event_context_init(mem_ctx);
226         }
227
228         if (event_ctx == NULL) {
229                 DEBUG(0, ("event_context_init failed\n"));
230                 talloc_free(tmp_ctx);
231                 return NULL;
232         }
233
234         /* allow hostnames of the form NAME#xx and do a netbios lookup */
235         if ((p = strchr(name, '#'))) {
236                 name_type = strtol(p+1, NULL, 16);
237                 *p = 0;
238         }
239
240         make_nbt_name(&nbt_name, host, name_type);
241         
242         status = resolve_name(&nbt_name, tmp_ctx, &address, event_ctx);
243         if (!NT_STATUS_IS_OK(status)) {
244                 talloc_free(tmp_ctx);
245                 return NULL;
246         }
247
248         status = smbcli_sock_connect(mem_ctx, address, port, name, event_ctx,
249                                      &result);
250
251         if (!NT_STATUS_IS_OK(status)) {
252                 DEBUG(9, ("smbcli_sock_connect failed: %s\n",
253                           nt_errstr(status)));
254                 talloc_free(tmp_ctx);
255                 return NULL;
256         }
257
258         talloc_free(tmp_ctx);
259
260         return result;
261 }