first public release of samba4 code
[gd/samba-autobuild/.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 BOOL cli_negprot(struct cli_state *cli)
57 {
58         NTSTATUS status;
59         status = smb_raw_negotiate(cli->transport);
60         return NT_STATUS_IS_OK(status);
61 }
62
63 /* wrapper around smb_raw_session_setup() */
64 BOOL cli_session_setup(struct cli_state *cli, 
65                        const char *user, 
66                        const char *password, 
67                        const char *domain)
68 {
69         union smb_sesssetup setup;
70         NTSTATUS status;
71         TALLOC_CTX *mem_ctx;
72
73         cli->session = cli_session_init(cli->transport);
74         if (!cli->session) return False;
75
76         mem_ctx = talloc_init("cli_session_setup");
77         if (!mem_ctx) return False;
78
79         setup.generic.level = RAW_SESSSETUP_GENERIC;
80         setup.generic.in.sesskey = cli->transport->negotiate.sesskey;
81         setup.generic.in.capabilities = CAP_UNICODE | CAP_STATUS32 | 
82                 CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS | 
83                 CAP_W2K_SMBS | CAP_LARGE_READX | CAP_LARGE_WRITEX;
84         setup.generic.in.password = password;
85         setup.generic.in.user = user;
86         setup.generic.in.domain = domain;
87
88         status = smb_raw_session_setup(cli->session, mem_ctx, &setup);
89
90         cli->session->vuid = setup.generic.out.vuid;
91
92         talloc_destroy(mem_ctx);
93
94         return NT_STATUS_IS_OK(status);
95 }
96
97 /* wrapper around smb_tree_connect() */
98 BOOL cli_send_tconX(struct cli_state *cli, const char *sharename, const char *devtype,
99                     const char *password)
100 {
101         union smb_tcon tcon;
102         TALLOC_CTX *mem_ctx;
103         NTSTATUS status;
104
105         cli->tree = cli_tree_init(cli->session);
106         if (!cli->tree) return False;
107
108         cli->tree->reference_count++;
109
110         /* setup a tree connect */
111         tcon.generic.level = RAW_TCON_TCONX;
112         tcon.tconx.in.flags = 0;
113         tcon.tconx.in.password = data_blob(password, strlen(password)+1);
114         tcon.tconx.in.path = sharename;
115         tcon.tconx.in.device = devtype;
116         
117         mem_ctx = talloc_init("tcon");
118         if (!mem_ctx) {
119                 return False;
120         }
121
122         status = smb_tree_connect(cli->tree, mem_ctx, &tcon);
123
124         cli->tree->tid = tcon.tconx.out.cnum;
125
126         talloc_destroy(mem_ctx);
127
128         return NT_STATUS_IS_OK(status);
129 }
130
131
132 /*
133   easy way to get to a fully connected cli_state in one call
134 */
135 NTSTATUS cli_full_connection(struct cli_state **ret_cli, 
136                              const char *myname,
137                              const char *host,
138                              struct in_addr *ip,
139                              const char *sharename,
140                              const char *devtype,
141                              const char *username,
142                              const char *domain,
143                              const char *password,
144                              uint_t flags,
145                              BOOL *retry)
146 {
147         struct cli_tree *tree;
148         NTSTATUS status;
149
150         *ret_cli = NULL;
151
152         status = cli_tree_full_connection(&tree, myname, host, 0, sharename, devtype,
153                                           username, domain, password);
154         if (!NT_STATUS_IS_OK(status)) {
155                 return status;
156         }
157
158         (*ret_cli) = cli_state_init();
159
160         (*ret_cli)->tree = tree;
161         (*ret_cli)->session = tree->session;
162         (*ret_cli)->transport = tree->session->transport;
163         tree->reference_count++;
164
165         return status;
166 }
167
168
169 /*
170   disconnect the tree
171 */
172 BOOL cli_tdis(struct cli_state *cli)
173 {
174         NTSTATUS status;
175         status = smb_tree_disconnect(cli->tree);
176         return NT_STATUS_IS_OK(status);
177 }
178
179 /****************************************************************************
180  Initialise a client state structure.
181 ****************************************************************************/
182 struct cli_state *cli_state_init(void)
183 {
184         struct cli_state *cli;
185         TALLOC_CTX *mem_ctx;
186
187         mem_ctx = talloc_init("cli_state");
188         if (!mem_ctx) return NULL;
189
190         cli = talloc_zero(mem_ctx, sizeof(*cli));
191         cli->mem_ctx = mem_ctx;
192
193         return cli;
194 }
195
196 /****************************************************************************
197  Shutdown a client structure.
198 ****************************************************************************/
199 void cli_shutdown(struct cli_state *cli)
200 {
201         if (!cli) return;
202         cli->tree->reference_count++;
203         cli_tree_close(cli->tree);
204         if (cli->mem_ctx) {
205                 talloc_destroy(cli->mem_ctx);
206         }
207 }