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