| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use FindBin '$Bin'; | ||
| 6 | use lib "$Bin/../lib"; | ||
| 7 | use GBrowse::ConfigData; | ||
| 8 | use Bio::Graphics::Browser2; | ||
| 9 | use Bio::Graphics::Browser2::UserDB; | ||
| 10 | use Getopt::Long; | ||
| 11 | |||
| 12 | my ($name,$pass); | ||
| 13 | |||
| 14 | my @ORIGINAL_ARGV = @ARGV; | ||
| 15 | |||
| 16 | GetOptions('name=s' => \$name, | ||
| 17 | 'password=s' => \$pass) or die <<EOF; | ||
| 18 | Usage: $0 [options] | ||
| 19 | |||
| 20 | Sets the administrator\'s login name and password. This login name | ||
| 21 | will have the ability to upload public tracks to GBrowse. | ||
| 22 | |||
| 23 | -name Login name for admin user [default "admin"]. | ||
| 24 | -pass Login password for admin user. | ||
| 25 | |||
| 26 | This script uses the "user_account_db" and "admin_account" options | ||
| 27 | in the currently installed GBrowse.conf configuration file to find | ||
| 28 | the appropriate accounts database and the name of the administrator. | ||
| 29 | If a password is not provided on the command line, you will be | ||
| 30 | prompted for it on standard input. | ||
| 31 | |||
| 32 | WARNING: This script should be run as the web server user using | ||
| 33 | "sudo -u www-data $0". If it detects that it is not being run as this | ||
| 34 | user, it will attempt to sudo itself for you. | ||
| 35 | EOF | ||
| 36 | ; | ||
| 37 | |||
| 38 | my $wwwuser = GBrowse::ConfigData->config('wwwuser'); | ||
| 39 | my $uid = (getpwnam($wwwuser))[2]; | ||
| 40 | unless ($uid == $<) { | ||
| 41 | print STDERR "Not running as $wwwuser. Trying to use sudo to remedy. You may be asked for your login password.\n"; | ||
| 42 | my @args = ('sudo','-u',$wwwuser,$0, @ORIGINAL_ARGV); | ||
| 43 | exec @args; | ||
| 44 | exit 0; | ||
| 45 | } | ||
| 46 | |||
| 47 | my $globals = Bio::Graphics::Browser2->open_globals or die "Couldn't open GBrowse.conf"; | ||
| 48 | my $userdb = Bio::Graphics::Browser2::UserDB->new($globals); | ||
| 49 | |||
| 50 | $name ||= $globals->admin_account; | ||
| 51 | |||
| 52 | unless ($pass) { | ||
| 53 | print STDERR "New password for $name: "; | ||
| 54 | system "stty -echo"; | ||
| 55 | $pass = <STDIN>; | ||
| 56 | chomp($pass); | ||
| 57 | print STDERR "\n"; | ||
| 58 | |||
| 59 | my $newpass; | ||
| 60 | print STDERR "New password for $name (confirm): "; | ||
| 61 | $newpass = <STDIN>; | ||
| 62 | chomp($newpass); | ||
| 63 | print STDERR "\n"; | ||
| 64 | system "stty echo"; | ||
| 65 | die "Passwords don't match!\n" unless $pass eq $newpass; | ||
| 66 | } | ||
| 67 | |||
| 68 | $userdb->delete_user_by_username($name); | ||
| 69 | |||
| 70 | # this creates a new session for the admin user | ||
| 71 | my $session = $globals->session; | ||
| 72 | my $sessionid = $session->id; | ||
| 73 | my $uploadsid = $session->uploadsid; | ||
| 74 | $session->flush(); | ||
| 75 | |||
| 76 | my ($status,undef,$message) = | ||
| 77 | $userdb->do_add_user($name,'admin@nowhere.net','GBrowse Administrator',$pass,$sessionid,'allow admin'); | ||
| 78 | warn $message,"\n"; | ||
| 79 | $userdb->set_confirmed_from_username($name); | ||
| 80 | |||
| 81 | warn "Admin account \"$name\" is now registered with sessionid=$sessionid, uploadsid=$uploadsid.\n" if $message =~ /success/i; | ||
| 82 | |||
| 83 | exit 0; | ||
| 84 | |||
| 85 | __END__ |