Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to protect a sub domain where the user is only given access for 1 day. Is it possible to set up a cron job that will automatically update a .htpasswd file with a new password daily?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You should probably use an application that was meant to do this, rather than hacking something together with cron. But, if you must make it hackalicious...

You'll have to run htpasswd in "batch mode". From the htpasswd man page:

htpasswd -mb /usr/web/.htpasswd-all jones Pwd4Steve

          Encrypts the password from the command line (Pwd4Steve)
          using the MD5 algorithm, and stores it in the specified
          file.

Something like this should work:

FILE=/path/to/your/htaccess/file
# There are a bunch of ways to get a new password, this is one:
NEW_PASS=`cat /dev/urandom|tr -dc "a-zA-Z0-9-_\$\?"|fold -w 9|head -n1`
HTPASSWD=/usr/bin/htpasswd
WEB_USER=samuel_l_jackson
# This should do it
$HTPASSWD -b $FILE $WEB_USER $NEW_PASS
# You'll probably want to email the password to your user, 
# so they can actually use the new password
share|improve this answer

Normally I would only answer: Yes it is possible.

But you can even integrate a database where the user and password is stored. For example mod_auth_mysql. And then you can even enable/disable/modify everything with standard SQL. I'll only leave a hint to this question and the homepage. Sometimes this approach does not work but in this case you could go around this by replacing mod_auth_mysql with mod_auth_pam and then integrate the MySQL stuff into PAM. But I'll let this as an exercise for the reader...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.