diff options
Diffstat (limited to 'source/lib/util.c')
-rw-r--r-- | source/lib/util.c | 648 |
1 files changed, 316 insertions, 332 deletions
diff --git a/source/lib/util.c b/source/lib/util.c index 51b92568b4d..b6266cb9257 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,13 +46,16 @@ #include <rpcsvc/nis.h> -#else /* !WITH_NISPLUS_HOME */ - -#include "rpcsvc/ypclnt.h" - #endif /* WITH_NISPLUS_HOME */ #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */ +#ifdef WITH_SSL +#include <openssl/ssl.h> +#undef Realloc /* SSLeay defines this and samba has a function of this name */ +extern SSL *ssl; +extern int sslFd; +#endif /* WITH_SSL */ + int Protocol = PROTOCOL_COREPLUS; /* a default finfo structure to ensure all fields are sensible */ @@ -90,17 +94,17 @@ char **my_netbios_names; char *tmpdir(void) { - char *p; + char *p; if ((p = getenv("TMPDIR"))) - return p; - return "/tmp"; + return p; + return "/tmp"; } /**************************************************************************** Determine whether we are in the specified group. ****************************************************************************/ -BOOL in_group(gid_t group, gid_t current_gid, int ngroups, const gid_t *groups) +BOOL in_group(gid_t group, gid_t current_gid, int ngroups, gid_t *groups) { int i; @@ -118,7 +122,7 @@ BOOL in_group(gid_t group, gid_t current_gid, int ngroups, const gid_t *groups) Like atoi but gets the value up to the separator character. ****************************************************************************/ -static char *Atoic(char *p, int *n, char *c) +char *Atoic(char *p, int *n, char *c) { if (!isdigit((int)*p)) { DEBUG(5, ("Atoic: malformed number\n")); @@ -130,8 +134,7 @@ static 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; } @@ -155,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) { @@ -175,14 +178,14 @@ 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)) || (S_ISFIFO(sbuf->st_mode))); } @@ -191,14 +194,14 @@ BOOL file_exist(const char *fname,SMB_STRUCT_STAT *sbuf) 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); } /******************************************************************* @@ -260,8 +263,8 @@ void show_msg(char *buf) int i; int bcc=0; - if (!DEBUGLVL(5)) return; - + if (DEBUGLEVEL < 5) return; + DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n", smb_len(buf), (int)CVAL(buf,smb_com), @@ -270,26 +273,31 @@ void show_msg(char *buf) (int)SVAL(buf,smb_err), (int)CVAL(buf,smb_flg), (int)SVAL(buf,smb_flg2))); - DEBUGADD(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\n", + DEBUG(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\nsmt_wct=%d\n", (int)SVAL(buf,smb_tid), (int)SVAL(buf,smb_pid), (int)SVAL(buf,smb_uid), - (int)SVAL(buf,smb_mid))); - DEBUGADD(5,("smt_wct=%d\n",(int)CVAL(buf,smb_wct))); + (int)SVAL(buf,smb_mid), + (int)CVAL(buf,smb_wct))); for (i=0;i<(int)CVAL(buf,smb_wct);i++) - DEBUGADD(5,("smb_vwv[%2d]=%5d (0x%X)\n",i, + { + DEBUG(5,("smb_vwv[%d]=%d (0x%X)\n",i, SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i))); - + } + bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct))); - DEBUGADD(5,("smb_bcc=%d\n",bcc)); + DEBUG(5,("smb_bcc=%d\n",bcc)); if (DEBUGLEVEL < 10) return; - if (DEBUGLEVEL < 50) bcc = MIN(bcc, 512); + if (DEBUGLEVEL < 50) + { + bcc = MIN(bcc, 512); + } - dump_data(10, smb_buf(buf), bcc); + dump_data(10, smb_buf(buf), bcc); } /******************************************************************* @@ -357,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; @@ -395,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; @@ -405,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 ****************************************************************************/ @@ -473,58 +427,55 @@ 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)); } /******************************************************************* close the low 3 fd's and open dev/null in their place ********************************************************************/ -void close_low_fds(BOOL stderr_too) +void close_low_fds(void) { +#ifndef VALGRIND int fd; int i; close(0); close(1); - - if (stderr_too) { - close(2); - } - +#ifndef __INSURE__ + close(2); +#endif /* try and use up these file descriptors, so silly library routines writing to stdout etc won't cause havoc */ for (i=0;i<3;i++) { - if (i == 2 && !stderr_too) - continue; - - fd = sys_open("/dev/null",O_RDWR,0); - if (fd < 0) fd = sys_open("/dev/null",O_WRONLY,0); - if (fd < 0) { - DEBUG(0,("Can't open /dev/null\n")); - return; - } - if (fd != i) { - DEBUG(0,("Didn't get file descriptor %d\n",i)); - return; - } + fd = sys_open("/dev/null",O_RDWR,0); + if (fd < 0) fd = sys_open("/dev/null",O_WRONLY,0); + if (fd < 0) { + DEBUG(0,("Can't open /dev/null\n")); + return; + } + if (fd != i) { + DEBUG(0,("Didn't get file descriptor %d\n",i)); + return; + } } +#endif } /**************************************************************************** @@ -678,26 +629,25 @@ void become_daemon(void) #endif /* HAVE_SETSID */ /* Close fd's 0,1,2. Needed if started by rsh */ - close_low_fds(False); /* Don't close stderr, let the debug system - attach it to the logfile */ + close_low_fds(); } - /**************************************************************************** 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); } /**************************************************************************** @@ -706,23 +656,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); } /**************************************************************************** @@ -757,8 +707,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; @@ -806,7 +755,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; } @@ -858,12 +807,13 @@ struct in_addr *interpret_addr2(const char *str) /******************************************************************* 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); } /******************************************************************* @@ -872,15 +822,15 @@ BOOL is_zero_ip(struct in_addr ip) 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)) @@ -915,7 +865,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 = ""; @@ -963,7 +913,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 = ""; @@ -1042,9 +992,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); } @@ -1053,19 +1000,19 @@ BOOL process_exists(pid_t pid) Convert a uid into a user name. ********************************************************************/ -const char *uidtoname(uid_t uid) +char *uidtoname(uid_t uid) { static fstring name; struct passwd *pass; - pass = getpwuid_alloc(uid); - if (pass) { - fstrcpy(name, pass->pw_name); - passwd_free(&pass); - } else { - slprintf(name, sizeof(name) - 1, "%ld",(long int)uid); - } - return name; + if (winbind_uidtoname(name, uid)) + return name; + + pass = sys_getpwuid(uid); + if (pass) + return(pass->pw_name); + slprintf(name, sizeof(name) - 1, "%d",(int)uid); + return(name); } @@ -1078,6 +1025,9 @@ 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); @@ -1086,7 +1036,7 @@ char *gidtoname(gid_t gid) } /******************************************************************* - Convert a user name into a uid. + Convert a user name into a uid. If winbindd is present uses this. ********************************************************************/ uid_t nametouid(char *name) @@ -1095,25 +1045,25 @@ uid_t nametouid(char *name) char *p; uid_t u; - pass = getpwnam_alloc(name); - if (pass) { - u = pass->pw_uid; - passwd_free(&pass); - return u; - } - u = (uid_t)strtol(name, &p, 0); if ((p != name) && (*p == '\0')) return u; + if (winbind_nametouid(&u, name)) + return u; + + pass = sys_getpwnam(name); + if (pass) + return(pass->pw_uid); return (uid_t)-1; } /******************************************************************* - Convert a name to a gid_t if possible. Return -1 if not a group. + Convert a name to a gid_t if possible. Return -1 if not a group. If winbindd + is present does a shortcut lookup... ********************************************************************/ -gid_t nametogid(const char *name) +gid_t nametogid(char *name) { struct group *grp; char *p; @@ -1123,7 +1073,10 @@ gid_t nametogid(const char *name) if ((p != name) && (*p == '\0')) return g; - grp = sys_getgrnam(name); + if (winbind_nametogid(&g, name)) + return g; + + grp = getgrnam(name); if (grp) return(grp->gr_gid); return (gid_t)-1; @@ -1135,18 +1088,8 @@ something really nasty happened - panic! void smb_panic(char *why) { char *cmd = lp_panic_action(); - int result; - if (cmd && *cmd) { - DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd)); - result = system(cmd); - - if (result == -1) - DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n", - strerror(errno))); - else - DEBUG(0, ("smb_panic(): action returned status %d\n", - WEXITSTATUS(result))); + system(cmd); } DEBUG(0,("PANIC: %s\n", why)); dbgflush(); @@ -1209,7 +1152,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'; @@ -1276,7 +1219,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) @@ -1309,7 +1252,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; } @@ -1402,21 +1345,20 @@ BOOL fcntl_lock(int fd, int op, SMB_OFF_T offset, SMB_OFF_T count, int type) } /******************************************************************* - Is the name specified one of my netbios names. - Returns true if it is equal, false otherwise. +is the name specified one of my netbios names +returns true is it is equal, false otherwise ********************************************************************/ - BOOL is_myname(char *s) { - int n; - BOOL ret = False; + int n; + BOOL ret = False; - for (n=0; my_netbios_names[n]; n++) { - if (strequal(my_netbios_names[n], s)) - ret=True; - } - DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret)); - return(ret); + for (n=0; my_netbios_names[n]; n++) { + if (strequal(my_netbios_names[n], s)) + ret=True; + } + DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret)); + return(ret); } /******************************************************************** @@ -1437,16 +1379,19 @@ const char* get_my_primary_ip (void) 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)) return True; /* maybe its an IP address? */ - if (is_ipaddress(s)) { + if (is_ipaddress(s)) + { struct iface_struct nics[MAX_INTERFACES]; int i, n; uint32 ip; @@ -1461,59 +1406,62 @@ BOOL is_myname_or_ipaddr(char *s) return True; } } - + /* check for an alias */ ptr = lp_netbios_aliases(); - for ( ; *ptr; ptr++ ) { - if (StrCaseCmp(s, *ptr) == 0) + while ( next_token(&ptr, nbname, NULL, sizeof(nbname)) ) + { + if (StrCaseCmp(s, nbname) == 0) return True; } + /* no match */ return False; + } + /******************************************************************* - Set the horrid remote_arch string based on an enum. +set the horrid remote_arch string based on an enum. ********************************************************************/ - void set_remote_arch(enum remote_arch_types type) { - extern fstring remote_arch; - ra_type = type; - switch( type ) { - case RA_WFWG: - fstrcpy(remote_arch, "WfWg"); - return; - case RA_OS2: - fstrcpy(remote_arch, "OS2"); - return; - case RA_WIN95: - fstrcpy(remote_arch, "Win95"); - return; - case RA_WINNT: - fstrcpy(remote_arch, "WinNT"); - return; - case RA_WIN2K: - fstrcpy(remote_arch, "Win2K"); - return; - case RA_SAMBA: - fstrcpy(remote_arch,"Samba"); - return; - default: - ra_type = RA_UNKNOWN; - fstrcpy(remote_arch, "UNKNOWN"); - break; - } + extern fstring remote_arch; + ra_type = type; + switch( type ) + { + case RA_WFWG: + fstrcpy(remote_arch, "WfWg"); + return; + case RA_OS2: + fstrcpy(remote_arch, "OS2"); + return; + case RA_WIN95: + fstrcpy(remote_arch, "Win95"); + return; + case RA_WINNT: + fstrcpy(remote_arch, "WinNT"); + return; + case RA_WIN2K: + fstrcpy(remote_arch, "Win2K"); + return; + case RA_SAMBA: + fstrcpy(remote_arch,"Samba"); + return; + default: + ra_type = RA_UNKNOWN; + fstrcpy(remote_arch, "UNKNOWN"); + break; + } } /******************************************************************* Get the remote_arch type. ********************************************************************/ - enum remote_arch_types get_remote_arch(void) { - return ra_type; + return ra_type; } @@ -1521,35 +1469,42 @@ void out_ascii(FILE *f, unsigned char *buf,int len) { int i; for (i=0;i<len;i++) + { fprintf(f, "%c", isprint(buf[i])?buf[i]:'.'); + } } void out_data(FILE *f,char *buf1,int len, int per_line) { unsigned char *buf = (unsigned char *)buf1; int i=0; - if (len<=0) { + if (len<=0) + { return; } fprintf(f, "[%03X] ",i); - for (i=0;i<len;) { + for (i=0;i<len;) + { fprintf(f, "%02X ",(int)buf[i]); i++; if (i%(per_line/2) == 0) fprintf(f, " "); - if (i%per_line == 0) { + if (i%per_line == 0) + { out_ascii(f,&buf[i-per_line ],per_line/2); fprintf(f, " "); out_ascii(f,&buf[i-per_line/2],per_line/2); fprintf(f, "\n"); if (i<len) fprintf(f, "[%03X] ",i); } } - if ((i%per_line) != 0) { + if ((i%per_line) != 0) + { int n; n = per_line - (i%per_line); fprintf(f, " "); if (n>(per_line/2)) fprintf(f, " "); - while (n--) { + while (n--) + { fprintf(f, " "); } n = MIN(per_line/2,i%per_line); @@ -1560,44 +1515,44 @@ 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; - int i=0; - if (len<=0) return; + unsigned char *buf = (unsigned char *)buf1; + int i=0; + if (len<=0) return; - if (!DEBUGLVL(level)) return; - - DEBUGADD(level,("[%03X] ",i)); - for (i=0;i<len;) { - 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 (i<len) DEBUGADD(level,("[%03X] ",i)); - } - } - 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")); - } + DEBUG(level,("[%03X] ",i)); + for (i=0;i<len;) { + DEBUG(level,("%02X ",(int)buf[i])); + i++; + if (i%8 == 0) DEBUG(level,(" ")); + if (i%16 == 0) { + print_asc(level,&buf[i-16],8); DEBUG(level,(" ")); + print_asc(level,&buf[i-8],8); DEBUG(level,("\n")); + if (i<len) DEBUG(level,("[%03X] ",i)); + } + } + if (i%16) { + int n; + + n = 16 - (i%16); + DEBUG(level,(" ")); + if (n>8) DEBUG(level,(" ")); + while (n--) DEBUG(level,(" ")); + + n = MIN(8,i%16); + print_asc(level,&buf[i-(i%16)],n); DEBUG(level,(" ")); + n = (i%16) - n; + if (n>0) print_asc(level,&buf[i-n],n); + DEBUG(level,("\n")); + } } char *tab_depth(int depth) @@ -1757,7 +1712,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); } @@ -1824,17 +1779,6 @@ char *smb_xstrdup(const char *s) return s1; } -/** - strndup that aborts on malloc fail. -**/ -char *smb_xstrndup(const char *s, size_t n) -{ - char *s1 = strndup(s, n); - if (!s1) - smb_panic("smb_xstrndup: malloc fail\n"); - return s1; -} - /* vasprintf that aborts on malloc fail */ @@ -1855,7 +1799,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; @@ -1881,7 +1825,7 @@ char *myhostname(void) /***************************************************************** a useful function for returning a path in the Samba lock directory *****************************************************************/ -char *lock_path(const char *name) +char *lock_path(char *name) { static pstring fname; @@ -1900,39 +1844,24 @@ char *lock_path(const char *name) /***************************************************************** a useful function for returning a path in the Samba pid directory - *****************************************************************/ -char *pid_path(const char *name) + *****************************************************************/ +char *pid_path(char *name) { static pstring fname; pstrcpy(fname,lp_piddir()); trim_string(fname,"","/"); - + if (!directory_exist(fname,NULL)) { mkdir(fname,0755); } - + pstrcat(fname,"/"); pstrcat(fname,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(const 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: @@ -1950,7 +1879,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 { @@ -1981,23 +1910,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 @@ -2005,19 +1917,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; } /********************************************************* @@ -2146,6 +2057,86 @@ BOOL unix_wild_match(char *pattern, char *string) return unix_do_match(p2, s2) == 0; } +/******************************************************************* + free() a data blob +*******************************************************************/ + +static void free_data_blob(DATA_BLOB *d) +{ + if ((d) && (d->free)) { + SAFE_FREE(d->data); + } +} + +/******************************************************************* + 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; + + if (!length) { + ZERO_STRUCT(ret); + return ret; + } + + if (p) { + ret.data = smb_xmemdup(p, length); + } else { + ret.data = smb_xmalloc(length); + } + ret.length = length; + ret.free = free_data_blob; + return ret; +} + +/******************************************************************* + 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; + + if (!p || !length) { + ZERO_STRUCT(ret); + return ret; + } + + ret.data = talloc_memdup(mem_ctx, p, length); + if (ret.data == NULL) + smb_panic("data_blob_talloc: talloc_memdup failed.\n"); + + ret.length = length; + ret.free = NULL; + return ret; +} + +/******************************************************************* +free a data blob +*******************************************************************/ +void data_blob_free(DATA_BLOB *d) +{ + if (d) { + if (d->free) { + (d->free)(d); + } + ZERO_STRUCTP(d); + } +} + +/******************************************************************* +clear a DATA_BLOB's contents +*******************************************************************/ +void data_blob_clear(DATA_BLOB *d) +{ + if (d->data) { + memset(d->data, 0, d->length); + } +} + #ifdef __INSURE__ /******************************************************************* @@ -2158,9 +2149,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); @@ -2169,11 +2158,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); |