[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[samba.git] / source / smbd / service.c
index 08370b1c80014336708fde1015692b7e6997f240..1c46e3776c28442e94b6fc77770d61c314d4c86f 100644 (file)
@@ -5,7 +5,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 
 extern userdom_struct current_user_info;
 
+static BOOL canonicalize_path(connection_struct *conn, pstring path)
+{
+#ifdef REALPATH_TAKES_NULL
+       char *resolved_name = SMB_VFS_REALPATH(conn,path,NULL);
+       if (!resolved_name) {
+               return False;
+       }
+       pstrcpy(path, resolved_name);
+       SAFE_FREE(resolved_name);
+       return True;
+#else
+#ifdef PATH_MAX
+        char resolved_name_buf[PATH_MAX+1];
+#else
+        pstring resolved_name_buf;
+#endif
+       char *resolved_name = SMB_VFS_REALPATH(conn,path,resolved_name_buf);
+       if (!resolved_name) {
+               return False;
+       }
+       pstrcpy(path, resolved_name);
+       return True;
+#endif /* REALPATH_TAKES_NULL */
+}
+
 /****************************************************************************
  Ensure when setting connectpath it is a canonicalized (no ./ // or ../)
  absolute path stating in / and not ending in /.
  Observent people will notice a similarity between this and check_path_syntax :-).
 ****************************************************************************/
 
-void set_conn_connectpath(connection_struct *conn, const pstring connectpath)
+void set_conn_connectpath(connection_struct *conn, const char *connectpath)
 {
        pstring destname;
        char *d = destname;
@@ -288,6 +312,7 @@ static int load_registry_service(const char *servicename)
                TALLOC_FREE(value);
        }
 
+       res = 0;
  error:
 
        TALLOC_FREE(key);
@@ -377,6 +402,17 @@ int find_service(fstring service)
        if (iService < 0) {
        }
 
+       if (iService < 0) {
+               iService = load_registry_service(service);
+       }
+
+       /* Is it a usershare service ? */
+       if (iService < 0 && *lp_usershare_path()) {
+               /* Ensure the name is canonicalized. */
+               strlower_m(service);
+               iService = load_usershare_service(service);
+       }
+
        /* just possibly it's a default service? */
        if (iService < 0) {
                char *pdefservice = lp_defaultservice();
@@ -389,6 +425,14 @@ int find_service(fstring service)
                         */
                        pstring defservice;
                        pstrcpy(defservice, pdefservice);
+
+                       /* Disallow anything except explicit share names. */
+                       if (strequal(defservice,HOMES_NAME) ||
+                                       strequal(defservice, PRINTERS_NAME) ||
+                                       strequal(defservice, "IPC$")) {
+                               goto fail;
+                       }
+
                        iService = find_service(defservice);
                        if (iService >= 0) {
                                all_string_sub(service, "_","/",0);
@@ -397,17 +441,6 @@ int find_service(fstring service)
                }
        }
 
-       if (iService < 0) {
-               iService = load_registry_service(service);
-       }
-
-       /* Is it a usershare service ? */
-       if (iService < 0 && *lp_usershare_path()) {
-               /* Ensure the name is canonicalized. */
-               strlower_m(service);
-               iService = load_usershare_service(service);
-       }
-
        if (iService >= 0) {
                if (!VALID_SNUM(iService)) {
                        DEBUG(0,("Invalid snum %d for %s\n",iService, service));
@@ -415,6 +448,8 @@ int find_service(fstring service)
                }
        }
 
+  fail:
+
        if (iService < 0)
                DEBUG(3,("find_service() failed to find service %s\n", service));
 
@@ -468,43 +503,28 @@ static NTSTATUS share_sanity_checks(int snum, fstring dev)
        return NT_STATUS_OK;
 }
 
-static NTSTATUS find_forced_user(int snum, BOOL vuser_is_guest,
-                                uid_t *uid, gid_t *gid, fstring username,
-                                struct nt_user_token **token)
+static NTSTATUS find_forced_user(connection_struct *conn, BOOL vuser_is_guest, fstring username)
 {
-       TALLOC_CTX *mem_ctx;
+       int snum = conn->params->service;
        char *fuser, *found_username;
-       struct nt_user_token *tmp_token;
        NTSTATUS result;
 
-       if (!(mem_ctx = talloc_new(NULL))) {
-               DEBUG(0, ("talloc_new failed\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       if (!(fuser = talloc_string_sub(mem_ctx, lp_force_user(snum), "%S",
+       if (!(fuser = talloc_string_sub(conn->mem_ctx, lp_force_user(snum), "%S",
                                        lp_servicename(snum)))) {
-               TALLOC_FREE(mem_ctx);
                return NT_STATUS_NO_MEMORY;
-               
        }
 
-       result = create_token_from_username(mem_ctx, fuser, vuser_is_guest,
-                                           uid, gid, &found_username,
-                                           &tmp_token);
+       result = create_token_from_username(conn->mem_ctx, fuser, vuser_is_guest,
+                                           &conn->uid, &conn->gid, &found_username,
+                                           &conn->nt_user_token);
        if (!NT_STATUS_IS_OK(result)) {
-               TALLOC_FREE(mem_ctx);
                return result;
        }
 
-       if (!(*token = dup_nt_token(NULL, tmp_token))) {
-               TALLOC_FREE(mem_ctx);
-               return NT_STATUS_NO_MEMORY;
-       }
-
        fstrcpy(username, found_username);
 
-       TALLOC_FREE(mem_ctx);
+       TALLOC_FREE(fuser);
+       TALLOC_FREE(found_username);
        return NT_STATUS_OK;
 }
 
@@ -638,6 +658,7 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
                return NULL;
        }
 
+       conn->params->service = snum;
        conn->nt_user_token = NULL;
 
        if (lp_guest_only(snum)) {
@@ -654,12 +675,12 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
                        *status = NT_STATUS_NO_SUCH_USER;
                        return NULL;
                }
-               status2 = create_token_from_username(NULL, pass->pw_name, True,
+               status2 = create_token_from_username(conn->mem_ctx, pass->pw_name, True,
                                                     &conn->uid, &conn->gid,
                                                     &found_username,
                                                     &conn->nt_user_token);
                if (!NT_STATUS_IS_OK(status2)) {
-                       TALLOC_FREE(found_username);
+                       TALLOC_FREE(pass);
                        conn_free(conn);
                        *status = status2;
                        return NULL;
@@ -701,6 +722,7 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
        } else if (lp_security() == SEC_SHARE) {
                NTSTATUS status2;
                char *found_username = NULL;
+
                /* add it as a possible user name if we 
                   are in share mode security */
                add_session_user(lp_servicename(snum));
@@ -713,12 +735,11 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
                        return NULL;
                }
                pass = Get_Pwnam(user);
-               status2 = create_token_from_username(NULL, pass->pw_name, True,
+               status2 = create_token_from_username(conn->mem_ctx, pass->pw_name, True,
                                                     &conn->uid, &conn->gid,
                                                     &found_username,
                                                     &conn->nt_user_token);
                if (!NT_STATUS_IS_OK(status2)) {
-                       TALLOC_FREE(found_username);
                        conn_free(conn);
                        *status = status2;
                        return NULL;
@@ -740,7 +761,6 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
                    sizeof(conn->client_address)-1);
        conn->num_files_open = 0;
        conn->lastused = conn->lastused_count = time(NULL);
-       conn->params->service = snum;
        conn->used = True;
        conn->printer = (strncmp(dev,"LPT",3) == 0);
        conn->ipc = ( (strncmp(dev,"IPC",3) == 0) ||
@@ -778,10 +798,9 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
        if (*lp_force_user(snum)) {
                NTSTATUS status2;
 
-               status2 = find_forced_user(snum,
-                                          (vuser != NULL) && vuser->guest,
-                                          &conn->uid, &conn->gid, user,
-                                          &conn->nt_user_token);
+               status2 = find_forced_user(conn,
+                               (vuser != NULL) && vuser->guest,
+                               user);
                if (!NT_STATUS_IS_OK(status2)) {
                        conn_free(conn);
                        *status = status2;
@@ -858,7 +877,7 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
                                           sid_string_static(sid)));
                                continue;
                        }
-                       if (!add_gid_to_array_unique(NULL, gid, &conn->groups,
+                       if (!add_gid_to_array_unique(conn->mem_ctx, gid, &conn->groups,
                                                &conn->ngroups)) {
                                DEBUG(0, ("add_gid_to_array_unique failed\n"));
                                conn_free(conn);
@@ -889,10 +908,28 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
         */
 
        {
+               BOOL can_write = False;
                NT_USER_TOKEN *token = conn->nt_user_token ?
-                       conn->nt_user_token : vuser->nt_user_token;
+                       conn->nt_user_token :
+                       (vuser ? vuser->nt_user_token : NULL);
+
+               /*
+                * I don't believe this can happen. But the
+                * logic above is convoluted enough to confuse
+                * automated checkers, so be sure. JRA.
+                */
+
+               if (token == NULL) {
+                       DEBUG(0,("make_connection: connection to %s "
+                                "denied due to missing "
+                                "NT token.\n",
+                                 lp_servicename(snum)));
+                       conn_free(conn);
+                       *status = NT_STATUS_ACCESS_DENIED;
+                       return NULL;
+               }
 
-               BOOL can_write = share_access_check(token,
+               can_write = share_access_check(token,
                                                    lp_servicename(snum),
                                                    FILE_WRITE_DATA);
 
@@ -937,18 +974,39 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
                set_conn_connectpath(conn,s);
        }
 
+       if ((!conn->printer) && (!conn->ipc)) {
+               conn->notify_ctx = notify_init(conn->mem_ctx, server_id_self(),
+                                              smbd_messaging_context(),
+                                              smbd_event_context(),
+                                              conn);
+       }
+
 /* ROOT Activities: */ 
-       /* check number of connections */
-       if (!claim_connection(conn,
-                             lp_servicename(snum),
-                             lp_max_connections(snum),
-                             False,0)) {
-               DEBUG(1,("too many connections - rejected\n"));
+       /*
+        * Enforce the max connections parameter.
+        */
+
+       if ((lp_max_connections(snum) > 0)
+           && (count_current_connections(lp_servicename(SNUM(conn)), True) >=
+               lp_max_connections(snum))) {
+
+               DEBUG(1, ("Max connections (%d) exceeded for %s\n",
+                         lp_max_connections(snum), lp_servicename(snum)));
                conn_free(conn);
                *status = NT_STATUS_INSUFFICIENT_RESOURCES;
                return NULL;
        }  
 
+       /*
+        * Get us an entry in the connections db
+        */
+       if (!claim_connection(conn, lp_servicename(snum), 0)) {
+               DEBUG(1, ("Could not store connections entry\n"));
+               conn_free(conn);
+               *status = NT_STATUS_INTERNAL_DB_ERROR;
+               return NULL;
+       }  
+
        /* Preexecs are done here as they might make the dir we are to ChDir
         * to below */
        /* execute any "root preexec = " line */
@@ -1053,27 +1111,31 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
                                 strerror(errno) ));
                }
                change_to_root_user();
-               /* Call VFS disconnect hook */    
+               /* Call VFS disconnect hook */
                SMB_VFS_DISCONNECT(conn);
                yield_connection(conn, lp_servicename(snum));
                conn_free(conn);
                *status = NT_STATUS_BAD_NETWORK_NAME;
                return NULL;
        }
-       
+
        string_set(&conn->origpath,conn->connectpath);
-       
+
 #if SOFTLINK_OPTIMISATION
        /* resolve any soft links early if possible */
        if (vfs_ChDir(conn,conn->connectpath) == 0) {
-               pstring s;
-               pstrcpy(s,conn->connectpath);
-               vfs_GetWd(conn,s);
+               TALLOC_CTX *ctx = talloc_stackframe();
+               char *s = vfs_GetWd(ctx,s);
+               if (!s) {
+                       *status = map_nt_error_from_unix(errno);
+                       return NULL;
+               }
                set_conn_connectpath(conn,s);
                vfs_ChDir(conn,conn->connectpath);
+               TALLOC_FREE(ctx);
        }
 #endif
-       
+
        /*
         * Print out the 'connected as' stuff here as we need
         * to know the effective uid and gid we will be using
@@ -1089,9 +1151,6 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
                dbgtext( "(uid=%d, gid=%d) ", (int)geteuid(), (int)getegid() );
                dbgtext( "(pid %d)\n", (int)sys_getpid() );
        }
-       
-       /* Setup the minimum value for a change notify wait time (seconds). */
-       set_change_notify_timeout(lp_change_notify_timeout(snum));
 
        /* we've finished with the user stuff - go back to root */
        change_to_root_user();
@@ -1182,7 +1241,7 @@ connection_struct *make_connection(const char *service_in, DATA_BLOB password,
 
        if (strequal(service_in,HOMES_NAME)) {
                if(lp_security() != SEC_SHARE) {
-                       DATA_BLOB no_pw = data_blob(NULL, 0);
+                       DATA_BLOB no_pw = data_blob_null;
                        if (vuser->homes_snum == -1) {
                                DEBUG(2, ("[homes] share not available for "
                                          "this user because it was not found "
@@ -1218,7 +1277,7 @@ connection_struct *make_connection(const char *service_in, DATA_BLOB password,
        } else if ((lp_security() != SEC_SHARE) && (vuser->homes_snum != -1)
                   && strequal(service_in,
                               lp_servicename(vuser->homes_snum))) {
-               DATA_BLOB no_pw = data_blob(NULL, 0);
+               DATA_BLOB no_pw = data_blob_null;
                DEBUG(5, ("making a connection to 'homes' service [%s] "
                          "created at session setup time\n", service_in));
                return make_connection_snum(vuser->homes_snum,