Scarab
Project 8 C++ Utility Library
authentication.cc
Go to the documentation of this file.
1 /*
2  * mt_authentication.cc
3  *
4  * Created on: Jun 15, 2015
5  * Author: nsoblath
6  */
7 
8 #define SCARAB_API_EXPORTS
9 
10 #include "authentication.hh"
11 
12 #include "logger.hh"
13 #include "param_codec.hh"
14 
15 #include <boost/filesystem.hpp>
16 
17 #ifndef _WIN32
18 #include <sys/types.h>
19 #include <pwd.h> // for struct passwd
20 #include <unistd.h> // for getpwuid, sysconf
21 #else
22 #include <Windows.h>
23 #endif
24 
25 using namespace boost::filesystem;
26 using std::string;
27 
28 namespace scarab
29 {
30 
31 
32  LOGGER( mtlog, "authentication" );
33 
34  authentication::authentication( const string& a_auth_filename, bool a_is_in_user_home ) :
35  param_node(),
36  f_auth_filename( a_auth_filename ),
37  f_is_loaded( false )
38  {
39  load( a_auth_filename, a_is_in_user_home );
40  }
41 
43  {
44  }
45 
46  bool authentication::load( const string& a_auth_file, bool a_is_in_user_home )
47  {
48  this->clear();
49 
50  f_auth_filename = a_auth_file;
51 
52  path t_auth_file_path( a_auth_file );
53 
54  if( a_is_in_user_home )
55  {
56  // get the username
57  const size_t t_uname_bufsize = 1024;
58  char t_username_buf[ t_uname_bufsize ];
59 #ifndef _WIN32
60  //if( getlogin_r( t_username_buf, t_uname_bufsize ) == 0 )
61  passwd* t_passwd = getpwuid( getuid() );
62  if( t_passwd != nullptr )
63  {
64  strcpy( t_username_buf, t_passwd->pw_name );
65  }
66  else
67  {
68  LERROR( mtlog, "Error reported while getting passwd info: " << strerror( errno ) );
69 #else
70  DWORD t_bufsize_win = t_uname_bufsize;
71  if( ! GetUserName( t_username_buf, &t_bufsize_win ) )
72  {
73 #endif
74  LERROR( mtlog, "Unable to get the username; authentications not loaded" );
75  return false;
76  }
77 
78  string t_username = string( t_username_buf );
79 
80  // get the user home directory
81 #ifndef _WIN32
82  long t_pwd_bufsize = sysconf( _SC_GETPW_R_SIZE_MAX );
83  if ( t_pwd_bufsize == -1 ) t_pwd_bufsize = 16384; // Should be more than enough
84 
85  char* t_pwd_buf = ( char* )malloc( (size_t)t_pwd_bufsize );
86  if( t_pwd_buf == NULL )
87  {
88  LERROR( mtlog, "Unable to allocate the pwd buffer; authentications not loaded");
89  return false;
90  }
91  else
92  {
93  struct passwd t_pwd;
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 )
97  {
98  LERROR( mtlog, "Unable to get the pwd data; authentications not loaded" );
99  return false;
100  }
101  else
102  {
103  t_auth_file_path = string( t_pwd.pw_dir ) / f_auth_filename;
104  }
105  }
106 #else
107  t_auth_file_path = path( getenv( "HOMEDRIVE" ) ) / path( getenv( "HOMEPATH" ) ) / f_auth_filename;
108 #endif
109  }
110 
111  // is the file there?
112  bool t_auth_file_present = false;
113  if( ! t_auth_file_path.empty() )
114  {
115  LDEBUG( mtlog, "Looking for authentication file: <" << t_auth_file_path << ">" );
116  try
117  {
118  t_auth_file_present = exists( t_auth_file_path ) && is_regular_file( t_auth_file_path );
119  if( ! t_auth_file_present )
120  {
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 ) << ")" );
122  return false;
123  }
124  }
125  catch( filesystem_error& e )
126  {
127  LERROR( mtlog, "Unable to determine if the authentication file is a regular file: " << e.what() );
128  return false;
129  }
130  }
131 
132  // if so, load it
133  if( t_auth_file_present )
134  {
135  param_translator t_translator;
136  param* t_read_file = t_translator.read_file( t_auth_file_path.string() );
137  if( t_read_file == NULL )
138  {
139  LERROR( mtlog, "Unable to parse authentication file" );
140  return false;
141  }
142  else if( ! t_read_file->is_node() )
143  {
144  LERROR( mtlog, "Authentication file must translate to a node" );
145  return false;
146  }
147  else
148  {
149  this->param_node::operator=( t_read_file->as_node() );
150  delete t_read_file;
151  LDEBUG( mtlog, "Authentications:\n" << *this );
152  f_is_loaded = true;
153  return true;
154  }
155  }
156  else
157  {
158  LWARN( mtlog, "Authentications not loaded" );
159  return false;
160  }
161  }
162 
163 } /* namespace mantis */
virtual bool is_node() const
Definition: param.hh:420
#define LWARN(...)
Definition: logger.hh:364
fs::path path
Definition: path.hh:25
#define LERROR(...)
Definition: logger.hh:365
Contains the logger class and macros, based on Kasper&#39;s KLogger class.
#define LDEBUG(...)
Definition: logger.hh:360
param_node & operator=(const param_node &rhs)
Definition: param.cc:461
param_node & as_node()
Definition: param.hh:437
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)
Definition: param_codec.cc:48