htpasswd.c revision db848a70422b56cf0f15a47c37e17ebe05e2ce04
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
/******************************************************************************
******************************************************************************
* NOTE! This program is not safe as a setuid executable! Do not make it
* setuid!
******************************************************************************
*****************************************************************************/
/*
* htpasswd.c: simple program for manipulating password file for
* the Apache HTTP server
*
* Originally by Rob McCool
*
* Exit values:
* 0: Success
* 1: Failure; file access/permission problem
* 2: Failure; command line syntax problem (usage message issued)
* 3: Failure; password verification failure
* 4: Failure; operation interrupted (such as with CTRL/C)
* 5: Failure; buffer would overflow (username, filename, or computed
* record too long)
* 6: Failure; username contains illegal or reserved characters
*/
#include "apr_lib.h"
#include "apr_errno.h"
#include "ap_config.h"
#include "ap.h"
#include "apr_md5.h"
#include "ap_sha1.h"
#include <signal.h>
#ifdef WIN32
#include <conio.h>
#endif
#ifndef CHARSET_EBCDIC
#define LF 10
#define CR 13
#else /*CHARSET_EBCDIC*/
#define LF '\n'
#define CR '\r'
#endif /*CHARSET_EBCDIC*/
#define MAX_STRING_LEN 256
#define ALG_PLAIN 0
#define ALG_CRYPT 1
#define ALG_APMD5 2
#define ALG_APSHA 3
#define ERR_FILEPERM 1
#define ERR_SYNTAX 2
#define ERR_PWMISMATCH 3
#define ERR_INTERRUPTED 4
#define ERR_OVERFLOW 5
#define ERR_BADUSER 6
/*
* This needs to be declared statically so the signal handler can
* access it.
*/
static char *tempfilename;
/*
* Get a line of input from the user, not including any terminating
* newline.
*/
{
register int i = 0;
while (1) {
s[i] = (char) fgetc(f);
if (s[i] == CR) {
s[i] = fgetc(f);
}
s[i] = '\0';
return (feof(f) ? 1 : 0);
}
++i;
}
}
{
int x;
for (x = 0; l[x]; x++) {
fputc(l[x], f);
}
fputc('\n', f);
}
static void to64(char *s, unsigned long v, int n)
{
static unsigned char itoa64[] = /* 0 ... 63 => ASCII - 64 */
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
while (--n >= 0) {
*s++ = itoa64[v&0x3f];
v >>= 6;
}
}
/*
* Make a password record from the given information. A zero return
* indicates success; failure means that the output buffer contains an
* error message instead.
*/
int alg)
{
char *pw;
char cpw[120];
char pwin[MAX_STRING_LEN];
char pwv[MAX_STRING_LEN];
char salt[9];
}
else {
sizeof(pwin) - 1);
return ERR_OVERFLOW;
}
return ERR_PWMISMATCH;
}
}
switch (alg) {
case ALG_APSHA:
/* XXX cpw >= 28 + strlen(sha1) chars - fixed len SHA */
break;
case ALG_APMD5:
break;
case ALG_PLAIN:
/* XXX this len limitation is not in sync with any HTTPd len. */
break;
case ALG_CRYPT:
default:
break;
}
/*
* Check to see if the buffer is large enough to hold the username,
* hash, and delimiters.
*/
return ERR_OVERFLOW;
}
return 0;
}
static int usage(void)
{
" (default)"
#endif
".\n");
" (default)"
#endif
".\n");
"than prompting for it.\n");
"On Windows and TPF systems the '-m' flag is used by default.\n");
"On all other systems, the '-p' flag will probably not work.\n");
return ERR_SYNTAX;
}
static void interrupted(void)
{
if (tempfilename != NULL) {
}
}
/*
* Check to see if the specified file can be opened for the given
* access.
*/
{
FILE *s;
if (s == NULL) {
return 0;
}
fclose(s);
return 1;
}
/*
* Return true if a file is readable.
*/
{
}
/*
* Return true if the specified file can be opened for write access.
*/
{
}
/*
* Return true if the named file exists, regardless of permissions.
*/
{
#ifdef WIN32
#else
#endif
int check;
#ifdef WIN32
#else
#endif
}
/*
* Copy from the current position of one file to the current position
* of another.
*/
{
static char line[MAX_STRING_LEN];
}
}
/*
* Let's do it. We end up doing a lot of file opening and closing,
* but what do we care? This application isn't run constantly.
*/
{
char user[MAX_STRING_LEN];
char password[MAX_STRING_LEN];
char record[MAX_STRING_LEN];
char line[MAX_STRING_LEN];
char pwfilename[MAX_STRING_LEN];
char *arg;
int found = 0;
int newfile = 0;
int noninteractive = 0;
int i;
int args_left = 2;
tempfilename = NULL;
/*
* Preliminary check to make sure they provided at least
* three arguments, we'll do better argument checking as
* we parse the command line.
*/
if (argc < 3) {
return usage();
}
/*
* Go through the argument list and pick out any options. They
* have to precede any other arguments.
*/
for (i = 1; i < argc; i++) {
if (*arg != '-') {
break;
}
while (*++arg != '\0') {
if (*arg == 'c') {
newfile++;
}
else if (*arg == 'm') {
}
else if (*arg == 's') {
}
else if (*arg == 'p') {
}
else if (*arg == 'd') {
}
else if (*arg == 'b') {
args_left++;
}
else {
return usage();
}
}
}
/*
* Make sure we still have exactly the right number of arguments left
* (the filename, the username, and possibly the password if -b was
* specified).
*/
return usage();
}
return ERR_OVERFLOW;
}
sizeof(user) - 1);
return ERR_OVERFLOW;
}
return ERR_BADUSER;
}
if (noninteractive) {
sizeof(password) - 1);
return ERR_OVERFLOW;
}
}
#ifdef WIN32
}
#endif
"just not work on this platform.\n");
}
#endif
/*
* Verify that the file exists if -c was omitted. We give a special
* message if it doesn't.
*/
argv[0], pwfilename);
perror("fopen");
}
/*
* Verify that we can read the existing file in the case of an update
* to it (rather than creation of a new one).
*/
argv[0], pwfilename);
perror("fopen");
}
/*
* Now check to see if we can preserve an existing file in case
* of password verification errors on a -c operation.
*/
"%s: existing auth data would be lost on password mismatch",
perror("fopen");
}
/*
* Now verify that the file is writable!
*/
if (! writable(pwfilename)) {
argv[0], pwfilename);
perror("fopen");
}
/*
* All the file access checks have been made. Time to go to work;
* try to create the record for the username in question. If that
* fails, there's no need to waste any time on file manipulations.
* Any error message text is returned in the record buffer, since
* the mkrecord() routine doesn't have access to argv[].
*/
alg);
if (i != 0) {
exit(i);
}
/*
* We can access the files the right way, and we have a record
* to add or update. Let's do it..
*/
perror("fopen");
}
/*
* If we're not creating a new file, copy records from the existing
* one to the temporary file until we find the specified user.
*/
if (! newfile) {
char scratch[MAX_STRING_LEN];
char *colon;
continue;
}
/*
* See if this is our user.
*/
*colon = '\0';
}
continue;
}
found++;
break;
}
}
if (found) {
}
else {
}
/*
* Now add the user record we created.
*/
/*
* If we're updating an existing file, there may be additional
* records beyond the one we're updating, so copy them.
*/
if (! newfile) {
}
/*
* The temporary file now contains the information that should be
* in the actual password file. Close the open files, re-open them
* in the appropriate mode, and copy them file to the real one.
*/
return 0;
}