--- loncom/auth/switchserver.pm	2010/03/22 20:11:08	1.25
+++ loncom/auth/switchserver.pm	2010/07/17 20:02:02	1.26
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Switch Servers Handler
 #
-# $Id: switchserver.pm,v 1.25 2010/03/22 20:11:08 droeschl Exp $
+# $Id: switchserver.pm,v 1.26 2010/07/17 20:02:02 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -83,7 +83,7 @@ sub handler {
 	    $env{'form.otherserver'} = 
 		&Apache::lonnet::spareserver(30000,undef,1);
 	}
-
+        
 	$switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
     }
 
@@ -95,6 +95,20 @@ sub handler {
 	return &do_redirect($r,$url,1)
     }
 
+    my $canhost = 1;
+    my @machinedoms = &Apache::lonnet::machine_domains($switch_to);
+    unless (grep(/^\Q$env{'user.domain'}\E/,@machinedoms)) {
+        my $machinedom = &Apache::lonnet::host_domain($env{'form.otherserver'});
+        my %defdomdefaults = &Apache::lonnet::get_domain_defaults($machinedom);
+        my %udomdefaults = &Apache::lonnet::get_domain_defaults($env{'user.domain'});
+        my $remoterev = &Apache::lonnet::get_server_loncaparev($env{'user.domain'},$env{'form.otherserver'});
+        $canhost = &Apache::lonnet::can_host_session($env{'user.domain'},$machinedom,
+                                                     $remoterev,$udomdefaults{'remotesessions'},
+                                                     $defdomdefaults{'hostedsessions'});
+    }
+
+    unless ($canhost) { return FORBIDDEN; }
+
     if ($env{'form.role'} && 
 	!exists($env{'user.role.'.$env{'form.role'}})) { return FORBIDDEN; }