r12313: Introduce yet another copy of the string_sub function:
authorVolker Lendecke <vlendec@samba.org>
Sun, 18 Dec 2005 18:06:15 +0000 (18:06 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 16:05:53 +0000 (11:05 -0500)
talloc_string_sub. Someone with time on his hands could convert all the
callers of all_string_sub to this.

realloc_string_sub is *only* called from within substitute.c, it could be
moved there I think.

Volker
(This used to be commit be6c9012da174d5d5116e5172a53bbe6486d6c38)

source3/auth/auth_util.c
source3/lib/util_str.c
source3/nsswitch/winbindd_pam.c
source3/passdb/pdb_ldap.c

index 497f16adf2bed3e2a94d3db97fa4edbde64ddeae..eb15fff7c8da5fc708ececbd8a7f7eb1a63b84c5 100644 (file)
@@ -591,33 +591,36 @@ static NTSTATUS create_nt_user_token(const DOM_SID *user_sid, const DOM_SID *gro
            (strlen(lp_log_nt_token_command()) > 0)) {
                TALLOC_CTX *mem_ctx;
                char *command;
-               fstring sidstr;
-               char *user_sidstr, *group_sidstr;
+               char *group_sidstr;
 
                mem_ctx = talloc_init("setnttoken");
                if (mem_ctx == NULL)
                        return NT_STATUS_NO_MEMORY;
 
-               sid_to_string(sidstr, &ptoken->user_sids[0]);
-               user_sidstr = talloc_strdup(mem_ctx, sidstr);
-
                group_sidstr = talloc_strdup(mem_ctx, "");
                for (i=1; i<ptoken->num_sids; i++) {
-                       sid_to_string(sidstr, &ptoken->user_sids[i]);
-                       group_sidstr = talloc_asprintf(mem_ctx, "%s %s",
-                                                      group_sidstr, sidstr);
+                       group_sidstr = talloc_asprintf(
+                               mem_ctx, "%s %s", group_sidstr,
+                               sid_string_static(&ptoken->user_sids[i]));
+               }
+
+               command = talloc_string_sub(
+                       mem_ctx, lp_log_nt_token_command(),
+                       "%s", sid_string_static(&ptoken->user_sids[0]));
+               command = talloc_string_sub(
+                       mem_ctx, command, "%t", group_sidstr);
+
+               if (command == NULL) {
+                       talloc_destroy(mem_ctx);
+                       return NT_STATUS_NO_MEMORY;
                }
 
-               command = SMB_STRDUP(lp_log_nt_token_command());
-               command = realloc_string_sub(command, "%s", user_sidstr);
-               command = realloc_string_sub(command, "%t", group_sidstr);
                DEBUG(8, ("running command: [%s]\n", command));
                if (smbrun(command, NULL) != 0) {
                        DEBUG(0, ("Could not log NT token\n"));
                        nt_status = NT_STATUS_ACCESS_DENIED;
                }
                talloc_destroy(mem_ctx);
-               SAFE_FREE(command);
        }
 
        *token = ptoken;
index 9b14dcfaf0faf8b50abe0f93f43da747ab501b5c..80bb2ff2ad7823a0ccc57a26a60ceb7c35dc9c07 100644 (file)
@@ -1003,7 +1003,8 @@ void pstring_sub(char *s,const char *pattern,const char *insert)
  as string.
 **/
 
-char *realloc_string_sub(char *string, const char *pattern, const char *insert)
+char *realloc_string_sub(char *string, const char *pattern,
+                        const char *insert)
 {
        char *p, *in;
        char *s;
@@ -1063,6 +1064,77 @@ char *realloc_string_sub(char *string, const char *pattern, const char *insert)
        return string;
 }
 
+/* Same as string_sub, but returns a talloc'ed string */
+
+char *talloc_string_sub(TALLOC_CTX *mem_ctx, const char *src,
+                       const char *pattern, const char *insert)
+{
+       char *p, *in;
+       char *s;
+       char *string;
+       ssize_t ls,lp,li,ld, i;
+
+       if (!insert || !pattern || !*pattern || !src || !*src)
+               return NULL;
+
+       string = talloc_strdup(mem_ctx, src);
+       if (string == NULL) {
+               DEBUG(0, ("talloc_strdup failed\n"));
+               return NULL;
+       }
+
+       s = string;
+
+       in = SMB_STRDUP(insert);
+       if (!in) {
+               DEBUG(0, ("talloc_string_sub: out of memory!\n"));
+               return NULL;
+       }
+       ls = (ssize_t)strlen(s);
+       lp = (ssize_t)strlen(pattern);
+       li = (ssize_t)strlen(insert);
+       ld = li - lp;
+       for (i=0;i<li;i++) {
+               switch (in[i]) {
+                       case '`':
+                       case '"':
+                       case '\'':
+                       case ';':
+                       case '$':
+                       case '%':
+                       case '\r':
+                       case '\n':
+                               in[i] = '_';
+                       default:
+                               /* ok */
+                               break;
+               }
+       }
+       
+       while ((p = strstr_m(s,pattern))) {
+               if (ld > 0) {
+                       int offset = PTR_DIFF(s,string);
+                       char *t = TALLOC_REALLOC(mem_ctx, string, ls + ld + 1);
+                       if (!t) {
+                               DEBUG(0, ("talloc_string_sub: out of "
+                                         "memory!\n"));
+                               SAFE_FREE(in);
+                               return NULL;
+                       }
+                       string = t;
+                       p = t + offset + (p - s);
+               }
+               if (li != lp) {
+                       memmove(p+li,p+lp,strlen(p+lp)+1);
+               }
+               memcpy(p, in, li);
+               s = p + li;
+               ls += ld;
+       }
+       SAFE_FREE(in);
+       return string;
+}
+
 /**
  Similar to string_sub() but allows for any character to be substituted. 
  Use with caution!
index e683f397b66e82ef7b8db37be1c2a31d593fee6b..1d9b77afee133ab56dca8d3f5875b0ecbc98bce2 100644 (file)
@@ -419,16 +419,21 @@ done:
        if ( NT_STATUS_IS_OK(result) &&
             (state->request.flags & WBFLAG_PAM_AFS_TOKEN) ) {
 
-               char *afsname = SMB_STRDUP(lp_afs_username_map());
+               char *afsname = talloc_strdup(state->mem_ctx,
+                                             lp_afs_username_map());
                char *cell;
 
                if (afsname == NULL) {
                        goto no_token;
                }
 
-               afsname = realloc_string_sub(afsname, "%D", name_domain);
-               afsname = realloc_string_sub(afsname, "%u", name_user);
-               afsname = realloc_string_sub(afsname, "%U", name_user);
+               afsname = talloc_string_sub(state->mem_ctx,
+                                           lp_afs_username_map(),
+                                           "%D", name_domain);
+               afsname = talloc_string_sub(state->mem_ctx, afsname,
+                                           "%u", name_user);
+               afsname = talloc_string_sub(state->mem_ctx, afsname,
+                                           "%U", name_user);
 
                {
                        DOM_SID user_sid;
@@ -437,7 +442,8 @@ done:
                        sid_copy(&user_sid, &info3.dom_sid.sid);
                        sid_append_rid(&user_sid, info3.user_rid);
                        sid_to_string(sidstr, &user_sid);
-                       afsname = realloc_string_sub(afsname, "%s", sidstr);
+                       afsname = talloc_string_sub(state->mem_ctx, afsname,
+                                                   "%s", sidstr);
                }
 
                if (afsname == NULL) {
@@ -466,7 +472,7 @@ done:
                                strlen(state->response.extra_data)+1;
 
        no_token:
-               SAFE_FREE(afsname);
+               talloc_free(afsname);
        }
                
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
index 20cf2d328ec386761dd6958429e561cfd1695c23..cb0bc8eeb68c122932bcb14b6c5fa8f437dfda90 100644 (file)
@@ -3708,8 +3708,7 @@ char *get_ldap_filter(TALLOC_CTX *mem_ctx, const char *username)
        escaped = escape_ldap_string_alloc(username);
        if (escaped == NULL) goto done;
 
-       filter = realloc_string_sub(filter, "%u", username);
-       result = talloc_strdup(mem_ctx, filter);
+       result = talloc_string_sub(mem_ctx, filter, "%u", username);
 
  done:
        SAFE_FREE(filter);