Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / key / skey.c
1 /* Stand-alone program for computing responses to S/Key challenges.
2  * Takes the iteration count and seed as command line args, prompts
3  * for the user's key, and produces both word and hex format responses.
4  *
5  * Usage example:
6  *      >skey 88 ka9q2
7  *      Enter password:
8  *      OMEN US HORN OMIT BACK AHOY
9  *      C848 666B 6435 0A93
10  *      >
11  */
12
13 #ifndef lint
14 static const char rcsid[] =
15   "$FreeBSD: src/usr.bin/key/skey.c,v 1.6.6.1 2001/03/04 08:35:48 kris Exp $";
16 #endif /* not lint */
17
18 #include <err.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #ifdef  __MSDOS__
25 #include <dos.h>
26 #else   /* Assume BSD Unix */
27 #include <fcntl.h>
28 #endif
29
30 #include <skey.h>
31
32 static void usage __P((void));
33
34 int
35 main(argc,argv)
36 int argc;
37 char *argv[];
38 {
39         int n,cnt,i;
40         char passwd[256] /* ,passwd2[256] */;
41         char key[8];
42         char *seed;
43         char buf[33];
44         char *slash;
45
46         cnt = 1;
47         while((i = getopt(argc,argv,"n:")) != -1){
48                 switch(i){
49                 case 'n':
50                         cnt = atoi(optarg);
51                         break;
52                 }
53         }
54         /* could be in the form <number>/<seed> */
55         if(argc <= optind + 1){
56                 /*look for / in it */
57                 if(argc <= optind)
58                         usage();
59
60                 slash = strchr(argv[optind], '/');
61                 if(slash == NULL)
62                         usage();
63                 *slash++ = '\0';
64                 seed = slash;
65
66                 if((n = atoi(argv[optind])) < 0){
67                         warnx("%s not positive",argv[optind]);
68                         usage();
69                 }
70         }
71         else {
72
73                 if((n = atoi(argv[optind])) < 0){
74                         warnx("%s not positive",argv[optind]);
75                         usage();
76                 }
77                 seed = argv[++optind];
78         }
79         fprintf(stderr,"Reminder - Do not use this program while logged in via telnet or rlogin.\n");
80
81         /* Get user's secret password */
82         for(;;){
83                 fprintf(stderr,"Enter secret password: ");
84                 readpass(passwd,sizeof(passwd));
85                 break;
86         /************
87                 fprintf(stderr,"Again secret password: ");
88                 readpass(passwd2,sizeof(passwd));
89                 if(strcmp(passwd,passwd2) == 0) break;
90                 fprintf(stderr, "Sorry no match\n");
91         **************/
92
93         }
94
95         /* Crunch seed and password into starting key */
96         if(keycrunch(key,seed,passwd) != 0)
97                 errx(1, "key crunch failed");
98         if(cnt == 1){
99                 while(n-- != 0)
100                         f(key);
101                 printf("%s\n",btoe(buf,key));
102 #ifdef  HEXIN
103                 printf("%s\n",put8(buf,key));
104 #endif
105         } else {
106                 for(i=0;i<=n-cnt;i++)
107                         f(key);
108                 for(;i<=n;i++){
109 #ifdef  HEXIN
110                         printf("%d: %-29s  %s\n",i,btoe(buf,key),put8(buf,key));
111 #else
112                         printf("%d: %-29s\n",i,btoe(buf,key));
113 #endif
114                         f(key);
115                 }
116         }
117         return 0;
118 }
119
120 static void
121 usage()
122 {
123         fprintf(stderr,"usage: key [-n count] <sequence #>[/] <key>\n");
124         exit(1);
125 }