xref: /netbsd-src/usr.bin/skey/skey.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: skey.c,v 1.5 1997/06/22 18:51:47 christos Exp $	*/
2 
3 /*
4  * S/KEY v1.1b (skey.c)
5  *
6  * Authors:
7  *          Neil M. Haller <nmh@thumper.bellcore.com>
8  *          Philip R. Karn <karn@chicago.qualcomm.com>
9  *          John S. Walden <jsw@thumper.bellcore.com>
10  *          Scott Chasin <chasin@crimelab.com>
11  *
12  *
13  * Stand-alone program for computing responses to S/Key challenges.
14  * Takes the iteration count and seed as command line args, prompts
15  * for the user's key, and produces both word and hex format responses.
16  *
17  * Usage example:
18  *	>skey 88 ka9q2
19  *	Enter password:
20  *	OMEN US HORN OMIT BACK AHOY
21  *	>
22  */
23 
24 #include <sys/cdefs.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <sgtty.h>
30 #include <skey.h>
31 
32 void    usage __P((char *));
33 
34 int
35 main(argc, argv)
36 	int	argc;
37 	char	*argv[];
38 {
39 	int     n, cnt, i, pass = 0;
40 	char    passwd[256], key[8], buf[33], *seed, *slash;
41 	extern int optind;
42 	extern char *optarg;
43 
44 	cnt = 1;
45 
46 	while ((i = getopt(argc, argv, "n:p:")) != EOF) {
47 		switch (i) {
48 		case 'n':
49 			cnt = atoi(optarg);
50 			break;
51 		case 'p':
52 			strcpy(passwd, optarg);
53 			pass = 1;
54 			break;
55 		}
56 	}
57 
58 	/* could be in the form <number>/<seed> */
59 
60 	if (argc <= optind + 1) {
61 		/* look for / in it */
62 		if (argc <= optind)
63 			usage(argv[0]);
64 		slash = strchr(argv[optind], '/');
65 		if (slash == NULL)
66 			usage(argv[0]);
67 		*slash++ = '\0';
68 		seed = slash;
69 
70 		if ((n = atoi(argv[optind])) < 0) {
71 			fprintf(stderr, "%s not positive\n", argv[optind]);
72 			usage(argv[0]);
73 		}
74 	} else {
75 
76 		if ((n = atoi(argv[optind])) < 0) {
77 			fprintf(stderr, "%s not positive\n", argv[optind]);
78 			usage(argv[0]);
79 		}
80 		seed = argv[++optind];
81 	}
82 
83 	/* Get user's secret password */
84 	if (!pass) {
85 		fprintf(stderr, "Enter secret password: ");
86 		readpass(passwd, sizeof(passwd));
87 	}
88 	rip(passwd);
89 
90 	/* Crunch seed and password into starting key */
91 	if (keycrunch(key, seed, passwd) != 0) {
92 		fprintf(stderr, "%s: key crunch failed\n", argv[0]);
93 		exit(1);
94 	}
95 	if (cnt == 1) {
96 		while (n-- != 0)
97 			f(key);
98 		printf("%s\n", btoe(buf, key));
99 #ifdef	HEXIN
100 		printf("%s\n", put8(buf, key));
101 #endif
102 	} else {
103 		for (i = 0; i <= n - cnt; i++)
104 			f(key);
105 		for (; i <= n; i++) {
106 #ifdef	HEXIN
107 			printf("%d: %-29s  %s\n", i, btoe(buf, key), put8(buf, key));
108 #else
109 			printf("%d: %-29s\n", i, btoe(buf, key));
110 #endif
111 			f(key);
112 		}
113 	}
114 	exit(0);
115 }
116 
117 void
118 usage(s)
119 	char   *s;
120 {
121 
122 	fprintf(stderr,
123 	    "Usage: %s [-n count] [-p password ] sequence# [/] key\n", s);
124 	exit(1);
125 }
126