util: move add_gid_to_array_unique to toplevel and add add_uid_to_array_unique.
[ira/wip.git] / source3 / lib / util.c
index 074b523ae0b8a320076629d7284ebfd0b9f366ff..b85f29e1362e9c8a200499aa4a6ee37bd2b2b965 100644 (file)
@@ -57,9 +57,6 @@ extern unsigned int global_clobber_region_line;
 
 enum protocol_types Protocol = PROTOCOL_COREPLUS;
 
-/* this is used by the chaining code */
-int chain_size = 0;
-
 static enum remote_arch_types ra_type = RA_UNKNOWN;
 
 /***********************************************************************
@@ -280,135 +277,168 @@ bool init_names(void)
   Used mainly in client tools.
 ****************************************************************************/
 
-static struct user_auth_info cmdline_auth_info = {
-       NULL,   /* username */
-       NULL,   /* password */
-       false,  /* got_pass */
-       false,  /* use_kerberos */
-       Undefined, /* signing state */
-       false,  /* smb_encrypt */
-       false   /* use machine account */
-};
+struct user_auth_info *user_auth_info_init(TALLOC_CTX *mem_ctx)
+{
+       struct user_auth_info *result;
+
+       result = TALLOC_ZERO_P(mem_ctx, struct user_auth_info);
+       if (result == NULL) {
+               return NULL;
+       }
+
+       result->signing_state = Undefined;
+       return result;
+}
 
-const char *get_cmdline_auth_info_username(void)
+const char *get_cmdline_auth_info_username(const struct user_auth_info *auth_info)
 {
-       if (!cmdline_auth_info.username) {
+       if (!auth_info->username) {
                return "";
        }
-       return cmdline_auth_info.username;
+       return auth_info->username;
 }
 
-void set_cmdline_auth_info_username(const char *username)
+void set_cmdline_auth_info_username(struct user_auth_info *auth_info,
+                                   const char *username)
 {
-       SAFE_FREE(cmdline_auth_info.username);
-       cmdline_auth_info.username = SMB_STRDUP(username);
-       if (!cmdline_auth_info.username) {
+       TALLOC_FREE(auth_info->username);
+       auth_info->username = talloc_strdup(auth_info, username);
+       if (!auth_info->username) {
                exit(ENOMEM);
        }
 }
 
-const char *get_cmdline_auth_info_password(void)
+const char *get_cmdline_auth_info_password(const struct user_auth_info *auth_info)
 {
-       if (!cmdline_auth_info.password) {
+       if (!auth_info->password) {
                return "";
        }
-       return cmdline_auth_info.password;
+       return auth_info->password;
 }
 
-void set_cmdline_auth_info_password(const char *password)
+void set_cmdline_auth_info_password(struct user_auth_info *auth_info,
+                                   const char *password)
 {
-       SAFE_FREE(cmdline_auth_info.password);
-       cmdline_auth_info.password = SMB_STRDUP(password);
-       if (!cmdline_auth_info.password) {
+       TALLOC_FREE(auth_info->password);
+       if (password == NULL) {
+               password = "";
+       }
+       auth_info->password = talloc_strdup(auth_info, password);
+       if (!auth_info->password) {
                exit(ENOMEM);
        }
-       cmdline_auth_info.got_pass = true;
+       auth_info->got_pass = true;
 }
 
-bool set_cmdline_auth_info_signing_state(const char *arg)
+bool set_cmdline_auth_info_signing_state(struct user_auth_info *auth_info,
+                                        const char *arg)
 {
-       cmdline_auth_info.signing_state = -1;
+       auth_info->signing_state = -1;
        if (strequal(arg, "off") || strequal(arg, "no") ||
                        strequal(arg, "false")) {
-               cmdline_auth_info.signing_state = false;
+               auth_info->signing_state = false;
        } else if (strequal(arg, "on") || strequal(arg, "yes") ||
                        strequal(arg, "true") || strequal(arg, "auto")) {
-               cmdline_auth_info.signing_state = true;
+               auth_info->signing_state = true;
        } else if (strequal(arg, "force") || strequal(arg, "required") ||
                        strequal(arg, "forced")) {
-               cmdline_auth_info.signing_state = Required;
+               auth_info->signing_state = Required;
        } else {
                return false;
        }
        return true;
 }
 
-int get_cmdline_auth_info_signing_state(void)
+int get_cmdline_auth_info_signing_state(const struct user_auth_info *auth_info)
+{
+       return auth_info->signing_state;
+}
+
+void set_cmdline_auth_info_use_kerberos(struct user_auth_info *auth_info,
+                                       bool b)
+{
+        auth_info->use_kerberos = b;
+}
+
+bool get_cmdline_auth_info_use_kerberos(const struct user_auth_info *auth_info)
 {
-       return cmdline_auth_info.signing_state;
+       return auth_info->use_kerberos;
 }
 
-void set_cmdline_auth_info_use_kerberos(bool b)
+void set_cmdline_auth_info_fallback_after_kerberos(struct user_auth_info *auth_info,
+                                       bool b)
 {
-        cmdline_auth_info.use_kerberos = b;
+       auth_info->fallback_after_kerberos = b;
 }
 
-bool get_cmdline_auth_info_use_kerberos(void)
+bool get_cmdline_auth_info_fallback_after_kerberos(const struct user_auth_info *auth_info)
 {
-       return cmdline_auth_info.use_kerberos;
+       return auth_info->fallback_after_kerberos;
 }
 
 /* This should only be used by lib/popt_common.c JRA */
-void set_cmdline_auth_info_use_krb5_ticket(void)
+void set_cmdline_auth_info_use_krb5_ticket(struct user_auth_info *auth_info)
 {
-       cmdline_auth_info.use_kerberos = true;
-       cmdline_auth_info.got_pass = true;
+       auth_info->use_kerberos = true;
+       auth_info->got_pass = true;
 }
 
 /* This should only be used by lib/popt_common.c JRA */
-void set_cmdline_auth_info_smb_encrypt(void)
+void set_cmdline_auth_info_smb_encrypt(struct user_auth_info *auth_info)
 {
-       cmdline_auth_info.smb_encrypt = true;
+       auth_info->smb_encrypt = true;
 }
 
-void set_cmdline_auth_info_use_machine_account(void)
+void set_cmdline_auth_info_use_machine_account(struct user_auth_info *auth_info)
 {
-       cmdline_auth_info.use_machine_account = true;
+       auth_info->use_machine_account = true;
 }
 
-bool get_cmdline_auth_info_got_pass(void)
+bool get_cmdline_auth_info_got_pass(const struct user_auth_info *auth_info)
 {
-       return cmdline_auth_info.got_pass;
+       return auth_info->got_pass;
 }
 
-bool get_cmdline_auth_info_smb_encrypt(void)
+bool get_cmdline_auth_info_smb_encrypt(const struct user_auth_info *auth_info)
 {
-       return cmdline_auth_info.smb_encrypt;
+       return auth_info->smb_encrypt;
 }
 
-bool get_cmdline_auth_info_use_machine_account(void)
+bool get_cmdline_auth_info_use_machine_account(const struct user_auth_info *auth_info)
 {
-       return cmdline_auth_info.use_machine_account;
+       return auth_info->use_machine_account;
 }
 
-bool get_cmdline_auth_info_copy(struct user_auth_info *info)
+struct user_auth_info *get_cmdline_auth_info_copy(TALLOC_CTX *mem_ctx,
+                                                 const struct user_auth_info *src)
 {
-       *info = cmdline_auth_info;
-       /* Now re-alloc the strings. */
-       info->username = SMB_STRDUP(get_cmdline_auth_info_username());
-       info->password = SMB_STRDUP(get_cmdline_auth_info_password());
-       if (!info->username || !info->password) {
-               return false;
+       struct user_auth_info *result;
+
+       result = user_auth_info_init(mem_ctx);
+       if (result == NULL) {
+               return NULL;
        }
-       return true;
+
+       *result = *src;
+
+       result->username = talloc_strdup(
+               result, get_cmdline_auth_info_username(src));
+       result->password = talloc_strdup(
+               result, get_cmdline_auth_info_password(src));
+       if ((result->username == NULL) || (result->password == NULL)) {
+               TALLOC_FREE(result);
+               return NULL;
+       }
+
+       return result;
 }
 
-bool set_cmdline_auth_info_machine_account_creds(void)
+bool set_cmdline_auth_info_machine_account_creds(struct user_auth_info *auth_info)
 {
        char *pass = NULL;
        char *account = NULL;
 
-       if (!get_cmdline_auth_info_use_machine_account()) {
+       if (!get_cmdline_auth_info_use_machine_account(auth_info)) {
                return false;
        }
 
@@ -430,8 +460,8 @@ bool set_cmdline_auth_info_machine_account_creds(void)
                return false;
        }
 
-       set_cmdline_auth_info_username(account);
-       set_cmdline_auth_info_password(pass);
+       set_cmdline_auth_info_username(auth_info, account);
+       set_cmdline_auth_info_password(auth_info, pass);
 
        SAFE_FREE(account);
        SAFE_FREE(pass);
@@ -440,87 +470,29 @@ bool set_cmdline_auth_info_machine_account_creds(void)
 }
 
 /****************************************************************************
- Add a gid to an array of gids if it's not already there.
-****************************************************************************/
-
-bool add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
-                            gid_t **gids, size_t *num_gids)
-{
-       int i;
-
-       if ((*num_gids != 0) && (*gids == NULL)) {
-               /*
-                * A former call to this routine has failed to allocate memory
-                */
-               return False;
-       }
-
-       for (i=0; i<*num_gids; i++) {
-               if ((*gids)[i] == gid) {
-                       return True;
-               }
-       }
-
-       *gids = TALLOC_REALLOC_ARRAY(mem_ctx, *gids, gid_t, *num_gids+1);
-       if (*gids == NULL) {
-               *num_gids = 0;
-               return False;
-       }
-
-       (*gids)[*num_gids] = gid;
-       *num_gids += 1;
-       return True;
-}
-
-/****************************************************************************
- Like atoi but gets the value up to the separator character.
+ Ensure we have a password if one not given.
 ****************************************************************************/
 
-static const char *Atoic(const char *p, int *n, const char *c)
+void set_cmdline_auth_info_getpass(struct user_auth_info *auth_info)
 {
-       if (!isdigit((int)*p)) {
-               DEBUG(5, ("Atoic: malformed number\n"));
-               return NULL;
-       }
-
-       (*n) = atoi(p);
-
-       while ((*p) && isdigit((int)*p))
-               p++;
+       char *label = NULL;
+       char *pass;
+       TALLOC_CTX *frame;
 
-       if (strchr_m(c, *p) == NULL) {
-               DEBUG(5, ("Atoic: no separator characters (%s) not found\n", c));
-               return NULL;
+       if (get_cmdline_auth_info_got_pass(auth_info) ||
+                       get_cmdline_auth_info_use_kerberos(auth_info)) {
+               /* Already got one... */
+               return;
        }
 
-       return p;
-}
-
-/*************************************************************************
- Reads a list of numbers.
- *************************************************************************/
-
-const char *get_numlist(const char *p, uint32 **num, int *count)
-{
-       int val;
-
-       if (num == NULL || count == NULL)
-               return NULL;
-
-       (*count) = 0;
-       (*num  ) = NULL;
-
-       while ((p = Atoic(p, &val, ":,")) != NULL && (*p) != ':') {
-               *num = SMB_REALLOC_ARRAY((*num), uint32, (*count)+1);
-               if (!(*num)) {
-                       return NULL;
-               }
-               (*num)[(*count)] = val;
-               (*count)++;
-               p++;
+       frame = talloc_stackframe();
+       label = talloc_asprintf(frame, "Enter %s's password: ",
+                       get_cmdline_auth_info_username(auth_info));
+       pass = getpass(label);
+       if (pass) {
+               set_cmdline_auth_info_password(auth_info, pass);
        }
-
-       return p;
+       TALLOC_FREE(frame);
 }
 
 /*******************************************************************
@@ -536,7 +508,7 @@ bool file_exist_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
        if (sys_stat(fname,sbuf) != 0) 
                return(False);
 
-       return((S_ISREG(sbuf->st_mode)) || (S_ISFIFO(sbuf->st_mode)));
+       return((S_ISREG(sbuf->st_ex_mode)) || (S_ISFIFO(sbuf->st_ex_mode)));
 }
 
 /*******************************************************************
@@ -549,7 +521,7 @@ bool socket_exist(const char *fname)
        if (sys_stat(fname,&st) != 0) 
                return(False);
 
-       return S_ISSOCK(st.st_mode);
+       return S_ISSOCK(st.st_ex_mode);
 }
 
 /*******************************************************************
@@ -567,12 +539,21 @@ bool directory_exist_stat(char *dname,SMB_STRUCT_STAT *st)
        if (sys_stat(dname,st) != 0) 
                return(False);
 
-       ret = S_ISDIR(st->st_mode);
+       ret = S_ISDIR(st->st_ex_mode);
        if(!ret)
                errno = ENOTDIR;
        return ret;
 }
 
+/*******************************************************************
+ Returns the size in bytes of the named given the stat struct.
+********************************************************************/
+
+uint64_t get_file_size_stat(const SMB_STRUCT_STAT *sbuf)
+{
+       return sbuf->st_ex_size;
+}
+
 /*******************************************************************
  Returns the size in bytes of the named file.
 ********************************************************************/
@@ -580,10 +561,10 @@ bool directory_exist_stat(char *dname,SMB_STRUCT_STAT *st)
 SMB_OFF_T get_file_size(char *file_name)
 {
        SMB_STRUCT_STAT buf;
-       buf.st_size = 0;
+       buf.st_ex_size = 0;
        if(sys_stat(file_name,&buf) != 0)
                return (SMB_OFF_T)-1;
-       return(buf.st_size);
+       return get_file_size_stat(&buf);
 }
 
 /*******************************************************************
@@ -823,43 +804,6 @@ char *clean_name(TALLOC_CTX *ctx, const char *s)
        return unix_clean_name(ctx, str);
 }
 
-/*******************************************************************
- Close the low 3 fd's and open dev/null in their place.
-********************************************************************/
-
-void close_low_fds(bool stderr_too)
-{
-#ifndef VALGRIND
-       int fd;
-       int i;
-
-       close(0);
-       close(1);
-
-       if (stderr_too)
-               close(2);
-
-       /* try and use up these file descriptors, so silly
-               library routines writing to stdout etc won't cause havoc */
-       for (i=0;i<3;i++) {
-               if (i == 2 && !stderr_too)
-                       continue;
-
-               fd = sys_open("/dev/null",O_RDWR,0);
-               if (fd < 0)
-                       fd = sys_open("/dev/null",O_WRONLY,0);
-               if (fd < 0) {
-                       DEBUG(0,("Can't open /dev/null\n"));
-                       return;
-               }
-               if (fd != i) {
-                       DEBUG(0,("Didn't get file descriptor %d\n",i));
-                       return;
-               }
-       }
-#endif
-}
-
 /*******************************************************************
  Write data into an fd at a given offset. Ignore seek errors.
 ********************************************************************/
@@ -950,40 +894,11 @@ void smb_msleep(unsigned int t)
 #endif
 }
 
-/****************************************************************************
- Become a daemon, discarding the controlling terminal.
-****************************************************************************/
-
-void become_daemon(bool Fork, bool no_process_group)
-{
-       if (Fork) {
-               if (sys_fork()) {
-                       _exit(0);
-               }
-       }
-
-  /* detach from the terminal */
-#ifdef HAVE_SETSID
-       if (!no_process_group) setsid();
-#elif defined(TIOCNOTTY)
-       if (!no_process_group) {
-               int i = sys_open("/dev/tty", O_RDWR, 0);
-               if (i != -1) {
-                       ioctl(i, (int) TIOCNOTTY, (char *)0);      
-                       close(i);
-               }
-       }
-#endif /* HAVE_SETSID */
-
-       /* Close fd's 0,1,2. Needed if started by rsh */
-       close_low_fds(False);  /* Don't close stderr, let the debug system
-                                 attach it to the logfile */
-}
-
-bool reinit_after_fork(struct messaging_context *msg_ctx,
+NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
+                      struct event_context *ev_ctx,
                       bool parent_longlived)
 {
-       NTSTATUS status;
+       NTSTATUS status = NT_STATUS_OK;
 
        /* Reset the state of the random
         * number generation system, so
@@ -994,21 +909,27 @@ bool reinit_after_fork(struct messaging_context *msg_ctx,
        /* tdb needs special fork handling */
        if (tdb_reopen_all(parent_longlived ? 1 : 0) == -1) {
                DEBUG(0,("tdb_reopen_all failed.\n"));
-               return false;
+               status = NT_STATUS_OPEN_FAILED;
+               goto done;
        }
 
-       /*
-        * For clustering, we need to re-init our ctdbd connection after the
-        * fork
-        */
-       status = messaging_reinit(msg_ctx);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("messaging_reinit() failed: %s\n",
-                        nt_errstr(status)));
-               return false;
+       if (ev_ctx) {
+               event_context_reinit(ev_ctx);
        }
 
-       return true;
+       if (msg_ctx) {
+               /*
+                * For clustering, we need to re-init our ctdbd connection after the
+                * fork
+                */
+               status = messaging_reinit(msg_ctx);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0,("messaging_reinit() failed: %s\n",
+                                nt_errstr(status)));
+               }
+       }
+ done:
+       return status;
 }
 
 /****************************************************************************
@@ -1227,35 +1148,6 @@ void add_to_large_array(TALLOC_CTX *mem_ctx, size_t element_size,
        *array_size = -1;
 }
 
-/****************************************************************************
- Get my own name and IP.
-****************************************************************************/
-
-char *talloc_get_myname(TALLOC_CTX *ctx)
-{
-       char *p;
-       char hostname[HOST_NAME_MAX];
-
-       *hostname = 0;
-
-       /* get my host name */
-       if (gethostname(hostname, sizeof(hostname)) == -1) {
-               DEBUG(0,("gethostname failed\n"));
-               return False;
-       }
-
-       /* Ensure null termination. */
-       hostname[sizeof(hostname)-1] = '\0';
-
-       /* split off any parts after an initial . */
-       p = strchr_m(hostname,'.');
-       if (p) {
-               *p = 0;
-       }
-
-       return talloc_strdup(ctx, hostname);
-}
-
 /****************************************************************************
  Get my own domain name, or "" if we have none.
 ****************************************************************************/
@@ -2188,24 +2080,6 @@ int set_maxfiles(int requested_max)
 #endif
 }
 
-/*****************************************************************
- Possibly replace mkstemp if it is broken.
-*****************************************************************/  
-
-int smb_mkstemp(char *name_template)
-{
-#if HAVE_SECURE_MKSTEMP
-       return mkstemp(name_template);
-#else
-       /* have a reasonable go at emulating it. Hope that
-          the system mktemp() isn't completly hopeless */
-       char *p = mktemp(name_template);
-       if (!p)
-               return -1;
-       return open(p, O_CREAT|O_EXCL|O_RDWR, 0600);
-#endif
-}
-
 /*****************************************************************
  malloc that aborts with smb_panic on fail or zero size.
  *****************************************************************/  
@@ -2256,14 +2130,19 @@ char *myhostname(void)
        if (ret == NULL) {
                /* This is cached forever so
                 * use talloc_autofree_context() ctx. */
-               ret = talloc_get_myname(talloc_autofree_context());
+               ret = get_myname(talloc_autofree_context());
        }
        return ret;
 }
 
-/*****************************************************************
- A useful function for returning a path in the Samba pid directory.
-*****************************************************************/
+/**
+ * @brief Returns an absolute path to a file concatenating the provided
+ * @a rootpath and @a basename
+ *
+ * @param name Filename, relative to @a rootpath
+ *
+ * @retval Pointer to a string containing the full path.
+ **/
 
 static char *xx_path(const char *name, const char *rootpath)
 {
@@ -2276,7 +2155,9 @@ static char *xx_path(const char *name, const char *rootpath)
        trim_string(fname,"","/");
 
        if (!directory_exist(fname)) {
-               mkdir(fname,0755);
+               if (!mkdir(fname,0755))
+                       DEBUG(1, ("Unable to create directory %s for file %s. "
+                             "Error was %s\n", fname, name, strerror(errno)));
        }
 
        return talloc_asprintf(talloc_tos(),
@@ -2285,18 +2166,26 @@ static char *xx_path(const char *name, const char *rootpath)
                                name);
 }
 
-/*****************************************************************
- A useful function for returning a path in the Samba lock directory.
-*****************************************************************/
+/**
+ * @brief Returns an absolute path to a file in the Samba lock directory.
+ *
+ * @param name File to find, relative to LOCKDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
 
 char *lock_path(const char *name)
 {
        return xx_path(name, lp_lockdir());
 }
 
-/*****************************************************************
- A useful function for returning a path in the Samba pid directory.
-*****************************************************************/
+/**
+ * @brief Returns an absolute path to a file in the Samba pid directory.
+ *
+ * @param name File to find, relative to PIDDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
 
 char *pid_path(const char *name)
 {
@@ -2342,13 +2231,30 @@ char *data_path(const char *name)
        return talloc_asprintf(talloc_tos(), "%s/%s", get_dyn_CODEPAGEDIR(), name);
 }
 
-/*****************************************************************
-a useful function for returning a path in the Samba state directory
- *****************************************************************/
+/**
+ * @brief Returns an absolute path to a file in the Samba state directory.
+ *
+ * @param name File to find, relative to STATEDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
 
 char *state_path(const char *name)
 {
-       return xx_path(name, get_dyn_STATEDIR());
+       return xx_path(name, lp_statedir());
+}
+
+/**
+ * @brief Returns an absolute path to a file in the Samba cache directory.
+ *
+ * @param name File to find, relative to CACHEDIR.
+ *
+ * @retval Pointer to a talloc'ed string containing the full path.
+ **/
+
+char *cache_path(const char *name)
+{
+       return xx_path(name, lp_cachedir());
 }
 
 /**
@@ -2364,23 +2270,10 @@ const char *shlib_ext(void)
 
 /*******************************************************************
  Given a filename - get its directory name
- NB: Returned in static storage.  Caveats:
- o  If caller wishes to preserve, they should copy.
 ********************************************************************/
 
-char *parent_dirname(const char *path)
-{
-       char *parent;
-
-       if (!parent_dirname_talloc(talloc_tos(), path, &parent, NULL)) {
-               return NULL;
-       }
-
-       return parent;
-}
-
-bool parent_dirname_talloc(TALLOC_CTX *mem_ctx, const char *dir,
-                          char **parent, const char **name)
+bool parent_dirname(TALLOC_CTX *mem_ctx, const char *dir, char **parent,
+                   const char **name)
 {
        char *p;
        ptrdiff_t len;
@@ -2399,10 +2292,9 @@ bool parent_dirname_talloc(TALLOC_CTX *mem_ctx, const char *dir,
 
        len = p-dir;
 
-       if (!(*parent = TALLOC_ARRAY(mem_ctx, char, len+1))) {
+       if (!(*parent = (char *)TALLOC_MEMDUP(mem_ctx, dir, len+1))) {
                return False;
        }
-       memcpy(*parent, dir, len);
        (*parent)[len] = '\0';
 
        if (name) {
@@ -3199,9 +3091,9 @@ NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
        return NT_STATUS_OK;
 }
 
-bool is_valid_policy_hnd(const POLICY_HND *hnd)
+bool is_valid_policy_hnd(const struct policy_handle *hnd)
 {
-       POLICY_HND tmp;
+       struct policy_handle tmp;
        ZERO_STRUCT(tmp);
        return (memcmp(&tmp, hnd, sizeof(tmp)) != 0);
 }