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