2006-03-20 03:28:12 +03:00
/*
Unix SMB / CIFS implementation .
Samba utility functions
Copyright ( C ) Andrew Tridgell 1992 - 1998
Copyright ( C ) Jeremy Allison 2001 - 2002
Copyright ( C ) Simo Sorce 2001
Copyright ( C ) Jim McDonough ( jmcd @ us . ibm . com ) 2003.
Copyright ( C ) James J Myers 2003
2007-08-31 03:15:12 +04:00
Copyright ( C ) Jelmer Vernooij 2005 - 2007
2006-03-20 03:28:12 +03:00
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
2007-07-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2006-03-20 03:28:12 +03:00
( 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
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2006-03-20 03:28:12 +03:00
*/
# include "includes.h"
2008-05-27 16:36:28 +04:00
# include "dynconfig/dynconfig.h"
2006-03-20 03:28:12 +03:00
# include "system/network.h"
# include "system/filesys.h"
2007-08-31 03:15:12 +04:00
# include "system/dir.h"
2007-09-08 16:42:09 +04:00
# include "param/param.h"
2006-03-20 03:28:12 +03:00
/**
* @ file
* @ brief Misc utility functions
*/
2010-07-16 08:32:42 +04:00
bool lpcfg_is_mydomain ( struct loadparm_context * lp_ctx ,
2007-10-01 22:52:55 +04:00
const char * domain )
{
2010-07-16 08:32:42 +04:00
return strequal ( lpcfg_workgroup ( lp_ctx ) , domain ) ;
2007-10-01 22:52:55 +04:00
}
2010-07-16 08:32:42 +04:00
bool lpcfg_is_my_domain_or_realm ( struct loadparm_context * lp_ctx ,
2009-05-26 06:31:39 +04:00
const char * domain )
{
2010-07-16 08:32:42 +04:00
return strequal ( lpcfg_workgroup ( lp_ctx ) , domain ) | |
strequal ( lpcfg_realm ( lp_ctx ) , domain ) ;
2009-05-26 06:31:39 +04:00
}
2006-03-20 03:28:12 +03:00
/**
see if a string matches either our primary or one of our secondary
netbios aliases . do a case insensitive match
*/
2010-07-16 08:32:42 +04:00
bool lpcfg_is_myname ( struct loadparm_context * lp_ctx , const char * name )
2006-03-20 03:28:12 +03:00
{
const char * * aliases ;
int i ;
2011-10-09 16:24:32 +04:00
if ( strcasecmp_m ( name , lpcfg_netbios_name ( lp_ctx ) ) = = 0 ) {
2007-10-07 01:39:52 +04:00
return true ;
2006-03-20 03:28:12 +03:00
}
2010-07-16 08:32:42 +04:00
aliases = lpcfg_netbios_aliases ( lp_ctx ) ;
2006-03-20 03:28:12 +03:00
for ( i = 0 ; aliases & & aliases [ i ] ; i + + ) {
2011-10-09 16:24:32 +04:00
if ( strcasecmp_m ( name , aliases [ i ] ) = = 0 ) {
2007-10-07 01:39:52 +04:00
return true ;
2006-03-20 03:28:12 +03:00
}
}
2007-10-07 01:39:52 +04:00
return false ;
2006-03-20 03:28:12 +03:00
}
2012-12-14 20:54:13 +04:00
static char * lpcfg_common_path ( TALLOC_CTX * mem_ctx ,
2012-12-27 19:09:39 +04:00
const char * parent ,
2012-12-14 20:54:13 +04:00
const char * name )
2006-03-20 03:28:12 +03:00
{
char * fname , * dname ;
2012-12-14 20:54:13 +04:00
bool ok ;
2006-03-20 03:28:12 +03:00
if ( name = = NULL ) {
return NULL ;
}
if ( name [ 0 ] = = 0 | | name [ 0 ] = = ' / ' | | strstr ( name , " :/ " ) ) {
return talloc_strdup ( mem_ctx , name ) ;
}
2012-12-27 19:09:39 +04:00
dname = talloc_strdup ( mem_ctx , parent ) ;
2012-12-14 20:54:13 +04:00
if ( dname = = NULL ) {
return NULL ;
}
2006-03-20 03:28:12 +03:00
trim_string ( dname , " " , " / " ) ;
2012-12-14 20:54:13 +04:00
ok = directory_create_or_exist ( dname , geteuid ( ) , 0755 ) ;
if ( ! ok ) {
DEBUG ( 1 , ( " Unable to create directory %s for file %s. "
" Error was %s \n " , dname , name , strerror ( errno ) ) ) ;
return NULL ;
2006-03-20 03:28:12 +03:00
}
2012-12-14 20:54:13 +04:00
fname = talloc_asprintf ( mem_ctx , " %s/%s " , dname , name ) ;
if ( fname = = NULL ) {
return dname ;
}
2006-03-20 03:28:12 +03:00
talloc_free ( dname ) ;
return fname ;
}
2012-12-14 20:54:13 +04:00
/**
A useful function for returning a path in the Samba lock directory .
* */
char * lpcfg_lock_path ( TALLOC_CTX * mem_ctx , struct loadparm_context * lp_ctx ,
const char * name )
{
2014-02-03 06:46:08 +04:00
return lpcfg_common_path ( mem_ctx , lpcfg_lock_directory ( lp_ctx ) , name ) ;
2012-12-14 20:54:13 +04:00
}
2011-07-12 15:04:08 +04:00
/**
A useful function for returning a path in the Samba state directory .
* */
char * lpcfg_state_path ( TALLOC_CTX * mem_ctx , struct loadparm_context * lp_ctx ,
const char * name )
{
2014-01-15 06:59:54 +04:00
return lpcfg_common_path ( mem_ctx , lpcfg_state_directory ( lp_ctx ) , name ) ;
2011-07-12 15:04:08 +04:00
}
/**
A useful function for returning a path in the Samba cache directory .
* */
char * lpcfg_cache_path ( TALLOC_CTX * mem_ctx , struct loadparm_context * lp_ctx ,
const char * name )
{
2014-01-15 06:59:54 +04:00
return lpcfg_common_path ( mem_ctx , lpcfg_cache_directory ( lp_ctx ) , name ) ;
2011-07-12 15:04:08 +04:00
}
2007-01-24 05:48:40 +03:00
/**
* @ brief Returns an absolute path to a file in the directory containing the current config file
*
* @ param name File to find , relative to the config file directory .
*
* @ retval Pointer to a talloc ' ed string containing the full path .
* */
2011-04-29 06:46:10 +04:00
char * lpcfg_config_path ( TALLOC_CTX * mem_ctx , struct loadparm_context * lp_ctx ,
2007-10-01 22:52:55 +04:00
const char * name )
2007-01-24 05:48:40 +03:00
{
char * fname , * config_dir , * p ;
2010-07-16 08:32:42 +04:00
config_dir = talloc_strdup ( mem_ctx , lpcfg_configfile ( lp_ctx ) ) ;
2007-12-14 03:46:25 +03:00
if ( config_dir = = NULL ) {
2009-03-04 05:58:07 +03:00
config_dir = talloc_strdup ( mem_ctx , lp_default_path ( ) ) ;
2007-12-14 03:46:25 +03:00
}
2007-01-24 05:48:40 +03:00
p = strrchr ( config_dir , ' / ' ) ;
2007-12-10 06:33:29 +03:00
if ( p = = NULL ) {
2009-09-02 15:51:02 +04:00
talloc_free ( config_dir ) ;
config_dir = talloc_strdup ( mem_ctx , " . " ) ;
if ( config_dir = = NULL ) {
return NULL ;
}
} else {
p [ 0 ] = ' \0 ' ;
2007-01-24 05:48:40 +03:00
}
fname = talloc_asprintf ( mem_ctx , " %s/%s " , config_dir , name ) ;
talloc_free ( config_dir ) ;
return fname ;
}
2006-03-20 03:28:12 +03:00
/**
* @ brief Returns an absolute path to a file in the Samba private directory .
*
* @ param name File to find , relative to PRIVATEDIR .
* if name is not relative , then use it as - is
*
* @ retval Pointer to a talloc ' ed string containing the full path .
* */
2011-04-29 06:47:11 +04:00
char * lpcfg_private_path ( TALLOC_CTX * mem_ctx ,
2007-10-01 22:52:55 +04:00
struct loadparm_context * lp_ctx ,
const char * name )
2006-03-20 03:28:12 +03:00
{
char * fname ;
if ( name = = NULL ) {
return NULL ;
}
if ( name [ 0 ] = = 0 | | name [ 0 ] = = ' / ' | | strstr ( name , " :/ " ) ) {
return talloc_strdup ( mem_ctx , name ) ;
}
2010-07-16 08:32:42 +04:00
fname = talloc_asprintf ( mem_ctx , " %s/%s " , lpcfg_private_dir ( lp_ctx ) , name ) ;
2006-03-20 03:28:12 +03:00
return fname ;
}
2013-04-11 11:42:10 +04:00
/**
* @ brief Returns an absolute path to a NTDB or TDB file in the Samba
* private directory .
*
* @ param name File to find , relative to PRIVATEDIR , without . ( n ) tdb extension .
* Only provide fixed - string names which are supposed to change with " use ntdb "
* option .
*
* @ retval Pointer to a talloc ' ed string containing the full path , for
* use with dbwrap_local_open ( ) .
* */
char * lpcfg_private_db_path ( TALLOC_CTX * mem_ctx ,
struct loadparm_context * lp_ctx ,
const char * name )
{
const char * extension = " .tdb " ;
if ( lpcfg_use_ntdb ( lp_ctx ) ) {
extension = " .ntdb " ;
}
return talloc_asprintf ( mem_ctx , " %s/%s%s " ,
lpcfg_private_dir ( lp_ctx ) , name , extension ) ;
}
2006-03-20 03:28:12 +03:00
/**
return a path in the smbd . tmp directory , where all temporary file
for smbd go . If NULL is passed for name then return the directory
path itself
*/
2008-04-02 06:53:27 +04:00
char * smbd_tmp_path ( TALLOC_CTX * mem_ctx ,
2010-07-16 08:32:42 +04:00
struct loadparm_context * lp_ctx ,
2007-10-01 22:52:55 +04:00
const char * name )
2006-03-20 03:28:12 +03:00
{
char * fname , * dname ;
2012-12-14 20:54:13 +04:00
bool ok ;
2006-03-20 03:28:12 +03:00
2011-04-29 06:47:11 +04:00
dname = lpcfg_private_path ( mem_ctx , lp_ctx , " smbd.tmp " ) ;
2012-12-14 20:54:13 +04:00
if ( dname = = NULL ) {
return NULL ;
}
ok = directory_create_or_exist ( dname , geteuid ( ) , 0755 ) ;
if ( ! ok ) {
return NULL ;
2006-03-20 03:28:12 +03:00
}
if ( name = = NULL ) {
return dname ;
}
fname = talloc_asprintf ( mem_ctx , " %s/%s " , dname , name ) ;
2012-12-14 20:54:13 +04:00
if ( fname = = NULL ) {
return dname ;
}
2006-03-20 03:28:12 +03:00
talloc_free ( dname ) ;
return fname ;
}
2011-05-03 04:40:33 +04:00
const char * lpcfg_imessaging_path ( TALLOC_CTX * mem_ctx ,
2007-10-01 22:52:55 +04:00
struct loadparm_context * lp_ctx )
{
2010-10-15 10:16:46 +04:00
return smbd_tmp_path ( mem_ctx , lp_ctx , " msg " ) ;
2007-10-01 22:52:55 +04:00
}
2006-03-20 03:28:12 +03:00
2011-03-25 00:37:00 +03:00
struct smb_iconv_handle * smb_iconv_handle_reinit_lp ( TALLOC_CTX * mem_ctx ,
2010-03-18 06:06:13 +03:00
struct loadparm_context * lp_ctx ,
2011-03-25 00:37:00 +03:00
struct smb_iconv_handle * old_ic )
2007-12-06 19:16:40 +03:00
{
2011-03-25 00:37:00 +03:00
return smb_iconv_handle_reinit ( mem_ctx , lpcfg_dos_charset ( lp_ctx ) ,
2011-09-20 06:13:21 +04:00
lpcfg_unix_charset ( lp_ctx ) ,
true ,
old_ic ) ;
2007-12-06 19:16:40 +03:00
}
2007-12-14 00:46:09 +03:00
2010-07-16 08:32:42 +04:00
const char * lpcfg_sam_name ( struct loadparm_context * lp_ctx )
2009-05-26 06:31:39 +04:00
{
2010-07-16 08:32:42 +04:00
switch ( lpcfg_server_role ( lp_ctx ) ) {
2011-06-06 05:38:04 +04:00
case ROLE_DOMAIN_BDC :
case ROLE_DOMAIN_PDC :
2012-06-10 16:08:20 +04:00
case ROLE_ACTIVE_DIRECTORY_DC :
2010-07-16 08:32:42 +04:00
return lpcfg_workgroup ( lp_ctx ) ;
2009-05-26 06:31:39 +04:00
default :
2010-07-16 08:32:42 +04:00
return lpcfg_netbios_name ( lp_ctx ) ;
2009-05-26 06:31:39 +04:00
}
}
2012-04-19 19:16:03 +04:00
void lpcfg_default_kdc_policy ( struct loadparm_context * lp_ctx ,
time_t * svc_tkt_lifetime ,
time_t * usr_tkt_lifetime ,
time_t * renewal_lifetime )
{
long val ;
val = lpcfg_parm_long ( lp_ctx , NULL ,
" kdc " , " service ticket lifetime " , 10 ) ;
* svc_tkt_lifetime = val * 60 * 60 ;
val = lpcfg_parm_long ( lp_ctx , NULL ,
" kdc " , " user ticket lifetime " , 10 ) ;
* usr_tkt_lifetime = val * 60 * 60 ;
val = lpcfg_parm_long ( lp_ctx , NULL ,
" kdc " , " renewal lifetime " , 24 * 7 ) ;
* renewal_lifetime = val * 60 * 60 ;
}