2000-04-17 08:46:10 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2000-04-17 08:46:10 +04:00
string substitution functions
Copyright ( C ) Andrew Tridgell 1992 - 2000
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 2 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 , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
fstring local_machine = " " ;
fstring remote_arch = " UNKNOWN " ;
2001-01-23 04:52:30 +03:00
userdom_struct current_user_info ;
2000-04-17 08:46:10 +04:00
fstring remote_proto = " UNKNOWN " ;
2002-11-20 03:53:24 +03:00
static fstring remote_machine ;
static fstring smb_user_name ;
2002-08-17 21:00:51 +04:00
2003-03-18 12:52:55 +03:00
/**
* Set the ' local ' machine name
* @ param local_name the name we are being called
* @ param if this is the ' final ' name for us , not be be changed again
*/
2002-08-17 21:00:51 +04:00
2003-03-18 12:52:55 +03:00
void set_local_machine_name ( const char * local_name , BOOL perm )
2002-08-17 21:00:51 +04:00
{
2003-03-18 12:52:55 +03:00
static BOOL already_perm = False ;
2002-08-17 21:00:51 +04:00
fstring tmp_local_machine ;
2003-04-23 05:34:56 +04:00
/*
* Windows NT / 2 k uses " *SMBSERVER " and XP uses " *SMBSERV "
* arrggg ! ! !
*/
2003-10-23 03:38:20 +04:00
if ( strequal ( local_name , " *SMBSERVER " ) )
2003-04-23 05:34:56 +04:00
return ;
2003-10-23 03:38:20 +04:00
if ( strequal ( local_name , " *SMBSERV " ) )
2003-04-23 05:21:42 +04:00
return ;
2003-03-18 12:52:55 +03:00
if ( already_perm )
return ;
already_perm = perm ;
2002-08-17 21:00:51 +04:00
fstrcpy ( tmp_local_machine , local_name ) ;
2003-09-05 23:59:55 +04:00
trim_char ( tmp_local_machine , ' ' , ' ' ) ;
2002-08-17 21:00:51 +04:00
alpha_strcpy ( local_machine , tmp_local_machine , SAFE_NETBIOS_CHARS , sizeof ( local_machine ) - 1 ) ;
2003-07-27 06:43:22 +04:00
strlower_m ( local_machine ) ;
2002-08-17 21:00:51 +04:00
}
2003-03-18 12:52:55 +03:00
/**
* Set the ' remote ' machine name
* @ param remote_name the name our client wants to be called by
* @ param if this is the ' final ' name for them , not be be changed again
*/
void set_remote_machine_name ( const char * remote_name , BOOL perm )
2002-08-17 21:00:51 +04:00
{
2003-03-18 12:52:55 +03:00
static BOOL already_perm = False ;
2002-08-17 21:00:51 +04:00
fstring tmp_remote_machine ;
2003-03-18 12:52:55 +03:00
if ( already_perm )
return ;
already_perm = perm ;
2002-08-17 21:00:51 +04:00
fstrcpy ( tmp_remote_machine , remote_name ) ;
2003-09-05 23:59:55 +04:00
trim_char ( tmp_remote_machine , ' ' , ' ' ) ;
2002-08-17 21:00:51 +04:00
alpha_strcpy ( remote_machine , tmp_remote_machine , SAFE_NETBIOS_CHARS , sizeof ( remote_machine ) - 1 ) ;
2003-07-27 06:43:22 +04:00
strlower_m ( remote_machine ) ;
2002-08-17 21:00:51 +04:00
}
const char * get_remote_machine_name ( void )
{
return remote_machine ;
}
const char * get_local_machine_name ( void )
{
2003-03-18 12:52:55 +03:00
if ( ! * local_machine ) {
return global_myname ( ) ;
}
2002-08-17 21:00:51 +04:00
return local_machine ;
}
2003-08-29 03:57:34 +04:00
/*******************************************************************
Setup the string used by % U substitution .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-11-20 03:53:24 +03:00
void sub_set_smb_name ( const char * name )
{
fstring tmp ;
/* don't let anonymous logins override the name */
2003-08-29 03:57:34 +04:00
if ( ! * name )
return ;
2002-11-20 03:53:24 +03:00
fstrcpy ( tmp , name ) ;
2003-09-05 23:59:55 +04:00
trim_char ( tmp , ' ' , ' ' ) ;
2003-07-03 23:11:31 +04:00
strlower_m ( tmp ) ;
2002-11-20 03:53:24 +03:00
alpha_strcpy ( smb_user_name , tmp , SAFE_NETBIOS_CHARS , sizeof ( smb_user_name ) - 1 ) ;
}
2003-08-29 03:57:34 +04:00
/*******************************************************************
Setup the strings used by substitutions . Called per packet . Ensure
% U name is set correctly also .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void set_current_user_info ( const userdom_struct * pcui )
{
current_user_info = * pcui ;
/* The following is safe as current_user_info.smb_name
* has already been sanitised in register_vuid . */
fstrcpy ( smb_user_name , current_user_info . smb_name ) ;
}
2002-11-20 03:53:24 +03:00
2000-04-17 08:46:10 +04:00
/*******************************************************************
Given a pointer to a % $ ( NAME ) expand it as an environment variable .
Return the number of characters by which the pointer should be advanced .
Based on code by Branko Cibej < branko . cibej @ hermes . si >
When this is called p points at the ' % ' character .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-01-17 02:32:10 +03:00
2000-04-17 08:46:10 +04:00
static size_t expand_env_var ( char * p , int len )
{
fstring envname ;
char * envval ;
char * q , * r ;
int copylen ;
if ( p [ 1 ] ! = ' $ ' )
return 1 ;
if ( p [ 2 ] ! = ' ( ' )
return 2 ;
/*
* Look for the terminating ' ) ' .
*/
2001-07-04 11:36:09 +04:00
if ( ( q = strchr_m ( p , ' ) ' ) ) = = NULL ) {
2000-04-17 08:46:10 +04:00
DEBUG ( 0 , ( " expand_env_var: Unterminated environment variable [%s] \n " , p ) ) ;
return 2 ;
}
/*
* Extract the name from within the % $ ( NAME ) string .
*/
r = p + 3 ;
copylen = MIN ( ( q - r ) , ( sizeof ( envname ) - 1 ) ) ;
strncpy ( envname , r , copylen ) ;
envname [ copylen ] = ' \0 ' ;
if ( ( envval = getenv ( envname ) ) = = NULL ) {
DEBUG ( 0 , ( " expand_env_var: Environment variable [%s] not set \n " , envname ) ) ;
return 2 ;
}
/*
* Copy the full % $ ( NAME ) into envname so it
* can be replaced .
*/
copylen = MIN ( ( q + 1 - p ) , ( sizeof ( envname ) - 1 ) ) ;
strncpy ( envname , p , copylen ) ;
envname [ copylen ] = ' \0 ' ;
string_sub ( p , envname , envval , len ) ;
return 0 ; /* Allow the environment contents to be parsed. */
}
2002-07-15 14:35:28 +04:00
/*******************************************************************
Given a pointer to a % $ ( NAME ) in p and the whole string in str
expand it as an environment variable .
Return a new allocated and expanded string .
Based on code by Branko Cibej < branko . cibej @ hermes . si >
When this is called p points at the ' % ' character .
May substitute multiple occurrencies of the same env var .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static char * realloc_expand_env_var ( char * str , char * p )
{
char * envname ;
char * envval ;
char * q , * r ;
int copylen ;
if ( p [ 0 ] ! = ' % ' | | p [ 1 ] ! = ' $ ' | | p [ 2 ] ! = ' ( ' )
return str ;
/*
* Look for the terminating ' ) ' .
*/
if ( ( q = strchr_m ( p , ' ) ' ) ) = = NULL ) {
DEBUG ( 0 , ( " expand_env_var: Unterminated environment variable [%s] \n " , p ) ) ;
return str ;
}
/*
* Extract the name from within the % $ ( NAME ) string .
*/
r = p + 3 ;
copylen = q - r ;
envname = ( char * ) malloc ( copylen + 1 + 4 ) ; /* reserve space for use later add %$() chars */
if ( envname = = NULL ) return NULL ;
strncpy ( envname , r , copylen ) ;
envname [ copylen ] = ' \0 ' ;
if ( ( envval = getenv ( envname ) ) = = NULL ) {
DEBUG ( 0 , ( " expand_env_var: Environment variable [%s] not set \n " , envname ) ) ;
SAFE_FREE ( envname ) ;
return str ;
}
/*
* Copy the full % $ ( NAME ) into envname so it
* can be replaced .
*/
copylen = q + 1 - p ;
strncpy ( envname , p , copylen ) ;
envname [ copylen ] = ' \0 ' ;
r = realloc_string_sub ( str , envname , envval ) ;
SAFE_FREE ( envname ) ;
if ( r = = NULL ) return NULL ;
return r ;
}
2000-04-17 08:46:10 +04:00
/*******************************************************************
Patch from jkf @ soton . ac . uk
Added this to implement % p ( NIS auto - map version of % H )
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-01-17 02:32:10 +03:00
static char * automount_path ( const char * user_name )
2000-04-17 08:46:10 +04:00
{
static pstring server_path ;
/* use the passwd entry as the default */
/* this will be the default if WITH_AUTOMOUNT is not used or fails */
2002-01-17 02:32:10 +03:00
2002-01-17 02:53:10 +03:00
pstrcpy ( server_path , get_user_home_dir ( user_name ) ) ;
2000-04-17 08:46:10 +04:00
# if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
2001-03-27 01:52:43 +04:00
if ( lp_nis_home_map ( ) ) {
char * home_path_start ;
2000-04-17 08:46:10 +04:00
char * automount_value = automount_lookup ( user_name ) ;
2001-03-27 01:52:43 +04:00
if ( strlen ( automount_value ) > 0 ) {
2001-07-04 11:36:09 +04:00
home_path_start = strchr_m ( automount_value , ' : ' ) ;
2001-03-27 01:52:43 +04:00
if ( home_path_start ! = NULL ) {
DEBUG ( 5 , ( " NIS lookup succeeded. Home path is: %s \n " ,
home_path_start ? ( home_path_start + 1 ) : " " ) ) ;
pstrcpy ( server_path , home_path_start + 1 ) ;
}
} else {
/* NIS key lookup failed: default to user home directory from password file */
2002-01-17 02:32:10 +03:00
DEBUG ( 5 , ( " NIS lookup failed. Using Home path from passwd file. Home path is: %s \n " , server_path ) ) ;
2000-04-17 08:46:10 +04:00
}
}
# endif
DEBUG ( 4 , ( " Home server path: %s \n " , server_path ) ) ;
return server_path ;
}
/*******************************************************************
Patch from jkf @ soton . ac . uk
This is Luke ' s original function with the NIS lookup code
moved out to a separate function .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-01-17 02:32:10 +03:00
2002-08-17 21:00:51 +04:00
static const char * automount_server ( const char * user_name )
2000-04-17 08:46:10 +04:00
{
static pstring server_name ;
2002-08-17 21:00:51 +04:00
const char * local_machine_name = get_local_machine_name ( ) ;
2000-04-17 08:46:10 +04:00
/* use the local machine name as the default */
/* this will be the default if WITH_AUTOMOUNT is not used or fails */
2002-08-17 21:00:51 +04:00
if ( local_machine_name & & * local_machine_name )
pstrcpy ( server_name , local_machine_name ) ;
2002-01-17 02:32:10 +03:00
else
2002-11-13 02:20:50 +03:00
pstrcpy ( server_name , global_myname ( ) ) ;
This commit is number 2 of 4.
In particular this commit focuses on:
The guts of the moving about inside passdb.
While these changes have been mildly tested, and are pretty small, any
assistance in this is appreciated.
----
These changes allow for the introduction of a large dose of 'const' to
the Samba tree.
There are a number of good reasons to do this:
- I want to allow the SAM_ACCOUNT structure to move from wasteful
pstrings and fstrings to allocated strings. We can't do that if
people are modifying these outputs, as they may well make
assumptions about getting pstrings and fstrings
- I want --with-pam_smbpass to compile with a slightly sane
volume of warnings, currently its pretty bad, even in 2.2
where is compiles at all.
- Tridge assures me that he no longer opposes 'const religion'
based on the ability to #define const the problem away.
- Changed Get_Pwnam(x,y) into two variants (so that the const
parameter can work correctly): - Get_Pwnam(const x) and
Get_Pwnam_Modify(x).
- Reworked smbd/chgpasswd.c to work with these mods, passing
around a 'struct passwd' rather than the modified username
passdb/
- Kill off disp_info stuff, it isn't used any more - Kill off
support for writing to the old smbpasswd format, it isn't relevent
to Samba 3.0
- Move around and modify the pdb_...() helper functions, adding
one that sets the last changed time to 'now' and that sets the
must change time appropriately.
- Remove the ugly forced update of the LCT- value in
pdb_smbpasswd. - Remove the implicit modification of the ACB
flags when both NT and LM passwords are set.
- Removed substation in pdb_getsampwnam output, as a single
password change will render them inoperable in any case (they
will be substituted and stored)
- Added a default RID to the init_sam_from_pw() function, based on
our rid algorithm.
- Added checks that an smbpasswd stored user has a uid-based RID.
- Fail to store tdb based users without a RID
lib/
- Change the substituion code to use global_myname if there is
no connection (and therefore no called name) at the present time.
(This used to be commit 8f607810eb24ed1157bbd2e896c2c167bc34d986)
2001-10-29 10:24:49 +03:00
2000-04-17 08:46:10 +04:00
# if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
2002-01-17 02:32:10 +03:00
if ( lp_nis_home_map ( ) ) {
2000-04-17 08:46:10 +04:00
int home_server_len ;
char * automount_value = automount_lookup ( user_name ) ;
home_server_len = strcspn ( automount_value , " : " ) ;
DEBUG ( 5 , ( " NIS lookup succeeded. Home server length: %d \n " , home_server_len ) ) ;
if ( home_server_len > sizeof ( pstring ) )
home_server_len = sizeof ( pstring ) ;
strncpy ( server_name , automount_value , home_server_len ) ;
server_name [ home_server_len ] = ' \0 ' ;
}
# endif
DEBUG ( 4 , ( " Home server: %s \n " , server_name ) ) ;
return server_name ;
}
/****************************************************************************
Do some standard substitutions in a string .
2002-07-15 14:35:28 +04:00
len is the length in bytes of the space allowed in string str . If zero means
don ' t allow expansions .
2000-04-17 08:46:10 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-01-17 02:32:10 +03:00
2002-07-15 14:35:28 +04:00
void standard_sub_basic ( const char * smb_name , char * str , size_t len )
2000-04-17 08:46:10 +04:00
{
2000-04-23 12:30:37 +04:00
char * p , * s ;
2000-04-19 08:01:16 +04:00
fstring pidstr ;
2001-10-18 22:52:01 +04:00
struct passwd * pass ;
2002-08-17 21:00:51 +04:00
const char * local_machine_name = get_local_machine_name ( ) ;
2000-04-17 08:46:10 +04:00
2001-07-04 11:36:09 +04:00
for ( s = str ; ( p = strchr_m ( s , ' % ' ) ) ; s = p ) {
2001-04-25 22:39:31 +04:00
fstring tmp_str ;
2002-07-15 14:35:28 +04:00
int l = ( int ) len - ( int ) ( p - str ) ;
if ( l < 0 )
l = 0 ;
2000-04-19 08:01:16 +04:00
2000-04-17 08:46:10 +04:00
switch ( * ( p + 1 ) ) {
2001-04-25 22:39:31 +04:00
case ' U ' :
2001-12-06 16:09:15 +03:00
fstrcpy ( tmp_str , smb_name ) ;
2003-07-03 23:11:31 +04:00
strlower_m ( tmp_str ) ;
2001-04-25 22:39:31 +04:00
string_sub ( p , " %U " , tmp_str , l ) ;
break ;
2001-10-18 22:52:01 +04:00
case ' G ' :
2001-12-06 16:09:15 +03:00
fstrcpy ( tmp_str , smb_name ) ;
2001-11-30 03:28:39 +03:00
if ( ( pass = Get_Pwnam ( tmp_str ) ) ! = NULL ) {
2001-10-18 22:52:01 +04:00
string_sub ( p , " %G " , gidtoname ( pass - > pw_gid ) , l ) ;
} else {
p + = 2 ;
}
break ;
2001-04-25 22:39:31 +04:00
case ' D ' :
fstrcpy ( tmp_str , current_user_info . domain ) ;
2003-07-03 23:11:31 +04:00
strupper_m ( tmp_str ) ;
2001-04-25 22:39:31 +04:00
string_sub ( p , " %D " , tmp_str , l ) ;
break ;
2002-07-15 14:35:28 +04:00
case ' I ' :
string_sub ( p , " %I " , client_addr ( ) , l ) ;
break ;
2003-11-07 13:11:48 +03:00
case ' i ' :
string_sub ( p , " %i " , client_socket_addr ( ) , l ) ;
break ;
This commit is number 2 of 4.
In particular this commit focuses on:
The guts of the moving about inside passdb.
While these changes have been mildly tested, and are pretty small, any
assistance in this is appreciated.
----
These changes allow for the introduction of a large dose of 'const' to
the Samba tree.
There are a number of good reasons to do this:
- I want to allow the SAM_ACCOUNT structure to move from wasteful
pstrings and fstrings to allocated strings. We can't do that if
people are modifying these outputs, as they may well make
assumptions about getting pstrings and fstrings
- I want --with-pam_smbpass to compile with a slightly sane
volume of warnings, currently its pretty bad, even in 2.2
where is compiles at all.
- Tridge assures me that he no longer opposes 'const religion'
based on the ability to #define const the problem away.
- Changed Get_Pwnam(x,y) into two variants (so that the const
parameter can work correctly): - Get_Pwnam(const x) and
Get_Pwnam_Modify(x).
- Reworked smbd/chgpasswd.c to work with these mods, passing
around a 'struct passwd' rather than the modified username
passdb/
- Kill off disp_info stuff, it isn't used any more - Kill off
support for writing to the old smbpasswd format, it isn't relevent
to Samba 3.0
- Move around and modify the pdb_...() helper functions, adding
one that sets the last changed time to 'now' and that sets the
must change time appropriately.
- Remove the ugly forced update of the LCT- value in
pdb_smbpasswd. - Remove the implicit modification of the ACB
flags when both NT and LM passwords are set.
- Removed substation in pdb_getsampwnam output, as a single
password change will render them inoperable in any case (they
will be substituted and stored)
- Added a default RID to the init_sam_from_pw() function, based on
our rid algorithm.
- Added checks that an smbpasswd stored user has a uid-based RID.
- Fail to store tdb based users without a RID
lib/
- Change the substituion code to use global_myname if there is
no connection (and therefore no called name) at the present time.
(This used to be commit 8f607810eb24ed1157bbd2e896c2c167bc34d986)
2001-10-29 10:24:49 +03:00
case ' L ' :
2002-08-17 21:00:51 +04:00
if ( local_machine_name & & * local_machine_name )
string_sub ( p , " %L " , local_machine_name , l ) ;
2002-09-25 19:19:00 +04:00
else {
pstring temp_name ;
2002-11-13 02:20:50 +03:00
pstrcpy ( temp_name , global_myname ( ) ) ;
2003-07-03 23:11:31 +04:00
strlower_m ( temp_name ) ;
2002-09-25 19:19:00 +04:00
string_sub ( p , " %L " , temp_name , l ) ;
}
This commit is number 2 of 4.
In particular this commit focuses on:
The guts of the moving about inside passdb.
While these changes have been mildly tested, and are pretty small, any
assistance in this is appreciated.
----
These changes allow for the introduction of a large dose of 'const' to
the Samba tree.
There are a number of good reasons to do this:
- I want to allow the SAM_ACCOUNT structure to move from wasteful
pstrings and fstrings to allocated strings. We can't do that if
people are modifying these outputs, as they may well make
assumptions about getting pstrings and fstrings
- I want --with-pam_smbpass to compile with a slightly sane
volume of warnings, currently its pretty bad, even in 2.2
where is compiles at all.
- Tridge assures me that he no longer opposes 'const religion'
based on the ability to #define const the problem away.
- Changed Get_Pwnam(x,y) into two variants (so that the const
parameter can work correctly): - Get_Pwnam(const x) and
Get_Pwnam_Modify(x).
- Reworked smbd/chgpasswd.c to work with these mods, passing
around a 'struct passwd' rather than the modified username
passdb/
- Kill off disp_info stuff, it isn't used any more - Kill off
support for writing to the old smbpasswd format, it isn't relevent
to Samba 3.0
- Move around and modify the pdb_...() helper functions, adding
one that sets the last changed time to 'now' and that sets the
must change time appropriately.
- Remove the ugly forced update of the LCT- value in
pdb_smbpasswd. - Remove the implicit modification of the ACB
flags when both NT and LM passwords are set.
- Removed substation in pdb_getsampwnam output, as a single
password change will render them inoperable in any case (they
will be substituted and stored)
- Added a default RID to the init_sam_from_pw() function, based on
our rid algorithm.
- Added checks that an smbpasswd stored user has a uid-based RID.
- Fail to store tdb based users without a RID
lib/
- Change the substituion code to use global_myname if there is
no connection (and therefore no called name) at the present time.
(This used to be commit 8f607810eb24ed1157bbd2e896c2c167bc34d986)
2001-10-29 10:24:49 +03:00
break ;
2002-07-15 14:35:28 +04:00
case ' M ' :
string_sub ( p , " %M " , client_name ( ) , l ) ;
break ;
case ' R ' :
string_sub ( p , " %R " , remote_proto , l ) ;
break ;
case ' T ' :
string_sub ( p , " %T " , timestring ( False ) , l ) ;
break ;
case ' a ' :
string_sub ( p , " %a " , remote_arch , l ) ;
break ;
2000-04-19 08:01:16 +04:00
case ' d ' :
2001-04-09 00:22:39 +04:00
slprintf ( pidstr , sizeof ( pidstr ) - 1 , " %d " , ( int ) sys_getpid ( ) ) ;
2000-04-19 08:01:16 +04:00
string_sub ( p , " %d " , pidstr , l ) ;
break ;
2002-07-15 14:35:28 +04:00
case ' h ' :
string_sub ( p , " %h " , myhostname ( ) , l ) ;
break ;
case ' m ' :
2002-08-17 21:00:51 +04:00
string_sub ( p , " %m " , get_remote_machine_name ( ) , l ) ;
2002-07-15 14:35:28 +04:00
break ;
case ' v ' :
2003-08-20 21:13:38 +04:00
string_sub ( p , " %v " , SAMBA_VERSION_STRING , l ) ;
2002-07-15 14:35:28 +04:00
break ;
case ' $ ' :
p + = expand_env_var ( p , l ) ;
break ; /* Expand environment variables */
2000-04-23 12:30:37 +04:00
case ' \0 ' :
p + + ;
break ; /* don't run off the end of the string */
default : p + = 2 ;
break ;
}
}
}
2002-07-15 14:35:28 +04:00
static void standard_sub_advanced ( int snum , const char * user ,
const char * connectpath , gid_t gid ,
const char * smb_name , char * str , size_t len )
2000-04-23 12:30:37 +04:00
{
char * p , * s , * home ;
2001-07-04 11:36:09 +04:00
for ( s = str ; ( p = strchr_m ( s , ' % ' ) ) ; s = p ) {
2002-07-15 14:35:28 +04:00
int l = ( int ) len - ( int ) ( p - str ) ;
if ( l < 0 )
l = 0 ;
2000-04-23 12:30:37 +04:00
switch ( * ( p + 1 ) ) {
2002-07-15 14:35:28 +04:00
case ' N ' :
string_sub ( p , " %N " , automount_server ( user ) , l ) ;
break ;
2000-04-17 08:46:10 +04:00
case ' H ' :
2002-07-15 14:35:28 +04:00
if ( ( home = get_user_home_dir ( user ) ) )
2000-04-17 08:46:10 +04:00
string_sub ( p , " %H " , home , l ) ;
2002-07-15 14:35:28 +04:00
else
2000-04-17 08:46:10 +04:00
p + = 2 ;
break ;
case ' P ' :
string_sub ( p , " %P " , connectpath , l ) ;
break ;
case ' S ' :
string_sub ( p , " %S " , lp_servicename ( snum ) , l ) ;
break ;
case ' g ' :
string_sub ( p , " %g " , gidtoname ( gid ) , l ) ;
break ;
case ' u ' :
string_sub ( p , " %u " , user , l ) ;
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
* username . The % p ( NIS server path ) code is
* here as it is used instead of the default
* " path = " string in [ homes ] and so needs the
* service name , not the username . */
case ' p ' :
string_sub ( p , " %p " , automount_path ( lp_servicename ( snum ) ) , l ) ;
break ;
case ' \0 ' :
p + + ;
break ; /* don't run off the end of the string */
default : p + = 2 ;
break ;
}
}
2000-04-23 12:30:37 +04:00
2002-07-15 14:35:28 +04:00
standard_sub_basic ( smb_name , str , len ) ;
2000-04-17 08:46:10 +04:00
}
/****************************************************************************
Do some standard substitutions in a string .
2002-07-15 14:35:28 +04:00
This function will return an allocated string that have to be freed .
2000-04-17 08:46:10 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-01-17 02:32:10 +03:00
2002-07-15 14:35:28 +04:00
char * talloc_sub_basic ( TALLOC_CTX * mem_ctx , const char * smb_name , const char * str )
2000-04-17 08:46:10 +04:00
{
2002-07-15 14:35:28 +04:00
char * a , * t ;
a = alloc_sub_basic ( smb_name , str ) ;
if ( ! a ) return NULL ;
t = talloc_strdup ( mem_ctx , a ) ;
SAFE_FREE ( a ) ;
return t ;
}
char * alloc_sub_basic ( const char * smb_name , const char * str )
{
char * b , * p , * s , * t , * r , * a_string ;
fstring pidstr ;
struct passwd * pass ;
2002-08-17 21:00:51 +04:00
const char * local_machine_name = get_local_machine_name ( ) ;
2002-07-15 14:35:28 +04:00
a_string = strdup ( str ) ;
if ( a_string = = NULL ) {
DEBUG ( 0 , ( " alloc_sub_specified: Out of memory! \n " ) ) ;
return NULL ;
}
for ( b = s = a_string ; ( p = strchr_m ( s , ' % ' ) ) ; s = a_string + ( p - b ) ) {
r = NULL ;
b = t = a_string ;
switch ( * ( p + 1 ) ) {
case ' U ' :
r = strdup_lower ( smb_name ) ;
if ( r = = NULL ) goto error ;
t = realloc_string_sub ( t , " %U " , r ) ;
break ;
case ' G ' :
r = strdup ( smb_name ) ;
if ( r = = NULL ) goto error ;
if ( ( pass = Get_Pwnam ( r ) ) ! = NULL ) {
t = realloc_string_sub ( t , " %G " , gidtoname ( pass - > pw_gid ) ) ;
}
break ;
case ' D ' :
r = strdup_upper ( current_user_info . domain ) ;
if ( r = = NULL ) goto error ;
t = realloc_string_sub ( t , " %D " , r ) ;
break ;
case ' I ' :
t = realloc_string_sub ( t , " %I " , client_addr ( ) ) ;
break ;
case ' L ' :
2002-08-17 21:00:51 +04:00
if ( local_machine_name & & * local_machine_name )
t = realloc_string_sub ( t , " %L " , local_machine_name ) ;
2002-07-15 14:35:28 +04:00
else
2002-11-13 02:20:50 +03:00
t = realloc_string_sub ( t , " %L " , global_myname ( ) ) ;
2002-07-15 14:35:28 +04:00
break ;
2003-11-05 07:34:31 +03:00
case ' N ' :
t = realloc_string_sub ( t , " %N " , automount_server ( smb_name ) ) ;
break ;
2002-07-15 14:35:28 +04:00
case ' M ' :
t = realloc_string_sub ( t , " %M " , client_name ( ) ) ;
break ;
case ' R ' :
t = realloc_string_sub ( t , " %R " , remote_proto ) ;
break ;
case ' T ' :
t = realloc_string_sub ( t , " %T " , timestring ( False ) ) ;
break ;
case ' a ' :
t = realloc_string_sub ( t , " %a " , remote_arch ) ;
break ;
case ' d ' :
slprintf ( pidstr , sizeof ( pidstr ) - 1 , " %d " , ( int ) sys_getpid ( ) ) ;
t = realloc_string_sub ( t , " %d " , pidstr ) ;
break ;
case ' h ' :
t = realloc_string_sub ( t , " %h " , myhostname ( ) ) ;
break ;
case ' m ' :
t = realloc_string_sub ( t , " %m " , remote_machine ) ;
break ;
case ' v ' :
2003-08-20 21:13:38 +04:00
t = realloc_string_sub ( t , " %v " , SAMBA_VERSION_STRING ) ;
2002-07-15 14:35:28 +04:00
break ;
case ' $ ' :
t = realloc_expand_env_var ( t , p ) ; /* Expand environment variables */
break ;
default :
break ;
}
p + + ;
SAFE_FREE ( r ) ;
if ( t = = NULL ) goto error ;
a_string = t ;
}
return a_string ;
error :
SAFE_FREE ( a_string ) ;
return NULL ;
2000-04-17 08:46:10 +04:00
}
/****************************************************************************
2002-07-15 14:35:28 +04:00
Do some specific substitutions in a string .
This function will return an allocated string that have to be freed .
2000-04-17 08:46:10 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-01-17 02:32:10 +03:00
2002-07-15 14:35:28 +04:00
char * talloc_sub_specified ( TALLOC_CTX * mem_ctx ,
const char * input_string ,
const char * username ,
const char * domain ,
uid_t uid ,
gid_t gid )
2002-01-17 02:32:10 +03:00
{
2002-07-15 14:35:28 +04:00
char * a , * t ;
a = alloc_sub_specified ( input_string , username , domain , uid , gid ) ;
if ( ! a ) return NULL ;
t = talloc_strdup ( mem_ctx , a ) ;
SAFE_FREE ( a ) ;
return t ;
}
2002-01-17 02:32:10 +03:00
2002-07-15 14:35:28 +04:00
char * alloc_sub_specified ( const char * input_string ,
const char * username ,
const char * domain ,
uid_t uid ,
gid_t gid )
{
char * a_string , * ret_string ;
char * b , * p , * s , * t ;
a_string = strdup ( input_string ) ;
if ( a_string = = NULL ) {
DEBUG ( 0 , ( " alloc_sub_specified: Out of memory! \n " ) ) ;
return NULL ;
}
for ( b = s = a_string ; ( p = strchr_m ( s , ' % ' ) ) ; s = a_string + ( p - b ) ) {
b = t = a_string ;
switch ( * ( p + 1 ) ) {
case ' U ' :
t = realloc_string_sub ( t , " %U " , username ) ;
break ;
case ' u ' :
t = realloc_string_sub ( t , " %u " , username ) ;
break ;
case ' G ' :
if ( gid ! = - 1 ) {
t = realloc_string_sub ( t , " %G " , gidtoname ( gid ) ) ;
} else {
t = realloc_string_sub ( t , " %G " , " NO_GROUP " ) ;
}
break ;
case ' g ' :
if ( gid ! = - 1 ) {
t = realloc_string_sub ( t , " %g " , gidtoname ( gid ) ) ;
} else {
t = realloc_string_sub ( t , " %g " , " NO_GROUP " ) ;
}
break ;
case ' D ' :
t = realloc_string_sub ( t , " %D " , domain ) ;
break ;
case ' N ' :
t = realloc_string_sub ( t , " %N " , automount_server ( username ) ) ;
break ;
default :
break ;
}
p + + ;
if ( t = = NULL ) {
SAFE_FREE ( a_string ) ;
return NULL ;
}
a_string = t ;
}
ret_string = alloc_sub_basic ( username , a_string ) ;
SAFE_FREE ( a_string ) ;
return ret_string ;
}
char * talloc_sub_advanced ( TALLOC_CTX * mem_ctx ,
int snum ,
const char * user ,
const char * connectpath ,
gid_t gid ,
const char * smb_name ,
2003-06-17 13:40:35 +04:00
const char * str )
2002-07-15 14:35:28 +04:00
{
char * a , * t ;
a = alloc_sub_advanced ( snum , user , connectpath , gid , smb_name , str ) ;
if ( ! a ) return NULL ;
t = talloc_strdup ( mem_ctx , a ) ;
SAFE_FREE ( a ) ;
return t ;
}
char * alloc_sub_advanced ( int snum , const char * user ,
const char * connectpath , gid_t gid ,
2003-06-17 13:40:35 +04:00
const char * smb_name , const char * str )
2002-07-15 14:35:28 +04:00
{
char * a_string , * ret_string ;
char * b , * p , * s , * t , * h ;
a_string = strdup ( str ) ;
if ( a_string = = NULL ) {
DEBUG ( 0 , ( " alloc_sub_specified: Out of memory! \n " ) ) ;
return NULL ;
}
for ( b = s = a_string ; ( p = strchr_m ( s , ' % ' ) ) ; s = a_string + ( p - b ) ) {
b = t = a_string ;
2002-01-17 02:32:10 +03:00
switch ( * ( p + 1 ) ) {
2002-07-15 14:35:28 +04:00
case ' N ' :
t = realloc_string_sub ( t , " %N " , automount_server ( user ) ) ;
break ;
case ' H ' :
if ( ( h = get_user_home_dir ( user ) ) )
t = realloc_string_sub ( t , " %H " , h ) ;
break ;
case ' P ' :
t = realloc_string_sub ( t , " %P " , connectpath ) ;
break ;
2002-01-17 02:32:10 +03:00
case ' S ' :
2002-07-15 14:35:28 +04:00
t = realloc_string_sub ( t , " %S " , lp_servicename ( snum ) ) ;
break ;
case ' g ' :
t = realloc_string_sub ( t , " %g " , gidtoname ( gid ) ) ;
break ;
case ' u ' :
t = realloc_string_sub ( t , " %u " , user ) ;
2002-01-17 02:32:10 +03:00
break ;
2002-07-15 14:35:28 +04:00
/* 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
* username . The % p ( NIS server path ) code is
* here as it is used instead of the default
* " path = " string in [ homes ] and so needs the
* service name , not the username . */
2002-01-17 02:32:10 +03:00
case ' p ' :
2002-07-15 14:35:28 +04:00
t = realloc_string_sub ( t , " %p " , automount_path ( lp_servicename ( snum ) ) ) ;
2002-01-17 02:32:10 +03:00
break ;
2002-07-15 14:35:28 +04:00
default :
2002-01-17 02:32:10 +03:00
break ;
}
2002-07-15 14:35:28 +04:00
p + + ;
if ( t = = NULL ) {
SAFE_FREE ( a_string ) ;
return NULL ;
}
a_string = t ;
2002-01-17 02:32:10 +03:00
}
2002-07-15 14:35:28 +04:00
ret_string = alloc_sub_basic ( smb_name , a_string ) ;
SAFE_FREE ( a_string ) ;
return ret_string ;
}
/****************************************************************************
Do some standard substitutions in a string .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void standard_sub_conn ( connection_struct * conn , char * str , size_t len )
{
standard_sub_advanced ( SNUM ( conn ) , conn - > user , conn - > connectpath ,
2002-11-20 03:53:24 +03:00
conn - > gid , smb_user_name , str , len ) ;
2002-01-17 02:32:10 +03:00
}
2003-06-17 13:40:35 +04:00
char * talloc_sub_conn ( TALLOC_CTX * mem_ctx , connection_struct * conn , const char * str )
2002-09-25 19:19:00 +04:00
{
return talloc_sub_advanced ( mem_ctx , SNUM ( conn ) , conn - > user ,
conn - > connectpath , conn - > gid ,
2002-11-20 03:53:24 +03:00
smb_user_name , str ) ;
2002-09-25 19:19:00 +04:00
}
2003-06-17 13:40:35 +04:00
char * alloc_sub_conn ( connection_struct * conn , const char * str )
2002-09-25 19:19:00 +04:00
{
return alloc_sub_advanced ( SNUM ( conn ) , conn - > user , conn - > connectpath ,
2002-11-20 03:53:24 +03:00
conn - > gid , smb_user_name , str ) ;
2002-09-25 19:19:00 +04:00
}
2002-01-17 02:32:10 +03:00
/****************************************************************************
Like standard_sub but by snum .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-07-15 14:35:28 +04:00
void standard_sub_snum ( int snum , char * str , size_t len )
2000-04-17 08:46:10 +04:00
{
2000-08-25 07:58:02 +04:00
extern struct current_user current_user ;
static uid_t cached_uid = - 1 ;
static fstring cached_user ;
/* calling uidtoname() on every substitute would be too expensive, so
we cache the result here as nearly every call is for the same uid */
if ( cached_uid ! = current_user . uid ) {
fstrcpy ( cached_user , uidtoname ( current_user . uid ) ) ;
cached_uid = current_user . uid ;
}
2002-07-15 14:35:28 +04:00
standard_sub_advanced ( snum , cached_user , " " , - 1 ,
2002-11-20 03:53:24 +03:00
smb_user_name , str , len ) ;
2000-04-19 08:01:16 +04:00
}