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