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