#!/usr/bin/perl use strict; use lib '/opt/autodoc/lib'; use Autodoc; use warnings; my $conf = load_conf('/opt/autodoc/etc/autodoc.json'); my $dbh = sqlconnect($conf->{sql}); while(1) { print "User management\n"; print "(l)ist, (a)dd, (d)elete, (q)uit\n"; print "? "; my $m = input('^[ladq]$'); if ( !defined $m ) { print "ERROR: Invalid input\n"; } elsif ( $m eq 'l' ) { foreach my $user ( list_users() ) { print "User: $user\n"; } } elsif ( $m eq 'a' ) { my ($user, $pass); while(!defined $user) { print "Username: "; $user = input('^[0-9a-zA-Z-]+$'); print "Invalid username\n" if !defined $user; } while(!defined $pass) { print "Password: "; $pass = input('^[0-9a-zA-Z-]+$'); print "Invalid password\n" if !defined $pass; } create_user($user,$pass); } elsif ( $m eq 'd' ) { print "Username to delete: "; my $user = input('^[0-9a-zA-Z-]+$'); delete_user($user); } elsif ( $m eq 'q' ) { print "Bye\n"; exit; } } sub create_user { my($user,$pass) = @_; my $crypt; open(X,"htpasswd -nbB '$user' '$pass' |"); while() { chomp; (undef, $crypt)=split(/:/) if /:/; } close(X); if ( defined $crypt ) { sqlquery($dbh, "INSERT INTO users SET username = ?, passwd = ?", $user, $crypt); } } sub delete_user { my($user) = @_; sqlquery($dbh, "DELETE FROM users WHERE username = ?", $user); } sub list_users { my @users; my $q = sqlquery($dbh, "SELECT username FROM users ORDER BY username"); while(my ($user) = $q->fetchrow_array()) { push @users, $user; } return @users; } sub input { my($re) = @_; my $str = ; chomp $str; return $str if $str =~ /$re/; return undef; }