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
41 #include "passwd_common.h"
42 #include "apr_signal.h"
43 #include "apr_getopt.h"
64 #define unlink _unlink
67 #define APHTP_NEWFILE 1
68 #define APHTP_NOFILE 2
69 #define APHTP_DELUSER 4
70 #define APHTP_VERIFY 8
72 apr_file_t *ftemp = NULL;
74 static int mkrecord(struct passwd_ctx *ctx, char *user)
76 char hash_str[MAX_STRING_LEN];
79 ctx->out_len = sizeof(hash_str);
85 ctx->out = apr_pstrcat(ctx->pool, user, ":", hash_str, NL, NULL);
86 if (strlen(ctx->out) >= MAX_STRING_LEN) {
87 ctx->errstr = "resultant record too long";
93 static void usage(void)
95 apr_file_printf(errfile, "Usage:" NL
96 "\thtpasswd [-cimBdpsDv] [-C cost] passwordfile username" NL
97 "\thtpasswd -b[cmBdpsDv] [-C cost] passwordfile username password" NL
99 "\thtpasswd -n[imBdps] [-C cost] username" NL
100 "\thtpasswd -nb[mBdps] [-C cost] username password" NL
101 " -c Create a new file." NL
102 " -n Don't update file; display results on stdout." NL
103 " -b Use the password from the command line rather than prompting "
105 " -i Read password from stdin without verification (for script usage)." NL
106 " -m Force MD5 encryption of the password (default)." NL
107 " -B Force bcrypt encryption of the password (very secure)." NL
108 " -C Set the computing time used for the bcrypt algorithm" NL
109 " (higher is more secure but slower, default: %d, valid: 4 to 31)." NL
110 " -d Force CRYPT encryption of the password (8 chars max, insecure)." NL
111 " -s Force SHA encryption of the password (insecure)." NL
112 " -p Do not encrypt the password (plaintext, insecure)." NL
113 " -D Delete the specified user." NL
114 " -v Verify password for the specified user." NL
115 "On other systems than Windows and NetWare the '-p' flag will "
116 "probably not work." NL
117 "The SHA algorithm does not use a salt and is less secure than the "
125 * Check to see if the specified file can be opened for the given
128 static int accessible(apr_pool_t *pool, char *fname, int mode)
130 apr_file_t *f = NULL;
132 if (apr_file_open(&f, fname, mode, APR_OS_DEFAULT, pool) != APR_SUCCESS) {
140 * Return true if the named file exists, regardless of permissions.
142 static int exists(char *fname, apr_pool_t *pool)
147 check = apr_stat(&sbuf, fname, APR_FINFO_TYPE, pool);
148 return ((check || sbuf.filetype != APR_REG) ? 0 : 1);
151 static void terminate(void)
159 static void check_args(int argc, const char *const argv[],
160 struct passwd_ctx *ctx, unsigned *mask, char **user,
170 apr_pool_t *pool = ctx->pool;
172 rv = apr_getopt_init(&state, pool, argc, argv);
173 if (rv != APR_SUCCESS)
176 while ((rv = apr_getopt(state, "cnmspdBbDiC:v", &opt, &opt_arg)) == APR_SUCCESS) {
179 *mask |= APHTP_NEWFILE;
183 *mask |= APHTP_NOFILE;
186 *mask |= APHTP_DELUSER;
189 *mask |= APHTP_VERIFY;
192 ret = parse_common_options(ctx, opt, opt_arg);
194 apr_file_printf(errfile, "%s: %s" NL, argv[0], ctx->errstr);
199 if (ctx->passwd_src == PW_ARG)
204 if ((*mask) & (*mask - 1)) {
205 /* not a power of two, i.e. more than one flag specified */
206 apr_file_printf(errfile, "%s: only one of -c -n -v -D may be specified" NL,
210 if ((*mask & APHTP_VERIFY) && ctx->passwd_src == PW_PROMPT)
211 ctx->passwd_src = PW_PROMPT_VERIFY;
214 * Make sure we still have exactly the right number of arguments left
215 * (the filename, the username, and possibly the password if -b was
219 if ((argc - i) != args_left) {
223 if (!(*mask & APHTP_NOFILE)) {
224 if (strlen(argv[i]) > (APR_PATH_MAX - 1)) {
225 apr_file_printf(errfile, "%s: filename too long" NL, argv[0]);
228 *pwfilename = apr_pstrdup(pool, argv[i++]);
230 if (strlen(argv[i]) > (MAX_STRING_LEN - 1)) {
231 apr_file_printf(errfile, "%s: username too long (> %d)" NL,
232 argv[0], MAX_STRING_LEN - 1);
235 *user = apr_pstrdup(pool, argv[i++]);
236 if ((arg = strchr(*user, ':')) != NULL) {
237 apr_file_printf(errfile, "%s: username contains illegal "
238 "character '%c'" NL, argv[0], *arg);
241 if (ctx->passwd_src == PW_ARG) {
242 if (strlen(argv[i]) > (MAX_STRING_LEN - 1)) {
243 apr_file_printf(errfile, "%s: password too long (> %d)" NL,
244 argv[0], MAX_STRING_LEN);
247 ctx->passwd = apr_pstrdup(pool, argv[i]);
251 static int verify(struct passwd_ctx *ctx, const char *hash)
256 if (ctx->passwd == NULL && (ret = get_password(ctx)) != 0)
258 rv = apr_password_validate(ctx->passwd, hash);
259 if (rv == APR_SUCCESS)
261 if (APR_STATUS_IS_EMISMATCH(rv)) {
262 ctx->errstr = "password verification failed";
263 return ERR_PWMISMATCH;
265 ctx->errstr = apr_psprintf(ctx->pool, "Could not verify password: %pm",
271 * Let's do it. We end up doing a lot of file opening and closing,
272 * but what do we care? This application isn't run constantly.
274 int main(int argc, const char * const argv[])
276 apr_file_t *fpw = NULL;
277 char line[MAX_STRING_LEN];
278 char *pwfilename = NULL;
280 char tn[] = "htpasswd.tmp.XXXXXX";
282 char *scratch, cp[MAX_STRING_LEN];
287 int existing_file = 0;
288 struct passwd_ctx ctx = { 0 };
289 #if APR_CHARSET_EBCDIC
291 apr_xlate_t *to_ascii;
294 apr_app_initialize(&argc, &argv, NULL);
296 apr_pool_create(&pool, NULL);
297 apr_pool_abort_set(abort_on_oom, pool);
298 apr_file_open_stderr(&errfile, pool);
302 #if APR_CHARSET_EBCDIC
303 rv = apr_xlate_open(&to_ascii, "ISO-8859-1", APR_DEFAULT_CHARSET, pool);
305 apr_file_printf(errfile, "apr_xlate_open(to ASCII)->%d" NL, rv);
308 rv = apr_SHA1InitEBCDIC(to_ascii);
310 apr_file_printf(errfile, "apr_SHA1InitEBCDIC()->%d" NL, rv);
313 rv = apr_MD5InitEBCDIC(to_ascii);
315 apr_file_printf(errfile, "apr_MD5InitEBCDIC()->%d" NL, rv);
318 #endif /*APR_CHARSET_EBCDIC*/
320 check_args(argc, argv, &ctx, &mask, &user, &pwfilename);
323 * Only do the file checks if we're supposed to frob it.
325 if (!(mask & APHTP_NOFILE)) {
326 existing_file = exists(pwfilename, pool);
329 * Check that this existing file is readable and writable.
331 if (!accessible(pool, pwfilename, APR_FOPEN_READ|APR_FOPEN_WRITE)) {
332 apr_file_printf(errfile, "%s: cannot open file %s for "
333 "read/write access" NL, argv[0], pwfilename);
339 * Error out if -c was omitted for this non-existant file.
341 if (!(mask & APHTP_NEWFILE)) {
342 apr_file_printf(errfile,
343 "%s: cannot modify file %s; use '-c' to create it" NL,
344 argv[0], pwfilename);
348 * As it doesn't exist yet, verify that we can create it.
350 if (!accessible(pool, pwfilename, APR_FOPEN_WRITE|APR_FOPEN_CREATE)) {
351 apr_file_printf(errfile, "%s: cannot create file %s" NL,
352 argv[0], pwfilename);
359 * All the file access checks (if any) have been made. Time to go to work;
360 * try to create the record for the username in question. If that
361 * fails, there's no need to waste any time on file manipulations.
362 * Any error message text is returned in the record buffer, since
363 * the mkrecord() routine doesn't have access to argv[].
365 if ((mask & (APHTP_DELUSER|APHTP_VERIFY)) == 0) {
366 i = mkrecord(&ctx, user);
368 apr_file_printf(errfile, "%s: %s" NL, argv[0], ctx.errstr);
371 if (mask & APHTP_NOFILE) {
372 printf("%s" NL, ctx.out);
377 if ((mask & APHTP_VERIFY) == 0) {
379 * We can access the files the right way, and we have a record
380 * to add or update. Let's do it..
382 if (apr_temp_dir_get((const char**)&dirname, pool) != APR_SUCCESS) {
383 apr_file_printf(errfile, "%s: could not determine temp dir" NL,
387 dirname = apr_psprintf(pool, "%s/%s", dirname, tn);
389 if (apr_file_mktemp(&ftemp, dirname, 0, pool) != APR_SUCCESS) {
390 apr_file_printf(errfile, "%s: unable to create temporary file %s" NL,
397 * If we're not creating a new file, copy records from the existing
398 * one to the temporary file until we find the specified user.
400 if (existing_file && !(mask & APHTP_NEWFILE)) {
401 if (apr_file_open(&fpw, pwfilename, APR_READ | APR_BUFFERED,
402 APR_OS_DEFAULT, pool) != APR_SUCCESS) {
403 apr_file_printf(errfile, "%s: unable to read file %s" NL,
404 argv[0], pwfilename);
407 while (apr_file_gets(line, sizeof(line), fpw) == APR_SUCCESS) {
412 while (apr_isspace(*scratch)) {
416 if (!*scratch || (*scratch == '#')) {
417 putline(ftemp, line);
421 * See if this is our user.
423 colon = strchr(scratch, ':');
429 * If we've not got a colon on the line, this could well
430 * not be a valid htpasswd file.
431 * We should bail at this point.
433 apr_file_printf(errfile, "%s: The file %s does not appear "
434 "to be a valid htpasswd file." NL,
435 argv[0], pwfilename);
439 if (strcmp(user, scratch) != 0) {
440 putline(ftemp, line);
444 /* We found the user we were looking for */
446 if ((mask & APHTP_DELUSER)) {
447 /* Delete entry from the file */
448 apr_file_printf(errfile, "Deleting ");
450 else if ((mask & APHTP_VERIFY)) {
452 char *hash = colon + 1;
455 len = strcspn(hash, "\r\n");
457 apr_file_printf(errfile, "Empty hash for user %s" NL,
463 i = verify(&ctx, hash);
465 apr_file_printf(errfile, "%s" NL, ctx.errstr);
471 apr_file_printf(errfile, "Updating ");
472 putline(ftemp, ctx.out);
479 if (mask & APHTP_DELUSER) {
480 apr_file_printf(errfile, "User %s not found" NL, user);
483 else if (mask & APHTP_VERIFY) {
484 apr_file_printf(errfile, "User %s not found" NL, user);
488 apr_file_printf(errfile, "Adding ");
489 putline(ftemp, ctx.out);
492 if (mask & APHTP_VERIFY) {
493 apr_file_printf(errfile, "Password for user %s correct." NL, user);
497 apr_file_printf(errfile, "password for user %s" NL, user);
499 /* The temporary file has all the data, just copy it to the new location.
501 if (apr_file_copy(dirname, pwfilename, APR_FILE_SOURCE_PERMS, pool) !=
503 apr_file_printf(errfile, "%s: unable to update file %s" NL,
504 argv[0], pwfilename);
507 apr_file_close(ftemp);