ntvfs: pass down the client capabilities into the ntvfs layer
[samba.git] / source4 / smb_server / smb / service.c
1 /* 
2    Unix SMB/CIFS implementation.
3    service (connection) handling
4    Copyright (C) Andrew Tridgell 1992-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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "smb_server/smb_server.h"
22 #include "smbd/service_stream.h"
23 #include "ntvfs/ntvfs.h"
24 #include "param/share.h"
25 #include "param/param.h"
26
27 /****************************************************************************
28   Make a connection, given the snum to connect to, and the vuser of the
29   connecting user if appropriate.
30 ****************************************************************************/
31 static NTSTATUS make_connection_scfg(struct smbsrv_request *req,
32                                      struct share_config *scfg,
33                                      enum ntvfs_type type,
34                                      DATA_BLOB password, 
35                                      const char *dev)
36 {
37         struct smbsrv_tcon *tcon;
38         NTSTATUS status;
39         uint64_t ntvfs_caps = 0;
40
41         tcon = smbsrv_smb_tcon_new(req->smb_conn, scfg->name);
42         if (!tcon) {
43                 DEBUG(0,("Couldn't find free connection.\n"));
44                 return NT_STATUS_INSUFFICIENT_RESOURCES;
45         }
46         req->tcon = tcon;
47
48         if (req->smb_conn->negotiate.client_caps & CAP_LEVEL_II_OPLOCKS) {
49                 ntvfs_caps |= NTVFS_CLIENT_CAP_LEVEL_II_OPLOCKS;
50         }
51
52         /* init ntvfs function pointers */
53         status = ntvfs_init_connection(tcon, scfg, type,
54                                        req->smb_conn->negotiate.protocol,
55                                        ntvfs_caps,
56                                        req->smb_conn->connection->event.ctx,
57                                        req->smb_conn->connection->msg_ctx,
58                                        req->smb_conn->lp_ctx,
59                                        req->smb_conn->connection->server_id,
60                                        &tcon->ntvfs);
61         if (!NT_STATUS_IS_OK(status)) {
62                 DEBUG(0, ("make_connection_scfg: connection failed for service %s\n", 
63                           scfg->name));
64                 goto failed;
65         }
66
67         status = ntvfs_set_oplock_handler(tcon->ntvfs, smbsrv_send_oplock_break, tcon);
68         if (!NT_STATUS_IS_OK(status)) {
69                 DEBUG(0,("make_connection: NTVFS failed to set the oplock handler!\n"));
70                 goto failed;
71         }
72
73         status = ntvfs_set_addr_callbacks(tcon->ntvfs, smbsrv_get_my_addr, smbsrv_get_peer_addr, req->smb_conn);
74         if (!NT_STATUS_IS_OK(status)) {
75                 DEBUG(0,("make_connection: NTVFS failed to set the addr callbacks!\n"));
76                 goto failed;
77         }
78
79         status = ntvfs_set_handle_callbacks(tcon->ntvfs,
80                                             smbsrv_handle_create_new,
81                                             smbsrv_handle_make_valid,
82                                             smbsrv_handle_destroy,
83                                             smbsrv_handle_search_by_wire_key,
84                                             smbsrv_handle_get_wire_key,
85                                             tcon);
86         if (!NT_STATUS_IS_OK(status)) {
87                 DEBUG(0,("make_connection: NTVFS failed to set the handle callbacks!\n"));
88                 goto failed;
89         }
90
91         req->ntvfs = ntvfs_request_create(req->tcon->ntvfs, req,
92                                           req->session->session_info,
93                                           SVAL(req->in.hdr,HDR_PID),
94                                           req->request_time,
95                                           req, NULL, 0);
96         if (!req->ntvfs) {
97                 status = NT_STATUS_NO_MEMORY;
98                 goto failed;
99         }
100
101         /* Invoke NTVFS connection hook */
102         status = ntvfs_connect(req->ntvfs, scfg->name);
103         if (!NT_STATUS_IS_OK(status)) {
104                 DEBUG(0,("make_connection: NTVFS make connection failed!\n"));
105                 goto failed;
106         }
107
108         return NT_STATUS_OK;
109
110 failed:
111         req->tcon = NULL;
112         talloc_free(tcon);
113         return status;
114 }
115
116 /****************************************************************************
117  Make a connection to a service.
118  *
119  * @param service 
120 ****************************************************************************/
121 static NTSTATUS make_connection(struct smbsrv_request *req,
122                                 const char *service, DATA_BLOB password, 
123                                 const char *dev)
124 {
125         NTSTATUS status;
126         enum ntvfs_type type;
127         const char *type_str;
128         struct share_config *scfg;
129         const char *sharetype;
130
131         /* the service might be of the form \\SERVER\SHARE. Should we put
132            the server name we get from this somewhere? */
133         if (strncmp(service, "\\\\", 2) == 0) {
134                 char *p = strchr(service+2, '\\');
135                 if (p) {
136                         service = p + 1;
137                 }
138         }
139
140         status = share_get_config(req, req->smb_conn->share_context, service, &scfg);
141         if (!NT_STATUS_IS_OK(status)) {
142                 DEBUG(0,("make_connection: couldn't find service %s\n", service));
143                 return NT_STATUS_BAD_NETWORK_NAME;
144         }
145
146         /* TODO: check the password, when it's share level security! */
147
148         if (!socket_check_access(req->smb_conn->connection->socket, 
149                                  scfg->name, 
150                                  share_string_list_option(req, scfg, SHARE_HOSTS_ALLOW), 
151                                  share_string_list_option(req, scfg, SHARE_HOSTS_DENY))) {
152                 return NT_STATUS_ACCESS_DENIED;
153         }
154
155         /* work out what sort of connection this is */
156         sharetype = share_string_option(scfg, "type", "DISK");
157         if (sharetype && strcmp(sharetype, "IPC") == 0) {
158                 type = NTVFS_IPC;
159                 type_str = "IPC";
160         } else if (sharetype && strcmp(sharetype, "PRINTER") == 0) {
161                 type = NTVFS_PRINT;
162                 type_str = "LPT:";
163         } else {
164                 type = NTVFS_DISK;
165                 type_str = "A:";
166         }
167
168         if (strcmp(dev, "?????") != 0 && strcasecmp(type_str, dev) != 0) {
169                 /* the client gave us the wrong device type */
170                 return NT_STATUS_BAD_DEVICE_TYPE;
171         }
172
173         return make_connection_scfg(req, scfg, type, password, dev);
174 }
175
176 /*
177   backend for tree connect call
178 */
179 NTSTATUS smbsrv_tcon_backend(struct smbsrv_request *req, union smb_tcon *con)
180 {
181         NTSTATUS status;
182
183         if (con->generic.level == RAW_TCON_TCON) {
184                 DATA_BLOB password;
185                 password = data_blob_string_const(con->tcon.in.password);
186
187                 status = make_connection(req, con->tcon.in.service, password, con->tcon.in.dev);
188                 
189                 if (!NT_STATUS_IS_OK(status)) {
190                         return status;
191                 }
192
193                 con->tcon.out.max_xmit = req->smb_conn->negotiate.max_recv;
194                 con->tcon.out.tid = req->tcon->tid;
195
196                 return status;
197         } 
198
199         /* TODO: take a look at tconx.in.flags! */
200
201         status = make_connection(req, con->tconx.in.path, con->tconx.in.password, 
202                                  con->tconx.in.device);
203         if (!NT_STATUS_IS_OK(status)) {
204                 return status;
205         }
206
207         con->tconx.out.tid = req->tcon->tid;
208         con->tconx.out.dev_type = talloc_strdup(req, req->tcon->ntvfs->dev_type);
209         con->tconx.out.fs_type = talloc_strdup(req, req->tcon->ntvfs->fs_type);
210         con->tconx.out.options = SMB_SUPPORT_SEARCH_BITS | (share_int_option(req->tcon->ntvfs->config, SHARE_CSC_POLICY, SHARE_CSC_POLICY_DEFAULT) << 2);
211         if (share_bool_option(req->tcon->ntvfs->config, SHARE_MSDFS_ROOT, SHARE_MSDFS_ROOT_DEFAULT) && lp_host_msdfs(req->smb_conn->lp_ctx)) {
212                 con->tconx.out.options |= SMB_SHARE_IN_DFS;
213         }
214
215         return status;
216 }