1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /******************************************************************************
18 ******************************************************************************
19 * NOTE! This program is not safe as a setuid executable! Do not make it
21 ******************************************************************************
22 *****************************************************************************/
24 * htpasswd.c: simple program for manipulating password file for
25 * the Apache HTTP server
27 * Originally by Rob McCool
31 * 1: Failure; file access/permission problem
32 * 2: Failure; command line syntax problem (usage message issued)
33 * 3: Failure; password verification failure
34 * 4: Failure; operation interrupted (such as with CTRL/C)
35 * 5: Failure; buffer would overflow (username, filename, or computed
37 * 6: Failure; username contains illegal or reserved characters
38 * 7: Failure; file is not a valid htpasswd file
43 #include "apr_strings.h"
44 #include "apr_errno.h"
45 #include "apr_file_io.h"
46 #include "apr_general.h"
47 #include "apr_signal.h"
72 #define unlink _unlink
75 #if !APR_CHARSET_EBCDIC
78 #else /*APR_CHARSET_EBCDIC*/
81 #endif /*APR_CHARSET_EBCDIC*/
83 #define MAX_STRING_LEN 256
89 #define ERR_FILEPERM 1
91 #define ERR_PWMISMATCH 3
92 #define ERR_INTERRUPTED 4
93 #define ERR_OVERFLOW 5
97 #define APHTP_NEWFILE 1
98 #define APHTP_NOFILE 2
99 #define APHTP_NONINTERACTIVE 4
100 #define APHTP_DELUSER 8
103 apr_file_t *ftemp = NULL;
105 static void to64(char *s, unsigned long v, int n)
107 static unsigned char itoa64[] = /* 0 ... 63 => ASCII - 64 */
108 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
111 *s++ = itoa64[v&0x3f];
116 static void putline(apr_file_t *f, const char *l)
122 * Make a password record from the given information. A zero return
123 * indicates success; failure means that the output buffer contains an
124 * error message instead.
126 static int mkrecord(char *user, char *record, apr_size_t rlen, char *passwd,
131 char pwin[MAX_STRING_LEN];
132 char pwv[MAX_STRING_LEN];
136 if (passwd != NULL) {
140 bufsize = sizeof(pwin);
141 if (apr_password_get("New password: ", pwin, &bufsize) != 0) {
142 apr_snprintf(record, (rlen - 1), "password too long (>%"
143 APR_SIZE_T_FMT ")", sizeof(pwin) - 1);
146 bufsize = sizeof(pwv);
147 apr_password_get("Re-type new password: ", pwv, &bufsize);
148 if (strcmp(pwin, pwv) != 0) {
149 apr_cpystrn(record, "password verification error", (rlen - 1));
150 return ERR_PWMISMATCH;
153 memset(pwv, '\0', sizeof(pwin));
158 /* XXX cpw >= 28 + strlen(sha1) chars - fixed len SHA */
159 apr_sha1_base64(pw,strlen(pw),cpw);
163 (void) srand((int) time((time_t *) NULL));
164 to64(&salt[0], rand(), 8);
167 apr_md5_encode((const char *)pw, (const char *)salt,
172 /* XXX this len limitation is not in sync with any HTTPd len. */
173 apr_cpystrn(cpw,pw,sizeof(cpw));
176 #if !(defined(WIN32) || defined(NETWARE))
179 (void) srand((int) time((time_t *) NULL));
180 to64(&salt[0], rand(), 8);
183 apr_cpystrn(cpw, (char *)crypt(pw, salt), sizeof(cpw) - 1);
187 memset(pw, '\0', strlen(pw));
190 * Check to see if the buffer is large enough to hold the username,
191 * hash, and delimiters.
193 if ((strlen(user) + 1 + strlen(cpw)) > (rlen - 1)) {
194 apr_cpystrn(record, "resultant record too long", (rlen - 1));
197 strcpy(record, user);
200 strcat(record, "\n");
204 static void usage(void)
206 apr_file_printf(errfile, "Usage:\n");
207 apr_file_printf(errfile, "\thtpasswd [-cmdpsD] passwordfile username\n");
208 apr_file_printf(errfile, "\thtpasswd -b[cmdpsD] passwordfile username "
210 apr_file_printf(errfile, "\thtpasswd -n[mdps] username\n");
211 apr_file_printf(errfile, "\thtpasswd -nb[mdps] username password\n");
212 apr_file_printf(errfile, " -c Create a new file.\n");
213 apr_file_printf(errfile, " -n Don't update file; display results on "
215 apr_file_printf(errfile, " -m Force MD5 encryption of the password"
216 #if defined(WIN32) || defined(TPF) || defined(NETWARE)
220 apr_file_printf(errfile, " -d Force CRYPT encryption of the password"
221 #if (!(defined(WIN32) || defined(TPF) || defined(NETWARE)))
225 apr_file_printf(errfile, " -p Do not encrypt the password (plaintext).\n");
226 apr_file_printf(errfile, " -s Force SHA encryption of the password.\n");
227 apr_file_printf(errfile, " -b Use the password from the command line "
228 "rather than prompting for it.\n");
229 apr_file_printf(errfile, " -D Delete the specified user.\n");
230 apr_file_printf(errfile,
231 "On Windows, NetWare and TPF systems the '-m' flag is used by "
233 apr_file_printf(errfile,
234 "On all other systems, the '-p' flag will probably not work.\n");
239 * Check to see if the specified file can be opened for the given
242 static int accessible(apr_pool_t *pool, char *fname, int mode)
244 apr_file_t *f = NULL;
246 if (apr_file_open(&f, fname, mode, APR_OS_DEFAULT, pool) != APR_SUCCESS) {
254 * Return true if the named file exists, regardless of permissions.
256 static int exists(char *fname, apr_pool_t *pool)
261 check = apr_stat(&sbuf, fname, APR_FINFO_TYPE, pool);
262 return ((check || sbuf.filetype != APR_REG) ? 0 : 1);
265 static void terminate(void)
273 static void check_args(apr_pool_t *pool, int argc, const char *const argv[],
274 int *alg, int *mask, char **user, char **pwfilename,
282 * Preliminary check to make sure they provided at least
283 * three arguments, we'll do better argument checking as
284 * we parse the command line.
291 * Go through the argument list and pick out any options. They
292 * have to precede any other arguments.
294 for (i = 1; i < argc; i++) {
299 while (*++arg != '\0') {
301 *mask |= APHTP_NEWFILE;
303 else if (*arg == 'n') {
304 *mask |= APHTP_NOFILE;
307 else if (*arg == 'm') {
310 else if (*arg == 's') {
313 else if (*arg == 'p') {
316 else if (*arg == 'd') {
319 else if (*arg == 'b') {
320 *mask |= APHTP_NONINTERACTIVE;
323 else if (*arg == 'D') {
324 *mask |= APHTP_DELUSER;
332 if ((*mask & APHTP_NEWFILE) && (*mask & APHTP_NOFILE)) {
333 apr_file_printf(errfile, "%s: -c and -n options conflict\n", argv[0]);
336 if ((*mask & APHTP_NEWFILE) && (*mask & APHTP_DELUSER)) {
337 apr_file_printf(errfile, "%s: -c and -D options conflict\n", argv[0]);
340 if ((*mask & APHTP_NOFILE) && (*mask & APHTP_DELUSER)) {
341 apr_file_printf(errfile, "%s: -n and -D options conflict\n", argv[0]);
345 * Make sure we still have exactly the right number of arguments left
346 * (the filename, the username, and possibly the password if -b was
349 if ((argc - i) != args_left) {
353 if (*mask & APHTP_NOFILE) {
357 if (strlen(argv[i]) > (APR_PATH_MAX - 1)) {
358 apr_file_printf(errfile, "%s: filename too long\n", argv[0]);
361 *pwfilename = apr_pstrdup(pool, argv[i]);
362 if (strlen(argv[i + 1]) > (MAX_STRING_LEN - 1)) {
363 apr_file_printf(errfile, "%s: username too long (> %d)\n",
364 argv[0], MAX_STRING_LEN - 1);
368 *user = apr_pstrdup(pool, argv[i + 1]);
369 if ((arg = strchr(*user, ':')) != NULL) {
370 apr_file_printf(errfile, "%s: username contains illegal "
371 "character '%c'\n", argv[0], *arg);
374 if (*mask & APHTP_NONINTERACTIVE) {
375 if (strlen(argv[i + 2]) > (MAX_STRING_LEN - 1)) {
376 apr_file_printf(errfile, "%s: password too long (> %d)\n",
377 argv[0], MAX_STRING_LEN);
380 *password = apr_pstrdup(pool, argv[i + 2]);
385 * Let's do it. We end up doing a lot of file opening and closing,
386 * but what do we care? This application isn't run constantly.
388 int main(int argc, const char * const argv[])
390 apr_file_t *fpw = NULL;
391 char record[MAX_STRING_LEN];
392 char line[MAX_STRING_LEN];
393 char *password = NULL;
394 char *pwfilename = NULL;
396 char tn[] = "htpasswd.tmp.XXXXXX";
398 char *scratch, cp[MAX_STRING_LEN];
404 int existing_file = 0;
405 #if APR_CHARSET_EBCDIC
407 apr_xlate_t *to_ascii;
410 apr_app_initialize(&argc, &argv, NULL);
412 apr_pool_create(&pool, NULL);
413 apr_file_open_stderr(&errfile, pool);
415 #if APR_CHARSET_EBCDIC
416 rv = apr_xlate_open(&to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, pool);
418 apr_file_printf(errfile, "apr_xlate_open(to ASCII)->%d\n", rv);
421 rv = apr_SHA1InitEBCDIC(to_ascii);
423 apr_file_printf(errfile, "apr_SHA1InitEBCDIC()->%d\n", rv);
426 rv = apr_MD5InitEBCDIC(to_ascii);
428 apr_file_printf(errfile, "apr_MD5InitEBCDIC()->%d\n", rv);
431 #endif /*APR_CHARSET_EBCDIC*/
433 check_args(pool, argc, argv, &alg, &mask, &user, &pwfilename, &password);
436 #if defined(WIN32) || defined(NETWARE)
437 if (alg == ALG_CRYPT) {
439 apr_file_printf(errfile, "Automatically using MD5 format.\n");
443 #if (!(defined(WIN32) || defined(TPF) || defined(NETWARE)))
444 if (alg == ALG_PLAIN) {
445 apr_file_printf(errfile,"Warning: storing passwords as plain text "
446 "might just not work on this platform.\n");
451 * Only do the file checks if we're supposed to frob it.
453 if (!(mask & APHTP_NOFILE)) {
454 existing_file = exists(pwfilename, pool);
457 * Check that this existing file is readable and writable.
459 if (!accessible(pool, pwfilename, APR_READ | APR_APPEND)) {
460 apr_file_printf(errfile, "%s: cannot open file %s for "
461 "read/write access\n", argv[0], pwfilename);
467 * Error out if -c was omitted for this non-existant file.
469 if (!(mask & APHTP_NEWFILE)) {
470 apr_file_printf(errfile,
471 "%s: cannot modify file %s; use '-c' to create it\n",
472 argv[0], pwfilename);
476 * As it doesn't exist yet, verify that we can create it.
478 if (!accessible(pool, pwfilename, APR_CREATE | APR_WRITE)) {
479 apr_file_printf(errfile, "%s: cannot create file %s\n",
480 argv[0], pwfilename);
487 * All the file access checks (if any) have been made. Time to go to work;
488 * try to create the record for the username in question. If that
489 * fails, there's no need to waste any time on file manipulations.
490 * Any error message text is returned in the record buffer, since
491 * the mkrecord() routine doesn't have access to argv[].
493 if (!(mask & APHTP_DELUSER)) {
494 i = mkrecord(user, record, sizeof(record) - 1,
497 apr_file_printf(errfile, "%s: %s\n", argv[0], record);
500 if (mask & APHTP_NOFILE) {
501 printf("%s\n", record);
507 * We can access the files the right way, and we have a record
508 * to add or update. Let's do it..
510 if (apr_temp_dir_get((const char**)&dirname, pool) != APR_SUCCESS) {
511 apr_file_printf(errfile, "%s: could not determine temp dir\n",
515 dirname = apr_psprintf(pool, "%s/%s", dirname, tn);
517 if (apr_file_mktemp(&ftemp, dirname, 0, pool) != APR_SUCCESS) {
518 apr_file_printf(errfile, "%s: unable to create temporary file %s\n",
524 * If we're not creating a new file, copy records from the existing
525 * one to the temporary file until we find the specified user.
527 if (existing_file && !(mask & APHTP_NEWFILE)) {
528 if (apr_file_open(&fpw, pwfilename, APR_READ | APR_BUFFERED,
529 APR_OS_DEFAULT, pool) != APR_SUCCESS) {
530 apr_file_printf(errfile, "%s: unable to read file %s\n",
531 argv[0], pwfilename);
534 while (apr_file_gets(line, sizeof(line), fpw) == APR_SUCCESS) {
539 while (apr_isspace(*scratch)) {
543 if (!*scratch || (*scratch == '#')) {
544 putline(ftemp, line);
548 * See if this is our user.
550 colon = strchr(scratch, ':');
556 * If we've not got a colon on the line, this could well
557 * not be a valid htpasswd file.
558 * We should bail at this point.
560 apr_file_printf(errfile, "\n%s: The file %s does not appear "
561 "to be a valid htpasswd file.\n",
562 argv[0], pwfilename);
566 if (strcmp(user, scratch) != 0) {
567 putline(ftemp, line);
571 if (!(mask & APHTP_DELUSER)) {
572 /* We found the user we were looking for.
573 * Add him to the file.
575 apr_file_printf(errfile, "Updating ");
576 putline(ftemp, record);
580 /* We found the user we were looking for.
581 * Delete them from the file.
583 apr_file_printf(errfile, "Deleting ");
590 if (!found && !(mask & APHTP_DELUSER)) {
591 apr_file_printf(errfile, "Adding ");
592 putline(ftemp, record);
594 else if (!found && (mask & APHTP_DELUSER)) {
595 apr_file_printf(errfile, "User %s not found\n", user);
598 apr_file_printf(errfile, "password for user %s\n", user);
600 /* The temporary file has all the data, just copy it to the new location.
602 if (apr_file_copy(dirname, pwfilename, APR_FILE_SOURCE_PERMS, pool) !=
604 apr_file_printf(errfile, "%s: unable to update file %s\n",
605 argv[0], pwfilename);
608 apr_file_close(ftemp);