1 /* readrec.c: The __opiereadrec() library function.
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>.
11 Modified by cmetz for OPIE 2.4. Check that seed, sequence number, and
12 response values are valid.
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.
23 #include <sys/types.h>
27 #endif /* HAVE_UNISTD_H */
30 #endif /* HAVE_STRING_H */
33 #endif /* HAVE_STDLIB_H */
36 #endif /* HAVE_FCNTL_H */
44 static int parserec FUNCTION((opie), struct opie *opie)
48 if (!(c2 = strchr(opie->opie_principal = opie->opie_buf, ' ')))
51 while(*c2 == ' ') c2++;
54 if (!(c2 = strchr(c = c2, ' ')))
62 opie->opie_n = strtoul(c, &c3, 10);
64 if (*c3 || (opie->opie_n <= 0) || (opie->opie_n > 9999))
68 if (!(c2 = strchr(opie->opie_seed = c2, ' ')))
73 for (c = opie->opie_seed; *c; c++)
77 while(*c2 == ' ') c2++;
79 if (!(c2 = strchr(opie->opie_val = c2, ' ')))
85 struct opie_otpkey otpkey;
87 if (!opieatob8(&otpkey, opie->opie_val))
94 int __opiereadrec FUNCTION((opie), struct opie *opie)
99 if (!(f = __opieopen(KEY_FILE, 0, 0600))) {
101 syslog(LOG_DEBUG, "__opiereadrec: __opieopen(KEY_FILE..) failed!");
109 if ((i = open(KEY_FILE, O_RDWR)) < 0) {
110 opie->opie_flags &= ~__OPIE_FLAGS_RW;
112 syslog(LOG_DEBUG, "__opiereadrec: open(KEY_FILE, O_RDWR) failed: %s", strerror(errno));
116 opie->opie_flags |= __OPIE_FLAGS_RW;
120 if (opie->opie_buf[0]) {
121 if (fseek(f, opie->opie_recstart, SEEK_SET))
124 if (fgets(opie->opie_buf, sizeof(opie->opie_buf), f))
130 opie->opie_flags |= __OPIE_FLAGS_READ;
135 if (!opie->opie_principal)
139 char *c, principal[OPIE_PRINCIPAL_MAX];
142 if (c = strchr(opie->opie_principal, ':'))
144 if (strlen(opie->opie_principal) > OPIE_PRINCIPAL_MAX)
145 (opie->opie_principal)[OPIE_PRINCIPAL_MAX] = 0;
147 strcpy(principal, opie->opie_principal);
150 if ((opie->opie_recstart = ftell(f)) < 0)
153 if (!fgets(opie->opie_buf, sizeof(opie->opie_buf), f)) {
160 } while (strcmp(principal, opie->opie_principal));