From 030ec8e71f9ef533b5d6aece01e67d357e23b7d2 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 17 Apr 2000 04:46:10 +0000 Subject: moved standard_sub() and friends into a separate module --- source/Makefile.in | 2 +- source/lib/substitute.c | 295 ++++++++++++++++++++++++++++++++++++++++++++++++ source/lib/util.c | 270 +------------------------------------------- 3 files changed, 297 insertions(+), 270 deletions(-) create mode 100644 source/lib/substitute.c diff --git a/source/Makefile.in b/source/Makefile.in index 2dfaea571f7..9bf86d9e7c8 100644 --- a/source/Makefile.in +++ b/source/Makefile.in @@ -106,7 +106,7 @@ LIB_OBJ = lib/charcnv.o lib/charset.o lib/debug.o lib/fault.o \ lib/util_array.o lib/util_str.o lib/util_sid.o \ lib/util_unistr.o lib/util_file.o \ lib/util.o lib/util_sock.o lib/util_sec.o smbd/ssl.o lib/fnmatch.o \ - tdb/tdb.o lib/talloc.o lib/hash.o + tdb/tdb.o lib/talloc.o lib/hash.o lib/substitute.o UBIQX_OBJ = ubiqx/ubi_BinTree.o ubiqx/ubi_Cache.o ubiqx/ubi_SplayTree.o \ ubiqx/ubi_dLinkList.o ubiqx/ubi_sLinkList.o ubiqx/debugparse.o diff --git a/source/lib/substitute.c b/source/lib/substitute.c new file mode 100644 index 00000000000..8e7b1cfa5a5 --- /dev/null +++ b/source/lib/substitute.c @@ -0,0 +1,295 @@ +/* + Unix SMB/Netbios implementation. + Version 3.0 + 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 int DEBUGLEVEL; + +fstring local_machine=""; +fstring remote_arch="UNKNOWN"; +pstring sesssetup_user=""; +pstring samlogon_user=""; +BOOL sam_logon_in_ssb = False; +fstring remote_proto="UNKNOWN"; +fstring remote_machine=""; + + +/******************************************************************* + 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 + 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(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. */ +} + +/******************************************************************* + Patch from jkf@soton.ac.uk + Added this to implement %p (NIS auto-map version of %H) +*******************************************************************/ +static char *automount_path(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() copes with get_user_home_dir() returning NULL */ + pstrcpy(server_path, get_user_home_dir(user_name)); + +#if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT)) + + if (lp_nis_home_map()) + { + char *home_path_start; + char *automount_value = automount_lookup(user_name); + home_path_start = strchr(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); + } + } +#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 char *automount_server(char *user_name) +{ + static pstring server_name; + + /* use the local machine name as the default */ + /* this will be the default if WITH_AUTOMOUNT is not used or fails */ + pstrcpy(server_name, local_machine); + +#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; +} + +/******************************************************************* + Substitute strings with useful parameters. + Rewritten by Stefaan A Eeckels and + Paul Rippin . +********************************************************************/ +void standard_sub_basic(char *str) +{ + char *s, *p; + char pidstr[10]; + struct passwd *pass; + char *username = sam_logon_in_ssb ? samlogon_user : sesssetup_user; + + for (s = str ; s && *s && (p = strchr(s,'%')); s = p ) + { + int l = sizeof(pstring) - (int)(p-str); + + if (l < 0) { + DEBUG(0,("ERROR: string overflow by %d in standard_sub_basic(%.50s)\n", + -l, str)); + + return; + } + + switch (*(p+1)) + { + case 'G' : + { + if ((pass = Get_Pwnam(username,False))!=NULL) { + string_sub(p,"%G",gidtoname(pass->pw_gid),l); + } else { + p += 2; + } + break; + } + case 'N' : string_sub(p,"%N", automount_server(username),l); break; + case 'I' : string_sub(p,"%I", client_addr(),l); break; + case 'L' : string_sub(p,"%L", local_machine,l); break; + case 'M' : string_sub(p,"%M", client_name(),l); break; + case 'R' : string_sub(p,"%R", remote_proto,l); break; + case 'T' : string_sub(p,"%T", timestring(False),l); break; + case 'U' : string_sub(p,"%U", username,l); break; + case 'a' : string_sub(p,"%a", remote_arch,l); break; + case 'd' : + { + slprintf(pidstr,sizeof(pidstr) - 1, "%d",(int)getpid()); + string_sub(p,"%d", pidstr,l); + break; + } + case 'h' : string_sub(p,"%h", myhostname(),l); break; + case 'm' : string_sub(p,"%m", remote_machine,l); break; + case 'v' : string_sub(p,"%v", VERSION,l); break; + case '$' : p += expand_env_var(p,l); break; /* Expand environment variables */ + case '\0': p++; break; /* don't run off end if last character is % */ + default : p+=2; break; + } + } + return; +} + + +/**************************************************************************** + Do some standard substitutions in a string. +****************************************************************************/ + +void standard_sub_advanced(int snum, char *user, char *connectpath, gid_t gid, char *str) +{ + char *p, *s, *home; + + for (s=str; (p=strchr(s, '%'));s=p) { + int l = sizeof(pstring) - (int)(p-str); + + switch (*(p+1)) { + case 'H': + if ((home = get_user_home_dir(user))) { + string_sub(p,"%H",home, l); + } else { + p += 2; + } + break; + + case 'P': + string_sub(p,"%P", connectpath, l); + break; + + case 'S': + string_sub(p,"%S", lp_servicename(snum), l); + break; + + case 'g': + string_sub(p,"%g", gidtoname(gid), l); + break; + case 'u': + string_sub(p,"%u", user, l); + break; + + /* Patch from jkf@soton.ac.uk Left the %N (NIS + * server name) in standard_sub_basic as it is + * a feature for logon servers, hence uses the + * username. The %p (NIS server path) code is + * here as it is used instead of the default + * "path =" string in [homes] and so needs the + * service name, not the username. */ + case 'p': + string_sub(p,"%p", automount_path(lp_servicename(snum)), l); + break; + case '\0': + p++; + break; /* don't run off the end of the string */ + + default: p+=2; + break; + } + } + + standard_sub_basic(str); +} + +/**************************************************************************** + Do some standard substitutions in a string. +****************************************************************************/ +void standard_sub(connection_struct *conn, char *str) +{ + if (conn==NULL) + standard_sub_advanced(-1, "", "", -1, str); + else + standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, str); +} + +/**************************************************************************** +like standard_sub but by snum +****************************************************************************/ +void standard_sub_snum(int snum, char *str) +{ + standard_sub_advanced(snum, "", "", -1, str); +} + diff --git a/source/lib/util.c b/source/lib/util.c index 16cbf4ab647..2f8b52ccbde 100644 --- a/source/lib/util.c +++ b/source/lib/util.c @@ -83,18 +83,9 @@ BOOL use_mangled_map = False; BOOL short_case_preserve; BOOL case_mangle; -fstring remote_machine=""; -fstring local_machine=""; -fstring remote_arch="UNKNOWN"; static enum remote_arch_types ra_type = RA_UNKNOWN; -fstring remote_proto="UNKNOWN"; pstring user_socket_options=DEFAULT_SOCKET_OPTIONS; -pstring sesssetup_user=""; -pstring samlogon_user=""; - -BOOL sam_logon_in_ssb = False; - pstring global_myname = ""; fstring global_myworkgroup = ""; char **my_netbios_names; @@ -1744,266 +1735,6 @@ static char *automount_lookup(char *user_name) #endif /* WITH_NISPLUS_HOME */ #endif -/******************************************************************* - Patch from jkf@soton.ac.uk - This is Luke's original function with the NIS lookup code - moved out to a separate function. -*******************************************************************/ -static char *automount_server(char *user_name) -{ - static pstring server_name; - - /* use the local machine name as the default */ - /* this will be the default if WITH_AUTOMOUNT is not used or fails */ - pstrcpy(server_name, local_machine); - -#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; -} - -/******************************************************************* - Patch from jkf@soton.ac.uk - Added this to implement %p (NIS auto-map version of %H) -*******************************************************************/ -static char *automount_path(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() copes with get_user_home_dir() returning NULL */ - pstrcpy(server_path, get_user_home_dir(user_name)); - -#if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT)) - - if (lp_nis_home_map()) - { - char *home_path_start; - char *automount_value = automount_lookup(user_name); - home_path_start = strchr(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); - } - } -#endif - - DEBUG(4,("Home server path: %s\n", server_path)); - - return server_path; -} - -/******************************************************************* - 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 - 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(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. */ -} - -/******************************************************************* - Substitute strings with useful parameters. - Rewritten by Stefaan A Eeckels and - Paul Rippin . -********************************************************************/ - -void standard_sub_basic(char *str) -{ - char *s, *p; - char pidstr[10]; - struct passwd *pass; - char *username = sam_logon_in_ssb ? samlogon_user : sesssetup_user; - - for (s = str ; s && *s && (p = strchr(s,'%')); s = p ) - { - int l = sizeof(pstring) - (int)(p-str); - - if (l < 0) { - DEBUG(0,("ERROR: string overflow by %d in standard_sub_basic(%.50s)\n", - -l, str)); - - return; - } - - switch (*(p+1)) - { - case 'G' : - { - if ((pass = Get_Pwnam(username,False))!=NULL) { - string_sub(p,"%G",gidtoname(pass->pw_gid),l); - } else { - p += 2; - } - break; - } - case 'N' : string_sub(p,"%N", automount_server(username),l); break; - case 'I' : string_sub(p,"%I", client_addr(),l); break; - case 'L' : string_sub(p,"%L", local_machine,l); break; - case 'M' : string_sub(p,"%M", client_name(),l); break; - case 'R' : string_sub(p,"%R", remote_proto,l); break; - case 'T' : string_sub(p,"%T", timestring(False),l); break; - case 'U' : string_sub(p,"%U", username,l); break; - case 'a' : string_sub(p,"%a", remote_arch,l); break; - case 'd' : - { - slprintf(pidstr,sizeof(pidstr) - 1, "%d",(int)getpid()); - string_sub(p,"%d", pidstr,l); - break; - } - case 'h' : string_sub(p,"%h", myhostname(),l); break; - case 'm' : string_sub(p,"%m", remote_machine,l); break; - case 'v' : string_sub(p,"%v", VERSION,l); break; - case '$' : p += expand_env_var(p,l); break; /* Expand environment variables */ - case '\0': p++; break; /* don't run off end if last character is % */ - default : p+=2; break; - } - } - return; -} - - -/**************************************************************************** - Do some standard substitutions in a string. -****************************************************************************/ - -void standard_sub_advanced(int snum, char *user, char *connectpath, gid_t gid, char *str) -{ - char *p, *s, *home; - - for (s=str; (p=strchr(s, '%'));s=p) { - int l = sizeof(pstring) - (int)(p-str); - - switch (*(p+1)) { - case 'H': - if ((home = get_user_home_dir(user))) { - string_sub(p,"%H",home, l); - } else { - p += 2; - } - break; - - case 'P': - string_sub(p,"%P", connectpath, l); - break; - - case 'S': - string_sub(p,"%S", lp_servicename(snum), l); - break; - - case 'g': - string_sub(p,"%g", gidtoname(gid), l); - break; - case 'u': - string_sub(p,"%u", user, l); - break; - - /* Patch from jkf@soton.ac.uk Left the %N (NIS - * server name) in standard_sub_basic as it is - * a feature for logon servers, hence uses the - * username. The %p (NIS server path) code is - * here as it is used instead of the default - * "path =" string in [homes] and so needs the - * service name, not the username. */ - case 'p': - string_sub(p,"%p", automount_path(lp_servicename(snum)), l); - break; - case '\0': - p++; - break; /* don't run off the end of the string */ - - default: p+=2; - break; - } - } - - standard_sub_basic(str); -} - -/**************************************************************************** - Do some standard substitutions in a string. -****************************************************************************/ -void standard_sub(connection_struct *conn, char *str) -{ - if (conn==NULL) - standard_sub_advanced(-1, "", "", -1, str); - else - standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, str); -} - -/**************************************************************************** -like standard_sub but by snum -****************************************************************************/ -void standard_sub_snum(int snum, char *str) -{ - standard_sub_advanced(snum, "", "", -1, str); -} /******************************************************************* are two IPs on the same subnet? @@ -2539,6 +2270,7 @@ set the horrid remote_arch string based on an enum. ********************************************************************/ void set_remote_arch(enum remote_arch_types type) { + extern fstring remote_arch; ra_type = type; switch( type ) { -- cgit