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
,
221 fstrcpy(current_user_info
.smb_name
, smb_name
);
222 fstrcpy(current_user_info
.unix_name
, unix_name
);
223 fstrcpy(current_user_info
.domain
, domain
);
225 /* The following is safe as current_user_info.smb_name
226 * has already been sanitised in register_existing_vuid. */
228 sub_set_smb_name(current_user_info
.smb_name
);
231 /*******************************************************************
232 Return the current active user name.
233 *******************************************************************/
235 const char *get_current_username(void)
237 if (current_user_info
.smb_name
[0] == '\0' ) {
238 return get_smb_user_name();
241 return current_user_info
.smb_name
;
244 /*******************************************************************
245 Given a pointer to a %$(NAME) in p and the whole string in str
246 expand it as an environment variable.
247 Return a new allocated and expanded string.
248 Based on code by Branko Cibej <branko.cibej@hermes.si>
249 When this is called p points at the '%' character.
250 May substitute multiple occurrencies of the same env var.
251 ********************************************************************/
253 static char * realloc_expand_env_var(char *str
, char *p
)
260 if (p
[0] != '%' || p
[1] != '$' || p
[2] != '(') {
265 * Look for the terminating ')'.
268 if ((q
= strchr_m(p
,')')) == NULL
) {
269 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p
));
274 * Extract the name from within the %$(NAME) string.
280 /* reserve space for use later add %$() chars */
281 if ( (envname
= (char *)SMB_MALLOC(copylen
+ 1 + 4)) == NULL
) {
285 strncpy(envname
,r
,copylen
);
286 envname
[copylen
] = '\0';
288 if ((envval
= getenv(envname
)) == NULL
) {
289 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname
));
295 * Copy the full %$(NAME) into envname so it
300 strncpy(envname
,p
,copylen
);
301 envname
[copylen
] = '\0';
302 r
= realloc_string_sub(str
, envname
, envval
);
308 /*******************************************************************
309 *******************************************************************/
311 static char *longvar_domainsid( void )
317 if ( !secrets_fetch_domain_sid( lp_workgroup(), &sid
) ) {
321 sid_string
= SMB_STRDUP( sid_to_fstring( tmp
, &sid
) );
324 DEBUG(0,("longvar_domainsid: failed to dup SID string!\n"));
330 /*******************************************************************
331 *******************************************************************/
338 static struct api_longvar longvar_table
[] = {
339 { "DomainSID", longvar_domainsid
},
343 static char *get_longvar_val( const char *varname
)
347 DEBUG(7,("get_longvar_val: expanding variable [%s]\n", varname
));
349 for ( i
=0; longvar_table
[i
].name
; i
++ ) {
350 if ( strequal( longvar_table
[i
].name
, varname
) ) {
351 return longvar_table
[i
].fn();
358 /*******************************************************************
359 Expand the long smb.conf variable names given a pointer to a %(NAME).
360 Return the number of characters by which the pointer should be advanced.
361 When this is called p points at the '%' character.
362 ********************************************************************/
364 static char *realloc_expand_longvar(char *str
, char *p
)
371 if ( p
[0] != '%' || p
[1] != '(' ) {
375 /* Look for the terminating ')'.*/
377 if ((q
= strchr_m(p
,')')) == NULL
) {
378 DEBUG(0,("realloc_expand_longvar: Unterminated environment variable [%s]\n", p
));
382 /* Extract the name from within the %(NAME) string.*/
385 copylen
= MIN( (q
-r
), (sizeof(varname
)-1) );
386 strncpy(varname
, r
, copylen
);
387 varname
[copylen
] = '\0';
389 if ((value
= get_longvar_val(varname
)) == NULL
) {
390 DEBUG(0,("realloc_expand_longvar: Variable [%s] not set. Skipping\n", varname
));
394 /* Copy the full %(NAME) into envname so it can be replaced.*/
396 copylen
= MIN( (q
+1-p
),(sizeof(varname
)-1) );
397 strncpy( varname
, p
, copylen
);
398 varname
[copylen
] = '\0';
399 r
= realloc_string_sub(str
, varname
, value
);
402 /* skip over the %(varname) */
407 /*******************************************************************
408 Patch from jkf@soton.ac.uk
409 Added this to implement %p (NIS auto-map version of %H)
410 *******************************************************************/
412 static const char *automount_path(const char *user_name
)
414 TALLOC_CTX
*ctx
= talloc_tos();
415 const char *server_path
;
417 /* use the passwd entry as the default */
418 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
420 server_path
= talloc_strdup(ctx
, get_user_home_dir(ctx
, user_name
));
425 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
427 if (lp_nis_home_map()) {
428 const char *home_path_start
;
429 char *automount_value
= automount_lookup(ctx
, user_name
);
431 if(automount_value
&& strlen(automount_value
) > 0) {
432 home_path_start
= strchr_m(automount_value
,':');
433 if (home_path_start
!= NULL
) {
434 DEBUG(5, ("NIS lookup succeeded. "
435 "Home path is: %s\n",
437 (home_path_start
+1):""));
438 server_path
= talloc_strdup(ctx
,
445 /* NIS key lookup failed: default to
446 * user home directory from password file */
447 DEBUG(5, ("NIS lookup failed. Using Home path from "
448 "passwd file. Home path is: %s\n", server_path
));
453 DEBUG(4,("Home server path: %s\n", server_path
));
457 /*******************************************************************
458 Patch from jkf@soton.ac.uk
459 This is Luke's original function with the NIS lookup code
460 moved out to a separate function.
461 *******************************************************************/
463 static const char *automount_server(const char *user_name
)
465 TALLOC_CTX
*ctx
= talloc_tos();
466 const char *server_name
;
467 const char *local_machine_name
= get_local_machine_name();
469 /* use the local machine name as the default */
470 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
471 if (local_machine_name
&& *local_machine_name
) {
472 server_name
= talloc_strdup(ctx
, local_machine_name
);
474 server_name
= talloc_strdup(ctx
, global_myname());
481 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
482 if (lp_nis_home_map()) {
485 char *automount_value
= automount_lookup(ctx
, user_name
);
486 if (!automount_value
) {
489 srv
= talloc_strdup(ctx
, automount_value
);
493 p
= strchr_m(srv
, ':');
499 DEBUG(5, ("NIS lookup succeeded. Home server %s\n",
504 DEBUG(4,("Home server: %s\n", server_name
));
508 /****************************************************************************
509 Do some standard substitutions in a string.
510 len is the length in bytes of the space allowed in string str. If zero means
511 don't allow expansions.
512 ****************************************************************************/
514 void standard_sub_basic(const char *smb_name
, const char *domain_name
,
515 char *str
, size_t len
)
519 if ( (s
= alloc_sub_basic( smb_name
, domain_name
, str
)) != NULL
) {
520 strncpy( str
, s
, len
);
527 /****************************************************************************
528 Do some standard substitutions in a string.
529 This function will return an allocated string that have to be freed.
530 ****************************************************************************/
532 char *talloc_sub_basic(TALLOC_CTX
*mem_ctx
, const char *smb_name
,
533 const char *domain_name
, const char *str
)
537 if ( (a
= alloc_sub_basic(smb_name
, domain_name
, str
)) == NULL
) {
540 t
= talloc_strdup(mem_ctx
, a
);
545 /****************************************************************************
546 ****************************************************************************/
548 char *alloc_sub_basic(const char *smb_name
, const char *domain_name
,
551 char *b
, *p
, *s
, *r
, *a_string
;
552 fstring pidstr
, vnnstr
;
553 char addr
[INET6_ADDRSTRLEN
];
554 const char *local_machine_name
= get_local_machine_name();
555 TALLOC_CTX
*tmp_ctx
= NULL
;
557 /* workaround to prevent a crash while looking at bug #687 */
560 DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
564 a_string
= SMB_STRDUP(str
);
565 if (a_string
== NULL
) {
566 DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
570 tmp_ctx
= talloc_stackframe();
572 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
579 r
= strdup_lower(smb_name
);
583 a_string
= realloc_string_sub(a_string
, "%U", r
);
587 r
= SMB_STRDUP(smb_name
);
591 pass
= Get_Pwnam_alloc(tmp_ctx
, r
);
593 a_string
= realloc_string_sub(
595 gidtoname(pass
->pw_gid
));
601 r
= strdup_upper(domain_name
);
605 a_string
= realloc_string_sub(a_string
, "%D", r
);
609 client_addr(get_client_fd(), addr
, sizeof(addr
));
610 if (strnequal(addr
,"::ffff:",7)) {
613 a_string
= realloc_string_sub(a_string
, "%I",
618 a_string
= realloc_string_sub( a_string
, "%i",
619 client_socket_addr(get_client_fd(), addr
, sizeof(addr
)) );
622 if ( StrnCaseCmp(p
, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
625 if (local_machine_name
&& *local_machine_name
) {
626 a_string
= realloc_string_sub(a_string
, "%L", local_machine_name
);
628 a_string
= realloc_string_sub(a_string
, "%L", global_myname());
632 a_string
= realloc_string_sub(a_string
, "%N", automount_server(smb_name
));
635 a_string
= realloc_string_sub(a_string
, "%M", client_name(get_client_fd()));
638 a_string
= realloc_string_sub(a_string
, "%R", remote_proto
);
641 a_string
= realloc_string_sub(a_string
, "%T", current_timestring(tmp_ctx
, False
));
644 a_string
= realloc_string_sub(a_string
, "%a",
645 get_remote_arch_str());
648 slprintf(pidstr
,sizeof(pidstr
)-1, "%d",(int)sys_getpid());
649 a_string
= realloc_string_sub(a_string
, "%d", pidstr
);
652 a_string
= realloc_string_sub(a_string
, "%h", myhostname());
655 a_string
= realloc_string_sub(a_string
, "%m",
661 a_string
= realloc_string_sub(a_string
, "%v", samba_version_string());
664 a_string
= realloc_string_sub(a_string
, "%w", lp_winbind_separator());
667 a_string
= realloc_expand_env_var(a_string
, p
); /* Expand environment variables */
670 a_string
= realloc_expand_longvar( a_string
, p
);
673 slprintf(vnnstr
,sizeof(vnnstr
)-1, "%u", get_my_vnn());
674 a_string
= realloc_string_sub(a_string
, "%V", vnnstr
);
683 if (a_string
== NULL
) {
694 TALLOC_FREE(tmp_ctx
);
698 /****************************************************************************
699 Do some specific substitutions in a string.
700 This function will return an allocated string that have to be freed.
701 ****************************************************************************/
703 char *talloc_sub_specified(TALLOC_CTX
*mem_ctx
,
704 const char *input_string
,
705 const char *username
,
711 char *ret_string
= NULL
;
715 if (!(tmp_ctx
= talloc_new(mem_ctx
))) {
716 DEBUG(0, ("talloc_new failed\n"));
720 a_string
= talloc_strdup(tmp_ctx
, input_string
);
721 if (a_string
== NULL
) {
722 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
726 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
732 a_string
= talloc_string_sub(
733 tmp_ctx
, a_string
, "%U", username
);
736 a_string
= talloc_string_sub(
737 tmp_ctx
, a_string
, "%u", username
);
741 a_string
= talloc_string_sub(
742 tmp_ctx
, a_string
, "%G",
745 a_string
= talloc_string_sub(
752 a_string
= talloc_string_sub(
753 tmp_ctx
, a_string
, "%g",
756 a_string
= talloc_string_sub(
757 tmp_ctx
, a_string
, "%g", "NO_GROUP");
761 a_string
= talloc_string_sub(tmp_ctx
, a_string
,
765 a_string
= talloc_string_sub(
766 tmp_ctx
, a_string
, "%N",
767 automount_server(username
));
774 if (a_string
== NULL
) {
779 /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
780 * away with the TALLOC_FREE(tmp_ctx) further down. */
782 ret_string
= talloc_sub_basic(mem_ctx
, username
, domain
, a_string
);
785 TALLOC_FREE(tmp_ctx
);
789 /****************************************************************************
790 ****************************************************************************/
792 static char *alloc_sub_advanced(const char *servicename
, const char *user
,
793 const char *connectpath
, gid_t gid
,
794 const char *smb_name
, const char *domain_name
,
797 char *a_string
, *ret_string
;
800 a_string
= SMB_STRDUP(str
);
801 if (a_string
== NULL
) {
802 DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
806 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
812 a_string
= realloc_string_sub(a_string
, "%N", automount_server(user
));
816 if ((h
= get_user_home_dir(talloc_tos(), user
)))
817 a_string
= realloc_string_sub(a_string
, "%H", h
);
822 a_string
= realloc_string_sub(a_string
, "%P", connectpath
);
825 a_string
= realloc_string_sub(a_string
, "%S", servicename
);
828 a_string
= realloc_string_sub(a_string
, "%g", gidtoname(gid
));
831 a_string
= realloc_string_sub(a_string
, "%u", user
);
834 /* Patch from jkf@soton.ac.uk Left the %N (NIS
835 * server name) in standard_sub_basic as it is
836 * a feature for logon servers, hence uses the
837 * username. The %p (NIS server path) code is
838 * here as it is used instead of the default
839 * "path =" string in [homes] and so needs the
840 * service name, not the username. */
842 a_string
= realloc_string_sub(a_string
, "%p",
843 automount_path(servicename
));
851 if (a_string
== NULL
) {
856 ret_string
= alloc_sub_basic(smb_name
, domain_name
, a_string
);
862 * This obviously is inefficient and needs to be merged into
863 * alloc_sub_advanced...
866 char *talloc_sub_advanced(TALLOC_CTX
*mem_ctx
,
867 const char *servicename
, const char *user
,
868 const char *connectpath
, gid_t gid
,
869 const char *smb_name
, const char *domain_name
,
874 if (!(a
= alloc_sub_advanced(servicename
, user
, connectpath
, gid
,
875 smb_name
, domain_name
, str
))) {
878 t
= talloc_strdup(mem_ctx
, a
);
884 void standard_sub_advanced(const char *servicename
, const char *user
,
885 const char *connectpath
, gid_t gid
,
886 const char *smb_name
, const char *domain_name
,
887 char *str
, size_t len
)
891 s
= alloc_sub_advanced(servicename
, user
, connectpath
,
892 gid
, smb_name
, domain_name
, str
);
895 strncpy( str
, s
, len
);
900 /****************************************************************************
901 Do some standard substitutions in a string.
902 ****************************************************************************/
904 char *standard_sub_conn(TALLOC_CTX
*ctx
, connection_struct
*conn
, const char *str
)
906 return talloc_sub_advanced(ctx
,
907 lp_servicename(SNUM(conn
)),
908 conn
->server_info
->unix_name
,
910 conn
->server_info
->utok
.gid
,