Skip to content

Commit

Permalink
sys/shell: add pseudomodule shell_lock_auto_locking
Browse files Browse the repository at this point in the history
Module to lock the shell after a given timeout of time x. When the
shell did not receive any input within time x, then the shell is
locked automatically.
  • Loading branch information
HendrikVE committed Nov 6, 2020
1 parent 617e2dc commit ad243b3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ PSEUDOMODULES += scanf_float
PSEUDOMODULES += sched_cb
PSEUDOMODULES += semtech_loramac_rx
PSEUDOMODULES += shell_hooks
PSEUDOMODULES += shell_lock_auto_locking
PSEUDOMODULES += slipdev_stdio
PSEUDOMODULES += sock
PSEUDOMODULES += sock_async
Expand Down
28 changes: 28 additions & 0 deletions sys/include/shell_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ extern "C" {
#endif /* SHELL_LOCK_PASSWORD */
#endif /* MODULE_SHELL_LOCK */

#ifdef MODULE_SHELL_LOCK_AUTO_LOCKING
/**
* @brief Lock the shell after this time span without user input
* Defaults to 5 minutes and can be overwritten by defining
* SHELL_LOCK_AUTO_LOCK_TIMEOUT_MS in the applications Makefile
*/
#define MAX_AUTO_LOCK_PAUSE_MS 5 * 60 * 1000

#ifdef SHELL_LOCK_AUTO_LOCK_TIMEOUT_MS
#undef MAX_AUTO_LOCK_PAUSE_MS
#define MAX_AUTO_LOCK_PAUSE_MS SHELL_LOCK_AUTO_LOCK_TIMEOUT_MS
#endif /* SHELL_LOCK_AUTO_LOCK_TIMEOUT_MS */

/**
* @brief Offset used for the thread for automated locking, so that the
* thread is not woken up shortely before it has to lock the shell.
*/
#define TIMER_SLEEP_OFFSET_MS 100
#endif /* MODULE_SHELL_LOCK_AUTO_LOCKING */

/**
* @brief Entry point for the lock mechanism. If locked, the user will
* be asked for a password. This function won't return until the
Expand All @@ -55,6 +75,14 @@ void shell_lock_checkpoint(char *line_buf, int buf_size);
*/
bool shell_lock_is_locked(void);

#ifdef MODULE_SHELL_LOCK_AUTO_LOCKING
/**
* @brief Restart the timeout interval before the shell is locked
* automatically.
*/
void shell_lock_auto_lock_refresh(void);
#endif /* MODULE_SHELL_LOCK_AUTO_LOCKING */

/**
* @brief Command list containing all commands used for this module.
*
Expand Down
7 changes: 6 additions & 1 deletion sys/shell/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ extern const shell_command_t _shell_lock_command_list[];

extern void shell_lock_checkpoint(char *line_buf, int len);
extern bool shell_lock_is_locked(void);
extern void shell_lock_reset(void);
extern void shell_lock_auto_lock_refresh(void);

enum parse_state {
PARSE_BLANK = 0x0,
Expand Down Expand Up @@ -506,6 +506,11 @@ void shell_run_once(const shell_command_t *shell_commands,
}
}

if (IS_USED(MODULE_SHELL_LOCK_AUTO_LOCKING)) {
/* reset lock countdown in case of new input */
shell_lock_auto_lock_refresh();
}

switch (res) {

case EOF:
Expand Down
43 changes: 43 additions & 0 deletions sys/shell_lock/shell_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
* slow down brute force attacks.
* Does not make use of any cryptographic features yet.
*
* This module also provides a pseudomodule for automated locking after a given
* interval. Add "USEMODULE += shell_lock_auto_locking" to your Makefile to
* enable this feature.
*
* @author Hendrik van Essen <hendrik.ve@fu-berlin.de>
*
* @}
Expand All @@ -35,6 +39,10 @@

static bool _shell_is_locked = true;

#ifdef MODULE_SHELL_LOCK_AUTO_LOCKING
static xtimer_t _shell_auto_lock_xtimer;
#endif

/* defined in shell.c */
extern void flush_if_needed(void);

Expand Down Expand Up @@ -136,6 +144,27 @@ void _login_barrier(char *line_buf, size_t buf_size)
}
}

#ifdef MODULE_SHELL_LOCK_AUTO_LOCKING
void _shell_auto_lock_xtimer_callback(void *arg)
{
(void) arg;

_shell_is_locked = true;
}

void _refresh_shell_auto_lock(void)
{
xtimer_remove(&_shell_auto_lock_xtimer);
xtimer_set(&_shell_auto_lock_xtimer,
(MAX_AUTO_LOCK_PAUSE_MS + TIMER_SLEEP_OFFSET_MS) * US_PER_MS);
}

void shell_lock_auto_lock_refresh(void)
{
_refresh_shell_auto_lock();
}
#endif

bool shell_lock_is_locked(void)
{
return _shell_is_locked;
Expand All @@ -148,6 +177,20 @@ void shell_lock_checkpoint(char *line_buf, int buf_size)

_login_barrier(line_buf, buf_size);

if (IS_USED(MODULE_SHELL_LOCK_AUTO_LOCKING)) {
printf("Shell was unlocked.\n\n");
}
else {
printf("Shell was unlocked.\n\n"
"IMPORTANT: Don't forget to lock the shell after usage, "
"because it won't lock itself.\n\n");
}

_shell_is_locked = false;
}

if (IS_USED(MODULE_SHELL_LOCK_AUTO_LOCKING)) {
_shell_auto_lock_xtimer.callback = &_shell_auto_lock_xtimer_callback;
_refresh_shell_auto_lock();
}
}

0 comments on commit ad243b3

Please sign in to comment.