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