122347Spst/* opieserv.c: Sample OTP server based on the opiechallenge() and
222347Spst               opieverify() library routines.
322347Spst
429964Sache%%% copyright-cmetz-96
592906SmarkmThis software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
692906SmarkmThe Inner Net License Version 3 applies to this software.
722347SpstYou should have received a copy of the license with this software. If
822347Spstyou didn't get a copy, you may request one from <license@inner.net>.
922347Spst
1022347Spst        History:
1122347Spst
1222347Spst	Modified by cmetz for OPIE 2.3. Send debug info to syslog.
1322347Spst        Created by cmetz for OPIE 2.2.
1422347Spst*/
1522347Spst#include "opie_cfg.h"
1622347Spst#include <stdio.h>
1722347Spst#if DEBUG
1822347Spst#include <syslog.h>
1922347Spst#endif /* DEBUG */
2022347Spst#include "opie.h"
2122347Spst
2222347Spstint main FUNCTION((argc, argv), int argc AND char *argv[])
2322347Spst{
2422347Spst	struct opie opie;
2522347Spst	char *principal;
2622347Spst	char buffer[1024];
2722347Spst        char challenge[OPIE_CHALLENGE_MAX+1];
2822347Spst        char response[OPIE_RESPONSE_MAX+1];
2922347Spst	int result;
3022347Spst
3122347Spst	if (argc <= 1) {
3222347Spst		fputs("Principal: ", stderr);
3322347Spst                if (!opiereadpass(buffer, sizeof(buffer)-1, 1))
3422347Spst                  fprintf(stderr, "Error reading principal!");
3522347Spst		principal = buffer;
3622347Spst	} else {
3722347Spst		principal = argv[1];
3822347Spst	}
3922347Spst#if DEBUG
4022347Spst       	syslog(LOG_DEBUG, "Principal is +%s+", principal);
4122347Spst#endif /* DEBUG */
4222347Spst
4322347Spst	switch (result = opiechallenge(&opie, principal, challenge)) {
4422347Spst		case -1:
4522347Spst			fputs("System error!\n", stderr);
4622347Spst			exit(1);
4722347Spst		case 0:
4822347Spst			break;
4922347Spst		case 1:
5022347Spst			fputs("User not found!\n", stderr);
5122347Spst			exit(1);
5222347Spst		case 2:
5322347Spst			fputs("System error!\n", stderr);
5422347Spst			exit(1);
5522347Spst		default:
5622347Spst			fprintf(stderr, "Unknown error %d!\n", result);
5722347Spst			exit(1);
5822347Spst	};
5922347Spst
6022347Spst	fputs(challenge, stdout);
6122347Spst        fputc('\n', stdout);
6222347Spst        fflush(stdout);
6322347Spst	fputs("Response: ", stderr);
6422347Spst        if (!opiereadpass(response, OPIE_RESPONSE_MAX, 1)) {
6522347Spst          fputs("Error reading response!\n", stderr);
6622347Spst          exit(1);
6722347Spst        };
6822347Spst
6922347Spst	switch (result = opieverify(&opie, response)) {
7022347Spst		case -1:
7122347Spst			fputs("System error!\n", stderr);
7222347Spst			exit(1);
7322347Spst		case 0:
7422347Spst			fputs("User verified.\n", stderr);
7522347Spst			exit(0);
7622347Spst		case 1:
7722347Spst			fputs("Verify failed!\n", stderr);
7822347Spst			exit(1);
7922347Spst		default:
8022347Spst			fprintf(stderr, "Unknown error %d!\n", result);
8122347Spst			exit(1);
8222347Spst	}
8322347Spst}
84