8

I want to be able to script with a password in variable $password.

htdigest -c /etc/apache2/pw/$user $user $password

At the moment it asks me to manually enter a password; this is not going to be possible since it will be an automated setup by a PHP script. Could someone please show me how to script this? Or an equivalent scriptable function?

8

See https://stackoverflow.com/questions/645659/how-do-you-htdigest-400-user-accounts

The easiest method, based on one of the suggestions in the top-voted answer, is probably this:

digest="$( printf "%s:%s:%s" "$user" "$realm" "$password" | 
           md5sum | awk '{print $1}' )"

printf "%s:%s:%s\n" "$user" "$realm" "$digest" >> "/etc/apache2/pw/$user"

I've used md5sum from GNU coreutils and awk rather than just md5 because it's what i have installed on my system and I couldn't be bothered finding out which package contains /usr/bin/md5 - you could also use sha512sum or other hashing program.

e.g. if user=foo, realm=bar, and password=baz then the command above will produce:

foo:bar:5bf2a4095f681d1c674655a55af66c5a

htdigest doesn't do anything magical or even unusual - it just outputs the user, realm, and password in the right format...as the command above does.

Deleting the digest for a given user:realm instead of just adding one, can easily be done with sed.

sed -i -e "/^$user:$realm:/d" "/etc/apache2/pw/$user"

And updating/changing the digest for a user:realm can also be done with sed in combination with the method above to generate the digest line. e.g.

digest="$( printf "%s:%s:%s" "$user" "$realm" "$new_password" | 
           md5sum | awk '{print $1}' )"

sed -i -e "/^$user:$realm:/ c$user:$realm:$digest" "/etc/apache2/pw/$user"
  • awesome :P had some issues when the file didn't already exist, so i create the file before running this :o – Mr. King Aug 21 '12 at 11:27
0

Check the 'proc_open' function in PHP http://fr.php.net/manual/en/function.proc-open.php

You can run htdigest with your option, then send in the pipes the new password.

You can probably found/create a PHP function which do the htdigest job. It should be easier thant use proc_open...

  • it's much more complex than just making a new apache user :p using php is not an option – Mr. King Aug 21 '12 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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