159118Skris/* parsechallenge.c: The __opieparsechallenge() library function.
222347Spst
329964Sache%%% copyright-cmetz-96
492906SmarkmThis software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
592906SmarkmThe Inner Net License Version 3 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
1192906Smarkm	Modified by cmetz for OPIE 2.4. Use OPIE_SEQUENCE_MAX, check for
1292906Smarkm	        sequence number of zero.
1359118Skris	Modified by cmetz for OPIE 2.32. Check for extended response sets.
1459118Skris		Change prefix to double underscore.
1522347Spst	Created by cmetz for OPIE 2.3 using generator.c as a guide.
1622347Spst*/
1722347Spst
1822347Spst#include "opie_cfg.h"
1922347Spst#if HAVE_STRING_H
2022347Spst#include <string.h>
2122347Spst#endif /* HAVE_STRING_H */
22257264Ssbruno#include <ctype.h>
23257264Ssbruno#include <stdlib.h>
2422347Spst#include "opie.h"
2522347Spst
2622347Spststruct algorithm {
2722347Spst  char *name;
2822347Spst  int num;
2922347Spst};
3022347Spst
3122347Spststatic struct algorithm algorithms[] = {
3222347Spst  { "md5", 5 },
3322347Spst  { "md4", 4 },
3422347Spst  { "sha1", 3 },
3522347Spst  { NULL, 0 },
3622347Spst};
3722347Spst
3859118Skrisint __opieparsechallenge FUNCTION((buffer, algorithm, sequence, seed, exts), char *buffer AND int *algorithm AND int *sequence AND char **seed AND int *exts)
3922347Spst{
4022347Spst  char *c;
4122347Spst
4222347Spst  if (!(c = strchr(buffer, ' ')))
4322347Spst    return 1;
4422347Spst
4522347Spst  {
4622347Spst    struct algorithm *a;
4722347Spst
4822347Spst    for (a = algorithms; a->name && strncmp(buffer, a->name, (int)(c - buffer)); a++);
4922347Spst    if (!a->name)
5022347Spst      return -1;
5122347Spst
5222347Spst    *algorithm = a->num;
5322347Spst  }
5422347Spst
5592906Smarkm  if (((*sequence = strtoul(++c, &c, 10)) > OPIE_SEQUENCE_MAX) || !*sequence)
5622347Spst    return -1;
5722347Spst
5822347Spst  while(*c && isspace(*c)) c++;
5922347Spst  if (!*c)
6022347Spst    return -1;
6122347Spst
6222347Spst  buffer = c;
6322347Spst  while(*c && !isspace(*c)) c++;
6422347Spst
6522347Spst  {
6622347Spst    int i = (int)(c - buffer);
6722347Spst
6822347Spst    if ((i > OPIE_SEED_MAX) || (i < OPIE_SEED_MIN))
6922347Spst      return -1;
7022347Spst  }
7122347Spst
7222347Spst  *seed = buffer;
7359118Skris  *(c++) = 0;
7422347Spst
7559118Skris  while(*c && !isspace(*c)) c++;
7659118Skris  if (*c && !strncmp(c, "ext", 3))
7759118Skris    *exts = 1;
7859118Skris  else
7959118Skris    *exts = 0;
8059118Skris
8122347Spst  return 0;
8222347Spst}
83