2009-08-24 23:55:06 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2009 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB 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, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/auth.h>
|
|
|
|
#include <grub/list.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/env.h>
|
|
|
|
#include <grub/normal.h>
|
2009-11-09 08:05:27 +00:00
|
|
|
#include <grub/time.h>
|
2009-12-20 23:32:15 +00:00
|
|
|
#include <grub/i18n.h>
|
2009-08-24 23:55:06 +00:00
|
|
|
|
|
|
|
struct grub_auth_user
|
|
|
|
{
|
|
|
|
struct grub_auth_user *next;
|
2012-01-24 12:31:12 +00:00
|
|
|
struct grub_auth_user **prev;
|
2009-08-24 23:55:06 +00:00
|
|
|
char *name;
|
|
|
|
grub_auth_callback_t callback;
|
|
|
|
void *arg;
|
|
|
|
int authenticated;
|
|
|
|
};
|
|
|
|
|
2011-03-23 11:05:13 +00:00
|
|
|
static struct grub_auth_user *users = NULL;
|
2009-08-24 23:55:06 +00:00
|
|
|
|
|
|
|
grub_err_t
|
|
|
|
grub_auth_register_authentication (const char *user,
|
|
|
|
grub_auth_callback_t callback,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
struct grub_auth_user *cur;
|
|
|
|
|
|
|
|
cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
|
|
|
|
if (!cur)
|
|
|
|
cur = grub_zalloc (sizeof (*cur));
|
|
|
|
if (!cur)
|
|
|
|
return grub_errno;
|
|
|
|
cur->callback = callback;
|
|
|
|
cur->arg = arg;
|
|
|
|
if (! cur->name)
|
|
|
|
{
|
|
|
|
cur->name = grub_strdup (user);
|
|
|
|
if (!cur->name)
|
|
|
|
{
|
|
|
|
grub_free (cur);
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
grub_list_push (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
|
|
|
|
}
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_err_t
|
|
|
|
grub_auth_unregister_authentication (const char *user)
|
|
|
|
{
|
|
|
|
struct grub_auth_user *cur;
|
|
|
|
cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
|
|
|
|
if (!cur)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "user '%s' not found", user);
|
|
|
|
if (!cur->authenticated)
|
|
|
|
{
|
|
|
|
grub_free (cur->name);
|
2012-01-24 12:31:12 +00:00
|
|
|
grub_list_remove (GRUB_AS_LIST (cur));
|
2009-08-24 23:55:06 +00:00
|
|
|
grub_free (cur);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cur->callback = NULL;
|
|
|
|
cur->arg = NULL;
|
|
|
|
}
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_err_t
|
|
|
|
grub_auth_authenticate (const char *user)
|
|
|
|
{
|
|
|
|
struct grub_auth_user *cur;
|
|
|
|
|
|
|
|
cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
|
|
|
|
if (!cur)
|
|
|
|
cur = grub_zalloc (sizeof (*cur));
|
|
|
|
if (!cur)
|
|
|
|
return grub_errno;
|
|
|
|
|
|
|
|
cur->authenticated = 1;
|
|
|
|
|
|
|
|
if (! cur->name)
|
|
|
|
{
|
|
|
|
cur->name = grub_strdup (user);
|
|
|
|
if (!cur->name)
|
|
|
|
{
|
|
|
|
grub_free (cur);
|
|
|
|
return grub_errno;
|
|
|
|
}
|
|
|
|
grub_list_push (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
|
|
|
|
}
|
|
|
|
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_err_t
|
|
|
|
grub_auth_deauthenticate (const char *user)
|
|
|
|
{
|
|
|
|
struct grub_auth_user *cur;
|
|
|
|
cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
|
|
|
|
if (!cur)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "user '%s' not found", user);
|
|
|
|
if (!cur->callback)
|
|
|
|
{
|
|
|
|
grub_free (cur->name);
|
2012-01-24 12:31:12 +00:00
|
|
|
grub_list_remove (GRUB_AS_LIST (cur));
|
2009-08-24 23:55:06 +00:00
|
|
|
grub_free (cur);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cur->authenticated = 0;
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
is_authenticated (const char *userlist)
|
|
|
|
{
|
|
|
|
const char *superusers;
|
2010-03-26 18:18:19 +00:00
|
|
|
struct grub_auth_user *user;
|
2009-08-24 23:55:06 +00:00
|
|
|
|
|
|
|
superusers = grub_env_get ("superusers");
|
|
|
|
|
|
|
|
if (!superusers)
|
|
|
|
return 1;
|
|
|
|
|
2010-03-26 18:32:21 +00:00
|
|
|
FOR_LIST_ELEMENTS (user, users)
|
2010-03-26 18:18:19 +00:00
|
|
|
{
|
|
|
|
if (!(user->authenticated))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((userlist && grub_strword (userlist, user->name))
|
|
|
|
|| grub_strword (superusers, user->name))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-08-24 23:55:06 +00:00
|
|
|
}
|
|
|
|
|
2009-12-24 14:34:33 +00:00
|
|
|
static int
|
|
|
|
grub_username_get (char buf[], unsigned buf_size)
|
|
|
|
{
|
|
|
|
unsigned cur_len = 0;
|
|
|
|
int key;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2010-05-10 12:54:51 +00:00
|
|
|
key = grub_getkey ();
|
2009-12-24 14:34:33 +00:00
|
|
|
if (key == '\n' || key == '\r')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (key == '\e')
|
|
|
|
{
|
|
|
|
cur_len = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key == '\b')
|
|
|
|
{
|
|
|
|
cur_len--;
|
|
|
|
grub_printf ("\b");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!grub_isprint (key))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (cur_len + 2 < buf_size)
|
|
|
|
{
|
|
|
|
buf[cur_len++] = key;
|
2010-03-15 10:49:27 +00:00
|
|
|
grub_printf ("%c", key);
|
2009-12-24 14:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
grub_memset (buf + cur_len, 0, buf_size - cur_len);
|
|
|
|
|
2010-03-15 10:49:27 +00:00
|
|
|
grub_xputs ("\n");
|
2009-12-24 14:34:33 +00:00
|
|
|
grub_refresh ();
|
|
|
|
|
|
|
|
return (key != '\e');
|
|
|
|
}
|
|
|
|
|
2009-08-24 23:55:06 +00:00
|
|
|
grub_err_t
|
|
|
|
grub_auth_check_authentication (const char *userlist)
|
|
|
|
{
|
|
|
|
char login[1024];
|
|
|
|
struct grub_auth_user *cur = NULL;
|
2009-11-09 14:30:47 +00:00
|
|
|
static unsigned long punishment_delay = 1;
|
2009-12-06 19:11:50 +00:00
|
|
|
char entered[GRUB_AUTH_MAX_PASSLEN];
|
2010-03-26 18:21:01 +00:00
|
|
|
struct grub_auth_user *user;
|
2009-08-24 23:55:06 +00:00
|
|
|
|
|
|
|
grub_memset (login, 0, sizeof (login));
|
|
|
|
|
|
|
|
if (is_authenticated (userlist))
|
2009-11-09 00:37:56 +00:00
|
|
|
{
|
|
|
|
punishment_delay = 1;
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
2009-08-24 23:55:06 +00:00
|
|
|
|
2009-12-24 14:34:33 +00:00
|
|
|
grub_puts_ (N_("Enter username: "));
|
|
|
|
|
|
|
|
if (!grub_username_get (login, sizeof (login) - 1))
|
2009-11-09 00:37:56 +00:00
|
|
|
goto access_denied;
|
2009-08-24 23:55:06 +00:00
|
|
|
|
2009-12-24 14:34:33 +00:00
|
|
|
grub_puts_ (N_("Enter password: "));
|
2009-08-24 23:55:06 +00:00
|
|
|
|
2009-12-06 19:11:50 +00:00
|
|
|
if (!grub_password_get (entered, GRUB_AUTH_MAX_PASSLEN))
|
|
|
|
goto access_denied;
|
2009-08-24 23:55:06 +00:00
|
|
|
|
2010-06-11 20:31:16 +00:00
|
|
|
FOR_LIST_ELEMENTS (user, users)
|
2010-03-26 18:21:01 +00:00
|
|
|
{
|
|
|
|
if (grub_strcmp (login, user->name) == 0)
|
|
|
|
cur = user;
|
|
|
|
}
|
2009-08-24 23:55:06 +00:00
|
|
|
|
2009-12-06 19:11:50 +00:00
|
|
|
if (!cur || ! cur->callback)
|
|
|
|
goto access_denied;
|
2009-08-24 23:55:06 +00:00
|
|
|
|
2010-10-17 13:41:54 +00:00
|
|
|
cur->callback (login, entered, cur->arg);
|
2009-08-24 23:55:06 +00:00
|
|
|
if (is_authenticated (userlist))
|
2009-11-09 00:37:56 +00:00
|
|
|
{
|
|
|
|
punishment_delay = 1;
|
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
access_denied:
|
|
|
|
grub_sleep (punishment_delay);
|
|
|
|
|
|
|
|
if (punishment_delay < GRUB_ULONG_MAX / 2)
|
|
|
|
punishment_delay *= 2;
|
|
|
|
|
2009-08-24 23:55:06 +00:00
|
|
|
return GRUB_ACCESS_DENIED;
|
|
|
|
}
|
2010-09-12 14:11:41 +00:00
|
|
|
|
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_authenticate (struct grub_command *cmd __attribute__ ((unused)),
|
|
|
|
int argc, char **args)
|
|
|
|
{
|
|
|
|
return grub_auth_check_authentication ((argc >= 1) ? args[0] : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_command_t cmd;
|
|
|
|
|
|
|
|
void
|
|
|
|
grub_normal_auth_init (void)
|
|
|
|
{
|
|
|
|
cmd = grub_register_command ("authenticate",
|
|
|
|
grub_cmd_authenticate,
|
|
|
|
N_("[USERLIST]"), N_("Authenticate users"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
grub_normal_auth_fini (void)
|
|
|
|
{
|
|
|
|
grub_unregister_command (cmd);
|
|
|
|
}
|