Hi, Michael.
system("mkdir -p $dir/.x2go"); Are you sure that nothing will break if `$dir' contains space or some other special^W sensitive characters? Of course, i understand that using such characters in homedir path is a really bad idea but.. i think apps must be secure by design.
And second thing:
and is deeper than /home/$USER I think that this comment is wrong. As I understand, it doesn't matter how deep dir, what matters is whether user's homedir exits or not. And if I've understood correctly (manual page for x2godbadmin is too short, although comments in the program are rather good) x2godbadmin doesn't create users, it only manages DB and creates ~/.x2go/ part if it's missing. So, if user is present in system (getpwnam returns info about existing users) but his homedir is missing I think that it's better to issue some warning instead of silent homedir creation (at least because this homedir won't contain files from /etc/skel, especially ~/.profile and ~/.bashrc).