2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2009 Free Software Foundation, Inc.
5
* GRUB is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* GRUB is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19
#include <grub/auth.h>
20
#include <grub/list.h>
22
#include <grub/misc.h>
24
#include <grub/normal.h>
25
#include <grub/time.h>
26
#include <grub/i18n.h>
30
struct grub_auth_user *next;
32
grub_auth_callback_t callback;
37
struct grub_auth_user *users = NULL;
40
grub_auth_register_authentication (const char *user,
41
grub_auth_callback_t callback,
44
struct grub_auth_user *cur;
46
cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
48
cur = grub_zalloc (sizeof (*cur));
51
cur->callback = callback;
55
cur->name = grub_strdup (user);
61
grub_list_push (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
67
grub_auth_unregister_authentication (const char *user)
69
struct grub_auth_user *cur;
70
cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
72
return grub_error (GRUB_ERR_BAD_ARGUMENT, "user '%s' not found", user);
73
if (!cur->authenticated)
75
grub_free (cur->name);
76
grub_list_remove (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
88
grub_auth_authenticate (const char *user)
90
struct grub_auth_user *cur;
92
cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
94
cur = grub_zalloc (sizeof (*cur));
98
cur->authenticated = 1;
102
cur->name = grub_strdup (user);
108
grub_list_push (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
111
return GRUB_ERR_NONE;
115
grub_auth_deauthenticate (const char *user)
117
struct grub_auth_user *cur;
118
cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
120
return grub_error (GRUB_ERR_BAD_ARGUMENT, "user '%s' not found", user);
123
grub_free (cur->name);
124
grub_list_remove (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
128
cur->authenticated = 0;
129
return GRUB_ERR_NONE;
133
is_authenticated (const char *userlist)
135
const char *superusers;
136
struct grub_auth_user *user;
138
superusers = grub_env_get ("superusers");
143
FOR_LIST_ELEMENTS (user, users)
145
if (!(user->authenticated))
148
if ((userlist && grub_strword (userlist, user->name))
149
|| grub_strword (superusers, user->name))
157
grub_username_get (char buf[], unsigned buf_size)
159
unsigned cur_len = 0;
164
key = GRUB_TERM_ASCII_CHAR (grub_getkey ());
165
if (key == '\n' || key == '\r')
181
if (!grub_isprint (key))
184
if (cur_len + 2 < buf_size)
186
buf[cur_len++] = key;
187
grub_printf ("%c", key);
191
grub_memset (buf + cur_len, 0, buf_size - cur_len);
196
return (key != '\e');
200
grub_auth_check_authentication (const char *userlist)
203
struct grub_auth_user *cur = NULL;
205
static unsigned long punishment_delay = 1;
206
char entered[GRUB_AUTH_MAX_PASSLEN];
207
struct grub_auth_user *user;
209
grub_memset (login, 0, sizeof (login));
211
if (is_authenticated (userlist))
213
punishment_delay = 1;
214
return GRUB_ERR_NONE;
217
grub_puts_ (N_("Enter username: "));
219
if (!grub_username_get (login, sizeof (login) - 1))
222
grub_puts_ (N_("Enter password: "));
224
if (!grub_password_get (entered, GRUB_AUTH_MAX_PASSLEN))
227
FOR_LIST_ELEMENTS (user, users)
229
if (grub_strcmp (login, user->name) == 0)
233
if (!cur || ! cur->callback)
236
err = cur->callback (login, entered, cur->arg);
237
if (is_authenticated (userlist))
239
punishment_delay = 1;
240
return GRUB_ERR_NONE;
244
grub_sleep (punishment_delay);
246
if (punishment_delay < GRUB_ULONG_MAX / 2)
247
punishment_delay *= 2;
249
return GRUB_ACCESS_DENIED;