/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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
*/
staticvoid htdbm_usage(void)
{
fprintf(stderr, "htdbm -- program for manipulating DBM password databases.\n\n" "Usage: htdbm [-cimBdpstvx] [-C cost] [-TDBTYPE] database username\n" " -b[cmBdptsv] [-C cost] [-TDBTYPE] database username password\n" " -n[imBdpst] [-C cost] username\n" " -nb[mBdpst] [-C cost] username password\n" " -v[imBdps] [-C cost] [-TDBTYPE] database username\n" " -vb[mBdps] [-C cost] [-TDBTYPE] database username password\n" " -x [-TDBTYPE] database username\n" " -l [-TDBTYPE] database\n" "Options:\n" " -c Create a new database.\n" " -n Don't update database; display results on stdout.\n" " -b Use the password from the command line rather than prompting for it.\n" " -i Read password from stdin without verification (for script usage).\n" " -m Force MD5 hashing of the password (default).\n" " -B Force BCRYPT hashing of the password (very secure).\n" " -C Set the computing time used for the bcrypt algorithm\n" " (higher is more secure but slower, default: %d, valid: 4 to 31).\n" " -d Force CRYPT hashing of the password (8 chars max, insecure).\n" " -s Force SHA hashing of the password (insecure).\n" " -p Do not hash the password (plaintext, insecure).\n" " -T DBM Type (SDBM|GDBM|DB|default).\n" " -l Display usernames from database on stdout.\n" " -v Verify the username/password.\n" " -x Remove the username record from database.\n" " -t The last param is username comment.\n" "The SHA algorithm does not use a salt and is less secure than the " "MD5 algorithm.\n",
BCRYPT_DEFAULT_COST); exit(ERR_SYNTAX);
}
int main(int argc, constchar * const argv[])
{
apr_pool_t *pool;
apr_status_t rv; char errbuf[MAX_STRING_LEN]; int need_file = 1; int need_user = 1; int need_pwd = 1; int need_cmnt = 0; int changed = 0; int cmd = HTDBM_MAKE; int i, ret, args_left = 2;
apr_getopt_t *state; char opt; constchar *opt_arg;
if ((rv = htdbm_init(&pool, &h)) != APR_SUCCESS) {
fprintf(stderr, "Unable to initialize htdbm terminating!\n");
apr_strerror(rv, errbuf, sizeof(errbuf)); exit(1);
}
rv = apr_getopt_init(&state, pool, argc, argv); if (rv != APR_SUCCESS) exit(ERR_SYNTAX);
while ((rv = apr_getopt(state, "cnmspdBbtivxlC:T:", &opt, &opt_arg)) == APR_SUCCESS) { switch (opt) { 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'T':
h->type = apr_pstrdup(h->ctx.pool, opt_arg); break; case'v':
h->rdonly = 1;
cmd = HTDBM_VERIFY; break; case'x':
need_pwd = 0;
cmd = HTDBM_DELETE; break; default:
ret = parse_common_options(&h->ctx, opt, opt_arg); if (ret) {
fprintf(stderr, "Error: %s\n", h->ctx.errstr); exit(ret);
}
}
} if (h->ctx.passwd_src == PW_ARG) {
need_pwd = 0;
args_left++;
} /* * Make sure we still have exactly the right number of arguments left * (the filename, the username, and possibly the password if -b was * specified).
*/
i = state->ind; if (rv != APR_EOF || argc - i != args_left)
htdbm_usage();
if (need_file) {
h->filename = apr_pstrdup(h->ctx.pool, argv[i++]); if ((rv = htdbm_open(h)) != APR_SUCCESS) {
fprintf(stderr, "Error opening database %s\n", h->filename);
apr_strerror(rv, errbuf, sizeof(errbuf));
fprintf(stderr,"%s\n",errbuf); exit(ERR_FILEPERM);
}
} if (need_user) {
h->username = apr_pstrdup(pool, argv[i++]); if (htdbm_valid_username(h) != APR_SUCCESS) exit(ERR_BADUSER);
} if (h->ctx.passwd_src == PW_ARG)
h->ctx.passwd = apr_pstrdup(pool, argv[i++]);
if (need_pwd) {
ret = get_password(&h->ctx); if (ret) {
fprintf(stderr, "Error: %s\n", h->ctx.errstr); exit(ret);
}
} if (need_cmnt)
h->comment = apr_pstrdup(pool, argv[i++]);
switch (cmd) { case HTDBM_VERIFY: if ((rv = htdbm_verify(h)) != APR_SUCCESS) { if (APR_STATUS_IS_ENOENT(rv)) {
fprintf(stderr, "The user '%s' could 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:
ret = htdbm_make(h); if (ret) exit(ret); 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) { if (!need_cmnt) {
fprintf(stderr, "%s:%s\n", h->username, h->ctx.passwd);
} else {
fprintf(stderr, "%s:%s:%s\n", h->username, h->ctx.passwd,
h->comment);
}
}
htdbm_terminate(h);
return 0; /* Suppress compiler warning. */
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.16Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.