Pages

Wednesday, April 7, 2010

Mapbender Installation Instructions fo Ubuntu 9.10(Karmic Koala)




Mapbender (2.6+) is shipped with an install script, located at
/mapbender_2.6/resources/db/install_2.6.sh 
(currently for unix-like and windows OS). You will be asked to input several bits of information, after that the installation will proceed automatically. Some of the things mentioned on this page, still apply. Especially all the prerequisites need to be fulfilled, the webserver configuration is not done automatically [Apache configuration, php configuration]. Installation of gettext. And also a final customization of the mapbender.conf has to be done.


Contents

Prerequisites: To be able to install and operate your own copy of Mapbender you need:
  • Webserver (sudo apt-get install apache2)
  • an installation of PHP (sudo apt-get install php5 ; sudo apt-get install php5-pgsql)
    • php-mbstring
    • php-gettext
    • php-gd2
    • php5-imagick
  • Database :Postgresql (sudo apt-get install postgresql-client-8.4 ; sudo apt-get install postgresql-server-dev-8.4)
  • Installation of gettext (sudo apt-get install gettext)

Directories

  • conf - directory for configuration files.
  • http - directory for the application.
  • license - licensing information files
  • log - logfiles will be saved here.
  • resource/db - directory for the SQL-Dump to build up the Mapbender database or update the database.
  • resources/locales - directory contains translation files for different languages (Mapbender.po files, they are basically object files).

Run the install-Script

Database required by Mapbender needs administrative privileges. There is an install-script that creates the database and creates tables and content
  • install_2.6.sh.
./mapbender/resources/db/install_2.6.sh



Apache virtual directory

After installation create a virtual directory. Edit  /etc/apache2/sites-available/default:


Alias /mapbender /var/www/apache2-default/mapbender_2.4.3/http/

       
          Options MultiViews
          DirectoryIndex index.php
          Order allow,deny
          Allow from all
       


You must reload (or stop and restart) the Apache program after making changes to the configuration file to make them take effect.


sudo /etc/init.d/apache2 stop
sudo /etc/init.d/apache2 restart


If you want to enable directory browsing, e.g. for debugging purposes add Indexes to the Options line. 



Make sure you have loaded the required modules (for example, Ubuntu and Apache2)
a2enmod deflate
a2enmod headers
a2enmod setenvif (for BrowserMatch)

PHP Configuration

You might have to make some adjustments to the file php.ini. Changes in the php.ini also become operative only after restarting the Apache web server !


sudo /etc/init.d/apache2 restart


1. During the test and installation phase of Mapbender it can be helpful log php-errors. Configure the file php.ini as follows:


error_reporting  =  E_ALL & ~E_NOTICE
log_errors = On
error_log = "/var/log/php.log"
 
2. Check the following details in php.ini allow_url_fopen parameter:
extension_dir = (path to extensions-directory)
allow_url_fopen = On
 
3. Mapbender manages access authorization in SESSIONS. Check the following details:
session.save_handler = files
session.save_path = (Path to SESSIONS-Directory). (Check authorisation!)

4. Further details of session management should be customized according to server load (see 'garbage collection' in php.ini)

5. With regard to other modules the extension gd2 should be included in the installation and configuration process.
extension=gd.so              (Linux)



# sudo apt-get install lbgd2-dev 

6. For some Mapbender modules access to a PostgreSQL database is required. If these modules should be used, you have to check the availability of the required extension in the php.ini file:
 
extension=pgsql.so           (Linux)


7. short_open_tag
short_open_tag = OFF


8. Mapbender requires simplexml and dom for XML parsing. If not installed, this will lead to some hard-to-find bugs, because PHP fails to throw exceptions! These modules should be already installed if you are using a Linux distribution, but if you compile PHP yourself you need to include these as well

Mapbender Database

Mapbender offers an install-script (install.sh) that sets up the database for you. The following descriptions shows what is happening.
Mapbender offers an update script (update.sh) that keeps your database upoto-date

PostgreSql Database

The user under which you load the database must have Postgres superuser rights during inserting the psql_*.sql files below so it can temporarily disable constraint triggers during insert.

You have to decide to either use UTF-8 or ISO-8859-1 character encoding.
  • database with UTF-8 encoding
createdb -E UTF8 mapbender
  • database with ISO-8859-1 encoding
createdb -E Latin1 mapbender
First load the empty Mapbender table definiton (schema)
psql -f pgsql_schema.sql 
Then load the data in mapbender/resources/db/postgresql/ - be sure to use the dump with the correct enconding!
  • UTF-8
psql -f pgsql_data.sql  (in subfolder utf8/)
  • ISO-8859-1
psql -f pgsql_data.sql  (in subfolder iso/)
At the end set the constraints and sequences. Here you have to check which PostgreSQL version you are using:
  • pgsql_serial_set_sequences.sql (for 8.x)
psql -f pgsql_serial_set_sequences.sql  
You can install PostGIS as follows (as User postgres (su postgres)):
  • createlang plpgsql (already installed in Mapbender >= 2.5)
  • psql -f lwpostgis.sql -d (change into the PostGIS directory!)
  • psql -f spatial_ref_sys.sql -d (change into the PostGIS directory!)


How to load the sqls in PostgreSQL: You find the SQL: /mapbender/resources/db/postgresql/
Notice: There is a difference between the ISO-8859-1 or UTF-8 data-files. Make sure you choose the right folder (iso or utf8)
psql -f pgsql_schema.sql mapbender
psql -f pgsql_data.sql mapbender
psql -f pgsql_serial_set_sequences.sql mapbender
Check the PostgreSQL log file for any errors during insert.
If you want to update from Mapbender Version 2.2.2 to Mapbender Version 2.2.3 load the sqls in the folder update
  • one column var_name in table gui_element_vars has changed
  • new table mb_user_wmc
  • update of the mapbender guis (admin1, admin2_de, admin2_en, gui and gui1. Notice, that gui_de und gui1_de are not longer part of the standard guis. We want to solve the multilanguage problem with element vars in the following versions)



Mapbender Configuration File mapbender.conf

You find the Mapbender Configuration File in the directory "conf". Here you find the file mapbender.conf-dist. Rename the file to mapbender.conf.
Choose the database you want to use (from Mapbender 2.7 on only PostgreSQL is supported):
define("SYS_DBTYPE", "pgsql"); 
If you use PostgreSQL type your database information here (here is an example):
else {
   define("DBSERVER", "localhost");
   define("PORT", "5432");
   define("DB", "mapbender");
   define("OWNER", "mb");
   define("PW", "mb");
}


perpared statements (PHP > 5.1.x, PostgreSQL > 7.4) define("PREPAREDSTATEMENTS",true);
encoding - default characterset UTF-8 / ISO-8859-1
define("CHARSET","UTF-8");




Define a directory for temporary files, this directory must have write access
define("TMPDIR", "../tmp");
URL for login (test this link to know if your mapbender software is running )
define("LOGIN", "http://localhost/mapbender/frames/login.php");
 define("MAXLOGIN", 3);
 $login = LOGIN;


URL to owsproxy (no terminating slash)
define("OWSPROXY", "http://localhost/owsproxy");


type of server-connection curl, socket, http
#define("CONNECTION", "curl");
 define("CONNECTION", "http");
 #define("CONNECTION", "socket");
define("CONNECTION_PROXY", "");
 define("CONNECTION_PORT", "");
 define("CONNECTION_USER", "");
 define("CONNECTION_PASSWORD", "");


security: path to modules
define("CHECK", false);


mail settings for internal mapbender mails Mapbender can communicate with its users by email. For example, when a user lost his/her password, Mapbender can send an email to this person with a new password.


define("USE_PHP_MAILING", "false");
define("MAILHOST", "");
define("MAILUSERNAME", "");
define("MAILPASSWORD", "");
define("MAILADMIN", "");
define("MAILADMINNAME", "");
$mailHost = MAILHOST;
$mailUsername =  MAILUSERNAME;
$mailPassword =  MAILPASSWORD;


Mapbender error logging
  • Set Mapbender error logging (see possible options below)
define("LOG_LEVEL", "error"); //"off","error","warning","notice" or "all"
define("LOG_JS", "on"); // "on", "alert", "console" or "off"
Internationalisation
define("USE_I18N", true);
  • Define default language
define("LANGUAGE", "de");   // "en", "de", "bg", "gr", "nl", "it", es" 
  • you can change the translations in the Mapbender.po (mapbender/resources/locale/de_DE/Mapbender.po files if you prefer a different translation.
    • You can edit the Mapbender.po files with an editor
    • or with poedit
    • after changing Mapbender.po you have to compile the Mapbender.mo files again, see gettext (make sure that gettext is installed).
Example - How to compile the Mapbender.mo files
msgfmt resources/locale/de_DE/LC_MESSAGES/Mapbender.po -o resources/locale/de_DE/LC_MESSAGES/Mapbender.mo


Change the Permission of log Folder

  • Linux : change the permission of log folder to 777 so that php script can write logs in it.
chmod -R 777 log


Check the Setup

You can check your setup with the script mapbender_setup.php. The script mapbender_setup.php moved to the directory:
/mapbender/tools/mapbender_setup.php
To access the script you have to move it temporary to /mapbender/http/tools/mapbender_setup.php. 

Please notice that for security reasons the directory tools should not be available for external users, as e.g. the mapbender_setup.php displays internal information about your system (PHP version, databasename, database user...). You can protect the directory by htaccess or just remove the tools-directory again after testing.
Run the mapbender_setup.php:
http://localhost/mapbender/tools/mapbender_setup.php
The mapbender_setup.php checks wether all configurations of the mapbender.conf are right and checks some system settings.



After the installation - first Login
After the installtion you can login with the user account root and the inital password root. Make sure to change this password asap in order to secure your Mapbender installation. Go to admin2_en Create and edit user to change the password.
You should not delete the user root. To administrate your applications you should create a new user f.e. admin which you use instead if user root.



If you feel any installation instruction that is causing trouble to you, then feel free to comment on this post. I will surely try to answer them when I get some time :)

Saturday, March 13, 2010

iptables

#icmp-type 0 echo-reply
#icmp-type 8 echo-request

#incoming ping packets
SERVER_IP="192.168.154.7"
iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -d $SERVER_IP -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type 0 -s $SERVER_IP -d 0/0 -m state --state ESTABLISHED,RELATED -j ACCEPT


#outgoing ping packets
SERVER_IP="192.168.154.4"
iptables -A OUTPUT -p icmp --icmp-type 8 -s $SERVER_IP -d 0/0 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 0 -s 0/0 -d $SERVER_IP -m state --state ESTABLISHED,RELATED -j ACCEPT


#disable outgoing ICMP request
HOST_TO_BLOCK="192.168.154.4"
iptables -A OUTPUT -p icmp --icmp-type 8 -s 0/0 -d $HOST_TO_BLOCK -j DROP

#disable incoming ICMP request
HOST_TO_BLOCK="192.168.154.7"
iptables -A OUTPUT -p icmp --icmp-type 8 -s$HOST_TO_BLOCK  -d 0/0 -j DROP


SQL HARDENING

#start installation by creating a unique, regular group and user account on the operating system, which will be dedicated to the MySQL database:

groupadd mysql
useradd mysql -c "MySQL Server" -d /dev/null -g mysql -s /sbin/nologin


#compiling and installing MySQL in the /usr/local/mysql directory:

./configure --prefix=/usr/local/mysql --with-mysqld-user=mysql --with-unix-socket-path=/tmp/mysql.sock --with-mysqld-ldflags=-all-static
make
su
make install
strip /usr/local/mysql/libexec/mysqld
scripts/mysql_install_db
chown -R root /usr/local/mysql
chown -R mysql /usr/local/mysql/var
chgrp -R mysql /usr/local/mysql

#--with-mysqld-ldflags=-all-static parameter causes the MySQL server to be linked statically which wil simplify the process of chrooting the server

#for installing the software in the /usr/local/mysql directory, run the MySQL daemon with the privileges of the mysql account, and create the mysql.sock socket in the /tmp directory.


#size of the database =medium

cp support-files/my-medium.cnf /etc/my.cnf
chown root:sys /etc/my.cnf
chmod 644 /etc/my.cnf


#start the MySQL server 

/usr/local/mysql/bin/mysqld_safe &


#establishing a connection with the database

/usr/local/mysql/bin/mysql -u root mysql

#Once the connection is successfully established, we can shutdown the database

/usr/local/mysql/bin/mysqladmin -u root shutdown


#securing MySQL is to prepare the chrooted environment, in which the MySQL server will run 


#preparing the chrooted environment

mkdir -p /chroot/mysql/dev
mkdir -p /chroot/mysql/etc
mkdir -p /chroot/mysql/tmp
mkdir -p /chroot/mysql/var/tmp
mkdir -p /chroot/mysql/usr/local/mysql/libexec
mkdir -p /chroot/mysql/usr/local/mysql/share/mysql/english


#access rights to the above directories 

chown -R root:sys /chroot/mysql
chmod -R 755 /chroot/mysql
chmod 1777 /chroot/mysql/tmp


#Next, the following files have to be copied into the new directory structure:

cp /usr/local/mysql/libexec/mysqld /chroot/mysql/usr/local/mysql/libexec/
cp /usr/local/mysql/share/mysql/english/errmsg.sys /chroot/mysql/usr/local/mysql/share/mysql/english/
cp /etc/hosts /chroot/mysql/etc/
cp /etc/host.conf /chroot/mysql/etc/
cp /etc/resolv.conf /chroot/mysql/etc/
cp /etc/group /chroot/mysql/etc/
cp /etc/master.passwd /chroot/mysql/etc/passwords
cp /etc/my.cnf /chroot/mysql/etc/


#From the files: /chroot/mysql/etc/passwords and /chroot/mysql/etc/group we must remove all the lines except the mysql account and group
#Next, we have to build the password database as follows 

cd /chroot/mysql/etc
pwd_mkdb -d /chroot/mysql/etc passwords
rm -rf /chroot/mysql/etc/master.passwd


#creating a special device file /dev/null:

ls -al /dev/null

mknod /chroot/mysql/dev/null c 2 2
chown root:sys /chroot/mysql/dev/null
chmod 666 /chroot/mysql/dev/null


We must also copy the mysql database, which contains grant tables created during MySQL installation:

cp -R /usr/local/mysql/var/ /chroot/mysql/usr/local/mysql/var
chown -R mysql:mysql /chroot/mysql/usr/local/mysql/var



#test if it runs correctly by executing the following command

chrootuid /chroot/mysql mysql /usr/local/mysql/libexec/mysqld &


#/chroot/mysql/etc/my.cnf for MySQL server and /etc/my.cnf for MySQL tools (mysqladmin, mysql, mysqldump)

#To disable listening on the 3306/tcp port, the following parameter should be added to the [mysqld] section of /chroot/mysql/etc/my.cnf:

skip-networking


#disable the use of
LOAD DATA LOCAL INFILE command 

#For SQL Injection vulnerabilities in PHP applications, the following parameter should be added in the [mysqld] section in /chroot/mysql/etc/my.cnf:

set-variable=local-infile=0

#in the [client] section of /etc/my.cnf

socket = /chroot/mysql/tmp/mysql.sock


#changing the database administrator's password

chrootuid /chroot/mysql mysql /usr/local/mysql/libexec/mysqld &

#and change the administrator's password as follows:

/usr/local/mysql/bin/mysql -u root
mysql> SET PASSWORD FOR root@localhost=PASSWORD('new_password');


#Remove default users/db ,sample database (test) and all accounts except the local root account

mysql> drop database test;
mysql> use mysql;
mysql> delete from db;
mysql> delete from user where not (host="localhost" and user="root");
mysql> flush privileges;


#change the default name of administrator's account (root), to a different one.so it will be difficult to perform brute-force and dictionary attacks on the administrator's password.

mysql> update user set user="mydbadmin" where user="root";
mysql> flush privileges;


#remove the content of the MySQL history file (~/.mysql_history)

cat /dev/null > ~/.mysql_history


#create all databases and accounts which will be used by specific PHP applications.
#should be emphasized that these accounts should have access rights only to the databases which are used by the PHP applications
#should not have any access rights to the
mysql database administrative privileges (FILE, GRANT, ALTER, SHOW DATABASE, RELOAD, SHUTDOWN, PROCESS, SUPER)


Tuesday, February 16, 2010

TCP and UDP Client Server Programs In JAVA


A Simple Server and Client Examples

A simple client and the server programs are explained for all the types of techniques. A code samples for the connection-oriented, next for the connectionless and then for broadcasting are as follows.

Connection-oriented Client and Server : ( TCP )

The client sends the message " Hi from client " and the server response will be " Hello from Server ".

Server Program

import java.net.*;
import java.lang.*;
import java.io.*;

public class Server{

//port number should be more than 1024

public static final int PORT = 1025;
         
public static void main( String args[])
{
 ServerSocket sersock = null;
 Socket sock = null;
 System.out.println(" Wait !! ");

 try
 {
  //  Initialising the ServerSocket
  sersock =  new ServerSocket(PORT);
                
  // Gives the Server Details Machine name, 
  Port number

  System.out.println("Server Started  :"+sersock);

  try
  {
                        
   // makes a socket connection to particular 
   client after 
   // which two way communication take place

   sock = sersock.accept();

   System.out.println("Client Connected  :"+ sock);

   // Receive message from client i.e Request 
   from client

   DataInputStream ins = new                 
   DataInputStream(sock.getInputStream());
   // Send message to the client i.e Response

   PrintStream ios = new   
   sPrintStream(sock.getOutputStream());
   ios.println("Hello from server");
   ios.close();

   // Close the Socket connection 
        
    sock.close();

    }
 catch(SocketException se)
 {
    System.out.println("Server Socket
  problem  "+se.getMessage());
    }
 catch(Exception e)
 {
    System.out.println("Couldn't start " 
                  + e.getMessage()) ;     
    }               
                        
 // Usage of some methods in Socket class

  System.out.println(" Connection from :  " + 
  sock.getInetAddress());
                
 } // main 
        
}  // Server class
 

Client Program

import java.lang.*;
import java.io.*;
import java.net.*;
import java.net.InetAddress;


class client
{
 Public static void main(String args[])
 {
 Socket sock=null;
 DataInputStream dis=null;
 PrintStream ps=null;
 System.out.println(" Trying to connect");
                                   
 try 
 {
 // to get the ip address of the 
 server by the name
              
 InetAddress ip =InetAddress.getByName
 ("Hari.calsoftlabs.com");

 // Connecting to the port 1025 declared 
 in the Serverclass
 // Creates a socket with the server
  bind to it.

  sock= new Socket(ip,Server.PORT);
  ps= new PrintStream(sock.getOutputStream());
  ps.println(" Hi from client");
  DataInputStream is = new 
  DataInputStream(sock.getInputStream());
  System.out.println(is.readLine());

 }
 0catch(SocketException e)
 {
  System.out.println("SocketException " + e);
 }
 catch(IOException e)
 {
  System.out.println("IOException " + e);
 }

  // Finally closing the socket from 
  the client side

 finally
 {
 try
  {
   sock.close();
  }
  catch(IOException ie)
  {
   System.out.println(" Close Error   :" + 
   ie.getMessage());
  }               
 }  // finally 
                        
} // main 
}   // Class Client 
 

Running the Server and Client

After you've successfully compiled the server and the client programs, you run them. You have to run the server program first. Just use the Java interpreter and specify the Server class name. Once the server has started, you can run the client program. After the client sends a request and receives a response from the server, you should see output similar to this :
On client side:
   Hello from Server
On Server side:
   Hi from client

Connectionless Client and Server : (UDP)

A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed.
The java.net package contains two classes to help you write Java programs that use datagrams to send and receive packets over the network: DatagramSocket, DatagramPacket, and MulticastSocket An application can send and receive DatagramPackets through a DatagramSocket. In addition, DatagramPackets can be broadcast to multiple recipients all listening to a MulticastSocket.
The following source code demonstrates a slightly more complex server that uses datagrams instead of sockets. After a connection is made, It echoes back whatever is sent by the client instead of simply hanging up the connection. It is Called as echo Server.

Server Program

import java.net.*;
import java.io.*;


public class EchoServer
{

//Initialize Port number and Packet Size
  
 static final int serverPort = 1026;
 static final int packetSize = 1024;

 public static void main(String args[]) 
 throws SocketException{

 DatagramPacket packet;
 DatagramSocket socket;
 byte[] data;    // For data to be 
 Sent in packets
 int clientPort;
 InetAddress address;
 String str;

 socket = new DatagramSocket(serverPort);

 for(;;){
 data = new byte[packetSize];

 // Create packets to receive the message

 packet = new DatagramPacket(data,packetSize); 
 System.out.println("Waiting to receive 
 the packets");

 try{

 // wait infinetely for arrive of the packet

 socket.receive(packet);

 }catch(IOException ie)
 {
 System.out.println(" Could not Receive
 :"+ie.getMessage());
 System.exit(0);
 }
  
 // get data about client in order to 
 echo data back
  
 address = packet.getAddress();
 clientPort = packet.getPort();
  
 // print string that was received 
 on server's console
 str = new String(data,0,0,packet.getLength());
 System.out.println("Message  :"+ str.trim());
 System.out.println("From   :"+address);
  
 // echo data back to the client 
 // Create packets to send to the client
  
 packet = new DatagramPacket(data,packetSize,
 address,clientPort);
  
 try
 {
 // sends packet   

 socket.send(packet);

 }
 catch(IOException ex)
 {
  System.out.println("Could not Send 
  "+ex.getMessage());
  System.exit(0);
  }
  
 } // for loop

 } // main


} // class EchoServer

Client Program

import java.net.*;
import java.io.*;


public class EchoClient{
   
 static final int serverPort = 1026;
 static final int packetSize = 1024;
   
 public static void main(String args[]) throws
 UnknownHostException, SocketException{
 DatagramSocket socket; //How we send packets
 DatagramPacket packet; //what we send it in
 InetAddress address; //Where to send
 String messageSend; //Message to be send
 String messageReturn; //What we get back 
 from the Server
 byte[] data;
       
 //Checks for the arguments that sent to 
 the java interpreter
 // Make sure command line parameters correctr
       
 if(args.length != 2)
 {
 System.out.println("Usage Error :
 Java EchoClient < Server name> < Message>");
 System.exit(0);
 }   
       
 // Gets the IP address of the Server
 address = InetAddress.getByName(args[0]);
 socket = new DatagramSocket();
               
 data = new byte[packetSize];
 messageSend = new String(args[1]);
 messageSend.getBytes
 (0,messageSend.length(),data,0);
               
 // remember datagrams hold bytes
 packet = new
 DatagramPacket(data,data.length,address,serverPort);
 System.out.println(" Trying to Send the packet ");
               
 try
 {
  // sends the packet
               
  socket.send(packet);
           
  }
  catch(IOException ie)
  {
  System.out.println("Could not Send :"+ie.getMessage());
  System.exit(0);
  }            
       
  packet is reinitialized to use it for recieving
       
  packet = new DatagramPacket(data,data.length);
               
  try
  {
  // Receives the packet from the server
                   
  socket.receive(packet);   
               
  }
  catch(IOException iee)
  {
  System.out.println("Could not receive :
  "+iee.getMessage() );
  System.exit(0);
  }       
       
  // display message received
       
  messageReturn = new String (packet.getData(),0);
  System.out.println("Message Returned : "+
  messageReturn.trim());
  }    // main
   
   
 } // Class EchoClient

Running the Server and Client

The client side and the server side networking code looks actually very similar.This is true with many applications that use datagrams because the java.net.DatagramSocket class is used to both send and receive DatagramPackets.
Suppose server running on the machine named Hari.calsoftlabs.com, whereas the client running on the xxxx.info.com. As you can see at the end of the example the server is running waiting for the another connection, while the execution of the client has halted.
Server Side :
To start the server :
Java EchoServer
Output:
Message:Hello
From   :xxxx.info.com
Client Side :
Java EchoClient abhishek-bit.blogspot.com Hello
Output:
Message Returned : Hello