From 5897b1bc3b059a29f57bde6c08344deceb41bd23 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 30 Sep 2015 17:07:41 +0200 Subject: major cleanup for all utils diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm index 3957459..894c03e 100755 --- a/lib/RHRD/rddb.pm +++ b/lib/RHRD/rddb.pm @@ -575,13 +575,26 @@ sub add_group_member { my ($dbh, $groupname, $username) = @_; - my ($cnt, $result, $errostring) = is_group_member($dbh, $groupname, $username); + my $sql = qq{select count(*) from USERS where LOGIN_NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); + + $sth->execute($username) + or return (undef, 'ERROR', "Database Error: " . $sth->errstr); + + my ($cnt) = $sth->fetchrow_array(); + $sth->finish(); + if($cnt < 1) { + return (undef, 'ERROR', "user '" . $username . "' does not exist"); + } + + ($cnt, my $result, my $errostring) = is_group_member($dbh, $groupname, $username); if($cnt > 0) { return (undef, 'ERROR', "already a member"); } - my $sql = qq{insert into USER_PERMS (GROUP_NAME, USER_NAME) values (?, ?);}; - my $sth = $dbh->prepare($sql) + $sql = qq{insert into USER_PERMS (GROUP_NAME, USER_NAME) values (?, ?);}; + $sth = $dbh->prepare($sql) or return (undef, 'ERROR', "Database Error: " . $dbh->errstr); $cnt = $sth->execute($groupname, $username) diff --git a/utils/get-rd-token b/utils/get-rd-token index 6965cab..b2b4dce 100755 --- a/utils/get-rd-token +++ b/utils/get-rd-token @@ -44,20 +44,21 @@ if($num_args == 1) { } my $token=''; +my $ret = 0; my ($dbh, undef, $errorstring) = RHRD::rddb::opendb(); if(defined $dbh) { ($token, undef, $errorstring) = RHRD::rddb::get_token($dbh, $requsername); unless($token) { print STDERR "$errorstring\n"; - exit 1; + $ret = 1; } RHRD::rddb::closedb($dbh); } else { print STDERR "$errorstring\n"; - exit 1; + $ret = 1; } -print "$token\n"; +print "$token\n" if $token; -exit 0 +exit $ret; diff --git a/utils/rd-group b/utils/rd-group index c72e7d2..ecf10cb 100755 --- a/utils/rd-group +++ b/utils/rd-group @@ -29,120 +29,292 @@ if ($> != 0 ) { exit 1; } -my $num_args = $#ARGV + 1; -if ($num_args < 2) { - print STDERR "Usage: rd-group (check|remove|get-members|get-carts|get-reports) \n" . +sub print_usage +{ + print STDERR "Usage: rd-group list\n" . + " rd-group (check|remove|get-members|get-carts|get-reports) \n" . " rd-group add [ ]\n" . " rd-group (add-member|remove-member|is-member) \n" . " rd-group set-carts [ [ [ [ ]]]\n" . " rd-group set-reports [ [ [ ]]]\n"; - exit 1; } +sub list +{ + my ($dbh) = @_; + + my @groups = RHRD::rddb::list_groups($dbh); + if(!defined $groups[0] && defined $groups[1]) { + print STDERR "$groups[1]: $groups[2]"; + return 1; + } + for my $group (@groups) { + print $group . "\n"; + } + return 0; +} + +sub check +{ + my ($dbh, $groupname) = @_; + + my ($result, $status, $errorstring) = RHRD::rddb::check_group($dbh, $groupname); + print STDERR "$result, $status: $errorstring\n"; + return $result; +} + +sub add +{ + my ($dbh, $groupname, $description) = @_; + + my ($cnt, $status, $errorstring) = RHRD::rddb::add_group($dbh, $groupname, $description); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print int($cnt) . " rows affected\n"; + return 0; +} + +sub remove +{ + my ($dbh, $groupname) = @_; + + my @results = RHRD::rddb::remove_group($dbh, $groupname); + 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; +} + +sub get_members +{ + my ($dbh, $groupname) = @_; + + my @users = RHRD::rddb::get_group_members($dbh, $groupname); + if(!defined $users[0] && defined $users[1]) { + print STDERR "$users[2]\n"; + return 1; + } + for my $user (sort @users) { + print "$user\n"; + } + return 0; +} + +sub add_member +{ + my ($dbh, $groupname, $username) = @_; + + my ($cnt, undef, $errorstring) = RHRD::rddb::add_group_member($dbh, $groupname, $username); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print int($cnt) . " rows affected\n"; + return 0; +} + +sub remove_member +{ + my ($dbh, $groupname, $username) = @_; + + my ($cnt, undef, $errorstring) = RHRD::rddb::remove_group_member($dbh, $groupname, $username); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print int($cnt) . " rows affected\n"; + return 0; +} + +sub is_member +{ + my ($dbh, $groupname, $username) = @_; + + my ($cnt, undef, $errorstring) = RHRD::rddb::is_group_member($dbh, $groupname, $username); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print $ARGV[2] . " is" . (($cnt) ? "" : " not") . " a member\n"; + return (($cnt) ? 0 : 1); +} + +sub get_carts +{ + my ($dbh, $groupname) = @_; + + my ($low, $high, $type, $enforce_range) = RHRD::rddb::get_group_carts($dbh, $groupname); + unless(defined $low) { + print STDERR "$type\n"; + return 1; + } + print "Range: " . int($low) . " - " . int($high) . ", Type: " . int($type) . ", Enforce Range: " . $enforce_range . "\n"; + return 0; +} + +sub set_carts +{ + my ($dbh, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range) = @_; + + my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_carts($dbh, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print int($cnt) . " rows affected\n"; + return 0; +} + +sub get_reports +{ + my ($dbh, $groupname) = @_; + + my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($dbh, $groupname); + unless(defined $nownext) { + print STDERR "$music\n"; + return 1; + } + print "Now-Next: " . $nownext . ", Traffic: " . $traffic . ", Music: " . $music . "\n"; + return 0; +} + +sub set_reports +{ + my ($dbh, $groupname, $now_next, $traffic, $music) = @_; + + my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_reports($dbh, $groupname, $now_next, $traffic, $music); + unless(defined $cnt) { + print STDERR "$errorstring\n"; + return 1; + } + print int($cnt) . " rows affected\n"; + + return 0; +} + + +my $num_args = $#ARGV + 1; +if($num_args < 1) { + print_usage(); + exit(1); +} my $cmd = $ARGV[0]; my $groupname = $ARGV[1]; +my $ret = 0; -my ($dbh, undef, $errorstring) = RHRD::rddb::opendb(); +my ($dbh, $status, $errorstring) = RHRD::rddb::opendb(); if(defined $dbh) { - if($cmd eq "check") { - (my $result, my $status, $errorstring) = RHRD::rddb::check_group($dbh, $groupname); - print STDERR "$result, $status: $errorstring\n"; - RHRD::rddb::closedb($dbh); - exit $result; - } elsif($cmd eq "add") { - (my $cnt, undef, $errorstring) = RHRD::rddb::add_group($dbh, $groupname, $ARGV[2]); - unless(defined $cnt) { - print STDERR "$errorstring\n"; - RHRD::rddb::closedb($dbh); - exit 1; + if($cmd eq "list") { + if($num_args != 1) { + print_usage(); + $ret = 1; + } else { + $ret = list($dbh); } - print int($cnt) . " rows affected\n"; - } elsif($cmd eq "remove") { - my @results = RHRD::rddb::remove_group($dbh, $groupname); - if(!defined $results[0] && defined $results[2]) { - print STDERR "$results[2]\n"; + } + elsif($cmd eq "check") { + if($num_args != 2) { + print_usage(); + $ret = 1; + } else { + $ret = check($dbh, $groupname); + } + } + elsif($cmd eq "add") { + if($num_args < 2 || $num_args > 3) { + print_usage(); + $ret = 1; } else { - for my $href (@results) { - print int($href->{cnt}) . " " . $href->{name} . " deleted\n"; - } + $ret = add($dbh, $groupname, $ARGV[2]); } - } elsif($cmd eq "get-members") { - my @users = RHRD::rddb::get_group_members($dbh, $groupname); - if(!defined $users[0] && defined $users[1]) { - print STDERR "$users[2]\n"; - RHRD::rddb::closedb($dbh); - exit 1; + } + elsif($cmd eq "remove") { + if($num_args != 2) { + print_usage(); + $ret = 1; + } else { + $ret = remove($dbh, $groupname); } - for my $user (sort @users) { - print "$user\n"; + } + elsif($cmd eq "get-members") { + if($num_args != 2) { + print_usage(); + $ret = 1; + } else { + $ret = get_members($dbh, $groupname); } - } elsif($cmd eq "add-member") { - (my $cnt, undef, $errorstring) = RHRD::rddb::add_group_member($dbh, $groupname, $ARGV[2]); - unless(defined $cnt) { - print STDERR "$errorstring\n"; - RHRD::rddb::closedb($dbh); - exit 1; + } + elsif($cmd eq "add-member") { + if($num_args != 3) { + print_usage(); + $ret = 1; + } else { + $ret = add_member($dbh, $groupname, $ARGV[2]); } - print int($cnt) . " rows affected\n"; - } elsif($cmd eq "remove-member") { - (my $cnt, undef, $errorstring) = RHRD::rddb::remove_group_member($dbh, $groupname, $ARGV[2]); - unless(defined $cnt) { - print STDERR "$errorstring\n"; - RHRD::rddb::closedb($dbh); - exit 1; + } + elsif($cmd eq "remove-member") { + if($num_args != 3) { + print_usage(); + $ret = 1; + } else { + $ret = remove_member($dbh, $groupname, $ARGV[2]); } - print int($cnt) . " rows affected\n"; - } elsif($cmd eq "is-member") { - (my $cnt, undef, $errorstring) = RHRD::rddb::is_group_member($dbh, $groupname, $ARGV[2]); - RHRD::rddb::closedb($dbh); - unless(defined $cnt) { - print STDERR "$errorstring\n"; - exit 1; + } + elsif($cmd eq "is-member") { + if($num_args != 3) { + print_usage(); + $ret = 1; + } else { + $ret = is_member($dbh, $groupname, $ARGV[2]); } - print $ARGV[2] . " is" . (($cnt) ? "" : " not") . " a member\n"; - exit (($cnt) ? 0 : 1); - } elsif($cmd eq "get-carts") { - my ($low, $high, $type, $enforce_range) = RHRD::rddb::get_group_carts($dbh, $groupname); - unless(defined $low) { - print STDERR "$type\n"; - RHRD::rddb::closedb($dbh); - exit 1; + } + elsif($cmd eq "get-carts") { + if($num_args != 2) { + print_usage(); + $ret = 1; + } else { + $ret = get_carts($dbh, $groupname); } - print "Range: " . int($low) . " - " . int($high) . ", Type: " . int($type) . ", Enforce Range: " . $enforce_range . "\n"; - } elsif($cmd eq "set-carts") { - (my $cnt, undef, $errorstring) = RHRD::rddb::set_group_carts($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5]); - unless(defined $cnt) { - print STDERR "$errorstring\n"; - RHRD::rddb::closedb($dbh); - exit 1; + } + elsif($cmd eq "set-carts") { + if($num_args < 2 || $num_args > 6) { + print_usage(); + $ret = 1; + } else { + $ret = set_carts($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5]); } - print int($cnt) . " rows affected\n"; - } elsif($cmd eq "get-reports") { - my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($dbh, $groupname); - unless(defined $nownext) { - print STDERR "$music\n"; - RHRD::rddb::closedb($dbh); - exit 1; + } + elsif($cmd eq "get-reports") { + if($num_args != 2) { + print_usage(); + $ret = 1; + } else { + $ret = get_reports($dbh, $groupname); } - print "Now-Next: " . $nownext . ", Traffic: " . $traffic . ", Music: " . $music . "\n"; - } elsif($cmd eq "set-reports") { - (my $cnt, undef, $errorstring) = RHRD::rddb::set_group_reports($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4]); - unless(defined $cnt) { - print STDERR "$errorstring\n"; - RHRD::rddb::closedb($dbh); - exit 1; + } + elsif($cmd eq "set-reports") { + if($num_args < 2 || $num_args > 5) { + print_usage(); + $ret = 1; + } else { + $ret = set_reports($dbh, $groupname, $ARGV[2], $ARGV[3], $ARGV[4]); } - print int($cnt) . " rows affected\n"; - } else { + } + 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; diff --git a/utils/rd-show b/utils/rd-show index d2e69c4..ff5a169 100755 --- a/utils/rd-show +++ b/utils/rd-show @@ -37,7 +37,7 @@ sub print_usage " rd-show add <num-carts> <rhythm> <dow> <starttime> <len>\n"; } -sub list_shows +sub list { my ($dbh) = @_; @@ -52,7 +52,7 @@ sub list_shows return 0; } -sub show_show +sub show { my ($dbh, $show_id) = @_; @@ -73,7 +73,7 @@ sub show_show return 0; } -sub add_show +sub add { my ($dbh, $shortname, $title, $numcarts, $rhythm, $dow, $starttime, $len) = @_; @@ -81,7 +81,7 @@ sub add_show return 0; } -sub remove_show +sub remove { my ($dbh, $show_id) = @_; @@ -102,40 +102,43 @@ if(defined $dbh) { if($cmd eq "list") { if($num_args != 1) { print_usage(); - exit(1); + $ret = 1; + } else { + $ret = list($dbh) } - $ret = list_shows($dbh) } elsif($cmd eq "show") { if($num_args != 2) { print_usage(); - exit(1); + $ret = 1; + } else { + $ret = show($dbh, $ARGV[1]) } - $ret = show_show($dbh, $ARGV[1]) } elsif($cmd eq "remove") { if($num_args != 2) { print_usage(); - exit(1); + $ret = 1; + } else { + $ret = remove($dbh, $ARGV[1]); } - $ret = remove_show($dbh, $ARGV[1]); } elsif($cmd eq "add") { if($num_args != 8) { print_usage(); - exit(1); + $ret = 1; + } else { + $ret = add($dbh, $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5], $ARGV[6], $ARGV[7]); } - $ret = add_show($dbh, $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5], $ARGV[6], $ARGV[7]); } else { print_usage(); - RHRD::rddb::closedb($dbh); - exit 1; + $ret = 1; } RHRD::rddb::closedb($dbh); } else { print STDERR "$errorstring\n"; - exit 1; + $ret = 1; } exit $ret; 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; diff --git a/utils/rhrd-ws-login b/utils/rhrd-ws-login index c7a8d00..a3cee53 100755 --- a/utils/rhrd-ws-login +++ b/utils/rhrd-ws-login @@ -23,32 +23,48 @@ use strict; use RHRD::rddb; -my $num_args = $#ARGV + 1; -if($num_args != 1 || ($ARGV[0] ne "login" && $ARGV[0] ne "logout")) { +sub print_usage +{ print STDERR "Usage: rhrd-ws-login (login|logout)\n"; - exit 1; } +my $num_args = $#ARGV + 1; +if($num_args != 1) { + print_usage(); + exit 1; +} +my $cmd = $ARGV[0]; my $username = $ENV{'USER'}; +my $ret = 0; + my ($dbh, undef, $errorstring) = RHRD::rddb::opendb(); if(defined $dbh) { - my $macro = "NN!"; - if($ARGV[0] eq "logout") { + my $macro = ''; + if($cmd eq "logout") { $macro = "LO!"; - } elsif($ARGV[0] eq "login") { + } + elsif($cmd eq "login") { (my $token, undef, $errorstring) = RHRD::rddb::get_token($dbh, $username); - unless($token) { + if($token) { + $token =~ s/([ !'"\\])/\\$1/g; + $macro = "LO $username $token!"; + } else { print STDERR "$errorstring\n"; - exit 0; + $macro = "LO!"; } - $token =~ s/([ !'"\\])/\\$1/g; - $macro = "LO $username $token!" + } + else { + print_usage(); + $ret = 1; } RHRD::rddb::closedb($dbh); - system('/usr/bin/rmlsend', $macro) == 0 or die "system(/usr/bin/rmlsend, $macro) failed: $?"; + unless ($macro eq '') { + system('/usr/bin/rmlsend', $macro) == 0 or die "system(/usr/bin/rmlsend, $macro) failed: $?"; + } } else { print STDERR "$errorstring\n"; + $ret = 1; } -exit 0 +exit $ret; diff --git a/utils/update-rd-tokens b/utils/update-rd-tokens index 63b4750..8983b17 100755 --- a/utils/update-rd-tokens +++ b/utils/update-rd-tokens @@ -32,31 +32,32 @@ if ($> != 0 ) { my $num_args = $#ARGV + 1; if ($num_args <= 0) { - print STDERR "WARNING: this will update all users tokens (except for admin)\n"; + print STDERR "WARNING: this will update all user tokens (except for admin)\n"; } my %EXCLUDED_USERS = map { $_ => 1 } @ARGV; $EXCLUDED_USERS{'admin'} = 1; +my $ret = 0; my ($dbh, undef, $errorstring) = RHRD::rddb::opendb(); if(defined $dbh) { my @users = RHRD::rddb::list_users($dbh); if(!defined $users[0] && defined $users[2]) { print STDERR "$users[2]\n"; - exit 1; - } - - foreach my $user (@users) { - next if(exists($EXCLUDED_USERS{$user})); - my $token = mkpasswd(-length => 16, -minnum => 3, -minupper => 3, -minspecial => 0); - RHRD::rddb::set_token($dbh, $user, $token); - print "$user -> $token\n"; + $ret = 1; + } else { + foreach my $user (@users) { + next if(exists($EXCLUDED_USERS{$user})); + my $token = mkpasswd(-length => 16, -minnum => 3, -minupper => 3, -minspecial => 0); + RHRD::rddb::set_token($dbh, $user, $token); + print "$user -> $token\n"; + } } RHRD::rddb::closedb($dbh); } else { print STDERR "$errorstring\n"; - exit 1; + $ret = 1; } -exit 0 +exit $ret; -- cgit v0.10.2