sync'ing up for 3.0alpha20 release
[Samba/gbeck.git] / source3 / lib / substitute.c
blob2550d00d14c7318015433147a968d6862bb5afcb
1 /*
2 Unix SMB/CIFS implementation.
3 string substitution functions
4 Copyright (C) Andrew Tridgell 1992-2000
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.
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.
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.
22 #include "includes.h"
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;
30 static fstring remote_machine="";
33 void set_local_machine_name(const char* local_name)
35 fstring tmp_local_machine;
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);
43 void set_remote_machine_name(const char* remote_name)
45 fstring tmp_remote_machine;
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);
53 const char* get_remote_machine_name(void)
55 return remote_machine;
58 const char* get_local_machine_name(void)
60 return local_machine;
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 ********************************************************************/
70 static size_t expand_env_var(char *p, int len)
72 fstring envname;
73 char *envval;
74 char *q, *r;
75 int copylen;
77 if (p[1] != '$')
78 return 1;
80 if (p[2] != '(')
81 return 2;
84 * Look for the terminating ')'.
87 if ((q = strchr_m(p,')')) == NULL) {
88 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
89 return 2;
93 * Extract the name from within the %$(NAME) string.
96 r = p+3;
97 copylen = MIN((q-r),(sizeof(envname)-1));
98 strncpy(envname,r,copylen);
99 envname[copylen] = '\0';
101 if ((envval = getenv(envname)) == NULL) {
102 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
103 return 2;
107 * Copy the full %$(NAME) into envname so it
108 * can be replaced.
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. */
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 ********************************************************************/
128 static char * realloc_expand_env_var(char *str, char *p)
130 char *envname;
131 char *envval;
132 char *q, *r;
133 int copylen;
135 if (p[0] != '%' || p[1] != '$' || p[2] != '(')
136 return str;
139 * Look for the terminating ')'.
142 if ((q = strchr_m(p,')')) == NULL) {
143 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
144 return str;
148 * Extract the name from within the %$(NAME) string.
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';
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;
165 * Copy the full %$(NAME) into envname so it
166 * can be replaced.
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;
178 /*******************************************************************
179 Patch from jkf@soton.ac.uk
180 Added this to implement %p (NIS auto-map version of %H)
181 *******************************************************************/
183 static char *automount_path(const char *user_name)
185 static pstring server_path;
187 /* use the passwd entry as the default */
188 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
190 pstrcpy(server_path, get_user_home_dir(user_name));
192 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
194 if (lp_nis_home_map()) {
195 char *home_path_start;
196 char *automount_value = automount_lookup(user_name);
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);
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 ));
210 #endif
212 DEBUG(4,("Home server path: %s\n", server_path));
214 return server_path;
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 *******************************************************************/
223 static const char *automount_server(const char *user_name)
225 static pstring server_name;
226 const char *local_machine_name = get_local_machine_name();
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);
235 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
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';
247 #endif
249 DEBUG(4,("Home server: %s\n", server_name));
251 return server_name;
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 ****************************************************************************/
260 void standard_sub_basic(const char *smb_name, char *str,size_t len)
262 char *p, *s;
263 fstring pidstr;
264 struct passwd *pass;
265 const char *local_machine_name = get_local_machine_name();
267 for (s=str; (p=strchr_m(s, '%'));s=p) {
268 fstring tmp_str;
270 int l = (int)len - (int)(p-str);
272 if (l < 0)
273 l = 0;
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;
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 pstring temp_name;
303 pstrcpy(temp_name, global_myname);
304 strlower(temp_name);
305 string_sub(p,"%L", temp_name,l);
307 break;
308 case 'M' :
309 string_sub(p,"%M", client_name(),l);
310 break;
311 case 'R' :
312 string_sub(p,"%R", remote_proto,l);
313 break;
314 case 'T' :
315 string_sub(p,"%T", timestring(False),l);
316 break;
317 case 'a' :
318 string_sub(p,"%a", remote_arch,l);
319 break;
320 case 'd' :
321 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
322 string_sub(p,"%d", pidstr,l);
323 break;
324 case 'h' :
325 string_sub(p,"%h", myhostname(),l);
326 break;
327 case 'm' :
328 string_sub(p,"%m", get_remote_machine_name(),l);
329 break;
330 case 'v' :
331 string_sub(p,"%v", VERSION,l);
332 break;
333 case '$' :
334 p += expand_env_var(p,l);
335 break; /* Expand environment variables */
336 case '\0':
337 p++;
338 break; /* don't run off the end of the string */
340 default: p+=2;
341 break;
346 static void standard_sub_advanced(int snum, const char *user,
347 const char *connectpath, gid_t gid,
348 const char *smb_name, char *str, size_t len)
350 char *p, *s, *home;
352 for (s=str; (p=strchr_m(s, '%'));s=p) {
353 int l = (int)len - (int)(p-str);
355 if (l < 0)
356 l = 0;
358 switch (*(p+1)) {
359 case 'N' :
360 string_sub(p,"%N", automount_server(user),l);
361 break;
362 case 'H':
363 if ((home = get_user_home_dir(user)))
364 string_sub(p,"%H",home, l);
365 else
366 p += 2;
367 break;
368 case 'P':
369 string_sub(p,"%P", connectpath, l);
370 break;
371 case 'S':
372 string_sub(p,"%S", lp_servicename(snum), l);
373 break;
374 case 'g':
375 string_sub(p,"%g", gidtoname(gid), l);
376 break;
377 case 'u':
378 string_sub(p,"%u", user, l);
379 break;
381 /* Patch from jkf@soton.ac.uk Left the %N (NIS
382 * server name) in standard_sub_basic as it is
383 * a feature for logon servers, hence uses the
384 * username. The %p (NIS server path) code is
385 * here as it is used instead of the default
386 * "path =" string in [homes] and so needs the
387 * service name, not the username. */
388 case 'p':
389 string_sub(p,"%p", automount_path(lp_servicename(snum)), l);
390 break;
391 case '\0':
392 p++;
393 break; /* don't run off the end of the string */
395 default: p+=2;
396 break;
400 standard_sub_basic(smb_name, str, len);
403 /****************************************************************************
404 Do some standard substitutions in a string.
405 This function will return an allocated string that have to be freed.
406 ****************************************************************************/
408 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name, const char *str)
410 char *a, *t;
411 a = alloc_sub_basic(smb_name, str);
412 if (!a) return NULL;
413 t = talloc_strdup(mem_ctx, a);
414 SAFE_FREE(a);
415 return t;
418 char *alloc_sub_basic(const char *smb_name, const char *str)
420 char *b, *p, *s, *t, *r, *a_string;
421 fstring pidstr;
422 struct passwd *pass;
423 const char *local_machine_name = get_local_machine_name();
425 a_string = strdup(str);
426 if (a_string == NULL) {
427 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
428 return NULL;
431 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
433 r = NULL;
434 b = t = a_string;
436 switch (*(p+1)) {
437 case 'U' :
438 r = strdup_lower(smb_name);
439 if (r == NULL) goto error;
440 t = realloc_string_sub(t, "%U", r);
441 break;
442 case 'G' :
443 r = strdup(smb_name);
444 if (r == NULL) goto error;
445 if ((pass = Get_Pwnam(r))!=NULL) {
446 t = realloc_string_sub(t, "%G", gidtoname(pass->pw_gid));
448 break;
449 case 'D' :
450 r = strdup_upper(current_user_info.domain);
451 if (r == NULL) goto error;
452 t = realloc_string_sub(t, "%D", r);
453 break;
454 case 'I' :
455 t = realloc_string_sub(t, "%I", client_addr());
456 break;
457 case 'L' :
458 if (local_machine_name && *local_machine_name)
459 t = realloc_string_sub(t, "%L", local_machine_name);
460 else
461 t = realloc_string_sub(t, "%L", global_myname);
462 break;
463 case 'M' :
464 t = realloc_string_sub(t, "%M", client_name());
465 break;
466 case 'R' :
467 t = realloc_string_sub(t, "%R", remote_proto);
468 break;
469 case 'T' :
470 t = realloc_string_sub(t, "%T", timestring(False));
471 break;
472 case 'a' :
473 t = realloc_string_sub(t, "%a", remote_arch);
474 break;
475 case 'd' :
476 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
477 t = realloc_string_sub(t, "%d", pidstr);
478 break;
479 case 'h' :
480 t = realloc_string_sub(t, "%h", myhostname());
481 break;
482 case 'm' :
483 t = realloc_string_sub(t, "%m", remote_machine);
484 break;
485 case 'v' :
486 t = realloc_string_sub(t, "%v", VERSION);
487 break;
488 case '$' :
489 t = realloc_expand_env_var(t, p); /* Expand environment variables */
490 break;
492 default:
493 break;
496 p++;
497 SAFE_FREE(r);
498 if (t == NULL) goto error;
499 a_string = t;
502 return a_string;
503 error:
504 SAFE_FREE(a_string);
505 return NULL;
508 /****************************************************************************
509 Do some specific substitutions in a string.
510 This function will return an allocated string that have to be freed.
511 ****************************************************************************/
513 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
514 const char *input_string,
515 const char *username,
516 const char *domain,
517 uid_t uid,
518 gid_t gid)
520 char *a, *t;
521 a = alloc_sub_specified(input_string, username, domain, uid, gid);
522 if (!a) return NULL;
523 t = talloc_strdup(mem_ctx, a);
524 SAFE_FREE(a);
525 return t;
528 char *alloc_sub_specified(const char *input_string,
529 const char *username,
530 const char *domain,
531 uid_t uid,
532 gid_t gid)
534 char *a_string, *ret_string;
535 char *b, *p, *s, *t;
537 a_string = strdup(input_string);
538 if (a_string == NULL) {
539 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
540 return NULL;
543 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
545 b = t = a_string;
547 switch (*(p+1)) {
548 case 'U' :
549 t = realloc_string_sub(t, "%U", username);
550 break;
551 case 'u' :
552 t = realloc_string_sub(t, "%u", username);
553 break;
554 case 'G' :
555 if (gid != -1) {
556 t = realloc_string_sub(t, "%G", gidtoname(gid));
557 } else {
558 t = realloc_string_sub(t, "%G", "NO_GROUP");
560 break;
561 case 'g' :
562 if (gid != -1) {
563 t = realloc_string_sub(t, "%g", gidtoname(gid));
564 } else {
565 t = realloc_string_sub(t, "%g", "NO_GROUP");
567 break;
568 case 'D' :
569 t = realloc_string_sub(t, "%D", domain);
570 break;
571 case 'N' :
572 t = realloc_string_sub(t, "%N", automount_server(username));
573 break;
574 default:
575 break;
578 p++;
579 if (t == NULL) {
580 SAFE_FREE(a_string);
581 return NULL;
583 a_string = t;
586 ret_string = alloc_sub_basic(username, a_string);
587 SAFE_FREE(a_string);
588 return ret_string;
591 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
592 int snum,
593 const char *user,
594 const char *connectpath,
595 gid_t gid,
596 const char *smb_name,
597 char *str)
599 char *a, *t;
600 a = alloc_sub_advanced(snum, user, connectpath, gid, smb_name, str);
601 if (!a) return NULL;
602 t = talloc_strdup(mem_ctx, a);
603 SAFE_FREE(a);
604 return t;
607 char *alloc_sub_advanced(int snum, const char *user,
608 const char *connectpath, gid_t gid,
609 const char *smb_name, char *str)
611 char *a_string, *ret_string;
612 char *b, *p, *s, *t, *h;
614 a_string = strdup(str);
615 if (a_string == NULL) {
616 DEBUG(0, ("alloc_sub_specified: Out of memory!\n"));
617 return NULL;
620 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
622 b = t = a_string;
624 switch (*(p+1)) {
625 case 'N' :
626 t = realloc_string_sub(t, "%N", automount_server(user));
627 break;
628 case 'H':
629 if ((h = get_user_home_dir(user)))
630 t = realloc_string_sub(t, "%H", h);
631 break;
632 case 'P':
633 t = realloc_string_sub(t, "%P", connectpath);
634 break;
635 case 'S':
636 t = realloc_string_sub(t, "%S", lp_servicename(snum));
637 break;
638 case 'g':
639 t = realloc_string_sub(t, "%g", gidtoname(gid));
640 break;
641 case 'u':
642 t = realloc_string_sub(t, "%u", user);
643 break;
645 /* Patch from jkf@soton.ac.uk Left the %N (NIS
646 * server name) in standard_sub_basic as it is
647 * a feature for logon servers, hence uses the
648 * username. The %p (NIS server path) code is
649 * here as it is used instead of the default
650 * "path =" string in [homes] and so needs the
651 * service name, not the username. */
652 case 'p':
653 t = realloc_string_sub(t, "%p", automount_path(lp_servicename(snum)));
654 break;
656 default:
657 break;
660 p++;
661 if (t == NULL) {
662 SAFE_FREE(a_string);
663 return NULL;
665 a_string = t;
668 ret_string = alloc_sub_basic(smb_name, a_string);
669 SAFE_FREE(a_string);
670 return ret_string;
673 /****************************************************************************
674 Do some standard substitutions in a string.
675 ****************************************************************************/
677 void standard_sub_conn(connection_struct *conn, char *str, size_t len)
679 standard_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
680 conn->gid, current_user_info.smb_name, str, len);
683 char *talloc_sub_conn(TALLOC_CTX *mem_ctx, connection_struct *conn, char *str)
685 return talloc_sub_advanced(mem_ctx, SNUM(conn), conn->user,
686 conn->connectpath, conn->gid,
687 current_user_info.smb_name, str);
690 char *alloc_sub_conn(connection_struct *conn, char *str)
692 return alloc_sub_advanced(SNUM(conn), conn->user, conn->connectpath,
693 conn->gid, current_user_info.smb_name, str);
696 /****************************************************************************
697 Like standard_sub but by snum.
698 ****************************************************************************/
700 void standard_sub_snum(int snum, char *str, size_t len)
702 extern struct current_user current_user;
703 static uid_t cached_uid = -1;
704 static fstring cached_user;
705 /* calling uidtoname() on every substitute would be too expensive, so
706 we cache the result here as nearly every call is for the same uid */
708 if (cached_uid != current_user.uid) {
709 fstrcpy(cached_user, uidtoname(current_user.uid));
710 cached_uid = current_user.uid;
713 standard_sub_advanced(snum, cached_user, "", -1,
714 current_user_info.smb_name, str, len);