#define APHTP_NEWFILE 1
#define APHTP_NOFILE 2
#define APHTP_DELUSER 4
+#define APHTP_VERIFY 8
apr_file_t *ftemp = NULL;
static void usage(void)
{
apr_file_printf(errfile, "Usage:" NL
- "\thtpasswd [-cimBdpsD] [-C cost] passwordfile username" NL
- "\thtpasswd -b[cmBdpsD] [-C cost] passwordfile username password" NL
+ "\thtpasswd [-cimBdpsDv] [-C cost] passwordfile username" NL
+ "\thtpasswd -b[cmBdpsDv] [-C cost] passwordfile username password" NL
NL
"\thtpasswd -n[imBdps] [-C cost] username" NL
"\thtpasswd -nb[mBdps] [-C cost] username password" NL
" -s Force SHA encryption of the password (insecure)." NL
" -p Do not encrypt the password (plaintext, insecure)." NL
" -D Delete the specified user." NL
+ " -v Verify password for the specified user." NL
"On other systems than Windows and NetWare the '-p' flag will "
"probably not work." NL
"The SHA algorithm does not use a salt and is less secure than the "
}
static void check_args(int argc, const char *const argv[],
- struct passwd_ctx *ctx, int *mask, char **user,
+ struct passwd_ctx *ctx, unsigned *mask, char **user,
char **pwfilename)
{
const char *arg;
if (rv != APR_SUCCESS)
exit(ERR_SYNTAX);
- while ((rv = apr_getopt(state, "cnmspdBbDiC:", &opt, &opt_arg)) == APR_SUCCESS) {
+ while ((rv = apr_getopt(state, "cnmspdBbDiC:v", &opt, &opt_arg)) == APR_SUCCESS) {
switch (opt) {
case 'c':
*mask |= APHTP_NEWFILE;
case 'D':
*mask |= APHTP_DELUSER;
break;
+ case 'v':
+ *mask |= APHTP_VERIFY;
+ break;
default:
ret = parse_common_options(ctx, opt, opt_arg);
if (ret) {
if (rv != APR_EOF)
usage();
- if ((*mask & APHTP_NEWFILE) && (*mask & APHTP_NOFILE)) {
- apr_file_printf(errfile, "%s: -c and -n options conflict" NL, argv[0]);
- exit(ERR_SYNTAX);
- }
- if ((*mask & APHTP_NEWFILE) && (*mask & APHTP_DELUSER)) {
- apr_file_printf(errfile, "%s: -c and -D options conflict" NL, argv[0]);
- exit(ERR_SYNTAX);
- }
- if ((*mask & APHTP_NOFILE) && (*mask & APHTP_DELUSER)) {
- apr_file_printf(errfile, "%s: -n and -D options conflict" NL, argv[0]);
+ if ((*mask) & (*mask - 1)) {
+ /* not a power of two, i.e. more than one flag specified */
+ apr_file_printf(errfile, "%s: only one of -c -n -v -D may be specified" NL,
+ argv[0]);
exit(ERR_SYNTAX);
}
+ if ((*mask & APHTP_VERIFY) && ctx->passwd_src == PW_PROMPT)
+ ctx->passwd_src = PW_PROMPT_VERIFY;
+
/*
* Make sure we still have exactly the right number of arguments left
* (the filename, the username, and possibly the password if -b was
}
}
+static int verify(struct passwd_ctx *ctx, const char *hash)
+{
+ apr_status_t rv;
+ int ret;
+
+ if (ctx->passwd == NULL && (ret = get_password(ctx)) != 0)
+ return ret;
+ rv = apr_password_validate(ctx->passwd, hash);
+ if (rv == APR_SUCCESS)
+ return 0;
+ if (APR_STATUS_IS_EMISMATCH(rv)) {
+ ctx->errstr = "password verification failed";
+ return ERR_PWMISMATCH;
+ }
+ ctx->errstr = apr_psprintf(ctx->pool, "Could not verify password: %pm",
+ &rv);
+ return ERR_GENERAL;
+}
+
/*
* Let's do it. We end up doing a lot of file opening and closing,
* but what do we care? This application isn't run constantly.
char *scratch, cp[MAX_STRING_LEN];
int found = 0;
int i;
- int mask = 0;
+ unsigned mask = 0;
apr_pool_t *pool;
int existing_file = 0;
struct passwd_ctx ctx = { 0 };
* Any error message text is returned in the record buffer, since
* the mkrecord() routine doesn't have access to argv[].
*/
- if (!(mask & APHTP_DELUSER)) {
+ if ((mask & (APHTP_DELUSER|APHTP_VERIFY)) == 0) {
i = mkrecord(&ctx, user);
if (i != 0) {
apr_file_printf(errfile, "%s: %s" NL, argv[0], ctx.errstr);
}
}
- /*
- * We can access the files the right way, and we have a record
- * to add or update. Let's do it..
- */
- if (apr_temp_dir_get((const char**)&dirname, pool) != APR_SUCCESS) {
- apr_file_printf(errfile, "%s: could not determine temp dir" NL,
- argv[0]);
- exit(ERR_FILEPERM);
- }
- dirname = apr_psprintf(pool, "%s/%s", dirname, tn);
+ if ((mask & APHTP_VERIFY) == 0) {
+ /*
+ * We can access the files the right way, and we have a record
+ * to add or update. Let's do it..
+ */
+ if (apr_temp_dir_get((const char**)&dirname, pool) != APR_SUCCESS) {
+ apr_file_printf(errfile, "%s: could not determine temp dir" NL,
+ argv[0]);
+ exit(ERR_FILEPERM);
+ }
+ dirname = apr_psprintf(pool, "%s/%s", dirname, tn);
- if (apr_file_mktemp(&ftemp, dirname, 0, pool) != APR_SUCCESS) {
- apr_file_printf(errfile, "%s: unable to create temporary file %s" NL,
- argv[0], dirname);
- exit(ERR_FILEPERM);
+ if (apr_file_mktemp(&ftemp, dirname, 0, pool) != APR_SUCCESS) {
+ apr_file_printf(errfile, "%s: unable to create temporary file %s" NL,
+ argv[0], dirname);
+ exit(ERR_FILEPERM);
+ }
}
/*
continue;
}
else {
- if (!(mask & APHTP_DELUSER)) {
- /* We found the user we were looking for.
- * Add him to the file.
- */
- apr_file_printf(errfile, "Updating ");
- putline(ftemp, ctx.out);
- found++;
+ /* We found the user we were looking for */
+ found++;
+ if ((mask & APHTP_DELUSER)) {
+ /* Delete entry from the file */
+ apr_file_printf(errfile, "Deleting ");
+ }
+ else if ((mask & APHTP_VERIFY)) {
+ /* Verify */
+ char *hash = colon + 1;
+ size_t len;
+
+ len = strcspn(hash, "\r\n");
+ if (len == 0) {
+ apr_file_printf(errfile, "Empty hash for user %s" NL,
+ user);
+ exit(ERR_INVALID);
+ }
+ hash[len] = '\0';
+
+ i = verify(&ctx, hash);
+ if (i != 0) {
+ apr_file_printf(errfile, "%s" NL, ctx.errstr);
+ exit(i);
+ }
}
else {
- /* We found the user we were looking for.
- * Delete them from the file.
- */
- apr_file_printf(errfile, "Deleting ");
- found++;
+ /* Update entry */
+ apr_file_printf(errfile, "Updating ");
+ putline(ftemp, ctx.out);
}
}
}
apr_file_close(fpw);
}
- if (!found && !(mask & APHTP_DELUSER)) {
- apr_file_printf(errfile, "Adding ");
- putline(ftemp, ctx.out);
+ if (!found) {
+ if (mask & APHTP_DELUSER) {
+ apr_file_printf(errfile, "User %s not found" NL, user);
+ exit(0);
+ }
+ else if (mask & APHTP_VERIFY) {
+ apr_file_printf(errfile, "User %s not found" NL, user);
+ exit(ERR_BADUSER);
+ }
+ else {
+ apr_file_printf(errfile, "Adding ");
+ putline(ftemp, ctx.out);
+ }
}
- else if (!found && (mask & APHTP_DELUSER)) {
- apr_file_printf(errfile, "User %s not found" NL, user);
+ if (mask & APHTP_VERIFY) {
+ apr_file_printf(errfile, "Password for user %s correct." NL, user);
exit(0);
}
+
apr_file_printf(errfile, "password for user %s" NL, user);
/* The temporary file has all the data, just copy it to the new location.