1
0
mirror of https://github.com/samba-team/samba.git synced 2025-03-27 22:50:26 +03:00

Use shared util.c.

This commit is contained in:
Jelmer Vernooij 2008-10-17 12:26:46 +02:00
parent 93310970e3
commit 519458a2fa
7 changed files with 196 additions and 448 deletions

View File

@ -18,6 +18,7 @@ LIBSAMBA-UTIL_OBJ_FILES = $(addprefix $(libutilsrcdir)/, \
util_file.o \
data_blob.o \
util.o \
util_net.o \
fsusage.o \
ms_fnmatch.o \
mutex.o \

View File

@ -25,6 +25,10 @@
#include "system/network.h"
#include "system/filesys.h"
#include "system/locale.h"
#undef malloc
#undef strcasecmp
#undef strdup
#undef realloc
/**
* @file
@ -216,113 +220,11 @@ _PUBLIC_ char *get_myname(void)
return hostname;
}
/**
Return true if a string could be a pure IP address.
**/
_PUBLIC_ bool is_ipaddress(const char *str)
{
bool pure_address = true;
int i;
if (str == NULL) return false;
for (i=0; pure_address && str[i]; i++)
if (!(isdigit((int)str[i]) || str[i] == '.'))
pure_address = false;
/* Check that a pure number is not misinterpreted as an IP */
pure_address = pure_address && (strchr(str, '.') != NULL);
return pure_address;
}
/**
Interpret an internet address or name into an IP address in 4 byte form.
**/
_PUBLIC_ uint32_t interpret_addr(const char *str)
{
struct hostent *hp;
uint32_t res;
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 = inet_addr(str);
} else {
/* otherwise assume it's a network name of some sort and use
sys_gethostbyname */
if ((hp = sys_gethostbyname(str)) == 0) {
DEBUG(3,("sys_gethostbyname: Unknown host. %s\n",str));
return 0;
}
if(hp->h_addr == NULL) {
DEBUG(3,("sys_gethostbyname: host address is invalid for host %s\n",str));
return 0;
}
memcpy((char *)&res,(char *)hp->h_addr, 4);
}
if (res == (uint32_t)-1)
return(0);
return(res);
}
/**
A convenient addition to interpret_addr().
**/
_PUBLIC_ struct in_addr interpret_addr2(const char *str)
{
struct in_addr ret;
uint32_t a = interpret_addr(str);
ret.s_addr = a;
return ret;
}
/**
Check if an IP is the 0.0.0.0.
**/
_PUBLIC_ bool is_zero_ip(struct in_addr ip)
{
return ip.s_addr == 0;
}
/**
Are two IPs on the same subnet?
**/
_PUBLIC_ bool same_net(struct in_addr ip1, struct in_addr ip2, struct in_addr mask)
{
uint32_t net1,net2,nmask;
nmask = ntohl(mask.s_addr);
net1 = ntohl(ip1.s_addr);
net2 = ntohl(ip2.s_addr);
return((net1 & nmask) == (net2 & nmask));
}
/**
Check if a process exists. Does this work on all unixes?
**/
_PUBLIC_ bool process_exists(pid_t pid)
_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. */
@ -381,7 +283,7 @@ _PUBLIC_ bool fcntl_lock(int fd, int op, off_t offset, off_t count, int type)
}
static void print_asc(int level, const uint8_t *buf,int len)
void print_asc(int level, const uint8_t *buf,int len)
{
int i;
for (i=0;i<len;i++)
@ -509,15 +411,64 @@ _PUBLIC_ void *smb_xmemdup(const void *p, size_t size)
strdup that aborts on malloc fail.
**/
_PUBLIC_ char *smb_xstrdup(const char *s)
char *smb_xstrdup(const char *s)
{
#if defined(PARANOID_MALLOC_CHECKER)
#ifdef strdup
#undef strdup
#endif
#endif
#ifndef HAVE_STRDUP
#define strdup rep_strdup
#endif
char *s1 = strdup(s);
if (!s1)
smb_panic("smb_xstrdup: malloc fail\n");
#if defined(PARANOID_MALLOC_CHECKER)
#ifdef strdup
#undef strdup
#endif
#define strdup(s) __ERROR_DONT_USE_STRDUP_DIRECTLY
#endif
if (!s1) {
smb_panic("smb_xstrdup: malloc failed");
}
return s1;
}
/**
strndup that aborts on malloc fail.
**/
char *smb_xstrndup(const char *s, size_t n)
{
#if defined(PARANOID_MALLOC_CHECKER)
#ifdef strndup
#undef strndup
#endif
#endif
#if (defined(BROKEN_STRNDUP) || !defined(HAVE_STRNDUP))
#undef HAVE_STRNDUP
#define strndup rep_strndup
#endif
char *s1 = strndup(s, n);
#if defined(PARANOID_MALLOC_CHECKER)
#ifdef strndup
#undef strndup
#endif
#define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY
#endif
if (!s1) {
smb_panic("smb_xstrndup: malloc failed");
}
return s1;
}
/**
Like strdup but for memory.
**/

128
lib/util/util_net.c Normal file
View File

@ -0,0 +1,128 @@
/*
Unix SMB/CIFS implementation.
Samba utility functions
Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
Copyright (C) Andrew Tridgell 1992-1998
Copyright (C) Jeremy Allison 2001-2002
Copyright (C) Simo Sorce 2001
Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003.
Copyright (C) James J Myers 2003
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
/**
Interpret an internet address or name into an IP address in 4 byte form.
**/
_PUBLIC_ uint32_t interpret_addr(const char *str)
{
struct hostent *hp;
uint32_t res;
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 = inet_addr(str);
} else {
/* otherwise assume it's a network name of some sort and use
sys_gethostbyname */
if ((hp = sys_gethostbyname(str)) == 0) {
DEBUG(3,("sys_gethostbyname: Unknown host. %s\n",str));
return 0;
}
if(hp->h_addr == NULL) {
DEBUG(3,("sys_gethostbyname: host address is invalid for host %s\n",str));
return 0;
}
memcpy((char *)&res,(char *)hp->h_addr, 4);
}
if (res == (uint32_t)-1)
return(0);
return(res);
}
/**
A convenient addition to interpret_addr().
**/
_PUBLIC_ struct in_addr interpret_addr2(const char *str)
{
struct in_addr ret;
uint32_t a = interpret_addr(str);
ret.s_addr = a;
return ret;
}
/**
Check if an IP is the 0.0.0.0.
**/
_PUBLIC_ bool is_zero_ip(struct in_addr ip)
{
return ip.s_addr == 0;
}
/**
Are two IPs on the same subnet?
**/
_PUBLIC_ bool same_net(struct in_addr ip1, struct in_addr ip2, struct in_addr mask)
{
uint32_t net1,net2,nmask;
nmask = ntohl(mask.s_addr);
net1 = ntohl(ip1.s_addr);
net2 = ntohl(ip2.s_addr);
return((net1 & nmask) == (net2 & nmask));
}
/**
Return true if a string could be a pure IP address.
**/
_PUBLIC_ bool is_ipaddress(const char *str)
{
bool pure_address = true;
int i;
if (str == NULL) return false;
for (i=0; pure_address && str[i]; i++)
if (!(isdigit((int)str[i]) || str[i] == '.'))
pure_address = false;
/* Check that a pure number is not misinterpreted as an IP */
pure_address = pure_address && (strchr(str, '.') != NULL);
return pure_address;
}

View File

@ -333,7 +333,7 @@ LIB_OBJ = $(LIBSAMBAUTIL_OBJ) \
../lib/util/xfile.o ../lib/util/util_strlist.o lib/wins_srv.o \
lib/util_str.o lib/clobber.o lib/util_sid.o lib/util_uuid.o \
lib/util_unistr.o ../lib/util/util_file.o lib/util_file.o ../lib/util/data_blob.o \
lib/util.o lib/util_sock.o lib/sock_exec.o lib/util_sec.o \
lib/util.o ../lib/util/util.o lib/util_sock.o lib/sock_exec.o lib/util_sec.o \
lib/substitute.o ../lib/util/fsusage.o lib/dbwrap_util.o \
lib/ms_fnmatch.o lib/select.o lib/errmap_unix.o \
lib/tallocmsg.o lib/dmallocmsg.o libsmb/smb_signing.o \

View File

@ -1232,10 +1232,10 @@ const char *tmpdir(void);
bool add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
gid_t **gids, size_t *num_gids);
const char *get_numlist(const char *p, uint32 **num, int *count);
bool file_exist(const char *fname,SMB_STRUCT_STAT *sbuf);
bool file_exist_stat(const char *fname,SMB_STRUCT_STAT *sbuf);
bool socket_exist(const char *fname);
time_t file_modtime(const char *fname);
bool directory_exist(char *dname,SMB_STRUCT_STAT *st);
bool directory_exist_stat(char *dname,SMB_STRUCT_STAT *st);
SMB_OFF_T get_file_size(char *file_name);
char *attrib_string(uint16 mode);
void show_msg(char *buf);
@ -1263,7 +1263,7 @@ void add_to_large_array(TALLOC_CTX *mem_ctx, size_t element_size,
void *element, void *_array, uint32 *num_elements,
ssize_t *array_size);
void safe_free(void *p);
char *get_myname(TALLOC_CTX *ctx);
char *get_myname_talloc(TALLOC_CTX *ctx);
char *get_mydnsdomname(TALLOC_CTX *ctx);
int interpret_protocol(const char *str,int def);
char *automount_lookup(TALLOC_CTX *ctx, const char *user_name);

View File

@ -439,19 +439,6 @@ bool set_cmdline_auth_info_machine_account_creds(void)
return true;
}
/**************************************************************************n
Find a suitable temporary directory. The result should be copied immediately
as it may be overwritten by a subsequent call.
****************************************************************************/
const char *tmpdir(void)
{
char *p;
if ((p = getenv("TMPDIR")))
return p;
return "/tmp";
}
/****************************************************************************
Add a gid to an array of gids if it's not already there.
****************************************************************************/
@ -540,7 +527,7 @@ const char *get_numlist(const char *p, uint32 **num, int *count)
Check if a file exists - call vfs_file_exist for samba files.
********************************************************************/
bool file_exist(const char *fname,SMB_STRUCT_STAT *sbuf)
bool file_exist_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
{
SMB_STRUCT_STAT st;
if (!sbuf)
@ -565,25 +552,11 @@ bool socket_exist(const char *fname)
return S_ISSOCK(st.st_mode);
}
/*******************************************************************
Check a files mod time.
********************************************************************/
time_t file_modtime(const char *fname)
{
SMB_STRUCT_STAT st;
if (sys_stat(fname,&st) != 0)
return(0);
return(st.st_mtime);
}
/*******************************************************************
Check if a directory exists.
********************************************************************/
bool directory_exist(char *dname,SMB_STRUCT_STAT *st)
bool directory_exist_stat(char *dname,SMB_STRUCT_STAT *st)
{
SMB_STRUCT_STAT st2;
bool ret;
@ -927,36 +900,6 @@ ssize_t write_data_at_offset(int fd, const char *buffer, size_t N, SMB_OFF_T pos
#endif
}
/****************************************************************************
Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
else
if SYSV use O_NDELAY
if BSD use FNDELAY
****************************************************************************/
int set_blocking(int fd, bool set)
{
int val;
#ifdef O_NONBLOCK
#define FLAG_TO_SET O_NONBLOCK
#else
#ifdef SYSV
#define FLAG_TO_SET O_NDELAY
#else /* BSD */
#define FLAG_TO_SET FNDELAY
#endif
#endif
if((val = sys_fcntl_long(fd, F_GETFL, 0)) == -1)
return -1;
if(set) /* Turn blocking on - ie. clear nonblock flag */
val &= ~FLAG_TO_SET;
else
val |= FLAG_TO_SET;
return sys_fcntl_long( fd, F_SETFL, val);
#undef FLAG_TO_SET
}
/*******************************************************************
Sleep for a specified number of milliseconds.
********************************************************************/
@ -1334,7 +1277,7 @@ void safe_free(void *p)
Get my own name and IP.
****************************************************************************/
char *get_myname(TALLOC_CTX *ctx)
char *talloc_get_myname(TALLOC_CTX *ctx)
{
char *p;
char hostname[HOST_NAME_MAX];
@ -1549,14 +1492,6 @@ bool process_exists(const struct server_id pid)
#endif
}
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);
return(kill(pid,0) == 0 || errno != ESRCH);
}
/*******************************************************************
Convert a uid into a user name.
********************************************************************/
@ -2002,42 +1937,6 @@ void free_namearray(name_compare_entry *name_array)
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_LOCKING
/****************************************************************************
Simple routine to do POSIX file locking. Cruft in NFS and 64->32 bit mapping
is dealt with in posix.c
Returns True if the lock was granted, False otherwise.
****************************************************************************/
bool fcntl_lock(int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
{
SMB_STRUCT_FLOCK lock;
int ret;
DEBUG(8,("fcntl_lock fd=%d op=%d offset=%.0f count=%.0f type=%d\n",
fd,op,(double)offset,(double)count,type));
lock.l_type = type;
lock.l_whence = SEEK_SET;
lock.l_start = offset;
lock.l_len = count;
lock.l_pid = 0;
ret = sys_fcntl_ptr(fd,op,&lock);
if (ret == -1) {
int sav = errno;
DEBUG(3,("fcntl_lock: lock failed at offset %.0f count %.0f op %d type %d (%s)\n",
(double)offset,(double)count,op,type,strerror(errno)));
errno = sav;
return False;
}
/* everything went OK */
DEBUG(8,("fcntl_lock: Lock call successful\n"));
return True;
}
/****************************************************************************
Simple routine to query existing file locks. Cruft in NFS and 64->32 bit mapping
is dealt with in posix.c
@ -2210,116 +2109,6 @@ enum remote_arch_types get_remote_arch(void)
return ra_type;
}
void print_asc(int level, const unsigned char *buf,int len)
{
int i;
for (i=0;i<len;i++)
DEBUG(level,("%c", isprint(buf[i])?buf[i]:'.'));
}
/**
* Write dump of binary data to the log file.
*
* The data is only written if the log level is at least level.
*/
static void _dump_data(int level, const uint8_t *buf, int len,
bool omit_zero_bytes)
{
int i=0;
const uint8_t empty[16];
bool skipped = false;
if (len<=0) return;
if (!DEBUGLVL(level)) return;
memset(&empty, '\0', 16);
for (i=0;i<len;) {
if (i%16 == 0) {
if ((omit_zero_bytes == true) &&
(i > 0) &&
(len > i+16) &&
(memcmp(&buf[i], &empty, 16) == 0))
{
i +=16;
continue;
}
if (i<len) {
DEBUGADD(level,("[%04X] ",i));
}
}
DEBUGADD(level,("%02X ",(int)buf[i]));
i++;
if (i%8 == 0) DEBUGADD(level,(" "));
if (i%16 == 0) {
print_asc(level,&buf[i-16],8); DEBUGADD(level,(" "));
print_asc(level,&buf[i-8],8); DEBUGADD(level,("\n"));
if ((omit_zero_bytes == true) &&
(len > i+16) &&
(memcmp(&buf[i], &empty, 16) == 0)) {
if (!skipped) {
DEBUGADD(level,("skipping zero buffer bytes\n"));
skipped = true;
}
}
}
}
if (i%16) {
int n;
n = 16 - (i%16);
DEBUGADD(level,(" "));
if (n>8) DEBUGADD(level,(" "));
while (n--) DEBUGADD(level,(" "));
n = MIN(8,i%16);
print_asc(level,&buf[i-(i%16)],n); DEBUGADD(level,( " " ));
n = (i%16) - n;
if (n>0) print_asc(level,&buf[i-n],n);
DEBUGADD(level,("\n"));
}
}
/**
* Write dump of binary data to the log file.
*
* The data is only written if the log level is at least level.
*/
_PUBLIC_ void dump_data(int level, const uint8_t *buf, int len)
{
_dump_data(level, buf, len, false);
}
/**
* Write dump of binary data to the log file.
*
* The data is only written if the log level is at least level.
* 16 zero bytes in a row are ommited
*/
_PUBLIC_ void dump_data_skip_zeros(int level, const uint8_t *buf, int len)
{
_dump_data(level, buf, len, true);
}
void dump_data_pw(const char *msg, const uchar * data, size_t len)
{
#ifdef DEBUG_PASSWORD
DEBUG(11, ("%s", msg));
if (data != NULL && len > 0)
{
dump_data(11, data, len);
}
#endif
}
const char *tab_depth(int level, int depth)
{
if( CHECK_DEBUGLVL(level) ) {
@ -2484,78 +2273,6 @@ void *smb_xmalloc_array(size_t size, unsigned int count)
return p;
}
/**
Memdup with smb_panic on fail.
**/
void *smb_xmemdup(const void *p, size_t size)
{
void *p2;
p2 = SMB_XMALLOC_ARRAY(unsigned char,size);
memcpy(p2, p, size);
return p2;
}
/**
strdup that aborts on malloc fail.
**/
char *smb_xstrdup(const char *s)
{
#if defined(PARANOID_MALLOC_CHECKER)
#ifdef strdup
#undef strdup
#endif
#endif
#ifndef HAVE_STRDUP
#define strdup rep_strdup
#endif
char *s1 = strdup(s);
#if defined(PARANOID_MALLOC_CHECKER)
#ifdef strdup
#undef strdup
#endif
#define strdup(s) __ERROR_DONT_USE_STRDUP_DIRECTLY
#endif
if (!s1) {
smb_panic("smb_xstrdup: malloc failed");
}
return s1;
}
/**
strndup that aborts on malloc fail.
**/
char *smb_xstrndup(const char *s, size_t n)
{
#if defined(PARANOID_MALLOC_CHECKER)
#ifdef strndup
#undef strndup
#endif
#endif
#if (defined(BROKEN_STRNDUP) || !defined(HAVE_STRNDUP))
#undef HAVE_STRNDUP
#define strndup rep_strndup
#endif
char *s1 = strndup(s, n);
#if defined(PARANOID_MALLOC_CHECKER)
#ifdef strndup
#undef strndup
#endif
#define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY
#endif
if (!s1) {
smb_panic("smb_xstrndup: malloc failed");
}
return s1;
}
/*
vasprintf that aborts on malloc fail
*/
@ -2575,22 +2292,6 @@ char *smb_xstrndup(const char *s, size_t n)
return n;
}
/*****************************************************************
Like strdup but for memory.
*****************************************************************/
void *memdup(const void *p, size_t size)
{
void *p2;
if (size == 0)
return NULL;
p2 = SMB_MALLOC(size);
if (!p2)
return NULL;
memcpy(p2, p, size);
return p2;
}
/*****************************************************************
Get local hostname and cache result.
*****************************************************************/
@ -2601,7 +2302,7 @@ char *myhostname(void)
if (ret == NULL) {
/* This is cached forever so
* use NULL talloc ctx. */
ret = get_myname(NULL);
ret = talloc_get_myname(NULL);
}
return ret;
}
@ -2620,7 +2321,7 @@ static char *xx_path(const char *name, const char *rootpath)
}
trim_string(fname,"","/");
if (!directory_exist(fname,NULL)) {
if (!directory_exist_stat(fname,NULL)) {
mkdir(fname,0755);
}
@ -3030,25 +2731,6 @@ bool name_to_fqdn(fstring fqdn, const char *name)
return true;
}
/**********************************************************************
Extension to talloc_get_type: Abort on type mismatch
***********************************************************************/
void *talloc_check_name_abort(const void *ptr, const char *name)
{
void *result;
result = talloc_check_name(ptr, name);
if (result != NULL)
return result;
DEBUG(0, ("Talloc type mismatch, expected %s, got %s\n",
name, talloc_get_name(ptr)));
smb_panic("talloc type mismatch");
/* Keep the compiler happy */
return NULL;
}
/**********************************************************************
Append a DATA_BLOB to a talloc'ed object
***********************************************************************/

View File

@ -155,20 +155,6 @@ void ndr_print_server_id(struct ndr_print *ndr, const char *name, const struct s
ndr->depth--;
}
/**
* see if a range of memory is all zero. A NULL pointer is considered
* to be all zero
*/
bool all_zero(const uint8_t *ptr, size_t size)
{
int i;
if (!ptr) return True;
for (i=0;i<size;i++) {
if (ptr[i]) return False;
}
return True;
}
_PUBLIC_ void ndr_print_bool(struct ndr_print *ndr, const char *name, const bool b)
{
ndr->print(ndr, "%-25s: %s", name, b?"true":"false");