s3:smbd: make typedef write_cache private to fileio.c
[Samba/gebeck_regimport.git] / source3 / lib / substitute.c
blob44582860a19e73d7d31dd3ef694d4158a5adf8a2
1 /*
2 Unix SMB/CIFS implementation.
3 string substitution functions
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Gerald Carter 2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/passwd.h"
24 #include "secrets.h"
25 #include "auth.h"
27 userdom_struct current_user_info;
28 fstring remote_proto="UNKNOWN";
30 /**
31 * Set the 'local' machine name
32 * @param local_name the name we are being called
33 * @param if this is the 'final' name for us, not be be changed again
36 static char *local_machine;
38 void free_local_machine_name(void)
40 TALLOC_FREE(local_machine);
43 bool set_local_machine_name(const char *local_name, bool perm)
45 static bool already_perm = false;
46 char *tmp_local_machine = NULL;
47 size_t len;
49 if (already_perm) {
50 return true;
53 tmp_local_machine = talloc_strdup(NULL, local_name);
54 if (!tmp_local_machine) {
55 return false;
57 trim_char(tmp_local_machine,' ',' ');
59 TALLOC_FREE(local_machine);
60 len = strlen(tmp_local_machine);
61 local_machine = (char *)TALLOC_ZERO(NULL, len+1);
62 if (!local_machine) {
63 TALLOC_FREE(tmp_local_machine);
64 return false;
66 /* alpha_strcpy includes the space for the terminating nul. */
67 alpha_strcpy(local_machine,tmp_local_machine,
68 SAFE_NETBIOS_CHARS,len+1);
69 strlower_m(local_machine);
70 TALLOC_FREE(tmp_local_machine);
72 already_perm = perm;
74 return true;
77 const char *get_local_machine_name(void)
79 if (!local_machine || !*local_machine) {
80 return lp_netbios_name();
83 return local_machine;
86 /**
87 * Set the 'remote' machine name
88 * @param remote_name the name our client wants to be called by
89 * @param if this is the 'final' name for them, not be be changed again
92 static char *remote_machine;
94 bool set_remote_machine_name(const char *remote_name, bool perm)
96 static bool already_perm = False;
97 char *tmp_remote_machine;
98 size_t len;
100 if (already_perm) {
101 return true;
104 tmp_remote_machine = talloc_strdup(NULL, remote_name);
105 if (!tmp_remote_machine) {
106 return false;
108 trim_char(tmp_remote_machine,' ',' ');
110 TALLOC_FREE(remote_machine);
111 len = strlen(tmp_remote_machine);
112 remote_machine = (char *)TALLOC_ZERO(NULL, len+1);
113 if (!remote_machine) {
114 TALLOC_FREE(tmp_remote_machine);
115 return false;
118 /* alpha_strcpy includes the space for the terminating nul. */
119 alpha_strcpy(remote_machine,tmp_remote_machine,
120 SAFE_NETBIOS_CHARS,len+1);
121 strlower_m(remote_machine);
122 TALLOC_FREE(tmp_remote_machine);
124 already_perm = perm;
126 return true;
129 const char *get_remote_machine_name(void)
131 return remote_machine ? remote_machine : "";
134 /*******************************************************************
135 Setup the string used by %U substitution.
136 ********************************************************************/
138 static char *smb_user_name;
140 void sub_set_smb_name(const char *name)
142 char *tmp;
143 size_t len;
144 bool is_machine_account = false;
146 /* don't let anonymous logins override the name */
147 if (!name || !*name) {
148 return;
151 tmp = talloc_strdup(NULL, name);
152 if (!tmp) {
153 return;
155 trim_char(tmp, ' ', ' ');
156 strlower_m(tmp);
158 len = strlen(tmp);
160 if (len == 0) {
161 TALLOC_FREE(tmp);
162 return;
165 /* long story but here goes....we have to allow usernames
166 ending in '$' as they are valid machine account names.
167 So check for a machine account and re-add the '$'
168 at the end after the call to alpha_strcpy(). --jerry */
170 if (tmp[len-1] == '$') {
171 is_machine_account = True;
174 TALLOC_FREE(smb_user_name);
175 smb_user_name = (char *)TALLOC_ZERO(NULL, len+1);
176 if (!smb_user_name) {
177 TALLOC_FREE(tmp);
178 return;
181 /* alpha_strcpy includes the space for the terminating nul. */
182 alpha_strcpy(smb_user_name, tmp,
183 SAFE_NETBIOS_CHARS,
184 len+1);
186 TALLOC_FREE(tmp);
188 if (is_machine_account) {
189 len = strlen(smb_user_name);
190 smb_user_name[len-1] = '$';
194 static char sub_peeraddr[INET6_ADDRSTRLEN];
195 static const char *sub_peername = NULL;
196 static char sub_sockaddr[INET6_ADDRSTRLEN];
198 void sub_set_socket_ids(const char *peeraddr, const char *peername,
199 const char *sockaddr)
201 const char *addr = peeraddr;
203 if (strnequal(addr, "::ffff:", 7)) {
204 addr += 7;
206 strlcpy(sub_peeraddr, addr, sizeof(sub_peeraddr));
208 if (sub_peername != NULL &&
209 sub_peername != sub_peeraddr) {
210 talloc_free(discard_const_p(char,sub_peername));
211 sub_peername = NULL;
213 sub_peername = talloc_strdup(NULL, peername);
214 if (sub_peername == NULL) {
215 sub_peername = sub_peeraddr;
219 * Shouldn't we do the ::ffff: cancellation here as well? The
220 * original code in talloc_sub_basic() did not do it, so I'm
221 * leaving it out here as well for compatibility.
223 strlcpy(sub_sockaddr, sockaddr, sizeof(sub_sockaddr));
226 static const char *get_smb_user_name(void)
228 return smb_user_name ? smb_user_name : "";
231 /*******************************************************************
232 Setup the strings used by substitutions. Called per packet. Ensure
233 %U name is set correctly also.
235 smb_name must be sanitized by alpha_strcpy
236 ********************************************************************/
238 void set_current_user_info(const char *smb_name, const char *unix_name,
239 const char *domain)
241 fstrcpy(current_user_info.smb_name, smb_name);
242 fstrcpy(current_user_info.unix_name, unix_name);
243 fstrcpy(current_user_info.domain, domain);
245 /* The following is safe as current_user_info.smb_name
246 * has already been sanitised in register_existing_vuid. */
248 sub_set_smb_name(current_user_info.smb_name);
251 /*******************************************************************
252 Return the current active user name.
253 *******************************************************************/
255 const char *get_current_username(void)
257 if (current_user_info.smb_name[0] == '\0' ) {
258 return get_smb_user_name();
261 return current_user_info.smb_name;
264 /*******************************************************************
265 Given a pointer to a %$(NAME) in p and the whole string in str
266 expand it as an environment variable.
267 str must be a talloced string.
268 Return a new allocated and expanded string.
269 Based on code by Branko Cibej <branko.cibej@hermes.si>
270 When this is called p points at the '%' character.
271 May substitute multiple occurrencies of the same env var.
272 ********************************************************************/
274 static char *realloc_expand_env_var(char *str, char *p)
276 char *envname;
277 char *envval;
278 char *q, *r;
279 int copylen;
281 if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
282 return str;
286 * Look for the terminating ')'.
289 if ((q = strchr_m(p,')')) == NULL) {
290 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
291 return str;
295 * Extract the name from within the %$(NAME) string.
298 r = p + 3;
299 copylen = q - r;
301 /* reserve space for use later add %$() chars */
302 if ( (envname = talloc_array(talloc_tos(), char, copylen + 1 + 4)) == NULL ) {
303 return NULL;
306 strncpy(envname,r,copylen);
307 envname[copylen] = '\0';
309 if ((envval = getenv(envname)) == NULL) {
310 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
311 TALLOC_FREE(envname);
312 return str;
316 * Copy the full %$(NAME) into envname so it
317 * can be replaced.
320 copylen = q + 1 - p;
321 strncpy(envname,p,copylen);
322 envname[copylen] = '\0';
323 r = realloc_string_sub(str, envname, envval);
324 TALLOC_FREE(envname);
326 return r;
329 /*******************************************************************
330 Patch from jkf@soton.ac.uk
331 Added this to implement %p (NIS auto-map version of %H)
332 *******************************************************************/
334 static const char *automount_path(const char *user_name)
336 TALLOC_CTX *ctx = talloc_tos();
337 const char *server_path;
339 /* use the passwd entry as the default */
340 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
342 server_path = talloc_strdup(ctx, get_user_home_dir(ctx, user_name));
343 if (!server_path) {
344 return "";
347 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
349 if (lp_nis_home_map()) {
350 const char *home_path_start;
351 char *automount_value = automount_lookup(ctx, user_name);
353 if(automount_value && strlen(automount_value) > 0) {
354 home_path_start = strchr_m(automount_value,':');
355 if (home_path_start != NULL) {
356 DEBUG(5, ("NIS lookup succeeded. "
357 "Home path is: %s\n",
358 home_path_start ?
359 (home_path_start+1):""));
360 server_path = talloc_strdup(ctx,
361 home_path_start+1);
362 if (!server_path) {
363 server_path = "";
366 } else {
367 /* NIS key lookup failed: default to
368 * user home directory from password file */
369 DEBUG(5, ("NIS lookup failed. Using Home path from "
370 "passwd file. Home path is: %s\n", server_path ));
373 #endif
375 DEBUG(4,("Home server path: %s\n", server_path));
376 return server_path;
379 /*******************************************************************
380 Patch from jkf@soton.ac.uk
381 This is Luke's original function with the NIS lookup code
382 moved out to a separate function.
383 *******************************************************************/
385 static const char *automount_server(const char *user_name)
387 TALLOC_CTX *ctx = talloc_tos();
388 const char *server_name;
389 const char *local_machine_name = get_local_machine_name();
391 /* use the local machine name as the default */
392 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
393 if (local_machine_name && *local_machine_name) {
394 server_name = talloc_strdup(ctx, local_machine_name);
395 } else {
396 server_name = talloc_strdup(ctx, lp_netbios_name());
399 if (!server_name) {
400 return "";
403 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
404 if (lp_nis_home_map()) {
405 char *p;
406 char *srv;
407 char *automount_value = automount_lookup(ctx, user_name);
408 if (!automount_value) {
409 return "";
411 srv = talloc_strdup(ctx, automount_value);
412 if (!srv) {
413 return "";
415 p = strchr_m(srv, ':');
416 if (!p) {
417 return "";
419 *p = '\0';
420 server_name = srv;
421 DEBUG(5, ("NIS lookup succeeded. Home server %s\n",
422 server_name));
424 #endif
426 DEBUG(4,("Home server: %s\n", server_name));
427 return server_name;
430 /****************************************************************************
431 Do some standard substitutions in a string.
432 len is the length in bytes of the space allowed in string str. If zero means
433 don't allow expansions.
434 ****************************************************************************/
436 void standard_sub_basic(const char *smb_name, const char *domain_name,
437 char *str, size_t len)
439 char *s;
441 if ( (s = talloc_sub_basic(talloc_tos(), smb_name, domain_name, str )) != NULL ) {
442 strncpy( str, s, len );
445 TALLOC_FREE( s );
448 /****************************************************************************
449 Do some standard substitutions in a string.
450 This function will return an talloced string that has to be freed.
451 ****************************************************************************/
453 char *talloc_sub_basic(TALLOC_CTX *mem_ctx,
454 const char *smb_name,
455 const char *domain_name,
456 const char *str)
458 char *b, *p, *s, *r, *a_string;
459 fstring pidstr, vnnstr;
460 const char *local_machine_name = get_local_machine_name();
461 TALLOC_CTX *tmp_ctx = NULL;
463 /* workaround to prevent a crash while looking at bug #687 */
465 if (!str) {
466 DEBUG(0,("talloc_sub_basic: NULL source string! This should not happen\n"));
467 return NULL;
470 a_string = talloc_strdup(mem_ctx, str);
471 if (a_string == NULL) {
472 DEBUG(0, ("talloc_sub_basic: Out of memory!\n"));
473 return NULL;
476 tmp_ctx = talloc_stackframe();
478 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
480 r = NULL;
481 b = a_string;
483 switch (*(p+1)) {
484 case 'U' :
485 r = strlower_talloc(tmp_ctx, smb_name);
486 if (r == NULL) {
487 goto error;
489 a_string = realloc_string_sub(a_string, "%U", r);
490 break;
491 case 'G' : {
492 struct passwd *pass;
493 r = talloc_strdup(tmp_ctx, smb_name);
494 if (r == NULL) {
495 goto error;
497 pass = Get_Pwnam_alloc(tmp_ctx, r);
498 if (pass != NULL) {
499 a_string = realloc_string_sub(
500 a_string, "%G",
501 gidtoname(pass->pw_gid));
503 TALLOC_FREE(pass);
504 break;
506 case 'D' :
507 r = strupper_talloc(tmp_ctx, domain_name);
508 if (r == NULL) {
509 goto error;
511 a_string = realloc_string_sub(a_string, "%D", r);
512 break;
513 case 'I' : {
514 a_string = realloc_string_sub(
515 a_string, "%I",
516 sub_peeraddr[0] ? sub_peeraddr : "0.0.0.0");
517 break;
519 case 'i':
520 a_string = realloc_string_sub(
521 a_string, "%i",
522 sub_sockaddr[0] ? sub_sockaddr : "0.0.0.0");
523 break;
524 case 'L' :
525 if ( strncasecmp_m(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
526 break;
528 if (local_machine_name && *local_machine_name) {
529 a_string = realloc_string_sub(a_string, "%L", local_machine_name);
530 } else {
531 a_string = realloc_string_sub(a_string, "%L", lp_netbios_name());
533 break;
534 case 'N':
535 a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
536 break;
537 case 'M' :
538 a_string = realloc_string_sub(a_string, "%M",
539 sub_peername ? sub_peername : "");
540 break;
541 case 'R' :
542 a_string = realloc_string_sub(a_string, "%R", remote_proto);
543 break;
544 case 'T' :
545 a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False));
546 break;
547 case 'a' :
548 a_string = realloc_string_sub(a_string, "%a",
549 get_remote_arch_str());
550 break;
551 case 'd' :
552 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)getpid());
553 a_string = realloc_string_sub(a_string, "%d", pidstr);
554 break;
555 case 'h' :
556 a_string = realloc_string_sub(a_string, "%h", myhostname());
557 break;
558 case 'm' :
559 a_string = realloc_string_sub(a_string, "%m",
560 remote_machine
561 ? remote_machine
562 : "");
563 break;
564 case 'v' :
565 a_string = realloc_string_sub(a_string, "%v", samba_version_string());
566 break;
567 case 'w' :
568 a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
569 break;
570 case '$' :
571 a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
572 break;
573 case 'V' :
574 slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
575 a_string = realloc_string_sub(a_string, "%V", vnnstr);
576 break;
577 default:
578 break;
581 p++;
582 TALLOC_FREE(r);
584 if (a_string == NULL) {
585 goto done;
589 goto done;
591 error:
592 TALLOC_FREE(a_string);
594 done:
595 TALLOC_FREE(tmp_ctx);
596 return a_string;
599 /****************************************************************************
600 Do some specific substitutions in a string.
601 This function will return an allocated string that have to be freed.
602 ****************************************************************************/
604 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
605 const char *input_string,
606 const char *username,
607 const char *domain,
608 uid_t uid,
609 gid_t gid)
611 char *a_string;
612 char *ret_string = NULL;
613 char *b, *p, *s;
614 TALLOC_CTX *tmp_ctx;
616 if (!(tmp_ctx = talloc_new(mem_ctx))) {
617 DEBUG(0, ("talloc_new failed\n"));
618 return NULL;
621 a_string = talloc_strdup(tmp_ctx, input_string);
622 if (a_string == NULL) {
623 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
624 goto done;
627 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
629 b = a_string;
631 switch (*(p+1)) {
632 case 'U' :
633 a_string = talloc_string_sub(
634 tmp_ctx, a_string, "%U", username);
635 break;
636 case 'u' :
637 a_string = talloc_string_sub(
638 tmp_ctx, a_string, "%u", username);
639 break;
640 case 'G' :
641 if (gid != -1) {
642 a_string = talloc_string_sub(
643 tmp_ctx, a_string, "%G",
644 gidtoname(gid));
645 } else {
646 a_string = talloc_string_sub(
647 tmp_ctx, a_string,
648 "%G", "NO_GROUP");
650 break;
651 case 'g' :
652 if (gid != -1) {
653 a_string = talloc_string_sub(
654 tmp_ctx, a_string, "%g",
655 gidtoname(gid));
656 } else {
657 a_string = talloc_string_sub(
658 tmp_ctx, a_string, "%g", "NO_GROUP");
660 break;
661 case 'D' :
662 a_string = talloc_string_sub(tmp_ctx, a_string,
663 "%D", domain);
664 break;
665 case 'N' :
666 a_string = talloc_string_sub(
667 tmp_ctx, a_string, "%N",
668 automount_server(username));
669 break;
670 default:
671 break;
674 p++;
675 if (a_string == NULL) {
676 goto done;
680 /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
681 * away with the TALLOC_FREE(tmp_ctx) further down. */
683 ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
685 done:
686 TALLOC_FREE(tmp_ctx);
687 return ret_string;
690 /****************************************************************************
691 ****************************************************************************/
693 char *talloc_sub_advanced(TALLOC_CTX *ctx,
694 const char *servicename,
695 const char *user,
696 const char *connectpath,
697 gid_t gid,
698 const char *smb_name,
699 const char *domain_name,
700 const char *str)
702 char *a_string, *ret_string;
703 char *b, *p, *s;
705 a_string = talloc_strdup(talloc_tos(), str);
706 if (a_string == NULL) {
707 DEBUG(0, ("talloc_sub_advanced: Out of memory!\n"));
708 return NULL;
711 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
713 b = a_string;
715 switch (*(p+1)) {
716 case 'N' :
717 a_string = realloc_string_sub(a_string, "%N", automount_server(user));
718 break;
719 case 'H': {
720 char *h;
721 if ((h = get_user_home_dir(talloc_tos(), user)))
722 a_string = realloc_string_sub(a_string, "%H", h);
723 TALLOC_FREE(h);
724 break;
726 case 'P':
727 a_string = realloc_string_sub(a_string, "%P", connectpath);
728 break;
729 case 'S':
730 a_string = realloc_string_sub(a_string, "%S", servicename);
731 break;
732 case 'g':
733 a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
734 break;
735 case 'u':
736 a_string = realloc_string_sub(a_string, "%u", user);
737 break;
739 /* Patch from jkf@soton.ac.uk Left the %N (NIS
740 * server name) in standard_sub_basic as it is
741 * a feature for logon servers, hence uses the
742 * username. The %p (NIS server path) code is
743 * here as it is used instead of the default
744 * "path =" string in [homes] and so needs the
745 * service name, not the username. */
746 case 'p':
747 a_string = realloc_string_sub(a_string, "%p",
748 automount_path(servicename));
749 break;
751 default:
752 break;
755 p++;
756 if (a_string == NULL) {
757 return NULL;
761 ret_string = talloc_sub_basic(ctx, smb_name, domain_name, a_string);
762 TALLOC_FREE(a_string);
763 return ret_string;
766 void standard_sub_advanced(const char *servicename, const char *user,
767 const char *connectpath, gid_t gid,
768 const char *smb_name, const char *domain_name,
769 char *str, size_t len)
771 char *s = talloc_sub_advanced(talloc_tos(),
772 servicename, user, connectpath,
773 gid, smb_name, domain_name, str);
775 if (!s) {
776 return;
778 strlcpy( str, s, len );
779 TALLOC_FREE( s );
782 /******************************************************************************
783 version of standard_sub_basic() for string lists; uses talloc_sub_basic()
784 for the work
785 *****************************************************************************/
787 bool str_list_sub_basic( char **list, const char *smb_name,
788 const char *domain_name )
790 TALLOC_CTX *ctx = list;
791 char *s, *tmpstr;
793 while ( *list ) {
794 s = *list;
795 tmpstr = talloc_sub_basic(ctx, smb_name, domain_name, s);
796 if ( !tmpstr ) {
797 DEBUG(0,("str_list_sub_basic: "
798 "talloc_sub_basic() return NULL!\n"));
799 return false;
802 TALLOC_FREE(*list);
803 *list = tmpstr;
805 list++;
808 return true;