Clean up a few unused functions, add a bit of static etc.
[ira/wip.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 ****************************************************************************/
164
165 void standard_sub_basic(const char *smb_name, char *str)
166 {
167         char *p, *s;
168         fstring pidstr;
169         struct passwd *pass;
170
171         for (s=str; (p=strchr_m(s, '%'));s=p) {
172                 fstring tmp_str;
173
174                 int l = sizeof(pstring) - (int)(p-str);
175                 
176                 switch (*(p+1)) {
177                 case 'U' : 
178                         fstrcpy(tmp_str, smb_name);
179                         strlower(tmp_str);
180                         string_sub(p,"%U",tmp_str,l);
181                         break;
182                 case 'G' :
183                         fstrcpy(tmp_str, smb_name);
184                         if ((pass = Get_Pwnam(tmp_str))!=NULL) {
185                                 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
186                         } else {
187                                 p += 2;
188                         }
189                         break;
190                 case 'D' :
191                         fstrcpy(tmp_str, current_user_info.domain);
192                         strupper(tmp_str);
193                         string_sub(p,"%D", tmp_str,l);
194                         break;
195                 case 'I' : string_sub(p,"%I", client_addr(),l); break;
196                 case 'L' : 
197                         if (*local_machine) {
198                                 string_sub(p,"%L", local_machine,l); 
199                         } else {
200                                 string_sub(p,"%L", global_myname,l); 
201                         }
202                         break;
203                 case 'M' : string_sub(p,"%M", client_name(),l); break;
204                 case 'R' : string_sub(p,"%R", remote_proto,l); break;
205                 case 'T' : string_sub(p,"%T", timestring(False),l); break;
206                 case 'a' : string_sub(p,"%a", remote_arch,l); break;
207                 case 'd' :
208                         slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
209                         string_sub(p,"%d", pidstr,l);
210                         break;
211                 case 'h' : string_sub(p,"%h", myhostname(),l); break;
212                 case 'm' : string_sub(p,"%m", remote_machine,l); break;
213                 case 'v' : string_sub(p,"%v", VERSION,l); break;
214                 case '$' : p += expand_env_var(p,l); break; /* Expand environment variables */
215                 case '\0': 
216                         p++; 
217                         break; /* don't run off the end of the string */
218                         
219                 default: p+=2; 
220                         break;
221                 }
222         }
223 }
224
225 /****************************************************************************
226  Do some standard substitutions in a string.
227 ****************************************************************************/
228
229 static void standard_sub_advanced(int snum, const char *user, const char *connectpath, gid_t gid, const char *smb_name, char *str)
230 {
231         char *p, *s, *home;
232
233         for (s=str; (p=strchr_m(s, '%'));s=p) {
234                 int l = sizeof(pstring) - (int)(p-str);
235                 
236                 switch (*(p+1)) {
237                 case 'N' : string_sub(p,"%N", automount_server(user),l); break;
238                 case 'H':
239                         if ((home = get_user_home_dir(user))) {
240                                 string_sub(p,"%H",home, l);
241                         } else {
242                                 p += 2;
243                         }
244                         break;
245                 case 'P': 
246                         string_sub(p,"%P", connectpath, l); 
247                         break;
248                         
249                 case 'S': 
250                         string_sub(p,"%S", lp_servicename(snum), l); 
251                         break;
252                         
253                 case 'g': 
254                         string_sub(p,"%g", gidtoname(gid), l); 
255                         break;
256                 case 'u': 
257                         string_sub(p,"%u", user, l); 
258                         break;
259                         
260                         /* Patch from jkf@soton.ac.uk Left the %N (NIS
261                          * server name) in standard_sub_basic as it is
262                          * a feature for logon servers, hence uses the
263                          * username.  The %p (NIS server path) code is
264                          * here as it is used instead of the default
265                          * "path =" string in [homes] and so needs the
266                          * service name, not the username.  */
267                 case 'p': 
268                         string_sub(p,"%p", automount_path(lp_servicename(snum)), l); 
269                         break;
270                 case '\0': 
271                         p++; 
272                         break; /* don't run off the end of the string */
273                         
274                 default: p+=2; 
275                         break;
276                 }
277         }
278
279         standard_sub_basic(smb_name, str);
280 }
281
282 const char *standard_sub_specified(TALLOC_CTX *mem_ctx, const char *input_string,
283                                    const char *username,
284                                    const char *domain,
285                                    uid_t uid,
286                                    gid_t gid) 
287 {
288         pstring input_pstring;
289         char *p, *s;
290
291         pstrcpy(input_pstring, input_string);
292         
293         for (s=input_pstring; (p=strchr_m(s, '%')); s=p) {
294
295                 int l = sizeof(pstring) - (int)(p-input_pstring);
296                 
297                 switch (*(p+1)) {
298                 case 'U' : 
299                         string_sub(p,"%U",username,l);
300                         break;
301                 case 'u' : 
302                         string_sub(p,"%u",username,l);
303                         break;
304                 case 'G' :
305                 case 'g' :
306                         if (gid != -1) {
307                                 string_sub(p,"%G",gidtoname(gid),l);
308                                 string_sub(p,"%g",gidtoname(gid),l);
309                         } else {
310                                 string_sub(p,"%G","NO_GROUP",l);
311                                 string_sub(p,"%g","NO_GROUP",l);
312                         }
313                         break;
314                 case 'D' :
315                         string_sub(p,"%D", domain,l);
316                         break;
317                 case 'N' : 
318                         string_sub(p,"%N", automount_server(username),l); 
319                         break;
320                 case '\0': 
321                         p++; 
322                         break; /* don't run off the end of the string */
323                         
324                 default: p+=2; 
325                         break;
326                 }
327         }
328
329         standard_sub_basic(username, input_pstring);
330         
331         return talloc_strdup(mem_ctx, input_pstring);
332 }
333
334 /****************************************************************************
335  Do some standard substitutions in a string.
336 ****************************************************************************/
337
338 void standard_sub_conn(connection_struct *conn, char *str)
339 {
340         standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath, conn->gid, current_user_info.smb_name, str);
341 }
342
343 /****************************************************************************
344  Like standard_sub but for a homes share where snum still points to the [homes]
345  share. No user specific snum created yet so servicename should be the username.
346 ****************************************************************************/
347
348 void standard_sub_home(int snum, const char *user, char *str)
349 {
350         char *p, *s;
351
352         for (s=str; (p=strchr_m(s, '%'));s=p) {
353                 int l = sizeof(pstring) - (int)(p-str);
354                 
355                 switch (*(p+1)) {
356                 case 'S': 
357                         string_sub(p,"%S", user, l); 
358                         break;
359                 case 'p': 
360                         string_sub(p,"%p", automount_path(user), l); 
361                         break;
362                 case '\0': 
363                         p++; 
364                         break; /* don't run off the end of the string */
365                         
366                 default: p+=2; 
367                         break;
368                 }
369         }
370
371         standard_sub_advanced(snum, user, "", -1, current_user_info.smb_name, str);
372 }
373
374 /****************************************************************************
375  Like standard_sub but by snum.
376 ****************************************************************************/
377
378 void standard_sub_snum(int snum, char *str)
379 {
380         extern struct current_user current_user;
381         static uid_t cached_uid = -1;
382         static fstring cached_user;
383         /* calling uidtoname() on every substitute would be too expensive, so
384            we cache the result here as nearly every call is for the same uid */
385
386         if (cached_uid != current_user.uid) {
387                 fstrcpy(cached_user, uidtoname(current_user.uid));
388                 cached_uid = current_user.uid;
389         }
390
391         standard_sub_advanced(snum, cached_user, "", -1, current_user_info.smb_name, str);
392 }
393