Revert "smbd: add an effective connection_struct->user_ev_ctx that holds the event...
[samba.git] / source3 / lib / util.c
index 2895c14241d1daaa90455cd6151b31660776da97..5dbd67349faf8f45abeb82c5d8ca91500cd24b66 100644 (file)
@@ -24,6 +24,7 @@
 #include "includes.h"
 #include "system/passwd.h"
 #include "system/filesys.h"
+#include "lib/util/server_id.h"
 #include "util_tdb.h"
 #include "ctdbd_conn.h"
 #include "../lib/util/util_pw.h"
@@ -34,6 +35,8 @@
 #include "lib/util/sys_rw.h"
 #include "lib/util/sys_rw_data.h"
 #include "lib/util/util_process.h"
+#include "lib/dbwrap/dbwrap_ctdb.h"
+#include "lib/gencache.h"
 
 #ifdef HAVE_SYS_PRCTL_H
 #include <sys/prctl.h>
 #define MAX_ALLOC_SIZE (1024*1024*256)
 
 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
+/* rpc/xdr.h uses TRUE and FALSE */
+#ifdef TRUE
+#undef TRUE
+#endif
+
+#ifdef FALSE
+#undef FALSE
+#endif
+
+#include "system/nis.h"
+
 #ifdef WITH_NISPLUS_HOME
 #ifdef BROKEN_NISPLUS_INCLUDE_FILES
 /*
@@ -425,6 +439,7 @@ static void reinit_after_fork_pipe_handler(struct tevent_context *ev,
                 * we have reached EOF on stdin, which means the
                 * parent has exited. Shutdown the server
                 */
+               TALLOC_FREE(fde);
                (void)kill(getpid(), SIGTERM);
        }
 }
@@ -436,6 +451,14 @@ NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
                           const char *comment)
 {
        NTSTATUS status = NT_STATUS_OK;
+       int ret;
+
+       /*
+        * The main process thread should never
+        * allow per_thread_cwd_enable() to be
+        * called.
+        */
+       per_thread_cwd_disable();
 
        if (reinit_after_fork_pipe[1] != -1) {
                close(reinit_after_fork_pipe[1]);
@@ -477,6 +500,16 @@ NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
                        DEBUG(0,("messaging_reinit() failed: %s\n",
                                 nt_errstr(status)));
                }
+
+               if (lp_clustering()) {
+                       ret = ctdb_async_ctx_reinit(
+                               NULL, messaging_tevent_context(msg_ctx));
+                       if (ret != 0) {
+                               DBG_ERR("db_ctdb_async_ctx_reinit failed: %s\n",
+                                       strerror(errno));
+                               return map_nt_error_from_unix(ret);
+                       }
+               }
        }
 
        if (comment) {
@@ -814,145 +847,6 @@ void smb_panic_s3(const char *why)
        dump_core();
 }
 
-/*******************************************************************
- Print a backtrace of the stack to the debug log. This function
- DELIBERATELY LEAKS MEMORY. The expectation is that you should
- exit shortly after calling it.
-********************************************************************/
-
-#ifdef HAVE_LIBUNWIND_H
-#include <libunwind.h>
-#endif
-
-#ifdef HAVE_EXECINFO_H
-#include <execinfo.h>
-#endif
-
-#ifdef HAVE_LIBEXC_H
-#include <libexc.h>
-#endif
-
-void log_stack_trace(void)
-{
-#ifdef HAVE_LIBUNWIND
-       /* Try to use libunwind before any other technique since on ia64
-        * libunwind correctly walks the stack in more circumstances than
-        * backtrace.
-        */ 
-       unw_cursor_t cursor;
-       unw_context_t uc;
-       unsigned i = 0;
-
-       char procname[256];
-       unw_word_t ip, sp, off;
-
-       procname[sizeof(procname) - 1] = '\0';
-
-       if (unw_getcontext(&uc) != 0) {
-               goto libunwind_failed;
-       }
-
-       if (unw_init_local(&cursor, &uc) != 0) {
-               goto libunwind_failed;
-       }
-
-       DEBUG(0, ("BACKTRACE:\n"));
-
-       do {
-           ip = sp = 0;
-           unw_get_reg(&cursor, UNW_REG_IP, &ip);
-           unw_get_reg(&cursor, UNW_REG_SP, &sp);
-
-           switch (unw_get_proc_name(&cursor,
-                       procname, sizeof(procname) - 1, &off) ) {
-           case 0:
-                   /* Name found. */
-           case -UNW_ENOMEM:
-                   /* Name truncated. */
-                   DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
-                           i, procname, (long long)off,
-                           (long long)ip, (long long) sp));
-                   break;
-           default:
-           /* case -UNW_ENOINFO: */
-           /* case -UNW_EUNSPEC: */
-                   /* No symbol name found. */
-                   DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
-                           i, "<unknown symbol>",
-                           (long long)ip, (long long) sp));
-           }
-           ++i;
-       } while (unw_step(&cursor) > 0);
-
-       return;
-
-libunwind_failed:
-       DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
-
-#elif HAVE_BACKTRACE_SYMBOLS
-       void *backtrace_stack[BACKTRACE_STACK_SIZE];
-       size_t backtrace_size;
-       char **backtrace_strings;
-
-       /* get the backtrace (stack frames) */
-       backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
-       backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
-
-       DEBUG(0, ("BACKTRACE: %lu stack frames:\n", 
-                 (unsigned long)backtrace_size));
-
-       if (backtrace_strings) {
-               int i;
-
-               for (i = 0; i < backtrace_size; i++)
-                       DEBUGADD(0, (" #%u %s\n", i, backtrace_strings[i]));
-
-               /* Leak the backtrace_strings, rather than risk what free() might do */
-       }
-
-#elif HAVE_LIBEXC
-
-       /* The IRIX libexc library provides an API for unwinding the stack. See
-        * libexc(3) for details. Apparantly trace_back_stack leaks memory, but
-        * since we are about to abort anyway, it hardly matters.
-        */
-
-#define NAMESIZE 32 /* Arbitrary */
-
-       __uint64_t      addrs[BACKTRACE_STACK_SIZE];
-       char *          names[BACKTRACE_STACK_SIZE];
-       char            namebuf[BACKTRACE_STACK_SIZE * NAMESIZE];
-
-       int             i;
-       int             levels;
-
-       ZERO_ARRAY(addrs);
-       ZERO_ARRAY(names);
-       ZERO_ARRAY(namebuf);
-
-       /* We need to be root so we can open our /proc entry to walk
-        * our stack. It also helps when we want to dump core.
-        */
-       become_root();
-
-       for (i = 0; i < BACKTRACE_STACK_SIZE; i++) {
-               names[i] = namebuf + (i * NAMESIZE);
-       }
-
-       levels = trace_back_stack(0, addrs, names,
-                       BACKTRACE_STACK_SIZE, NAMESIZE - 1);
-
-       DEBUG(0, ("BACKTRACE: %d stack frames:\n", levels));
-       for (i = 0; i < levels; i++) {
-               DEBUGADD(0, (" #%d 0x%llx %s\n", i, addrs[i], names[i]));
-       }
-#undef NAMESIZE
-
-#else
-       DEBUG(0, ("unable to produce a stack trace on this platform\n"));
-#endif
-}
-
 /*******************************************************************
   A readdir wrapper which just returns the file name.
  ********************************************************************/
@@ -1150,13 +1044,13 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist_in)
  F_UNLCK in *ptype if the region is unlocked). False if the call failed.
 ****************************************************************************/
 
-bool fcntl_getlock(int fd, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
+bool fcntl_getlock(int fd, int op, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppid)
 {
        struct flock lock;
        int ret;
 
-       DEBUG(8,("fcntl_getlock fd=%d offset=%.0f count=%.0f type=%d\n",
-                   fd,(double)*poffset,(double)*pcount,*ptype));
+       DEBUG(8,("fcntl_getlock fd=%d op=%d offset=%.0f count=%.0f type=%d\n",
+                   fd,op,(double)*poffset,(double)*pcount,*ptype));
 
        lock.l_type = *ptype;
        lock.l_whence = SEEK_SET;
@@ -1164,7 +1058,7 @@ bool fcntl_getlock(int fd, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppi
        lock.l_len = *pcount;
        lock.l_pid = 0;
 
-       ret = sys_fcntl_ptr(fd,F_GETLK,&lock);
+       ret = sys_fcntl_ptr(fd,op,&lock);
 
        if (ret == -1) {
                int sav = errno;
@@ -1184,6 +1078,37 @@ bool fcntl_getlock(int fd, off_t *poffset, off_t *pcount, int *ptype, pid_t *ppi
        return True;
 }
 
+#if defined(HAVE_OFD_LOCKS)
+int map_process_lock_to_ofd_lock(int op, bool *use_ofd_locks)
+{
+       switch (op) {
+       case F_GETLK:
+       case F_OFD_GETLK:
+               op = F_OFD_GETLK;
+               break;
+       case F_SETLK:
+       case F_OFD_SETLK:
+               op = F_OFD_SETLK;
+               break;
+       case F_SETLKW:
+       case F_OFD_SETLKW:
+               op = F_OFD_SETLKW;
+               break;
+       default:
+               *use_ofd_locks = false;
+               return -1;
+       }
+       *use_ofd_locks = true;
+       return op;
+}
+#else /* HAVE_OFD_LOCKS */
+int map_process_lock_to_ofd_lock(int op, bool *use_ofd_locks)
+{
+       *use_ofd_locks = false;
+       return op;
+}
+#endif /* HAVE_OFD_LOCKS */
+
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_ALL
 
@@ -1230,14 +1155,46 @@ void ra_lanman_string( const char *native_lanman )
                set_remote_arch( RA_WIN2K3 );
 }
 
-static const char *remote_arch_str;
+static const char *remote_arch_strings[] = {
+       [RA_UNKNOWN] =  "UNKNOWN",
+       [RA_WFWG] =     "WfWg",
+       [RA_OS2] =      "OS2",
+       [RA_WIN95] =    "Win95",
+       [RA_WINNT] =    "WinNT",
+       [RA_WIN2K] =    "Win2K",
+       [RA_WINXP] =    "WinXP",
+       [RA_WIN2K3] =   "Win2K3",
+       [RA_VISTA] =    "Vista",
+       [RA_SAMBA] =    "Samba",
+       [RA_CIFSFS] =   "CIFSFS",
+       [RA_WINXP64] =  "WinXP64",
+       [RA_OSX] =      "OSX",
+};
 
 const char *get_remote_arch_str(void)
 {
-       if (!remote_arch_str) {
-               return "UNKNOWN";
+       if (ra_type >= ARRAY_SIZE(remote_arch_strings)) {
+               /*
+                * set_remote_arch() already checks this so ra_type
+                * should be in the allowed range, but anyway, let's
+                * do another bound check here.
+                */
+               DBG_ERR("Remote arch info out of sync [%d] missing\n", ra_type);
+               ra_type = RA_UNKNOWN;
        }
-       return remote_arch_str;
+       return remote_arch_strings[ra_type];
+}
+
+enum remote_arch_types get_remote_arch_from_str(const char *remote_arch_string)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(remote_arch_strings); i++) {
+               if (strcmp(remote_arch_string, remote_arch_strings[i]) == 0) {
+                       return i;
+               }
+       }
+       return RA_UNKNOWN;
 }
 
 /*******************************************************************
@@ -1246,52 +1203,20 @@ const char *get_remote_arch_str(void)
 
 void set_remote_arch(enum remote_arch_types type)
 {
-       ra_type = type;
-       switch( type ) {
-       case RA_WFWG:
-               remote_arch_str = "WfWg";
-               break;
-       case RA_OS2:
-               remote_arch_str = "OS2";
-               break;
-       case RA_WIN95:
-               remote_arch_str = "Win95";
-               break;
-       case RA_WINNT:
-               remote_arch_str = "WinNT";
-               break;
-       case RA_WIN2K:
-               remote_arch_str = "Win2K";
-               break;
-       case RA_WINXP:
-               remote_arch_str = "WinXP";
-               break;
-       case RA_WINXP64:
-               remote_arch_str = "WinXP64";
-               break;
-       case RA_WIN2K3:
-               remote_arch_str = "Win2K3";
-               break;
-       case RA_VISTA:
-               remote_arch_str = "Vista";
-               break;
-       case RA_SAMBA:
-               remote_arch_str = "Samba";
-               break;
-       case RA_CIFSFS:
-               remote_arch_str = "CIFSFS";
-               break;
-       case RA_OSX:
-               remote_arch_str = "OSX";
-               break;
-       default:
+       if (ra_type >= ARRAY_SIZE(remote_arch_strings)) {
+               /*
+                * This protects against someone adding values to enum
+                * remote_arch_types without updating
+                * remote_arch_strings array.
+                */
+               DBG_ERR("Remote arch info out of sync [%d] missing\n", ra_type);
                ra_type = RA_UNKNOWN;
-               remote_arch_str = "UNKNOWN";
-               break;
+               return;
        }
 
+       ra_type = type;
        DEBUG(10,("set_remote_arch: Client arch is \'%s\'\n",
-                               remote_arch_str));
+                 get_remote_arch_str()));
 }
 
 /*******************************************************************
@@ -1303,6 +1228,148 @@ enum remote_arch_types get_remote_arch(void)
        return ra_type;
 }
 
+#define RA_CACHE_TTL 7*24*3600
+
+static bool remote_arch_cache_key(const struct GUID *client_guid,
+                                 fstring key)
+{
+       struct GUID_txt_buf guid_buf;
+       const char *guid_string = NULL;
+
+       guid_string = GUID_buf_string(client_guid, &guid_buf);
+       if (guid_string == NULL) {
+               return false;
+       }
+
+       fstr_sprintf(key, "RA/%s", guid_string);
+       return true;
+}
+
+struct ra_parser_state {
+       bool found;
+       enum remote_arch_types ra;
+};
+
+static void ra_parser(const struct gencache_timeout *t,
+                     DATA_BLOB blob,
+                     void *priv_data)
+{
+       struct ra_parser_state *state = (struct ra_parser_state *)priv_data;
+       const char *ra_str = NULL;
+
+       if (gencache_timeout_expired(t)) {
+               return;
+       }
+
+       if ((blob.length == 0) || (blob.data[blob.length-1] != '\0')) {
+               DBG_ERR("Remote arch cache key not a string\n");
+               return;
+       }
+
+       ra_str = (const char *)blob.data;
+       DBG_INFO("Got remote arch [%s] from cache\n", ra_str);
+
+       state->ra = get_remote_arch_from_str(ra_str);
+       state->found = true;
+       return;
+}
+
+static bool remote_arch_cache_get(const struct GUID *client_guid)
+{
+       bool ok;
+       fstring ra_key;
+       struct ra_parser_state state = (struct ra_parser_state) {
+               .found = false,
+               .ra = RA_UNKNOWN,
+       };
+
+       ok = remote_arch_cache_key(client_guid, ra_key);
+       if (!ok) {
+               return false;
+       }
+
+       ok = gencache_parse(ra_key, ra_parser, &state);
+       if (!ok || !state.found) {
+               return true;
+       }
+
+       if (state.ra == RA_UNKNOWN) {
+               return true;
+       }
+
+       set_remote_arch(state.ra);
+       return true;
+}
+
+static bool remote_arch_cache_set(const struct GUID *client_guid)
+{
+       bool ok;
+       fstring ra_key;
+       const char *ra_str = NULL;
+
+       if (get_remote_arch() == RA_UNKNOWN) {
+               return true;
+       }
+
+       ok = remote_arch_cache_key(client_guid, ra_key);
+       if (!ok) {
+               return false;
+       }
+
+       ra_str = get_remote_arch_str();
+       if (ra_str == NULL) {
+               return false;
+       }
+
+       ok = gencache_set(ra_key, ra_str, time(NULL) + RA_CACHE_TTL);
+       if (!ok) {
+               return false;
+       }
+
+       return true;
+}
+
+bool remote_arch_cache_update(const struct GUID *client_guid)
+{
+       bool ok;
+
+       if (get_remote_arch() == RA_UNKNOWN) {
+
+               become_root();
+               ok = remote_arch_cache_get(client_guid);
+               unbecome_root();
+
+               return ok;
+       }
+
+       become_root();
+       ok = remote_arch_cache_set(client_guid);
+       unbecome_root();
+
+       return ok;
+}
+
+bool remote_arch_cache_delete(const struct GUID *client_guid)
+{
+       bool ok;
+       fstring ra_key;
+
+       ok = remote_arch_cache_key(client_guid, ra_key);
+       if (!ok) {
+               return false;
+       }
+
+       become_root();
+       ok = gencache_del(ra_key);
+       unbecome_root();
+
+       if (!ok) {
+               return false;
+       }
+
+       return true;
+}
+
 const char *tab_depth(int level, int depth)
 {
        if( CHECK_DEBUGLVL(level) ) {
@@ -1581,7 +1648,7 @@ bool mask_match(const char *string, const char *pattern, bool is_case_sensitive)
        if (ISDOT(pattern))
                return False;
 
-       return ms_fnmatch(pattern, string, Protocol <= PROTOCOL_LANMAN2, is_case_sensitive) == 0;
+       return ms_fnmatch_protocol(pattern, string, Protocol, is_case_sensitive) == 0;
 }
 
 /*******************************************************************
@@ -1614,152 +1681,6 @@ bool mask_match_list(const char *string, char **list, int listLen, bool is_case_
        return False;
 }
 
-/*********************************************************
- Recursive routine that is called by unix_wild_match.
-*********************************************************/
-
-static bool unix_do_match(const char *regexp, const char *str)
-{
-       const char *p;
-
-       for( p = regexp; *p && *str; ) {
-
-               switch(*p) {
-                       case '?':
-                               str++;
-                               p++;
-                               break;
-
-                       case '*':
-
-                               /*
-                                * Look for a character matching 
-                                * the one after the '*'.
-                                */
-                               p++;
-                               if(!*p)
-                                       return true; /* Automatic match */
-                               while(*str) {
-
-                                       while(*str && (*p != *str))
-                                               str++;
-
-                                       /*
-                                        * Patch from weidel@multichart.de. In the case of the regexp
-                                        * '*XX*' we want to ensure there are at least 2 'X' characters
-                                        * in the string after the '*' for a match to be made.
-                                        */
-
-                                       {
-                                               int matchcount=0;
-
-                                               /*
-                                                * Eat all the characters that match, but count how many there were.
-                                                */
-
-                                               while(*str && (*p == *str)) {
-                                                       str++;
-                                                       matchcount++;
-                                               }
-
-                                               /*
-                                                * Now check that if the regexp had n identical characters that
-                                                * matchcount had at least that many matches.
-                                                */
-
-                                               while ( *(p+1) && (*(p+1) == *p)) {
-                                                       p++;
-                                                       matchcount--;
-                                               }
-
-                                               if ( matchcount <= 0 )
-                                                       return false;
-                                       }
-
-                                       str--; /* We've eaten the match char after the '*' */
-
-                                       if(unix_do_match(p, str))
-                                               return true;
-
-                                       if(!*str)
-                                               return false;
-                                       else
-                                               str++;
-                               }
-                               return false;
-
-                       default:
-                               if(*str != *p)
-                                       return false;
-                               str++;
-                               p++;
-                               break;
-               }
-       }
-
-       if(!*p && !*str)
-               return true;
-
-       if (!*p && str[0] == '.' && str[1] == 0)
-               return true;
-
-       if (!*str && *p == '?') {
-               while (*p == '?')
-                       p++;
-               return(!*p);
-       }
-
-       if(!*str && (*p == '*' && p[1] == '\0'))
-               return true;
-
-       return false;
-}
-
-/*******************************************************************
- Simple case insensitive interface to a UNIX wildcard matcher.
- Returns True if match, False if not.
-*******************************************************************/
-
-bool unix_wild_match(const char *pattern, const char *string)
-{
-       TALLOC_CTX *ctx = talloc_stackframe();
-       char *p2;
-       char *s2;
-       char *p;
-       bool ret = false;
-
-       p2 = talloc_strdup(ctx,pattern);
-       s2 = talloc_strdup(ctx,string);
-       if (!p2 || !s2) {
-               TALLOC_FREE(ctx);
-               return false;
-       }
-       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++) {
-               while( *p == '*' && (p[1] == '?' ||p[1] == '*')) {
-                       memmove(&p[1], &p[2], strlen(&p[2])+1);
-               }
-       }
-
-       if (strequal(p2,"*")) {
-               TALLOC_FREE(ctx);
-               return true;
-       }
-
-       ret = unix_do_match(p2, s2);
-       TALLOC_FREE(ctx);
-       return ret;
-}
-
 /**********************************************************************
   Converts a name to a fully qualified domain name.
   Returns true if lookup succeeded, false if not (then fqdn is set to name)
@@ -1932,7 +1853,7 @@ int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, i
  call (they take care of winbind separator and other winbind specific settings).
 ****************************************************************/
 
-void split_domain_user(TALLOC_CTX *mem_ctx,
+bool split_domain_user(TALLOC_CTX *mem_ctx,
                       const char *full_name,
                       char **domain,
                       char **user)
@@ -1944,11 +1865,23 @@ void split_domain_user(TALLOC_CTX *mem_ctx,
        if (p != NULL) {
                *domain = talloc_strndup(mem_ctx, full_name,
                                         PTR_DIFF(p, full_name));
+               if (*domain == NULL) {
+                       return false;
+               }
                *user = talloc_strdup(mem_ctx, p+1);
+               if (*user == NULL) {
+                       TALLOC_FREE(*domain);
+                       return false;
+               }
        } else {
-               *domain = talloc_strdup(mem_ctx, "");
+               *domain = NULL;
                *user = talloc_strdup(mem_ctx, full_name);
+               if (*user == NULL) {
+                       return false;
+               }
        }
+
+       return true;
 }
 
 /****************************************************************
@@ -2217,6 +2150,32 @@ struct security_unix_token *copy_unix_token(TALLOC_CTX *ctx, const struct securi
        return cpy;
 }
 
+/****************************************************************************
+ Return a root token
+****************************************************************************/
+
+struct security_unix_token *root_unix_token(TALLOC_CTX *mem_ctx)
+{
+       struct security_unix_token *t = NULL;
+
+       t = talloc_zero(mem_ctx, struct security_unix_token);
+       if (t == NULL) {
+               return NULL;
+       }
+
+       /*
+        * This is not needed, but lets make it explicit, not implicit.
+        */
+       *t = (struct security_unix_token) {
+               .uid = 0,
+               .gid = 0,
+               .ngroups = 0,
+               .groups = NULL
+       };
+
+       return t;
+}
+
 /****************************************************************************
  Check that a file matches a particular file type.
 ****************************************************************************/