[dovecot-cvs] dovecot/src/util .cvsignore, 1.1, 1.2 dovecotpw.c, NONE, 1.1

cras at dovecot.org cras at dovecot.org
Mon Jul 26 20:07:31 EEST 2004


Update of /home/cvs/dovecot/src/util
In directory talvi:/tmp/cvs-serv21361/util

Modified Files:
	.cvsignore 
Added Files:
	dovecotpw.c 
Log Message:
Forgot from last dovecotpw commit.



Index: .cvsignore
===================================================================
RCS file: /home/cvs/dovecot/src/util/.cvsignore,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- .cvsignore	20 Aug 2003 23:24:29 -0000	1.1
+++ .cvsignore	26 Jul 2004 17:07:29 -0000	1.2
@@ -7,3 +7,4 @@
 Makefile.in
 so_locations
 rawlog
+dovecotpw

--- NEW FILE: dovecotpw.c ---
/* Copyright (C) 2004 Joshua Goodall */

#include "lib.h"
#include "password-scheme.h"
#include "randgen.h"
#include "safe-memset.h"

#include <ctype.h>
#include <fcntl.h>
#include <getopt.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define DEFAULT_SCHEME "HMAC-MD5"

static void
usage(const char *s)
{
	fprintf(stderr,
	    "usage: %s [-l] [-p plaintext] [-s scheme] [-u user] [-V]\n", s);
	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
	    "    -l            List known password schemes",
	    "    -p plaintext  New password",
	    "    -s scheme     Password scheme",
	    "    -u user       Username (if scheme uses it)",
	    "    -V            Internally verify the hash");

	exit(1);
}

int main(int argc, char *argv[] __attr_unused__)
{
	extern char *optarg;
	extern int optind;
	const char *hash = NULL;
	const char *user = NULL;
	char *scheme = NULL;
	char *plaintext = NULL;
	char ch;
	int lflag = 0, Vflag = 0;

	lib_init();
	random_init();
	password_schemes_init();
	
	while ((ch = getopt(argc, argv, "lp:s:u:V")) != -1) {
		switch (ch) {
		case 'l':
			lflag = 1;
			break;
		case 'p':
			plaintext = i_strdup(optarg);
			safe_memset(optarg, 0, strlen(optarg));
			break;
		case 's':
			scheme = i_strdup(optarg);
			break;
		case 'u':
			user = i_strdup(optarg);
			break;
		case 'V':
			Vflag = 1;
			break;
		case '?':
		default:
			usage(basename(*argv));
		}
	}

	if (lflag) {
		const struct password_scheme *p = NULL;
		const char *s;

		while ((s = password_list_schemes(&p)) != NULL)
			printf("%s ", s);
		printf("\n");
		exit(0);
	}

	if (argc != optind)
		usage(basename(*argv));

	if (scheme == NULL)
		scheme = i_strdup(DEFAULT_SCHEME);
	else {
		char *c;
		for (c = scheme; *c != '\0'; c++)
			*c = i_toupper(*c);
	}


	while (plaintext == NULL) {
		char *check;
		static int lives = 3;

		plaintext = i_strdup(getpass("Enter new password: "));
		check = i_strdup(getpass("Retype new password: "));
		if (strcmp(plaintext, check) != 0) {
			fprintf(stderr, "Passwords don't match!\n");
			if (--lives == 0)
				exit(1);
			safe_memset(plaintext, 0, strlen(plaintext));
			safe_memset(check, 0, strlen(check));
			i_free(plaintext);
			i_free(check);
			plaintext = NULL;
		}
	}

	if ((hash = password_generate(plaintext, user, scheme)) == NULL) {
		fprintf(stderr, "error generating password hash\n");
		exit(1);
	}
	if (Vflag == 1) {
		const char *checkscheme, *checkpass;

		checkpass = t_strdup_printf("{%s}%s", scheme, hash);
		checkscheme = password_get_scheme(&checkpass);

		if (strcmp(scheme, checkscheme) != 0) {
			fprintf(stderr, "reverse scheme lookup check failed\n");
			exit(2);
		}
		if (password_verify(plaintext, checkpass,
				    checkscheme, user) != 1) {
			fprintf(stderr,
				"reverse password verification check failed\n");
			exit(2);
		}

		printf("{%s}%s (verified)\n", scheme, hash);
	} else
		printf("{%s}%s\n", scheme, hash);

        return 0;
}



More information about the dovecot-cvs mailing list