Merged in %S fixes and XX_NOT_CHANGED fixes from 2.2.
[samba.git] / source / lib / substitute.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    string substitution functions
5    Copyright (C) Andrew Tridgell 1992-2000
6    
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.
11    
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.
16    
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.
20 */
21
22
23 #include "includes.h"
24
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;
31
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 ********************************************************************/
38
39 static size_t expand_env_var(char *p, int len)
40 {
41         fstring envname;
42         char *envval;
43         char *q, *r;
44         int copylen;
45
46         if (p[1] != '$')
47                 return 1;
48
49         if (p[2] != '(')
50                 return 2;
51
52         /*
53          * Look for the terminating ')'.
54          */
55
56         if ((q = strchr_m(p,')')) == NULL) {
57                 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
58                 return 2;
59         }
60
61         /*
62          * Extract the name from within the %$(NAME) string.
63          */
64
65         r = p+3;
66         copylen = MIN((q-r),(sizeof(envname)-1));
67         strncpy(envname,r,copylen);
68         envname[copylen] = '\0';
69
70         if ((envval = getenv(envname)) == NULL) {
71                 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
72                 return 2;
73         }
74
75         /*
76          * Copy the full %$(NAME) into envname so it
77          * can be replaced.
78          */
79
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. */
85 }
86
87 /*******************************************************************
88  Patch from jkf@soton.ac.uk
89  Added this to implement %p (NIS auto-map version of %H)
90 *******************************************************************/
91
92 static char *automount_path(const char *user_name)
93 {
94         static pstring server_path;
95         struct passwd *pass;
96
97         /* use the passwd entry as the default */
98         /* this will be the default if WITH_AUTOMOUNT is not used or fails */
99
100         if (((pass = Get_Pwnam(user_name))!=NULL) && (pass->pw_dir != NULL))
101                 pstrcpy(server_path, pass->pw_dir );
102         else
103                 *server_path = '\0';
104
105 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
106
107         if (lp_nis_home_map()) {
108                 char *home_path_start;
109                 char *automount_value = automount_lookup(user_name);
110
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);
117                         }
118                 } else {
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 ));
122                 }
123         }
124 #endif
125
126         DEBUG(4,("Home server path: %s\n", server_path));
127
128         return server_path;
129 }
130
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 *******************************************************************/
136
137 static char *automount_server(const char *user_name)
138 {
139         static pstring server_name;
140
141         /* use the local machine name as the default */
142         /* this will be the default if WITH_AUTOMOUNT is not used or fails */
143         if (*local_machine)
144                 pstrcpy(server_name, local_machine);
145         else
146                 pstrcpy(server_name, global_myname);
147         
148 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
149
150         if (lp_nis_home_map()) {
151                 int home_server_len;
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';
159         }
160 #endif
161
162         DEBUG(4,("Home server: %s\n", server_name));
163
164         return server_name;
165 }
166
167 /****************************************************************************
168  Do some standard substitutions in a string.
169 ****************************************************************************/
170
171 void standard_sub_basic(const char *smb_name, char *str)
172 {
173         char *p, *s;
174         fstring pidstr;
175         struct passwd *pass;
176
177         for (s=str; (p=strchr_m(s, '%'));s=p) {
178                 fstring tmp_str;
179
180                 int l = sizeof(pstring) - (int)(p-str);
181                 
182                 switch (*(p+1)) {
183                 case 'U' : 
184                         fstrcpy(tmp_str, smb_name);
185                         strlower(tmp_str);
186                         string_sub(p,"%U",tmp_str,l);
187                         break;
188                 case 'G' :
189                         fstrcpy(tmp_str, smb_name);
190                         if ((pass = Get_Pwnam(tmp_str))!=NULL) {
191                                 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
192                         } else {
193                                 p += 2;
194                         }
195                         break;
196                 case 'D' :
197                         fstrcpy(tmp_str, current_user_info.domain);
198                         strupper(tmp_str);
199                         string_sub(p,"%D", tmp_str,l);
200                         break;
201                 case 'I' : string_sub(p,"%I", client_addr(),l); break;
202                 case 'L' : 
203                         if (*local_machine) {
204                                 string_sub(p,"%L", local_machine,l); 
205                         } else {
206                                 string_sub(p,"%L", global_myname,l); 
207                         }
208                         break;
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;
213                 case 'd' :
214                         slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
215                         string_sub(p,"%d", pidstr,l);
216                         break;
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 */
221                 case '\0': 
222                         p++; 
223                         break; /* don't run off the end of the string */
224                         
225                 default: p+=2; 
226                         break;
227                 }
228         }
229 }
230
231 /****************************************************************************
232  Do some standard substitutions in a string.
233 ****************************************************************************/
234
235 void standard_sub_advanced(int snum, const char *user, const char *connectpath, gid_t gid, const char *smb_name, char *str)
236 {
237         char *p, *s, *home;
238
239         for (s=str; (p=strchr_m(s, '%'));s=p) {
240                 int l = sizeof(pstring) - (int)(p-str);
241                 
242                 switch (*(p+1)) {
243                 case 'N' : string_sub(p,"%N", automount_server(user),l); break;
244                 case 'H':
245                         if ((home = get_user_home_dir(user))) {
246                                 string_sub(p,"%H",home, l);
247                         } else {
248                                 p += 2;
249                         }
250                         break;
251                 case 'P': 
252                         string_sub(p,"%P", connectpath, l); 
253                         break;
254                         
255                 case 'S': 
256                         string_sub(p,"%S", lp_servicename(snum), l); 
257                         break;
258                         
259                 case 'g': 
260                         string_sub(p,"%g", gidtoname(gid), l); 
261                         break;
262                 case 'u': 
263                         string_sub(p,"%u", user, l); 
264                         break;
265                         
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.  */
273                 case 'p': 
274                         string_sub(p,"%p", automount_path(lp_servicename(snum)), l); 
275                         break;
276                 case '\0': 
277                         p++; 
278                         break; /* don't run off the end of the string */
279                         
280                 default: p+=2; 
281                         break;
282                 }
283         }
284
285         standard_sub_basic(smb_name, str);
286 }
287
288 /****************************************************************************
289  Do some standard substitutions in a string.
290 ****************************************************************************/
291
292 void standard_sub_conn(connection_struct *conn, char *str)
293 {
294         standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, current_user_info.smb_name, str);
295 }
296
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 ****************************************************************************/
301
302 void standard_sub_home(int snum, const char *user, char *str)
303 {
304         char *p, *s;
305
306         for (s=str; (p=strchr_m(s, '%'));s=p) {
307                 int l = sizeof(pstring) - (int)(p-str);
308                 
309                 switch (*(p+1)) {
310                 case 'S': 
311                         string_sub(p,"%S", user, l); 
312                         break;
313                 case 'p': 
314                         string_sub(p,"%p", automount_path(user), l); 
315                         break;
316                 case '\0': 
317                         p++; 
318                         break; /* don't run off the end of the string */
319                         
320                 default: p+=2; 
321                         break;
322                 }
323         }
324
325         standard_sub_advanced(snum, user, "", -1, current_user_info.smb_name, str);
326 }
327
328 /****************************************************************************
329  Like standard_sub but by snum.
330 ****************************************************************************/
331
332 void standard_sub_snum(int snum, char *str)
333 {
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 */
339
340         if (cached_uid != current_user.uid) {
341                 fstrcpy(cached_user, uidtoname(current_user.uid));
342                 cached_uid = current_user.uid;
343         }
344
345         standard_sub_advanced(snum, cached_user, "", -1, current_user_info.smb_name, str);
346 }
347
348 /*******************************************************************
349  Substitute strings with useful parameters.
350 ********************************************************************/
351
352 void standard_sub_vuser(char *str, user_struct *vuser)
353 {
354         standard_sub_advanced(-1, vuser->user.unix_name, "", -1, current_user_info.smb_name, str);
355 }
356
357 /*******************************************************************
358  Substitute strings with useful parameters.
359 ********************************************************************/
360
361 void standard_sub_vsnum(char *str, user_struct *vuser, int snum)
362 {
363         standard_sub_advanced(snum, vuser->user.unix_name, "", -1, current_user_info.smb_name, str);
364 }