RIP BOOL. Convert BOOL -> bool. I found a few interesting
[nivanova/samba-autobuild/.git] / source3 / smbd / service.c
index 9efe63a82c1552230d06533c33a7952d381a7a65..bb279b701fd3193eb36c9e69af85b369e74092a5 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;
        const char *s = connectpath;
-        BOOL start_of_name_component = True;
+        bool start_of_name_component = True;
 
        *d++ = '/'; /* Always start with root. */
 
@@ -137,7 +161,7 @@ void set_conn_connectpath(connection_struct *conn, const pstring connectpath)
  Load parameters specific to a connection/service.
 ****************************************************************************/
 
-BOOL set_current_service(connection_struct *conn, uint16 flags, BOOL do_chdir)
+bool set_current_service(connection_struct *conn, uint16 flags, bool do_chdir)
 {
        static connection_struct *last_conn;
        static uint16 last_flags;
@@ -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,7 +503,7 @@ static NTSTATUS share_sanity_checks(int snum, fstring dev)
        return NT_STATUS_OK;
 }
 
-static NTSTATUS find_forced_user(connection_struct *conn, BOOL vuser_is_guest, fstring username)
+static NTSTATUS find_forced_user(connection_struct *conn, bool vuser_is_guest, fstring username)
 {
        int snum = conn->params->service;
        char *fuser, *found_username;
@@ -500,7 +535,7 @@ static NTSTATUS find_forced_user(connection_struct *conn, BOOL vuser_is_guest, f
  * one found.
  */
 
-static NTSTATUS find_forced_group(BOOL force_user,
+static NTSTATUS find_forced_group(bool force_user,
                                  int snum, const char *username,
                                  DOM_SID *pgroup_sid,
                                  gid_t *pgid)
@@ -510,7 +545,7 @@ static NTSTATUS find_forced_group(BOOL force_user,
        DOM_SID group_sid;
        enum lsa_SidType type;
        char *groupname;
-       BOOL user_must_be_member = False;
+       bool user_must_be_member = False;
        gid_t gid;
 
        ZERO_STRUCTP(pgroup_sid);
@@ -601,7 +636,7 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
                                               NTSTATUS *status)
 {
        struct passwd *pass = NULL;
-       BOOL guest = False;
+       bool guest = False;
        connection_struct *conn;
        SMB_STRUCT_STAT st;
        fstring user;
@@ -738,7 +773,7 @@ static connection_struct *make_connection_snum(int snum, user_struct *vuser,
                 * insensitive for now. */
                conn->case_sensitive = False;
        } else {
-               conn->case_sensitive = (BOOL)lp_casesensitive(snum);
+               conn->case_sensitive = (bool)lp_casesensitive(snum);
        }
 
        conn->case_preserve = lp_preservecase(snum);
@@ -873,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);
 
@@ -921,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 */
@@ -1037,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
@@ -1073,7 +1151,7 @@ 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() );
        }
-       
+
        /* we've finished with the user stuff - go back to root */
        change_to_root_user();
        return(conn);
@@ -1163,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 "
@@ -1199,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,