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