htdbm.c revision 8cd53d45c40b817e3467b29262ee0a2892fe98dd
6ae232055d4d8a97267517c5e50074c2c819941and/* ====================================================================
6ae232055d4d8a97267517c5e50074c2c819941and * The Apache Software License, Version 1.1
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
6ae232055d4d8a97267517c5e50074c2c819941and * reserved.
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * Redistribution and use in source and binary forms, with or without
6ae232055d4d8a97267517c5e50074c2c819941and * modification, are permitted provided that the following conditions
6ae232055d4d8a97267517c5e50074c2c819941and * are met:
6ae232055d4d8a97267517c5e50074c2c819941and *
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * 1. Redistributions of source code must retain the above copyright
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * notice, this list of conditions and the following disclaimer.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen *
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * 2. Redistributions in binary form must reproduce the above copyright
6ae232055d4d8a97267517c5e50074c2c819941and * notice, this list of conditions and the following disclaimer in
6ae232055d4d8a97267517c5e50074c2c819941and * the documentation and/or other materials provided with the
6ae232055d4d8a97267517c5e50074c2c819941and * distribution.
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen *
3f08db06526d6901aa08c110b5bc7dde6bc39905nd * 3. The end-user documentation included with the redistribution,
6ae232055d4d8a97267517c5e50074c2c819941and * if any, must include the following acknowledgment:
6ae232055d4d8a97267517c5e50074c2c819941and * "This product includes software developed by the
6ae232055d4d8a97267517c5e50074c2c819941and * Apache Software Foundation (http://www.apache.org/)."
b43f840409794ed298e8634f6284741f193b6c4ftakashi * Alternately, this acknowledgment may appear in the software itself,
6ae232055d4d8a97267517c5e50074c2c819941and * if and wherever such third-party acknowledgments normally appear.
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * 4. The names "Apache" and "Apache Software Foundation" must
b43f840409794ed298e8634f6284741f193b6c4ftakashi * not be used to endorse or promote products derived from this
bc9d4698fce0238d2f6f2682e99423ebb1149976rbowen * software without prior written permission. For written
6ae232055d4d8a97267517c5e50074c2c819941and * permission, please contact apache@apache.org.
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung *
6ae232055d4d8a97267517c5e50074c2c819941and * 5. Products derived from this software may not be called "Apache",
b43f840409794ed298e8634f6284741f193b6c4ftakashi * nor may "Apache" appear in their name, without prior written
b43f840409794ed298e8634f6284741f193b6c4ftakashi * permission of the Apache Software Foundation.
b43f840409794ed298e8634f6284741f193b6c4ftakashi *
b43f840409794ed298e8634f6284741f193b6c4ftakashi * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
6ae232055d4d8a97267517c5e50074c2c819941and * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
6ae232055d4d8a97267517c5e50074c2c819941and * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
6ae232055d4d8a97267517c5e50074c2c819941and * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
6ae232055d4d8a97267517c5e50074c2c819941and * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
6ae232055d4d8a97267517c5e50074c2c819941and * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
6ae232055d4d8a97267517c5e50074c2c819941and * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
6ae232055d4d8a97267517c5e50074c2c819941and * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
6ae232055d4d8a97267517c5e50074c2c819941and * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
6ae232055d4d8a97267517c5e50074c2c819941and * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
6ae232055d4d8a97267517c5e50074c2c819941and * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6ae232055d4d8a97267517c5e50074c2c819941and * SUCH DAMAGE.
6ae232055d4d8a97267517c5e50074c2c819941and * ====================================================================
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * This software consists of voluntary contributions made by many
6ae232055d4d8a97267517c5e50074c2c819941and * individuals on behalf of the Apache Software Foundation. For more
6ae232055d4d8a97267517c5e50074c2c819941and * information on the Apache Software Foundation, please see
6ae232055d4d8a97267517c5e50074c2c819941and * <http://www.apache.org/>.
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * Portions of this software are based upon public domain software
6ae232055d4d8a97267517c5e50074c2c819941and * originally written at the National Center for Supercomputing Applications,
6ae232055d4d8a97267517c5e50074c2c819941and * University of Illinois, Urbana-Champaign.
6ae232055d4d8a97267517c5e50074c2c819941and */
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh
6ae232055d4d8a97267517c5e50074c2c819941and/*
6ae232055d4d8a97267517c5e50074c2c819941and * htdbm.c: simple program for manipulating DBM
6ae232055d4d8a97267517c5e50074c2c819941and * password databases for the Apache HTTP server
6ae232055d4d8a97267517c5e50074c2c819941and *
6ae232055d4d8a97267517c5e50074c2c819941and * Contributed by Mladen Turk <mturk@mappingsoft.com>
6ae232055d4d8a97267517c5e50074c2c819941and * 12 Oct 2001
6ae232055d4d8a97267517c5e50074c2c819941and */
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_lib.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_strings.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_file_io.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_file_info.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_pools.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_signal.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_md5.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_sha1.h"
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_dbm.h"
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#if APR_HAVE_STDLIB_H
6ae232055d4d8a97267517c5e50074c2c819941and#include <stdlib.h>
6ae232055d4d8a97267517c5e50074c2c819941and#endif
6ae232055d4d8a97267517c5e50074c2c819941and#if APR_HAVE_STRING_H
6ae232055d4d8a97267517c5e50074c2c819941and#include <string.h>
6ae232055d4d8a97267517c5e50074c2c819941and#endif
6ae232055d4d8a97267517c5e50074c2c819941and#if APR_HAVE_STRINGS_H
6ae232055d4d8a97267517c5e50074c2c819941and#include <strings.h>
6ae232055d4d8a97267517c5e50074c2c819941and#endif
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#if APR_CHARSET_EBCDIC
6ae232055d4d8a97267517c5e50074c2c819941and#include "apr_xlate.h"
6ae232055d4d8a97267517c5e50074c2c819941and#endif /*APR_CHARSET_EBCDIC*/
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#if APR_HAVE_CRYPT_H
6ae232055d4d8a97267517c5e50074c2c819941and#include <crypt.h>
6ae232055d4d8a97267517c5e50074c2c819941and#endif
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#if !APR_CHARSET_EBCDIC
6ae232055d4d8a97267517c5e50074c2c819941and#define LF 10
6ae232055d4d8a97267517c5e50074c2c819941and#define CR 13
6ae232055d4d8a97267517c5e50074c2c819941and#else /*APR_CHARSET_EBCDIC*/
6ae232055d4d8a97267517c5e50074c2c819941and#define LF '\n'
6ae232055d4d8a97267517c5e50074c2c819941and#define CR '\r'
6ae232055d4d8a97267517c5e50074c2c819941and#endif /*APR_CHARSET_EBCDIC*/
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#define MAX_STRING_LEN 256
6ae232055d4d8a97267517c5e50074c2c819941and#define ALG_PLAIN 0
6ae232055d4d8a97267517c5e50074c2c819941and#define ALG_APMD5 1
6ae232055d4d8a97267517c5e50074c2c819941and#define ALG_APSHA 2
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#if APR_HAVE_CRYPT_H
6ae232055d4d8a97267517c5e50074c2c819941and#define ALG_CRYPT 3
6ae232055d4d8a97267517c5e50074c2c819941and#endif
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#define ERR_FILEPERM 1
6ae232055d4d8a97267517c5e50074c2c819941and#define ERR_SYNTAX 2
6ae232055d4d8a97267517c5e50074c2c819941and#define ERR_PWMISMATCH 3
6ae232055d4d8a97267517c5e50074c2c819941and#define ERR_INTERRUPTED 4
6ae232055d4d8a97267517c5e50074c2c819941and#define ERR_OVERFLOW 5
6ae232055d4d8a97267517c5e50074c2c819941and#define ERR_BADUSER 6
6ae232055d4d8a97267517c5e50074c2c819941and#define ERR_EMPTY 7
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andtypedef struct htdbm_t htdbm_t;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstruct htdbm_t {
6ae232055d4d8a97267517c5e50074c2c819941and apr_dbm_t *dbm;
6ae232055d4d8a97267517c5e50074c2c819941and apr_pool_t *pool;
6ae232055d4d8a97267517c5e50074c2c819941and#if APR_CHARSET_EBCDIC
6ae232055d4d8a97267517c5e50074c2c819941and apr_xlate_t *to_ascii;
6ae232055d4d8a97267517c5e50074c2c819941and#endif
6ae232055d4d8a97267517c5e50074c2c819941and char *filename;
6ae232055d4d8a97267517c5e50074c2c819941and char *username;
6ae232055d4d8a97267517c5e50074c2c819941and char *userpass;
6ae232055d4d8a97267517c5e50074c2c819941and char *comment;
6ae232055d4d8a97267517c5e50074c2c819941and int create;
6ae232055d4d8a97267517c5e50074c2c819941and int rdonly;
6ae232055d4d8a97267517c5e50074c2c819941and int alg;
6ae232055d4d8a97267517c5e50074c2c819941and};
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#define HTDBM_MAKE 0
6ae232055d4d8a97267517c5e50074c2c819941and#define HTDBM_DELETE 1
6ae232055d4d8a97267517c5e50074c2c819941and#define HTDBM_VERIFY 2
6ae232055d4d8a97267517c5e50074c2c819941and#define HTDBM_LIST 3
6ae232055d4d8a97267517c5e50074c2c819941and#define HTDBM_NOFILE 4
6ae232055d4d8a97267517c5e50074c2c819941and#define HTDBM_STDIN 5
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic void htdbm_terminate(htdbm_t *htdbm)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and if (htdbm->dbm)
6ae232055d4d8a97267517c5e50074c2c819941and apr_dbm_close(htdbm->dbm);
6ae232055d4d8a97267517c5e50074c2c819941and htdbm->dbm = NULL;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic htdbm_t *h;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic void htdbm_interrupted(void)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and htdbm_terminate(h);
6ae232055d4d8a97267517c5e50074c2c819941and fprintf(stderr, "htdbm Interrupted !\n");
6ae232055d4d8a97267517c5e50074c2c819941and exit(ERR_INTERRUPTED);
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic apr_status_t htdbm_init(apr_pool_t **pool, htdbm_t **hdbm)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#if APR_CHARSET_EBCDIC
6ae232055d4d8a97267517c5e50074c2c819941and apr_status_t rv;
6ae232055d4d8a97267517c5e50074c2c819941and#endif
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and apr_initialize();
6ae232055d4d8a97267517c5e50074c2c819941and atexit(apr_terminate);
6ae232055d4d8a97267517c5e50074c2c819941and apr_pool_create( pool, NULL);
6ae232055d4d8a97267517c5e50074c2c819941and apr_signal(SIGINT, (void (*)(int)) htdbm_interrupted);
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and (*hdbm) = (htdbm_t *)apr_pcalloc(*pool, sizeof(htdbm_t));
6ae232055d4d8a97267517c5e50074c2c819941and (*hdbm)->pool = *pool;
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and#if APR_CHARSET_EBCDIC
6ae232055d4d8a97267517c5e50074c2c819941and rv = apr_xlate_open(to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, (*hdbm)->pool);
6ae232055d4d8a97267517c5e50074c2c819941and if (rv) {
6ae232055d4d8a97267517c5e50074c2c819941and fprintf(stderr, "apr_xlate_open(to ASCII)->%d\n", rv);
6ae232055d4d8a97267517c5e50074c2c819941and return APR_EGENERAL;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and rv = apr_SHA1InitEBCDIC((*hdbm)->to_ascii);
6ae232055d4d8a97267517c5e50074c2c819941and if (rv) {
6ae232055d4d8a97267517c5e50074c2c819941and fprintf(stderr, "apr_SHA1InitEBCDIC()->%d\n", rv);
6ae232055d4d8a97267517c5e50074c2c819941and return APR_EGENERAL;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and rv = apr_MD5InitEBCDIC((*hdbm)->to_ascii);
6ae232055d4d8a97267517c5e50074c2c819941and if (rv) {
6ae232055d4d8a97267517c5e50074c2c819941and fprintf(stderr, "apr_MD5InitEBCDIC()->%d\n", rv);
6ae232055d4d8a97267517c5e50074c2c819941and return APR_EGENERAL;
6ae232055d4d8a97267517c5e50074c2c819941and }
6ae232055d4d8a97267517c5e50074c2c819941and#endif /*APR_CHARSET_EBCDIC*/
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941and /* Set MD5 as default */
6ae232055d4d8a97267517c5e50074c2c819941and (*hdbm)->alg = ALG_APMD5;
6ae232055d4d8a97267517c5e50074c2c819941and return APR_SUCCESS;
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic apr_status_t) htdbm_open(htdbm_t *htdbm)
6ae232055d4d8a97267517c5e50074c2c819941and{
6ae232055d4d8a97267517c5e50074c2c819941and if (htdbm->create)
6ae232055d4d8a97267517c5e50074c2c819941and return apr_dbm_open(&htdbm->dbm, htdbm->filename, APR_DBM_RWCREATE,
6ae232055d4d8a97267517c5e50074c2c819941and APR_OS_DEFAULT, htdbm->pool);
6ae232055d4d8a97267517c5e50074c2c819941and else
6ae232055d4d8a97267517c5e50074c2c819941and return apr_dbm_open(&htdbm->dbm, htdbm->filename,
6ae232055d4d8a97267517c5e50074c2c819941and htdbm->rdonly ? APR_DBM_READONLY : APR_DBM_READWRITE,
6ae232055d4d8a97267517c5e50074c2c819941and APR_OS_DEFAULT, htdbm->pool);
6ae232055d4d8a97267517c5e50074c2c819941and}
6ae232055d4d8a97267517c5e50074c2c819941and
6ae232055d4d8a97267517c5e50074c2c819941andstatic char * ap_getword(apr_pool_t *atrans, char **line, char stop)
b43f840409794ed298e8634f6284741f193b6c4ftakashi{
bc9d4698fce0238d2f6f2682e99423ebb1149976rbowen char *pos = strrchr(*line, stop);
6ae232055d4d8a97267517c5e50074c2c819941and char *res;
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung
727872d18412fc021f03969b8641810d8896820bhumbedooh if (!pos) {
0d0ba3a410038e179b695446bb149cce6264e0abnd res = apr_pstrdup(atrans, *line);
727872d18412fc021f03969b8641810d8896820bhumbedooh *line += strlen(*line);
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh return res;
0d0ba3a410038e179b695446bb149cce6264e0abnd }
cc7e1025de9ac63bd4db6fe7f71c158b2cf09fe4humbedooh
727872d18412fc021f03969b8641810d8896820bhumbedooh res = apr_pstrndup(atrans, *line, pos - *line);
0d0ba3a410038e179b695446bb149cce6264e0abnd
0d0ba3a410038e179b695446bb149cce6264e0abnd while (*pos == stop)
0d0ba3a410038e179b695446bb149cce6264e0abnd ++pos;
ac082aefa89416cbdc9a1836eaf3bed9698201c8humbedooh *line = pos;
0d0ba3a410038e179b695446bb149cce6264e0abnd return res;
0d0ba3a410038e179b695446bb149cce6264e0abnd}
0d0ba3a410038e179b695446bb149cce6264e0abnd
727872d18412fc021f03969b8641810d8896820bhumbedoohstatic apr_status_t htdbm_save(htdbm_t *htdbm, int *changed)
0d0ba3a410038e179b695446bb149cce6264e0abnd{
0d0ba3a410038e179b695446bb149cce6264e0abnd apr_datum_t key, val;
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh
b43f840409794ed298e8634f6284741f193b6c4ftakashi if (!htdbm->username)
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen return APR_SUCCESS;
0d0ba3a410038e179b695446bb149cce6264e0abnd
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd key.dptr = htdbm->username;
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd key.dsize = strlen(htdbm->username);
7fec19672a491661b2fe4b29f685bc7f4efa64d4nd if (apr_dbm_exists(htdbm->dbm, key))
6ae232055d4d8a97267517c5e50074c2c819941and *changed = 1;
val.dsize = strlen(htdbm->userpass);
if (!htdbm->comment)
val.dptr = htdbm->userpass;
else {
val.dptr = apr_pstrcat(htdbm->pool, htdbm->userpass, ";",
htdbm->comment, NULL);
val.dsize += (strlen(htdbm->comment) + 1);
}
return apr_dbm_store(htdbm->dbm, key, val);
}
static apr_status_t htdbm_del(htdbm_t *htdbm)
{
apr_datum_t key;
key.dptr = htdbm->username;
key.dsize = strlen(htdbm->username);
if (!apr_dbm_exists(htdbm->dbm, key))
return APR_ENOENT;
return apr_dbm_delete(htdbm->dbm, key);
}
static apr_status_t htdbm_verify(htdbm_t *htdbm)
{
apr_datum_t key, val;
char pwd[MAX_STRING_LEN] = {0};
char *rec, *cmnt;
key.dptr = htdbm->username;
key.dsize = strlen(htdbm->username);
if (!apr_dbm_exists(htdbm->dbm, key))
return APR_ENOENT;
if (apr_dbm_fetch(htdbm->dbm, key, &val) != APR_SUCCESS)
return APR_ENOENT;
rec = apr_pstrndup(htdbm->pool, val.dptr, val.dsize);
cmnt = strchr(rec, ';');
if (cmnt)
strncpy(pwd, rec, cmnt - rec);
else
strcpy(pwd, rec);
return apr_password_validate(htdbm->userpass, pwd);
}
static apr_status_t htdbm_list(htdbm_t *htdbm)
{
apr_status_t rv;
apr_datum_t key, val;
char *rec, *cmnt;
char kb[MAX_STRING_LEN];
int i = 0;
rv = apr_dbm_firstkey(htdbm->dbm, &key);
if (rv != APR_SUCCESS) {
fprintf(stderr, "Empty database -- %s\n", htdbm->filename);
return APR_ENOENT;
}
rec = apr_pcalloc(htdbm->pool, HUGE_STRING_LEN);
fprintf(stderr, "Dumping records from database -- %s\n", htdbm->filename);
fprintf(stderr, " %-32sComment\n", "Username");
while (key.dptr != NULL) {
rv = apr_dbm_fetch(htdbm->dbm, key, &val);
if (rv != APR_SUCCESS) {
fprintf(stderr, "Failed getting data from %s\n", htdbm->filename);
return APR_EGENERAL;
}
strncpy(kb, key.dptr, key.dsize);
kb[key.dsize] = '\0';
fprintf(stderr, " %-32s", kb);
strncpy(rec, val.dptr, val.dsize);
rec[val.dsize] = '\0';
cmnt = strchr(rec, ';');
if (cmnt)
fprintf(stderr, cmnt + 1);
fprintf(stderr, "\n");
rv = apr_dbm_nextkey(htdbm->dbm, &key);
if (rv != APR_SUCCESS)
fprintf(stderr, "Failed getting NextKey\n");
++i;
}
fprintf(stderr, "Total #records : %d\n", i);
return APR_SUCCESS;
}
static void to64(char *s, unsigned long v, int n)
{
static unsigned char itoa64[] = /* 0 ... 63 => ASCII - 64 */
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
while (--n >= 0) {
*s++ = itoa64[v&0x3f];
v >>= 6;
}
}
static apr_status_t htdbm_make(htdbm_t *htdbm)
{
char cpw[MAX_STRING_LEN];
char salt[9];
switch (htdbm->alg) {
case ALG_APSHA:
/* XXX cpw >= 28 + strlen(sha1) chars - fixed len SHA */
apr_sha1_base64(htdbm->userpass,strlen(htdbm->userpass),cpw);
break;
case ALG_APMD5:
(void) srand((int) time((time_t *) NULL));
to64(&salt[0], rand(), 8);
salt[8] = '\0';
apr_md5_encode((const char *)htdbm->userpass, (const char *)salt,
cpw, sizeof(cpw));
break;
case ALG_PLAIN:
/* XXX this len limitation is not in sync with any HTTPd len. */
apr_cpystrn(cpw,htdbm->userpass,sizeof(cpw));
break;
#if APR_HAVE_CRYPT_H
case ALG_CRYPT:
(void) srand((int) time((time_t *) NULL));
to64(&salt[0], rand(), 8);
salt[8] = '\0';
apr_cpystrn(cpw, (char *)crypt(htdbm->userpass, salt), sizeof(cpw) - 1);
fprintf(stderr, "CRYPT is now depriciated, use MD5 instead !\n");
#endif
default:
break;
}
htdbm->userpass = apr_pstrdup(htdbm->pool, cpw);
return APR_SUCCESS;
}
static apr_status_t htdbm_valid_username(htdbm_t *htdbm)
{
if (!htdbm->username || (strlen(htdbm->username) > 64) || (strlen(htdbm->username) < 1)) {
fprintf(stderr, "Invalid username length\n");
return APR_EINVAL;
}
if (strchr(htdbm->username, ':')) {
fprintf(stderr, "Username contains invalid characters\n");
return APR_EINVAL;
}
return APR_SUCCESS;
}
static void htdbm_usage(void)
{
#if APR_HAVE_CRYPT_H
#define CRYPT_OPTION "d"
#else
#define CRYPT_OPTION ""
#endif
fprintf(stderr, "htdbm -- program for manipulating DBM password databases.\n\n");
fprintf(stderr, "Usage: htdbm [-cm"CRYPT_OPTION"pstvx] database username\n");
fprintf(stderr, " -b[cm"CRYPT_OPTION"ptsv] database username password\n");
fprintf(stderr, " -n[m"CRYPT_OPTION"pst] username\n");
fprintf(stderr, " -nb[m"CRYPT_OPTION"pst] username password\n");
fprintf(stderr, " -v[m"CRYPT_OPTION"ps] database username\n");
fprintf(stderr, " -vb[m"CRYPT_OPTION"ps] database username password\n");
fprintf(stderr, " -x[m"CRYPT_OPTION"ps] database username\n");
fprintf(stderr, " -l database\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " -b Use the password from the command line rather"
"than prompting for it.\n");
fprintf(stderr, " -c Create a new database.\n");
fprintf(stderr, " -n Don't update database; display results on stdout.\n");
fprintf(stderr, " -m Force MD5 encryption of the password (default).\n");
#if APR_HAVE_CRYPT_H
fprintf(stderr, " -d Force CRYPT encryption of the password (now depriciated).\n");
#endif
fprintf(stderr, " -p Do not encrypt the password (plaintext).\n");
fprintf(stderr, " -s Force SHA encryption of the password.\n");
fprintf(stderr, " -l Display usernames from database on stdout.\n");
fprintf(stderr, " -t The last param is username comment.\n");
fprintf(stderr, " -v Verify the username/password.\n");
fprintf(stderr, " -x Remove the username record from database.\n");
exit(ERR_SYNTAX);
}
int main(int argc, const char *argv[])
{
apr_pool_t *pool;
apr_status_t rv;
apr_size_t l;
char pwi[MAX_STRING_LEN];
char pwc[MAX_STRING_LEN];
char errbuf[MAX_STRING_LEN];
const char *arg;
int need_file = 1;
int need_user = 1;
int need_pwd = 1;
int need_cmnt = 0;
int pwd_supplied = 0;
int changed;
int cmd = HTDBM_MAKE;
int i;
int args_left = 2;
if ((rv = htdbm_init(&pool, &h)) != APR_SUCCESS) {
fprintf(stderr, "Unable to initialize htdbm terminating!\n");
apr_strerror(rv, errbuf, sizeof(errbuf));
exit(1);
}
/*
* Preliminary check to make sure they provided at least
* three arguments, we'll do better argument checking as
* we parse the command line.
*/
if (argc < 3)
htdbm_usage();
/*
* Go through the argument list and pick out any options. They
* have to precede any other arguments.
*/
for (i = 1; i < argc; i++) {
arg = argv[i];
if (*arg != '-')
break;
while (*++arg != '\0') {
switch (*arg) {
case 'b':
pwd_supplied = 1;
need_pwd = 0;
args_left++;
break;
case 'c':
h->create = 1;
break;
case 'n':
need_file = 0;
cmd = HTDBM_NOFILE;
args_left--;
break;
case 'l':
need_pwd = 0;
need_user = 0;
cmd = HTDBM_LIST;
h->rdonly = 1;
args_left--;
break;
case 't':
need_cmnt = 1;
args_left++;
break;
case 'v':
h->rdonly = 1;
cmd = HTDBM_VERIFY;
break;
case 'x':
need_pwd = 0;
cmd = HTDBM_DELETE;
break;
case 'm':
h->alg = ALG_APMD5;
break;
case 'p':
h->alg = ALG_PLAIN;
break;
case 's':
h->alg = ALG_APSHA;
break;
#if APR_HAVE_CRYPT_H
case 'd':
h->alg = ALG_CRYPT;
break;
#endif
default:
htdbm_usage();
break;
}
}
}
/*
* Make sure we still have exactly the right number of arguments left
* (the filename, the username, and possibly the password if -b was
* specified).
*/
if ((argc - i) != args_left)
htdbm_usage();
if (!need_file)
i--;
else {
h->filename = apr_pstrdup(h->pool, argv[i]);
if ((rv = htdbm_open(h)) != APR_SUCCESS) {
fprintf(stderr, "Error oppening database %s\n", argv[i]);
apr_strerror(rv, errbuf, sizeof(errbuf));
exit(ERR_FILEPERM);
}
}
if (need_user) {
h->username = apr_pstrdup(pool, argv[i+1]);
if (htdbm_valid_username(h) != APR_SUCCESS)
exit(ERR_BADUSER);
}
if (pwd_supplied)
h->userpass = apr_pstrdup(pool, argv[i+2]);
if (need_pwd) {
l = sizeof(pwc);
if (apr_password_get("Enter password : ", pwi, &l) != APR_SUCCESS) {
fprintf(stderr, "Password too long\n");
exit(ERR_OVERFLOW);
}
l = sizeof(pwc);
if (apr_password_get("Re-type password : ", pwc, &l) != APR_SUCCESS) {
fprintf(stderr, "Password too long\n");
exit(ERR_OVERFLOW);
}
if (strcmp(pwi, pwc) != 0) {
fprintf(stderr, "Password verification error\n");
exit(ERR_PWMISMATCH);
}
h->userpass = apr_pstrdup(pool, pwi);
}
if (need_cmnt && pwd_supplied)
h->comment = apr_pstrdup(pool, argv[i+3]);
else if (need_cmnt)
h->comment = apr_pstrdup(pool, argv[i+2]);
switch (cmd) {
case HTDBM_VERIFY:
if ((rv = htdbm_verify(h)) != APR_SUCCESS) {
if(rv == APR_ENOENT) {
fprintf(stderr, "The user '%s' cold not be found in database\n", h->username);
exit(ERR_BADUSER);
}
else {
fprintf(stderr, "Password mismatch for user '%s'\n", h->username);
exit(ERR_PWMISMATCH);
}
}
else
fprintf(stderr, "Password validated for user '%s'\n", h->username);
break;
case HTDBM_DELETE:
if (htdbm_del(h) != APR_SUCCESS) {
fprintf(stderr, "Cannot find user '%s' in database\n", h->username);
exit(ERR_BADUSER);
}
h->username = NULL;
changed = 1;
break;
case HTDBM_LIST:
htdbm_list(h);
break;
default:
htdbm_make(h);
break;
}
if (need_file && !h->rdonly) {
if ((rv = htdbm_save(h, &changed)) != APR_SUCCESS) {
apr_strerror(rv, errbuf, sizeof(errbuf));
exit(ERR_FILEPERM);
}
fprintf(stdout, "Database %s %s.\n", h->filename,
h->create ? "created" : (changed ? "modified" : "updated"));
}
if (cmd == HTDBM_NOFILE)
fprintf(stderr, "%s:%s\n", h->username, h->userpass);
htdbm_terminate(h);
apr_terminate();
return 0; /* Supress compiler warning. */
}