s3-lib: Do not set an empty string in split_domain_user()
[vlendec/samba-autobuild/.git] / source3 / lib / util.c
index 320fdc2e0e49b2916564fae315edaea760b41ea0..bab399824bf811f34fba93ac75c2f6d71e5d0068 100644 (file)
@@ -1150,13 +1150,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 +1164,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 +1184,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 +1261,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_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 remote_arch_str;
+       return RA_UNKNOWN;
 }
 
 /*******************************************************************
@@ -1246,52 +1309,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 +1334,146 @@ 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(time_t timeout, DATA_BLOB blob, void *priv_data)
+{
+       struct ra_parser_state *state = (struct ra_parser_state *)priv_data;
+       const char *ra_str = NULL;
+
+       if (timeout <= time(NULL)) {
+               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) ) {
@@ -1496,71 +1667,6 @@ char *myhostname_upper(void)
        return ret;
 }
 
-/**
- * @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)
-{
-       char *fname = NULL;
-
-       fname = talloc_strdup(talloc_tos(), rootpath);
-       if (!fname) {
-               return NULL;
-       }
-       trim_string(fname,"","/");
-
-       if (!directory_create_or_exist(fname, 0755)) {
-               return NULL;
-       }
-
-       return talloc_asprintf_append(fname, "/%s", name);
-}
-
-/**
- * @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_lock_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, lp_state_directory());
-}
-
-/**
- * @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_cache_directory());
-}
-
 /*******************************************************************
  Given a filename - get its directory name
 ********************************************************************/
@@ -1883,32 +1989,6 @@ bool name_to_fqdn(fstring fqdn, const char *name)
        return true;
 }
 
-/**********************************************************************
- Append a DATA_BLOB to a talloc'ed object
-***********************************************************************/
-
-void *talloc_append_blob(TALLOC_CTX *mem_ctx, void *buf, DATA_BLOB blob)
-{
-       size_t old_size = 0;
-       char *result;
-
-       if (blob.length == 0) {
-               return buf;
-       }
-
-       if (buf != NULL) {
-               old_size = talloc_get_size(buf);
-       }
-
-       result = (char *)TALLOC_REALLOC(mem_ctx, buf, old_size + blob.length);
-       if (result == NULL) {
-               return NULL;
-       }
-
-       memcpy(result + old_size, blob.data, blob.length);
-       return result;
-}
-
 uint32_t map_share_mode_to_deny_mode(uint32_t share_access, uint32_t private_options)
 {
        switch (share_access & ~FILE_SHARE_DELETE) {
@@ -2023,7 +2103,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)
@@ -2035,11 +2115,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;
 }
 
 /****************************************************************