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 pstrcpy(server_path, get_user_home_dir(user_name));
102 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
104 if (lp_nis_home_map()) {
105 char *home_path_start;
106 char *automount_value = automount_lookup(user_name);
108 if(strlen(automount_value) > 0) {
109 home_path_start = strchr_m(automount_value,':');
110 if (home_path_start != NULL) {
111 DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
112 home_path_start?(home_path_start+1):""));
113 pstrcpy(server_path, home_path_start+1);
116 /* NIS key lookup failed: default to user home directory from password file */
117 DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path ));
122 DEBUG(4,("Home server path: %s\n", server_path));
127 /*******************************************************************
128 Patch from jkf@soton.ac.uk
129 This is Luke's original function with the NIS lookup code
130 moved out to a separate function.
131 *******************************************************************/
133 static char *automount_server(const char *user_name)
135 static pstring server_name;
137 /* use the local machine name as the default */
138 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
140 pstrcpy(server_name, local_machine);
142 pstrcpy(server_name, global_myname);
144 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
146 if (lp_nis_home_map()) {
148 char *automount_value = automount_lookup(user_name);
149 home_server_len = strcspn(automount_value,":");
150 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len));
151 if (home_server_len > sizeof(pstring))
152 home_server_len = sizeof(pstring);
153 strncpy(server_name, automount_value, home_server_len);
154 server_name[home_server_len] = '\0';
158 DEBUG(4,("Home server: %s\n", server_name));
163 /****************************************************************************
164 Do some standard substitutions in a string.
165 ****************************************************************************/
167 void standard_sub_basic(const char *smb_name, char *str)
173 for (s=str; (p=strchr_m(s, '%'));s=p) {
176 int l = sizeof(pstring) - (int)(p-str);
180 fstrcpy(tmp_str, smb_name);
182 string_sub(p,"%U",tmp_str,l);
185 fstrcpy(tmp_str, smb_name);
186 if ((pass = Get_Pwnam(tmp_str))!=NULL) {
187 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
193 fstrcpy(tmp_str, current_user_info.domain);
195 string_sub(p,"%D", tmp_str,l);
197 case 'I' : string_sub(p,"%I", client_addr(),l); break;
199 if (*local_machine) {
200 string_sub(p,"%L", local_machine,l);
202 string_sub(p,"%L", global_myname,l);
205 case 'M' : string_sub(p,"%M", client_name(),l); break;
206 case 'R' : string_sub(p,"%R", remote_proto,l); break;
207 case 'T' : string_sub(p,"%T", timestring(False),l); break;
208 case 'a' : string_sub(p,"%a", remote_arch,l); break;
210 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
211 string_sub(p,"%d", pidstr,l);
213 case 'h' : string_sub(p,"%h", myhostname(),l); break;
214 case 'm' : string_sub(p,"%m", remote_machine,l); break;
215 case 'v' : string_sub(p,"%v", VERSION,l); break;
216 case '$' : p += expand_env_var(p,l); break; /* Expand environment variables */
219 break; /* don't run off the end of the string */
227 /****************************************************************************
228 Do some standard substitutions in a string.
229 ****************************************************************************/
231 void standard_sub_advanced(int snum, const char *user, const char *connectpath, gid_t gid, const char *smb_name, char *str)
235 for (s=str; (p=strchr_m(s, '%'));s=p) {
236 int l = sizeof(pstring) - (int)(p-str);
239 case 'N' : string_sub(p,"%N", automount_server(user),l); break;
241 if ((home = get_user_home_dir(user))) {
242 string_sub(p,"%H",home, l);
248 string_sub(p,"%P", connectpath, l);
252 string_sub(p,"%S", lp_servicename(snum), l);
256 string_sub(p,"%g", gidtoname(gid), l);
259 string_sub(p,"%u", user, l);
262 /* Patch from jkf@soton.ac.uk Left the %N (NIS
263 * server name) in standard_sub_basic as it is
264 * a feature for logon servers, hence uses the
265 * username. The %p (NIS server path) code is
266 * here as it is used instead of the default
267 * "path =" string in [homes] and so needs the
268 * service name, not the username. */
270 string_sub(p,"%p", automount_path(lp_servicename(snum)), l);
274 break; /* don't run off the end of the string */
281 standard_sub_basic(smb_name, str);
284 /****************************************************************************
285 Do some standard substitutions in a string.
286 ****************************************************************************/
288 void standard_sub_conn(connection_struct *conn, char *str)
290 standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, current_user_info.smb_name, str);
293 /****************************************************************************
294 Like standard_sub but for a homes share where snum still points to the [homes]
295 share. No user specific snum created yet so servicename should be the username.
296 ****************************************************************************/
298 void standard_sub_home(int snum, const char *user, char *str)
302 for (s=str; (p=strchr_m(s, '%'));s=p) {
303 int l = sizeof(pstring) - (int)(p-str);
307 string_sub(p,"%S", user, l);
310 string_sub(p,"%p", automount_path(user), l);
314 break; /* don't run off the end of the string */
321 standard_sub_advanced(snum, user, "", -1, current_user_info.smb_name, str);
324 /****************************************************************************
325 Like standard_sub but by snum.
326 ****************************************************************************/
328 void standard_sub_snum(int snum, char *str)
330 extern struct current_user current_user;
331 static uid_t cached_uid = -1;
332 static fstring cached_user;
333 /* calling uidtoname() on every substitute would be too expensive, so
334 we cache the result here as nearly every call is for the same uid */
336 if (cached_uid != current_user.uid) {
337 fstrcpy(cached_user, uidtoname(current_user.uid));
338 cached_uid = current_user.uid;
341 standard_sub_advanced(snum, cached_user, "", -1, current_user_info.smb_name, str);
344 /*******************************************************************
345 Substitute strings with useful parameters.
346 ********************************************************************/
348 void standard_sub_vuser(char *str, user_struct *vuser)
350 standard_sub_advanced(-1, vuser->user.unix_name, "", -1, current_user_info.smb_name, str);
353 /*******************************************************************
354 Substitute strings with useful parameters.
355 ********************************************************************/
357 void standard_sub_vsnum(char *str, user_struct *vuser, int snum)
359 standard_sub_advanced(snum, vuser->user.unix_name, "", -1, current_user_info.smb_name, str);