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