summaryrefslogtreecommitdiffstats
path: root/usr.bin/key/skey.c
blob: 42ef6c5e79f754561dd78301c3909f2824663add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Stand-alone program for computing responses to S/Key challenges.
 * Takes the iteration count and seed as command line args, prompts
 * for the user's key, and produces both word and hex format responses.
 *
 * Usage example:
 *	>skey 88 ka9q2
 *	Enter password:
 *	OMEN US HORN OMIT BACK AHOY
 *	C848 666B 6435 0A93
 *	>
 */

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef	__MSDOS__
#include <dos.h>
#else	/* Assume BSD unix */
#include <fcntl.h>
#endif

#include <skey.h>

static void usage __P((void));

int
main(argc,argv)
int argc;
char *argv[];
{
	int n,cnt,i;
	char passwd[256] /* ,passwd2[256] */;
	char key[8];
	char *seed;
	char buf[33];
	char *slash;

	cnt = 1;
	while((i = getopt(argc,argv,"n:")) != -1){
		switch(i){
		case 'n':
			cnt = atoi(optarg);
			break;
		}
	}
	/* could be in the form <number>/<seed> */
	if(argc <= optind + 1){
		/*look for / in it */
		if(argc <= optind)
			usage();

		slash = strchr(argv[optind], '/');
		if(slash == NULL)
			usage();
		*slash++ = '\0';
		seed = slash;

		if((n = atoi(argv[optind])) < 0){
			fprintf(stderr,"%s not positive\n",argv[optind]);
			usage();
		}
	}
	else {

		if((n = atoi(argv[optind])) < 0){
			fprintf(stderr,"%s not positive\n",argv[optind]);
			usage();
		}
		seed = argv[++optind];
	}
	fprintf(stderr,"Reminder - Do not use this program while logged in via telnet or rlogin.\n");

	/* Get user's secret password */
	for(;;){
		fprintf(stderr,"Enter secret password: ");
		readpass(passwd,sizeof(passwd));
		break;
	/************
		fprintf(stderr,"Again secret password: ");
		readpass(passwd2,sizeof(passwd));
		if(strcmp(passwd,passwd2) == 0) break;
		fprintf(stderr, "Sorry no match\n");
        **************/

	}

	/* Crunch seed and password into starting key */
	if(keycrunch(key,seed,passwd) != 0)
		errx(1, "key crunch failed");
	if(cnt == 1){
		while(n-- != 0)
			f(key);
		printf("%s\n",btoe(buf,key));
#ifdef	HEXIN
		printf("%s\n",put8(buf,key));
#endif
	} else {
		for(i=0;i<=n-cnt;i++)
			f(key);
		for(;i<=n;i++){
#ifdef	HEXIN
			printf("%d: %-29s  %s\n",i,btoe(buf,key),put8(buf,key));
#else
			printf("%d: %-29s\n",i,btoe(buf,key));
#endif
			f(key);
		}
	}
	return 0;
}

static void
usage()
{
	fprintf(stderr,"usage: key [-n count] <sequence #>[/] <key>\n");
	exit(1);
}
OpenPOWER on IntegriCloud