Address the string_sub problem by changing len = 0 to mean "no expand".
[samba.git] / source / lib / substitute.c
1 /* 
2    Unix SMB/CIFS implementation.
3    string substitution functions
4    Copyright (C) Andrew Tridgell 1992-2000
5    
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.
10    
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.
15    
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.
19 */
20
21
22 #include "includes.h"
23
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;
30
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 ********************************************************************/
37
38 static size_t expand_env_var(char *p, int len)
39 {
40         fstring envname;
41         char *envval;
42         char *q, *r;
43         int copylen;
44
45         if (p[1] != '$')
46                 return 1;
47
48         if (p[2] != '(')
49                 return 2;
50
51         /*
52          * Look for the terminating ')'.
53          */
54
55         if ((q = strchr_m(p,')')) == NULL) {
56                 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
57                 return 2;
58         }
59
60         /*
61          * Extract the name from within the %$(NAME) string.
62          */
63
64         r = p+3;
65         copylen = MIN((q-r),(sizeof(envname)-1));
66         strncpy(envname,r,copylen);
67         envname[copylen] = '\0';
68
69         if ((envval = getenv(envname)) == NULL) {
70                 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
71                 return 2;
72         }
73
74         /*
75          * Copy the full %$(NAME) into envname so it
76          * can be replaced.
77          */
78
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. */
84 }
85
86 /*******************************************************************
87  Patch from jkf@soton.ac.uk
88  Added this to implement %p (NIS auto-map version of %H)
89 *******************************************************************/
90
91 static char *automount_path(const char *user_name)
92 {
93         static pstring server_path;
94
95         /* use the passwd entry as the default */
96         /* this will be the default if WITH_AUTOMOUNT is not used or fails */
97
98         pstrcpy(server_path, get_user_home_dir(user_name));
99
100 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
101
102         if (lp_nis_home_map()) {
103                 char *home_path_start;
104                 char *automount_value = automount_lookup(user_name);
105
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);
112                         }
113                 } else {
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 ));
116                 }
117         }
118 #endif
119
120         DEBUG(4,("Home server path: %s\n", server_path));
121
122         return server_path;
123 }
124
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 *******************************************************************/
130
131 static char *automount_server(const char *user_name)
132 {
133         static pstring server_name;
134
135         /* use the local machine name as the default */
136         /* this will be the default if WITH_AUTOMOUNT is not used or fails */
137         if (*local_machine)
138                 pstrcpy(server_name, local_machine);
139         else
140                 pstrcpy(server_name, global_myname);
141         
142 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
143
144         if (lp_nis_home_map()) {
145                 int home_server_len;
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';
153         }
154 #endif
155
156         DEBUG(4,("Home server: %s\n", server_name));
157
158         return server_name;
159 }
160
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 ****************************************************************************/
166
167 void standard_sub_basic(const char *smb_name, char *str,size_t len)
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 = (int)len - (int)(p-str);
177
178                 if (l < 0)
179                         l = 0;
180                 
181                 switch (*(p+1)) {
182                 case 'U' : 
183                         fstrcpy(tmp_str, smb_name);
184                         strlower(tmp_str);
185                         string_sub(p,"%U",tmp_str,l);
186                         break;
187                 case 'G' :
188                         fstrcpy(tmp_str, smb_name);
189                         if ((pass = Get_Pwnam(tmp_str))!=NULL) {
190                                 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
191                         } else {
192                                 p += 2;
193                         }
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' :
201                         string_sub(p,"%I", client_addr(),l);
202                         break;
203                 case 'L' : 
204                         if (*local_machine)
205                                 string_sub(p,"%L", local_machine,l); 
206                         else
207                                 string_sub(p,"%L", global_myname,l); 
208                         break;
209                 case 'M' :
210                         string_sub(p,"%M", client_name(),l);
211                         break;
212                 case 'R' :
213                         string_sub(p,"%R", remote_proto,l);
214                         break;
215                 case 'T' :
216                         string_sub(p,"%T", timestring(False),l);
217                         break;
218                 case 'a' :
219                         string_sub(p,"%a", remote_arch,l);
220                         break;
221                 case 'd' :
222                         slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
223                         string_sub(p,"%d", pidstr,l);
224                         break;
225                 case 'h' :
226                         string_sub(p,"%h", myhostname(),l);
227                         break;
228                 case 'm' :
229                         string_sub(p,"%m", remote_machine,l);
230                         break;
231                 case 'v' :
232                         string_sub(p,"%v", VERSION,l);
233                         break;
234                 case '$' :
235                         p += expand_env_var(p,l);
236                         break; /* Expand environment variables */
237                 case '\0': 
238                         p++; 
239                         break; /* don't run off the end of the string */
240                         
241                 default: p+=2; 
242                         break;
243                 }
244         }
245 }
246
247 /****************************************************************************
248  Do some standard substitutions in a string.
249 ****************************************************************************/
250
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)
254 {
255         char *p, *s, *home;
256
257         for (s=str; (p=strchr_m(s, '%'));s=p) {
258                 int l = (int)len - (int)(p-str);
259         
260                 if (l < 0)
261                         l = 0;
262         
263                 switch (*(p+1)) {
264                 case 'N' :
265                         string_sub(p,"%N", automount_server(user),l);
266                         break;
267                 case 'H':
268                         if ((home = get_user_home_dir(user)))
269                                 string_sub(p,"%H",home, l);
270                         else
271                                 p += 2;
272                         break;
273                 case 'P': 
274                         string_sub(p,"%P", connectpath, l); 
275                         break;
276                 case 'S': 
277                         string_sub(p,"%S", lp_servicename(snum), l); 
278                         break;
279                 case 'g': 
280                         string_sub(p,"%g", gidtoname(gid), l); 
281                         break;
282                 case 'u': 
283                         string_sub(p,"%u", user, l); 
284                         break;
285                         
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.  */
293                 case 'p': 
294                         string_sub(p,"%p", automount_path(lp_servicename(snum)), l); 
295                         break;
296                 case '\0': 
297                         p++; 
298                         break; /* don't run off the end of the string */
299                         
300                 default: p+=2; 
301                         break;
302                 }
303         }
304
305         standard_sub_basic(smb_name, str, len);
306 }
307
308 const char *standard_sub_specified(TALLOC_CTX *mem_ctx, const char *input_string,
309                                    const char *username,
310                                    const char *domain,
311                                    uid_t uid,
312                                    gid_t gid) 
313 {
314         pstring input_pstring;
315         char *p, *s;
316
317         pstrcpy(input_pstring, input_string);
318         
319         for (s=input_pstring; (p=strchr_m(s, '%')); s=p) {
320
321                 int l = sizeof(pstring) - (int)(p-input_pstring);
322                 
323                 switch (*(p+1)) {
324                 case 'U' : 
325                         string_sub(p,"%U",username,l);
326                         break;
327                 case 'u' : 
328                         string_sub(p,"%u",username,l);
329                         break;
330                 case 'G' :
331                 case 'g' :
332                         if (gid != -1) {
333                                 string_sub(p,"%G",gidtoname(gid),l);
334                                 string_sub(p,"%g",gidtoname(gid),l);
335                         } else {
336                                 string_sub(p,"%G","NO_GROUP",l);
337                                 string_sub(p,"%g","NO_GROUP",l);
338                         }
339                         break;
340                 case 'D' :
341                         string_sub(p,"%D", domain,l);
342                         break;
343                 case 'N' : 
344                         string_sub(p,"%N", automount_server(username),l); 
345                         break;
346                 case '\0': 
347                         p++; 
348                         break; /* don't run off the end of the string */
349                         
350                 default: p+=2; 
351                         break;
352                 }
353         }
354
355         standard_sub_basic(username, input_pstring, sizeof(pstring));
356         return talloc_strdup(mem_ctx, input_pstring);
357 }
358
359 /****************************************************************************
360  Do some standard substitutions in a string.
361 ****************************************************************************/
362
363 void standard_sub_conn(connection_struct *conn, char *str, size_t len)
364 {
365         standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
366                         conn->gid, current_user_info.smb_name, str, len);
367 }
368
369 /****************************************************************************
370  Like standard_sub but by snum.
371 ****************************************************************************/
372
373 void standard_sub_snum(int snum, char *str, size_t len)
374 {
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 */
380
381         if (cached_uid != current_user.uid) {
382                 fstrcpy(cached_user, uidtoname(current_user.uid));
383                 cached_uid = current_user.uid;
384         }
385
386         standard_sub_advanced(snum, cached_user, "", -1,
387                 current_user_info.smb_name, str, len);
388 }