123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- #!/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(<X>) {
- 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 = <STDIN>; chomp $str;
-
- return $str if $str =~ /$re/;
- return undef;
- }
|