string substitution functions
Copyright (C) Andrew Tridgell 1992-2000
Copyright (C) Gerald Carter 2006
-
+
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
-
+
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
-
+
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
+#include "system/passwd.h"
+#include "secrets.h"
+#include "auth.h"
-extern struct current_user current_user;
+static char *alloc_sub_basic(const char *smb_name, const char *domain_name,
+ const char *str);
userdom_struct current_user_info;
fstring remote_proto="UNKNOWN";
static char *local_machine;
+void free_local_machine_name(void)
+{
+ SAFE_FREE(local_machine);
+}
+
bool set_local_machine_name(const char *local_name, bool perm)
{
static bool already_perm = false;
char *tmp_local_machine = NULL;
- char addr[INET6_ADDRSTRLEN];
size_t len;
+ if (already_perm) {
+ return true;
+ }
+
tmp_local_machine = SMB_STRDUP(local_name);
if (!tmp_local_machine) {
return false;
}
trim_char(tmp_local_machine,' ',' ');
- /*
- * Windows NT/2k uses "*SMBSERVER" and XP uses "*SMBSERV"
- * arrggg!!!
- */
-
- if (strequal(tmp_local_machine, "*SMBSERVER") ||
- strequal(tmp_local_machine, "*SMBSERV") ) {
- SAFE_FREE(local_machine);
- local_machine = SMB_STRDUP(client_socket_addr(get_client_fd(),
- addr, sizeof(addr)) );
- SAFE_FREE(tmp_local_machine);
- return local_machine ? true : false;
- }
-
- if (already_perm) {
- return true;
- }
-
SAFE_FREE(local_machine);
len = strlen(tmp_local_machine);
local_machine = SMB_CALLOC_ARRAY(char, len+1);
}
}
+static char sub_peeraddr[INET6_ADDRSTRLEN];
+static const char *sub_peername = "";
+static char sub_sockaddr[INET6_ADDRSTRLEN];
+
+void sub_set_socket_ids(const char *peeraddr, const char *peername,
+ const char *sockaddr)
+{
+ const char *addr = peeraddr;
+
+ if (strnequal(addr, "::ffff:", 7)) {
+ addr += 7;
+ }
+ strlcpy(sub_peeraddr, addr, sizeof(sub_peeraddr));
+
+ sub_peername = SMB_STRDUP(peername);
+ if (sub_peername == NULL) {
+ sub_peername = sub_peeraddr;
+ }
+
+ /*
+ * Shouldn't we do the ::ffff: cancellation here as well? The
+ * original code in alloc_sub_basic() did not do it, so I'm
+ * leaving it out here as well for compatibility.
+ */
+ strlcpy(sub_sockaddr, sockaddr, sizeof(sub_sockaddr));
+}
+
static const char *get_smb_user_name(void)
{
return smb_user_name ? smb_user_name : "";
/*******************************************************************
Setup the strings used by substitutions. Called per packet. Ensure
%U name is set correctly also.
+
+ smb_name must be sanitized by alpha_strcpy
********************************************************************/
-void set_current_user_info(const userdom_struct *pcui)
+void set_current_user_info(const char *smb_name, const char *unix_name,
+ const char *domain)
{
- current_user_info = *pcui;
+ fstrcpy(current_user_info.smb_name, smb_name);
+ fstrcpy(current_user_info.unix_name, unix_name);
+ fstrcpy(current_user_info.domain, domain);
+
/* The following is safe as current_user_info.smb_name
* has already been sanitised in register_existing_vuid. */
r = p + 3;
copylen = q - r;
-
+
/* reserve space for use later add %$() chars */
if ( (envname = (char *)SMB_MALLOC(copylen + 1 + 4)) == NULL ) {
return NULL;
}
-
+
strncpy(envname,r,copylen);
envname[copylen] = '\0';
envname[copylen] = '\0';
r = realloc_string_sub(str, envname, envval);
SAFE_FREE(envname);
-
+
return r;
}
static char *longvar_domainsid( void )
{
- DOM_SID sid;
+ struct dom_sid sid;
fstring tmp;
char *sid_string;
-
+
if ( !secrets_fetch_domain_sid( lp_workgroup(), &sid ) ) {
return NULL;
}
-
+
sid_string = SMB_STRDUP( sid_to_fstring( tmp, &sid ) );
-
+
if ( !sid_string ) {
DEBUG(0,("longvar_domainsid: failed to dup SID string!\n"));
}
-
+
return sid_string;
}
static char *get_longvar_val( const char *varname )
{
int i;
-
+
DEBUG(7,("get_longvar_val: expanding variable [%s]\n", varname));
-
+
for ( i=0; longvar_table[i].name; i++ ) {
if ( strequal( longvar_table[i].name, varname ) ) {
return longvar_table[i].fn();
}
}
-
+
return NULL;
}
(home_path_start+1):""));
server_path = talloc_strdup(ctx,
home_path_start+1);
+ if (!server_path) {
+ server_path = "";
+ }
}
} else {
/* NIS key lookup failed: default to
}
#endif
- if (!server_path) {
- server_path = "";
- }
DEBUG(4,("Home server path: %s\n", server_path));
return server_path;
}
return "";
}
srv = talloc_strdup(ctx, automount_value);
+ if (!srv) {
+ return "";
+ }
p = strchr_m(srv, ':');
if (!p) {
return "";
}
#endif
- if (!server_name) {
- server_name = "";
- }
DEBUG(4,("Home server: %s\n", server_name));
return server_name;
}
char *str, size_t len)
{
char *s;
-
+
if ( (s = alloc_sub_basic( smb_name, domain_name, str )) != NULL ) {
strncpy( str, s, len );
}
-
+
SAFE_FREE( s );
-
}
/****************************************************************************
const char *domain_name, const char *str)
{
char *a, *t;
-
+
if ( (a = alloc_sub_basic(smb_name, domain_name, str)) == NULL ) {
return NULL;
}
/****************************************************************************
****************************************************************************/
-char *alloc_sub_basic(const char *smb_name, const char *domain_name,
- const char *str)
+static char *alloc_sub_basic(const char *smb_name, const char *domain_name,
+ const char *str)
{
char *b, *p, *s, *r, *a_string;
fstring pidstr, vnnstr;
- char addr[INET6_ADDRSTRLEN];
const char *local_machine_name = get_local_machine_name();
+ TALLOC_CTX *tmp_ctx = NULL;
/* workaround to prevent a crash while looking at bug #687 */
-
+
if (!str) {
DEBUG(0,("alloc_sub_basic: NULL source string! This should not happen\n"));
return NULL;
}
-
+
a_string = SMB_STRDUP(str);
if (a_string == NULL) {
DEBUG(0, ("alloc_sub_basic: Out of memory!\n"));
return NULL;
}
-
+
+ tmp_ctx = talloc_stackframe();
+
for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
r = NULL;
b = a_string;
-
+
switch (*(p+1)) {
case 'U' :
- r = strdup_lower(smb_name);
+ r = strlower_talloc(tmp_ctx, smb_name);
if (r == NULL) {
goto error;
}
break;
case 'G' : {
struct passwd *pass;
- r = SMB_STRDUP(smb_name);
+ r = talloc_strdup(tmp_ctx, smb_name);
if (r == NULL) {
goto error;
}
- pass = Get_Pwnam_alloc(talloc_tos(), r);
+ pass = Get_Pwnam_alloc(tmp_ctx, r);
if (pass != NULL) {
a_string = realloc_string_sub(
a_string, "%G",
break;
}
case 'D' :
- r = strdup_upper(domain_name);
+ r = strupper_talloc(tmp_ctx, domain_name);
if (r == NULL) {
goto error;
}
a_string = realloc_string_sub(a_string, "%D", r);
break;
- case 'I' :
- a_string = realloc_string_sub(a_string, "%I",
- client_addr(get_client_fd(),addr, sizeof(addr)));
+ case 'I' : {
+ a_string = realloc_string_sub(
+ a_string, "%I",
+ sub_peeraddr[0] ? sub_peeraddr : "0.0.0.0");
break;
+ }
case 'i':
- a_string = realloc_string_sub( a_string, "%i",
- client_socket_addr(get_client_fd(), addr, sizeof(addr)) );
+ a_string = realloc_string_sub(
+ a_string, "%i",
+ sub_sockaddr[0] ? sub_sockaddr : "0.0.0.0");
break;
case 'L' :
- if ( StrnCaseCmp(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
+ if ( strncasecmp_m(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
break;
}
if (local_machine_name && *local_machine_name) {
a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
break;
case 'M' :
- a_string = realloc_string_sub(a_string, "%M", client_name(get_client_fd()));
+ a_string = realloc_string_sub(a_string, "%M",
+ sub_peername);
break;
case 'R' :
a_string = realloc_string_sub(a_string, "%R", remote_proto);
break;
case 'T' :
- a_string = realloc_string_sub(a_string, "%T", current_timestring(False));
+ a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False));
break;
case 'a' :
a_string = realloc_string_sub(a_string, "%a",
a_string = realloc_string_sub(a_string, "%h", myhostname());
break;
case 'm' :
- a_string = realloc_string_sub(a_string, "%m", remote_machine);
+ a_string = realloc_string_sub(a_string, "%m",
+ remote_machine
+ ? remote_machine
+ : "");
break;
case 'v' :
- a_string = realloc_string_sub(a_string, "%v", SAMBA_VERSION_STRING);
+ a_string = realloc_string_sub(a_string, "%v", samba_version_string());
break;
case 'w' :
a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
}
p++;
- SAFE_FREE(r);
-
- if ( !a_string ) {
- return NULL;
+ TALLOC_FREE(r);
+
+ if (a_string == NULL) {
+ goto done;
}
}
- return a_string;
+ goto done;
error:
SAFE_FREE(a_string);
- return NULL;
+
+done:
+ TALLOC_FREE(tmp_ctx);
+ return a_string;
}
/****************************************************************************
DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
goto done;
}
-
+
for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
-
+
b = a_string;
-
+
switch (*(p+1)) {
case 'U' :
a_string = talloc_string_sub(
DEBUG(0, ("alloc_sub_advanced: Out of memory!\n"));
return NULL;
}
-
+
for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
-
+
b = a_string;
-
+
switch (*(p+1)) {
case 'N' :
a_string = realloc_string_sub(a_string, "%N", automount_server(user));
case 'u':
a_string = realloc_string_sub(a_string, "%u", user);
break;
-
+
/* Patch from jkf@soton.ac.uk Left the %N (NIS
* server name) in standard_sub_basic as it is
* a feature for logon servers, hence uses the
a_string = realloc_string_sub(a_string, "%p",
automount_path(servicename));
break;
-
+
default:
break;
}
{
return talloc_sub_advanced(ctx,
lp_servicename(SNUM(conn)),
- conn->user,
+ conn->session_info->unix_name,
conn->connectpath,
- conn->gid,
+ conn->session_info->utok.gid,
get_smb_user_name(),
"",
str);