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.
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)
31 if (!service || !homedir)
34 if ((iHomeService = lp_servicenumber(HOMES_NAME)) < 0)
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
45 const char *p = strchr(service,*lp_winbind_separator());
47 /* We only want the 'user' part of the string */
53 if (!lp_add_home(service, iHomeService, username, homedir)) {
57 return lp_servicenumber(service);
63 * Find a service entry. service is always in dos codepage.
65 * @param service is modified (to canonical form??)
67 static int find_service(const char *service)
71 iService = lp_servicenumber(service);
73 /* If we still don't have a service, attempt to add it as a printer. */
77 if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) >= 0) {
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);
88 DEBUG(0,("failed to add %s as a printer service!\n", service));
90 DEBUG(3,("%s is not a valid printer name\n", service));
95 /* Check for default vfs service? Unsure whether to implement this */
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,"..")) {
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>.
112 pstrcpy(defservice, pdefservice);
113 iService = find_service(defservice);
115 /* REWRITE: all_string_sub(service, "_","/",0); */
116 iService = lp_add_service(service, iService);
121 if (iService >= 0 && !VALID_SNUM(iService)) {
122 DEBUG(0,("Invalid snum %d for %s\n",iService, service));
126 if (iService == -1) {
127 DEBUG(3,("find_service() failed to find service %s\n", service));
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,
143 struct tcon_context *conn;
146 conn = conn_new(req->smb_ctx);
148 DEBUG(0,("Couldn't find free connection.\n"));
149 return NT_STATUS_INSUFFICIENT_RESOURCES;
153 conn->service = snum;
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.
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;
171 conn->read_only = True;
175 /* check number of connections */
176 if (!claim_connection(conn,
177 lp_servicename(SNUM(conn)),
178 lp_max_connections(SNUM(conn)),
180 DEBUG(1,("too many connections - rejected\n"));
181 conn_free(req->smb_ctx, conn);
182 return NT_STATUS_INSUFFICIENT_RESOURCES;
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);
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);
206 /****************************************************************************
207 Make a connection to a 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)
216 enum ntvfs_type type;
217 const char *type_str;
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, '\\');
228 snum = find_service(service);
231 DEBUG(0,("%s couldn't find service %s\n",
232 sub_get_remote_machine(), service));
233 return NT_STATUS_BAD_NETWORK_NAME;
236 /* work out what sort of connection this is */
237 if (strcmp(lp_fstype(snum), "IPC") == 0) {
240 } else if (lp_print_ok(snum)) {
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;
253 return make_connection_snum(req, snum, type, password, dev);
256 /****************************************************************************
258 ****************************************************************************/
259 void close_cnum(struct tcon_context *conn)
261 DEBUG(3,("%s closed connection to service %s\n",
262 conn->smb_ctx->socket.client_addr, lp_servicename(SNUM(conn))));
264 yield_connection(conn, lp_servicename(SNUM(conn)));
266 /* tell the ntvfs backend that we are disconnecting */
267 conn->ntvfs_ops->disconnect(conn);
269 conn_free(conn->smb_ctx, conn);
275 backend for tree connect call
277 NTSTATUS tcon_backend(struct request_context *req, union smb_tcon *con)
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;
286 if (con->generic.level == RAW_TCON_TCON) {
288 password = data_blob(con->tcon.in.password, strlen(con->tcon.in.password) + 1);
290 status = make_connection(req, con->tcon.in.service, password, con->tcon.in.dev, req->user_ctx->vuid);
292 if (!NT_STATUS_IS_OK(status)) {
296 con->tcon.out.max_xmit = req->smb_ctx->negotiate.max_recv;
297 con->tcon.out.cnum = req->conn->cnum;
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)) {
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;