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