|
Packit |
90a5c9 |
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
90a5c9 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
90a5c9 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
90a5c9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
90a5c9 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
90a5c9 |
* the License. You may obtain a copy of the License at
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
90a5c9 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
90a5c9 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
90a5c9 |
* See the License for the specific language governing permissions and
|
|
Packit |
90a5c9 |
* limitations under the License.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/******************************************************************************
|
|
Packit |
90a5c9 |
******************************************************************************
|
|
Packit |
90a5c9 |
* NOTE! This program is not safe as a setuid executable! Do not make it
|
|
Packit |
90a5c9 |
* setuid!
|
|
Packit |
90a5c9 |
******************************************************************************
|
|
Packit |
90a5c9 |
*****************************************************************************/
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* htpasswd.c: simple program for manipulating password file for
|
|
Packit |
90a5c9 |
* the Apache HTTP server
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Originally by Rob McCool
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Exit values:
|
|
Packit |
90a5c9 |
* 0: Success
|
|
Packit |
90a5c9 |
* 1: Failure; file access/permission problem
|
|
Packit |
90a5c9 |
* 2: Failure; command line syntax problem (usage message issued)
|
|
Packit |
90a5c9 |
* 3: Failure; password verification failure
|
|
Packit |
90a5c9 |
* 4: Failure; operation interrupted (such as with CTRL/C)
|
|
Packit |
90a5c9 |
* 5: Failure; buffer would overflow (username, filename, or computed
|
|
Packit |
90a5c9 |
* record too long)
|
|
Packit |
90a5c9 |
* 6: Failure; username contains illegal or reserved characters
|
|
Packit |
90a5c9 |
* 7: Failure; file is not a valid htpasswd file
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "passwd_common.h"
|
|
Packit |
90a5c9 |
#include "apr_signal.h"
|
|
Packit |
90a5c9 |
#include "apr_getopt.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#if APR_HAVE_STDIO_H
|
|
Packit |
90a5c9 |
#include <stdio.h>
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "apr_md5.h"
|
|
Packit |
90a5c9 |
#include "apr_sha1.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#if APR_HAVE_STDLIB_H
|
|
Packit |
90a5c9 |
#include <stdlib.h>
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
#if APR_HAVE_STRING_H
|
|
Packit |
90a5c9 |
#include <string.h>
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
#if APR_HAVE_UNISTD_H
|
|
Packit |
90a5c9 |
#include <unistd.h>
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#ifdef WIN32
|
|
Packit |
90a5c9 |
#include <conio.h>
|
|
Packit |
90a5c9 |
#define unlink _unlink
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#define APHTP_NEWFILE 1
|
|
Packit |
90a5c9 |
#define APHTP_NOFILE 2
|
|
Packit |
90a5c9 |
#define APHTP_DELUSER 4
|
|
Packit |
90a5c9 |
#define APHTP_VERIFY 8
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_file_t *ftemp = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int mkrecord(struct passwd_ctx *ctx, char *user)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
char hash_str[MAX_STRING_LEN];
|
|
Packit |
90a5c9 |
int ret;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ctx->out = hash_str;
|
|
Packit |
90a5c9 |
ctx->out_len = sizeof(hash_str);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ret = mkhash(ctx);
|
|
Packit |
90a5c9 |
if (ret) {
|
|
Packit |
90a5c9 |
ctx->out = NULL;
|
|
Packit |
90a5c9 |
ctx->out_len = 0;
|
|
Packit |
90a5c9 |
return ret;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ctx->out = apr_pstrcat(ctx->pool, user, ":", hash_str, NL, NULL);
|
|
Packit |
90a5c9 |
ctx->out_len = strlen(ctx->out);
|
|
Packit |
90a5c9 |
if (ctx->out_len >= MAX_STRING_LEN) {
|
|
Packit |
90a5c9 |
ctx->errstr = "resultant record too long";
|
|
Packit |
90a5c9 |
return ERR_OVERFLOW;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void usage(void)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "Usage:" NL
|
|
Packit |
90a5c9 |
"\thtpasswd [-cimBdpsDv] [-C cost] passwordfile username" NL
|
|
Packit |
90a5c9 |
"\thtpasswd -b[cmBdpsDv] [-C cost] passwordfile username password" NL
|
|
Packit |
90a5c9 |
NL
|
|
Packit |
90a5c9 |
"\thtpasswd -n[imBdps] [-C cost] username" NL
|
|
Packit |
90a5c9 |
"\thtpasswd -nb[mBdps] [-C cost] username password" NL
|
|
Packit |
90a5c9 |
" -c Create a new file." NL
|
|
Packit |
90a5c9 |
" -n Don't update file; display results on stdout." NL
|
|
Packit |
90a5c9 |
" -b Use the password from the command line rather than prompting "
|
|
Packit |
90a5c9 |
"for it." NL
|
|
Packit |
90a5c9 |
" -i Read password from stdin without verification (for script usage)." NL
|
|
Packit |
90a5c9 |
" -m Force MD5 encryption of the password (default)." NL
|
|
Packit |
90a5c9 |
" -B Force bcrypt encryption of the password (very secure)." NL
|
|
Packit |
90a5c9 |
" -C Set the computing time used for the bcrypt algorithm" NL
|
|
Packit |
90a5c9 |
" (higher is more secure but slower, default: %d, valid: 4 to 31)." NL
|
|
Packit |
90a5c9 |
" -d Force CRYPT encryption of the password (8 chars max, insecure)." NL
|
|
Packit |
90a5c9 |
" -s Force SHA encryption of the password (insecure)." NL
|
|
Packit |
90a5c9 |
" -p Do not encrypt the password (plaintext, insecure)." NL
|
|
Packit |
90a5c9 |
" -D Delete the specified user." NL
|
|
Packit |
90a5c9 |
" -v Verify password for the specified user." NL
|
|
Packit |
90a5c9 |
"On other systems than Windows and NetWare the '-p' flag will "
|
|
Packit |
90a5c9 |
"probably not work." NL
|
|
Packit |
90a5c9 |
"The SHA algorithm does not use a salt and is less secure than the "
|
|
Packit |
90a5c9 |
"MD5 algorithm." NL,
|
|
Packit |
90a5c9 |
BCRYPT_DEFAULT_COST
|
|
Packit |
90a5c9 |
);
|
|
Packit |
90a5c9 |
exit(ERR_SYNTAX);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Check to see if the specified file can be opened for the given
|
|
Packit |
90a5c9 |
* access.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static int accessible(apr_pool_t *pool, char *fname, int mode)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_file_t *f = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (apr_file_open(&f, fname, mode, APR_OS_DEFAULT, pool) != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
apr_file_close(f);
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Return true if the named file exists, regardless of permissions.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static int exists(char *fname, apr_pool_t *pool)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_finfo_t sbuf;
|
|
Packit |
90a5c9 |
apr_status_t check;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
check = apr_stat(&sbuf, fname, APR_FINFO_TYPE, pool);
|
|
Packit |
90a5c9 |
return ((check || sbuf.filetype != APR_REG) ? 0 : 1);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void terminate(void)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_terminate();
|
|
Packit |
90a5c9 |
#ifdef NETWARE
|
|
Packit |
90a5c9 |
pressanykey();
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void check_args(int argc, const char *const argv[],
|
|
Packit |
90a5c9 |
struct passwd_ctx *ctx, unsigned *mask, char **user,
|
|
Packit |
90a5c9 |
char **pwfilename)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
const char *arg;
|
|
Packit |
90a5c9 |
int args_left = 2;
|
|
Packit |
90a5c9 |
int i, ret;
|
|
Packit |
90a5c9 |
apr_getopt_t *state;
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
char opt;
|
|
Packit |
90a5c9 |
const char *opt_arg;
|
|
Packit |
90a5c9 |
apr_pool_t *pool = ctx->pool;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = apr_getopt_init(&state, pool, argc, argv);
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS)
|
|
Packit |
90a5c9 |
exit(ERR_SYNTAX);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
while ((rv = apr_getopt(state, "cnmspdBbDiC:v", &opt, &opt_arg)) == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
switch (opt) {
|
|
Packit |
90a5c9 |
case 'c':
|
|
Packit |
90a5c9 |
*mask |= APHTP_NEWFILE;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case 'n':
|
|
Packit |
90a5c9 |
args_left--;
|
|
Packit |
90a5c9 |
*mask |= APHTP_NOFILE;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case 'D':
|
|
Packit |
90a5c9 |
*mask |= APHTP_DELUSER;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case 'v':
|
|
Packit |
90a5c9 |
*mask |= APHTP_VERIFY;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
ret = parse_common_options(ctx, opt, opt_arg);
|
|
Packit |
90a5c9 |
if (ret) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: %s" NL, argv[0], ctx->errstr);
|
|
Packit |
90a5c9 |
exit(ret);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (ctx->passwd_src == PW_ARG)
|
|
Packit |
90a5c9 |
args_left++;
|
|
Packit |
90a5c9 |
if (rv != APR_EOF)
|
|
Packit |
90a5c9 |
usage();
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if ((*mask) & (*mask - 1)) {
|
|
Packit |
90a5c9 |
/* not a power of two, i.e. more than one flag specified */
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: only one of -c -n -v -D may be specified" NL,
|
|
Packit |
90a5c9 |
argv[0]);
|
|
Packit |
90a5c9 |
exit(ERR_SYNTAX);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if ((*mask & APHTP_VERIFY) && ctx->passwd_src == PW_PROMPT)
|
|
Packit |
90a5c9 |
ctx->passwd_src = PW_PROMPT_VERIFY;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Make sure we still have exactly the right number of arguments left
|
|
Packit |
90a5c9 |
* (the filename, the username, and possibly the password if -b was
|
|
Packit |
90a5c9 |
* specified).
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
i = state->ind;
|
|
Packit |
90a5c9 |
if ((argc - i) != args_left) {
|
|
Packit |
90a5c9 |
usage();
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!(*mask & APHTP_NOFILE)) {
|
|
Packit |
90a5c9 |
if (strlen(argv[i]) > (APR_PATH_MAX - 1)) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: filename too long" NL, argv[0]);
|
|
Packit |
90a5c9 |
exit(ERR_OVERFLOW);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
*pwfilename = apr_pstrdup(pool, argv[i++]);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (strlen(argv[i]) > (MAX_STRING_LEN - 1)) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: username too long (> %d)" NL,
|
|
Packit |
90a5c9 |
argv[0], MAX_STRING_LEN - 1);
|
|
Packit |
90a5c9 |
exit(ERR_OVERFLOW);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
*user = apr_pstrdup(pool, argv[i++]);
|
|
Packit |
90a5c9 |
if ((arg = strchr(*user, ':')) != NULL) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: username contains illegal "
|
|
Packit |
90a5c9 |
"character '%c'" NL, argv[0], *arg);
|
|
Packit |
90a5c9 |
exit(ERR_BADUSER);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (ctx->passwd_src == PW_ARG) {
|
|
Packit |
90a5c9 |
if (strlen(argv[i]) > (MAX_STRING_LEN - 1)) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: password too long (> %d)" NL,
|
|
Packit |
90a5c9 |
argv[0], MAX_STRING_LEN);
|
|
Packit |
90a5c9 |
exit(ERR_OVERFLOW);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ctx->passwd = apr_pstrdup(pool, argv[i]);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int verify(struct passwd_ctx *ctx, const char *hash)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
int ret;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (ctx->passwd == NULL && (ret = get_password(ctx)) != 0)
|
|
Packit |
90a5c9 |
return ret;
|
|
Packit |
90a5c9 |
rv = apr_password_validate(ctx->passwd, hash);
|
|
Packit |
90a5c9 |
if (rv == APR_SUCCESS)
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
if (APR_STATUS_IS_EMISMATCH(rv)) {
|
|
Packit |
90a5c9 |
ctx->errstr = "password verification failed";
|
|
Packit |
90a5c9 |
return ERR_PWMISMATCH;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ctx->errstr = apr_psprintf(ctx->pool, "Could not verify password: %pm",
|
|
Packit |
90a5c9 |
&rv;;
|
|
Packit |
90a5c9 |
return ERR_GENERAL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Let's do it. We end up doing a lot of file opening and closing,
|
|
Packit |
90a5c9 |
* but what do we care? This application isn't run constantly.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
int main(int argc, const char * const argv[])
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_file_t *fpw = NULL;
|
|
Packit |
90a5c9 |
char line[MAX_STRING_LEN];
|
|
Packit |
90a5c9 |
char *pwfilename = NULL;
|
|
Packit |
90a5c9 |
char *user = NULL;
|
|
Packit |
90a5c9 |
char tn[] = "htpasswd.tmp.XXXXXX";
|
|
Packit |
90a5c9 |
char *dirname;
|
|
Packit |
90a5c9 |
char *scratch, cp[MAX_STRING_LEN];
|
|
Packit |
90a5c9 |
int found = 0;
|
|
Packit |
90a5c9 |
int i;
|
|
Packit |
90a5c9 |
unsigned mask = 0;
|
|
Packit |
90a5c9 |
apr_pool_t *pool;
|
|
Packit |
90a5c9 |
int existing_file = 0;
|
|
Packit |
90a5c9 |
struct passwd_ctx ctx = { 0 };
|
|
Packit |
90a5c9 |
#if APR_CHARSET_EBCDIC
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
apr_xlate_t *to_ascii;
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_app_initialize(&argc, &argv, NULL);
|
|
Packit |
90a5c9 |
atexit(terminate);
|
|
Packit |
90a5c9 |
apr_pool_create(&pool, NULL);
|
|
Packit |
90a5c9 |
apr_pool_abort_set(abort_on_oom, pool);
|
|
Packit |
90a5c9 |
apr_file_open_stderr(&errfile, pool);
|
|
Packit |
90a5c9 |
ctx.pool = pool;
|
|
Packit |
90a5c9 |
ctx.alg = ALG_APMD5;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#if APR_CHARSET_EBCDIC
|
|
Packit |
90a5c9 |
rv = apr_xlate_open(&to_ascii, "ISO-8859-1", APR_DEFAULT_CHARSET, pool);
|
|
Packit |
90a5c9 |
if (rv) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "apr_xlate_open(to ASCII)->%d" NL, rv);
|
|
Packit |
90a5c9 |
exit(1);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
rv = apr_SHA1InitEBCDIC(to_ascii);
|
|
Packit |
90a5c9 |
if (rv) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "apr_SHA1InitEBCDIC()->%d" NL, rv);
|
|
Packit |
90a5c9 |
exit(1);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
rv = apr_MD5InitEBCDIC(to_ascii);
|
|
Packit |
90a5c9 |
if (rv) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "apr_MD5InitEBCDIC()->%d" NL, rv);
|
|
Packit |
90a5c9 |
exit(1);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
#endif /*APR_CHARSET_EBCDIC*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
check_args(argc, argv, &ctx, &mask, &user, &pwfilename);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Only do the file checks if we're supposed to frob it.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!(mask & APHTP_NOFILE)) {
|
|
Packit |
90a5c9 |
existing_file = exists(pwfilename, pool);
|
|
Packit |
90a5c9 |
if (existing_file && (mask & APHTP_VERIFY) == 0) {
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Check that this existing file is readable and writable.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!accessible(pool, pwfilename, APR_FOPEN_READ|APR_FOPEN_WRITE)) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: cannot open file %s for "
|
|
Packit |
90a5c9 |
"read/write access" NL, argv[0], pwfilename);
|
|
Packit |
90a5c9 |
exit(ERR_FILEPERM);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (existing_file && (mask & APHTP_VERIFY) != 0) {
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Check that this existing file is readable.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!accessible(pool, pwfilename, APR_FOPEN_READ)) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: cannot open file %s for "
|
|
Packit |
90a5c9 |
"read access" NL, argv[0], pwfilename);
|
|
Packit |
90a5c9 |
exit(ERR_FILEPERM);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Error out if -c was omitted for this non-existant file.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!(mask & APHTP_NEWFILE)) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile,
|
|
Packit |
90a5c9 |
"%s: cannot modify file %s; use '-c' to create it" NL,
|
|
Packit |
90a5c9 |
argv[0], pwfilename);
|
|
Packit |
90a5c9 |
exit(ERR_FILEPERM);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* As it doesn't exist yet, verify that we can create it.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!accessible(pool, pwfilename, APR_FOPEN_WRITE|APR_FOPEN_CREATE)) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: cannot create file %s" NL,
|
|
Packit |
90a5c9 |
argv[0], pwfilename);
|
|
Packit |
90a5c9 |
exit(ERR_FILEPERM);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* All the file access checks (if any) have been made. Time to go to work;
|
|
Packit |
90a5c9 |
* try to create the record for the username in question. If that
|
|
Packit |
90a5c9 |
* fails, there's no need to waste any time on file manipulations.
|
|
Packit |
90a5c9 |
* Any error message text is returned in the record buffer, since
|
|
Packit |
90a5c9 |
* the mkrecord() routine doesn't have access to argv[].
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((mask & (APHTP_DELUSER|APHTP_VERIFY)) == 0) {
|
|
Packit |
90a5c9 |
i = mkrecord(&ctx, user);
|
|
Packit |
90a5c9 |
if (i != 0) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: %s" NL, argv[0], ctx.errstr);
|
|
Packit |
90a5c9 |
exit(i);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (mask & APHTP_NOFILE) {
|
|
Packit |
90a5c9 |
printf("%s" NL, ctx.out);
|
|
Packit |
90a5c9 |
exit(0);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if ((mask & APHTP_VERIFY) == 0) {
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* We can access the files the right way, and we have a record
|
|
Packit |
90a5c9 |
* to add or update. Let's do it..
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (apr_temp_dir_get((const char**)&dirname, pool) != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: could not determine temp dir" NL,
|
|
Packit |
90a5c9 |
argv[0]);
|
|
Packit |
90a5c9 |
exit(ERR_FILEPERM);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
dirname = apr_psprintf(pool, "%s/%s", dirname, tn);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (apr_file_mktemp(&ftemp, dirname, 0, pool) != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: unable to create temporary file %s" NL,
|
|
Packit |
90a5c9 |
argv[0], dirname);
|
|
Packit |
90a5c9 |
exit(ERR_FILEPERM);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* If we're not creating a new file, copy records from the existing
|
|
Packit |
90a5c9 |
* one to the temporary file until we find the specified user.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (existing_file && !(mask & APHTP_NEWFILE)) {
|
|
Packit |
90a5c9 |
if (apr_file_open(&fpw, pwfilename, APR_READ | APR_BUFFERED,
|
|
Packit |
90a5c9 |
APR_OS_DEFAULT, pool) != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: unable to read file %s" NL,
|
|
Packit |
90a5c9 |
argv[0], pwfilename);
|
|
Packit |
90a5c9 |
exit(ERR_FILEPERM);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
while (apr_file_gets(line, sizeof(line), fpw) == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
char *colon;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
strcpy(cp, line);
|
|
Packit |
90a5c9 |
scratch = cp;
|
|
Packit |
90a5c9 |
while (apr_isspace(*scratch)) {
|
|
Packit |
90a5c9 |
++scratch;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!*scratch || (*scratch == '#')) {
|
|
Packit |
90a5c9 |
putline(ftemp, line);
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* See if this is our user.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
colon = strchr(scratch, ':');
|
|
Packit |
90a5c9 |
if (colon != NULL) {
|
|
Packit |
90a5c9 |
*colon = '\0';
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* If we've not got a colon on the line, this could well
|
|
Packit |
90a5c9 |
* not be a valid htpasswd file.
|
|
Packit |
90a5c9 |
* We should bail at this point.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: The file %s does not appear "
|
|
Packit |
90a5c9 |
"to be a valid htpasswd file." NL,
|
|
Packit |
90a5c9 |
argv[0], pwfilename);
|
|
Packit |
90a5c9 |
apr_file_close(fpw);
|
|
Packit |
90a5c9 |
exit(ERR_INVALID);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (strcmp(user, scratch) != 0) {
|
|
Packit |
90a5c9 |
putline(ftemp, line);
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
/* We found the user we were looking for */
|
|
Packit |
90a5c9 |
found++;
|
|
Packit |
90a5c9 |
if ((mask & APHTP_DELUSER)) {
|
|
Packit |
90a5c9 |
/* Delete entry from the file */
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "Deleting ");
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if ((mask & APHTP_VERIFY)) {
|
|
Packit |
90a5c9 |
/* Verify */
|
|
Packit |
90a5c9 |
char *hash = colon + 1;
|
|
Packit |
90a5c9 |
size_t len;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
len = strcspn(hash, "\r\n");
|
|
Packit |
90a5c9 |
if (len == 0) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "Empty hash for user %s" NL,
|
|
Packit |
90a5c9 |
user);
|
|
Packit |
90a5c9 |
exit(ERR_INVALID);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
hash[len] = '\0';
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
i = verify(&ctx, hash);
|
|
Packit |
90a5c9 |
if (i != 0) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s" NL, ctx.errstr);
|
|
Packit |
90a5c9 |
exit(i);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
/* Update entry */
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "Updating ");
|
|
Packit |
90a5c9 |
putline(ftemp, ctx.out);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
apr_file_close(fpw);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (!found) {
|
|
Packit |
90a5c9 |
if (mask & APHTP_DELUSER) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "User %s not found" NL, user);
|
|
Packit |
90a5c9 |
exit(0);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (mask & APHTP_VERIFY) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "User %s not found" NL, user);
|
|
Packit |
90a5c9 |
exit(ERR_BADUSER);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "Adding ");
|
|
Packit |
90a5c9 |
putline(ftemp, ctx.out);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (mask & APHTP_VERIFY) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "Password for user %s correct." NL, user);
|
|
Packit |
90a5c9 |
exit(0);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "password for user %s" NL, user);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* The temporary file has all the data, just copy it to the new location.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (apr_file_copy(dirname, pwfilename, APR_OS_DEFAULT, pool) !=
|
|
Packit |
90a5c9 |
APR_SUCCESS) {
|
|
Packit |
90a5c9 |
apr_file_printf(errfile, "%s: unable to update file %s" NL,
|
|
Packit |
90a5c9 |
argv[0], pwfilename);
|
|
Packit |
90a5c9 |
exit(ERR_FILEPERM);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
apr_file_close(ftemp);
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|