htdigest.c revision 686cd089cc7f4d7be5752f026490ab3016763e95
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder/* Copyright 1999-2004 The Apache Software Foundation
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder *
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder * Licensed under the Apache License, Version 2.0 (the "License");
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * you may not use this file except in compliance with the License.
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu * You may obtain a copy of the License at
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder *
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder * http://www.apache.org/licenses/LICENSE-2.0
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder *
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder * Unless required by applicable law or agreed to in writing, software
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * distributed under the License is distributed on an "AS IS" BASIS,
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * See the License for the specific language governing permissions and
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * limitations under the License.
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder */
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder/******************************************************************************
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder ******************************************************************************
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * NOTE! This program is not safe as a setuid executable! Do not make it
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * setuid!
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder ******************************************************************************
a3631d87c556bfbce810f6207abeee30ca6a6590Christian Maeder *****************************************************************************/
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder/*
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder * htdigest.c: simple program for manipulating digest passwd file for Apache
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder *
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder * by Alexei Kosut, based on htpasswd.c, by Rob McCool
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder */
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#include "apr.h"
10b02b2343246df6773585636fe3ddbefa3b6a1bChristian Maeder#include "apr_file_io.h"
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#include "apr_md5.h"
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#include "apr_lib.h" /* for apr_getpass() */
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#include "apr_general.h"
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder#include "apr_signal.h"
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder#include "apr_strings.h" /* for apr_pstrdup() */
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#define APR_WANT_STDIO
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder#define APR_WANT_STRFUNC
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#include "apr_want.h"
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder#if APR_HAVE_SYS_TYPES_H
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder#include <sys/types.h>
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#endif
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#if APR_HAVE_STDLIB_H
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder#include <stdlib.h>
e418cbe496169f326cdaa6b4ba60f23d74c6b0bdChristian Maeder#endif
e418cbe496169f326cdaa6b4ba60f23d74c6b0bdChristian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#ifdef WIN32
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder#include <conio.h>
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#endif
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#if APR_CHARSET_EBCDIC
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#define LF '\n'
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#define CR '\r'
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#else
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#define LF 10
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#define CR 13
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#endif /* APR_CHARSET_EBCDIC */
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#define MAX_STRING_LEN 256
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maederapr_file_t *tfp = NULL;
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maederapr_file_t *errfile;
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maederapr_pool_t *cntxt;
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder#if APR_CHARSET_EBCDIC
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maederapr_xlate_t *to_ascii;
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#endif
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maederstatic void cleanup_tempfile_and_exit(int rc)
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder{
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder if (tfp) {
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_close(tfp);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder }
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder exit(rc);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder}
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maederstatic void getword(char *word, char *line, char stop)
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder{
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder int x = 0, y;
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder for (x = 0; ((line[x]) && (line[x] != stop)); x++)
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder word[x] = line[x];
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder word[x] = '\0';
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder if (line[x])
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder ++x;
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder y = 0;
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder while ((line[y++] = line[x++]));
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder}
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maederstatic int get_line(char *s, int n, apr_file_t *f)
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder{
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder register int i = 0;
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder char ch;
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_status_t rv = APR_EINVAL;
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder while (i < (n - 1) &&
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder ((rv = apr_file_getc(&ch, f)) == APR_SUCCESS) && (ch != '\n')) {
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder s[i++] = ch;
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder }
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder if (ch == '\n')
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder s[i++] = ch;
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder s[i] = '\0';
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder if (rv != APR_SUCCESS)
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder return 1;
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder return 0;
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder}
7bffb8b0e6cae4bb7ecb59b99327add6106c06b9Christian Maeder
7bffb8b0e6cae4bb7ecb59b99327add6106c06b9Christian Maederstatic void putline(apr_file_t *f, char *l)
f52a4838c101d52bbbd689f6b51f2c1c9202f0a8Christian Maeder{
7bffb8b0e6cae4bb7ecb59b99327add6106c06b9Christian Maeder int x;
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder for (x = 0; l[x]; x++)
7bffb8b0e6cae4bb7ecb59b99327add6106c06b9Christian Maeder apr_file_putc(l[x], f);
7bffb8b0e6cae4bb7ecb59b99327add6106c06b9Christian Maeder}
7bffb8b0e6cae4bb7ecb59b99327add6106c06b9Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maederstatic void add_password(const char *user, const char *realm, apr_file_t *f)
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder{
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder char *pw;
7bffb8b0e6cae4bb7ecb59b99327add6106c06b9Christian Maeder apr_md5_ctx_t context;
7bffb8b0e6cae4bb7ecb59b99327add6106c06b9Christian Maeder unsigned char digest[16];
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder char string[MAX_STRING_LEN];
7bffb8b0e6cae4bb7ecb59b99327add6106c06b9Christian Maeder char pwin[MAX_STRING_LEN];
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder char pwv[MAX_STRING_LEN];
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder unsigned int i;
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_size_t len = sizeof(pwin);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder if (apr_password_get("New password: ", pwin, &len) != APR_SUCCESS) {
adda0e6252b14215228e4071b347c49b808894f8Christian Maeder apr_file_printf(errfile, "password too long");
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder cleanup_tempfile_and_exit(5);
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder }
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder len = sizeof(pwin);
adda0e6252b14215228e4071b347c49b808894f8Christian Maeder apr_password_get("Re-type new password: ", pwv, &len);
adda0e6252b14215228e4071b347c49b808894f8Christian Maeder if (strcmp(pwin, pwv) != 0) {
adda0e6252b14215228e4071b347c49b808894f8Christian Maeder apr_file_printf(errfile, "They don't match, sorry.\n");
adda0e6252b14215228e4071b347c49b808894f8Christian Maeder cleanup_tempfile_and_exit(1);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder }
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder pw = pwin;
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_printf(f, "%s:%s:", user, realm);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder /* Do MD5 stuff */
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder sprintf(string, "%s:%s:%s", user, realm, pw);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_md5_init(&context);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#if APR_CHARSET_EBCDIC
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_md5_set_xlate(&context, to_ascii);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#endif
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_md5_update(&context, (unsigned char *) string, strlen(string));
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_md5_final(digest, &context);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder for (i = 0; i < 16; i++)
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_printf(f, "%02x", digest[i]);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_printf(f, "\n");
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder}
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maederstatic void usage(void)
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder{
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_printf(errfile, "Usage: htdigest [-c] passwordfile realm username\n");
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_printf(errfile, "The -c flag creates a new file.\n");
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder exit(1);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder}
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maederstatic void interrupted(void)
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder{
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_printf(errfile, "Interrupted.\n");
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder cleanup_tempfile_and_exit(1);
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder}
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maederstatic void terminate(void)
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder{
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_terminate();
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#ifdef NETWARE
95894a5e04344f915a76542612de99a058d1abffChristian Maeder pressanykey();
95894a5e04344f915a76542612de99a058d1abffChristian Maeder#endif
95894a5e04344f915a76542612de99a058d1abffChristian Maeder}
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maederint main(int argc, const char * const argv[])
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder{
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_t *f;
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_status_t rv;
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder char tn[] = "htdigest.tmp.XXXXXX";
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder char *dirname;
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder char user[MAX_STRING_LEN];
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder char realm[MAX_STRING_LEN];
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder char line[MAX_STRING_LEN];
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder char l[MAX_STRING_LEN];
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder char w[MAX_STRING_LEN];
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder char x[MAX_STRING_LEN];
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder int found;
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_app_initialize(&argc, &argv, NULL);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder atexit(terminate);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_pool_create(&cntxt, NULL);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_open_stderr(&errfile, cntxt);
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder#if APR_CHARSET_EBCDIC
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder rv = apr_xlate_open(&to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, cntxt);
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder if (rv) {
31d3d7a2f915455d04aadfd36560d1032942b771Christian Maeder apr_file_printf(errfile, "apr_xlate_open(): %s (%d)\n",
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_strerror(rv, line, sizeof(line)), rv);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder exit(1);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder }
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder#endif
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_signal(SIGINT, (void (*)(int)) interrupted);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder if (argc == 5) {
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder if (strcmp(argv[1], "-c"))
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder usage();
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder rv = apr_file_open(&f, argv[2], APR_WRITE | APR_CREATE, -1, cntxt);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder if (rv != APR_SUCCESS) {
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder char errmsg[120];
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_printf(errfile, "Could not open passwd file %s for writing: %s\n",
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder argv[2],
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_strerror(rv, errmsg, sizeof errmsg));
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder exit(1);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder }
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_printf(errfile, "Adding password for %s in realm %s.\n",
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder argv[4], argv[3]);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder add_password(argv[4], argv[3], f);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_close(f);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder exit(0);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder }
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder else if (argc != 4)
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder usage();
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder if (apr_temp_dir_get((const char**)&dirname, cntxt) != APR_SUCCESS) {
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_printf(errfile, "%s: could not determine temp dir\n",
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder argv[0]);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder exit(1);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder }
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder dirname = apr_psprintf(cntxt, "%s/%s", dirname, tn);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder if (apr_file_mktemp(&tfp, dirname, 0, cntxt) != APR_SUCCESS) {
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_printf(errfile, "Could not open temp file %s.\n", dirname);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder exit(1);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder }
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder if (apr_file_open(&f, argv[1], APR_READ, -1, cntxt) != APR_SUCCESS) {
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_printf(errfile,
cf39e23ee25b89496d451fcafc70ece1cf760891Christian Maeder "Could not open passwd file %s for reading.\n", argv[1]);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_file_printf(errfile, "Use -c option to create new one.\n");
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder cleanup_tempfile_and_exit(1);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder }
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_cpystrn(user, argv[3], sizeof(user));
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder apr_cpystrn(realm, argv[2], sizeof(realm));
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder found = 0;
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder while (!(get_line(line, MAX_STRING_LEN, f))) {
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder if (found || (line[0] == '#') || (!line[0])) {
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder putline(tfp, line);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder continue;
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder }
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder strcpy(l, line);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder getword(w, l, ':');
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder getword(x, l, ':');
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder if (strcmp(user, w) || strcmp(realm, x)) {
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder putline(tfp, line);
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder continue;
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder }
10b02b2343246df6773585636fe3ddbefa3b6a1bChristian Maeder else {
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_printf(errfile, "Changing password for user %s in realm %s\n",
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder user, realm);
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder add_password(user, realm, tfp);
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder found = 1;
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder }
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder }
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder if (!found) {
45ad02e03fb913ba373d8fdcfe50244be3df31eaChristian Maeder apr_file_printf(errfile, "Adding user %s in realm %s\n", user, realm);
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder add_password(user, realm, tfp);
1fac054baed931dc57f0e41dd0ade39adac28c49Christian Maeder }
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_close(f);
1fac054baed931dc57f0e41dd0ade39adac28c49Christian Maeder
7bffb8b0e6cae4bb7ecb59b99327add6106c06b9Christian Maeder /* The temporary file has all the data, just copy it to the new location.
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder */
1fac054baed931dc57f0e41dd0ade39adac28c49Christian Maeder if (apr_file_copy(dirname, argv[1], APR_FILE_SOURCE_PERMS, cntxt) !=
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder APR_SUCCESS) {
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_printf(errfile, "%s: unable to update file %s\n",
1fac054baed931dc57f0e41dd0ade39adac28c49Christian Maeder argv[0], argv[1]);
1fac054baed931dc57f0e41dd0ade39adac28c49Christian Maeder }
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder apr_file_close(tfp);
7bffb8b0e6cae4bb7ecb59b99327add6106c06b9Christian Maeder
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder return 0;
ee31a8a5f5d786472f2b5dfb271b38e6d401fa35Christian Maeder}
e2ca90217abd35b3d5f98bfe73ecffb34badd837Christian Maeder