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 fstring local_machine="";
26 fstring remote_arch="UNKNOWN";
27 userdom_struct current_user_info;
28 fstring remote_proto="UNKNOWN";
29 fstring remote_machine="";
30 extern pstring global_myname;
32 /*******************************************************************
33 Given a pointer to a %$(NAME) expand it as an environment variable.
34 Return the number of characters by which the pointer should be advanced.
35 Based on code by Branko Cibej <branko.cibej@hermes.si>
36 When this is called p points at the '%' character.
37 ********************************************************************/
39 static size_t expand_env_var(char *p, int len)
53 * Look for the terminating ')'.
56 if ((q = strchr_m(p,')')) == NULL) {
57 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
62 * Extract the name from within the %$(NAME) string.
66 copylen = MIN((q-r),(sizeof(envname)-1));
67 strncpy(envname,r,copylen);
68 envname[copylen] = '\0';
70 if ((envval = getenv(envname)) == NULL) {
71 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
76 * Copy the full %$(NAME) into envname so it
80 copylen = MIN((q+1-p),(sizeof(envname)-1));
81 strncpy(envname,p,copylen);
82 envname[copylen] = '\0';
83 string_sub(p,envname,envval,len);
84 return 0; /* Allow the environment contents to be parsed. */
87 /*******************************************************************
88 Patch from jkf@soton.ac.uk
89 Added this to implement %p (NIS auto-map version of %H)
90 *******************************************************************/
92 static char *automount_path(const char *user_name)
94 static pstring server_path;
97 /* use the passwd entry as the default */
98 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
100 if (((pass = Get_Pwnam(user_name))!=NULL) && (pass->pw_dir != NULL))
101 pstrcpy(server_path, pass->pw_dir );
105 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
107 if (lp_nis_home_map()) {
108 char *home_path_start;
109 char *automount_value = automount_lookup(user_name);
111 if(strlen(automount_value) > 0) {
112 home_path_start = strchr_m(automount_value,':');
113 if (home_path_start != NULL) {
114 DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
115 home_path_start?(home_path_start+1):""));
116 pstrcpy(server_path, home_path_start+1);
119 /* NIS key lookup failed: default to user home directory from password file */
120 pstrcpy(server_path, get_user_home_dir(user_name));
121 DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path ));
126 DEBUG(4,("Home server path: %s\n", server_path));
131 /*******************************************************************
132 Patch from jkf@soton.ac.uk
133 This is Luke's original function with the NIS lookup code
134 moved out to a separate function.
135 *******************************************************************/
137 static char *automount_server(const char *user_name)
139 static pstring server_name;
141 /* use the local machine name as the default */
142 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
144 pstrcpy(server_name, local_machine);
146 pstrcpy(server_name, global_myname);
148 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
150 if (lp_nis_home_map()) {
152 char *automount_value = automount_lookup(user_name);
153 home_server_len = strcspn(automount_value,":");
154 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len));
155 if (home_server_len > sizeof(pstring))
156 home_server_len = sizeof(pstring);
157 strncpy(server_name, automount_value, home_server_len);
158 server_name[home_server_len] = '\0';
162 DEBUG(4,("Home server: %s\n", server_name));
167 /****************************************************************************
168 Do some standard substitutions in a string.
169 ****************************************************************************/
171 void standard_sub_basic(const char *smb_name, char *str)
177 for (s=str; (p=strchr_m(s, '%'));s=p) {
180 int l = sizeof(pstring) - (int)(p-str);
184 fstrcpy(tmp_str, smb_name);
186 string_sub(p,"%U",tmp_str,l);
189 fstrcpy(tmp_str, smb_name);
190 if ((pass = Get_Pwnam(tmp_str))!=NULL) {
191 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
197 fstrcpy(tmp_str, current_user_info.domain);
199 string_sub(p,"%D", tmp_str,l);
201 case 'I' : string_sub(p,"%I", client_addr(),l); break;
203 if (*local_machine) {
204 string_sub(p,"%L", local_machine,l);
206 string_sub(p,"%L", global_myname,l);
209 case 'M' : string_sub(p,"%M", client_name(),l); break;
210 case 'R' : string_sub(p,"%R", remote_proto,l); break;
211 case 'T' : string_sub(p,"%T", timestring(False),l); break;
212 case 'a' : string_sub(p,"%a", remote_arch,l); break;
214 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
215 string_sub(p,"%d", pidstr,l);
217 case 'h' : string_sub(p,"%h", myhostname(),l); break;
218 case 'm' : string_sub(p,"%m", remote_machine,l); break;
219 case 'v' : string_sub(p,"%v", VERSION,l); break;
220 case '$' : p += expand_env_var(p,l); break; /* Expand environment variables */
223 break; /* don't run off the end of the string */
231 /****************************************************************************
232 Do some standard substitutions in a string.
233 ****************************************************************************/
235 void standard_sub_advanced(int snum, const char *user, const char *connectpath, gid_t gid, const char *smb_name, char *str)
239 for (s=str; (p=strchr_m(s, '%'));s=p) {
240 int l = sizeof(pstring) - (int)(p-str);
243 case 'N' : string_sub(p,"%N", automount_server(user),l); break;
245 if ((home = get_user_home_dir(user))) {
246 string_sub(p,"%H",home, l);
252 string_sub(p,"%P", connectpath, l);
256 string_sub(p,"%S", lp_servicename(snum), l);
260 string_sub(p,"%g", gidtoname(gid), l);
263 string_sub(p,"%u", user, l);
266 /* Patch from jkf@soton.ac.uk Left the %N (NIS
267 * server name) in standard_sub_basic as it is
268 * a feature for logon servers, hence uses the
269 * username. The %p (NIS server path) code is
270 * here as it is used instead of the default
271 * "path =" string in [homes] and so needs the
272 * service name, not the username. */
274 string_sub(p,"%p", automount_path(lp_servicename(snum)), l);
278 break; /* don't run off the end of the string */
285 standard_sub_basic(smb_name, str);
288 /****************************************************************************
289 Do some standard substitutions in a string.
290 ****************************************************************************/
292 void standard_sub_conn(connection_struct *conn, char *str)
294 standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, current_user_info.smb_name, str);
297 /****************************************************************************
298 Like standard_sub but for a homes share where snum still points to the [homes]
299 share. No user specific snum created yet so servicename should be the username.
300 ****************************************************************************/
302 void standard_sub_home(int snum, const char *user, char *str)
306 for (s=str; (p=strchr_m(s, '%'));s=p) {
307 int l = sizeof(pstring) - (int)(p-str);
311 string_sub(p,"%S", user, l);
314 string_sub(p,"%p", automount_path(user), l);
318 break; /* don't run off the end of the string */
325 standard_sub_advanced(snum, user, "", -1, current_user_info.smb_name, str);
328 /****************************************************************************
329 Like standard_sub but by snum.
330 ****************************************************************************/
332 void standard_sub_snum(int snum, char *str)
334 extern struct current_user current_user;
335 static uid_t cached_uid = -1;
336 static fstring cached_user;
337 /* calling uidtoname() on every substitute would be too expensive, so
338 we cache the result here as nearly every call is for the same uid */
340 if (cached_uid != current_user.uid) {
341 fstrcpy(cached_user, uidtoname(current_user.uid));
342 cached_uid = current_user.uid;
345 standard_sub_advanced(snum, cached_user, "", -1, current_user_info.smb_name, str);
348 /*******************************************************************
349 Substitute strings with useful parameters.
350 ********************************************************************/
352 void standard_sub_vuser(char *str, user_struct *vuser)
354 standard_sub_advanced(-1, vuser->user.unix_name, "", -1, current_user_info.smb_name, str);
357 /*******************************************************************
358 Substitute strings with useful parameters.
359 ********************************************************************/
361 void standard_sub_vsnum(char *str, user_struct *vuser, int snum)
363 standard_sub_advanced(snum, vuser->user.unix_name, "", -1, current_user_info.smb_name, str);