8 #define SCARAB_API_EXPORTS 15 #include <boost/filesystem.hpp> 18 #include <sys/types.h> 32 LOGGER( mtlog,
"authentication" );
34 authentication::authentication(
const string& a_auth_filename,
bool a_is_in_user_home ) :
36 f_auth_filename( a_auth_filename ),
39 load( a_auth_filename, a_is_in_user_home );
50 f_auth_filename = a_auth_file;
52 path t_auth_file_path( a_auth_file );
54 if( a_is_in_user_home )
57 const size_t t_uname_bufsize = 1024;
58 char t_username_buf[ t_uname_bufsize ];
61 passwd* t_passwd = getpwuid( getuid() );
62 if( t_passwd !=
nullptr )
64 strcpy( t_username_buf, t_passwd->pw_name );
68 LERROR( mtlog,
"Error reported while getting passwd info: " << strerror( errno ) );
70 DWORD t_bufsize_win = t_uname_bufsize;
71 if( ! GetUserName( t_username_buf, &t_bufsize_win ) )
74 LERROR( mtlog,
"Unable to get the username; authentications not loaded" );
78 string t_username = string( t_username_buf );
82 long t_pwd_bufsize = sysconf( _SC_GETPW_R_SIZE_MAX );
83 if ( t_pwd_bufsize == -1 ) t_pwd_bufsize = 16384;
85 char* t_pwd_buf = (
char* )malloc( (
size_t)t_pwd_bufsize );
86 if( t_pwd_buf == NULL )
88 LERROR( mtlog,
"Unable to allocate the pwd buffer; authentications not loaded");
94 struct passwd* t_pwd_result;
95 getpwnam_r( t_username.c_str(), &t_pwd, t_pwd_buf, t_pwd_bufsize, &t_pwd_result );
96 if( t_pwd_result == NULL )
98 LERROR( mtlog,
"Unable to get the pwd data; authentications not loaded" );
103 t_auth_file_path = string( t_pwd.pw_dir ) / f_auth_filename;
107 t_auth_file_path =
path( getenv(
"HOMEDRIVE" ) ) /
path( getenv(
"HOMEPATH" ) ) / f_auth_filename;
112 bool t_auth_file_present =
false;
113 if( ! t_auth_file_path.empty() )
115 LDEBUG( mtlog,
"Looking for authentication file: <" << t_auth_file_path <<
">" );
118 t_auth_file_present = exists( t_auth_file_path ) && is_regular_file( t_auth_file_path );
119 if( ! t_auth_file_present )
121 LERROR( mtlog,
"File either doesn't exist (" << exists( t_auth_file_path ) <<
") or isn't a regular file (" << is_regular_file( t_auth_file_path ) <<
")" );
125 catch( filesystem_error& e )
127 LERROR( mtlog,
"Unable to determine if the authentication file is a regular file: " << e.what() );
133 if( t_auth_file_present )
136 param* t_read_file = t_translator.
read_file( t_auth_file_path.string() );
137 if( t_read_file == NULL )
139 LERROR( mtlog,
"Unable to parse authentication file" );
142 else if( ! t_read_file->
is_node() )
144 LERROR( mtlog,
"Authentication file must translate to a node" );
151 LDEBUG( mtlog,
"Authentications:\n" << *
this );
158 LWARN( mtlog,
"Authentications not loaded" );
virtual bool is_node() const
Contains the logger class and macros, based on Kasper's KLogger class.
param_node & operator=(const param_node &rhs)
bool load(const std::string &a_auth_file, bool a_is_in_user_home=true)
LOGGER(mtlog,"authentication")
param * read_file(const std::string &a_filename, const param_node *a_options=nullptr)