📄 substitute.c
字号:
/* Unix SMB/CIFS implementation. string substitution functions Copyright (C) Andrew Tridgell 1992-2000 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "includes.h"extern struct current_user current_user;fstring local_machine="";fstring remote_arch="UNKNOWN";userdom_struct current_user_info;fstring remote_proto="UNKNOWN";static fstring remote_machine;static fstring smb_user_name;/** * Set the 'local' machine name * @param local_name the name we are being called * @param if this is the 'final' name for us, not be be changed again */void set_local_machine_name(const char* local_name, BOOL perm){ static BOOL already_perm = False; fstring tmp_local_machine; fstrcpy(tmp_local_machine,local_name); trim_char(tmp_local_machine,' ',' '); /* * Windows NT/2k uses "*SMBSERVER" and XP uses "*SMBSERV" * arrggg!!! */ if ( strequal(tmp_local_machine, "*SMBSERVER") || strequal(tmp_local_machine, "*SMBSERV") ) { fstrcpy( local_machine, client_socket_addr() ); return; } if (already_perm) return; already_perm = perm; alpha_strcpy(local_machine,tmp_local_machine,SAFE_NETBIOS_CHARS,sizeof(local_machine)-1); strlower_m(local_machine);}/** * Set the 'remote' machine name * @param remote_name the name our client wants to be called by * @param if this is the 'final' name for them, not be be changed again */void set_remote_machine_name(const char* remote_name, BOOL perm){ static BOOL already_perm = False; fstring tmp_remote_machine; if (already_perm) return; already_perm = perm; fstrcpy(tmp_remote_machine,remote_name); trim_char(tmp_remote_machine,' ',' '); alpha_strcpy(remote_machine,tmp_remote_machine,SAFE_NETBIOS_CHARS,sizeof(remote_machine)-1); strlower_m(remote_machine);}const char* get_remote_machine_name(void) { return remote_machine;}const char* get_local_machine_name(void) { if (!*local_machine) { return global_myname(); } return local_machine;}/******************************************************************* Setup the string used by %U substitution.********************************************************************/void sub_set_smb_name(const char *name){ fstring tmp; int len; BOOL is_machine_account = False; /* don't let anonymous logins override the name */ if (! *name) return; fstrcpy( tmp, name ); trim_char( tmp, ' ', ' ' ); strlower_m( tmp ); len = strlen( tmp ); if ( len == 0 ) return; /* long story but here goes....we have to allow usernames ending in '$' as they are valid machine account names. So check for a machine account and re-add the '$' at the end after the call to alpha_strcpy(). --jerry */ if ( tmp[len-1] == '$' ) is_machine_account = True; alpha_strcpy( smb_user_name, tmp, SAFE_NETBIOS_CHARS, sizeof(smb_user_name)-1 ); if ( is_machine_account ) { len = strlen( smb_user_name ); smb_user_name[len-1] = '$'; }}char* sub_get_smb_name( void ){ return smb_user_name;}/******************************************************************* Setup the strings used by substitutions. Called per packet. Ensure %U name is set correctly also.********************************************************************/void set_current_user_info(const userdom_struct *pcui){ current_user_info = *pcui; /* The following is safe as current_user_info.smb_name * has already been sanitised in register_vuid. */ fstrcpy(smb_user_name, current_user_info.smb_name);}/******************************************************************* return the current active user name*******************************************************************/const char* get_current_username( void ){ if ( current_user_info.smb_name[0] == '\0' ) return smb_user_name; return current_user_info.smb_name; }/******************************************************************* Given a pointer to a %$(NAME) expand it as an environment variable. Return the number of characters by which the pointer should be advanced. Based on code by Branko Cibej <branko.cibej@hermes.si> When this is called p points at the '%' character.********************************************************************/static size_t expand_env_var(char *p, int len){ fstring envname; char *envval; char *q, *r; int copylen; if (p[1] != '$') return 1; if (p[2] != '(') return 2; /* * Look for the terminating ')'. */ if ((q = strchr_m(p,')')) == NULL) { DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p)); return 2; } /* * Extract the name from within the %$(NAME) string. */ r = p+3; copylen = MIN((q-r),(sizeof(envname)-1)); strncpy(envname,r,copylen); envname[copylen] = '\0'; if ((envval = getenv(envname)) == NULL) { DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname)); return 2; } /* * Copy the full %$(NAME) into envname so it * can be replaced. */ copylen = MIN((q+1-p),(sizeof(envname)-1)); strncpy(envname,p,copylen); envname[copylen] = '\0'; string_sub(p,envname,envval,len); return 0; /* Allow the environment contents to be parsed. */}/******************************************************************* Given a pointer to a %$(NAME) in p and the whole string in str expand it as an environment variable. Return a new allocated and expanded string. Based on code by Branko Cibej <branko.cibej@hermes.si> When this is called p points at the '%' character. May substitute multiple occurrencies of the same env var.********************************************************************/static char * realloc_expand_env_var(char *str, char *p){ char *envname; char *envval; char *q, *r; int copylen; if (p[0] != '%' || p[1] != '$' || p[2] != '(') return str; /* * Look for the terminating ')'. */ if ((q = strchr_m(p,')')) == NULL) { DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p)); return str; } /* * Extract the name from within the %$(NAME) string. */ r = p + 3; copylen = q - r; envname = (char *)SMB_MALLOC(copylen + 1 + 4); /* reserve space for use later add %$() chars */ if (envname == NULL) return NULL; strncpy(envname,r,copylen); envname[copylen] = '\0'; if ((envval = getenv(envname)) == NULL) { DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname)); SAFE_FREE(envname); return str; } /* * Copy the full %$(NAME) into envname so it * can be replaced. */ copylen = q + 1 - p; strncpy(envname,p,copylen); envname[copylen] = '\0'; r = realloc_string_sub(str, envname, envval); SAFE_FREE(envname); if (r == NULL) return NULL; return r;}/******************************************************************* Patch from jkf@soton.ac.uk Added this to implement %p (NIS auto-map version of %H)*******************************************************************/static char *automount_path(const char *user_name){ static pstring server_path; /* use the passwd entry as the default */ /* this will be the default if WITH_AUTOMOUNT is not used or fails */ pstrcpy(server_path, get_user_home_dir(user_name));#if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT)) if (lp_nis_home_map()) { const char *home_path_start; const char *automount_value = automount_lookup(user_name); if(strlen(automount_value) > 0) { home_path_start = strchr_m(automount_value,':'); if (home_path_start != NULL) { DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n", home_path_start?(home_path_start+1):"")); pstrcpy(server_path, home_path_start+1); } } else { /* NIS key lookup failed: default to user home directory from password file */ DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path )); } }#endif DEBUG(4,("Home server path: %s\n", server_path)); return server_path;}/******************************************************************* Patch from jkf@soton.ac.uk This is Luke's original function with the NIS lookup code moved out to a separate function.*******************************************************************/static const char *automount_server(const char *user_name){ static pstring server_name; const char *local_machine_name = get_local_machine_name(); /* use the local machine name as the default */ /* this will be the default if WITH_AUTOMOUNT is not used or fails */ if (local_machine_name && *local_machine_name) pstrcpy(server_name, local_machine_name); else pstrcpy(server_name, global_myname()); #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT)) if (lp_nis_home_map()) { int home_server_len; char *automount_value = automount_lookup(user_name); home_server_len = strcspn(automount_value,":"); DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len)); if (home_server_len > sizeof(pstring)) home_server_len = sizeof(pstring); strncpy(server_name, automount_value, home_server_len); server_name[home_server_len] = '\0'; }#endif DEBUG(4,("Home server: %s\n", server_name)); return server_name;}/**************************************************************************** Do some standard substitutions in a string. len is the length in bytes of the space allowed in string str. If zero means don't allow expansions.****************************************************************************/void standard_sub_basic(const char *smb_name, char *str,size_t len){ char *p, *s; fstring pidstr; struct passwd *pass; const char *local_machine_name = get_local_machine_name(); for (s=str; (p=strchr_m(s, '%'));s=p) { fstring tmp_str; int l = (int)len - (int)(p-str); if (l < 0) l = 0; switch (*(p+1)) { case 'U' : fstrcpy(tmp_str, smb_name); strlower_m(tmp_str); string_sub(p,"%U",tmp_str,l); break; case 'G' : fstrcpy(tmp_str, smb_name); if ((pass = Get_Pwnam(tmp_str))!=NULL) { string_sub(p,"%G",gidtoname(pass->pw_gid),l); } else { p += 2; } break; case 'D' : fstrcpy(tmp_str, current_user_info.domain); strupper_m(tmp_str); string_sub(p,"%D", tmp_str,l); break; case 'I' : string_sub(p,"%I", client_addr(),l); break; case 'i' : string_sub(p,"%i", client_socket_addr(),l); break; case 'L' : if (!StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%"))) { p++; break; } if (local_machine_name && *local_machine_name) { string_sub_once(p, "%L", local_machine_name, l); } else { pstring temp_name; pstrcpy(temp_name, global_myname()); strlower_m(temp_name); string_sub_once(p, "%L", temp_name, l); } break; case 'M' :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -