1/* parsechallenge.c: The __opieparsechallenge() library function.
2
3%%% copyright-cmetz-96
4This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
5The Inner Net License Version 3 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	Modified by cmetz for OPIE 2.4. Use OPIE_SEQUENCE_MAX, check for
12	        sequence number of zero.
13	Modified by cmetz for OPIE 2.32. Check for extended response sets.
14		Change prefix to double underscore.
15	Created by cmetz for OPIE 2.3 using generator.c as a guide.
16*/
17
18#include "opie_cfg.h"
19#if HAVE_STRING_H
20#include <string.h>
21#endif /* HAVE_STRING_H */
22#include <ctype.h>
23#include <stdlib.h>
24#include "opie.h"
25
26struct algorithm {
27  char *name;
28  int num;
29};
30
31static struct algorithm algorithms[] = {
32  { "md5", 5 },
33  { "md4", 4 },
34  { "sha1", 3 },
35  { NULL, 0 },
36};
37
38int __opieparsechallenge FUNCTION((buffer, algorithm, sequence, seed, exts), char *buffer AND int *algorithm AND int *sequence AND char **seed AND int *exts)
39{
40  char *c;
41
42  if (!(c = strchr(buffer, ' ')))
43    return 1;
44
45  {
46    struct algorithm *a;
47
48    for (a = algorithms; a->name && strncmp(buffer, a->name, (int)(c - buffer)); a++);
49    if (!a->name)
50      return -1;
51
52    *algorithm = a->num;
53  }
54
55  if (((*sequence = strtoul(++c, &c, 10)) > OPIE_SEQUENCE_MAX) || !*sequence)
56    return -1;
57
58  while(*c && isspace(*c)) c++;
59  if (!*c)
60    return -1;
61
62  buffer = c;
63  while(*c && !isspace(*c)) c++;
64
65  {
66    int i = (int)(c - buffer);
67
68    if ((i > OPIE_SEED_MAX) || (i < OPIE_SEED_MIN))
69      return -1;
70  }
71
72  *seed = buffer;
73  *(c++) = 0;
74
75  while(*c && !isspace(*c)) c++;
76  if (*c && !strncmp(c, "ext", 3))
77    *exts = 1;
78  else
79    *exts = 0;
80
81  return 0;
82}
83