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