Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * $FreeBSD: src/usr.bin/key/skey.c,v 1.6.6.1 2001/03/04 08:35:48 kris Exp $
13  * $DragonFly: src/usr.bin/key/skey.c,v 1.2 2003/06/17 04:29:27 dillon Exp $
14  */
15
16 #include <err.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 #ifdef  __MSDOS__
23 #include <dos.h>
24 #else   /* Assume BSD Unix */
25 #include <fcntl.h>
26 #endif
27
28 #include <skey.h>
29
30 static void usage __P((void));
31
32 int
33 main(argc,argv)
34 int argc;
35 char *argv[];
36 {
37         int n,cnt,i;
38         char passwd[256] /* ,passwd2[256] */;
39         char key[8];
40         char *seed;
41         char buf[33];
42         char *slash;
43
44         cnt = 1;
45         while((i = getopt(argc,argv,"n:")) != -1){
46                 switch(i){
47                 case 'n':
48                         cnt = atoi(optarg);
49                         break;
50                 }
51         }
52         /* could be in the form <number>/<seed> */
53         if(argc <= optind + 1){
54                 /*look for / in it */
55                 if(argc <= optind)
56                         usage();
57
58                 slash = strchr(argv[optind], '/');
59                 if(slash == NULL)
60                         usage();
61                 *slash++ = '\0';
62                 seed = slash;
63
64                 if((n = atoi(argv[optind])) < 0){
65                         warnx("%s not positive",argv[optind]);
66                         usage();
67                 }
68         }
69         else {
70
71                 if((n = atoi(argv[optind])) < 0){
72                         warnx("%s not positive",argv[optind]);
73                         usage();
74                 }
75                 seed = argv[++optind];
76         }
77         fprintf(stderr,"Reminder - Do not use this program while logged in via telnet or rlogin.\n");
78
79         /* Get user's secret password */
80         for(;;){
81                 fprintf(stderr,"Enter secret password: ");
82                 readpass(passwd,sizeof(passwd));
83                 break;
84         /************
85                 fprintf(stderr,"Again secret password: ");
86                 readpass(passwd2,sizeof(passwd));
87                 if(strcmp(passwd,passwd2) == 0) break;
88                 fprintf(stderr, "Sorry no match\n");
89         **************/
90
91         }
92
93         /* Crunch seed and password into starting key */
94         if(keycrunch(key,seed,passwd) != 0)
95                 errx(1, "key crunch failed");
96         if(cnt == 1){
97                 while(n-- != 0)
98                         f(key);
99                 printf("%s\n",btoe(buf,key));
100 #ifdef  HEXIN
101                 printf("%s\n",put8(buf,key));
102 #endif
103         } else {
104                 for(i=0;i<=n-cnt;i++)
105                         f(key);
106                 for(;i<=n;i++){
107 #ifdef  HEXIN
108                         printf("%d: %-29s  %s\n",i,btoe(buf,key),put8(buf,key));
109 #else
110                         printf("%d: %-29s\n",i,btoe(buf,key));
111 #endif
112                         f(key);
113                 }
114         }
115         return 0;
116 }
117
118 static void
119 usage()
120 {
121         fprintf(stderr,"usage: key [-n count] <sequence #>[/] <key>\n");
122         exit(1);
123 }