📄 htdbm.c
字号:
/* Copyright 2001-2005 The Apache Software Foundation or its licensors, as * applicable. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * htdbm.c: simple program for manipulating DBM * password databases for the Apache HTTP server * * Contributed by Mladen Turk <mturk@mappingsoft.com> * 12 Oct 2001 */#include "apr.h"#include "apr_lib.h"#include "apr_strings.h"#include "apr_file_io.h"#include "apr_file_info.h"#include "apr_pools.h"#include "apr_signal.h"#include "apr_md5.h"#include "apr_sha1.h"#include "apr_dbm.h"#if APR_HAVE_STDLIB_H#include <stdlib.h>#endif#if APR_HAVE_STRING_H#include <string.h>#endif#if APR_HAVE_STRINGS_H#include <strings.h>#endif#include <time.h>#if APR_CHARSET_EBCDIC#include "apr_xlate.h"#endif /*APR_CHARSET_EBCDIC*/#if APR_HAVE_CRYPT_H#include <crypt.h>#endif#if !APR_CHARSET_EBCDIC#define LF 10#define CR 13#else /*APR_CHARSET_EBCDIC*/#define LF '\n'#define CR '\r'#endif /*APR_CHARSET_EBCDIC*/#define MAX_STRING_LEN 256#define ALG_PLAIN 0#define ALG_APMD5 1#define ALG_APSHA 2 #if APR_HAVE_CRYPT_H#define ALG_CRYPT 3#endif#define ERR_FILEPERM 1#define ERR_SYNTAX 2#define ERR_PWMISMATCH 3#define ERR_INTERRUPTED 4#define ERR_OVERFLOW 5#define ERR_BADUSER 6#define ERR_EMPTY 7typedef struct htdbm_t htdbm_t;struct htdbm_t { apr_dbm_t *dbm; apr_pool_t *pool;#if APR_CHARSET_EBCDIC apr_xlate_t *to_ascii;#endif char *filename; char *username; char *userpass; char *comment; char *type; int create; int rdonly; int alg;};#define HTDBM_MAKE 0#define HTDBM_DELETE 1#define HTDBM_VERIFY 2#define HTDBM_LIST 3#define HTDBM_NOFILE 4#define HTDBM_STDIN 5static void terminate(void){ apr_terminate();#ifdef NETWARE pressanykey();#endif}static void htdbm_terminate(htdbm_t *htdbm) { if (htdbm->dbm) apr_dbm_close(htdbm->dbm); htdbm->dbm = NULL;}static htdbm_t *h; static void htdbm_interrupted(void) { htdbm_terminate(h); fprintf(stderr, "htdbm Interrupted !\n"); exit(ERR_INTERRUPTED);}static apr_status_t htdbm_init(apr_pool_t **pool, htdbm_t **hdbm) {#if APR_CHARSET_EBCDIC apr_status_t rv;#endif apr_pool_create( pool, NULL); apr_signal(SIGINT, (void (*)(int)) htdbm_interrupted); (*hdbm) = (htdbm_t *)apr_pcalloc(*pool, sizeof(htdbm_t)); (*hdbm)->pool = *pool;#if APR_CHARSET_EBCDIC rv = apr_xlate_open(&((*hdbm)->to_ascii), "ISO8859-1", APR_DEFAULT_CHARSET, (*hdbm)->pool); if (rv) { fprintf(stderr, "apr_xlate_open(to ASCII)->%d\n", rv); return APR_EGENERAL; } rv = apr_SHA1InitEBCDIC((*hdbm)->to_ascii); if (rv) { fprintf(stderr, "apr_SHA1InitEBCDIC()->%d\n", rv); return APR_EGENERAL; } rv = apr_MD5InitEBCDIC((*hdbm)->to_ascii); if (rv) { fprintf(stderr, "apr_MD5InitEBCDIC()->%d\n", rv); return APR_EGENERAL; }#endif /*APR_CHARSET_EBCDIC*/ /* Set MD5 as default */ (*hdbm)->alg = ALG_APMD5; (*hdbm)->type = "default"; return APR_SUCCESS;}static apr_status_t htdbm_open(htdbm_t *htdbm) { if (htdbm->create) return apr_dbm_open_ex(&htdbm->dbm, htdbm->type, htdbm->filename, APR_DBM_RWCREATE, APR_OS_DEFAULT, htdbm->pool); else return apr_dbm_open_ex(&htdbm->dbm, htdbm->type, htdbm->filename, htdbm->rdonly ? APR_DBM_READONLY : APR_DBM_READWRITE, APR_OS_DEFAULT, htdbm->pool);}static apr_status_t htdbm_save(htdbm_t *htdbm, int *changed) { apr_datum_t key, val; if (!htdbm->username) return APR_SUCCESS; key.dptr = htdbm->username; key.dsize = strlen(htdbm->username); if (apr_dbm_exists(htdbm->dbm, key)) *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];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -