r11159: Added some const to fix warnings.
[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                 const char *home_path_start;
284                 const 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 (!StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%"))) {
390                                 p++;
391                                 break;
392                         }
393
394                         if (local_machine_name && *local_machine_name) {
395                                 string_sub_once(p, "%L", local_machine_name, l); 
396                         } else {
397                                 pstring temp_name;
398
399                                 pstrcpy(temp_name, global_myname());
400                                 strlower_m(temp_name);
401                                 string_sub_once(p, "%L", temp_name, l); 
402                         }
403                         break;
404                 case 'M' :
405                         string_sub(p,"%M", client_name(),l);
406                         break;
407                 case 'R' :
408                         string_sub(p,"%R", remote_proto,l);
409                         break;
410                 case 'T' :
411                         string_sub(p,"%T", timestring(False),l);
412                         break;
413                 case 'a' :
414                         string_sub(p,"%a", remote_arch,l);
415                         break;
416                 case 'd' :
417                         slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
418                         string_sub(p,"%d", pidstr,l);
419                         break;
420                 case 'h' :
421                         string_sub(p,"%h", myhostname(),l);
422                         break;
423                 case 'm' :
424                         string_sub(p,"%m", get_remote_machine_name(),l);
425                         break;
426                 case 'v' :
427                         string_sub(p,"%v", SAMBA_VERSION_STRING,l);
428                         break;
429                 case '$' :
430                         p += expand_env_var(p,l);
431                         break; /* Expand environment variables */
432                 case '\0': 
433                         p++; 
434                         break; /* don't run off the end of the string */
435                         
436                 default: p+=2; 
437                         break;
438                 }
439         }
440 }
441
442 static void standard_sub_advanced(int snum, const char *user, 
443                                   const char *connectpath, gid_t gid, 
444                                   const char *smb_name, char *str, size_t len)
445 {
446         char *p, *s, *home;
447
448         for (s=str; (p=strchr_m(s, '%'));s=p) {
449                 int l = (int)len - (int)(p-str);
450         
451                 if (l < 0)
452                         l = 0;
453         
454                 switch (*(p+1)) {
455                 case 'N' :
456                         string_sub(p,"%N", automount_server(user),l);
457                         break;
458                 case 'H':
459                         if ((home = get_user_home_dir(user)))
460                                 string_sub(p,"%H",home, l);
461                         else
462                                 p += 2;
463                         break;
464                 case 'P': 
465                         string_sub(p,"%P", connectpath, l); 
466                         break;
467                 case 'S': 
468                         if ( snum != -1 )
469                                 string_sub(p,"%S", lp_servicename(snum), l); 
470                         break;
471                 case 'g': 
472                         string_sub(p,"%g", gidtoname(gid), l); 
473                         break;
474                 case 'u': 
475                         string_sub(p,"%u", user, l); 
476                         break;
477                         
478                         /* Patch from jkf@soton.ac.uk Left the %N (NIS
479                          * server name) in standard_sub_basic as it is
480                          * a feature for logon servers, hence uses the
481                          * username.  The %p (NIS server path) code is
482                          * here as it is used instead of the default
483                          * "path =" string in [homes] and so needs the
484                          * service name, not the username.  */
485                 case 'p': 
486                         if ( snum != -1 )
487                                 string_sub(p,"%p", automount_path(lp_servicename(snum)), l); 
488                         break;
489                 case '\0': 
490                         p++; 
491                         break; /* don't run off the end of the string */
492                         
493                 default: p+=2; 
494                         break;
495                 }
496         }
497
498         standard_sub_basic(smb_name, str, len);
499 }
500
501 /****************************************************************************
502  Do some standard substitutions in a string.
503  This function will return an allocated string that have to be freed.
504 ****************************************************************************/
505
506 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name, const char *str)
507 {
508         char *a, *t;
509         a = alloc_sub_basic(smb_name, str);
510         if (!a) return NULL;
511         t = talloc_strdup(mem_ctx, a);
512         SAFE_FREE(a);
513         return t;
514 }
515
516 char *alloc_sub_basic(const char *smb_name, const char *str)
517 {
518         char *b, *p, *s, *t, *r, *a_string;
519         fstring pidstr;
520         struct passwd *pass;
521         const char *local_machine_name = get_local_machine_name();
522
523         /* workaround to prevent a crash while lookinf at bug #687 */
524         
525         if ( !str ) {
526                 DEBUG(0,("alloc_sub_basic: NULL source string!  This should not happen\n"));
527                 return NULL;
528         }
529         
530         a_string = SMB_STRDUP(str);
531         if (a_string == NULL) {
532                 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
533                 return NULL;
534         }
535         
536         for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
537
538                 r = NULL;
539                 b = t = a_string;
540                 
541                 switch (*(p+1)) {
542                 case 'U' : 
543                         r = strdup_lower(smb_name);
544                         if (r == NULL) goto error;
545                         t = realloc_string_sub(t, "%U", r);
546                         break;
547                 case 'G' :
548                         r = SMB_STRDUP(smb_name);
549                         if (r == NULL) goto error;
550                         if ((pass = Get_Pwnam(r))!=NULL) {
551                                 t = realloc_string_sub(t, "%G", gidtoname(pass->pw_gid));
552                         } 
553                         break;
554                 case 'D' :
555                         r = strdup_upper(current_user_info.domain);
556                         if (r == NULL) goto error;
557                         t = realloc_string_sub(t, "%D", r);
558                         break;
559                 case 'I' :
560                         t = realloc_string_sub(t, "%I", client_addr());
561                         break;
562                 case 'L' : 
563                         if (local_machine_name && *local_machine_name)
564                                 t = realloc_string_sub(t, "%L", local_machine_name); 
565                         else
566                                 t = realloc_string_sub(t, "%L", global_myname()); 
567                         break;
568                 case 'N':
569                         t = realloc_string_sub(t, "%N", automount_server(smb_name));
570                         break;
571                 case 'M' :
572                         t = realloc_string_sub(t, "%M", client_name());
573                         break;
574                 case 'R' :
575                         t = realloc_string_sub(t, "%R", remote_proto);
576                         break;
577                 case 'T' :
578                         t = realloc_string_sub(t, "%T", timestring(False));
579                         break;
580                 case 'a' :
581                         t = realloc_string_sub(t, "%a", remote_arch);
582                         break;
583                 case 'd' :
584                         slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
585                         t = realloc_string_sub(t, "%d", pidstr);
586                         break;
587                 case 'h' :
588                         t = realloc_string_sub(t, "%h", myhostname());
589                         break;
590                 case 'm' :
591                         t = realloc_string_sub(t, "%m", remote_machine);
592                         break;
593                 case 'v' :
594                         t = realloc_string_sub(t, "%v", SAMBA_VERSION_STRING);
595                         break;
596                 case '$' :
597                         t = realloc_expand_env_var(t, p); /* Expand environment variables */
598                         break;
599                         
600                 default: 
601                         break;
602                 }
603
604                 p++;
605                 SAFE_FREE(r);
606                 if (t == NULL) goto error;
607                 a_string = t;
608         }
609
610         return a_string;
611 error:
612         SAFE_FREE(a_string);
613         return NULL;
614 }
615
616 /****************************************************************************
617  Do some specific substitutions in a string.
618  This function will return an allocated string that have to be freed.
619 ****************************************************************************/
620
621 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
622                         const char *input_string,
623                         const char *username,
624                         const char *domain,
625                         uid_t uid,
626                         gid_t gid)
627 {
628         char *a, *t;
629         a = alloc_sub_specified(input_string, username, domain, uid, gid);
630         if (!a) return NULL;
631         t = talloc_strdup(mem_ctx, a);
632         SAFE_FREE(a);
633         return t;
634 }
635
636 char *alloc_sub_specified(const char *input_string,
637                         const char *username,
638                         const char *domain,
639                         uid_t uid,
640                         gid_t gid)
641 {
642         char *a_string, *ret_string;
643         char *b, *p, *s, *t;
644
645         a_string = SMB_STRDUP(input_string);
646         if (a_string == NULL) {
647                 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
648                 return NULL;
649         }
650         
651         for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
652                 
653                 b = t = a_string;
654                 
655                 switch (*(p+1)) {
656                 case 'U' : 
657                         t = realloc_string_sub(t, "%U", username);
658                         break;
659                 case 'u' : 
660                         t = realloc_string_sub(t, "%u", username);
661                         break;
662                 case 'G' :
663                         if (gid != -1) {
664                                 t = realloc_string_sub(t, "%G", gidtoname(gid));
665                         } else {
666                                 t = realloc_string_sub(t, "%G", "NO_GROUP");
667                         }
668                         break;
669                 case 'g' :
670                         if (gid != -1) {
671                                 t = realloc_string_sub(t, "%g", gidtoname(gid));
672                         } else {
673                                 t = realloc_string_sub(t, "%g", "NO_GROUP");
674                         }
675                         break;
676                 case 'D' :
677                         t = realloc_string_sub(t, "%D", domain);
678                         break;
679                 case 'N' : 
680                         t = realloc_string_sub(t, "%N", automount_server(username)); 
681                         break;
682                 default: 
683                         break;
684                 }
685
686                 p++;
687                 if (t == NULL) {
688                         SAFE_FREE(a_string);
689                         return NULL;
690                 }
691                 a_string = t;
692         }
693
694         ret_string = alloc_sub_basic(username, a_string);
695         SAFE_FREE(a_string);
696         return ret_string;
697 }
698
699 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
700                         int snum,
701                         const char *user,
702                         const char *connectpath,
703                         gid_t gid,
704                         const char *smb_name,
705                         const char *str)
706 {
707         char *a, *t;
708         a = alloc_sub_advanced(snum, user, connectpath, gid, smb_name, str);
709         if (!a) return NULL;
710         t = talloc_strdup(mem_ctx, a);
711         SAFE_FREE(a);
712         return t;
713 }
714
715 char *alloc_sub_advanced(int snum, const char *user, 
716                                   const char *connectpath, gid_t gid, 
717                                   const char *smb_name, const char *str)
718 {
719         char *a_string, *ret_string;
720         char *b, *p, *s, *t, *h;
721
722         a_string = SMB_STRDUP(str);
723         if (a_string == NULL) {
724                 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
725                 return NULL;
726         }
727         
728         for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
729                 
730                 b = t = a_string;
731                 
732                 switch (*(p+1)) {
733                 case 'N' :
734                         t = realloc_string_sub(t, "%N", automount_server(user));
735                         break;
736                 case 'H':
737                         if ((h = get_user_home_dir(user)))
738                                 t = realloc_string_sub(t, "%H", h);
739                         break;
740                 case 'P': 
741                         t = realloc_string_sub(t, "%P", connectpath); 
742                         break;
743                 case 'S': 
744                         t = realloc_string_sub(t, "%S", lp_servicename(snum)); 
745                         break;
746                 case 'g': 
747                         t = realloc_string_sub(t, "%g", gidtoname(gid)); 
748                         break;
749                 case 'u': 
750                         t = realloc_string_sub(t, "%u", user); 
751                         break;
752                         
753                         /* Patch from jkf@soton.ac.uk Left the %N (NIS
754                          * server name) in standard_sub_basic as it is
755                          * a feature for logon servers, hence uses the
756                          * username.  The %p (NIS server path) code is
757                          * here as it is used instead of the default
758                          * "path =" string in [homes] and so needs the
759                          * service name, not the username.  */
760                 case 'p': 
761                         t = realloc_string_sub(t, "%p", automount_path(lp_servicename(snum))); 
762                         break;
763                         
764                 default: 
765                         break;
766                 }
767
768                 p++;
769                 if (t == NULL) {
770                         SAFE_FREE(a_string);
771                         return NULL;
772                 }
773                 a_string = t;
774         }
775
776         ret_string = alloc_sub_basic(smb_name, a_string);
777         SAFE_FREE(a_string);
778         return ret_string;
779 }
780
781 /****************************************************************************
782  Do some standard substitutions in a string.
783 ****************************************************************************/
784
785 void standard_sub_conn(connection_struct *conn, char *str, size_t len)
786 {
787         standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
788                         conn->gid, smb_user_name, str, len);
789 }
790
791 char *talloc_sub_conn(TALLOC_CTX *mem_ctx, connection_struct *conn, const char *str)
792 {
793         return talloc_sub_advanced(mem_ctx, SNUM(conn), conn->user,
794                         conn->connectpath, conn->gid,
795                         smb_user_name, str);
796 }
797
798 char *alloc_sub_conn(connection_struct *conn, const char *str)
799 {
800         return alloc_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
801                         conn->gid, smb_user_name, str);
802 }
803
804 /****************************************************************************
805  Like standard_sub but by snum.
806 ****************************************************************************/
807
808 void standard_sub_snum(int snum, char *str, size_t len)
809 {
810         static uid_t cached_uid = -1;
811         static fstring cached_user;
812         /* calling uidtoname() on every substitute would be too expensive, so
813            we cache the result here as nearly every call is for the same uid */
814
815         if (cached_uid != current_user.uid) {
816                 fstrcpy(cached_user, uidtoname(current_user.uid));
817                 cached_uid = current_user.uid;
818         }
819
820         standard_sub_advanced(snum, cached_user, "", current_user.gid,
821                               smb_user_name, str, len);
822 }