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/>.
24 extern struct current_user current_user;
26 fstring local_machine="";
27 fstring remote_arch="UNKNOWN";
28 userdom_struct current_user_info;
29 fstring remote_proto="UNKNOWN";
31 static fstring remote_machine;
32 static fstring smb_user_name;
35 * Set the 'local' machine name
36 * @param local_name the name we are being called
37 * @param if this is the 'final' name for us, not be be changed again
40 void set_local_machine_name(const char* local_name, bool perm)
42 static bool already_perm = False;
43 fstring tmp_local_machine;
45 fstrcpy(tmp_local_machine,local_name);
46 trim_char(tmp_local_machine,' ',' ');
49 * Windows NT/2k uses "*SMBSERVER" and XP uses "*SMBSERV"
53 if ( strequal(tmp_local_machine, "*SMBSERVER") || strequal(tmp_local_machine, "*SMBSERV") ) {
54 fstrcpy( local_machine, client_socket_addr() );
63 alpha_strcpy(local_machine,tmp_local_machine,SAFE_NETBIOS_CHARS,sizeof(local_machine)-1);
64 strlower_m(local_machine);
68 * Set the 'remote' machine name
69 * @param remote_name the name our client wants to be called by
70 * @param if this is the 'final' name for them, not be be changed again
73 void set_remote_machine_name(const char* remote_name, bool perm)
75 static bool already_perm = False;
76 fstring tmp_remote_machine;
83 fstrcpy(tmp_remote_machine,remote_name);
84 trim_char(tmp_remote_machine,' ',' ');
85 alpha_strcpy(remote_machine,tmp_remote_machine,SAFE_NETBIOS_CHARS,sizeof(remote_machine)-1);
86 strlower_m(remote_machine);
89 const char* get_remote_machine_name(void)
91 return remote_machine;
94 const char* get_local_machine_name(void)
96 if (!*local_machine) {
97 return global_myname();
100 return local_machine;
103 /*******************************************************************
104 Setup the string used by %U substitution.
105 ********************************************************************/
107 void sub_set_smb_name(const char *name)
111 bool is_machine_account = False;
113 /* don't let anonymous logins override the name */
118 fstrcpy( tmp, name );
119 trim_char( tmp, ' ', ' ' );
127 /* long story but here goes....we have to allow usernames
128 ending in '$' as they are valid machine account names.
129 So check for a machine account and re-add the '$'
130 at the end after the call to alpha_strcpy(). --jerry */
132 if ( tmp[len-1] == '$' )
133 is_machine_account = True;
135 alpha_strcpy( smb_user_name, tmp, SAFE_NETBIOS_CHARS, sizeof(smb_user_name)-1 );
137 if ( is_machine_account ) {
138 len = strlen( smb_user_name );
139 smb_user_name[len-1] = '$';
143 /*******************************************************************
144 Setup the strings used by substitutions. Called per packet. Ensure
145 %U name is set correctly also.
146 ********************************************************************/
148 void set_current_user_info(const userdom_struct *pcui)
150 current_user_info = *pcui;
151 /* The following is safe as current_user_info.smb_name
152 * has already been sanitised in register_existing_vuid. */
153 fstrcpy(smb_user_name, current_user_info.smb_name);
156 /*******************************************************************
157 return the current active user name
158 *******************************************************************/
160 const char* get_current_username( void )
162 if ( current_user_info.smb_name[0] == '\0' )
163 return smb_user_name;
165 return current_user_info.smb_name;
168 /*******************************************************************
169 Given a pointer to a %$(NAME) in p and the whole string in str
170 expand it as an environment variable.
171 Return a new allocated and expanded string.
172 Based on code by Branko Cibej <branko.cibej@hermes.si>
173 When this is called p points at the '%' character.
174 May substitute multiple occurrencies of the same env var.
175 ********************************************************************/
177 static char * realloc_expand_env_var(char *str, char *p)
184 if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
189 * Look for the terminating ')'.
192 if ((q = strchr_m(p,')')) == NULL) {
193 DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
198 * Extract the name from within the %$(NAME) string.
204 /* reserve space for use later add %$() chars */
205 if ( (envname = (char *)SMB_MALLOC(copylen + 1 + 4)) == NULL ) {
209 strncpy(envname,r,copylen);
210 envname[copylen] = '\0';
212 if ((envval = getenv(envname)) == NULL) {
213 DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
219 * Copy the full %$(NAME) into envname so it
224 strncpy(envname,p,copylen);
225 envname[copylen] = '\0';
226 r = realloc_string_sub(str, envname, envval);
232 /*******************************************************************
233 *******************************************************************/
235 static char *longvar_domainsid( void )
240 if ( !secrets_fetch_domain_sid( lp_workgroup(), &sid ) ) {
244 sid_string = SMB_STRDUP( sid_string_static( &sid ) );
247 DEBUG(0,("longvar_domainsid: failed to dup SID string!\n"));
253 /*******************************************************************
254 *******************************************************************/
261 static struct api_longvar longvar_table[] = {
262 { "DomainSID", longvar_domainsid },
266 static char *get_longvar_val( const char *varname )
270 DEBUG(7,("get_longvar_val: expanding variable [%s]\n", varname));
272 for ( i=0; longvar_table[i].name; i++ ) {
273 if ( strequal( longvar_table[i].name, varname ) ) {
274 return longvar_table[i].fn();
281 /*******************************************************************
282 Expand the long smb.conf variable names given a pointer to a %(NAME).
283 Return the number of characters by which the pointer should be advanced.
284 When this is called p points at the '%' character.
285 ********************************************************************/
287 static char *realloc_expand_longvar(char *str, char *p)
294 if ( p[0] != '%' || p[1] != '(' ) {
298 /* Look for the terminating ')'.*/
300 if ((q = strchr_m(p,')')) == NULL) {
301 DEBUG(0,("realloc_expand_longvar: Unterminated environment variable [%s]\n", p));
305 /* Extract the name from within the %(NAME) string.*/
308 copylen = MIN( (q-r), (sizeof(varname)-1) );
309 strncpy(varname, r, copylen);
310 varname[copylen] = '\0';
312 if ((value = get_longvar_val(varname)) == NULL) {
313 DEBUG(0,("realloc_expand_longvar: Variable [%s] not set. Skipping\n", varname));
317 /* Copy the full %(NAME) into envname so it can be replaced.*/
319 copylen = MIN( (q+1-p),(sizeof(varname)-1) );
320 strncpy( varname, p, copylen );
321 varname[copylen] = '\0';
322 r = realloc_string_sub(str, varname, value);
325 /* skip over the %(varname) */
330 /*******************************************************************
331 Patch from jkf@soton.ac.uk
332 Added this to implement %p (NIS auto-map version of %H)
333 *******************************************************************/
335 static char *automount_path(const char *user_name)
339 /* use the passwd entry as the default */
340 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
342 pstrcpy(server_path, get_user_home_dir(user_name));
344 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
346 if (lp_nis_home_map()) {
347 const char *home_path_start;
348 const char *automount_value = automount_lookup(user_name);
350 if(strlen(automount_value) > 0) {
351 home_path_start = strchr_m(automount_value,':');
352 if (home_path_start != NULL) {
353 DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n",
354 home_path_start?(home_path_start+1):""));
355 pstrcpy(server_path, home_path_start+1);
358 /* NIS key lookup failed: default to user home directory from password file */
359 DEBUG(5, ("NIS lookup failed. Using Home path from passwd file. Home path is: %s\n", server_path ));
364 DEBUG(4,("Home server path: %s\n", server_path));
366 return talloc_strdup(talloc_tos(), server_path);
369 /*******************************************************************
370 Patch from jkf@soton.ac.uk
371 This is Luke's original function with the NIS lookup code
372 moved out to a separate function.
373 *******************************************************************/
375 static const char *automount_server(const char *user_name)
378 const char *local_machine_name = get_local_machine_name();
380 /* use the local machine name as the default */
381 /* this will be the default if WITH_AUTOMOUNT is not used or fails */
382 if (local_machine_name && *local_machine_name)
383 pstrcpy(server_name, local_machine_name);
385 pstrcpy(server_name, global_myname());
387 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
389 if (lp_nis_home_map()) {
391 char *automount_value = automount_lookup(user_name);
392 home_server_len = strcspn(automount_value,":");
393 DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len));
394 if (home_server_len > sizeof(pstring))
395 home_server_len = sizeof(pstring);
396 strncpy(server_name, automount_value, home_server_len);
397 server_name[home_server_len] = '\0';
401 DEBUG(4,("Home server: %s\n", server_name));
403 return talloc_strdup(talloc_tos(), server_name);
406 /****************************************************************************
407 Do some standard substitutions in a string.
408 len is the length in bytes of the space allowed in string str. If zero means
409 don't allow expansions.
410 ****************************************************************************/
412 void standard_sub_basic(const char *smb_name, const char *domain_name,
413 char *str, size_t len)
417 if ( (s = alloc_sub_basic( smb_name, domain_name, str )) != NULL ) {
418 strncpy( str, s, len );
425 /****************************************************************************
426 Do some standard substitutions in a string.
427 This function will return an allocated string that have to be freed.
428 ****************************************************************************/
430 char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name,
431 const char *domain_name, const char *str)
435 if ( (a = alloc_sub_basic(smb_name, domain_name, str)) == NULL ) {
438 t = talloc_strdup(mem_ctx, a);
443 /****************************************************************************
444 ****************************************************************************/
446 char *alloc_sub_basic(const char *smb_name, const char *domain_name,
449 char *b, *p, *s, *r, *a_string;
450 fstring pidstr, vnnstr;
452 char addr[INET6_ADDRSTRLEN];
453 const char *local_machine_name = get_local_machine_name();
455 /* workaround to prevent a crash while looking at bug #687 */
458 DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
462 a_string = SMB_STRDUP(str);
463 if (a_string == NULL) {
464 DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
468 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
475 r = strdup_lower(smb_name);
479 a_string = realloc_string_sub(a_string, "%U", r);
482 r = SMB_STRDUP(smb_name);
486 if ((pass = Get_Pwnam(r))!=NULL) {
487 a_string = realloc_string_sub(a_string, "%G", gidtoname(pass->pw_gid));
491 r = strdup_upper(domain_name);
495 a_string = realloc_string_sub(a_string, "%D", r);
498 a_string = realloc_string_sub(a_string, "%I", client_addr(addr));
501 a_string = realloc_string_sub( a_string, "%i", client_socket_addr() );
504 if ( StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
507 if (local_machine_name && *local_machine_name) {
508 a_string = realloc_string_sub(a_string, "%L", local_machine_name);
510 a_string = realloc_string_sub(a_string, "%L", global_myname());
514 a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
517 a_string = realloc_string_sub(a_string, "%M", client_name());
520 a_string = realloc_string_sub(a_string, "%R", remote_proto);
523 a_string = realloc_string_sub(a_string, "%T", current_timestring(False));
526 a_string = realloc_string_sub(a_string, "%a", remote_arch);
529 slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid());
530 a_string = realloc_string_sub(a_string, "%d", pidstr);
533 a_string = realloc_string_sub(a_string, "%h", myhostname());
536 a_string = realloc_string_sub(a_string, "%m", remote_machine);
539 a_string = realloc_string_sub(a_string, "%v", SAMBA_VERSION_STRING);
542 a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
545 a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
548 a_string = realloc_expand_longvar( a_string, p );
551 slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
552 a_string = realloc_string_sub(a_string, "%V", vnnstr);
573 /****************************************************************************
574 Do some specific substitutions in a string.
575 This function will return an allocated string that have to be freed.
576 ****************************************************************************/
578 char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
579 const char *input_string,
580 const char *username,
586 char *ret_string = NULL;
590 if (!(tmp_ctx = talloc_new(mem_ctx))) {
591 DEBUG(0, ("talloc_new failed\n"));
595 a_string = talloc_strdup(tmp_ctx, input_string);
596 if (a_string == NULL) {
597 DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
601 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
607 a_string = talloc_string_sub(
608 tmp_ctx, a_string, "%U", username);
611 a_string = talloc_string_sub(
612 tmp_ctx, a_string, "%u", username);
616 a_string = talloc_string_sub(
617 tmp_ctx, a_string, "%G",
620 a_string = talloc_string_sub(
627 a_string = talloc_string_sub(
628 tmp_ctx, a_string, "%g",
631 a_string = talloc_string_sub(
632 tmp_ctx, a_string, "%g", "NO_GROUP");
636 a_string = talloc_string_sub(tmp_ctx, a_string,
640 a_string = talloc_string_sub(
641 tmp_ctx, a_string, "%N",
642 automount_server(username));
649 if (a_string == NULL) {
654 /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
655 * away with the TALLOC_FREE(tmp_ctx) further down. */
657 ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
660 TALLOC_FREE(tmp_ctx);
664 /****************************************************************************
665 ****************************************************************************/
667 static char *alloc_sub_advanced(const char *servicename, const char *user,
668 const char *connectpath, gid_t gid,
669 const char *smb_name, const char *domain_name,
672 char *a_string, *ret_string;
675 a_string = SMB_STRDUP(str);
676 if (a_string == NULL) {
677 DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
681 for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
687 a_string = realloc_string_sub(a_string, "%N", automount_server(user));
690 if ((h = get_user_home_dir(user)))
691 a_string = realloc_string_sub(a_string, "%H", h);
694 a_string = realloc_string_sub(a_string, "%P", connectpath);
697 a_string = realloc_string_sub(a_string, "%S", servicename);
700 a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
703 a_string = realloc_string_sub(a_string, "%u", user);
706 /* Patch from jkf@soton.ac.uk Left the %N (NIS
707 * server name) in standard_sub_basic as it is
708 * a feature for logon servers, hence uses the
709 * username. The %p (NIS server path) code is
710 * here as it is used instead of the default
711 * "path =" string in [homes] and so needs the
712 * service name, not the username. */
714 a_string = realloc_string_sub(a_string, "%p",
715 automount_path(servicename));
723 if (a_string == NULL) {
728 ret_string = alloc_sub_basic(smb_name, domain_name, a_string);
734 * This obviously is inefficient and needs to be merged into
735 * alloc_sub_advanced...
738 char *talloc_sub_advanced(TALLOC_CTX *mem_ctx,
739 const char *servicename, const char *user,
740 const char *connectpath, gid_t gid,
741 const char *smb_name, const char *domain_name,
746 if (!(a = alloc_sub_advanced(servicename, user, connectpath, gid,
747 smb_name, domain_name, str))) {
750 t = talloc_strdup(mem_ctx, a);
756 void standard_sub_advanced(const char *servicename, const char *user,
757 const char *connectpath, gid_t gid,
758 const char *smb_name, const char *domain_name,
759 char *str, size_t len)
763 s = alloc_sub_advanced(servicename, user, connectpath,
764 gid, smb_name, domain_name, str);
767 strncpy( str, s, len );
772 /****************************************************************************
773 * Do some standard substitutions in a string.
774 * ****************************************************************************/
776 void standard_sub_conn(connection_struct *conn, char *str, size_t len)
780 s = alloc_sub_advanced(lp_servicename(SNUM(conn)), conn->user, conn->connectpath,
781 conn->gid, smb_user_name, "", str);
784 strncpy( str, s, len );