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