a7b1c333957de42c4979c490c3a0e5e8e2c6e0ac
[jelmer/samba4-debian.git] / source / smb_server / 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
23
24 /****************************************************************************
25  Add a home service. Returns the new service number or -1 if fail.
26 ****************************************************************************/
27 int add_home_service(const char *service, const char *username, const char *homedir)
28 {
29         int iHomeService;
30
31         if (!service || !homedir)
32                 return -1;
33
34         if ((iHomeService = lp_servicenumber(HOMES_NAME)) < 0)
35                 return -1;
36
37         /*
38          * If this is a winbindd provided username, remove
39          * the domain component before adding the service.
40          * Log a warning if the "path=" parameter does not
41          * include any macros.
42          */
43
44         {
45                 const char *p = strchr(service,*lp_winbind_separator());
46
47                 /* We only want the 'user' part of the string */
48                 if (p) {
49                         service = p + 1;
50                 }
51         }
52
53         if (!lp_add_home(service, iHomeService, username, homedir)) {
54                 return -1;
55         }
56         
57         return lp_servicenumber(service);
58
59 }
60
61
62 /**
63  * Find a service entry. service is always in dos codepage.
64  *
65  * @param service is modified (to canonical form??)
66  **/
67 static int find_service(const char *service)
68 {
69         int iService;
70
71         iService = lp_servicenumber(service);
72
73         /* If we still don't have a service, attempt to add it as a printer. */
74         if (iService == -1) {
75                 int iPrinterService;
76
77                 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0) {
78                         const char *pszTemp;
79
80                         DEBUG(3,("checking whether %s is a valid printer name...\n", service));
81                         pszTemp = lp_printcapname();
82                         if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp)) {
83                                 DEBUG(3,("%s is a valid printer name\n", service));
84                                 DEBUG(3,("adding %s as a printer service\n", service));
85                                 lp_add_printer(service, iPrinterService);
86                                 iService = lp_servicenumber(service);
87                                 if (iService < 0)
88                                         DEBUG(0,("failed to add %s as a printer service!\n", service));
89                         } else {
90                                 DEBUG(3,("%s is not a valid printer name\n", service));
91                         }
92                 }
93         }
94
95         /* Check for default vfs service?  Unsure whether to implement this */
96         if (iService == -1) {
97         }
98
99         /* just possibly it's a default service? */
100         if (iService == -1) {
101                 char *pdefservice = lp_defaultservice();
102                 if (pdefservice && *pdefservice && 
103                     !strequal(pdefservice,service) &&
104                     !strstr(service,"..")) {
105                         /*
106                          * We need to do a local copy here as lp_defaultservice() 
107                          * returns one of the rotating lp_string buffers that
108                          * could get overwritten by the recursive find_service() call
109                          * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
110                          */
111                         pstring defservice;
112                         pstrcpy(defservice, pdefservice);
113                         iService = find_service(defservice);
114                         if (iService >= 0) {
115                                 /* REWRITE: all_string_sub(service, "_","/",0); */
116                                 iService = lp_add_service(service, iService);
117                         }
118                 }
119         }
120
121         if (iService >= 0 && !VALID_SNUM(iService)) {
122                 DEBUG(0,("Invalid snum %d for %s\n",iService, service));
123                 iService = -1;
124         }
125
126         if (iService == -1) {
127                 DEBUG(3,("find_service() failed to find service %s\n", service));
128         }
129
130         return iService;
131 }
132
133
134 /****************************************************************************
135   Make a connection, given the snum to connect to, and the vuser of the
136   connecting user if appropriate.
137 ****************************************************************************/
138 static NTSTATUS make_connection_snum(struct request_context *req,
139                                      int snum, enum ntvfs_type type,
140                                      DATA_BLOB password, 
141                                      const char *dev)
142 {
143         struct tcon_context *conn;
144         NTSTATUS status;
145
146         conn = conn_new(req->smb_ctx);
147         if (!conn) {
148                 DEBUG(0,("Couldn't find free connection.\n"));
149                 return NT_STATUS_INSUFFICIENT_RESOURCES;
150         }
151         req->conn = conn;
152
153         conn->service = snum;
154         conn->type = type;
155
156         /*
157          * New code to check if there's a share security descripter
158          * added from NT server manager. This is done after the
159          * smb.conf checks are done as we need a uid and token. JRA.
160          *
161          */
162
163         if (!share_access_check(req, conn, snum, SA_RIGHT_FILE_WRITE_DATA)) {
164                 if (!share_access_check(req, conn, snum, SA_RIGHT_FILE_READ_DATA)) {
165                         /* No access, read or write. */
166                         DEBUG(0,( "make_connection: connection to %s denied due to security descriptor.\n",
167                                   lp_servicename(snum)));
168                         conn_free(req->smb_ctx, conn);
169                         return NT_STATUS_ACCESS_DENIED;
170                 } else {
171                         conn->read_only = True;
172                 }
173         }
174
175         /* check number of connections */
176         if (!claim_connection(conn,
177                               lp_servicename(SNUM(conn)),
178                               lp_max_connections(SNUM(conn)),
179                               False,0)) {
180                 DEBUG(1,("too many connections - rejected\n"));
181                 conn_free(req->smb_ctx, conn);
182                 return NT_STATUS_INSUFFICIENT_RESOURCES;
183         }  
184
185         /* init ntvfs function pointers */
186         status = ntvfs_init_connection(req);
187         if (!NT_STATUS_IS_OK(status)) {
188                 DEBUG(0, ("ntvfs_init_connection failed for service %s\n", lp_servicename(SNUM(conn))));
189                 conn_free(req->smb_ctx, conn);
190                 return status;
191         }
192         
193         /* Invoke NTVFS connection hook */
194         if (conn->ntvfs_ops->connect) {
195                 status = conn->ntvfs_ops->connect(req, lp_servicename(snum));
196                 if (!NT_STATUS_IS_OK(status)) {
197                         DEBUG(0,("make_connection: NTVFS make connection failed!\n"));
198                         conn_free(req->smb_ctx, conn);
199                         return status;
200                 }
201         }
202         
203         return NT_STATUS_OK;
204 }
205
206 /****************************************************************************
207  Make a connection to a service.
208  *
209  * @param service 
210 ****************************************************************************/
211 static NTSTATUS make_connection(struct request_context *req,
212                                 const char *service, DATA_BLOB password, 
213                                 const char *dev, uint16_t vuid)
214 {
215         int snum;
216         enum ntvfs_type type;
217         const char *type_str;
218
219         /* the service might be of the form \\SERVER\SHARE. Should we put
220            the server name we get from this somewhere? */
221         if (strncmp(service, "\\\\", 2) == 0) {
222                 char *p = strchr(service+2, '\\');
223                 if (p) {
224                         service = p + 1;
225                 }
226         }
227
228         snum = find_service(service);
229
230         if (snum == -1) {
231                 DEBUG(0,("%s couldn't find service %s\n",
232                          sub_get_remote_machine(), service));
233                 return NT_STATUS_BAD_NETWORK_NAME;
234         }
235
236         /* work out what sort of connection this is */
237         if (strcmp(lp_fstype(snum), "IPC") == 0) {
238                 type = NTVFS_IPC;
239                 type_str = "IPC";
240         } else if (lp_print_ok(snum)) {
241                 type = NTVFS_PRINT;
242                 type_str = "LPT:";
243         } else {
244                 type = NTVFS_DISK;
245                 type_str = "A:";
246         }
247
248         if (strcmp(dev, "?????") != 0 && strcasecmp(type_str, dev) != 0) {
249                 /* the client gave us the wrong device type */
250                 return NT_STATUS_BAD_DEVICE_TYPE;
251         }
252
253         return make_connection_snum(req, snum, type, password, dev);
254 }
255
256 /****************************************************************************
257 close a cnum
258 ****************************************************************************/
259 void close_cnum(struct tcon_context *conn)
260 {
261         DEBUG(3,("%s closed connection to service %s\n",
262                  conn->smb_ctx->socket.client_addr, lp_servicename(SNUM(conn))));
263
264         yield_connection(conn, lp_servicename(SNUM(conn)));
265
266         /* tell the ntvfs backend that we are disconnecting */
267         conn->ntvfs_ops->disconnect(conn);
268
269         conn_free(conn->smb_ctx, conn);
270 }
271
272
273
274 /*
275   backend for tree connect call
276 */
277 NTSTATUS tcon_backend(struct request_context *req, union smb_tcon *con)
278 {
279         NTSTATUS status;
280
281         /* can only do bare tcon in share level security */
282         if (req->user_ctx == NULL && lp_security() != SEC_SHARE) {
283                 return NT_STATUS_ACCESS_DENIED;
284         }
285
286         if (con->generic.level == RAW_TCON_TCON) {
287                 DATA_BLOB password;
288                 password = data_blob(con->tcon.in.password, strlen(con->tcon.in.password) + 1);
289
290                 status = make_connection(req, con->tcon.in.service, password, con->tcon.in.dev, req->user_ctx->vuid);
291                 
292                 if (!NT_STATUS_IS_OK(status)) {
293                         return status;
294                 }
295
296                 con->tcon.out.max_xmit = req->smb_ctx->negotiate.max_recv;
297                 con->tcon.out.cnum = req->conn->cnum;
298                 
299                 return status;
300         } 
301
302         status = make_connection(req, con->tconx.in.path, con->tconx.in.password, 
303                                  con->tconx.in.device, req->user_ctx->vuid);
304         if (!NT_STATUS_IS_OK(status)) {
305                 return status;
306         }
307
308         con->tconx.out.cnum = req->conn->cnum;
309         con->tconx.out.dev_type = talloc_strdup(req->mem_ctx, req->conn->dev_type);
310         con->tconx.out.fs_type = talloc_strdup(req->mem_ctx, req->conn->fs_type);
311         con->tconx.out.options = SMB_SUPPORT_SEARCH_BITS | (lp_csc_policy(req->conn->service) << 2);
312         if (lp_msdfs_root(req->conn->service) && lp_host_msdfs()) {
313                 con->tconx.out.options |= SMB_SHARE_IN_DFS;
314         }
315
316         return status;
317 }