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/>.
25 static char *alloc_sub_basic(const char *smb_name
, const char *domain_name
,
28 userdom_struct current_user_info
;
29 fstring remote_proto
="UNKNOWN";
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
37 static char *local_machine
;
39 void free_local_machine_name(void)
41 SAFE_FREE(local_machine
);
44 bool set_local_machine_name(const char *local_name
, bool perm
)
46 static bool already_perm
= false;
47 char *tmp_local_machine
= NULL
;
54 tmp_local_machine
= SMB_STRDUP(local_name
);
55 if (!tmp_local_machine
) {
58 trim_char(tmp_local_machine
,' ',' ');
60 SAFE_FREE(local_machine
);
61 len
= strlen(tmp_local_machine
);
62 local_machine
= SMB_CALLOC_ARRAY(char, len
+1);
64 SAFE_FREE(tmp_local_machine
);
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
);
78 const char *get_local_machine_name(void)
80 if (!local_machine
|| !*local_machine
) {
81 return global_myname();
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
93 static char *remote_machine
;
95 bool set_remote_machine_name(const char *remote_name
, bool perm
)
97 static bool already_perm
= False
;
98 char *tmp_remote_machine
;
105 tmp_remote_machine
= SMB_STRDUP(remote_name
);
106 if (!tmp_remote_machine
) {
109 trim_char(tmp_remote_machine
,' ',' ');
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
);
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
);
130 const char *get_remote_machine_name(void)
132 return remote_machine
? remote_machine
: "";
135 /*******************************************************************
136 Setup the string used by %U substitution.
137 ********************************************************************/
139 static char *smb_user_name
;
141 void sub_set_smb_name(const char *name
)
145 bool is_machine_account
= false;
147 /* don't let anonymous logins override the name */
148 if (!name
|| !*name
) {
152 tmp
= SMB_STRDUP(name
);
156 trim_char(tmp
, ' ', ' ');
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 */
171 if (tmp
[len
-1] == '$') {
172 is_machine_account
= True
;
175 SAFE_FREE(smb_user_name
);
176 smb_user_name
= SMB_CALLOC_ARRAY(char, len
+1);
177 if (!smb_user_name
) {
182 /* alpha_strcpy includes the space for the terminating nul. */
183 alpha_strcpy(smb_user_name
, tmp
,
189 if (is_machine_account
) {
190 len
= strlen(smb_user_name
);
191 smb_user_name
[len
-1] = '$';
195 static char sub_peeraddr
[INET6_ADDRSTRLEN
];
196 static const char *sub_peername
= "";
197 static char sub_sockaddr
[INET6_ADDRSTRLEN
];
199 void sub_set_socket_ids(const char *peeraddr
, const char *peername
,
200 const char *sockaddr
)
202 const char *addr
= peeraddr
;
204 if (strnequal(addr
, "::ffff:", 7)) {
207 strlcpy(sub_peeraddr
, addr
, sizeof(sub_peeraddr
));
209 sub_peername
= SMB_STRDUP(peername
);
210 if (sub_peername
== NULL
) {
211 sub_peername
= sub_peeraddr
;
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.
219 strlcpy(sub_sockaddr
, sockaddr
, sizeof(sub_sockaddr
));
222 static const char *get_smb_user_name(void)
224 return smb_user_name
? smb_user_name
: "";
227 /*******************************************************************
228 Setup the strings used by substitutions. Called per packet. Ensure
229 %U name is set correctly also.
231 smb_name must be sanitized by alpha_strcpy
232 ********************************************************************/
234 void set_current_user_info(const char *smb_name
, const char *unix_name
,
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
);
241 /* The following is safe as current_user_info.smb_name
242 * has already been sanitised in register_existing_vuid. */
244 sub_set_smb_name(current_user_info
.smb_name
);
247 /*******************************************************************
248 Return the current active user name.
249 *******************************************************************/
251 const char *get_current_username(void)
253 if (current_user_info
.smb_name
[0] == '\0' ) {
254 return get_smb_user_name();
257 return current_user_info
.smb_name
;
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 ********************************************************************/
269 static char * realloc_expand_env_var(char *str
, char *p
)
276 if (p
[0] != '%' || p
[1] != '$' || p
[2] != '(') {
281 * Look for the terminating ')'.
284 if ((q
= strchr_m(p
,')')) == NULL
) {
285 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p
));
290 * Extract the name from within the %$(NAME) string.
296 /* reserve space for use later add %$() chars */
297 if ( (envname
= (char *)SMB_MALLOC(copylen
+ 1 + 4)) == NULL
) {
301 strncpy(envname
,r
,copylen
);
302 envname
[copylen
] = '\0';
304 if ((envval
= getenv(envname
)) == NULL
) {
305 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname
));
311 * Copy the full %$(NAME) into envname so it
316 strncpy(envname
,p
,copylen
);
317 envname
[copylen
] = '\0';
318 r
= realloc_string_sub(str
, envname
, envval
);
324 /*******************************************************************
325 *******************************************************************/
327 static char *longvar_domainsid( void )
333 if ( !secrets_fetch_domain_sid( lp_workgroup(), &sid
) ) {
337 sid_string
= SMB_STRDUP( sid_to_fstring( tmp
, &sid
) );
340 DEBUG(0,("longvar_domainsid: failed to dup SID string!\n"));
346 /*******************************************************************
347 *******************************************************************/
354 static struct api_longvar longvar_table
[] = {
355 { "DomainSID", longvar_domainsid
},
359 static char *get_longvar_val( const char *varname
)
363 DEBUG(7,("get_longvar_val: expanding variable [%s]\n", varname
));
365 for ( i
=0; longvar_table
[i
].name
; i
++ ) {
366 if ( strequal( longvar_table
[i
].name
, varname
) ) {
367 return longvar_table
[i
].fn();
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 ********************************************************************/
380 static char *realloc_expand_longvar(char *str
, char *p
)
387 if ( p
[0] != '%' || p
[1] != '(' ) {
391 /* Look for the terminating ')'.*/
393 if ((q
= strchr_m(p
,')')) == NULL
) {
394 DEBUG(0,("realloc_expand_longvar: Unterminated environment variable [%s]\n", p
));
398 /* Extract the name from within the %(NAME) string.*/
401 copylen
= MIN( (q
-r
), (sizeof(varname
)-1) );
402 strncpy(varname
, r
, copylen
);
403 varname
[copylen
] = '\0';
405 if ((value
= get_longvar_val(varname
)) == NULL
) {
406 DEBUG(0,("realloc_expand_longvar: Variable [%s] not set. Skipping\n", varname
));
410 /* Copy the full %(NAME) into envname so it can be replaced.*/
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
);
418 /* skip over the %(varname) */
423 /*******************************************************************
424 Patch from jkf@soton.ac.uk
425 Added this to implement %p (NIS auto-map version of %H)
426 *******************************************************************/
428 static const char *automount_path(const char *user_name
)
430 TALLOC_CTX
*ctx
= talloc_tos();
431 const char *server_path
;
433 /* use the passwd entry as the default */
434 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
436 server_path
= talloc_strdup(ctx
, get_user_home_dir(ctx
, user_name
));
441 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
443 if (lp_nis_home_map()) {
444 const char *home_path_start
;
445 char *automount_value
= automount_lookup(ctx
, user_name
);
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",
453 (home_path_start
+1):""));
454 server_path
= talloc_strdup(ctx
,
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
));
469 DEBUG(4,("Home server path: %s\n", server_path
));
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 *******************************************************************/
479 static const char *automount_server(const char *user_name
)
481 TALLOC_CTX
*ctx
= talloc_tos();
482 const char *server_name
;
483 const char *local_machine_name
= get_local_machine_name();
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
);
490 server_name
= talloc_strdup(ctx
, global_myname());
497 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
498 if (lp_nis_home_map()) {
501 char *automount_value
= automount_lookup(ctx
, user_name
);
502 if (!automount_value
) {
505 srv
= talloc_strdup(ctx
, automount_value
);
509 p
= strchr_m(srv
, ':');
515 DEBUG(5, ("NIS lookup succeeded. Home server %s\n",
520 DEBUG(4,("Home server: %s\n", server_name
));
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 ****************************************************************************/
530 void standard_sub_basic(const char *smb_name
, const char *domain_name
,
531 char *str
, size_t len
)
535 if ( (s
= alloc_sub_basic( smb_name
, domain_name
, str
)) != NULL
) {
536 strncpy( str
, s
, len
);
542 /****************************************************************************
543 Do some standard substitutions in a string.
544 This function will return an allocated string that have to be freed.
545 ****************************************************************************/
547 char *talloc_sub_basic(TALLOC_CTX
*mem_ctx
, const char *smb_name
,
548 const char *domain_name
, const char *str
)
552 if ( (a
= alloc_sub_basic(smb_name
, domain_name
, str
)) == NULL
) {
555 t
= talloc_strdup(mem_ctx
, a
);
560 /****************************************************************************
561 ****************************************************************************/
563 static char *alloc_sub_basic(const char *smb_name
, const char *domain_name
,
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
;
571 /* workaround to prevent a crash while looking at bug #687 */
574 DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
578 a_string
= SMB_STRDUP(str
);
579 if (a_string
== NULL
) {
580 DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
584 tmp_ctx
= talloc_stackframe();
586 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
593 r
= strlower_talloc(tmp_ctx
, smb_name
);
597 a_string
= realloc_string_sub(a_string
, "%U", r
);
601 r
= talloc_strdup(tmp_ctx
, smb_name
);
605 pass
= Get_Pwnam_alloc(tmp_ctx
, r
);
607 a_string
= realloc_string_sub(
609 gidtoname(pass
->pw_gid
));
615 r
= strupper_talloc(tmp_ctx
, domain_name
);
619 a_string
= realloc_string_sub(a_string
, "%D", r
);
622 a_string
= realloc_string_sub(
624 sub_peeraddr
[0] ? sub_peeraddr
: "0.0.0.0");
628 a_string
= realloc_string_sub(
630 sub_sockaddr
[0] ? sub_sockaddr
: "0.0.0.0");
633 if ( StrnCaseCmp(p
, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
636 if (local_machine_name
&& *local_machine_name
) {
637 a_string
= realloc_string_sub(a_string
, "%L", local_machine_name
);
639 a_string
= realloc_string_sub(a_string
, "%L", global_myname());
643 a_string
= realloc_string_sub(a_string
, "%N", automount_server(smb_name
));
646 a_string
= realloc_string_sub(a_string
, "%M",
650 a_string
= realloc_string_sub(a_string
, "%R", remote_proto
);
653 a_string
= realloc_string_sub(a_string
, "%T", current_timestring(tmp_ctx
, False
));
656 a_string
= realloc_string_sub(a_string
, "%a",
657 get_remote_arch_str());
660 slprintf(pidstr
,sizeof(pidstr
)-1, "%d",(int)sys_getpid());
661 a_string
= realloc_string_sub(a_string
, "%d", pidstr
);
664 a_string
= realloc_string_sub(a_string
, "%h", myhostname());
667 a_string
= realloc_string_sub(a_string
, "%m",
673 a_string
= realloc_string_sub(a_string
, "%v", samba_version_string());
676 a_string
= realloc_string_sub(a_string
, "%w", lp_winbind_separator());
679 a_string
= realloc_expand_env_var(a_string
, p
); /* Expand environment variables */
682 a_string
= realloc_expand_longvar( a_string
, p
);
685 slprintf(vnnstr
,sizeof(vnnstr
)-1, "%u", get_my_vnn());
686 a_string
= realloc_string_sub(a_string
, "%V", vnnstr
);
695 if (a_string
== NULL
) {
706 TALLOC_FREE(tmp_ctx
);
710 /****************************************************************************
711 Do some specific substitutions in a string.
712 This function will return an allocated string that have to be freed.
713 ****************************************************************************/
715 char *talloc_sub_specified(TALLOC_CTX
*mem_ctx
,
716 const char *input_string
,
717 const char *username
,
723 char *ret_string
= NULL
;
727 if (!(tmp_ctx
= talloc_new(mem_ctx
))) {
728 DEBUG(0, ("talloc_new failed\n"));
732 a_string
= talloc_strdup(tmp_ctx
, input_string
);
733 if (a_string
== NULL
) {
734 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
738 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
744 a_string
= talloc_string_sub(
745 tmp_ctx
, a_string
, "%U", username
);
748 a_string
= talloc_string_sub(
749 tmp_ctx
, a_string
, "%u", username
);
753 a_string
= talloc_string_sub(
754 tmp_ctx
, a_string
, "%G",
757 a_string
= talloc_string_sub(
764 a_string
= talloc_string_sub(
765 tmp_ctx
, a_string
, "%g",
768 a_string
= talloc_string_sub(
769 tmp_ctx
, a_string
, "%g", "NO_GROUP");
773 a_string
= talloc_string_sub(tmp_ctx
, a_string
,
777 a_string
= talloc_string_sub(
778 tmp_ctx
, a_string
, "%N",
779 automount_server(username
));
786 if (a_string
== NULL
) {
791 /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
792 * away with the TALLOC_FREE(tmp_ctx) further down. */
794 ret_string
= talloc_sub_basic(mem_ctx
, username
, domain
, a_string
);
797 TALLOC_FREE(tmp_ctx
);
801 /****************************************************************************
802 ****************************************************************************/
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
,
809 char *a_string
, *ret_string
;
812 a_string
= SMB_STRDUP(str
);
813 if (a_string
== NULL
) {
814 DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
818 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
824 a_string
= realloc_string_sub(a_string
, "%N", automount_server(user
));
828 if ((h
= get_user_home_dir(talloc_tos(), user
)))
829 a_string
= realloc_string_sub(a_string
, "%H", h
);
834 a_string
= realloc_string_sub(a_string
, "%P", connectpath
);
837 a_string
= realloc_string_sub(a_string
, "%S", servicename
);
840 a_string
= realloc_string_sub(a_string
, "%g", gidtoname(gid
));
843 a_string
= realloc_string_sub(a_string
, "%u", user
);
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. */
854 a_string
= realloc_string_sub(a_string
, "%p",
855 automount_path(servicename
));
863 if (a_string
== NULL
) {
868 ret_string
= alloc_sub_basic(smb_name
, domain_name
, a_string
);
874 * This obviously is inefficient and needs to be merged into
875 * alloc_sub_advanced...
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
,
886 if (!(a
= alloc_sub_advanced(servicename
, user
, connectpath
, gid
,
887 smb_name
, domain_name
, str
))) {
890 t
= talloc_strdup(mem_ctx
, a
);
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
)
903 s
= alloc_sub_advanced(servicename
, user
, connectpath
,
904 gid
, smb_name
, domain_name
, str
);
907 strncpy( str
, s
, len
);
912 /****************************************************************************
913 Do some standard substitutions in a string.
914 ****************************************************************************/
916 char *standard_sub_conn(TALLOC_CTX
*ctx
, connection_struct
*conn
, const char *str
)
918 return talloc_sub_advanced(ctx
,
919 lp_servicename(SNUM(conn
)),
920 conn
->server_info
->unix_name
,
922 conn
->server_info
->utok
.gid
,