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