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