debug: new debug class for kerberos
[kai/samba-autobuild/.git] / lib / util / util.c
index 2d1d83036d36b85faf82e6e4b2951695f93bc59d..ef148e98d29d5cdd2ef7d164858bacda8ea6e8e2 100644 (file)
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "includes.h"
+#include "replace.h"
+#include <talloc.h>
 #include "system/network.h"
 #include "system/filesys.h"
 #include "system/locale.h"
 #include "system/shmem.h"
+#include "system/passwd.h"
+#include "system/time.h"
+#include "system/wait.h"
+#include "debug.h"
+#include "samba_util.h"
+#include "lib/util/select.h"
 
 #undef malloc
 #undef strcasecmp
 #undef strncasecmp
 #undef strdup
 #undef realloc
-
-#if defined(UID_WRAPPER)
-#if !defined(UID_WRAPPER_REPLACE) && !defined(UID_WRAPPER_NOT_REPLACE)
-#define UID_WRAPPER_REPLACE
-#include "../uid_wrapper/uid_wrapper.h"
-#endif
-#else
-#define uwrap_enabled() 0
-#endif
+#undef calloc
 
 /**
  * @file
@@ -68,31 +67,28 @@ _PUBLIC_ const char *tmpdir(void)
 **/
 int create_unlink_tmp(const char *dir)
 {
-       char *fname;
+       size_t len = strlen(dir ? dir : (dir = tmpdir()));
+       char fname[len+25];
        int fd;
+       mode_t mask;
 
-       if (!dir) {
-               dir = tmpdir();
-       }
-
-       fname = talloc_asprintf(talloc_tos(), "%s/listenerlock_XXXXXX", dir);
-       if (fname == NULL) {
+       len = snprintf(fname, sizeof(fname), "%s/listenerlock_XXXXXX", dir);
+       if (len >= sizeof(fname)) {
                errno = ENOMEM;
                return -1;
        }
+       mask = umask(S_IRWXO | S_IRWXG);
        fd = mkstemp(fname);
+       umask(mask);
        if (fd == -1) {
-               TALLOC_FREE(fname);
                return -1;
        }
        if (unlink(fname) == -1) {
                int sys_errno = errno;
                close(fd);
-               TALLOC_FREE(fname);
                errno = sys_errno;
                return -1;
        }
-       TALLOC_FREE(fname);
        return fd;
 }
 
@@ -125,6 +121,50 @@ _PUBLIC_ time_t file_modtime(const char *fname)
        return(st.st_mtime);
 }
 
+/**
+ Check file permissions.
+**/
+
+_PUBLIC_ bool file_check_permissions(const char *fname,
+                                    uid_t uid,
+                                    mode_t file_perms,
+                                    struct stat *pst)
+{
+       int ret;
+       struct stat st;
+
+       if (pst == NULL) {
+               pst = &st;
+       }
+
+       ZERO_STRUCTP(pst);
+
+       ret = stat(fname, pst);
+       if (ret != 0) {
+               DEBUG(0, ("stat failed on file '%s': %s\n",
+                        fname, strerror(errno)));
+               return false;
+       }
+
+       if (pst->st_uid != uid && !uid_wrapper_enabled()) {
+               DEBUG(0, ("invalid ownership of file '%s': "
+                        "owned by uid %u, should be %u\n",
+                        fname, (unsigned int)pst->st_uid,
+                        (unsigned int)uid));
+               return false;
+       }
+
+       if ((pst->st_mode & 0777) != file_perms) {
+               DEBUG(0, ("invalid permissions on file "
+                        "'%s': has 0%o should be 0%o\n", fname,
+                        (unsigned int)(pst->st_mode & 0777),
+                        (unsigned int)file_perms));
+               return false;
+       }
+
+       return true;
+}
+
 /**
  Check if a directory exists.
 **/
@@ -147,106 +187,113 @@ _PUBLIC_ bool directory_exist(const char *dname)
 /**
  * Try to create the specified directory if it didn't exist.
  *
- * @retval true if the directory already existed and has the right permissions 
+ * @retval true if the directory already existed
  * or was successfully created.
  */
-_PUBLIC_ bool directory_create_or_exist(const char *dname, uid_t uid, 
-                              mode_t dir_perms)
+_PUBLIC_ bool directory_create_or_exist(const char *dname,
+                                       mode_t dir_perms)
 {
+       int ret;
+       struct stat st;
        mode_t old_umask;
-       struct stat st;
-      
+
+       ret = lstat(dname, &st);
+       if (ret == 0) {
+               return true;
+       }
+
+       if (errno != ENOENT) {
+               DBG_WARNING("lstat failed on directory %s: %s\n",
+                           dname, strerror(errno));
+               return false;
+       }
+
+       /* Create directory */
        old_umask = umask(0);
-       if (lstat(dname, &st) == -1) {
-               if (errno == ENOENT) {
-                       /* Create directory */
-                       if (mkdir(dname, dir_perms) == -1) {
-                               DEBUG(0, ("error creating directory "
-                                         "%s: %s\n", dname, 
-                                         strerror(errno)));
-                               umask(old_umask);
-                               return false;
-                       }
-               } else {
-                       DEBUG(0, ("lstat failed on directory %s: %s\n",
-                                 dname, strerror(errno)));
-                       umask(old_umask);
-                       return false;
-               }
-       } else {
-               /* Check ownership and permission on existing directory */
-               if (!S_ISDIR(st.st_mode)) {
-                       DEBUG(0, ("directory %s isn't a directory\n",
-                               dname));
-                       umask(old_umask);
-                       return false;
-               }
-               if (st.st_uid != uid && !uwrap_enabled()) {
-                       DEBUG(0, ("invalid ownership on directory "
-                                 "%s\n", dname));
-                       umask(old_umask);
-                       return false;
-               }
-               if ((st.st_mode & 0777) != dir_perms) {
-                       DEBUG(0, ("invalid permissions on directory "
-                                 "'%s': has 0%o should be 0%o\n", dname,
-                                 (st.st_mode & 0777), dir_perms));
-                       umask(old_umask);
-                       return false;
-               }
+       ret = mkdir(dname, dir_perms);
+       if (ret == -1 && errno != EEXIST) {
+               DEBUG(0, ("mkdir failed on directory "
+                         "%s: %s\n", dname,
+                         strerror(errno)));
+               umask(old_umask);
+               return false;
        }
-       return true;
-}       
+       umask(old_umask);
 
+       ret = lstat(dname, &st);
+       if (ret == -1) {
+               DEBUG(0, ("lstat failed on created directory %s: %s\n",
+                         dname, strerror(errno)));
+               return false;
+       }
 
-/**
- Sleep for a specified number of milliseconds.
-**/
+       return true;
+}
 
-_PUBLIC_ void smb_msleep(unsigned int t)
+/**
+ * @brief Try to create a specified directory if it doesn't exist.
+ *
+ * The function creates a directory with the given uid and permissions if it
+ * doesn't exist. If it exists it makes sure the uid and permissions are
+ * correct and it will fail if they are different.
+ *
+ * @param[in]  dname  The directory to create.
+ *
+ * @param[in]  uid    The uid the directory needs to belong too.
+ *
+ * @param[in]  dir_perms  The expected permissions of the directory.
+ *
+ * @return True on success, false on error.
+ */
+_PUBLIC_ bool directory_create_or_exist_strict(const char *dname,
+                                              uid_t uid,
+                                              mode_t dir_perms)
 {
-#if defined(HAVE_NANOSLEEP)
-       struct timespec ts;
-       int ret;
-
-       ts.tv_sec = t/1000;
-       ts.tv_nsec = 1000000*(t%1000);
+       struct stat st;
+       bool ok;
+       int rc;
 
-       do {
-               errno = 0;
-               ret = nanosleep(&ts, &ts);
-       } while (ret < 0 && errno == EINTR && (ts.tv_sec > 0 || ts.tv_nsec > 0));
-#else
-       unsigned int tdiff=0;
-       struct timeval tval,t1,t2;
-       fd_set fds;
+       ok = directory_create_or_exist(dname, dir_perms);
+       if (!ok) {
+               return false;
+       }
 
-       GetTimeOfDay(&t1);
-       t2 = t1;
+       rc = lstat(dname, &st);
+       if (rc == -1) {
+               DEBUG(0, ("lstat failed on created directory %s: %s\n",
+                         dname, strerror(errno)));
+               return false;
+       }
 
-       while (tdiff < t) {
-               tval.tv_sec = (t-tdiff)/1000;
-               tval.tv_usec = 1000*((t-tdiff)%1000);
+       /* Check ownership and permission on existing directory */
+       if (!S_ISDIR(st.st_mode)) {
+               DEBUG(0, ("directory %s isn't a directory\n",
+                       dname));
+               return false;
+       }
+       if (st.st_uid != uid && !uid_wrapper_enabled()) {
+               DBG_NOTICE("invalid ownership on directory "
+                         "%s\n", dname);
+               return false;
+       }
+       if ((st.st_mode & 0777) != dir_perms) {
+               DEBUG(0, ("invalid permissions on directory "
+                         "'%s': has 0%o should be 0%o\n", dname,
+                         (unsigned int)(st.st_mode & 0777), (unsigned int)dir_perms));
+               return false;
+       }
 
-               /* Never wait for more than 1 sec. */
-               if (tval.tv_sec > 1) {
-                       tval.tv_sec = 1;
-                       tval.tv_usec = 0;
-               }
+       return true;
+}
 
-               FD_ZERO(&fds);
-               errno = 0;
-               select(0,&fds,NULL,NULL,&tval);
 
-               GetTimeOfDay(&t2);
-               if (t2.tv_sec < t1.tv_sec) {
-                       /* Someone adjusted time... */
-                       t1 = t2;
-               }
+/**
+ Sleep for a specified number of milliseconds.
+**/
 
-               tdiff = usec_time_diff(&t2,&t1)/1000;
-       }
-#endif
+_PUBLIC_ void smb_msleep(unsigned int t)
+{
+       sys_poll_intr(NULL, 0, t);
 }
 
 /**
@@ -284,7 +331,9 @@ _PUBLIC_ bool process_exists_by_pid(pid_t pid)
 {
        /* Doing kill with a non-positive pid causes messages to be
         * sent to places we don't want. */
-       SMB_ASSERT(pid > 0);
+       if (pid <= 0) {
+               return false;
+       }
        return(kill(pid,0) == 0 || errno != ESRCH);
 }
 
@@ -338,6 +387,19 @@ _PUBLIC_ bool fcntl_lock(int fd, int op, off_t offset, off_t count, int type)
        return true;
 }
 
+struct debug_channel_level {
+       int channel;
+       int level;
+};
+
+static void debugadd_channel_cb(const char *buf, void *private_data)
+{
+       struct debug_channel_level *dcl =
+               (struct debug_channel_level *)private_data;
+
+       DEBUGADDC(dcl->channel, dcl->level,("%s", buf));
+}
+
 static void debugadd_cb(const char *buf, void *private_data)
 {
        int *plevel = (int *)private_data;
@@ -424,7 +486,7 @@ void dump_data_cb(const uint8_t *buf, int len,
        if (i%16) {
                int n;
                n = 16 - (i%16);
-               cb(" ", private_data);
+               cb("  ", private_data);
                if (n>8) {
                        cb(" ", private_data);
                }
@@ -456,6 +518,22 @@ _PUBLIC_ void dump_data(int level, const uint8_t *buf, int len)
        dump_data_cb(buf, len, false, debugadd_cb, &level);
 }
 
+/**
+ * Write dump of binary data to the log file.
+ *
+ * The data is only written if the log level is at least level for
+ * debug class dbgc_class.
+ */
+_PUBLIC_ void dump_data_dbgc(int dbgc_class, int level, const uint8_t *buf, int len)
+{
+       struct debug_channel_level dcl = { dbgc_class, level };
+
+       if (!DEBUGLVLC(dbgc_class, level)) {
+               return;
+       }
+       dump_data_cb(buf, len, false, debugadd_channel_cb, &dcl);
+}
+
 /**
  * Write dump of binary data to the log file.
  *
@@ -470,6 +548,17 @@ _PUBLIC_ void dump_data_skip_zeros(int level, const uint8_t *buf, int len)
        dump_data_cb(buf, len, true, debugadd_cb, &level);
 }
 
+static void fprintf_cb(const char *buf, void *private_data)
+{
+       FILE *f = (FILE *)private_data;
+       fprintf(f, "%s", buf);
+}
+
+void dump_data_file(const uint8_t *buf, int len, bool omit_zero_bytes,
+                   FILE *f)
+{
+       dump_data_cb(buf, len, omit_zero_bytes, fprintf_cb, f);
+}
 
 /**
  malloc that aborts with smb_panic on fail or zero size.
@@ -563,7 +652,7 @@ char *smb_xstrndup(const char *s, size_t n)
  Like strdup but for memory.
 **/
 
-_PUBLIC_ void *memdup(const void *p, size_t size)
+_PUBLIC_ void *smb_memdup(const void *p, size_t size)
 {
        void *p2;
        if (size == 0)
@@ -599,7 +688,7 @@ _PUBLIC_ void dump_data_pw(const char *msg, const uint8_t * data, size_t len)
  */
 _PUBLIC_ bool all_zero(const uint8_t *ptr, size_t size)
 {
-       int i;
+       size_t i;
        if (!ptr) return true;
        for (i=0;i<size;i++) {
                if (ptr[i]) return false;
@@ -634,6 +723,34 @@ void *malloc_array(size_t el_size, unsigned int count)
        return realloc_array(NULL, el_size, count, false);
 }
 
+/****************************************************************************
+ Type-safe memalign
+****************************************************************************/
+
+void *memalign_array(size_t el_size, size_t align, unsigned int count)
+{
+       if (el_size == 0 || count >= MAX_MALLOC_SIZE/el_size) {
+               return NULL;
+       }
+
+       return memalign(align, el_size*count);
+}
+
+/****************************************************************************
+ Type-safe calloc.
+****************************************************************************/
+
+void *calloc_array(size_t size, size_t nmemb)
+{
+       if (nmemb >= MAX_MALLOC_SIZE/size) {
+               return NULL;
+       }
+       if (size == 0 || nmemb == 0) {
+               return NULL;
+       }
+       return calloc(nmemb, size);
+}
+
 /**
  Trim the specified elements off the front and back of a string.
 **/
@@ -645,24 +762,29 @@ _PUBLIC_ bool trim_string(char *s, const char *front, const char *back)
        size_t len;
 
        /* Ignore null or empty strings. */
-       if (!s || (s[0] == '\0'))
+       if (!s || (s[0] == '\0')) {
                return false;
+       }
+       len = strlen(s);
 
        front_len       = front? strlen(front) : 0;
        back_len        = back? strlen(back) : 0;
 
-       len = strlen(s);
-
        if (front_len) {
-               while (len && strncmp(s, front, front_len)==0) {
+               size_t front_trim = 0;
+
+               while (strncmp(s+front_trim, front, front_len)==0) {
+                       front_trim += front_len;
+               }
+               if (front_trim > 0) {
                        /* Must use memmove here as src & dest can
                         * easily overlap. Found by valgrind. JRA. */
-                       memmove(s, s+front_len, (len-front_len)+1);
-                       len -= front_len;
+                       memmove(s, s+front_trim, (len-front_trim)+1);
+                       len -= front_trim;
                        ret=true;
                }
        }
-       
+
        if (back_len) {
                while ((len >= back_len) && strncmp(s+len-back_len,back,back_len)==0) {
                        s[len-back_len]='\0';
@@ -689,15 +811,15 @@ _PUBLIC_ _PURE_ size_t count_chars(const char *s, char c)
 }
 
 /**
Routine to get hex characters and turn them into a 16 byte array.
- the array can be variable length, and any non-hex-numeric
- characters are skipped.  "0xnn" or "0Xnn" is specially catered
- for.
-
- valid examples: "0A5D15"; "0x15, 0x49, 0xa2"; "59\ta9\te3\n"
-
-
-**/
* Routine to get hex characters and turn them into a byte array.
+ * the array can be variable length.
+ * -  "0xnn" or "0Xnn" is specially catered for.
+ * - The first non-hex-digit character (apart from possibly leading "0x"
+ *   finishes the conversion and skips the rest of the input.
+ * - A single hex-digit character at the end of the string is skipped.
+ *
+ * valid examples: "0A5D15"; "0x123456"
+ */
 _PUBLIC_ size_t strhex_to_str(char *p, size_t p_len, const char *strhex, size_t strhex_len)
 {
        size_t i = 0;
@@ -711,14 +833,18 @@ _PUBLIC_ size_t strhex_to_str(char *p, size_t p_len, const char *strhex, size_t
                i += 2; /* skip two chars */
        }
 
-       for (; i < strhex_len && strhex[i] != 0; i++) {
-               if (!(p1 = strchr(hexchars, toupper((unsigned char)strhex[i]))))
+       for (; i+1 < strhex_len && strhex[i] != 0 && strhex[i+1] != 0; i++) {
+               p1 = strchr(hexchars, toupper((unsigned char)strhex[i]));
+               if (p1 == NULL) {
                        break;
+               }
 
                i++; /* next hex digit */
 
-               if (!(p2 = strchr(hexchars, toupper((unsigned char)strhex[i]))))
+               p2 = strchr(hexchars, toupper((unsigned char)strhex[i]));
+               if (p2 == NULL) {
                        break;
+               }
 
                /* get the two nybbles */
                hinybble = PTR_DIFF(p1, hexchars);
@@ -737,8 +863,8 @@ _PUBLIC_ size_t strhex_to_str(char *p, size_t p_len, const char *strhex, size_t
        return num_chars;
 }
 
-/** 
- * Parse a hex string and return a data blob. 
+/**
+ * Parse a hex string and return a data blob.
  */
 _PUBLIC_ _PURE_ DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *strhex) 
 {
@@ -751,38 +877,73 @@ _PUBLIC_ _PURE_ DATA_BLOB strhex_to_data_blob(TALLOC_CTX *mem_ctx, const char *s
        return ret_blob;
 }
 
-
 /**
- * Routine to print a buffer as HEX digits, into an allocated string.
+ * Parse a hex dump and return a data blob. Hex dump is structured as 
+ * is generated from dump_data_cb() elsewhere in this file
+ * 
  */
-_PUBLIC_ void hex_encode(const unsigned char *buff_in, size_t len, char **out_hex_buffer)
+_PUBLIC_ _PURE_ DATA_BLOB hexdump_to_data_blob(TALLOC_CTX *mem_ctx, const char *hexdump, size_t hexdump_len)
 {
-       int i;
-       char *hex_buffer;
-
-       *out_hex_buffer = malloc_array_p(char, (len*2)+1);
-       hex_buffer = *out_hex_buffer;
+       DATA_BLOB ret_blob = { 0 };
+       size_t i = 0;
+       size_t char_count = 0;
+       /* hexdump line length is 77 chars long. We then use the ASCII representation of the bytes
+        * at the end of the final line to calculate how many are in that line, minus the extra space
+        * and newline. */
+       size_t hexdump_byte_count = (16 * (hexdump_len / 77));
+       if (hexdump_len % 77) {
+               hexdump_byte_count += ((hexdump_len % 77) - 59 - 2);
+       }
+       
+       ret_blob = data_blob_talloc(mem_ctx, NULL, hexdump_byte_count+1);
+       for (; i+1 < hexdump_len && hexdump[i] != 0 && hexdump[i+1] != 0; i++) {
+               if ((i%77) == 0) 
+                       i += 7; /* Skip the offset at the start of the line */
+               if ((i%77) < 56) { /* position 56 is after both hex chunks */
+                       if (hexdump[i] != ' ') {
+                               char_count += strhex_to_str((char *)&ret_blob.data[char_count],
+                                                           hexdump_byte_count - char_count,
+                                                           &hexdump[i], 2);
+                               i += 2;
+                       } else {
+                               i++;
+                       }
+               } else {
+                       i++;
+               }
+       }
+       ret_blob.length = char_count;
+       
+       return ret_blob;
+}
 
-       for (i = 0; i < len; i++)
-               slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
+/**
+ * Print a buf in hex. Assumes dst is at least (srclen*2)+1 large.
+ */
+_PUBLIC_ void hex_encode_buf(char *dst, const uint8_t *src, size_t srclen)
+{
+       size_t i;
+       for (i=0; i<srclen; i++) {
+               snprintf(dst + i*2, 3, "%02X", src[i]);
+       }
+       /*
+        * Ensure 0-termination for 0-length buffers
+        */
+       dst[srclen*2] = '\0';
 }
 
 /**
- * talloc version of hex_encode()
+ * talloc version of hex_encode_buf()
  */
 _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len)
 {
-       int i;
        char *hex_buffer;
 
        hex_buffer = talloc_array(mem_ctx, char, (len*2)+1);
        if (!hex_buffer) {
                return NULL;
        }
-
-       for (i = 0; i < len; i++)
-               slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
-
+       hex_encode_buf(hex_buffer, buff_in, len);
        talloc_set_name_const(hex_buffer, hex_buffer);
        return hex_buffer;
 }
@@ -819,207 +980,6 @@ _PUBLIC_ size_t ascii_len_n(const char *src, size_t n)
        return len;
 }
 
-/**
- Set a boolean variable from the text value stored in the passed string.
- Returns true in success, false if the passed string does not correctly 
- represent a boolean.
-**/
-
-_PUBLIC_ bool set_boolean(const char *boolean_string, bool *boolean)
-{
-       if (strwicmp(boolean_string, "yes") == 0 ||
-           strwicmp(boolean_string, "true") == 0 ||
-           strwicmp(boolean_string, "on") == 0 ||
-           strwicmp(boolean_string, "1") == 0) {
-               *boolean = true;
-               return true;
-       } else if (strwicmp(boolean_string, "no") == 0 ||
-                  strwicmp(boolean_string, "false") == 0 ||
-                  strwicmp(boolean_string, "off") == 0 ||
-                  strwicmp(boolean_string, "0") == 0) {
-               *boolean = false;
-               return true;
-       }
-       return false;
-}
-
-/**
-return the number of bytes occupied by a buffer in CH_UTF16 format
-the result includes the null termination
-**/
-_PUBLIC_ size_t utf16_len(const void *buf)
-{
-       size_t len;
-
-       for (len = 0; SVAL(buf,len); len += 2) ;
-
-       return len + 2;
-}
-
-/**
-return the number of bytes occupied by a buffer in CH_UTF16 format
-the result includes the null termination
-limited by 'n' bytes
-**/
-_PUBLIC_ size_t utf16_len_n(const void *src, size_t n)
-{
-       size_t len;
-
-       for (len = 0; (len+2 < n) && SVAL(src, len); len += 2) ;
-
-       if (len+2 <= n) {
-               len += 2;
-       }
-
-       return len;
-}
-
-/**
- * @file
- * @brief String utilities.
- **/
-
-static bool next_token_internal_talloc(TALLOC_CTX *ctx,
-                               const char **ptr,
-                                char **pp_buff,
-                                const char *sep,
-                                bool ltrim)
-{
-       const char *s;
-       const char *saved_s;
-       char *pbuf;
-       bool quoted;
-       size_t len=1;
-
-       *pp_buff = NULL;
-       if (!ptr) {
-               return(false);
-       }
-
-       s = *ptr;
-
-       /* default to simple separators */
-       if (!sep) {
-               sep = " \t\n\r";
-       }
-
-       /* find the first non sep char, if left-trimming is requested */
-       if (ltrim) {
-               while (*s && strchr_m(sep,*s)) {
-                       s++;
-               }
-       }
-
-       /* nothing left? */
-       if (!*s) {
-               return false;
-       }
-
-       /* When restarting we need to go from here. */
-       saved_s = s;
-
-       /* Work out the length needed. */
-       for (quoted = false; *s &&
-                       (quoted || !strchr_m(sep,*s)); s++) {
-               if (*s == '\"') {
-                       quoted = !quoted;
-               } else {
-                       len++;
-               }
-       }
-
-       /* We started with len = 1 so we have space for the nul. */
-       *pp_buff = talloc_array(ctx, char, len);
-       if (!*pp_buff) {
-               return false;
-       }
-
-       /* copy over the token */
-       pbuf = *pp_buff;
-       s = saved_s;
-       for (quoted = false; *s &&
-                       (quoted || !strchr_m(sep,*s)); s++) {
-               if ( *s == '\"' ) {
-                       quoted = !quoted;
-               } else {
-                       *pbuf++ = *s;
-               }
-       }
-
-       *ptr = (*s) ? s+1 : s;
-       *pbuf = 0;
-
-       return true;
-}
-
-bool next_token_talloc(TALLOC_CTX *ctx,
-                       const char **ptr,
-                       char **pp_buff,
-                       const char *sep)
-{
-       return next_token_internal_talloc(ctx, ptr, pp_buff, sep, true);
-}
-
-/*
- * Get the next token from a string, return false if none found.  Handles
- * double-quotes.  This version does not trim leading separator characters
- * before looking for a token.
- */
-
-bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx,
-                       const char **ptr,
-                       char **pp_buff,
-                       const char *sep)
-{
-       return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false);
-}
-
-/**
- * Get the next token from a string, return False if none found.
- * Handles double-quotes.
- *
- * Based on a routine by GJC@VILLAGE.COM.
- * Extensively modified by Andrew.Tridgell@anu.edu.au
- **/
-_PUBLIC_ bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
-{
-       const char *s;
-       bool quoted;
-       size_t len=1;
-
-       if (!ptr)
-               return false;
-
-       s = *ptr;
-
-       /* default to simple separators */
-       if (!sep)
-               sep = " \t\n\r";
-
-       /* find the first non sep char */
-       while (*s && strchr_m(sep,*s))
-               s++;
-
-       /* nothing left? */
-       if (!*s)
-               return false;
-
-       /* copy over the token */
-       for (quoted = false; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
-               if (*s == '\"') {
-                       quoted = !quoted;
-               } else {
-                       len++;
-                       *buff++ = *s;
-               }
-       }
-
-       *ptr = (*s) ? s+1 : s;
-       *buff = 0;
-
-       return true;
-}
-
 struct anonymous_shared_header {
        union {
                size_t length;
@@ -1057,8 +1017,21 @@ void *anonymous_shared_allocate(size_t orig_bufsz)
        buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED,
                        -1 /* fd */, 0 /* offset */);
 #else
+{
+       int saved_errno;
+       int fd;
+
+       fd = open("/dev/zero", O_RDWR);
+       if (fd == -1) {
+               return NULL;
+       }
+
        buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
-                       open("/dev/zero", O_RDWR), 0 /* offset */);
+                  fd, 0 /* offset */);
+       saved_errno = errno;
+       close(fd);
+       errno = saved_errno;
+}
 #endif
 
        if (buf == MAP_FAILED) {
@@ -1073,6 +1046,70 @@ void *anonymous_shared_allocate(size_t orig_bufsz)
        return ptr;
 }
 
+void *anonymous_shared_resize(void *ptr, size_t new_size, bool maymove)
+{
+#ifdef HAVE_MREMAP
+       void *buf;
+       size_t pagesz = getpagesize();
+       size_t pagecnt;
+       size_t bufsz;
+       struct anonymous_shared_header *hdr;
+       int flags = 0;
+
+       if (ptr == NULL) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       hdr = (struct anonymous_shared_header *)ptr;
+       hdr--;
+       if (hdr->u.length > (new_size + sizeof(*hdr))) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       bufsz = new_size + sizeof(*hdr);
+
+       /* round up to full pages */
+       pagecnt = bufsz / pagesz;
+       if (bufsz % pagesz) {
+               pagecnt += 1;
+       }
+       bufsz = pagesz * pagecnt;
+
+       if (new_size >= bufsz) {
+               /* integer wrap */
+               errno = ENOSPC;
+               return NULL;
+       }
+
+       if (bufsz <= hdr->u.length) {
+               return ptr;
+       }
+
+       if (maymove) {
+               flags = MREMAP_MAYMOVE;
+       }
+
+       buf = mremap(hdr, hdr->u.length, bufsz, flags);
+
+       if (buf == MAP_FAILED) {
+               errno = ENOSPC;
+               return NULL;
+       }
+
+       hdr = (struct anonymous_shared_header *)buf;
+       hdr->u.length = bufsz;
+
+       ptr = (void *)(&hdr[1]);
+
+       return ptr;
+#else
+       errno = ENOSPC;
+       return NULL;
+#endif
+}
+
 void anonymous_shared_free(void *ptr)
 {
        struct anonymous_shared_header *hdr;