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