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 extern struct current_user current_user;
26 userdom_struct current_user_info;
27 fstring remote_proto="UNKNOWN";
30 * Set the 'local' machine name
31 * @param local_name the name we are being called
32 * @param if this is the 'final' name for us, not be be changed again
35 static char *local_machine;
37 bool set_local_machine_name(const char *local_name, bool perm)
39 static bool already_perm = false;
40 char *tmp_local_machine = NULL;
41 char addr[INET6_ADDRSTRLEN];
44 tmp_local_machine = SMB_STRDUP(local_name);
45 if (!tmp_local_machine) {
48 trim_char(tmp_local_machine,' ',' ');
51 * Windows NT/2k uses "*SMBSERVER" and XP uses "*SMBSERV"
55 if (strequal(tmp_local_machine, "*SMBSERVER") ||
56 strequal(tmp_local_machine, "*SMBSERV") ) {
57 SAFE_FREE(local_machine);
58 local_machine = SMB_STRDUP(client_socket_addr(get_client_fd(),
59 addr, sizeof(addr)) );
60 SAFE_FREE(tmp_local_machine);
61 return local_machine ? true : false;
68 SAFE_FREE(local_machine);
69 len = strlen(tmp_local_machine);
70 local_machine = SMB_CALLOC_ARRAY(char, len+1);
72 SAFE_FREE(tmp_local_machine);
75 /* alpha_strcpy includes the space for the terminating nul. */
76 alpha_strcpy(local_machine,tmp_local_machine,
77 SAFE_NETBIOS_CHARS,len+1);
78 strlower_m(local_machine);
79 SAFE_FREE(tmp_local_machine);
86 const char *get_local_machine_name(void)
88 if (!local_machine || !*local_machine) {
89 return global_myname();
96 * Set the 'remote' machine name
97 * @param remote_name the name our client wants to be called by
98 * @param if this is the 'final' name for them, not be be changed again
101 static char *remote_machine;
103 bool set_remote_machine_name(const char *remote_name, bool perm)
105 static bool already_perm = False;
106 char *tmp_remote_machine;
113 tmp_remote_machine = SMB_STRDUP(remote_name);
114 if (!tmp_remote_machine) {
117 trim_char(tmp_remote_machine,' ',' ');
119 SAFE_FREE(remote_machine);
120 len = strlen(tmp_remote_machine);
121 remote_machine = SMB_CALLOC_ARRAY(char, len+1);
122 if (!remote_machine) {
123 SAFE_FREE(tmp_remote_machine);
127 /* alpha_strcpy includes the space for the terminating nul. */
128 alpha_strcpy(remote_machine,tmp_remote_machine,
129 SAFE_NETBIOS_CHARS,len+1);
130 strlower_m(remote_machine);
131 SAFE_FREE(tmp_remote_machine);
138 const char *get_remote_machine_name(void)
140 return remote_machine ? remote_machine : "";
143 /*******************************************************************
144 Setup the string used by %U substitution.
145 ********************************************************************/
147 static char *smb_user_name;
149 void sub_set_smb_name(const char *name)
153 bool is_machine_account = false;
155 /* don't let anonymous logins override the name */
156 if (!name || !*name) {
160 tmp = SMB_STRDUP(name);
164 trim_char(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 SAFE_FREE(smb_user_name);
184 smb_user_name = SMB_CALLOC_ARRAY(char, 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 const char *get_smb_user_name(void)
205 return smb_user_name ? smb_user_name : "";
208 /*******************************************************************
209 Setup the strings used by substitutions. Called per packet. Ensure
210 %U name is set correctly also.
211 ********************************************************************/
213 void set_current_user_info(const userdom_struct *pcui)
215 current_user_info = *pcui;
216 /* The following is safe as current_user_info.smb_name
217 * has already been sanitised in register_existing_vuid. */
219 sub_set_smb_name(current_user_info.smb_name);
222 /*******************************************************************
223 Return the current active user name.
224 *******************************************************************/
226 const char *get_current_username(void)
228 if (current_user_info.smb_name[0] == '\0' ) {
229 return get_smb_user_name();
232 return current_user_info.smb_name;
235 /*******************************************************************
236 Given a pointer to a %$(NAME) in p and the whole string in str
237 expand it as an environment variable.
238 Return a new allocated and expanded string.
239 Based on code by Branko Cibej <branko.cibej@hermes.si>
240 When this is called p points at the '%' character.
241 May substitute multiple occurrencies of the same env var.
242 ********************************************************************/
244 static char * realloc_expand_env_var(char *str, char *p)
251 if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
256 * Look for the terminating ')'.
259 if ((q = strchr_m(p,')')) == NULL) {
260 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
265 * Extract the name from within the %$(NAME) string.
271 /* reserve space for use later add %$() chars */
272 if ( (envname = (char *)SMB_MALLOC(copylen + 1 + 4)) == NULL ) {
276 strncpy(envname,r,copylen);
277 envname[copylen] = '\0';
279 if ((envval = getenv(envname)) == NULL) {
280 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
286 * Copy the full %$(NAME) into envname so it
291 strncpy(envname,p,copylen);
292 envname[copylen] = '\0';
293 r = realloc_string_sub(str, envname, envval);
299 /*******************************************************************
300 *******************************************************************/
302 static char *longvar_domainsid( void )
307 if ( !secrets_fetch_domain_sid( lp_workgroup(), &sid ) ) {
311 sid_string = SMB_STRDUP( sid_string_static( &sid ) );
314 DEBUG(0,("longvar_domainsid: failed to dup SID string!\n"));
320 /*******************************************************************
321 *******************************************************************/
328 static struct api_longvar longvar_table[] = {
329 { "DomainSID", longvar_domainsid },
333 static char *get_longvar_val( const char *varname )
337 DEBUG(7,("get_longvar_val: expanding variable [%s]\n", varname));
339 for ( i=0; longvar_table[i].name; i++ ) {
340 if ( strequal( longvar_table[i].name, varname ) ) {
341 return longvar_table[i].fn();
348 /*******************************************************************
349 Expand the long smb.conf variable names given a pointer to a %(NAME).
350 Return the number of characters by which the pointer should be advanced.
351 When this is called p points at the '%' character.
352 ********************************************************************/
354 static char *realloc_expand_longvar(char *str, char *p)
361 if ( p[0] != '%' || p[1] != '(' ) {
365 /* Look for the terminating ')'.*/
367 if ((q = strchr_m(p,')')) == NULL) {
368 DEBUG(0,("realloc_expand_longvar: Unterminated environment variable [%s]\n", p));
372 /* Extract the name from within the %(NAME) string.*/
375 copylen = MIN( (q-r), (sizeof(varname)-1) );
376 strncpy(varname, r, copylen);
377 varname[copylen] = '\0';
379 if ((value = get_longvar_val(varname)) == NULL) {
380 DEBUG(0,("realloc_expand_longvar: Variable [%s] not set. Skipping\n", varname));
384 /* Copy the full %(NAME) into envname so it can be replaced.*/
386 copylen = MIN( (q+1-p),(sizeof(varname)-1) );
387 strncpy( varname, p, copylen );
388 varname[copylen] = '\0';
389 r = realloc_string_sub(str, varname, value);
392 /* skip over the %(varname) */
397 /*******************************************************************
398 Patch from jkf@soton.ac.uk
399 Added this to implement %p (NIS auto-map version of %H)
400 *******************************************************************/
402 static const char *automount_path(const char *user_name)
404 TALLOC_CTX *ctx = talloc_tos();
405 const char *server_path;
407 /* use the passwd entry as the default */
408 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
410 server_path = talloc_strdup(ctx, get_user_home_dir(user_name));
415 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
417 if (lp_nis_home_map()) {
418 const char *home_path_start;
419 char *automount_value = automount_lookup(ctx, user_name);
421 if(automount_value && strlen(automount_value) > 0) {
422 home_path_start = strchr_m(automount_value,':');
423 if (home_path_start != NULL) {
424 DEBUG(5, ("NIS lookup succeeded. "
425 "Home path is: %s\n",
427 (home_path_start+1):""));
428 server_path = talloc_strdup(ctx,
432 /* NIS key lookup failed: default to
433 * user home directory from password file */
434 DEBUG(5, ("NIS lookup failed. Using Home path from "
435 "passwd file. Home path is: %s\n", server_path ));
443 DEBUG(4,("Home server path: %s\n", server_path));
447 /*******************************************************************
448 Patch from jkf@soton.ac.uk
449 This is Luke's original function with the NIS lookup code
450 moved out to a separate function.
451 *******************************************************************/
453 static const char *automount_server(const char *user_name)
455 TALLOC_CTX *ctx = talloc_tos();
456 const char *server_name;
457 const char *local_machine_name = get_local_machine_name();
459 /* use the local machine name as the default */
460 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
461 if (local_machine_name && *local_machine_name) {
462 server_name = talloc_strdup(ctx, local_machine_name);
464 server_name = talloc_strdup(ctx, global_myname());
471 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
472 if (lp_nis_home_map()) {
475 char *automount_value = automount_lookup(ctx, user_name);
476 if (!automount_value) {
479 srv = talloc_strdup(ctx, automount_value);
480 p = strchr_m(srv, ':');
486 DEBUG(5, ("NIS lookup succeeded. Home server %s\n",
494 DEBUG(4,("Home server: %s\n", server_name));
498 /****************************************************************************
499 Do some standard substitutions in a string.
500 len is the length in bytes of the space allowed in string str. If zero means
501 don't allow expansions.
502 ****************************************************************************/
504 void standard_sub_basic(const char *smb_name, const char *domain_name,
505 char *str, size_t len)
509 if ( (s = alloc_sub_basic( smb_name, domain_name, str )) != NULL ) {
510 strncpy( str, s, len );
517 /****************************************************************************
518 Do some standard substitutions in a string.
519 This function will return an allocated string that have to be freed.
520 ****************************************************************************/
522 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name,
523 const char *domain_name, const char *str)
527 if ( (a = alloc_sub_basic(smb_name, domain_name, str)) == NULL ) {
530 t = talloc_strdup(mem_ctx, a);
535 /****************************************************************************
536 ****************************************************************************/
538 char *alloc_sub_basic(const char *smb_name, const char *domain_name,
541 char *b, *p, *s, *r, *a_string;
542 fstring pidstr, vnnstr;
544 char addr[INET6_ADDRSTRLEN];
545 const char *local_machine_name = get_local_machine_name();
547 /* workaround to prevent a crash while looking at bug #687 */
550 DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
554 a_string = SMB_STRDUP(str);
555 if (a_string == NULL) {
556 DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
560 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
567 r = strdup_lower(smb_name);
571 a_string = realloc_string_sub(a_string, "%U", r);
574 r = SMB_STRDUP(smb_name);
578 if ((pass = Get_Pwnam(r))!=NULL) {
579 a_string = realloc_string_sub(a_string, "%G", gidtoname(pass->pw_gid));
583 r = strdup_upper(domain_name);
587 a_string = realloc_string_sub(a_string, "%D", r);
590 a_string = realloc_string_sub(a_string, "%I",
591 client_addr(get_client_fd(),addr, sizeof(addr)));
594 a_string = realloc_string_sub( a_string, "%i",
595 client_socket_addr(get_client_fd(), addr, sizeof(addr)) );
598 if ( StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
601 if (local_machine_name && *local_machine_name) {
602 a_string = realloc_string_sub(a_string, "%L", local_machine_name);
604 a_string = realloc_string_sub(a_string, "%L", global_myname());
608 a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
611 a_string = realloc_string_sub(a_string, "%M", client_name(get_client_fd()));
614 a_string = realloc_string_sub(a_string, "%R", remote_proto);
617 a_string = realloc_string_sub(a_string, "%T", current_timestring(False));
620 a_string = realloc_string_sub(a_string, "%a",
621 get_remote_arch_str());
624 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
625 a_string = realloc_string_sub(a_string, "%d", pidstr);
628 a_string = realloc_string_sub(a_string, "%h", myhostname());
631 a_string = realloc_string_sub(a_string, "%m", remote_machine);
634 a_string = realloc_string_sub(a_string, "%v", SAMBA_VERSION_STRING);
637 a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
640 a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
643 a_string = realloc_expand_longvar( a_string, p );
646 slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
647 a_string = realloc_string_sub(a_string, "%V", vnnstr);
668 /****************************************************************************
669 Do some specific substitutions in a string.
670 This function will return an allocated string that have to be freed.
671 ****************************************************************************/
673 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
674 const char *input_string,
675 const char *username,
681 char *ret_string = NULL;
685 if (!(tmp_ctx = talloc_new(mem_ctx))) {
686 DEBUG(0, ("talloc_new failed\n"));
690 a_string = talloc_strdup(tmp_ctx, input_string);
691 if (a_string == NULL) {
692 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
696 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
702 a_string = talloc_string_sub(
703 tmp_ctx, a_string, "%U", username);
706 a_string = talloc_string_sub(
707 tmp_ctx, a_string, "%u", username);
711 a_string = talloc_string_sub(
712 tmp_ctx, a_string, "%G",
715 a_string = talloc_string_sub(
722 a_string = talloc_string_sub(
723 tmp_ctx, a_string, "%g",
726 a_string = talloc_string_sub(
727 tmp_ctx, a_string, "%g", "NO_GROUP");
731 a_string = talloc_string_sub(tmp_ctx, a_string,
735 a_string = talloc_string_sub(
736 tmp_ctx, a_string, "%N",
737 automount_server(username));
744 if (a_string == NULL) {
749 /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
750 * away with the TALLOC_FREE(tmp_ctx) further down. */
752 ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
755 TALLOC_FREE(tmp_ctx);
759 /****************************************************************************
760 ****************************************************************************/
762 static char *alloc_sub_advanced(const char *servicename, const char *user,
763 const char *connectpath, gid_t gid,
764 const char *smb_name, const char *domain_name,
767 char *a_string, *ret_string;
770 a_string = SMB_STRDUP(str);
771 if (a_string == NULL) {
772 DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
776 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
782 a_string = realloc_string_sub(a_string, "%N", automount_server(user));
785 if ((h = get_user_home_dir(user)))
786 a_string = realloc_string_sub(a_string, "%H", h);
789 a_string = realloc_string_sub(a_string, "%P", connectpath);
792 a_string = realloc_string_sub(a_string, "%S", servicename);
795 a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
798 a_string = realloc_string_sub(a_string, "%u", user);
801 /* Patch from jkf@soton.ac.uk Left the %N (NIS
802 * server name) in standard_sub_basic as it is
803 * a feature for logon servers, hence uses the
804 * username. The %p (NIS server path) code is
805 * here as it is used instead of the default
806 * "path =" string in [homes] and so needs the
807 * service name, not the username. */
809 a_string = realloc_string_sub(a_string, "%p",
810 automount_path(servicename));
818 if (a_string == NULL) {
823 ret_string = alloc_sub_basic(smb_name, domain_name, a_string);
829 * This obviously is inefficient and needs to be merged into
830 * alloc_sub_advanced...
833 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
834 const char *servicename, const char *user,
835 const char *connectpath, gid_t gid,
836 const char *smb_name, const char *domain_name,
841 if (!(a = alloc_sub_advanced(servicename, user, connectpath, gid,
842 smb_name, domain_name, str))) {
845 t = talloc_strdup(mem_ctx, a);
851 void standard_sub_advanced(const char *servicename, const char *user,
852 const char *connectpath, gid_t gid,
853 const char *smb_name, const char *domain_name,
854 char *str, size_t len)
858 s = alloc_sub_advanced(servicename, user, connectpath,
859 gid, smb_name, domain_name, str);
862 strncpy( str, s, len );
867 /****************************************************************************
868 Do some standard substitutions in a string.
869 ****************************************************************************/
871 char *standard_sub_conn(TALLOC_CTX *ctx, connection_struct *conn, const char *str)
873 return talloc_sub_advanced(ctx,
874 lp_servicename(SNUM(conn)),