Commit 023042a3 authored by Seblu's avatar Seblu
Browse files

- fix bugs sur test de connection existante dans la classe connection

- ajout de test de connection sur toutes les fonctions de la class connection
- changement du mode de dispatch par la classe console, il faut desormais passer par login pour obtenir une class fils
- separation des headers des classes clients filles et maman
- ajout type d'erreur ERR_PARSE
- Archi du backend console
parent bf7aa142
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -33,7 +33,9 @@ sls_SOURCES= src/sls.hh \
		src/connection.cc	\
		src/client.hh		\
		src/client.cc		\
		src/client_console.hh	\
		src/client_console.cc	\
		src/client_daemon.hh	\
		src/client_daemon.cc

CLEANFILES= *~ '\#*'
+35 −23
Original line number Diff line number Diff line
@@ -18,56 +18,68 @@

#include "sls.hh"
#include "client.hh"
#include "client_console.hh"
#include "client_daemon.hh"

static inline Client *login_fail(Connection &c);

/*******************************************************************************
** Method of Classes
*******************************************************************************/
Client *Client::login(Connection &c) {

  // read login info
  string slogin = c.recvln();
  string spass = c.recvln();

  // FIXME: find better method than fucking magic number
  char clientbuf[512];
  char loginbuf[512];
  char passbuf[512];


  // cut read data
  if (sscanf(slogin.c_str(), "%512s %512s\n", clientbuf, loginbuf) != 2)
    return login_fail(c);
  if (sscanf(spass.c_str(), "PASS %512s\n", passbuf) != 1)
    return login_fail(c);

  if (sscanf(slogin.c_str(), "HOST %512s\n", loginbuf) == 1) {
    // check login and password
  // create client
  Client *real_client;
  if (strcmp(clientbuf, "HOST") == 0)
    real_client = new Daemon(c);
  else if (strcmp(clientbuf, "USER") == 0)
    real_client = new Console(c);
  else
    return login_fail(c);

    // Register login and pass in client
  // check login and password and register it in DB if success
  if (real_client->trust(loginbuf, passbuf) == false) {
    delete real_client;
    return login_fail(c);
  }

  // Print succefful login
    std::cout << "Connection id " << c.getid() << ": Host " << loginbuf 
  std::cout << "CId " << c.getid() << ": Host " << loginbuf
	    << " logged from " << c.getremoteip()
	    << " on port " << c.getremoteport() << ".\n";
    return new Daemon(c);
  }
  else if (sscanf(slogin.c_str(), "USER %512s\n", loginbuf) == 1) {
    return new Console(c);
  }

  return login_fail(c);
}

Client *Client::login_fail(Connection &c) {
  std::cout << "Connection id " << c.getid() << ": Bad authentification.\n";
  c.sendln("Bad authentification.");
  return 0;
  return real_client;
}

/*******************************************************************************
** Public Classes
** Public methods
*******************************************************************************/

Client::Client(Connection &c) : c_ (c) {}

Client::~Client() {}

/*******************************************************************************
** Local functions
*******************************************************************************/


void Client::exec() {
  assert(0);
static Client *login_fail(Connection &c) {
  std::cout << "CId " << c.getid() << ": Bad authentification.\n";
  c.sendln("Bad authentification.");
  return 0;
}
+8 −26
Original line number Diff line number Diff line
@@ -21,42 +21,24 @@

# include "connection.hh"

/*!
** Client Class
/**
 * Client
 */
class Client {
public:
  Client(Connection &c);
  virtual ~Client();

  virtual void exec();
  virtual void exec() = 0;

  static Client *login(Connection &c);

protected:
  Connection c_;

private:
  static Client *login_fail(Connection &c);
};

/*!
** Client Daemon Class
 */
class Daemon: public Client {
public:
  Daemon(Connection &c);
  void exec();
};
  Client(Connection &c);

/*!
** Client Console Class
 */
class Console : public Client {
public:
  Console(Connection &c);
  void exec();
  virtual bool trust(const char *login, const char *pass) = 0;
protected:
  Connection c_;
  string login_;
};


#endif
+74 −3
Original line number Diff line number Diff line
@@ -17,10 +17,81 @@
*/

#include "sls.hh"
#include "client.hh"
#include "error.hh"
#include "option.hh"
#include "client_console.hh"

Console::Console(Connection &c) : Client(c) {}
/*******************************************************************************
** Public methods
*******************************************************************************/

/**
 * Entry point for a console backend
 */
void Console::exec() {
  c_.sendln("You are a console !!");
  while (1) {
    // receive line
    string cmd = c_.recvln();

    // Print info
    if (O.verbose) std::cout << "Cid " << c_.getid() << ": Receive: " << cmd << ".\n";
    else std::cout << "Cid " << c_.getid() << ": Receive request.\n";

    // call parser
    try { parse(cmd); }
    catch (const Error &e) {
      if (e.code() == ERR_PARSE) {
	// Print info
	if (O.verbose) std::cout << "Cid " << c_.getid() << ": Parse error: " << e << ".\n";
	else std::cout << "Cid " << c_.getid() << ": Invalid command.\n";
      }
      else throw;
    }
  }
}

/*******************************************************************************
** Protected methods
*******************************************************************************/

/**
 * Constructor
 *
 * @param c Attached connection
 */
Console::Console(Connection &c) : Client(c) {}

/**
 * Check if login and pass are valid user
 * and register login and pass are logged in DB
 *
 * @param login user login
 * @param pass user pass
 *
 * @return if user is logged or not
 */
bool Console::trust(const char *login, const char *pass) {
  assert(login);
  assert(pass);

  // check login and password in DB
  c_.sendln("User always trusted !!");

  // store username
  login_ = login;

  // register as logged in DB

  return true;
}

/**
 * Parse and execute
 *
 * @param cmd string to parse
 */
void Console::parse(const string &cmd) {
  if (!cmd.empty())
    throw Error(ERR_PARSE, "Empty command");
  // FIXME
}
+40 −0
Original line number Diff line number Diff line
/*
  This file is part of SLS.
  Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>

  SLS is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  SLS 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
  along with SLS; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef CLIENT_CONSOLE_HH
# define CLIENT_CONSOLE_HH

# include "client.hh"

/*!
** Client Console Class
 */
class Console : public Client {
public:
  friend class Client;
  virtual void exec();
protected:
  Console(Connection &c);

  virtual bool trust(const char *login, const char *pass);

  void parse(const string &cmd);

};

#endif
Loading