htpasswd.c revision cccd31fa4a72fe23cc3249c06db181b274a55a69
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny/* ====================================================================
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * The Apache Software License, Version 1.1
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny *
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * Copyright (c) 2000 The Apache Software Foundation. All rights
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * reserved.
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny *
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * Redistribution and use in source and binary forms, with or without
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * modification, are permitted provided that the following conditions
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * are met:
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny *
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * 1. Redistributions of source code must retain the above copyright
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * notice, this list of conditions and the following disclaimer.
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny *
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * 2. Redistributions in binary form must reproduce the above copyright
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * notice, this list of conditions and the following disclaimer in
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * the documentation and/or other materials provided with the
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * distribution.
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny *
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * 3. The end-user documentation included with the redistribution,
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * if any, must include the following acknowledgment:
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * "This product includes software developed by the
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * Apache Software Foundation (http://www.apache.org/)."
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * Alternately, this acknowledgment may appear in the software itself,
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * if and wherever such third-party acknowledgments normally appear.
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny *
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * 4. The names "Apache" and "Apache Software Foundation" must
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * not be used to endorse or promote products derived from this
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * software without prior written permission. For written
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * permission, please contact apache@apache.org.
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny *
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * 5. Products derived from this software may not be called "Apache",
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * nor may "Apache" appear in their name, without prior written
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce * permission of the Apache Software Foundation.
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny *
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
a6cca9c284724fafd670a3163812f248ba53ad97Jakub Hrozek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose * SUCH DAMAGE.
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose * ====================================================================
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose *
a6cca9c284724fafd670a3163812f248ba53ad97Jakub Hrozek * This software consists of voluntary contributions made by many
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek * individuals on behalf of the Apache Software Foundation. For more
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * information on the Apache Software Foundation, please see
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * <http://www.apache.org/>.
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny *
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * Portions of this software are based upon public domain software
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * originally written at the National Center for Supercomputing Applications,
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * University of Illinois, Urbana-Champaign.
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny */
aab938c5975f0e3b85c7c79a5d718e5fefed7217Simo Sorce
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny/******************************************************************************
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny ******************************************************************************
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * NOTE! This program is not safe as a setuid executable! Do not make it
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * setuid!
aab938c5975f0e3b85c7c79a5d718e5fefed7217Simo Sorce ******************************************************************************
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny *****************************************************************************/
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny/*
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * htpasswd.c: simple program for manipulating password file for
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * the Apache HTTP server
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny *
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * Originally by Rob McCool
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny *
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * Exit values:
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * 0: Success
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * 1: Failure; file access/permission problem
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * 2: Failure; command line syntax problem (usage message issued)
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * 3: Failure; password verification failure
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * 4: Failure; operation interrupted (such as with CTRL/C)
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * 5: Failure; buffer would overflow (username, filename, or computed
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * record too long)
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * 6: Failure; username contains illegal or reserved characters
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny */
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny#include "apr.h"
aab938c5975f0e3b85c7c79a5d718e5fefed7217Simo Sorce#include "apr_strings.h"
aab938c5975f0e3b85c7c79a5d718e5fefed7217Simo Sorce#include "apr_errno.h"
aab938c5975f0e3b85c7c79a5d718e5fefed7217Simo Sorce#include "apr_file_io.h"
7fe69bb6ec70bce439c6b975a9a0044c98ff502bSimo Sorce
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov#if APR_HAVE_STDIO_H
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov#include <stdio.h>
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny#endif
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny#include "ap_config.h"
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny#include "apr_md5.h"
aab938c5975f0e3b85c7c79a5d718e5fefed7217Simo Sorce#include "ap_sha1.h"
aab938c5975f0e3b85c7c79a5d718e5fefed7217Simo Sorce#include <signal.h>
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny#include <time.h>
aab938c5975f0e3b85c7c79a5d718e5fefed7217Simo Sorce
aab938c5975f0e3b85c7c79a5d718e5fefed7217Simo Sorce#ifdef HAVE_CRYPT_H
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce#include <crypt.h>
aab938c5975f0e3b85c7c79a5d718e5fefed7217Simo Sorce#endif
aab938c5975f0e3b85c7c79a5d718e5fefed7217Simo Sorce
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny#ifdef WIN32
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose#include <conio.h>
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose#define unlink _unlink
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose#endif
a6cca9c284724fafd670a3163812f248ba53ad97Jakub Hrozek
a6cca9c284724fafd670a3163812f248ba53ad97Jakub Hrozek#ifndef CHARSET_EBCDIC
a6cca9c284724fafd670a3163812f248ba53ad97Jakub Hrozek#define LF 10
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose#define CR 13
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose#else /*CHARSET_EBCDIC*/
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose#define LF '\n'
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek#define CR '\r'
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek#endif /*CHARSET_EBCDIC*/
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek#define MAX_STRING_LEN 256
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#define ALG_PLAIN 0
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#define ALG_CRYPT 1
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#define ALG_APMD5 2
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#define ALG_APSHA 3
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#define ERR_FILEPERM 1
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#define ERR_SYNTAX 2
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov#define ERR_PWMISMATCH 3
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov#define ERR_INTERRUPTED 4
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#define ERR_OVERFLOW 5
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#define ERR_BADUSER 6
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce/*
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * This needs to be declared statically so the signal handler can
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * access it.
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce */
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorcestatic char *tempfilename;
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce/*
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * If our platform knows about the tmpnam() external buffer size, create
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * a buffer to pass in. This is needed in a threaded environment, or
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce * one that thinks it is (like HP-UX).
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce */
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#ifdef L_tmpnam
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorcestatic char tname_buf[L_tmpnam];
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#else
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorcestatic char *tname_buf = NULL;
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce#endif
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce/*
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * Get a line of input from the user, not including any terminating
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * newline.
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce */
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorcestatic int getline(char *s, int n, FILE *f)
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce{
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce register int i = 0;
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce while (1) {
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce s[i] = (char) fgetc(f);
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose if (s[i] == CR) {
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose s[i] = fgetc(f);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov }
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov if ((s[i] == 0x4) || (s[i] == LF) || (i == (n - 1))) {
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose s[i] = '\0';
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose return (feof(f) ? 1 : 0);
9822d4d468ec74e4e173f5adf0db12d02974cd18Sumit Bose }
a6cca9c284724fafd670a3163812f248ba53ad97Jakub Hrozek ++i;
a6cca9c284724fafd670a3163812f248ba53ad97Jakub Hrozek }
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek}
a6cca9c284724fafd670a3163812f248ba53ad97Jakub Hrozek
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashovstatic void putline(FILE *f, char *l)
a6cca9c284724fafd670a3163812f248ba53ad97Jakub Hrozek{
a6cca9c284724fafd670a3163812f248ba53ad97Jakub Hrozek int x;
a6cca9c284724fafd670a3163812f248ba53ad97Jakub Hrozek
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose for (x = 0; l[x]; x++) {
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose fputc(l[x], f);
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose }
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose fputc('\n', f);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov}
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bosestatic void to64(char *s, unsigned long v, int n)
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose{
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose static unsigned char itoa64[] = /* 0 ... 63 => ASCII - 64 */
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose while (--n >= 0) {
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose *s++ = itoa64[v&0x3f];
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose v >>= 6;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose }
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose}
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose/*
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose * Make a password record from the given information. A zero return
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose * indicates success; failure means that the output buffer contains an
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose * error message instead.
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose */
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bosestatic int mkrecord(char *user, char *record, size_t rlen, char *passwd,
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose int alg)
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose{
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose char *pw;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose char cpw[120];
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose char pwin[MAX_STRING_LEN];
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose char pwv[MAX_STRING_LEN];
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose char salt[9];
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose size_t bufsize;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose if (passwd != NULL) {
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose pw = passwd;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose }
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose else {
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose bufsize = sizeof(pwin);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose if (apr_getpass("New password: ", pwin, &bufsize) != 0) {
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose apr_snprintf(record, (rlen - 1), "password too long (>%d)",
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose sizeof(pwin) - 1);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose return ERR_OVERFLOW;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose }
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose bufsize = sizeof(pwv);
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek apr_getpass("Re-type new password: ", pwv, &bufsize);
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek if (strcmp(pwin, pwv) != 0) {
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek apr_cpystrn(record, "password verification error", (rlen - 1));
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek return ERR_PWMISMATCH;
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek }
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek pw = pwin;
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek memset(pwv, '\0', sizeof(pwin));
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce }
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce switch (alg) {
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce case ALG_APSHA:
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce /* XXX cpw >= 28 + strlen(sha1) chars - fixed len SHA */
a6cca9c284724fafd670a3163812f248ba53ad97Jakub Hrozek ap_sha1_base64(pw,strlen(pw),cpw);
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek break;
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce case ALG_APMD5:
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce (void) srand((int) time((time_t *) NULL));
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce to64(&salt[0], rand(), 8);
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce salt[8] = '\0';
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny apr_MD5Encode((const char *)pw, (const char *)salt,
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny cpw, sizeof(cpw));
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny break;
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny case ALG_PLAIN:
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny /* XXX this len limitation is not in sync with any HTTPd len. */
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny apr_cpystrn(cpw,pw,sizeof(cpw));
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny break;
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny#ifndef WIN32
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce case ALG_CRYPT:
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny default:
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny (void) srand((int) time((time_t *) NULL));
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny to64(&salt[0], rand(), 8);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny salt[8] = '\0';
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny apr_cpystrn(cpw, (char *)crypt(pw, salt), sizeof(cpw) - 1);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny break;
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce#endif
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny memset(pw, '\0', strlen(pw));
17195241500e46272018d7897d6e87249870caf2Pavel Reichl
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny /*
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose * Check to see if the buffer is large enough to hold the username,
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * hash, and delimiters.
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny */
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if ((strlen(user) + 1 + strlen(cpw)) > (rlen - 1)) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny apr_cpystrn(record, "resultant record too long", (rlen - 1));
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny return ERR_OVERFLOW;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce strcpy(record, user);
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce strcat(record, ":");
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny strcat(record, cpw);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny return 0;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny}
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorcestatic int usage(void)
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce{
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny fprintf(stderr, "Usage:\n");
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny fprintf(stderr, "\thtpasswd [-cmdps] passwordfile username\n");
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny fprintf(stderr, "\thtpasswd -b[cmdps] passwordfile username password\n\n");
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny fprintf(stderr, "\thtpasswd -n[mdps] username\n");
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny fprintf(stderr, "\thtpasswd -nb[mdps] username password\n");
cb388d52f49f54963379cc20a25e14d17fe6e9a3Simo Sorce fprintf(stderr, " -c Create a new file.\n");
cb388d52f49f54963379cc20a25e14d17fe6e9a3Simo Sorce fprintf(stderr, " -n Don't update file; display results on stdout.\n");
cb388d52f49f54963379cc20a25e14d17fe6e9a3Simo Sorce fprintf(stderr, " -m Force MD5 encryption of the password"
cb388d52f49f54963379cc20a25e14d17fe6e9a3Simo Sorce#if defined(WIN32) || defined(TPF)
cb388d52f49f54963379cc20a25e14d17fe6e9a3Simo Sorce " (default)"
cb388d52f49f54963379cc20a25e14d17fe6e9a3Simo Sorce#endif
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov ".\n");
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov fprintf(stderr, " -d Force CRYPT encryption of the password"
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny#if (!(defined(WIN32) || defined(TPF)))
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny " (default)"
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny#endif
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny ".\n");
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce fprintf(stderr, " -p Do not encrypt the password (plaintext).\n");
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce fprintf(stderr, " -s Force SHA encryption of the password.\n");
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce fprintf(stderr, " -b Use the password from the command line rather "
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce "than prompting for it.\n");
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce fprintf(stderr,
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce "On Windows and TPF systems the '-m' flag is used by default.\n");
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce fprintf(stderr,
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce "On all other systems, the '-p' flag will probably not work.\n");
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce return ERR_SYNTAX;
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce}
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorce
204cfc89a076fd32bf34f2abb3f809304aaa88abSimo Sorcestatic void interrupted(void)
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny{
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny fprintf(stderr, "Interrupted.\n");
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce if (tempfilename != NULL) {
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce unlink(tempfilename);
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce }
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce exit(ERR_INTERRUPTED);
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce}
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny/*
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * Check to see if the specified file can be opened for the given
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * access.
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny */
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zelenystatic int accessible(char *fname, char *mode)
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny{
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny FILE *s;
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce s = fopen(fname, mode);
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce if (s == NULL) {
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce return 0;
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce }
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce fclose(s);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny return 1;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny}
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny/*
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * Return true if a file is readable.
17195241500e46272018d7897d6e87249870caf2Pavel Reichl */
17195241500e46272018d7897d6e87249870caf2Pavel Reichlstatic int readable(char *fname)
17195241500e46272018d7897d6e87249870caf2Pavel Reichl{
17195241500e46272018d7897d6e87249870caf2Pavel Reichl return accessible(fname, "r");
17195241500e46272018d7897d6e87249870caf2Pavel Reichl}
17195241500e46272018d7897d6e87249870caf2Pavel Reichl
17195241500e46272018d7897d6e87249870caf2Pavel Reichl/*
17195241500e46272018d7897d6e87249870caf2Pavel Reichl * Return true if the specified file can be opened for write access.
17195241500e46272018d7897d6e87249870caf2Pavel Reichl */
17195241500e46272018d7897d6e87249870caf2Pavel Reichlstatic int writable(char *fname)
17195241500e46272018d7897d6e87249870caf2Pavel Reichl{
17195241500e46272018d7897d6e87249870caf2Pavel Reichl return accessible(fname, "a");
17195241500e46272018d7897d6e87249870caf2Pavel Reichl}
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose/*
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose * Return true if the named file exists, regardless of permissions.
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose */
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bosestatic int exists(char *fname)
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose{
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose apr_finfo_t sbuf;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose apr_status_t check;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose check = apr_stat(&sbuf, fname, NULL);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose return (check ? 0 : 1);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose}
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose/*
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose * Copy from the current position of one file to the current position
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose * of another.
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose */
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bosestatic void copy_file(FILE *target, FILE *source)
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose{
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose static char line[MAX_STRING_LEN];
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose while (fgets(line, sizeof(line), source) != NULL) {
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose fputs(line, target);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose }
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose}
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose/*
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose * Let's do it. We end up doing a lot of file opening and closing,
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose * but what do we care? This application isn't run constantly.
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose */
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Boseint main(int argc, char *argv[])
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose{
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose FILE *ftemp = NULL;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose FILE *fpw = NULL;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose char user[MAX_STRING_LEN];
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose char password[MAX_STRING_LEN];
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose char record[MAX_STRING_LEN];
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose char line[MAX_STRING_LEN];
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose char pwfilename[MAX_STRING_LEN];
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose char *arg;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose int found = 0;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose int alg = ALG_CRYPT;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose int newfile = 0;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose int nofile = 0;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose int noninteractive = 0;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose int i;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose int args_left = 2;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose#ifdef CHARSET_EBCDIC
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose apr_pool_t *pool;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose apr_status_t rv;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose apr_xlate_t *to_ascii;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose apr_initialize();
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose atexit(apr_terminate);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose apr_create_pool(&pool, NULL);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose rv = apr_xlate_open(&to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, pool);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose if (rv) {
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose fprintf(stderr, "apr_xlate_open(to ASCII)->%d\n", rv);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose exit(1);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose }
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose rv = ap_SHA1InitEBCDIC(to_ascii);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose if (rv) {
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose fprintf(stderr, "ap_SHA1InitEBCDIC()->%d\n", rv);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose exit(1);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose }
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose rv = apr_MD5InitEBCDIC(to_ascii);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose if (rv) {
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose fprintf(stderr, "apr_MD5InitEBCDIC()->%d\n", rv);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose exit(1);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose }
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose#endif /*CHARSET_EBCDIC*/
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose tempfilename = NULL;
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose signal(SIGINT, (void (*)(int)) interrupted);
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose /*
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose * Preliminary check to make sure they provided at least
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose * three arguments, we'll do better argument checking as
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose * we parse the command line.
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose */
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose if (argc < 3) {
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose return usage();
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose }
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose
9f734d4c122e37cc3080974342ed9586d05d5f83Sumit Bose /*
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * Go through the argument list and pick out any options. They
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * have to precede any other arguments.
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny */
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny for (i = 1; i < argc; i++) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny arg = argv[i];
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce if (*arg != '-') {
17195241500e46272018d7897d6e87249870caf2Pavel Reichl break;
17195241500e46272018d7897d6e87249870caf2Pavel Reichl }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny while (*++arg != '\0') {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (*arg == 'c') {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny newfile++;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny else if (*arg == 'n') {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny nofile++;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny args_left--;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny else if (*arg == 'm') {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny alg = ALG_APMD5;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny else if (*arg == 's') {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny alg = ALG_APSHA;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny else if (*arg == 'p') {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny alg = ALG_PLAIN;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce else if (*arg == 'd') {
0754ff886f909f0404038eb9c99dd61be1acf5b9Simo Sorce alg = ALG_CRYPT;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny else if (*arg == 'b') {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny noninteractive++;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny args_left++;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce else {
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce return usage();
7fe69bb6ec70bce439c6b975a9a0044c98ff502bSimo Sorce }
7fe69bb6ec70bce439c6b975a9a0044c98ff502bSimo Sorce }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny /*
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * Make sure we still have exactly the right number of arguments left
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * (the filename, the username, and possibly the password if -b was
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce * specified).
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny */
c03b28a38b14fdb59f74864ae4dc56affe256508Simo Sorce if ((argc - i) != args_left) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny return usage();
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (newfile && nofile) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny fprintf(stderr, "%s: -c and -n options conflict\n", argv[0]);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny return ERR_SYNTAX;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce if (nofile) {
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce i--;
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce }
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce else {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (strlen(argv[i]) > (sizeof(pwfilename) - 1)) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny fprintf(stderr, "%s: filename too long\n", argv[0]);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny return ERR_OVERFLOW;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny strcpy(pwfilename, argv[i]);
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce if (strlen(argv[i + 1]) > (sizeof(user) - 1)) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny fprintf(stderr, "%s: username too long (>%d)\n", argv[0],
c03b28a38b14fdb59f74864ae4dc56affe256508Simo Sorce sizeof(user) - 1);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny return ERR_OVERFLOW;
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny strcpy(user, argv[i + 1]);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if ((arg = strchr(user, ':')) != NULL) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny fprintf(stderr, "%s: username contains illegal character '%c'\n",
17195241500e46272018d7897d6e87249870caf2Pavel Reichl argv[0], *arg);
17195241500e46272018d7897d6e87249870caf2Pavel Reichl return ERR_BADUSER;
17195241500e46272018d7897d6e87249870caf2Pavel Reichl }
17195241500e46272018d7897d6e87249870caf2Pavel Reichl if (noninteractive) {
17195241500e46272018d7897d6e87249870caf2Pavel Reichl if (strlen(argv[i + 2]) > (sizeof(password) - 1)) {
17195241500e46272018d7897d6e87249870caf2Pavel Reichl fprintf(stderr, "%s: password too long (>%d)\n", argv[0],
17195241500e46272018d7897d6e87249870caf2Pavel Reichl sizeof(password) - 1);
17195241500e46272018d7897d6e87249870caf2Pavel Reichl return ERR_OVERFLOW;
17195241500e46272018d7897d6e87249870caf2Pavel Reichl }
17195241500e46272018d7897d6e87249870caf2Pavel Reichl strcpy(password, argv[i + 2]);
17195241500e46272018d7897d6e87249870caf2Pavel Reichl }
17195241500e46272018d7897d6e87249870caf2Pavel Reichl
17195241500e46272018d7897d6e87249870caf2Pavel Reichl#ifdef WIN32
17195241500e46272018d7897d6e87249870caf2Pavel Reichl if (alg == ALG_CRYPT) {
17195241500e46272018d7897d6e87249870caf2Pavel Reichl alg = ALG_APMD5;
17195241500e46272018d7897d6e87249870caf2Pavel Reichl fprintf(stderr, "Automatically using MD5 format on Windows.\n");
17195241500e46272018d7897d6e87249870caf2Pavel Reichl }
17195241500e46272018d7897d6e87249870caf2Pavel Reichl#endif
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny#if (!(defined(WIN32) || defined(TPF)))
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if (alg == ALG_PLAIN) {
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny fprintf(stderr,"Warning: storing passwords as plain text might "
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny "just not work on this platform.\n");
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny#endif
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov if (! nofile) {
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce /*
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * Only do the file checks if we're supposed to frob it.
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny *
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * Verify that the file exists if -c was omitted. We give a special
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * message if it doesn't.
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny */
3b0e0352d8076909608d04750d3ea6b0d9ba33f6Jakub Hrozek if ((! newfile) && (! exists(pwfilename))) {
3b0e0352d8076909608d04750d3ea6b0d9ba33f6Jakub Hrozek fprintf(stderr,
3b0e0352d8076909608d04750d3ea6b0d9ba33f6Jakub Hrozek "%s: cannot modify file %s; use '-c' to create it\n",
3b0e0352d8076909608d04750d3ea6b0d9ba33f6Jakub Hrozek argv[0], pwfilename);
3b0e0352d8076909608d04750d3ea6b0d9ba33f6Jakub Hrozek perror("fopen");
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny exit(ERR_FILEPERM);
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny }
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny /*
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * Verify that we can read the existing file in the case of an update
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny * to it (rather than creation of a new one).
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny */
84c611c1b7c04cc7735ab54d4e5f48284b79e6fbJan Zeleny if ((! newfile) && (! readable(pwfilename))) {
3912262270a6449ebe1d3e92c27c217b4044f894Simo Sorce fprintf(stderr, "%s: cannot open file %s for read access\n",
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce argv[0], pwfilename);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce perror("fopen");
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose exit(ERR_FILEPERM);
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek }
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek /*
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * Now check to see if we can preserve an existing file in case
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * of password verification errors on a -c operation.
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny */
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce if (newfile && exists(pwfilename) && (! readable(pwfilename))) {
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce fprintf(stderr, "%s: cannot open file %s for read access\n"
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce "%s: existing auth data would be lost on "
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce "password mismatch",
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce argv[0], pwfilename, argv[0]);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce perror("fopen");
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose exit(ERR_FILEPERM);
b3458bbb5315b05d7ac1abc58f1c380761756603Jakub Hrozek }
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose /*
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek * Now verify that the file is writable!
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce */
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce if (! writable(pwfilename)) {
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose fprintf(stderr, "%s: cannot open file %s for write access\n",
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce argv[0], pwfilename);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce perror("fopen");
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce exit(ERR_FILEPERM);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce }
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose }
b3458bbb5315b05d7ac1abc58f1c380761756603Jakub Hrozek
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose /*
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek * All the file access checks (if any) have been made. Time to go to work;
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek * try to create the record for the username in question. If that
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * fails, there's no need to waste any time on file manipulations.
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * Any error message text is returned in the record buffer, since
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * the mkrecord() routine doesn't have access to argv[].
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny */
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny i = mkrecord(user, record, sizeof(record) - 1,
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny noninteractive ? password : NULL,
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny alg);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce if (i != 0) {
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce fprintf(stderr, "%s: %s\n", argv[0], record);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce exit(i);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce }
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce if (nofile) {
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce printf("%s\n", record);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce exit(0);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce }
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny /*
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * We can access the files the right way, and we have a record
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * to add or update. Let's do it..
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce */
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce errno = 0;
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce tempfilename = tmpnam(tname_buf);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce if ((tempfilename == NULL) || (*tempfilename == '\0')) {
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce fprintf(stderr, "%s: unable to generate temporary filename\n",
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce argv[0]);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce if (errno == 0) {
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce errno = ENOENT;
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce }
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose perror("tmpnam");
b3458bbb5315b05d7ac1abc58f1c380761756603Jakub Hrozek exit(ERR_FILEPERM);
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose }
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek ftemp = fopen(tempfilename, "w+");
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce if (ftemp == NULL) {
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce fprintf(stderr, "%s: unable to create temporary file '%s'\n", argv[0],
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny tempfilename);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce perror("fopen");
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce exit(ERR_FILEPERM);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce }
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce /*
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce * If we're not creating a new file, copy records from the existing
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce * one to the temporary file until we find the specified user.
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce */
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce if (! newfile) {
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce char scratch[MAX_STRING_LEN];
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce fpw = fopen(pwfilename, "r");
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce while (! (getline(line, sizeof(line), fpw))) {
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce char *colon;
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce if ((line[0] == '#') || (line[0] == '\0')) {
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce putline(ftemp, line);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce continue;
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce }
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce strcpy(scratch, line);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce /*
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce * See if this is our user.
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce */
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose colon = strchr(scratch, ':');
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose if (colon != NULL) {
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose *colon = '\0';
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose }
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose if (strcmp(user, scratch) != 0) {
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose putline(ftemp, line);
b3458bbb5315b05d7ac1abc58f1c380761756603Jakub Hrozek continue;
b3458bbb5315b05d7ac1abc58f1c380761756603Jakub Hrozek }
b3458bbb5315b05d7ac1abc58f1c380761756603Jakub Hrozek found++;
b3458bbb5315b05d7ac1abc58f1c380761756603Jakub Hrozek break;
b3458bbb5315b05d7ac1abc58f1c380761756603Jakub Hrozek }
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose }
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose if (found) {
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose fprintf(stderr, "Updating ");
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose }
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose else {
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose fprintf(stderr, "Adding ");
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose }
c5711b0279ea85d69fe3c77dfb194360c346e1d7Sumit Bose fprintf(stderr, "password for user %s\n", user);
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek /*
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek * Now add the user record we created.
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek */
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek putline(ftemp, record);
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek /*
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek * If we're updating an existing file, there may be additional
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek * records beyond the one we're updating, so copy them.
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny */
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny if (! newfile) {
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose copy_file(ftemp, fpw);
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek fclose(fpw);
ea224c3813a537639778f91ac762732b3c289603Jakub Hrozek }
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce /*
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * The temporary file now contains the information that should be
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * in the actual password file. Close the open files, re-open them
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny * in the appropriate mode, and copy them file to the real one.
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce */
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce fclose(ftemp);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce fpw = fopen(pwfilename, "w+");
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny ftemp = fopen(tempfilename, "r");
e76d78338026fa47dca32eaf7f5c15eabb1b951aJan Zeleny copy_file(fpw, ftemp);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce fclose(fpw);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce fclose(ftemp);
09d7c105839bfc7447ea0f766413ed86675ca075Sumit Bose unlink(tempfilename);
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce return 0;
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce}
1187a07ed4207c1c326fdf83915dddfe472b8620Simo Sorce