]> granicus.if.org Git - apache/blob - support/htdbm.c
htdbm:
[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_file_open_stderr(&errfile, *pool);
114     apr_signal(SIGINT, (void (*)(int)) htdbm_interrupted);
115
116     (*hdbm) = (htdbm_t *)apr_pcalloc(*pool, sizeof(htdbm_t));
117     (*hdbm)->ctx.pool = *pool;
118
119 #if APR_CHARSET_EBCDIC
120     rv = apr_xlate_open(&((*hdbm)->to_ascii), "ISO-8859-1", APR_DEFAULT_CHARSET, (*hdbm)->ctx.pool);
121     if (rv) {
122         fprintf(stderr, "apr_xlate_open(to ASCII)->%d\n", rv);
123         return APR_EGENERAL;
124     }
125     rv = apr_SHA1InitEBCDIC((*hdbm)->to_ascii);
126     if (rv) {
127         fprintf(stderr, "apr_SHA1InitEBCDIC()->%d\n", rv);
128         return APR_EGENERAL;
129     }
130     rv = apr_MD5InitEBCDIC((*hdbm)->to_ascii);
131     if (rv) {
132         fprintf(stderr, "apr_MD5InitEBCDIC()->%d\n", rv);
133         return APR_EGENERAL;
134     }
135 #endif /*APR_CHARSET_EBCDIC*/
136
137     /* Set MD5 as default */
138     (*hdbm)->ctx.alg = ALG_APMD5;
139     (*hdbm)->type = "default";
140     return APR_SUCCESS;
141 }
142
143 static apr_status_t htdbm_open(htdbm_t *htdbm)
144 {
145     if (htdbm->create)
146         return apr_dbm_open_ex(&htdbm->dbm, htdbm->type, htdbm->filename, APR_DBM_RWCREATE,
147                             APR_OS_DEFAULT, htdbm->ctx.pool);
148     else
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);
152 }
153
154 static apr_status_t htdbm_save(htdbm_t *htdbm, int *changed)
155 {
156     apr_datum_t key, val;
157
158     if (!htdbm->username)
159         return APR_SUCCESS;
160
161     key.dptr = htdbm->username;
162     key.dsize = strlen(htdbm->username);
163     if (apr_dbm_exists(htdbm->dbm, key))
164         *changed = 1;
165
166     val.dsize = strlen(htdbm->ctx.passwd);
167     if (!htdbm->comment)
168         val.dptr  = htdbm->ctx.passwd;
169     else {
170         val.dptr = apr_pstrcat(htdbm->ctx.pool, htdbm->ctx.passwd, ":",
171                                htdbm->comment, NULL);
172         val.dsize += (strlen(htdbm->comment) + 1);
173     }
174     return apr_dbm_store(htdbm->dbm, key, val);
175 }
176
177 static apr_status_t htdbm_del(htdbm_t *htdbm)
178 {
179     apr_datum_t key;
180
181     key.dptr = htdbm->username;
182     key.dsize = strlen(htdbm->username);
183     if (!apr_dbm_exists(htdbm->dbm, key))
184         return APR_ENOENT;
185
186     return apr_dbm_delete(htdbm->dbm, key);
187 }
188
189 static apr_status_t htdbm_verify(htdbm_t *htdbm)
190 {
191     apr_datum_t key, val;
192     char *pwd;
193     char *rec, *cmnt;
194
195     key.dptr = htdbm->username;
196     key.dsize = strlen(htdbm->username);
197     if (!apr_dbm_exists(htdbm->dbm, key))
198         return APR_ENOENT;
199     if (apr_dbm_fetch(htdbm->dbm, key, &val) != APR_SUCCESS)
200         return APR_ENOENT;
201     rec = apr_pstrndup(htdbm->ctx.pool, val.dptr, val.dsize);
202     cmnt = strchr(rec, ':');
203     if (cmnt)
204         pwd = apr_pstrndup(htdbm->ctx.pool, rec, cmnt - rec);
205     else
206         pwd = apr_pstrdup(htdbm->ctx.pool, rec);
207     return apr_password_validate(htdbm->ctx.passwd, pwd);
208 }
209
210 static apr_status_t htdbm_list(htdbm_t *htdbm)
211 {
212     apr_status_t rv;
213     apr_datum_t key, val;
214     char *cmnt;
215     int i = 0;
216
217     rv = apr_dbm_firstkey(htdbm->dbm, &key);
218     if (rv != APR_SUCCESS) {
219         fprintf(stderr, "Empty database -- %s\n", htdbm->filename);
220         return APR_ENOENT;
221     }
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);
228             return APR_EGENERAL;
229         }
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);
233         if (cmnt)
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");
239         ++i;
240     }
241
242     fprintf(stderr, "Total #records : %d\n", i);
243     return APR_SUCCESS;
244 }
245
246 static int htdbm_make(htdbm_t *htdbm)
247 {
248     char cpw[MAX_STRING_LEN];
249     int ret;
250
251     htdbm->ctx.out = cpw;
252     htdbm->ctx.out_len = sizeof(cpw);
253     ret = mkhash(&htdbm->ctx);
254     if (ret != 0) {
255         fprintf(stderr, "Error: %s\n", htdbm->ctx.errstr);
256         return ret;
257     }
258     htdbm->ctx.passwd = apr_pstrdup(htdbm->ctx.pool, cpw);
259     return 0;
260 }
261
262 static apr_status_t htdbm_valid_username(htdbm_t *htdbm)
263 {
264     if (!htdbm->username || (strlen(htdbm->username) > 64) || (strlen(htdbm->username) < 1)) {
265         fprintf(stderr, "Invalid username length\n");
266         return APR_EINVAL;
267     }
268     if (strchr(htdbm->username, ':')) {
269         fprintf(stderr, "Username contains invalid characters\n");
270         return APR_EINVAL;
271     }
272     return APR_SUCCESS;
273 }
274
275 static void htdbm_usage(void)
276 {
277     fprintf(stderr,
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"
287         "Options:\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 "
305         "MD5 algorithm.\n",
306         BCRYPT_DEFAULT_COST);
307     exit(ERR_SYNTAX);
308 }
309
310 int main(int argc, const char * const argv[])
311 {
312     apr_pool_t *pool;
313     apr_status_t rv;
314     char errbuf[MAX_STRING_LEN];
315     int  need_file = 1;
316     int  need_user = 1;
317     int  need_pwd  = 1;
318     int  need_cmnt = 0;
319     int  changed = 0;
320     int  cmd = HTDBM_MAKE;
321     int  i, ret, args_left = 2;
322     apr_getopt_t *state;
323     char opt;
324     const char *opt_arg;
325
326     apr_app_initialize(&argc, &argv, NULL);
327     atexit(terminate);
328
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));
332         exit(1);
333     }
334
335     rv = apr_getopt_init(&state, pool, argc, argv);
336     if (rv != APR_SUCCESS)
337         exit(ERR_SYNTAX);
338
339     while ((rv = apr_getopt(state, "cnmspdBbDivxlC:T:", &opt, &opt_arg)) == APR_SUCCESS) {
340         switch (opt) {
341         case 'c':
342             h->create = 1;
343             break;
344         case 'n':
345             need_file = 0;
346             cmd = HTDBM_NOFILE;
347                 args_left--;
348             break;
349         case 'l':
350             need_pwd = 0;
351             need_user = 0;
352             cmd = HTDBM_LIST;
353             h->rdonly = 1;
354             args_left--;
355             break;
356         case 't':
357             need_cmnt = 1;
358             args_left++;
359             break;
360         case 'T':
361             h->type = apr_pstrdup(h->ctx.pool, opt_arg);
362             break;
363         case 'v':
364             h->rdonly = 1;
365             cmd = HTDBM_VERIFY;
366             break;
367         case 'x':
368             need_pwd = 0;
369             cmd = HTDBM_DELETE;
370             break;
371         default:
372             ret = parse_common_options(&h->ctx, opt, opt_arg);
373             if (ret) {
374                 fprintf(stderr, "Error: %s\n", h->ctx.errstr);
375                 exit(ret);
376             }
377         }
378     }
379     if (h->ctx.passwd_src == PW_ARG) {
380             need_pwd = 0;
381             args_left++;
382     }
383     /*
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
386      * specified).
387      */
388     i = state->ind;
389     if (rv != APR_EOF || argc - i != args_left)
390         htdbm_usage();
391
392     if (need_file) {
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);
398             exit(ERR_FILEPERM);
399         }
400     }
401     if (need_user) {
402         h->username = apr_pstrdup(pool, argv[i++]);
403         if (htdbm_valid_username(h) != APR_SUCCESS)
404             exit(ERR_BADUSER);
405     }
406     if (h->ctx.passwd_src == PW_ARG)
407         h->ctx.passwd = apr_pstrdup(pool, argv[i++]);
408
409     if (need_pwd) {
410         ret = get_password(&h->ctx);
411         if (ret) {
412             fprintf(stderr, "Error: %s\n", h->ctx.errstr);
413             exit(ret);
414         }
415     }
416     if (need_cmnt)
417         h->comment = apr_pstrdup(pool, argv[i++]);
418
419     switch (cmd) {
420         case HTDBM_VERIFY:
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);
424                     exit(ERR_BADUSER);
425                 }
426                 else {
427                     fprintf(stderr, "Password mismatch for user '%s'\n", h->username);
428                     exit(ERR_PWMISMATCH);
429                 }
430             }
431             else
432                 fprintf(stderr, "Password validated for user '%s'\n", h->username);
433             break;
434         case HTDBM_DELETE:
435             if (htdbm_del(h) != APR_SUCCESS) {
436                 fprintf(stderr, "Cannot find user '%s' in database\n", h->username);
437                 exit(ERR_BADUSER);
438             }
439             h->username = NULL;
440             changed = 1;
441             break;
442         case HTDBM_LIST:
443             htdbm_list(h);
444             break;
445         default:
446             ret = htdbm_make(h);
447             if (ret)
448                 exit(ret);
449             break;
450     }
451     if (need_file && !h->rdonly) {
452         if ((rv = htdbm_save(h, &changed)) != APR_SUCCESS) {
453             apr_strerror(rv, errbuf, sizeof(errbuf));
454             exit(ERR_FILEPERM);
455         }
456         fprintf(stdout, "Database %s %s.\n", h->filename,
457                 h->create ? "created" : (changed ? "modified" : "updated"));
458     }
459     if (cmd == HTDBM_NOFILE) {
460         if (!need_cmnt) {
461             fprintf(stderr, "%s:%s\n", h->username, h->ctx.passwd);
462         }
463         else {
464             fprintf(stderr, "%s:%s:%s\n", h->username, h->ctx.passwd,
465                     h->comment);
466         }
467     }
468     htdbm_terminate(h);
469
470     return 0; /* Suppress compiler warning. */
471 }