2 Unix SMB/Netbios implementation.
4 string substitution functions
5 Copyright (C) Andrew Tridgell 1992-2000
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern int DEBUGLEVEL;
27 fstring local_machine="";
28 fstring remote_arch="UNKNOWN";
29 pstring sesssetup_user="";
30 pstring samlogon_user="";
31 BOOL sam_logon_in_ssb = False;
32 fstring remote_proto="UNKNOWN";
33 fstring remote_machine="";
36 /*******************************************************************
37 Given a pointer to a %$(NAME) expand it as an environment variable.
38 Return the number of characters by which the pointer should be advanced.
39 Based on code by Branko Cibej <branko.cibej@hermes.si>
40 When this is called p points at the '%' character.
41 ********************************************************************/
42 static size_t expand_env_var(char *p, int len)
56 * Look for the terminating ')'.
59 if ((q = strchr(p,')')) == NULL) {
60 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
65 * Extract the name from within the %$(NAME) string.
69 copylen = MIN((q-r),(sizeof(envname)-1));
70 strncpy(envname,r,copylen);
71 envname[copylen] = '\0';
73 if ((envval = getenv(envname)) == NULL) {
74 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
79 * Copy the full %$(NAME) into envname so it
83 copylen = MIN((q+1-p),(sizeof(envname)-1));
84 strncpy(envname,p,copylen);
85 envname[copylen] = '\0';
86 string_sub(p,envname,envval,len);
87 return 0; /* Allow the environment contents to be parsed. */
90 /*******************************************************************
91 Patch from jkf@soton.ac.uk
92 Added this to implement %p (NIS auto-map version of %H)
93 *******************************************************************/
94 static char *automount_path(char *user_name)
96 static pstring server_path;
98 /* use the passwd entry as the default */
99 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
100 /* pstrcpy() copes with get_user_home_dir() returning NULL */
101 pstrcpy(server_path, get_user_home_dir(user_name));
103 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
105 if (lp_nis_home_map())
107 char *home_path_start;
108 char *automount_value = automount_lookup(user_name);
109 home_path_start = strchr(automount_value,':');
110 if (home_path_start != NULL)
112 DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
113 home_path_start?(home_path_start+1):""));
114 pstrcpy(server_path, home_path_start+1);
119 DEBUG(4,("Home server path: %s\n", server_path));
125 /*******************************************************************
126 Patch from jkf@soton.ac.uk
127 This is Luke's original function with the NIS lookup code
128 moved out to a separate function.
129 *******************************************************************/
130 static char *automount_server(char *user_name)
132 static pstring server_name;
134 /* use the local machine name as the default */
135 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
136 pstrcpy(server_name, local_machine);
138 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
140 if (lp_nis_home_map())
143 char *automount_value = automount_lookup(user_name);
144 home_server_len = strcspn(automount_value,":");
145 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len));
146 if (home_server_len > sizeof(pstring))
148 home_server_len = sizeof(pstring);
150 strncpy(server_name, automount_value, home_server_len);
151 server_name[home_server_len] = '\0';
155 DEBUG(4,("Home server: %s\n", server_name));
160 /****************************************************************************
161 Do some standard substitutions in a string.
162 ****************************************************************************/
163 void standard_sub_basic(char *str)
168 for (s=str; (p=strchr(s, '%'));s=p) {
169 int l = sizeof(pstring) - (int)(p-str);
172 case 'I' : string_sub(p,"%I", client_addr(),l); break;
173 case 'L' : string_sub(p,"%L", local_machine,l); break;
174 case 'M' : string_sub(p,"%M", client_name(),l); break;
175 case 'R' : string_sub(p,"%R", remote_proto,l); break;
176 case 'T' : string_sub(p,"%T", timestring(False),l); break;
177 case 'a' : string_sub(p,"%a", remote_arch,l); break;
179 slprintf(pidstr,sizeof(pidstr), "%d",(int)sys_getpid());
180 string_sub(p,"%d", pidstr,l);
182 case 'h' : string_sub(p,"%h", myhostname(),l); break;
183 case 'm' : string_sub(p,"%m", remote_machine,l); break;
184 case 'v' : string_sub(p,"%v", VERSION,l); break;
185 case '$' : p += expand_env_var(p,l); break; /* Expand environment variables */
188 break; /* don't run off the end of the string */
197 /****************************************************************************
198 Do some standard substitutions in a string.
199 ****************************************************************************/
200 static void standard_sub_advanced(int snum, char *user, char *connectpath, gid_t gid, char *str)
205 for (s=str; (p=strchr(s, '%'));s=p) {
206 int l = sizeof(pstring) - (int)(p-str);
209 case 'U' : string_sub(p,"%U", user,l); break;
211 if ((pass = Get_Pwnam(user,False))!=NULL) {
212 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
217 case 'N' : string_sub(p,"%N", automount_server(user),l); break;
219 if ((home = get_user_home_dir(user))) {
220 string_sub(p,"%H",home, l);
226 string_sub(p,"%P", connectpath, l);
230 string_sub(p,"%S", lp_servicename(snum), l);
234 string_sub(p,"%g", gidtoname(gid), l);
237 string_sub(p,"%u", user, l);
240 /* Patch from jkf@soton.ac.uk Left the %N (NIS
241 * server name) in standard_sub_basic as it is
242 * a feature for logon servers, hence uses the
243 * username. The %p (NIS server path) code is
244 * here as it is used instead of the default
245 * "path =" string in [homes] and so needs the
246 * service name, not the username. */
248 string_sub(p,"%p", automount_path(lp_servicename(snum)), l);
252 break; /* don't run off the end of the string */
259 standard_sub_basic(str);
262 /****************************************************************************
263 Do some standard substitutions in a string.
264 ****************************************************************************/
265 void standard_sub_conn(connection_struct *conn, char *str)
267 standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, str);
270 /****************************************************************************
271 like standard_sub but by snum
272 ****************************************************************************/
273 void standard_sub_snum(int snum, char *str)
275 standard_sub_advanced(snum, "", "", -1, str);
278 /*******************************************************************
279 Substitute strings with useful parameters.
280 ********************************************************************/
281 void standard_sub_vuser(char *str, user_struct *vuser)
283 standard_sub_advanced(-1, vuser->user.unix_name, "", -1, str);
286 /*******************************************************************
287 Substitute strings with useful parameters.
288 ********************************************************************/
289 void standard_sub_vsnum(char *str, user_struct *vuser, int snum)
291 standard_sub_advanced(snum, vuser->user.unix_name, "", -1, str);