Merge 2610c05b5b95cc7036b3d6dfb894c6cfbdb68483 as Samba-4.0alpha16
[kai/samba.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 #include "libcli/raw/raw_proto.h"
31
32 struct sock_connect_state {
33         struct composite_context *ctx;
34         const char *host_name;
35         int num_ports;
36         uint16_t *ports;
37         const char *socket_options;
38         struct smbcli_socket *result;
39 };
40
41 /*
42   connect a smbcli_socket context to an IP/port pair
43   if port is 0 then choose 445 then 139
44 */
45
46 static void smbcli_sock_connect_recv_conn(struct composite_context *ctx);
47
48 struct composite_context *smbcli_sock_connect_send(TALLOC_CTX *mem_ctx,
49                                                    const char *host_addr,
50                                                    const char **ports,
51                                                    const char *host_name,
52                                                    struct resolve_context *resolve_ctx,
53                                                    struct tevent_context *event_ctx,
54                                                    const char *socket_options)
55 {
56         struct composite_context *result, *ctx;
57         struct sock_connect_state *state;
58         int i;
59
60         result = talloc_zero(mem_ctx, struct composite_context);
61         if (result == NULL) goto failed;
62         result->state = COMPOSITE_STATE_IN_PROGRESS;
63
64         result->event_ctx = event_ctx;
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         state->num_ports = str_list_length(ports);
76         state->ports = talloc_array(state, uint16_t, state->num_ports);
77         if (state->ports == NULL) goto failed;
78         for (i=0;ports[i];i++) {
79                 state->ports[i] = atoi(ports[i]);
80         }
81         state->socket_options = talloc_reference(state, socket_options);
82
83         if (!host_addr) {
84                 host_addr = host_name;
85         }
86
87         ctx = socket_connect_multi_send(state, host_addr,
88                                         state->num_ports, state->ports,
89                                         resolve_ctx,
90                                         state->ctx->event_ctx);
91         if (ctx == NULL) goto failed;
92         ctx->async.fn = smbcli_sock_connect_recv_conn;
93         ctx->async.private_data = state;
94         return result;
95
96 failed:
97         talloc_free(result);
98         return NULL;
99 }
100
101 static void smbcli_sock_connect_recv_conn(struct composite_context *ctx)
102 {
103         struct sock_connect_state *state =
104                 talloc_get_type(ctx->async.private_data,
105                                 struct sock_connect_state);
106         struct socket_context *sock;
107         uint16_t port;
108
109         state->ctx->status = socket_connect_multi_recv(ctx, state, &sock,
110                                                        &port);
111         if (!composite_is_ok(state->ctx)) return;
112
113         state->ctx->status =
114                 socket_set_option(sock, state->socket_options, NULL);
115         if (!composite_is_ok(state->ctx)) return;
116
117
118         state->result = talloc_zero(state, struct smbcli_socket);
119         if (composite_nomem(state->result, state->ctx)) return;
120
121         state->result->sock = talloc_steal(state->result, sock);
122         state->result->port = port;
123         state->result->hostname = talloc_steal(sock, state->host_name);
124
125         state->result->event.ctx = state->ctx->event_ctx;
126         if (composite_nomem(state->result->event.ctx, state->ctx)) return;
127
128         composite_done(state->ctx);
129 }
130
131 /*
132   finish a smbcli_sock_connect_send() operation
133 */
134 NTSTATUS smbcli_sock_connect_recv(struct composite_context *c,
135                                   TALLOC_CTX *mem_ctx,
136                                   struct smbcli_socket **result)
137 {
138         NTSTATUS status = composite_wait(c);
139         if (NT_STATUS_IS_OK(status)) {
140                 struct sock_connect_state *state =
141                         talloc_get_type(c->private_data,
142                                         struct sock_connect_state);
143                 *result = talloc_steal(mem_ctx, state->result);
144         }
145         talloc_free(c);
146         return status;
147 }
148
149 /*
150   connect a smbcli_socket context to an IP/port pair
151   if port is 0 then choose the ports listed in smb.conf (normally 445 then 139)
152
153   sync version of the function
154 */
155 NTSTATUS smbcli_sock_connect(TALLOC_CTX *mem_ctx,
156                              const char *host_addr, const char **ports,
157                              const char *host_name,
158                              struct resolve_context *resolve_ctx,
159                              struct tevent_context *event_ctx,
160                                  const char *socket_options,
161                              struct smbcli_socket **result)
162 {
163         struct composite_context *c =
164                 smbcli_sock_connect_send(mem_ctx, host_addr, ports, host_name,
165                                          resolve_ctx,
166                                          event_ctx, socket_options);
167         return smbcli_sock_connect_recv(c, mem_ctx, result);
168 }
169
170
171 /****************************************************************************
172  mark the socket as dead
173 ****************************************************************************/
174 _PUBLIC_ void smbcli_sock_dead(struct smbcli_socket *sock)
175 {
176         talloc_free(sock->event.fde);
177         sock->event.fde = NULL;
178         talloc_free(sock->sock);
179         sock->sock = NULL;
180 }
181
182 /****************************************************************************
183  Set socket options on a open connection.
184 ****************************************************************************/
185 void smbcli_sock_set_options(struct smbcli_socket *sock, const char *options)
186 {
187         socket_set_option(sock->sock, options, NULL);
188 }
189
190 /****************************************************************************
191 resolve a hostname and connect 
192 ****************************************************************************/
193 _PUBLIC_ struct smbcli_socket *smbcli_sock_connect_byname(const char *host, const char **ports,
194                                                  TALLOC_CTX *mem_ctx,
195                                                  struct resolve_context *resolve_ctx,
196                                                  struct tevent_context *event_ctx,
197                                                  const char *socket_options)
198 {
199         int name_type = NBT_NAME_SERVER;
200         const char *address;
201         NTSTATUS status;
202         struct nbt_name nbt_name;
203         char *name, *p;
204         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
205         struct smbcli_socket *result;
206
207         if (event_ctx == NULL) {
208                 DEBUG(0, ("Invalid NULL event context passed in as parameter\n"));
209                 return NULL;
210         }
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         /* allow hostnames of the form NAME#xx and do a netbios lookup */
225         if ((p = strchr(name, '#'))) {
226                 name_type = strtol(p+1, NULL, 16);
227                 *p = 0;
228         }
229
230         make_nbt_name(&nbt_name, host, name_type);
231         
232         status = resolve_name_ex(resolve_ctx, 0, 0, &nbt_name, tmp_ctx, &address, event_ctx);
233         if (!NT_STATUS_IS_OK(status)) {
234                 talloc_free(tmp_ctx);
235                 return NULL;
236         }
237
238         status = smbcli_sock_connect(mem_ctx, address, ports, name, resolve_ctx,
239                                      event_ctx, 
240                                          socket_options, &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 }