Home | History | Annotate | Line # | Download | only in skey
skey.c revision 1.9.4.1
      1 /*	$NetBSD: skey.c,v 1.9.4.1 2000/07/17 19:55:53 mjl Exp $	*/
      2 
      3 /*
      4  * S/KEY v1.1b (skey.c)
      5  *
      6  * Authors:
      7  *          Neil M. Haller <nmh (at) thumper.bellcore.com>
      8  *          Philip R. Karn <karn (at) chicago.qualcomm.com>
      9  *          John S. Walden <jsw (at) thumper.bellcore.com>
     10  *          Scott Chasin <chasin (at) 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 #ifndef lint
     26 __RCSID("$NetBSD: skey.c,v 1.9.4.1 2000/07/17 19:55:53 mjl Exp $");
     27 #endif
     28 
     29 #include <ctype.h>
     30 #include <err.h>
     31 #include <fcntl.h>
     32 #include <stdio.h>
     33 #include <skey.h>
     34 #include <stdlib.h>
     35 #include <string.h>
     36 #include <unistd.h>
     37 
     38 int	main __P((int, char **));
     39 void    usage __P((char *));
     40 
     41 int
     42 main(int	argc, char **argv)
     43 {
     44 	int     n, cnt, i, pass = 0, hexmode = 0;
     45 	char    passwd[SKEY_MAX_PW_LEN+1], key[SKEY_BINKEY_SIZE];
     46 	char	buf[33], *seed, *slash, *t;
     47 
     48 	cnt = 1;
     49 
     50 	while ((i = getopt(argc, argv, "n:p:t:x")) != -1) {
     51 		switch (i) {
     52 		case 'n':
     53 			cnt = atoi(optarg);
     54 			break;
     55 		case 'p':
     56 			if (strlcpy(passwd, argv[++i], sizeof(passwd)) >=
     57 							sizeof(passwd))
     58 				errx(1, "Password too long");
     59 			pass = 1;
     60 			break;
     61 		case 't':
     62 			if (skey_set_algorithm(optarg) == NULL) {
     63 				errx(1, "Unknown hash algorithm %s", optarg);
     64                         }
     65 			break;
     66 		case 'x':
     67 			hexmode = 1;
     68 			break;
     69 		default:
     70 			usage(argv[0]);
     71 			break;
     72 		}
     73 	}
     74 
     75 	/* could be in the form <number>/<seed> */
     76 	if (argc <= optind + 1) {
     77 		/* look for / in it */
     78 		if (argc <= optind)
     79 			usage(argv[0]);
     80 		slash = strchr(argv[optind], '/');
     81 		if (slash == NULL)
     82 			usage(argv[0]);
     83 		*slash++ = '\0';
     84 		seed = slash;
     85 
     86 		if ((n = atoi(argv[optind])) < 0) {
     87 			fprintf(stderr, "%s not positive\n", argv[optind]);
     88 			usage(argv[0]);
     89                 } else if (n > SKEY_MAX_SEQ) {
     90 			warnx("%d is larger than max (%d)", n, SKEY_MAX_SEQ);
     91 			usage(argv[0]);
     92 		}
     93 	} else {
     94 
     95 		if ((n = atoi(argv[optind])) < 0) {
     96 			fprintf(stderr, "%s not positive\n", argv[optind]);
     97 			usage(argv[0]);
     98 		} else if (n > SKEY_MAX_SEQ) {
     99 			warnx("%d is larger than max (%d)", n, SKEY_MAX_SEQ);
    100 			usage(argv[0]);
    101 		}
    102 		seed = argv[++optind];
    103 	}
    104 
    105 	for(t = seed; *t; t++) {
    106 		if(!isalnum(*t))
    107 			errx(1, "seed must be alphanumeric");
    108 	}
    109 
    110 	if(!*seed || strlen(seed) > SKEY_MAX_SEED_LEN)
    111 		errx(1, "seed must be between 1 and %d long", SKEY_MAX_SEED_LEN);
    112 
    113 	/* Get user's secret password */
    114 	if (!pass) {
    115 		(void)fputs("Reminder - Do not use this program while "
    116 			    "logged in via telnet or rlogin.\n", stderr);
    117 		fprintf(stderr, "Enter secret password: ");
    118 		readpass(passwd, sizeof(passwd));
    119 		if (passwd[0] == '\0')
    120 			exit(1);
    121 	}
    122 
    123 	if(strlen(passwd) < SKEY_MIN_PW_LEN)
    124 		errx(1, "password must be at least %d long", SKEY_MIN_PW_LEN);
    125 
    126 	/* Crunch seed and password into starting key */
    127 	if (keycrunch(key, seed, passwd) != 0)
    128 		errx(1, "key crunch failed");
    129 
    130 	if (cnt == 1) {
    131 		while (n-- != 0)
    132 			f(key);
    133 		(void)puts(hexmode ? put8(buf, key) : btoe(buf, key));
    134 	} else {
    135 		for (i = 0; i <= n - cnt; i++)
    136 			f(key);
    137 		for (; i <= n; i++) {
    138 			(void)printf("%3d: %-29s", i, btoe(buf, key));
    139 			if (hexmode)
    140 				(void)printf("\t%s", put8(buf, key));
    141 			puts("");
    142 			f(key);
    143 		}
    144 	}
    145 	exit(0);
    146 }
    147 
    148 void
    149 usage(char *s)
    150 {
    151 
    152 	fprintf(stderr,
    153 	    "Usage: %s [-n count] [-p password ] [-t hash] [-x] sequence#"
    154 	    	"[/] key\n", s);
    155 	exit(1);
    156 }
    157