Skip to content

Commit

Permalink
Merge pull request #768 from atsign-foundation/gkc/fix-761-again
Browse files Browse the repository at this point in the history
fix: remove sudoer assumption in install.sh; fix bugs in remove / rename part of install_single_binary
  • Loading branch information
XavierChanth committed Jan 30, 2024
2 parents 2f16062 + b69fa42 commit afc6719
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
Expand Up @@ -57,8 +57,8 @@ class LocalSshKeyUtil implements AtSshKeyUtil {
]).catchError((e) => throw e);

await Future.wait([
files[0].writeAsString(keyPair.privateKeyContents),
files[1].writeAsString(keyPair.publicKeyContents),
files[0].writeAsString(keyPair.privateKeyContents),
files[1].writeAsString(keyPair.publicKeyContents),
]).catchError((e) => throw e);

if (!Platform.isWindows) {
Expand Down
3 changes: 2 additions & 1 deletion packages/dart/noports_core/lib/src/srv/srv_impl.dart
Expand Up @@ -9,6 +9,7 @@ import 'package:cryptography/dart.dart';
import 'package:dartssh2/dartssh2.dart';
import 'package:meta/meta.dart';
import 'package:noports_core/srv.dart';
import 'package:noports_core/sshnp.dart';
import 'package:socket_connector/socket_connector.dart';

@visibleForTesting
Expand Down Expand Up @@ -62,7 +63,7 @@ class SrvImplExec implements Srv<Process> {
String? command = await Srv.getLocalBinaryPath();
String postfix = Platform.isWindows ? '.exe' : '';
if (command == null) {
throw Exception(
throw SshnpError(
'Unable to locate srv$postfix binary.\n'
'N.B. sshnp is expected to be compiled and run from source, not via the dart command.',
);
Expand Down
Expand Up @@ -312,8 +312,7 @@ void main() {
when(
() => mockAtClient.get(
any<AtKey>(
that: predicate(
(AtKey key) => key.key.startsWith('username.')),
that: predicate((AtKey key) => key.key.startsWith('username.')),
),
),
).thenAnswer((i) async => AtValue()..value = 'mySharedUsername');
Expand Down
24 changes: 20 additions & 4 deletions packages/dart/sshnoports/bundles/shell/install.sh
Expand Up @@ -5,6 +5,7 @@ is_root() {
[ "$(id -u)" -eq 0 ]
}

user_home=$HOME
define_env() {
script_dir="$(dirname -- "$( readlink -f -- "$0"; )")"
bin_dir="/usr/local/bin"
Expand All @@ -13,11 +14,14 @@ define_env() {
user="$SUDO_USER"
if [ -z "$user" ]; then
user="root"
else
# we are root, but via sudo
# so get home directory of SUDO_USER
user_home=$(sudo -u "$user" sh -c 'echo $HOME')
fi
else
user="$USER"
fi
user_home=$(sudo -u "$user" sh -c 'echo $HOME')
user_bin_dir="$user_home/.local/bin"
user_sshnpd_dir="$user_home/.sshnpd"
user_log_dir="$user_sshnpd_dir/logs"
Expand Down Expand Up @@ -93,17 +97,29 @@ install_single_binary() {
mkdir -p "$dest"
if test -f "$dest/$1"; then
if test -f "$dest/$1.old"; then
rm -f "$dest/$1.old" || echo "Failed to remove $dest/$1.old - aborting" && exit 1
if rm -f "$dest/$1.old"
then
echo "=> Removed $dest/$1.old"
else
echo "Failed to remove $dest/$1.old - aborting"
exit 1
fi
fi
if mv "$dest/$1" "$dest/$1.old"
then
echo "=> Renamed existing binary $dest/$1 to $dest/$1.old"
else
echo "Failed to rename $dest/$1 to $dest/$1.old - aborting"
exit 1
fi
mv "$dest/$1" "$dest/$1.old" || echo "Failed to rename $dest/$1 to $dest/$1.old - aborting" && exit 1
fi
cp -f "$script_dir/$1" "$dest/$1"

echo "=> Installed $1 to $dest"
if is_root & ! [ -f "$user_bin_dir/$1" ] ; then
mkdir -p "$user_bin_dir"
ln -sf "$dest/$1" "$user_bin_dir/$1"
echo "=> Linked $user_bin_dir/$1 to $dest"
echo "=> Linked $user_bin_dir/$1 to $dest/$1"
fi
}

Expand Down

0 comments on commit afc6719

Please sign in to comment.