Debianization for 1.01-2
[qmail] / debian / preinst
diff --git a/debian/preinst b/debian/preinst
new file mode 100644 (file)
index 0000000..bfacc91
--- /dev/null
@@ -0,0 +1,218 @@
+#!/usr/bin/perl
+
+require 5.002;
+use strict 'subs';
+use strict 'barewords';
+use English;
+
+sub check_uid($$$$$$) {
+    my ($name, $uid, $gid, $gecos, $home, $shell) = @_;
+    my $ok = 1;
+
+    print "Checking user $name (uid $uid, gid $gid, homedir $home)... ";
+    @name_entry = getpwnam("$name");
+    if ( @name_entry == () ) {
+       my @uid_entry = getpwuid($uid);
+       if ( @uid_entry == () ) {
+           system("adduser --system --quiet --home $home --gid $gid --uid $uid --gecos \'$gecos\' $name >/dev/null 2>&1");
+           if ( $? != 0 ) {
+               print "\n Error while adding user $name!\n";
+               $ok = 0;
+           }
+           else {
+               print "added.\n";
+           }
+           system("chsh -s $shell $name");
+       }
+       else {
+           print "error!\n Uid $uid is being used by user $uid_entry[0]\n";
+           $ok = 0;
+       }
+    }
+    else {
+       if ( $name_entry[3] != $gid ) {
+           print "error!\n" if $ok;
+           print " User $name has primary group $name_entry[3] instead of $gid\n";
+           $ok = 0;
+       }
+       if ( $name_entry[2] != $uid ) {
+           print "error!\n" if $ok;
+           print " User $name has uid $name_entry[2] instead of $uid\n";
+           $ok = 0;
+       }
+       if ( $name_entry[7] ne $home ) {
+           print "error!\n" if $ok;
+           print " User $name has home directory $name_entry[7] instead of $home\n";
+           $ok = 0;
+       }
+       if ( $ok ) {
+           print "ok.\n";
+       }
+    }
+    return $ok;
+}
+
+sub check_gid($$@) {
+    my ($name, $gid, @members) = @_;
+    my $ok = 1;
+
+    print "Checking group $name (gid $gid)... ";
+    @name_entry = getgrnam($name);
+    if ( @name_entry == () ) {
+       my @gid_entry = getgrgid($gid);
+       if ( @gid_entry == () ) {
+           system("addgroup --quiet --gid $gid $name");
+           if ( $? != 0 ) {
+               print "\n Error while adding group $name\n";
+               $ok = 0;
+           }
+           else {
+               print "added.\n";
+           }
+       }
+       else {
+           print "error!\n Gid $gid is being used by group $gid_entry[0]\n";
+           $ok = 0;
+       }
+    }
+    else {
+       if ( $name_entry[2] != $gid ) {
+           print "error!\n Group $name has gid $name_entry[2] instead of $gid\n";
+           $ok = 0;
+       }
+       if ( $ok ) {
+           print "ok.\n";
+       }
+    }
+    return $ok;
+}
+
+sub is_qmail_installed {
+    my $qmail_installed = 0;
+
+    print "Checking if qmail is already installed on this computer... ";
+
+    $qmail_home = '';
+    if ( -d '/var/qmail' ) {
+       print "\n Found /var/qmail directory";
+       $qmail_home = '/var/qmail';
+       $qmail_installed = 1;
+    }
+    else {
+       $qmail_home = `qmail-home`;
+       if ( $qmail_home ne '') {
+           print "\n Found qmail home directory at $qmail_home (using qmail-home program)";
+           $qmail_installed = 1;
+       }
+    }
+
+    if ( -d '/var/spool/qmail' ) {
+       print "\n Found qmail spool directory at /var/spool/qmail";
+       $qmail_installed = 1;
+    }
+    if ( -d '/etc/qmail' ) {
+       print "\n Found qmail control directory at /etc/qmail";
+       $qmail_installed = 1;
+    }
+    
+    if ( (-r '/etc/inetd.conf') and `fgrep -q qmail-smtpd /etc/inetd.conf` ) {
+       print "\n Found reference to qmail-smtpd in /etc/inetd.conf";
+       $qmail_installed = 1;
+    }
+    if ( (-r '/etc/xinetd.conf') and `fgrep -q qmail-smtpd /etc/xinetd.conf` ) {
+       print "\n Found reference to qmail-smtpd in /etc/xinetd.conf";
+       $qmail_installed = 1;
+    }
+
+    if ( -x '/etc/init.d/qmail' ) {
+       print "\n Found /etc/init.d/qmail script";
+       $qmail_installed = 1;
+    }
+    elsif ( `fgrep -q qmail-start /etc/init.d/*` ) {
+       print "\n Found reference to qmail-start in /etc/init.d directory";
+       $qmail_installed = 1;
+    }
+    if ( -x '/etc/rc.local' and `fgrep -q qmail-start /etc/rc.local` ) {
+       print "\n Found reference to qmail-start in /etc/rc.local";
+       $qmail_installed = 1;
+    }
+
+    if ( `killall -0 qmail-send >/dev/null 2>&1` ) {
+       print "\n Found qmail-send process running";
+       $qmail_installed = 1;
+    }
+
+    print 'no.' unless $qmail_installed;
+    print "\n";
+    return $qmail_installed;
+}
+       
+
+$| = 1;
+
+$action = shift;
+
+if ( $action eq 'install' ) {
+    $old_version = shift;
+    if ( ! defined $old_version || $old_version eq '' ) {
+       print "First installation of the Debian qmail package...\n";
+       # Check if a non-Debian qmail is installed...
+       if ( is_qmail_installed() ) {
+           print "Please remove your copy of qmail before installing the qmail Debian package.\n\n";
+           exit 1;
+       }
+       # Check for qmail uids and gids
+       my $errors;
+       $errors++ unless check_gid('qmail', 70, ());
+       #$errors++ unless check_gid('nogroup', 65534, ());
+
+       $errors++ unless check_uid('alias', 70, 65534, 'qmail alias', '/var/qmail/alias', '/bin/sh');
+       $errors++ unless check_uid('qmaild', 71, 65534, 'qmail daemon', '/var/qmail', '/bin/sh');
+       $errors++ unless check_uid('qmails', 72, 70, 'qmail send', '/var/qmail', '/bin/sh');
+       $errors++ unless check_uid('qmailr', 73, 70, 'qmail remote', '/var/qmail', '/bin/sh');
+       $errors++ unless check_uid('qmailq', 74, 70, 'qmail queue', '/var/qmail', '/bin/sh');
+       $errors++ unless check_uid('qmaill', 75, 65534, 'qmail log', '/var/qmail', '/bin/sh');
+       $errors++ unless check_uid('qmailp', 76, 65534, 'qmail pw', '/var/qmail', '/bin/sh');
+       #$errors++ unless check_uid('nobody', 65534, 65534, 'nobody', '/tmp', '/bin/sh');
+
+       if ( $errors ) {
+           print "\n$errors entries have errors. Please correct these errors and reinstall qmail.\n";
+           exit 2;
+       }
+    }
+
+    # Make sure there are no smtp entries in /etc/inetd.conf
+    # Kludge around smail buggy /etc/inetd.conf handling. (Grr.)
+    my $fixed_smail = 0;
+    my $found_commented = 0;
+    my $found_uncommented = 0;
+    my $new_inetd = "/etc/inetd.conf.qmail-preinst.$$";
+    open I, '</etc/inetd.conf' or die "Could not open /etc/inetd.conf\n";
+    open N, ">$new_inetd" or die "Could not create $new_inetd\n";
+    while (<I>) {
+       if ( m|^\# *smtp\s+.*/usr/sbin/in.smtpd.*\(will be restored by smail postinst\)\s*$| ) {
+                $fixed_smail = 1;
+               next;
+            } elsif ( m/^\# *smtp\s+/ ) {
+                $found_commented= 1;
+            } elsif ( m/^smtp\s+/ ) {
+                $found_uncommented= 1;
+            }
+       print N or die "Cannot write to $new_inetd\n";
+       }
+    close N or die "Could not close $new_inetd\n";
+    close I or die "Could not close /etc/inetd.conf\n";
+    if ( $found_commented or $found_uncommented ) {
+       print "Your /etc/inetd.conf already containts entries for the SMTP service.\n";
+       print "Please remove all SMTP entries from /etc/inetd.conf (even those commented out)\n";
+       print "and reinstall the qmail package.\n\n";
+       exit 1; 
+    }
+    if ( $fixed_smail ) {
+       print "Removing commented smtp entry left by buggy version of smail postinst... ";
+       rename "$new_inetd", '/etc/inetd.conf' or die "failed!\n";
+       print "done.\n";
+    }
+}
+
+exit 0;