r13655: Use new name of build header
[bbaumbach/samba-autobuild/.git] / source4 / lib / util.c
index 59d83a966a651650c726bb5766f628a3f7263dd4..17dde332e1a83d9f7f4a04926a4fc03ee6f79f20 100644 (file)
@@ -28,7 +28,7 @@
 #include "system/iconv.h"
 #include "system/filesys.h"
 
-/**************************************************************************n
+/***************************************************************************
  Find a suitable temporary directory. The result should be copied immediately
  as it may be overwritten by a subsequent call.
 ****************************************************************************/
@@ -88,22 +88,53 @@ BOOL directory_exist(const char *dname)
        return ret;
 }
 
-/*******************************************************************
- Returns the size in bytes of the named file.
-********************************************************************/
-off_t get_file_size(char *file_name)
-{
-       struct stat buf;
-       buf.st_size = 0;
-       if(stat(file_name,&buf) != 0)
-               return (off_t)-1;
-       return(buf.st_size);
-}
+BOOL directory_create_or_exist(const char *dname, uid_t uid, 
+                              mode_t dir_perms)
+{
+       mode_t old_umask;
+       struct stat st;
+      
+       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) || 
+                   ((st.st_mode & 0777) != dir_perms)) {
+                       DEBUG(0, ("invalid permissions on directory "
+                                 "%s\n", dname));
+                       umask(old_umask);
+                       return False;
+               }
+       }
+       return True;
+}       
+
 
 /*******************************************************************
  Close the low 3 fd's and open dev/null in their place.
 ********************************************************************/
-void close_low_fds(BOOL stderr_too)
+static void close_low_fds(BOOL stderr_too)
 {
 #ifndef VALGRIND
        int fd;
@@ -280,30 +311,6 @@ char* get_myname(void)
        return hostname;
 }
 
-/****************************************************************************
- Interpret a protocol description string, with a default.
-****************************************************************************/
-
-int interpret_protocol(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);
-}
-
 /****************************************************************************
  Return true if a string could be a pure IP address.
 ****************************************************************************/
@@ -331,17 +338,22 @@ uint32_t interpret_addr(const char *str)
        struct hostent *hp;
        uint32_t res;
 
-       if (str == NULL || 
+       if (str == NULL || *str == 0 ||
            strcmp(str,"0.0.0.0") == 0) {
                return 0;
        }
        if (strcmp(str,"255.255.255.255") == 0) {
                return 0xFFFFFFFF;
        }
+       /* recognise 'localhost' as a special name. This fixes problems with
+          some hosts that don't have localhost in /etc/hosts */
+       if (strcasecmp(str,"localhost") == 0) {
+               str = "127.0.0.1";
+       }
 
        /* if it's in the form of an IP address then get the lib to interpret it */
        if (is_ipaddress(str)) {
-               res = sys_inet_addr(str);
+               res = inet_addr(str);
        } else {
                /* otherwise assume it's a network name of some sort and use 
                        sys_gethostbyname */
@@ -383,17 +395,6 @@ BOOL is_zero_ip(struct ipv4_addr ip)
        return ip.addr == 0;
 }
 
-/*******************************************************************
- Set an IP to 0.0.0.0.
-******************************************************************/
-
-void zero_ip(struct ipv4_addr *ip)
-{
-       *ip = sys_inet_makeaddr(0,0);
-       return;
-}
-
-
 /*******************************************************************
  Are two IPs on the same subnet?
 ********************************************************************/
@@ -512,24 +513,6 @@ void dump_data(int level, const uint8_t *buf,int len)
        }       
 }
 
-/*****************************************************************
- Possibly replace mkstemp if it is broken.
-*****************************************************************/  
-
-int smb_mkstemp(char *template)
-{
-#if HAVE_SECURE_MKSTEMP
-       return mkstemp(template);
-#else
-       /* have a reasonable go at emulating it. Hope that
-          the system mktemp() isn't completly hopeless */
-       char *p = mktemp(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.
  *****************************************************************/  
@@ -586,44 +569,41 @@ void *memdup(const void *p, size_t size)
 }
 
 /*****************************************************************
Get local hostname and cache result.
A useful function for returning a path in the Samba lock directory.
 *****************************************************************/  
-
-char *myhostname(TALLOC_CTX *mem_ctx)
+char *lock_path(TALLOC_CTX* mem_ctx, const char *name)
 {
-       char *myname, *ret;
-       myname = get_myname();
-       ret = talloc_strdup(mem_ctx, myname);
-       free(myname);
-       return ret;
+       char *fname, *dname;
+       if (name == NULL) {
+               return NULL;
+       }
+       if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
+               return talloc_strdup(mem_ctx, name);
+       }
 
-}
+       dname = talloc_strdup(mem_ctx, lp_lockdir());
+       trim_string(dname,"","/");
+       
+       if (!directory_exist(dname)) {
+               mkdir(dname,0755);
+       }
+       
+       fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
 
-/**********************************************************************
- Converts a name to a fully qalified domain name.
-***********************************************************************/
+       talloc_free(dname);
 
-char *name_to_fqdn(TALLOC_CTX *mem_ctx, const char *name)
-{
-       struct hostent *hp = sys_gethostbyname(name);
-       if ( hp && hp->h_name && *hp->h_name ) {
-               DEBUG(10,("name_to_fqdn: lookup for %s -> %s.\n", name, hp->h_name));
-               return talloc_strdup(mem_ctx, hp->h_name);
-       } else {
-               DEBUG(10,("name_to_fqdn: lookup for %s failed.\n", name));
-               return talloc_strdup(mem_ctx, name);
-       }
+       return fname;
 }
 
 
 /*****************************************************************
- A useful function for returning a path in the Samba lock directory.
+ A useful function for returning a path in the Samba piddir directory.
 *****************************************************************/  
-char *lock_path(TALLOC_CTX* mem_ctx, const char *name)
+static char *pid_path(TALLOC_CTX* mem_ctx, const char *name)
 {
        char *fname, *dname;
 
-       dname = talloc_strdup(mem_ctx, lp_lockdir());
+       dname = talloc_strdup(mem_ctx, lp_piddir());
        trim_string(dname,"","/");
        
        if (!directory_exist(dname)) {
@@ -637,6 +617,7 @@ char *lock_path(TALLOC_CTX* mem_ctx, const char *name)
        return fname;
 }
 
+
 /**
  * @brief Returns an absolute path to a file in the Samba lib directory.
  *
@@ -656,13 +637,19 @@ char *lib_path(TALLOC_CTX* mem_ctx, const char *name)
  * @brief Returns an absolute path to a file in the Samba private directory.
  *
  * @param name File to find, relative to PRIVATEDIR.
+ * if name is not relative, then use it as-is
  *
  * @retval Pointer to a talloc'ed string containing the full path.
  **/
-
 char *private_path(TALLOC_CTX* mem_ctx, const char *name)
 {
        char *fname;
+       if (name == NULL) {
+               return NULL;
+       }
+       if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
+               return talloc_strdup(mem_ctx, name);
+       }
        fname = talloc_asprintf(mem_ctx, "%s/%s", lp_private_dir(), name);
        return fname;
 }
@@ -676,7 +663,7 @@ char *smbd_tmp_path(TALLOC_CTX *mem_ctx, const char *name)
 {
        char *fname, *dname;
 
-       dname = lock_path(mem_ctx, "smbd.tmp");
+       dname = pid_path(mem_ctx, "smbd.tmp");
        if (!directory_exist(dname)) {
                mkdir(dname,0755);
        }
@@ -691,17 +678,22 @@ char *smbd_tmp_path(TALLOC_CTX *mem_ctx, const char *name)
        return fname;
 }
 
-/**
- * @brief Returns the platform specific shared library extension.
- *
- * @retval Pointer to a static #fstring containing the extension.
- **/
-
-const char *shlib_ext(void)
+static char *modules_path(TALLOC_CTX* mem_ctx, const char *name)
 {
-  return dyn_SHLIBEXT;
+       return talloc_asprintf(mem_ctx, "%s/%s", dyn_MODULESDIR, name);
 }
 
+init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem)
+{
+       char *path = modules_path(mem_ctx, subsystem);
+       init_module_fn *ret;
+
+       ret = load_modules(mem_ctx, path);
+
+       talloc_free(path);
+
+       return ret;
+}
 
 void dump_data_pw(const char *msg, const uint8_t * data, size_t len)
 {