1 /* Copyright 2001-2004 Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * htdbm.c: simple program for manipulating DBM
18 * password databases for the Apache HTTP server
20 * Contributed by Mladen Turk <mturk mappingsoft.com>
26 #include "apr_strings.h"
27 #include "apr_file_io.h"
28 #include "apr_file_info.h"
29 #include "apr_pools.h"
30 #include "apr_signal.h"
41 #if APR_HAVE_STRINGS_H
46 #if APR_CHARSET_EBCDIC
47 #include "apr_xlate.h"
48 #endif /*APR_CHARSET_EBCDIC*/
55 #if !APR_CHARSET_EBCDIC
58 #else /*APR_CHARSET_EBCDIC*/
61 #endif /*APR_CHARSET_EBCDIC*/
63 #define MAX_STRING_LEN 256
73 #define ERR_FILEPERM 1
75 #define ERR_PWMISMATCH 3
76 #define ERR_INTERRUPTED 4
77 #define ERR_OVERFLOW 5
82 typedef struct htdbm_t htdbm_t;
87 #if APR_CHARSET_EBCDIC
88 apr_xlate_t *to_ascii;
102 #define HTDBM_DELETE 1
103 #define HTDBM_VERIFY 2
105 #define HTDBM_NOFILE 4
106 #define HTDBM_STDIN 5
108 static void htdbm_terminate(htdbm_t *htdbm)
112 apr_dbm_close(htdbm->dbm);
118 static void htdbm_interrupted(void)
121 fprintf(stderr, "htdbm Interrupted !\n");
122 exit(ERR_INTERRUPTED);
125 static apr_status_t htdbm_init(apr_pool_t **pool, htdbm_t **hdbm)
128 #if APR_CHARSET_EBCDIC
132 apr_pool_create( pool, NULL);
133 apr_signal(SIGINT, (void (*)(int)) htdbm_interrupted);
135 (*hdbm) = (htdbm_t *)apr_pcalloc(*pool, sizeof(htdbm_t));
136 (*hdbm)->pool = *pool;
138 #if APR_CHARSET_EBCDIC
139 rv = apr_xlate_open(&((*hdbm)->to_ascii), "ISO8859-1", APR_DEFAULT_CHARSET, (*hdbm)->pool);
141 fprintf(stderr, "apr_xlate_open(to ASCII)->%d\n", rv);
144 rv = apr_SHA1InitEBCDIC((*hdbm)->to_ascii);
146 fprintf(stderr, "apr_SHA1InitEBCDIC()->%d\n", rv);
149 rv = apr_MD5InitEBCDIC((*hdbm)->to_ascii);
151 fprintf(stderr, "apr_MD5InitEBCDIC()->%d\n", rv);
154 #endif /*APR_CHARSET_EBCDIC*/
156 /* Set MD5 as default */
157 (*hdbm)->alg = ALG_APMD5;
158 (*hdbm)->type = "default";
162 static apr_status_t htdbm_open(htdbm_t *htdbm)
165 return apr_dbm_open_ex(&htdbm->dbm, htdbm->type, htdbm->filename, APR_DBM_RWCREATE,
166 APR_OS_DEFAULT, htdbm->pool);
168 return apr_dbm_open_ex(&htdbm->dbm, htdbm->type, htdbm->filename,
169 htdbm->rdonly ? APR_DBM_READONLY : APR_DBM_READWRITE,
170 APR_OS_DEFAULT, htdbm->pool);
173 static apr_status_t htdbm_save(htdbm_t *htdbm, int *changed)
175 apr_datum_t key, val;
177 if (!htdbm->username)
180 key.dptr = htdbm->username;
181 key.dsize = strlen(htdbm->username);
182 if (apr_dbm_exists(htdbm->dbm, key))
185 val.dsize = strlen(htdbm->userpass);
187 val.dptr = htdbm->userpass;
189 val.dptr = apr_pstrcat(htdbm->pool, htdbm->userpass, ":",
190 htdbm->comment, NULL);
191 val.dsize += (strlen(htdbm->comment) + 1);
193 return apr_dbm_store(htdbm->dbm, key, val);
196 static apr_status_t htdbm_del(htdbm_t *htdbm)
200 key.dptr = htdbm->username;
201 key.dsize = strlen(htdbm->username);
202 if (!apr_dbm_exists(htdbm->dbm, key))
205 return apr_dbm_delete(htdbm->dbm, key);
208 static apr_status_t htdbm_verify(htdbm_t *htdbm)
210 apr_datum_t key, val;
211 char pwd[MAX_STRING_LEN] = {0};
214 key.dptr = htdbm->username;
215 key.dsize = strlen(htdbm->username);
216 if (!apr_dbm_exists(htdbm->dbm, key))
218 if (apr_dbm_fetch(htdbm->dbm, key, &val) != APR_SUCCESS)
220 rec = apr_pstrndup(htdbm->pool, val.dptr, val.dsize);
221 cmnt = strchr(rec, ';');
223 strncpy(pwd, rec, cmnt - rec);
226 return apr_password_validate(htdbm->userpass, pwd);
229 static apr_status_t htdbm_list(htdbm_t *htdbm)
232 apr_datum_t key, val;
234 char kb[MAX_STRING_LEN];
237 rv = apr_dbm_firstkey(htdbm->dbm, &key);
238 if (rv != APR_SUCCESS) {
239 fprintf(stderr, "Empty database -- %s\n", htdbm->filename);
242 rec = apr_pcalloc(htdbm->pool, HUGE_STRING_LEN);
244 fprintf(stderr, "Dumping records from database -- %s\n", htdbm->filename);
245 fprintf(stderr, " %-32sComment\n", "Username");
246 while (key.dptr != NULL) {
247 rv = apr_dbm_fetch(htdbm->dbm, key, &val);
248 if (rv != APR_SUCCESS) {
249 fprintf(stderr, "Failed getting data from %s\n", htdbm->filename);
252 strncpy(kb, key.dptr, key.dsize);
253 kb[key.dsize] = '\0';
254 fprintf(stderr, " %-32s", kb);
255 strncpy(rec, val.dptr, val.dsize);
256 rec[val.dsize] = '\0';
257 cmnt = strchr(rec, ';');
259 fprintf(stderr, cmnt + 1);
260 fprintf(stderr, "\n");
261 rv = apr_dbm_nextkey(htdbm->dbm, &key);
262 if (rv != APR_SUCCESS)
263 fprintf(stderr, "Failed getting NextKey\n");
267 fprintf(stderr, "Total #records : %d\n", i);
271 static void to64(char *s, unsigned long v, int n)
273 static unsigned char itoa64[] = /* 0 ... 63 => ASCII - 64 */
274 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
277 *s++ = itoa64[v&0x3f];
282 static apr_status_t htdbm_make(htdbm_t *htdbm)
284 char cpw[MAX_STRING_LEN];
287 switch (htdbm->alg) {
289 /* XXX cpw >= 28 + strlen(sha1) chars - fixed len SHA */
290 apr_sha1_base64(htdbm->userpass,strlen(htdbm->userpass),cpw);
294 (void) srand((int) time((time_t *) NULL));
295 to64(&salt[0], rand(), 8);
297 apr_md5_encode((const char *)htdbm->userpass, (const char *)salt,
301 /* XXX this len limitation is not in sync with any HTTPd len. */
302 apr_cpystrn(cpw,htdbm->userpass,sizeof(cpw));
306 (void) srand((int) time((time_t *) NULL));
307 to64(&salt[0], rand(), 8);
309 apr_cpystrn(cpw, (char *)crypt(htdbm->userpass, salt), sizeof(cpw) - 1);
310 fprintf(stderr, "CRYPT is now deprecated, use MD5 instead!\n");
315 htdbm->userpass = apr_pstrdup(htdbm->pool, cpw);
319 static apr_status_t htdbm_valid_username(htdbm_t *htdbm)
321 if (!htdbm->username || (strlen(htdbm->username) > 64) || (strlen(htdbm->username) < 1)) {
322 fprintf(stderr, "Invalid username length\n");
325 if (strchr(htdbm->username, ':')) {
326 fprintf(stderr, "Username contains invalid characters\n");
332 static void htdbm_usage(void)
336 #define CRYPT_OPTION "d"
338 #define CRYPT_OPTION ""
340 fprintf(stderr, "htdbm -- program for manipulating DBM password databases.\n\n");
341 fprintf(stderr, "Usage: htdbm [-cm"CRYPT_OPTION"pstvx] [-TDBTYPE] database username\n");
342 fprintf(stderr, " -b[cm"CRYPT_OPTION"ptsv] [-TDBTYPE] database username password\n");
343 fprintf(stderr, " -n[m"CRYPT_OPTION"pst] username\n");
344 fprintf(stderr, " -nb[m"CRYPT_OPTION"pst] username password\n");
345 fprintf(stderr, " -v[m"CRYPT_OPTION"ps] [-TDBTYPE] database username\n");
346 fprintf(stderr, " -vb[m"CRYPT_OPTION"ps] [-TDBTYPE] database username password\n");
347 fprintf(stderr, " -x[m"CRYPT_OPTION"ps] [-TDBTYPE] database username\n");
348 fprintf(stderr, " -l [-TDBTYPE] database\n");
349 fprintf(stderr, "Options:\n");
350 fprintf(stderr, " -b Use the password from the command line rather "
351 "than prompting for it.\n");
352 fprintf(stderr, " -c Create a new database.\n");
353 fprintf(stderr, " -n Don't update database; display results on stdout.\n");
354 fprintf(stderr, " -m Force MD5 encryption of the password (default).\n");
356 fprintf(stderr, " -d Force CRYPT encryption of the password (now deprecated).\n");
358 fprintf(stderr, " -p Do not encrypt the password (plaintext).\n");
359 fprintf(stderr, " -s Force SHA encryption of the password.\n");
360 fprintf(stderr, " -T DBM Type (SDBM|GDBM|DB|default).\n");
361 fprintf(stderr, " -l Display usernames from database on stdout.\n");
362 fprintf(stderr, " -t The last param is username comment.\n");
363 fprintf(stderr, " -v Verify the username/password.\n");
364 fprintf(stderr, " -x Remove the username record from database.\n");
370 int main(int argc, const char * const argv[])
375 char pwi[MAX_STRING_LEN];
376 char pwc[MAX_STRING_LEN];
377 char errbuf[MAX_STRING_LEN];
383 int pwd_supplied = 0;
385 int cmd = HTDBM_MAKE;
389 apr_app_initialize(&argc, &argv, NULL);
390 atexit(apr_terminate);
392 if ((rv = htdbm_init(&pool, &h)) != APR_SUCCESS) {
393 fprintf(stderr, "Unable to initialize htdbm terminating!\n");
394 apr_strerror(rv, errbuf, sizeof(errbuf));
398 * Preliminary check to make sure they provided at least
399 * three arguments, we'll do better argument checking as
400 * we parse the command line.
405 * Go through the argument list and pick out any options. They
406 * have to precede any other arguments.
408 for (i = 1; i < argc; i++) {
413 while (*++arg != '\0') {
440 h->type = apr_pstrdup(h->pool, ++arg);
443 --arg; /* so incrementing this in the loop with find a null */
474 * Make sure we still have exactly the right number of arguments left
475 * (the filename, the username, and possibly the password if -b was
478 if ((argc - i) != args_left)
484 h->filename = apr_pstrdup(h->pool, argv[i]);
485 if ((rv = htdbm_open(h)) != APR_SUCCESS) {
486 fprintf(stderr, "Error opening database %s\n", argv[i]);
487 apr_strerror(rv, errbuf, sizeof(errbuf));
488 fprintf(stderr,"%s\n",errbuf);
493 h->username = apr_pstrdup(pool, argv[i+1]);
494 if (htdbm_valid_username(h) != APR_SUCCESS)
498 h->userpass = apr_pstrdup(pool, argv[i+2]);
502 if (apr_password_get("Enter password : ", pwi, &l) != APR_SUCCESS) {
503 fprintf(stderr, "Password too long\n");
507 if (apr_password_get("Re-type password : ", pwc, &l) != APR_SUCCESS) {
508 fprintf(stderr, "Password too long\n");
511 if (strcmp(pwi, pwc) != 0) {
512 fprintf(stderr, "Password verification error\n");
513 exit(ERR_PWMISMATCH);
516 h->userpass = apr_pstrdup(pool, pwi);
518 if (need_cmnt && pwd_supplied)
519 h->comment = apr_pstrdup(pool, argv[i+3]);
521 h->comment = apr_pstrdup(pool, argv[i+2]);
525 if ((rv = htdbm_verify(h)) != APR_SUCCESS) {
526 if(rv == APR_ENOENT) {
527 fprintf(stderr, "The user '%s' could not be found in database\n", h->username);
531 fprintf(stderr, "Password mismatch for user '%s'\n", h->username);
532 exit(ERR_PWMISMATCH);
536 fprintf(stderr, "Password validated for user '%s'\n", h->username);
539 if (htdbm_del(h) != APR_SUCCESS) {
540 fprintf(stderr, "Cannot find user '%s' in database\n", h->username);
554 if (need_file && !h->rdonly) {
555 if ((rv = htdbm_save(h, &changed)) != APR_SUCCESS) {
556 apr_strerror(rv, errbuf, sizeof(errbuf));
559 fprintf(stdout, "Database %s %s.\n", h->filename,
560 h->create ? "created" : (changed ? "modified" : "updated"));
562 if (cmd == HTDBM_NOFILE) {
564 fprintf(stderr, "%s:%s\n", h->username, h->userpass);
567 fprintf(stderr, "%s:%s:%s\n", h->username, h->userpass,
574 return 0; /* Suppress compiler warning. */