Guacamole autologin

After updating my guacamole installation to 0.6.2 and struggling for some time to get the autologin to work again, I decided it’s time for some little instructions (mainly for my future self 🙂 )
—-Update 6.12.12— added instructions for guacamole-0.7.0

Here we go:

#euse –enable cgi vnc
#emerge tomcat guacamole php
#/etc/init.d/tomcat start
#/etc/init.d/guacd start

make guacamole itself work via tomcat and test it…

Now it gets tricky… make php work within the guacamole application.

Download the javabridge from here http://php-java-bridge.sourceforge.net/pjb/
Place the .war file in the webapps folder and access it once via http. This will unpack its content and one can pick the three jar files from WEB-INF/lib and copy them over to the guacamole’s WEB-INF/lib. Also copy the whole cgi folder over to the guacamole directory. In it, I had to place a link to the php-cgi binary.
ln -sf /usr/bin/php-cgi /var/lib/tomcat-7/webapps/guacamole/WEB-INF/cgi/i386-linux/php-cgi
Next place the following lines in the guacamole’s web.xml under <web-app>

<listener><listener-class>php.java.servlet.ContextLoaderListener</listener-class></listener>
<servlet><servlet-name>PhpJavaServlet</servlet-name><servlet-class>php.java.servlet.PhpJavaServlet</servlet-class>
</servlet>
<servlet><servlet-name>PhpCGIServlet</servlet-name><servlet-class>php.java.servlet.fastcgi.FastCGIServlet</servlet-class>
<init-param><param-name>prefer_system_php_exec</param-name><param-value>On</param-value></init-param>
<init-param><param-name>php_include_java</param-name><param-value>Off</param-value></init-param>
</servlet>
<servlet-mapping><servlet-name>PhpJavaServlet</servlet-name><url-pattern>*.phpjavabridge</url-pattern> </servlet-mapping>
<servlet-mapping><servlet-name>PhpCGIServlet</servlet-name><url-pattern>*.php</url-pattern></servlet-mapping>

After the guacamole-folder gets renamed/copied or somehow else redestributed to tomcat, php should be available within the container!

Next we need to modify the index to autologin…
copy index.xhtml to index.php and remove the xml header and place something like this in it to catch the delivered user credentials.

<?php
if (isset($_REQUEST[username])){
if (isset($_REQUEST[password])){
$username=$_REQUEST[username];
$password=$_REQUEST[password];
}
}
?>

—–
Guacamole-0.7.0: 
Add the following function in index.php right before root-ui.js gets loaded:

<script type=”text/javascript”>
window.onload = function() {

var data =
“username=<?php echo $username ?>”
+ “&password=<?php echo $password ?>”

// Log in
var xhr = new XMLHttpRequest();
xhr.open(“POST”, “login”, false);
xhr.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
xhr.send(data);

//This php part is for the redirect. if you don’t like it, leave it away…
<?php
if (isset($_REQUEST[id])){
$startvnc=$_REQUEST[id];
echo “window.location = ‘client.xhtml?id=$startvnc’;”;
}
?>

// Handle failures
if (xhr.status != 200)
throw new Error(“Invalid login”);
};
</script>

 

—–
Guacamole-0.6.2: Then add the following function just below the complete loginForm.onsubmit funtion (it is a slightly modified version of it which gets executed at loadtime and holds the credendtials captured above.)

window.onload = function() {

// Get parameters from query string
var parameters = window.location.search.substring(1);

// Get username and password from form
var data =
“username=<?php echo $username ?>”
+ “&password=<?php echo $password ?>”

// Include query parameters in submission data
if (parameters) data += “&” + parameters;

try {

// Log in
var xhr = new XMLHttpRequest();
xhr.open(“POST”, “login”, false);
xhr.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
xhr.send(data);

// Handle failures
if (xhr.status != 200)
throw new Error(“Invalid login”);

// Ensure username/password fiels are blurred after submit

username.blur();
password.blur();

resetUI();

}
catch (e) {

var loginError = document.getElementById(“login-error”);

// Display error, reset and refocus password field
loginError.textContent = e.message;
password.value = “”;
password.focus();

return false;

}

// On success, hide loginUI, get and show connection list.
return false;

}

// On success, hide loginUI, get and show connection list.
return false;

}

If you also like to get automatically directed to a specific connection, then you should add the following to the index.php, directly before comment that says “// Remove all rows from connections list”:

<?php
if (isset($_REQUEST[id])){
$startvnc=$_REQUEST[id];
echo “window.location = ‘client.xhtml?id=$startvnc’;”;
}
?>

Now you should be able to use a form somewhere with username and password field (maybe as type=hidden, behind a simple “vnc” button) and the guacamole index.php as action=.

Have fun! 🙂

Ein Gedanke zu “Guacamole autologin