Mail-Toaster

 view release on metacpan or  search on metacpan

lib/Mail/Toaster/Qmail.pm  view on Meta::CPAN

        unless ( $rcpthosts{$domain} ) {
            print "\t$domain\n";
            $count++;
        }
        $domains++;
    }

    if ( ! $count || $count == 0 ) {
        print "Congrats, your rcpthosts is correct!\n";
        return 1;
    }

    if ( $domains > 50 ) {
        print
"\nDomains listed above should be added to $mrcpt. Don't forget to run 'qmail cdb' afterwards.\n";
    }
    else {
        print "\nDomains listed above should be added to $rcpt. \n";
    }
}

sub config {
    my $self = shift;
    my %p = validate( @_, { $self->get_std_opts } );
    my %args = $self->toaster->get_std_args( %p );
    return $p{test_ok} if defined $p{test_ok};

    my $conf = $self->conf;
    my $host = $conf->{toaster_hostname};
       $host = hostname if $host =~ /(?:qmail|system)/;

    my $postmaster = $conf->{toaster_admin_email};
    my $ciphers    = $conf->{openssl_ciphers} || 'pci';

    if ( $ciphers =~ /^[a-z]+$/ ) {
        $ciphers = $self->setup->openssl_get_ciphers( $ciphers );
    };

    my @changes = (
        { file => 'control/me',                 setting => $host, },
        { file => 'control/concurrencyremote',  setting => $conf->{'qmail_concurrencyremote'},},
        { file => 'control/mfcheck',            setting => $conf->{'qmail_mfcheck_enable'},   },
        { file => 'control/tarpitcount',        setting => $conf->{'qmail_tarpit_count'},     },
        { file => 'control/tarpitdelay',        setting => $conf->{'qmail_tarpit_delay'},     },
        { file => 'control/spfbehavior',        setting => $conf->{'qmail_spf_behavior'},     },
        { file => 'alias/.qmail-postmaster',    setting => $postmaster,   },
        { file => 'alias/.qmail-root',          setting => $postmaster,   },
        { file => 'alias/.qmail-mailer-daemon', setting => $postmaster,   },
        { file => 'control/tlsserverciphers',   setting => $ciphers },
        { file => 'control/tlsclientciphers',   setting => $ciphers },
    );

    push @changes, $self->control_sql if $conf->{vpopmail_mysql};

    $self->config_write( \@changes );

    my $uid = getpwnam('vpopmail');
    my $gid = getgrnam('vchkpw');

    my $control = $self->get_control_dir;
    chown( $uid, $gid, "$control/servercert.pem" );
    chown( $uid, $gid, "$control/sql" );
    chmod oct('0640'), "$control/servercert.pem";
    chmod oct('0640'), "$control/clientcert.pem";
    chmod oct('0640'), "$control/sql";
    chmod oct('0644'), "$control/concurrencyremote";

    $self->config_freebsd if $OSNAME eq 'freebsd';

    # qmail control script (qmail cdb, qmail restart, etc)
    $self->control_create( %args );

    # create all the service and supervised dirs
    $self->toaster->service_dir_create( %args );
    $self->toaster->supervise_dirs_create( %args );

    # install the supervised control files
    $self->install_qmail_control_files( %args );
    $self->install_qmail_control_log_files( %args );
}

sub config_freebsd {
    my $self = shift;
    my $tmp  = $self->conf->{'toaster_tmp_dir'} || "/tmp";

    # disable sendmail
    $self->freebsd->conf_check(
        check => "sendmail_enable",
        line  => 'sendmail_enable="NONE"',
    );

    # don't build sendmail when we rebuild the world
    $self->util->file_write( "/etc/make.conf",
        lines  => ["NO_SENDMAIL=true"],
        append => 1,
    )
    if ! `grep NO_SENDMAIL /etc/make.conf`;

    # make sure mailer.conf is set up for qmail
    my $tmp_mailer_conf = "$tmp/mailer.conf";
    my $qmdir = $self->get_qmail_dir;
    my $maillogs = $self->util->find_bin('maillogs',fatal=>0 )
        || '/usr/local/bin/maillogs';
    open my $MAILER_CONF, '>', $tmp_mailer_conf
        or $self->error( "unable to open $tmp_mailer_conf: $!",fatal=>0);

    print $MAILER_CONF "
# \$FreeBSD: release/9.1.0/etc/mail/mailer.conf 93858 2002-04-05 04:25:14Z gshapiro \$
#
sendmail        $qmdir/bin/sendmail
send-mail       $qmdir/bin/sendmail
mailq           $maillogs yesterday
#mailq          $qmdir/bin/qmail-qread
newaliases      $qmdir/bin/newaliases
hoststat        $qmdir/bin/qmail-tcpto
purgestat       $qmdir/bin/qmail-tcpok
#
# Execute the \"real\" sendmail program, named /usr/libexec/sendmail/sendmail
#
#sendmail        /usr/libexec/sendmail/sendmail
#send-mail       /usr/libexec/sendmail/sendmail
#mailq           /usr/libexec/sendmail/sendmail

lib/Mail/Toaster/Qmail.pm  view on Meta::CPAN

    else { $patch = "$package-toaster-2.6.patch"; }

    my $site = "http://cr.yp.to/software";

    unless ( -e "$package.tar.gz" ) {
        if ( -e "/usr/ports/distfiles/$package.tar.gz" ) {
            use File::Copy;
            copy( "/usr/ports/distfiles/$package.tar.gz",
                "$src/mail/$package.tar.gz" );
        }
        else {
            $self->util->get_url( "$site/$package.tar.gz" );
            unless ( -e "$package.tar.gz" ) {
                die "install_qmail FAILED: couldn't fetch $package.tar.gz!\n";
            }
        }
    }

    unless ( -e $patch ) {
        $self->util->get_url( "$toaster_url/patches/$patch" );
        unless ( -e $patch ) { die "\n\nfailed to fetch patch $patch!\n\n"; }
    }

    my $tar      = $self->util->find_bin( "tar"  );
    my $patchbin = $self->util->find_bin( "patch" );
    unless ( $tar && $patchbin ) { die "couldn't find tar or patch!\n"; }

    $self->util->syscmd( "$tar -xzf $package.tar.gz" );
    chdir("$src/mail/$package")
      or die "install_qmail: cd $src/mail/$package failed: $!\n";
    $self->util->syscmd( "$patchbin < $src/mail/$patch" );

    $self->util->file_write( "conf-qmail", lines => [$qmaildir] )
      or die "couldn't write to conf-qmail: $!";

    $self->util->file_write( "conf-vpopmail", lines => [$vpopdir] )
      or die "couldn't write to conf-vpopmail: $!";

    $self->util->file_write( "conf-mysql", lines => [$mysql] )
      or die "couldn't write to conf-mysql: $!";

    my $servicectl = "/usr/local/sbin/services";

    if ( -x $servicectl ) {

        print "Stopping Qmail!\n";
        $self->util->syscmd( "$servicectl stop" );
        $self->send_stop();
    }

    my $make = $self->util->find_bin( "gmake", fatal => 0 );
    $make  ||= $self->util->find_bin( "make" );

    $self->util->syscmd( "$make setup" );

    unless ( -f "$qmaildir/control/servercert.pem" ) {
        $self->util->syscmd( "$make cert" );
    }

    if ($chkusr) {
        $self->util->chown( "$qmaildir/bin/qmail-smtpd",
            uid => 'vpopmail',
            gid => 'vchkpw',
        );

        $self->util->chmod( file => "$qmaildir/bin/qmail-smtpd",
            mode  => '6555',
        );
    }

    unless ( -e "/usr/share/skel/Maildir" ) {

# deprecated, not necessary unless using system accounts
# $self->util->syscmd( "$qmaildir/bin/maildirmake /usr/share/skel/Maildir" );
    }

    $self->config();

    if ( -x $servicectl ) {
        print "Starting Qmail & supervised services!\n";
        $self->util->syscmd( "$servicectl start" );
    }
}

sub install_qmail_control_files {
    my $self = shift;
    my %p = validate( @_, { $self->get_std_opts } );

    my $supervise = $self->get_supervise_dir;

    return $p{'test_ok'} if defined $p{'test_ok'};

    foreach my $prot ( $self->toaster->get_daemons(1) ) {
        my $supdir = $self->toaster->supervise_dir_get( $prot);
        my $run_f = "$supdir/run";

        if ( -e $run_f ) {
            $self->audit( "install_qmail_control_files: $run_f already exists!");
            next;
        }

        if    ( $prot eq "smtp"   ) { $self->build_smtp_run   }
        elsif ( $prot eq "send"   ) { $self->build_send_run   }
        elsif ( $prot eq "pop3"   ) { $self->build_pop3_run   }
        elsif ( $prot eq "submit" ) { $self->build_submit_run }
        elsif ( $prot eq "qmail-deliverable" ) { $self->build_qmail_deliverable_run }
        elsif ( $prot eq "vpopmaild" ) { $self->build_vpopmaild_run }
        elsif ( $prot eq "qpsmtpd" ) { $self->build_qpsmtpd_run }
        else  { $self->error("I need help making run for $prot!"); };
    }
}

sub install_qmail_groups_users {
    my $self = shift;
    my %p = validate( @_, { $self->get_std_opts } );

    my $err = "ERROR: You need to update your toaster-watcher.conf file!\n";

    my $qmailg   = $self->conf->{'qmail_group'}       || 'qmail';
    my $alias    = $self->conf->{'qmail_user_alias'}  || 'alias';
    my $qmaild   = $self->conf->{'qmail_user_daemon'} || 'qmaild';

lib/Mail/Toaster/Qmail.pm  view on Meta::CPAN

            },
            $self->get_std_opts,
        },
    );

    my %args = $self->toaster->get_std_args( %p );
    my $prots = $p{prots};
    push @$prots, "vpopmaild" if $self->conf->{vpopmail_daemon};

    my $supervise = $self->get_supervise_dir;

    my %valid_prots = map { $_ => 1 } qw/ smtp send pop3 submit vpopmaild /;

    return $p{test_ok} if defined $p{test_ok};

    # Create log/run files
    foreach my $serv (@$prots) {

        die "invalid protocol: $serv!\n" unless $valid_prots{$serv};

        my $supervisedir = $self->toaster->supervise_dir_get( $serv );
        my $run_f = "$supervisedir/log/run";

        $self->audit( "install_qmail_control_log_files: preparing $run_f");

        my @lines = $self->toaster->supervised_do_not_edit_notice;
        push @lines, $self->toaster->supervised_multilog($serv);

        my $tmpfile = "/tmp/mt_supervise_" . $serv . "_log_run";
        $self->util->file_write( $tmpfile, lines => \@lines );

        $self->audit( "install_qmail_control_log_files: comparing $run_f");

        my $notify = $self->conf->{'supervise_rebuild_notice'} ? 1 : 0;

        if ( -s $tmpfile ) {
            $self->util->install_if_changed(
                newfile  => $tmpfile, existing => $run_f,
                mode     => '0755',   clean    => 1,
                notify   => $notify,  email    => $self->conf->{'toaster_admin_email'},
            ) or return;
            $self->audit( " updating $run_f, ok" );
        }

        $self->toaster->supervised_dir_test( $serv );
    }
}

sub install_ssl_temp_key {
    my ( $self, $cert, $fatal ) = @_;

    my $user  = $self->conf->{'smtpd_run_as_user'} || "vpopmail";
    my $group = $self->conf->{'qmail_group'}       || "qmail";

    $self->util->chmod(
        file_or_dir => "$cert.new",
        mode        => '0660',
        fatal       => $fatal,
    );

    $self->util->chown( "$cert.new",
        uid   => $user,
        gid   => $group,
        fatal => $fatal,
    );

    move( "$cert.new", $cert );
}

sub maildir_in_skel {

    my $skel = "/usr/share/skel";
    if ( ! -d $skel ) {
        $skel = "/etc/skel" if -d "/etc/skel";    # linux
    }

    if ( ! -e "$skel/Maildir" ) {
        # only necessary for systems with local email accounts
        #$self->util->syscmd( "$qmaildir/bin/maildirmake $skel/Maildir" ) ;
    }
}

sub netqmail {
    my $self = shift;
    my %p = validate( @_, {
            'package' => { type=>SCALAR,  optional=>1, },
            $self->get_std_opts,
        },
    );

    my $package = $p{package};
    my $ver     = $self->conf->{'install_netqmail'} || "1.05";
    my $src     = $self->conf->{'toaster_src_dir'}  || "/usr/local/src";
    my $vhome   = $self->setup->vpopmail->get_vpop_dir;

    $package ||= "netqmail-$ver";

    return $p{test_ok} if defined $p{test_ok};

    $self->install_qmail_groups_users();

    # check to see if qmail-smtpd already has vpopmail support
    return 0 if ! $self->netqmail_rebuild;

    $self->util->cwd_source_dir( "$src/mail" );

    $self->netqmail_get_sources( $package ) or return;
    my @patches = $self->netqmail_get_patches( $package );

    $self->util->extract_archive( "$package.tar.gz" );

    # netqmail requires a "collate" step before it can be built
    chdir("$src/mail/$package")
        or die "netqmail: cd $src/mail/$package failed: $!\n";

    $self->util->syscmd( "./collate.sh" );

    chdir("$src/mail/$package/$package")
        or die "netqmail: cd $src/mail/$package/$package failed: $!\n";

    my $patchbin = $self->util->find_bin( 'patch' );

lib/Mail/Toaster/Qmail.pm  view on Meta::CPAN

    my $dl_url     = $self->conf->{'toaster_dl_url'}    || "/internet/mail/toaster";
    my $toaster_url = "$dl_site$dl_url";

    foreach my $patch (@patches) {
        next if -e $patch;
        $self->util->get_url( "$toaster_url/patches/$patch" );
        next if -e $patch;
        return $self->error( "failed to fetch patch $patch!" );
    }
    return @patches;
};

sub netqmail_makefile_fixups {
    my $self = shift;
    my $vpopdir = $self->setup->vpopmail->get_vpop_dir;

    # find the openssl libraries
    my $prefix = $self->conf->{'toaster_prefix'} || "/usr/local/";
    my $ssl_lib = "$prefix/lib";
    if ( !-e "$ssl_lib/libcrypto.a" ) {
        if    ( -e "/opt/local/lib/libcrypto.a" ) { $ssl_lib = "/opt/local/lib"; }
        elsif ( -e "/usr/local/lib/libcrypto.a" ) { $ssl_lib = "/usr/local/lib"; }
        elsif ( -e "/opt/lib/libcrypto.a"       ) { $ssl_lib = "/opt/lib"; }
        elsif ( -e "/usr/lib/libcrypto.a"       ) { $ssl_lib = "/usr/lib"; }
    }


    my @lines = $self->util->file_read( "Makefile" );
    foreach my $line (@lines) {
        if ( $vpopdir ne "/home/vpopmail" ) {    # fix up vpopmail home dir
            if ( $line =~ /^VPOPMAIL_HOME/ ) {
                $line = 'VPOPMAIL_HOME=' . $vpopdir;
            }
        }

        # add in the discovered ssl library location
        if ( $line =~
            /tls.o ssl_timeoutio.o -L\/usr\/local\/ssl\/lib -lssl -lcrypto/ )
        {
            $line =
              '	tls.o ssl_timeoutio.o -L' . $ssl_lib . ' -lssl -lcrypto \\';
        }

        # again with the ssl libs
        if ( $line =~
/constmap.o tls.o ssl_timeoutio.o ndelay.a -L\/usr\/local\/ssl\/lib -lssl -lcrypto \\/
          )
        {
            $line =
                '	constmap.o tls.o ssl_timeoutio.o ndelay.a -L' . $ssl_lib
              . ' -lssl -lcrypto \\';
        }
    }
    $self->util->file_write( "Makefile", lines => \@lines );
};

sub netqmail_permissions {
    my $self = shift;

    my $qmaildir = $self->get_qmail_dir;
    $self->util->chown( "$qmaildir/bin/qmail-smtpd",
        uid  => 'vpopmail',
        gid  => 'vchkpw',
    );

    $self->util->chmod(
        file_or_dir => "$qmaildir/bin/qmail-smtpd",
        mode        => '6555',
    );
};

sub netqmail_queue_extra {
    my $self = shift;

    print "netqmail: enabling QUEUE_EXTRA...\n";
    my $success = 0;
    my @lines = $self->util->file_read( "extra.h" );
    foreach my $line (@lines) {
        if ( $line =~ /#define QUEUE_EXTRA ""/ ) {
            $line = '#define QUEUE_EXTRA "Tlog\0"';
            $success++;
        }

        if ( $line =~ /#define QUEUE_EXTRALEN 0/ ) {
            $line = '#define QUEUE_EXTRALEN 5';
            $success++;
        }
    }

    if ( $success == 2 ) {
        print "success.\n";
        $self->util->file_write( "extra.h", lines => \@lines );
    }
    else {
        print "FAILED.\n";
    }
}

sub netqmail_rebuild {
    my $self = shift;

    my $qdir = $self->get_qmail_dir;

    return 1 if ! -x "$qdir/bin/qmail-smtpd";    # not yet installed

    # does not have vpopmail support
    return 1 if ! `strings $qdir/bin/qmail-smtpd | grep vpopmail`;

    return $self->util->yes_or_no(
                "toasterized qmail is already installed, do you want to reinstall",
                timeout => 30,
            );
}

sub netqmail_ssl {
    my $self = shift;
    my $make = shift;

    my $qmaildir = $self->get_qmail_dir;

    if ( ! -d "$qmaildir/control" ) {



( run in 1.080 second using v1.01-cache-2.11-cpan-71847e10f99 )