diff --git a/src/dird/dird_conf.c b/src/dird/dird_conf.c index 849fcdb2773..89f08f10882 100644 --- a/src/dird/dird_conf.c +++ b/src/dird/dird_conf.c @@ -1231,13 +1231,9 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str) if (bstrcasecmp(item->name, "runscript")) { if (list != NULL) { foreach_alist(runscript, list) { - int len; - POOLMEM *cmdbuf; + POOL_MEM esc; - len = strlen(runscript->command); - cmdbuf = get_pool_memory(PM_NAME); - cmdbuf = check_pool_memory_size(cmdbuf, len * 2); - escape_string(cmdbuf, runscript->command, len); + escape_string(esc, runscript->command, strlen(runscript->command)); /* * Don't print runscript when its inherited from a JobDef. @@ -1252,28 +1248,28 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str) if (runscript->short_form) { if (runscript->when == SCRIPT_Before && /* runbeforejob */ (bstrcmp(runscript->target, ""))) { - Mmsg(temp, "run before job = \"%s\"\n", cmdbuf); + Mmsg(temp, "run before job = \"%s\"\n", esc.c_str()); } else if (runscript->when == SCRIPT_After && /* runafterjob */ runscript->on_success && !runscript->on_failure && !runscript->fail_on_error && bstrcmp(runscript->target, "")) { - Mmsg(temp, "run after job = \"%s\"\n", cmdbuf); + Mmsg(temp, "run after job = \"%s\"\n", esc.c_str()); } else if (runscript->when == SCRIPT_After && /* client run after job */ runscript->on_success && !runscript->on_failure && !runscript->fail_on_error && !bstrcmp(runscript->target, "")) { - Mmsg(temp, "client run after job = \"%s\"\n", cmdbuf); + Mmsg(temp, "client run after job = \"%s\"\n", esc.c_str()); } else if (runscript->when == SCRIPT_Before && /* client run before job */ !bstrcmp(runscript->target, "")) { - Mmsg(temp, "client run before job = \"%s\"\n", cmdbuf); + Mmsg(temp, "client run before job = \"%s\"\n", esc.c_str()); } else if (runscript->when == SCRIPT_After && /* run after failed job */ runscript->on_failure && !runscript->on_success && !runscript->fail_on_error && bstrcmp(runscript->target, "")) { - Mmsg(temp, "run after failed job = \"%s\"\n", cmdbuf); + Mmsg(temp, "run after failed job = \"%s\"\n", esc.c_str()); } indent_config_item(cfg_str, 1, temp.c_str()); } else { @@ -1285,7 +1281,7 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str) cmdstring = (char *)"console"; } - Mmsg(temp, "%s = \"%s\"\n", cmdstring, cmdbuf); + Mmsg(temp, "%s = \"%s\"\n", cmdstring, esc.c_str()); indent_config_item(cfg_str, 2, temp.c_str()); /* @@ -1358,8 +1354,6 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str) indent_config_item(cfg_str, 1, "}\n"); } - - free_pool_memory(cmdbuf); } } /* foreach runscript */ } @@ -1786,12 +1780,6 @@ bool FILESETRES::print_config(POOL_MEM &buff, bool hide_sensitive_data) POOL_MEM temp; const char *p; - /* escape strings */ - POOLMEM *buf; - int len; - buf = get_pool_memory(PM_NAME); - - Dmsg0(200,"FILESETRES::print_config\n"); Mmsg(temp, "FileSet {\n"); @@ -2110,11 +2098,13 @@ bool FILESETRES::print_config(POOL_MEM &buff, bool hide_sensitive_data) * File = entries. */ if (incexe->name_list.size()) { + char *entry; + POOL_MEM esc; + for (int l = 0; l < incexe->name_list.size(); l++) { - len = strlen((char*)incexe->name_list.get(l)); - buf = check_pool_memory_size(buf, len * 2); - escape_string(buf, (char*)incexe->name_list.get(l), len); - Mmsg(temp, "File = \"%s\"\n", buf); + entry = (char *)incexe->name_list.get(l); + escape_string(esc, entry, strlen(entry)); + Mmsg(temp, "File = \"%s\"\n", esc.c_str()); indent_config_item(cfg_str, 2, temp.c_str()); } } @@ -2123,11 +2113,13 @@ bool FILESETRES::print_config(POOL_MEM &buff, bool hide_sensitive_data) * Plugin = entries. */ if (incexe->plugin_list.size()) { + char *entry; + POOL_MEM esc; + for (int l = 0; l < incexe->plugin_list.size(); l++) { - len = strlen((char*)incexe->plugin_list.get(l)); - buf = check_pool_memory_size(buf, len * 2); - escape_string(buf, (char*)incexe->plugin_list.get(l), len); - Mmsg(temp, "Plugin = \"%s\"\n", buf); + entry = (char *)incexe->plugin_list.get(l); + escape_string(esc, entry, strlen(entry)); + Mmsg(temp, "Plugin = \"%s\"\n", esc.c_str()); indent_config_item(cfg_str, 2, temp.c_str()); } } @@ -2158,13 +2150,14 @@ bool FILESETRES::print_config(POOL_MEM &buff, bool hide_sensitive_data) INCEXE *incexe = exclude_items[j]; if (incexe->name_list.size()) { - indent_config_item(cfg_str, 1, "Exclude {\n"); + char *entry; + POOL_MEM esc; + indent_config_item(cfg_str, 1, "Exclude {\n"); for (int k = 0; k < incexe->name_list.size(); k++) { - len = strlen((char*)incexe->name_list.get(k)); - buf = check_pool_memory_size(buf, len * 2); - escape_string(buf, (char*)incexe->name_list.get(k), len); - Mmsg(temp, "File = \"%s\"\n", buf); + entry = (char *)incexe->name_list.get(k); + escape_string(esc, entry, strlen(entry)); + Mmsg(temp, "File = \"%s\"\n", esc.c_str()); indent_config_item(cfg_str, 2, temp.c_str()); } @@ -2173,8 +2166,6 @@ bool FILESETRES::print_config(POOL_MEM &buff, bool hide_sensitive_data) } /* loop over all exclude blocks */ } - free_pool_memory(buf); - pm_strcat(cfg_str, "}\n\n"); pm_strcat(buff, cfg_str.c_str()); diff --git a/src/lib/protos.h b/src/lib/protos.h index cb3fb88ce84..182471a99cd 100644 --- a/src/lib/protos.h +++ b/src/lib/protos.h @@ -384,7 +384,7 @@ void set_tls_enable(TLS_CONTEXT *ctx, bool value); bool get_tls_verify_peer(TLS_CONTEXT *ctx); /* util.c */ -void escape_string(char *snew, char *old, int len); +void escape_string(POOL_MEM &snew, char *old, int len); bool is_buf_zero(char *buf, int len); void lcase(char *str); void bash_spaces(char *str); diff --git a/src/lib/res.c b/src/lib/res.c index 165bd827689..43f3102077d 100644 --- a/src/lib/res.c +++ b/src/lib/res.c @@ -1388,8 +1388,6 @@ static inline void print_config_time(RES_ITEM *item, POOL_MEM &cfg_str) bool MSGSRES::print_config(POOL_MEM &buff, bool hide_sensitive_data) { - int len; - POOLMEM *cmdbuf; POOL_MEM cfg_str; /* configuration as string */ POOL_MEM temp; MSGSRES *msgres; @@ -1401,31 +1399,29 @@ bool MSGSRES::print_config(POOL_MEM &buff, bool hide_sensitive_data) Mmsg(temp, " %s = \"%s\"\n", "Name", msgres->name()); pm_strcat(cfg_str, temp.c_str()); - cmdbuf = get_pool_memory(PM_NAME); if (msgres->mail_cmd) { - len = strlen(msgres->mail_cmd); - cmdbuf = check_pool_memory_size(cmdbuf, len * 2); - escape_string(cmdbuf, msgres->mail_cmd, len); - Mmsg(temp, " MailCommand = \"%s\"\n", cmdbuf); + POOL_MEM esc; + + escape_string(esc, msgres->mail_cmd, strlen(msgres->mail_cmd)); + Mmsg(temp, " MailCommand = \"%s\"\n", esc.c_str()); pm_strcat(cfg_str, temp.c_str()); } if (msgres->operator_cmd) { - len = strlen(msgres->operator_cmd); - cmdbuf = check_pool_memory_size(cmdbuf, len * 2); - escape_string(cmdbuf, msgres->operator_cmd, len); - Mmsg(temp, " OperatorCommand = \"%s\"\n", cmdbuf); + POOL_MEM esc; + + escape_string(esc, msgres->operator_cmd, strlen(msgres->operator_cmd)); + Mmsg(temp, " OperatorCommand = \"%s\"\n", esc.c_str()); pm_strcat(cfg_str, temp.c_str()); } if (msgres->timestamp_format) { - len = strlen(msgres->timestamp_format); - cmdbuf = check_pool_memory_size(cmdbuf, len * 2); - escape_string(cmdbuf, msgres->timestamp_format, len); - Mmsg(temp, " TimestampFormat = \"%s\"\n", cmdbuf); + POOL_MEM esc; + + escape_string(esc, msgres->timestamp_format, strlen(msgres->timestamp_format)); + Mmsg(temp, " TimestampFormat = \"%s\"\n", esc.c_str()); pm_strcat(cfg_str, temp.c_str()); } - free_pool_memory(cmdbuf); for (d = msgres->dest_chain; d; d = d->next) { int nr_set = 0; diff --git a/src/lib/util.c b/src/lib/util.c index 5c064a980db..12568fd2070 100644 --- a/src/lib/util.c +++ b/src/lib/util.c @@ -29,18 +29,18 @@ /* * Various BAREOS Utility subroutines - * */ /* - * Escape special characters in bareos configuration strings - * needed for dumping config strings + * Escape special characters in bareos configuration strings + * needed for dumping config strings */ -void escape_string(char *snew, char *old, int len) +void escape_string(POOL_MEM &snew, char *old, int len) { char *n, *o; - n = snew; + snew.check_size(len * 2); + n = snew.c_str(); o = old; while (len--) { switch (*o) { @@ -75,7 +75,9 @@ void escape_string(char *snew, char *old, int len) *n = 0; } -/* Return true of buffer has all zero bytes */ +/* + * Return true of buffer has all zero bytes + */ bool is_buf_zero(char *buf, int len) { uint64_t *ip; @@ -86,7 +88,10 @@ bool is_buf_zero(char *buf, int len) return false; } ip = (uint64_t *)buf; - /* Optimize by checking uint64_t for zero */ + + /* + * Optimize by checking uint64_t for zero + */ len64 = len / sizeof(uint64_t); for (i=0; i < len64; i++) { if (ip[i] != 0) { @@ -105,7 +110,9 @@ bool is_buf_zero(char *buf, int len) } -/* Convert a string in place to lower case */ +/* + * Convert a string in place to lower case + */ void lcase(char *str) { while (*str) { @@ -116,11 +123,11 @@ void lcase(char *str) } } -/* Convert spaces to non-space character. +/* + * Convert spaces to non-space character. * This makes scanf of fields containing spaces easier. */ -void -bash_spaces(char *str) +void bash_spaces(char *str) { while (*str) { if (*str == ' ') @@ -129,11 +136,11 @@ bash_spaces(char *str) } } -/* Convert spaces to non-space character. +/* + * Convert spaces to non-space character. * This makes scanf of fields containing spaces easier. */ -void -bash_spaces(POOL_MEM &pm) +void bash_spaces(POOL_MEM &pm) { char *str = pm.c_str(); while (*str) { @@ -144,9 +151,10 @@ bash_spaces(POOL_MEM &pm) } -/* Convert non-space characters (0x1) back into spaces */ -void -unbash_spaces(char *str) +/* + * Convert non-space characters (0x1) back into spaces + */ +void unbash_spaces(char *str) { while (*str) { if (*str == 0x1) @@ -155,9 +163,10 @@ unbash_spaces(char *str) } } -/* Convert non-space characters (0x1) back into spaces */ -void -unbash_spaces(POOL_MEM &pm) +/* + * Convert non-space characters (0x1) back into spaces + */ +void unbash_spaces(POOL_MEM &pm) { char *str = pm.c_str(); while (*str) { @@ -199,7 +208,6 @@ char *encode_time(utime_t utime, char *buf) } - /* * Convert a JobStatus code into a human readable form */ @@ -345,7 +353,6 @@ void jobstatus_to_ascii_gui(int JobStatus, char *msg, int maxlen) } } - /* * Convert Job Termination Status into a string */ @@ -380,7 +387,6 @@ const char *job_status_to_str(int stat) return str; } - /* * Convert Job Type into a string */ @@ -432,7 +438,8 @@ const char *job_type_to_str(int type) return str; } -/* Convert ActionOnPurge to string (Truncate, Erase, Destroy) +/* + * Convert ActionOnPurge to string (Truncate, Erase, Destroy) */ char *action_on_purge_to_string(int aop, POOL_MEM &ret) { @@ -524,10 +531,9 @@ const char *volume_status_to_str(const char *status) } -/*********************************************************************** +/* * Encode the mode bits into a 10 character string like LS does - ***********************************************************************/ - + */ char *encode_mode(mode_t mode, char *buf) { char *cp = buf; @@ -575,7 +581,9 @@ int do_shell_expansion(char *name, int name_len) BPIPE *bpipe; const char *shellcmd; - /* Check if any meta characters are present */ + /* + * Check if any meta characters are present + */ len = strlen(meta); for (i = 0; i < len; i++) { if (strchr(name, meta[i])) { @@ -586,7 +594,9 @@ int do_shell_expansion(char *name, int name_len) if (found) { cmd = get_pool_memory(PM_FNAME); line = get_pool_memory(PM_FNAME); - /* look for shell */ + /* + * Look for shell + */ if ((shellcmd = getenv("SHELL")) == NULL) { shellcmd = "/bin/sh"; } @@ -613,14 +623,14 @@ int do_shell_expansion(char *name, int name_len) } #endif - -/* MAKESESSIONKEY -- Generate session key with optional start - key. If mode is TRUE, the key will be - translated to a string, otherwise it is - returned as 16 binary bytes. - - from SpeakFreely by John Walker */ - +/* + * MAKESESSIONKEY -- Generate session key with optional start + * key. If mode is TRUE, the key will be + * translated to a string, otherwise it is + * returned as 16 binary bytes. + * + * from SpeakFreely by John Walker + */ void make_session_key(char *key, char *seed, int mode) { int j, k; @@ -635,13 +645,14 @@ void make_session_key(char *key, char *seed, int mode) bstrncat(s, seed, sizeof(s)); } - /* The following creates a seed for the session key generator - based on a collection of volatile and environment-specific - information unlikely to be vulnerable (as a whole) to an - exhaustive search attack. If one of these items isn't - available on your machine, replace it with something - equivalent or, if you like, just delete it. */ - + /* + * The following creates a seed for the session key generator + * based on a collection of volatile and environment-specific + * information unlikely to be vulnerable (as a whole) to an + * exhaustive search attack. If one of these items isn't + * available on your machine, replace it with something + * equivalent or, if you like, just delete it. + */ #if defined(HAVE_WIN32) { LARGE_INTEGER li;