139139#define APHTP_NEWFILE 1
140140#define APHTP_NOFILE 2
141141#define APHTP_NONINTERACTIVE 4
142+ #define APHTP_DELUSER 8
142143
143144apr_file_t * errfile ;
144145apr_file_t * ftemp = NULL ;
@@ -245,8 +246,8 @@ static int mkrecord(char *user, char *record, apr_size_t rlen, char *passwd,
245246static 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