Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

See http://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:

(echo -n "$user:$realm:" && echo -n "$user:$realm:$password" | md5sum | awk '{print $1}' ) >> /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 has /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. 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.

DIGESTLINE=$(echo -n "$user:$realm:" && echo -n "$user:$realm:$password" | md5sum | awk '{print $1}')

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

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...

share|improve this answer
    
it's much more complex than just making a new apache user :p using php is not an option –  King Aug 21 '12 at 11:26

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.