2 Unix SMB/CIFS implementation.
3 service (connection) handling
4 Copyright (C) Andrew Tridgell 1992-2003
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.
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.
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.
22 #include "smb_server/smb_server.h"
26 /****************************************************************************
27 Add a home service. Returns the new service number or -1 if fail.
28 ****************************************************************************/
29 int add_home_service(const char *service, const char *username, const char *homedir)
33 if (!service || !homedir)
36 if ((iHomeService = lp_servicenumber(HOMES_NAME)) < 0)
40 * If this is a winbindd provided username, remove
41 * the domain component before adding the service.
42 * Log a warning if the "path=" parameter does not
47 const char *p = strchr(service,*lp_winbind_separator());
49 /* We only want the 'user' part of the string */
55 if (!lp_add_home(service, iHomeService, username, homedir)) {
59 return lp_servicenumber(service);
65 * Find a service entry. service is always in dos codepage.
67 * @param service is modified (to canonical form??)
69 static int find_service(const char *service)
73 iService = lp_servicenumber(service);
75 /* If we still don't have a service, attempt to add it as a printer. */
79 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0) {
82 DEBUG(3,("checking whether %s is a valid printer name...\n", service));
83 pszTemp = lp_printcapname();
84 if ((pszTemp != NULL) && pcap_printername_ok(service, pszTemp)) {
85 DEBUG(3,("%s is a valid printer name\n", service));
86 DEBUG(3,("adding %s as a printer service\n", service));
87 lp_add_printer(service, iPrinterService);
88 iService = lp_servicenumber(service);
90 DEBUG(0,("failed to add %s as a printer service!\n", service));
92 DEBUG(3,("%s is not a valid printer name\n", service));
97 /* Check for default vfs service? Unsure whether to implement this */
101 /* just possibly it's a default service? */
102 if (iService == -1) {
103 const char *pdefservice = lp_defaultservice();
104 if (pdefservice && *pdefservice &&
105 !strequal(pdefservice,service) &&
106 !strstr(service,"..")) {
108 * We need to do a local copy here as lp_defaultservice()
109 * returns one of the rotating lp_string buffers that
110 * could get overwritten by the recursive find_service() call
111 * below. Fix from Josef Hinteregger <joehtg@joehtg.co.at>.
114 pstrcpy(defservice, pdefservice);
115 iService = find_service(defservice);
117 /* REWRITE: all_string_sub(service, "_","/",0); */
118 iService = lp_add_service(service, iService);
123 if (iService >= 0 && !VALID_SNUM(iService)) {
124 DEBUG(0,("Invalid snum %d for %s\n",iService, service));
128 if (iService == -1) {
129 DEBUG(3,("find_service() failed to find service %s\n", service));
136 /****************************************************************************
137 Make a connection, given the snum to connect to, and the vuser of the
138 connecting user if appropriate.
139 ****************************************************************************/
140 static NTSTATUS make_connection_snum(struct smbsrv_request *req,
141 int snum, enum ntvfs_type type,
145 struct smbsrv_tcon *tcon;
148 if (!socket_check_access(req->smb_conn->connection->socket,
149 lp_servicename(snum),
151 lp_hostsdeny(snum))) {
152 return NT_STATUS_ACCESS_DENIED;
155 tcon = conn_new(req->smb_conn);
157 DEBUG(0,("Couldn't find free connection.\n"));
158 return NT_STATUS_INSUFFICIENT_RESOURCES;
162 tcon->service = snum;
164 /* init ntvfs function pointers */
165 status = ntvfs_init_connection(req, type);
166 if (!NT_STATUS_IS_OK(status)) {
167 DEBUG(0, ("ntvfs_init_connection failed for service %s\n", lp_servicename(SNUM(tcon))));
168 conn_free(req->smb_conn, tcon);
172 /* Invoke NTVFS connection hook */
173 status = ntvfs_connect(req, lp_servicename(snum));
174 if (!NT_STATUS_IS_OK(status)) {
175 DEBUG(0,("make_connection: NTVFS make connection failed!\n"));
176 conn_free(req->smb_conn, tcon);
183 /****************************************************************************
184 Make a connection to a service.
187 ****************************************************************************/
188 static NTSTATUS make_connection(struct smbsrv_request *req,
189 const char *service, DATA_BLOB password,
190 const char *dev, uint16_t vuid)
193 enum ntvfs_type type;
194 const char *type_str;
196 /* the service might be of the form \\SERVER\SHARE. Should we put
197 the server name we get from this somewhere? */
198 if (strncmp(service, "\\\\", 2) == 0) {
199 char *p = strchr(service+2, '\\');
205 snum = find_service(service);
208 DEBUG(0,("couldn't find service %s\n", service));
209 return NT_STATUS_BAD_NETWORK_NAME;
212 /* work out what sort of connection this is */
213 if (strcmp(lp_fstype(snum), "IPC") == 0) {
216 } else if (lp_print_ok(snum)) {
224 if (strcmp(dev, "?????") != 0 && strcasecmp(type_str, dev) != 0) {
225 /* the client gave us the wrong device type */
226 return NT_STATUS_BAD_DEVICE_TYPE;
229 return make_connection_snum(req, snum, type, password, dev);
232 /****************************************************************************
234 ****************************************************************************/
235 void close_cnum(struct smbsrv_tcon *tcon)
237 DEBUG(3,("%s closed connection to service %s\n",
238 socket_get_peer_addr(tcon->smb_conn->connection->socket, tcon),
239 lp_servicename(SNUM(tcon))));
241 /* tell the ntvfs backend that we are disconnecting */
242 ntvfs_disconnect(tcon);
244 conn_free(tcon->smb_conn, tcon);
250 backend for tree connect call
252 NTSTATUS tcon_backend(struct smbsrv_request *req, union smb_tcon *con)
255 uint16_t vuid = UID_FIELD_INVALID;
257 /* can only do bare tcon in share level security */
258 if (req->session == NULL && lp_security() != SEC_SHARE) {
259 return NT_STATUS_ACCESS_DENIED;
263 vuid = req->session->vuid;
266 if (con->generic.level == RAW_TCON_TCON) {
268 password = data_blob(con->tcon.in.password, strlen(con->tcon.in.password) + 1);
270 status = make_connection(req, con->tcon.in.service, password, con->tcon.in.dev, vuid);
272 if (!NT_STATUS_IS_OK(status)) {
276 con->tcon.out.max_xmit = req->smb_conn->negotiate.max_recv;
277 con->tcon.out.cnum = req->tcon->cnum;
282 status = make_connection(req, con->tconx.in.path, con->tconx.in.password,
283 con->tconx.in.device, vuid);
284 if (!NT_STATUS_IS_OK(status)) {
288 con->tconx.out.cnum = req->tcon->cnum;
289 con->tconx.out.dev_type = talloc_strdup(req, req->tcon->dev_type);
290 con->tconx.out.fs_type = talloc_strdup(req, req->tcon->fs_type);
291 con->tconx.out.options = SMB_SUPPORT_SEARCH_BITS | (lp_csc_policy(req->tcon->service) << 2);
292 if (lp_msdfs_root(req->tcon->service) && lp_host_msdfs()) {
293 con->tconx.out.options |= SMB_SHARE_IN_DFS;