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