lib/param: Remove special handling for 'state dir' and 'cache dir'
[amitay/samba.git] / source3 / lib / util.c
index fa464482969ba8120949901eb8f5a17badc3e1c8..cddb53c3cb9f49e9fb5cc5e28d5573d068365609 100644 (file)
@@ -30,6 +30,7 @@
 #include "messages.h"
 #include <ccan/hash/hash.h>
 #include "libcli/security/security.h"
+#include "serverid.h"
 
 #ifdef HAVE_SYS_PRCTL_H
 #include <sys/prctl.h>
@@ -427,7 +428,7 @@ static void reinit_after_fork_pipe_handler(struct tevent_context *ev,
 
 
 NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
-                          struct event_context *ev_ctx,
+                          struct tevent_context *ev_ctx,
                           bool parent_longlived)
 {
        NTSTATUS status = NT_STATUS_OK;
@@ -558,31 +559,6 @@ char *get_mydnsdomname(TALLOC_CTX *ctx)
        }
 }
 
-/****************************************************************************
- Interpret a protocol description string, with a default.
-****************************************************************************/
-
-int interpret_protocol(const char *str,int def)
-{
-       if (strequal(str,"NT1"))
-               return(PROTOCOL_NT1);
-       if (strequal(str,"LANMAN2"))
-               return(PROTOCOL_LANMAN2);
-       if (strequal(str,"LANMAN1"))
-               return(PROTOCOL_LANMAN1);
-       if (strequal(str,"CORE"))
-               return(PROTOCOL_CORE);
-       if (strequal(str,"COREPLUS"))
-               return(PROTOCOL_COREPLUS);
-       if (strequal(str,"CORE+"))
-               return(PROTOCOL_COREPLUS);
-
-       DEBUG(0,("Unrecognised protocol level %s\n",str));
-
-       return(def);
-}
-
-
 #if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))
 /******************************************************************
  Remove any mount options such as -rsize=2048,wsize=2048 etc.
@@ -617,7 +593,7 @@ char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
 {
        char *value = NULL;
 
-       char *nis_map = (char *)lp_nis_home_map_name();
+       char *nis_map = (char *)lp_homedir_map();
 
        char buffer[NIS_MAXATTRVAL + 1];
        nis_result *result;
@@ -669,7 +645,7 @@ char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
        char *nis_result;     /* yp_match inits this */
        int nis_result_len;  /* and set this */
        char *nis_domain;     /* yp_get_default_domain inits this */
-       char *nis_map = (char *)lp_nis_home_map_name();
+       char *nis_map = lp_homedir_map(talloc_tos());
 
        if ((nis_error = yp_get_default_domain(&nis_domain)) != 0) {
                DEBUG(3, ("YP Error: %s\n", yperr_string(nis_error)));
@@ -706,92 +682,9 @@ char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
 #endif /* WITH_NISPLUS_HOME */
 #endif
 
-/****************************************************************************
- Check if a process exists. Does this work on all unixes?
-****************************************************************************/
-
 bool process_exists(const struct server_id pid)
 {
-       if (procid_is_me(&pid)) {
-               return True;
-       }
-
-       if (procid_is_local(&pid)) {
-               return (kill(pid.pid,0) == 0 || errno != ESRCH);
-       }
-
-#ifdef CLUSTER_SUPPORT
-       return ctdbd_process_exists(messaging_ctdbd_connection(),
-                                   pid.vnn, pid.pid);
-#else
-       return False;
-#endif
-}
-
-bool processes_exist(const struct server_id *pids, int num_pids,
-                    bool *results)
-{
-       struct server_id *remote_pids = NULL;
-       int *remote_idx = NULL;
-       bool *remote_results = NULL;
-       int i, num_remote_pids;
-       bool result = false;
-
-       remote_pids = talloc_array(talloc_tos(), struct server_id, num_pids);
-       if (remote_pids == NULL) {
-               goto fail;
-       }
-       remote_idx = talloc_array(talloc_tos(), int, num_pids);
-       if (remote_idx == NULL) {
-               goto fail;
-       }
-       remote_results = talloc_array(talloc_tos(), bool, num_pids);
-       if (remote_results == NULL) {
-               goto fail;
-       }
-
-       num_remote_pids = 0;
-
-       for (i=0; i<num_pids; i++) {
-               if (procid_is_me(&pids[i])) {
-                       results[i] = true;
-                       continue;
-               }
-               if (procid_is_local(&pids[i])) {
-                       results[i] = ((kill(pids[i].pid,0) == 0) ||
-                                     (errno != ESRCH));
-                       continue;
-               }
-
-               remote_pids[num_remote_pids] = pids[i];
-               remote_idx[num_remote_pids] = i;
-               num_remote_pids += 1;
-       }
-
-       if (num_remote_pids != 0) {
-#ifdef CLUSTER_SUPPORT
-               if (!ctdb_processes_exist(messaging_ctdbd_connection(),
-                                         remote_pids, num_remote_pids,
-                                         remote_results)) {
-                       goto fail;
-               }
-#else
-               for (i=0; i<num_remote_pids; i++) {
-                       remote_results[i] = false;
-               }
-#endif
-
-               for (i=0; i<num_remote_pids; i++) {
-                       results[remote_idx[i]] = remote_results[i];
-               }
-       }
-
-       result = true;
-fail:
-       TALLOC_FREE(remote_results);
-       TALLOC_FREE(remote_idx);
-       TALLOC_FREE(remote_pids);
-       return result;
+       return serverid_exists(&pid);
 }
 
 /*******************************************************************
@@ -899,7 +792,7 @@ void smb_panic_s3(const char *why)
        prctl(PR_SET_PTRACER, getpid(), 0, 0, 0);
 #endif
 
-       cmd = lp_panic_action();
+       cmd = lp_panic_action(talloc_tos());
        if (cmd && *cmd) {
                DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd));
                result = system(cmd);
@@ -1569,10 +1462,12 @@ char *myhostname(void)
 
 char *myhostname_upper(void)
 {
-       char *name;
        static char *ret;
        if (ret == NULL) {
-               name = get_myname(talloc_tos());
+               char *name = get_myname(NULL);
+               if (name == NULL) {
+                       return NULL;
+               }
                ret = strupper_talloc(NULL, name);
                talloc_free(name);
        }
@@ -1599,15 +1494,17 @@ static char *xx_path(const char *name, const char *rootpath)
        trim_string(fname,"","/");
 
        if (!directory_exist(fname)) {
-               if (!mkdir(fname,0755))
-                       DEBUG(1, ("Unable to create directory %s for file %s. "
-                             "Error was %s\n", fname, name, strerror(errno)));
+               if (mkdir(fname,0755) == -1) {
+                       /* Did someone else win the race ? */
+                       if (errno != EEXIST) {
+                               DEBUG(1, ("Unable to create directory %s for file %s. "
+                                       "Error was %s\n", fname, name, strerror(errno)));
+                               return NULL;
+                       }
+               }
        }
 
-       return talloc_asprintf(talloc_tos(),
-                               "%s/%s",
-                               fname,
-                               name);
+       return talloc_asprintf_append(fname, "/%s", name);
 }
 
 /**
@@ -1620,7 +1517,7 @@ static char *xx_path(const char *name, const char *rootpath)
 
 char *lock_path(const char *name)
 {
-       return xx_path(name, lp_lockdir());
+       return xx_path(name, lp_lock_directory());
 }
 
 /**
@@ -1633,7 +1530,7 @@ char *lock_path(const char *name)
 
 char *state_path(const char *name)
 {
-       return xx_path(name, lp_statedir());
+       return xx_path(name, lp_state_directory());
 }
 
 /**
@@ -1646,7 +1543,7 @@ char *state_path(const char *name)
 
 char *cache_path(const char *name)
 {
-       return xx_path(name, lp_cachedir());
+       return xx_path(name, lp_cache_directory());
 }
 
 /*******************************************************************
@@ -1892,8 +1789,14 @@ bool unix_wild_match(const char *pattern, const char *string)
                TALLOC_FREE(ctx);
                return false;
        }
-       strlower_m(p2);
-       strlower_m(s2);
+       if (!strlower_m(p2)) {
+               TALLOC_FREE(ctx);
+               return false;
+       }
+       if (!strlower_m(s2)) {
+               TALLOC_FREE(ctx);
+               return false;
+       }
 
        /* Remove any *? and ** from the pattern as they are meaningless */
        for(p = p2; *p; p++) {
@@ -2100,36 +2003,7 @@ bool procid_is_me(const struct server_id *pid)
 
 struct server_id interpret_pid(const char *pid_string)
 {
-       struct server_id result;
-       unsigned long long pid;
-       unsigned int vnn, task_id = 0;
-
-       ZERO_STRUCT(result);
-
-       /* We accept various forms with 1, 2 or 3 component forms
-        * because the server_id_str() can print different forms, and
-        * we want backwards compatibility for scripts that may call
-        * smbclient. */
-       if (sscanf(pid_string, "%u:%llu.%u", &vnn, &pid, &task_id) == 3) {
-               result.vnn = vnn;
-               result.pid = pid;
-               result.task_id = task_id;
-       } else if (sscanf(pid_string, "%u:%llu", &vnn, &pid) == 2) {
-               result.vnn = vnn;
-               result.pid = pid;
-               result.task_id = 0;
-       } else if (sscanf(pid_string, "%llu.%u", &pid, &task_id) == 2) {
-               result.vnn = get_my_vnn();
-               result.pid = pid;
-               result.task_id = task_id;
-       } else if (sscanf(pid_string, "%llu", &pid) == 1) {
-               result.vnn = get_my_vnn();
-               result.pid = pid;
-       } else {
-               result.vnn = NONCLUSTER_VNN;
-               result.pid = (uint64_t)-1;
-       }
-       return result;
+       return server_id_from_string(get_my_vnn(), pid_string);
 }
 
 char *procid_str_static(const struct server_id *pid)
@@ -2519,3 +2393,45 @@ struct security_unix_token *copy_unix_token(TALLOC_CTX *ctx, const struct securi
        }
        return cpy;
 }
+
+/****************************************************************************
+ Check that a file matches a particular file type.
+****************************************************************************/
+
+bool dir_check_ftype(uint32_t mode, uint32_t dirtype)
+{
+       uint32_t mask;
+
+       /* Check the "may have" search bits. */
+       if (((mode & ~dirtype) &
+                       (FILE_ATTRIBUTE_HIDDEN |
+                        FILE_ATTRIBUTE_SYSTEM |
+                        FILE_ATTRIBUTE_DIRECTORY)) != 0) {
+               return false;
+       }
+
+       /* Check the "must have" bits,
+          which are the may have bits shifted eight */
+       /* If must have bit is set, the file/dir can
+          not be returned in search unless the matching
+          file attribute is set */
+       mask = ((dirtype >> 8) & (FILE_ATTRIBUTE_DIRECTORY|
+                                   FILE_ATTRIBUTE_ARCHIVE|
+                                  FILE_ATTRIBUTE_READONLY|
+                                    FILE_ATTRIBUTE_HIDDEN|
+                                    FILE_ATTRIBUTE_SYSTEM)); /* & 0x37 */
+       if(mask) {
+               if((mask & (mode & (FILE_ATTRIBUTE_DIRECTORY|
+                                     FILE_ATTRIBUTE_ARCHIVE|
+                                    FILE_ATTRIBUTE_READONLY|
+                                      FILE_ATTRIBUTE_HIDDEN|
+                                       FILE_ATTRIBUTE_SYSTEM))) == mask) {
+                       /* check if matching attribute present */
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
+       return true;
+}