Skip to content

Commit

Permalink
console: moved functions into separate C-modules
Browse files Browse the repository at this point in the history
- removed b-conio completely
- moved tee
- moved pam authentication
- ConsoleOutput instead of sendit and senditf
  • Loading branch information
franku committed Sep 17, 2018
1 parent 1ae1820 commit b26b35c
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 1,535 deletions.
2 changes: 1 addition & 1 deletion core/src/console/CMakeLists.txt
Expand Up @@ -19,7 +19,7 @@

include_directories(. .. ../include )

SET (BCONSSRCS console.cc console_conf.cc)
SET (BCONSSRCS auth_pam.cc console.cc console_conf.cc console_output.cc)
IF(HAVE_WIN32)
LIST(APPEND BCONSSRCS ../win32/console/consoleres.rc)
ENDIF()
Expand Down
149 changes: 149 additions & 0 deletions core/src/console/auth_pam.cc
@@ -0,0 +1,149 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2018 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program 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
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/

#include "auth_pam.h"

#include <security/pam_appl.h>
#include <readline/readline.h>
#include <readline/history.h>

#include "include/bareos.h"
#include "console_output.h"

enum class PamAuthState {
INIT,
RECEIVE_MSG_TYPE,
RECEIVE_MSG,
READ_INPUT,
SEND_INPUT,
AUTH_OK
};

static PamAuthState state = PamAuthState::INIT;
static bool SetEcho(FILE *stdin, bool on);


bool ConsolePamAuthenticate(FILE *stdin, BareosSocket *UA_sock)
{
bool quit = false;
bool error = false;

int type;
btimer_t *tid = nullptr;
char *userinput = nullptr;

while (!error && !quit) {
switch(state) {
case PamAuthState::INIT:
if(tid) {
StopBsockTimer(tid);
}
tid = StartBsockTimer(UA_sock, 10);
if (!tid) {
error = true;
}
state = PamAuthState::RECEIVE_MSG_TYPE;
break;
case PamAuthState::RECEIVE_MSG_TYPE:
if (UA_sock->recv() == 1) {
type = UA_sock->msg[0];
switch (type) {
case PAM_PROMPT_ECHO_OFF:
case PAM_PROMPT_ECHO_ON:
SetEcho (stdin, type == PAM_PROMPT_ECHO_ON);
state = PamAuthState::RECEIVE_MSG;
break;
case PAM_SUCCESS:
state = PamAuthState::AUTH_OK;
quit = true;
break;
default:
Dmsg1(100, "Error, unknown pam type %d\n", type);
error = true;
break;
} /* switch (type) */
} else {
error = true;
}
break;
case PamAuthState::RECEIVE_MSG:
if (UA_sock->recv() > 0) {
ConsoleOutput(UA_sock->msg);
state = PamAuthState::READ_INPUT;
} else {
error = true;
}
break;
case PamAuthState::READ_INPUT: {
userinput = readline("");
if (userinput) {
state = PamAuthState::SEND_INPUT;
} else {
error = true;
}
}
break;
case PamAuthState::SEND_INPUT:
UA_sock->fsend(userinput);
Actuallyfree(userinput);
state = PamAuthState::INIT;
break;
default:
break;
}
if (UA_sock->IsStop() || UA_sock->IsError()) {
if(userinput) {
Actuallyfree(userinput);
}
if(tid) {
StopBsockTimer(tid);
}
error = true;
break;
}
}; /* while (!quit) */

SetEcho (stdin, true);
ConsoleOutput("\n");

return !error;
}

#include <termios.h>
static bool SetEcho(FILE *stdin, bool on)
{
struct termios termios_old, termios_new;

/* Turn echoing off and fail if we can’t. */
if (tcgetattr (fileno (stdin), &termios_old) != 0)
return false;
termios_new = termios_old;
if (on) {
termios_new.c_lflag |= ECHO;
} else {
termios_new.c_lflag &= ~ECHO;
}
if (tcsetattr (fileno (stdin), TCSAFLUSH, &termios_new) != 0)
return false;
return true;
}
32 changes: 14 additions & 18 deletions core/src/console/conio.h → core/src/console/auth_pam.h
@@ -1,7 +1,9 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2004-2006 Free Software Foundation Europe e.V.
Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2018 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand All @@ -19,20 +21,14 @@
02110-1301, USA.
*/

#ifndef BAREOS_CONSOLE_CONIO_H_
#define BAREOS_CONSOLE_CONIO_H_
extern int InputLine(char *line, int len);
extern void ConInit(FILE *input);

extern "C" {
extern void ConTerm();
}

extern void ConSetZedKeys();
extern void t_sendl(char *buf, int len);
extern void t_send(char *buf);
extern void t_char(char c);
extern int usrbrk(void);
extern void clrbrk(void);
extern void trapctlc(void);
#endif
#ifndef BAREOS_CONSOLE_AUTH_PAM_H_
#define BAREOS_CONSOLE_AUTH_PAM_H_

#include <cstdio>

class BareosSocket;

bool ConsolePamAuthenticate(FILE *stdin, BareosSocket *UA_sock);


#endif /* BAREOS_CONSOLE_AUTH_PAM_H_ */

0 comments on commit b26b35c

Please sign in to comment.