Mail-Toaster

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

  5.26 - Jun 31, 2010
       added php build and post-install config (php.ini) support for freebsd
       replaced inline squirrelmail config with local overrides
       added build and post-install config support for lighttpd
       added sqlite build option for roundcube when install_mysql = 0
       added t/01.syntax (syntax checks for bin/*, lib/*, cgi_files/ezmlm.cgi)
       renamed port_install to install_port (see DEVELOP)
       added munin install support and configuration
       updated t_setup -s mailtoaster target (from $util-> to $toaster->)
       cleaned up more debug and log messages
       changed API for cwd_source_dir, chown, extract_archive, get_url, is_readable,
         is_writable, check_pidfile, install_package
       changed some builtin chown calls to using $util->chown
       renamed util->file_get to util->get_url
       renamed util->file_archive to archive_file
       renamed util->pidfile_check to check_pidfile
       renamed M:T:U:archive_expand to extract_archive
       for objects in classes beneath Mail::Toaster:
         new objects inherit debug and fatal settings from the Mail::Toaster object
         when loading a M:T:Utility, use $toaster->get_util instead of loading
         when a method is run, it can inherit its parents values for debug and
           fatal by adding %std_opts to its own parameter validation
         removed hundreds of places where debug settings were explicity passed.

Changes  view on Meta::CPAN

        Don't do passwd test on cygwin. Don't do network tests on Cygwin

     toaster-watcher.conf
       added install_roundcube option

     Darwin.pm
       updated $utility calls with MT 5 syntax updates
       udpated test file to use Test::More

     t/Utility.t
        only attempt to chown_system if we are root, don't run get_my_ips on
        netbsd (until I figure out why it doesn't work

     Changes
       added file to distribution

     bin/toaster_setup.pl
       added RT URL to bug section for reporting

     bin/sysadmin
       updated for MT 5 syntax

bin/toaster_audit.pl  view on Meta::CPAN

;
        $changes++;
    }

    if ( ! `grep errorlog $http_conf` ) {
        print '   server.errorlog       = "/var/log/http/error.log"';
    };

    if ( ! -d "/var/log/http" ) {
        print "    mkdir /var/log/http\n";
        print "    chown www:www /var/log/http\n";
        $changes++;
    };

    my $mess = "Consider making the changes shown above to $http_conf\n";
    _changes($changes, $mess);
    sleep 2;
};

sub interface_polling {
    return 0 unless $OSNAME eq "freebsd";

doc/Changes.html  view on Meta::CPAN


<pre><code>   added php build and post-install config (php.ini) support for freebsd
   replaced inline squirrelmail config with local overrides
   added build and post-install config support for lighttpd
   added sqlite build option for roundcube when install_mysql = 0
   added t/01.syntax (syntax checks for bin/*, lib/*, cgi_files/ezmlm.cgi)
   renamed port_install to install_port (see DEVELOP)
   added munin install support and configuration
   updated t_setup -s mailtoaster target (from $util-&gt; to $toaster-&gt;)
   cleaned up more debug and log messages
   changed API for cwd_source_dir, chown, extract_archive, get_url, is_readable,
     is_writable, check_pidfile, install_package
   changed some builtin chown calls to using $util-&gt;chown
   renamed util-&gt;file_get to util-&gt;get_url
   renamed util-&gt;file_archive to archive_file
   renamed util-&gt;pidfile_check to check_pidfile
   renamed M:T:U:archive_expand to extract_archive
   for objects in classes beneath Mail::Toaster:
     new objects inherit debug and fatal settings from the Mail::Toaster object
     when loading a M:T:Utility, use $toaster-&gt;get_util instead of loading
     when a method is run, it can inherit its parents values for debug and
       fatal by adding %std_opts to its own parameter validation
     removed hundreds of places where debug settings were explicity passed.

doc/Changes.html  view on Meta::CPAN

</dl>

<pre><code> toaster-watcher.conf
   added install_roundcube option

 Darwin.pm
   updated $utility calls with MT 5 syntax updates
   udpated test file to use Test::More

 t/Utility.t
    only attempt to chown_system if we are root, don&#39;t run get_my_ips on
    netbsd (until I figure out why it doesn&#39;t work

 Changes
   added file to distribution

 bin/toaster_setup.pl
   added RT URL to bug section for reporting

 bin/sysadmin
   updated for MT 5 syntax

doc/Changes.pod  view on Meta::CPAN


   added php build and post-install config (php.ini) support for freebsd
   replaced inline squirrelmail config with local overrides
   added build and post-install config support for lighttpd
   added sqlite build option for roundcube when install_mysql = 0
   added t/01.syntax (syntax checks for bin/*, lib/*, cgi_files/ezmlm.cgi)
   renamed port_install to install_port (see DEVELOP)
   added munin install support and configuration
   updated t_setup -s mailtoaster target (from $util-> to $toaster->)
   cleaned up more debug and log messages
   changed API for cwd_source_dir, chown, extract_archive, get_url, is_readable,
     is_writable, check_pidfile, install_package
   changed some builtin chown calls to using $util->chown
   renamed util->file_get to util->get_url
   renamed util->file_archive to archive_file
   renamed util->pidfile_check to check_pidfile
   renamed M:T:U:archive_expand to extract_archive
   for objects in classes beneath Mail::Toaster:
     new objects inherit debug and fatal settings from the Mail::Toaster object
     when loading a M:T:Utility, use $toaster->get_util instead of loading
     when a method is run, it can inherit its parents values for debug and
       fatal by adding %std_opts to its own parameter validation
     removed hundreds of places where debug settings were explicity passed.

doc/Changes.pod  view on Meta::CPAN

=back

 toaster-watcher.conf
   added install_roundcube option

 Darwin.pm
   updated $utility calls with MT 5 syntax updates
   udpated test file to use Test::More

 t/Utility.t
    only attempt to chown_system if we are root, don't run get_my_ips on
    netbsd (until I figure out why it doesn't work

 Changes
   added file to distribution

 bin/toaster_setup.pl
   added RT URL to bug section for reporting

 bin/sysadmin
   updated for MT 5 syntax

doc/modules/Utility.html  view on Meta::CPAN

<pre><code>  ############ cwd_source_dir ###################
  # Usage      : $util-&gt;cwd_source_dir( &quot;/usr/local/src&quot; );
  # Purpose    : prepare a location to build source files in
  # Returns    : 0 - failure,  1 - success
  # Parameters : S - dir - a directory to build programs in</code></pre>

</dd>
<dt id="check_homedir_ownership">check_homedir_ownership</dt>
<dd>

<p>Checks the ownership on all home directories to see if they are owned by their respective users in /etc/password. Offers to repair the permissions on incorrectly owned directories. This is useful when someone that knows better does something like ...

<pre><code>  ######### check_homedir_ownership ############
  # Usage      : $util-&gt;check_homedir_ownership();
  # Purpose    : repair user homedir ownership
  # Returns    : 0 - failure,  1 - success
  # Parameters :
  #   Optional : I - auto - no prompts, just fix everything
  # See Also   : sysadmin</code></pre>

<p>Comments: Auto mode should be run with great caution. Run it first to see the results and then, if everything looks good, run in auto mode to do the actual repairs.</p>

</dd>
<dt id="chown_system">chown_system</dt>
<dd>

<p>The advantage this sub has over a Pure Perl implementation is that it can utilize sudo to gain elevated permissions that we might not otherwise have.</p>

<pre><code>  ############### chown_system #################
  # Usage      : $util-&gt;chown_system( dir=&gt;&quot;/tmp/example&quot;, user=&gt;&#39;matt&#39; );
  # Purpose    : change the ownership of a file or directory
  # Returns    : 0 - failure,  1 - success
  # Parameters : S - dir    - the directory to chown
  #            : S - user   - a system username
  #   Optional : S - group  - a sytem group name
  #            : I - recurse - include all files/folders in directory?
  # Comments   : Uses the system chown binary
  # See Also   : n/a</code></pre>

</dd>
<dt id="clean_tmp_dir">clean_tmp_dir</dt>
<dd>

<pre><code>  ############## clean_tmp_dir ################
  # Usage      : $util-&gt;clean_tmp_dir( $dir );
  # Purpose    : clean up old build stuff before rebuilding
  # Returns    : 0 - failure,  1 - success

doc/modules/Utility.html  view on Meta::CPAN

   mode   - the permissions (numeric)

 arguments optional:
   sudo  - the output of $util-&gt;sudo

 result:
   0 - failure
   1 - success</code></pre>

</dd>
<dt id="chown">chown</dt>
<dd>

<p>Set the ownership (user and group) of a file. Will use the native perl methods (by default) but can also use system calls and prepend sudo if additional permissions are needed.</p>

<pre><code>  $util-&gt;chown(
                file_or_dir =&gt; &#39;/etc/resolv.conf&#39;,
                uid =&gt; &#39;root&#39;,
                gid =&gt; &#39;wheel&#39;,
                sudo =&gt; 1
  );

 arguments required:
   file_or_dir - a file or directory to alter permission on
   uid   - the uid or user name
   gid   - the gid or group name

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


    my $logbase  = $self->toaster->get_log_dir;
    my $counters = $self->conf->{logs_counters} || "counters";

    my $user  = $self->conf->{logs_user}  || 'qmaill';
    my $group = $self->conf->{logs_group} || 'qnofiles';

    if ( !-e $logbase ) {
        mkpath( $logbase, 0, oct('0755') )
            or return $self->error( "Couldn't create $logbase: $!", %p );
        $self->util->chown($logbase, uid=>$user, gid=>$group) or return;
    };

    if ( -w $logbase ) {
        $self->util->chown($logbase, uid=>$user, gid=>$group) or return;
    }

    my $dir = "$logbase/$counters";

    if ( ! -e $dir ) {
        eval { mkpath( $dir, 0, oct('0755') ); };
        return $self->error( "Couldn't create $dir: $!",fatal=>0) if $EVAL_ERROR;
        $self->util->chown($dir, uid=>$user, gid=>$group) or return;
    }
    $self->error( "$dir is not a directory!",fatal=>0) if ! -d $dir;

    my $script = "/usr/local/bin/maillogs";
       $script = '/usr/local/sbin/maillogs' if ! -x $script;

    return $self->error( "$script must be installed!",fatal=>0) if ! -e $script;
    return $self->error( "$script must be executable!",fatal=>0) if ! -x $script;
    return 1;
}

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

        carp "FAILED.\n $file does not exist and the user $UID has "
            . "insufficent privileges to create it!" if $verbose;
        return;
    };

    $self->counter_write( log => $file, values => { created => time, },);

    my $user = $self->{conf}{logs_user} || "qmaill";
    my $group = $self->{conf}{logs_group} || "qnofiles";

    $self->util->chown( $file, uid=>$user, gid=>$group, verbose=>0);

    print "done.\n";
    return 1;
};

sub counter_read {
    my $self  = shift;
    my %p = validate(@_, { 'file' => SCALAR, $self->get_std_opts } );
    my %args = $self->get_std_args( %p );

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

    );

    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 );

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

    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" ) {

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


    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 {

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

              . ' -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',
    );
};

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

    my $prefix  = $self->conf->{toaster_prefix}      || "/usr/local";
    my $confdir = $self->conf->{system_config_dir}   || "/usr/local/etc";
    my $clamuid = $self->conf->{install_clamav_user} || "clamav";
    my $share   = "$prefix/share/clamav";

    foreach my $file ( $share, "$share/daily.cvd", "$share/main.cvd",
        "$share/viruses.db", "$share/viruses.db2", "/var/log/clamav/freshclam.log", ) {

        if ( -e $file ) {
            print "setting the ownership of $file to $clamuid.\n";
            $self->util->chown( $file, uid => $clamuid, gid => 'clamav' );
        };
    }

    $self->util->syscmd( "pw user mod clamav -G qmail" )
        or return $self->error( "failed to add clamav to the qmail group" );

    return 1;
}

sub clamav_start {

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

            if (-f $qpath) {
                $self->audit( "  .qmail already exists in $path.");
                next PATH;
            };

            $self->audit( "  .qmail created in $path.");
            system "echo \"$spam_cmd \" >> $path/.qmail";

            my $uid = getpwnam("vpopmail");
            my $gid = getgrnam("vchkpw");
            chown( $uid, $gid, "$path/.qmail" );
            chmod oct('0644'), "$path/.qmail";
        }
    }

    return 1;
}

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

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


    $self->freebsd->conf_check(
        check => "lighttpd_enable",
        line  => 'lighttpd_enable="YES"',
    );

    my @logs = qw/ lighttpd.error.log lighttpd.access.log /;
    foreach ( @logs ) {
        $self->util->file_write( "/var/log/$_", lines => [' '] )
            if ! -e "/var/log/$_";
        $self->util->chown("/var/log/$_", uid => 'www', gid => 'www');
    };
};

sub lighttpd_config {
    my $self = shift;

    my $letc = '/usr/local/etc';
    $letc = "$letc/lighttpd" if -d "$letc/lighttpd";

    my $lconf = "$letc/lighttpd.conf";

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

    my $self = shift;

    my $user  = $self->conf->{qmail_log_user}  || "qmaill";
    my $group = $self->conf->{qmail_log_group} || "qnofiles";
    my $uid = getpwnam($user);
    my $gid = getgrnam($group);

    # if it exists, make sure it's owned by qmail:qnofiles
    my $logdir = $self->conf->{qmail_log_base} || "/var/log/mail";
    if ( -w $logdir ) {
        chown( $uid, $gid, $logdir )
            or $self->error( "Couldn't chown $logdir to $uid: $!");
        $self->audit( "maillogs: set ownership of $logdir to $user",verbose=>1 );
    }

    if ( ! -d $logdir ) {
        mkdir( $logdir, oct('0755') )
            or $self->error( "maillogs: couldn't create $logdir: $!" );
        chown( $uid, $gid, $logdir )
            or $self->error( "maillogs: couldn't chown $logdir: $!");
        $self->audit( "maillogs: created $logdir", verbose=>1 );
    }

    foreach my $prot (qw/ send smtp pop3 submit /) {
        my $dir = "$logdir/$prot";
        if ( -d $dir ) {
            $self->audit( "maillogs: create $dir, (exists)", verbose=>1 );
        }
        else {
            mkdir( $dir, oct('0755') )
              or $self->error( "maillogs: couldn't create $dir: $!" );
            $self->audit( "maillogs: created $dir", verbose=>1);
        }
        chown( $uid, $gid, $dir )
          or $self->error( "maillogs: chown $dir failed: $!");
    }
};

sub mrm {
    my $self  = shift;
    my $verbose = $self->{verbose};

    my %p = validate( @_, {
            'fatal'   => { type => BOOLEAN, optional => 1, default => 1 },
            'verbose'   => { type => BOOLEAN, optional => 1, default => $verbose },

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

OPTIONS_FILE_UNSET+=NOCACHE
OPTIONS_FILE_$spam+=SPAM_DETECTION
OPTIONS_FILE_UNSET+=SPAM_NEEDS_EMAIL
OPTIONS_FILE_SET+=TRIVIAL_PASSWORD
OPTIONS_FILE_SET+=USER_INDEX
",
            );

    if ( $conf->{qmailadmin_install_as_root} ) {
        my $gid = getgrnam("vchkpw");
        chown( 0, $gid, "/usr/local/www/cgi-bin.default/qmailadmin/qmailadmin" );
    }
}

sub qpsmtpd {
    my $self = shift;

# install Qmail::Deliverable
# install vpopmaild service

# install qpsmtpd
print '
- git clone https://github.com/qpsmtpd-dev/qpsmtpd-dev
- cp -r config.sample config
- chown smtpd:smtpd qpsmtpd
- chmod +s qpsmtpd
';

# install qpsmtpd service
print '
- services stop
- rm /var/service/smtp
- stop toaster-watcher and do previous step again
- ln -s /usr/local/src/qpsmtpd-dev/  /var/serivces/qpsmtpd
- cp /var/qmail/supervise/smtp/log/run log/run

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


sub roundcube_config_sqlite {
    my $self = shift;

    my $rcdir = "/usr/local/www/roundcube";
    my $config = "$rcdir/config/db.inc.php";

    my $spool = '/var/spool/roundcubemail';
    mkpath $spool;
    my (undef,undef,$uid,$gid) = getpwnam('www');
    chown $uid, $gid, $spool;

    # configure roundcube to use sqlite for DB
    $self->config->apply_tweaks(
        file => $config,
        changes => [
            {   search  => q{$rcmail_config['db_dsnw'] = 'mysql://roundcube:pass@localhost/roundcubemail';},
                replace => q{$rcmail_config['db_dsnw'] = 'sqlite:////var/spool/roundcubemail/sqlite.db?mode=0646';},
            },
        ],
    );

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

    }
    else {
        print "\n\nNOTICE: Be sure to install socklog!!\n\n";
    }
    $self->socklog_qmail_control( 'send', $ip, $user, $logdir );
    $self->socklog_qmail_control( 'smtp', $ip, $user, $logdir );
    $self->socklog_qmail_control( 'pop3', $ip, $user, $logdir );

    unless ( -d $logdir ) {
        mkdir( $logdir, oct('0755') ) or croak "socklog: couldn't create $logdir: $!";
        chown( $uid, $gid, $logdir ) or croak "socklog: couldn't chown  $logdir: $!";
    }

    foreach my $prot (qw/ send smtp pop3 /) {
        unless ( -d "$logdir/$prot" ) {
            mkdir( "$logdir/$prot", oct('0755') )
              or croak "socklog: couldn't create $logdir/$prot: $!";
        }
        chown( $uid, $gid, "$logdir/$prot" )
          or croak "socklog: couldn't chown $logdir/$prot: $!";
    }
}

sub socklog_qmail_control {
    my ( $self, $serv, $ip, $user, $log ) = @_;

    $ip        ||= "192.168.2.9";
    $user      ||= "qmaill";
    my $qmdir = $self->qmail->get_qmail_dir;
    my $supervise = $self->qmail->get_supervise_dir;

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

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

    my $cachedir = "/var/run/sqwebmail";
    my $prefix   = $self->conf->{toaster_prefix} || "/usr/local";

    unless ( -e $cachedir ) {
        my $uid = getpwnam("bin");
        my $gid = getgrnam("bin");
        mkdir( $cachedir, oct('0755') );
        chown( $uid, $gid, $cachedir );
    }

    my $file = "/usr/local/etc/sqwebmail/sqwebmaild";
    return if ! -w $file;

    my @lines = $self->util->file_read( $file );
    foreach my $line (@lines) { #
        if ( $line =~ /^[#]{0,1}PIDFILE/ ) {
            $line = "PIDFILE=$cachedir/sqwebmaild.pid";
        };

lib/Mail/Toaster/Setup/Maildrop.pm  view on Meta::CPAN

            clean    => 0,
            notify   => 1,
            archive  => 1,
        );
    }
    else {
        $self->util->file_write( $filterfile, lines => \@lines, mode  => '0600' );
        $self->audit("installed new $filterfile, ok");
    };

    $self->util->chown( $filterfile, uid => $user, gid => $group );

    $file = "/etc/newsyslog.conf";
    if ( -e $file  && ! `grep maildrop $file`) {
        $self->util->file_write( $file,
            lines =>
            ["/var/log/mail/maildrop.log $user:$group 644	3	1000 *	Z"],
            append => 1,
        );
    };
    return 1;

lib/Mail/Toaster/Setup/Maildrop.pm  view on Meta::CPAN

	VUSERINFO="$prefix/vpopmail/bin/vuserinfo"
	`test -x \$VUSERINFO`
	if ( \$RETURNCODE == 0)
	{
		log "   creating \$VHOME/Maildir/maildirsize for quotas"
		`\$VUSERINFO -Q \$EXT\@\$HOST`

		`test -s "\$VHOME/Maildir/maildirsize"`
   		if ( \$RETURNCODE == 0 )
   		{
     			`/usr/sbin/chown vpopmail:vchkpw \$VHOME/Maildir/maildirsize`
				`/bin/chmod 640 \$VHOME/Maildir/maildirsize`
		}
	}
	else
	{
		log "   WARNING: cannot find vuserinfo! Please edit mailfilter"
	}
}

EOMAILDROP4

lib/Mail/Toaster/Setup/Maildrop.pm  view on Meta::CPAN

    return @lines;
}

sub filter_logs {
    my $self = shift;

    my $log = $self->conf->{'qmail_log_base'} || "/var/log/mail";

    $self->util->mkdir_system( dir => $log, verbose => 0 ) if ! -d $log;

    $self->util->chown( $log,
        uid   => $self->conf->{'qmail_log_user'}  || 'qmaill',
        gid   => $self->conf->{'qmail_log_group'} || 'qnofiles',
        sudo  => $UID == 0 ? 0 : 1,
    );

    my $logf = "$log/maildrop.log";

    $self->util->file_write( $logf, lines => ["begin"] ) if ! -e $logf;

    $self->util->chown( $logf,
        uid   => $self->conf->{'vpopmail_user'}  || "vpopmail",
        gid   => $self->conf->{'vpopmail_group'} || "vchkpw",
        sudo  => $UID == 0 ? 0 : 1,
    );
}

sub imap_subscribe {
    my $self = shift;
    my $prefix = $self->conf->{'toaster_prefix'} || "/usr/local";
    my $sub_file = "$prefix/sbin/subscribeIMAP.sh";

lib/Mail/Toaster/Setup/Maildrop.pm  view on Meta::CPAN

        # if it is not there, add it
        if [ "$TEST" = "" ]; then
                echo "$1" >> $LIST
        fi
else
        # the file does not exist so we define the full list
        # and then create the file.
        FULL="INBOX\nSent\nTrash\nDrafts\n$1"

        echo -e $FULL > $LIST
        /usr/sbin/chown vpopmail:vchkpw $LIST
        /bin/chmod 644 $LIST
fi
';

    $self->util->file_write( $sub_file, lines => \@lines );

    $self->util->chmod(
        file_or_dir => $sub_file,
        mode        => '0555',
        sudo        => $UID == 0 ? 0 : 1,

lib/Mail/Toaster/Setup/Simscan.pm  view on Meta::CPAN

        }
        else {
            $self->util->file_write( $file, lines => \@lines );
        }
    }

    my $user  = $self->conf->{'simscan_user'}       || 'simscan';
    my $group = $self->conf->{'smtpd_run_as_group'} || 'qmail';

    $self->util->syscmd( "pw user mod simscan -G qmail,clamav" );
    $self->util->chown( '/var/qmail/simscan', uid => $user, gid => $group );
    $self->util->chown( '/var/qmail/bin/simscan', uid => $user, gid=>$group );
    $self->util->chmod( dir => '/var/qmail/simscan', mode => '0770' );

    if ( -x "/var/qmail/bin/simscanmk" ) {
        $self->util->syscmd( "/var/qmail/bin/simscanmk" );
        system "/var/qmail/bin/simscanmk";
    }
}

sub install_freebsd_port {
    my $self = shift;

lib/Mail/Toaster/Setup/Vpopmail.pm  view on Meta::CPAN

        return '';
    };

    if ( $self->is_newer( min => "5.3.22", cur => $version ) ) {
        my $vpopetc = $self->get_vpop_etc;
        $self->util->file_write( "$vpopetc/defaultdomain",
            lines => [ $default_domain ],
            verbose => 0,
        );

        $self->util->chown( "$vpopetc/defaultdomain",
            uid  => $self->conf->{'vpopmail_user'}  || "vpopmail",
            gid  => $self->conf->{'vpopmail_group'} || "vchkpw",
        );

        return '';
    }

    print "default domain: $default_domain\n";
    return " --enable-default-domain=$default_domain";
};

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

            or return $self->error( "couldn't chmod $file: $!", %args );
    }

    # note the conversion of ($mode) to an octal value. Very important!
    CORE::chmod( oct($mode), $file ) or
        return $self->error( "couldn't chmod $file: $!", %args);

    $self->audit("chmod $mode $file");
}

sub chown {
    my $self = shift;
    my $file = shift;
    my %p = validate( @_,
        {   'uid'  => { type => SCALAR  },
            'gid'  => { type => SCALAR  },
            'sudo' => { type => BOOLEAN, optional => 1 },
            $self->get_std_opts,
        }
    );

    my %args = $self->get_std_args( %p );
    my ( $uid, $gid, $sudo ) = ( $p{uid}, $p{gid}, $p{sudo} );

    $file or return $self->error( "missing file or dir", %args );
    return $self->error( "file $file does not exist!", %args ) if ! -e $file;

    $self->audit("chown: preparing to chown $uid $file");

    # sudo forces system chown instead of the perl builtin
    return $self->chown_system( $file,
        %args,
        user  => $uid,
        group => $gid,
    ) if $sudo;

    my ( $nuid, $ngid ); # if uid or gid is not numeric, convert it

    if ( $uid =~ /\A[0-9]+\z/ ) {
        $nuid = int($uid);
        $self->audit("  using $nuid from int($uid)");

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

    if ( $gid =~ /\A[0-9\-]+\z/ ) {
        $ngid = int( $gid );
        $self->audit("  using $ngid from int($gid)");
    }
    else {
        $ngid = getgrnam( $gid );
        return $self->error( "failed to get gid for $gid", %args) if ! defined $ngid;
        $self->audit("  converted $gid to numeric: $ngid");
    }

    chown( $nuid, $ngid, $file )
        or return $self->error( "couldn't chown $file: $!",%args);

    return 1;
}

sub chown_system {
    my $self = shift;
    my $dir = shift;
    my %p = validate( @_,
        {   'user'    => { type => SCALAR,  optional => 0, },
            'group'   => { type => SCALAR,  optional => 1, },
            'recurse' => { type => BOOLEAN, optional => 1, },
            $self->get_std_opts,
        }
    );

    my ( $user, $group, $recurse ) = ( $p{user}, $p{group}, $p{recurse} );
    my %args = $self->get_std_args( %p );

    $dir or return $self->error( "missing file or dir", %args );
    my $cmd = $self->find_bin( 'chown', %args );

    $cmd .= " -R"     if $recurse;
    $cmd .= " $user";
    $cmd .= ":$group" if $group;
    $cmd .= " $dir";

    $self->audit( "cmd: $cmd" );

    $self->syscmd( $cmd, %args ) or
        return $self->error( "couldn't chown with $cmd: $!", %args);

    my $mess;
    $mess .= "Recursively " if $recurse;
    $mess .= "changed $dir to be owned by $user";
    $self->audit( $mess );

    return 1;
}

sub clean_tmp_dir {

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

            type  => "text",
        ) or do {
            $self->audit( "$existing is already up-to-date.", %args);
            unlink $newfile if $p{clean};
            return 2;
        };
    };

    $self->audit("checking $existing", %args);

    $self->chown( $newfile,
        uid => $uid,
        gid => $gid,
        sudo => $sudo,
        %args
    )
    if ( $uid && $gid );  # set file ownership on the new file

    # set file permissions on the new file
    $self->chmod(
        file_or_dir => $existing,
        mode        => $mode,
        sudo        => $sudo,
        %args
    )
    if ( -e $existing && $mode );

    $self->install_if_changed_notify( $notify, $email, $existing, $diffie);
    $self->archive_file( $existing, %args) if ( -e $existing && $p{archive} );
    $self->install_if_changed_copy( $sudo, $newfile, $existing, $p{clean}, \%args );

    $self->chown( $existing,
        uid         => $uid,
        gid         => $gid,
        sudo        => $sudo,
        %args
    ) if ( $uid && $gid ); # set ownership on new existing file

    $self->chmod(
        file_or_dir => $existing,
        mode        => $mode,
        sudo        => $sudo,

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

sub is_readable {
    my $self = shift;
    my $file = shift or die "missing file or dir name\n";
    my %p = validate( @_, { $self->get_std_opts } );

    my %args = ( verbose => $p{verbose}, fatal => $p{fatal} );

    -e $file or return $self->error( "$file does not exist.", %args);
    -r $file or return $self->error( "$file is not readable by you ("
            . getpwuid($>)
            . "). You need to fix this, using chown or chmod.", %args);

    return 1;
}

sub is_writable {
    my $self = shift;
    my $file = shift or die "missing file or dir name\n";

    my %p = validate( @_, { $self->get_std_opts } );
    my %args = $self->get_std_args( %p );

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

        return $self->error( "failed to create $dir", %args);
    }

    if ( $p{sudo} ) {
        my $sudo = $self->sudo();

        $self->audit( "trying $sudo $mkdir -p $dir");
        $self->syscmd( "$sudo $mkdir -p $dir", %args);

        $self->audit( "setting ownership to $<.");
        my $chown = $self->find_bin( 'chown', %args);
        $self->syscmd( "$sudo $chown $< $dir", %args);

        $self->chmod( dir => $dir, mode => $mode, sudo => $sudo, %args)
            if $mode;
        return -d $dir ? 1 : 0;
    }

    $self->audit( "trying mkdir -p $dir" );

    # no root and no sudo, just try and see what happens
    $self->syscmd( "$mkdir -p $dir", %args ) or return;

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


  ############ cwd_source_dir ###################
  # Usage      : $util->cwd_source_dir( "/usr/local/src" );
  # Purpose    : prepare a location to build source files in
  # Returns    : 0 - failure,  1 - success
  # Parameters : S - dir - a directory to build programs in


=item check_homedir_ownership

Checks the ownership on all home directories to see if they are owned by their respective users in /etc/password. Offers to repair the permissions on incorrectly owned directories. This is useful when someone that knows better does something like "ch...

  ######### check_homedir_ownership ############
  # Usage      : $util->check_homedir_ownership();
  # Purpose    : repair user homedir ownership
  # Returns    : 0 - failure,  1 - success
  # Parameters :
  #   Optional : I - auto - no prompts, just fix everything
  # See Also   : sysadmin

Comments: Auto mode should be run with great caution. Run it first to see the results and then, if everything looks good, run in auto mode to do the actual repairs.


=item chown_system

The advantage this sub has over a Pure Perl implementation is that it can utilize sudo to gain elevated permissions that we might not otherwise have.


  ############### chown_system #################
  # Usage      : $util->chown_system( dir=>"/tmp/example", user=>'matt' );
  # Purpose    : change the ownership of a file or directory
  # Returns    : 0 - failure,  1 - success
  # Parameters : S - dir    - the directory to chown
  #            : S - user   - a system username
  #   Optional : S - group  - a sytem group name
  #            : I - recurse - include all files/folders in directory?
  # Comments   : Uses the system chown binary
  # See Also   : n/a


=item clean_tmp_dir


  ############## clean_tmp_dir ################
  # Usage      : $util->clean_tmp_dir( $dir );
  # Purpose    : clean up old build stuff before rebuilding
  # Returns    : 0 - failure,  1 - success

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

   mode   - the permissions (numeric)

 arguments optional:
   sudo  - the output of $util->sudo

 result:
   0 - failure
   1 - success


=item chown

Set the ownership (user and group) of a file. Will use the native perl methods (by default) but can also use system calls and prepend sudo if additional permissions are needed.

  $util->chown(
		file_or_dir => '/etc/resolv.conf',
		uid => 'root',
		gid => 'wheel',
		sudo => 1
  );

 arguments required:
   file_or_dir - a file or directory to alter permission on
   uid   - the uid or user name
   gid   - the gid or group name

t/Utility.t  view on Meta::CPAN

}

# a dir to create
ok( $util->cwd_source_dir( "$tmp/foo" ), 'cwd_source_dir' );
print "\t\t wd: " . cwd . "\n" if $verbose;

# go back to our previous working directory
chdir($cwd) or die;
print "\t\t wd: " . cwd . "\n" if $verbose;

# chown_system
my $sudo_bin = $util->find_bin( 'sudo', fatal => 0 );
if ( $UID == 0 && $sudo_bin && -x $sudo_bin ) {

    # avoid the possiblity of a sudo call in testing
    ok( $util->chown_system( $tmp, user => $<, fatal => 0), 'chown_system');
}

# clean_tmp_dir
TODO: {
    my $why = " - no test written yet";
}
ok( $util->clean_tmp_dir( $tmp ), 'clean_tmp_dir' );

print "\t\t wd: " . cwd . "\n" if $verbose;

t/Utility.t  view on Meta::CPAN

    ok( $util->cwd_source_dir( $tmp ), 'cwd_source_dir' );

    my $url = "http://www.mail-toaster.org/etc/maildrop-qmail-domain";
    ok( $util->get_url( $url, fatal=>0 ), 'get_url' );
    ok( $util->get_url( $url, dir => $tmp, fatal=>0 ), 'get_url');
}

chdir($cwd);
print "\t\t  wd: " . Cwd::cwd . "\n" if $verbose;

# chown
my $uid = getpwuid($UID);
my $gid = getgrgid($GID);
my $root = 'root';
my $grep = $util->find_bin( 'grep' );
my $wheel = `$grep wheel /etc/group` ? 'wheel' : 'root';

SKIP: {
    skip "the temp file for file_ch* is missing!", 4 if ( !-f $rwtest );

    # this one should work
    ok( $util->chown( $rwtest,
            uid   => $uid,
            gid   => $gid,
            sudo  => 0,
            fatal => 0
        ),
        'chown uid'
    );

    if ( $UID == 0 ) {
        ok( $util->chown( $rwtest,
                uid   => $root,
                gid   => $wheel,
                sudo  => 0,
                fatal => 0,
            ),
            'chown user'
        );
    }

    # try a user/group that does not exist
    ok( !$util->chown( $rwtest,
            uid   => 'frobnob6i',
            gid   => 'frobnob6i',
            sudo  => 0,
            fatal => 0
        ),
        'chown nonexisting uid'
    );

    # try a user/group that I may not have permission to
    if ( $UID != 0 && lc($OSNAME) ne 'irix') {
        ok( !$util->chown( $rwtest,
                uid   => $root,
                gid   => $wheel,
                sudo  => 0,
                fatal => 0
            ),
            'chown no perms'
        );
    }
}

# tests system_chown because sudo is set, might cause testers to freak out
#	ok ($util->chown( $rwtest, uid=>$uid, gid=>$gid, sudo=>1, fatal=>0 ), 'chown');
#	ok ( ! $util->chown( $rwtest, uid=>'frobnob6i', gid=>'frobnob6i', sudo=>1, fatal=>0 ), 'chown');
#	ok ( ! $util->chown( $rwtest, uid=>$root, gid=>$wheel, sudo=>1,fatal=>0), 'chown');

# chmod
# get the permissions of the file in octal file mode
use File::stat;
my $st = stat($rwtest) or warn "No $tmp: $!\n";
my $before = sprintf "%lo", $st->mode & oct('07777');

#$util->syscmd( "ls -al $rwtest" );   # use ls -al to view perms

# change the permissions to something slightly unique



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