App-FileSync
view release on metacpan or search on metacpan
lib/App/FileSync/Server.pm view on Meta::CPAN
ÎFS
run = 1 â hello ⨾ open ⨾ lock ⨾ loop ⨾ done succeed
run = 0 â index' = index â§ base' = base
=head3 PSEUDOCODE
read and check the hello against the pinned arguments
open the repo; check the client is allowed in its role
take the lock (refuse with code "locked" otherwise)
load the index and the client's base; tidy temporaries and the trash
send the policy, then serve commands until done
on any error: log it, journal the failed session, discard partial
uploads, and send the error to the client
=cut
sub run {
my ($self) = @_;
my $connection = $self->{conn};
my $ok = eval {
$self->_validate_hello($connection->recv());
my $store = $self->{store} = App::FileSync::Store->new(
root => $self->{root}, repo => $self->{repo}, language => $self->{language},
);
$self->_check_client_allowed();
$self->{lock} = $store->acquire_lock();
unless ($self->{lock}) {
$self->{refusal} = 'locked';
$self->_throw('server.locked', { repo => $self->{repo} });
}
$self->{index} = $store->load_index();
$self->{base} = $store->load_base($self->{client});
$store->clean_stale_temps();
$store->prune_trash() unless $self->{dry_run};
$self->{logger}->info('session opened', { map { $_ => $self->{$_} } qw(repo client role) });
my $policy = $store->config();
$connection->send({
ok => 1,
proto => $PROTO,
server => { os => $^O },
repo => { name => $self->{repo} },
policy => { map { $_ => $policy->{$_} } qw(ignore trash_days max_deletes max_delete_percent) },
});
$self->_loop();
1;
};
return 1 if $ok;
# The client shows this text to a person, so no Perl file and line, and
# only where in the repo, not where the server keeps it. The log is
# rewritten too: over ssh the server's stderr reaches the client. Only
# the journal, which stays on the server, keeps the absolute paths.
my $error = strip_location($@) || $self->i18n('server.unknown_error');
my $shown = $self->_for_client($error);
$self->{logger}->error($shown);
$self->_discard_uploads();
$self->_journal_failed_session($error);
eval {
$connection->send({ ok => 0, error => $shown, fatal => 1, ($self->{refusal} ? (code => $self->{refusal}) : ()) });
};
return 0;
}
# _for_client
# Purpose: take the server's file layout out of an error for the client
# (and for the log, whose stderr reaches the client over ssh).
# Entry: $text is an error as the server logs it.
# Exit: returns the text with each path under the master made relative
# to the repo ("x", not "/srv/filesync/master/docs/x") and any
# other mention of the server root replaced by a placeholder.
# Side effects: none.
sub _for_client :Private {
my ($self, $text) = @_;
# new() refuses a server without a root or a repo, so both are set. The
# store builds its paths with catdir, so match the canonical form: a root
# given as "/srv/filesync/" appears as "/srv/filesync/master/...".
my $root = File::Spec->canonpath($self->{root});
my $master = File::Spec->catdir($root, 'master', $self->{repo});
$text =~ s{\Q$master\E[\\/]}{}g;
# Only a whole path component: a root of /srv must not touch /srvdata.
my $placeholder = $self->i18n('server.root_placeholder');
$text =~ s{\Q$root\E(?=[\\/'"\s:,)]|\z)}{$placeholder}g;
return $text;
}
=head2 validate_forced_command
Purpose: check the command a client asked sshd to run against the arguments
pinned in F<authorized_keys>.
The pinned arguments are the authority for a session. sshd passes the command
the client asked for in C<SSH_ORIGINAL_COMMAND>; it must agree with the pinned
repo, client, role and protocol and ask for nothing else: in particular it may
not choose its own C<--root>.
Args: C<$original> (the requested command; undef or blank is accepted),
C<\%pinned> (C<repo>, C<client>, C<role>).
Returns: C<(1, undef)> or C<(0, $reason)>. A list, deliberately: callers
write C<my ($ok, $why) = ...>, matching L<App::FileSync/validate_relpath>.
Side Effects: none.
Usage:
my ($ok, $reason) = App::FileSync::Server::validate_forced_command($ENV{SSH_ORIGINAL_COMMAND}, \%pinned);
=head3 EXAMPLE
my ($ok, $why) = App::FileSync::Server::validate_forced_command(
'filesync-server --repo docs --client laptop --role client --proto 2',
{ repo => 'docs', client => 'laptop', role => 'client' },
); # (1, undef)
=head3 API SPECIFICATION
=head4 INPUT
[
{ name => 'original', type => 'string', optional => 1 },
{ name => 'pinned', type => 'hashref' },
]
=head4 OUTPUT
( run in 0.758 second using v1.01-cache-2.11-cpan-60884a8df76 )