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