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 len is the length in bytes of the space allowed in string str. If zero means
164 don't allow expansions.
165 ****************************************************************************/
167 void standard_sub_basic(const char *smb_name, char *str,size_t len)
173 for (s=str; (p=strchr_m(s, '%'));s=p) {
176 int l = (int)len - (int)(p-str);
183 fstrcpy(tmp_str, smb_name);
185 string_sub(p,"%U",tmp_str,l);
188 fstrcpy(tmp_str, smb_name);
189 if ((pass = Get_Pwnam(tmp_str))!=NULL) {
190 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
196 fstrcpy(tmp_str, current_user_info.domain);
198 string_sub(p,"%D", tmp_str,l);
201 string_sub(p,"%I", client_addr(),l);
205 string_sub(p,"%L", local_machine,l);
207 string_sub(p,"%L", global_myname,l);
210 string_sub(p,"%M", client_name(),l);
213 string_sub(p,"%R", remote_proto,l);
216 string_sub(p,"%T", timestring(False),l);
219 string_sub(p,"%a", remote_arch,l);
222 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
223 string_sub(p,"%d", pidstr,l);
226 string_sub(p,"%h", myhostname(),l);
229 string_sub(p,"%m", remote_machine,l);
232 string_sub(p,"%v", VERSION,l);
235 p += expand_env_var(p,l);
236 break; /* Expand environment variables */
239 break; /* don't run off the end of the string */
247 /****************************************************************************
248 Do some standard substitutions in a string.
249 ****************************************************************************/
251 static void standard_sub_advanced(int snum, const char *user,
252 const char *connectpath, gid_t gid,
253 const char *smb_name, char *str, size_t len)
257 for (s=str; (p=strchr_m(s, '%'));s=p) {
258 int l = (int)len - (int)(p-str);
265 string_sub(p,"%N", automount_server(user),l);
268 if ((home = get_user_home_dir(user)))
269 string_sub(p,"%H",home, l);
274 string_sub(p,"%P", connectpath, l);
277 string_sub(p,"%S", lp_servicename(snum), l);
280 string_sub(p,"%g", gidtoname(gid), l);
283 string_sub(p,"%u", user, l);
286 /* Patch from jkf@soton.ac.uk Left the %N (NIS
287 * server name) in standard_sub_basic as it is
288 * a feature for logon servers, hence uses the
289 * username. The %p (NIS server path) code is
290 * here as it is used instead of the default
291 * "path =" string in [homes] and so needs the
292 * service name, not the username. */
294 string_sub(p,"%p", automount_path(lp_servicename(snum)), l);
298 break; /* don't run off the end of the string */
305 standard_sub_basic(smb_name, str, len);
308 const char *standard_sub_specified(TALLOC_CTX *mem_ctx, const char *input_string,
309 const char *username,
314 pstring input_pstring;
317 pstrcpy(input_pstring, input_string);
319 for (s=input_pstring; (p=strchr_m(s, '%')); s=p) {
321 int l = sizeof(pstring) - (int)(p-input_pstring);
325 string_sub(p,"%U",username,l);
328 string_sub(p,"%u",username,l);
333 string_sub(p,"%G",gidtoname(gid),l);
334 string_sub(p,"%g",gidtoname(gid),l);
336 string_sub(p,"%G","NO_GROUP",l);
337 string_sub(p,"%g","NO_GROUP",l);
341 string_sub(p,"%D", domain,l);
344 string_sub(p,"%N", automount_server(username),l);
348 break; /* don't run off the end of the string */
355 standard_sub_basic(username, input_pstring, sizeof(pstring));
356 return talloc_strdup(mem_ctx, input_pstring);
359 /****************************************************************************
360 Do some standard substitutions in a string.
361 ****************************************************************************/
363 void standard_sub_conn(connection_struct *conn, char *str, size_t len)
365 standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
366 conn->gid, current_user_info.smb_name, str, len);
369 /****************************************************************************
370 Like standard_sub but by snum.
371 ****************************************************************************/
373 void standard_sub_snum(int snum, char *str, size_t len)
375 extern struct current_user current_user;
376 static uid_t cached_uid = -1;
377 static fstring cached_user;
378 /* calling uidtoname() on every substitute would be too expensive, so
379 we cache the result here as nearly every call is for the same uid */
381 if (cached_uid != current_user.uid) {
382 fstrcpy(cached_user, uidtoname(current_user.uid));
383 cached_uid = current_user.uid;
386 standard_sub_advanced(snum, cached_user, "", -1,
387 current_user_info.smb_name, str, len);