2 Unix SMB/CIFS implementation.
3 string substitution functions
4 Copyright (C) Andrew Tridgell 1992-2000
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 fstring local_machine="";
25 fstring remote_arch="UNKNOWN";
26 userdom_struct current_user_info;
27 fstring remote_proto="UNKNOWN";
28 fstring remote_machine="";
29 extern pstring global_myname;
31 /*******************************************************************
32 Given a pointer to a %$(NAME) expand it as an environment variable.
33 Return the number of characters by which the pointer should be advanced.
34 Based on code by Branko Cibej <branko.cibej@hermes.si>
35 When this is called p points at the '%' character.
36 ********************************************************************/
38 static size_t expand_env_var(char *p, int len)
52 * Look for the terminating ')'.
55 if ((q = strchr_m(p,')')) == NULL) {
56 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
61 * Extract the name from within the %$(NAME) string.
65 copylen = MIN((q-r),(sizeof(envname)-1));
66 strncpy(envname,r,copylen);
67 envname[copylen] = '\0';
69 if ((envval = getenv(envname)) == NULL) {
70 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
75 * Copy the full %$(NAME) into envname so it
79 copylen = MIN((q+1-p),(sizeof(envname)-1));
80 strncpy(envname,p,copylen);
81 envname[copylen] = '\0';
82 string_sub(p,envname,envval,len);
83 return 0; /* Allow the environment contents to be parsed. */
86 /*******************************************************************
87 Patch from jkf@soton.ac.uk
88 Added this to implement %p (NIS auto-map version of %H)
89 *******************************************************************/
91 static char *automount_path(const char *user_name)
93 static pstring server_path;
95 /* use the passwd entry as the default */
96 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
98 pstrcpy(server_path, get_user_home_dir(user_name));
100 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
102 if (lp_nis_home_map()) {
103 char *home_path_start;
104 char *automount_value = automount_lookup(user_name);
106 if(strlen(automount_value) > 0) {
107 home_path_start = strchr_m(automount_value,':');
108 if (home_path_start != NULL) {
109 DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
110 home_path_start?(home_path_start+1):""));
111 pstrcpy(server_path, home_path_start+1);
114 /* NIS key lookup failed: default to user home directory from password file */
115 DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path ));
120 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 *******************************************************************/
131 static char *automount_server(const char *user_name)
133 static pstring server_name;
135 /* use the local machine name as the default */
136 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
138 pstrcpy(server_name, local_machine);
140 pstrcpy(server_name, global_myname);
142 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
144 if (lp_nis_home_map()) {
146 char *automount_value = automount_lookup(user_name);
147 home_server_len = strcspn(automount_value,":");
148 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len));
149 if (home_server_len > sizeof(pstring))
150 home_server_len = sizeof(pstring);
151 strncpy(server_name, automount_value, home_server_len);
152 server_name[home_server_len] = '\0';
156 DEBUG(4,("Home server: %s\n", server_name));
161 /****************************************************************************
162 Do some standard substitutions in a string.
163 ****************************************************************************/
165 void standard_sub_basic(const char *smb_name, char *str)
171 for (s=str; (p=strchr_m(s, '%'));s=p) {
174 int l = sizeof(pstring) - (int)(p-str);
178 fstrcpy(tmp_str, smb_name);
180 string_sub(p,"%U",tmp_str,l);
183 fstrcpy(tmp_str, smb_name);
184 if ((pass = Get_Pwnam(tmp_str))!=NULL) {
185 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
191 fstrcpy(tmp_str, current_user_info.domain);
193 string_sub(p,"%D", tmp_str,l);
195 case 'I' : string_sub(p,"%I", client_addr(),l); break;
197 if (*local_machine) {
198 string_sub(p,"%L", local_machine,l);
200 string_sub(p,"%L", global_myname,l);
203 case 'M' : string_sub(p,"%M", client_name(),l); break;
204 case 'R' : string_sub(p,"%R", remote_proto,l); break;
205 case 'T' : string_sub(p,"%T", timestring(False),l); break;
206 case 'a' : string_sub(p,"%a", remote_arch,l); break;
208 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
209 string_sub(p,"%d", pidstr,l);
211 case 'h' : string_sub(p,"%h", myhostname(),l); break;
212 case 'm' : string_sub(p,"%m", remote_machine,l); break;
213 case 'v' : string_sub(p,"%v", VERSION,l); break;
214 case '$' : p += expand_env_var(p,l); break; /* Expand environment variables */
217 break; /* don't run off the end of the string */
225 /****************************************************************************
226 Do some standard substitutions in a string.
227 ****************************************************************************/
229 static void standard_sub_advanced(int snum, const char *user, const char *connectpath, gid_t gid, const char *smb_name, char *str)
233 for (s=str; (p=strchr_m(s, '%'));s=p) {
234 int l = sizeof(pstring) - (int)(p-str);
237 case 'N' : string_sub(p,"%N", automount_server(user),l); break;
239 if ((home = get_user_home_dir(user))) {
240 string_sub(p,"%H",home, l);
246 string_sub(p,"%P", connectpath, l);
250 string_sub(p,"%S", lp_servicename(snum), l);
254 string_sub(p,"%g", gidtoname(gid), l);
257 string_sub(p,"%u", user, l);
260 /* Patch from jkf@soton.ac.uk Left the %N (NIS
261 * server name) in standard_sub_basic as it is
262 * a feature for logon servers, hence uses the
263 * username. The %p (NIS server path) code is
264 * here as it is used instead of the default
265 * "path =" string in [homes] and so needs the
266 * service name, not the username. */
268 string_sub(p,"%p", automount_path(lp_servicename(snum)), l);
272 break; /* don't run off the end of the string */
279 standard_sub_basic(smb_name, str);
282 const char *standard_sub_specified(TALLOC_CTX *mem_ctx, const char *input_string,
283 const char *username,
288 pstring input_pstring;
291 pstrcpy(input_pstring, input_string);
293 for (s=input_pstring; (p=strchr_m(s, '%')); s=p) {
295 int l = sizeof(pstring) - (int)(p-input_pstring);
299 string_sub(p,"%U",username,l);
302 string_sub(p,"%u",username,l);
307 string_sub(p,"%G",gidtoname(gid),l);
308 string_sub(p,"%g",gidtoname(gid),l);
310 string_sub(p,"%G","NO_GROUP",l);
311 string_sub(p,"%g","NO_GROUP",l);
315 string_sub(p,"%D", domain,l);
318 string_sub(p,"%N", automount_server(username),l);
322 break; /* don't run off the end of the string */
329 standard_sub_basic(username, input_pstring);
331 return talloc_strdup(mem_ctx, input_pstring);
334 /****************************************************************************
335 Do some standard substitutions in a string.
336 ****************************************************************************/
338 void standard_sub_conn(connection_struct *conn, char *str)
340 standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, current_user_info.smb_name, str);
343 /****************************************************************************
344 Like standard_sub but for a homes share where snum still points to the [homes]
345 share. No user specific snum created yet so servicename should be the username.
346 ****************************************************************************/
348 void standard_sub_home(int snum, const char *user, char *str)
352 for (s=str; (p=strchr_m(s, '%'));s=p) {
353 int l = sizeof(pstring) - (int)(p-str);
357 string_sub(p,"%S", user, l);
360 string_sub(p,"%p", automount_path(user), l);
364 break; /* don't run off the end of the string */
371 standard_sub_advanced(snum, user, "", -1, current_user_info.smb_name, str);
374 /****************************************************************************
375 Like standard_sub but by snum.
376 ****************************************************************************/
378 void standard_sub_snum(int snum, char *str)
380 extern struct current_user current_user;
381 static uid_t cached_uid = -1;
382 static fstring cached_user;
383 /* calling uidtoname() on every substitute would be too expensive, so
384 we cache the result here as nearly every call is for the same uid */
386 if (cached_uid != current_user.uid) {
387 fstrcpy(cached_user, uidtoname(current_user.uid));
388 cached_uid = current_user.uid;
391 standard_sub_advanced(snum, cached_user, "", -1, current_user_info.smb_name, str);