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