#! /usr/bin/env perl # WARNING: This is insecure as it will leave the users password in the # bash history file use strict; use warnings; my $pwfile = '/srv/vmail/passwd'; # location of the password file my $user = shift; my $password = shift; die "Usage: $0 username password\n" unless $user && $password; my $found = 0; # determines if user already exists # call doveadm to get the hash my $key = `/usr/bin/doveadm pw -s ssha256 -u '$user' -p '$password'`; # read the password file open PW,"<$pwfile" or die "Could not open the password file: $!\n"; my @data = ; close PW; # go through it and see if the user already exists my $newLine = "$user:$key"; for ( my $line = 0; $line < @data; $line++ ) { my ($thisUser,$thisPass) = split( ':', $data[$line] ); if ( $thisUser eq $user ) { # yes, so replace the line and mark found $data[$line] = $newLine; $found = 1; last; } # if statement } # for loop push @data, $newLine unless $found; # we did not find them, so add chomp @data; # remove all line endings # write the file back out open PW,">$pwfile" or die "Could not write the password file: $!\n"; print PW join( "\n", @data ) . "\n"; close PW; # tell user what we did print "User $user "; print $found ? "modified\n" : "added\n"; 1;