Deleted Added
full compact
readrec.c (59118) readrec.c (92906)
1/* readrec.c: The __opiereadrec() library function.
2
3%%% copyright-cmetz-96
1/* readrec.c: The __opiereadrec() library function.
2
3%%% copyright-cmetz-96
4This software is Copyright 1996-1998 by Craig Metz, All Rights Reserved.
5The Inner Net License Version 2 applies to this software.
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
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.4. Check that seed, sequence number, and
12 response values are valid.
11 Modified by cmetz for OPIE 2.31. Removed active attack protection
12 support. Fixed a debug message typo. Keep going after bogus
13 records. Set read flag.
14 Created by cmetz for OPIE 2.3.
15*/
16#include "opie_cfg.h"
17
18#include <stdio.h>
19#include <sys/types.h>
20#include <errno.h>
21#if HAVE_UNISTD_H
22#include <unistd.h>
23#endif /* HAVE_UNISTD_H */
24#if HAVE_STRING_H
25#include <string.h>
26#endif /* HAVE_STRING_H */
27#if HAVE_STDLIB_H
28#include <stdlib.h>
29#endif /* HAVE_STDLIB_H */
30#if HAVE_FCNTL_H
31#include <fcntl.h>
32#endif /* HAVE_FCNTL_H */
33#include <ctype.h>
34#include <errno.h>
35#if DEBUG
36#include <syslog.h>
37#endif /* DEBUG */
38#include "opie.h"
39
40static int parserec FUNCTION((opie), struct opie *opie)
41{
42 char *c, *c2;
43
44 if (!(c2 = strchr(opie->opie_principal = opie->opie_buf, ' ')))
45 return -1;
46
47 while(*c2 == ' ') c2++;
48 *(c2 - 1) = 0;
49
50 if (!(c2 = strchr(c = c2, ' ')))
51 return -1;
52
53 *(c2++) = 0;
54
55 {
56 char *c3;
57
58 opie->opie_n = strtoul(c, &c3, 10);
59
13 Modified by cmetz for OPIE 2.31. Removed active attack protection
14 support. Fixed a debug message typo. Keep going after bogus
15 records. Set read flag.
16 Created by cmetz for OPIE 2.3.
17*/
18#include "opie_cfg.h"
19
20#include <stdio.h>
21#include <sys/types.h>
22#include <errno.h>
23#if HAVE_UNISTD_H
24#include <unistd.h>
25#endif /* HAVE_UNISTD_H */
26#if HAVE_STRING_H
27#include <string.h>
28#endif /* HAVE_STRING_H */
29#if HAVE_STDLIB_H
30#include <stdlib.h>
31#endif /* HAVE_STDLIB_H */
32#if HAVE_FCNTL_H
33#include <fcntl.h>
34#endif /* HAVE_FCNTL_H */
35#include <ctype.h>
36#include <errno.h>
37#if DEBUG
38#include <syslog.h>
39#endif /* DEBUG */
40#include "opie.h"
41
42static int parserec FUNCTION((opie), struct opie *opie)
43{
44 char *c, *c2;
45
46 if (!(c2 = strchr(opie->opie_principal = opie->opie_buf, ' ')))
47 return -1;
48
49 while(*c2 == ' ') c2++;
50 *(c2 - 1) = 0;
51
52 if (!(c2 = strchr(c = c2, ' ')))
53 return -1;
54
55 *(c2++) = 0;
56
57 {
58 char *c3;
59
60 opie->opie_n = strtoul(c, &c3, 10);
61
60 if (*c3)
62 if (*c3 || (opie->opie_n <= 0) || (opie->opie_n > 9999))
61 return -1;
62 };
63
64 if (!(c2 = strchr(opie->opie_seed = c2, ' ')))
65 return -1;
66
67 *(c2++) = 0;
68
63 return -1;
64 };
65
66 if (!(c2 = strchr(opie->opie_seed = c2, ' ')))
67 return -1;
68
69 *(c2++) = 0;
70
71 for (c = opie->opie_seed; *c; c++)
72 if (!isalnum(*c))
73 return -1;
74
69 while(*c2 == ' ') c2++;
70
71 if (!(c2 = strchr(opie->opie_val = c2, ' ')))
72 return -1;
73
74 *(c2++) = 0;
75
75 while(*c2 == ' ') c2++;
76
77 if (!(c2 = strchr(opie->opie_val = c2, ' ')))
78 return -1;
79
80 *(c2++) = 0;
81
82 {
83 struct opie_otpkey otpkey;
84
85 if (!opieatob8(&otpkey, opie->opie_val))
86 return -1;
87 }
88
76 return 0;
77}
78
79int __opiereadrec FUNCTION((opie), struct opie *opie)
80{
81 FILE *f = NULL;
82 int rval = -1;
83
84 if (!(f = __opieopen(KEY_FILE, 0, 0644))) {
85#if DEBUG
86 syslog(LOG_DEBUG, "__opiereadrec: __opieopen(KEY_FILE..) failed!");
87#endif /* DEBUG */
88 goto ret;
89 }
90
91 {
92 int i;
93
94 if ((i = open(KEY_FILE, O_RDWR)) < 0) {
95 opie->opie_flags &= ~__OPIE_FLAGS_RW;
96#if DEBUG
97 syslog(LOG_DEBUG, "__opiereadrec: open(KEY_FILE, O_RDWR) failed: %s", strerror(errno));
98#endif /* DEBUG */
99 } else {
100 close(i);
101 opie->opie_flags |= __OPIE_FLAGS_RW;
102 }
103 }
104
105 if (opie->opie_buf[0]) {
106 if (fseek(f, opie->opie_recstart, SEEK_SET))
107 goto ret;
108
109 if (fgets(opie->opie_buf, sizeof(opie->opie_buf), f))
110 goto ret;
111
112 if (parserec(opie))
113 goto ret;
114
115 opie->opie_flags |= __OPIE_FLAGS_READ;
116 rval = 0;
117 goto ret;
118 }
119
120 if (!opie->opie_principal)
121 goto ret;
122
123 {
124 char *c, principal[OPIE_PRINCIPAL_MAX];
125 int i;
126
127 if (c = strchr(opie->opie_principal, ':'))
128 *c = 0;
129 if (strlen(opie->opie_principal) > OPIE_PRINCIPAL_MAX)
130 (opie->opie_principal)[OPIE_PRINCIPAL_MAX] = 0;
131
132 strcpy(principal, opie->opie_principal);
133
134 do {
135 if ((opie->opie_recstart = ftell(f)) < 0)
136 goto ret;
137
138 if (!fgets(opie->opie_buf, sizeof(opie->opie_buf), f)) {
139 rval = 1;
140 goto ret;
141 }
142
143 if (parserec(opie))
144 continue;
145 } while (strcmp(principal, opie->opie_principal));
146
147 rval = 0;
148 }
149
150ret:
151 if (f)
152 fclose(f);
153 return rval;
154}
89 return 0;
90}
91
92int __opiereadrec FUNCTION((opie), struct opie *opie)
93{
94 FILE *f = NULL;
95 int rval = -1;
96
97 if (!(f = __opieopen(KEY_FILE, 0, 0644))) {
98#if DEBUG
99 syslog(LOG_DEBUG, "__opiereadrec: __opieopen(KEY_FILE..) failed!");
100#endif /* DEBUG */
101 goto ret;
102 }
103
104 {
105 int i;
106
107 if ((i = open(KEY_FILE, O_RDWR)) < 0) {
108 opie->opie_flags &= ~__OPIE_FLAGS_RW;
109#if DEBUG
110 syslog(LOG_DEBUG, "__opiereadrec: open(KEY_FILE, O_RDWR) failed: %s", strerror(errno));
111#endif /* DEBUG */
112 } else {
113 close(i);
114 opie->opie_flags |= __OPIE_FLAGS_RW;
115 }
116 }
117
118 if (opie->opie_buf[0]) {
119 if (fseek(f, opie->opie_recstart, SEEK_SET))
120 goto ret;
121
122 if (fgets(opie->opie_buf, sizeof(opie->opie_buf), f))
123 goto ret;
124
125 if (parserec(opie))
126 goto ret;
127
128 opie->opie_flags |= __OPIE_FLAGS_READ;
129 rval = 0;
130 goto ret;
131 }
132
133 if (!opie->opie_principal)
134 goto ret;
135
136 {
137 char *c, principal[OPIE_PRINCIPAL_MAX];
138 int i;
139
140 if (c = strchr(opie->opie_principal, ':'))
141 *c = 0;
142 if (strlen(opie->opie_principal) > OPIE_PRINCIPAL_MAX)
143 (opie->opie_principal)[OPIE_PRINCIPAL_MAX] = 0;
144
145 strcpy(principal, opie->opie_principal);
146
147 do {
148 if ((opie->opie_recstart = ftell(f)) < 0)
149 goto ret;
150
151 if (!fgets(opie->opie_buf, sizeof(opie->opie_buf), f)) {
152 rval = 1;
153 goto ret;
154 }
155
156 if (parserec(opie))
157 continue;
158 } while (strcmp(principal, opie->opie_principal));
159
160 rval = 0;
161 }
162
163ret:
164 if (f)
165 fclose(f);
166 return rval;
167}