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