s3-loadparm: Fix resume command typo for "printing = vlp".
[Samba.git] / source / lib / substitute.c
blob6c74f54649f70360608694f03df4f484d713a7fe
1 /*
2 Unix SMB/CIFS implementation.
3 string substitution functions
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Gerald Carter 2006
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.
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.
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/>.
22 #include "includes.h"
24 extern struct current_user current_user;
26 userdom_struct current_user_info;
27 fstring remote_proto="UNKNOWN";
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
35 static char *local_machine;
37 void free_local_machine_name(void)
39 SAFE_FREE(local_machine);
42 bool set_local_machine_name(const char *local_name, bool perm)
44 static bool already_perm = false;
45 char *tmp_local_machine = NULL;
46 char addr[INET6_ADDRSTRLEN];
47 size_t len;
49 tmp_local_machine = SMB_STRDUP(local_name);
50 if (!tmp_local_machine) {
51 return false;
53 trim_char(tmp_local_machine,' ',' ');
56 * Windows NT/2k uses "*SMBSERVER" and XP uses "*SMBSERV"
57 * arrggg!!!
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;
69 if (already_perm) {
70 return true;
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;
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);
86 already_perm = perm;
88 return true;
91 const char *get_local_machine_name(void)
93 if (!local_machine || !*local_machine) {
94 return global_myname();
97 return local_machine;
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
106 static char *remote_machine;
108 bool set_remote_machine_name(const char *remote_name, bool perm)
110 static bool already_perm = False;
111 char *tmp_remote_machine;
112 size_t len;
114 if (already_perm) {
115 return true;
118 tmp_remote_machine = SMB_STRDUP(remote_name);
119 if (!tmp_remote_machine) {
120 return false;
122 trim_char(tmp_remote_machine,' ',' ');
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;
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);
138 already_perm = perm;
140 return true;
143 const char *get_remote_machine_name(void)
145 return remote_machine ? remote_machine : "";
148 /*******************************************************************
149 Setup the string used by %U substitution.
150 ********************************************************************/
152 static char *smb_user_name;
154 void sub_set_smb_name(const char *name)
156 char *tmp;
157 size_t len;
158 bool is_machine_account = false;
160 /* don't let anonymous logins override the name */
161 if (!name || !*name) {
162 return;
165 tmp = SMB_STRDUP(name);
166 if (!tmp) {
167 return;
169 trim_char(tmp, ' ', ' ');
170 strlower_m(tmp);
172 len = strlen(tmp);
174 if (len == 0) {
175 SAFE_FREE(tmp);
176 return;
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 */
184 if (tmp[len-1] == '$') {
185 is_machine_account = True;
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;
195 /* alpha_strcpy includes the space for the terminating nul. */
196 alpha_strcpy(smb_user_name, tmp,
197 SAFE_NETBIOS_CHARS,
198 len+1);
200 SAFE_FREE(tmp);
202 if (is_machine_account) {
203 len = strlen(smb_user_name);
204 smb_user_name[len-1] = '$';
208 static const char *get_smb_user_name(void)
210 return smb_user_name ? smb_user_name : "";
213 /*******************************************************************
214 Setup the strings used by substitutions. Called per packet. Ensure
215 %U name is set correctly also.
216 ********************************************************************/
218 void set_current_user_info(const userdom_struct *pcui)
220 current_user_info = *pcui;
221 /* The following is safe as current_user_info.smb_name
222 * has already been sanitised in register_existing_vuid. */
224 sub_set_smb_name(current_user_info.smb_name);
227 /*******************************************************************
228 Return the current active user name.
229 *******************************************************************/
231 const char *get_current_username(void)
233 if (current_user_info.smb_name[0] == '\0' ) {
234 return get_smb_user_name();
237 return current_user_info.smb_name;
240 /*******************************************************************
241 Given a pointer to a %$(NAME) in p and the whole string in str
242 expand it as an environment variable.
243 Return a new allocated and expanded string.
244 Based on code by Branko Cibej <branko.cibej@hermes.si>
245 When this is called p points at the '%' character.
246 May substitute multiple occurrencies of the same env var.
247 ********************************************************************/
249 static char * realloc_expand_env_var(char *str, char *p)
251 char *envname;
252 char *envval;
253 char *q, *r;
254 int copylen;
256 if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
257 return str;
261 * Look for the terminating ')'.
264 if ((q = strchr_m(p,')')) == NULL) {
265 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
266 return str;
270 * Extract the name from within the %$(NAME) string.
273 r = p + 3;
274 copylen = q - r;
276 /* reserve space for use later add %$() chars */
277 if ( (envname = (char *)SMB_MALLOC(copylen + 1 + 4)) == NULL ) {
278 return NULL;
281 strncpy(envname,r,copylen);
282 envname[copylen] = '\0';
284 if ((envval = getenv(envname)) == NULL) {
285 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
286 SAFE_FREE(envname);
287 return str;
291 * Copy the full %$(NAME) into envname so it
292 * can be replaced.
295 copylen = q + 1 - p;
296 strncpy(envname,p,copylen);
297 envname[copylen] = '\0';
298 r = realloc_string_sub(str, envname, envval);
299 SAFE_FREE(envname);
301 return r;
304 /*******************************************************************
305 *******************************************************************/
307 static char *longvar_domainsid( void )
309 DOM_SID sid;
310 fstring tmp;
311 char *sid_string;
313 if ( !secrets_fetch_domain_sid( lp_workgroup(), &sid ) ) {
314 return NULL;
317 sid_string = SMB_STRDUP( sid_to_fstring( tmp, &sid ) );
319 if ( !sid_string ) {
320 DEBUG(0,("longvar_domainsid: failed to dup SID string!\n"));
323 return sid_string;
326 /*******************************************************************
327 *******************************************************************/
329 struct api_longvar {
330 const char *name;
331 char* (*fn)( void );
334 static struct api_longvar longvar_table[] = {
335 { "DomainSID", longvar_domainsid },
336 { NULL, NULL }
339 static char *get_longvar_val( const char *varname )
341 int i;
343 DEBUG(7,("get_longvar_val: expanding variable [%s]\n", varname));
345 for ( i=0; longvar_table[i].name; i++ ) {
346 if ( strequal( longvar_table[i].name, varname ) ) {
347 return longvar_table[i].fn();
351 return NULL;
354 /*******************************************************************
355 Expand the long smb.conf variable names given a pointer to a %(NAME).
356 Return the number of characters by which the pointer should be advanced.
357 When this is called p points at the '%' character.
358 ********************************************************************/
360 static char *realloc_expand_longvar(char *str, char *p)
362 fstring varname;
363 char *value;
364 char *q, *r;
365 int copylen;
367 if ( p[0] != '%' || p[1] != '(' ) {
368 return str;
371 /* Look for the terminating ')'.*/
373 if ((q = strchr_m(p,')')) == NULL) {
374 DEBUG(0,("realloc_expand_longvar: Unterminated environment variable [%s]\n", p));
375 return str;
378 /* Extract the name from within the %(NAME) string.*/
380 r = p+2;
381 copylen = MIN( (q-r), (sizeof(varname)-1) );
382 strncpy(varname, r, copylen);
383 varname[copylen] = '\0';
385 if ((value = get_longvar_val(varname)) == NULL) {
386 DEBUG(0,("realloc_expand_longvar: Variable [%s] not set. Skipping\n", varname));
387 return str;
390 /* Copy the full %(NAME) into envname so it can be replaced.*/
392 copylen = MIN( (q+1-p),(sizeof(varname)-1) );
393 strncpy( varname, p, copylen );
394 varname[copylen] = '\0';
395 r = realloc_string_sub(str, varname, value);
396 SAFE_FREE( value );
398 /* skip over the %(varname) */
400 return r;
403 /*******************************************************************
404 Patch from jkf@soton.ac.uk
405 Added this to implement %p (NIS auto-map version of %H)
406 *******************************************************************/
408 static const char *automount_path(const char *user_name)
410 TALLOC_CTX *ctx = talloc_tos();
411 const char *server_path;
413 /* use the passwd entry as the default */
414 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
416 server_path = talloc_strdup(ctx, get_user_home_dir(ctx, user_name));
417 if (!server_path) {
418 return "";
421 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
423 if (lp_nis_home_map()) {
424 const char *home_path_start;
425 char *automount_value = automount_lookup(ctx, user_name);
427 if(automount_value && strlen(automount_value) > 0) {
428 home_path_start = strchr_m(automount_value,':');
429 if (home_path_start != NULL) {
430 DEBUG(5, ("NIS lookup succeeded. "
431 "Home path is: %s\n",
432 home_path_start ?
433 (home_path_start+1):""));
434 server_path = talloc_strdup(ctx,
435 home_path_start+1);
436 if (!server_path) {
437 server_path = "";
440 } else {
441 /* NIS key lookup failed: default to
442 * user home directory from password file */
443 DEBUG(5, ("NIS lookup failed. Using Home path from "
444 "passwd file. Home path is: %s\n", server_path ));
447 #endif
449 DEBUG(4,("Home server path: %s\n", server_path));
450 return server_path;
453 /*******************************************************************
454 Patch from jkf@soton.ac.uk
455 This is Luke's original function with the NIS lookup code
456 moved out to a separate function.
457 *******************************************************************/
459 static const char *automount_server(const char *user_name)
461 TALLOC_CTX *ctx = talloc_tos();
462 const char *server_name;
463 const char *local_machine_name = get_local_machine_name();
465 /* use the local machine name as the default */
466 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
467 if (local_machine_name && *local_machine_name) {
468 server_name = talloc_strdup(ctx, local_machine_name);
469 } else {
470 server_name = talloc_strdup(ctx, global_myname());
473 if (!server_name) {
474 return "";
477 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
478 if (lp_nis_home_map()) {
479 char *p;
480 char *srv;
481 char *automount_value = automount_lookup(ctx, user_name);
482 if (!automount_value) {
483 return "";
485 srv = talloc_strdup(ctx, automount_value);
486 if (!srv) {
487 return "";
489 p = strchr_m(srv, ':');
490 if (!p) {
491 return "";
493 *p = '\0';
494 server_name = srv;
495 DEBUG(5, ("NIS lookup succeeded. Home server %s\n",
496 server_name));
498 #endif
500 DEBUG(4,("Home server: %s\n", server_name));
501 return server_name;
504 /****************************************************************************
505 Do some standard substitutions in a string.
506 len is the length in bytes of the space allowed in string str. If zero means
507 don't allow expansions.
508 ****************************************************************************/
510 void standard_sub_basic(const char *smb_name, const char *domain_name,
511 char *str, size_t len)
513 char *s;
515 if ( (s = alloc_sub_basic( smb_name, domain_name, str )) != NULL ) {
516 strncpy( str, s, len );
519 SAFE_FREE( s );
523 /****************************************************************************
524 Do some standard substitutions in a string.
525 This function will return an allocated string that have to be freed.
526 ****************************************************************************/
528 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name,
529 const char *domain_name, const char *str)
531 char *a, *t;
533 if ( (a = alloc_sub_basic(smb_name, domain_name, str)) == NULL ) {
534 return NULL;
536 t = talloc_strdup(mem_ctx, a);
537 SAFE_FREE(a);
538 return t;
541 /****************************************************************************
542 ****************************************************************************/
544 char *alloc_sub_basic(const char *smb_name, const char *domain_name,
545 const char *str)
547 char *b, *p, *s, *r, *a_string;
548 fstring pidstr, vnnstr;
549 char addr[INET6_ADDRSTRLEN];
550 const char *local_machine_name = get_local_machine_name();
551 TALLOC_CTX *tmp_ctx = NULL;
553 /* workaround to prevent a crash while looking at bug #687 */
555 if (!str) {
556 DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
557 return NULL;
560 a_string = SMB_STRDUP(str);
561 if (a_string == NULL) {
562 DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
563 return NULL;
566 tmp_ctx = talloc_stackframe();
568 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
570 r = NULL;
571 b = a_string;
573 switch (*(p+1)) {
574 case 'U' :
575 r = strdup_lower(smb_name);
576 if (r == NULL) {
577 goto error;
579 a_string = realloc_string_sub(a_string, "%U", r);
580 break;
581 case 'G' : {
582 struct passwd *pass;
583 r = SMB_STRDUP(smb_name);
584 if (r == NULL) {
585 goto error;
587 pass = Get_Pwnam_alloc(tmp_ctx, r);
588 if (pass != NULL) {
589 a_string = realloc_string_sub(
590 a_string, "%G",
591 gidtoname(pass->pw_gid));
593 TALLOC_FREE(pass);
594 break;
596 case 'D' :
597 r = strdup_upper(domain_name);
598 if (r == NULL) {
599 goto error;
601 a_string = realloc_string_sub(a_string, "%D", r);
602 break;
603 case 'I' : {
604 int offset = 0;
605 client_addr(get_client_fd(), addr, sizeof(addr));
606 if (strnequal(addr,"::ffff:",7)) {
607 offset = 7;
609 a_string = realloc_string_sub(a_string, "%I",
610 addr + offset);
611 break;
613 case 'i':
614 a_string = realloc_string_sub( a_string, "%i",
615 client_socket_addr(get_client_fd(), addr, sizeof(addr)) );
616 break;
617 case 'L' :
618 if ( StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
619 break;
621 if (local_machine_name && *local_machine_name) {
622 a_string = realloc_string_sub(a_string, "%L", local_machine_name);
623 } else {
624 a_string = realloc_string_sub(a_string, "%L", global_myname());
626 break;
627 case 'N':
628 a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
629 break;
630 case 'M' :
631 a_string = realloc_string_sub(a_string, "%M", client_name(get_client_fd()));
632 break;
633 case 'R' :
634 a_string = realloc_string_sub(a_string, "%R", remote_proto);
635 break;
636 case 'T' :
637 a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False));
638 break;
639 case 'a' :
640 a_string = realloc_string_sub(a_string, "%a",
641 get_remote_arch_str());
642 break;
643 case 'd' :
644 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
645 a_string = realloc_string_sub(a_string, "%d", pidstr);
646 break;
647 case 'h' :
648 a_string = realloc_string_sub(a_string, "%h", myhostname());
649 break;
650 case 'm' :
651 a_string = realloc_string_sub(a_string, "%m",
652 remote_machine
653 ? remote_machine
654 : "");
655 break;
656 case 'v' :
657 a_string = realloc_string_sub(a_string, "%v", SAMBA_VERSION_STRING);
658 break;
659 case 'w' :
660 a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
661 break;
662 case '$' :
663 a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
664 break;
665 case '(':
666 a_string = realloc_expand_longvar( a_string, p );
667 break;
668 case 'V' :
669 slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
670 a_string = realloc_string_sub(a_string, "%V", vnnstr);
671 break;
672 default:
673 break;
676 p++;
677 SAFE_FREE(r);
679 if (a_string == NULL) {
680 goto done;
684 goto done;
686 error:
687 SAFE_FREE(a_string);
689 done:
690 TALLOC_FREE(tmp_ctx);
691 return a_string;
694 /****************************************************************************
695 Do some specific substitutions in a string.
696 This function will return an allocated string that have to be freed.
697 ****************************************************************************/
699 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
700 const char *input_string,
701 const char *username,
702 const char *domain,
703 uid_t uid,
704 gid_t gid)
706 char *a_string;
707 char *ret_string = NULL;
708 char *b, *p, *s;
709 TALLOC_CTX *tmp_ctx;
711 if (!(tmp_ctx = talloc_new(mem_ctx))) {
712 DEBUG(0, ("talloc_new failed\n"));
713 return NULL;
716 a_string = talloc_strdup(tmp_ctx, input_string);
717 if (a_string == NULL) {
718 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
719 goto done;
722 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
724 b = a_string;
726 switch (*(p+1)) {
727 case 'U' :
728 a_string = talloc_string_sub(
729 tmp_ctx, a_string, "%U", username);
730 break;
731 case 'u' :
732 a_string = talloc_string_sub(
733 tmp_ctx, a_string, "%u", username);
734 break;
735 case 'G' :
736 if (gid != -1) {
737 a_string = talloc_string_sub(
738 tmp_ctx, a_string, "%G",
739 gidtoname(gid));
740 } else {
741 a_string = talloc_string_sub(
742 tmp_ctx, a_string,
743 "%G", "NO_GROUP");
745 break;
746 case 'g' :
747 if (gid != -1) {
748 a_string = talloc_string_sub(
749 tmp_ctx, a_string, "%g",
750 gidtoname(gid));
751 } else {
752 a_string = talloc_string_sub(
753 tmp_ctx, a_string, "%g", "NO_GROUP");
755 break;
756 case 'D' :
757 a_string = talloc_string_sub(tmp_ctx, a_string,
758 "%D", domain);
759 break;
760 case 'N' :
761 a_string = talloc_string_sub(
762 tmp_ctx, a_string, "%N",
763 automount_server(username));
764 break;
765 default:
766 break;
769 p++;
770 if (a_string == NULL) {
771 goto done;
775 /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
776 * away with the TALLOC_FREE(tmp_ctx) further down. */
778 ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
780 done:
781 TALLOC_FREE(tmp_ctx);
782 return ret_string;
785 /****************************************************************************
786 ****************************************************************************/
788 static char *alloc_sub_advanced(const char *servicename, const char *user,
789 const char *connectpath, gid_t gid,
790 const char *smb_name, const char *domain_name,
791 const char *str)
793 char *a_string, *ret_string;
794 char *b, *p, *s;
796 a_string = SMB_STRDUP(str);
797 if (a_string == NULL) {
798 DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
799 return NULL;
802 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
804 b = a_string;
806 switch (*(p+1)) {
807 case 'N' :
808 a_string = realloc_string_sub(a_string, "%N", automount_server(user));
809 break;
810 case 'H': {
811 char *h;
812 if ((h = get_user_home_dir(talloc_tos(), user)))
813 a_string = realloc_string_sub(a_string, "%H", h);
814 TALLOC_FREE(h);
815 break;
817 case 'P':
818 a_string = realloc_string_sub(a_string, "%P", connectpath);
819 break;
820 case 'S':
821 a_string = realloc_string_sub(a_string, "%S", servicename);
822 break;
823 case 'g':
824 a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
825 break;
826 case 'u':
827 a_string = realloc_string_sub(a_string, "%u", user);
828 break;
830 /* Patch from jkf@soton.ac.uk Left the %N (NIS
831 * server name) in standard_sub_basic as it is
832 * a feature for logon servers, hence uses the
833 * username. The %p (NIS server path) code is
834 * here as it is used instead of the default
835 * "path =" string in [homes] and so needs the
836 * service name, not the username. */
837 case 'p':
838 a_string = realloc_string_sub(a_string, "%p",
839 automount_path(servicename));
840 break;
842 default:
843 break;
846 p++;
847 if (a_string == NULL) {
848 return NULL;
852 ret_string = alloc_sub_basic(smb_name, domain_name, a_string);
853 SAFE_FREE(a_string);
854 return ret_string;
858 * This obviously is inefficient and needs to be merged into
859 * alloc_sub_advanced...
862 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
863 const char *servicename, const char *user,
864 const char *connectpath, gid_t gid,
865 const char *smb_name, const char *domain_name,
866 const char *str)
868 char *a, *t;
870 if (!(a = alloc_sub_advanced(servicename, user, connectpath, gid,
871 smb_name, domain_name, str))) {
872 return NULL;
874 t = talloc_strdup(mem_ctx, a);
875 SAFE_FREE(a);
876 return t;
880 void standard_sub_advanced(const char *servicename, const char *user,
881 const char *connectpath, gid_t gid,
882 const char *smb_name, const char *domain_name,
883 char *str, size_t len)
885 char *s;
887 s = alloc_sub_advanced(servicename, user, connectpath,
888 gid, smb_name, domain_name, str);
890 if ( s ) {
891 strncpy( str, s, len );
892 SAFE_FREE( s );
896 /****************************************************************************
897 Do some standard substitutions in a string.
898 ****************************************************************************/
900 char *standard_sub_conn(TALLOC_CTX *ctx, connection_struct *conn, const char *str)
902 return talloc_sub_advanced(ctx,
903 lp_servicename(SNUM(conn)),
904 conn->user,
905 conn->connectpath,
906 conn->gid,
907 get_smb_user_name(),
909 str);