parsechallenge.c revision 22347
122347Spst/* parsechallenge.c: The _opieparsechallenge() library function.
222347Spst
322347Spst%%% copyright-cmetz
422347SpstThis software is Copyright 1996 by Craig Metz, All Rights Reserved.
522347SpstThe Inner Net License Version 2 applies to this software.
622347SpstYou should have received a copy of the license with this software. If
722347Spstyou didn't get a copy, you may request one from <license@inner.net>.
822347Spst
922347Spst        History:
1022347Spst
1122347Spst	Created by cmetz for OPIE 2.3 using generator.c as a guide.
1222347Spst*/
1322347Spst
1422347Spst#include "opie_cfg.h"
1522347Spst#if HAVE_STRING_H
1622347Spst#include <string.h>
1722347Spst#endif /* HAVE_STRING_H */
1822347Spst#include "opie.h"
1922347Spst
2022347Spststruct algorithm {
2122347Spst  char *name;
2222347Spst  int num;
2322347Spst};
2422347Spst
2522347Spststatic struct algorithm algorithms[] = {
2622347Spst  { "md5", 5 },
2722347Spst  { "md4", 4 },
2822347Spst  { "sha1", 3 },
2922347Spst  { NULL, 0 },
3022347Spst};
3122347Spst
3222347Spstint _opieparsechallenge FUNCTION((buffer, algorithm, sequence, seed), char *buffer AND int *algorithm AND int *sequence AND char **seed)
3322347Spst{
3422347Spst  char *c;
3522347Spst
3622347Spst  if (!(c = strchr(buffer, ' ')))
3722347Spst    return 1;
3822347Spst
3922347Spst  {
4022347Spst    struct algorithm *a;
4122347Spst
4222347Spst    for (a = algorithms; a->name && strncmp(buffer, a->name, (int)(c - buffer)); a++);
4322347Spst    if (!a->name)
4422347Spst      return -1;
4522347Spst
4622347Spst    *algorithm = a->num;
4722347Spst  }
4822347Spst
4922347Spst  if ((*sequence = strtoul(++c, &c, 10)) > 9999)
5022347Spst    return -1;
5122347Spst
5222347Spst  while(*c && isspace(*c)) c++;
5322347Spst  if (!*c)
5422347Spst    return -1;
5522347Spst
5622347Spst  buffer = c;
5722347Spst  while(*c && !isspace(*c)) c++;
5822347Spst
5922347Spst  {
6022347Spst    int i = (int)(c - buffer);
6122347Spst
6222347Spst    if ((i > OPIE_SEED_MAX) || (i < OPIE_SEED_MIN))
6322347Spst      return -1;
6422347Spst  }
6522347Spst
6622347Spst  *seed = buffer;
6722347Spst  *c = 0;
6822347Spst
6922347Spst  return 0;
7022347Spst}
71