5ba53fe08732b2932ab5901a018d5b0f018b7e3b
[dragonfly.git] / contrib / opie / libopie / writerec.c
1 /* writerec.c: The __opiewriterec() library function.
2
3 %%% copyright-cmetz-96
4 This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
5 The Inner Net License Version 3 applies to this software.
6 You should have received a copy of the license with this software. If
7 you 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. Check that seed and sequence number are
12                 valid.
13         Modified by cmetz for OPIE 2.31. Removed active attack protection
14                 support. Fixed passwd bug.
15         Created by cmetz for OPIE 2.3 from passwd.c.
16 */
17 #include "opie_cfg.h"
18
19 #include <stdio.h>
20 #if TM_IN_SYS_TIME
21 #include <sys/time.h>
22 #else /* TM_IN_SYS_TIME */
23 #include <time.h>
24 #endif /* TM_IN_SYS_TIME */
25 #include <sys/types.h>
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif /* HAVE_UNISTD_H */
29 #if HAVE_STRING_H
30 #include <string.h>
31 #endif /* HAVE_STRING_H */
32 #if HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif /* HAVE_STDLIB_H */
35 #include <ctype.h>
36 #include "opie.h"
37
38 char *__opienone = "****************";
39
40 int __opiewriterec FUNCTION((opie), struct opie *opie)
41 {
42   char buf[17], buf2[64];
43   time_t now;
44   FILE *f, *f2 = NULL;
45   int i = 0;
46   char *c;
47
48   time(&now);
49   if (strftime(buf2, sizeof(buf2), " %b %d,%Y %T", localtime(&now)) < 1)
50     return -1;
51
52   if (!(opie->opie_flags & __OPIE_FLAGS_READ)) {
53     struct opie opie2;
54     i = opielookup(&opie2, opie->opie_principal);
55     opie->opie_flags = opie2.opie_flags;
56     opie->opie_recstart = opie2.opie_recstart;
57   }
58
59   for (c = opie->opie_seed; *c; c++)
60     if (!isalnum(*c))
61       return -1;
62
63   if ((opie->opie_n < 0) || (opie->opie_n > 9999))
64       return -1;
65
66   switch(i) {
67   case 0:
68     if (!(f = __opieopen(KEY_FILE, 1, 0644)))
69       return -1;
70     if (fseek(f, opie->opie_recstart, SEEK_SET))
71       return -1;
72     break;
73   case 1:
74     if (!(f = __opieopen(KEY_FILE, 2, 0644)))
75       return -1;
76     break;
77   default:
78     return -1;
79   }
80
81   if (fprintf(f, "%s %04d %-16s %s %-21s\n", opie->opie_principal, opie->opie_n, opie->opie_seed, opie->opie_val ? opie->opie_val : __opienone, buf2) < 1)
82     return -1;
83
84   fclose(f);
85
86   return 0;
87 }