removed stat() call in lp_add_home()
[Samba.git] / source / lib / substitute.c
blob95393a94ede7e799240edff1ea19bac6c79201df
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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.
23 #include "includes.h"
25 fstring local_machine="";
26 fstring remote_arch="UNKNOWN";
27 userdom_struct current_user_info;
28 pstring samlogon_user="";
29 BOOL sam_logon_in_ssb = False;
30 fstring remote_proto="UNKNOWN";
31 fstring remote_machine="";
34 /*******************************************************************
35 Given a pointer to a %$(NAME) expand it as an environment variable.
36 Return the number of characters by which the pointer should be advanced.
37 Based on code by Branko Cibej <branko.cibej@hermes.si>
38 When this is called p points at the '%' character.
39 ********************************************************************/
41 static size_t expand_env_var(char *p, int len)
43 fstring envname;
44 char *envval;
45 char *q, *r;
46 int copylen;
48 if (p[1] != '$')
49 return 1;
51 if (p[2] != '(')
52 return 2;
55 * Look for the terminating ')'.
58 if ((q = strchr(p,')')) == NULL) {
59 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
60 return 2;
64 * Extract the name from within the %$(NAME) string.
67 r = p+3;
68 copylen = MIN((q-r),(sizeof(envname)-1));
69 strncpy(envname,r,copylen);
70 envname[copylen] = '\0';
72 if ((envval = getenv(envname)) == NULL) {
73 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
74 return 2;
78 * Copy the full %$(NAME) into envname so it
79 * can be replaced.
82 copylen = MIN((q+1-p),(sizeof(envname)-1));
83 strncpy(envname,p,copylen);
84 envname[copylen] = '\0';
85 string_sub(p,envname,envval,len);
86 return 0; /* Allow the environment contents to be parsed. */
89 /*******************************************************************
90 Patch from jkf@soton.ac.uk
91 Added this to implement %p (NIS auto-map version of %H)
92 *******************************************************************/
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 */
101 pstrcpy(server_path, get_user_home_dir(user_name));
103 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
105 if (lp_nis_home_map()) {
106 char *home_path_start;
107 char *automount_value = automount_lookup(user_name);
109 if(strlen(automount_value) > 0) {
110 home_path_start = strchr(automount_value,':');
111 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);
116 } else {
117 /* NIS key lookup failed: default to user home directory from password file */
118 DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path ));
121 #endif
123 DEBUG(4,("Home server path: %s\n", server_path));
125 return server_path;
128 /*******************************************************************
129 Patch from jkf@soton.ac.uk
130 This is Luke's original function with the NIS lookup code
131 moved out to a separate function.
132 *******************************************************************/
134 static char *automount_server(char *user_name)
136 extern pstring global_myname;
137 static pstring server_name;
139 /* use the local machine name as the default */
140 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
141 if (*local_machine)
142 pstrcpy(server_name, local_machine);
143 else
144 pstrcpy(server_name, global_myname);
146 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
148 if (lp_nis_home_map()) {
149 int home_server_len;
150 char *automount_value = automount_lookup(user_name);
151 home_server_len = strcspn(automount_value,":");
152 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len));
153 if (home_server_len > sizeof(pstring))
154 home_server_len = sizeof(pstring);
155 strncpy(server_name, automount_value, home_server_len);
156 server_name[home_server_len] = '\0';
158 #endif
160 DEBUG(4,("Home server: %s\n", server_name));
162 return server_name;
165 /****************************************************************************
166 Do some standard substitutions in a string.
167 ****************************************************************************/
169 void standard_sub_basic(char *str, int len)
171 extern pstring global_myname;
172 char *p, *s;
173 fstring pidstr;
174 struct passwd *pass;
176 for (s=str; (p=strchr(s, '%'));s=p) {
177 fstring tmp_str;
179 int l = len - (int)(p-str);
181 switch (*(p+1)) {
182 case 'U' :
183 fstrcpy(tmp_str, sam_logon_in_ssb?samlogon_user:current_user_info.smb_name);
184 strlower(tmp_str);
185 string_sub(p,"%U",tmp_str,l);
186 break;
187 case 'G' :
188 fstrcpy(tmp_str, sam_logon_in_ssb?samlogon_user:current_user_info.smb_name);
189 if ((pass = Get_Pwnam(tmp_str, False))!=NULL) {
190 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
191 } else {
192 p += 2;
194 break;
195 case 'D' :
196 fstrcpy(tmp_str, current_user_info.domain);
197 strupper(tmp_str);
198 string_sub(p,"%D", tmp_str,l);
199 break;
200 case 'I' : string_sub(p,"%I", client_addr(),l); break;
201 case 'L' :
202 if (*local_machine)
203 string_sub(p,"%L", local_machine,l);
204 else {
205 pstring temp_name;
207 pstrcpy(temp_name, global_myname);
208 strlower(temp_name);
209 string_sub(p,"%L", temp_name,l);
211 break;
212 case 'M' : string_sub(p,"%M", client_name(),l); break;
213 case 'R' : string_sub(p,"%R", remote_proto,l); break;
214 case 'T' : string_sub(p,"%T", timestring(False),l); break;
215 case 'a' : string_sub(p,"%a", remote_arch,l); break;
216 case 'd' :
217 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
218 string_sub(p,"%d", pidstr,l);
219 break;
220 case 'h' : string_sub(p,"%h", myhostname(),l); break;
221 case 'm' : string_sub(p,"%m", remote_machine,l); break;
222 case 'v' : string_sub(p,"%v", VERSION,l); break;
223 case '$' : p += expand_env_var(p,l); break; /* Expand environment variables */
224 case '\0':
225 p++;
226 break; /* don't run off the end of the string */
228 default: p+=2;
229 break;
234 /****************************************************************************
235 Do some standard substitutions in a string.
236 ****************************************************************************/
238 void standard_sub_advanced(int snum, char *user, char *connectpath, gid_t gid, char *str, int len)
240 char *p, *s, *home;
242 for (s=str; (p=strchr(s, '%'));s=p) {
243 int l = len - (int)(p-str);
245 switch (*(p+1)) {
246 case 'N' : string_sub(p,"%N", automount_server(user),l); break;
247 case 'H':
248 if ((home = get_user_home_dir(user))) {
249 string_sub(p,"%H",home, l);
250 } else {
251 p += 2;
253 break;
254 case 'P':
255 string_sub(p,"%P", connectpath, l);
256 break;
258 case 'S':
259 string_sub(p,"%S", lp_servicename(snum), l);
260 break;
262 case 'g':
263 string_sub(p,"%g", gidtoname(gid), l);
264 break;
265 case 'u':
266 string_sub(p,"%u", user, l);
267 break;
269 /* Patch from jkf@soton.ac.uk Left the %N (NIS
270 * server name) in standard_sub_basic as it is
271 * a feature for logon servers, hence uses the
272 * username. The %p (NIS server path) code is
273 * here as it is used instead of the default
274 * "path =" string in [homes] and so needs the
275 * service name, not the username. */
276 case 'p':
277 string_sub(p,"%p", automount_path(lp_servicename(snum)), l);
278 break;
279 case '\0':
280 p++;
281 break; /* don't run off the end of the string */
283 default: p+=2;
284 break;
288 standard_sub_basic(str,len);
291 /****************************************************************************
292 Do some standard substitutions in a string.
293 ****************************************************************************/
295 void standard_sub_conn(connection_struct *conn, char *str, int len)
297 standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, str, len);
300 /****************************************************************************
301 Like standard_sub but for a homes share where snum still points to the [homes]
302 share. No user specific snum created yet so servicename should be the username.
303 ****************************************************************************/
305 void standard_sub_home(int snum, char *user, char *str, int len)
307 char *p, *s;
309 for (s=str; (p=strchr(s, '%'));s=p) {
310 int l = len - (int)(p-str);
312 switch (*(p+1)) {
313 case 'S':
314 string_sub(p,"%S", user, l);
315 break;
316 case 'p':
317 string_sub(p,"%p", automount_path(user), l);
318 break;
319 case '\0':
320 p++;
321 break; /* don't run off the end of the string */
323 default: p+=2;
324 break;
328 standard_sub_advanced(snum, user, "", -1, str, len);
331 /****************************************************************************
332 Like standard_sub but by snum.
333 ****************************************************************************/
335 void standard_sub_snum(int snum, char *str, int len)
337 extern struct current_user current_user;
338 static uid_t cached_uid = -1;
339 static fstring cached_user;
340 /* calling uidtoname() on every substitute would be too expensive, so
341 we cache the result here as nearly every call is for the same uid */
343 if (cached_uid != current_user.uid) {
344 fstrcpy(cached_user, uidtoname(current_user.uid));
345 cached_uid = current_user.uid;
348 standard_sub_advanced(snum, cached_user, "", -1, str, len);
351 /*******************************************************************
352 Substitute strings with useful parameters.
353 ********************************************************************/
355 void standard_sub_vuser(char *str, int len, user_struct *vuser)
357 standard_sub_advanced(-1, vuser->user.unix_name, "", -1, str, len);
360 /*******************************************************************
361 Substitute strings with useful parameters.
362 ********************************************************************/
364 void standard_sub_vsnum(char *str, int len, user_struct *vuser, int snum)
366 standard_sub_advanced(snum, vuser->user.unix_name, "", -1, str, len);