App-Yabsm

 view release on metacpan or  search on metacpan

lib/App/Yabsm/Command/Config.pm  view on Meta::CPAN

    }

    unless (App::Yabsm::Command::Daemon::yabsm_user_exists()) {
        die q(yabsm: error: cannot find user named 'yabsm')."\n";
    }

    unless (App::Yabsm::Command::Daemon::yabsm_group_exists()) {
        die q(yabsm: error: cannot find group named 'yabsm')."\n";
    }

    POSIX::setgid(scalar(getgrnam 'yabsm'));
    POSIX::setuid(scalar(getpwnam 'yabsm'));

    App::Yabsm::Backup::SSH::check_ssh_backup_config_or_die(undef, $ssh_backup, $config_ref);

    say 'all good';
}

sub print_yabsm_user_ssh_key {

    # Print the yabsm users public key to STDOUT.

lib/App/Yabsm/Command/Daemon.pm  view on Meta::CPAN

    my $config_ref      = shift;

    i_am_root_or_die();

    have_prerequisites_or_die();

    install_signal_handlers();

    create_yabsmd_runtime_dirs($config_ref);

    my ($yabsm_uid, $yabsm_gid) = create_yabsm_user_and_group($config_ref);

    open my $sudoer_fh, '>', '/etc/sudoers.d/yabsm-btrfs'
      or die "yabsm: error: cannot open '/etc/sudoers.d/yabsm-btrfs' for writing";
    my $btrfs_bin = `which btrfs 2>/dev/null`;
    print $sudoer_fh "yabsm ALL=(root) NOPASSWD: $btrfs_bin";
    close $sudoer_fh;

    if ($create_log_file) {
        open my $log_fh, '>>', '/var/log/yabsm'
          or confess q(yabsm: internal error: cannot open file '/var/log/yabsm' for writing);
        close $log_fh;
        chown $yabsm_uid, $yabsm_gid, '/var/log/yabsm';
        chmod 0644, '/var/log/yabsm';
    }

    if ($create_pid_file) {
        open my $pid_fh, '>', '/run/yabsmd.pid'
          or confess q(yabsm: internal error: cannot not open file '/run/yabsmd.pid' for writing);
        close $pid_fh;
        chown $yabsm_uid, $yabsm_gid, '/run/yabsmd.pid';
        chmod 0644, '/run/yabsmd.pid';
    }

    POSIX::setgid($yabsm_gid);
    POSIX::setuid($yabsm_uid);

    create_yabsm_user_ssh_key(0, $config_ref);

    return 1;
}

sub create_cron_scheduler {

    # Return a Schedule::Cron object that schedules every snap, ssh_backup, and

lib/App/Yabsm/Command/Daemon.pm  view on Meta::CPAN

    $SIG{USR1}   = $cleanup_and_exit;
    $SIG{USR2}   = $cleanup_and_exit;
    $SIG{VTALRM} = $cleanup_and_exit;
    $SIG{XCPU}   = $cleanup_and_exit;
    $SIG{XFSZ}   = $cleanup_and_exit;
}

sub create_yabsm_user_ssh_key {

    # Create an SSH key for the yabsm user if one doesn't already exist. This
    # function dies unless the processes ruid and rgid are that of the yabsm user
    # and group.
    #
    # If the $force value is false then only create the key if the users
    # configuration defines at least one ssh_backup, and if it is true then
    # create the key even if no ssh_backup's are defined.

    arg_count_or_die(2, 2, @_);

    my $force      = shift;
    my $config_ref = shift;

    if ($force || all_ssh_backups($config_ref)) {

        my $yabsm_uid = getpwnam('yabsm') or confess(q(yabsm: internal error: cannot find user named 'yabsm'));
        my $yabsm_gid = getgrnam('yabsm') or confess(q(yabsm: internal error: cannot find group named 'yabsm'));

        unless (POSIX::getuid() == $yabsm_uid && POSIX::getgid() == $yabsm_gid) {
            my $username  = getpwuid POSIX::getuid();
            my $groupname = getgrgid POSIX::getgid();
            confess "yabsm: internal error: expected to be running as user and group yabsm but instead running as user '$username' and group '$groupname'";
        }

        my $yabsm_user_home = yabsm_user_home($config_ref);

        my $ssh_dir  = "$yabsm_user_home/.ssh";
        my $priv_key = "$ssh_dir/id_ed25519";
        my $pub_key  = "$ssh_dir/id_ed25519.pub";

        unless (-f $priv_key && -f $pub_key) {
            system_or_die('ssh-keygen', '-t', 'ed25519', '-f', $priv_key, '-N', '');
            chown $yabsm_uid, $yabsm_gid, $priv_key, $pub_key;
            chmod 0600, $priv_key;
            chmod 0644, $pub_key;
        }

        return 1;
    }

    return 0;
}

lib/App/Yabsm/Command/Daemon.pm  view on Meta::CPAN


    unless (yabsm_group_exists()) {
        system_or_die('groupadd', 'yabsm');
    }

    # The yabsm users home dir must be reinitialized in case the user changed
    # their yabsm_dir since the last time we ran the daemon.
    system_or_die('usermod', '-m', '-d', yabsm_user_home($config_ref), 'yabsm');

    my $yabsm_uid = getpwnam('yabsm');
    my $yabsm_gid = getgrnam('yabsm');

    return wantarray ? ($yabsm_uid, $yabsm_gid) : 1;
}

sub yabsm_user_exists {

    # Return 1 if there exists a locked user on the system named 'yabsm'.

    arg_count_or_die(0, 0, @_);

    i_am_root_or_die();

lib/App/Yabsm/Tools.pm  view on Meta::CPAN


    $path =~ /^\//
      or die "yabsm: internal error: '$path' is not an absolute path starting with '/'";

    my $dir = $path;

    until (-d $dir) {
        $dir = dirname($dir);
    }

    my ($uid, $gid) = (stat $dir)[4,5];

    -d $path and return 1;

    make_path($path, {uid => $uid, group => $gid}) and return 1;

    my $username = getpwuid $<;

    die "yabsm: error: could not create path '$path' while running as user '$username'\n";
}

sub i_am_root {

    # Return 1 if current user is root and return 0 otherwise.



( run in 1.553 second using v1.01-cache-2.11-cpan-ceb78f64989 )