Tomcat Configuration - Home, Base and catalina.policy

Here is some generic information regarding Tomcat 5.5 configuration and giving read write execute permissions to a servlet. Java's security manager can be quite complex and usually leads to a lot of pain when configuring a servlet to do something that is above and beyond the standard "read", "write", "execute" such as delete. Debugging such Security Manager permission error can be fairly daunting.

Tomcat configuration has two variables catalina.home and catalina.base. The purpose of this is to allow multiple instances of tomcat run from a single installation.

catalina.home is the installation directory and contains the directories bin, common, server.

catalina.base is your instance directory and contains the instance directories such as work, shared, conf, webapps.


Security

In your conf directory there is another directory called policy.d. Inside the policy.d directory there are a few files. All of these files are merged into one and written to the catalina.policy file in the conf directory. DO NOT edit the catalina.policy file as this will have no effect. You have to edit the files inside the policy.d directory.

To give your servlet extra permissions edit the file 50users.policy and add your servlet
 1. grant codeBase "file:${catalina.base}/webapps/Android/WEB-INF/-" {
 2.  permission java.io.FilePermission "${catalina.base}/webapps/Android/-", "read,write,execute,delete";
 3. };
Hide line numbers

You must be careful here. If, for example you are using another package to modify some files such as the DeleteQuietly function in the io-commons.jar package, then you must give this jar file the permissions too, if it is in another directory.


Debugging Security

To debug you must start tomcat with the option -Djava.security.manager -Djava.security.debug=all

If you are using the startup script in /etc/init.d/tomcat5.5 then you should edit the tomcat5.5 file and add the setting as shown below.

 1. if [ "$TOMCAT5_SECURITY" = "yes" ]; then
 2.         JAVA_OPTS="$JAVA_OPTS  -Djava.security.manager -Djava.security.debug=all  -Djava.security.policy=$CATALINA_BASE/conf/catalina.policy"
 3. fi
Hide line numbers

This will give you a huge amount of logging info, but you can easily search the output for something like 'access denied' to find out what is causing the problem

Mount Windows NT partition From Ubuntu Live CD

What ususally happens is a virus attacks your PC and you cannot boot up in wondows. The easiest way to recover this is with a Unix/Linux live CD. In this example we will use an Ubuntu Live CD to boot up and mount your NTFS partitions so you can copy data from them to a USB Drive.

The only problem is that the partitions mount with "Read Permissions" only so you need to access them as root. But the live Ubuntu CD doesn't really have a root password. But using sudo we can set the root password to give us the required permission.
  1. Boot using Ubuntu LiveCD
  2. In the Ubuntu desktop click on the applications menu and System Settings -> Advanced tab -> Disk -> Filesystems
  3. Click on "Administrator Mode" at the bottom.
  4. You should see the windows partitions. Double click the partition you want to mount.
  5. In the 'type' drop down list box select "NTFS - NT File System".
  6. Select Mount point. It this case we're going to browse to /home/ubuntu/Desktop and create a new folder WinD and select this folder.
  7. "Security & Safety - make sure you check the "Writeable" box.
  8. Select OK.
  9. Now Enable the partition by clicking the "Enable" button at the bottom.
  10. Now open a console and and go to the mount point /home/ubuntu/Desktop in this example.
  11. In the console, set the root pasword using "sudo passwd root".
  12. Now su
  13. Voila, now you can access the mount (/home/ubuntu/desktop/WinD in this example) and you can set more liberal permissions so you can access them with a file browser.

Sending Mail Via Google's SMTP Server Using PHP

Aim
We will create a simple php application that uses PHPMailer to connect to Google's smtp server to send out an email. You can use this for any email provider who allows SMTP access. This is useful from an e-commerce point of view, to send out emails that will not go straight into the recipients junk mail inbox. This method is preferred over sendmail which uses and SMTP relay. If you are hosting your application on a shared host, using PhpMailer will get around email limits set for sendmail by your hosting provider.

Assumptions
This article assumes that you have a compatible version of PHP installed and configured and you have downloaded and unzipped the PHPMailer

Versions used in this example
Sofware/ComponentImage
Windows XP SP2N/A
Php 5.2.12N/A
PhpMailer 5.1N/A
Links to these files can be found here.

For the purposes of this example PhpMailer has been unzipped in the diretory where the example script resides

Application
As you can see we're connecting to google's SMTP server via the url ssl://smtp.gmail.com:465. You have to login with a valid gmail username and password.

 1. require("PHPMailer_v5.1/class.phpmailer.php");
 2. 
 3. 
 4. $mailer = new PHPMailer();
 5. $mailer->IsSMTP();
 6. $mailer->Host = 'ssl://smtp.gmail.com:465';
 7. $mailer->SMTPAuth = TRUE;
 8. 
 9. $mailer->Username = 'harrypotter@gmail.com';
10. $mailer->Password = 'alhamora';
11. 
12. $mailer->From = 'harrpotter@gmail.com';
13. $mailer->FromName = 'Harry Potter';
14. $mailer->Body = "Hello there all\nHow is everyone today?";
15. $mailer->Subject = "Hello from Harry";
16. 
17. $mailer->AddAddress(hermionegranger@hotmail.com);
18. $mailer->AddAddress(ronweasely@yahoo.com);
19. 
20. if(!$mailer->Send()) {
21.     error_log("Mailer :  error ".$mailer->ErrorInfo)." : $to";
22.     echo "fail";
23. }
24. else {
25.     echo "sent";
26. }
Hide line numbers

Java Check Daylight Saving Time Utility

Aim
This simple example is to create a small java application that you can use to view timezones and see when the JDK/JRE changes to daylight savings time or changes back from daylight savings time. It's important to know when DST occurs, especially for systems that have time sensitive information.

You can use the java/oracle tzupdater.jar utility to update your jre/jdk for all the latest timezones and daylight saving time configurations.

Application
This application enumerates the timezones available on a system. You can then select a timezone and set a date and then increment that date to see when DST occurs. This utility uses the 'inDaylightTime()' function to test if the time is in daylight savings time or not.

 1. import java.util.*;
 2. import java.text.*;
 3. 
 4. public class tzutility{
 5.   public static void main(String[] args){
 6.     Date date = new Date();
 7.     String TimeZoneIds[] = TimeZone.getAvailableIDs();
 8.     for(int i = 0; i < TimeZoneIds.length; i++){
 9.       TimeZone tz = TimeZone.getTimeZone(TimeZoneIds[i]);
10.       String tzName = tz.getDisplayName(tz.inDaylightTime(date), TimeZone.LONG);
11.       System.out.println(TimeZoneIds[i] + ":   ");
12.     }
13.     try{
14.       DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
15.       TimeZone nswtz = TimeZone.getTimeZone("Australia/Queensland");
16.       Date d = df.parse("2010-09-01");
17.       Calendar cal = Calendar.getInstance();
18.       cal.setTime(d);
19.       for(int i = 0 ; i < 100 ; i++ ){
20.         System.out.println(cal.getTime()+" "+nswtz.inDaylightTime(cal.getTime()));
21.         cal.add(Calendar.DATE, 1);
22.       }
23.     }catch(Exception e){
24.       e.printStackTrace();
25.     }
26.   }
27. }
Hide line numbers