Fix a really slow memory leak (in master at least). Found by Ira Cooper <ira@wakeful...
[kai/samba.git] / source3 / lib / substitute.c
1 /* 
2    Unix SMB/CIFS implementation.
3    string substitution functions
4    Copyright (C) Andrew Tridgell 1992-2000
5    Copyright (C) Gerald Carter   2006
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21
22 #include "includes.h"
23 #include "system/passwd.h"
24 #include "secrets.h"
25 #include "auth.h"
26
27 static char *alloc_sub_basic(const char *smb_name, const char *domain_name,
28                              const char *str);
29
30 userdom_struct current_user_info;
31 fstring remote_proto="UNKNOWN";
32
33 /**
34  * Set the 'local' machine name
35  * @param local_name the name we are being called
36  * @param if this is the 'final' name for us, not be be changed again
37  */
38
39 static char *local_machine;
40
41 void free_local_machine_name(void)
42 {
43         SAFE_FREE(local_machine);
44 }
45
46 bool set_local_machine_name(const char *local_name, bool perm)
47 {
48         static bool already_perm = false;
49         char *tmp_local_machine = NULL;
50         size_t len;
51
52         if (already_perm) {
53                 return true;
54         }
55
56         tmp_local_machine = SMB_STRDUP(local_name);
57         if (!tmp_local_machine) {
58                 return false;
59         }
60         trim_char(tmp_local_machine,' ',' ');
61
62         SAFE_FREE(local_machine);
63         len = strlen(tmp_local_machine);
64         local_machine = SMB_CALLOC_ARRAY(char, len+1);
65         if (!local_machine) {
66                 SAFE_FREE(tmp_local_machine);
67                 return false;
68         }
69         /* alpha_strcpy includes the space for the terminating nul. */
70         alpha_strcpy(local_machine,tmp_local_machine,
71                         SAFE_NETBIOS_CHARS,len+1);
72         strlower_m(local_machine);
73         SAFE_FREE(tmp_local_machine);
74
75         already_perm = perm;
76
77         return true;
78 }
79
80 const char *get_local_machine_name(void)
81 {
82         if (!local_machine || !*local_machine) {
83                 return lp_netbios_name();
84         }
85
86         return local_machine;
87 }
88
89 /**
90  * Set the 'remote' machine name
91  * @param remote_name the name our client wants to be called by
92  * @param if this is the 'final' name for them, not be be changed again
93  */
94
95 static char *remote_machine;
96
97 bool set_remote_machine_name(const char *remote_name, bool perm)
98 {
99         static bool already_perm = False;
100         char *tmp_remote_machine;
101         size_t len;
102
103         if (already_perm) {
104                 return true;
105         }
106
107         tmp_remote_machine = SMB_STRDUP(remote_name);
108         if (!tmp_remote_machine) {
109                 return false;
110         }
111         trim_char(tmp_remote_machine,' ',' ');
112
113         SAFE_FREE(remote_machine);
114         len = strlen(tmp_remote_machine);
115         remote_machine = SMB_CALLOC_ARRAY(char, len+1);
116         if (!remote_machine) {
117                 SAFE_FREE(tmp_remote_machine);
118                 return false;
119         }
120
121         /* alpha_strcpy includes the space for the terminating nul. */
122         alpha_strcpy(remote_machine,tmp_remote_machine,
123                         SAFE_NETBIOS_CHARS,len+1);
124         strlower_m(remote_machine);
125         SAFE_FREE(tmp_remote_machine);
126
127         already_perm = perm;
128
129         return true;
130 }
131
132 const char *get_remote_machine_name(void)
133 {
134         return remote_machine ? remote_machine : "";
135 }
136
137 /*******************************************************************
138  Setup the string used by %U substitution.
139 ********************************************************************/
140
141 static char *smb_user_name;
142
143 void sub_set_smb_name(const char *name)
144 {
145         char *tmp;
146         size_t len;
147         bool is_machine_account = false;
148
149         /* don't let anonymous logins override the name */
150         if (!name || !*name) {
151                 return;
152         }
153
154         tmp = SMB_STRDUP(name);
155         if (!tmp) {
156                 return;
157         }
158         trim_char(tmp, ' ', ' ');
159         strlower_m(tmp);
160
161         len = strlen(tmp);
162
163         if (len == 0) {
164                 SAFE_FREE(tmp);
165                 return;
166         }
167
168         /* long story but here goes....we have to allow usernames
169            ending in '$' as they are valid machine account names.
170            So check for a machine account and re-add the '$'
171            at the end after the call to alpha_strcpy().   --jerry  */
172
173         if (tmp[len-1] == '$') {
174                 is_machine_account = True;
175         }
176
177         SAFE_FREE(smb_user_name);
178         smb_user_name = SMB_CALLOC_ARRAY(char, len+1);
179         if (!smb_user_name) {
180                 SAFE_FREE(tmp);
181                 return;
182         }
183
184         /* alpha_strcpy includes the space for the terminating nul. */
185         alpha_strcpy(smb_user_name, tmp,
186                         SAFE_NETBIOS_CHARS,
187                         len+1);
188
189         SAFE_FREE(tmp);
190
191         if (is_machine_account) {
192                 len = strlen(smb_user_name);
193                 smb_user_name[len-1] = '$';
194         }
195 }
196
197 static char sub_peeraddr[INET6_ADDRSTRLEN];
198 static const char *sub_peername = NULL;
199 static char sub_sockaddr[INET6_ADDRSTRLEN];
200
201 void sub_set_socket_ids(const char *peeraddr, const char *peername,
202                         const char *sockaddr)
203 {
204         const char *addr = peeraddr;
205
206         if (strnequal(addr, "::ffff:", 7)) {
207                 addr += 7;
208         }
209         strlcpy(sub_peeraddr, addr, sizeof(sub_peeraddr));
210
211         if (sub_peername != NULL &&
212                         sub_peername != sub_peeraddr) {
213                 free(discard_const_p(char,sub_peername));
214                 sub_peername = NULL;
215         }
216         sub_peername = SMB_STRDUP(peername);
217         if (sub_peername == NULL) {
218                 sub_peername = sub_peeraddr;
219         }
220
221         /*
222          * Shouldn't we do the ::ffff: cancellation here as well? The
223          * original code in alloc_sub_basic() did not do it, so I'm
224          * leaving it out here as well for compatibility.
225          */
226         strlcpy(sub_sockaddr, sockaddr, sizeof(sub_sockaddr));
227 }
228
229 static const char *get_smb_user_name(void)
230 {
231         return smb_user_name ? smb_user_name : "";
232 }
233
234 /*******************************************************************
235  Setup the strings used by substitutions. Called per packet. Ensure
236  %U name is set correctly also.
237
238  smb_name must be sanitized by alpha_strcpy
239 ********************************************************************/
240
241 void set_current_user_info(const char *smb_name, const char *unix_name,
242                            const char *domain)
243 {
244         fstrcpy(current_user_info.smb_name, smb_name);
245         fstrcpy(current_user_info.unix_name, unix_name);
246         fstrcpy(current_user_info.domain, domain);
247
248         /* The following is safe as current_user_info.smb_name
249          * has already been sanitised in register_existing_vuid. */
250
251         sub_set_smb_name(current_user_info.smb_name);
252 }
253
254 /*******************************************************************
255  Return the current active user name.
256 *******************************************************************/
257
258 const char *get_current_username(void)
259 {
260         if (current_user_info.smb_name[0] == '\0' ) {
261                 return get_smb_user_name();
262         }
263
264         return current_user_info.smb_name;
265 }
266
267 /*******************************************************************
268  Given a pointer to a %$(NAME) in p and the whole string in str
269  expand it as an environment variable.
270  Return a new allocated and expanded string.
271  Based on code by Branko Cibej <branko.cibej@hermes.si>
272  When this is called p points at the '%' character.
273  May substitute multiple occurrencies of the same env var.
274 ********************************************************************/
275
276 static char * realloc_expand_env_var(char *str, char *p)
277 {
278         char *envname;
279         char *envval;
280         char *q, *r;
281         int copylen;
282
283         if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
284                 return str;
285         }
286
287         /*
288          * Look for the terminating ')'.
289          */
290
291         if ((q = strchr_m(p,')')) == NULL) {
292                 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
293                 return str;
294         }
295
296         /*
297          * Extract the name from within the %$(NAME) string.
298          */
299
300         r = p + 3;
301         copylen = q - r;
302
303         /* reserve space for use later add %$() chars */
304         if ( (envname = (char *)SMB_MALLOC(copylen + 1 + 4)) == NULL ) {
305                 return NULL;
306         }
307
308         strncpy(envname,r,copylen);
309         envname[copylen] = '\0';
310
311         if ((envval = getenv(envname)) == NULL) {
312                 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
313                 SAFE_FREE(envname);
314                 return str;
315         }
316
317         /*
318          * Copy the full %$(NAME) into envname so it
319          * can be replaced.
320          */
321
322         copylen = q + 1 - p;
323         strncpy(envname,p,copylen);
324         envname[copylen] = '\0';
325         r = realloc_string_sub(str, envname, envval);
326         SAFE_FREE(envname);
327
328         return r;
329 }
330
331 /*******************************************************************
332  Patch from jkf@soton.ac.uk
333  Added this to implement %p (NIS auto-map version of %H)
334 *******************************************************************/
335
336 static const char *automount_path(const char *user_name)
337 {
338         TALLOC_CTX *ctx = talloc_tos();
339         const char *server_path;
340
341         /* use the passwd entry as the default */
342         /* this will be the default if WITH_AUTOMOUNT is not used or fails */
343
344         server_path = talloc_strdup(ctx, get_user_home_dir(ctx, user_name));
345         if (!server_path) {
346                 return "";
347         }
348
349 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
350
351         if (lp_nis_home_map()) {
352                 const char *home_path_start;
353                 char *automount_value = automount_lookup(ctx, user_name);
354
355                 if(automount_value && strlen(automount_value) > 0) {
356                         home_path_start = strchr_m(automount_value,':');
357                         if (home_path_start != NULL) {
358                                 DEBUG(5, ("NIS lookup succeeded. "
359                                         "Home path is: %s\n",
360                                         home_path_start ?
361                                                 (home_path_start+1):""));
362                                 server_path = talloc_strdup(ctx,
363                                                         home_path_start+1);
364                                 if (!server_path) {
365                                         server_path = "";
366                                 }
367                         }
368                 } else {
369                         /* NIS key lookup failed: default to
370                          * user home directory from password file */
371                         DEBUG(5, ("NIS lookup failed. Using Home path from "
372                         "passwd file. Home path is: %s\n", server_path ));
373                 }
374         }
375 #endif
376
377         DEBUG(4,("Home server path: %s\n", server_path));
378         return server_path;
379 }
380
381 /*******************************************************************
382  Patch from jkf@soton.ac.uk
383  This is Luke's original function with the NIS lookup code
384  moved out to a separate function.
385 *******************************************************************/
386
387 static const char *automount_server(const char *user_name)
388 {
389         TALLOC_CTX *ctx = talloc_tos();
390         const char *server_name;
391         const char *local_machine_name = get_local_machine_name();
392
393         /* use the local machine name as the default */
394         /* this will be the default if WITH_AUTOMOUNT is not used or fails */
395         if (local_machine_name && *local_machine_name) {
396                 server_name = talloc_strdup(ctx, local_machine_name);
397         } else {
398                 server_name = talloc_strdup(ctx, lp_netbios_name());
399         }
400
401         if (!server_name) {
402                 return "";
403         }
404
405 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
406         if (lp_nis_home_map()) {
407                 char *p;
408                 char *srv;
409                 char *automount_value = automount_lookup(ctx, user_name);
410                 if (!automount_value) {
411                         return "";
412                 }
413                 srv = talloc_strdup(ctx, automount_value);
414                 if (!srv) {
415                         return "";
416                 }
417                 p = strchr_m(srv, ':');
418                 if (!p) {
419                         return "";
420                 }
421                 *p = '\0';
422                 server_name = srv;
423                 DEBUG(5, ("NIS lookup succeeded.  Home server %s\n",
424                                         server_name));
425         }
426 #endif
427
428         DEBUG(4,("Home server: %s\n", server_name));
429         return server_name;
430 }
431
432 /****************************************************************************
433  Do some standard substitutions in a string.
434  len is the length in bytes of the space allowed in string str. If zero means
435  don't allow expansions.
436 ****************************************************************************/
437
438 void standard_sub_basic(const char *smb_name, const char *domain_name,
439                         char *str, size_t len)
440 {
441         char *s;
442
443         if ( (s = alloc_sub_basic( smb_name, domain_name, str )) != NULL ) {
444                 strncpy( str, s, len );
445         }
446
447         SAFE_FREE( s );
448 }
449
450 /****************************************************************************
451  Do some standard substitutions in a string.
452  This function will return an allocated string that have to be freed.
453 ****************************************************************************/
454
455 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name,
456                        const char *domain_name, const char *str)
457 {
458         char *a, *t;
459
460         if ( (a = alloc_sub_basic(smb_name, domain_name, str)) == NULL ) {
461                 return NULL;
462         }
463         t = talloc_strdup(mem_ctx, a);
464         SAFE_FREE(a);
465         return t;
466 }
467
468 /****************************************************************************
469 ****************************************************************************/
470
471 static char *alloc_sub_basic(const char *smb_name, const char *domain_name,
472                              const char *str)
473 {
474         char *b, *p, *s, *r, *a_string;
475         fstring pidstr, vnnstr;
476         const char *local_machine_name = get_local_machine_name();
477         TALLOC_CTX *tmp_ctx = NULL;
478
479         /* workaround to prevent a crash while looking at bug #687 */
480
481         if (!str) {
482                 DEBUG(0,("alloc_sub_basic: NULL source string!  This should not happen\n"));
483                 return NULL;
484         }
485
486         a_string = SMB_STRDUP(str);
487         if (a_string == NULL) {
488                 DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
489                 return NULL;
490         }
491
492         tmp_ctx = talloc_stackframe();
493
494         for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
495
496                 r = NULL;
497                 b = a_string;
498
499                 switch (*(p+1)) {
500                 case 'U' : 
501                         r = strlower_talloc(tmp_ctx, smb_name);
502                         if (r == NULL) {
503                                 goto error;
504                         }
505                         a_string = realloc_string_sub(a_string, "%U", r);
506                         break;
507                 case 'G' : {
508                         struct passwd *pass;
509                         r = talloc_strdup(tmp_ctx, smb_name);
510                         if (r == NULL) {
511                                 goto error;
512                         }
513                         pass = Get_Pwnam_alloc(tmp_ctx, r);
514                         if (pass != NULL) {
515                                 a_string = realloc_string_sub(
516                                         a_string, "%G",
517                                         gidtoname(pass->pw_gid));
518                         }
519                         TALLOC_FREE(pass);
520                         break;
521                 }
522                 case 'D' :
523                         r = strupper_talloc(tmp_ctx, domain_name);
524                         if (r == NULL) {
525                                 goto error;
526                         }
527                         a_string = realloc_string_sub(a_string, "%D", r);
528                         break;
529                 case 'I' : {
530                         a_string = realloc_string_sub(
531                                 a_string, "%I",
532                                 sub_peeraddr[0] ? sub_peeraddr : "0.0.0.0");
533                         break;
534                 }
535                 case 'i': 
536                         a_string = realloc_string_sub(
537                                 a_string, "%i",
538                                 sub_sockaddr[0] ? sub_sockaddr : "0.0.0.0");
539                         break;
540                 case 'L' : 
541                         if ( strncasecmp_m(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
542                                 break;
543                         }
544                         if (local_machine_name && *local_machine_name) {
545                                 a_string = realloc_string_sub(a_string, "%L", local_machine_name); 
546                         } else {
547                                 a_string = realloc_string_sub(a_string, "%L", lp_netbios_name());
548                         }
549                         break;
550                 case 'N':
551                         a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
552                         break;
553                 case 'M' :
554                         a_string = realloc_string_sub(a_string, "%M",
555                                                       sub_peername ? sub_peername : "");
556                         break;
557                 case 'R' :
558                         a_string = realloc_string_sub(a_string, "%R", remote_proto);
559                         break;
560                 case 'T' :
561                         a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False));
562                         break;
563                 case 'a' :
564                         a_string = realloc_string_sub(a_string, "%a",
565                                         get_remote_arch_str());
566                         break;
567                 case 'd' :
568                         slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
569                         a_string = realloc_string_sub(a_string, "%d", pidstr);
570                         break;
571                 case 'h' :
572                         a_string = realloc_string_sub(a_string, "%h", myhostname());
573                         break;
574                 case 'm' :
575                         a_string = realloc_string_sub(a_string, "%m",
576                                                       remote_machine
577                                                       ? remote_machine
578                                                       : "");
579                         break;
580                 case 'v' :
581                         a_string = realloc_string_sub(a_string, "%v", samba_version_string());
582                         break;
583                 case 'w' :
584                         a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
585                         break;
586                 case '$' :
587                         a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
588                         break;
589                 case 'V' :
590                         slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
591                         a_string = realloc_string_sub(a_string, "%V", vnnstr);
592                         break;
593                 default: 
594                         break;
595                 }
596
597                 p++;
598                 TALLOC_FREE(r);
599
600                 if (a_string == NULL) {
601                         goto done;
602                 }
603         }
604
605         goto done;
606
607 error:
608         SAFE_FREE(a_string);
609
610 done:
611         TALLOC_FREE(tmp_ctx);
612         return a_string;
613 }
614
615 /****************************************************************************
616  Do some specific substitutions in a string.
617  This function will return an allocated string that have to be freed.
618 ****************************************************************************/
619
620 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
621                         const char *input_string,
622                         const char *username,
623                         const char *domain,
624                         uid_t uid,
625                         gid_t gid)
626 {
627         char *a_string;
628         char *ret_string = NULL;
629         char *b, *p, *s;
630         TALLOC_CTX *tmp_ctx;
631
632         if (!(tmp_ctx = talloc_new(mem_ctx))) {
633                 DEBUG(0, ("talloc_new failed\n"));
634                 return NULL;
635         }
636
637         a_string = talloc_strdup(tmp_ctx, input_string);
638         if (a_string == NULL) {
639                 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
640                 goto done;
641         }
642
643         for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
644
645                 b = a_string;
646
647                 switch (*(p+1)) {
648                 case 'U' : 
649                         a_string = talloc_string_sub(
650                                 tmp_ctx, a_string, "%U", username);
651                         break;
652                 case 'u' : 
653                         a_string = talloc_string_sub(
654                                 tmp_ctx, a_string, "%u", username);
655                         break;
656                 case 'G' :
657                         if (gid != -1) {
658                                 a_string = talloc_string_sub(
659                                         tmp_ctx, a_string, "%G",
660                                         gidtoname(gid));
661                         } else {
662                                 a_string = talloc_string_sub(
663                                         tmp_ctx, a_string,
664                                         "%G", "NO_GROUP");
665                         }
666                         break;
667                 case 'g' :
668                         if (gid != -1) {
669                                 a_string = talloc_string_sub(
670                                         tmp_ctx, a_string, "%g",
671                                         gidtoname(gid));
672                         } else {
673                                 a_string = talloc_string_sub(
674                                         tmp_ctx, a_string, "%g", "NO_GROUP");
675                         }
676                         break;
677                 case 'D' :
678                         a_string = talloc_string_sub(tmp_ctx, a_string,
679                                                      "%D", domain);
680                         break;
681                 case 'N' : 
682                         a_string = talloc_string_sub(
683                                 tmp_ctx, a_string, "%N",
684                                 automount_server(username)); 
685                         break;
686                 default: 
687                         break;
688                 }
689
690                 p++;
691                 if (a_string == NULL) {
692                         goto done;
693                 }
694         }
695
696         /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
697          * away with the TALLOC_FREE(tmp_ctx) further down. */
698
699         ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
700
701  done:
702         TALLOC_FREE(tmp_ctx);
703         return ret_string;
704 }
705
706 /****************************************************************************
707 ****************************************************************************/
708
709 static char *alloc_sub_advanced(const char *servicename, const char *user, 
710                          const char *connectpath, gid_t gid, 
711                          const char *smb_name, const char *domain_name,
712                          const char *str)
713 {
714         char *a_string, *ret_string;
715         char *b, *p, *s;
716
717         a_string = SMB_STRDUP(str);
718         if (a_string == NULL) {
719                 DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
720                 return NULL;
721         }
722
723         for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
724
725                 b = a_string;
726
727                 switch (*(p+1)) {
728                 case 'N' :
729                         a_string = realloc_string_sub(a_string, "%N", automount_server(user));
730                         break;
731                 case 'H': {
732                         char *h;
733                         if ((h = get_user_home_dir(talloc_tos(), user)))
734                                 a_string = realloc_string_sub(a_string, "%H", h);
735                         TALLOC_FREE(h);
736                         break;
737                 }
738                 case 'P': 
739                         a_string = realloc_string_sub(a_string, "%P", connectpath); 
740                         break;
741                 case 'S': 
742                         a_string = realloc_string_sub(a_string, "%S", servicename);
743                         break;
744                 case 'g': 
745                         a_string = realloc_string_sub(a_string, "%g", gidtoname(gid)); 
746                         break;
747                 case 'u': 
748                         a_string = realloc_string_sub(a_string, "%u", user); 
749                         break;
750
751                         /* Patch from jkf@soton.ac.uk Left the %N (NIS
752                          * server name) in standard_sub_basic as it is
753                          * a feature for logon servers, hence uses the
754                          * username.  The %p (NIS server path) code is
755                          * here as it is used instead of the default
756                          * "path =" string in [homes] and so needs the
757                          * service name, not the username.  */
758                 case 'p': 
759                         a_string = realloc_string_sub(a_string, "%p",
760                                                       automount_path(servicename)); 
761                         break;
762
763                 default: 
764                         break;
765                 }
766
767                 p++;
768                 if (a_string == NULL) {
769                         return NULL;
770                 }
771         }
772
773         ret_string = alloc_sub_basic(smb_name, domain_name, a_string);
774         SAFE_FREE(a_string);
775         return ret_string;
776 }
777
778 /*
779  * This obviously is inefficient and needs to be merged into
780  * alloc_sub_advanced...
781  */
782
783 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
784                           const char *servicename, const char *user,
785                           const char *connectpath, gid_t gid,
786                           const char *smb_name, const char *domain_name,
787                           const char *str)
788 {
789         char *a, *t;
790
791         if (!(a = alloc_sub_advanced(servicename, user, connectpath, gid,
792                                      smb_name, domain_name, str))) {
793                 return NULL;
794         }
795         t = talloc_strdup(mem_ctx, a);
796         SAFE_FREE(a);
797         return t;
798 }
799
800
801 void standard_sub_advanced(const char *servicename, const char *user,
802                            const char *connectpath, gid_t gid,
803                            const char *smb_name, const char *domain_name,
804                            char *str, size_t len)
805 {
806         char *s;
807
808         s = alloc_sub_advanced(servicename, user, connectpath,
809                                gid, smb_name, domain_name, str);
810
811         if ( s ) {
812                 strncpy( str, s, len );
813                 SAFE_FREE( s );
814         }
815 }
816
817 /****************************************************************************
818  Do some standard substitutions in a string.
819 ****************************************************************************/
820
821 char *standard_sub_conn(TALLOC_CTX *ctx, connection_struct *conn, const char *str)
822 {
823         /* Make clear that we require the optional unix_token and unix_info in the source3 code */
824         SMB_ASSERT(conn->session_info->unix_token);
825         SMB_ASSERT(conn->session_info->unix_info);
826         return talloc_sub_advanced(ctx,
827                                 lp_servicename(SNUM(conn)),
828                                 conn->session_info->unix_info->unix_name,
829                                 conn->connectpath,
830                                 conn->session_info->unix_token->gid,
831                                 get_smb_user_name(),
832                                 "",
833                                 str);
834 }
835
836 /******************************************************************************
837  version of standard_sub_basic() for string lists; uses talloc_sub_basic()
838  for the work
839  *****************************************************************************/
840
841 bool str_list_sub_basic( char **list, const char *smb_name,
842                          const char *domain_name )
843 {
844         TALLOC_CTX *ctx = list;
845         char *s, *tmpstr;
846
847         while ( *list ) {
848                 s = *list;
849                 tmpstr = talloc_sub_basic(ctx, smb_name, domain_name, s);
850                 if ( !tmpstr ) {
851                         DEBUG(0,("str_list_sub_basic: "
852                                 "alloc_sub_basic() return NULL!\n"));
853                         return false;
854                 }
855
856                 TALLOC_FREE(*list);
857                 *list = tmpstr;
858
859                 list++;
860         }
861
862         return true;
863 }