diff options
author | Christian Pointner <equinox@spreadspace.org> | 2015-09-30 15:07:41 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2015-09-30 15:07:41 (GMT) |
commit | 5897b1bc3b059a29f57bde6c08344deceb41bd23 (patch) | |
tree | f2ca2ba16403ac0d65286fc1b5e3d9783440b32b /utils/rd-user | |
parent | efb5d6557591ef21c27aa46aae220d862c36feeb (diff) |
major cleanup for all utils
Diffstat (limited to 'utils/rd-user')
-rwxr-xr-x | utils/rd-user | 119 |
1 files changed, 92 insertions, 27 deletions
diff --git a/utils/rd-user b/utils/rd-user index 6b191be..2a032dd 100755 --- a/utils/rd-user +++ b/utils/rd-user @@ -30,51 +30,116 @@ if ($> != 0 ) { exit 1; } -my $num_args = $#ARGV + 1; -if ($num_args != 2 && $num_args !=3) { - print STDERR "Usage: rd-user (check|add|remove) <username> [ fullname ]\n"; - exit 1; +sub print_usage +{ + print STDERR "Usage: rd-user list\n" . + " rd-user (check|remove) <username>\n" . + " rd-user add <username> [ <fullname> ]\n"; +} + +sub list +{ + my ($dbh) = @_; + + my @users = RHRD::rddb::list_users($dbh); + if(!defined $users[0] && defined $users[1]) { + print STDERR "$users[1]: $users[2]"; + return 1; + } + for my $user (@users) { + print $user . "\n"; + } + return 0; } +sub check +{ + my ($dbh, $username) = @_; + + my ($result, $status, $errorstring) = RHRD::rddb::check_user($dbh, $username); + print STDERR "$result, $status: $errorstring\n"; + return $result; +} + +sub add +{ + my ($dbh, $username, $fullname) = @_; + + my $token = mkpasswd(-length => 16, -minnum => 3, -minupper => 3, -minspecial => 0); + my ($cnt, undef, $errorstring) = RHRD::rddb::add_user($dbh, $username, $token, $fullname); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print int($cnt) . " rows affected\n"; + return 0; +} + +sub remove +{ + my ($dbh, $username) = @_; + + my @results = RHRD::rddb::remove_user($dbh, $username); + if(!defined $results[0] && defined $results[2]) { + print STDERR "$results[2]\n"; + return 1; + } + for my $href (@results) { + print int($href->{cnt}) . " " . $href->{name} . " deleted\n"; + } + return 0; +} + + +my $num_args = $#ARGV + 1; +if($num_args < 1) { + print_usage(); + exit(1); +} my $cmd = $ARGV[0]; my $username = $ARGV[1]; -my $fullname = $ARGV[2]; +my $ret = 0; my ($dbh, undef, $errorstring) = RHRD::rddb::opendb(); if(defined $dbh) { - if($cmd eq "check") { - (my $result, my $status, $errorstring) = RHRD::rddb::check_user($dbh, $username); - print STDERR "$result, $status: $errorstring\n"; - RHRD::rddb::closedb($dbh); - exit $result; + if($cmd eq "list") { + if($num_args != 1) { + print_usage(); + $ret = 1; + } else { + $ret = list($dbh); + } + } + elsif($cmd eq "check") { + if($num_args != 2) { + print_usage(); + $ret = 1; + } else { + $ret = check($dbh, $username); + } } elsif($cmd eq "add") { - my $token = mkpasswd(-length => 16, -minnum => 3, -minupper => 3, -minspecial => 0); - (my $cnt, undef, $errorstring) = RHRD::rddb::add_user($dbh, $username, $token, $fullname); - unless(defined $cnt) { - print STDERR "$errorstring\n"; - RHRD::rddb::closedb($dbh); - exit 1; + if($num_args < 2 || $num_args > 3) { + print_usage(); + $ret = 1; + } else { + $ret = add($dbh, $username, $ARGV[2]); } - print int($cnt) . " rows affected\n"; } elsif($cmd eq "remove") { - my @results = RHRD::rddb::remove_user($dbh, $username); - if(!defined $results[0] && defined $results[2]) { - print STDERR "$results[2]\n"; + if($num_args != 2) { + print_usage(); + $ret = 1; } else { - for my $href (@results) { - print int($href->{cnt}) . " " . $href->{name} . " deleted\n"; - } + $ret = remove($dbh, $username); } } else { print STDERR "unknown command\n"; - RHRD::rddb::closedb($dbh); - exit 1; + $ret = 1; } RHRD::rddb::closedb($dbh); } else { print STDERR "$errorstring\n"; - exit 1; + $ret = 1; } -exit 0 +exit $ret; |