r12528: Add seperate proto headers for ntvfs, tdr, smb_server and nbt_server.
[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 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 #include "smb_server/smb_server.h"
23 #include "smbd/service_stream.h"
24 #include "ntvfs/ntvfs.h"
25
26
27 /**
28  * Find a service entry. service is always in dos codepage.
29  *
30  * @param service is modified (to canonical form??)
31  **/
32 static int find_service(const char *service)
33 {
34         int iService;
35
36         iService = lp_servicenumber(service);
37
38         if (iService >= 0 && !lp_snum_ok(iService)) {
39                 DEBUG(0,("Invalid snum %d for %s\n",iService, service));
40                 iService = -1;
41         }
42
43         if (iService == -1) {
44                 DEBUG(3,("find_service() failed to find service %s\n", service));
45         }
46
47         return iService;
48 }
49
50
51 /****************************************************************************
52   Make a connection, given the snum to connect to, and the vuser of the
53   connecting user if appropriate.
54 ****************************************************************************/
55 static NTSTATUS make_connection_snum(struct smbsrv_request *req,
56                                      int snum, enum ntvfs_type type,
57                                      DATA_BLOB password, 
58                                      const char *dev)
59 {
60         struct smbsrv_tcon *tcon;
61         NTSTATUS status;
62
63         if (!socket_check_access(req->smb_conn->connection->socket, 
64                                  lp_servicename(snum), 
65                                  lp_hostsallow(snum), 
66                                  lp_hostsdeny(snum))) {
67                 return NT_STATUS_ACCESS_DENIED;
68         }
69
70         tcon = smbsrv_smb_tcon_new(req->smb_conn);
71         if (!tcon) {
72                 DEBUG(0,("Couldn't find free connection.\n"));
73                 return NT_STATUS_INSUFFICIENT_RESOURCES;
74         }
75         req->tcon = tcon;
76
77         tcon->service = snum;
78
79         /* init ntvfs function pointers */
80         status = ntvfs_init_connection(req, type);
81         if (!NT_STATUS_IS_OK(status)) {
82                 DEBUG(0, ("ntvfs_init_connection failed for service %s\n", 
83                           lp_servicename(tcon->service)));
84                 req->tcon = NULL;
85                 talloc_free(tcon);
86                 return status;
87         }
88
89         /* Invoke NTVFS connection hook */
90         status = ntvfs_connect(req, lp_servicename(snum));
91         if (!NT_STATUS_IS_OK(status)) {
92                 DEBUG(0,("make_connection: NTVFS make connection failed!\n"));
93                 req->tcon = NULL;
94                 talloc_free(tcon);
95                 return status;
96         }
97
98         return NT_STATUS_OK;
99 }
100
101 /****************************************************************************
102  Make a connection to a service.
103  *
104  * @param service 
105 ****************************************************************************/
106 static NTSTATUS make_connection(struct smbsrv_request *req,
107                                 const char *service, DATA_BLOB password, 
108                                 const char *dev)
109 {
110         int snum;
111         enum ntvfs_type type;
112         const char *type_str;
113
114         /* TODO: check the password, when it's share level security! */
115
116         /* the service might be of the form \\SERVER\SHARE. Should we put
117            the server name we get from this somewhere? */
118         if (strncmp(service, "\\\\", 2) == 0) {
119                 char *p = strchr(service+2, '\\');
120                 if (p) {
121                         service = p + 1;
122                 }
123         }
124
125         snum = find_service(service);
126
127         if (snum == -1) {
128                 DEBUG(0,("couldn't find service %s\n", service));
129                 return NT_STATUS_BAD_NETWORK_NAME;
130         }
131
132         /* work out what sort of connection this is */
133         if (strcmp(lp_fstype(snum), "IPC") == 0) {
134                 type = NTVFS_IPC;
135                 type_str = "IPC";
136         } else if (lp_print_ok(snum)) {
137                 type = NTVFS_PRINT;
138                 type_str = "LPT:";
139         } else {
140                 type = NTVFS_DISK;
141                 type_str = "A:";
142         }
143
144         if (strcmp(dev, "?????") != 0 && strcasecmp(type_str, dev) != 0) {
145                 /* the client gave us the wrong device type */
146                 return NT_STATUS_BAD_DEVICE_TYPE;
147         }
148
149         return make_connection_snum(req, snum, type, password, dev);
150 }
151
152 /*
153   backend for tree connect call
154 */
155 NTSTATUS tcon_backend(struct smbsrv_request *req, union smb_tcon *con)
156 {
157         NTSTATUS status;
158
159         /* can only do bare tcon in share level security */
160         if (!req->session && lp_security() != SEC_SHARE) {
161                 return NT_STATUS_ACCESS_DENIED;
162         }
163
164         if (con->generic.level == RAW_TCON_TCON) {
165                 DATA_BLOB password;
166                 password = data_blob(con->tcon.in.password, strlen(con->tcon.in.password) + 1);
167
168                 status = make_connection(req, con->tcon.in.service, password, con->tcon.in.dev);
169                 
170                 if (!NT_STATUS_IS_OK(status)) {
171                         return status;
172                 }
173
174                 con->tcon.out.max_xmit = req->smb_conn->negotiate.max_recv;
175                 con->tcon.out.tid = req->tcon->tid;
176
177                 return status;
178         } 
179
180         status = make_connection(req, con->tconx.in.path, con->tconx.in.password, 
181                                  con->tconx.in.device);
182         if (!NT_STATUS_IS_OK(status)) {
183                 return status;
184         }
185
186         con->tconx.out.tid = req->tcon->tid;
187         con->tconx.out.dev_type = talloc_strdup(req, req->tcon->dev_type);
188         con->tconx.out.fs_type = talloc_strdup(req, req->tcon->fs_type);
189         con->tconx.out.options = SMB_SUPPORT_SEARCH_BITS | (lp_csc_policy(req->tcon->service) << 2);
190         if (lp_msdfs_root(req->tcon->service) && lp_host_msdfs()) {
191                 con->tconx.out.options |= SMB_SHARE_IN_DFS;
192         }
193
194         return status;
195 }