diff options
Diffstat (limited to 'source/lib/util.c')
-rw-r--r-- | source/lib/util.c | 338 |
1 files changed, 133 insertions, 205 deletions
diff --git a/source/lib/util.c b/source/lib/util.c index ea1670ea276..921d683f4fb 100644 --- a/source/lib/util.c +++ b/source/lib/util.c @@ -1,5 +1,6 @@ /* - Unix SMB/CIFS implementation. + Unix SMB/Netbios implementation. + Version 1.9. Samba utility functions Copyright (C) Andrew Tridgell 1992-1998 Copyright (C) Jeremy Allison 2001 @@ -45,10 +46,6 @@ #include <rpcsvc/nis.h> -#else /* !WITH_NISPLUS_HOME */ - -#include "rpcsvc/ypclnt.h" - #endif /* WITH_NISPLUS_HOME */ #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */ @@ -91,15 +88,16 @@ char **my_netbios_names; /**************************************************************************** - find a suitable temporary directory. The result should be copied immediately + Find a suitable temporary directory. The result should be copied immediately as it may be overwritten by a subsequent call. - ****************************************************************************/ +****************************************************************************/ + char *tmpdir(void) { - char *p; + char *p; if ((p = getenv("TMPDIR"))) - return p; - return "/tmp"; + return p; + return "/tmp"; } /**************************************************************************** @@ -121,7 +119,7 @@ BOOL in_group(gid_t group, gid_t current_gid, int ngroups, gid_t *groups) } /**************************************************************************** - Like atoi but gets the value up to the separator character. + Like atoi but gets the value up to the separater character. ****************************************************************************/ char *Atoic(char *p, int *n, char *c) @@ -136,8 +134,7 @@ char *Atoic(char *p, int *n, char *c) while ((*p) && isdigit((int)*p)) p++; - if (strchr_m(c, *p) == NULL) - { + if (strchr(c, *p) == NULL) { DEBUG(5, ("Atoic: no separator characters (%s) not found\n", c)); return NULL; } @@ -161,7 +158,7 @@ char *get_numlist(char *p, uint32 **num, int *count) while ((p = Atoic(p, &val, ":,")) != NULL && (*p) != ':') { uint32 *tn; - + tn = Realloc((*num), ((*count)+1) * sizeof(uint32)); if (tn == NULL) { @@ -181,30 +178,30 @@ char *get_numlist(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(char *fname,SMB_STRUCT_STAT *sbuf) { - SMB_STRUCT_STAT st; + SMB_STRUCT_STAT st; if (!sbuf) sbuf = &st; - if (sys_stat(fname,sbuf) != 0) - return(False); + if (sys_stat(fname,sbuf) != 0) + return(False); - return(S_ISREG(sbuf->st_mode)); + return(S_ISREG(sbuf->st_mode)); } /******************************************************************* Check a files mod time. ********************************************************************/ -time_t file_modtime(const char *fname) +time_t file_modtime(char *fname) { - SMB_STRUCT_STAT st; + SMB_STRUCT_STAT st; - if (sys_stat(fname,&st) != 0) - return(0); + if (sys_stat(fname,&st) != 0) + return(0); - return(st.st_mtime); + return(st.st_mtime); } /******************************************************************* @@ -368,7 +365,7 @@ void dos_clean_name(char *s) *p = 0; pstrcpy(s1,p+3); - if ((p=strrchr_m(s,'\\')) != NULL) + if ((p=strrchr(s,'\\')) != NULL) *p = 0; else *s = 0; @@ -406,7 +403,7 @@ void unix_clean_name(char *s) *p = 0; pstrcpy(s1,p+3); - if ((p=strrchr_m(s,'/')) != NULL) + if ((p=strrchr(s,'/')) != NULL) *p = 0; else *s = 0; @@ -416,60 +413,6 @@ void unix_clean_name(char *s) trim_string(s,NULL,"/.."); } -/******************************************************************* -convert '\' to '/' -reduce a file name, removing or reducing /../ , /./ , // elements. -remove also any trailing . and / -return a new allocated string. -********************************************************************/ -smb_ucs2_t *unix_clean_path(const smb_ucs2_t *s) -{ - smb_ucs2_t *ns; - smb_ucs2_t *p, *r, *t; - - DEBUG(3, ("unix_clean_path\n")); /* [%unicode]\n")); */ - if(!s) return NULL; - - /* convert '\' to '/' */ - ns = strdup_w(s); - if (!ns) return NULL; - unix_format_w(ns); - - /* remove all double slashes */ - p = ns; - ns = all_string_sub_wa(p, "//", "/"); - SAFE_FREE(p); - if (!ns) return NULL; - - /* remove any /./ */ - p = ns; - ns = all_string_sub_wa(p, "/./", "/"); - SAFE_FREE(p); - if (!ns) return NULL; - - /* reduce any /../ */ - t = ns; - while (*t && (r = strstr_wa(t, "/.."))) { - t = &(r[3]); - if (*t == UCS2_CHAR('/') || *t == 0) { - *r = 0; - p = strrchr_w(ns, UCS2_CHAR('/')); - if (!p) p = ns; - if (*t == 0) *p = 0; - else memmove(p, t, (strlen_w(t) + 1) * sizeof(smb_ucs2_t)); - t = p; - } - } - - /* remove any leading ./ trailing /. */ - trim_string_wa(ns, "./", "/."); - - /* remove any leading and trailing / */ - trim_string_wa(ns, "/", "/"); - - return ns; -} - /**************************************************************************** make a dir struct ****************************************************************************/ @@ -484,25 +427,25 @@ void make_dir_struct(char *buf,char *mask,char *fname,SMB_OFF_T size,int mode,ti size = 0; memset(buf+1,' ',11); - if ((p = strchr_m(mask2,'.')) != NULL) + if ((p = strchr(mask2,'.')) != NULL) { *p = 0; - push_ascii(buf+1,mask2,8, 0); - push_ascii(buf+9,p+1,3, 0); + memcpy(buf+1,mask2,MIN(strlen(mask2),8)); + memcpy(buf+9,p+1,MIN(strlen(p+1),3)); *p = '.'; } else - push_ascii(buf+1,mask2,11, 0); + memcpy(buf+1,mask2,MIN(strlen(mask2),11)); memset(buf+21,'\0',DIR_STRUCT_SIZE-21); SCVAL(buf,21,mode); put_dos_date(buf,22,date); SSVAL(buf,26,size & 0xFFFF); SSVAL(buf,28,(size >> 16)&0xFFFF); - push_ascii(buf+30,fname,12, 0); + StrnCpy(buf+30,fname,12); if (!case_sensitive) strupper(buf+30); - DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf+30, fname)); + DEBUG(8,("put name [%s] into dir struct\n",buf+30)); } @@ -687,22 +630,22 @@ void become_daemon(void) close_low_fds(); } - /**************************************************************************** -put up a yes/no prompt + Put up a yes/no prompt ****************************************************************************/ + BOOL yesno(char *p) { - pstring ans; - printf("%s",p); + pstring ans; + printf("%s",p); - if (!fgets(ans,sizeof(ans)-1,stdin)) - return(False); + if (!fgets(ans,sizeof(ans)-1,stdin)) + return(False); - if (*ans == 'y' || *ans == 'Y') - return(True); + if (*ans == 'y' || *ans == 'Y') + return(True); - return(False); + return(False); } /**************************************************************************** @@ -711,23 +654,23 @@ BOOL yesno(char *p) void *Realloc(void *p,size_t size) { - void *ret=NULL; + void *ret=NULL; - if (size == 0) { - SAFE_FREE(p); - DEBUG(5,("Realloc asked for 0 bytes\n")); - return 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 (!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)); + if (!ret) + DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size)); - return(ret); + return(ret); } /**************************************************************************** @@ -762,8 +705,7 @@ BOOL get_myname(char *my_name) if (my_name) { /* split off any parts after an initial . */ - char *p = strchr_m(hostname,'.'); - + char *p = strchr(hostname,'.'); if (p) *p = 0; @@ -811,7 +753,7 @@ BOOL is_ipaddress(const char *str) pure_address = False; /* Check that a pure number is not misinterpreted as an IP */ - pure_address = pure_address && (strchr_m(str, '.') != NULL); + pure_address = pure_address && (strchr(str, '.') != NULL); return pure_address; } @@ -862,28 +804,31 @@ struct in_addr *interpret_addr2(const char *str) } /******************************************************************* - check if an IP is the 0.0.0.0 - ******************************************************************/ + Check if an IP is the 0.0.0.0 + ******************************************************************/ + BOOL is_zero_ip(struct in_addr ip) { - uint32 a; - putip((char *)&a,(char *)&ip); - return(a == 0); + uint32 a; + putip((char *)&a,(char *)&ip); + return(a == 0); } -/* Set an IP to 0.0.0.0 */ +/******************************************************************* + Set an IP to 0.0.0.0 + ******************************************************************/ void zero_ip(struct in_addr *ip) { - static BOOL init; - static struct in_addr ipzero; + static BOOL init; + static struct in_addr ipzero; - if (!init) { - ipzero = *interpret_addr2("0.0.0.0"); - init = True; - } + if (!init) { + ipzero = *interpret_addr2("0.0.0.0"); + init = True; + } - *ip = ipzero; + *ip = ipzero; } #if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT)) @@ -918,7 +863,7 @@ static void strip_mount_options( pstring *str) *******************************************************************/ #ifdef WITH_NISPLUS_HOME -char *automount_lookup(const char *user_name) +char *automount_lookup(char *user_name) { static fstring last_key = ""; static pstring last_value = ""; @@ -966,7 +911,7 @@ char *automount_lookup(const char *user_name) return last_value; } #else /* WITH_NISPLUS_HOME */ -char *automount_lookup(const char *user_name) +char *automount_lookup(char *user_name) { static fstring last_key = ""; static pstring last_value = ""; @@ -1045,9 +990,6 @@ check if a process exists. Does this work on all unixes? BOOL process_exists(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); } @@ -1061,8 +1003,12 @@ char *uidtoname(uid_t uid) static fstring name; struct passwd *pass; + if (winbind_uidtoname(name, uid)) + return name; + pass = sys_getpwuid(uid); - if (pass) return(pass->pw_name); + if (pass) + return(pass->pw_name); slprintf(name, sizeof(name) - 1, "%d",(int)uid); return(name); } @@ -1077,8 +1023,12 @@ char *gidtoname(gid_t gid) static fstring name; struct group *grp; + if (winbind_gidtoname(name, gid)) + return name; + grp = getgrgid(gid); - if (grp) return(grp->gr_name); + if (grp) + return(grp->gr_name); slprintf(name,sizeof(name) - 1, "%d",(int)gid); return(name); } @@ -1097,11 +1047,12 @@ uid_t nametouid(char *name) if ((p != name) && (*p == '\0')) return u; - pass = getpwnam_alloc(name); - if (pass) { + if (winbind_nametouid(&u, name)) + return u; + + pass = sys_getpwnam(name); + if (pass) return(pass->pw_uid); - passwd_free(&pass); - } return (uid_t)-1; } @@ -1110,7 +1061,7 @@ uid_t nametouid(char *name) is present does a shortcut lookup... ********************************************************************/ -gid_t nametogid(const char *name) +gid_t nametogid(char *name) { struct group *grp; char *p; @@ -1120,6 +1071,9 @@ gid_t nametogid(const char *name) if ((p != name) && (*p == '\0')) return g; + if (winbind_nametogid(&g, name)) + return g; + grp = getgrnam(name); if (grp) return(grp->gr_gid); @@ -1196,7 +1150,7 @@ BOOL is_in_path(char *name, name_compare_entry *namelist) } /* Get the last component of the unix name. */ - p = strrchr_m(name, '/'); + p = strrchr(name, '/'); strncpy(last_component, p ? ++p : name, sizeof(last_component)-1); last_component[sizeof(last_component)-1] = '\0'; @@ -1263,7 +1217,7 @@ void set_namearray(name_compare_entry **ppname_array, char *namelist) continue; } /* find the next / */ - name_end = strchr_m(nameptr, '/'); + name_end = strchr(nameptr, '/'); /* oops - the last check for a / didn't find one. */ if (name_end == NULL) @@ -1296,7 +1250,7 @@ void set_namearray(name_compare_entry **ppname_array, char *namelist) continue; } /* find the next / */ - if ((name_end = strchr_m(nameptr, '/')) != NULL) + if ((name_end = strchr(nameptr, '/')) != NULL) { *name_end = 0; } @@ -1405,9 +1359,29 @@ BOOL is_myname(char *s) return(ret); } +/******************************************************************** + Return only the first IP address of our configured interfaces + as a string + *******************************************************************/ + +const char* get_my_primary_ip (void) +{ + static fstring ip_string; + int n; + struct iface_struct nics[MAX_INTERFACES]; + + if ((n=get_interfaces(nics, MAX_INTERFACES)) <= 0) + return NULL; + + fstrcpy(ip_string, inet_ntoa(nics[0].ip)); + return ip_string; +} + + BOOL is_myname_or_ipaddr(char *s) { - char **ptr; + char *ptr; + pstring nbname; /* optimize for the common case */ if (strequal(s, global_myname)) @@ -1430,15 +1404,15 @@ BOOL is_myname_or_ipaddr(char *s) return True; } } - + /* check for an alias */ ptr = lp_netbios_aliases(); - for ( ; *ptr; ptr++ ) + while ( next_token(&ptr, nbname, NULL, sizeof(nbname)) ) { - if (StrCaseCmp(s, *ptr) == 0) + if (StrCaseCmp(s, nbname) == 0) return True; } - + /* no match */ return False; @@ -1539,16 +1513,16 @@ void out_data(FILE *f,char *buf1,int len, int per_line) } } -void print_asc(int level, const unsigned char *buf,int len) +void print_asc(int level, unsigned char *buf,int len) { int i; for (i=0;i<len;i++) DEBUG(level,("%c", isprint(buf[i])?buf[i]:'.')); } -void dump_data(int level, const char *buf1,int len) +void dump_data(int level,char *buf1,int len) { - const unsigned char *buf = (const unsigned char *)buf1; + unsigned char *buf = (unsigned char *)buf1; int i=0; if (len<=0) return; @@ -1736,7 +1710,7 @@ BOOL reg_split_key(char *full_keyname, uint32 *reg_type, char *key_name) return False; } - if (next_token(&full_keyname, tmp, "\n\r", sizeof(tmp))) + if (next_token(NULL, tmp, "\n\r", sizeof(tmp))) { fstrcpy(key_name, tmp); } @@ -1767,10 +1741,10 @@ int smb_mkstemp(char *template) #endif } - -/** +/***************************************************************** malloc that aborts with smb_panic on fail or zero size. -**/ + *****************************************************************/ + void *smb_xmalloc(size_t size) { void *p; @@ -1819,7 +1793,7 @@ int smb_xvasprintf(char **ptr, const char *format, va_list ap) /***************************************************************** like strdup but for memory *****************************************************************/ -void *memdup(const void *p, size_t size) +void *memdup(void *p, size_t size) { void *p2; if (size == 0) return NULL; @@ -1862,21 +1836,6 @@ char *lock_path(char *name) return fname; } - -/** - * @brief Returns an absolute path to a file in the Samba lib directory. - * - * @param name File to find, relative to LIBDIR. - * - * @retval Pointer to a static #pstring containing the full path. - **/ -char *lib_path(char *name) -{ - static pstring fname; - snprintf(fname, sizeof(fname), "%s/%s", dyn_LIBDIR, name); - return fname; -} - /******************************************************************* Given a filename - get its directory name NB: Returned in static storage. Caveats: @@ -1894,7 +1853,7 @@ char *parent_dirname(const char *path) return(NULL); pstrcpy(dirpath, path); - p = strrchr_m(dirpath, '/'); /* Find final '/', if any */ + p = strrchr(dirpath, '/'); /* Find final '/', if any */ if (!p) { pstrcpy(dirpath, "."); /* No final "/", so dir is "." */ } else { @@ -1925,23 +1884,6 @@ BOOL ms_has_wild(char *s) return False; } -BOOL ms_has_wild_w(const smb_ucs2_t *s) -{ - smb_ucs2_t c; - if (!s) return False; - while ((c = *s++)) { - switch (c) { - case UCS2_CHAR('*'): - case UCS2_CHAR('?'): - case UCS2_CHAR('<'): - case UCS2_CHAR('>'): - case UCS2_CHAR('"'): - return True; - } - } - return False; -} - /******************************************************************* a wrapper that handles case sensitivity and the special handling of the ".." name @@ -1949,19 +1891,18 @@ BOOL ms_has_wild_w(const smb_ucs2_t *s) BOOL mask_match(char *string, char *pattern, BOOL is_case_sensitive) { fstring p2, s2; - if (strcmp(string,"..") == 0) string = "."; if (strcmp(pattern,".") == 0) return False; if (is_case_sensitive) { - return ms_fnmatch(pattern, string, Protocol) == 0; + return ms_fnmatch(pattern, string) == 0; } fstrcpy(p2, pattern); fstrcpy(s2, string); strlower(p2); strlower(s2); - return ms_fnmatch(p2, s2, Protocol) == 0; + return ms_fnmatch(p2, s2) == 0; } /********************************************************* @@ -2093,6 +2034,7 @@ BOOL unix_wild_match(char *pattern, char *string) /******************************************************************* free() a data blob *******************************************************************/ + static void free_data_blob(DATA_BLOB *d) { if ((d) && (d->free)) { @@ -2104,6 +2046,7 @@ static void free_data_blob(DATA_BLOB *d) construct a data blob, must be freed with data_blob_free() you can pass NULL for p and get a blank data blob *******************************************************************/ + DATA_BLOB data_blob(const void *p, size_t length) { DATA_BLOB ret; @@ -2126,6 +2069,7 @@ DATA_BLOB data_blob(const void *p, size_t length) /******************************************************************* construct a data blob, using supplied TALLOC_CTX *******************************************************************/ + DATA_BLOB data_blob_talloc(TALLOC_CTX *mem_ctx, const void *p, size_t length) { DATA_BLOB ret; @@ -2167,15 +2111,6 @@ void data_blob_clear(DATA_BLOB *d) } } -/******************************************************************* -free a data blob and clear its contents -*******************************************************************/ -void data_blob_clear_free(DATA_BLOB *d) -{ - data_blob_clear(d); - data_blob_free(d); -} - #ifdef __INSURE__ /******************************************************************* @@ -2188,9 +2123,7 @@ int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6) static int (*fn)(); int ret; char pidstr[10]; - /* you can get /usr/bin/backtrace from - http://samba.org/ftp/unpacked/junkcode/backtrace */ - pstring cmd = "/usr/bin/backtrace %d"; + pstring cmd = "/usr/X11R6/bin/xterm -display :0 -T Panic -n Panic -e /bin/sh -c 'cat /tmp/ierrs.*.%d ; gdb /proc/%d/exe %d'"; slprintf(pidstr, sizeof(pidstr)-1, "%d", sys_getpid()); pstring_sub(cmd, "%d", pidstr); @@ -2199,11 +2132,6 @@ int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6) static void *h; h = dlopen("/usr/local/parasoft/insure++lite/lib.linux2/libinsure.so", RTLD_LAZY); fn = dlsym(h, "_Insure_trap_error"); - - if (!h || h == _Insure_trap_error) { - h = dlopen("/usr/local/parasoft/lib.linux2/libinsure.so", RTLD_LAZY); - fn = dlsym(h, "_Insure_trap_error"); - } } ret = fn(a1, a2, a3, a4, a5, a6); |