diff options
author | Jeremy Allison <jra@samba.org> | 2007-11-15 14:19:52 -0800 |
---|---|---|
committer | Jeremy Allison <jra@samba.org> | 2007-11-15 14:19:52 -0800 |
commit | 68be9a820059ee96dd26c527efd7c14e679d3f2c (patch) | |
tree | c3c853a01013fc7977ab02a31e673fe17b4135e6 /source3/lib | |
parent | 8e1b0f81c27dc332560f19de27fb86ac96c59775 (diff) | |
download | samba-68be9a820059ee96dd26c527efd7c14e679d3f2c.tar.gz samba-68be9a820059ee96dd26c527efd7c14e679d3f2c.tar.bz2 samba-68be9a820059ee96dd26c527efd7c14e679d3f2c.zip |
More pstring removal. This one was tricky. I had to add
one horror (pstring_clean_name()) which will have to
remain until I've removed all pstrings from the client code.
Jeremy.
(This used to be commit 1ea3ac80146b83c2522b69e7747c823366a2b47d)
Diffstat (limited to 'source3/lib')
-rw-r--r-- | source3/lib/afs.c | 18 | ||||
-rw-r--r-- | source3/lib/debug.c | 105 | ||||
-rw-r--r-- | source3/lib/fault.c | 29 | ||||
-rw-r--r-- | source3/lib/popt_common.c | 26 | ||||
-rw-r--r-- | source3/lib/readline.c | 13 | ||||
-rw-r--r-- | source3/lib/smbldap_util.c | 93 | ||||
-rw-r--r-- | source3/lib/sysquotas.c | 44 | ||||
-rw-r--r-- | source3/lib/util.c | 204 | ||||
-rw-r--r-- | source3/lib/util_file.c | 4 | ||||
-rw-r--r-- | source3/lib/util_unistr.c | 52 |
10 files changed, 370 insertions, 218 deletions
diff --git a/source3/lib/afs.c b/source3/lib/afs.c index 35f213fd08..dadd83d513 100644 --- a/source3/lib/afs.c +++ b/source3/lib/afs.c @@ -213,10 +213,10 @@ bool afs_login(connection_struct *conn) extern userdom_struct current_user_info; extern struct current_user current_user; DATA_BLOB ticket; - pstring afs_username; - char *cell; + char *afs_username = NULL; + char *cell = NULL; bool result; - char *ticket_str; + char *ticket_str = NULL; const DOM_SID *user_sid; struct ClearToken ct; @@ -229,7 +229,13 @@ bool afs_login(connection_struct *conn) afs_username, sizeof(afs_username)); user_sid = ¤t_user.nt_user_token->user_sids[0]; - pstring_sub(afs_username, "%s", sid_string_static(user_sid)); + afs_username = talloc_string_sub(talloc_tos(), + lp_afs_username_map(), + "%s", + sid_string_static(user_sid)); + if (!afs_username) { + return false; + } /* The pts command always generates completely lower-case user * names. */ @@ -240,13 +246,13 @@ bool afs_login(connection_struct *conn) if (cell == NULL) { DEBUG(1, ("AFS username doesn't contain a @, " "could not find cell\n")); - return False; + return false; } *cell = '\0'; cell += 1; - DEBUG(10, ("Trying to log into AFS for user %s@%s\n", + DEBUG(10, ("Trying to log into AFS for user %s@%s\n", afs_username, cell)); if (!afs_createtoken(afs_username, cell, &ticket, &ct)) diff --git a/source3/lib/debug.c b/source3/lib/debug.c index 72285277ec..49ec40ae84 100644 --- a/source3/lib/debug.c +++ b/source3/lib/debug.c @@ -29,7 +29,8 @@ * for a terminating null byte. */ -#define FORMAT_BUFR_MAX ( sizeof( format_bufr ) - 1 ) +#define FORMAT_BUFR_SIZE 1024 +#define FORMAT_BUFR_MAX (FORMAT_BUFR_SIZE - 1) /* -------------------------------------------------------------------------- ** * This module implements Samba's debugging utility. @@ -78,16 +79,16 @@ */ XFILE *dbf = NULL; -pstring debugf = ""; +static char *debugf = NULL; bool debug_warn_unknown_class = True; bool debug_auto_add_unknown_class = True; bool AllowDebugChange = True; -/* - used to check if the user specified a - logfile on the command line +/* + used to check if the user specified a + logfile on the command line */ -bool override_logfile; +bool override_logfile; /* @@ -137,7 +138,7 @@ static int debug_count = 0; #ifdef WITH_SYSLOG static int syslog_level = 0; #endif -static pstring format_bufr = { '\0' }; +static char *format_bufr = NULL; static size_t format_pos = 0; static bool log_overflow = False; @@ -536,6 +537,10 @@ void debug_init(void) for(p = default_classname_table; *p; p++) { debug_add_class(*p); } + format_bufr = SMB_MALLOC(FORMAT_BUFR_SIZE); + if (!format_bufr) { + smb_panic("debug_init: unable to create buffer"); + } } void debug_register_msgs(struct messaging_context *msg_ctx) @@ -583,6 +588,16 @@ void setup_logging(const char *pname, bool interactive) #endif } +/*************************************************************************** + Set the logfile name. +**************************************************************************/ + +void debug_set_logfile(const char *name) +{ + SAFE_FREE(debugf); + debugf = SMB_STRDUP(name); +} + /************************************************************************** reopen the log files note that we now do this unconditionally @@ -593,7 +608,7 @@ void setup_logging(const char *pname, bool interactive) bool reopen_logs( void ) { - pstring fname; + char *fname = NULL; mode_t oldumask; XFILE *new_dbf = NULL; XFILE *old_dbf = NULL; @@ -603,19 +618,27 @@ bool reopen_logs( void ) return True; oldumask = umask( 022 ); - - pstrcpy(fname, debugf ); - debugf[0] = '\0'; + + fname = debugf; + if (!fname) { + return false; + } + debugf = NULL; if (lp_loaded()) { char *logfname; logfname = lp_logfile(); - if (*logfname) - pstrcpy(fname, logfname); + if (*logfname) { + SAFE_FREE(fname); + fname = SMB_STRDUP(logfname); + if (!fname) { + return false; + } + } } - pstrcpy( debugf, fname ); + debugf = fname; new_dbf = x_fopen( debugf, O_WRONLY|O_APPEND|O_CREAT, 0644); if (!new_dbf) { @@ -702,15 +725,18 @@ void check_log_size( void ) if( sys_fstat( x_fileno( dbf ), &st ) == 0 && st.st_size > maxlog ) { (void)reopen_logs(); if( dbf && get_file_size( debugf ) > maxlog ) { - pstring name; + char *name = NULL; + + if (asprintf(&name, "%s.old", debugf ) < 0) { + return; + } + (void)rename(debugf, name); - slprintf( name, sizeof(name)-1, "%s.old", debugf ); - (void)rename( debugf, name ); - if (!reopen_logs()) { /* We failed to reopen a log - continue using the old name. */ (void)rename(name, debugf); } + SAFE_FREE(name); } } @@ -747,7 +773,7 @@ void check_log_size( void ) int Debug1( const char *format_str, ... ) { - va_list ap; + va_list ap; int old_errno = errno; debug_count++; @@ -762,8 +788,8 @@ void check_log_size( void ) } /* prevent recursion by checking if reopen_logs() has temporaily - set the debugf string to "" */ - if( debugf[0] == '\0') + set the debugf string to NULL */ + if( debugf == NULL) return( 0 ); #ifdef WITH_SYSLOG @@ -789,29 +815,31 @@ void check_log_size( void ) /* map debug levels to syslog() priorities * note that not all DEBUG(0, ...) calls are * necessarily errors */ - static int priority_map[] = { + static int priority_map[] = { LOG_ERR, /* 0 */ LOG_WARNING, /* 1 */ LOG_NOTICE, /* 2 */ LOG_INFO, /* 3 */ }; int priority; - pstring msgbuf; + char *msgbuf = NULL; if( syslog_level >= ( sizeof(priority_map) / sizeof(priority_map[0]) ) || syslog_level < 0) priority = LOG_DEBUG; else priority = priority_map[syslog_level]; - va_start( ap, format_str ); - vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap ); - va_end( ap ); + va_start(ap, format_str); + vasprintf(&msgbuf, format_str, ap); + va_end(ap); - msgbuf[255] = '\0'; - syslog( priority, "%s", msgbuf ); + if (msgbuf) { + syslog(priority, "%s", msgbuf); + } + SAFE_FREE(msgbuf); } #endif - + check_log_size(); #ifdef WITH_SYSLOG @@ -1018,13 +1046,18 @@ bool dbghdr(int level, int cls, const char *file, const char *func, int line) bool dbgtext( const char *format_str, ... ) { va_list ap; - pstring msgbuf; - - va_start( ap, format_str ); - vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap ); - va_end( ap ); + char *msgbuf = NULL; + bool ret = true; - format_debug_text( msgbuf ); + va_start(ap, format_str); + vasprintf(&msgbuf, format_str, ap); + va_end(ap); - return( True ); + if (msgbuf) { + format_debug_text(msgbuf); + } else { + ret = false; + } + SAFE_FREE(msgbuf); + return ret; } diff --git a/source3/lib/fault.c b/source3/lib/fault.c index 6ab1a07900..52c4ae63e5 100644 --- a/source3/lib/fault.c +++ b/source3/lib/fault.c @@ -24,7 +24,7 @@ #endif static void (*cont_fn)(void *); -static pstring corepath; +static char *corepath; /******************************************************************* report a fault @@ -93,11 +93,13 @@ make all the preparations to safely dump a core file void dump_core_setup(const char *progname) { - pstring logbase; - char * end; + char *logbase = NULL; + char *end = NULL; if (lp_logfile() && *lp_logfile()) { - snprintf(logbase, sizeof(logbase), "%s", lp_logfile()); + if (asprintf(&logbase, "%s", lp_logfile()) < 0) { + return; + } if ((end = strrchr_m(logbase, '/'))) { *end = '\0'; } @@ -106,21 +108,32 @@ void dump_core_setup(const char *progname) * line by the -l option but the "log file" option is not set * in smb.conf. */ - snprintf(logbase, sizeof(logbase), "%s", dyn_LOGFILEBASE); + if (asprintf(&logbase, "%s", dyn_LOGFILEBASE) < 0) { + return; + } } SMB_ASSERT(progname != NULL); - snprintf(corepath, sizeof(corepath), "%s/cores", logbase); + if (asprintf(&corepath, "%s/cores", logbase) < 0) { + SAFE_FREE(logbase); + return; + } mkdir(corepath,0700); - snprintf(corepath, sizeof(corepath), "%s/cores/%s", - logbase, progname); + SAFE_FREE(corepath); + if (asprintf(&corepath, "%s/cores/%s", + logbase, progname) < 0) { + SAFE_FREE(logbase); + return; + } mkdir(corepath,0700); sys_chown(corepath,getuid(),getgid()); chmod(corepath,0700); + SAFE_FREE(corepath); + #ifdef HAVE_GETRLIMIT #ifdef RLIMIT_CORE { diff --git a/source3/lib/popt_common.c b/source3/lib/popt_common.c index 7cc066d667..dbb66b0ba5 100644 --- a/source3/lib/popt_common.c +++ b/source3/lib/popt_common.c @@ -41,19 +41,22 @@ struct user_auth_info cmdline_auth_info; static void set_logfile(poptContext con, const char * arg) { - pstring logfile; + char *logfile = NULL; const char *pname; - + /* Find out basename of current program */ pname = strrchr_m(poptGetInvocationName(con),'/'); if (!pname) pname = poptGetInvocationName(con); - else + else pname++; - pstr_sprintf(logfile, "%s/log.%s", arg, pname); + if (asprintf(&logfile, "%s/log.%s", arg, pname) < 0) { + return; + } lp_set_logfile(logfile); + SAFE_FREE(logfile); } static bool PrintSambaVersionString; @@ -285,22 +288,24 @@ const struct poptOption popt_common_dynconfig[] = { * get a password from a a file or file descriptor * exit on failure * ****************************************************************************/ + static void get_password_file(struct user_auth_info *a) { int fd = -1; char *p; bool close_it = False; - pstring spec; + char *spec = NULL; char pass[128]; if ((p = getenv("PASSWD_FD")) != NULL) { - pstrcpy(spec, "descriptor "); - pstrcat(spec, p); + if (asprintf(&spec, "descriptor %s", p) < 0) { + return; + } sscanf(p, "%d", &fd); - close_it = False; + close_it = false; } else if ((p = getenv("PASSWD_FILE")) != NULL) { fd = sys_open(p, O_RDONLY, 0); - pstrcpy(spec, p); + spec = SMB_STRDUP(p); if (fd < 0) { fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n", spec, strerror(errno)); @@ -325,15 +330,18 @@ static void get_password_file(struct user_auth_info *a) } else { fprintf(stderr, "Error reading password from file %s: %s\n", spec, "empty password\n"); + SAFE_FREE(spec); exit(1); } default: fprintf(stderr, "Error reading password from file %s: %s\n", spec, strerror(errno)); + SAFE_FREE(spec); exit(1); } } + SAFE_FREE(spec); pstrcpy(a->password, pass); if (close_it) close(fd); diff --git a/source3/lib/readline.c b/source3/lib/readline.c index 9d1597abb1..6fed929be0 100644 --- a/source3/lib/readline.c +++ b/source3/lib/readline.c @@ -53,7 +53,7 @@ static char *smb_readline_replacement(const char *prompt, void (*callback)(void) char **(completion_fn)(const char *text, int start, int end)) { fd_set fds; - static pstring line; + static char *line; struct timeval timeout; int fd = x_fileno(x_stdin); char *ret; @@ -64,15 +64,22 @@ static char *smb_readline_replacement(const char *prompt, void (*callback)(void) x_fflush(x_stdout); } + if (line == NULL) { + line = SMB_MALLOC(BUFSIZ); + if (!line) { + return NULL; + } + } + while (1) { timeout.tv_sec = 5; timeout.tv_usec = 0; FD_ZERO(&fds); FD_SET(fd,&fds); - + if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) { - ret = x_fgets(line, sizeof(line), x_stdin); + ret = x_fgets(line, BUFSIZ, x_stdin); return ret; } if (callback) diff --git a/source3/lib/smbldap_util.c b/source3/lib/smbldap_util.c index 8ea9d42a29..42861ae111 100644 --- a/source3/lib/smbldap_util.c +++ b/source3/lib/smbldap_util.c @@ -36,7 +36,7 @@ static NTSTATUS add_new_domain_account_policies(struct smbldap_state *ldap_state int i, rc; uint32 policy_default; const char *policy_attr = NULL; - pstring dn; + char *dn = NULL; LDAPMod **mods = NULL; char *escape_domain_name; @@ -48,15 +48,17 @@ static NTSTATUS add_new_domain_account_policies(struct smbldap_state *ldap_state return NT_STATUS_NO_MEMORY; } - pstr_sprintf(dn, "%s=%s,%s", + if (asprintf(&dn, "%s=%s,%s", get_attr_key2string(dominfo_attr_list, LDAP_ATTR_DOMAIN), - escape_domain_name, lp_ldap_suffix()); + escape_domain_name, lp_ldap_suffix()) < 0) { + SAFE_FREE(escape_domain_name); + return NT_STATUS_NO_MEMORY; + } SAFE_FREE(escape_domain_name); for (i=1; decode_account_policy_name(i) != NULL; i++) { - - pstring val; + char *val = NULL; policy_attr = get_account_policy_attr(i); if (!policy_attr) { @@ -66,17 +68,23 @@ static NTSTATUS add_new_domain_account_policies(struct smbldap_state *ldap_state if (!account_policy_get_default(i, &policy_default)) { DEBUG(0,("add_new_domain_account_policies: failed to get default account policy\n")); + SAFE_FREE(dn); return ntstatus; } DEBUG(10,("add_new_domain_account_policies: adding \"%s\" with value: %d\n", policy_attr, policy_default)); - pstr_sprintf(val, "%d", policy_default); + if (asprintf(&val, "%d", policy_default) < 0) { + SAFE_FREE(dn); + return NT_STATUS_NO_MEMORY; + } smbldap_set_mod( &mods, LDAP_MOD_REPLACE, policy_attr, val); rc = smbldap_modify(ldap_state, dn, mods); + SAFE_FREE(val); + if (rc!=LDAP_SUCCESS) { char *ld_error = NULL; ldap_get_option(ldap_state->ldap_struct, LDAP_OPT_ERROR_STRING, &ld_error); @@ -84,11 +92,13 @@ static NTSTATUS add_new_domain_account_policies(struct smbldap_state *ldap_state dn, ldap_err2string(rc), ld_error ? ld_error : "unknown")); SAFE_FREE(ld_error); + SAFE_FREE(dn); ldap_mods_free(mods, True); return ntstatus; } } + SAFE_FREE(dn); ldap_mods_free(mods, True); return NT_STATUS_OK; @@ -101,12 +111,13 @@ static NTSTATUS add_new_domain_account_policies(struct smbldap_state *ldap_state TODO: Add other attributes, and allow modification. *********************************************************************/ -static NTSTATUS add_new_domain_info(struct smbldap_state *ldap_state, - const char *domain_name) +static NTSTATUS add_new_domain_info(struct smbldap_state *ldap_state, + const char *domain_name) { fstring sid_string; fstring algorithmic_rid_base_string; - pstring filter, dn; + char *filter = NULL; + char *dn = NULL; LDAPMod **mods = NULL; int rc; LDAPMessage *result = NULL; @@ -121,29 +132,33 @@ static NTSTATUS add_new_domain_info(struct smbldap_state *ldap_state, return NT_STATUS_NO_MEMORY; } - slprintf (filter, sizeof (filter) - 1, "(&(%s=%s)(objectclass=%s))", - get_attr_key2string(dominfo_attr_list, LDAP_ATTR_DOMAIN), - escape_domain_name, LDAP_OBJ_DOMINFO); + if (asprintf(&filter, "(&(%s=%s)(objectclass=%s))", + get_attr_key2string(dominfo_attr_list, LDAP_ATTR_DOMAIN), + escape_domain_name, LDAP_OBJ_DOMINFO) < 0) { + SAFE_FREE(escape_domain_name); + return NT_STATUS_NO_MEMORY; + } SAFE_FREE(escape_domain_name); - attr_list = get_attr_list( NULL, dominfo_attr_list ); + attr_list = get_attr_list(NULL, dominfo_attr_list ); rc = smbldap_search_suffix(ldap_state, filter, attr_list, &result); TALLOC_FREE( attr_list ); + SAFE_FREE(filter); if (rc != LDAP_SUCCESS) { return NT_STATUS_UNSUCCESSFUL; } num_result = ldap_count_entries(ldap_state->ldap_struct, result); - + if (num_result > 1) { DEBUG (0, ("add_new_domain_info: More than domain with that name exists: bailing " "out!\n")); ldap_msgfree(result); return NT_STATUS_UNSUCCESSFUL; } - + /* Check if we need to add an entry */ DEBUG(3,("add_new_domain_info: Adding new domain\n")); @@ -154,9 +169,12 @@ static NTSTATUS add_new_domain_info(struct smbldap_state *ldap_state, return NT_STATUS_NO_MEMORY; } - pstr_sprintf(dn, "%s=%s,%s", + if (asprintf(&dn, "%s=%s,%s", get_attr_key2string(dominfo_attr_list, LDAP_ATTR_DOMAIN), - escape_domain_name, lp_ldap_suffix()); + escape_domain_name, lp_ldap_suffix()) < 0) { + SAFE_FREE(escape_domain_name); + return NT_STATUS_NO_MEMORY; + } SAFE_FREE(escape_domain_name); @@ -168,7 +186,7 @@ static NTSTATUS add_new_domain_info(struct smbldap_state *ldap_state, smbldap_set_mod(&mods, LDAP_MOD_ADD, get_attr_key2string(dominfo_attr_list, - LDAP_ATTR_DOMAIN), + LDAP_ATTR_DOMAIN), domain_name); /* If we don't have an entry, then ask secrets.tdb for what it thinks. @@ -185,21 +203,21 @@ static NTSTATUS add_new_domain_info(struct smbldap_state *ldap_state, algorithmic_rid_base()); smbldap_set_mod(&mods, LDAP_MOD_ADD, get_attr_key2string(dominfo_attr_list, - LDAP_ATTR_ALGORITHMIC_RID_BASE), + LDAP_ATTR_ALGORITHMIC_RID_BASE), algorithmic_rid_base_string); smbldap_set_mod(&mods, LDAP_MOD_ADD, "objectclass", LDAP_OBJ_DOMINFO); - + /* add the sambaNextUserRid attributes. */ - + { uint32 rid = BASE_RID; fstring rid_str; - + fstr_sprintf( rid_str, "%i", rid ); DEBUG(10,("add_new_domain_info: setting next available user rid [%s]\n", rid_str)); - smbldap_set_mod(&mods, LDAP_MOD_ADD, + smbldap_set_mod(&mods, LDAP_MOD_ADD, get_attr_key2string(dominfo_attr_list, - LDAP_ATTR_NEXT_USERRID), + LDAP_ATTR_NEXT_USERRID), rid_str); } @@ -214,13 +232,14 @@ static NTSTATUS add_new_domain_info(struct smbldap_state *ldap_state, dn, ldap_err2string(rc), ld_error?ld_error:"unknown")); SAFE_FREE(ld_error); - + SAFE_FREE(dn); ldap_mods_free(mods, True); return NT_STATUS_UNSUCCESSFUL; } DEBUG(2,("add_new_domain_info: added: domain = %s in the LDAP database\n", domain_name)); ldap_mods_free(mods, True); + SAFE_FREE(dn); return NT_STATUS_OK; } @@ -233,22 +252,25 @@ NTSTATUS smbldap_search_domain_info(struct smbldap_state *ldap_state, bool try_add) { NTSTATUS status = NT_STATUS_UNSUCCESSFUL; - pstring filter; + char *filter = NULL; int rc; const char **attr_list; int count; char *escape_domain_name; - + escape_domain_name = escape_ldap_string_alloc(domain_name); if (!escape_domain_name) { DEBUG(0, ("Out of memory!\n")); return NT_STATUS_NO_MEMORY; } - pstr_sprintf(filter, "(&(objectClass=%s)(%s=%s))", + if (asprintf(&filter, "(&(objectClass=%s)(%s=%s))", LDAP_OBJ_DOMINFO, - get_attr_key2string(dominfo_attr_list, LDAP_ATTR_DOMAIN), - escape_domain_name); + get_attr_key2string(dominfo_attr_list, LDAP_ATTR_DOMAIN), + escape_domain_name) < 0) { + SAFE_FREE(escape_domain_name); + return NT_STATUS_NO_MEMORY; + } SAFE_FREE(escape_domain_name); @@ -264,14 +286,17 @@ NTSTATUS smbldap_search_domain_info(struct smbldap_state *ldap_state, goto failed; } + SAFE_FREE(filter); + count = ldap_count_entries(ldap_state->ldap_struct, *result); - if (count == 1) + if (count == 1) { return NT_STATUS_OK; + } ldap_msgfree(*result); *result = NULL; - + if (count < 1) { DEBUG(3, ("smbldap_search_domain_info: Got no domain info entries for domain\n")); @@ -285,7 +310,7 @@ NTSTATUS smbldap_search_domain_info(struct smbldap_state *ldap_state, domain_name, nt_errstr(status))); goto failed; } - + status = add_new_domain_account_policies(ldap_state, domain_name); if (!NT_STATUS_IS_OK(status)) { DEBUG(0, ("smbldap_search_domain_info: Adding domain account policies for %s failed with %s\n", @@ -294,7 +319,7 @@ NTSTATUS smbldap_search_domain_info(struct smbldap_state *ldap_state, } return smbldap_search_domain_info(ldap_state, result, domain_name, False); - + } if (count > 1 ) { diff --git a/source3/lib/sysquotas.c b/source3/lib/sysquotas.c index 094422ac9b..4a2d88abdf 100644 --- a/source3/lib/sysquotas.c +++ b/source3/lib/sysquotas.c @@ -177,19 +177,19 @@ static struct { #ifdef HAVE_XFS_QUOTAS {"xfs", sys_get_xfs_quota, sys_set_xfs_quota}, #endif /* HAVE_XFS_QUOTAS */ - {NULL, NULL, NULL} + {NULL, NULL, NULL} }; static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp) { const char *get_quota_command; char **lines = NULL; - + get_quota_command = lp_get_quota_command(); if (get_quota_command && *get_quota_command) { const char *p; char *p2; - pstring syscmd; + char *syscmd = NULL; int _id = -1; switch(qtype) { @@ -206,13 +206,16 @@ static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t return -1; } - slprintf(syscmd, sizeof(syscmd)-1, - "%s \"%s\" %d %d", - get_quota_command, path, qtype, _id); + if (asprintf(&syscmd, "%s \"%s\" %d %d", + get_quota_command, path, qtype, _id) < 0) { + return -1; + } DEBUG (3, ("get_quota: Running command %s\n", syscmd)); lines = file_lines_pload(syscmd, NULL); + SAFE_FREE(syscmd); + if (lines) { char *line = lines[0]; @@ -325,7 +328,7 @@ static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t errno = ENOSYS; return -1; - + invalid_param: file_lines_free(lines); @@ -336,11 +339,11 @@ invalid_param: static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp) { const char *set_quota_command; - + set_quota_command = lp_set_quota_command(); if (set_quota_command && *set_quota_command) { - char **lines; - pstring syscmd; + char **lines = NULL; + char *syscmd = NULL; int _id = -1; switch(qtype) { @@ -357,37 +360,40 @@ static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t } #ifdef LARGE_SMB_OFF_T - slprintf(syscmd, sizeof(syscmd)-1, + if (asprintf(&syscmd, "%s \"%s\" %d %d " "%u %llu %llu " - "%llu %llu %llu ", + "%llu %llu %llu ", set_quota_command, path, qtype, _id, dp->qflags, (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit, (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit, - (long long unsigned)dp->bsize); + (long long unsigned)dp->bsize) < 0) { + return -1; + } #else /* LARGE_SMB_OFF_T */ - slprintf(syscmd, sizeof(syscmd)-1, + if (asprintf(&syscmd, "%s \"%s\" %d %d " "%u %lu %lu " - "%lu %lu %lu ", + "%lu %lu %lu ", set_quota_command, path, qtype, _id, dp->qflags, (long unsigned)dp->softlimit,(long unsigned)dp->hardlimit, (long unsigned)dp->isoftlimit,(long unsigned)dp->ihardlimit, - (long unsigned)dp->bsize); + (long unsigned)dp->bsize) < 0) { + return -1; + } #endif /* LARGE_SMB_OFF_T */ - - DEBUG (3, ("get_quota: Running command %s\n", syscmd)); lines = file_lines_pload(syscmd, NULL); + SAFE_FREE(syscmd); if (lines) { char *line = lines[0]; DEBUG (3, ("Read output from set_quota, \"%s\"\n", line)); file_lines_free(lines); - + return 0; } DEBUG (0, ("set_quota_command failed!\n")); diff --git a/source3/lib/util.c b/source3/lib/util.c index 4bb6f57d52..eeaa7ea69c 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -583,80 +583,128 @@ ssize_t message_push_blob(uint8 **outbuf, DATA_BLOB blob) Reduce a file name, removing .. elements. ********************************************************************/ -void dos_clean_name(char *s) +static char *dos_clean_name(TALLOC_CTX *ctx, const char *s) { - char *p=NULL; + char *p = NULL; + char *str = NULL; DEBUG(3,("dos_clean_name [%s]\n",s)); /* remove any double slashes */ - all_string_sub(s, "\\\\", "\\", 0); + str = talloc_all_string_sub(ctx, s, "\\\\", "\\"); + if (!str) { + return NULL; + } /* Remove leading .\\ characters */ - if(strncmp(s, ".\\", 2) == 0) { - trim_string(s, ".\\", NULL); - if(*s == 0) - pstrcpy(s,".\\"); + if(strncmp(str, ".\\", 2) == 0) { + trim_string(str, ".\\", NULL); + if(*str == 0) { + str = talloc_strdup(ctx, ".\\"); + if (!str) { + return NULL; + } + } } - while ((p = strstr_m(s,"\\..\\")) != NULL) { - pstring s1; + while ((p = strstr_m(str,"\\..\\")) != NULL) { + char *s1; *p = 0; - pstrcpy(s1,p+3); + s1 = p+3; - if ((p=strrchr_m(s,'\\')) != NULL) + if ((p=strrchr_m(str,'\\')) != NULL) { *p = 0; - else - *s = 0; - pstrcat(s,s1); - } + } else { + *str = 0; + } + str = talloc_asprintf(ctx, + "%s%s", + str, + s1); + if (!str) { + return NULL; + } + } - trim_string(s,NULL,"\\.."); - all_string_sub(s, "\\.\\", "\\", 0); + trim_string(str,NULL,"\\.."); + return talloc_all_string_sub(ctx, str, "\\.\\", "\\"); } /******************************************************************* - Reduce a file name, removing .. elements. + Reduce a file name, removing .. elements. ********************************************************************/ -void unix_clean_name(char *s) +char *unix_clean_name(TALLOC_CTX *ctx, const char *s) { - char *p=NULL; + char *p = NULL; + char *str = NULL; DEBUG(3,("unix_clean_name [%s]\n",s)); /* remove any double slashes */ - all_string_sub(s, "//","/", 0); + str = talloc_all_string_sub(ctx, s, "//","/"); + if (!str) { + return NULL; + } /* Remove leading ./ characters */ - if(strncmp(s, "./", 2) == 0) { - trim_string(s, "./", NULL); - if(*s == 0) - pstrcpy(s,"./"); + if(strncmp(str, "./", 2) == 0) { + trim_string(str, "./", NULL); + if(*str == 0) { + str = talloc_strdup(ctx, "./"); + if (!str) { + return NULL; + } + } } - while ((p = strstr_m(s,"/../")) != NULL) { - pstring s1; + while ((p = strstr_m(str,"/../")) != NULL) { + char *s1; *p = 0; - pstrcpy(s1,p+3); + s1 = p+3; - if ((p=strrchr_m(s,'/')) != NULL) + if ((p=strrchr_m(str,'/')) != NULL) { *p = 0; - else - *s = 0; - pstrcat(s,s1); - } + } else { + *str = 0; + } + str = talloc_asprintf(ctx, + "%s%s", + str, + s1); + if (!str) { + return NULL; + } + } - trim_string(s,NULL,"/.."); - all_string_sub(s, "/./", "/", 0); + trim_string(str,NULL,"/.."); + return talloc_all_string_sub(ctx, str, "/./", "/"); } -void clean_name(char *s) +char *clean_name(TALLOC_CTX *ctx, const char *s) { - dos_clean_name(s); - unix_clean_name(s); + char *str = dos_clean_name(ctx, s); + if (!str) { + return NULL; + } + return unix_clean_name(ctx, str); +} + +/******************************************************************* + Horrible temporary hack until pstring is dead. +********************************************************************/ + +char *pstring_clean_name(pstring s) +{ + char *str = clean_name(NULL,s); + if (!str) { + return NULL; + } + pstrcpy(s, str); + TALLOC_FREE(str); + return s; } /******************************************************************* @@ -911,9 +959,9 @@ void become_daemon(bool Fork, bool no_process_group) Put up a yes/no prompt. ****************************************************************************/ -bool yesno(char *p) +bool yesno(const char *p) { - pstring ans; + char ans[20]; printf("%s",p); if (!fgets(ans,sizeof(ans)-1,stdin)) @@ -1250,23 +1298,22 @@ int interpret_protocol(const char *str,int def) /****************************************************************** Remove any mount options such as -rsize=2048,wsize=2048 etc. Based on a fix from <Thomas.Hepper@icem.de>. + Returns a malloc'ed string. *******************************************************************/ -static void strip_mount_options( pstring *str) +static char *strip_mount_options(const char *str) { - if (**str == '-') { - char *p = *str; + if (*str == '-') { + char *p = str; while(*p && !isspace(*p)) p++; while(*p && isspace(*p)) p++; if(*p) { - pstring tmp_str; - - pstrcpy(tmp_str, p); - pstrcpy(*str, tmp_str); + return SMB_STRDUP(p); } } + return NULL; } /******************************************************************* @@ -1288,6 +1335,7 @@ char *automount_lookup(const char *user_name) nis_result *result; nis_object *object; entry_obj *entry; + char *tmpstr = NULL; if (strcmp(user_name, last_key)) { slprintf(buffer, sizeof(buffer)-1, "[key=%s],%s", user_name, nis_map); @@ -1313,7 +1361,11 @@ char *automount_lookup(const char *user_name) nis_freeresult(result); } - strip_mount_options(&last_value); + tmpstr = strip_mount_options(last_value); + if (tmpstr) { + pstrcpy(last_value, tmpstr); + SAFE_FREE(tmpstr); + } DEBUG(4, ("NIS+ Lookup: %s resulted in %s\n", user_name, last_value)); return last_value; @@ -1345,9 +1397,14 @@ char *automount_lookup(const char *user_name) } else { if ((nis_error = yp_match(nis_domain, nis_map, user_name, strlen(user_name), &nis_result, &nis_result_len)) == 0) { + char *tmpstr = NULL; fstrcpy(last_key, user_name); pstrcpy(last_value, nis_result); - strip_mount_options(&last_value); + tmpstr = strip_mount_options(last_value); + if (tmpstr) { + pstrcpy(last_value, tmpstr); + SAFE_FREE(tmpstr); + } } else if(nis_error == YPERR_KEY) { @@ -2103,8 +2160,13 @@ void dump_data_pw(const char *msg, const uchar * data, size_t len) char *tab_depth(int depth) { static pstring spaces; - memset(spaces, ' ', depth * 4); - spaces[depth * 4] = 0; + size_t len = depth * 4; + if (len > sizeof(pstring)-1) { + len = sizeof(pstring)-1; + } + + memset(spaces, ' ', len); + spaces[len] = 0; return spaces; } @@ -2815,46 +2877,6 @@ void *talloc_check_name_abort(const void *ptr, const char *name) return NULL; } - -#ifdef __INSURE__ - -/******************************************************************* -This routine is a trick to immediately catch errors when debugging -with insure. A xterm with a gdb is popped up when insure catches -a error. It is Linux specific. -********************************************************************/ - -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"; - - slprintf(pidstr, sizeof(pidstr)-1, "%d", sys_getpid()); - pstring_sub(cmd, "%d", pidstr); - - if (!fn) { - 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); - - system(cmd); - - return ret; -} -#endif - uint32 map_share_mode_to_deny_mode(uint32 share_access, uint32 private_options) { switch (share_access & ~FILE_SHARE_DELETE) { diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 673a15df38..b628b06cc6 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -105,9 +105,9 @@ static char *file_pload(char *syscmd, size_t *size) { int fd, n; char *p; - pstring buf; + char buf[1024]; size_t total; - + fd = sys_popen(syscmd); if (fd == -1) { return NULL; diff --git a/source3/lib/util_unistr.c b/source3/lib/util_unistr.c index c4569e102e..8fad1162ac 100644 --- a/source3/lib/util_unistr.c +++ b/source3/lib/util_unistr.c @@ -81,12 +81,15 @@ void load_case_tables(void) static int initialised; char *old_locale = NULL, *saved_locale = NULL; int i; + TALLOC_CTX *frame = NULL; if (initialised) { return; } initialised = 1; + frame = talloc_stackframe(); + upcase_table = (smb_ucs2_t *)map_file(data_path("upcase.dat"), 0x20000); upcase_table_use_unmap = ( upcase_table != NULL ); @@ -147,6 +150,7 @@ void load_case_tables(void) SAFE_FREE(saved_locale); } #endif + TALLOC_FREE(frame); } /* @@ -157,7 +161,7 @@ void load_case_tables(void) int check_dos_char(smb_ucs2_t c) { lazy_initialize_conv(); - + /* Find the right byte, and right bit within the byte; return * 1 or 0 */ return (doschar_table[(c & 0xffff) / 8] & (1 << (c & 7))) != 0; @@ -329,26 +333,54 @@ int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src) * copy because I don't really know how pull_ucs2 and friends calculate the * target size. If this turns out to be a major bottleneck someone with deeper * multi-byte knowledge needs to revisit this. + * I just did (JRA :-). No longer uses copy. * My (VL) use is dsr_getdcname, which returns 6 strings, the alternative would * have been to manually talloc_strdup them in rpc_client/cli_netlogon.c. */ -char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *mem_ctx, const UNISTR2 *src) +char *rpcstr_pull_unistr2_talloc(TALLOC_CTX *ctx, const UNISTR2 *src) { - pstring tmp; - size_t result; - - result = pull_ucs2(NULL, tmp, src->buffer, sizeof(tmp), - src->uni_str_len * 2, 0); - if (result == (size_t)-1) { + char *dest = NULL; + size_t dest_len = convert_string_talloc(ctx, + CH_UTF16LE, + CH_UNIX, + src->buffer, + src->uni_str_len * 2, + (void **)&dest, + true); + if (dest_len == (size_t)-1) { return NULL; } - return talloc_strdup(mem_ctx, tmp); + /* Ensure we're returning a null terminated string. */ + if (dest_len) { + /* Did we already process the terminating zero ? */ + if (dest[dest_len-1] != 0) { + size_t size = talloc_get_size(dest); + /* Have we got space to append the '\0' ? */ + if (size <= dest_len) { + /* No, realloc. */ + dest = TALLOC_REALLOC_ARRAY(ctx, dest, char, + dest_len+1); + if (!dest) { + /* talloc fail. */ + dest_len = (size_t)-1; + return NULL; + } + } + /* Yay - space ! */ + dest[dest_len] = '\0'; + dest_len++; + } + } else if (dest) { + dest[0] = 0; + } + + return dest; } /* Converts a string from internal samba format to unicode - */ + */ int rpcstr_push(void *dest, const char *src, size_t dest_len, int flags) { |