summaryrefslogtreecommitdiffstats
path: root/common/password.c
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2015-08-27 15:29:58 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2015-08-28 08:00:27 +0200
commit90e766a78fe8ebf8acdc19713e9194266c78c093 (patch)
tree58556b422db0819d335e60c4b5e573206d8c872d /common/password.c
parentf38ba32965c5686c062884fab2e9f505015af82a (diff)
downloadbarebox-90e766a78fe8ebf8acdc19713e9194266c78c093.tar.gz
login: rework login mechanism
We used to have the login functionality in the /env/bin/init script. This is hard to review and it's too easy to break the login functionality with changes to this script. Move the places to ask for a password to C code where we have only a few places where we have to ask for a password. Mainly these are run_shell() and the menutree command. This patch introduces a login() function which will only return if the correct password has been entered. Following calls will return immediately without asking for a password again. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common/password.c')
-rw-r--r--common/password.c75
1 files changed, 73 insertions, 2 deletions
diff --git a/common/password.c b/common/password.c
index c845422..6532143 100644
--- a/common/password.c
+++ b/common/password.c
@@ -24,8 +24,11 @@
#include <digest.h>
#include <malloc.h>
#include <xfuncs.h>
+#include <magicvar.h>
#include <clock.h>
+#include <init.h>
#include <stdlib.h>
+#include <globalvar.h>
#include <generated/passwd.h>
#include <crypto/pbkdf2.h>
@@ -73,7 +76,7 @@ int password(unsigned char *passwd, size_t length, int flags, int timeout)
case CTL_CH('c'):
passwd[0] = '\0';
puts("\r\n");
- return 0;
+ return -EINTR;
case CTL_CH('h'):
case BB_KEY_DEL7:
case BB_KEY_DEL:
@@ -104,7 +107,7 @@ int password(unsigned char *passwd, size_t length, int flags, int timeout)
}
} while (!is_timeout(start, timeout * SECOND) || timeout == 0);
- return -1;
+ return -ETIMEDOUT;
}
EXPORT_SYMBOL(password);
@@ -374,6 +377,8 @@ int set_env_passwd(unsigned char* passwd, size_t length)
hash_len = PBKDF2_LENGTH;
} else {
d = digest_alloc(PASSWD_SUM);
+ if (!d)
+ return -EINVAL;
hash_len = digest_length(d);
}
@@ -406,3 +411,69 @@ err:
return ret;
}
EXPORT_SYMBOL(set_env_passwd);
+
+#define PASSWD_MAX_LENGTH (128 + 1)
+
+#if defined(CONFIG_PASSWD_MODE_STAR)
+#define LOGIN_MODE STAR
+#elif defined(CONFIG_PASSWD_MODE_CLEAR)
+#define LOGIN_MODE CLEAR
+#else
+#define LOGIN_MODE HIDE
+#endif
+
+static int logged_in;
+static int login_timeout;
+static char *login_fail_command;
+
+/**
+ * login() - Prompt for password
+ *
+ * This function only returns when the correct password has been entered or
+ * no password is necessary because either no password is configured or the
+ * correct password has been entered in a previous call to this function.
+ */
+void login(void)
+{
+ unsigned char passwd[PASSWD_MAX_LENGTH];
+ int ret;
+
+ if (!is_passwd_enable())
+ return;
+
+ if (logged_in)
+ return;
+
+ while (1) {
+ printf("Password: ");
+
+ ret = password(passwd, PASSWD_MAX_LENGTH, LOGIN_MODE, login_timeout);
+ if (ret < 0)
+ run_command(login_fail_command);
+
+ if (ret < 0)
+ continue;
+
+ if (check_passwd(passwd, ret) != 1)
+ continue;
+
+ logged_in = 1;
+ return;
+ }
+}
+
+static int login_global_init(void)
+{
+ login_fail_command = xstrdup("boot");
+
+ globalvar_add_simple_int("login.timeout", &login_timeout, "%d");
+ globalvar_add_simple_string("login.fail_command", &login_fail_command);
+
+ return 0;
+}
+late_initcall(login_global_init);
+
+BAREBOX_MAGICVAR_NAMED(global_login_fail_command, global.login.fail_command,
+ "command to run when password entry failed");
+BAREBOX_MAGICVAR_NAMED(global_login_timeout, global.login.timeout,
+ "timeout to type the password");