kernel - Revert part of the contig allocation work
[dragonfly.git] / contrib / opie / libopie / writerec.c
blob7d0de092851bd361c213da882ede5cd99e2554bb
1 /* writerec.c: The __opiewriterec() library function.
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>.
9 History:
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.
17 $FreeBSD: head/contrib/opie/libopie/writerec.c 148506 2005-07-29 09:59:24Z ache $
19 #include "opie_cfg.h"
21 #include <stdio.h>
22 #if TM_IN_SYS_TIME
23 #include <sys/time.h>
24 #else /* TM_IN_SYS_TIME */
25 #include <time.h>
26 #endif /* TM_IN_SYS_TIME */
27 #include <sys/types.h>
28 #if HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif /* HAVE_UNISTD_H */
31 #if HAVE_STRING_H
32 #include <string.h>
33 #endif /* HAVE_STRING_H */
34 #if HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif /* HAVE_STDLIB_H */
37 #include <ctype.h>
38 #include "opie.h"
40 char *__opienone = "****************";
42 int __opiewriterec FUNCTION((opie), struct opie *opie)
44 char buf[64];
45 time_t now;
46 FILE *f;
47 int i = 0;
48 char *c;
50 time(&now);
51 if (strftime(buf, sizeof(buf), " %b %d,%Y %T", localtime(&now)) < 1)
52 return -1;
54 if (!(opie->opie_flags & __OPIE_FLAGS_READ)) {
55 struct opie opie2;
56 i = opielookup(&opie2, opie->opie_principal);
57 opie->opie_flags = opie2.opie_flags;
58 opie->opie_recstart = opie2.opie_recstart;
61 for (c = opie->opie_seed; *c; c++)
62 if (!isalnum(*c))
63 return -1;
65 if ((opie->opie_n < 0) || (opie->opie_n > 9999))
66 return -1;
68 switch(i) {
69 case 0:
70 if (!(f = __opieopen(KEY_FILE, 1, 0600)))
71 return -1;
72 if (fseek(f, opie->opie_recstart, SEEK_SET))
73 return -1;
74 break;
75 case 1:
76 if (!(f = __opieopen(KEY_FILE, 2, 0600)))
77 return -1;
78 break;
79 default:
80 return -1;
83 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, buf) < 1)
84 return -1;
86 fclose(f);
88 return 0;