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