122347Spst/* opiegen.c: Sample OTP generator based on the opiegenerator()
222347Spst              library routine.
322347Spst
429964Sache%%% portions-copyright-cmetz-96
592906SmarkmPortions of this software are Copyright 1996-1999 by Craig Metz, All Rights
622347SpstReserved. The Inner Net License Version 2 applies to these portions of
722347Spstthe software.
822347SpstYou should have received a copy of the license with this software. If
922347Spstyou didn't get a copy, you may request one from <license@inner.net>.
1022347Spst
1122347Spst        History:
1222347Spst
1322347Spst	Modified by cmetz for OPIE 2.3. OPIE_PASS_MAX changed to
1422347Spst		OPIE_SECRET_MAX. Send debug info to syslog.
1522347Spst	Modified by cmetz for OPIE 2.2. Use FUNCTION definition et al.
1622347Spst             Fixed include order.
1722347Spst        Created at NRL for OPIE 2.2.
1822347Spst*/
1922347Spst#include "opie_cfg.h"
2022347Spst#include <stdio.h>
2122347Spst#if DEBUG
2222347Spst#include <syslog.h>
2322347Spst#endif /* DEBUG */
2422347Spst#include "opie.h"
2522347Spst
2622347Spstint main FUNCTION((argc, argv), int argc AND char *argv[])
2722347Spst{
2822347Spst	char buffer[OPIE_CHALLENGE_MAX+1];
2922347Spst	char secret[OPIE_SECRET_MAX+1];
3022347Spst        char response[OPIE_RESPONSE_MAX+1];
3122347Spst	int result;
3222347Spst
3322347Spst	if (opieinsecure()) {
3422347Spst		fputs("Sorry, but you don't seem to be on a secure terminal.\n", stderr);
3522347Spst#if !DEBUG
3622347Spst		exit(1);
3722347Spst#endif /* !DEBUG */
3822347Spst	}
3922347Spst
4022347Spst	if (argc <= 1) {
4122347Spst		fputs("Challenge: ", stderr);
4222347Spst                if (!opiereadpass(buffer, sizeof(buffer)-1, 1))
4322347Spst                  fprintf(stderr, "Error reading challenge!");
4422347Spst	} else {
4522347Spst		char *ap, *ep, *c;
4622347Spst		int i;
4722347Spst
4822347Spst		ep = buffer + sizeof(buffer) - 1;
4922347Spst		for (i = 1, ap = buffer; (i < argc) && (ap < ep); i++) {
5022347Spst			c = argv[i];
5122347Spst			while ((*(ap++) = *(c++)) && (ap < ep));
5222347Spst				*(ap - 1) = ' ';
5322347Spst		}
5422347Spst		*(ap - 1) = 0;
5522347Spst#if DEBUG
5622347Spst        	syslog(LOG_DEBUG, "opiegen: challenge is +%s+\n", buffer);
5722347Spst#endif /* DEBUG */
5822347Spst	}
5922347Spst	buffer[sizeof(buffer)-1] = 0;
6022347Spst
6122347Spst	fputs("Secret pass phrase: ", stderr);
6222347Spst        if (!opiereadpass(secret, OPIE_SECRET_MAX, 0)) {
6322347Spst          fputs("Error reading secret pass phrase!\n", stderr);
6422347Spst          exit(1);
6522347Spst        };
6622347Spst
6722347Spst	switch (result = opiegenerator(buffer, secret, response)) {
6822347Spst                case -2:
6922347Spst			fputs("Not a valid OTP secret pass phrase.\n", stderr);
7022347Spst			break;
7122347Spst		case -1:
7222347Spst			fputs("Error processing challenge!\n", stderr);
7322347Spst			break;
7422347Spst		case 1:
7522347Spst			fputs("Not a valid OTP challenge.\n", stderr);
7622347Spst			break;
7722347Spst		case 0:
7822347Spst			fputs(response, stdout);
7922347Spst			fputc('\n', stdout);
8022347Spst			fflush(stdout);
8122347Spst			memset(secret, 0, sizeof(secret));
8222347Spst			exit(0);
8322347Spst		default:
8422347Spst			fprintf(stderr, "Unknown error %d!\n", result);
8522347Spst	}
8622347Spst	memset(secret, 0, sizeof(secret));
8722347Spst	return 1;
8822347Spst}
89