The problem
I'm provisioning laboratory of about 10 computers, the problems are:
- We will scrape old and add new computers fairly regulary
- There are more than one admin of this system
For now I kept admin's passwords in plaintet, well all except mine, that wasn't stored since I set up all computers, and set my password by hand. And storing admin passwords in plaintext (even on secure servers) is bad.
Generating crypt hashes
Linux stores password as hashed values, but these hashes are not 'off the shelf`, basically most modern distributions use SHA-512 hashes, with own magic changes. So only way to get your password hashed is to call crypt function.
Good thing is that python provides own wrappers for this so I didn't have to write glue code to C.
import crypt;
result = crypt.crypt('password', 'salt');
Inserting crypt hashes into shadow database
This is a bit trickier, python has binding for both /etc/passwd and /etc/shadow databases, but unfortunately these are read only.
Howewer there is the
chpasswd command, that allows one to specify encrypted passwords for users. It expects standard input that contains lines with format {username}:{password}, one pair per line.
If you call it with -e switch it will treat passed values as valid hashes and insert into /etc/shadow without changes.
For example:
echo "test:$6$23IagSE3$z.EZe5H1pfHCHMWw8UYqTpEGyS7apgoOBL5sG27/adt5CBC44LrLygywwRtvReY7lMlGj82SgQgjcP6OjE7Cf1" > chpasswd -e