Merge commit 'release-4-0-0alpha15' into master4-tmp
[nivanova/samba-autobuild/.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 = "";
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         sub_peername = SMB_STRDUP(peername);
212         if (sub_peername == NULL) {
213                 sub_peername = sub_peeraddr;
214         }
215
216         /*
217          * Shouldn't we do the ::ffff: cancellation here as well? The
218          * original code in alloc_sub_basic() did not do it, so I'm
219          * leaving it out here as well for compatibility.
220          */
221         strlcpy(sub_sockaddr, sockaddr, sizeof(sub_sockaddr));
222 }
223
224 static const char *get_smb_user_name(void)
225 {
226         return smb_user_name ? smb_user_name : "";
227 }
228
229 /*******************************************************************
230  Setup the strings used by substitutions. Called per packet. Ensure
231  %U name is set correctly also.
232
233  smb_name must be sanitized by alpha_strcpy
234 ********************************************************************/
235
236 void set_current_user_info(const char *smb_name, const char *unix_name,
237                            const char *domain)
238 {
239         fstrcpy(current_user_info.smb_name, smb_name);
240         fstrcpy(current_user_info.unix_name, unix_name);
241         fstrcpy(current_user_info.domain, domain);
242
243         /* The following is safe as current_user_info.smb_name
244          * has already been sanitised in register_existing_vuid. */
245
246         sub_set_smb_name(current_user_info.smb_name);
247 }
248
249 /*******************************************************************
250  Return the current active user name.
251 *******************************************************************/
252
253 const char *get_current_username(void)
254 {
255         if (current_user_info.smb_name[0] == '\0' ) {
256                 return get_smb_user_name();
257         }
258
259         return current_user_info.smb_name;
260 }
261
262 /*******************************************************************
263  Given a pointer to a %$(NAME) in p and the whole string in str
264  expand it as an environment variable.
265  Return a new allocated and expanded string.
266  Based on code by Branko Cibej <branko.cibej@hermes.si>
267  When this is called p points at the '%' character.
268  May substitute multiple occurrencies of the same env var.
269 ********************************************************************/
270
271 static char * realloc_expand_env_var(char *str, char *p)
272 {
273         char *envname;
274         char *envval;
275         char *q, *r;
276         int copylen;
277
278         if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
279                 return str;
280         }
281
282         /*
283          * Look for the terminating ')'.
284          */
285
286         if ((q = strchr_m(p,')')) == NULL) {
287                 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
288                 return str;
289         }
290
291         /*
292          * Extract the name from within the %$(NAME) string.
293          */
294
295         r = p + 3;
296         copylen = q - r;
297
298         /* reserve space for use later add %$() chars */
299         if ( (envname = (char *)SMB_MALLOC(copylen + 1 + 4)) == NULL ) {
300                 return NULL;
301         }
302
303         strncpy(envname,r,copylen);
304         envname[copylen] = '\0';
305
306         if ((envval = getenv(envname)) == NULL) {
307                 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
308                 SAFE_FREE(envname);
309                 return str;
310         }
311
312         /*
313          * Copy the full %$(NAME) into envname so it
314          * can be replaced.
315          */
316
317         copylen = q + 1 - p;
318         strncpy(envname,p,copylen);
319         envname[copylen] = '\0';
320         r = realloc_string_sub(str, envname, envval);
321         SAFE_FREE(envname);
322
323         return r;
324 }
325
326 /*******************************************************************
327  Patch from jkf@soton.ac.uk
328  Added this to implement %p (NIS auto-map version of %H)
329 *******************************************************************/
330
331 static const char *automount_path(const char *user_name)
332 {
333         TALLOC_CTX *ctx = talloc_tos();
334         const char *server_path;
335
336         /* use the passwd entry as the default */
337         /* this will be the default if WITH_AUTOMOUNT is not used or fails */
338
339         server_path = talloc_strdup(ctx, get_user_home_dir(ctx, user_name));
340         if (!server_path) {
341                 return "";
342         }
343
344 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
345
346         if (lp_nis_home_map()) {
347                 const char *home_path_start;
348                 char *automount_value = automount_lookup(ctx, user_name);
349
350                 if(automount_value && strlen(automount_value) > 0) {
351                         home_path_start = strchr_m(automount_value,':');
352                         if (home_path_start != NULL) {
353                                 DEBUG(5, ("NIS lookup succeeded. "
354                                         "Home path is: %s\n",
355                                         home_path_start ?
356                                                 (home_path_start+1):""));
357                                 server_path = talloc_strdup(ctx,
358                                                         home_path_start+1);
359                                 if (!server_path) {
360                                         server_path = "";
361                                 }
362                         }
363                 } else {
364                         /* NIS key lookup failed: default to
365                          * user home directory from password file */
366                         DEBUG(5, ("NIS lookup failed. Using Home path from "
367                         "passwd file. Home path is: %s\n", server_path ));
368                 }
369         }
370 #endif
371
372         DEBUG(4,("Home server path: %s\n", server_path));
373         return server_path;
374 }
375
376 /*******************************************************************
377  Patch from jkf@soton.ac.uk
378  This is Luke's original function with the NIS lookup code
379  moved out to a separate function.
380 *******************************************************************/
381
382 static const char *automount_server(const char *user_name)
383 {
384         TALLOC_CTX *ctx = talloc_tos();
385         const char *server_name;
386         const char *local_machine_name = get_local_machine_name();
387
388         /* use the local machine name as the default */
389         /* this will be the default if WITH_AUTOMOUNT is not used or fails */
390         if (local_machine_name && *local_machine_name) {
391                 server_name = talloc_strdup(ctx, local_machine_name);
392         } else {
393                 server_name = talloc_strdup(ctx, lp_netbios_name());
394         }
395
396         if (!server_name) {
397                 return "";
398         }
399
400 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
401         if (lp_nis_home_map()) {
402                 char *p;
403                 char *srv;
404                 char *automount_value = automount_lookup(ctx, user_name);
405                 if (!automount_value) {
406                         return "";
407                 }
408                 srv = talloc_strdup(ctx, automount_value);
409                 if (!srv) {
410                         return "";
411                 }
412                 p = strchr_m(srv, ':');
413                 if (!p) {
414                         return "";
415                 }
416                 *p = '\0';
417                 server_name = srv;
418                 DEBUG(5, ("NIS lookup succeeded.  Home server %s\n",
419                                         server_name));
420         }
421 #endif
422
423         DEBUG(4,("Home server: %s\n", server_name));
424         return server_name;
425 }
426
427 /****************************************************************************
428  Do some standard substitutions in a string.
429  len is the length in bytes of the space allowed in string str. If zero means
430  don't allow expansions.
431 ****************************************************************************/
432
433 void standard_sub_basic(const char *smb_name, const char *domain_name,
434                         char *str, size_t len)
435 {
436         char *s;
437
438         if ( (s = alloc_sub_basic( smb_name, domain_name, str )) != NULL ) {
439                 strncpy( str, s, len );
440         }
441
442         SAFE_FREE( s );
443 }
444
445 /****************************************************************************
446  Do some standard substitutions in a string.
447  This function will return an allocated string that have to be freed.
448 ****************************************************************************/
449
450 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name,
451                        const char *domain_name, const char *str)
452 {
453         char *a, *t;
454
455         if ( (a = alloc_sub_basic(smb_name, domain_name, str)) == NULL ) {
456                 return NULL;
457         }
458         t = talloc_strdup(mem_ctx, a);
459         SAFE_FREE(a);
460         return t;
461 }
462
463 /****************************************************************************
464 ****************************************************************************/
465
466 static char *alloc_sub_basic(const char *smb_name, const char *domain_name,
467                              const char *str)
468 {
469         char *b, *p, *s, *r, *a_string;
470         fstring pidstr, vnnstr;
471         const char *local_machine_name = get_local_machine_name();
472         TALLOC_CTX *tmp_ctx = NULL;
473
474         /* workaround to prevent a crash while looking at bug #687 */
475
476         if (!str) {
477                 DEBUG(0,("alloc_sub_basic: NULL source string!  This should not happen\n"));
478                 return NULL;
479         }
480
481         a_string = SMB_STRDUP(str);
482         if (a_string == NULL) {
483                 DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
484                 return NULL;
485         }
486
487         tmp_ctx = talloc_stackframe();
488
489         for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
490
491                 r = NULL;
492                 b = a_string;
493
494                 switch (*(p+1)) {
495                 case 'U' : 
496                         r = strlower_talloc(tmp_ctx, smb_name);
497                         if (r == NULL) {
498                                 goto error;
499                         }
500                         a_string = realloc_string_sub(a_string, "%U", r);
501                         break;
502                 case 'G' : {
503                         struct passwd *pass;
504                         r = talloc_strdup(tmp_ctx, smb_name);
505                         if (r == NULL) {
506                                 goto error;
507                         }
508                         pass = Get_Pwnam_alloc(tmp_ctx, r);
509                         if (pass != NULL) {
510                                 a_string = realloc_string_sub(
511                                         a_string, "%G",
512                                         gidtoname(pass->pw_gid));
513                         }
514                         TALLOC_FREE(pass);
515                         break;
516                 }
517                 case 'D' :
518                         r = strupper_talloc(tmp_ctx, domain_name);
519                         if (r == NULL) {
520                                 goto error;
521                         }
522                         a_string = realloc_string_sub(a_string, "%D", r);
523                         break;
524                 case 'I' : {
525                         a_string = realloc_string_sub(
526                                 a_string, "%I",
527                                 sub_peeraddr[0] ? sub_peeraddr : "0.0.0.0");
528                         break;
529                 }
530                 case 'i': 
531                         a_string = realloc_string_sub(
532                                 a_string, "%i",
533                                 sub_sockaddr[0] ? sub_sockaddr : "0.0.0.0");
534                         break;
535                 case 'L' : 
536                         if ( strncasecmp_m(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
537                                 break;
538                         }
539                         if (local_machine_name && *local_machine_name) {
540                                 a_string = realloc_string_sub(a_string, "%L", local_machine_name); 
541                         } else {
542                                 a_string = realloc_string_sub(a_string, "%L", lp_netbios_name());
543                         }
544                         break;
545                 case 'N':
546                         a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
547                         break;
548                 case 'M' :
549                         a_string = realloc_string_sub(a_string, "%M",
550                                                       sub_peername);
551                         break;
552                 case 'R' :
553                         a_string = realloc_string_sub(a_string, "%R", remote_proto);
554                         break;
555                 case 'T' :
556                         a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False));
557                         break;
558                 case 'a' :
559                         a_string = realloc_string_sub(a_string, "%a",
560                                         get_remote_arch_str());
561                         break;
562                 case 'd' :
563                         slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
564                         a_string = realloc_string_sub(a_string, "%d", pidstr);
565                         break;
566                 case 'h' :
567                         a_string = realloc_string_sub(a_string, "%h", myhostname());
568                         break;
569                 case 'm' :
570                         a_string = realloc_string_sub(a_string, "%m",
571                                                       remote_machine
572                                                       ? remote_machine
573                                                       : "");
574                         break;
575                 case 'v' :
576                         a_string = realloc_string_sub(a_string, "%v", samba_version_string());
577                         break;
578                 case 'w' :
579                         a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
580                         break;
581                 case '$' :
582                         a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
583                         break;
584                 case 'V' :
585                         slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
586                         a_string = realloc_string_sub(a_string, "%V", vnnstr);
587                         break;
588                 default: 
589                         break;
590                 }
591
592                 p++;
593                 TALLOC_FREE(r);
594
595                 if (a_string == NULL) {
596                         goto done;
597                 }
598         }
599
600         goto done;
601
602 error:
603         SAFE_FREE(a_string);
604
605 done:
606         TALLOC_FREE(tmp_ctx);
607         return a_string;
608 }
609
610 /****************************************************************************
611  Do some specific substitutions in a string.
612  This function will return an allocated string that have to be freed.
613 ****************************************************************************/
614
615 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
616                         const char *input_string,
617                         const char *username,
618                         const char *domain,
619                         uid_t uid,
620                         gid_t gid)
621 {
622         char *a_string;
623         char *ret_string = NULL;
624         char *b, *p, *s;
625         TALLOC_CTX *tmp_ctx;
626
627         if (!(tmp_ctx = talloc_new(mem_ctx))) {
628                 DEBUG(0, ("talloc_new failed\n"));
629                 return NULL;
630         }
631
632         a_string = talloc_strdup(tmp_ctx, input_string);
633         if (a_string == NULL) {
634                 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
635                 goto done;
636         }
637
638         for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
639
640                 b = a_string;
641
642                 switch (*(p+1)) {
643                 case 'U' : 
644                         a_string = talloc_string_sub(
645                                 tmp_ctx, a_string, "%U", username);
646                         break;
647                 case 'u' : 
648                         a_string = talloc_string_sub(
649                                 tmp_ctx, a_string, "%u", username);
650                         break;
651                 case 'G' :
652                         if (gid != -1) {
653                                 a_string = talloc_string_sub(
654                                         tmp_ctx, a_string, "%G",
655                                         gidtoname(gid));
656                         } else {
657                                 a_string = talloc_string_sub(
658                                         tmp_ctx, a_string,
659                                         "%G", "NO_GROUP");
660                         }
661                         break;
662                 case 'g' :
663                         if (gid != -1) {
664                                 a_string = talloc_string_sub(
665                                         tmp_ctx, a_string, "%g",
666                                         gidtoname(gid));
667                         } else {
668                                 a_string = talloc_string_sub(
669                                         tmp_ctx, a_string, "%g", "NO_GROUP");
670                         }
671                         break;
672                 case 'D' :
673                         a_string = talloc_string_sub(tmp_ctx, a_string,
674                                                      "%D", domain);
675                         break;
676                 case 'N' : 
677                         a_string = talloc_string_sub(
678                                 tmp_ctx, a_string, "%N",
679                                 automount_server(username)); 
680                         break;
681                 default: 
682                         break;
683                 }
684
685                 p++;
686                 if (a_string == NULL) {
687                         goto done;
688                 }
689         }
690
691         /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
692          * away with the TALLOC_FREE(tmp_ctx) further down. */
693
694         ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
695
696  done:
697         TALLOC_FREE(tmp_ctx);
698         return ret_string;
699 }
700
701 /****************************************************************************
702 ****************************************************************************/
703
704 static char *alloc_sub_advanced(const char *servicename, const char *user, 
705                          const char *connectpath, gid_t gid, 
706                          const char *smb_name, const char *domain_name,
707                          const char *str)
708 {
709         char *a_string, *ret_string;
710         char *b, *p, *s;
711
712         a_string = SMB_STRDUP(str);
713         if (a_string == NULL) {
714                 DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
715                 return NULL;
716         }
717
718         for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
719
720                 b = a_string;
721
722                 switch (*(p+1)) {
723                 case 'N' :
724                         a_string = realloc_string_sub(a_string, "%N", automount_server(user));
725                         break;
726                 case 'H': {
727                         char *h;
728                         if ((h = get_user_home_dir(talloc_tos(), user)))
729                                 a_string = realloc_string_sub(a_string, "%H", h);
730                         TALLOC_FREE(h);
731                         break;
732                 }
733                 case 'P': 
734                         a_string = realloc_string_sub(a_string, "%P", connectpath); 
735                         break;
736                 case 'S': 
737                         a_string = realloc_string_sub(a_string, "%S", servicename);
738                         break;
739                 case 'g': 
740                         a_string = realloc_string_sub(a_string, "%g", gidtoname(gid)); 
741                         break;
742                 case 'u': 
743                         a_string = realloc_string_sub(a_string, "%u", user); 
744                         break;
745
746                         /* Patch from jkf@soton.ac.uk Left the %N (NIS
747                          * server name) in standard_sub_basic as it is
748                          * a feature for logon servers, hence uses the
749                          * username.  The %p (NIS server path) code is
750                          * here as it is used instead of the default
751                          * "path =" string in [homes] and so needs the
752                          * service name, not the username.  */
753                 case 'p': 
754                         a_string = realloc_string_sub(a_string, "%p",
755                                                       automount_path(servicename)); 
756                         break;
757
758                 default: 
759                         break;
760                 }
761
762                 p++;
763                 if (a_string == NULL) {
764                         return NULL;
765                 }
766         }
767
768         ret_string = alloc_sub_basic(smb_name, domain_name, a_string);
769         SAFE_FREE(a_string);
770         return ret_string;
771 }
772
773 /*
774  * This obviously is inefficient and needs to be merged into
775  * alloc_sub_advanced...
776  */
777
778 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
779                           const char *servicename, const char *user,
780                           const char *connectpath, gid_t gid,
781                           const char *smb_name, const char *domain_name,
782                           const char *str)
783 {
784         char *a, *t;
785
786         if (!(a = alloc_sub_advanced(servicename, user, connectpath, gid,
787                                      smb_name, domain_name, str))) {
788                 return NULL;
789         }
790         t = talloc_strdup(mem_ctx, a);
791         SAFE_FREE(a);
792         return t;
793 }
794
795
796 void standard_sub_advanced(const char *servicename, const char *user,
797                            const char *connectpath, gid_t gid,
798                            const char *smb_name, const char *domain_name,
799                            char *str, size_t len)
800 {
801         char *s;
802
803         s = alloc_sub_advanced(servicename, user, connectpath,
804                                gid, smb_name, domain_name, str);
805
806         if ( s ) {
807                 strncpy( str, s, len );
808                 SAFE_FREE( s );
809         }
810 }
811
812 /****************************************************************************
813  Do some standard substitutions in a string.
814 ****************************************************************************/
815
816 char *standard_sub_conn(TALLOC_CTX *ctx, connection_struct *conn, const char *str)
817 {
818         return talloc_sub_advanced(ctx,
819                                 lp_servicename(SNUM(conn)),
820                                 conn->session_info->unix_name,
821                                 conn->connectpath,
822                                 conn->session_info->utok.gid,
823                                 get_smb_user_name(),
824                                 "",
825                                 str);
826 }
827
828 /******************************************************************************
829  version of standard_sub_basic() for string lists; uses talloc_sub_basic()
830  for the work
831  *****************************************************************************/
832
833 bool str_list_sub_basic( char **list, const char *smb_name,
834                          const char *domain_name )
835 {
836         TALLOC_CTX *ctx = list;
837         char *s, *tmpstr;
838
839         while ( *list ) {
840                 s = *list;
841                 tmpstr = talloc_sub_basic(ctx, smb_name, domain_name, s);
842                 if ( !tmpstr ) {
843                         DEBUG(0,("str_list_sub_basic: "
844                                 "alloc_sub_basic() return NULL!\n"));
845                         return false;
846                 }
847
848                 TALLOC_FREE(*list);
849                 *list = tmpstr;
850
851                 list++;
852         }
853
854         return true;
855 }