*/
#include "includes.h"
+#include "dynconfig.h"
#include "system/network.h"
#include "system/iconv.h"
+#include "system/dir.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.
****************************************************************************/
/*******************************************************************
Check if a file exists - call vfs_file_exist for samba files.
********************************************************************/
-BOOL file_exist(const char *fname, struct stat *sbuf)
+BOOL file_exist(const char *fname)
{
struct stat st;
- if (!sbuf)
- sbuf = &st;
-
- if (stat(fname,sbuf) != 0)
- return(False);
- return((S_ISREG(sbuf->st_mode)) || (S_ISFIFO(sbuf->st_mode)));
+ if (stat(fname, &st) != 0) {
+ return False;
+ }
+
+ return ((S_ISREG(st.st_mode)) || (S_ISFIFO(st.st_mode)));
}
/*******************************************************************
Check if a directory exists.
********************************************************************/
-BOOL directory_exist(const char *dname,struct stat *st)
+BOOL directory_exist(const char *dname)
{
- struct stat st2;
+ struct stat st;
BOOL ret;
- if (!st)
- st = &st2;
-
- if (stat(dname,st) != 0)
- return(False);
+ if (stat(dname,&st) != 0) {
+ return False;
+ }
- ret = S_ISDIR(st->st_mode);
+ ret = S_ISDIR(st.st_mode);
if(!ret)
errno = ENOTDIR;
return ret;
}
-/****************************************************************************
- Expand a pointer to be a particular size.
-****************************************************************************/
-
-void *Realloc(void *p,size_t size)
-{
- void *ret=NULL;
-
- if (size == 0) {
- SAFE_FREE(p);
- DEBUG(5,("Realloc asked for 0 bytes\n"));
- return NULL;
- }
-
- if (!p)
- ret = (void *)malloc(size);
- else
- ret = (void *)realloc(p,size);
-
- if (!ret)
- DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size));
-
- return(ret);
-}
-
/****************************************************************************
Free memory, checks for NULL.
Use directly SAFE_FREE()
return hostname;
}
-/****************************************************************************
- Get my own name, including domain.
-****************************************************************************/
-
-BOOL get_myfullname(char *my_name)
-{
- pstring hostname;
-
- *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';
-
- if (my_name)
- fstrcpy(my_name, hostname);
- return True;
-}
-
-/****************************************************************************
- Get my own domain name.
-****************************************************************************/
-
-BOOL get_mydomname(fstring my_domname)
-{
- pstring hostname;
- char *p;
-
- *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';
-
- p = strchr_m(hostname, '.');
-
- if (!p)
- return False;
-
- p++;
-
- if (my_domname)
- fstrcpy(my_domname, p);
-
- return True;
-}
-
/****************************************************************************
Interpret a protocol description string, with a default.
****************************************************************************/
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)) {
DEBUG(3,("sys_gethostbyname: host address is invalid for host %s\n",str));
return 0;
}
- putip((char *)&res,(char *)hp->h_addr);
+ memcpy((char *)&res,(char *)hp->h_addr, 4);
}
if (res == (uint32_t)-1)
BOOL is_zero_ip(struct ipv4_addr ip)
{
- uint32_t a;
- putip((char *)&a,(char *)&ip);
- return(a == 0);
+ return ip.addr == 0;
}
/*******************************************************************
DEBUGADD(level,("%c", isprint(buf[i])?buf[i]:'.'));
}
-void dump_data(int level, const char *buf1,int len)
+void dump_data(int level, const uint8_t *buf,int len)
{
- const uint8_t *buf = (const uint8_t *)buf1;
int i=0;
if (len<=0) return;
dname = talloc_strdup(mem_ctx, lp_lockdir());
trim_string(dname,"","/");
- if (!directory_exist(dname,NULL)) {
+ if (!directory_exist(dname)) {
mkdir(dname,0755);
}
return fname;
}
+/**
+ * @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;
+}
+
/*
return a path in the smbd.tmp directory, where all temporary file
for smbd go. If NULL is passed for name then return the directory
char *fname, *dname;
dname = lock_path(mem_ctx, "smbd.tmp");
- if (!directory_exist(dname,NULL)) {
+ if (!directory_exist(dname)) {
mkdir(dname,0755);
}
/* see if a range of memory is all zero. A NULL pointer is considered
to be all zero */
-BOOL all_zero(const char *ptr, uint_t size)
+BOOL all_zero(const uint8_t *ptr, uint_t size)
{
int i;
if (!ptr) return True;
return True;
}
+/*
+ realloc an array, checking for integer overflow in the array size
+*/
+void *realloc_array(void *ptr, size_t el_size, unsigned count)
+{
+#define MAX_MALLOC_SIZE 0x7fffffff
+ if (count == 0 ||
+ count >= MAX_MALLOC_SIZE/el_size) {
+ return NULL;
+ }
+ if (!ptr) {
+ return malloc(el_size * count);
+ }
+ return realloc(ptr, el_size * count);
+}