From 17c7b2315b781b8cbdac3dc5728655900a72a266 Mon Sep 17 00:00:00 2001 From: Andre Malo Date: Wed, 5 Mar 2003 16:37:00 +0000 Subject: [PATCH] Restore the ability of htdigest.exe to create files that contain more than one user. On win32 we cannot system("copy") a file, while it's open. PR: PR 12910 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@98893 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES | 5 +++- support/htdigest.c | 64 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/CHANGES b/CHANGES index 267b7d0cd8..914326968c 100644 --- a/CHANGES +++ b/CHANGES @@ -2,7 +2,10 @@ Changes with Apache 2.1.0-dev [Remove entries to the current 2.0 section below, when backported] - *) Added the WindowsSocketsWorkaroud directive for Windows NT/2000/XP + *) Restore the ability of htdigest.exe to create files that contain + more than one user. PR 12910. [André Malo] + + *) Added the WindowsSocketsWorkaround directive for Windows NT/2000/XP to work around problems with certain VPN and Firewall products that have buggy AcceptEx implementations. [Allan Edwards w/ suggestions from Bill Stoddard & Bill Rowe] diff --git a/support/htdigest.c b/support/htdigest.c index b3874de0e4..ece9173d6d 100644 --- a/support/htdigest.c +++ b/support/htdigest.c @@ -70,6 +70,7 @@ #include "apr_lib.h" /* for apr_getpass() */ #include "apr_general.h" #include "apr_signal.h" +#include "apr_strings.h" /* for apr_pstrdup() */ #define APR_WANT_STDIO #define APR_WANT_STRFUNC @@ -97,12 +98,45 @@ #define MAX_STRING_LEN 256 +/* DELONCLOSE is quite cool, but: + * we need to close the file before we can copy it. + * otherwise it's locked by the system ;-( + * + * XXX: Other systems affected? (Netware?, OS2?) + */ +#if (defined(WIN32)) +#define OMIT_DELONCLOSE 1 +#endif + apr_file_t *tfp = NULL; apr_pool_t *cntxt; #if APR_CHARSET_EBCDIC apr_xlate_t *to_ascii; #endif +static void cleanup_tempfile_and_exit(int rc) +{ + if (tfp) { +#ifdef OMIT_DELONCLOSE + const char *cfilename; + char *filename = NULL; + + if (apr_file_name_get(&cfilename, tfp) == APR_SUCCESS) { + filename = apr_pstrdup(cntxt, cfilename); + } +#endif + apr_file_close(tfp); + +#ifdef OMIT_DELONCLOSE + if (filename) { + apr_file_remove(filename, cntxt); + } +#endif + } + + exit(rc); +} + static void getword(char *word, char *line, char stop) { int x = 0, y; @@ -160,16 +194,13 @@ static void add_password(const char *user, const char *realm, apr_file_t *f) if (apr_password_get("New password: ", pwin, &len) != APR_SUCCESS) { fprintf(stderr, "password too long"); - exit(5); + cleanup_tempfile_and_exit(5); } len = sizeof(pwin); apr_password_get("Re-type new password: ", pwv, &len); if (strcmp(pwin, pwv) != 0) { fprintf(stderr, "They don't match, sorry.\n"); - if (tfp) { - apr_file_close(tfp); - } - exit(1); + cleanup_tempfile_and_exit(1); } pw = pwin; apr_file_printf(f, "%s:%s:", user, realm); @@ -200,10 +231,7 @@ static void usage(void) static void interrupted(void) { fprintf(stderr, "Interrupted.\n"); - if (tfp) { - apr_file_close(tfp); - } - exit(1); + cleanup_tempfile_and_exit(1); } static void terminate(void) @@ -262,7 +290,13 @@ int main(int argc, const char * const argv[]) else if (argc != 4) usage(); - if (apr_file_mktemp(&tfp, tn, 0, cntxt) != APR_SUCCESS) { + if (apr_file_mktemp(&tfp, tn, +#ifdef OMIT_DELONCLOSE + APR_CREATE | APR_READ | APR_WRITE | APR_EXCL +#else + 0 +#endif + , cntxt) != APR_SUCCESS) { fprintf(stderr, "Could not open temp file.\n"); exit(1); } @@ -271,7 +305,7 @@ int main(int argc, const char * const argv[]) fprintf(stderr, "Could not open passwd file %s for reading.\n", argv[1]); fprintf(stderr, "Use -c option to create new one.\n"); - exit(1); + cleanup_tempfile_and_exit(1); } strcpy(user, argv[3]); strcpy(realm, argv[2]); @@ -305,7 +339,15 @@ int main(int argc, const char * const argv[]) #else sprintf(command, "cp %s %s", tn, argv[1]); #endif + +#ifdef OMIT_DELONCLOSE + apr_file_close(tfp); + system(command); + apr_file_remove(tn, cntxt); +#else system(command); apr_file_close(tfp); +#endif + return 0; } -- 2.40.0