Commit 0437d116 authored by Seblu's avatar Seblu
Browse files

Fix connection bug

Command are now executed
login/auth is ok
parent b5f09f10
Loading
Loading
Loading
Loading
+5 −2
Original line number Original line Diff line number Diff line
@@ -20,9 +20,11 @@ slc_SOURCES= src/slc.cc \
		src/options.cc		\
		src/options.cc		\
		src/engine.cc		\
		src/engine.cc		\
		src/screen.cc		\
		src/screen.cc		\
		src/cmd.cc		\
		src/history.cc		\
		src/history.cc		\
		src/sll/connection.cc	\
		src/sll/connection.cc	\
		src/sll/error.cc
		src/sll/error.cc	\
		src/sll/cypher.cc


noinst_SOURCES=	src/slc.hh		\
noinst_SOURCES=	src/slc.hh		\
		src/options.hh		\
		src/options.hh		\
@@ -31,7 +33,8 @@ noinst_SOURCES= src/slc.hh \
		src/history.hh		\
		src/history.hh		\
		src/sll/error.hh	\
		src/sll/error.hh	\
		src/sll/connection.hxx	\
		src/sll/connection.hxx	\
		src/sll/connection.hh
		src/sll/connection.hh	\
		src/sll/cypher.hh




CLEANFILES= *~ '\#*' .*.swp .*~
CLEANFILES= *~ '\#*' .*.swp .*~

slc/trunk/src/cmd.cc

0 → 100644
+153 −0
Original line number Original line Diff line number Diff line
/*
  This file is part of SLC.
  Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>

  SLC 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.

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

#include "slc.hh"
#include "engine.hh"
#include "screen.hh"
#include "options.hh"
#include "sll/connection.hh"

namespace cmd {

  /*!
  ** Exec command if @param s is not a command.
  */
  void exec(const char *s) {
    // string is a local command
    if (*s == '/') {
      if (!strcmp(s, "/exit")) {
	S << INF_DATA << "Exiting.\n";
	exit(0);
      }
      else if (!strcmp(s, "/logout"))
	cmd::logout();
      else if (!strcmp(s, "/login"))
	cmd::login();
      else if (!strcmp(s, "/help"))
	cmd::help();
      else if (!strncmp(s, "/user", 5))
	cmd::user(s);
      else if (!strncmp(s, "/pass", 5))
	cmd::pass(s);
      else
	S << WAR_DATA << s << ": Unkown command.\n";
    }
    // String is a remote command
    else {
      if (C.connected()) {
	C.sendln(s);
	S << ">> " << s << "\n";
      }
      else
	S << "Unable to send: " << s << ". Try to connect with /login !\n";
    }
  }


  /**
   * Log into server
   *
   */
  void login() {
    if (C.connected()) {
      S << INF_DATA << "Already connected. Please disconnect before.\n";
      return;
    }
    S << INF_DATA << "Login.\n";
    E.login();
  }


  /**
   * Log out of server
   *
   */
  void logout() {
    if (!C.connected()) {
      S << INF_DATA << "Not connected. Please connect before.\n";
      return;
    }
    S << INF_DATA << "Logout.\n";
    C.disconnect();
  }

  /**
   * Print cmd help
   *
   */
  void help() {
    S << INF_DATA << "Commands:\n";
    S << INF_DATA << "/exit: Quit.\n";
    S << INF_DATA << "/login: Connect and try to login on server.\n";
    S << INF_DATA << "/logout: Disconnect from server.\n";
    S << INF_DATA << "/user <name>: Change username.\n";
    S << INF_DATA << "/pass <pass>: Change password.\n";
    S << INF_DATA << "/help: Print this help.\n";
  }

  /**
   * Change user name to @param s
   */
  void user(const char *s) {
    if (C.connected()) {
      S << INF_DATA << "Your are connected. Please deconnect before.\n";
      return;
    }

    // extract name
    string login = s;
    login.erase(0, 6);

    if (login.empty()) {
      S << WAR_DATA << "Empty username.\n";
      return;
    }

    // set new name
    O.login = login;

    // display
    S << INF_DATA << "Username changed to `" << O.login << "'.\n";
  }

  /**
   * Change pass name @param s
   */
  void pass(const char *s) {
    if (C.connected()) {
      S << INF_DATA << "Your are connected. Please deconnect before.\n";
      return;
    }

    // extract name
    string pass = s;
    pass.erase(0, 6);

    if (pass.empty()) {
      S << WAR_DATA << "Empty password.\n";
      return;
    }

    // set new name
    O.pass = pass;

    // display
    S << INF_DATA << "Password changed.\n";
  }

}
+100 −0
Original line number Original line Diff line number Diff line
/*
  This file is part of SLC.
  Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>

  SLC 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.

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

#include "slc.hh"

#include "sll/connection.hh"
#include "sll/cypher.hh"

#include "engine.hh"
#include "screen.hh"
#include "options.hh"

#include <string.h>
#include <openssl/sha.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/evp.h>
#include <openssl/md5.h>

void Engine::on() {
  // Create graphic env
  S.init();

  // Show login screen
  S.login();

  // Showing dialog screen
  S.dialog();

  // connect and auth
  login();

  // Treat keyboards events
  S.eventsloop();
}

void Engine::off() {
  // Close connection
  try { C.disconnect(); }
  catch (...) {}
}

/**
 * Connect and auth to server
 *
 */
void Engine::login() {
  try {

    S << INF_DATA << "Trying to connect to " << O.server << " on " << O.port << ".\n";
    C.connect(O.server.c_str(), O.port);

    S << INF_DATA << "Trying to auth as " << O.login << ".\n";
    auth();
  }
  catch (const Error &e) {
    try {C.disconnect(); }
    catch (...) {}

    if (e.code() == ERR_NET || e.code() == ERR_AUTH)
      S << WAR_DATA << e.message() << ".\n";
    else
      throw;
  }
}

/**
 * Try authenticate to server
 *
 */
void Engine::auth() {
  char buf[MAX_LINE_SIZE];

  snprintf(buf, MAX_LINE_SIZE, "USER %s", O.login.c_str());
  C.sendln(buf);

  snprintf(buf, MAX_LINE_SIZE, "PASS %s",
	   Cypher::sha1_64(O.pass.c_str(), O.pass.length()).c_str());
  C.sendln(buf);

  string valid = C.recvln();

  if (valid != "OK")
    throw Error(ERR_AUTH, "Authentication refused");
}
+32 −0
Original line number Original line Diff line number Diff line
/*
  This file is part of SLC.
  Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>

  SLC 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.

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

#ifndef ENGINE_HH
# define ENGINE_HH

class Engine {
public:
  void on();
  void off();
  void login();

protected:
  void auth();
};

#endif
+8 −38
Original line number Original line Diff line number Diff line
@@ -16,16 +16,17 @@
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
*/


#include <ctype.h>
#include <signal.h>
#include <math.h>

#include "slc.hh"
#include "slc.hh"
#include "screen.hh"
#include "screen.hh"
#include "options.hh"
#include "options.hh"
#include "history.hh"
#include "history.hh"
#include "engine.hh"
#include "sll/connection.hh"
#include "sll/connection.hh"


#include <ctype.h>
#include <signal.h>
#include <math.h>

/*
/*
*********************************************************************************
*********************************************************************************
*************************************** PUBLIC **********************************
*************************************** PUBLIC **********************************
@@ -210,9 +211,10 @@ void Screen::eventsloop() {
      // check non empty buffer
      // check non empty buffer
      if (*cmd_buffer_ == 0) break;
      if (*cmd_buffer_ == 0) break;
      // save in history
      // save in history
      if (strncmp(cmd_buffer_, "/pass", 4))
	H.add(cmd_buffer_);
	H.add(cmd_buffer_);
      // Send to execution
      // Send to execution
      cmd_exec(cmd_buffer_);
      cmd::exec(cmd_buffer_);
      // purge buffer
      // purge buffer
      *cmd_buffer_ = 0;
      *cmd_buffer_ = 0;
      cmd_buf_off_ = 0;
      cmd_buf_off_ = 0;
@@ -513,38 +515,6 @@ void Screen::msg_save(const string &filename) const {
  }
  }


  fs.close();
  fs.close();

}

/*!
** Exec command or send if @param s is not a command.
*/
void Screen::cmd_exec(const char *s) {
  // string is a local command
  if (*s == '/') {
    if (!strcmp(s, "/exit")) {
      S << s << "\n";
      exit(0);
    }
    else if (!strcmp(s, "/logout")) {
      S << s << "\n";
      //C.stop();
    }
    else if (!strcmp(s, "/login")) {
      S << s << "\n";
      //C.start();
    }
    else
      S << s << ": Unkown command.\n";
  }
  // String is a remote command
  else {
    if (C.connected()) {
      C.sendln(s);
      S << ">> " << s << "\n";
    }
    else S << "Unable to send: " << s << ". Try to reconnect with /login !\n";
  }
}
}


/*!
/*!
Loading