htdigest.c revision f2159535eb8f7ea510a17038b8156256abc77a41
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny/* ====================================================================
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * The Apache Software License, Version 1.1
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny *
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * Copyright (c) 2000 The Apache Software Foundation. All rights
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * reserved.
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny *
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * Redistribution and use in source and binary forms, with or without
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * modification, are permitted provided that the following conditions
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * are met:
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny *
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * 1. Redistributions of source code must retain the above copyright
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * notice, this list of conditions and the following disclaimer.
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny *
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * 2. Redistributions in binary form must reproduce the above copyright
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * notice, this list of conditions and the following disclaimer in
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * the documentation and/or other materials provided with the
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * distribution.
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny *
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * 3. The end-user documentation included with the redistribution,
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * if any, must include the following acknowledgment:
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * "This product includes software developed by the
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * Apache Software Foundation (http://www.apache.org/)."
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * Alternately, this acknowledgment may appear in the software itself,
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * if and wherever such third-party acknowledgments normally appear.
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny *
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * 4. The names "Apache" and "Apache Software Foundation" must
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny * not be used to endorse or promote products derived from this
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * software without prior written permission. For written
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * permission, please contact apache@apache.org.
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny *
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * 5. Products derived from this software may not be called "Apache",
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * nor may "Apache" appear in their name, without prior written
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * permission of the Apache Software Foundation.
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny *
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * SUCH DAMAGE.
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * ====================================================================
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny *
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * This software consists of voluntary contributions made by many
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * individuals on behalf of the Apache Software Foundation. For more
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * information on the Apache Software Foundation, please see
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny * <http://www.apache.org/>.
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny */
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny/******************************************************************************
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny ******************************************************************************
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * NOTE! This program is not safe as a setuid executable! Do not make it
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * setuid!
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny ******************************************************************************
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny *****************************************************************************/
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny/*
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * htdigest.c: simple program for manipulating digest passwd file for Apache
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny *
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny * by Alexei Kosut, based on htpasswd.c, by Rob McCool
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny */
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#include "apr_lib.h"
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny#include "apr_md5.h"
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny#ifdef HAVE_SYS_TYPES_H
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny#include <sys/types.h>
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny#endif
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#ifdef HAVE_SYS_SIGNAL_H
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#include <sys/signal.h>
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#endif
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#ifdef HAVE_SIGNAL_H
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#include <signal.h>
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#endif
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny#include <stdlib.h>
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#ifdef WIN32
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#include <conio.h>
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#endif
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#if 'A' == 0xC1
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#define CHARSET_EBCDIC
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#endif
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#ifdef CHARSET_EBCDIC
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#define LF '\n'
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#define CR '\r'
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#else
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#define LF 10
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#define CR 13
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#endif /* CHARSET_EBCDIC */
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#define MAX_STRING_LEN 256
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zelenychar *tn;
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zelenyapr_pool_t *cntxt;
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny#ifdef CHARSET_EBCDIC
ca4b7b92738f3dd463914e3de5757cd98d37a983Stephen Gallagherapr_xlate_t *to_ascii;
ca4b7b92738f3dd463914e3de5757cd98d37a983Stephen Gallagher#endif
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zelenystatic void getword(char *word, char *line, char stop)
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny{
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny int x = 0, y;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny for (x = 0; ((line[x]) && (line[x] != stop)); x++)
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny word[x] = line[x];
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny word[x] = '\0';
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny if (line[x])
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny ++x;
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny y = 0;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny while ((line[y++] = line[x++]));
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny}
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zelenystatic int getline(char *s, int n, apr_file_t *f)
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny{
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny register int i = 0;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny char ch;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny while (1) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_getc(&ch, f);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny s[i] = ch;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (s[i] == CR)
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_getc(&ch, f);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny s[i] = ch;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if ((s[i] == 0x4) || (s[i] == LF) || (i == (n - 1))) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny s[i] = '\0';
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (apr_eof(f) == APR_EOF) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny return 1;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny return 0;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny ++i;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny}
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zelenystatic void putline(apr_file_t *f, char *l)
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny{
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny int x;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny for (x = 0; l[x]; x++)
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_putc(l[x], f);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_putc('\n', f);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny}
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zelenystatic void add_password(char *user, char *realm, apr_file_t *f)
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny{
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny char *pw;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_md5_ctx_t context;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny unsigned char digest[16];
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny char string[MAX_STRING_LEN];
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny char pwin[MAX_STRING_LEN];
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny char pwv[MAX_STRING_LEN];
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny unsigned int i;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny size_t len = sizeof(pwin);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny if (apr_getpass("New password: ", pwin, &len) != APR_SUCCESS) {
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny fprintf(stderr, "password too long");
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny exit(5);
2f3ee3f49019f5b60adbe073070f31e6e2d7c7abStephen Gallagher }
2f3ee3f49019f5b60adbe073070f31e6e2d7c7abStephen Gallagher len = sizeof(pwin);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_getpass("Re-type new password: ", pwv, &len);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (strcmp(pwin, pwv) != 0) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny fprintf(stderr, "They don't match, sorry.\n");
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (tn) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_remove_file(tn, cntxt);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny exit(1);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny pw = pwin;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_fprintf(f, "%s:%s:", user, realm);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny /* Do MD5 stuff */
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny sprintf(string, "%s:%s:%s", user, realm, pw);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_MD5Init(&context);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#ifdef CHARSET_EBCDIC
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_MD5SetXlate(&context, to_ascii);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#endif
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_MD5Update(&context, (unsigned char *) string, strlen(string));
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_MD5Final(digest, &context);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny for (i = 0; i < 16; i++)
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_fprintf(f, "%02x", digest[i]);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_fprintf(f, "\n");
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny}
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zelenystatic void usage(void)
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny{
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny fprintf(stderr, "Usage: htdigest [-c] passwordfile realm username\n");
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny fprintf(stderr, "The -c flag creates a new file.\n");
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny exit(1);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny}
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zelenystatic void interrupted(void)
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny{
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny fprintf(stderr, "Interrupted.\n");
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (tn)
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_remove_file(tn, cntxt);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny exit(1);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny}
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zelenyint main(int argc, char *argv[])
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny{
ca4b7b92738f3dd463914e3de5757cd98d37a983Stephen Gallagher apr_file_t *tfp = NULL, *f;
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny apr_status_t rv;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny char user[MAX_STRING_LEN];
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny char realm[MAX_STRING_LEN];
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny char line[MAX_STRING_LEN];
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny char l[MAX_STRING_LEN];
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny char w[MAX_STRING_LEN];
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny char x[MAX_STRING_LEN];
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny char command[MAX_STRING_LEN];
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny int found;
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny rv = apr_initialize();
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (rv) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny fprintf(stderr, "apr_initialize(): %s (%d)\n",
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_strerror(rv, line, sizeof(line)), rv);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny exit(1);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny atexit(apr_terminate);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_create_pool(&cntxt, NULL);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#ifdef CHARSET_EBCDIC
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny rv = apr_xlate_open(&to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, cntxt);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (rv) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny fprintf(stderr, "apr_xlate_open(): %s (%d)\n",
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_strerror(rv, line, sizeof(line)), rv);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny exit(1);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny#endif
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny tn = NULL;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_signal(SIGINT, (void (*)(int)) interrupted);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (argc == 5) {
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny if (strcmp(argv[1], "-c"))
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny usage();
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (apr_open(&tfp, argv[2], APR_WRITE | APR_CREATE, -1, cntxt) != APR_SUCCESS) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny fprintf(stderr, "Could not open passwd file %s for writing.\n",
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny argv[2]);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny perror("apr_open");
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny exit(1);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny printf("Adding password for %s in realm %s.\n", argv[4], argv[3]);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny add_password(argv[4], argv[3], tfp);
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny apr_close(tfp);
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny exit(0);
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny }
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny else if (argc != 4)
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny usage();
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny tn = tmpnam(NULL);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (apr_open(&tfp, tn, APR_WRITE | APR_CREATE, -1, cntxt)!= APR_SUCCESS) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny fprintf(stderr, "Could not open temp file.\n");
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny exit(1);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
74f857536411b46712f9b3dc0f1c53924b36dc02Stephen Gallagher
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (apr_open(&f, argv[1], APR_READ, -1, cntxt) != APR_SUCCESS) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny fprintf(stderr,
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny "Could not open passwd file %s for reading.\n", argv[1]);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny fprintf(stderr, "Use -c option to create new one.\n");
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny exit(1);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny strcpy(user, argv[3]);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny strcpy(realm, argv[2]);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny found = 0;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny while (!(getline(line, MAX_STRING_LEN, f))) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (found || (line[0] == '#') || (!line[0])) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny putline(tfp, line);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny continue;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny strcpy(l, line);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny getword(w, l, ':');
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny getword(x, l, ':');
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny if (strcmp(user, w) || strcmp(realm, x)) {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny putline(tfp, line);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny continue;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny else {
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny printf("Changing password for user %s in realm %s\n", user, realm);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny add_password(user, realm, tfp);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny found = 1;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny }
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny }
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny if (!found) {
fdab7bbf8933351f6254438c30ff361cd748b15aJan Zeleny printf("Adding user %s in realm %s\n", user, realm);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny add_password(user, realm, tfp);
2f3ee3f49019f5b60adbe073070f31e6e2d7c7abStephen Gallagher }
2f3ee3f49019f5b60adbe073070f31e6e2d7c7abStephen Gallagher apr_close(f);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_close(tfp);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#if defined(OS2) || defined(WIN32)
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny sprintf(command, "copy \"%s\" \"%s\"", tn, argv[1]);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#else
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny sprintf(command, "cp %s %s", tn, argv[1]);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny#endif
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny system(command);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny apr_remove_file(tn, cntxt);
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny return 0;
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny}
ad07ed37b6b51ef134d4524edaf2259e19ac984fJan Zeleny