1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
/* 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
*/
#include "passwd_common.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"
#include "apr_getopt.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_UNISTD_H
#include <unistd.h>
#endif
#if APR_HAVE_CRYPT_H
#include <crypt.h>
#endif
typedef struct htdbm_t htdbm_t;
struct htdbm_t {
apr_dbm_t *dbm;
struct passwd_ctx ctx;
#if APR_CHARSET_EBCDIC
apr_xlate_t *to_ascii;
#endif
char *filename;
char *username;
char *comment;
char *type;
int create;
int rdonly;
};
#define HTDBM_MAKE 0
#define HTDBM_DELETE 1
#define HTDBM_VERIFY 2
#define HTDBM_LIST 3
#define HTDBM_NOFILE 4
static 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_pool_abort_set(abort_on_oom, *pool);
apr_file_open_stderr(&errfile, *pool);
apr_signal(SIGINT, (void (*)(int)) htdbm_interrupted);
(*hdbm) = (htdbm_t *)apr_pcalloc(*pool, sizeof(htdbm_t));
(*hdbm)->ctx.pool = *pool;
#if APR_CHARSET_EBCDIC
rv = apr_xlate_open(&((*hdbm)->to_ascii), "ISO-8859-1", APR_DEFAULT_CHARSET, (*hdbm)->ctx.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)->ctx.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->ctx.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->ctx.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->ctx.passwd);
if (!htdbm->comment)
val.dptr = htdbm->ctx.passwd;
else {
val.dptr = apr_pstrcat(htdbm->ctx.pool, htdbm->ctx.passwd, ":",
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;
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->ctx.pool, val.dptr, val.dsize);
cmnt = strchr(rec, ':');
if (cmnt)
pwd = apr_pstrndup(htdbm->ctx.pool, rec, cmnt - rec);
else
pwd = apr_pstrdup(htdbm->ctx.pool, rec);
return apr_password_validate(htdbm->ctx.passwd, pwd);
}
static apr_status_t htdbm_list(htdbm_t *htdbm)
{
apr_status_t rv;
apr_datum_t key, val;
char *cmnt;
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;
}
fprintf(stderr, "Dumping records from database -- %s\n", htdbm->filename);
fprintf(stderr, " %-32s Comment\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;
}
/* Note: we don't store \0-terminators on our dbm data */
fprintf(stderr, " %-32.*s", (int)key.dsize, key.dptr);
cmnt = memchr(val.dptr, ':', val.dsize);
if (cmnt)
fprintf(stderr, " %.*s", (int)(val.dptr+val.dsize - (cmnt+1)), 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 int htdbm_make(htdbm_t *htdbm)
{
char cpw[MAX_STRING_LEN];
int ret;
htdbm->ctx.out = cpw;
htdbm->ctx.out_len = sizeof(cpw);
ret = mkhash(&htdbm->ctx);
if (ret != 0) {
fprintf(stderr, "Error: %s\n", htdbm->ctx.errstr);
return ret;
}
htdbm->ctx.passwd = apr_pstrdup(htdbm->ctx.pool, cpw);
return 0;
}
static apr_status_t htdbm_valid_username(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)
{
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 encryption of the password (default).\n"
" -B Force BCRYPT encryption 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 encryption of the password (8 chars max, insecure).\n"
" -s Force SHA encryption of the password (insecure).\n"
" -p Do not encrypt 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, const char * 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;
const char *opt_arg;
apr_app_initialize(&argc, &argv, NULL);
atexit(terminate);
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. */
}
|