r1429: enable spnego in smbclient too.
[jra/samba/.git] / source4 / libcli / cliconnect.c
1 /*
2    Unix SMB/CIFS implementation.
3    client connect/disconnect routines
4    Copyright (C) Andrew Tridgell 2003
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /*
24   wrapper around cli_sock_connect()
25 */
26 BOOL cli_socket_connect(struct cli_state *cli, const char *server, struct in_addr *ip)
27 {
28         struct cli_socket *sock;
29
30         sock = cli_sock_init();
31         if (!sock) return False;
32
33         if (!cli_sock_connect_byname(sock, server, 0)) {
34                 cli_sock_close(sock);
35                 return False;
36         }
37         
38         cli->transport = cli_transport_init(sock);
39         if (!cli->transport) {
40                 cli_sock_close(sock);
41                 return False;
42         }
43
44         return True;
45 }
46
47 /* wrapper around cli_transport_connect() */
48 BOOL cli_transport_establish(struct cli_state *cli, 
49                              struct nmb_name *calling,
50                              struct nmb_name *called)
51 {
52         return cli_transport_connect(cli->transport, calling, called);
53 }
54
55 /* wrapper around smb_raw_negotiate() */
56 NTSTATUS cli_negprot(struct cli_state *cli)
57 {
58         return smb_raw_negotiate(cli->transport);
59 }
60
61 /* wrapper around smb_raw_session_setup() */
62 NTSTATUS cli_session_setup(struct cli_state *cli, 
63                            const char *user, 
64                            const char *password, 
65                            const char *domain)
66 {
67         union smb_sesssetup setup;
68         NTSTATUS status;
69         TALLOC_CTX *mem_ctx;
70
71         cli->session = cli_session_init(cli->transport);
72         if (!cli->session) return NT_STATUS_UNSUCCESSFUL;
73
74         mem_ctx = talloc_init("cli_session_setup");
75         if (!mem_ctx) return NT_STATUS_NO_MEMORY;
76
77         setup.generic.level = RAW_SESSSETUP_GENERIC;
78         setup.generic.in.sesskey = cli->transport->negotiate.sesskey;
79         setup.generic.in.capabilities = cli->transport->negotiate.capabilities;
80         if (!user || !user[0]) {
81                 setup.generic.in.password = NULL;
82                 setup.generic.in.user = "";
83                 setup.generic.in.domain = "";
84                 setup.generic.in.capabilities &= ~CAP_EXTENDED_SECURITY;
85         } else {
86                 setup.generic.in.password = password;
87                 setup.generic.in.user = user;
88                 setup.generic.in.domain = domain;
89         }
90
91         status = smb_raw_session_setup(cli->session, mem_ctx, &setup);
92
93         cli->session->vuid = setup.generic.out.vuid;
94
95         talloc_destroy(mem_ctx);
96
97         return status;
98 }
99
100 /* wrapper around smb_tree_connect() */
101 NTSTATUS cli_send_tconX(struct cli_state *cli, const char *sharename, 
102                         const char *devtype, const char *password)
103 {
104         union smb_tcon tcon;
105         TALLOC_CTX *mem_ctx;
106         NTSTATUS status;
107
108         cli->tree = cli_tree_init(cli->session);
109         if (!cli->tree) return NT_STATUS_UNSUCCESSFUL;
110
111         cli->tree->reference_count++;
112
113         /* setup a tree connect */
114         tcon.generic.level = RAW_TCON_TCONX;
115         tcon.tconx.in.flags = 0;
116         tcon.tconx.in.password = data_blob(password, strlen(password)+1);
117         tcon.tconx.in.path = sharename;
118         tcon.tconx.in.device = devtype;
119         
120         mem_ctx = talloc_init("tcon");
121         if (!mem_ctx)
122                 return NT_STATUS_NO_MEMORY;
123
124         status = smb_tree_connect(cli->tree, mem_ctx, &tcon);
125
126         cli->tree->tid = tcon.tconx.out.cnum;
127
128         talloc_destroy(mem_ctx);
129
130         return status;
131 }
132
133
134 /*
135   easy way to get to a fully connected cli_state in one call
136 */
137 NTSTATUS cli_full_connection(struct cli_state **ret_cli, 
138                              const char *myname,
139                              const char *host,
140                              struct in_addr *ip,
141                              const char *sharename,
142                              const char *devtype,
143                              const char *username,
144                              const char *domain,
145                              const char *password,
146                              uint_t flags,
147                              BOOL *retry)
148 {
149         struct cli_tree *tree;
150         NTSTATUS status;
151         char *p;
152         TALLOC_CTX *mem_ctx;
153
154         mem_ctx = talloc_init("cli_full_connection");
155
156         *ret_cli = NULL;
157
158         /* if the username is of the form DOMAIN\username then split out the domain */
159         p = strpbrk(username, "\\/");
160         if (p) {
161                 domain = talloc_strndup(mem_ctx, username, PTR_DIFF(p, username));
162                 username = talloc_strdup(mem_ctx, p+1);
163         }
164
165         status = cli_tree_full_connection(&tree, myname, host, 0, sharename, devtype,
166                                           username, domain, password);
167         if (!NT_STATUS_IS_OK(status)) {
168                 goto done;
169         }
170
171         (*ret_cli) = cli_state_init();
172
173         (*ret_cli)->tree = tree;
174         (*ret_cli)->session = tree->session;
175         (*ret_cli)->transport = tree->session->transport;
176         tree->reference_count++;
177
178 done:
179         talloc_destroy(mem_ctx);
180         return status;
181 }
182
183
184 /*
185   disconnect the tree
186 */
187 NTSTATUS cli_tdis(struct cli_state *cli)
188 {
189         return smb_tree_disconnect(cli->tree);
190 }
191
192 /****************************************************************************
193  Initialise a client state structure.
194 ****************************************************************************/
195 struct cli_state *cli_state_init(void)
196 {
197         struct cli_state *cli;
198         TALLOC_CTX *mem_ctx;
199
200         mem_ctx = talloc_init("cli_state");
201         if (!mem_ctx) return NULL;
202
203         cli = talloc_zero(mem_ctx, sizeof(*cli));
204         cli->mem_ctx = mem_ctx;
205
206         return cli;
207 }
208
209 /****************************************************************************
210  Shutdown a client structure.
211 ****************************************************************************/
212 void cli_shutdown(struct cli_state *cli)
213 {
214         if (!cli) return;
215         if (cli->tree) {
216                 cli->tree->reference_count++;
217                 cli_tree_close(cli->tree);
218         }
219         if (cli->mem_ctx) {
220                 talloc_destroy(cli->mem_ctx);
221         }
222 }