1/* passwd.c: The opiepasswd() 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.32. Renamed mode to flags. Made flag
12		values symbolic constants. Added a flag for insecure override
13		support.
14	Modified by cmetz for OPIE 2.31. Removed active attack protection
15		support.
16	Modified by cmetz for OPIE 2.3. Split most of the function off
17		and turned this into a front-end for the new __opiewriterec().
18		Added code to compute the key from the secret. Use the opie_
19		prefix. Use new opieatob8() and opiebtoa8() return values.
20	Created by cmetz for OPIE 2.22.
21*/
22
23#include <string.h>
24#include "opie_cfg.h"
25#include "opie.h"
26
27int opiepasswd FUNCTION((old, flags, principal, n, seed, ks), struct opie *old AND int flags AND char *principal AND int n AND char *seed AND char *ks)
28{
29  int i;
30  struct opie opie;
31
32  if ((flags & OPIEPASSWD_CONSOLE) && opieinsecure())
33#if INSECURE_OVERRIDE
34    if (!(flags & OPIEPASSWD_FORCE))
35#endif /* INSECURE_OVERRIDE */
36    return -1;
37
38  memset(&opie, 0, sizeof(struct opie));
39
40  if (old) {
41    opie.opie_flags = old->opie_flags;
42    opie.opie_recstart = old->opie_recstart;
43  }
44
45  opie.opie_principal = principal;
46  opie.opie_n = n;
47  opie.opie_seed = seed;
48
49  if (ks) {
50    struct opie_otpkey key;
51
52    if (flags & OPIEPASSWD_CONSOLE) {
53      if (opiekeycrunch(MDX, &key, seed, ks))
54	return -1;
55      for (i = n; i; i--)
56	opiehash(&key, MDX);
57      if (!(opie.opie_val = opiebtoa8(opie.opie_buf, &key)))
58	return -1;
59    } else {
60      if ((opieetob(&key, ks) != 1) && !opieatob8(&key, ks))
61	  return 1;
62      if (!(opie.opie_val = opiebtoa8(opie.opie_buf, &key)))
63	return 1;
64    }
65  }
66
67  if (opielock(principal))
68    return -1;
69
70  i = __opiewriterec(&opie);
71
72  if (opieunlock())
73    return -1;
74
75  return i;
76}
77