ctdb-daemon: Do not allow database detach if AllowClientDBAttach=1
[Samba.git] / source3 / lib / substitute_generic.c
blobf24608eb96cfac66f059d14cad5190631d0fe3f8
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-2001
6 Copyright (C) Simo Sorce 2001-2002
7 Copyright (C) Martin Pool 2003
8 Copyright (C) James Peach 2006
9 Copyright (C) Jeremy Allison 1992-2007
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
27 void fstring_sub(char *s,const char *pattern,const char *insert)
29 string_sub(s, pattern, insert, sizeof(fstring));
32 /**
33 Similar to string_sub2, but it will accept only allocated strings
34 and may realloc them so pay attention at what you pass on no
35 pointers inside strings, no const may be passed
36 as string.
37 **/
39 char *realloc_string_sub2(char *string,
40 const char *pattern,
41 const char *insert,
42 bool remove_unsafe_characters,
43 bool allow_trailing_dollar)
45 char *p, *in;
46 char *s;
47 ssize_t ls,lp,li,ld, i;
49 if (!insert || !pattern || !*pattern || !string || !*string)
50 return NULL;
52 s = string;
54 in = talloc_strdup(talloc_tos(), insert);
55 if (!in) {
56 DEBUG(0, ("realloc_string_sub: out of memory!\n"));
57 return NULL;
59 ls = (ssize_t)strlen(s);
60 lp = (ssize_t)strlen(pattern);
61 li = (ssize_t)strlen(insert);
62 ld = li - lp;
63 for (i=0;i<li;i++) {
64 switch (in[i]) {
65 case '$':
66 /* allow a trailing $
67 * (as in machine accounts) */
68 if (allow_trailing_dollar && (i == li - 1 )) {
69 break;
71 case '`':
72 case '"':
73 case '\'':
74 case ';':
75 case '%':
76 case '\r':
77 case '\n':
78 if ( remove_unsafe_characters ) {
79 in[i] = '_';
80 break;
82 default:
83 /* ok */
84 break;
88 while ((p = strstr_m(s,pattern))) {
89 if (ld > 0) {
90 int offset = PTR_DIFF(s,string);
91 string = talloc_realloc(NULL, string, char, ls + ld + 1);
92 if (!string) {
93 DEBUG(0, ("realloc_string_sub: "
94 "out of memory!\n"));
95 talloc_free(in);
96 return NULL;
98 p = string + offset + (p - s);
100 if (li != lp) {
101 memmove(p+li,p+lp,strlen(p+lp)+1);
103 memcpy(p, in, li);
104 s = p + li;
105 ls += ld;
107 talloc_free(in);
108 return string;
111 char *realloc_string_sub(char *string,
112 const char *pattern,
113 const char *insert)
115 return realloc_string_sub2(string, pattern, insert, true, false);