Packaging of Bugzilla 2.18 in Debian

Alexis Sukrieh sukria at sukria.net
Tue Jan 18 21:12:23 UTC 2005


* Joel Peshkin (bugreport at peshkin.net) disait :
> 
> Actually, what we need to do is to kill 2 birds with one stone.  It is 
> useful to have a way to create an administrator from checksetup without 
> requiring database hacking.  If we do that, the update portion should be 
> a no-brainer.

Well, don't know if you guys will find this patch ok, but anyway, it
works ;)

If that patched checksetup.pl run with an answer file, it can update an existing account.

I plan to use this patch in the Debian package, that will be great for
the postinst phase : allways using checksetup.pl for updating the
Bugzilla's administrator.

Regards,

Alexis.

-- 
                                  Alexis Sukrieh <sukria at sukria.net>
                                               http://www.sukria.net

« Quidquid latine dictum sit, altum sonatur. » 
Whatever is said in Latin sounds profound.
-------------- next part --------------
--- checksetup.pl	2005-01-14 10:51:36.000000000 +0100
+++ checksetup.pl	2005-01-18 22:03:49.000000000 +0100
@@ -1,4 +1,5 @@
 #!/usr/bin/perl -w
+$|=1;
 # -*- Mode: perl; indent-tabs-mode: nil -*-
 #
 # The contents of this file are subject to the Mozilla Public
@@ -114,10 +115,11 @@
 #
 
 use strict;
-use lib ".";
+use lib "/usr/share/bugzilla";
 
 use vars qw( $db_name %answer );
 use Bugzilla::Constants;
+use constant MAX_PROMPT_CYCLE => 10;
 
 my $silent;
 
@@ -4195,7 +4197,9 @@
                     " WHERE name = 'admin' AND id = group_id");
 $sth->execute;
 # when we have no admin users, prompt for admin email address and password ...
-if ($sth->rows == 0) {
+# if we are in non-interactive mode, process anyway.
+if ($sth->rows == 0 or defined $answer{'ADMIN_EMAIL'}) {
+  my $update_only = 1 if $sth->rows > 0;
   my $login = "";
   my $realname = "";
   my $pass1 = "";
@@ -4220,12 +4224,19 @@
       and at least one \'.\' after the @.';
   }
 
+  unless ($update_only) {
   print "\nLooks like we don't have an administrator set up yet.  Either this is your\n";
   print "first time using Bugzilla, or your administrator's privileges might have accidently\n";
   print "been deleted.\n";
+  }
+
+  #this is useful when using this script in non-interactive mode.
+  my $cycle_count = 0;
+  
   while(! $admin_ok ) {
-    while( $login eq "" ) {
-      print "Enter the e-mail address of the administrator: ";
+    while( $login eq "") {
+      print "Enter the e-mail address of the administrator: " 
+      	unless defined $answer{'ADMIN_EMAIL'};
       $login = $answer{'ADMIN_EMAIL'} 
           || ($silent && die("cant preload ADMIN_EMAIL")) 
           || <STDIN>;
@@ -4241,14 +4253,19 @@
         # Go round, and ask them again
         $login = "";
       }
+      
+      die "Failed to get a valid email address: $login\n" if
+	      ($cycle_count++ > MAX_PROMPT_CYCLE);
     }
     $login = $dbh->quote($login);
     $sth = $dbh->prepare("SELECT login_name FROM profiles" .
                         " WHERE login_name=$login");
     $sth->execute;
     if ($sth->rows > 0) {
+      if (not $answer{'ADMIN_OK'}) {
       print "$login already has an account.\n";
       print "Make this user the administrator? [Y/n] ";
+      }
       my $ok = $answer{'ADMIN_OK'} 
           || ($silent && die("cant preload ADMIN_OK")) 
           || <STDIN>;
@@ -4275,17 +4293,21 @@
     }
   }
 
-  if ($admin_create) {
 
+    $cycle_count = 0;
     while( $realname eq "" ) {
-      print "Enter the real name of the administrator: ";
+      print "Enter the real name of the administrator: " 
+      	unless defined $answer{'ADMIN_REALNAME'};
       $realname = $answer{'ADMIN_REALNAME'} 
           || ($silent && die("cant preload ADMIN_REALNAME")) 
           || <STDIN>;
       chomp $realname;
+      
       if(! $realname ) {
         print "\nReally.  We need a full name.\n";
       }
+      die "Failed to get a valid realname: $realname\n" if
+	      ($cycle_count++ > MAX_PROMPT_CYCLE);
     }
 
     # trap a few interrupts so we can fix the echo if we get aborted.
@@ -4294,17 +4316,23 @@
     $SIG{QUIT} = \&bailout;
     $SIG{TERM} = \&bailout;
 
-    if ($^O !~ /MSWin32/i) {
+    # only on interactive mode !
+    if ((not $answer{'ADMIN_PASSWORD'}) and $^O !~ /MSWin32/i) {
         system("stty","-echo");  # disable input echoing
     }
+    my $interactive = 1;
+    $interactive = 0 if defined $answer{'ADMIN_PASSWORD'};
 
     while( $pass1 ne $pass2 ) {
+      $cycle_count = 0;
       while( $pass1 eq "" || $pass1 !~ /^[[:print:]]{3,16}$/ ) {
-        print "Enter a password for the administrator account: ";
+        print "Enter a password for the administrator account: "
+		unless defined $answer{'ADMIN_PASSWORD'};
         $pass1 = $answer{'ADMIN_PASSWORD'} 
             || ($silent && die("cant preload ADMIN_PASSWORD")) 
             || <STDIN>;
         chomp $pass1;
+	
         if(! $pass1 ) {
           print "\n\nAn empty password is a security risk. Try again!\n";
         } elsif ( $pass1 !~ /^.{3,16}$/ ) {
@@ -4312,8 +4340,13 @@
         } elsif ( $pass1 !~ /^[[:print:]]{3,16}$/ ) {
           print "\n\nThe password contains non-printable characters.\n";
         }
+        
+	die "Failed to get a valid password: $pass1\n" if
+	      ($cycle_count++ > MAX_PROMPT_CYCLE);
       }
-      print "\nPlease retype the password to verify: ";
+      print "\nPlease retype the password to verify: " 
+      	if $interactive;
+	
       $pass2 = $answer{'ADMIN_PASSWORD'} 
           || ($silent && die("cant preload ADMIN_PASSWORD")) 
           || <STDIN>;
@@ -4343,9 +4376,26 @@
     # Set default email flags for the Admin, same as for users
     my $defaultflagstring = $dbh->quote(Bugzilla::Constants::DEFAULT_EMAIL_SETTINGS);
 
-    $dbh->do("INSERT INTO profiles (login_name, realname, cryptpassword, emailflags) " .
-             "VALUES ($login, $realname, $cryptedpassword, $defaultflagstring)");
+  if ($admin_create) {
+    warn "Creating the administrator $login...\n"
+    	if ($answer{'ADMIN_REALNAME'});
+
+    unless($dbh->do("INSERT INTO profiles (login_name, realname, cryptpassword, emailflags) " .
+             "VALUES ($login, $realname, $cryptedpassword, $defaultflagstring)")) {
+	die "Unable to create the $login profile.";
   }
+  }
+
+  # The ADMIN_REALNAME exists already, but we have to update his profile.
+  else {
+	warn "Updating the $login account...\n";
+	unless ($dbh->do("UPDATE profiles SET realname=$realname, cryptpassword=$cryptedpassword ".
+		 "WHERE login_name=$login")) {
+		die "Unable to update the $login profile.";
+	}
+  }
+
+  unless ($update_only) {
     # Put the admin in each group if not already    
     my $query = "select userid from profiles where login_name = $login";    
     $sth = $dbh->prepare($query); 
@@ -4371,6 +4422,7 @@
             (member_id, grantor_id, isbless)
             VALUES ($admingroupid, $group, 1)");
     }
+  }
 
   print "\n$login is now set up as an administrator account.\n";
 }


More information about the developers mailing list