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