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/>.
23 #include "system/passwd.h"
27 userdom_struct current_user_info
;
28 fstring remote_proto
="UNKNOWN";
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
36 static char *local_machine
;
38 void free_local_machine_name(void)
40 TALLOC_FREE(local_machine
);
43 bool set_local_machine_name(const char *local_name
, bool perm
)
45 static bool already_perm
= false;
46 char *tmp_local_machine
= NULL
;
53 tmp_local_machine
= talloc_strdup(NULL
, local_name
);
54 if (!tmp_local_machine
) {
57 trim_char(tmp_local_machine
,' ',' ');
59 TALLOC_FREE(local_machine
);
60 len
= strlen(tmp_local_machine
);
61 local_machine
= (char *)TALLOC_ZERO(NULL
, len
+1);
63 TALLOC_FREE(tmp_local_machine
);
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
);
73 TALLOC_FREE(tmp_local_machine
);
80 const char *get_local_machine_name(void)
82 if (!local_machine
|| !*local_machine
) {
83 return lp_netbios_name();
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
95 static char *remote_machine
;
97 bool set_remote_machine_name(const char *remote_name
, bool perm
)
99 static bool already_perm
= False
;
100 char *tmp_remote_machine
;
107 tmp_remote_machine
= talloc_strdup(NULL
, remote_name
);
108 if (!tmp_remote_machine
) {
111 trim_char(tmp_remote_machine
,' ',' ');
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
);
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
);
128 TALLOC_FREE(tmp_remote_machine
);
135 const char *get_remote_machine_name(void)
137 return remote_machine
? remote_machine
: "";
140 /*******************************************************************
141 Setup the string used by %U substitution.
142 ********************************************************************/
144 static char *smb_user_name
;
146 void sub_set_smb_name(const char *name
)
150 bool is_machine_account
= false;
152 /* don't let anonymous logins override the name */
153 if (!name
|| !*name
) {
157 tmp
= talloc_strdup(NULL
, name
);
161 trim_char(tmp
, ' ', ' ');
162 if (!strlower_m(tmp
)) {
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 */
179 if (tmp
[len
-1] == '$') {
180 is_machine_account
= True
;
183 TALLOC_FREE(smb_user_name
);
184 smb_user_name
= (char *)TALLOC_ZERO(NULL
, len
+1);
185 if (!smb_user_name
) {
190 /* alpha_strcpy includes the space for the terminating nul. */
191 alpha_strcpy(smb_user_name
, tmp
,
197 if (is_machine_account
) {
198 len
= strlen(smb_user_name
);
199 smb_user_name
[len
-1] = '$';
203 static char sub_peeraddr
[INET6_ADDRSTRLEN
];
204 static const char *sub_peername
= NULL
;
205 static char sub_sockaddr
[INET6_ADDRSTRLEN
];
207 void sub_set_socket_ids(const char *peeraddr
, const char *peername
,
208 const char *sockaddr
)
210 const char *addr
= peeraddr
;
212 if (strnequal(addr
, "::ffff:", 7)) {
215 strlcpy(sub_peeraddr
, addr
, sizeof(sub_peeraddr
));
217 if (sub_peername
!= NULL
&&
218 sub_peername
!= sub_peeraddr
) {
219 talloc_free(discard_const_p(char,sub_peername
));
222 sub_peername
= talloc_strdup(NULL
, peername
);
223 if (sub_peername
== NULL
) {
224 sub_peername
= sub_peeraddr
;
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.
232 strlcpy(sub_sockaddr
, sockaddr
, sizeof(sub_sockaddr
));
235 static const char *get_smb_user_name(void)
237 return smb_user_name
? smb_user_name
: "";
240 /*******************************************************************
241 Setup the strings used by substitutions. Called per packet. Ensure
242 %U name is set correctly also.
244 smb_name must be sanitized by alpha_strcpy
245 ********************************************************************/
247 void set_current_user_info(const char *smb_name
, const char *unix_name
,
250 fstrcpy(current_user_info
.smb_name
, smb_name
);
251 fstrcpy(current_user_info
.unix_name
, unix_name
);
252 fstrcpy(current_user_info
.domain
, domain
);
254 /* The following is safe as current_user_info.smb_name
255 * has already been sanitised in register_existing_vuid. */
257 sub_set_smb_name(current_user_info
.smb_name
);
260 /*******************************************************************
261 Return the current active user name.
262 *******************************************************************/
264 const char *get_current_username(void)
266 if (current_user_info
.smb_name
[0] == '\0' ) {
267 return get_smb_user_name();
270 return current_user_info
.smb_name
;
273 /*******************************************************************
274 Given a pointer to a %$(NAME) in p and the whole string in str
275 expand it as an environment variable.
276 str must be a talloced string.
277 Return a new allocated and expanded string.
278 Based on code by Branko Cibej <branko.cibej@hermes.si>
279 When this is called p points at the '%' character.
280 May substitute multiple occurrencies of the same env var.
281 ********************************************************************/
283 static char *realloc_expand_env_var(char *str
, char *p
)
290 if (p
[0] != '%' || p
[1] != '$' || p
[2] != '(') {
295 * Look for the terminating ')'.
298 if ((q
= strchr_m(p
,')')) == NULL
) {
299 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p
));
304 * Extract the name from within the %$(NAME) string.
310 /* reserve space for use later add %$() chars */
311 if ( (envname
= talloc_array(talloc_tos(), char, copylen
+ 1 + 4)) == NULL
) {
315 strncpy(envname
,r
,copylen
);
316 envname
[copylen
] = '\0';
318 if ((envval
= getenv(envname
)) == NULL
) {
319 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname
));
320 TALLOC_FREE(envname
);
325 * Copy the full %$(NAME) into envname so it
330 strncpy(envname
,p
,copylen
);
331 envname
[copylen
] = '\0';
332 r
= realloc_string_sub(str
, envname
, envval
);
333 TALLOC_FREE(envname
);
338 /*******************************************************************
339 Patch from jkf@soton.ac.uk
340 Added this to implement %p (NIS auto-map version of %H)
341 *******************************************************************/
343 static const char *automount_path(const char *user_name
)
345 TALLOC_CTX
*ctx
= talloc_tos();
346 const char *server_path
;
348 /* use the passwd entry as the default */
349 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
351 server_path
= talloc_strdup(ctx
, get_user_home_dir(ctx
, user_name
));
356 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
358 if (lp_nis_home_map()) {
359 const char *home_path_start
;
360 char *automount_value
= automount_lookup(ctx
, user_name
);
362 if(automount_value
&& strlen(automount_value
) > 0) {
363 home_path_start
= strchr_m(automount_value
,':');
364 if (home_path_start
!= NULL
) {
365 DEBUG(5, ("NIS lookup succeeded. "
366 "Home path is: %s\n",
368 (home_path_start
+1):""));
369 server_path
= talloc_strdup(ctx
,
376 /* NIS key lookup failed: default to
377 * user home directory from password file */
378 DEBUG(5, ("NIS lookup failed. Using Home path from "
379 "passwd file. Home path is: %s\n", server_path
));
384 DEBUG(4,("Home server path: %s\n", server_path
));
388 /*******************************************************************
389 Patch from jkf@soton.ac.uk
390 This is Luke's original function with the NIS lookup code
391 moved out to a separate function.
392 *******************************************************************/
394 static const char *automount_server(const char *user_name
)
396 TALLOC_CTX
*ctx
= talloc_tos();
397 const char *server_name
;
398 const char *local_machine_name
= get_local_machine_name();
400 /* use the local machine name as the default */
401 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
402 if (local_machine_name
&& *local_machine_name
) {
403 server_name
= talloc_strdup(ctx
, local_machine_name
);
405 server_name
= talloc_strdup(ctx
, lp_netbios_name());
412 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
413 if (lp_nis_home_map()) {
416 char *automount_value
= automount_lookup(ctx
, user_name
);
417 if (!automount_value
) {
420 srv
= talloc_strdup(ctx
, automount_value
);
424 p
= strchr_m(srv
, ':');
430 DEBUG(5, ("NIS lookup succeeded. Home server %s\n",
435 DEBUG(4,("Home server: %s\n", server_name
));
439 /****************************************************************************
440 Do some standard substitutions in a string.
441 len is the length in bytes of the space allowed in string str. If zero means
442 don't allow expansions.
443 ****************************************************************************/
445 void standard_sub_basic(const char *smb_name
, const char *domain_name
,
446 char *str
, size_t len
)
450 if ( (s
= talloc_sub_basic(talloc_tos(), smb_name
, domain_name
, str
)) != NULL
) {
451 strncpy( str
, s
, len
);
457 /****************************************************************************
458 Do some standard substitutions in a string.
459 This function will return an talloced string that has to be freed.
460 ****************************************************************************/
462 char *talloc_sub_basic(TALLOC_CTX
*mem_ctx
,
463 const char *smb_name
,
464 const char *domain_name
,
467 char *b
, *p
, *s
, *r
, *a_string
;
468 fstring pidstr
, vnnstr
;
469 const char *local_machine_name
= get_local_machine_name();
470 TALLOC_CTX
*tmp_ctx
= NULL
;
472 /* workaround to prevent a crash while looking at bug #687 */
475 DEBUG(0,("talloc_sub_basic: NULL source string! This should not happen\n"));
479 a_string
= talloc_strdup(mem_ctx
, str
);
480 if (a_string
== NULL
) {
481 DEBUG(0, ("talloc_sub_basic: Out of memory!\n"));
485 tmp_ctx
= talloc_stackframe();
487 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
494 r
= strlower_talloc(tmp_ctx
, smb_name
);
498 a_string
= realloc_string_sub(a_string
, "%U", r
);
502 r
= talloc_strdup(tmp_ctx
, smb_name
);
506 pass
= Get_Pwnam_alloc(tmp_ctx
, r
);
508 a_string
= realloc_string_sub(
510 gidtoname(pass
->pw_gid
));
516 r
= strupper_talloc(tmp_ctx
, domain_name
);
520 a_string
= realloc_string_sub(a_string
, "%D", r
);
523 a_string
= realloc_string_sub(
525 sub_peeraddr
[0] ? sub_peeraddr
: "0.0.0.0");
529 a_string
= realloc_string_sub(
531 sub_sockaddr
[0] ? sub_sockaddr
: "0.0.0.0");
534 if ( strncasecmp_m(p
, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
537 if (local_machine_name
&& *local_machine_name
) {
538 a_string
= realloc_string_sub(a_string
, "%L", local_machine_name
);
540 a_string
= realloc_string_sub(a_string
, "%L", lp_netbios_name());
544 a_string
= realloc_string_sub(a_string
, "%N", automount_server(smb_name
));
547 a_string
= realloc_string_sub(a_string
, "%M",
548 sub_peername
? sub_peername
: "");
551 a_string
= realloc_string_sub(a_string
, "%R", remote_proto
);
554 a_string
= realloc_string_sub(a_string
, "%T", current_timestring(tmp_ctx
, False
));
557 a_string
= realloc_string_sub(a_string
, "%a",
558 get_remote_arch_str());
561 slprintf(pidstr
,sizeof(pidstr
)-1, "%d",(int)getpid());
562 a_string
= realloc_string_sub(a_string
, "%d", pidstr
);
565 a_string
= realloc_string_sub(a_string
, "%h", myhostname());
568 a_string
= realloc_string_sub(a_string
, "%m",
574 a_string
= realloc_string_sub(a_string
, "%v", samba_version_string());
577 a_string
= realloc_string_sub(a_string
, "%w", lp_winbind_separator());
580 a_string
= realloc_expand_env_var(a_string
, p
); /* Expand environment variables */
583 slprintf(vnnstr
,sizeof(vnnstr
)-1, "%u", get_my_vnn());
584 a_string
= realloc_string_sub(a_string
, "%V", vnnstr
);
593 if (a_string
== NULL
) {
601 TALLOC_FREE(a_string
);
604 TALLOC_FREE(tmp_ctx
);
608 /****************************************************************************
609 Do some specific substitutions in a string.
610 This function will return an allocated string that have to be freed.
611 ****************************************************************************/
613 char *talloc_sub_specified(TALLOC_CTX
*mem_ctx
,
614 const char *input_string
,
615 const char *username
,
621 char *ret_string
= NULL
;
625 if (!(tmp_ctx
= talloc_new(mem_ctx
))) {
626 DEBUG(0, ("talloc_new failed\n"));
630 a_string
= talloc_strdup(tmp_ctx
, input_string
);
631 if (a_string
== NULL
) {
632 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
636 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
642 a_string
= talloc_string_sub(
643 tmp_ctx
, a_string
, "%U", username
);
646 a_string
= talloc_string_sub(
647 tmp_ctx
, a_string
, "%u", username
);
651 a_string
= talloc_string_sub(
652 tmp_ctx
, a_string
, "%G",
655 a_string
= talloc_string_sub(
662 a_string
= talloc_string_sub(
663 tmp_ctx
, a_string
, "%g",
666 a_string
= talloc_string_sub(
667 tmp_ctx
, a_string
, "%g", "NO_GROUP");
671 a_string
= talloc_string_sub(tmp_ctx
, a_string
,
675 a_string
= talloc_string_sub(
676 tmp_ctx
, a_string
, "%N",
677 automount_server(username
));
684 if (a_string
== NULL
) {
689 /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
690 * away with the TALLOC_FREE(tmp_ctx) further down. */
692 ret_string
= talloc_sub_basic(mem_ctx
, username
, domain
, a_string
);
695 TALLOC_FREE(tmp_ctx
);
699 /****************************************************************************
700 ****************************************************************************/
702 char *talloc_sub_advanced(TALLOC_CTX
*ctx
,
703 const char *servicename
,
705 const char *connectpath
,
707 const char *smb_name
,
708 const char *domain_name
,
711 char *a_string
, *ret_string
;
714 a_string
= talloc_strdup(talloc_tos(), str
);
715 if (a_string
== NULL
) {
716 DEBUG(0, ("talloc_sub_advanced: Out of memory!\n"));
720 for (b
= s
= a_string
; (p
= strchr_m(s
, '%')); s
= a_string
+ (p
- b
)) {
726 a_string
= realloc_string_sub(a_string
, "%N", automount_server(user
));
730 if ((h
= get_user_home_dir(talloc_tos(), user
)))
731 a_string
= realloc_string_sub(a_string
, "%H", h
);
736 a_string
= realloc_string_sub(a_string
, "%P", connectpath
);
739 a_string
= realloc_string_sub(a_string
, "%S", servicename
);
742 a_string
= realloc_string_sub(a_string
, "%g", gidtoname(gid
));
745 a_string
= realloc_string_sub(a_string
, "%u", user
);
748 /* Patch from jkf@soton.ac.uk Left the %N (NIS
749 * server name) in standard_sub_basic as it is
750 * a feature for logon servers, hence uses the
751 * username. The %p (NIS server path) code is
752 * here as it is used instead of the default
753 * "path =" string in [homes] and so needs the
754 * service name, not the username. */
756 a_string
= realloc_string_sub(a_string
, "%p",
757 automount_path(servicename
));
765 if (a_string
== NULL
) {
770 ret_string
= talloc_sub_basic(ctx
, smb_name
, domain_name
, a_string
);
771 TALLOC_FREE(a_string
);
775 void standard_sub_advanced(const char *servicename
, const char *user
,
776 const char *connectpath
, gid_t gid
,
777 const char *smb_name
, const char *domain_name
,
778 char *str
, size_t len
)
780 char *s
= talloc_sub_advanced(talloc_tos(),
781 servicename
, user
, connectpath
,
782 gid
, smb_name
, domain_name
, str
);
787 strlcpy( str
, s
, len
);
791 /******************************************************************************
792 version of standard_sub_basic() for string lists; uses talloc_sub_basic()
794 *****************************************************************************/
796 bool str_list_sub_basic( char **list
, const char *smb_name
,
797 const char *domain_name
)
799 TALLOC_CTX
*ctx
= list
;
804 tmpstr
= talloc_sub_basic(ctx
, smb_name
, domain_name
, s
);
806 DEBUG(0,("str_list_sub_basic: "
807 "talloc_sub_basic() return NULL!\n"));