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 strlower_m(local_machine);
70 TALLOC_FREE(tmp_local_machine);
77 const char *get_local_machine_name(void)
79 if (!local_machine || !*local_machine) {
80 return lp_netbios_name();
87 * Set the 'remote' machine name
88 * @param remote_name the name our client wants to be called by
89 * @param if this is the 'final' name for them, not be be changed again
92 static char *remote_machine;
94 bool set_remote_machine_name(const char *remote_name, bool perm)
96 static bool already_perm = False;
97 char *tmp_remote_machine;
104 tmp_remote_machine = talloc_strdup(NULL, remote_name);
105 if (!tmp_remote_machine) {
108 trim_char(tmp_remote_machine,' ',' ');
110 TALLOC_FREE(remote_machine);
111 len = strlen(tmp_remote_machine);
112 remote_machine = (char *)TALLOC_ZERO(NULL, len+1);
113 if (!remote_machine) {
114 TALLOC_FREE(tmp_remote_machine);
118 /* alpha_strcpy includes the space for the terminating nul. */
119 alpha_strcpy(remote_machine,tmp_remote_machine,
120 SAFE_NETBIOS_CHARS,len+1);
121 strlower_m(remote_machine);
122 TALLOC_FREE(tmp_remote_machine);
129 const char *get_remote_machine_name(void)
131 return remote_machine ? remote_machine : "";
134 /*******************************************************************
135 Setup the string used by %U substitution.
136 ********************************************************************/
138 static char *smb_user_name;
140 void sub_set_smb_name(const char *name)
144 bool is_machine_account = false;
146 /* don't let anonymous logins override the name */
147 if (!name || !*name) {
151 tmp = talloc_strdup(NULL, name);
155 trim_char(tmp, ' ', ' ');
165 /* long story but here goes....we have to allow usernames
166 ending in '$' as they are valid machine account names.
167 So check for a machine account and re-add the '$'
168 at the end after the call to alpha_strcpy(). --jerry */
170 if (tmp[len-1] == '$') {
171 is_machine_account = True;
174 TALLOC_FREE(smb_user_name);
175 smb_user_name = (char *)TALLOC_ZERO(NULL, len+1);
176 if (!smb_user_name) {
181 /* alpha_strcpy includes the space for the terminating nul. */
182 alpha_strcpy(smb_user_name, tmp,
188 if (is_machine_account) {
189 len = strlen(smb_user_name);
190 smb_user_name[len-1] = '$';
194 static char sub_peeraddr[INET6_ADDRSTRLEN];
195 static const char *sub_peername = NULL;
196 static char sub_sockaddr[INET6_ADDRSTRLEN];
198 void sub_set_socket_ids(const char *peeraddr, const char *peername,
199 const char *sockaddr)
201 const char *addr = peeraddr;
203 if (strnequal(addr, "::ffff:", 7)) {
206 strlcpy(sub_peeraddr, addr, sizeof(sub_peeraddr));
208 if (sub_peername != NULL &&
209 sub_peername != sub_peeraddr) {
210 talloc_free(discard_const_p(char,sub_peername));
213 sub_peername = talloc_strdup(NULL, peername);
214 if (sub_peername == NULL) {
215 sub_peername = sub_peeraddr;
219 * Shouldn't we do the ::ffff: cancellation here as well? The
220 * original code in talloc_sub_basic() did not do it, so I'm
221 * leaving it out here as well for compatibility.
223 strlcpy(sub_sockaddr, sockaddr, sizeof(sub_sockaddr));
226 static const char *get_smb_user_name(void)
228 return smb_user_name ? smb_user_name : "";
231 /*******************************************************************
232 Setup the strings used by substitutions. Called per packet. Ensure
233 %U name is set correctly also.
235 smb_name must be sanitized by alpha_strcpy
236 ********************************************************************/
238 void set_current_user_info(const char *smb_name, const char *unix_name,
241 fstrcpy(current_user_info.smb_name, smb_name);
242 fstrcpy(current_user_info.unix_name, unix_name);
243 fstrcpy(current_user_info.domain, domain);
245 /* The following is safe as current_user_info.smb_name
246 * has already been sanitised in register_existing_vuid. */
248 sub_set_smb_name(current_user_info.smb_name);
251 /*******************************************************************
252 Return the current active user name.
253 *******************************************************************/
255 const char *get_current_username(void)
257 if (current_user_info.smb_name[0] == '\0' ) {
258 return get_smb_user_name();
261 return current_user_info.smb_name;
264 /*******************************************************************
265 Given a pointer to a %$(NAME) in p and the whole string in str
266 expand it as an environment variable.
267 str must be a talloced string.
268 Return a new allocated and expanded string.
269 Based on code by Branko Cibej <branko.cibej@hermes.si>
270 When this is called p points at the '%' character.
271 May substitute multiple occurrencies of the same env var.
272 ********************************************************************/
274 static char *realloc_expand_env_var(char *str, char *p)
281 if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
286 * Look for the terminating ')'.
289 if ((q = strchr_m(p,')')) == NULL) {
290 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
295 * Extract the name from within the %$(NAME) string.
301 /* reserve space for use later add %$() chars */
302 if ( (envname = talloc_array(talloc_tos(), char, copylen + 1 + 4)) == NULL ) {
306 strncpy(envname,r,copylen);
307 envname[copylen] = '\0';
309 if ((envval = getenv(envname)) == NULL) {
310 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
311 TALLOC_FREE(envname);
316 * Copy the full %$(NAME) into envname so it
321 strncpy(envname,p,copylen);
322 envname[copylen] = '\0';
323 r = realloc_string_sub(str, envname, envval);
324 TALLOC_FREE(envname);
329 /*******************************************************************
330 Patch from jkf@soton.ac.uk
331 Added this to implement %p (NIS auto-map version of %H)
332 *******************************************************************/
334 static const char *automount_path(const char *user_name)
336 TALLOC_CTX *ctx = talloc_tos();
337 const char *server_path;
339 /* use the passwd entry as the default */
340 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
342 server_path = talloc_strdup(ctx, get_user_home_dir(ctx, user_name));
347 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
349 if (lp_nis_home_map()) {
350 const char *home_path_start;
351 char *automount_value = automount_lookup(ctx, user_name);
353 if(automount_value && strlen(automount_value) > 0) {
354 home_path_start = strchr_m(automount_value,':');
355 if (home_path_start != NULL) {
356 DEBUG(5, ("NIS lookup succeeded. "
357 "Home path is: %s\n",
359 (home_path_start+1):""));
360 server_path = talloc_strdup(ctx,
367 /* NIS key lookup failed: default to
368 * user home directory from password file */
369 DEBUG(5, ("NIS lookup failed. Using Home path from "
370 "passwd file. Home path is: %s\n", server_path ));
375 DEBUG(4,("Home server path: %s\n", server_path));
379 /*******************************************************************
380 Patch from jkf@soton.ac.uk
381 This is Luke's original function with the NIS lookup code
382 moved out to a separate function.
383 *******************************************************************/
385 static const char *automount_server(const char *user_name)
387 TALLOC_CTX *ctx = talloc_tos();
388 const char *server_name;
389 const char *local_machine_name = get_local_machine_name();
391 /* use the local machine name as the default */
392 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
393 if (local_machine_name && *local_machine_name) {
394 server_name = talloc_strdup(ctx, local_machine_name);
396 server_name = talloc_strdup(ctx, lp_netbios_name());
403 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
404 if (lp_nis_home_map()) {
407 char *automount_value = automount_lookup(ctx, user_name);
408 if (!automount_value) {
411 srv = talloc_strdup(ctx, automount_value);
415 p = strchr_m(srv, ':');
421 DEBUG(5, ("NIS lookup succeeded. Home server %s\n",
426 DEBUG(4,("Home server: %s\n", server_name));
430 /****************************************************************************
431 Do some standard substitutions in a string.
432 len is the length in bytes of the space allowed in string str. If zero means
433 don't allow expansions.
434 ****************************************************************************/
436 void standard_sub_basic(const char *smb_name, const char *domain_name,
437 char *str, size_t len)
441 if ( (s = talloc_sub_basic(talloc_tos(), smb_name, domain_name, str )) != NULL ) {
442 strncpy( str, s, len );
448 /****************************************************************************
449 Do some standard substitutions in a string.
450 This function will return an talloced string that has to be freed.
451 ****************************************************************************/
453 char *talloc_sub_basic(TALLOC_CTX *mem_ctx,
454 const char *smb_name,
455 const char *domain_name,
458 char *b, *p, *s, *r, *a_string;
459 fstring pidstr, vnnstr;
460 const char *local_machine_name = get_local_machine_name();
461 TALLOC_CTX *tmp_ctx = NULL;
463 /* workaround to prevent a crash while looking at bug #687 */
466 DEBUG(0,("talloc_sub_basic: NULL source string! This should not happen\n"));
470 a_string = talloc_strdup(mem_ctx, str);
471 if (a_string == NULL) {
472 DEBUG(0, ("talloc_sub_basic: Out of memory!\n"));
476 tmp_ctx = talloc_stackframe();
478 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
485 r = strlower_talloc(tmp_ctx, smb_name);
489 a_string = realloc_string_sub(a_string, "%U", r);
493 r = talloc_strdup(tmp_ctx, smb_name);
497 pass = Get_Pwnam_alloc(tmp_ctx, r);
499 a_string = realloc_string_sub(
501 gidtoname(pass->pw_gid));
507 r = strupper_talloc(tmp_ctx, domain_name);
511 a_string = realloc_string_sub(a_string, "%D", r);
514 a_string = realloc_string_sub(
516 sub_peeraddr[0] ? sub_peeraddr : "0.0.0.0");
520 a_string = realloc_string_sub(
522 sub_sockaddr[0] ? sub_sockaddr : "0.0.0.0");
525 if ( strncasecmp_m(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
528 if (local_machine_name && *local_machine_name) {
529 a_string = realloc_string_sub(a_string, "%L", local_machine_name);
531 a_string = realloc_string_sub(a_string, "%L", lp_netbios_name());
535 a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
538 a_string = realloc_string_sub(a_string, "%M",
539 sub_peername ? sub_peername : "");
542 a_string = realloc_string_sub(a_string, "%R", remote_proto);
545 a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False));
548 a_string = realloc_string_sub(a_string, "%a",
549 get_remote_arch_str());
552 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)getpid());
553 a_string = realloc_string_sub(a_string, "%d", pidstr);
556 a_string = realloc_string_sub(a_string, "%h", myhostname());
559 a_string = realloc_string_sub(a_string, "%m",
565 a_string = realloc_string_sub(a_string, "%v", samba_version_string());
568 a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
571 a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
574 slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
575 a_string = realloc_string_sub(a_string, "%V", vnnstr);
584 if (a_string == NULL) {
592 TALLOC_FREE(a_string);
595 TALLOC_FREE(tmp_ctx);
599 /****************************************************************************
600 Do some specific substitutions in a string.
601 This function will return an allocated string that have to be freed.
602 ****************************************************************************/
604 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
605 const char *input_string,
606 const char *username,
612 char *ret_string = NULL;
616 if (!(tmp_ctx = talloc_new(mem_ctx))) {
617 DEBUG(0, ("talloc_new failed\n"));
621 a_string = talloc_strdup(tmp_ctx, input_string);
622 if (a_string == NULL) {
623 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
627 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
633 a_string = talloc_string_sub(
634 tmp_ctx, a_string, "%U", username);
637 a_string = talloc_string_sub(
638 tmp_ctx, a_string, "%u", username);
642 a_string = talloc_string_sub(
643 tmp_ctx, a_string, "%G",
646 a_string = talloc_string_sub(
653 a_string = talloc_string_sub(
654 tmp_ctx, a_string, "%g",
657 a_string = talloc_string_sub(
658 tmp_ctx, a_string, "%g", "NO_GROUP");
662 a_string = talloc_string_sub(tmp_ctx, a_string,
666 a_string = talloc_string_sub(
667 tmp_ctx, a_string, "%N",
668 automount_server(username));
675 if (a_string == NULL) {
680 /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
681 * away with the TALLOC_FREE(tmp_ctx) further down. */
683 ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
686 TALLOC_FREE(tmp_ctx);
690 /****************************************************************************
691 ****************************************************************************/
693 char *talloc_sub_advanced(TALLOC_CTX *ctx,
694 const char *servicename,
696 const char *connectpath,
698 const char *smb_name,
699 const char *domain_name,
702 char *a_string, *ret_string;
705 a_string = talloc_strdup(talloc_tos(), str);
706 if (a_string == NULL) {
707 DEBUG(0, ("talloc_sub_advanced: Out of memory!\n"));
711 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
717 a_string = realloc_string_sub(a_string, "%N", automount_server(user));
721 if ((h = get_user_home_dir(talloc_tos(), user)))
722 a_string = realloc_string_sub(a_string, "%H", h);
727 a_string = realloc_string_sub(a_string, "%P", connectpath);
730 a_string = realloc_string_sub(a_string, "%S", servicename);
733 a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
736 a_string = realloc_string_sub(a_string, "%u", user);
739 /* Patch from jkf@soton.ac.uk Left the %N (NIS
740 * server name) in standard_sub_basic as it is
741 * a feature for logon servers, hence uses the
742 * username. The %p (NIS server path) code is
743 * here as it is used instead of the default
744 * "path =" string in [homes] and so needs the
745 * service name, not the username. */
747 a_string = realloc_string_sub(a_string, "%p",
748 automount_path(servicename));
756 if (a_string == NULL) {
761 ret_string = talloc_sub_basic(ctx, smb_name, domain_name, a_string);
762 TALLOC_FREE(a_string);
766 void standard_sub_advanced(const char *servicename, const char *user,
767 const char *connectpath, gid_t gid,
768 const char *smb_name, const char *domain_name,
769 char *str, size_t len)
771 char *s = talloc_sub_advanced(talloc_tos(),
772 servicename, user, connectpath,
773 gid, smb_name, domain_name, str);
778 strlcpy( str, s, len );
782 /******************************************************************************
783 version of standard_sub_basic() for string lists; uses talloc_sub_basic()
785 *****************************************************************************/
787 bool str_list_sub_basic( char **list, const char *smb_name,
788 const char *domain_name )
790 TALLOC_CTX *ctx = list;
795 tmpstr = talloc_sub_basic(ctx, smb_name, domain_name, s);
797 DEBUG(0,("str_list_sub_basic: "
798 "talloc_sub_basic() return NULL!\n"));