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/>.
23 #include "substitute.h"
24 #include "system/passwd.h"
27 #include "lib/util/string_wrappers.h"
29 /* Max DNS name is 253 + '\0' */
30 #define MACHINE_NAME_SIZE 254
32 static char local_machine[MACHINE_NAME_SIZE];
33 static char remote_machine[MACHINE_NAME_SIZE];
35 userdom_struct current_user_info;
36 static fstring remote_proto="UNKNOWN";
38 void set_remote_proto(const char *proto)
40 fstrcpy(remote_proto, proto);
44 * Set the 'local' machine name
45 * @param local_name the name we are being called
46 * @param if this is the 'final' name for us, not be be changed again
48 bool set_local_machine_name(const char *local_name, bool perm)
50 static bool already_perm = false;
51 char tmp[MACHINE_NAME_SIZE];
57 strlcpy(tmp, local_name, sizeof(tmp));
58 trim_char(tmp, ' ', ' ');
60 alpha_strcpy(local_machine,
63 sizeof(local_machine) - 1);
64 if (!strlower_m(local_machine)) {
73 const char *get_local_machine_name(void)
75 if (local_machine[0] == '\0') {
76 return lp_netbios_name();
83 * Set the 'remote' machine name
85 * @param remote_name the name our client wants to be called by
86 * @param if this is the 'final' name for them, not be be changed again
88 bool set_remote_machine_name(const char *remote_name, bool perm)
90 static bool already_perm = False;
91 char tmp[MACHINE_NAME_SIZE];
97 strlcpy(tmp, remote_name, sizeof(tmp));
98 trim_char(tmp, ' ', ' ');
100 alpha_strcpy(remote_machine,
103 sizeof(remote_machine) - 1);
104 if (!strlower_m(remote_machine)) {
113 const char *get_remote_machine_name(void)
115 return remote_machine;
118 static char sub_peeraddr[INET6_ADDRSTRLEN];
119 static const char *sub_peername = NULL;
120 static char sub_sockaddr[INET6_ADDRSTRLEN];
122 void sub_set_socket_ids(const char *peeraddr, const char *peername,
123 const char *sockaddr)
125 const char *addr = peeraddr;
127 if (strnequal(addr, "::ffff:", 7)) {
130 strlcpy(sub_peeraddr, addr, sizeof(sub_peeraddr));
132 if (sub_peername != NULL &&
133 sub_peername != sub_peeraddr) {
134 talloc_free(discard_const_p(char,sub_peername));
137 sub_peername = talloc_strdup(NULL, peername);
138 if (sub_peername == NULL) {
139 sub_peername = sub_peeraddr;
143 * Shouldn't we do the ::ffff: cancellation here as well? The
144 * original code in talloc_sub_basic() did not do it, so I'm
145 * leaving it out here as well for compatibility.
147 strlcpy(sub_sockaddr, sockaddr, sizeof(sub_sockaddr));
150 /*******************************************************************
151 Setup the strings used by substitutions. Called per packet. Ensure
152 %U name is set correctly also.
154 smb_name must be sanitized by alpha_strcpy
155 ********************************************************************/
157 void set_current_user_info(const char *smb_name, const char *unix_name,
160 static const void *last_smb_name;
161 static const void *last_unix_name;
162 static const void *last_domain;
164 if (likely(last_smb_name == smb_name &&
165 last_unix_name == unix_name &&
166 last_domain == domain))
171 fstrcpy(current_user_info.smb_name, smb_name);
172 fstrcpy(current_user_info.unix_name, unix_name);
173 fstrcpy(current_user_info.domain, domain);
175 last_smb_name = smb_name;
176 last_unix_name = unix_name;
177 last_domain = domain;
180 /*******************************************************************
181 Return the current active user name.
182 *******************************************************************/
184 const char *get_current_username(void)
186 return current_user_info.smb_name;
189 const char *get_current_user_info_domain(void)
191 return current_user_info.domain;
194 /*******************************************************************
195 Given a pointer to a %$(NAME) in p and the whole string in str
196 expand it as an environment variable.
197 str must be a talloced string.
198 Return a new allocated and expanded string.
199 Based on code by Branko Cibej <branko.cibej@hermes.si>
200 When this is called p points at the '%' character.
201 May substitute multiple occurrencies of the same env var.
202 ********************************************************************/
204 static char *realloc_expand_env_var(char *str, char *p)
211 if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
216 * Look for the terminating ')'.
219 if ((q = strchr_m(p,')')) == NULL) {
220 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
225 * Extract the name from within the %$(NAME) string.
231 /* reserve space for use later add %$() chars */
232 if ( (envname = talloc_array(talloc_tos(), char, copylen + 1 + 4)) == NULL ) {
236 strncpy(envname,r,copylen);
237 envname[copylen] = '\0';
239 if ((envval = getenv(envname)) == NULL) {
240 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
241 TALLOC_FREE(envname);
246 * Copy the full %$(NAME) into envname so it
251 strncpy(envname,p,copylen);
252 envname[copylen] = '\0';
253 r = realloc_string_sub(str, envname, envval);
254 TALLOC_FREE(envname);
259 /****************************************************************************
260 Do some standard substitutions in a string.
261 len is the length in bytes of the space allowed in string str. If zero means
262 don't allow expansions.
263 ****************************************************************************/
265 void standard_sub_basic(const char *smb_name, const char *domain_name,
266 char *str, size_t len)
270 if ( (s = talloc_sub_basic(talloc_tos(), smb_name, domain_name, str )) != NULL ) {
271 strncpy( str, s, len );
278 * Limit addresses to hexalpha charactes and underscore, safe for path
279 * components for Windows clients.
281 static void make_address_pathsafe(char *addr)
283 while(addr && *addr) {
284 if(!isxdigit(*addr)) {
291 /****************************************************************************
292 Do some standard substitutions in a string.
293 This function will return a talloced string that has to be freed.
294 ****************************************************************************/
296 char *talloc_sub_basic(TALLOC_CTX *mem_ctx,
297 const char *smb_name,
298 const char *domain_name,
301 char *b, *p, *s, *r, *a_string;
302 fstring pidstr, vnnstr;
303 const char *local_machine_name = get_local_machine_name();
304 TALLOC_CTX *tmp_ctx = NULL;
306 /* workaround to prevent a crash while looking at bug #687 */
309 DEBUG(0,("talloc_sub_basic: NULL source string! This should not happen\n"));
313 a_string = talloc_strdup(mem_ctx, str);
314 if (a_string == NULL) {
315 DEBUG(0, ("talloc_sub_basic: Out of memory!\n"));
319 tmp_ctx = talloc_stackframe();
321 for (s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
328 r = strlower_talloc(tmp_ctx, smb_name);
332 a_string = realloc_string_sub(a_string, "%U", r);
336 bool is_domain_name = false;
337 const char *sep = lp_winbind_separator();
339 if (domain_name != NULL && domain_name[0] != '\0' &&
340 (lp_security() == SEC_ADS ||
341 lp_security() == SEC_DOMAIN)) {
342 r = talloc_asprintf(tmp_ctx,
347 is_domain_name = true;
349 r = talloc_strdup(tmp_ctx, smb_name);
355 pass = Get_Pwnam_alloc(tmp_ctx, r);
359 group_name = gidtoname(pass->pw_gid);
360 if (is_domain_name) {
362 group_sep = strchr_m(group_name, *sep);
363 if (group_sep != NULL) {
364 group_name = group_sep + 1;
367 a_string = realloc_string_sub(a_string,
375 r = strupper_talloc(tmp_ctx, domain_name);
379 a_string = realloc_string_sub(a_string, "%D", r);
382 a_string = realloc_string_sub(
384 sub_peeraddr[0] ? sub_peeraddr : "0.0.0.0");
388 r = talloc_strdup(tmp_ctx,
389 sub_peeraddr[0] ? sub_peeraddr : "0.0.0.0");
390 make_address_pathsafe(r);
391 a_string = realloc_string_sub(a_string, "%J", r);
395 a_string = realloc_string_sub(
397 sub_sockaddr[0] ? sub_sockaddr : "0.0.0.0");
400 r = talloc_strdup(tmp_ctx,
401 sub_sockaddr[0] ? sub_sockaddr : "0.0.0.0");
402 make_address_pathsafe(r);
403 a_string = realloc_string_sub(a_string, "%j", r);
407 if ( strncasecmp_m(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
410 if (local_machine_name && *local_machine_name) {
411 a_string = realloc_string_sub(a_string, "%L", local_machine_name);
413 a_string = realloc_string_sub(a_string, "%L", lp_netbios_name());
417 a_string = realloc_string_sub(a_string,
422 a_string = realloc_string_sub(a_string, "%M",
423 sub_peername ? sub_peername : "");
426 a_string = realloc_string_sub(a_string, "%R", remote_proto);
429 a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False));
432 a_string = realloc_string_sub(a_string, "%t",
433 current_minimal_timestring(tmp_ctx, False));
436 a_string = realloc_string_sub(a_string, "%a",
437 get_remote_arch_str());
440 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)getpid());
441 a_string = realloc_string_sub(a_string, "%d", pidstr);
444 a_string = realloc_string_sub(a_string, "%h", myhostname());
447 a_string = realloc_string_sub(a_string, "%m",
451 a_string = realloc_string_sub(a_string, "%v", samba_version_string());
454 a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
457 a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
460 slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
461 a_string = realloc_string_sub(a_string, "%V", vnnstr);
470 if (a_string == NULL) {
478 TALLOC_FREE(a_string);
481 TALLOC_FREE(tmp_ctx);
485 /****************************************************************************
486 Do some specific substitutions in a string.
487 This function will return an allocated string that have to be freed.
488 ****************************************************************************/
490 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
491 const char *input_string,
492 const char *username,
499 char *ret_string = NULL;
503 if (!(tmp_ctx = talloc_new(mem_ctx))) {
504 DEBUG(0, ("talloc_new failed\n"));
508 a_string = talloc_strdup(tmp_ctx, input_string);
509 if (a_string == NULL) {
510 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
514 for (s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
520 a_string = talloc_string_sub(
521 tmp_ctx, a_string, "%U", username);
524 a_string = talloc_string_sub(
525 tmp_ctx, a_string, "%u", username);
531 if (grpname != NULL) {
534 name = gidtoname(gid);
537 a_string = talloc_string_sub(tmp_ctx,
542 a_string = talloc_string_sub(
551 if (grpname != NULL) {
554 name = gidtoname(gid);
557 a_string = talloc_string_sub(tmp_ctx,
562 a_string = talloc_string_sub(
563 tmp_ctx, a_string, "%g", "NO_GROUP");
567 a_string = talloc_string_sub(tmp_ctx, a_string,
571 a_string = talloc_string_sub(tmp_ctx, a_string,
572 "%N", lp_netbios_name());
579 if (a_string == NULL) {
584 /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
585 * away with the TALLOC_FREE(tmp_ctx) further down. */
587 ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
590 TALLOC_FREE(tmp_ctx);
594 /****************************************************************************
595 ****************************************************************************/
597 char *talloc_sub_advanced(TALLOC_CTX *ctx,
598 const char *servicename,
600 const char *connectpath,
607 a_string = talloc_strdup(talloc_tos(), str);
608 if (a_string == NULL) {
609 DEBUG(0, ("talloc_sub_advanced_only: Out of memory!\n"));
613 for (s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
619 a_string = realloc_string_sub(a_string,
625 if ((h = get_user_home_dir(talloc_tos(), user)))
626 a_string = realloc_string_sub(a_string, "%H", h);
631 a_string = realloc_string_sub(a_string, "%P", connectpath);
634 a_string = realloc_string_sub(a_string, "%S", servicename);
637 a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
640 a_string = realloc_string_sub(a_string, "%u", user);
647 if (a_string == NULL) {
655 char *talloc_sub_full(TALLOC_CTX *ctx,
656 const char *servicename,
658 const char *connectpath,
660 const char *smb_name,
661 const char *domain_name,
664 char *a_string, *ret_string;
666 a_string = talloc_sub_advanced(ctx, servicename, user, connectpath,
668 if (a_string == NULL) {
672 ret_string = talloc_sub_basic(ctx, smb_name, domain_name, a_string);
673 TALLOC_FREE(a_string);