-/* Copyright 1999-2004 The Apache Software Foundation
- *
- * Licensed 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
+/* 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
*
#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? (OS2?)
- */
-#if 0
-#define OMIT_DELONCLOSE 1
-#endif
-
apr_file_t *tfp = NULL;
+apr_file_t *errfile;
apr_pool_t *cntxt;
#if APR_CHARSET_EBCDIC
apr_xlate_t *to_ascii;
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);
}
char ch;
apr_status_t rv = APR_EINVAL;
- while (i < (n - 1) &&
+ while (i < (n - 1) &&
((rv = apr_file_getc(&ch, f)) == APR_SUCCESS) && (ch != '\n')) {
s[i++] = ch;
}
s[i++] = ch;
s[i] = '\0';
- if (rv != APR_SUCCESS)
+ if (rv != APR_SUCCESS)
return 1;
return 0;
char *pw;
apr_md5_ctx_t context;
unsigned char digest[16];
- char string[MAX_STRING_LEN];
+ char string[3 * MAX_STRING_LEN]; /* this includes room for 2 * ':' + '\0' */
char pwin[MAX_STRING_LEN];
char pwv[MAX_STRING_LEN];
unsigned int i;
apr_size_t len = sizeof(pwin);
if (apr_password_get("New password: ", pwin, &len) != APR_SUCCESS) {
- fprintf(stderr, "password too long");
+ apr_file_printf(errfile, "password too long");
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");
+ apr_file_printf(errfile, "They don't match, sorry.\n");
cleanup_tempfile_and_exit(1);
}
pw = pwin;
apr_file_printf(f, "%s:%s:", user, realm);
/* Do MD5 stuff */
- sprintf(string, "%s:%s:%s", user, realm, pw);
+ apr_snprintf(string, sizeof(string), "%s:%s:%s", user, realm, pw);
apr_md5_init(&context);
#if APR_CHARSET_EBCDIC
static void usage(void)
{
- fprintf(stderr, "Usage: htdigest [-c] passwordfile realm username\n");
- fprintf(stderr, "The -c flag creates a new file.\n");
+ apr_file_printf(errfile, "Usage: htdigest [-c] passwordfile realm username\n");
+ apr_file_printf(errfile, "The -c flag creates a new file.\n");
exit(1);
}
static void interrupted(void)
{
- fprintf(stderr, "Interrupted.\n");
+ apr_file_printf(errfile, "Interrupted.\n");
cleanup_tempfile_and_exit(1);
}
char *dirname;
char user[MAX_STRING_LEN];
char realm[MAX_STRING_LEN];
- char line[MAX_STRING_LEN];
- char l[MAX_STRING_LEN];
+ char line[3 * MAX_STRING_LEN];
+ char l[3 * MAX_STRING_LEN];
char w[MAX_STRING_LEN];
char x[MAX_STRING_LEN];
- char command[MAX_STRING_LEN];
int found;
-
+
apr_app_initialize(&argc, &argv, NULL);
- atexit(terminate);
+ atexit(terminate);
apr_pool_create(&cntxt, NULL);
+ apr_file_open_stderr(&errfile, cntxt);
#if APR_CHARSET_EBCDIC
- rv = apr_xlate_open(&to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, cntxt);
+ rv = apr_xlate_open(&to_ascii, "ISO-8859-1", APR_DEFAULT_CHARSET, cntxt);
if (rv) {
- fprintf(stderr, "apr_xlate_open(): %s (%d)\n",
+ apr_file_printf(errfile, "apr_xlate_open(): %s (%d)\n",
apr_strerror(rv, line, sizeof(line)), rv);
exit(1);
}
#endif
-
+
apr_signal(SIGINT, (void (*)(int)) interrupted);
if (argc == 5) {
if (strcmp(argv[1], "-c"))
usage();
- rv = apr_file_open(&f, argv[2], APR_WRITE | APR_CREATE, -1, cntxt);
+ rv = apr_file_open(&f, argv[2], APR_WRITE | APR_CREATE,
+ APR_OS_DEFAULT, cntxt);
if (rv != APR_SUCCESS) {
char errmsg[120];
- fprintf(stderr, "Could not open passwd file %s for writing: %s\n",
+ apr_file_printf(errfile, "Could not open passwd file %s for writing: %s\n",
argv[2],
apr_strerror(rv, errmsg, sizeof errmsg));
exit(1);
}
- printf("Adding password for %s in realm %s.\n", argv[4], argv[3]);
- add_password(argv[4], argv[3], f);
+ apr_cpystrn(user, argv[4], sizeof(user));
+ apr_cpystrn(realm, argv[3], sizeof(realm));
+ apr_file_printf(errfile, "Adding password for %s in realm %s.\n",
+ user, realm);
+ add_password(user, realm, f);
apr_file_close(f);
exit(0);
}
usage();
if (apr_temp_dir_get((const char**)&dirname, cntxt) != APR_SUCCESS) {
- fprintf(stderr, "%s: could not determine temp dir\n",
+ apr_file_printf(errfile, "%s: could not determine temp dir\n",
argv[0]);
exit(1);
}
dirname = apr_psprintf(cntxt, "%s/%s", dirname, tn);
- if (apr_file_mktemp(&tfp, dirname,
-#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");
+ if (apr_file_mktemp(&tfp, dirname, 0, cntxt) != APR_SUCCESS) {
+ apr_file_printf(errfile, "Could not open temp file %s.\n", dirname);
exit(1);
}
- if (apr_file_open(&f, argv[1], APR_READ, -1, cntxt) != APR_SUCCESS) {
- fprintf(stderr,
+ if (apr_file_open(&f, argv[1], APR_READ, APR_OS_DEFAULT, cntxt) != APR_SUCCESS) {
+ apr_file_printf(errfile,
"Could not open passwd file %s for reading.\n", argv[1]);
- fprintf(stderr, "Use -c option to create new one.\n");
+ apr_file_printf(errfile, "Use -c option to create new one.\n");
cleanup_tempfile_and_exit(1);
}
apr_cpystrn(user, argv[3], sizeof(user));
apr_cpystrn(realm, argv[2], sizeof(realm));
found = 0;
- while (!(get_line(line, MAX_STRING_LEN, f))) {
+ while (!(get_line(line, sizeof(line), f))) {
if (found || (line[0] == '#') || (!line[0])) {
putline(tfp, line);
continue;
continue;
}
else {
- printf("Changing password for user %s in realm %s\n", user, realm);
+ apr_file_printf(errfile, "Changing password for user %s in realm %s\n",
+ user, realm);
add_password(user, realm, tfp);
found = 1;
}
}
if (!found) {
- printf("Adding user %s in realm %s\n", user, realm);
+ apr_file_printf(errfile, "Adding user %s in realm %s\n", user, realm);
add_password(user, realm, tfp);
}
apr_file_close(f);
-#ifdef OMIT_DELONCLOSE
- apr_file_close(tfp);
-#endif
+
/* The temporary file has all the data, just copy it to the new location.
*/
if (apr_file_copy(dirname, argv[1], APR_FILE_SOURCE_PERMS, cntxt) !=
APR_SUCCESS) {
- fprintf(stderr, "%s: unable to update file %s\n",
+ apr_file_printf(errfile, "%s: unable to update file %s\n",
argv[0], argv[1]);
}
-#ifdef OMIT_DELONCLOSE
- apr_file_remove(dirname, cntxt);
-#else
apr_file_close(tfp);
-#endif
return 0;
}