[X2Go-Commits] x2goserver.git - build-baikal (branch) updated: 3.0.99-1

X2Go dev team git-admin at x2go.org
Fri Jan 3 20:52:06 CET 2014


The branch, build-baikal has been updated
       via  643997dc42f0d3c41b99393bd989d315febe6cd9 (commit)
      from  9d54d62536cb70a10e9f6705182688fc66c0f82b (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
-----------------------------------------------------------------------

Summary of changes:
 INSTALL                          |   38 ---
 debian/docs => cd                |    0
 debian/changelog                 |    7 +
 debian/control                   |    2 +-
 debian/copyright                 |    6 +-
 debian/dirs                      |    2 +
 debian/{postinst.ex => postinst} |    3 +
 debian/{preinst => preinst.bc}   |   15 +-
 debian/rules                     |   23 +-
 sql                              |   10 +-
 x2gochangestatus                 |   11 +
 x2gocleansessions                |    4 +-
 x2gocreatebase.sh                |   42 ---
 x2gocreatesession                |   17 ++
 x2godbadmin                      |  472 ++++++++++++++++++++++++++++++++
 x2godbwrapper.pm                 |  553 ++++++++++++++++++++++++++++++++++++++
 x2gogetagent                     |    8 +
 x2gogetdisplays                  |    9 +
 x2gogetports                     |    9 +
 x2gogetservers                   |    8 +-
 x2goinsertport                   |   14 +
 x2goinsertsession                |   13 +
 x2golistsessions                 |   15 +-
 x2golistsessions_root            |    2 +-
 x2golistsessions_sql             |   21 +-
 x2gomountdirs                    |   10 +-
 x2gopgwrapper                    |   37 ---
 x2gopgwrapper_local              |  175 ------------
 x2gopgwrapper_net                |  183 -------------
 x2gopgwrapper_sqlite             |  201 --------------
 x2goresume                       |   11 +
 x2goresume-session               |    8 +-
 x2goruncommand                   |    2 +-
 x2goshowblocks                   |   13 +-
 x2gosqlite.sh                    |   54 ----
 x2gosqlitewrapper                |  321 ++++++++++++++++++++++
 x2gostartagent                   |   24 +-
 x2gosuspend-session              |    7 +-
 x2goterminate-session            |    4 +-
 x2goumount                       |    5 +-
 x2goumount_session               |   14 +-
 41 files changed, 1558 insertions(+), 815 deletions(-)
 delete mode 100644 INSTALL
 copy debian/docs => cd (100%)
 copy debian/{postinst.ex => postinst} (87%)
 mode change 100644 => 100755
 rename debian/{preinst => preinst.bc} (84%)
 create mode 100755 x2gochangestatus
 delete mode 100755 x2gocreatebase.sh
 create mode 100755 x2gocreatesession
 create mode 100755 x2godbadmin
 create mode 100644 x2godbwrapper.pm
 create mode 100755 x2gogetagent
 create mode 100755 x2gogetdisplays
 create mode 100755 x2gogetports
 create mode 100755 x2goinsertport
 create mode 100755 x2goinsertsession
 delete mode 100755 x2gopgwrapper
 delete mode 100755 x2gopgwrapper_local
 delete mode 100755 x2gopgwrapper_net
 delete mode 100755 x2gopgwrapper_sqlite
 create mode 100755 x2goresume
 delete mode 100755 x2gosqlite.sh
 create mode 100755 x2gosqlitewrapper

The diff of changes is:
diff --git a/INSTALL b/INSTALL
deleted file mode 100644
index 15df0dc..0000000
--- a/INSTALL
+++ /dev/null
@@ -1,38 +0,0 @@
-install sudo,fuse,sshfs
-
-use visudo and add string in sudoers:
-%users ALL=(ALL) NOPASSWD: /usr/bin/x2gopgwrapper
-
-if you  want to allow only users from group x2gousers use X2GO system, 
-#add group "x2gousers"
-and change in sudoers  %users to %x2gousers
-
-INSTALL server scripts:
-
-mkdir /etc/x2go
-cp sql /etc/x2go/
-copy x2go* in any directory in $PATH
-daemon x2gocleansessions - clean all finished sessions, enable it by system start
-
-IF YOU WANT TO USE LOCAL DATABASE:
-you need to install postgresql
-
-echo -n local > /etc/x2go/sql
-
-use x2gocreatebase.sh to create database for x2go
-
-IF YOU WANT TO USE REMOTE DATABASE:
-echo -n "sqlserver address" > /etc/x2go/sql
-
-you need to creata DSA key for ssh connection with database server
-mkdirhier /root/.x2go/ssh/.pg/
-ssh-keygen -t dsa -f /root/.x2go/ssh/.pg/id_dsa
-press "Enter" for empty passphrase
-
-copy public part "/root/.x2go/ssh/.pg/id_dsa.pub" on computer with running sql server
-(for example with scp)
-and add it to ~postgres/.ssh/authorized_keys
-cat id_dsa.pub >> ~postgres/.ssh/authorized_keys
-
-IF YOU WANT TO USE SQLITE
-echo -n sqlite > /etc/x2go/sql
diff --git a/debian/docs b/cd
similarity index 100%
copy from debian/docs
copy to cd
diff --git a/debian/changelog b/debian/changelog
index 7e4db68..fdb9d32 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,10 @@
+x2goserver (3.0.99-1) unstable; urgency=low
+
+  * not use sudo with postgresql, wrappers in perl instead of bash
+  * use unprivileged user x2gouser with sqlite
+
+ -- Oleksandr Shneyder <oleksandr.shneyder at obviously-nice.de>  Tue, 25 Jan 2011 17:09:01 +0100
+
 x2goserver (3.0.1-9) unstable; urgency=low
 
   * changes in x2gomountdir to use with plasmoid
diff --git a/debian/control b/debian/control
index b0e9cce..db467de 100644
--- a/debian/control
+++ b/debian/control
@@ -7,7 +7,7 @@ Standards-Version: 3.7.2
 
 Package: x2goserver
 Architecture: all
-Depends: x2goagent, sudo, lsof, openssh-client, openssh-server, libconfig-simple-perl
+Depends: x2goagent, sudo, lsof, openssh-client, openssh-server, libconfig-simple-perl, makepasswd, libdbd-pg-perl, libdbd-sqlite3-perl
 Recommends: sshfs
 Description: x2goserver (daemon and tools)
  x2go is a serverbased computing environment with
diff --git a/debian/copyright b/debian/copyright
index 4a7fdf6..487b08b 100644
--- a/debian/copyright
+++ b/debian/copyright
@@ -5,7 +5,7 @@ It was downloaded from www.obviously-nice.de
 
 Upstream Author: Oleksandr Shneyder <oleksandr.shneyder at obviously-nice.de>
 
-Copyright (C) 2007  obviously nice - http://www.obviouslynice.de
+Copyright (C) 2007-2011  obviously nice - http://www.obviouslynice.de
 
 This program is free software; you can redistribute it and/or modify  
 it under the terms of the GNU General Public License as published by  
@@ -22,7 +22,7 @@ along with this program; if not, write to the
 Free Software Foundation, Inc.,                                       
 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             
 
-Copyright (C) 2007  Oleksandr Shneyder
+Copyright (C) 2007-2011  Oleksandr Shneyder
 oleksandr.shneyder at obviously-nice.de
 
 On Debian systems, the complete text of the GNU General
@@ -30,6 +30,6 @@ Public License can be found in `/usr/share/common-licenses/GPL'.
 
 
 
-The Debian packaging is (C) 2007, Oleksandr Shneyder <oleksandr.shneyder at obviously-nice.de> and
+The Debian packaging is (C) 2007-2011, Oleksandr Shneyder <oleksandr.shneyder at obviously-nice.de> and
 is licensed under the GPL, see above.
 
diff --git a/debian/dirs b/debian/dirs
index f2b2ebc..8d37723 100644
--- a/debian/dirs
+++ b/debian/dirs
@@ -1,5 +1,7 @@
 usr/bin
 usr/sbin
 etc/x2go
+etc/x2go/x2gosql
+etc/x2go/x2gosql/passwords
 usr/lib/x2go/script
 var/db/x2go
\ No newline at end of file
diff --git a/debian/postinst.ex b/debian/postinst
old mode 100644
new mode 100755
similarity index 87%
copy from debian/postinst.ex
copy to debian/postinst
index 5a6a780..67086e0
--- a/debian/postinst.ex
+++ b/debian/postinst
@@ -20,6 +20,9 @@ set -e
 
 case "$1" in
     configure)
+    chmod 700 /etc/x2go/x2gosql/passwords
+    touch /etc/x2go/x2gosql/passwords/pgadmin
+    chmod 600 /etc/x2go/x2gosql/passwords/pgadmin
     ;;
 
     abort-upgrade|abort-remove|abort-deconfigure)
diff --git a/debian/preinst b/debian/preinst.bc
similarity index 84%
rename from debian/preinst
rename to debian/preinst.bc
index 1f73c6b..054e1f1 100755
--- a/debian/preinst
+++ b/debian/preinst.bc
@@ -66,16 +66,13 @@ Press Enter to continue
          addgroup --system x2gousers
     fi
     
-    SUDOCONF=`cat /etc/sudoers | grep x2gopgwrapper | grep x2gousers`
-    if [ "$SUDOCONF" == "" ]
-    then
-          echo "
-#### X2GO section
-%x2gousers ALL=(ALL) NOPASSWD: /usr/bin/x2gopgwrapper
-" >> /etc/sudoers       
+    X2GOUSR=`getent passwd | grep x2gouser`
+    
+    if [ "$X2GOUSR" == "" ]
+    then 
+         useradd -r -d /var/db/x2go/ x2gouser
     fi
-
-    echo "Attention: If you want to allow users to log into the x2go system, you'll need to add them to the \"x2gousers\" group"
+    
     ;;
 
     abort-upgrade)
diff --git a/debian/rules b/debian/rules
index 983e78f..ac4ff98 100755
--- a/debian/rules
+++ b/debian/rules
@@ -54,20 +54,25 @@ install: build
 	dh_installdirs
 
 	# Add here commands to install the package into debian/x2goserver.
+	install  x2gochangestatus  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2gocmdexitmessage  $(CURDIR)/debian/x2goserver/usr/bin/
+	install  x2gocreatesession  $(CURDIR)/debian/x2goserver/usr/bin/
+	install  x2gogetagent  $(CURDIR)/debian/x2goserver/usr/bin/
+	install  x2gogetdisplays  $(CURDIR)/debian/x2goserver/usr/bin/
+	install  x2gogetports  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2gogetservers  $(CURDIR)/debian/x2goserver/usr/bin/
+	install  x2goinsertport  $(CURDIR)/debian/x2goserver/usr/bin/
+	install  x2goinsertsession  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2golistsessions  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2golistdesktops  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2golistsessions_root  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2golistsessions_sql  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2gomountdirs  $(CURDIR)/debian/x2goserver/usr/bin/
-	install  x2gopgwrapper  $(CURDIR)/debian/x2goserver/usr/bin/
-	install  x2gopgwrapper_local  $(CURDIR)/debian/x2goserver/usr/bin/
-	install  x2gopgwrapper_sqlite  $(CURDIR)/debian/x2goserver/usr/bin/
-	install  x2gopgwrapper_net  $(CURDIR)/debian/x2goserver/usr/bin/
+	install  x2goresume  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2goresume-session  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2goruncommand  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2goshowblocks  $(CURDIR)/debian/x2goserver/usr/bin/
+	install  x2gosqlitewrapper  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2gostartagent  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2gosuspend-session  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2gosuspend-agent  $(CURDIR)/debian/x2goserver/usr/bin/
@@ -76,11 +81,15 @@ install: build
 	install  x2goumount  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2goumount_session  $(CURDIR)/debian/x2goserver/usr/bin/
 	install  x2gosessionlimit  $(CURDIR)/debian/x2goserver/usr/bin/
-	cp  sql  $(CURDIR)/debian/x2goserver/etc/x2go/
+
+
+
+
+	cp  sql  $(CURDIR)/debian/x2goserver/etc/x2go/x2gosql/
 	cp  x2goserver.conf  $(CURDIR)/debian/x2goserver/etc/x2go/
+	cp x2godbwrapper.pm $(CURDIR)/debian/x2goserver/usr/lib/x2go/
 	install  x2gocleansessions  $(CURDIR)/debian/x2goserver/usr/sbin/
-	install  x2gocreatebase.sh  $(CURDIR)/debian/x2goserver/usr/lib/x2go/script
-	install  x2gosqlite.sh  $(CURDIR)/debian/x2goserver/usr/lib/x2go/script
+	install  x2godbadmin  $(CURDIR)/debian/x2goserver/usr/lib/x2go/script
 
 
 
diff --git a/sql b/sql
index c2c027f..dfbfca0 100644
--- a/sql
+++ b/sql
@@ -1 +1,9 @@
-local
\ No newline at end of file
+#postgres or sqlite
+backend=postgres
+
+[postgres]
+host=localhost
+port=5432
+#database admin (must have permissions to create databases and users)
+dbadmin=postgres
+
diff --git a/x2gochangestatus b/x2gochangestatus
new file mode 100755
index 0000000..f7ecfe9
--- /dev/null
+++ b/x2gochangestatus
@@ -0,0 +1,11 @@
+#!/usr/bin/perl
+use strict;
+
+
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+my $status=shift or die;
+my $sid=shift or die;
+
+db_changestatus($status, $sid);
diff --git a/x2gocleansessions b/x2gocleansessions
index 52ee57d..373326d 100755
--- a/x2gocleansessions
+++ b/x2gocleansessions
@@ -82,7 +82,7 @@ elsif ($pid == 0 )
      }       
      elsif(! check_pid (@sinfo[0], at sinfo[1], at sinfo[12]))
      {
-         system("su @sinfo[11] -c \"sudo x2gopgwrapper changestatus 'F' @sinfo[1] \" > /dev/null");
+         system("su @sinfo[11] -c \"x2gochangestatus 'F' @sinfo[1] \" > /dev/null");
 	 #print "@sinfo[1], pid @sinfo[0] not exist, changing status from @sinfo[4] to F\n";
 	 #print "(@sinfo[1])Unmounting all shares\n";	 
 	 system( "su @sinfo[11] -c \"export HOSTNAME && x2goumount_session @sinfo[1]\" 2> /dev/null");
@@ -93,7 +93,7 @@ elsif ($pid == 0 )
        {
            if(!check_stat(@sinfo[1], at sinfo[11]))
 	   {
-               system("su @sinfo[11] -c  \"sudo x2gopgwrapper changestatus 'S' @sinfo[1] \" > /dev/null");
+               system("su @sinfo[11] -c  \"x2gochangestatus 'S' @sinfo[1] \" > /dev/null");
 	       #print "@sinfo[1], is suspended, changing status from @sinfo[4] to S\n";
 	       #print "(@sinfo[1])Unmounting all shares\n";	        
 	       system( "su @sinfo[11] -c \"export HOSTNAME && x2goumount_session @sinfo[1]\" 2> /dev/null");
diff --git a/x2gocreatebase.sh b/x2gocreatebase.sh
deleted file mode 100755
index 6bb025d..0000000
--- a/x2gocreatebase.sh
+++ /dev/null
@@ -1,42 +0,0 @@
-#!/bin/bash
-
-su postgres -c "dropdb x2go_sessions"
-su postgres -c "createdb x2go_sessions"
-su postgres -c "echo \"create table sessions(
-		session_id varchar(500) primary key, 
-                display integer not null, 
-		uname varchar(100) not null, 
-		server varchar(100) not null,
-		client inet,
-		status char(1) not null default 'R',
-		init_time timestamp not null default now(),
-		last_time timestamp not null default now(),
-		cookie char(33),
-		agent_pid int,
-		gr_port int,
-		sound_port int,
-		fs_port int,
-		unique(display)
-		)\" | psql x2go_sessions"
-
-su postgres -c "echo \"create table messages(mess_id varchar(20) primary key, message text)\" | psql x2go_sessions"
-
-su postgres -c "echo \"create table user_messages(
-                mess_id varchar(20) not null, 
-		uname varchar(100) not null
-		)\" | psql x2go_sessions"
-
-su postgres -c "echo \"create table used_ports(
-                server varchar(100) not null,
-		session_id varchar(500) references sessions on delete cascade, 
-		port integer primary key
-		)\" | psql x2go_sessions"
-
-su postgres -c "echo \"create table mounts(
-                session_id varchar(500) references sessions on delete restrict,
-		path varchar(512) not null, 
-		client inet not null, 
-		primary key(path,client)
-		)\" | psql x2go_sessions"
-
-
diff --git a/x2gocreatesession b/x2gocreatesession
new file mode 100755
index 0000000..7776e6e
--- /dev/null
+++ b/x2gocreatesession
@@ -0,0 +1,17 @@
+#!/usr/bin/perl
+use strict;
+
+
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+
+my $cookie=shift or die;
+my $pid=shift or die;
+my $client=shift or die;
+my $gr_port=shift or die;
+my $snd_port=shift or die;
+my $fs_port=shift or die;
+my $sid=shift or die;
+
+db_createsession($cookie,$pid,$client,$gr_port, $snd_port, $fs_port, $sid);
diff --git a/x2godbadmin b/x2godbadmin
new file mode 100755
index 0000000..6a93e05
--- /dev/null
+++ b/x2godbadmin
@@ -0,0 +1,472 @@
+#!/usr/bin/perl
+
+use strict;
+
+use Getopt::Long;
+use Config::Simple;   
+use DBI;                                                                                                                                       
+                                                       
+                                                                                                   
+sub show_usage()
+{
+      print "X2Go SQL admin interface. Use it to create x2go database and insert or remove users or groups in x2go database\n".
+            "Usage:\nx2godbadmin --createdb\n".
+            "x2godbadmin --listusers\n".
+            "x2godbadmin --adduser|rmuser <UNIX user>\n".
+            "x2godbadmin --addgroup|rmgroup <UNIX group>\n";
+}
+
+my $help='';
+my $createdb='';
+my $adduser='';
+my $rmuser='';
+my $addgroup='';
+my $rmgroup='';
+my $listusers='';
+
+GetOptions('listusers' => \$listusers, 'createdb' => \$createdb, 'help' => \$help, 'adduser=s' => \$adduser, 
+           'addgroup=s' => \$addgroup, 'rmuser=s' => \$rmuser, 'rmgroup=s' => \$rmgroup);
+
+if ($help  || ! ( $createdb || $adduser || $rmuser || $addgroup || $rmgroup || $listusers))
+{
+      show_usage();
+      exit(0);
+}
+
+
+my $Config = new Config::Simple(syntax=>'ini');
+$Config->read('/etc/x2go/x2gosql/sql' ) or die "Can't read config file /etc/x2go/x2gosql/sql";
+if($Config->param("backend") eq 'sqlite')
+{
+      my $user="x2gouser";
+      my  ($name, $pass, $uid, $pgid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwnam($user);
+      my $dbfile="$dir/x2go_sessions";
+      
+      if(! $uid)
+      {	
+	print "Can not find user ($user)\n";
+	exit(-1);
+      }
+
+    
+    
+    if($listusers|| $adduser||$addgroup||$rmuser||$rmgroup)
+    {
+      print "Only \"--createdb\" option is available with sqlite backend\n";
+      exit(0);
+    }
+    if($createdb)
+    {
+	  if (! -d "$dir" )
+	  {
+		mkdir("$dir");
+	  }
+          if( -e $dbfile)
+	  {
+	    unlink($dbfile);
+	  }
+	  my $dbh=DBI->connect(
+	  "dbi:SQLite:dbname=$dbfile","","",{AutoCommit => 1}) or die $_;
+
+	  my $sth=$dbh->prepare("create table sessions(
+				  session_id varchar(500) primary key,
+				  display integer not null,
+				  uname varchar(100) not null,
+				  server varchar(100) not null,
+				  client inet,
+				  status char(1) not null default 'R',
+				  init_time timestamp not null default CURRENT_TIMESTAMP,
+				  last_time timestamp not null default CURRENT_TIMESTAMP,
+				  cookie char(33),
+				  agent_pid int,
+				  gr_port int,
+				  sound_port int,
+				  fs_port int,
+				  unique(display))");
+          $sth->execute() or die;
+	  
+          my $sth=$dbh->prepare("create table messages(mess_id varchar(20) primary key, message text)");
+          $sth->execute() or die;
+	  
+          my $sth=$dbh->prepare("create table user_messages(
+                mess_id varchar(20) not null,
+                uname varchar(100) not null)");
+          $sth->execute() or die;
+	  
+          my $sth=$dbh->prepare("create table used_ports(
+                server varchar(100) not null,
+                session_id varchar(500) references sessions on delete cascade,
+                port integer primary key)");
+          $sth->execute() or die;
+	  
+          my $sth=$dbh->prepare("create table mounts(
+                session_id varchar(500) references sessions on delete restrict,
+                path varchar(512) not null,
+                client inet not null,
+                primary key(path,client))");
+          $sth->execute() or die;
+	  
+          my $sth=$dbh->prepare("CREATE TRIGGER fkd_mounts_session_id
+				  BEFORE DELETE ON sessions
+				  FOR EACH ROW BEGIN
+				    SELECT CASE
+				      WHEN ((SELECT session_id FROM mounts WHERE session_id = OLD.session_id) IS NOT NULL)
+				      THEN RAISE(ABORT, 'delete on table \"sessions\" violates foreign key on table \"mounts\"')
+				    END;
+				  END;");
+          $sth->execute() or die; 
+	  
+          my $sth=$dbh->prepare("CREATE TRIGGER fkd_ports_session_id
+				  BEFORE DELETE ON sessions
+				  FOR EACH ROW 
+                                    BEGIN
+				       DELETE  FROM used_ports WHERE session_id = OLD.session_id;
+				    END;
+				  END;");
+          $sth->execute() or die; 
+	  
+	  $sth->finish();	  
+	  $dbh->disconnect();
+	  chmod(0700,"$dir");
+	  chown($uid,$pgid,"$dir");
+	  chmod(0600,"$dbfile");
+	  chown($uid,$pgid,"$dbfile");
+
+	  exit(0);
+    }
+}
+
+    my $host=$Config->param("postgres.host");
+    my $port=$Config->param("postgres.port");
+    my $dbadmin=$Config->param("postgres.dbadmin");
+    my $x2goadmin="x2godbuser";
+    my $x2goadminpass=`makepasswd`;
+    chomp($x2goadminpass);
+    my $db="x2go_sessions";
+
+    if(!$host)
+    {
+      $host='localhost';
+    }
+    if(!$port)
+    {
+      $port='5432';
+    }
+    if(!$dbadmin)
+    {
+      $dbadmin='postgres';
+    }
+
+    open (FL,"< /etc/x2go/x2gosql/passwords/pgadmin ") or die "Can't read password file /etc/x2go/x2gosql/passwords/pgadmin";
+    my $dbadminpass=<FL>;
+    close(FL);
+    chomp($dbadminpass);
+
+    my $dbh;
+    if($createdb)
+    {
+	  $dbh=DBI->connect("dbi:Pg:;host=$host;port=$port;", "$dbadmin", "$dbadminpass",{AutoCommit => 1}) or die $_;
+	  create_database();
+	  $dbh->disconnect();
+	  $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbadmin", "$dbadminpass",{AutoCommit => 1}) or die $_;
+	  create_tables();
+	  $dbh->disconnect();      
+	  exit(0);
+    }
+
+    if($listusers)
+    {
+	  $dbh=DBI->connect("dbi:Pg:;host=$host;port=$port;", "$dbadmin", "$dbadminpass",{AutoCommit => 1}) or die $_;
+	  list_users();
+	  $dbh->disconnect();
+	  exit(0);
+    }
+
+    $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbadmin", "$dbadminpass",{AutoCommit => 1}) or die $_;
+    if($adduser)
+    {
+	add_user($adduser);
+    }
+
+    if($addgroup)
+    {
+      my ($name, $passwd, $gid, $members)  = getgrnam( $addgroup); 
+      my @grp_members=split(' ',$members);
+      foreach (@grp_members)
+      {
+	chomp($_);
+	add_user($_);
+      }
+    }
+
+    if($rmuser)
+    {
+      rm_user($rmuser);
+    }
+
+    if($rmgroup)
+    {
+      my ($name, $passwd, $gid, $members)  = getgrnam( $rmgroup); 
+      my @grp_members=split(' ',$members);
+      foreach (@grp_members)
+      {
+	chomp($_);
+	rm_user($_);
+      }
+    }
+    $dbh->disconnect();      
+
+sub list_users()
+{
+      my $sth=$dbh->prepare("select rolname from pg_roles where rolname like 'x2gouser_%'");
+      $sth->execute()or die;
+      printf ("%-20s DB user\n","UNIX user");
+      print "---------------------------------------\n";
+      my @data;
+      while (@data = $sth->fetchrow_array) 
+      {
+	  @data[0]=~s/x2gouser_//;
+          printf ("%-20s x2gouser_ at data[0]\n", at data[0]);
+      }
+      $sth->finish();
+}
+
+sub rm_user()
+{
+      my $user=shift;
+      
+      print ("rm DB user x2gouser_$user\n"); 
+      
+      my $sth=$dbh->prepare("DROP OWNED BY x2gouser_$user");
+      $sth->execute();      
+      
+      my $sth=$dbh->prepare("drop USER if exists x2gouser_$user");
+      $sth->execute();
+      $sth->finish();
+
+      my ($name, $pass, $uid, $pgid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwnam($user);
+      if(! $uid)
+      {	
+	return;
+      }
+      if ( -e "$dir/.x2go/sqlpass" )
+      {
+	unlink("$dir/.x2go/sqlpass");
+      }
+}
+
+
+sub add_user()
+{
+      my $user=shift;
+      my ($name, $pass, $uid, $pgid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwnam($user);
+      if(! $uid)
+      {	
+	print "Can not find user ($user)\n";
+	return;
+      }
+      $pass=`makepasswd`;
+      chomp($pass);
+      
+      my $sth=$dbh->prepare("DROP OWNED BY x2gouser_$user");
+      $sth->{Warn}=0;
+      $sth->{PrintError}=0;          
+      $sth->execute();      
+      
+      $sth=$dbh->prepare("drop USER if exists x2gouser_$user");
+      $sth->{Warn}=0;
+      $sth->{PrintError}=0;          
+      $sth->execute();
+
+      print ("create DB user x2gouser_$user\n"); 
+      $sth=$dbh->prepare("create USER x2gouser_$user WITH ENCRYPTED PASSWORD '$pass'");
+      $sth->execute();
+
+      $sth=$dbh->prepare("GRANT INSERT, UPDATE, DELETE ON sessions, used_ports, mounts TO x2gouser_$user");
+      $sth->execute();
+  
+      $sth=$dbh->prepare("GRANT SELECT, UPDATE, DELETE ON sessions_view, mounts_view, servers_view, ports_view TO x2gouser_$user");
+      $sth->execute();
+      $sth->finish();
+
+      if (! -d "$dir/.x2go" )
+      {
+	mkdir("$dir/.x2go");
+      }
+      #save user password
+      open (FL,"> $dir/.x2go/sqlpass") or die "Can't open password file $dir/.x2go/sqlpass";
+      print FL $pass;
+      close(FL);
+      chmod(0700,"$dir/.x2go");
+      chown($uid,$pgid,"$dir/.x2go");
+      chmod(0600,"$dir/.x2go/sqlpass");
+      chown($uid,$pgid,"$dir/.x2go/sqlpass");
+}
+
+sub create_tables()
+{
+          my $sth=$dbh->prepare("
+                create table sessions(
+                session_id text primary key,
+                display integer not null,
+                uname text not null,
+                server text not null,
+                client inet,
+                status char(1) not null default 'R',
+                init_time timestamp not null default now(),
+                last_time timestamp not null default now(),
+                cookie char(33),
+                agent_pid int,
+                gr_port int,
+                sound_port int,
+                fs_port int,
+                creator_id text NOT NULL default current_user,
+                unique(display))
+                ");
+          $sth->execute() or die;
+	  
+	  $sth=$dbh->prepare("
+                create VIEW sessions_view as 
+                SELECT 
+                agent_pid, session_id, display, server, status, init_time, cookie, client, gr_port,
+				     sound_port, last_time, uname, fs_port from  sessions 
+				     where creator_id = current_user");
+          $sth->execute() or die;
+	  
+	  $sth=$dbh->prepare("
+                create VIEW servers_view as 
+                SELECT 
+                server, display, status from  sessions");
+          $sth->execute() or die;
+	  
+	  
+          
+          $sth=$dbh->prepare("create or replace RULE update_sess_priv AS ON UPDATE
+                TO sessions where (OLD.creator_id <> current_user or OLD.creator_id <> NEW.creator_id) and current_user <> '$x2goadmin'
+                DO INSTEAD NOTHING");
+          $sth->execute() or die;
+          
+          $sth=$dbh->prepare("create or replace RULE insert_sess_priv AS ON INSERT
+                TO sessions where NEW.creator_id <> current_user and current_user <> '$x2goadmin'
+                DO INSTEAD NOTHING");
+          $sth->execute() or die;
+	  
+          $sth=$dbh->prepare("create or replace RULE delete_sess_priv AS ON DELETE
+                TO sessions where OLD.creator_id <> current_user and current_user <> '$x2goadmin'
+                DO INSTEAD NOTHING");
+          $sth->execute() or die;
+	  
+          $sth=$dbh->prepare("create or replace RULE update_sess_view AS ON UPDATE
+                TO sessions_view DO INSTEAD 
+                update sessions set 
+                status=NEW.status,
+		last_time=NEW.last_time,
+		cookie=NEW.cookie,
+		agent_pid=NEW.agent_pid,
+		client=NEW.client,
+		gr_port=NEW.gr_port,
+		sound_port=NEW.sound_port,
+		fs_port=NEW.fs_port
+	        where session_id=OLD.session_id and creator_id=current_user");
+          $sth->execute() or die;	  
+
+          $sth=$dbh->prepare("create table messages(mess_id varchar(20) primary key, message text)");
+          $sth->execute() or die;
+
+          $sth=$dbh->prepare("create table user_messages(
+                mess_id text not null,
+                uname text not null)");
+          $sth->execute() or die;
+
+          $sth=$dbh->prepare("create table used_ports(
+                server text not null,
+                session_id text references sessions on delete cascade,
+                creator_id text NOT NULL default current_user,
+                port integer primary key)");
+          $sth->execute() or die;
+	  
+	  $sth=$dbh->prepare("
+                create VIEW ports_view as 
+                SELECT 
+                server, port from used_ports");
+          $sth->execute() or die;
+	  
+
+          $sth=$dbh->prepare("create or replace RULE insert_port_priv AS ON INSERT
+                TO used_ports where NEW.creator_id <> current_user and current_user <> '$x2goadmin'
+                DO INSTEAD NOTHING");
+          $sth->execute() or die;
+
+          $sth=$dbh->prepare("create or replace RULE update_port_priv AS ON UPDATE
+                TO used_ports where (NEW.creator_id <> current_user or OLD.creator_id <> current_user) and current_user <> '$x2goadmin'
+                DO INSTEAD NOTHING");
+          $sth->execute() or die;
+
+          $sth=$dbh->prepare("create or replace RULE delete_port_priv AS ON DELETE
+                TO used_ports where OLD.creator_id <> current_user and current_user <> '$x2goadmin'
+                DO INSTEAD NOTHING ");
+          $sth->execute() or die;
+
+          $sth=$dbh->prepare("create table mounts(
+                session_id text references sessions on delete restrict,
+                path text not null,
+                client inet not null,
+                creator_id text NOT NULL default current_user,
+                primary key(path,client))");
+          $sth->execute() or die;
+
+	  
+  	  $sth=$dbh->prepare("
+                create VIEW mounts_view as 
+                SELECT 
+                client,path, session_id from mounts
+				     where creator_id = current_user");
+          $sth->execute() or die;
+	  
+          $sth=$dbh->prepare("create or replace RULE delete_mounts_view AS ON DELETE
+                TO mounts_view DO INSTEAD 
+                delete from mounts
+	        where session_id=OLD.session_id and creator_id=current_user and path=OLD.path");
+          $sth->execute() or die;	  
+	  	  
+	  
+          $sth=$dbh->prepare("create or replace RULE insert_mount_priv AS ON INSERT
+                TO mounts where NEW.creator_id <> current_user and current_user <> '$x2goadmin'
+                DO INSTEAD NOTHING");
+          $sth->execute() or die;
+
+          $sth=$dbh->prepare(" create or replace RULE update_mount_priv AS ON UPDATE
+                TO mounts where (NEW.creator_id <> current_user or OLD.creator_id <> current_user) and current_user <> '$x2goadmin'
+                DO INSTEAD NOTHING");
+          $sth->execute() or die;
+
+          $sth=$dbh->prepare(" create or replace RULE delete_mount_priv AS ON DELETE
+                TO mounts where OLD.creator_id <> current_user and current_user <> '$x2goadmin'
+                DO INSTEAD NOTHING");
+          $sth->execute() or die;
+
+          $sth=$dbh->prepare("GRANT ALL PRIVILEGES ON sessions, messages, user_messages, used_ports, mounts TO $x2goadmin");
+          $sth->execute() or die;
+	  $sth->finish();
+}
+
+sub create_database
+{
+         #drop db if exists
+         my $sth=$dbh->prepare("drop database if exists x2go_sessions");
+         $sth->execute();
+         #drop x2goadmin
+         $sth=$dbh->prepare("drop user if exists $x2goadmin");
+         $sth->execute();
+         #create db
+         $sth=$dbh->prepare("create database $db");
+         $sth->execute() or die;
+         #create x2goadmin
+         $sth=$dbh->prepare("create USER $x2goadmin WITH ENCRYPTED PASSWORD '$x2goadminpass'");
+         $sth->execute() or die;
+         #save x2goadmin password
+         open (FL,"> /etc/x2go/x2gosql/passwords/x2goadmin ") or die "Can't write password file /etc/x2go/x2gosql/passwords/x2goadmin";
+         print FL $x2goadminpass;
+         close(FL);       
+	 $sth->finish();
+}
\ No newline at end of file
diff --git a/x2godbwrapper.pm b/x2godbwrapper.pm
new file mode 100644
index 0000000..3d78b4b
--- /dev/null
+++ b/x2godbwrapper.pm
@@ -0,0 +1,553 @@
+package x2godbwrapper;
+
+use strict;
+use Config::Simple;   
+use DBI;   
+
+use POSIX;
+
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+my ($uname, $pass, $uid, $pgid, $quota, $comment, $gcos, $homedir, $shell, $expire) = getpwuid(getuid());
+
+my $Config = new Config::Simple(syntax=>'ini');
+$Config->read('/etc/x2go/x2gosql/sql' ) or die "Can't read config file /etc/x2go/x2gosql/sql";
+my $backend=$Config->param("backend");
+
+my $host;
+my $port;
+my $db="x2go_sessions";
+my $dbpass;
+my $dbuser;
+
+
+if($backend ne 'postgres' && $backend ne 'sqlite')
+{
+  die "unknown backend $backend";
+}
+
+if($backend eq 'postgres')
+{
+  $host=$Config->param("postgres.host");
+  $port=$Config->param("postgres.port");
+  if(!$host)
+  {
+    $host='localhost';
+  }
+  if(!$port)
+  {
+    $port='5432';
+  }
+  my $passfile;
+  if($uname eq 'root')
+  {
+    $dbuser='x2godbuser';
+    $passfile="/etc/x2go/x2gosql/passwords/x2goadmin";
+  }
+  else
+  {
+    $dbuser="x2gouser_$uname";
+    $passfile="$homedir/.x2go/sqlpass";
+  }
+
+  open (FL,"< $passfile") or die "Can't read password file $passfile<br><b>Use x2godbadmin on server to configure database access for user $uname</b><br>";
+  $dbpass=<FL>;
+  close(FL);
+  chomp($dbpass);
+}
+
+use base 'Exporter';
+
+our @EXPORT=('db_listsessions','db_listsessions_all', 'db_getservers', 'db_getagent', 'db_resume', 'db_changestatus', 
+	     'db_getdisplays', 'db_insertsession', 'db_getports', 'db_insertport', 'db_createsession', 'db_insertmount', 
+	     'db_getmounts', 'db_deletemount', 'db_getdisplay', 'dbsys_getmounts', 'dbsys_listsessionsroot', 
+	     'dbsys_listsessionsroot_all', 'dbsys_rmsessionsroot');
+
+	     
+	     
+	     
+ 
+sub dbsys_rmsessionsroot
+{
+       my $sid=shift or die "argument \"session_id\" missed";
+       if($backend eq 'postgres')
+       {
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;
+	       
+	       my $sth=$dbh->prepare("delete from sessions  where session_id='$sid'");
+               $sth->execute()or die;
+       }
+       if($backend eq 'sqlite')
+       {
+	   `sudo -u x2gouser x2gosqlitewrapper rmsessionsroot $sid`;
+       }
+}
+
+sub dbsys_listsessionsroot
+{
+       my $server=shift or die "argument \"server\" missed";
+       if($backend eq 'postgres')
+       {
+       my @strings;
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;
+	       
+	       my $sth=$dbh->prepare("select agent_pid, session_id, display, server, status,
+				     to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,
+				     sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,
+				     to_char(now()-init_time,'SSSS'),fs_port  from  sessions
+				     where server='$server'  order by status desc");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=join('|', at data);
+               }
+	       $sth->finish();
+	       $dbh->disconnect();
+       return @strings;
+       }
+       if($backend eq 'sqlite')
+       {
+	   return split("\n",`sudo -u x2gouser x2gosqlitewrapper listsessionsroot $server`);
+       }
+}
+
+sub dbsys_listsessionsroot_all
+{
+       if($backend eq 'postgres')
+       {
+       my @strings;
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;
+	       
+	       my $sth=$dbh->prepare("select agent_pid, session_id, display, server, status,
+				     to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,
+				     sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,
+				     to_char(now()-init_time,'SSSS'),fs_port  from  sessions
+				     order by status desc");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=join('|', at data);
+               }
+	       $sth->finish();
+	       $dbh->disconnect();
+       return @strings;
+       }
+       if($backend eq 'sqlite')
+       {
+	   return split("\n",`sudo -u x2gouser x2gosqlitewrapper listsessionsroot_all`);
+       }
+}
+
+	     
+sub dbsys_getmounts
+{
+       my $sid=shift or die "argument \"session_id\" missed";
+       if($backend eq 'postgres')
+       {
+       my @strings;
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;
+	       my $sth=$dbh->prepare("select client, path from mounts where session_id='$sid'");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=join("|", at data);
+               }
+	       $sth->finish();
+	       $dbh->disconnect();
+       return @strings;
+       }
+       if($backend eq 'sqlite')
+       {
+	   return split("\n",`sudo -u x2gouser x2gosqlitewrapper getmounts $sid`);
+       }
+
+}
+
+sub db_getmounts
+{
+       my $sid=shift or die "argument \"session_id\" missed";
+       if($backend eq 'postgres')
+       {
+       my @strings;
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;
+	       my $sth=$dbh->prepare("select client, path from mounts_view where session_id='$sid'");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=join("|", at data);
+               }
+	       $sth->finish();
+	       $dbh->disconnect();
+       return @strings;
+       }
+       if($backend eq 'sqlite')
+       {
+	   return split("\n",`sudo -u x2gouser x2gosqlitewrapper getmounts $sid`);
+       }
+}
+	     
+sub db_deletemount
+{
+       my $sid=shift or die "argument \"session_id\" missed";
+       my $path=shift or die "argument \"path\" missed";
+       if($backend eq 'postgres')
+       {
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;	       
+	       my $sth=$dbh->prepare("delete from mounts_view where session_id='$sid' and path='$path'");
+               $sth->execute();
+               $sth->finish();
+	       $dbh->disconnect();
+       }
+       if($backend eq 'sqlite')
+       {
+	   `sudo -u x2gouser x2gosqlitewrapper deletemount $sid \"$path\"`;
+       }
+
+}
+
+sub db_insertmount
+{
+       my $sid=shift or die "argument \"session_id\" missed";
+       my $path=shift or die "argument \"path\" missed";
+       my $client=shift or die "argument \"client\" missed";
+       my $res_ok=1;
+       if($backend eq 'postgres')
+       {
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;	       
+	       my $sth=$dbh->prepare("insert into mounts (session_id,path,client) values  ('$sid','$path','$client')");
+               $sth->execute();
+	       if(!$sth->err())
+	       {
+		 $res_ok=1;
+	       }
+               $sth->finish();
+	       $dbh->disconnect();
+       }
+       if($backend eq 'sqlite')
+       {
+	   if( `sudo -u x2gouser x2gosqlitewrapper insertmount $sid \"$path\" $client` eq "ok")
+	   {
+	     $res_ok=1;
+	   }
+       }
+       return $res_ok;
+}
+
+	     
+sub db_insertsession
+{
+	my $display=shift or die "argument \"display\" missed";
+	my $server=shift or die "argument \"server\" missed";
+        my $sid=shift or die "argument \"session_id\" missed";
+       if($backend eq 'postgres')
+       {
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;	       
+	       my $sth=$dbh->prepare("insert into sessions (display,server,uname,session_id) values ('$display','$server','$uname','$sid')");
+               $sth->execute()or die $_;
+               $sth->finish();
+	       $dbh->disconnect();
+       }
+       if($backend eq 'sqlite')
+       {
+	   my $err=`sudo -u x2gouser x2gosqlitewrapper insertsession $display $server $sid`;
+	   if($err ne "ok")
+	   {
+	     die "$err: x2gosqlitewrapper insertsession $display $server $sid";
+	   }
+       }
+
+}
+
+sub db_createsession
+{
+       my $cookie=shift or die"argument \"cookie\" missed";
+       my $pid=shift or die"argument \"pid\" missed";
+       my $client=shift or die"argument \"client\" missed";
+       my $gr_port=shift or die"argument \"gr_port\" missed";
+       my $snd_port=shift or die"argument \"snd_port\" missed";
+       my $fs_port=shift or die"argument \"fs_port\" missed";
+       my $sid=shift or die "argument \"session_id\" missed";
+       if($backend eq 'postgres')
+       {
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;	       
+	       my $sth=$dbh->prepare("update sessions_view set status='R',last_time=now(),
+				     cookie='$cookie',agent_pid='$pid',client='$client',gr_port='$gr_port',
+				     sound_port='$snd_port',fs_port='$fs_port' where session_id='$sid'");
+               $sth->execute()or die;
+	       $sth->finish();
+	       $dbh->disconnect();
+       }
+       if($backend eq 'sqlite')
+       {
+	   my $err= `sudo -u x2gouser x2gosqlitewrapper createsession $cookie $pid $client $gr_port $snd_port $fs_port $sid`;
+	   if($err ne "ok")
+	   {
+	     die $err;
+	   }
+       }
+
+}
+
+sub db_insertport
+{
+	my $server=shift or die "argument \"server\" missed";
+	my $sid=shift or die "argument \"session_id\" missed";
+	my $sshport=shift or die "argument \"port\" missed";
+       if($backend eq 'postgres')
+       {
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;	       
+	       my $sth=$dbh->prepare("insert into used_ports (server,session_id,port) values  ('$server','$sid','$sshport')");
+               $sth->execute()or die;
+	       $sth->finish();
+	       $dbh->disconnect();
+       }
+       if($backend eq 'sqlite')
+       {
+	   `sudo -u x2gouser x2gosqlitewrapper insertport $server $sid $sshport`;
+       }
+
+}
+
+	     
+sub db_resume
+{
+       my $client=shift or die "argument \"client\" missed";
+       my $sid=shift or die "argument \"session_id\" missed";       
+       if($backend eq 'postgres')
+       {
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;	       
+	       my $sth=$dbh->prepare("update sessions_view set last_time=now(),status='R',client='$client' where session_id = '$sid'");
+               $sth->execute()or die;
+	       $sth->finish();
+	       $dbh->disconnect();
+       }
+       if($backend eq 'sqlite')
+       {
+	   `sudo -u x2gouser x2gosqlitewrapper resume $client $sid`;
+       }
+
+}
+
+sub db_changestatus
+{
+       my $status=shift or die "argument \"status\" missed";
+       my $sid=shift or die "argument \"session_id\" missed";       
+       if($backend eq 'postgres')
+       {
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;	       
+	       my $sth=$dbh->prepare("update sessions_view set last_time=now(),status='$status' where session_id = '$sid'");
+               $sth->execute()or die;
+	       $sth->finish();
+	       $dbh->disconnect();
+       }
+       if($backend eq 'sqlite')
+       {
+	   `sudo -u x2gouser x2gosqlitewrapper changestatus $status $sid`;
+       }
+
+}
+
+sub db_getdisplays
+{
+       #ignore $server
+       my $server=shift or die "argument \"server\" missed";         
+       if($backend eq 'postgres')
+       {
+       my @strings;
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;
+	       my $sth=$dbh->prepare("select display from servers_view");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]='|'. at data[0].'|';
+               }
+	       $sth->finish();
+	       $dbh->disconnect();
+       return @strings;
+       }
+       if($backend eq 'sqlite')
+       {
+	   return split("\n",`sudo -u x2gouser x2gosqlitewrapper getdisplays $server`);
+       }
+
+}
+
+sub db_getports
+{
+       #ignore $server
+       my $server=shift or die "argument \"server\" missed";         
+       if($backend eq 'postgres')
+       {
+       my @strings;
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;
+	       my $sth=$dbh->prepare("select port from ports_view");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]='|'. at data[0].'|';
+               }
+	       $sth->finish();
+	       $dbh->disconnect();
+       return @strings;
+       }
+       if($backend eq 'sqlite')
+       {
+	   return split("\n",`sudo -u x2gouser x2gosqlitewrapper getports $server`);
+       }
+
+}
+
+sub db_getservers
+{
+       if($backend eq 'postgres')
+       {
+       my @strings;
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;
+	       
+	       my $sth=$dbh->prepare("select server,count(*) from servers_view where status != 'F' group by server");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=@data[0];
+               }
+	       $sth->finish();
+	       $dbh->disconnect();
+       return @strings;
+       }
+              if($backend eq 'sqlite')
+       {
+	   return split("\n",`sudo -u x2gouser x2gosqlitewrapper getservers`);
+       }
+
+}
+
+sub db_getagent
+{
+       my $sid=shift or die "argument \"session_id\" missed";
+       my $agent;
+       if($backend eq 'postgres')
+       {
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;
+	       
+	       my $sth=$dbh->prepare("select agent_pid from sessions_view
+				      where session_id ='$sid'");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               if(@data = $sth->fetchrow_array) 
+               {
+		   $agent=@data[0];
+               }
+               $sth->finish();
+	       $dbh->disconnect();
+       }
+       if($backend eq 'sqlite')
+       {
+	   $agent=`sudo -u x2gouser x2gosqlitewrapper getagent $sid`;
+       }
+       return $agent;
+}
+
+sub db_getdisplay
+{
+       my $sid=shift or die "argument \"session_id\" missed";
+       my $display;
+       if($backend eq 'postgres')
+       {
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;
+	       
+	       my $sth=$dbh->prepare("select display from sessions_view
+				      where session_id ='$sid'");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               if(@data = $sth->fetchrow_array) 
+               {
+		   $display=@data[0];
+               }
+               $sth->finish();
+	       $dbh->disconnect();
+       }
+       if($backend eq 'sqlite')
+       {
+	   $display=`sudo -u x2gouser x2gosqlitewrapper getdisplay $sid`;
+       }
+       return $display;
+}
+sub db_listsessions
+{
+       my $server=shift or die "argument \"server\" missed";
+       if($backend eq 'postgres')
+       {
+	       my @strings;
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;
+	       
+	       my $sth=$dbh->prepare("select agent_pid, session_id, display, server, status,
+				     to_char(init_time,'DD.MM.YY*HH24:MI:SS'), cookie, client, gr_port,
+				     sound_port, to_char( last_time, 'DD.MM.YY*HH24:MI:SS'), uname,
+				     to_char(now()- init_time,'SSSS'), fs_port from  sessions_view
+				      where status !='F' and server='$server' and  
+				      (  session_id not like '%XSHAD%') order by status desc");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=join('|', at data);
+               }
+	       $sth->finish();
+	       $dbh->disconnect();
+               return @strings;
+       }
+       if($backend eq 'sqlite')
+       {
+	   return split("\n",`sudo -u x2gouser x2gosqlitewrapper listsessions $server`);
+       }
+
+}
+
+sub db_listsessions_all
+{
+       if($backend eq 'postgres')
+       {
+	       my @strings;
+	       my $dbh=DBI->connect("dbi:Pg:dbname=$db;host=$host;port=$port;", "$dbuser", "$dbpass",{AutoCommit => 1}) or die $_;
+	       
+	       my $sth=$dbh->prepare("select agent_pid, session_id, display, server, status,
+				     to_char(init_time,'DD.MM.YY*HH24:MI:SS'), cookie, client, gr_port,
+				     sound_port, to_char( last_time, 'DD.MM.YY*HH24:MI:SS'), uname,
+				     to_char(now()- init_time,'SSSS'), fs_port from  sessions_view
+				      where status !='F'  and  
+				      (  session_id not like '%XSHAD%') order by status desc");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=join('|', at data);
+               }
+	       $sth->finish();
+	       $dbh->disconnect();
+	       return @strings;
+       }
+       if($backend eq 'sqlite')
+       {
+	   return split("\n",`sudo -u x2gouser x2gosqlitewrapper listsessions_all`);
+       }
+
+}
diff --git a/x2gogetagent b/x2gogetagent
new file mode 100755
index 0000000..cf0b265
--- /dev/null
+++ b/x2gogetagent
@@ -0,0 +1,8 @@
+#!/usr/bin/perl
+use strict;
+
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+my $sid=shift or die;
+print db_getagent $sid;
diff --git a/x2gogetdisplays b/x2gogetdisplays
new file mode 100755
index 0000000..3e153e7
--- /dev/null
+++ b/x2gogetdisplays
@@ -0,0 +1,9 @@
+#!/usr/bin/perl
+use strict;
+
+
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+my $host=shift or die;
+print join("\n", db_getdisplays($host));
diff --git a/x2gogetports b/x2gogetports
new file mode 100755
index 0000000..0a1ec60
--- /dev/null
+++ b/x2gogetports
@@ -0,0 +1,9 @@
+#!/usr/bin/perl
+use strict;
+
+
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+my $host=shift or die;
+print join("\n", db_getports($host));
diff --git a/x2gogetservers b/x2gogetservers
index 02bf621..f81ed27 100755
--- a/x2gogetservers
+++ b/x2gogetservers
@@ -1,6 +1,8 @@
 #!/usr/bin/perl
 use strict;
 
-my $outp=`sudo x2gopgwrapper getservers`;
-$outp=~s/\|//g;
-print "$outp";
+
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+print join("\n", db_getservers);
diff --git a/x2goinsertport b/x2goinsertport
new file mode 100755
index 0000000..128ac5e
--- /dev/null
+++ b/x2goinsertport
@@ -0,0 +1,14 @@
+#!/usr/bin/perl
+use strict;
+
+
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+my $server=shift or die;
+my $sid=shift or die;
+my $port=shift or die;
+
+db_insertport($server,$sid,$port);
+print "inserted";
+
diff --git a/x2goinsertsession b/x2goinsertsession
new file mode 100755
index 0000000..fc616d5
--- /dev/null
+++ b/x2goinsertsession
@@ -0,0 +1,13 @@
+#!/usr/bin/perl
+use strict;
+
+
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+my $display=shift or die;
+my $server=shift or die;
+my $sid=shift or die;
+
+db_insertsession($display, $server, $sid);
+print "inserted";
diff --git a/x2golistsessions b/x2golistsessions
index 7d53683..88fc44e 100755
--- a/x2golistsessions
+++ b/x2golistsessions
@@ -3,6 +3,10 @@ use Sys::Hostname;
 use strict;
 
 
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+
 sub check_stat
 {
    my $sess=shift;
@@ -20,18 +24,17 @@ if( ! $serv)
 {
    $serv=hostname;
 }
-my $uname=$ENV{'USER'};
-my $outp;
+
+my @outp;
 if($serv eq "--all-servers")
 {
- $outp=`sudo x2gopgwrapper listsessions_all`;
+ @outp=db_listsessions_all();
 }
 else
 {
- $outp=`sudo x2gopgwrapper listsessions $serv`;
+ @outp=db_listsessions($serv);
 }
 
-my @outp=split("\n","$outp");
 for(my $i=0;$i<@outp;$i++)
 {
      @outp[$i] =~ s/ //g;
@@ -47,7 +50,7 @@ for(my $i=0;$i<@outp;$i++)
        {
            if(!check_stat(@sinfo[1]))
 	   {
-               system("sudo x2gopgwrapper changestatus 'S' @sinfo[1] > /dev/null");
+               db_changestatus( 'S', @sinfo[1] );
 	       @outp[$i] =~ s/\|R\|/\|S\|/;
                system( "x2goumount_session @sinfo[1]");
 
diff --git a/x2golistsessions_root b/x2golistsessions_root
index efb71f3..c74cbfd 100755
--- a/x2golistsessions_root
+++ b/x2golistsessions_root
@@ -43,7 +43,7 @@ for(my $i=0;$i<@outp;$i++)
        {
            if(!check_stat(@sinfo[1], at sinfo[11]))
 	   {
-               system("su - @sinfo[11] -c \"sudo x2gopgwrapper changestatus 'S' @sinfo[1]\" > /dev/null");
+               system("su - @sinfo[11] -c \"x2gochangestatus 'S' @sinfo[1]\" > /dev/null");
                @outp[$i] =~ s/\|R\|/\|S\|/;
            }
        }       
diff --git a/x2golistsessions_sql b/x2golistsessions_sql
index aa44679..071fac8 100755
--- a/x2golistsessions_sql
+++ b/x2golistsessions_sql
@@ -1,27 +1,30 @@
 #!/usr/bin/perl
 use strict;
+
+
+
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+
+
 my $serv=shift;
-my $outp;
+my @array;
 if($serv eq "--all-servers")
 {
- $outp=`sudo x2gopgwrapper listsessionsroot_all`;
+    @array=dbsys_listsessionsroot_all();
 }
 else
 {
- $outp=`sudo x2gopgwrapper listsessionsroot $serv`;
+    @array=dbsys_listsessionsroot ($serv);
 }
-$outp =~ s/ //g;
-$outp =~ s/\*/ /g;
-
-
-my @array=split("\n",$outp);
 my $i;
 for ( $i=0;$i<@array;$i++) 
 {
        my @ln=split('\|', at array[$i]);
        if(@ln[4] eq "F")
        {
-          system("sudo x2gopgwrapper rmsessionsroot @ln[1]");
+          dbsys_rmsessionsroot( @ln[1]);
        }
        print "@array[$i]\n";
 }
diff --git a/x2gomountdirs b/x2gomountdirs
index 0a5bbb3..71c277a 100755
--- a/x2gomountdirs
+++ b/x2gomountdirs
@@ -1,6 +1,9 @@
 #!/usr/bin/perl
 use strict;
 
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
 my $type=shift;
 my $session=shift;
 my $user=shift;
@@ -170,10 +173,7 @@ for(my $i=0;$i<@dirs;$i++)
             $mntpath="$mdir/$p";
        }
 
-       print "starting:sudo x2gopgwrapper insertmount $session \"$mntpath\" $host\n";
-       my $outp=`sudo x2gopgwrapper insertmount $session \"$mntpath\" $host`;
-       print "result: $outp";
-       if($outp =~ m/INSERT/)
+       if(db_insertmount( $session, $mntpath, $host))
        {
              my $code_conv=$ENV{'X2GO_ICONV'};
              if($code_conv ne "")
@@ -229,7 +229,7 @@ for(my $i=0;$i<@dirs;$i++)
     	    else
     	    {
                print "mount @dirs[$i] failed\n";
-               `sudo x2gopgwrapper deletemount $session \"$mntpath\"`;
+               db_deletemount( $session, $mntpath);
 	       rmdir($mntpath);
     	    }
        }
diff --git a/x2gopgwrapper b/x2gopgwrapper
deleted file mode 100755
index bfad849..0000000
--- a/x2gopgwrapper
+++ /dev/null
@@ -1,37 +0,0 @@
-#!/bin/bash
-
-if [ "$1" == "startshadowagent" ]
-then
-    
-    CLIENT=$2
-    
-    SHADOW_SET=${11}
-    
-    SHADOW_MODE=`echo "$SHADOW_SET"|awk '{split($0,a,"XSHAD"); print a[1]}'`
-    SHADOW_USER=`echo "$SHADOW_SET"|awk '{split($0,a,"XSHAD"); print a[2]}'`
-    SHADOW_DESKTOP=`echo "$SHADOW_SET"|awk '{split($0,a,"XSHAD"); print a[3]}'`
-
-    ANSWER=`su $SHADOW_USER -c "DISPLAY=$SHADOW_DESKTOP x2godesktopsharing client ACCESS $SUDO_USER $CLIENT"`
-    
-    if [ "$ANSWER" != "GRANT" ]
-    then
-        echo "DEN"
-        exit 
-    fi
-    OUTPUT=`su $SHADOW_USER -c "SSH_CLIENT=$CLIENT x2gostartagent $3 $4 $5 $6 $7 $8 $9 ${10} ${11}"`
-    echo $OUTPUT
-    PID=`echo $OUTPUT | awk '{print $3}'`
-    ANSWER=`su $SHADOW_USER -c "DISPLAY=$SHADOW_DESKTOP x2godesktopsharing client AGENT $PID $SUDO_USER $CLIENT"`
-    exit
-fi
-
-SQLHOST=`cat /etc/x2go/sql`
-if [ "$SQLHOST" ==  "local" ]
-then
-  x2gopgwrapper_local $@ 2> /dev/null
-elif [ "$SQLHOST" ==  "sqlite" ]
-then
-  x2gopgwrapper_sqlite $@ 2> /dev/null
-else
-  x2gopgwrapper_net $SQLHOST $@
-fi
\ No newline at end of file
diff --git a/x2gopgwrapper_local b/x2gopgwrapper_local
deleted file mode 100755
index 5f9abb1..0000000
--- a/x2gopgwrapper_local
+++ /dev/null
@@ -1,175 +0,0 @@
-#!/bin/su postgres  
-cd ~
-#use only with sudo !!
-
-UNAME=$SUDO_USER
-
-case "$1" in
-
-getdisplays)
-  echo "select '|'||display||'|' from sessions;"|psql -t x2go_sessions
-  ;;
-
-getports)
-  echo "select '|'||port||'|' from used_ports;"|psql -t x2go_sessions
-  ;;
-
-getservers)
-  echo "select server,count(*) from sessions where status != 'F' group by server;"|psql -t x2go_sessions
-  ;;
-
-listsessions)
-  echo "select agent_pid, session_id, display, server, status,\
-   to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
-   sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port from  sessions  \
-   where status !='F' and server='$2' and uname='$UNAME'  and  (  session_id not like '%XSHAD%') order by status desc;"|psql -t x2go_sessions
-  ;;
-
-listsessions_all)
-  echo "select agent_pid, session_id, display, server, status,\
-   to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
-   sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port  from  sessions  \
-   where status !='F' and uname='$UNAME' and  (  session_id not like '%XSHAD%') order by status desc;"|psql -t x2go_sessions
-  ;;
-
-listsessionsroot)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select agent_pid, session_id, display, server, status,\
-   to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
-   sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port  from  sessions  \
-   where server='$2'  order by status desc;"|psql -t x2go_sessions
-  ;;
-
-listsessionsroot_all)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select agent_pid, session_id, display, server, status,\
-   to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
-   sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port  from  sessions  \
-   order by status desc;"|psql -t x2go_sessions
-  ;;
-
-listsusp)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select session_id, display, uname, server,extract( day from now()-last_time)*24*60+extract(hour from now()-last_time)*60+extract(minute from now()-last_time)\
-   from sessions where server='$2' and status='S';"|psql -t x2go_sessions
-  ;;
-
-listallrunning)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select agent_pid, session_id, display, server, status,\
-   to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
-   sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port  from  sessions  \
-   where status='R';"|psql -t x2go_sessions
-  ;;
-
-listmails)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select user_messages.mess_id,sessions.session_id,\
-      sessions.uname,sessions.display from sessions,user_messages,\
-      messages where sessions.uname=user_messages.uname and sessions.status!='F'\
-      and messages.mess_id=user_messages.mess_id and sessions.server='$2';"|psql -t x2go_sessions
-  ;;
-
-getmail)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select message from messages where mess_id='$2';"|psql -t x2go_sessions
-  ;;
-
-rmmail)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "delete from user_messages where mess_id='$2' and uname='$3';"|psql -t x2go_sessions
-  ;;
-
-rmsessionsroot)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "delete from  sessions  \
-   where session_id='$2';"|psql -t x2go_sessions
-  ;;
-
-getagent)
-  echo "select agent_pid from sessions  where session_id = '$2';"|psql -t x2go_sessions
-  ;;
-
-getdisplay)
-  echo "select display from sessions  where session_id = '$2';"|psql -t x2go_sessions
-  ;;
-
-changestatus)
-  echo "update sessions set last_time=now(),status='$2' where session_id = '$3' and uname='$UNAME';"|psql -t x2go_sessions
-  ;;
-
-resume)
-  echo "update sessions set last_time=now(),status='R',client='$2' where session_id = '$3' and uname='$UNAME';"|psql -t x2go_sessions
-  ;;
-
-insertsession)
-  echo "insert into sessions (display,server,uname,session_id) values \
-  ('$2','$3','$UNAME','$4');"|psql x2go_sessions
-  ;;
-
-createsession)
-  echo "update sessions set status='R',last_time=now(),cookie='$2',agent_pid='$3',\
-   client='$4',gr_port='$5',sound_port='$6',fs_port='$7' where session_id='$8' and uname='$UNAME';"|psql x2go_sessions
-  ;;
-
-insertport)
-  echo "insert into used_ports (server,session_id,port) values \
-  ('$2','$3','$4');"|psql x2go_sessions
-  ;;
-
-insertmount)
-  echo "insert into mounts (session_id,path,client) values \
-  ('$2','$3','$4');"|psql x2go_sessions
-  ;;
-
-deletemount)
-  echo "delete from mounts where session_id='$2' and path='$3';"|psql x2go_sessions
-  ;;
-
-getmounts)
-  echo "select client,path from mounts where session_id = '$2';"|psql -t x2go_sessions
-  ;;
-
-*)
-  echo "$1: wrong argument"
-  ;;
-
-esac
-
diff --git a/x2gopgwrapper_net b/x2gopgwrapper_net
deleted file mode 100755
index c779836..0000000
--- a/x2gopgwrapper_net
+++ /dev/null
@@ -1,183 +0,0 @@
-#!/bin/bash
-
-cd ~
-#use only with sudo !!
-
-SERVER=$1
-UNAME=$SUDO_USER
-
-case "$2" in
-
-getdisplays)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select '|'||display||'|' from sessions;\"|psql -t x2go_sessions"
-  ;;
-
-getports)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select '|'||port||'|' from used_ports;\"|psql -t x2go_sessions"
-  ;;
-
-getservers)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select server,count(*) from sessions where status != 'F' group by server;\"|psql -t x2go_sessions"
-  ;;
-
-listsessions)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select agent_pid, session_id, display, server, status,\
-   to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
-   sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port from  sessions  \
-   where status !='F' and server='$3' and uname='$UNAME' and  (  session_id not like '%XSHAD%') order by status desc;\"|psql -t x2go_sessions"
-  ;;
-
-listsessions_all)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select agent_pid, session_id, display, server, status,\
-   to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
-   sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port  from  sessions  \
-   where status !='F' and uname='$UNAME' and  (  session_id not like '%XSHAD%') order by status desc;\"|psql -t x2go_sessions"
-  ;;
-
-listsessionsroot)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select agent_pid, session_id, display, server, status,\
-   to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
-   sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port  from  sessions  \
-   where server='$3'  order by status desc;\"|psql -t x2go_sessions"
-  ;;
-
-listsessionsroot_all)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select agent_pid, session_id, display, server, status,\
-   to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
-   sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port  from  sessions  \
-   order by status desc;\"|psql -t x2go_sessions"
-  ;;
-
-listsusp)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select session_id, display, uname, server,extract( day from now()-last_time)*24*60+extract(hour from now()-last_time)*60+extract(minute from now()-last_time)\
-  from sessions where server='$3' and status='S';\"|psql -t x2go_sessions"
-  ;;
-
-
-listallrunning)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select agent_pid, session_id, display, server, status,\
-   to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
-   sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port  from  sessions  \
-   where status='R';\"|psql -t x2go_sessions"
-  ;;
-
-listmails)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select user_messages.mess_id,sessions.session_id,\
-      sessions.uname,sessions.display from sessions,user_messages,\
-      messages where sessions.uname=user_messages.uname and sessions.status!='F'\
-      and messages.mess_id=user_messages.mess_id and sessions.server='$3';\"|psql -t x2go_sessions"
-  ;;
-
-getmail)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select message\
-      from messages where mess_id='$3';\"|psql -t x2go_sessions"
-  ;;
-
-
-rmmail)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"delete from \
-  user_messages where mess_id='$3' and uname='$4';\"|psql -t x2go_sessions"
-  ;;
-
-
-rmsessionsroot)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"delete from  sessions  \
-   where session_id='$3' ;\"|psql -t x2go_sessions"
-  ;;
-
-
-getagent)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select agent_pid from sessions  where session_id = '$3';\"|psql -t x2go_sessions"
-  ;;
-
-getdisplay)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select display from sessions  where session_id = '$3';\"|psql -t x2go_sessions"
-  ;;
-
-changestatus)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"update sessions set last_time=now(),status='$3' where session_id = '$4' and uname='$UNAME';\"|psql -t x2go_sessions"
-  ;;
-
-resume)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"update sessions set last_time=now(),status='R',client='$3' where session_id = '$4' and uname='$UNAME';\"|psql -t x2go_sessions"
-  ;;
-
-insertsession)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"insert into sessions (display,server,uname,session_id) values \
-  ('$3','$4','$UNAME','$5');\"|psql x2go_sessions"
-  ;;
-
-createsession)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"update sessions set status='R',last_time=now(),cookie='$3',agent_pid='$4',\
-   client='$5',gr_port='$6',sound_port='$7',fs_port='$8' where session_id='$9' and uname='$UNAME';\"|psql x2go_sessions"
-  ;;
-
-insertport)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"insert into used_ports (server,session_id,port) values \
-  ('$3','$4','$5');\"|psql x2go_sessions"
-  ;;
-
-insertmount)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"insert into mounts (session_id,path,client) values \
-  ('$3','$4','$5');\"|psql x2go_sessions"
-  ;;
-
-deletemount)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"delete from mounts where session_id='$3' and path='$4';\"|psql x2go_sessions"
-  ;;
-
-getmounts)
-  ssh -i /root/.x2go/ssh/.pg/id_dsa postgres@$SERVER "echo \"select client,path from mounts where session_id = '$3';\"|psql -t x2go_sessions"
-  ;;
-
-*)
-  echo "$2: wrong argument"
-  ;;
-
-esac
-
diff --git a/x2gopgwrapper_sqlite b/x2gopgwrapper_sqlite
deleted file mode 100755
index 4c0bb32..0000000
--- a/x2gopgwrapper_sqlite
+++ /dev/null
@@ -1,201 +0,0 @@
-#!/bin/bash
-#use only with sudo !!
-
-UNAME=$SUDO_USER
-
-DATABASE=/var/db/x2go/x2go_sessions
-
-
-case "$1" in
-
-getdisplays)
-  echo "select '|'||display||'|' from sessions;"|sqlite $DATABASE
-  ;;
-
-getports)
-  echo "select '|'||port||'|' from used_ports;"|sqlite $DATABASE
-  ;;
-
-getservers)
-  echo "select server,count(*) from sessions where status != 'F' group by server;"|sqlite $DATABASE
-  ;;
-
-listsessions)
-  echo "select agent_pid, session_id, display, server, status,\
-   substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),9,11),\
-   cookie,client,gr_port,sound_port,\
-   substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),9,11),\
-   uname,\
-   strftime('%s','now','localtime') - strftime('%s',init_time),fs_port from  sessions  \
-   where status !='F' and server='$2' and uname='$UNAME' and  (  session_id not like '%XSHAD%')  order by status desc;"|sqlite $DATABASE
-  ;;
-
-listsessions_all)
-  echo "select agent_pid, session_id, display, server, status,\
-   substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),9,11),\
-   cookie,client,gr_port,sound_port,\
-   substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),9,11),\
-   uname,\
-   strftime('%s','now','localtime') - strftime('%s',init_time),fs_port from  sessions  \
-   where status !='F' and uname='$UNAME' and  (  session_id not like '%XSHAD%') order by status desc;"|sqlite $DATABASE
-  ;;
-
-listsessionsroot)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select agent_pid, session_id, display, server, status,\
-   substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),9,11),\
-   cookie,client,gr_port,sound_port,\
-   substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),9,11),\
-   uname,\
-   strftime('%s','now','localtime') - strftime('%s',init_time),fs_port from  sessions  \
-   where server='$2'  order by status desc;"|sqlite $DATABASE
-  ;;
-
-listsessionsroot_all)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select agent_pid, session_id, display, server, status,\
-   substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),9,11),\
-   cookie,client,gr_port,sound_port,\
-   substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),9,11),\
-   uname,\
-   strftime('%s','now','localtime') - strftime('%s',init_time),fs_port from  sessions  \
-   order by status desc;"|sqlite $DATABASE
-  ;;
-
-listsusp)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select session_id, display, uname, server,round((strftime('%s','now','localtime') - strftime('%s',last_time))/60)\
-   from sessions where server='$2' and status='S';"|sqlite $DATABASE
-  ;;
-
-listallrunning)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select agent_pid, session_id, display, server, status,\
-   substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),9,11),\
-   cookie,client,gr_port,sound_port,\
-   substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),9,11),\
-   uname,\
-   strftime('%s','now','localtime') - strftime('%s',init_time),fs_port from  sessions  \
-   where status='R';"|sqlite $DATABASE
-  ;;
-
-listmails)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select user_messages.mess_id,sessions.session_id,\
-      sessions.uname,sessions.display from sessions,user_messages,\
-      messages where sessions.uname=user_messages.uname and sessions.status!='F'\
-      and messages.mess_id=user_messages.mess_id and sessions.server='$2';"|sqlite $DATABASE
-  ;;
-
-getmail)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select message from messages where mess_id='$2';"|sqlite $DATABASE
-  ;;
-
-rmmail)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "delete from user_messages where mess_id='$2' and uname='$3';"|sqlite $DATABASE
-  ;;
-
-rmsessionsroot)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "delete from  sessions  \
-   where session_id='$2';"|sqlite $DATABASE
-  echo "delete from  used_ports  \
-   where session_id='$2';"|sqlite $DATABASE
-  ;;
-
-getagent)
-  echo "select agent_pid from sessions  where session_id = '$2';"|sqlite $DATABASE
-  ;;
-
-getdisplay)
-  echo "select display from sessions  where session_id = '$2';"|sqlite $DATABASE
-  ;;
-
-changestatus)
-  echo "update sessions set last_time=datetime('now','localtime'),status='$2' where session_id = '$3' and uname='$UNAME';"|sqlite $DATABASE
-  ;;
-
-resume)
-  echo "update sessions set last_time=datetime('now','localtime'),status='R',client='$2' where session_id = '$3' and uname='$UNAME';"|sqlite $DATABASE
-  ;;
-
-insertsession)
-  OUTP=`echo "insert into sessions (display,server,uname,session_id, init_time, last_time) values \
-  ('$2','$3','$UNAME','$4', datetime('now','localtime'), datetime('now','localtime'));"|sqlite $DATABASE`
-  if [ "$OUTP" == "" ]
-  then
-    echo "INSERT 0 1"
-  fi
-  ;;
-
-createsession)
-  echo "update sessions set status='R',last_time=datetime('now','localtime'),cookie='$2',agent_pid='$3',\
-   client='$4',gr_port='$5',sound_port='$6',fs_port='$7' where session_id='$8' and uname='$UNAME';"|sqlite $DATABASE
-  ;;
-
-insertport)
-   OUTP=`echo "insert into used_ports (server,session_id,port) values \
-  ('$2','$3','$4');"|sqlite $DATABASE`
-  if [ "$OUTP" == "" ]
-  then
-    echo "INSERT 0 1"
-  fi
-  ;;
-
-insertmount)
-   OUTP=`echo "insert into mounts (session_id,path,client) values \
-  ('$2','$3','$4');"|sqlite $DATABASE`
-  if [ "$OUTP" == "" ]
-  then
-    echo "INSERT 0 1"
-  fi
-  ;;
-
-deletemount)
-  echo "delete from mounts where session_id='$2' and path='$3';"|sqlite $DATABASE
-  ;;
-
-getmounts)
-  echo "select client,path from mounts where session_id = '$2';"|sqlite $DATABASE
-  ;;
-
-*)
-  echo "$1: wrong argument"
-  ;;
-
-esac
-
diff --git a/x2goresume b/x2goresume
new file mode 100755
index 0000000..251fb5e
--- /dev/null
+++ b/x2goresume
@@ -0,0 +1,11 @@
+#!/usr/bin/perl
+use strict;
+
+
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+my $client=shift or die;
+my $sid=shift or die;
+
+db_resume($client, $sid);
diff --git a/x2goresume-session b/x2goresume-session
index bd14495..c9ca10a 100755
--- a/x2goresume-session
+++ b/x2goresume-session
@@ -10,9 +10,7 @@ X2GO_SET_KBD=$7
 
 
 
-X2GO_AGENT_PID=`sudo x2gopgwrapper getagent $SESSION_NAME` 
-X2GO_AGENT_PID=`echo "$X2GO_AGENT_PID"| awk {'print $1'}`
-
+X2GO_AGENT_PID=`x2gogetagent $SESSION_NAME`
 
 
 X2GO_ROOT=${HOME}/.x2go
@@ -68,8 +66,6 @@ fi
 
 echo "$NEWOPTIONS" >${SESSION_DIR}/options
 
-
-sudo x2gopgwrapper resume  $X2GO_CLIENT $SESSION_NAME  > /dev/null
-
+x2goresume  $X2GO_CLIENT $SESSION_NAME  > /dev/null
 
 kill -HUP $X2GO_AGENT_PID
diff --git a/x2goruncommand b/x2goruncommand
index 7c0ac30..4c44833 100755
--- a/x2goruncommand
+++ b/x2goruncommand
@@ -112,6 +112,6 @@ else
 fi 
 
 kill -TERM  $X2GO_AGENT_PID
-sudo x2gopgwrapper changestatus 'F' $X2GO_SESSION  > /dev/null
+x2gochangestatus 'F' $X2GO_SESSION  > /dev/null
 export HOSTNAME
 x2goumount_session $X2GO_SESSION
diff --git a/x2goshowblocks b/x2goshowblocks
index e84dcc2..911ef48 100755
--- a/x2goshowblocks
+++ b/x2goshowblocks
@@ -1,16 +1,17 @@
 #!/usr/bin/perl
 use strict;
+
+
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+
 my $uname=shift;
 my $id=shift;
 my $tbl="mounts";
-my $outp=`sudo x2gopgwrapper getmounts '$id'`;
-#print $outp;
-$outp =~ s/ //g;
-
-my @outp=split("\n","$outp");
+my @outp=dbsys_getmounts( $id);
 for(my $i=0;$i<@outp;$i++)
 {
     my $path=(split("\\|", at outp[$i]))[1];
-#    print $path;
     print `su $uname -c "lsof 2>/dev/null | grep $path"`;
 }
diff --git a/x2gosqlite.sh b/x2gosqlite.sh
deleted file mode 100755
index a42ecc1..0000000
--- a/x2gosqlite.sh
+++ /dev/null
@@ -1,54 +0,0 @@
-#!/bin/bash
-
-DATABASE=/var/db/x2go/x2go_sessions
-
-#rm $DATABASE
-
-echo "create table sessions(
-		session_id varchar(500) primary key,
-                display integer not null, 
-		uname varchar(100) not null, 
-		server varchar(100) not null,
-		client inet,
-		status char(1) not null default 'R',
-		init_time timestamp not null default CURRENT_TIMESTAMP,
-		last_time timestamp not null default CURRENT_TIMESTAMP,
-		cookie char(33),
-		agent_pid int,
-		gr_port int,
-		sound_port int,
-		fs_port int,
-		unique(display)
-		);" | sqlite $DATABASE
-
-
-echo "create table messages(mess_id varchar(20) primary key, message text);" | sqlite $DATABASE
-
-echo "create table user_messages(
-                mess_id varchar(20) not null, 
-		uname varchar(100) not null
-		);" | sqlite $DATABASE
-
-
-echo "create table used_ports(
-                server varchar(100) not null,
-		session_id varchar(500) references sessions on delete cascade, 
-		port integer primary key
-		);" | sqlite $DATABASE
-
-echo "create table mounts(
-                session_id varchar(500) references sessions on delete restrict,
-		path varchar(512) not null, 
-		client inet not null, 
-		primary key(path,client)
-		);" | sqlite $DATABASE
-
-echo "CREATE TRIGGER fkd_mounts_session_id
-BEFORE DELETE ON sessions
-FOR EACH ROW BEGIN 
-  SELECT CASE
-    WHEN ((SELECT session_id FROM mounts WHERE session_id = OLD.session_id) IS NOT NULL)
-    THEN RAISE(ABORT, 'delete on table \"sessions\" violates foreign key on table \"mounts\"')
-  END;
-END;" | sqlite $DATABASE
-            
diff --git a/x2gosqlitewrapper b/x2gosqlitewrapper
new file mode 100755
index 0000000..70d1ac0
--- /dev/null
+++ b/x2gosqlitewrapper
@@ -0,0 +1,321 @@
+#!/usr/bin/perl
+use strict;
+
+use DBI;          
+use POSIX;                                                                                                                             
+
+my $realuser=$ENV{SUDO_USER};
+my ($uname, $pass, $uid, $pgid, $quota, $comment, $gcos, $homedir, $shell, $expire) = getpwuid(getuid());
+
+if($uname ne "x2gouser")
+{
+  die "Use \"sudo -u x2gouser\" to run this programm";
+}
+
+my $cmd=shift or die "command not specified";
+
+
+my $user="x2gouser";
+my  ($name, $pass, $uid, $pgid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwnam($user);
+my $dbfile="$dir/x2go_sessions";
+my $dbfile="/var/db/x2go/x2go_sessions";
+      
+my $dbh=DBI->connect("dbi:SQLite:dbname=$dbfile","","",{AutoCommit => 1}) or die $_;
+
+
+if($cmd eq  "rmsessionsroot")
+{
+  checkroot();
+       my $sid=shift or die "argument \"session_id\" missed";
+       my $sth=$dbh->prepare("delete from sessions  where session_id='$sid'");
+       $sth->execute()or die;
+       $sth->finish();
+}
+
+elsif($cmd eq  "listsessionsroot")
+{
+  checkroot();
+       my $server=shift or die "argument \"server\" missed";
+       my @strings;	       
+	       my $sth=$dbh->prepare("select agent_pid, session_id, display, server, status,
+                                      substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),9,11),
+                                      cookie,client,gr_port,sound_port,
+                                      substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),9,11),
+                                      uname,
+                                      strftime('%s','now','localtime') - strftime('%s',init_time),fs_port from  sessions 
+                                      where server='$server'  order by status desc");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=join('|', at data);
+               }
+	       $sth->finish();
+	       print join("\n", at strings);
+}
+
+elsif($cmd eq  "listsessionsroot_all")
+{
+  checkroot();
+       my @strings;	       
+	       my $sth=$dbh->prepare("select agent_pid, session_id, display, server, status,
+                                      substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),9,11),
+                                      cookie,client,gr_port,sound_port,
+                                      substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),9,11),
+                                      uname,
+                                      strftime('%s','now','localtime') - strftime('%s',init_time),fs_port from  sessions 
+                                      order by status desc");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=join('|', at data);
+               }
+	       $sth->finish();
+	       print join("\n", at strings);
+}
+
+	     
+elsif($cmd eq  "getmounts")
+{
+       my $sid=shift or die "argument \"session_id\" missed";
+       my @strings;
+	       my $sth=$dbh->prepare("select client, path from mounts where session_id='$sid'");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=join("|", at data);
+               }
+	       $sth->finish();
+	       $dbh->disconnect();
+	       print join("\n", at strings);
+}
+
+elsif($cmd eq  "deletemount")
+{
+       my $sid=shift or die "argument \"session_id\" missed";
+       my $path=shift or die "argument \"path\" missed";
+       my $sth=$dbh->prepare("delete from mounts where session_id='$sid' and path='$path'");
+       $sth->execute();
+       $sth->finish();
+}
+
+elsif($cmd eq  "insertmount")
+{
+       my $sid=shift or die "argument \"session_id\" missed";
+       my $path=shift or die "argument \"path\" missed";
+       my $client=shift or die "argument \"client\" missed";
+       my $sth=$dbh->prepare("insert into mounts (session_id,path,client) values  ('$sid','$path','$client')");
+       $sth->execute();
+       if(!$sth->err())
+       {
+	 print "ok";
+       }
+       $sth->finish();
+}
+
+elsif($cmd eq  "insertsession")
+{
+	my $display=shift or die "argument \"display\" missed";
+	my $server=shift or die "argument \"server\" missed";
+        my $sid=shift or die "argument \"session_id\" missed";
+        my $sth=$dbh->prepare("insert into sessions (display,server,uname,session_id, init_time, last_time) values 
+                              ('$display','$server','$realuser','$sid', datetime('now','localtime'), datetime('now','localtime'))");
+        $sth->execute()or die $_;
+        $sth->finish();
+        print "ok";
+}
+
+elsif($cmd eq  "createsession")
+{
+       my $cookie=shift or die"argument \"cookie\" missed";
+       my $pid=shift or die"argument \"pid\" missed";
+       my $client=shift or die"argument \"client\" missed";
+       my $gr_port=shift or die"argument \"gr_port\" missed";
+       my $snd_port=shift or die"argument \"snd_port\" missed";
+       my $fs_port=shift or die"argument \"fs_port\" missed";
+       my $sid=shift or die "argument \"session_id\" missed";
+       my $sth=$dbh->prepare("update sessions set status='R',last_time=datetime('now','localtime'),cookie='$cookie',agent_pid='$pid',
+                             client='$client',gr_port='$gr_port',sound_port='$snd_port',fs_port='$fs_port' where session_id='$sid' and uname='$realuser'");
+       $sth->execute()or die;
+       $sth->finish();
+       print "ok";
+}
+
+elsif($cmd eq  "insertport")
+{
+	my $server=shift or die "argument \"server\" missed";
+	my $sid=shift or die "argument \"session_id\" missed";
+	my $sshport=shift or die "argument \"port\" missed";
+        my $sth=$dbh->prepare("insert into used_ports (server,session_id,port) values  ('$server','$sid','$sshport')");
+        $sth->execute()or die;
+        $sth->finish();
+
+}
+
+	     
+elsif($cmd eq  "resume")
+{
+       my $client=shift or die "argument \"client\" missed";
+       my $sid=shift or die "argument \"session_id\" missed";       
+	       my $sth=$dbh->prepare("update sessions set last_time=datetime('now','localtime'),status='R',
+                                      client='$client' where session_id = '$sid' and uname='$realuser'");
+               $sth->execute()or die;
+	       $sth->finish();
+}
+
+elsif($cmd eq  "changestatus")
+{
+       my $status=shift or die "argument \"status\" missed";
+       my $sid=shift or die "argument \"session_id\" missed";       
+	       my $sth=$dbh->prepare("update sessions set last_time=datetime('now','localtime'),
+                             status='$status' where session_id = '$sid' and uname='$realuser'");
+               $sth->execute()or die;
+	       $sth->finish();
+}
+
+elsif($cmd eq  "getdisplays")
+{
+       #ignore $server
+       my @strings;
+	       my $sth=$dbh->prepare("select display from sessions");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]='|'. at data[0].'|';
+               }
+	       $sth->finish();
+	       print join("\n", at strings);
+}
+
+elsif($cmd eq  "getports")
+{
+       #ignore $server
+       my $server=shift or die "argument \"server\" missed";         
+       my @strings;
+	       my $sth=$dbh->prepare("select port from used_ports");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]='|'. at data[0].'|';
+               }
+	       $sth->finish();
+	       print join("\n", at strings);
+}
+
+elsif($cmd eq  "getservers")
+{
+       my @strings;
+	       my $sth=$dbh->prepare("select server,count(*) from sessions where status != 'F' group by server");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=@data[0];
+               }
+	       $sth->finish();
+	       print join("\n", at strings);
+}
+
+elsif($cmd eq  "getagent")
+{
+       my $sid=shift or die "argument \"session_id\" missed";
+       my $agent;
+	       my $sth=$dbh->prepare("select agent_pid from sessions
+				      where session_id ='$sid'");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               if(@data = $sth->fetchrow_array) 
+               {
+		   $agent=@data[0];
+               }
+               $sth->finish();
+       print $agent;
+}
+
+elsif($cmd eq  "getdisplay")
+{
+       my $sid=shift or die "argument \"session_id\" missed";
+       my $display;
+	       my $sth=$dbh->prepare("select display from sessions
+				      where session_id ='$sid'");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               if(@data = $sth->fetchrow_array) 
+               {
+		   $display=@data[0];
+               }
+               $sth->finish();
+       print $display;
+}
+
+elsif($cmd eq  "listsessions")
+{
+       my $server=shift or die "argument \"server\" missed";
+	       my @strings;
+	       my $sth=$dbh->prepare("select agent_pid, session_id, display, server, status,
+                                      substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),9,11),
+                                      cookie,client,gr_port,sound_port,
+                                      substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),9,11),
+                                      uname,
+                                      strftime('%s','now','localtime') - strftime('%s',init_time),fs_port from  sessions 
+                                      where status !='F' and server='$server' and uname='$realuser' 
+                                      and  (  session_id not like '%XSHAD%')  order by status desc");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=join('|', at data);
+               }
+	       $sth->finish();
+	       print join("\n", at strings);
+}
+
+elsif($cmd eq  "listsessions_all")
+{
+	       my @strings;
+	       my $sth=$dbh->prepare("select agent_pid, session_id, display, server, status,
+                                      substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',init_time),9,11),
+                                      cookie,client,gr_port,sound_port,
+                                      substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),0,6)||substr(strftime('%d.%m.%Y*%H:%M:%S',last_time),9,11),
+                                      uname,
+                                      strftime('%s','now','localtime') - strftime('%s',init_time),fs_port from  sessions 
+                                      where status !='F' and uname='$realuser' and  (  session_id not like '%XSHAD%')  order by status desc");
+               $sth->execute()or die;
+               my @data;
+	       my $i=0;
+               while (@data = $sth->fetchrow_array) 
+               {
+		   @strings[$i++]=join('|', at data);
+               }
+	       $sth->finish();
+	       print join("\n", at strings);
+}
+else
+{
+      print "unknown command $cmd\n";
+}
+
+$dbh->disconnect();
+
+
+
+sub checkroot
+{
+  if ($realuser ne "root")
+  {
+    die "$realuser, you can not do this job";
+  }
+}
\ No newline at end of file
diff --git a/x2gostartagent b/x2gostartagent
index 0dea745..75a5cc4 100755
--- a/x2gostartagent
+++ b/x2gostartagent
@@ -29,10 +29,11 @@ then
     
     if [ "$SHADOW_USER" != "$USER" ]
     then
-	OUTPUT=`sudo x2gopgwrapper startshadowagent $X2GO_CLIENT $@`
-	if [ "$OUTPUT" == "DEN" ]
+        
+        OUTPUT=`x2godesktopsharing client $X2GO_CLIENT $@`
+        if [ "$OUTPUT" == "DENY" ]
 	then
-            echo "ACCESS DENIED"
+            echo "ACCESS DENIED" 1>&2
             exit -1
 	fi
 	X2GO_COOKIE=`echo $OUTPUT | awk '{print $2}'`
@@ -56,7 +57,7 @@ LWORD=`echo $LIMIT | awk '{print $1}'`
 
 if [ "$LWORD" == "LIMIT" ]
 then
-   echo  $LIMIT
+   echo  $LIMIT 1>&2
    exit -1
 fi
 
@@ -80,10 +81,10 @@ then
 fi
 
 
-USED_DISPLAYS=`sudo x2gopgwrapper getdisplays $HOSTNAME`
+USED_DISPLAYS=`x2gogetdisplays $HOSTNAME`
 
 
-while [ "`echo \"$OUTPUT\" | awk '{print $1,$3}'`" != "INSERT 1" ]
+while [ "$OUTPUT"  != "inserted" ]
 do
  X2GO_PORT=$(($X2GO_PORT + 1))
  X2GO_PORT=`echo "for(\\$i=$X2GO_PORT;\\$br ne \"true\";\\$i++){ if(\"$USED_DISPLAYS\" =~ m/\\|\\$i\\|/){\\$br=\"false\";}else{\\$br=\"true\";print \\$i;}}"|perl`
@@ -98,18 +99,18 @@ do
        SESSION_NAME="$SESSION_NAME"_st${SESSION_TYPE}${X2GO_CMD}_dp${COLORDEPTH}
        SESSION_NAME=`echo "$SESSION_NAME" | sed  -e  "s/:/PP/g"`
    fi
-   OUTPUT=`sudo x2gopgwrapper insertsession $X2GO_PORT $HOSTNAME $SESSION_NAME`
+   OUTPUT=`x2goinsertsession $X2GO_PORT $HOSTNAME $SESSION_NAME`
  fi 
 done
 
 
-USED_PORTS=`sudo x2gopgwrapper getports $HOSTNAME`
+USED_PORTS=`x2gogetports $HOSTNAME`
 
 
 while [ "$GR_PORT" == "" ] || [ "$SOUND_PORT" == "" ] || [ "$FS_PORT" == "" ]
 do
   OUTPUT=""
-  while [ "`echo \"$OUTPUT\" | awk '{print $1,$3}'`" != "INSERT 1" ]
+  while [ "$OUTPUT"  != "inserted" ]
   do
     SSH_PORT=$(($SSH_PORT + 1))
 
@@ -124,7 +125,7 @@ do
                                   Proto => 'tcp',Listen => 1,Reuse =>1 ) or die ;print \"OK\";close(\\$sock);"|perl 2>/dev/null`
       if [ "$CR" == "OK" ]
       then
-         OUTPUT=`sudo x2gopgwrapper insertport $HOSTNAME $SESSION_NAME $SSH_PORT`
+         OUTPUT=`x2goinsertport $HOSTNAME $SESSION_NAME $SSH_PORT`
       fi
     fi
   done
@@ -243,7 +244,8 @@ fi
 X2GO_AGENT_PID=$!
 
 X2GO_SND_PORT=1024
-sudo x2gopgwrapper createsession $X2GO_COOKIE $X2GO_AGENT_PID $X2GO_CLIENT $GR_PORT $SOUND_PORT $FS_PORT $SESSION_NAME > /dev/null
+
+x2gocreatesession $X2GO_COOKIE $X2GO_AGENT_PID $X2GO_CLIENT $GR_PORT $SOUND_PORT $FS_PORT $SESSION_NAME > /dev/null
 
 echo $X2GO_PORT
 echo $X2GO_COOKIE
diff --git a/x2gosuspend-session b/x2gosuspend-session
index db819f3..c79d490 100755
--- a/x2gosuspend-session
+++ b/x2gosuspend-session
@@ -3,17 +3,14 @@
 SESSION_NAME=$1
 
 
-X2GO_AGENT_PID=`sudo x2gopgwrapper getagent $SESSION_NAME` 
-echo "agent: $X2GO_AGENT_PID"
+X2GO_AGENT_PID=`x2gogetagent $SESSION_NAME` 
 X2GO_AGENT_PID=`echo "$X2GO_AGENT_PID"| awk {'print $1'}`
 
-echo "agent: $X2GO_AGENT_PID"
-
 
 #workaround for knotify
 killall -HUP knotify
 
-sudo x2gopgwrapper changestatus 'S' $SESSION_NAME  > /dev/null
+x2gochangestatus 'S' $SESSION_NAME  > /dev/null
 
 
 kill -HUP $X2GO_AGENT_PID
diff --git a/x2goterminate-session b/x2goterminate-session
index 59560d4..5d31543 100755
--- a/x2goterminate-session
+++ b/x2goterminate-session
@@ -1,10 +1,10 @@
 #!/bin/bash
 
 SESSION_NAME=$1
-X2GO_AGENT_PID=`sudo x2gopgwrapper getagent $SESSION_NAME` 
+X2GO_AGENT_PID=`x2gogetagent $SESSION_NAME` 
 X2GO_AGENT_PID=`echo "$X2GO_AGENT_PID"| awk {'print $1'}`
 
-sudo x2gopgwrapper changestatus 'F' $SESSION_NAME  > /dev/null
+x2gochangestatus 'F' $SESSION_NAME  > /dev/null
 
 kill -TERM $X2GO_AGENT_PID
 export HOSTNAME
diff --git a/x2goumount b/x2goumount
index 928c3c1..c24fbeb 100755
--- a/x2goumount
+++ b/x2goumount
@@ -1,6 +1,9 @@
 #!/usr/bin/perl
 use strict;
 
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
 my $fname=shift;
 open(F,"<$fname") or die "can't open $fname";
 my $dir=<F>;
@@ -22,7 +25,7 @@ if($ENV{'GNOME_DESKTOP_SESSION_ID'} ne "")
 if(system( "fusermount -u $dir" ) == 0)
 {
      unlink($fname);
-     system("sudo x2gopgwrapper deletemount $session \"$dir\"");
+     db_deletemount( $session, $dir);
      rmdir ($dir);
 }
 else
diff --git a/x2goumount_session b/x2goumount_session
index 5e3da84..3b73702 100755
--- a/x2goumount_session
+++ b/x2goumount_session
@@ -2,6 +2,10 @@
 use Sys::Hostname;
 use strict;
 
+use lib "/usr/lib/x2go";
+use x2godbwrapper; 
+
+
 my $session=shift;
 my $only_path=shift;
 
@@ -18,14 +22,12 @@ if($only_path)
 }
 
 
-my $outp=`sudo x2gopgwrapper getdisplay $session`;
+my $display=db_getdisplay($session);
 
-$outp=~s/ //g;
 
-my $display=$outp;
+print "DISPLAY=$display\n";
 
-$outp=`sudo x2gopgwrapper getmounts $session`;
-my @outp=split("\n","$outp");
+my @outp=db_getmounts($session);
 my $i;
 
 
@@ -108,7 +110,7 @@ break:
 	   unlink("$remote(sshfs-cdrom)");
 	}
         #print "$session \"@line[1]\"\n"; 
-	system("sudo x2gopgwrapper deletemount $session \"@line[1]\"> /dev/null");
+	db_deletemount ($session, @line[1]);
         rmdir (@line[1]);
     }
 cont:    


hooks/post-receive
-- 
x2goserver.git (X2Go Server)

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "x2goserver.git" (X2Go Server).




More information about the x2go-commits mailing list