#!/opt/casp/tools/bin/linux2/perl5/bin/perl -I/opt/casp/tools/bin/linux2/perl5/lib -I/opt/casp use INSTALL::common; use INSTALL::parameter; use INSTALL::global; use strict; no strict 'refs'; my $pParameters = \%INSTALL::parameter::variables; my $pGlobals = \%INSTALL::global::variables; $pParameters->{asphome} = "/opt/casp"; $pParameters->{com_layer} = "ChiliCOM"; INSTALL::common::set_standard_globals; #============================================================================== #------------------------ Default global values #============================================================================== my $CONST_ComponentName = "Administration Console"; use INSTALL::component; my $pComponent = INSTALL::component::open($CONST_ComponentName, "/opt/casp"); my $URL = "http://$pGlobals->{full_hostname}:"; # Handle case of the Admin console having yet to be installed. if ($pComponent) { $URL = "http://$pGlobals->{full_hostname}:$pComponent->{admd_port}"; } sub display { print STDERR (@_); } sub error { report("Error: ",@_); } sub usage { report("Usage: ", "admtool (interactive mode)", "admtool [-sehuq]", "admtool -a username[:password]", "admtool -r username", "" ); report(" ", "-h Show this screen.", "-s Start admin server.", "-q Query admin status", "-e Stop admin server.", "-a Add a user.", "-r Remove a user", "-u List users", "" ); exit(1); } sub admin_start { my $casp_status = system("/opt/casp/admin/bin/caspctrl status > /dev/null 2>&1"); my $apache_status = system("/opt/casp/admin/bin/apachectl ". "'binary=/opt/casp/admin/bin/admd' ". "'conf=/opt/casp/admin/conf/admd.conf' ". "status ". "> /dev/null 2>&1"); my $casp_message = "Already running"; my $apache_message = "Already running"; my $result = 0; if ($casp_status) { if (system("/opt/casp/admin/bin/caspctrl startall > /dev/null 2>&1")) { $casp_message = "Failed to start"; } else { $casp_message = "Started"; $result |= 1; } } if ($apache_status) { if (system("/opt/casp/admin/bin/apachectl ". "'binary=/opt/casp/admin/bin/admd' ". "'conf=/opt/casp/admin/conf/admd.conf' ". "start ". "> /dev/null 2>&1")) { $apache_message = "Failed to start"; } else { $apache_message = "Started"; $result |= 1; } } report("Administration Console Status"); report("============================="); report(" ", "Admin Web Server: $apache_message", "Admin ASP Server: $casp_message", " URL: $URL"); return($result); } sub admin_stop { my $casp_status = system("/opt/casp/admin/bin/caspctrl status > /dev/null 2>&1"); my $apache_status = system("/opt/casp/admin/bin/apachectl ". "'binary=/opt/casp/admin/bin/admd' ". "'conf=/opt/casp/admin/conf/admd.conf' ". "status ". "> /dev/null 2>&1"); my $casp_message = "Not running"; my $apache_message = "Not running"; my $result = 0; unless($casp_status) { if (system("/opt/casp/admin/bin/caspctrl stopall > /dev/null 2>&1")) { $casp_message = "Failed to stop"; } else { $casp_message = "Stopped"; $result |= 1; } } unless($apache_status) { if (system("/opt/casp/admin/bin/apachectl ". "'binary=/opt/casp/admin/bin/admd' ". "'conf=/opt/casp/admin/conf/admd.conf' ". "stop ". "> /dev/null 2>&1")) { $apache_message = "Failed to stop"; } else { $apache_message = "Stopped"; $result |= 1; } } report("Administration Console Status"); report("============================="); report(" ", "Admin Web Server: $apache_message", "Admin ASP Server: $casp_message", " URL: $URL"); return($result); } sub admin_status { my $casp_running = not system("/opt/casp/admin/bin/caspctrl status > /dev/null 2>&1"); my $apache_running = not system("/opt/casp/admin/bin/apachectl ". "'binary=/opt/casp/admin/bin/admd' ". "'conf=/opt/casp/admin/conf/admd.conf' ". "status ". "> /dev/null 2>&1"); report("Administration Console Status"); report("============================="); report(" ", "Admin Web Server: ".($apache_running ? "Running" : "Stopped"), "Admin ASP Server: ".($casp_running ? "Running" : "Stopped"), " URL: $URL"); if ($casp_running && $apache_running) { return(0); } elsif ($casp_running) { return(1); } elsif ($apache_running) { return(2); } else { return(3); } } sub user_list_internal { local *HTACCESS; my @result; if (open(HTACCESS,") { if (/^([^\#]*):.*/) { push @result,$1 } } close(HTACCESS); } return(@result); } sub user_list { my @user_list = user_list_internal; report("Administration Console Users"); report("============================"); if (@user_list) { for(my $i=1; $i<= scalar(@user_list); $i++) { report(" $i. ",$user_list[$i - 1]); } } else { report(" ","No user are installed."); } return(1); } sub user_add { ($#_ <= 1) || die "Invalid number of arguments."; my ($username,$password) = @_; my ($user, $pass) = @_; if ($#_ < 1) { do { RETRY_USER_ADD: $user = $username; if (not $user) { display("Username to add (or none to cancel) [none]: "); $user = ; chomp $user; if (not $user) { $user = "none"; } } if ($user eq "none") { report("No user was added to the administration console's user list."); return(0); } display("Password: "); system("stty -echo"); $pass = ; report(""); chomp $pass; if (not $pass) { report("Empty passwords are not allowed."); system("stty echo"); goto RETRY_USER_ADD; } display("Confirm password: "); my $confirm = ; system("stty echo"); chomp $confirm; report(""); if ($confirm ne $pass) { report("Passwords do not match."); report(""); goto RETRY_USER_ADD; } } while (system("/opt/casp/admin/conf/htpasswd -b ". "/opt/casp/admin/conf/service.pwd ". "$user $pass ". "> /dev/null 2>&1")); report("The user $user was added to the administration console's user list."); return(1); } else { unless($user) { return(error("The username may not be empty.")); } unless($pass) { return(error("The password may not be empty.")); } if (system("/opt/casp/admin/conf/htpasswd -b ". "/opt/casp/admin/conf/service.pwd ". "$user $pass ". "> /dev/null 2>&1")) { error("Unable to add user $user to administration console's user list."); } else { report("The user $user was added to the administration console's user list."); return(1); } } } sub user_del_internal { ($#_ == 0) || die "Invalid number of arguments."; my ($username) = @_; local *HTACCESS; my @lines; if (open(HTACCESS,") { if ($line !~ /^$username:/) { push @lines, $line; } } } if (open(HTACCESS,">/opt/casp/admin/conf/service.pwd")) { print HTACCESS @lines; return(1); } else { return(0); } } sub user_del { ($#_ <= 0) || die "Invalid number of arguments."; my ($username) = @_; my $user; my @user_list = user_list_internal; do { if (defined $user) { report("User '$user' is not an administration console user, try again."); } $user = $username; unless($user) { display("Username to remove (or none to cancel) [none]: "); $user = ; chomp $user; if (not $user) { $user = "none"; } } if ($user eq "none") { report("No user was removed from the administration console's user list."); return(undef); } } while((not grep { $_ eq $user } @user_list) && (not $username)); if ((grep { $_ eq $user } @user_list) && user_del_internal($user)) { report("The user $user was removed from the administration console's user list."); } else { report("The user $user could not be removed from the users list."); return(0); } return(1); } use INSTALL::query; my @CONST_Commands = ("admin_start", "admin_stop", "admin_status", "user_add", "user_del", "user_list" ); sub menu { my $result; while(1) { $result = INSTALL::query::user_menu ("Sun Chili!Soft ASP - Administration Tool", "", "Please choose one", "terminal_mark=:", "Start admin server.", "Stop admin server.", "Admin server status.", "Add a user.", "Remove a user.", "List users.", "default=Quit." ); if ($result > scalar(@CONST_Commands)) { last; } else { report(""); &{$CONST_Commands[$result - 1]}; report(""); report("Press Enter to return to the previous menu ..."); ; } } return(1); } sub match { my ($a,@b) = @_; if (@b) { foreach(@b) { if ($a ne $_) { return(undef); } } return(1); } else { return($a eq $_); } } if (@ARGV) { my $previous_option; foreach(@ARGV) { match("-a",$previous_option) && do { if ($_ =~ /^([^:]*):(.*)/) { exit(not user_add($1,$2)); } else { exit(not user_add($_)); } $previous_option = undef; }; match("-r",$previous_option) && exit(not user_del($_)); (match("-h") || match("-?")) && usage; match("-s") && exit(not admin_start); match("-q") && exit(admin_status); # Special in that it returns the error code in exit status form. match("-e") && exit(not admin_stop); match("-u") && exit(not user_list); if (match("-a") || match("-r")) { $previous_option = $_; next; } error("Invalid option: $_") || usage; } if ($previous_option) { usage; } } else { exit(not menu); }