1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * htdbm.c: simple program for manipulating DBM
19 * password databases for the Apache HTTP server
21 * Contributed by Mladen Turk <mturk mappingsoft.com>
25 #include "passwd_common.h"
26 #include "apr_file_io.h"
27 #include "apr_file_info.h"
28 #include "apr_pools.h"
29 #include "apr_signal.h"
33 #include "apr_getopt.h"
41 #if APR_HAVE_STRINGS_H
46 #if APR_CHARSET_EBCDIC
47 #include "apr_xlate.h"
48 #endif /*APR_CHARSET_EBCDIC*/
58 typedef struct htdbm_t htdbm_t;
62 struct passwd_ctx ctx;
63 #if APR_CHARSET_EBCDIC
64 apr_xlate_t *to_ascii;
76 #define HTDBM_DELETE 1
77 #define HTDBM_VERIFY 2
79 #define HTDBM_NOFILE 4
81 static void terminate(void)
89 static void htdbm_terminate(htdbm_t *htdbm)
92 apr_dbm_close(htdbm->dbm);
98 static void htdbm_interrupted(void)
101 fprintf(stderr, "htdbm Interrupted !\n");
102 exit(ERR_INTERRUPTED);
105 static apr_status_t htdbm_init(apr_pool_t **pool, htdbm_t **hdbm)
108 #if APR_CHARSET_EBCDIC
112 apr_pool_create( pool, NULL);
113 apr_file_open_stderr(&errfile, *pool);
114 apr_signal(SIGINT, (void (*)(int)) htdbm_interrupted);
116 (*hdbm) = (htdbm_t *)apr_pcalloc(*pool, sizeof(htdbm_t));
117 (*hdbm)->ctx.pool = *pool;
119 #if APR_CHARSET_EBCDIC
120 rv = apr_xlate_open(&((*hdbm)->to_ascii), "ISO-8859-1", APR_DEFAULT_CHARSET, (*hdbm)->ctx.pool);
122 fprintf(stderr, "apr_xlate_open(to ASCII)->%d\n", rv);
125 rv = apr_SHA1InitEBCDIC((*hdbm)->to_ascii);
127 fprintf(stderr, "apr_SHA1InitEBCDIC()->%d\n", rv);
130 rv = apr_MD5InitEBCDIC((*hdbm)->to_ascii);
132 fprintf(stderr, "apr_MD5InitEBCDIC()->%d\n", rv);
135 #endif /*APR_CHARSET_EBCDIC*/
137 /* Set MD5 as default */
138 (*hdbm)->ctx.alg = ALG_APMD5;
139 (*hdbm)->type = "default";
143 static apr_status_t htdbm_open(htdbm_t *htdbm)
146 return apr_dbm_open_ex(&htdbm->dbm, htdbm->type, htdbm->filename, APR_DBM_RWCREATE,
147 APR_OS_DEFAULT, htdbm->ctx.pool);
149 return apr_dbm_open_ex(&htdbm->dbm, htdbm->type, htdbm->filename,
150 htdbm->rdonly ? APR_DBM_READONLY : APR_DBM_READWRITE,
151 APR_OS_DEFAULT, htdbm->ctx.pool);
154 static apr_status_t htdbm_save(htdbm_t *htdbm, int *changed)
156 apr_datum_t key, val;
158 if (!htdbm->username)
161 key.dptr = htdbm->username;
162 key.dsize = strlen(htdbm->username);
163 if (apr_dbm_exists(htdbm->dbm, key))
166 val.dsize = strlen(htdbm->ctx.passwd);
168 val.dptr = htdbm->ctx.passwd;
170 val.dptr = apr_pstrcat(htdbm->ctx.pool, htdbm->ctx.passwd, ":",
171 htdbm->comment, NULL);
172 val.dsize += (strlen(htdbm->comment) + 1);
174 return apr_dbm_store(htdbm->dbm, key, val);
177 static apr_status_t htdbm_del(htdbm_t *htdbm)
181 key.dptr = htdbm->username;
182 key.dsize = strlen(htdbm->username);
183 if (!apr_dbm_exists(htdbm->dbm, key))
186 return apr_dbm_delete(htdbm->dbm, key);
189 static apr_status_t htdbm_verify(htdbm_t *htdbm)
191 apr_datum_t key, val;
195 key.dptr = htdbm->username;
196 key.dsize = strlen(htdbm->username);
197 if (!apr_dbm_exists(htdbm->dbm, key))
199 if (apr_dbm_fetch(htdbm->dbm, key, &val) != APR_SUCCESS)
201 rec = apr_pstrndup(htdbm->ctx.pool, val.dptr, val.dsize);
202 cmnt = strchr(rec, ':');
204 pwd = apr_pstrndup(htdbm->ctx.pool, rec, cmnt - rec);
206 pwd = apr_pstrdup(htdbm->ctx.pool, rec);
207 return apr_password_validate(htdbm->ctx.passwd, pwd);
210 static apr_status_t htdbm_list(htdbm_t *htdbm)
213 apr_datum_t key, val;
217 rv = apr_dbm_firstkey(htdbm->dbm, &key);
218 if (rv != APR_SUCCESS) {
219 fprintf(stderr, "Empty database -- %s\n", htdbm->filename);
222 fprintf(stderr, "Dumping records from database -- %s\n", htdbm->filename);
223 fprintf(stderr, " %-32s Comment\n", "Username");
224 while (key.dptr != NULL) {
225 rv = apr_dbm_fetch(htdbm->dbm, key, &val);
226 if (rv != APR_SUCCESS) {
227 fprintf(stderr, "Failed getting data from %s\n", htdbm->filename);
230 /* Note: we don't store \0-terminators on our dbm data */
231 fprintf(stderr, " %-32.*s", (int)key.dsize, key.dptr);
232 cmnt = memchr(val.dptr, ':', val.dsize);
234 fprintf(stderr, " %.*s", (int)(val.dptr+val.dsize - (cmnt+1)), cmnt + 1);
235 fprintf(stderr, "\n");
236 rv = apr_dbm_nextkey(htdbm->dbm, &key);
237 if (rv != APR_SUCCESS)
238 fprintf(stderr, "Failed getting NextKey\n");
242 fprintf(stderr, "Total #records : %d\n", i);
246 static int htdbm_make(htdbm_t *htdbm)
248 char cpw[MAX_STRING_LEN];
251 htdbm->ctx.out = cpw;
252 htdbm->ctx.out_len = sizeof(cpw);
253 ret = mkhash(&htdbm->ctx);
255 fprintf(stderr, "Error: %s\n", htdbm->ctx.errstr);
258 htdbm->ctx.passwd = apr_pstrdup(htdbm->ctx.pool, cpw);
262 static apr_status_t htdbm_valid_username(htdbm_t *htdbm)
264 if (!htdbm->username || (strlen(htdbm->username) > 64) || (strlen(htdbm->username) < 1)) {
265 fprintf(stderr, "Invalid username length\n");
268 if (strchr(htdbm->username, ':')) {
269 fprintf(stderr, "Username contains invalid characters\n");
275 static void htdbm_usage(void)
278 "htdbm -- program for manipulating DBM password databases.\n\n"
279 "Usage: htdbm [-cimBdpstvx] [-C cost] [-TDBTYPE] database username\n"
280 " -b[cmBdptsv] [-C cost] [-TDBTYPE] database username password\n"
281 " -n[imBdpst] [-C cost] username\n"
282 " -nb[mBdpst] [-C cost] username password\n"
283 " -v[imBdps] [-C cost] [-TDBTYPE] database username\n"
284 " -vb[mBdps] [-C cost] [-TDBTYPE] database username password\n"
285 " -x [-TDBTYPE] database username\n"
286 " -l [-TDBTYPE] database\n"
288 " -c Create a new database.\n"
289 " -n Don't update database; display results on stdout.\n"
290 " -b Use the password from the command line rather than prompting for it.\n"
291 " -i Read password from stdin without verification (for script usage).\n"
292 " -m Force MD5 encryption of the password (default).\n"
293 " -B Force BCRYPT encryption of the password (very secure).\n"
294 " -C Set the computing time used for the bcrypt algorithm\n"
295 " (higher is more secure but slower, default: %d, valid: 4 to 31).\n"
296 " -d Force CRYPT encryption of the password (8 chars max, insecure).\n"
297 " -s Force SHA encryption of the password (insecure).\n"
298 " -p Do not encrypt the password (plaintext, insecure).\n"
299 " -T DBM Type (SDBM|GDBM|DB|default).\n"
300 " -l Display usernames from database on stdout.\n"
301 " -v Verify the username/password.\n"
302 " -x Remove the username record from database.\n"
303 " -t The last param is username comment.\n"
304 "The SHA algorithm does not use a salt and is less secure than the "
306 BCRYPT_DEFAULT_COST);
310 int main(int argc, const char * const argv[])
314 char errbuf[MAX_STRING_LEN];
320 int cmd = HTDBM_MAKE;
321 int i, ret, args_left = 2;
326 apr_app_initialize(&argc, &argv, NULL);
329 if ((rv = htdbm_init(&pool, &h)) != APR_SUCCESS) {
330 fprintf(stderr, "Unable to initialize htdbm terminating!\n");
331 apr_strerror(rv, errbuf, sizeof(errbuf));
335 rv = apr_getopt_init(&state, pool, argc, argv);
336 if (rv != APR_SUCCESS)
339 while ((rv = apr_getopt(state, "cnmspdBbDivxlC:T:", &opt, &opt_arg)) == APR_SUCCESS) {
361 h->type = apr_pstrdup(h->ctx.pool, opt_arg);
372 ret = parse_common_options(&h->ctx, opt, opt_arg);
374 fprintf(stderr, "Error: %s\n", h->ctx.errstr);
379 if (h->ctx.passwd_src == PW_ARG) {
384 * Make sure we still have exactly the right number of arguments left
385 * (the filename, the username, and possibly the password if -b was
389 if (rv != APR_EOF || argc - i != args_left)
393 h->filename = apr_pstrdup(h->ctx.pool, argv[i++]);
394 if ((rv = htdbm_open(h)) != APR_SUCCESS) {
395 fprintf(stderr, "Error opening database %s\n", h->filename);
396 apr_strerror(rv, errbuf, sizeof(errbuf));
397 fprintf(stderr,"%s\n",errbuf);
402 h->username = apr_pstrdup(pool, argv[i++]);
403 if (htdbm_valid_username(h) != APR_SUCCESS)
406 if (h->ctx.passwd_src == PW_ARG)
407 h->ctx.passwd = apr_pstrdup(pool, argv[i++]);
410 ret = get_password(&h->ctx);
412 fprintf(stderr, "Error: %s\n", h->ctx.errstr);
417 h->comment = apr_pstrdup(pool, argv[i++]);
421 if ((rv = htdbm_verify(h)) != APR_SUCCESS) {
422 if (APR_STATUS_IS_ENOENT(rv)) {
423 fprintf(stderr, "The user '%s' could not be found in database\n", h->username);
427 fprintf(stderr, "Password mismatch for user '%s'\n", h->username);
428 exit(ERR_PWMISMATCH);
432 fprintf(stderr, "Password validated for user '%s'\n", h->username);
435 if (htdbm_del(h) != APR_SUCCESS) {
436 fprintf(stderr, "Cannot find user '%s' in database\n", h->username);
451 if (need_file && !h->rdonly) {
452 if ((rv = htdbm_save(h, &changed)) != APR_SUCCESS) {
453 apr_strerror(rv, errbuf, sizeof(errbuf));
456 fprintf(stdout, "Database %s %s.\n", h->filename,
457 h->create ? "created" : (changed ? "modified" : "updated"));
459 if (cmd == HTDBM_NOFILE) {
461 fprintf(stderr, "%s:%s\n", h->username, h->ctx.passwd);
464 fprintf(stderr, "%s:%s:%s\n", h->username, h->ctx.passwd,
470 return 0; /* Suppress compiler warning. */