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