]> granicus.if.org Git - apache/blob - support/htdbm.c
* Lock the worker, not the balancer. We even do not know if we have a balancer
[apache] / support / htdbm.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
18  * htdbm.c: simple program for manipulating DBM
19  * password databases for the Apache HTTP server
20  *
21  * Contributed by Mladen Turk <mturk mappingsoft.com>
22  * 12 Oct 2001
23  */
24
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"
30 #include "apr_md5.h"
31 #include "apr_sha1.h"
32 #include "apr_dbm.h"
33 #include "apr_getopt.h"
34
35 #if APR_HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 #if APR_HAVE_STRING_H
39 #include <string.h>
40 #endif
41 #if APR_HAVE_STRINGS_H
42 #include <strings.h>
43 #endif
44 #include <time.h>
45
46 #if APR_CHARSET_EBCDIC
47 #include "apr_xlate.h"
48 #endif /*APR_CHARSET_EBCDIC*/
49
50 #if APR_HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
53 #if APR_HAVE_CRYPT_H
54 #include <crypt.h>
55 #endif
56
57
58 typedef struct htdbm_t htdbm_t;
59
60 struct htdbm_t {
61     apr_dbm_t               *dbm;
62     struct passwd_ctx       ctx;
63 #if APR_CHARSET_EBCDIC
64     apr_xlate_t             *to_ascii;
65 #endif
66     char                    *filename;
67     char                    *username;
68     char                    *comment;
69     char                    *type;
70     int                     create;
71     int                     rdonly;
72 };
73
74
75 #define HTDBM_MAKE   0
76 #define HTDBM_DELETE 1
77 #define HTDBM_VERIFY 2
78 #define HTDBM_LIST   3
79 #define HTDBM_NOFILE 4
80
81 static void terminate(void)
82 {
83     apr_terminate();
84 #ifdef NETWARE
85     pressanykey();
86 #endif
87 }
88
89 static void htdbm_terminate(htdbm_t *htdbm)
90 {
91     if (htdbm->dbm)
92         apr_dbm_close(htdbm->dbm);
93     htdbm->dbm = NULL;
94 }
95
96 static htdbm_t *h;
97
98 static void htdbm_interrupted(void)
99 {
100     htdbm_terminate(h);
101     fprintf(stderr, "htdbm Interrupted !\n");
102     exit(ERR_INTERRUPTED);
103 }
104
105 static apr_status_t htdbm_init(apr_pool_t **pool, htdbm_t **hdbm)
106 {
107
108 #if APR_CHARSET_EBCDIC
109     apr_status_t rv;
110 #endif
111
112     apr_pool_create( pool, NULL);
113     apr_pool_abort_set(abort_on_oom, *pool);
114     apr_file_open_stderr(&errfile, *pool);
115     apr_signal(SIGINT, (void (*)(int)) htdbm_interrupted);
116
117     (*hdbm) = (htdbm_t *)apr_pcalloc(*pool, sizeof(htdbm_t));
118     (*hdbm)->ctx.pool = *pool;
119
120 #if APR_CHARSET_EBCDIC
121     rv = apr_xlate_open(&((*hdbm)->to_ascii), "ISO-8859-1", APR_DEFAULT_CHARSET, (*hdbm)->ctx.pool);
122     if (rv) {
123         fprintf(stderr, "apr_xlate_open(to ASCII)->%d\n", rv);
124         return APR_EGENERAL;
125     }
126     rv = apr_SHA1InitEBCDIC((*hdbm)->to_ascii);
127     if (rv) {
128         fprintf(stderr, "apr_SHA1InitEBCDIC()->%d\n", rv);
129         return APR_EGENERAL;
130     }
131     rv = apr_MD5InitEBCDIC((*hdbm)->to_ascii);
132     if (rv) {
133         fprintf(stderr, "apr_MD5InitEBCDIC()->%d\n", rv);
134         return APR_EGENERAL;
135     }
136 #endif /*APR_CHARSET_EBCDIC*/
137
138     /* Set MD5 as default */
139     (*hdbm)->ctx.alg = ALG_APMD5;
140     (*hdbm)->type = "default";
141     return APR_SUCCESS;
142 }
143
144 static apr_status_t htdbm_open(htdbm_t *htdbm)
145 {
146     if (htdbm->create)
147         return apr_dbm_open_ex(&htdbm->dbm, htdbm->type, htdbm->filename, APR_DBM_RWCREATE,
148                             APR_OS_DEFAULT, htdbm->ctx.pool);
149     else
150         return apr_dbm_open_ex(&htdbm->dbm, htdbm->type, htdbm->filename,
151                             htdbm->rdonly ? APR_DBM_READONLY : APR_DBM_READWRITE,
152                             APR_OS_DEFAULT, htdbm->ctx.pool);
153 }
154
155 static apr_status_t htdbm_save(htdbm_t *htdbm, int *changed)
156 {
157     apr_datum_t key, val;
158
159     if (!htdbm->username)
160         return APR_SUCCESS;
161
162     key.dptr = htdbm->username;
163     key.dsize = strlen(htdbm->username);
164     if (apr_dbm_exists(htdbm->dbm, key))
165         *changed = 1;
166
167     val.dsize = strlen(htdbm->ctx.passwd);
168     if (!htdbm->comment)
169         val.dptr  = htdbm->ctx.passwd;
170     else {
171         val.dptr = apr_pstrcat(htdbm->ctx.pool, htdbm->ctx.passwd, ":",
172                                htdbm->comment, NULL);
173         val.dsize += (strlen(htdbm->comment) + 1);
174     }
175     return apr_dbm_store(htdbm->dbm, key, val);
176 }
177
178 static apr_status_t htdbm_del(htdbm_t *htdbm)
179 {
180     apr_datum_t key;
181
182     key.dptr = htdbm->username;
183     key.dsize = strlen(htdbm->username);
184     if (!apr_dbm_exists(htdbm->dbm, key))
185         return APR_ENOENT;
186
187     return apr_dbm_delete(htdbm->dbm, key);
188 }
189
190 static apr_status_t htdbm_verify(htdbm_t *htdbm)
191 {
192     apr_datum_t key, val;
193     char *pwd;
194     char *rec, *cmnt;
195
196     key.dptr = htdbm->username;
197     key.dsize = strlen(htdbm->username);
198     if (!apr_dbm_exists(htdbm->dbm, key))
199         return APR_ENOENT;
200     if (apr_dbm_fetch(htdbm->dbm, key, &val) != APR_SUCCESS)
201         return APR_ENOENT;
202     rec = apr_pstrndup(htdbm->ctx.pool, val.dptr, val.dsize);
203     cmnt = strchr(rec, ':');
204     if (cmnt)
205         pwd = apr_pstrndup(htdbm->ctx.pool, rec, cmnt - rec);
206     else
207         pwd = apr_pstrdup(htdbm->ctx.pool, rec);
208     return apr_password_validate(htdbm->ctx.passwd, pwd);
209 }
210
211 static apr_status_t htdbm_list(htdbm_t *htdbm)
212 {
213     apr_status_t rv;
214     apr_datum_t key, val;
215     char *cmnt;
216     int i = 0;
217
218     rv = apr_dbm_firstkey(htdbm->dbm, &key);
219     if (rv != APR_SUCCESS) {
220         fprintf(stderr, "Empty database -- %s\n", htdbm->filename);
221         return APR_ENOENT;
222     }
223     fprintf(stderr, "Dumping records from database -- %s\n", htdbm->filename);
224     fprintf(stderr, "    %-32s Comment\n", "Username");
225     while (key.dptr != NULL) {
226         rv = apr_dbm_fetch(htdbm->dbm, key, &val);
227         if (rv != APR_SUCCESS) {
228             fprintf(stderr, "Failed getting data from %s\n", htdbm->filename);
229             return APR_EGENERAL;
230         }
231         /* Note: we don't store \0-terminators on our dbm data */
232         fprintf(stderr, "    %-32.*s", (int)key.dsize, key.dptr);
233         cmnt = memchr(val.dptr, ':', val.dsize);
234         if (cmnt)
235             fprintf(stderr, " %.*s", (int)(val.dptr+val.dsize - (cmnt+1)), cmnt + 1);
236         fprintf(stderr, "\n");
237         rv = apr_dbm_nextkey(htdbm->dbm, &key);
238         if (rv != APR_SUCCESS)
239             fprintf(stderr, "Failed getting NextKey\n");
240         ++i;
241     }
242
243     fprintf(stderr, "Total #records : %d\n", i);
244     return APR_SUCCESS;
245 }
246
247 static int htdbm_make(htdbm_t *htdbm)
248 {
249     char cpw[MAX_STRING_LEN];
250     int ret;
251
252     htdbm->ctx.out = cpw;
253     htdbm->ctx.out_len = sizeof(cpw);
254     ret = mkhash(&htdbm->ctx);
255     if (ret != 0) {
256         fprintf(stderr, "Error: %s\n", htdbm->ctx.errstr);
257         return ret;
258     }
259     htdbm->ctx.passwd = apr_pstrdup(htdbm->ctx.pool, cpw);
260     return 0;
261 }
262
263 static apr_status_t htdbm_valid_username(htdbm_t *htdbm)
264 {
265     if (!htdbm->username || (strlen(htdbm->username) > 64) || (strlen(htdbm->username) < 1)) {
266         fprintf(stderr, "Invalid username length\n");
267         return APR_EINVAL;
268     }
269     if (strchr(htdbm->username, ':')) {
270         fprintf(stderr, "Username contains invalid characters\n");
271         return APR_EINVAL;
272     }
273     return APR_SUCCESS;
274 }
275
276 static void htdbm_usage(void)
277 {
278     fprintf(stderr,
279         "htdbm -- program for manipulating DBM password databases.\n\n"
280         "Usage: htdbm   [-cimBdpstvx] [-C cost] [-TDBTYPE] database username\n"
281         "                -b[cmBdptsv] [-C cost] [-TDBTYPE] database username password\n"
282         "                -n[imBdpst]  [-C cost] username\n"
283         "                -nb[mBdpst]  [-C cost] username password\n"
284         "                -v[imBdps]   [-C cost] [-TDBTYPE] database username\n"
285         "                -vb[mBdps]   [-C cost] [-TDBTYPE] database username password\n"
286         "                -x                     [-TDBTYPE] database username\n"
287         "                -l                     [-TDBTYPE] database\n"
288         "Options:\n"
289         "   -c   Create a new database.\n"
290         "   -n   Don't update database; display results on stdout.\n"
291         "   -b   Use the password from the command line rather than prompting for it.\n"
292         "   -i   Read password from stdin without verification (for script usage).\n"
293         "   -m   Force MD5 encryption of the password (default).\n"
294         "   -B   Force BCRYPT encryption of the password (very secure).\n"
295         "   -C   Set the computing time used for the bcrypt algorithm\n"
296         "        (higher is more secure but slower, default: %d, valid: 4 to 31).\n"
297         "   -d   Force CRYPT encryption of the password (8 chars max, insecure).\n"
298         "   -s   Force SHA encryption of the password (insecure).\n"
299         "   -p   Do not encrypt the password (plaintext, insecure).\n"
300         "   -T   DBM Type (SDBM|GDBM|DB|default).\n"
301         "   -l   Display usernames from database on stdout.\n"
302         "   -v   Verify the username/password.\n"
303         "   -x   Remove the username record from database.\n"
304         "   -t   The last param is username comment.\n"
305         "The SHA algorithm does not use a salt and is less secure than the "
306         "MD5 algorithm.\n",
307         BCRYPT_DEFAULT_COST);
308     exit(ERR_SYNTAX);
309 }
310
311 int main(int argc, const char * const argv[])
312 {
313     apr_pool_t *pool;
314     apr_status_t rv;
315     char errbuf[MAX_STRING_LEN];
316     int  need_file = 1;
317     int  need_user = 1;
318     int  need_pwd  = 1;
319     int  need_cmnt = 0;
320     int  changed = 0;
321     int  cmd = HTDBM_MAKE;
322     int  i, ret, args_left = 2;
323     apr_getopt_t *state;
324     char opt;
325     const char *opt_arg;
326
327     apr_app_initialize(&argc, &argv, NULL);
328     atexit(terminate);
329
330     if ((rv = htdbm_init(&pool, &h)) != APR_SUCCESS) {
331         fprintf(stderr, "Unable to initialize htdbm terminating!\n");
332         apr_strerror(rv, errbuf, sizeof(errbuf));
333         exit(1);
334     }
335
336     rv = apr_getopt_init(&state, pool, argc, argv);
337     if (rv != APR_SUCCESS)
338         exit(ERR_SYNTAX);
339
340     while ((rv = apr_getopt(state, "cnmspdBbtivxlC:T:", &opt, &opt_arg)) == APR_SUCCESS) {
341         switch (opt) {
342         case 'c':
343             h->create = 1;
344             break;
345         case 'n':
346             need_file = 0;
347             cmd = HTDBM_NOFILE;
348             args_left--;
349             break;
350         case 'l':
351             need_pwd = 0;
352             need_user = 0;
353             cmd = HTDBM_LIST;
354             h->rdonly = 1;
355             args_left--;
356             break;
357         case 't':
358             need_cmnt = 1;
359             args_left++;
360             break;
361         case 'T':
362             h->type = apr_pstrdup(h->ctx.pool, opt_arg);
363             break;
364         case 'v':
365             h->rdonly = 1;
366             cmd = HTDBM_VERIFY;
367             break;
368         case 'x':
369             need_pwd = 0;
370             cmd = HTDBM_DELETE;
371             break;
372         default:
373             ret = parse_common_options(&h->ctx, opt, opt_arg);
374             if (ret) {
375                 fprintf(stderr, "Error: %s\n", h->ctx.errstr);
376                 exit(ret);
377             }
378         }
379     }
380     if (h->ctx.passwd_src == PW_ARG) {
381             need_pwd = 0;
382             args_left++;
383     }
384     /*
385      * Make sure we still have exactly the right number of arguments left
386      * (the filename, the username, and possibly the password if -b was
387      * specified).
388      */
389     i = state->ind;
390     if (rv != APR_EOF || argc - i != args_left)
391         htdbm_usage();
392
393     if (need_file) {
394         h->filename = apr_pstrdup(h->ctx.pool, argv[i++]);
395         if ((rv = htdbm_open(h)) != APR_SUCCESS) {
396             fprintf(stderr, "Error opening database %s\n", h->filename);
397             apr_strerror(rv, errbuf, sizeof(errbuf));
398             fprintf(stderr,"%s\n",errbuf);
399             exit(ERR_FILEPERM);
400         }
401     }
402     if (need_user) {
403         h->username = apr_pstrdup(pool, argv[i++]);
404         if (htdbm_valid_username(h) != APR_SUCCESS)
405             exit(ERR_BADUSER);
406     }
407     if (h->ctx.passwd_src == PW_ARG)
408         h->ctx.passwd = apr_pstrdup(pool, argv[i++]);
409
410     if (need_pwd) {
411         ret = get_password(&h->ctx);
412         if (ret) {
413             fprintf(stderr, "Error: %s\n", h->ctx.errstr);
414             exit(ret);
415         }
416     }
417     if (need_cmnt)
418         h->comment = apr_pstrdup(pool, argv[i++]);
419
420     switch (cmd) {
421         case HTDBM_VERIFY:
422             if ((rv = htdbm_verify(h)) != APR_SUCCESS) {
423                 if (APR_STATUS_IS_ENOENT(rv)) {
424                     fprintf(stderr, "The user '%s' could not be found in database\n", h->username);
425                     exit(ERR_BADUSER);
426                 }
427                 else {
428                     fprintf(stderr, "Password mismatch for user '%s'\n", h->username);
429                     exit(ERR_PWMISMATCH);
430                 }
431             }
432             else
433                 fprintf(stderr, "Password validated for user '%s'\n", h->username);
434             break;
435         case HTDBM_DELETE:
436             if (htdbm_del(h) != APR_SUCCESS) {
437                 fprintf(stderr, "Cannot find user '%s' in database\n", h->username);
438                 exit(ERR_BADUSER);
439             }
440             h->username = NULL;
441             changed = 1;
442             break;
443         case HTDBM_LIST:
444             htdbm_list(h);
445             break;
446         default:
447             ret = htdbm_make(h);
448             if (ret)
449                 exit(ret);
450             break;
451     }
452     if (need_file && !h->rdonly) {
453         if ((rv = htdbm_save(h, &changed)) != APR_SUCCESS) {
454             apr_strerror(rv, errbuf, sizeof(errbuf));
455             exit(ERR_FILEPERM);
456         }
457         fprintf(stdout, "Database %s %s.\n", h->filename,
458                 h->create ? "created" : (changed ? "modified" : "updated"));
459     }
460     if (cmd == HTDBM_NOFILE) {
461         if (!need_cmnt) {
462             fprintf(stderr, "%s:%s\n", h->username, h->ctx.passwd);
463         }
464         else {
465             fprintf(stderr, "%s:%s:%s\n", h->username, h->ctx.passwd,
466                     h->comment);
467         }
468     }
469     htdbm_terminate(h);
470
471     return 0; /* Suppress compiler warning. */
472 }