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 fstring local_machine="";
27 fstring remote_arch="UNKNOWN";
28 userdom_struct current_user_info;
29 fstring remote_proto="UNKNOWN";
31 static fstring remote_machine;
32 static fstring smb_user_name;
35 * Set the 'local' machine name
36 * @param local_name the name we are being called
37 * @param if this is the 'final' name for us, not be be changed again
40 void set_local_machine_name(const char* local_name, bool perm)
42 static bool already_perm = False;
43 fstring tmp_local_machine;
44 char addr[INET6_ADDRSTRLEN];
46 fstrcpy(tmp_local_machine,local_name);
47 trim_char(tmp_local_machine,' ',' ');
50 * Windows NT/2k uses "*SMBSERVER" and XP uses "*SMBSERV"
54 if ( strequal(tmp_local_machine, "*SMBSERVER") || strequal(tmp_local_machine, "*SMBSERV") ) {
55 fstrcpy( local_machine,
56 client_socket_addr(get_client_fd(), addr, sizeof(addr)) );
65 alpha_strcpy(local_machine,tmp_local_machine,SAFE_NETBIOS_CHARS,sizeof(local_machine)-1);
66 strlower_m(local_machine);
70 * Set the 'remote' machine name
71 * @param remote_name the name our client wants to be called by
72 * @param if this is the 'final' name for them, not be be changed again
75 void set_remote_machine_name(const char* remote_name, bool perm)
77 static bool already_perm = False;
78 fstring tmp_remote_machine;
85 fstrcpy(tmp_remote_machine,remote_name);
86 trim_char(tmp_remote_machine,' ',' ');
87 alpha_strcpy(remote_machine,tmp_remote_machine,SAFE_NETBIOS_CHARS,sizeof(remote_machine)-1);
88 strlower_m(remote_machine);
91 const char* get_remote_machine_name(void)
93 return remote_machine;
96 const char* get_local_machine_name(void)
98 if (!*local_machine) {
99 return global_myname();
102 return local_machine;
105 /*******************************************************************
106 Setup the string used by %U substitution.
107 ********************************************************************/
109 void sub_set_smb_name(const char *name)
113 bool is_machine_account = False;
115 /* don't let anonymous logins override the name */
120 fstrcpy( tmp, name );
121 trim_char( tmp, ' ', ' ' );
129 /* long story but here goes....we have to allow usernames
130 ending in '$' as they are valid machine account names.
131 So check for a machine account and re-add the '$'
132 at the end after the call to alpha_strcpy(). --jerry */
134 if ( tmp[len-1] == '$' )
135 is_machine_account = True;
137 alpha_strcpy( smb_user_name, tmp, SAFE_NETBIOS_CHARS, sizeof(smb_user_name)-1 );
139 if ( is_machine_account ) {
140 len = strlen( smb_user_name );
141 smb_user_name[len-1] = '$';
145 /*******************************************************************
146 Setup the strings used by substitutions. Called per packet. Ensure
147 %U name is set correctly also.
148 ********************************************************************/
150 void set_current_user_info(const userdom_struct *pcui)
152 current_user_info = *pcui;
153 /* The following is safe as current_user_info.smb_name
154 * has already been sanitised in register_existing_vuid. */
155 fstrcpy(smb_user_name, current_user_info.smb_name);
158 /*******************************************************************
159 return the current active user name
160 *******************************************************************/
162 const char* get_current_username( void )
164 if ( current_user_info.smb_name[0] == '\0' )
165 return smb_user_name;
167 return current_user_info.smb_name;
170 /*******************************************************************
171 Given a pointer to a %$(NAME) in p and the whole string in str
172 expand it as an environment variable.
173 Return a new allocated and expanded string.
174 Based on code by Branko Cibej <branko.cibej@hermes.si>
175 When this is called p points at the '%' character.
176 May substitute multiple occurrencies of the same env var.
177 ********************************************************************/
179 static char * realloc_expand_env_var(char *str, char *p)
186 if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
191 * Look for the terminating ')'.
194 if ((q = strchr_m(p,')')) == NULL) {
195 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
200 * Extract the name from within the %$(NAME) string.
206 /* reserve space for use later add %$() chars */
207 if ( (envname = (char *)SMB_MALLOC(copylen + 1 + 4)) == NULL ) {
211 strncpy(envname,r,copylen);
212 envname[copylen] = '\0';
214 if ((envval = getenv(envname)) == NULL) {
215 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
221 * Copy the full %$(NAME) into envname so it
226 strncpy(envname,p,copylen);
227 envname[copylen] = '\0';
228 r = realloc_string_sub(str, envname, envval);
234 /*******************************************************************
235 *******************************************************************/
237 static char *longvar_domainsid( void )
242 if ( !secrets_fetch_domain_sid( lp_workgroup(), &sid ) ) {
246 sid_string = SMB_STRDUP( sid_string_static( &sid ) );
249 DEBUG(0,("longvar_domainsid: failed to dup SID string!\n"));
255 /*******************************************************************
256 *******************************************************************/
263 static struct api_longvar longvar_table[] = {
264 { "DomainSID", longvar_domainsid },
268 static char *get_longvar_val( const char *varname )
272 DEBUG(7,("get_longvar_val: expanding variable [%s]\n", varname));
274 for ( i=0; longvar_table[i].name; i++ ) {
275 if ( strequal( longvar_table[i].name, varname ) ) {
276 return longvar_table[i].fn();
283 /*******************************************************************
284 Expand the long smb.conf variable names given a pointer to a %(NAME).
285 Return the number of characters by which the pointer should be advanced.
286 When this is called p points at the '%' character.
287 ********************************************************************/
289 static char *realloc_expand_longvar(char *str, char *p)
296 if ( p[0] != '%' || p[1] != '(' ) {
300 /* Look for the terminating ')'.*/
302 if ((q = strchr_m(p,')')) == NULL) {
303 DEBUG(0,("realloc_expand_longvar: Unterminated environment variable [%s]\n", p));
307 /* Extract the name from within the %(NAME) string.*/
310 copylen = MIN( (q-r), (sizeof(varname)-1) );
311 strncpy(varname, r, copylen);
312 varname[copylen] = '\0';
314 if ((value = get_longvar_val(varname)) == NULL) {
315 DEBUG(0,("realloc_expand_longvar: Variable [%s] not set. Skipping\n", varname));
319 /* Copy the full %(NAME) into envname so it can be replaced.*/
321 copylen = MIN( (q+1-p),(sizeof(varname)-1) );
322 strncpy( varname, p, copylen );
323 varname[copylen] = '\0';
324 r = realloc_string_sub(str, varname, value);
327 /* skip over the %(varname) */
332 /*******************************************************************
333 Patch from jkf@soton.ac.uk
334 Added this to implement %p (NIS auto-map version of %H)
335 *******************************************************************/
337 static char *automount_path(const char *user_name)
341 /* use the passwd entry as the default */
342 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
344 pstrcpy(server_path, get_user_home_dir(user_name));
346 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
348 if (lp_nis_home_map()) {
349 const char *home_path_start;
350 const char *automount_value = automount_lookup(user_name);
352 if(strlen(automount_value) > 0) {
353 home_path_start = strchr_m(automount_value,':');
354 if (home_path_start != NULL) {
355 DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
356 home_path_start?(home_path_start+1):""));
357 pstrcpy(server_path, home_path_start+1);
360 /* NIS key lookup failed: default to user home directory from password file */
361 DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path ));
366 DEBUG(4,("Home server path: %s\n", server_path));
368 return talloc_strdup(talloc_tos(), server_path);
371 /*******************************************************************
372 Patch from jkf@soton.ac.uk
373 This is Luke's original function with the NIS lookup code
374 moved out to a separate function.
375 *******************************************************************/
377 static const char *automount_server(const char *user_name)
380 const char *local_machine_name = get_local_machine_name();
382 /* use the local machine name as the default */
383 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
384 if (local_machine_name && *local_machine_name)
385 pstrcpy(server_name, local_machine_name);
387 pstrcpy(server_name, global_myname());
389 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
391 if (lp_nis_home_map()) {
393 char *automount_value = automount_lookup(user_name);
394 home_server_len = strcspn(automount_value,":");
395 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len));
396 if (home_server_len > sizeof(pstring))
397 home_server_len = sizeof(pstring);
398 strncpy(server_name, automount_value, home_server_len);
399 server_name[home_server_len] = '\0';
403 DEBUG(4,("Home server: %s\n", server_name));
405 return talloc_strdup(talloc_tos(), server_name);
408 /****************************************************************************
409 Do some standard substitutions in a string.
410 len is the length in bytes of the space allowed in string str. If zero means
411 don't allow expansions.
412 ****************************************************************************/
414 void standard_sub_basic(const char *smb_name, const char *domain_name,
415 char *str, size_t len)
419 if ( (s = alloc_sub_basic( smb_name, domain_name, str )) != NULL ) {
420 strncpy( str, s, len );
427 /****************************************************************************
428 Do some standard substitutions in a string.
429 This function will return an allocated string that have to be freed.
430 ****************************************************************************/
432 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name,
433 const char *domain_name, const char *str)
437 if ( (a = alloc_sub_basic(smb_name, domain_name, str)) == NULL ) {
440 t = talloc_strdup(mem_ctx, a);
445 /****************************************************************************
446 ****************************************************************************/
448 char *alloc_sub_basic(const char *smb_name, const char *domain_name,
451 char *b, *p, *s, *r, *a_string;
452 fstring pidstr, vnnstr;
454 char addr[INET6_ADDRSTRLEN];
455 const char *local_machine_name = get_local_machine_name();
457 /* workaround to prevent a crash while looking at bug #687 */
460 DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
464 a_string = SMB_STRDUP(str);
465 if (a_string == NULL) {
466 DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
470 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
477 r = strdup_lower(smb_name);
481 a_string = realloc_string_sub(a_string, "%U", r);
484 r = SMB_STRDUP(smb_name);
488 if ((pass = Get_Pwnam(r))!=NULL) {
489 a_string = realloc_string_sub(a_string, "%G", gidtoname(pass->pw_gid));
493 r = strdup_upper(domain_name);
497 a_string = realloc_string_sub(a_string, "%D", r);
500 a_string = realloc_string_sub(a_string, "%I",
501 client_addr(get_client_fd(),addr, sizeof(addr)));
504 a_string = realloc_string_sub( a_string, "%i",
505 client_socket_addr(get_client_fd(), addr, sizeof(addr)) );
508 if ( StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
511 if (local_machine_name && *local_machine_name) {
512 a_string = realloc_string_sub(a_string, "%L", local_machine_name);
514 a_string = realloc_string_sub(a_string, "%L", global_myname());
518 a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
521 a_string = realloc_string_sub(a_string, "%M", client_name(get_client_fd()));
524 a_string = realloc_string_sub(a_string, "%R", remote_proto);
527 a_string = realloc_string_sub(a_string, "%T", current_timestring(False));
530 a_string = realloc_string_sub(a_string, "%a", remote_arch);
533 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
534 a_string = realloc_string_sub(a_string, "%d", pidstr);
537 a_string = realloc_string_sub(a_string, "%h", myhostname());
540 a_string = realloc_string_sub(a_string, "%m", remote_machine);
543 a_string = realloc_string_sub(a_string, "%v", SAMBA_VERSION_STRING);
546 a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
549 a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
552 a_string = realloc_expand_longvar( a_string, p );
555 slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
556 a_string = realloc_string_sub(a_string, "%V", vnnstr);
577 /****************************************************************************
578 Do some specific substitutions in a string.
579 This function will return an allocated string that have to be freed.
580 ****************************************************************************/
582 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
583 const char *input_string,
584 const char *username,
590 char *ret_string = NULL;
594 if (!(tmp_ctx = talloc_new(mem_ctx))) {
595 DEBUG(0, ("talloc_new failed\n"));
599 a_string = talloc_strdup(tmp_ctx, input_string);
600 if (a_string == NULL) {
601 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
605 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
611 a_string = talloc_string_sub(
612 tmp_ctx, a_string, "%U", username);
615 a_string = talloc_string_sub(
616 tmp_ctx, a_string, "%u", username);
620 a_string = talloc_string_sub(
621 tmp_ctx, a_string, "%G",
624 a_string = talloc_string_sub(
631 a_string = talloc_string_sub(
632 tmp_ctx, a_string, "%g",
635 a_string = talloc_string_sub(
636 tmp_ctx, a_string, "%g", "NO_GROUP");
640 a_string = talloc_string_sub(tmp_ctx, a_string,
644 a_string = talloc_string_sub(
645 tmp_ctx, a_string, "%N",
646 automount_server(username));
653 if (a_string == NULL) {
658 /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
659 * away with the TALLOC_FREE(tmp_ctx) further down. */
661 ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
664 TALLOC_FREE(tmp_ctx);
668 /****************************************************************************
669 ****************************************************************************/
671 static char *alloc_sub_advanced(const char *servicename, const char *user,
672 const char *connectpath, gid_t gid,
673 const char *smb_name, const char *domain_name,
676 char *a_string, *ret_string;
679 a_string = SMB_STRDUP(str);
680 if (a_string == NULL) {
681 DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
685 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
691 a_string = realloc_string_sub(a_string, "%N", automount_server(user));
694 if ((h = get_user_home_dir(user)))
695 a_string = realloc_string_sub(a_string, "%H", h);
698 a_string = realloc_string_sub(a_string, "%P", connectpath);
701 a_string = realloc_string_sub(a_string, "%S", servicename);
704 a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
707 a_string = realloc_string_sub(a_string, "%u", user);
710 /* Patch from jkf@soton.ac.uk Left the %N (NIS
711 * server name) in standard_sub_basic as it is
712 * a feature for logon servers, hence uses the
713 * username. The %p (NIS server path) code is
714 * here as it is used instead of the default
715 * "path =" string in [homes] and so needs the
716 * service name, not the username. */
718 a_string = realloc_string_sub(a_string, "%p",
719 automount_path(servicename));
727 if (a_string == NULL) {
732 ret_string = alloc_sub_basic(smb_name, domain_name, a_string);
738 * This obviously is inefficient and needs to be merged into
739 * alloc_sub_advanced...
742 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
743 const char *servicename, const char *user,
744 const char *connectpath, gid_t gid,
745 const char *smb_name, const char *domain_name,
750 if (!(a = alloc_sub_advanced(servicename, user, connectpath, gid,
751 smb_name, domain_name, str))) {
754 t = talloc_strdup(mem_ctx, a);
760 void standard_sub_advanced(const char *servicename, const char *user,
761 const char *connectpath, gid_t gid,
762 const char *smb_name, const char *domain_name,
763 char *str, size_t len)
767 s = alloc_sub_advanced(servicename, user, connectpath,
768 gid, smb_name, domain_name, str);
771 strncpy( str, s, len );
776 /****************************************************************************
777 * Do some standard substitutions in a string.
778 * ****************************************************************************/
780 void standard_sub_conn(connection_struct *conn, char *str, size_t len)
784 s = alloc_sub_advanced(lp_servicename(SNUM(conn)), conn->user, conn->connectpath,
785 conn->gid, smb_user_name, "", str);
788 strncpy( str, s, len );