diff --git a/sld/trunk/src/daemon.cc b/sld/trunk/src/daemon.cc index a3e5faba404972c0e300748254e5f9b25b51e68f..a15d9824720837485ee0ff36ca4775546f33f658 100644 --- a/sld/trunk/src/daemon.cc +++ b/sld/trunk/src/daemon.cc @@ -118,136 +118,25 @@ void SLDaemon::run() { logerr << "!! " << e.message() << "\n"; } } - //delete[] line; } } // recupere les erreurs de reseaux. catch (const Error &e) { - if (e.code() != ERR_NET) + if (e.code() != ERR_NET && e.code() != ERR_AUTH) throw; // disconnect if (c_.connected()) c_.disconnect(); -// if (socket_fs_ != 0) { - if (verbose()) // TODO: Print the time at lost - logerr << "Connexion lost. Retrying in " << options.retrydelay << " seconds.\n"; -// fclose(socket_fs_); -// socket_fs_ = 0; -// } + if (verbose()) // TODO: Print the time at lost + logerr << "Connexion lost. Retrying in " << options.retrydelay << " seconds.\n"; + sleep(options.retrydelay); goto net_connect; } } -//****************************************************************************** -// network functions -//****************************************************************************** - -// void SLDaemon::connect() { -// struct sockaddr_in daddr; -// struct hostent *h; - -// // Check no existing connexion -// assert(socket_fs_ == 0); - -// // retrieve remote host info -// h = gethostbyname(options.server.c_str()); -// if (h == 0) -// throw Error(ERR_NET, hstrerror(h_errno)); - -// // create socket -// socket_fd_ = socket(PF_INET, SOCK_STREAM, 0); -// if (socket_fd_ == -1) -// throw Error(ERR_NET, strerror(errno)); - -// daddr.sin_family = AF_INET; -// daddr.sin_port = htons(options.port); -// daddr.sin_addr = *((struct in_addr *) h->h_addr); -// memset(daddr.sin_zero, '\0', sizeof daddr.sin_zero); - -// if (verbose()) -// logout << "Connecting to " << h->h_name << " (" << inet_ntoa(*(struct in_addr*)h->h_addr) -// << ") on port " << options.port << "...\n"; - -// // connect -// if (::connect(socket_fd_, (struct sockaddr *) &daddr, sizeof daddr) == -1) -// throw Error(ERR_NET, strerror(errno)); - -// if (verbose()) -// logout << "Connected to " << h->h_name << " (" << inet_ntoa(*(struct in_addr*)h->h_addr) -// << ") on port " << options.port << "...\n"; - -// // initialize socket stream -// if ((socket_fs_ = fdopen(socket_fd_, "r+")) == 0) -// throw Error(ERR_NET, strerror(errno)); -// } - -// void SLDaemon::disconnect() { -// if (!c_.connected()) -// return; -// c_.disconnect(); -// // TODO: print close time -// if (verbose()) -// logout << "Connection closed.\n"; -// } - -// void SLDaemon::send(long int i) { -// fprintf(socket_fs_, "%li", i); -// } - -// void SLDaemon::send(const string str, bool buf) { -// send(str.c_str(), str.length(), buf); -// } - -// void SLDaemon::send(const char *data, size_t len, bool buf) { -// if (len == 0) -// return; -// if (fwrite(data, 1, len, socket_fs_) != len) -// throw Error(ERR_NET, strerror(errno)); -// if (!buf && fflush(socket_fs_)) -// throw Error(ERR_NET, strerror(errno)); -// } - -// void SLDaemon::sendln(const string &s) { -// if (!fprintf(socket_fs_, "%s\n", s.c_str())) -// throw Error(ERR_NET, strerror(errno)); -// if (verbose()) -// logout << SND_DATA << s << "\n"; -// } - -// char *SLDaemon::recv(size_t size) { -// char *data = new char[size]; - -// if (fread(data, 1, size, socket_fs_) != size) { -// delete[] data; -// throw Error(ERR_NET, strerror(errno)); -// } -// return data; -// } - -// char *SLDaemon::recvln() { -// char *line = new char[MAX_LINE_SIZE]; - -// errno=0; -// if (fgets(line, MAX_LINE_SIZE, socket_fs_) == 0) { -// delete[] line; -// if(feof(socket_fs_)) -// throw Error(ERR_NET, (string) "recvln: Connexion close"); -// throw Error(ERR_FILE, (string) "recvln: " + strerror(errno)); -// } -// if (verbose()) -// logout << RCV_DATA << line; - -// return line; -// } - -// void SLDaemon::flush() { -// if (fflush(socket_fs_)) -// throw Error(ERR_NET, strerror(errno)); -// } - //****************************************************************************** // protocol functions //****************************************************************************** @@ -261,6 +150,11 @@ void SLDaemon::auth() { snprintf(buf, MAX_LINE_SIZE, "PASS %s", Cypher::sha1_64(options.pass.c_str(), options.pass.length()).c_str()); c_.sendln(buf); + + string valid = c_.recvln(); + + if (valid != "OK") + throw Error(ERR_AUTH, "Bad Authentification."); } //****************************************************************************** diff --git a/sld/trunk/src/daemon.hh b/sld/trunk/src/daemon.hh index a90239e2b8c064018fa22c64f886a85e6da906f5..b39ab3113a8f7b72001be2b0853f64984d065068 100644 --- a/sld/trunk/src/daemon.hh +++ b/sld/trunk/src/daemon.hh @@ -61,8 +61,6 @@ protected: // current socket Connection c_; -// int socket_fd_; -// FILE* socket_fs_; // pid list typedef std::set t_job; @@ -71,17 +69,6 @@ protected: // Options functions void check_options() const; - // network functions -// void disconnect(); -// void send(const char *data, size_t len, bool buf = true); -// void send(const string str, bool buf = true); -// void send(long int i); -// void sendln(const string &s); -// char *recv(size_t size); -// void recv(size_t size, const string &filename); -// char *recvln(); -// void flush(); - // protocol functions void auth(); @@ -97,7 +84,6 @@ protected: void cmd_file(const char *line); // others functions -// string md5(const string &file) const; void recv2file(size_t size, const string &filename); void clean_dir(const string &dir) const; diff --git a/sld/trunk/src/sld.cc b/sld/trunk/src/sld.cc index 150dadcda3a7bb4f731323153bf91323af0b406b..2bb4c181fe3630be4335e0067091276a521c2c20 100644 --- a/sld/trunk/src/sld.cc +++ b/sld/trunk/src/sld.cc @@ -120,22 +120,25 @@ static bool register_signals() { return false; } - // register sigint handler - if (signal(SIGINT, sigint) == SIG_ERR) { - logerr << "sld: error: " << strerror(errno) << ".\n"; - return false; - } + if (SLDaemon::Instance().options.daemon) { - // register sigkill handler - if (signal(SIGTERM, sigint) == SIG_ERR) { - logerr << "sld: error: " << strerror(errno) << ".\n"; - return false; - } + // register sigint handler + if (signal(SIGINT, sigint) == SIG_ERR) { + logerr << "sld: error: " << strerror(errno) << ".\n"; + return false; + } - // register sigkill handler - if (signal(SIGHUP, sigint) == SIG_ERR) { - logerr << "sld: error: " << strerror(errno) << ".\n"; - return false; + // register sigkill handler + if (signal(SIGTERM, sigint) == SIG_ERR) { + logerr << "sld: error: " << strerror(errno) << ".\n"; + return false; + } + + // register sigkill handler + if (signal(SIGHUP, sigint) == SIG_ERR) { + logerr << "sld: error: " << strerror(errno) << ".\n"; + return false; + } } return true; diff --git a/sld/trunk/src/sll/connection.cc b/sld/trunk/src/sll/connection.cc index d78aa7752092db4bb30eb3dad884009f6ce55071..25e3eddc76cc38bace2d21c07414fd7396fa2e7c 100644 --- a/sld/trunk/src/sll/connection.cc +++ b/sld/trunk/src/sll/connection.cc @@ -106,14 +106,14 @@ void Connection::connect(const char *addr, int port) { } // take read and write mutex - pthread_mutex_lock(&r_mutex_); pthread_mutex_lock(&w_mutex_); + pthread_mutex_lock(&r_mutex_); // retrieve remote host info struct hostent *h = gethostbyname(addr); if (h == 0) { - pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&c_mutex_); throw Error(ERR_NET, (string) "Unable to resolve: " + addr + ": " + hstrerror(h_errno)); } @@ -121,8 +121,8 @@ void Connection::connect(const char *addr, int port) { // create socket try { socket_(); } catch (...) { - pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&c_mutex_); throw; } @@ -138,8 +138,8 @@ void Connection::connect(const char *addr, int port) { if (::connect(socket_fd_, (struct sockaddr *) &daddr, sizeof daddr) == -1) { int errno__ = errno; disconnect_(); - pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&c_mutex_); throw Error(ERR_NET, (string) "Unable to connect to " + addr + ": " + strerror(errno__)); } @@ -152,14 +152,14 @@ void Connection::connect(const char *addr, int port) { setremoteport_(); } catch (...) { - pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&c_mutex_); throw; } - pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&c_mutex_); } @@ -243,26 +243,34 @@ void Connection::listen(int port, int max) { * @return null on error, else a new connection */ Connection *Connection::accept() { - // keep the socket fd + // accept is a read/write op pthread_mutex_lock(&c_mutex_); + pthread_mutex_lock(&r_mutex_); + pthread_mutex_lock(&w_mutex_); + + // get the socket fd int socket_fd = socket_fd_; pthread_mutex_unlock(&c_mutex_); // test socket validity - if (socket_fd < 0) + if (socket_fd < 0) { + pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&w_mutex_); throw Error(ERR_NET, "No connection established but trying to accept"); + } struct sockaddr_in r_addr; socklen_t r_sin_size = sizeof r_addr; int r_fd; - // accept is a read/write operation - pthread_mutex_lock(&r_mutex_); - pthread_mutex_lock(&w_mutex_); - if ((r_fd = ::accept(socket_fd, (struct sockaddr *) &r_addr, &r_sin_size)) == -1) { + int errno__ = errno; + pthread_mutex_lock(&c_mutex_); + disconnect_(); + pthread_mutex_unlock(&r_mutex_); + pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&c_mutex_); - throw Error(ERR_NET, (string) "accept: " + strerror(errno)); + throw Error(ERR_NET, (string) "accept: " + strerror(errno__)); } pthread_mutex_unlock(&r_mutex_); @@ -299,8 +307,8 @@ void Connection::send(const char* buf, size_t len) { pthread_mutex_lock(&r_mutex_); disconnect_(); pthread_mutex_unlock(&r_mutex_); - pthread_mutex_unlock(&c_mutex_); pthread_mutex_unlock(&w_mutex_); + pthread_mutex_unlock(&c_mutex_); if (errno__ == ECONNRESET || errno__ == EPIPE) throw Error(ERR_NET, "Connection reset by peer"); else @@ -354,9 +362,9 @@ string Connection::recvln() { pthread_mutex_lock(&c_mutex_); pthread_mutex_lock(&w_mutex_); disconnect_(); + pthread_mutex_unlock(&w_mutex_); pthread_mutex_unlock(&r_mutex_); pthread_mutex_unlock(&c_mutex_); - pthread_mutex_unlock(&r_mutex_); *local_buf = 0; diff --git a/sld/trunk/src/sll/cypher.cc b/sld/trunk/src/sll/cypher.cc index 01485946e13cf3d7666353a9ccfe1d7b71a10f29..b9aa4e2c2d77056e7322883152556aaf5e23d3ae 100644 --- a/sld/trunk/src/sll/cypher.cc +++ b/sld/trunk/src/sll/cypher.cc @@ -52,7 +52,7 @@ string Cypher::sha1_64(const char *data, size_t size) { BIO_get_mem_ptr(b64, &bptr); if (bptr->length > 0) - ret.insert(0, bptr->data, bptr->length); + ret.insert(0, bptr->data, bptr->length - 1); else throw Error(ERR_CYPHER, "Unable to compute sha1_64");