parsechallenge.c revision 22347
1/* parsechallenge.c: The _opieparsechallenge() library function.
2
3%%% copyright-cmetz
4This software is Copyright 1996 by Craig Metz, All Rights Reserved.
5The Inner Net License Version 2 applies to this software.
6You should have received a copy of the license with this software. If
7you didn't get a copy, you may request one from <license@inner.net>.
8
9        History:
10
11	Created by cmetz for OPIE 2.3 using generator.c as a guide.
12*/
13
14#include "opie_cfg.h"
15#if HAVE_STRING_H
16#include <string.h>
17#endif /* HAVE_STRING_H */
18#include "opie.h"
19
20struct algorithm {
21  char *name;
22  int num;
23};
24
25static struct algorithm algorithms[] = {
26  { "md5", 5 },
27  { "md4", 4 },
28  { "sha1", 3 },
29  { NULL, 0 },
30};
31
32int _opieparsechallenge FUNCTION((buffer, algorithm, sequence, seed), char *buffer AND int *algorithm AND int *sequence AND char **seed)
33{
34  char *c;
35
36  if (!(c = strchr(buffer, ' ')))
37    return 1;
38
39  {
40    struct algorithm *a;
41
42    for (a = algorithms; a->name && strncmp(buffer, a->name, (int)(c - buffer)); a++);
43    if (!a->name)
44      return -1;
45
46    *algorithm = a->num;
47  }
48
49  if ((*sequence = strtoul(++c, &c, 10)) > 9999)
50    return -1;
51
52  while(*c && isspace(*c)) c++;
53  if (!*c)
54    return -1;
55
56  buffer = c;
57  while(*c && !isspace(*c)) c++;
58
59  {
60    int i = (int)(c - buffer);
61
62    if ((i > OPIE_SEED_MAX) || (i < OPIE_SEED_MIN))
63      return -1;
64  }
65
66  *seed = buffer;
67  *c = 0;
68
69  return 0;
70}
71