Merge branch 'vendor/OPENSSL'
[dragonfly.git] / contrib / opie / opiegen.c
1 /* opiegen.c: Sample OTP generator based on the opiegenerator()
2               library routine.
3
4 %%% portions-copyright-cmetz-96
5 Portions of this software are Copyright 1996-1999 by Craig Metz, All Rights
6 Reserved. The Inner Net License Version 2 applies to these portions of
7 the software.
8 You should have received a copy of the license with this software. If
9 you didn't get a copy, you may request one from <license@inner.net>.
10
11         History:
12
13         Modified by cmetz for OPIE 2.3. OPIE_PASS_MAX changed to
14                 OPIE_SECRET_MAX. Send debug info to syslog.
15         Modified by cmetz for OPIE 2.2. Use FUNCTION definition et al.
16              Fixed include order.
17         Created at NRL for OPIE 2.2.
18 */
19 #include "opie_cfg.h"
20 #include <stdio.h>
21 #if DEBUG
22 #include <syslog.h>
23 #endif /* DEBUG */
24 #include "opie.h"
25
26 int main FUNCTION((argc, argv), int argc AND char *argv[])
27 {
28         char buffer[OPIE_CHALLENGE_MAX+1];
29         char secret[OPIE_SECRET_MAX+1];
30         char response[OPIE_RESPONSE_MAX+1];
31         int result;
32
33         if (opieinsecure()) {
34                 fputs("Sorry, but you don't seem to be on a secure terminal.\n", stderr);
35 #if !DEBUG
36                 exit(1);
37 #endif /* !DEBUG */
38         }
39
40         if (argc <= 1) {
41                 fputs("Challenge: ", stderr);
42                 if (!opiereadpass(buffer, sizeof(buffer)-1, 1))
43                   fprintf(stderr, "Error reading challenge!");
44         } else {
45                 char *ap, *ep, *c;
46                 int i;
47
48                 ep = buffer + sizeof(buffer) - 1;
49                 for (i = 1, ap = buffer; (i < argc) && (ap < ep); i++) {
50                         c = argv[i];
51                         while ((*(ap++) = *(c++)) && (ap < ep));
52                                 *(ap - 1) = ' ';
53                 }
54                 *(ap - 1) = 0;
55 #if DEBUG
56                 syslog(LOG_DEBUG, "opiegen: challenge is +%s+\n", buffer);
57 #endif /* DEBUG */
58         }
59         buffer[sizeof(buffer)-1] = 0;
60
61         fputs("Secret pass phrase: ", stderr);
62         if (!opiereadpass(secret, OPIE_SECRET_MAX, 0)) {
63           fputs("Error reading secret pass phrase!\n", stderr);
64           exit(1);
65         };
66
67         switch (result = opiegenerator(buffer, secret, response)) {
68                 case -2:
69                         fputs("Not a valid OTP secret pass phrase.\n", stderr);
70                         break;
71                 case -1:
72                         fputs("Error processing challenge!\n", stderr);
73                         break;
74                 case 1:
75                         fputs("Not a valid OTP challenge.\n", stderr);
76                         break;
77                 case 0:
78                         fputs(response, stdout);
79                         fputc('\n', stdout);
80                         fflush(stdout);
81                         memset(secret, 0, sizeof(secret));
82                         exit(0);
83                 default:
84                         fprintf(stderr, "Unknown error %d!\n", result);
85         }
86         memset(secret, 0, sizeof(secret));
87         return 1;
88 }