This commit was generated by cvs2svn to compensate for changes in r30,
[garming/samba-autobuild/.git] / source4 / smbd / 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         /* now handle the special case of a home directory */
74         if (iService == -1) {
75                 char *phome_dir = get_user_home_dir(service);
76
77                 if(!phome_dir) {
78                         /*
79                          * Try mapping the servicename, it may
80                          * be a Windows to unix mapped user name.
81                          */
82 /* REWRITE:
83                         if (map_username(service))
84                                 phome_dir = get_user_home_dir(service);
85 */
86                 }
87                 
88                 DEBUG(3,("checking for home directory %s gave %s\n",service,
89                          phome_dir?phome_dir:"(NULL)"));
90                 
91                 iService = add_home_service(service,service /* 'username' */, phome_dir);
92         }
93
94         /* If we still don't have a service, attempt to add it as a printer. */
95         if (iService == -1) {
96                 int iPrinterService;
97
98                 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0) {
99                         char *pszTemp;
100
101                         DEBUG(3,("checking whether %s is a valid printer name...\n", service));
102                         pszTemp = lp_printcapname();
103                         if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp)) {
104                                 DEBUG(3,("%s is a valid printer name\n", service));
105                                 DEBUG(3,("adding %s as a printer service\n", service));
106                                 lp_add_printer(service, iPrinterService);
107                                 iService = lp_servicenumber(service);
108                                 if (iService < 0)
109                                         DEBUG(0,("failed to add %s as a printer service!\n", service));
110                         } else {
111                                 DEBUG(3,("%s is not a valid printer name\n", service));
112                         }
113                 }
114         }
115
116         /* Check for default vfs service?  Unsure whether to implement this */
117         if (iService == -1) {
118         }
119
120         /* just possibly it's a default service? */
121         if (iService == -1) {
122                 char *pdefservice = lp_defaultservice();
123                 if (pdefservice && *pdefservice && 
124                     !strequal(pdefservice,service) &&
125                     !strstr(service,"..")) {
126                         /*
127                          * We need to do a local copy here as lp_defaultservice() 
128                          * returns one of the rotating lp_string buffers that
129                          * could get overwritten by the recursive find_service() call
130                          * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
131                          */
132                         pstring defservice;
133                         pstrcpy(defservice, pdefservice);
134                         iService = find_service(defservice);
135                         if (iService >= 0) {
136                                 /* REWRITE: all_string_sub(service, "_","/",0); */
137                                 iService = lp_add_service(service, iService);
138                         }
139                 }
140         }
141
142         if (iService >= 0 && !VALID_SNUM(iService)) {
143                 DEBUG(0,("Invalid snum %d for %s\n",iService, service));
144                 iService = -1;
145         }
146
147         if (iService == -1) {
148                 DEBUG(3,("find_service() failed to find service %s\n", service));
149         }
150
151         return iService;
152 }
153
154
155 /****************************************************************************
156   Make a connection, given the snum to connect to, and the vuser of the
157   connecting user if appropriate.
158 ****************************************************************************/
159 static NTSTATUS make_connection_snum(struct request_context *req,
160                                      int snum, enum ntvfs_type type,
161                                      DATA_BLOB password, 
162                                      const char *dev)
163 {
164         struct tcon_context *conn;
165         NTSTATUS status;
166
167         conn = conn_new(req->smb);
168         if (!conn) {
169                 DEBUG(0,("Couldn't find free connection.\n"));
170                 return NT_STATUS_INSUFFICIENT_RESOURCES;
171         }
172         req->conn = conn;
173
174         conn->service = snum;
175         conn->type = type;
176
177         /*
178          * New code to check if there's a share security descripter
179          * added from NT server manager. This is done after the
180          * smb.conf checks are done as we need a uid and token. JRA.
181          *
182          */
183
184         if (!share_access_check(req, conn, snum, SA_RIGHT_FILE_WRITE_DATA)) {
185                 if (!share_access_check(req, conn, snum, SA_RIGHT_FILE_READ_DATA)) {
186                         /* No access, read or write. */
187                         DEBUG(0,( "make_connection: connection to %s denied due to security descriptor.\n",
188                                   lp_servicename(snum)));
189                         conn_free(req->smb, conn);
190                         return NT_STATUS_ACCESS_DENIED;
191                 } else {
192                         conn->read_only = True;
193                 }
194         }
195
196         /* check number of connections */
197         if (!claim_connection(conn,
198                               lp_servicename(SNUM(conn)),
199                               lp_max_connections(SNUM(conn)),
200                               False,0)) {
201                 DEBUG(1,("too many connections - rejected\n"));
202                 conn_free(req->smb, conn);
203                 return NT_STATUS_INSUFFICIENT_RESOURCES;
204         }  
205
206         /* init ntvfs function pointers */
207         status = ntvfs_init_connection(req);
208         if (!NT_STATUS_IS_OK(status)) {
209                 DEBUG(0, ("ntvfs_init_connection failed for service %s\n", lp_servicename(SNUM(conn))));
210                 conn_free(req->smb, conn);
211                 return status;
212         }
213         
214         /* Invoke NTVFS connection hook */
215         if (conn->ntvfs_ops->connect) {
216                 status = conn->ntvfs_ops->connect(req, lp_servicename(snum));
217                 if (!NT_STATUS_IS_OK(status)) {
218                         DEBUG(0,("make_connection: NTVFS make connection failed!\n"));
219                         conn_free(req->smb, conn);
220                         return status;
221                 }
222         }
223         
224         return NT_STATUS_OK;
225 }
226
227 /****************************************************************************
228  Make a connection to a service.
229  *
230  * @param service 
231 ****************************************************************************/
232 static NTSTATUS make_connection(struct request_context *req,
233                                 const char *service, DATA_BLOB password, 
234                                 const char *dev, uint16 vuid)
235 {
236         int snum;
237         enum ntvfs_type type;
238         const char *type_str;
239
240         /* the service might be of the form \\SERVER\SHARE. Should we put
241            the server name we get from this somewhere? */
242         if (strncmp(service, "\\\\", 2) == 0) {
243                 char *p = strchr(service+2, '\\');
244                 if (p) {
245                         service = p + 1;
246                 }
247         }
248
249         snum = find_service(service);
250
251         if (snum == -1) {
252                 DEBUG(0,("%s couldn't find service %s\n",
253                          sub_get_remote_machine(), service));
254                 return NT_STATUS_BAD_NETWORK_NAME;
255         }
256
257         /* work out what sort of connection this is */
258         if (strcmp(lp_fstype(snum), "IPC") == 0) {
259                 type = NTVFS_IPC;
260                 type_str = "IPC";
261         } else if (lp_print_ok(snum)) {
262                 type = NTVFS_PRINT;
263                 type_str = "LPT:";
264         } else {
265                 type = NTVFS_DISK;
266                 type_str = "A:";
267         }
268
269         if (strcmp(dev, "?????") != 0 && strcasecmp(type_str, dev) != 0) {
270                 /* the client gave us the wrong device type */
271                 return NT_STATUS_BAD_DEVICE_TYPE;
272         }
273
274         return make_connection_snum(req, snum, type, password, dev);
275 }
276
277 /****************************************************************************
278 close a cnum
279 ****************************************************************************/
280 void close_cnum(struct tcon_context *conn)
281 {
282         DEBUG(3, ("%s (%s) closed connection to service %s\n",
283                   sub_get_remote_machine(),conn->smb->socket.client_addr,
284                   lp_servicename(SNUM(conn))));
285
286         yield_connection(conn, lp_servicename(SNUM(conn)));
287
288         /* tell the ntvfs backend that we are disconnecting */
289         conn->ntvfs_ops->disconnect(conn);
290
291         conn_free(conn->smb, conn);
292 }
293
294
295
296 /*
297   backend for tree connect call
298 */
299 NTSTATUS tcon_backend(struct request_context *req, union smb_tcon *con)
300 {
301         NTSTATUS status;
302
303         /* can only do bare tcon in share level security */
304         if (req->user_ctx == NULL && lp_security() != SEC_SHARE) {
305                 return NT_STATUS_ACCESS_DENIED;
306         }
307
308         if (con->generic.level == RAW_TCON_TCON) {
309                 DATA_BLOB password;
310                 password = data_blob(con->tcon.in.password, strlen(con->tcon.in.password) + 1);
311
312                 status = make_connection(req, con->tcon.in.service, password, con->tcon.in.dev, req->user_ctx->vuid);
313                 
314                 if (!NT_STATUS_IS_OK(status)) {
315                         return status;
316                 }
317
318                 con->tcon.out.max_xmit = req->smb->negotiate.max_recv;
319                 con->tcon.out.cnum = req->conn->cnum;
320                 
321                 return status;
322         } 
323
324         status = make_connection(req, con->tconx.in.path, con->tconx.in.password, 
325                                  con->tconx.in.device, req->user_ctx->vuid);
326         if (!NT_STATUS_IS_OK(status)) {
327                 return status;
328         }
329
330         con->tconx.out.cnum = req->conn->cnum;
331         con->tconx.out.dev_type = talloc_strdup(req->mem_ctx, req->conn->dev_type);
332         con->tconx.out.fs_type = talloc_strdup(req->mem_ctx, req->conn->fs_type);
333         con->tconx.out.options = SMB_SUPPORT_SEARCH_BITS | (lp_csc_policy(req->conn->service) << 2);
334         if (lp_msdfs_root(req->conn->service) && lp_host_msdfs()) {
335                 con->tconx.out.options |= SMB_SHARE_IN_DFS;
336         }
337
338         return status;
339 }