1 /* Copyright 1999-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 /******************************************************************************
17 ******************************************************************************
18 * NOTE! This program is not safe as a setuid executable! Do not make it
20 ******************************************************************************
21 *****************************************************************************/
23 * htpasswd.c: simple program for manipulating password file for
24 * the Apache HTTP server
26 * Originally by Rob McCool
30 * 1: Failure; file access/permission problem
31 * 2: Failure; command line syntax problem (usage message issued)
32 * 3: Failure; password verification failure
33 * 4: Failure; operation interrupted (such as with CTRL/C)
34 * 5: Failure; buffer would overflow (username, filename, or computed
36 * 6: Failure; username contains illegal or reserved characters
37 * 7: Failure; file is not a valid htpasswd file
42 #include "apr_strings.h"
43 #include "apr_errno.h"
44 #include "apr_file_io.h"
45 #include "apr_general.h"
46 #include "apr_signal.h"
71 #define unlink _unlink
74 #if !APR_CHARSET_EBCDIC
77 #else /*APR_CHARSET_EBCDIC*/
80 #endif /*APR_CHARSET_EBCDIC*/
82 #define MAX_STRING_LEN 256
88 #define ERR_FILEPERM 1
90 #define ERR_PWMISMATCH 3
91 #define ERR_INTERRUPTED 4
92 #define ERR_OVERFLOW 5
96 #define APHTP_NEWFILE 1
97 #define APHTP_NOFILE 2
98 #define APHTP_NONINTERACTIVE 4
99 #define APHTP_DELUSER 8
102 apr_file_t *ftemp = NULL;
104 static void to64(char *s, unsigned long v, int n)
106 static unsigned char itoa64[] = /* 0 ... 63 => ASCII - 64 */
107 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
110 *s++ = itoa64[v&0x3f];
115 static void putline(apr_file_t *f, const char *l)
121 * Make a password record from the given information. A zero return
122 * indicates success; failure means that the output buffer contains an
123 * error message instead.
125 static int mkrecord(char *user, char *record, apr_size_t rlen, char *passwd,
130 char pwin[MAX_STRING_LEN];
131 char pwv[MAX_STRING_LEN];
135 if (passwd != NULL) {
139 bufsize = sizeof(pwin);
140 if (apr_password_get("New password: ", pwin, &bufsize) != 0) {
141 apr_snprintf(record, (rlen - 1), "password too long (>%"
142 APR_SIZE_T_FMT ")", sizeof(pwin) - 1);
145 bufsize = sizeof(pwv);
146 apr_password_get("Re-type new password: ", pwv, &bufsize);
147 if (strcmp(pwin, pwv) != 0) {
148 apr_cpystrn(record, "password verification error", (rlen - 1));
149 return ERR_PWMISMATCH;
152 memset(pwv, '\0', sizeof(pwin));
157 /* XXX cpw >= 28 + strlen(sha1) chars - fixed len SHA */
158 apr_sha1_base64(pw,strlen(pw),cpw);
162 (void) srand((int) time((time_t *) NULL));
163 to64(&salt[0], rand(), 8);
166 apr_md5_encode((const char *)pw, (const char *)salt,
171 /* XXX this len limitation is not in sync with any HTTPd len. */
172 apr_cpystrn(cpw,pw,sizeof(cpw));
175 #if !(defined(WIN32) || defined(NETWARE))
178 (void) srand((int) time((time_t *) NULL));
179 to64(&salt[0], rand(), 8);
182 apr_cpystrn(cpw, (char *)crypt(pw, salt), sizeof(cpw) - 1);
186 memset(pw, '\0', strlen(pw));
189 * Check to see if the buffer is large enough to hold the username,
190 * hash, and delimiters.
192 if ((strlen(user) + 1 + strlen(cpw)) > (rlen - 1)) {
193 apr_cpystrn(record, "resultant record too long", (rlen - 1));
196 strcpy(record, user);
199 strcat(record, "\n");
203 static void usage(void)
205 apr_file_printf(errfile, "Usage:\n");
206 apr_file_printf(errfile, "\thtpasswd [-cmdpsD] passwordfile username\n");
207 apr_file_printf(errfile, "\thtpasswd -b[cmdpsD] passwordfile username "
209 apr_file_printf(errfile, "\thtpasswd -n[mdps] username\n");
210 apr_file_printf(errfile, "\thtpasswd -nb[mdps] username password\n");
211 apr_file_printf(errfile, " -c Create a new file.\n");
212 apr_file_printf(errfile, " -n Don't update file; display results on "
214 apr_file_printf(errfile, " -m Force MD5 encryption of the password"
215 #if defined(WIN32) || defined(TPF) || defined(NETWARE)
219 apr_file_printf(errfile, " -d Force CRYPT encryption of the password"
220 #if (!(defined(WIN32) || defined(TPF) || defined(NETWARE)))
224 apr_file_printf(errfile, " -p Do not encrypt the password (plaintext).\n");
225 apr_file_printf(errfile, " -s Force SHA encryption of the password.\n");
226 apr_file_printf(errfile, " -b Use the password from the command line "
227 "rather than prompting for it.\n");
228 apr_file_printf(errfile, " -D Delete the specified user.\n");
229 apr_file_printf(errfile,
230 "On Windows, NetWare and TPF systems the '-m' flag is used by "
232 apr_file_printf(errfile,
233 "On all other systems, the '-p' flag will probably not work.\n");
238 * Check to see if the specified file can be opened for the given
241 static int accessible(apr_pool_t *pool, char *fname, int mode)
243 apr_file_t *f = NULL;
245 if (apr_file_open(&f, fname, mode, APR_OS_DEFAULT, pool) != APR_SUCCESS) {
253 * Return true if the named file exists, regardless of permissions.
255 static int exists(char *fname, apr_pool_t *pool)
260 check = apr_stat(&sbuf, fname, APR_FINFO_TYPE, pool);
261 return ((check || sbuf.filetype != APR_REG) ? 0 : 1);
264 static void terminate(void)
272 static void check_args(apr_pool_t *pool, int argc, const char *const argv[],
273 int *alg, int *mask, char **user, char **pwfilename,
281 * Preliminary check to make sure they provided at least
282 * three arguments, we'll do better argument checking as
283 * we parse the command line.
290 * Go through the argument list and pick out any options. They
291 * have to precede any other arguments.
293 for (i = 1; i < argc; i++) {
298 while (*++arg != '\0') {
300 *mask |= APHTP_NEWFILE;
302 else if (*arg == 'n') {
303 *mask |= APHTP_NOFILE;
306 else if (*arg == 'm') {
309 else if (*arg == 's') {
312 else if (*arg == 'p') {
315 else if (*arg == 'd') {
318 else if (*arg == 'b') {
319 *mask |= APHTP_NONINTERACTIVE;
322 else if (*arg == 'D') {
323 *mask |= APHTP_DELUSER;
331 if ((*mask & APHTP_NEWFILE) && (*mask & APHTP_NOFILE)) {
332 apr_file_printf(errfile, "%s: -c and -n options conflict\n", argv[0]);
335 if ((*mask & APHTP_NEWFILE) && (*mask & APHTP_DELUSER)) {
336 apr_file_printf(errfile, "%s: -c and -D options conflict\n", argv[0]);
339 if ((*mask & APHTP_NOFILE) && (*mask & APHTP_DELUSER)) {
340 apr_file_printf(errfile, "%s: -n and -D options conflict\n", argv[0]);
344 * Make sure we still have exactly the right number of arguments left
345 * (the filename, the username, and possibly the password if -b was
348 if ((argc - i) != args_left) {
352 if (*mask & APHTP_NOFILE) {
356 if (strlen(argv[i]) > (APR_PATH_MAX - 1)) {
357 apr_file_printf(errfile, "%s: filename too long\n", argv[0]);
360 *pwfilename = apr_pstrdup(pool, argv[i]);
361 if (strlen(argv[i + 1]) > (MAX_STRING_LEN - 1)) {
362 apr_file_printf(errfile, "%s: username too long (> %d)\n",
363 argv[0], MAX_STRING_LEN - 1);
367 *user = apr_pstrdup(pool, argv[i + 1]);
368 if ((arg = strchr(*user, ':')) != NULL) {
369 apr_file_printf(errfile, "%s: username contains illegal "
370 "character '%c'\n", argv[0], *arg);
373 if (*mask & APHTP_NONINTERACTIVE) {
374 if (strlen(argv[i + 2]) > (MAX_STRING_LEN - 1)) {
375 apr_file_printf(errfile, "%s: password too long (> %d)\n",
376 argv[0], MAX_STRING_LEN);
379 *password = apr_pstrdup(pool, argv[i + 2]);
384 * Let's do it. We end up doing a lot of file opening and closing,
385 * but what do we care? This application isn't run constantly.
387 int main(int argc, const char * const argv[])
389 apr_file_t *fpw = NULL;
390 char record[MAX_STRING_LEN];
391 char line[MAX_STRING_LEN];
392 char *password = NULL;
393 char *pwfilename = NULL;
395 char tn[] = "htpasswd.tmp.XXXXXX";
397 char scratch[MAX_STRING_LEN];
403 int existing_file = 0;
404 #if APR_CHARSET_EBCDIC
406 apr_xlate_t *to_ascii;
409 apr_app_initialize(&argc, &argv, NULL);
411 apr_pool_create(&pool, NULL);
412 apr_file_open_stderr(&errfile, pool);
414 #if APR_CHARSET_EBCDIC
415 rv = apr_xlate_open(&to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, pool);
417 apr_file_printf(errfile, "apr_xlate_open(to ASCII)->%d\n", rv);
420 rv = apr_SHA1InitEBCDIC(to_ascii);
422 apr_file_printf(errfile, "apr_SHA1InitEBCDIC()->%d\n", rv);
425 rv = apr_MD5InitEBCDIC(to_ascii);
427 apr_file_printf(errfile, "apr_MD5InitEBCDIC()->%d\n", rv);
430 #endif /*APR_CHARSET_EBCDIC*/
432 check_args(pool, argc, argv, &alg, &mask, &user, &pwfilename, &password);
435 #if defined(WIN32) || defined(NETWARE)
436 if (alg == ALG_CRYPT) {
438 apr_file_printf(errfile, "Automatically using MD5 format.\n");
442 #if (!(defined(WIN32) || defined(TPF) || defined(NETWARE)))
443 if (alg == ALG_PLAIN) {
444 apr_file_printf(errfile,"Warning: storing passwords as plain text "
445 "might just not work on this platform.\n");
450 * Only do the file checks if we're supposed to frob it.
452 if (!(mask & APHTP_NOFILE)) {
453 existing_file = exists(pwfilename, pool);
456 * Check that this existing file is readable and writable.
458 if (!accessible(pool, pwfilename, APR_READ | APR_APPEND)) {
459 apr_file_printf(errfile, "%s: cannot open file %s for "
460 "read/write access\n", argv[0], pwfilename);
466 * Error out if -c was omitted for this non-existant file.
468 if (!(mask & APHTP_NEWFILE)) {
469 apr_file_printf(errfile,
470 "%s: cannot modify file %s; use '-c' to create it\n",
471 argv[0], pwfilename);
475 * As it doesn't exist yet, verify that we can create it.
477 if (!accessible(pool, pwfilename, APR_CREATE | APR_WRITE)) {
478 apr_file_printf(errfile, "%s: cannot create file %s\n",
479 argv[0], pwfilename);
486 * All the file access checks (if any) have been made. Time to go to work;
487 * try to create the record for the username in question. If that
488 * fails, there's no need to waste any time on file manipulations.
489 * Any error message text is returned in the record buffer, since
490 * the mkrecord() routine doesn't have access to argv[].
492 if (!(mask & APHTP_DELUSER)) {
493 i = mkrecord(user, record, sizeof(record) - 1,
496 apr_file_printf(errfile, "%s: %s\n", argv[0], record);
499 if (mask & APHTP_NOFILE) {
500 printf("%s\n", record);
506 * We can access the files the right way, and we have a record
507 * to add or update. Let's do it..
509 if (apr_temp_dir_get((const char**)&dirname, pool) != APR_SUCCESS) {
510 apr_file_printf(errfile, "%s: could not determine temp dir\n",
514 dirname = apr_psprintf(pool, "%s/%s", dirname, tn);
516 if (apr_file_mktemp(&ftemp, dirname, 0, pool) != APR_SUCCESS) {
517 apr_file_printf(errfile, "%s: unable to create temporary file %s\n",
523 * If we're not creating a new file, copy records from the existing
524 * one to the temporary file until we find the specified user.
526 if (existing_file && !(mask & APHTP_NEWFILE)) {
527 if (apr_file_open(&fpw, pwfilename, APR_READ | APR_BUFFERED,
528 APR_OS_DEFAULT, pool) != APR_SUCCESS) {
529 apr_file_printf(errfile, "%s: unable to read file %s\n",
530 argv[0], pwfilename);
533 while (apr_file_gets(line, sizeof(line), fpw) == APR_SUCCESS) {
536 if ((line[0] == '#') || (line[0] == '\0')) {
537 putline(ftemp, line);
540 strcpy(scratch, line);
542 * See if this is our user.
544 colon = strchr(scratch, ':');
550 * If we've not got a colon on the line, this could well
551 * not be a valid htpasswd file.
552 * We should bail at this point.
554 apr_file_printf(errfile, "\n%s: The file %s does not appear "
555 "to be a valid htpasswd file.\n",
556 argv[0], pwfilename);
560 if (strcmp(user, scratch) != 0) {
561 putline(ftemp, line);
565 if (!(mask & APHTP_DELUSER)) {
566 /* We found the user we were looking for.
567 * Add him to the file.
569 apr_file_printf(errfile, "Updating ");
570 putline(ftemp, record);
574 /* We found the user we were looking for.
575 * Delete them from the file.
577 apr_file_printf(errfile, "Deleting ");
584 if (!found && !(mask & APHTP_DELUSER)) {
585 apr_file_printf(errfile, "Adding ");
586 putline(ftemp, record);
588 else if (!found && (mask & APHTP_DELUSER)) {
589 apr_file_printf(errfile, "User %s not found\n", user);
592 apr_file_printf(errfile, "password for user %s\n", user);
594 /* The temporary file has all the data, just copy it to the new location.
596 if (apr_file_copy(dirname, pwfilename, APR_FILE_SOURCE_PERMS, pool) !=
598 apr_file_printf(errfile, "%s: unable to update file %s\n",
599 argv[0], pwfilename);
602 apr_file_close(ftemp);