summaryrefslogtreecommitdiff
path: root/lib/util
diff options
context:
space:
mode:
authorAndrew Bartlett <abartlet@samba.org>2011-04-29 13:20:51 +1000
committerAndrew Bartlett <abartlet@samba.org>2011-04-29 16:38:15 +1000
commit93ace5cc2484b53fc33d4689ccc286defbe2b728 (patch)
tree8d1ccae212c54bc149fa6772c8ddaf0c0bb1c4c5 /lib/util
parent9a9124b08760a6235059f517b9a138337754cd02 (diff)
downloadsamba-93ace5cc2484b53fc33d4689ccc286defbe2b728.tar.gz
samba-93ace5cc2484b53fc33d4689ccc286defbe2b728.tar.bz2
samba-93ace5cc2484b53fc33d4689ccc286defbe2b728.zip
lib/util Re-merge the string_sub() and all_string_sub() from source3
Andrew Bartlett
Diffstat (limited to 'lib/util')
-rw-r--r--lib/util/substitute.c55
-rw-r--r--lib/util/util.h2
-rwxr-xr-xlib/util/wscript_build4
3 files changed, 46 insertions, 15 deletions
diff --git a/lib/util/substitute.c b/lib/util/substitute.c
index 32945a7213..500d12777f 100644
--- a/lib/util/substitute.c
+++ b/lib/util/substitute.c
@@ -29,18 +29,20 @@
**/
/**
- Substitute a string for a pattern in another string. Make sure there is
+ Substitute a string for a pattern in another string. Make sure there is
enough room!
- This routine looks for pattern in s and replaces it with
- insert. It may do multiple replacements.
+ This routine looks for pattern in s and replaces it with
+ insert. It may do multiple replacements or just one.
Any of " ; ' $ or ` in the insert string are replaced with _
if len==0 then the string cannot be extended. This is different from the old
use of len==0 which was for no length checks to be done.
**/
-_PUBLIC_ void string_sub(char *s, const char *pattern, const char *insert, size_t len)
+static void string_sub2(char *s,const char *pattern, const char *insert, size_t len,
+ bool remove_unsafe_characters, bool replace_once,
+ bool allow_trailing_dollar)
{
char *p;
ssize_t ls, lp, li, i;
@@ -55,9 +57,10 @@ _PUBLIC_ void string_sub(char *s, const char *pattern, const char *insert, size_
if (len == 0)
len = ls + 1; /* len is number of *bytes* */
- while (lp <= ls && (p = strstr(s, pattern))) {
+ while (lp <= ls && (p = strstr_m(s,pattern))) {
if (ls + (li-lp) >= len) {
- DEBUG(0,("ERROR: string overflow by %d in string_sub(%.50s, %d)\n",
+ DEBUG(0,("ERROR: string overflow by "
+ "%d in string_sub(%.50s, %d)\n",
(int)(ls + (li-lp) - len),
pattern, (int)len));
break;
@@ -67,25 +70,50 @@ _PUBLIC_ void string_sub(char *s, const char *pattern, const char *insert, size_
}
for (i=0;i<li;i++) {
switch (insert[i]) {
+ case '$':
+ /* allow a trailing $
+ * (as in machine accounts) */
+ if (allow_trailing_dollar && (i == li - 1 )) {
+ p[i] = insert[i];
+ break;
+ }
case '`':
case '"':
case '\'':
case ';':
- case '$':
case '%':
case '\r':
case '\n':
- p[i] = '_';
- break;
+ if ( remove_unsafe_characters ) {
+ p[i] = '_';
+ /* yes this break should be here
+ * since we want to fall throw if
+ * not replacing unsafe chars */
+ break;
+ }
default:
p[i] = insert[i];
}
}
s = p + li;
ls += (li-lp);
+
+ if (replace_once)
+ break;
}
}
+void string_sub_once(char *s, const char *pattern,
+ const char *insert, size_t len)
+{
+ string_sub2( s, pattern, insert, len, true, true, false );
+}
+
+void string_sub(char *s,const char *pattern, const char *insert, size_t len)
+{
+ string_sub2( s, pattern, insert, len, true, false, false );
+}
+
/**
* Talloc'ed version of string_sub
*/
@@ -146,13 +174,14 @@ _PUBLIC_ void all_string_sub(char *s,const char *pattern,const char *insert, siz
if (!*pattern)
return;
-
+
if (len == 0)
len = ls + 1; /* len is number of *bytes* */
-
- while (lp <= ls && (p = strstr(s,pattern))) {
+
+ while (lp <= ls && (p = strstr_m(s,pattern))) {
if (ls + (li-lp) >= len) {
- DEBUG(0,("ERROR: string overflow by %d in all_string_sub(%.50s, %d)\n",
+ DEBUG(0,("ERROR: string overflow by "
+ "%d in all_string_sub(%.50s, %d)\n",
(int)(ls + (li-lp) - len),
pattern, (int)len));
break;
diff --git a/lib/util/util.h b/lib/util/util.h
index 45779912f3..8bbaa0e393 100644
--- a/lib/util/util.h
+++ b/lib/util/util.h
@@ -284,6 +284,8 @@ _PUBLIC_ char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_
**/
_PUBLIC_ void string_sub(char *s,const char *pattern, const char *insert, size_t len);
+_PUBLIC_ void string_sub_once(char *s, const char *pattern,
+ const char *insert, size_t len);
_PUBLIC_ char *string_sub_talloc(TALLOC_CTX *mem_ctx, const char *s,
const char *pattern, const char *insert);
diff --git a/lib/util/wscript_build b/lib/util/wscript_build
index f1bb9e7493..561dcc4379 100755
--- a/lib/util/wscript_build
+++ b/lib/util/wscript_build
@@ -5,11 +5,11 @@ common_util_sources = '''talloc_stack.c smb_threads.c xfile.c data_blob.c
genrand.c fsusage.c blocking.c become_daemon.c
signal.c system.c params.c util.c util_id.c util_net.c
util_strlist.c idtree.c debug.c fault.c base64.c
- util_str_common.c'''
+ util_str_common.c substitute.c'''
common_util_headers = 'debug.h'
common_util_public_deps = 'talloc pthread LIBCRYPTO'
-s4_util_sources = '''dprintf.c ms_fnmatch.c parmlist.c substitute.c util_str.c'''
+s4_util_sources = '''dprintf.c ms_fnmatch.c parmlist.c util_str.c'''
s4_util_deps = 'DYNCONFIG'
s4_util_public_deps = 'talloc CHARSET execinfo uid_wrapper'
s4_util_public_headers = 'attr.h byteorder.h data_blob.h memory.h safe_string.h time.h talloc_stack.h xfile.h dlinklist.h util.h'