Skip to content

Commit 5527171

Browse files
author
Thom May
committed
Add a delete flag to htpasswd.
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@99828 13f79535-47bb-0310-9956-ffa450edef68
1 parent 264cf54 commit 5527171

3 files changed

Lines changed: 54 additions & 19 deletions

File tree

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ Changes with Apache 2.1.0-dev
22

33
[Remove entries to the current 2.0 section below, when backported]
44

5+
*) Add a delete flag to htpasswd.
6+
[Thom May]
7+
58
*) Ensure that ssl-std.conf is generated at configure time, and switch
69
to using the expanded config variables to work the same as
710
httpd-std.conf PR 19611

docs/man/htpasswd.1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ htpasswd \- Manage user files for basic authentication
2727
.SH "SYNOPSIS"
2828

2929
.PP
30-
\fBhtpasswd\fR [ -\fBc\fR ] [ -\fBm\fR ] \fIpasswdfile\fR \fIusername\fR
30+
\fBhtpasswd\fR [ -\fBc\fR ] [ -\fBm\fR ] [ -\fBD\fB ] \fIpasswdfile\fR \fIusername\fR
3131

3232
.PP
33-
\fBhtpasswd\fR -\fBb\fR [ -\fBc\fR ] [ -\fBm\fR | -\fBd\fR | -\fBp\fR | -\fBs\fR ] \fIpasswdfile\fR \fIusername\fR \fIpassword\fR
33+
\fBhtpasswd\fR -\fBb\fR [ -\fBc\fR ] [ -\fBm\fR | -\fBd\fR | -\fBp\fR | -\fBs\fR ] [ -\fBD\fB ] \fIpasswdfile\fR \fIusername\fR \fIpassword\fR
3434

3535
.PP
3636
\fBhtpasswd\fR -\fBn\fR [ -\fBm\fR | -\fBd\fR | -\fBs\fR | -\fBp\fR ] \fIusername\fR
@@ -80,6 +80,9 @@ Use SHA encryption for passwords\&. Facilitates migration from/to Netscape serve
8080
-p
8181
Use plaintext passwords\&. Though htpasswd will support creation on all platforms, the httpd daemon will only accept plain text passwords on Windows, Netware and TPF\&.
8282
.TP
83+
-D
84+
Delete user\&. If the username exists in the specified htpasswd file, it will be deleted\&.
85+
.TP
8386
\fIpasswdfile\fR
8487
Name of the file to contain the user name and password\&. If -c is given, this file is created if it does not already exist, or rewritten and truncated if it does exist\&.
8588
.TP

support/htpasswd.c

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
#define APHTP_NEWFILE 1
140140
#define APHTP_NOFILE 2
141141
#define APHTP_NONINTERACTIVE 4
142+
#define APHTP_DELUSER 8
142143

143144
apr_file_t *errfile;
144145
apr_file_t *ftemp = NULL;
@@ -245,8 +246,8 @@ static int mkrecord(char *user, char *record, apr_size_t rlen, char *passwd,
245246
static void usage(void)
246247
{
247248
apr_file_printf(errfile, "Usage:\n");
248-
apr_file_printf(errfile, "\thtpasswd [-cmdps] passwordfile username\n");
249-
apr_file_printf(errfile, "\thtpasswd -b[cmdps] passwordfile username "
249+
apr_file_printf(errfile, "\thtpasswd [-cmdpsD] passwordfile username\n");
250+
apr_file_printf(errfile, "\thtpasswd -b[cmdpsD] passwordfile username "
250251
"password\n\n");
251252
apr_file_printf(errfile, "\thtpasswd -n[mdps] username\n");
252253
apr_file_printf(errfile, "\thtpasswd -nb[mdps] username password\n");
@@ -267,6 +268,7 @@ static void usage(void)
267268
apr_file_printf(errfile, " -s Force SHA encryption of the password.\n");
268269
apr_file_printf(errfile, " -b Use the password from the command line "
269270
"rather than prompting for it.\n");
271+
apr_file_printf(errfile, " -D Delete the specified user.\n");
270272
apr_file_printf(errfile,
271273
"On Windows, NetWare and TPF systems the '-m' flag is used by "
272274
"default.\n");
@@ -359,6 +361,9 @@ static void check_args(apr_pool_t *pool, int argc, const char *const argv[],
359361
*mask |= APHTP_NONINTERACTIVE;
360362
args_left++;
361363
}
364+
else if (*arg == 'D') {
365+
*mask |= APHTP_DELUSER;
366+
}
362367
else {
363368
usage();
364369
}
@@ -369,6 +374,14 @@ static void check_args(apr_pool_t *pool, int argc, const char *const argv[],
369374
apr_file_printf(errfile, "%s: -c and -n options conflict\n", argv[0]);
370375
exit(ERR_SYNTAX);
371376
}
377+
if ((*mask & APHTP_NEWFILE) && (*mask & APHTP_DELUSER)) {
378+
apr_file_printf(errfile, "%s: -c and -D options conflict\n", argv[0]);
379+
exit(ERR_SYNTAX);
380+
}
381+
if ((*mask & APHTP_NOFILE) && (*mask & APHTP_DELUSER)) {
382+
apr_file_printf(errfile, "%s: -n and -D options conflict\n", argv[0]);
383+
exit(ERR_SYNTAX);
384+
}
372385
/*
373386
* Make sure we still have exactly the right number of arguments left
374387
* (the filename, the username, and possibly the password if -b was
@@ -532,15 +545,17 @@ int main(int argc, const char * const argv[])
532545
* Any error message text is returned in the record buffer, since
533546
* the mkrecord() routine doesn't have access to argv[].
534547
*/
535-
i = mkrecord(user, record, sizeof(record) - 1,
536-
password, alg);
537-
if (i != 0) {
538-
apr_file_printf(errfile, "%s: %s\n", argv[0], record);
539-
exit(i);
540-
}
541-
if (mask & APHTP_NOFILE) {
542-
printf("%s\n", record);
543-
exit(0);
548+
if (!(mask & APHTP_DELUSER)) {
549+
i = mkrecord(user, record, sizeof(record) - 1,
550+
password, alg);
551+
if (i != 0) {
552+
apr_file_printf(errfile, "%s: %s\n", argv[0], record);
553+
exit(i);
554+
}
555+
if (mask & APHTP_NOFILE) {
556+
printf("%s\n", record);
557+
exit(0);
558+
}
544559
}
545560

546561
/*
@@ -597,19 +612,33 @@ int main(int argc, const char * const argv[])
597612
continue;
598613
}
599614
else {
600-
/* We found the user we were looking for, add him to the file.
601-
*/
602-
apr_file_printf(errfile, "Updating ");
603-
putline(ftemp, record);
604-
found++;
615+
if (!(mask & APHTP_DELUSER)) {
616+
/* We found the user we were looking for.
617+
* Add him to the file.
618+
*/
619+
apr_file_printf(errfile, "Updating ");
620+
putline(ftemp, record);
621+
found++;
622+
}
623+
else {
624+
/* We found the user we were looking for.
625+
* Delete them from the file.
626+
*/
627+
apr_file_printf(errfile, "Deleting ");
628+
found++;
629+
}
605630
}
606631
}
607632
apr_file_close(fpw);
608633
}
609-
if (!found) {
634+
if (!found && !(mask & APHTP_DELUSER)) {
610635
apr_file_printf(errfile, "Adding ");
611636
putline(ftemp, record);
612637
}
638+
else if (!found && (mask & APHTP_DELUSER)) {
639+
apr_file_printf(errfile, "User %s not found\n", user);
640+
exit(0);
641+
}
613642
apr_file_printf(errfile, "password for user %s\n", user);
614643

615644
/* The temporary file has all the data, just copy it to the new location.

0 commit comments

Comments
 (0)