htdbm.c revision f4e4643c309e5b5da60e13f9a25984d54b307caa
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd/* ====================================================================
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * The Apache Software License, Version 1.1
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * reserved.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Redistribution and use in source and binary forms, with or without
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * modification, are permitted provided that the following conditions
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * are met:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * 1. Redistributions of source code must retain the above copyright
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * notice, this list of conditions and the following disclaimer.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * 2. Redistributions in binary form must reproduce the above copyright
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * notice, this list of conditions and the following disclaimer in
27e52281f1522522b170cafc76b08b58aa70ccaand * the documentation and/or other materials provided with the
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * distribution.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * 3. The end-user documentation included with the redistribution,
4b5981e276e93df97c34e4da05ca5cf8bbd937dand * if any, must include the following acknowledgment:
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * "This product includes software developed by the
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Apache Software Foundation (http://www.apache.org/)."
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * Alternately, this acknowledgment may appear in the software itself,
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * if and wherever such third-party acknowledgments normally appear.
ecc5150d35c0dc5ee5119c2717e6660fa331abbftakashi *
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd * 4. The names "Apache" and "Apache Software Foundation" must
63f06dce77bb2d9b1c5aa5deeb47a1069987fd1end * not be used to endorse or promote products derived from this
ecc5150d35c0dc5ee5119c2717e6660fa331abbftakashi * software without prior written permission. For written
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * permission, please contact apache@apache.org.
a76bfec941c4c5e3e1e24d7007108f4d3dac91a5nd *
a76bfec941c4c5e3e1e24d7007108f4d3dac91a5nd * 5. Products derived from this software may not be called "Apache",
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * nor may "Apache" appear in their name, without prior written
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * permission of the Apache Software Foundation.
6df89e6e4adeb986b41b7ec6b7593a887e031ce7nd *
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * SUCH DAMAGE.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * ====================================================================
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *
71fccc298df6a1540d408151a26aa22beed55d0bnd * This software consists of voluntary contributions made by many
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * individuals on behalf of the Apache Software Foundation. For more
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * information on the Apache Software Foundation, please see
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * <http://www.apache.org/>.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Portions of this software are based upon public domain software
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * originally written at the National Center for Supercomputing Applications,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * University of Illinois, Urbana-Champaign.
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd/*
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * htdbm.c: simple program for manipulating DBM
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * password databases for the Apache HTTP server
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * Contributed by Mladen Turk <mturk@mappingsoft.com>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd * 12 Oct 2001
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "apr.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "apr_lib.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "apr_strings.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "apr_file_io.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "apr_file_info.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "apr_pools.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "apr_signal.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "apr_md5.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "apr_sha1.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "apr_dbm.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#if APR_HAVE_STDLIB_H
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include <stdlib.h>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#endif
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#if APR_HAVE_STRING_H
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include <string.h>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#endif
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#if APR_HAVE_STRINGS_H
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include <strings.h>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#endif
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#if APR_CHARSET_EBCDIC
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include "apr_xlate.h"
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#endif /*APR_CHARSET_EBCDIC*/
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#if APR_HAVE_CRYPT_H
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#include <crypt.h>
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#endif
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
71fccc298df6a1540d408151a26aa22beed55d0bnd#if !APR_CHARSET_EBCDIC
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define LF 10
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define CR 13
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#else /*APR_CHARSET_EBCDIC*/
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define LF '\n'
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define CR '\r'
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#endif /*APR_CHARSET_EBCDIC*/
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define MAX_STRING_LEN 256
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define ALG_PLAIN 0
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define ALG_APMD5 1
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define ALG_APSHA 2
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#if APR_HAVE_CRYPT_H
5652dbe450e4fcfdf36d4cfb42d7f2345ded29a4maczniak#define ALG_CRYPT 3
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#endif
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define ERR_FILEPERM 1
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define ERR_SYNTAX 2
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define ERR_PWMISMATCH 3
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define ERR_INTERRUPTED 4
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define ERR_OVERFLOW 5
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define ERR_BADUSER 6
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define ERR_EMPTY 7
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndtypedef struct apu_htdbm_t apu_htdbm_t;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstruct apu_htdbm_t {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd apr_dbm_t *dbm;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd apr_pool_t *pool;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#if APR_CHARSET_EBCDIC
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd apr_xlate_t *to_ascii;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#endif
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd char *filename;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd char *username;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd char *userpass;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd char *comment;
71fccc298df6a1540d408151a26aa22beed55d0bnd int create;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd int rdonly;
a8703cfb67133446eef7af1043640e71486e9ecand int alg;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd};
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define APU_HTDBM_DECLARE(x) static x
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define APU_HTDBM_STANDALONE 1
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define APU_HTDBM_MAKE 0
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define APU_HTDBM_DELETE 1
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define APU_HTDBM_VERIFY 2
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define APU_HTDBM_LIST 3
71fccc298df6a1540d408151a26aa22beed55d0bnd#define APU_HTDBM_NOFILE 4
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#define APU_HTDBM_STDIN 5
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndAPU_HTDBM_DECLARE(void) apu_htdbm_terminate(apu_htdbm_t *htdbm)
71fccc298df6a1540d408151a26aa22beed55d0bnd{
71fccc298df6a1540d408151a26aa22beed55d0bnd
71fccc298df6a1540d408151a26aa22beed55d0bnd if (htdbm->dbm)
71fccc298df6a1540d408151a26aa22beed55d0bnd apr_dbm_close(htdbm->dbm);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd htdbm->dbm = NULL;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd}
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
b3c7a2279fa7a45f5807d9a404760b9b3760df50nd#if APU_HTDBM_STANDALONE
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndstatic apu_htdbm_t *h;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndAPU_HTDBM_DECLARE(void) apu_htdbm_interrupted(void)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd{
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd apu_htdbm_terminate(h);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd fprintf(stderr, "htdbm Interrupted !\n");
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd exit(ERR_INTERRUPTED);
22d5d84393d960a2027f472036f3fee15d7dbce9nd}
22d5d84393d960a2027f472036f3fee15d7dbce9nd#endif
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndAPU_HTDBM_DECLARE(apr_status_t) apu_htdbm_init(apr_pool_t **pool, apu_htdbm_t **hdbm)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd{
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
623eebe956d9c2d6d073ed3eae855b56030b40e9noodl#if APR_CHARSET_EBCDIC
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd apr_status_t rv;
ffb88a4885747797937e30a5ac8b1606da3cb4adnd#endif
909ce17e2bd0faef7b1c294f2307f009793fd493nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#if APU_HTDBM_STANDALONE
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd apr_initialize();
42af92a661a06b3cebc88d585aad75064a309d51nd atexit(apr_terminate);
ffb88a4885747797937e30a5ac8b1606da3cb4adnd apr_pool_create( pool, NULL);
6fe26506780e73be2a412d758af77fafdf03291and apr_signal(SIGINT, (void (*)(int)) apu_htdbm_interrupted);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#endif
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd (*hdbm) = (apu_htdbm_t *)apr_pcalloc(*pool, sizeof(apu_htdbm_t));
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd (*hdbm)->pool = *pool;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
08cf4a15275e4cb65a424b3a1db5410bfb51085cjim#if APR_CHARSET_EBCDIC
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd rv = apr_xlate_open(to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, (*hdbm)->pool);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (rv) {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd fprintf(stderr, "apr_xlate_open(to ASCII)->%d\n", rv);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_EGENERAL;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd rv = apr_SHA1InitEBCDIC((*hdbm)->to_ascii);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (rv) {
05ede5110427cb9dc071cc671d5aaba5d3b88c79nd fprintf(stderr, "apr_SHA1InitEBCDIC()->%d\n", rv);
e8b603fa9ccf7b17b11b42df6d8916fd97c2331dnd return APR_EGENERAL;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd rv = apr_MD5InitEBCDIC((*hdbm)->to_ascii);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (rv) {
611049e38bfbaeb173d2d7fab2e44a48753436a1nd fprintf(stderr, "apr_MD5InitEBCDIC()->%d\n", rv);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_EGENERAL;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd#endif /*APR_CHARSET_EBCDIC*/
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd /* Set MD5 as default */
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd (*hdbm)->alg = ALG_APMD5;
03a4ff9ac4c9b8009249010e7c53bb86ff05915and return APR_SUCCESS;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd}
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
4b5981e276e93df97c34e4da05ca5cf8bbd937dandAPU_HTDBM_DECLARE(apr_status_t) apu_htdbm_open(apu_htdbm_t *htdbm)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd{
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (htdbm->create)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return apr_dbm_open(&htdbm->dbm, htdbm->filename, APR_DBM_RWCREATE,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd APR_OS_DEFAULT, htdbm->pool);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd else
d2b809e5d72658bff23819d8b77f20e4939af541nd return apr_dbm_open(&htdbm->dbm, htdbm->filename,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd htdbm->rdonly ? APR_DBM_READONLY : APR_DBM_READWRITE,
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd APR_OS_DEFAULT, htdbm->pool);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd}
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndAPU_HTDBM_DECLARE(char *) ap_getword(apr_pool_t *atrans, char **line, char stop)
27d778df0b517e1578f907d2e51eb961cd8ee5fbjim{
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd char *pos = strrchr(*line, stop);
a43bfa789f4e52dde53ae8e53fa0427b5c1cf977nd char *res;
a43bfa789f4e52dde53ae8e53fa0427b5c1cf977nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (!pos) {
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi res = apr_pstrdup(atrans, *line);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *line += strlen(*line);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return res;
ecc5150d35c0dc5ee5119c2717e6660fa331abbftakashi }
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
79b024b81f6bb3c44dce77a7552191daf8b522d2jim res = apr_pstrndup(atrans, *line, pos - *line);
f772e8f448c223e5ea306f1bf92d97d968f972d5jim
f772e8f448c223e5ea306f1bf92d97d968f972d5jim while (*pos == stop)
f772e8f448c223e5ea306f1bf92d97d968f972d5jim ++pos;
fac8c35bfb158112226ab43ddf84d59daca5dc30nd *line = pos;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return res;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd}
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5ndAPU_HTDBM_DECLARE(apr_status_t) apu_htdbm_save(apu_htdbm_t *htdbm, int *changed)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd{
898711b68797304101de0882fa576c8908acfae6nd apr_datum_t key, val;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (!htdbm->username)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_SUCCESS;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
03c25fb6f628ac81f2ecb637d1e7502dcee783f3nd key.dptr = htdbm->username;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd key.dsize = strlen(htdbm->username);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (apr_dbm_exists(htdbm->dbm, key))
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd *changed = 1;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd val.dsize = strlen(htdbm->userpass);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (!htdbm->comment)
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd val.dptr = htdbm->userpass;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd else {
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd val.dptr = apr_pstrcat(htdbm->pool, htdbm->userpass, ";",
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd htdbm->comment, NULL);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd val.dsize += (strlen(htdbm->comment) + 1);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd }
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return apr_dbm_store(htdbm->dbm, key, val);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd}
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
71fccc298df6a1540d408151a26aa22beed55d0bndAPU_HTDBM_DECLARE(apr_status_t) apu_htdbm_del(apu_htdbm_t *htdbm)
71fccc298df6a1540d408151a26aa22beed55d0bnd{
71fccc298df6a1540d408151a26aa22beed55d0bnd apr_datum_t key;
71fccc298df6a1540d408151a26aa22beed55d0bnd
71fccc298df6a1540d408151a26aa22beed55d0bnd key.dptr = htdbm->username;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd key.dsize = strlen(htdbm->username);
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd if (!apr_dbm_exists(htdbm->dbm, key))
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd return APR_ENOENT;
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd return apr_dbm_delete(htdbm->dbm, key);
ecc5150d35c0dc5ee5119c2717e6660fa331abbftakashi}
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd
63f06dce77bb2d9b1c5aa5deeb47a1069987fd1endAPU_HTDBM_DECLARE(apr_status_t) apu_htdbm_verify(apu_htdbm_t *htdbm)
ecc5150d35c0dc5ee5119c2717e6660fa331abbftakashi{
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd apr_datum_t key, val;
6eed902e5b4d3e016e220bfbf8769a87c4cb242enoodl char pwd[MAX_STRING_LEN] = {0};
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd char *rec, *cmnt;
6116c12fdd3ed06d388fe6572e50a22e9320dfa5nd
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);
}
APU_HTDBM_DECLARE(apr_status_t) apu_htdbm_list(apu_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;
}
}
APU_HTDBM_DECLARE(apr_status_t) apu_htdbm_make(apu_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;
}
APU_HTDBM_DECLARE(apr_status_t) apu_htdbm_valid_username(apu_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 = APU_HTDBM_MAKE;
int i;
int args_left = 2;
if ((rv = apu_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 = APU_HTDBM_NOFILE;
args_left--;
break;
case 'l':
need_pwd = 0;
need_user = 0;
cmd = APU_HTDBM_LIST;
h->rdonly = 1;
args_left--;
break;
case 't':
need_cmnt = 1;
args_left++;
break;
case 'v':
h->rdonly = 1;
cmd = APU_HTDBM_VERIFY;
break;
case 'x':
need_pwd = 0;
cmd = APU_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 = apu_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 (apu_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 APU_HTDBM_VERIFY:
if ((rv = apu_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 APU_HTDBM_DELETE:
if (apu_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 APU_HTDBM_LIST:
apu_htdbm_list(h);
break;
default:
apu_htdbm_make(h);
break;
}
if (need_file && !h->rdonly) {
if ((rv = apu_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 == APU_HTDBM_NOFILE)
fprintf(stderr, "%s:%s\n", h->username, h->userpass);
apu_htdbm_terminate(h);
apr_terminate();
return 0; /* Supress compiler warning. */
}