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>
28
struct grub_auth_user *next;
30
grub_auth_callback_t callback;
35
struct grub_auth_user *users = NULL;
38
grub_auth_strcmp (const char *user_input, const char *template)
41
const char *ptr1, *ptr2;
42
for (ptr1 = user_input, ptr2 = template; *ptr1; ptr1++)
43
if (*ptr1 == (ptr2 ? *ptr2 : ptr1[1]) && ok && ptr2 != NULL)
52
grub_iswordseparator (int c)
54
return (grub_isspace (c) || c == ',' || c == ';' || c == '|' || c == '&');
58
grub_auth_strword (const char *haystack, const char *needle)
60
const char *n_pos = needle;
63
while (grub_iswordseparator (*haystack))
69
/* Crawl both the needle and the haystack word we're on. */
70
while(*haystack && !grub_iswordseparator (*haystack))
72
if (*haystack == *n_pos && ok)
88
grub_auth_register_authentication (const char *user,
89
grub_auth_callback_t callback,
92
struct grub_auth_user *cur;
94
cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
96
cur = grub_zalloc (sizeof (*cur));
99
cur->callback = callback;
103
cur->name = grub_strdup (user);
109
grub_list_push (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
111
return GRUB_ERR_NONE;
115
grub_auth_unregister_authentication (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);
121
if (!cur->authenticated)
123
grub_free (cur->name);
124
grub_list_remove (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
129
cur->callback = NULL;
132
return GRUB_ERR_NONE;
136
grub_auth_authenticate (const char *user)
138
struct grub_auth_user *cur;
140
cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
142
cur = grub_zalloc (sizeof (*cur));
146
cur->authenticated = 1;
150
cur->name = grub_strdup (user);
156
grub_list_push (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
159
return GRUB_ERR_NONE;
163
grub_auth_deauthenticate (const char *user)
165
struct grub_auth_user *cur;
166
cur = grub_named_list_find (GRUB_AS_NAMED_LIST (users), user);
168
return grub_error (GRUB_ERR_BAD_ARGUMENT, "user '%s' not found", user);
171
grub_free (cur->name);
172
grub_list_remove (GRUB_AS_LIST_P (&users), GRUB_AS_LIST (cur));
176
cur->authenticated = 0;
177
return GRUB_ERR_NONE;
181
is_authenticated (const char *userlist)
183
const char *superusers;
185
auto int hook (grub_list_t item);
186
int hook (grub_list_t item)
189
if (!((struct grub_auth_user *) item)->authenticated)
191
name = ((struct grub_auth_user *) item)->name;
193
return (userlist && grub_auth_strword (userlist, name))
194
|| grub_auth_strword (superusers, name);
197
superusers = grub_env_get ("superusers");
202
return grub_list_iterate (GRUB_AS_LIST (users), hook);
206
grub_auth_check_authentication (const char *userlist)
209
struct grub_auth_user *cur = NULL;
212
auto int hook (grub_list_t item);
213
int hook (grub_list_t item)
215
if (grub_auth_strcmp (login, ((struct grub_auth_user *) item)->name) == 0)
216
cur = (struct grub_auth_user *) item;
220
auto int hook_any (grub_list_t item);
221
int hook_any (grub_list_t item)
223
if (((struct grub_auth_user *) item)->callback)
224
cur = (struct grub_auth_user *) item;
228
grub_memset (login, 0, sizeof (login));
230
if (is_authenticated (userlist))
231
return GRUB_ERR_NONE;
233
if (!grub_cmdline_get ("Enter username: ", login, sizeof (login) - 1,
235
return GRUB_ACCESS_DENIED;
237
grub_list_iterate (GRUB_AS_LIST (users), hook);
239
if (!cur || ! cur->callback)
241
grub_list_iterate (GRUB_AS_LIST (users), hook_any);
243
/* No users present at all. */
245
return GRUB_ACCESS_DENIED;
247
/* Display any of available authentication schemes. */
248
err = cur->callback (login, 0);
250
return GRUB_ACCESS_DENIED;
252
err = cur->callback (login, cur->arg);
253
if (is_authenticated (userlist))
254
return GRUB_ERR_NONE;
255
return GRUB_ACCESS_DENIED;