This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[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  Given a pointer to a %$(NAME) in p and the whole string in str
88  expand it as an environment variable.
89  Return a new allocated and expanded string.
90  Based on code by Branko Cibej <branko.cibej@hermes.si>
91  When this is called p points at the '%' character.
92  May substitute multiple occurrencies of the same env var.
93 ********************************************************************/
94
95
96 static char * realloc_expand_env_var(char *str, char *p)
97 {
98         char *envname;
99         char *envval;
100         char *q, *r;
101         int copylen;
102
103         if (p[0] != '%' || p[1] != '$' || p[2] != '(')
104                 return str;
105
106         /*
107          * Look for the terminating ')'.
108          */
109
110         if ((q = strchr_m(p,')')) == NULL) {
111                 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
112                 return str;
113         }
114
115         /*
116          * Extract the name from within the %$(NAME) string.
117          */
118
119         r = p + 3;
120         copylen = q - r;
121         envname = (char *)malloc(copylen + 1 + 4); /* reserve space for use later add %$() chars */
122         if (envname == NULL) return NULL;
123         strncpy(envname,r,copylen);
124         envname[copylen] = '\0';
125
126         if ((envval = getenv(envname)) == NULL) {
127                 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
128                 SAFE_FREE(envname);
129                 return str;
130         }
131
132         /*
133          * Copy the full %$(NAME) into envname so it
134          * can be replaced.
135          */
136
137         copylen = q + 1 - p;
138         strncpy(envname,p,copylen);
139         envname[copylen] = '\0';
140         r = realloc_string_sub(str, envname, envval);
141         SAFE_FREE(envname);
142         if (r == NULL) return NULL;
143         return r;
144 }
145
146 /*******************************************************************
147  Patch from jkf@soton.ac.uk
148  Added this to implement %p (NIS auto-map version of %H)
149 *******************************************************************/
150
151 static char *automount_path(const char *user_name)
152 {
153         static pstring server_path;
154
155         /* use the passwd entry as the default */
156         /* this will be the default if WITH_AUTOMOUNT is not used or fails */
157
158         pstrcpy(server_path, get_user_home_dir(user_name));
159
160 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
161
162         if (lp_nis_home_map()) {
163                 char *home_path_start;
164                 char *automount_value = automount_lookup(user_name);
165
166                 if(strlen(automount_value) > 0) {
167                         home_path_start = strchr_m(automount_value,':');
168                         if (home_path_start != NULL) {
169                                 DEBUG(5, ("NIS lookup succeeded.  Home path is: %s\n",
170                                                 home_path_start?(home_path_start+1):""));
171                                 pstrcpy(server_path, home_path_start+1);
172                         }
173                 } else {
174                         /* NIS key lookup failed: default to user home directory from password file */
175                         DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path ));
176                 }
177         }
178 #endif
179
180         DEBUG(4,("Home server path: %s\n", server_path));
181
182         return server_path;
183 }
184
185 /*******************************************************************
186  Patch from jkf@soton.ac.uk
187  This is Luke's original function with the NIS lookup code
188  moved out to a separate function.
189 *******************************************************************/
190
191 static char *automount_server(const char *user_name)
192 {
193         static pstring server_name;
194
195         /* use the local machine name as the default */
196         /* this will be the default if WITH_AUTOMOUNT is not used or fails */
197         if (*local_machine)
198                 pstrcpy(server_name, local_machine);
199         else
200                 pstrcpy(server_name, global_myname);
201         
202 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
203
204         if (lp_nis_home_map()) {
205                 int home_server_len;
206                 char *automount_value = automount_lookup(user_name);
207                 home_server_len = strcspn(automount_value,":");
208                 DEBUG(5, ("NIS lookup succeeded.  Home server length: %d\n",home_server_len));
209                 if (home_server_len > sizeof(pstring))
210                         home_server_len = sizeof(pstring);
211                 strncpy(server_name, automount_value, home_server_len);
212                 server_name[home_server_len] = '\0';
213         }
214 #endif
215
216         DEBUG(4,("Home server: %s\n", server_name));
217
218         return server_name;
219 }
220
221 /****************************************************************************
222  Do some standard substitutions in a string.
223  len is the length in bytes of the space allowed in string str. If zero means
224  don't allow expansions.
225 ****************************************************************************/
226
227 void standard_sub_basic(const char *smb_name, char *str,size_t len)
228 {
229         char *p, *s;
230         fstring pidstr;
231         struct passwd *pass;
232
233         for (s=str; (p=strchr_m(s, '%'));s=p) {
234                 fstring tmp_str;
235
236                 int l = (int)len - (int)(p-str);
237
238                 if (l < 0)
239                         l = 0;
240                 
241                 switch (*(p+1)) {
242                 case 'U' : 
243                         fstrcpy(tmp_str, smb_name);
244                         strlower(tmp_str);
245                         string_sub(p,"%U",tmp_str,l);
246                         break;
247                 case 'G' :
248                         fstrcpy(tmp_str, smb_name);
249                         if ((pass = Get_Pwnam(tmp_str))!=NULL) {
250                                 string_sub(p,"%G",gidtoname(pass->pw_gid),l);
251                         } else {
252                                 p += 2;
253                         }
254                         break;
255                 case 'D' :
256                         fstrcpy(tmp_str, current_user_info.domain);
257                         strupper(tmp_str);
258                         string_sub(p,"%D", tmp_str,l);
259                         break;
260                 case 'I' :
261                         string_sub(p,"%I", client_addr(),l);
262                         break;
263                 case 'L' : 
264                         if (*local_machine)
265                                 string_sub(p,"%L", local_machine,l); 
266                         else
267                                 string_sub(p,"%L", global_myname,l); 
268                         break;
269                 case 'M' :
270                         string_sub(p,"%M", client_name(),l);
271                         break;
272                 case 'R' :
273                         string_sub(p,"%R", remote_proto,l);
274                         break;
275                 case 'T' :
276                         string_sub(p,"%T", timestring(False),l);
277                         break;
278                 case 'a' :
279                         string_sub(p,"%a", remote_arch,l);
280                         break;
281                 case 'd' :
282                         slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
283                         string_sub(p,"%d", pidstr,l);
284                         break;
285                 case 'h' :
286                         string_sub(p,"%h", myhostname(),l);
287                         break;
288                 case 'm' :
289                         string_sub(p,"%m", remote_machine,l);
290                         break;
291                 case 'v' :
292                         string_sub(p,"%v", VERSION,l);
293                         break;
294                 case '$' :
295                         p += expand_env_var(p,l);
296                         break; /* Expand environment variables */
297                 case '\0': 
298                         p++; 
299                         break; /* don't run off the end of the string */
300                         
301                 default: p+=2; 
302                         break;
303                 }
304         }
305 }
306
307 static void standard_sub_advanced(int snum, const char *user, 
308                                   const char *connectpath, gid_t gid, 
309                                   const char *smb_name, char *str, size_t len)
310 {
311         char *p, *s, *home;
312
313         for (s=str; (p=strchr_m(s, '%'));s=p) {
314                 int l = (int)len - (int)(p-str);
315         
316                 if (l < 0)
317                         l = 0;
318         
319                 switch (*(p+1)) {
320                 case 'N' :
321                         string_sub(p,"%N", automount_server(user),l);
322                         break;
323                 case 'H':
324                         if ((home = get_user_home_dir(user)))
325                                 string_sub(p,"%H",home, l);
326                         else
327                                 p += 2;
328                         break;
329                 case 'P': 
330                         string_sub(p,"%P", connectpath, l); 
331                         break;
332                 case 'S': 
333                         string_sub(p,"%S", lp_servicename(snum), l); 
334                         break;
335                 case 'g': 
336                         string_sub(p,"%g", gidtoname(gid), l); 
337                         break;
338                 case 'u': 
339                         string_sub(p,"%u", user, l); 
340                         break;
341                         
342                         /* Patch from jkf@soton.ac.uk Left the %N (NIS
343                          * server name) in standard_sub_basic as it is
344                          * a feature for logon servers, hence uses the
345                          * username.  The %p (NIS server path) code is
346                          * here as it is used instead of the default
347                          * "path =" string in [homes] and so needs the
348                          * service name, not the username.  */
349                 case 'p': 
350                         string_sub(p,"%p", automount_path(lp_servicename(snum)), l); 
351                         break;
352                 case '\0': 
353                         p++; 
354                         break; /* don't run off the end of the string */
355                         
356                 default: p+=2; 
357                         break;
358                 }
359         }
360
361         standard_sub_basic(smb_name, str, len);
362 }
363
364 /****************************************************************************
365  Do some standard substitutions in a string.
366  This function will return an allocated string that have to be freed.
367 ****************************************************************************/
368
369 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name, const char *str)
370 {
371         char *a, *t;
372         a = alloc_sub_basic(smb_name, str);
373         if (!a) return NULL;
374         t = talloc_strdup(mem_ctx, a);
375         SAFE_FREE(a);
376         return t;
377 }
378
379 char *alloc_sub_basic(const char *smb_name, const char *str)
380 {
381         char *b, *p, *s, *t, *r, *a_string;
382         fstring pidstr;
383         struct passwd *pass;
384
385         a_string = strdup(str);
386         if (a_string == NULL) {
387                 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
388                 return NULL;
389         }
390         
391         for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
392
393                 r = NULL;
394                 b = t = a_string;
395                 
396                 switch (*(p+1)) {
397                 case 'U' : 
398                         r = strdup_lower(smb_name);
399                         if (r == NULL) goto error;
400                         t = realloc_string_sub(t, "%U", r);
401                         break;
402                 case 'G' :
403                         r = strdup(smb_name);
404                         if (r == NULL) goto error;
405                         if ((pass = Get_Pwnam(r))!=NULL) {
406                                 t = realloc_string_sub(t, "%G", gidtoname(pass->pw_gid));
407                         } 
408                         break;
409                 case 'D' :
410                         r = strdup_upper(current_user_info.domain);
411                         if (r == NULL) goto error;
412                         t = realloc_string_sub(t, "%D", r);
413                         break;
414                 case 'I' :
415                         t = realloc_string_sub(t, "%I", client_addr());
416                         break;
417                 case 'L' : 
418                         if (*local_machine)
419                                 t = realloc_string_sub(t, "%L", local_machine); 
420                         else
421                                 t = realloc_string_sub(t, "%L", global_myname); 
422                         break;
423                 case 'M' :
424                         t = realloc_string_sub(t, "%M", client_name());
425                         break;
426                 case 'R' :
427                         t = realloc_string_sub(t, "%R", remote_proto);
428                         break;
429                 case 'T' :
430                         t = realloc_string_sub(t, "%T", timestring(False));
431                         break;
432                 case 'a' :
433                         t = realloc_string_sub(t, "%a", remote_arch);
434                         break;
435                 case 'd' :
436                         slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
437                         t = realloc_string_sub(t, "%d", pidstr);
438                         break;
439                 case 'h' :
440                         t = realloc_string_sub(t, "%h", myhostname());
441                         break;
442                 case 'm' :
443                         t = realloc_string_sub(t, "%m", remote_machine);
444                         break;
445                 case 'v' :
446                         t = realloc_string_sub(t, "%v", VERSION);
447                         break;
448                 case '$' :
449                         t = realloc_expand_env_var(t, p); /* Expand environment variables */
450                         break;
451                         
452                 default: 
453                         break;
454                 }
455
456                 p++;
457                 SAFE_FREE(r);
458                 if (t == NULL) goto error;
459                 a_string = t;
460         }
461
462         return a_string;
463 error:
464         SAFE_FREE(a_string);
465         return NULL;
466 }
467
468 /****************************************************************************
469  Do some specific substitutions in a string.
470  This function will return an allocated string that have to be freed.
471 ****************************************************************************/
472
473 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
474                         const char *input_string,
475                         const char *username,
476                         const char *domain,
477                         uid_t uid,
478                         gid_t gid)
479 {
480         char *a, *t;
481         a = alloc_sub_specified(input_string, username, domain, uid, gid);
482         if (!a) return NULL;
483         t = talloc_strdup(mem_ctx, a);
484         SAFE_FREE(a);
485         return t;
486 }
487
488 char *alloc_sub_specified(const char *input_string,
489                         const char *username,
490                         const char *domain,
491                         uid_t uid,
492                         gid_t gid)
493 {
494         char *a_string, *ret_string;
495         char *b, *p, *s, *t;
496
497         a_string = strdup(input_string);
498         if (a_string == NULL) {
499                 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
500                 return NULL;
501         }
502         
503         for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
504                 
505                 b = t = a_string;
506                 
507                 switch (*(p+1)) {
508                 case 'U' : 
509                         t = realloc_string_sub(t, "%U", username);
510                         break;
511                 case 'u' : 
512                         t = realloc_string_sub(t, "%u", username);
513                         break;
514                 case 'G' :
515                         if (gid != -1) {
516                                 t = realloc_string_sub(t, "%G", gidtoname(gid));
517                         } else {
518                                 t = realloc_string_sub(t, "%G", "NO_GROUP");
519                         }
520                         break;
521                 case 'g' :
522                         if (gid != -1) {
523                                 t = realloc_string_sub(t, "%g", gidtoname(gid));
524                         } else {
525                                 t = realloc_string_sub(t, "%g", "NO_GROUP");
526                         }
527                         break;
528                 case 'D' :
529                         t = realloc_string_sub(t, "%D", domain);
530                         break;
531                 case 'N' : 
532                         t = realloc_string_sub(t, "%N", automount_server(username)); 
533                         break;
534                 default: 
535                         break;
536                 }
537
538                 p++;
539                 if (t == NULL) {
540                         SAFE_FREE(a_string);
541                         return NULL;
542                 }
543                 a_string = t;
544         }
545
546         ret_string = alloc_sub_basic(username, a_string);
547         SAFE_FREE(a_string);
548         return ret_string;
549 }
550
551 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
552                         int snum,
553                         const char *user,
554                         const char *connectpath,
555                         gid_t gid,
556                         const char *smb_name,
557                         char *str)
558 {
559         char *a, *t;
560         a = alloc_sub_advanced(snum, user, connectpath, gid, smb_name, str);
561         if (!a) return NULL;
562         t = talloc_strdup(mem_ctx, a);
563         SAFE_FREE(a);
564         return t;
565 }
566
567 char *alloc_sub_advanced(int snum, const char *user, 
568                                   const char *connectpath, gid_t gid, 
569                                   const char *smb_name, char *str)
570 {
571         char *a_string, *ret_string;
572         char *b, *p, *s, *t, *h;
573
574         a_string = strdup(str);
575         if (a_string == NULL) {
576                 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
577                 return NULL;
578         }
579         
580         for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
581                 
582                 b = t = a_string;
583                 
584                 switch (*(p+1)) {
585                 case 'N' :
586                         t = realloc_string_sub(t, "%N", automount_server(user));
587                         break;
588                 case 'H':
589                         if ((h = get_user_home_dir(user)))
590                                 t = realloc_string_sub(t, "%H", h);
591                         break;
592                 case 'P': 
593                         t = realloc_string_sub(t, "%P", connectpath); 
594                         break;
595                 case 'S': 
596                         t = realloc_string_sub(t, "%S", lp_servicename(snum)); 
597                         break;
598                 case 'g': 
599                         t = realloc_string_sub(t, "%g", gidtoname(gid)); 
600                         break;
601                 case 'u': 
602                         t = realloc_string_sub(t, "%u", user); 
603                         break;
604                         
605                         /* Patch from jkf@soton.ac.uk Left the %N (NIS
606                          * server name) in standard_sub_basic as it is
607                          * a feature for logon servers, hence uses the
608                          * username.  The %p (NIS server path) code is
609                          * here as it is used instead of the default
610                          * "path =" string in [homes] and so needs the
611                          * service name, not the username.  */
612                 case 'p': 
613                         t = realloc_string_sub(t, "%p", automount_path(lp_servicename(snum))); 
614                         break;
615                         
616                 default: 
617                         break;
618                 }
619
620                 p++;
621                 if (t == NULL) {
622                         SAFE_FREE(a_string);
623                         return NULL;
624                 }
625                 a_string = t;
626         }
627
628         ret_string = alloc_sub_basic(smb_name, a_string);
629         SAFE_FREE(a_string);
630         return ret_string;
631 }
632
633 /****************************************************************************
634  Do some standard substitutions in a string.
635 ****************************************************************************/
636
637 void standard_sub_conn(connection_struct *conn, char *str, size_t len)
638 {
639         standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
640                         conn->gid, current_user_info.smb_name, str, len);
641 }
642
643 /****************************************************************************
644  Like standard_sub but by snum.
645 ****************************************************************************/
646
647 void standard_sub_snum(int snum, char *str, size_t len)
648 {
649         extern struct current_user current_user;
650         static uid_t cached_uid = -1;
651         static fstring cached_user;
652         /* calling uidtoname() on every substitute would be too expensive, so
653            we cache the result here as nearly every call is for the same uid */
654
655         if (cached_uid != current_user.uid) {
656                 fstrcpy(cached_user, uidtoname(current_user.uid));
657                 cached_uid = current_user.uid;
658         }
659
660         standard_sub_advanced(snum, cached_user, "", -1,
661                 current_user_info.smb_name, str, len);
662 }