You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by rj...@apache.org on 2012/12/11 11:37:28 UTC

svn commit: r1420084 - in /httpd/httpd/branches/2.4.x: ./ CHANGES STATUS support/Makefile.in support/NWGNUhtdbm support/NWGNUhtpasswd support/htdbm.c support/htdbm.dsp support/htpasswd.c support/htpasswd.dsp support/passwd_common.c support/passwd_common.h

Author: rjung
Date: Tue Dec 11 10:37:25 2012
New Revision: 1420084

URL: http://svn.apache.org/viewvc?rev=1420084&view=rev
Log:
Start refactoring of htpasswd and htdbm
- Move many common code parts into separate source file. This adds some
  of htpasswd's recent improvements to htdbm.
- Rework salt generation to use the full 48bit of entropy for MD5
  Previously, it would only generate 2^32 different salts on a given
  platform.
- Use apr_getopt().
Submitted by: sf
Backport of r1395253 from trunk.

Add passwd_common.c build support for Windows and Netware
Submitted by: sf
Backport of r1395254 from trunk.

Add support for bcrypt (requires apr-util 1.5 or higher).
PR: 49288
Submitted by: sf
Backport of r1395255 from trunk.

Optionally read passwords from stdin as more secure alternative to -b.
PR: 40243
Submitted by: Adomas Paltanavicius <adomas paltanavicius gmail com>, sf
Backport of r1395256 from trunk.
Submitted by: Adomas Paltanavicius <adomas paltanavicius gmail com>, sf

Removed __func__ usage which is c99.
Submitted by: sf
Backport of r1395792 from trunk.

All of the above backported by: sf
All of the above reviewed by: sf, gsmith, rjung

fix unresolved external symbol __imp__UuidCreate error
Submitted by: gsmith
Backported by: gsmith
Reviewed by: gsmith, rjung
Backport of r1419084 from trunk.

Added:
    httpd/httpd/branches/2.4.x/support/passwd_common.c
      - copied, changed from r1395253, httpd/httpd/trunk/support/passwd_common.c
    httpd/httpd/branches/2.4.x/support/passwd_common.h
      - copied, changed from r1395253, httpd/httpd/trunk/support/passwd_common.h
Modified:
    httpd/httpd/branches/2.4.x/   (props changed)
    httpd/httpd/branches/2.4.x/CHANGES
    httpd/httpd/branches/2.4.x/STATUS
    httpd/httpd/branches/2.4.x/support/Makefile.in
    httpd/httpd/branches/2.4.x/support/NWGNUhtdbm
    httpd/httpd/branches/2.4.x/support/NWGNUhtpasswd
    httpd/httpd/branches/2.4.x/support/htdbm.c
    httpd/httpd/branches/2.4.x/support/htdbm.dsp
    httpd/httpd/branches/2.4.x/support/htpasswd.c
    httpd/httpd/branches/2.4.x/support/htpasswd.dsp

Propchange: httpd/httpd/branches/2.4.x/
------------------------------------------------------------------------------
  Merged /httpd/httpd/trunk:r1395253-1395256,1395792,1419084

Modified: httpd/httpd/branches/2.4.x/CHANGES
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/CHANGES?rev=1420084&r1=1420083&r2=1420084&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/CHANGES [utf-8] (original)
+++ httpd/httpd/branches/2.4.x/CHANGES [utf-8] Tue Dec 11 10:37:25 2012
@@ -2,6 +2,17 @@
 
 Changes with Apache 2.4.4
 
+  *) htpasswd, htdbm: Optionally read passwords from stdin, as more
+     secure alternative to -b.  PR 40243. [Adomas Paltanavicius <adomas
+     paltanavicius gmail com>, Stefan Fritsch]
+
+  *) htpasswd, htdbm: Add support for bcrypt algorithm (requires
+     apr-util 1.5 or higher). PR 49288. [Stefan Fritsch]
+
+  *) htpasswd, htdbm: Put full 48bit of entropy into salt, improve
+     error handling. Add some of htpasswd's improvements to htdbm,
+     e.g. warn if password is truncated by crypt(). [Stefan Fritsch]
+
   *) mod_auth_form: Support the expr parser in the
      AuthFormLoginRequiredLocation, AuthFormLoginSuccessLocation and
      AuthFormLogoutLocation directives. [Graham Leggett]

Modified: httpd/httpd/branches/2.4.x/STATUS
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/STATUS?rev=1420084&r1=1420083&r2=1420084&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/STATUS (original)
+++ httpd/httpd/branches/2.4.x/STATUS Tue Dec 11 10:37:25 2012
@@ -98,17 +98,6 @@ PATCHES ACCEPTED TO BACKPORT FROM TRUNK:
      +1: jim, druggeri, rjung
      druggeri note: Docs notes on new elements would be nice for quick reference
 
-   * htpasswd/htdbm: Refactor, add bcrypt support, add -i option.
-     trunk patch: http://svn.apache.org/viewvc?view=revision&revision=1395253
-                  http://svn.apache.org/viewvc?view=revision&revision=1395254
-                  http://svn.apache.org/viewvc?view=revision&revision=1395255
-                  http://svn.apache.org/viewvc?view=revision&revision=1395256
-                  http://svn.apache.org/viewvc?view=revision&revision=1395792
-     2.4.x patch: trunk patches work
-     +1: sf
-     +1 gsmith, rjung: provided you also include this fix for Windows
-                http://svn.apache.org/viewvc?view=revision&revision=1419084
-
 
 PATCHES PROPOSED TO BACKPORT FROM TRUNK:
   [ New proposals should be added at the end of the list ]

Modified: httpd/httpd/branches/2.4.x/support/Makefile.in
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/support/Makefile.in?rev=1420084&r1=1420083&r2=1420084&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/support/Makefile.in (original)
+++ httpd/httpd/branches/2.4.x/support/Makefile.in Tue Dec 11 10:37:25 2012
@@ -36,7 +36,9 @@ install:
 	    fi ; \
 	fi
 
-htpasswd_OBJECTS = htpasswd.lo
+htpasswd.lo: passwd_common.h
+passwd_common.lo: passwd_common.h
+htpasswd_OBJECTS = htpasswd.lo passwd_common.lo
 htpasswd: $(htpasswd_OBJECTS)
 	$(LINK) $(htpasswd_LTFLAGS) $(htpasswd_OBJECTS) $(PROGRAM_LDADD) $(CRYPT_LIBS)
 
@@ -52,7 +54,8 @@ logresolve_OBJECTS = logresolve.lo
 logresolve: $(logresolve_OBJECTS)
 	$(LINK) $(logresolve_LTFLAGS) $(logresolve_OBJECTS) $(PROGRAM_LDADD)
 
-htdbm_OBJECTS = htdbm.lo
+htdbm.lo: passwd_common.h
+htdbm_OBJECTS = htdbm.lo passwd_common.lo
 htdbm: $(htdbm_OBJECTS)
 	$(LINK) $(htdbm_LTFLAGS) $(htdbm_OBJECTS) $(PROGRAM_LDADD) $(CRYPT_LIBS)
 

Modified: httpd/httpd/branches/2.4.x/support/NWGNUhtdbm
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/support/NWGNUhtdbm?rev=1420084&r1=1420083&r2=1420084&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/support/NWGNUhtdbm (original)
+++ httpd/httpd/branches/2.4.x/support/NWGNUhtdbm Tue Dec 11 10:37:25 2012
@@ -169,6 +169,7 @@ TARGET_lib = \
 #
 FILES_nlm_objs = \
 	$(OBJDIR)/htdbm.o \
+	$(OBJDIR)/passwd_common.o \
 	$(EOLIST)
 
 #

Modified: httpd/httpd/branches/2.4.x/support/NWGNUhtpasswd
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/support/NWGNUhtpasswd?rev=1420084&r1=1420083&r2=1420084&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/support/NWGNUhtpasswd (original)
+++ httpd/httpd/branches/2.4.x/support/NWGNUhtpasswd Tue Dec 11 10:37:25 2012
@@ -169,6 +169,7 @@ TARGET_lib = \
 #
 FILES_nlm_objs = \
 	$(OBJDIR)/htpasswd.o \
+	$(OBJDIR)/passwd_common.o \
 	$(EOLIST)
 
 #

Modified: httpd/httpd/branches/2.4.x/support/htdbm.c
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/support/htdbm.c?rev=1420084&r1=1420083&r2=1420084&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/support/htdbm.c (original)
+++ httpd/httpd/branches/2.4.x/support/htdbm.c Tue Dec 11 10:37:25 2012
@@ -22,9 +22,7 @@
  * 12 Oct 2001
  */
 
-#include "apr.h"
-#include "apr_lib.h"
-#include "apr_strings.h"
+#include "passwd_common.h"
 #include "apr_file_io.h"
 #include "apr_file_info.h"
 #include "apr_pools.h"
@@ -32,6 +30,7 @@
 #include "apr_md5.h"
 #include "apr_sha1.h"
 #include "apr_dbm.h"
+#include "apr_getopt.h"
 
 #if APR_HAVE_STDLIB_H
 #include <stdlib.h>
@@ -56,49 +55,20 @@
 #endif
 
 
-#if !APR_CHARSET_EBCDIC
-#define LF 10
-#define CR 13
-#else /*APR_CHARSET_EBCDIC*/
-#define LF '\n'
-#define CR '\r'
-#endif /*APR_CHARSET_EBCDIC*/
-
-#define MAX_STRING_LEN 256
-#define ALG_PLAIN 0
-#define ALG_APMD5 1
-#define ALG_APSHA 2
-
-#if (!(defined(WIN32) || defined(NETWARE)))
-#define ALG_CRYPT 3
-#endif
-
-
-#define ERR_FILEPERM    1
-#define ERR_SYNTAX      2
-#define ERR_PWMISMATCH  3
-#define ERR_INTERRUPTED 4
-#define ERR_OVERFLOW    5
-#define ERR_BADUSER     6
-#define ERR_EMPTY       7
-
-
 typedef struct htdbm_t htdbm_t;
 
 struct htdbm_t {
     apr_dbm_t               *dbm;
-    apr_pool_t              *pool;
+    struct passwd_ctx       ctx;
 #if APR_CHARSET_EBCDIC
     apr_xlate_t             *to_ascii;
 #endif
     char                    *filename;
     char                    *username;
-    char                    *userpass;
     char                    *comment;
     char                    *type;
     int                     create;
     int                     rdonly;
-    int                     alg;
 };
 
 
@@ -107,7 +77,6 @@ struct htdbm_t {
 #define HTDBM_VERIFY 2
 #define HTDBM_LIST   3
 #define HTDBM_NOFILE 4
-#define HTDBM_STDIN  5
 
 static void terminate(void)
 {
@@ -141,13 +110,14 @@ static apr_status_t htdbm_init(apr_pool_
 #endif
 
     apr_pool_create( pool, NULL);
+    apr_file_open_stderr(&errfile, *pool);
     apr_signal(SIGINT, (void (*)(int)) htdbm_interrupted);
 
     (*hdbm) = (htdbm_t *)apr_pcalloc(*pool, sizeof(htdbm_t));
-    (*hdbm)->pool = *pool;
+    (*hdbm)->ctx.pool = *pool;
 
 #if APR_CHARSET_EBCDIC
-    rv = apr_xlate_open(&((*hdbm)->to_ascii), "ISO-8859-1", APR_DEFAULT_CHARSET, (*hdbm)->pool);
+    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;
@@ -165,7 +135,7 @@ static apr_status_t htdbm_init(apr_pool_
 #endif /*APR_CHARSET_EBCDIC*/
 
     /* Set MD5 as default */
-    (*hdbm)->alg = ALG_APMD5;
+    (*hdbm)->ctx.alg = ALG_APMD5;
     (*hdbm)->type = "default";
     return APR_SUCCESS;
 }
@@ -174,11 +144,11 @@ static apr_status_t htdbm_open(htdbm_t *
 {
     if (htdbm->create)
         return apr_dbm_open_ex(&htdbm->dbm, htdbm->type, htdbm->filename, APR_DBM_RWCREATE,
-                            APR_OS_DEFAULT, htdbm->pool);
+                            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->pool);
+                            APR_OS_DEFAULT, htdbm->ctx.pool);
 }
 
 static apr_status_t htdbm_save(htdbm_t *htdbm, int *changed)
@@ -193,11 +163,11 @@ static apr_status_t htdbm_save(htdbm_t *
     if (apr_dbm_exists(htdbm->dbm, key))
         *changed = 1;
 
-    val.dsize = strlen(htdbm->userpass);
+    val.dsize = strlen(htdbm->ctx.passwd);
     if (!htdbm->comment)
-        val.dptr  = htdbm->userpass;
+        val.dptr  = htdbm->ctx.passwd;
     else {
-        val.dptr = apr_pstrcat(htdbm->pool, htdbm->userpass, ":",
+        val.dptr = apr_pstrcat(htdbm->ctx.pool, htdbm->ctx.passwd, ":",
                                htdbm->comment, NULL);
         val.dsize += (strlen(htdbm->comment) + 1);
     }
@@ -228,13 +198,13 @@ static apr_status_t htdbm_verify(htdbm_t
         return APR_ENOENT;
     if (apr_dbm_fetch(htdbm->dbm, key, &val) != APR_SUCCESS)
         return APR_ENOENT;
-    rec = apr_pstrndup(htdbm->pool, val.dptr, val.dsize);
+    rec = apr_pstrndup(htdbm->ctx.pool, val.dptr, val.dsize);
     cmnt = strchr(rec, ':');
     if (cmnt)
-        pwd = apr_pstrndup(htdbm->pool, rec, cmnt - rec);
+        pwd = apr_pstrndup(htdbm->ctx.pool, rec, cmnt - rec);
     else
-        pwd = apr_pstrdup(htdbm->pool, rec);
-    return apr_password_validate(htdbm->userpass, pwd);
+        pwd = apr_pstrdup(htdbm->ctx.pool, rec);
+    return apr_password_validate(htdbm->ctx.passwd, pwd);
 }
 
 static apr_status_t htdbm_list(htdbm_t *htdbm)
@@ -273,67 +243,20 @@ static apr_status_t htdbm_list(htdbm_t *
     return APR_SUCCESS;
 }
 
-static void to64(char *s, unsigned long v, int n)
-{
-    static unsigned char itoa64[] =         /* 0 ... 63 => ASCII - 64 */
-    "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-
-    while (--n >= 0) {
-        *s++ = itoa64[v&0x3f];
-        v >>= 6;
-    }
-}
-
-static apr_status_t htdbm_make(htdbm_t *htdbm)
+static int htdbm_make(htdbm_t *htdbm)
 {
     char cpw[MAX_STRING_LEN];
-    char salt[9];
-#if (!(defined(WIN32) || defined(NETWARE)))
-    char *cbuf;
-#endif
+    int ret;
 
-    switch (htdbm->alg) {
-        case ALG_APSHA:
-            /* XXX cpw >= 28 + strlen(sha1) chars - fixed len SHA */
-            apr_sha1_base64(htdbm->userpass,strlen(htdbm->userpass),cpw);
-        break;
-
-        case ALG_APMD5:
-            (void) srand((int) time((time_t *) NULL));
-            to64(&salt[0], rand(), 8);
-            salt[8] = '\0';
-            apr_md5_encode((const char *)htdbm->userpass, (const char *)salt,
-                            cpw, sizeof(cpw));
-        break;
-        case ALG_PLAIN:
-            /* XXX this len limitation is not in sync with any HTTPd len. */
-            apr_cpystrn(cpw,htdbm->userpass,sizeof(cpw));
-#if (!(defined(WIN32) || defined(NETWARE)))
-            fprintf(stderr, "Warning: Plain text passwords aren't supported by the "
-                    "server on this platform!\n");
-#endif
-        break;
-#if (!(defined(WIN32) || defined(NETWARE)))
-        case ALG_CRYPT:
-            (void) srand((int) time((time_t *) NULL));
-            to64(&salt[0], rand(), 8);
-            salt[8] = '\0';
-            cbuf = crypt(htdbm->userpass, salt);
-            if (cbuf == NULL) {
-                char errbuf[128];
-                
-                fprintf(stderr, "crypt() failed: %s\n", 
-                        apr_strerror(errno, errbuf, sizeof errbuf));
-                exit(ERR_PWMISMATCH);
-            }
-            apr_cpystrn(cpw, cbuf, sizeof(cpw) - 1);
-            fprintf(stderr, "CRYPT is now deprecated, use MD5 instead!\n");
-#endif
-        default:
-        break;
+    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->userpass = apr_pstrdup(htdbm->pool, cpw);
-    return APR_SUCCESS;
+    htdbm->ctx.passwd = apr_pstrdup(htdbm->ctx.pool, cpw);
+    return 0;
 }
 
 static apr_status_t htdbm_valid_username(htdbm_t *htdbm)
@@ -351,60 +274,54 @@ static apr_status_t htdbm_valid_username
 
 static void htdbm_usage(void)
 {
-
-#if (!(defined(WIN32) || defined(NETWARE)))
-#define CRYPT_OPTION "d"
-#else
-#define CRYPT_OPTION ""
-#endif
-    fprintf(stderr, "htdbm -- program for manipulating DBM password databases.\n\n");
-    fprintf(stderr, "Usage: htdbm    [-cm"CRYPT_OPTION"pstvx] [-TDBTYPE] database username\n");
-    fprintf(stderr, "                -b[cm"CRYPT_OPTION"ptsv] [-TDBTYPE] database username password\n");
-    fprintf(stderr, "                -n[m"CRYPT_OPTION"pst]   username\n");
-    fprintf(stderr, "                -nb[m"CRYPT_OPTION"pst]  username password\n");
-    fprintf(stderr, "                -v[m"CRYPT_OPTION"ps]    [-TDBTYPE] database username\n");
-    fprintf(stderr, "                -vb[m"CRYPT_OPTION"ps]   [-TDBTYPE] database username password\n");
-    fprintf(stderr, "                -x[m"CRYPT_OPTION"ps]    [-TDBTYPE] database username\n");
-    fprintf(stderr, "                -l                       [-TDBTYPE] database\n");
-    fprintf(stderr, "Options:\n");
-    fprintf(stderr, "   -b   Use the password from the command line rather "
-                    "than prompting for it.\n");
-    fprintf(stderr, "   -c   Create a new database.\n");
-    fprintf(stderr, "   -n   Don't update database; display results on stdout.\n");
-    fprintf(stderr, "   -m   Force MD5 encryption of the password (default).\n");
-#if (!(defined(WIN32) || defined(NETWARE)))
-    fprintf(stderr, "   -d   Force CRYPT encryption of the password (now deprecated).\n");
-#endif
-    fprintf(stderr, "   -p   Do not encrypt the password (plaintext).\n");
-    fprintf(stderr, "   -s   Force SHA encryption of the password.\n");
-    fprintf(stderr, "   -T   DBM Type (SDBM|GDBM|DB|default).\n");
-    fprintf(stderr, "   -l   Display usernames from database on stdout.\n");
-    fprintf(stderr, "   -t   The last param is username comment.\n");
-    fprintf(stderr, "   -v   Verify the username/password.\n");
-    fprintf(stderr, "   -x   Remove the username record from database.\n");
+    fprintf(stderr,
+        "htdbm -- program for manipulating DBM password databases.\n\n"
+        "Usage: htdbm   [-cimBdpstvx] [-Ccost] [-TDBTYPE] database username\n"
+        "                -b[cmBdptsv] [-Ccost] [-TDBTYPE] database username password\n"
+        "                -n[imBdpst]  [-Ccost] username\n"
+        "                -nb[mBdpst]  [-Ccost] username password\n"
+        "                -v[imBdps]   [-Ccost] [-TDBTYPE] database username\n"
+        "                -vb[mBdps]   [-Ccost] [-TDBTYPE] database username password\n"
+        "                -x           [-Ccost] [-TDBTYPE] database username\n"
+        "                -l           [-Ccost] [-TDBTYPE] database\n"
+        "Options:\n"
+        "   -b   Use the password from the command line rather than prompting for it.\n"
+        "   -c   Create a new database.\n"
+        "   -n   Don't update database; display results on stdout.\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"
+        "   -d   Force CRYPT encryption of the password (8 chars max, insecure).\n"
+        "   -p   Do not encrypt the password (plaintext).\n"
+        "   -s   Force SHA encryption of the password (insecure).\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"
+        "   -T   DBM Type (SDBM|GDBM|DB|default).\n"
+        "   -l   Display usernames from database on stdout.\n"
+        "   -t   The last param is username comment.\n"
+        "   -v   Verify the username/password.\n"
+        "   -x   Remove the username record from database.\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;
-    apr_size_t l;
-    char pwi[MAX_STRING_LEN];
-    char pwc[MAX_STRING_LEN];
     char errbuf[MAX_STRING_LEN];
-    const char *arg;
     int  need_file = 1;
     int  need_user = 1;
     int  need_pwd  = 1;
     int  need_cmnt = 0;
-    int  pwd_supplied = 0;
     int  changed = 0;
     int  cmd = HTDBM_MAKE;
-    int  i;
-    int args_left = 2;
+    int  i, ret, args_left = 2;
+    apr_getopt_t *state;
+    char opt;
+    const char *opt_arg;
 
     apr_app_initialize(&argc, &argv, NULL);
     atexit(terminate);
@@ -414,131 +331,90 @@ int main(int argc, const char * const ar
         apr_strerror(rv, errbuf, sizeof(errbuf));
         exit(1);
     }
-    /*
-     * Preliminary check to make sure they provided at least
-     * three arguments, we'll do better argument checking as
-     * we parse the command line.
-     */
-    if (argc < 3)
-       htdbm_usage();
-    /*
-     * Go through the argument list and pick out any options.  They
-     * have to precede any other arguments.
-     */
-    for (i = 1; i < argc; i++) {
-        arg = argv[i];
-        if (*arg != '-')
-            break;
-
-        while (*++arg != '\0') {
-            switch (*arg) {
-            case 'b':
-                pwd_supplied = 1;
-                need_pwd = 0;
-                args_left++;
-                break;
-            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;
+
+    rv = apr_getopt_init(&state, pool, argc, argv);
+    if (rv != APR_SUCCESS)
+        exit(ERR_SYNTAX);
+
+    while ((rv = apr_getopt(state, "cnmspdBbDiC: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 't':
-                need_cmnt = 1;
-                args_left++;
-                break;
-            case 'T':
-                h->type = apr_pstrdup(h->pool, ++arg);
-                while (*arg != '\0')
-                    ++arg;
-                --arg; /* so incrementing this in the loop with find a null */
-                break;
-            case 'v':
-                h->rdonly = 1;
-                cmd = HTDBM_VERIFY;
-                break;
-            case 'x':
-                need_pwd = 0;
-                cmd = HTDBM_DELETE;
-                break;
-            case 'm':
-                h->alg = ALG_APMD5;
-                break;
-            case 'p':
-                h->alg = ALG_PLAIN;
-                break;
-            case 's':
-                h->alg = ALG_APSHA;
-                break;
-#if (!(defined(WIN32) || defined(NETWARE)))
-            case 'd':
-                h->alg = ALG_CRYPT;
-                break;
-#endif
-            default:
-                htdbm_usage();
-                break;
+            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).
      */
-    if ((argc - i) != args_left)
+    i = state->ind;
+    if (rv != APR_EOF || argc - i != args_left)
         htdbm_usage();
 
-    if (!need_file)
-        i--;
-    else {
-        h->filename = apr_pstrdup(h->pool, argv[i]);
-            if ((rv = htdbm_open(h)) != APR_SUCCESS) {
-            fprintf(stderr, "Error opening database %s\n", argv[i]);
+    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+1]);
+        h->username = apr_pstrdup(pool, argv[i++]);
         if (htdbm_valid_username(h) != APR_SUCCESS)
             exit(ERR_BADUSER);
     }
-    if (pwd_supplied)
-        h->userpass = apr_pstrdup(pool, argv[i+2]);
+    if (h->ctx.passwd_src == PW_ARG)
+        h->ctx.passwd = apr_pstrdup(pool, argv[i++]);
 
     if (need_pwd) {
-        l = sizeof(pwc);
-        if (apr_password_get("Enter password        : ", pwi, &l) != APR_SUCCESS) {
-            fprintf(stderr, "Password too long\n");
-            exit(ERR_OVERFLOW);
-        }
-        l = sizeof(pwc);
-        if (apr_password_get("Re-type password      : ", pwc, &l) != APR_SUCCESS) {
-            fprintf(stderr, "Password too long\n");
-            exit(ERR_OVERFLOW);
-        }
-        if (strcmp(pwi, pwc) != 0) {
-            fprintf(stderr, "Password verification error\n");
-            exit(ERR_PWMISMATCH);
+        ret = get_password(&h->ctx);
+        if (ret) {
+            fprintf(stderr, "Error: %s\n", h->ctx.errstr);
+            exit(ret);
         }
-
-        h->userpass = apr_pstrdup(pool,  pwi);
     }
-    if (need_cmnt && pwd_supplied)
-        h->comment = apr_pstrdup(pool, argv[i+3]);
-    else if (need_cmnt)
-        h->comment = apr_pstrdup(pool, argv[i+2]);
+    if (need_cmnt)
+        h->comment = apr_pstrdup(pool, argv[i++]);
 
     switch (cmd) {
         case HTDBM_VERIFY:
@@ -567,9 +443,10 @@ int main(int argc, const char * const ar
             htdbm_list(h);
             break;
         default:
-            htdbm_make(h);
+            ret = htdbm_make(h);
+            if (ret)
+                exit(ret);
             break;
-
     }
     if (need_file && !h->rdonly) {
         if ((rv = htdbm_save(h, &changed)) != APR_SUCCESS) {
@@ -581,10 +458,10 @@ int main(int argc, const char * const ar
     }
     if (cmd == HTDBM_NOFILE) {
         if (!need_cmnt) {
-            fprintf(stderr, "%s:%s\n", h->username, h->userpass);
+            fprintf(stderr, "%s:%s\n", h->username, h->ctx.passwd);
         }
         else {
-            fprintf(stderr, "%s:%s:%s\n", h->username, h->userpass,
+            fprintf(stderr, "%s:%s:%s\n", h->username, h->ctx.passwd,
                     h->comment);
         }
     }

Modified: httpd/httpd/branches/2.4.x/support/htdbm.dsp
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/support/htdbm.dsp?rev=1420084&r1=1420083&r2=1420084&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/support/htdbm.dsp (original)
+++ httpd/httpd/branches/2.4.x/support/htdbm.dsp Tue Dec 11 10:37:25 2012
@@ -49,8 +49,8 @@ BSC32=bscmake.exe
 # ADD BASE BSC32 /nologo
 # ADD BSC32 /nologo
 LINK32=link.exe
-# ADD BASE LINK32 kernel32.lib advapi32.lib wsock32.lib ws2_32.lib shell32.lib /nologo /subsystem:console
-# ADD LINK32 kernel32.lib advapi32.lib wsock32.lib ws2_32.lib shell32.lib /nologo /subsystem:console /debug /opt:ref
+# ADD BASE LINK32 kernel32.lib advapi32.lib wsock32.lib rpcrt4.lib ws2_32.lib shell32.lib /nologo /subsystem:console
+# ADD LINK32 kernel32.lib advapi32.lib wsock32.lib ws2_32.lib rpcrt4.lib shell32.lib /nologo /subsystem:console /debug /opt:ref
 # Begin Special Build Tool
 TargetPath=.\Release\htdbm.exe
 SOURCE="$(InputPath)"
@@ -79,8 +79,8 @@ BSC32=bscmake.exe
 # ADD BASE BSC32 /nologo
 # ADD BSC32 /nologo
 LINK32=link.exe
-# ADD BASE LINK32 kernel32.lib advapi32.lib wsock32.lib ws2_32.lib shell32.lib /nologo /subsystem:console /incremental:no /debug
-# ADD LINK32 kernel32.lib advapi32.lib wsock32.lib ws2_32.lib shell32.lib /nologo /subsystem:console /incremental:no /debug
+# ADD BASE LINK32 kernel32.lib advapi32.lib wsock32.lib ws2_32.lib rpcrt4.lib shell32.lib /nologo /subsystem:console /incremental:no /debug
+# ADD LINK32 kernel32.lib advapi32.lib wsock32.lib ws2_32.lib rpcrt4.lib shell32.lib /nologo /subsystem:console /incremental:no /debug
 # Begin Special Build Tool
 TargetPath=.\Debug\htdbm.exe
 SOURCE="$(InputPath)"
@@ -100,6 +100,10 @@ SOURCE=.\htdbm.c
 # End Source File
 # Begin Source File
 
+SOURCE=.\passwd_common.c
+# End Source File
+# Begin Source File
+
 SOURCE=..\build\win32\httpd.rc
 # End Source File
 # End Target

Modified: httpd/httpd/branches/2.4.x/support/htpasswd.c
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/support/htpasswd.c?rev=1420084&r1=1420083&r2=1420084&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/support/htpasswd.c (original)
+++ httpd/httpd/branches/2.4.x/support/htpasswd.c Tue Dec 11 10:37:25 2012
@@ -38,13 +38,9 @@
  *  7: Failure; file is not a valid htpasswd file
  */
 
-#include "apr.h"
-#include "apr_lib.h"
-#include "apr_strings.h"
-#include "apr_errno.h"
-#include "apr_file_io.h"
-#include "apr_general.h"
+#include "passwd_common.h"
 #include "apr_signal.h"
+#include "apr_getopt.h"
 
 #if APR_HAVE_STDIO_H
 #include <stdio.h>
@@ -52,11 +48,7 @@
 
 #include "apr_md5.h"
 #include "apr_sha1.h"
-#include <time.h>
 
-#if APR_HAVE_CRYPT_H
-#include <crypt.h>
-#endif
 #if APR_HAVE_STDLIB_H
 #include <stdlib.h>
 #endif
@@ -72,230 +64,59 @@
 #define unlink _unlink
 #endif
 
-#if !APR_CHARSET_EBCDIC
-#define LF 10
-#define CR 13
-#else /*APR_CHARSET_EBCDIC*/
-#define LF '\n'
-#define CR '\r'
-#endif /*APR_CHARSET_EBCDIC*/
-
-#define MAX_STRING_LEN 256
-#define ALG_PLAIN 0
-#define ALG_CRYPT 1
-#define ALG_APMD5 2
-#define ALG_APSHA 3
-
-#define ERR_FILEPERM 1
-#define ERR_SYNTAX 2
-#define ERR_PWMISMATCH 3
-#define ERR_INTERRUPTED 4
-#define ERR_OVERFLOW 5
-#define ERR_BADUSER 6
-#define ERR_INVALID 7
-
 #define APHTP_NEWFILE        1
 #define APHTP_NOFILE         2
-#define APHTP_NONINTERACTIVE 4
-#define APHTP_DELUSER        8
+#define APHTP_DELUSER        4
 
-apr_file_t *errfile;
 apr_file_t *ftemp = NULL;
 
-#define NL APR_EOL_STR
-
-#if defined(WIN32) || defined(NETWARE)
-#define CRYPT_ALGO_SUPPORTED 0
-#else
-#define CRYPT_ALGO_SUPPORTED 1
-#endif
-
-#if CRYPT_ALGO_SUPPORTED
-static void to64(char *s, unsigned long v, int n)
-{
-    static unsigned char itoa64[] =         /* 0 ... 63 => ASCII - 64 */
-        "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-
-    while (--n >= 0) {
-        *s++ = itoa64[v&0x3f];
-        v >>= 6;
-    }
-}
-#endif
-
-static void generate_salt(char *s, size_t size)
-{
-    static unsigned char tbl[] =
-        "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-    size_t i;
-    for (i = 0; i < size; ++i) {
-        int idx = (int) (64.0 * rand() / (RAND_MAX + 1.0));
-        s[i] = tbl[idx];
-    }
-}
-
-static apr_status_t seed_rand(void)
+static int mkrecord(struct passwd_ctx *ctx, char *user)
 {
-    int seed = 0;
-    apr_status_t rv;
-    rv = apr_generate_random_bytes((unsigned char*) &seed, sizeof(seed));
-    if (rv) {
-        apr_file_printf(errfile, "Unable to generate random bytes: %pm" NL, &rv);
-        return rv;
-    }
-    srand(seed);
-    return rv;
-}
-
-static void putline(apr_file_t *f, const char *l)
-{
-    apr_status_t rc;
-    rc = apr_file_puts(l, f);
-    if (rc != APR_SUCCESS) {
-        char errstr[MAX_STRING_LEN];
-        apr_strerror(rc, errstr, MAX_STRING_LEN);
-        apr_file_printf(errfile, "Error writing temp file: %s" NL, errstr);
-        apr_file_close(f);
-        exit(ERR_FILEPERM);
-    }
-}
-
-/*
- * Make a password record from the given information.  A zero return
- * indicates success; failure means that the output buffer contains an
- * error message instead.
- */
-static int mkrecord(char *user, char *record, apr_size_t rlen, char *passwd,
-                    int alg)
-{
-    char *pw;
-    char cpw[120];
-    char pwin[MAX_STRING_LEN];
-    char pwv[MAX_STRING_LEN];
-    char salt[9];
-    apr_size_t bufsize;
-#if CRYPT_ALGO_SUPPORTED
-    char *cbuf;
-#endif
-
-    if (passwd != NULL) {
-        pw = passwd;
-    }
-    else {
-        bufsize = sizeof(pwin);
-        if (apr_password_get("New password: ", pwin, &bufsize) != 0) {
-            apr_snprintf(record, (rlen - 1), "password too long (>%"
-                         APR_SIZE_T_FMT ")", sizeof(pwin) - 1);
-            return ERR_OVERFLOW;
-        }
-        bufsize = sizeof(pwv);
-        apr_password_get("Re-type new password: ", pwv, &bufsize);
-        if (strcmp(pwin, pwv) != 0) {
-            apr_cpystrn(record, "password verification error", (rlen - 1));
-            return ERR_PWMISMATCH;
-        }
-        pw = pwin;
-        memset(pwv, '\0', sizeof(pwin));
-    }
-    switch (alg) {
-
-    case ALG_APSHA:
-        /* XXX cpw >= 28 + strlen(sha1) chars - fixed len SHA */
-        apr_sha1_base64(pw,strlen(pw),cpw);
-        break;
-
-    case ALG_APMD5:
-        if (seed_rand()) {
-            break;
-        }
-        generate_salt(&salt[0], 8);
-        salt[8] = '\0';
-
-        apr_md5_encode((const char *)pw, (const char *)salt,
-                     cpw, sizeof(cpw));
-        break;
-
-    case ALG_PLAIN:
-        /* XXX this len limitation is not in sync with any HTTPd len. */
-        apr_cpystrn(cpw,pw,sizeof(cpw));
-        break;
-
-#if CRYPT_ALGO_SUPPORTED
-    case ALG_CRYPT:
-    default:
-        if (seed_rand()) {
-            break;
-        }
-        to64(&salt[0], rand(), 8);
-        salt[8] = '\0';
-
-        cbuf = crypt(pw, salt);
-        if (cbuf == NULL) {
-            char errbuf[128];
-
-            apr_snprintf(record, rlen-1, "crypt() failed: %s", 
-                         apr_strerror(errno, errbuf, sizeof errbuf));
-            return ERR_PWMISMATCH;
-        }
-
-        apr_cpystrn(cpw, cbuf, sizeof(cpw) - 1);
-        if (strlen(pw) > 8) {
-            char *truncpw = strdup(pw);
-            truncpw[8] = '\0';
-            if (!strcmp(cpw, crypt(truncpw, salt))) {
-                apr_file_printf(errfile, "Warning: Password truncated to 8 characters "
-                                "by CRYPT algorithm." NL);
-            }
-            free(truncpw);
-        }
-        break;
-#endif /* CRYPT_ALGO_SUPPORTED */
-    }
-    memset(pw, '\0', strlen(pw));
-
-    /*
-     * Check to see if the buffer is large enough to hold the username,
-     * hash, and delimiters.
-     */
-    if ((strlen(user) + 1 + strlen(cpw)) > (rlen - 1)) {
-        apr_cpystrn(record, "resultant record too long", (rlen - 1));
+    char hash_str[MAX_STRING_LEN];
+    int ret;
+    ctx->out = hash_str;
+    ctx->out_len = sizeof(hash_str);
+
+    ret = mkhash(ctx);
+    if (ret)
+        return ret;
+
+    ctx->out = apr_pstrcat(ctx->pool, user, ":", hash_str, NL, NULL);
+    if (strlen(ctx->out) >= MAX_STRING_LEN) {
+        ctx->errstr = "resultant record too long";
         return ERR_OVERFLOW;
     }
-    strcpy(record, user);
-    strcat(record, ":");
-    strcat(record, cpw);
-    strcat(record, "\n");
     return 0;
 }
 
 static void usage(void)
 {
-    apr_file_printf(errfile, "Usage:" NL);
-    apr_file_printf(errfile, "\thtpasswd [-cmdpsD] passwordfile username" NL);
-    apr_file_printf(errfile, "\thtpasswd -b[cmdpsD] passwordfile username "
-                    "password" NL NL);
-    apr_file_printf(errfile, "\thtpasswd -n[mdps] username" NL);
-    apr_file_printf(errfile, "\thtpasswd -nb[mdps] username password" NL);
-    apr_file_printf(errfile, " -c  Create a new file." NL);
-    apr_file_printf(errfile, " -n  Don't update file; display results on "
-                    "stdout." NL);
-    apr_file_printf(errfile, " -m  Force MD5 encryption of the password"
-        " (default)"
-        "." NL);
-    apr_file_printf(errfile, " -d  Force CRYPT encryption of the password"
-            " (8 chars max, insecure)." NL);
-    apr_file_printf(errfile, " -p  Do not encrypt the password (plaintext)." NL);
-    apr_file_printf(errfile, " -s  Force SHA encryption of the password"
-            " (insecure)." NL);
-    apr_file_printf(errfile, " -b  Use the password from the command line "
-            "rather than prompting for it." NL);
-    apr_file_printf(errfile, " -D  Delete the specified user." NL);
-    apr_file_printf(errfile,
-            "On other systems than Windows and NetWare the '-p' flag will "
-            "probably not work." NL);
-    apr_file_printf(errfile,
-            "The SHA algorithm does not use a salt and is less secure than "
-            "the MD5 algorithm." NL);
+    apr_file_printf(errfile, "Usage:" NL
+        "\thtpasswd [-cimBdpsD] [-C cost] passwordfile username" NL
+        "\thtpasswd -b[cmBdpsD] [-C cost] passwordfile username password" NL
+        NL
+        "\thtpasswd -n[imBdps] [-C cost] username" NL
+        "\thtpasswd -nb[mBdps] [-C cost] username password" NL
+        " -c  Create a new file." NL
+        " -n  Don't update file; display results on stdout." NL
+        " -i  Read password from stdin without verification (for script usage)" NL
+        " -m  Force MD5 encryption of the password (default)." NL
+        " -B  Force bcrypt encryption of the password (very secure)." NL
+        " -C  Set the computing time used for the bcrypt algorithm" NL
+        "     (higher is more secure but slower, default: %d, valid: 4 to 31)" NL
+        " -d  Force CRYPT encryption of the password (8 chars max, "
+            "insecure)." NL
+        " -p  Do not encrypt the password (plaintext, insecure)." NL
+        " -s  Force SHA encryption of the password (insecure)." NL
+        " -b  Use the password from the command line rather than prompting "
+            "for it." NL
+        " -D  Delete the specified user." NL
+        "On other systems than Windows and NetWare the '-p' flag will "
+            "probably not work." NL
+        "The SHA algorithm does not use a salt and is less secure than the "
+            "MD5 algorithm." NL,
+        BCRYPT_DEFAULT_COST
+    );
     exit(ERR_SYNTAX);
 }
 
@@ -334,64 +155,47 @@ static void terminate(void)
 #endif
 }
 
-static void check_args(apr_pool_t *pool, int argc, const char *const argv[],
-                       int *alg, int *mask, char **user, char **pwfilename,
-                       char **password)
+static void check_args(int argc, const char *const argv[],
+                       struct passwd_ctx *ctx, int *mask, char **user,
+                       char **pwfilename)
 {
     const char *arg;
     int args_left = 2;
-    int i;
+    int i, ret;
+    apr_getopt_t *state;
+    apr_status_t rv;
+    char opt;
+    const char *opt_arg;
+    apr_pool_t *pool = ctx->pool;
 
-    /*
-     * Preliminary check to make sure they provided at least
-     * three arguments, we'll do better argument checking as
-     * we parse the command line.
-     */
-    if (argc < 3) {
-        usage();
-    }
+    rv = apr_getopt_init(&state, pool, argc, argv);
+    if (rv != APR_SUCCESS)
+        exit(ERR_SYNTAX);
 
-    /*
-     * Go through the argument list and pick out any options.  They
-     * have to precede any other arguments.
-     */
-    for (i = 1; i < argc; i++) {
-        arg = argv[i];
-        if (*arg != '-') {
+    while ((rv = apr_getopt(state, "cnmspdBbDiC:", &opt, &opt_arg)) == APR_SUCCESS) {
+        switch (opt) {
+        case 'c':
+            *mask |= APHTP_NEWFILE;
             break;
-        }
-        while (*++arg != '\0') {
-            if (*arg == 'c') {
-                *mask |= APHTP_NEWFILE;
-            }
-            else if (*arg == 'n') {
-                *mask |= APHTP_NOFILE;
-                args_left--;
-            }
-            else if (*arg == 'm') {
-                *alg = ALG_APMD5;
-            }
-            else if (*arg == 's') {
-                *alg = ALG_APSHA;
-            }
-            else if (*arg == 'p') {
-                *alg = ALG_PLAIN;
-            }
-            else if (*arg == 'd') {
-                *alg = ALG_CRYPT;
-            }
-            else if (*arg == 'b') {
-                *mask |= APHTP_NONINTERACTIVE;
-                args_left++;
-            }
-            else if (*arg == 'D') {
-                *mask |= APHTP_DELUSER;
-            }
-            else {
-                usage();
+        case 'n':
+            args_left--;
+            *mask |= APHTP_NOFILE;
+            break;
+        case 'D':
+            *mask |= APHTP_DELUSER;
+            break;
+        default:
+            ret = parse_common_options(ctx, opt, opt_arg);
+            if (ret) {
+                apr_file_printf(errfile, "%s: %s" NL, argv[0], ctx->errstr);
+                exit(ret);
             }
         }
     }
+    if (ctx->passwd_src == PW_ARG)
+        args_left++;
+    if (rv != APR_EOF)
+        usage();
 
     if ((*mask & APHTP_NEWFILE) && (*mask & APHTP_NOFILE)) {
         apr_file_printf(errfile, "%s: -c and -n options conflict" NL, argv[0]);
@@ -410,38 +214,36 @@ static void check_args(apr_pool_t *pool,
      * (the filename, the username, and possibly the password if -b was
      * specified).
      */
+    i = state->ind;
     if ((argc - i) != args_left) {
         usage();
     }
 
-    if (*mask & APHTP_NOFILE) {
-        i--;
-    }
-    else {
+    if (!(*mask & APHTP_NOFILE)) {
         if (strlen(argv[i]) > (APR_PATH_MAX - 1)) {
             apr_file_printf(errfile, "%s: filename too long" NL, argv[0]);
             exit(ERR_OVERFLOW);
         }
-        *pwfilename = apr_pstrdup(pool, argv[i]);
-        if (strlen(argv[i + 1]) > (MAX_STRING_LEN - 1)) {
-            apr_file_printf(errfile, "%s: username too long (> %d)" NL,
-                argv[0], MAX_STRING_LEN - 1);
-            exit(ERR_OVERFLOW);
-        }
+        *pwfilename = apr_pstrdup(pool, argv[i++]);
+    }
+    if (strlen(argv[i]) > (MAX_STRING_LEN - 1)) {
+        apr_file_printf(errfile, "%s: username too long (> %d)" NL,
+                        argv[0], MAX_STRING_LEN - 1);
+        exit(ERR_OVERFLOW);
     }
-    *user = apr_pstrdup(pool, argv[i + 1]);
+    *user = apr_pstrdup(pool, argv[i++]);
     if ((arg = strchr(*user, ':')) != NULL) {
         apr_file_printf(errfile, "%s: username contains illegal "
                         "character '%c'" NL, argv[0], *arg);
         exit(ERR_BADUSER);
     }
-    if (*mask & APHTP_NONINTERACTIVE) {
-        if (strlen(argv[i + 2]) > (MAX_STRING_LEN - 1)) {
+    if (ctx->passwd_src == PW_ARG) {
+        if (strlen(argv[i]) > (MAX_STRING_LEN - 1)) {
             apr_file_printf(errfile, "%s: password too long (> %d)" NL,
                 argv[0], MAX_STRING_LEN);
             exit(ERR_OVERFLOW);
         }
-        *password = apr_pstrdup(pool, argv[i + 2]);
+        ctx->passwd = apr_pstrdup(pool, argv[i]);
     }
 }
 
@@ -452,9 +254,8 @@ static void check_args(apr_pool_t *pool,
 int main(int argc, const char * const argv[])
 {
     apr_file_t *fpw = NULL;
-    char record[MAX_STRING_LEN];
+    const char *errstr = NULL;
     char line[MAX_STRING_LEN];
-    char *password = NULL;
     char *pwfilename = NULL;
     char *user = NULL;
     char tn[] = "htpasswd.tmp.XXXXXX";
@@ -462,10 +263,10 @@ int main(int argc, const char * const ar
     char *scratch, cp[MAX_STRING_LEN];
     int found = 0;
     int i;
-    int alg = ALG_APMD5;
     int mask = 0;
     apr_pool_t *pool;
     int existing_file = 0;
+    struct passwd_ctx ctx = { 0 };
 #if APR_CHARSET_EBCDIC
     apr_status_t rv;
     apr_xlate_t *to_ascii;
@@ -475,6 +276,8 @@ int main(int argc, const char * const ar
     atexit(terminate);
     apr_pool_create(&pool, NULL);
     apr_file_open_stderr(&errfile, pool);
+    ctx.pool = pool;
+    ctx.alg = ALG_APMD5;
 
 #if APR_CHARSET_EBCDIC
     rv = apr_xlate_open(&to_ascii, "ISO-8859-1", APR_DEFAULT_CHARSET, pool);
@@ -494,22 +297,7 @@ int main(int argc, const char * const ar
     }
 #endif /*APR_CHARSET_EBCDIC*/
 
-    check_args(pool, argc, argv, &alg, &mask, &user, &pwfilename, &password);
-
-
-#if !CRYPT_ALGO_SUPPORTED
-    if (alg == ALG_CRYPT) {
-        alg = ALG_APMD5;
-        apr_file_printf(errfile, "Automatically using MD5 format." NL);
-    }
-#endif
-
-#if CRYPT_ALGO_SUPPORTED
-    if (alg == ALG_PLAIN) {
-        apr_file_printf(errfile,"Warning: storing passwords as plain text "
-                        "might just not work on this platform." NL);
-    }
-#endif
+    check_args(argc, argv, &ctx, &mask, &user, &pwfilename);
 
     /*
      * Only do the file checks if we're supposed to frob it.
@@ -555,14 +343,13 @@ int main(int argc, const char * const ar
      * the mkrecord() routine doesn't have access to argv[].
      */
     if (!(mask & APHTP_DELUSER)) {
-        i = mkrecord(user, record, sizeof(record) - 1,
-                     password, alg);
+        i = mkrecord(&ctx, user);
         if (i != 0) {
-            apr_file_printf(errfile, "%s: %s" NL, argv[0], record);
+            apr_file_printf(errfile, "%s: %s" NL, argv[0], errstr);
             exit(i);
         }
         if (mask & APHTP_NOFILE) {
-            printf("%s" NL, record);
+            printf("%s" NL, ctx.out);
             exit(0);
         }
     }
@@ -637,7 +424,7 @@ int main(int argc, const char * const ar
                      * Add him to the file.
                     */
                     apr_file_printf(errfile, "Updating ");
-                    putline(ftemp, record);
+                    putline(ftemp, ctx.out);
                     found++;
                 }
                 else {
@@ -653,7 +440,7 @@ int main(int argc, const char * const ar
     }
     if (!found && !(mask & APHTP_DELUSER)) {
         apr_file_printf(errfile, "Adding ");
-        putline(ftemp, record);
+        putline(ftemp, ctx.out);
     }
     else if (!found && (mask & APHTP_DELUSER)) {
         apr_file_printf(errfile, "User %s not found" NL, user);

Modified: httpd/httpd/branches/2.4.x/support/htpasswd.dsp
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/support/htpasswd.dsp?rev=1420084&r1=1420083&r2=1420084&view=diff
==============================================================================
--- httpd/httpd/branches/2.4.x/support/htpasswd.dsp (original)
+++ httpd/httpd/branches/2.4.x/support/htpasswd.dsp Tue Dec 11 10:37:25 2012
@@ -100,6 +100,10 @@ SOURCE=.\htpasswd.c
 # End Source File
 # Begin Source File
 
+SOURCE=.\passwd_common.c
+# End Source File
+# Begin Source File
+
 SOURCE=..\build\win32\httpd.rc
 # End Source File
 # End Target

Copied: httpd/httpd/branches/2.4.x/support/passwd_common.c (from r1395253, httpd/httpd/trunk/support/passwd_common.c)
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/support/passwd_common.c?p2=httpd/httpd/branches/2.4.x/support/passwd_common.c&p1=httpd/httpd/trunk/support/passwd_common.c&r1=1395253&r2=1420084&rev=1420084&view=diff
==============================================================================
--- httpd/httpd/trunk/support/passwd_common.c (original)
+++ httpd/httpd/branches/2.4.x/support/passwd_common.c Tue Dec 11 10:37:25 2012
@@ -58,7 +58,7 @@ static int generate_salt(char *s, size_t
 
     n = (size * 6 + 7)/8;
     if (n > sizeof(rnd)) {
-        apr_file_printf(errfile, "%s: BUG: Buffer too small", __func__);
+        apr_file_printf(errfile, "generate_salt(): BUG: Buffer too small");
         abort();
     }
     rv = apr_generate_random_bytes(rnd, n);
@@ -95,18 +95,41 @@ void putline(apr_file_t *f, const char *
 
 int get_password(struct passwd_ctx *ctx)
 {
-    char buf[MAX_STRING_LEN + 1];
-    apr_size_t bufsize = sizeof(buf);
-    if (apr_password_get("New password: ", ctx->out, &ctx->out_len) != 0)
-        goto err_too_long;
-    apr_password_get("Re-type new password: ", buf, &bufsize);
-    if (strcmp(ctx->out, buf) != 0) {
-        ctx->errstr = "password verification error";
-        memset(ctx->out, '\0', ctx->out_len);
+    if (ctx->passwd_src == PW_STDIN) {
+        char *buf = ctx->out;
+        apr_file_t *file_stdin;
+        apr_size_t nread;
+        if (apr_file_open_stdin(&file_stdin, ctx->pool) != APR_SUCCESS) {
+            ctx->errstr = "Unable to read from stdin.";
+            return ERR_GENERAL;
+        }
+        if (apr_file_read_full(file_stdin, buf, ctx->out_len - 1,
+                               &nread) != APR_EOF
+            || nread == ctx->out_len - 1) {
+            goto err_too_long;
+        }
+        buf[nread] = '\0';
+        if (nread >= 1 && buf[nread-1] == '\n') {
+            buf[nread-1] = '\0';
+            if (nread >= 2 && buf[nread-2] == '\r')
+                buf[nread-2] = '\0';
+        }
+        apr_file_close(file_stdin);
+    }
+    else {
+        char buf[MAX_STRING_LEN + 1];
+        apr_size_t bufsize = sizeof(buf);
+        if (apr_password_get("New password: ", ctx->out, &ctx->out_len) != 0)
+            goto err_too_long;
+        apr_password_get("Re-type new password: ", buf, &bufsize);
+        if (strcmp(ctx->out, buf) != 0) {
+            ctx->errstr = "password verification error";
+            memset(ctx->out, '\0', ctx->out_len);
+            memset(buf, '\0', sizeof(buf));
+            return ERR_PWMISMATCH;
+        }
         memset(buf, '\0', sizeof(buf));
-        return ERR_PWMISMATCH;
     }
-    memset(buf, '\0', sizeof(buf));
     return 0;
 
 err_too_long:
@@ -131,6 +154,11 @@ int mkhash(struct passwd_ctx *ctx)
     char *cbuf;
 #endif
 
+    if (ctx->cost != 0 && ctx->alg != ALG_BCRYPT) {
+        apr_file_printf(errfile,
+                        "Warning: Ignoring -C argument for this algorithm." NL);
+    }
+
     if (ctx->passwd != NULL) {
         pw = ctx->passwd;
     }
@@ -189,8 +217,32 @@ int mkhash(struct passwd_ctx *ctx)
         }
         break;
 #endif /* CRYPT_ALGO_SUPPORTED */
+
+#if BCRYPT_ALGO_SUPPORTED
+    case ALG_BCRYPT:
+        rv = apr_generate_random_bytes((unsigned char*)salt, 16);
+        if (rv != APR_SUCCESS) {
+            ctx->errstr = apr_psprintf(ctx->pool, "Unable to generate random "
+                                       "bytes: %pm", &rv);
+            ret = ERR_RANDOM;
+            break;
+        }
+
+        if (ctx->cost == 0)
+            ctx->cost = BCRYPT_DEFAULT_COST;
+        rv = apr_bcrypt_encode(pw, ctx->cost, (unsigned char*)salt, 16,
+                               ctx->out, ctx->out_len);
+        if (rv != APR_SUCCESS) {
+            ctx->errstr = apr_psprintf(ctx->pool, "Unable to encode with "
+                                       "bcrypt: %pm", &rv);
+            ret = ERR_PWMISMATCH;
+            break;
+        }
+        break;
+#endif /* BCRYPT_ALGO_SUPPORTED */
+
     default:
-        apr_file_printf(errfile, "%s: BUG: invalid algorithm %d", __func__,
+        apr_file_printf(errfile, "mkhash(): BUG: invalid algorithm %d",
                         ctx->alg);
         abort();
     }
@@ -205,6 +257,9 @@ int parse_common_options(struct passwd_c
     case 'b':
         ctx->passwd_src = PW_ARG;
         break;
+    case 'i':
+        ctx->passwd_src = PW_STDIN;
+        break;
     case 'm':
         ctx->alg = ALG_APMD5;
         break;
@@ -232,8 +287,29 @@ int parse_common_options(struct passwd_c
         ctx->alg = ALG_APMD5;
 #endif
         break;
+    case 'B':
+#if BCRYPT_ALGO_SUPPORTED
+        ctx->alg = ALG_BCRYPT;
+#else
+        /* Don't fall back to something less secure */
+        ctx->errstr = "BCRYPT algorithm not supported on this platform";
+        return ERR_ALG_NOT_SUPP;
+#endif
+        break;
+    case 'C': {
+            char *endptr;
+            long num = strtol(opt_arg, &endptr, 10);
+            if (*endptr != '\0' || num <= 0) {
+                ctx->errstr = "argument to -C must be a positive integer";
+                return ERR_SYNTAX;
+            }
+            ctx->cost = num;
+            break;
+        }
     default:
-        apr_file_printf(errfile, "%s: BUG: invalid option %c", __func__, opt);
+        apr_file_printf(errfile, 
+                        "parse_common_options(): BUG: invalid option %c",
+                        opt);
         abort();
     }
     return 0;

Copied: httpd/httpd/branches/2.4.x/support/passwd_common.h (from r1395253, httpd/httpd/trunk/support/passwd_common.h)
URL: http://svn.apache.org/viewvc/httpd/httpd/branches/2.4.x/support/passwd_common.h?p2=httpd/httpd/branches/2.4.x/support/passwd_common.h&p1=httpd/httpd/trunk/support/passwd_common.h&r1=1395253&r2=1420084&rev=1420084&view=diff
==============================================================================
--- httpd/httpd/trunk/support/passwd_common.h (original)
+++ httpd/httpd/branches/2.4.x/support/passwd_common.h Tue Dec 11 10:37:25 2012
@@ -21,6 +21,9 @@
 #include "apr_file_io.h"
 #include "apr_general.h"
 #include "apr_version.h"
+#if !APR_VERSION_AT_LEAST(2,0,0)
+#include "apu_version.h"
+#endif
 
 #define MAX_STRING_LEN 256
 
@@ -28,6 +31,9 @@
 #define ALG_CRYPT 1
 #define ALG_APMD5 2
 #define ALG_APSHA 3
+#define ALG_BCRYPT 4
+
+#define BCRYPT_DEFAULT_COST 5
 
 #define ERR_FILEPERM 1
 #define ERR_SYNTAX 2
@@ -50,6 +56,13 @@
 #define PLAIN_ALGO_SUPPORTED 0
 #endif
 
+#if APR_VERSION_AT_LEAST(2,0,0) || \
+    (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 5)
+#define BCRYPT_ALGO_SUPPORTED 1
+#else
+#define BCRYPT_ALGO_SUPPORTED 0
+#endif
+
 /*
  * Must be initialized with apr_file_open_stderr() before using any of the
  * below functions.
@@ -63,9 +76,11 @@ struct passwd_ctx {
     apr_size_t      out_len;
     char            *passwd;
     int             alg;
+    int             cost;
     enum {
         PW_PROMPT = 0,
-        PW_ARG
+        PW_ARG,
+        PW_STDIN
     } passwd_src;
 };