Documentation

Generated on Thu Aug 31 00:02:37 2006

 

Connection Class Reference

#include <Connection.h>

Inheritance diagram for Connection:

Inheritance graph
[legend]
List of all members.

Public Member Functions

ssize_t send (const char *, size_t)
ssize_t receive (char *, size_t)
Connectionoperator<< (long long int num)
Connectionoperator<< (string message)
bool engage_tls ()
bool disengage_tls ()
bool verify_cert ()
bitset< TOTAL_U_BITsettings () const
bool settings (int bit)
void settings (int bit, int way)
pthread_t thread_id (void)
void thread_id (pthread_t var)
string host_name (void)
void host_name (string var)
int socket_id (void)
void socket_id (int var)
sockaddr_in addr (void)
void addr (struct sockaddr_in var)
size_t packet_size (void)
void packet_size (size_t var)
timeval timeout (void)
void timeout (struct timeval var)
string user_name (void)
void user_name (string var)
unsigned long long transfered (void)
void transfered (unsigned long long var)

Public Attributes

pthread_rwlock_t lock

Protected Member Functions

 Connection ()
 ~Connection ()

Protected Attributes

bitset< TOTAL_U_BIT_settings

Private Attributes

pthread_t _thread_id
string _host_name
int _socket_id
sockaddr_in _addr
size_t _packet_size
timeval _timeout
string _user_name
unsigned long long _transfered
gnutls_session session

Detailed Description

Definition at line 55 of file Connection.h.


Constructor & Destructor Documentation

Connection::Connection (  )  [protected]

Definition at line 16 of file Connection.cpp.

References _packet_size, _settings, _transfered, and READ_SIZE.

00017 {
00018   pthread_rwlock_init(&this->lock, NULL);
00019   this->_settings.reset();
00020   this->_packet_size = READ_SIZE;
00021   this->_transfered = 0;
00022 }

Connection::~Connection (  )  [protected]

Definition at line 24 of file Connection.cpp.

References cleanup_list, cleanup_list_lock, and TLS_INITED.

00025 {
00026   if(this->_socket_id)
00027     close(this->_socket_id);
00028 
00029   pthread_rwlock_destroy(&this->lock);
00030 
00031 #ifdef USE_TLS
00032   // TLS cleanup here.
00033   if(this->settings(TLS_INITED))
00034   {
00035     gnutls_db_remove_session(this->session);
00036     gnutls_deinit(this->session);
00037   }
00038 #endif
00039   if(this->thread_id())
00040   {
00041     sem_wait(&cleanup_list_lock);
00042     cleanup_list.push_back(this->thread_id());
00043     sem_post(&cleanup_list_lock);
00044   }
00045 }


Member Function Documentation

void Connection::addr ( struct sockaddr_in  var  )  [inline]

Definition at line 98 of file Connection.h.

00103 :
#ifdef USE_TLS

struct sockaddr_in Connection::addr ( void   )  [inline]

Definition at line 98 of file Connection.h.

Referenced by Data::Data(), data_wrapper(), Data::open_active(), Data::open_passive(), and User::User().

00103 :
#ifdef USE_TLS

bool Connection::disengage_tls (  ) 

Definition at line 138 of file Connection.cpp.

References B_RESET, settings(), and TLS_ENABLED.

00139 {
00140   gnutls_bye(this->session, GNUTLS_SHUT_RDWR);
00141   this->settings(TLS_ENABLED, B_RESET);
00142   return true;
00143 }

Here is the call graph for this function:

bool Connection::engage_tls (  ) 

Definition at line 104 of file Connection.cpp.

References B_SET, packet_size(), session, settings(), tls_db_delete(), tls_db_fetch(), tls_db_store(), TLS_ENABLED, TLS_INITED, and x509_cred.

Referenced by data_wrapper(), and Commands::list().

00105 {
00106   bool ret = false;
00107   gnutls_init(&session, GNUTLS_SERVER);
00108 
00109   gnutls_set_default_priority(this->session);
00110 
00111   gnutls_credentials_set(this->session, GNUTLS_CRD_CERTIFICATE, x509_cred);
00112 
00113   gnutls_certificate_server_set_request(this->session, GNUTLS_CERT_REQUEST);
00114 
00115   gnutls_dh_set_prime_bits(this->session, 1024);
00116 
00117   // these are for the connection cache
00118   gnutls_db_set_retrieve_function(this->session, tls_db_fetch);
00119   gnutls_db_set_remove_function(this->session, tls_db_delete);
00120   gnutls_db_set_store_function(this->session, tls_db_store);
00121   gnutls_db_set_ptr(this->session, NULL);
00122 
00123   // associating the session with the socket.
00124   gnutls_transport_set_ptr(this->session,
00125       reinterpret_cast<gnutls_transport_ptr>(this->socket_id()));
00126   this->settings(TLS_INITED, B_SET);
00127 
00128   if(gnutls_handshake(this->session) >= 0)
00129   {
00130     this->settings(TLS_ENABLED, B_SET);
00131     this->packet_size(gnutls_record_get_max_size(this->session));
00132     ret = true;
00133   }
00134 
00135   return ret;
00136 }

Here is the call graph for this function:

void Connection::host_name ( string  var  )  [inline]

Definition at line 96 of file Connection.h.

00103 :
#ifdef USE_TLS

string Connection::host_name ( void   )  [inline]

Definition at line 96 of file Connection.h.

Referenced by data_wrapper(), User::User(), and verify_cert().

00103 :
#ifdef USE_TLS

Connection & Connection::operator<< ( string  message  ) 

Definition at line 96 of file Connection.cpp.

References B_SET, FAILURE, and settings().

00097 {
00098   if(this->send(message.c_str(), message.size()) < 0)
00099   this->settings(FAILURE, B_SET);
00100   return *this;
00101 }

Here is the call graph for this function:

Connection & Connection::operator<< ( long long int  num  ) 

Definition at line 91 of file Connection.cpp.

References Utilities::itos(), and util.

00092 {
00093   return (*this << util.itos(num));
00094 }

Here is the call graph for this function:

void Connection::packet_size ( size_t  var  )  [inline]

Definition at line 99 of file Connection.h.

00103 :
#ifdef USE_TLS

size_t Connection::packet_size ( void   )  [inline]

Definition at line 99 of file Connection.h.

Referenced by data_wrapper(), and engage_tls().

00103 :
#ifdef USE_TLS

ssize_t Connection::receive ( char *  ,
size_t   
)

Definition at line 74 of file Connection.cpp.

References _packet_size, TLS_ENABLED, and transfered().

Referenced by data_wrapper().

00075 {
00076   assert(len <= this->_packet_size && len > 0);
00077   ssize_t ret = -1;
00078 
00079 #ifdef USE_TLS
00080   if(this->settings(TLS_ENABLED))
00081     ret = gnutls_record_recv(this->session, message, len);
00082   else
00083 #endif
00084     ret = read(this->socket_id(), message, len);
00085 
00086   this->transfered(this->transfered() + ret);
00087 
00088   return ret;
00089 }

Here is the call graph for this function:

ssize_t Connection::send ( const char *  ,
size_t   
)

Definition at line 47 of file Connection.cpp.

References _packet_size, Utilities::itos(), Log::log_this(), logging, TLS_ENABLED, transfered(), TYPE_INFO, and util.

Referenced by data_wrapper().

00048 {
00049   assert(len <= this->_packet_size && len > 0);
00050   ssize_t ret = -1;
00051 
00052   BABY_WRLOCK(this->lock);
00053 #ifdef USE_TLS
00054   if(this->settings(TLS_ENABLED))
00055   {
00056     ret = gnutls_record_send(this->session, message, len);
00057     if(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
00058       ret = gnutls_record_send(this->session, message, len);
00059   }
00060   else
00061 #endif // USE_TLS
00062     ret = write(this->_socket_id, message, len);
00063 
00064   BABY_UNLOCK(this->lock);
00065   if(ret < 0)
00066     logging->log_this(3, TYPE_INFO, "write to socket " +
00067         util.itos(this->socket_id()) + " failed: %m");
00068   else
00069     this->transfered(this->transfered() + ret);
00070 
00071   return ret;
00072 }

Here is the call graph for this function:

void Connection::settings ( int  bit,
int  way 
) [inline]

Definition at line 80 of file Connection.h.

References _settings, B_FLIP, B_RESET, and B_SET.

00081     {
00082       BABY_WRLOCK(this->lock);
00083       if(way == B_SET)
00084         this->_settings.set(bit);
00085       else if(way == B_RESET)
00086         this->_settings.reset(bit);
00087       else if(way == B_FLIP)
00088         this->_settings.flip(bit);
00089       BABY_UNLOCK(this->lock);
00090     }

bool Connection::settings ( int  bit  )  [inline]

Definition at line 72 of file Connection.h.

References _settings, and TOTAL_U_BIT.

00073     {
00074       assert(bit < TOTAL_U_BIT);
00075       BABY_RDLOCK(this->lock);
00076       bool b = this->_settings.test(bit);
00077       BABY_UNLOCK(this->lock);
00078       return(b);
00079     }

bitset<TOTAL_U_BIT> Connection::settings (  )  const [inline]

void Connection::socket_id ( int  var  )  [inline]

Definition at line 97 of file Connection.h.

00103 :
#ifdef USE_TLS

int Connection::socket_id ( void   )  [inline]

Definition at line 97 of file Connection.h.

Referenced by Data::open_active(), Data::open_passive(), and User::User().

00103 :
#ifdef USE_TLS

void Connection::thread_id ( pthread_t  var  )  [inline]

Definition at line 95 of file Connection.h.

00103 :
#ifdef USE_TLS

pthread_t Connection::thread_id ( void   )  [inline]

Definition at line 95 of file Connection.h.

Referenced by Data::Data(), data_wrapper(), and User::User().

00103 :
#ifdef USE_TLS

void Connection::timeout ( struct timeval  var  )  [inline]

Definition at line 100 of file Connection.h.

00103 :
#ifdef USE_TLS

struct timeval Connection::timeout ( void   )  [inline]

Definition at line 100 of file Connection.h.

Referenced by Data::Data(), User::login(), Data::open_active(), Data::open_passive(), User::operator>>(), and User::User().

00103 :
#ifdef USE_TLS

void Connection::transfered ( unsigned long long  var  )  [inline]

Definition at line 102 of file Connection.h.

00103 :
#ifdef USE_TLS

unsigned long long Connection::transfered ( void   )  [inline]

Definition at line 102 of file Connection.h.

Referenced by receive(), send(), and Data::~Data().

00103 :
#ifdef USE_TLS

void Connection::user_name ( string  var  )  [inline]

Definition at line 101 of file Connection.h.

00103 :
#ifdef USE_TLS

string Connection::user_name ( void   )  [inline]

Definition at line 101 of file Connection.h.

Referenced by data_wrapper(), verify_cert(), and Data::~Data().

00103 :
#ifdef USE_TLS

bool Connection::verify_cert (  ) 

Definition at line 145 of file Connection.cpp.

References config, Configuration::general, host_name(), Utilities::is_set(), Log::log_this(), logging, TYPE_AUTH, TYPE_DEBUG, user_name(), util, and general_stru::verify_client.

00146 {
00147   bool ret = false;
00148   if (gnutls_certificate_type_get(this->session) == GNUTLS_CRT_X509)
00149   {
00150     int verified = gnutls_certificate_verify_peers(this->session);
00151     if(util.is_set(verified, GNUTLS_CERT_REVOKED))
00152       logging->log_this(2, TYPE_AUTH, this->user_name() +
00153           + " tried to login with a revoked certificate.");
00154     else if(!util.is_set(verified, GNUTLS_CERT_INVALID) &&
00155         !util.is_set(verified, GNUTLS_CERT_SIGNER_NOT_FOUND))
00156     {
00157       unsigned int cert_list_size = 0;
00158       // I really hope gnutls has their locking code in shape. get_peers just
00159       // gives us some memory.. *shudder*                     
00160       const gnutls_datum *cert_list = gnutls_certificate_get_peers(
00161           this->session, &cert_list_size);
00162       if(cert_list_size > 0)
00163       {
00164         gnutls_x509_crt peer_cert;
00165         gnutls_x509_crt_init(&peer_cert);
00166         gnutls_x509_crt_import(peer_cert, &cert_list[0], GNUTLS_X509_FMT_DER);
00167         char dn_buf[200] = {0}, ca_buf[200] = {0};
00168         size_t buf_size = 199, ca_size = 199;
00169         gnutls_x509_crt_get_dn(peer_cert, dn_buf, &buf_size);
00170         gnutls_x509_crt_get_issuer_dn(peer_cert, ca_buf, &ca_size);
00171         if(buf_size > 0 && ca_size > 0)
00172         {
00173           // here we call an external program to ask if this user may login
00174           // with this cert.
00175           //  format is "<user> <host/ip> <ca-dn> <cert-dn>"
00176           string call = config->general.verify_client + " " +
00177             this->user_name() + " " + this->host_name() +
00178             " \"" + ca_buf + "\" \"" + dn_buf + "\"";
00179           logging->log_this(5, TYPE_DEBUG, "running:" + call);
00180           int approved = system(call.c_str());
00181           if(WEXITSTATUS(approved) == 0)
00182             ret = true;
00183         }
00184         gnutls_x509_crt_deinit(peer_cert);
00185       }
00186     }
00187   }
00188   return ret;
00189 }

Here is the call graph for this function:


Member Data Documentation

struct sockaddr_in Connection::_addr [private]

Definition at line 98 of file Connection.h.

string Connection::_host_name [private]

Definition at line 95 of file Connection.h.

size_t Connection::_packet_size [private]

Definition at line 98 of file Connection.h.

Referenced by Connection(), receive(), and send().

bitset<TOTAL_U_BIT> Connection::_settings [protected]

Definition at line 94 of file Connection.h.

Referenced by Connection(), Data::Data(), and settings().

int Connection::_socket_id [private]

Definition at line 96 of file Connection.h.

pthread_t Connection::_thread_id [private]

Definition at line 95 of file Connection.h.

struct timeval Connection::_timeout [private]

Definition at line 100 of file Connection.h.

unsigned long long Connection::_transfered [private]

Definition at line 101 of file Connection.h.

Referenced by Connection().

string Connection::_user_name [private]

Definition at line 100 of file Connection.h.

pthread_rwlock_t Connection::lock

Definition at line 58 of file Connection.h.

Referenced by Commands::retr(), Commands::stor(), and Data::~Data().

gnutls_session Connection::session [private]

Definition at line 102 of file Connection.h.

Referenced by engage_tls().


The documentation for this class was generated from the following files: