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