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/>.
24 userdom_struct current_user_info
;
25 fstring remote_proto
="UNKNOWN";
28 * Set the 'local' machine name
29 * @param local_name the name we are being called
30 * @param if this is the 'final' name for us, not be be changed again
33 static char *local_machine
;
35 void free_local_machine_name(void)
37 SAFE_FREE(local_machine
);
40 bool set_local_machine_name(const char *local_name
, bool perm
)
42 static bool already_perm
= false;
43 char *tmp_local_machine
= NULL
;
44 char addr
[INET6_ADDRSTRLEN
];
47 tmp_local_machine
= SMB_STRDUP(local_name
);
48 if (!tmp_local_machine
) {
51 trim_char(tmp_local_machine
,' ',' ');
54 * Windows NT/2k uses "*SMBSERVER" and XP uses "*SMBSERV"
58 if (strequal(tmp_local_machine
, "*SMBSERVER") ||
59 strequal(tmp_local_machine
, "*SMBSERV") ) {
60 SAFE_FREE(local_machine
);
61 local_machine
= SMB_STRDUP(client_socket_addr(get_client_fd(),
62 addr
, sizeof(addr
)) );
63 SAFE_FREE(tmp_local_machine
);
64 return local_machine
? true : false;
71 SAFE_FREE(local_machine
);
72 len
= strlen(tmp_local_machine
);
73 local_machine
= SMB_CALLOC_ARRAY(char, len
+1);
75 SAFE_FREE(tmp_local_machine
);
78 /* alpha_strcpy includes the space for the terminating nul. */
79 alpha_strcpy(local_machine
,tmp_local_machine
,
80 SAFE_NETBIOS_CHARS
,len
+1);
81 strlower_m(local_machine
);
82 SAFE_FREE(tmp_local_machine
);
89 const char *get_local_machine_name(void)
91 if (!local_machine
|| !*local_machine
) {
92 return global_myname();
99 * Set the 'remote' machine name
100 * @param remote_name the name our client wants to be called by
101 * @param if this is the 'final' name for them, not be be changed again
104 static char *remote_machine
;
106 bool set_remote_machine_name(const char *remote_name
, bool perm
)
108 static bool already_perm
= False
;
109 char *tmp_remote_machine
;
116 tmp_remote_machine
= SMB_STRDUP(remote_name
);
117 if (!tmp_remote_machine
) {
120 trim_char(tmp_remote_machine
,' ',' ');
122 SAFE_FREE(remote_machine
);
123 len
= strlen(tmp_remote_machine
);
124 remote_machine
= SMB_CALLOC_ARRAY(char, len
+1);
125 if (!remote_machine
) {
126 SAFE_FREE(tmp_remote_machine
);
130 /* alpha_strcpy includes the space for the terminating nul. */
131 alpha_strcpy(remote_machine
,tmp_remote_machine
,
132 SAFE_NETBIOS_CHARS
,len
+1);
133 strlower_m(remote_machine
);
134 SAFE_FREE(tmp_remote_machine
);
141 const char *get_remote_machine_name(void)
143 return remote_machine
? remote_machine
: "";
146 /*******************************************************************
147 Setup the string used by %U substitution.
148 ********************************************************************/
150 static char *smb_user_name
;
152 void sub_set_smb_name(const char *name
)
156 bool is_machine_account
= false;
158 /* don't let anonymous logins override the name */
159 if (!name
|| !*name
) {
163 tmp
= SMB_STRDUP(name
);
167 trim_char(tmp
, ' ', ' ');
177 /* long story but here goes....we have to allow usernames
178 ending in '$' as they are valid machine account names.
179 So check for a machine account and re-add the '$'
180 at the end after the call to alpha_strcpy(). --jerry */
182 if (tmp
[len
-1] == '$') {
183 is_machine_account
= True
;
186 SAFE_FREE(smb_user_name
);
187 smb_user_name
= SMB_CALLOC_ARRAY(char, len
+1);
188 if (!smb_user_name
) {
193 /* alpha_strcpy includes the space for the terminating nul. */
194 alpha_strcpy(smb_user_name
, tmp
,
200 if (is_machine_account
) {
201 len
= strlen(smb_user_name
);
202 smb_user_name
[len
-1] = '$';
206 static const char *get_smb_user_name(void)
208 return smb_user_name
? smb_user_name
: "";
211 /*******************************************************************
212 Setup the strings used by substitutions. Called per packet. Ensure
213 %U name is set correctly also.
215 smb_name must be sanitized by alpha_strcpy
216 ********************************************************************/
218 void set_current_user_info(const char *smb_name
, const char *unix_name
,
219 const char *full_name
, const char *domain
)
221 fstrcpy(current_user_info
.smb_name
, smb_name
);
222 fstrcpy(current_user_info
.unix_name
, unix_name
);
223 fstrcpy(current_user_info
.full_name
, full_name
);
224 fstrcpy(current_user_info
.domain
, domain
);
226 /* The following is safe as current_user_info.smb_name
227 * has already been sanitised in register_existing_vuid. */
229 sub_set_smb_name(current_user_info
.smb_name
);
232 /*******************************************************************
233 Return the current active user name.
234 *******************************************************************/
236 const char *get_current_username(void)
238 if (current_user_info
.smb_name
[0] == '\0' ) {
239 return get_smb_user_name();
242 return current_user_info
.smb_name
;
245 /*******************************************************************
246 Given a pointer to a %$(NAME) in p and the whole string in str
247 expand it as an environment variable.
248 Return a new allocated and expanded string.
249 Based on code by Branko Cibej <branko.cibej@hermes.si>
250 When this is called p points at the '%' character.
251 May substitute multiple occurrencies of the same env var.
252 ********************************************************************/
254 static char * realloc_expand_env_var(char *str
, char *p
)
261 if (p
[0] != '%' || p
[1] != '$' || p
[2] != '(') {
266 * Look for the terminating ')'.
269 if ((q
= strchr_m(p
,')')) == NULL
) {
270 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p
));
275 * Extract the name from within the %$(NAME) string.
281 /* reserve space for use later add %$() chars */
282 if ( (envname
= (char *)SMB_MALLOC(copylen
+ 1 + 4)) == NULL
) {
286 strncpy(envname
,r
,copylen
);
287 envname
[copylen
] = '\0';
289 if ((envval
= getenv(envname
)) == NULL
) {
290 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname
));
296 * Copy the full %$(NAME) into envname so it
301 strncpy(envname
,p
,copylen
);
302 envname
[copylen
] = '\0';
303 r
= realloc_string_sub(str
, envname
, envval
);
309 /*******************************************************************
310 *******************************************************************/
312 static char *longvar_domainsid( void )
318 if ( !secrets_fetch_domain_sid( lp_workgroup(), &sid
) ) {
322 sid_string
= SMB_STRDUP( sid_to_fstring( tmp
, &sid
) );
325 DEBUG(0,("longvar_domainsid: failed to dup SID string!\n"));
331 /*******************************************************************
332 *******************************************************************/
339 static struct api_longvar longvar_table
[] = {
340 { "DomainSID", longvar_domainsid
},
344 static char *get_longvar_val( const char *varname
)
348 DEBUG(7,("get_longvar_val: expanding variable [%s]\n", varname
));
350 for ( i
=0; longvar_table
[i
].name
; i
++ ) {
351 if ( strequal( longvar_table
[i
].name
, varname
) ) {
352 return longvar_table
[i
].fn();
359 /*******************************************************************
360 Expand the long smb.conf variable names given a pointer to a %(NAME).
361 Return the number of characters by which the pointer should be advanced.
362 When this is called p points at the '%' character.
363 ********************************************************************/
365 static char *realloc_expand_longvar(char *str
, char *p
)
372 if ( p
[0] != '%' || p
[1] != '(' ) {
376 /* Look for the terminating ')'.*/
378 if ((q
= strchr_m(p
,')')) == NULL
) {
379 DEBUG(0,("realloc_expand_longvar: Unterminated environment variable [%s]\n", p
));
383 /* Extract the name from within the %(NAME) string.*/
386 copylen
= MIN( (q
-r
), (sizeof(varname
)-1) );
387 strncpy(varname
, r
, copylen
);
388 varname
[copylen
] = '\0';
390 if ((value
= get_longvar_val(varname
)) == NULL
) {
391 DEBUG(0,("realloc_expand_longvar: Variable [%s] not set. Skipping\n", varname
));
395 /* Copy the full %(NAME) into envname so it can be replaced.*/
397 copylen
= MIN( (q
+1-p
),(sizeof(varname
)-1) );
398 strncpy( varname
, p
, copylen
);
399 varname
[copylen
] = '\0';
400 r
= realloc_string_sub(str
, varname
, value
);
403 /* skip over the %(varname) */
408 /*******************************************************************
409 Patch from jkf@soton.ac.uk
410 Added this to implement %p (NIS auto-map version of %H)
411 *******************************************************************/
413 static const char *automount_path(const char *user_name
)
415 TALLOC_CTX
*ctx
= talloc_tos();
416 const char *server_path
;
418 /* use the passwd entry as the default */
419 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
421 server_path
= talloc_strdup(ctx
, get_user_home_dir(ctx
, user_name
));
426 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
428 if (lp_nis_home_map()) {
429 const char *home_path_start
;
430 char *automount_value
= automount_lookup(ctx
, user_name
);
432 if(automount_value
&& strlen(automount_value
) > 0) {
433 home_path_start
= strchr_m(automount_value
,':');
434 if (home_path_start
!= NULL
) {
435 DEBUG(5, ("NIS lookup succeeded. "
436 "Home path is: %s\n",
438 (home_path_start
+1):""));
439 server_path
= talloc_strdup(ctx
,
446 /* NIS key lookup failed: default to
447 * user home directory from password file */
448 DEBUG(5, ("NIS lookup failed. Using Home path from "
449 "passwd file. Home path is: %s\n", server_path
));
454 DEBUG(4,("Home server path: %s\n", server_path
));
458 /*******************************************************************
459 Patch from jkf@soton.ac.uk
460 This is Luke's original function with the NIS lookup code
461 moved out to a separate function.
462 *******************************************************************/
464 static const char *automount_server(const char *user_name
)
466 TALLOC_CTX
*ctx
= talloc_tos();
467 const char *server_name
;
468 const char *local_machine_name
= get_local_machine_name();
470 /* use the local machine name as the default */
471 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
472 if (local_machine_name
&& *local_machine_name
) {
473 server_name
= talloc_strdup(ctx
, local_machine_name
);
475 server_name
= talloc_strdup(ctx
, global_myname());
482 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
483 if (lp_nis_home_map()) {
486 char *automount_value
= automount_lookup(ctx
, user_name
);
487 if (!automount_value
) {
490 srv
= talloc_strdup(ctx
, automount_value
);
494 p
= strchr_m(srv
, ':');
500 DEBUG(5, ("NIS lookup succeeded. Home server %s\n",
505 DEBUG(4,("Home server: %s\n", server_name
));
509 /****************************************************************************
510 Do some standard substitutions in a string.
511 len is the length in bytes of the space allowed in string str. If zero means
512 don't allow expansions.
513 ****************************************************************************/
515 void standard_sub_basic(const char *smb_name
, const char *domain_name
,
516 char *str
, size_t len
)
520 if ( (s
= alloc_sub_basic( smb_name
, domain_name
, str
)) != NULL
) {
521 strncpy( str
, s
, len
);
528 /****************************************************************************
529 Do some standard substitutions in a string.
530 This function will return an allocated string that have to be freed.
531 ****************************************************************************/
533 char *talloc_sub_basic(TALLOC_CTX
*mem_ctx
, const char *smb_name
,
534 const char *domain_name
, const char *str
)
538 if ( (a
= alloc_sub_basic(smb_name
, domain_name
, str
)) == NULL
) {
541 t
= talloc_strdup(mem_ctx
, a
);
546 /****************************************************************************
547 ****************************************************************************/
549 char *alloc_sub_basic(const char *smb_name
, const char *domain_name
,
552 char *b
, *p
, *s
, *r
, *a_string
;
553 fstring pidstr
, vnnstr
;
554 char addr
[INET6_ADDRSTRLEN
];
555 const char *local_machine_name
= get_local_machine_name();
556 TALLOC_CTX
*tmp_ctx
= NULL
;
558 /* workaround to prevent a crash while looking at bug #687 */
561 DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
565 a_string
= SMB_STRDUP(str
);
566 if (a_string
== NULL
) {
567 DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
571 tmp_ctx
= talloc_stackframe();
573 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
580 r
= strdup_lower(smb_name
);
584 a_string
= realloc_string_sub(a_string
, "%U", r
);
588 r
= SMB_STRDUP(smb_name
);
592 pass
= Get_Pwnam_alloc(tmp_ctx
, r
);
594 a_string
= realloc_string_sub(
596 gidtoname(pass
->pw_gid
));
602 r
= strdup_upper(domain_name
);
606 a_string
= realloc_string_sub(a_string
, "%D", r
);
610 client_addr(get_client_fd(), addr
, sizeof(addr
));
611 if (strnequal(addr
,"::ffff:",7)) {
614 a_string
= realloc_string_sub(a_string
, "%I",
619 a_string
= realloc_string_sub( a_string
, "%i",
620 client_socket_addr(get_client_fd(), addr
, sizeof(addr
)) );
623 if ( StrnCaseCmp(p
, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
626 if (local_machine_name
&& *local_machine_name
) {
627 a_string
= realloc_string_sub(a_string
, "%L", local_machine_name
);
629 a_string
= realloc_string_sub(a_string
, "%L", global_myname());
633 a_string
= realloc_string_sub(a_string
, "%N", automount_server(smb_name
));
636 a_string
= realloc_string_sub(a_string
, "%M", client_name(get_client_fd()));
639 a_string
= realloc_string_sub(a_string
, "%R", remote_proto
);
642 a_string
= realloc_string_sub(a_string
, "%T", current_timestring(tmp_ctx
, False
));
645 a_string
= realloc_string_sub(a_string
, "%a",
646 get_remote_arch_str());
649 slprintf(pidstr
,sizeof(pidstr
)-1, "%d",(int)sys_getpid());
650 a_string
= realloc_string_sub(a_string
, "%d", pidstr
);
653 a_string
= realloc_string_sub(a_string
, "%h", myhostname());
656 a_string
= realloc_string_sub(a_string
, "%m",
662 a_string
= realloc_string_sub(a_string
, "%v", SAMBA_VERSION_STRING
);
665 a_string
= realloc_string_sub(a_string
, "%w", lp_winbind_separator());
668 a_string
= realloc_expand_env_var(a_string
, p
); /* Expand environment variables */
671 a_string
= realloc_expand_longvar( a_string
, p
);
674 slprintf(vnnstr
,sizeof(vnnstr
)-1, "%u", get_my_vnn());
675 a_string
= realloc_string_sub(a_string
, "%V", vnnstr
);
684 if (a_string
== NULL
) {
695 TALLOC_FREE(tmp_ctx
);
699 /****************************************************************************
700 Do some specific substitutions in a string.
701 This function will return an allocated string that have to be freed.
702 ****************************************************************************/
704 char *talloc_sub_specified(TALLOC_CTX
*mem_ctx
,
705 const char *input_string
,
706 const char *username
,
712 char *ret_string
= NULL
;
716 if (!(tmp_ctx
= talloc_new(mem_ctx
))) {
717 DEBUG(0, ("talloc_new failed\n"));
721 a_string
= talloc_strdup(tmp_ctx
, input_string
);
722 if (a_string
== NULL
) {
723 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
727 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
733 a_string
= talloc_string_sub(
734 tmp_ctx
, a_string
, "%U", username
);
737 a_string
= talloc_string_sub(
738 tmp_ctx
, a_string
, "%u", username
);
742 a_string
= talloc_string_sub(
743 tmp_ctx
, a_string
, "%G",
746 a_string
= talloc_string_sub(
753 a_string
= talloc_string_sub(
754 tmp_ctx
, a_string
, "%g",
757 a_string
= talloc_string_sub(
758 tmp_ctx
, a_string
, "%g", "NO_GROUP");
762 a_string
= talloc_string_sub(tmp_ctx
, a_string
,
766 a_string
= talloc_string_sub(
767 tmp_ctx
, a_string
, "%N",
768 automount_server(username
));
775 if (a_string
== NULL
) {
780 /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
781 * away with the TALLOC_FREE(tmp_ctx) further down. */
783 ret_string
= talloc_sub_basic(mem_ctx
, username
, domain
, a_string
);
786 TALLOC_FREE(tmp_ctx
);
790 /****************************************************************************
791 ****************************************************************************/
793 static char *alloc_sub_advanced(const char *servicename
, const char *user
,
794 const char *connectpath
, gid_t gid
,
795 const char *smb_name
, const char *domain_name
,
798 char *a_string
, *ret_string
;
801 a_string
= SMB_STRDUP(str
);
802 if (a_string
== NULL
) {
803 DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
807 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
813 a_string
= realloc_string_sub(a_string
, "%N", automount_server(user
));
817 if ((h
= get_user_home_dir(talloc_tos(), user
)))
818 a_string
= realloc_string_sub(a_string
, "%H", h
);
823 a_string
= realloc_string_sub(a_string
, "%P", connectpath
);
826 a_string
= realloc_string_sub(a_string
, "%S", servicename
);
829 a_string
= realloc_string_sub(a_string
, "%g", gidtoname(gid
));
832 a_string
= realloc_string_sub(a_string
, "%u", user
);
835 /* Patch from jkf@soton.ac.uk Left the %N (NIS
836 * server name) in standard_sub_basic as it is
837 * a feature for logon servers, hence uses the
838 * username. The %p (NIS server path) code is
839 * here as it is used instead of the default
840 * "path =" string in [homes] and so needs the
841 * service name, not the username. */
843 a_string
= realloc_string_sub(a_string
, "%p",
844 automount_path(servicename
));
852 if (a_string
== NULL
) {
857 ret_string
= alloc_sub_basic(smb_name
, domain_name
, a_string
);
863 * This obviously is inefficient and needs to be merged into
864 * alloc_sub_advanced...
867 char *talloc_sub_advanced(TALLOC_CTX
*mem_ctx
,
868 const char *servicename
, const char *user
,
869 const char *connectpath
, gid_t gid
,
870 const char *smb_name
, const char *domain_name
,
875 if (!(a
= alloc_sub_advanced(servicename
, user
, connectpath
, gid
,
876 smb_name
, domain_name
, str
))) {
879 t
= talloc_strdup(mem_ctx
, a
);
885 void standard_sub_advanced(const char *servicename
, const char *user
,
886 const char *connectpath
, gid_t gid
,
887 const char *smb_name
, const char *domain_name
,
888 char *str
, size_t len
)
892 s
= alloc_sub_advanced(servicename
, user
, connectpath
,
893 gid
, smb_name
, domain_name
, str
);
896 strncpy( str
, s
, len
);
901 /****************************************************************************
902 Do some standard substitutions in a string.
903 ****************************************************************************/
905 char *standard_sub_conn(TALLOC_CTX
*ctx
, connection_struct
*conn
, const char *str
)
907 return talloc_sub_advanced(ctx
,
908 lp_servicename(SNUM(conn
)),
909 conn
->server_info
->unix_name
,
911 conn
->server_info
->utok
.gid
,