~jonathank89/burg/burg-percise

« back to all changes in this revision

Viewing changes to normal/auth.c

merge mainline into mips

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
struct grub_auth_user *users = NULL;
38
38
 
39
 
int
40
 
grub_auth_strcmp (const char *s1, const char *s2)
41
 
{
42
 
  int ret;
43
 
  grub_uint64_t end;
44
 
 
45
 
  end = grub_get_time_ms () + 100;
46
 
  ret = grub_strcmp (s1, s2);
47
 
 
48
 
  /* This prevents an attacker from deriving information about the
49
 
     password from the time it took to execute this function.  */
50
 
  while (grub_get_time_ms () < end);
51
 
 
52
 
  return ret;
53
 
}
54
 
 
55
 
static int
56
 
grub_iswordseparator (int c)
57
 
{
58
 
  return (grub_isspace (c) || c == ',' || c == ';' || c == '|' || c == '&');
59
 
}
60
 
 
61
 
int
62
 
grub_auth_strword (const char *haystack, const char *needle)
63
 
{
64
 
  const char *n_pos = needle;
65
 
  int found = 0;
66
 
 
67
 
  while (grub_iswordseparator (*haystack))
68
 
    haystack++;
69
 
 
70
 
  while (*haystack)
71
 
    {
72
 
      int ok = 1;
73
 
      /* Crawl both the needle and the haystack word we're on.  */
74
 
      while(*haystack && !grub_iswordseparator (*haystack))
75
 
        {
76
 
          if (*haystack == *n_pos && ok)
77
 
            n_pos++;
78
 
          else
79
 
            ok = 0;
80
 
 
81
 
          haystack++;
82
 
        }
83
 
 
84
 
      if (ok)
85
 
        found = 1;
86
 
    }
87
 
 
88
 
  return found;
89
 
}
90
 
 
91
39
grub_err_t
92
40
grub_auth_register_authentication (const char *user,
93
41
                                   grub_auth_callback_t callback,
194
142
      return 0;
195
143
    name = ((struct grub_auth_user *) item)->name;
196
144
 
197
 
    return (userlist && grub_auth_strword (userlist, name))
198
 
      || grub_auth_strword (superusers, name);
 
145
    return (userlist && grub_strword (userlist, name))
 
146
      || grub_strword (superusers, name);
199
147
  }
200
148
 
201
149
  superusers = grub_env_get ("superusers");
206
154
  return grub_list_iterate (GRUB_AS_LIST (users), hook);
207
155
}
208
156
 
 
157
static int
 
158
grub_username_get (char buf[], unsigned buf_size)
 
159
{
 
160
  unsigned cur_len = 0;
 
161
  int key;
 
162
 
 
163
  while (1)
 
164
    {
 
165
      key = GRUB_TERM_ASCII_CHAR (grub_getkey ()); 
 
166
      if (key == '\n' || key == '\r')
 
167
        break;
 
168
 
 
169
      if (key == '\e')
 
170
        {
 
171
          cur_len = 0;
 
172
          break;
 
173
        }
 
174
 
 
175
      if (key == '\b')
 
176
        {
 
177
          cur_len--;
 
178
          grub_printf ("\b");
 
179
          continue;
 
180
        }
 
181
 
 
182
      if (!grub_isprint (key))
 
183
        continue;
 
184
 
 
185
      if (cur_len + 2 < buf_size)
 
186
        {
 
187
          buf[cur_len++] = key;
 
188
          grub_putchar (key);
 
189
        }
 
190
    }
 
191
 
 
192
  grub_memset (buf + cur_len, 0, buf_size - cur_len);
 
193
 
 
194
  grub_putchar ('\n');
 
195
  grub_refresh ();
 
196
 
 
197
  return (key != '\e');
 
198
}
 
199
 
209
200
grub_err_t
210
201
grub_auth_check_authentication (const char *userlist)
211
202
{
213
204
  struct grub_auth_user *cur = NULL;
214
205
  grub_err_t err;
215
206
  static unsigned long punishment_delay = 1;
 
207
  char entered[GRUB_AUTH_MAX_PASSLEN];
216
208
 
217
209
  auto int hook (grub_list_t item);
218
210
  int hook (grub_list_t item)
219
211
  {
220
 
    if (grub_auth_strcmp (login, ((struct grub_auth_user *) item)->name) == 0)
 
212
    if (grub_strcmp (login, ((struct grub_auth_user *) item)->name) == 0)
221
213
      cur = (struct grub_auth_user *) item;
222
214
    return 0;
223
215
  }
238
230
      return GRUB_ERR_NONE;
239
231
    }
240
232
 
241
 
  if (!grub_cmdline_get (N_("Enter username:"), login, sizeof (login) - 1,
242
 
                         0, 0, 0))
 
233
  grub_puts_ (N_("Enter username: "));
 
234
 
 
235
  if (!grub_username_get (login, sizeof (login) - 1))
 
236
    goto access_denied;
 
237
 
 
238
  grub_puts_ (N_("Enter password: "));
 
239
 
 
240
  if (!grub_password_get (entered, GRUB_AUTH_MAX_PASSLEN))
243
241
    goto access_denied;
244
242
 
245
243
  grub_list_iterate (GRUB_AS_LIST (users), hook);
246
244
 
247
245
  if (!cur || ! cur->callback)
248
 
    {
249
 
      grub_list_iterate (GRUB_AS_LIST (users), hook_any);
250
 
 
251
 
      /* No users present at all.  */
252
 
      if (!cur)
253
 
        goto access_denied;
254
 
 
255
 
      /* Display any of available authentication schemes.  */
256
 
      err = cur->callback (login, 0);
257
 
 
258
 
      goto access_denied;
259
 
    }
260
 
  err = cur->callback (login, cur->arg);
 
246
    goto access_denied;
 
247
 
 
248
  err = cur->callback (login, entered, cur->arg);
261
249
  if (is_authenticated (userlist))
262
250
    {
263
251
      punishment_delay = 1;