summaryrefslogtreecommitdiff
path: root/nfs-ssh-fw
blob: 4eb39270349ce8a9fcacb298629b5409ecd66d1c (plain)
  1. #!/usr/bin/perl -w
  2. # Originally written by James Strandboge <jstrand1@rochester.rr.com> in
  3. # the article "Encrypted NFS with OpenSSH and Linux" found here:
  4. # http://www.samag.com/documents/s=4072/sam0203d/sam0203d.htm
  5. use strict;
  6. use File::Basename;
  7. ## CONFIGURATION
  8. my $nfs_server = "nfs1"; # the nfs server to connect to
  9. my $nfs_server_user = "james"; # a valid username on the nfs server
  10. my $use_version = "2"; # nfs-user-server uses 2, otherwise 3
  11. # would be better. Check output of
  12. # 'rpcinfo -p <servername>'
  13. my $nfsd_client_port = "2818"; # we will port forward nfsd here
  14. my $mountd_client_port = "3045"; # we will port forward mountd here
  15. my $sleep_length = "86400"; # how long to sleep before restarting
  16. # 86400 secs is one day. Note
  17. # this is overridden if a command is
  18. # specified in the server's
  19. # authorized_keys2 file
  20. # need to keep '-f', can also specify encryption algorithm, the ssh version
  21. # and the id key
  22. my $ssh_opts = "-f -c blowfish -2 -i /home/james/.ssh/id_dsa_nfs";
  23. my %rpcinfo_col = ( # change as per output of rpcinfo -p
  24. 'program' => '0',
  25. 'version' => '1',
  26. 'protocol' => '2',
  27. 'port' => '3',
  28. 'daemon' => '4'
  29. );
  30. ## END CONFIGURATION
  31. # not much should need to change below here
  32. my $prog_name = basename($0);
  33. my $nfsd_server_port = "";
  34. my $mountd_server_port = "";
  35. # for signals
  36. $SIG{INT} = sub { die "$0 interrupted and dying (does not kill ssh)\n" };
  37. my $first_time = 1;
  38. while (1) {
  39. if ($first_time) {
  40. print "$prog_name: Starting ssh/nfs forwarding&#151;\n";
  41. $first_time = 0;
  42. } else {
  43. print "$prog_name: Restarting ssh/nfs forwarding&#151;\n";
  44. }
  45. # first, get the rpcinfo
  46. my @rpcinfo = `rpcinfo -p $nfs_server`;
  47. print "My rpcinfo =\n @rpcinfo";
  48. # now get the nfsd and mountd port numbers
  49. foreach (@rpcinfo) {
  50. my @line = split;
  51. if ($line[$rpcinfo_col{"version"}] eq $use_version &&
  52. $line[$rpcinfo_col{"daemon"}] eq "nfs" &&
  53. $line[$rpcinfo_col{"protocol"}] eq "tcp") {
  54. $nfsd_server_port = $line[$rpcinfo_col{"port"}];
  55. print (" nfsd port = $nfsd_server_port");
  56. } elsif ($line[$rpcinfo_col{"version"}] eq $use_version &&
  57. $line[$rpcinfo_col{"daemon"}] eq "mountd" &&
  58. $line[$rpcinfo_col{"protocol"}] eq "tcp") {
  59. $mountd_server_port = $line[$rpcinfo_col{"port"}];
  60. print (", mountd port = $mountd_server_port\n");
  61. }
  62. }
  63. # now run ssh (if this fails, we get the error message and
  64. # retry). This should run all the time. This also won't die
  65. # unless the nfs mount is done.
  66. `/usr/bin/ssh $ssh_opts -L \
  67. $nfsd_client_port:$nfs_server:$nfsd_server_port -L \
  68. $mountd_client_port:$nfs_server:$mountd_server_port -l \
  69. $nfs_server_user $nfs_server /bin/sleep $sleep_length`;