5a2bc1d890ce9d1cfa8201813138e3fbb546802c
[tprouty/samba.git] / source3 / 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         pstrcpy(server_path, get_user_home_dir(user_name));
101
102 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
103
104         if (lp_nis_home_map()) {
105                 char *home_path_start;
106                 char *automount_value = automount_lookup(user_name);
107
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);
114                         }
115                 } else {
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 ));
118                 }
119         }
120 #endif
121
122         DEBUG(4,("Home server path: %s\n", server_path));
123
124         return server_path;
125 }
126
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 *******************************************************************/
132
133 static char *automount_server(const char *user_name)
134 {
135         static pstring server_name;
136
137         /* use the local machine name as the default */
138         /* this will be the default if WITH_AUTOMOUNT is not used or fails */
139         if (*local_machine)
140                 pstrcpy(server_name, local_machine);
141         else
142                 pstrcpy(server_name, global_myname);
143         
144 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
145
146         if (lp_nis_home_map()) {
147                 int home_server_len;
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';
155         }
156 #endif
157
158         DEBUG(4,("Home server: %s\n", server_name));
159
160         return server_name;
161 }
162
163 /****************************************************************************
164  Do some standard substitutions in a string.
165 ****************************************************************************/
166
167 void standard_sub_basic(const char *smb_name, char *str)
168 {
169         char *p, *s;
170         fstring pidstr;
171         struct passwd *pass;
172
173         for (s=str; (p=strchr_m(s, '%'));s=p) {
174                 fstring tmp_str;
175
176                 int l = sizeof(pstring) - (int)(p-str);
177                 
178                 switch (*(p+1)) {
179                 case 'U' : 
180                         fstrcpy(tmp_str, smb_name);
181                         strlower(tmp_str);
182                         string_sub(p,"%U",tmp_str,l);
183                         break;
184                 case 'G' :
185                         fstrcpy(tmp_str, smb_name);
186                         if ((pass = Get_Pwnam(tmp_str))!=NULL) {
187                                 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
188                         } else {
189                                 p += 2;
190                         }
191                         break;
192                 case 'D' :
193                         fstrcpy(tmp_str, current_user_info.domain);
194                         strupper(tmp_str);
195                         string_sub(p,"%D", tmp_str,l);
196                         break;
197                 case 'I' : string_sub(p,"%I", client_addr(),l); break;
198                 case 'L' : 
199                         if (*local_machine) {
200                                 string_sub(p,"%L", local_machine,l); 
201                         } else {
202                                 string_sub(p,"%L", global_myname,l); 
203                         }
204                         break;
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;
209                 case 'd' :
210                         slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
211                         string_sub(p,"%d", pidstr,l);
212                         break;
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 */
217                 case '\0': 
218                         p++; 
219                         break; /* don't run off the end of the string */
220                         
221                 default: p+=2; 
222                         break;
223                 }
224         }
225 }
226
227 /****************************************************************************
228  Do some standard substitutions in a string.
229 ****************************************************************************/
230
231 void standard_sub_advanced(int snum, const char *user, const char *connectpath, gid_t gid, const char *smb_name, char *str)
232 {
233         char *p, *s, *home;
234
235         for (s=str; (p=strchr_m(s, '%'));s=p) {
236                 int l = sizeof(pstring) - (int)(p-str);
237                 
238                 switch (*(p+1)) {
239                 case 'N' : string_sub(p,"%N", automount_server(user),l); break;
240                 case 'H':
241                         if ((home = get_user_home_dir(user))) {
242                                 string_sub(p,"%H",home, l);
243                         } else {
244                                 p += 2;
245                         }
246                         break;
247                 case 'P': 
248                         string_sub(p,"%P", connectpath, l); 
249                         break;
250                         
251                 case 'S': 
252                         string_sub(p,"%S", lp_servicename(snum), l); 
253                         break;
254                         
255                 case 'g': 
256                         string_sub(p,"%g", gidtoname(gid), l); 
257                         break;
258                 case 'u': 
259                         string_sub(p,"%u", user, l); 
260                         break;
261                         
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.  */
269                 case 'p': 
270                         string_sub(p,"%p", automount_path(lp_servicename(snum)), l); 
271                         break;
272                 case '\0': 
273                         p++; 
274                         break; /* don't run off the end of the string */
275                         
276                 default: p+=2; 
277                         break;
278                 }
279         }
280
281         standard_sub_basic(smb_name, str);
282 }
283
284 /****************************************************************************
285  Do some standard substitutions in a string.
286 ****************************************************************************/
287
288 void standard_sub_conn(connection_struct *conn, char *str)
289 {
290         standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, current_user_info.smb_name, str);
291 }
292
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 ****************************************************************************/
297
298 void standard_sub_home(int snum, const char *user, char *str)
299 {
300         char *p, *s;
301
302         for (s=str; (p=strchr_m(s, '%'));s=p) {
303                 int l = sizeof(pstring) - (int)(p-str);
304                 
305                 switch (*(p+1)) {
306                 case 'S': 
307                         string_sub(p,"%S", user, l); 
308                         break;
309                 case 'p': 
310                         string_sub(p,"%p", automount_path(user), l); 
311                         break;
312                 case '\0': 
313                         p++; 
314                         break; /* don't run off the end of the string */
315                         
316                 default: p+=2; 
317                         break;
318                 }
319         }
320
321         standard_sub_advanced(snum, user, "", -1, current_user_info.smb_name, str);
322 }
323
324 /****************************************************************************
325  Like standard_sub but by snum.
326 ****************************************************************************/
327
328 void standard_sub_snum(int snum, char *str)
329 {
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 */
335
336         if (cached_uid != current_user.uid) {
337                 fstrcpy(cached_user, uidtoname(current_user.uid));
338                 cached_uid = current_user.uid;
339         }
340
341         standard_sub_advanced(snum, cached_user, "", -1, current_user_info.smb_name, str);
342 }
343
344 /*******************************************************************
345  Substitute strings with useful parameters.
346 ********************************************************************/
347
348 void standard_sub_vuser(char *str, user_struct *vuser)
349 {
350         standard_sub_advanced(-1, vuser->user.unix_name, "", -1, current_user_info.smb_name, str);
351 }
352
353 /*******************************************************************
354  Substitute strings with useful parameters.
355 ********************************************************************/
356
357 void standard_sub_vsnum(char *str, user_struct *vuser, int snum)
358 {
359         standard_sub_advanced(snum, vuser->user.unix_name, "", -1, current_user_info.smb_name, str);
360 }