xref: /freebsd-13-stable/contrib/opie/libopie/writerec.c (revision 85ca5e684ec2bb1ee12f72684a2f282f79b21ee3)
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 $FreeBSD$
18 */
19 #include "opie_cfg.h"
20 
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"
39 
40 char *__opienone = "****************";
41 
42 int __opiewriterec FUNCTION((opie), struct opie *opie)
43 {
44   char buf[17], buf2[64];
45   time_t now;
46   FILE *f, *f2 = NULL;
47   int i = 0;
48   char *c;
49 
50   time(&now);
51   if (strftime(buf2, sizeof(buf2), " %b %d,%Y %T", localtime(&now)) < 1)
52     return -1;
53 
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;
59   }
60 
61   for (c = opie->opie_seed; *c; c++)
62     if (!isalnum(*c))
63       return -1;
64 
65   if ((opie->opie_n < 0) || (opie->opie_n > 9999))
66       return -1;
67 
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;
81   }
82 
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, buf2) < 1)
84     return -1;
85 
86   fclose(f);
87 
88   return 0;
89 }
90