Blockchain, is a continuously growing list of records, called blocks, which are linked and secured using cryptography. Each block typically contains a cryptographic hash of the previous block, a timestamp and transaction data. By design, a blockchain is inherently resistant to modification of the data. It is "an open, distributed ledger (like a book having a collection of financial accounts) that can record transactions between two parties efficiently and in a verifiable and permanent way".

For use as a distributed ledger, a blockchain is typically managed by a peer-to-peer network collectively adhering to a protocol for validating new blocks. Once recorded, the data in any given block cannot be altered without the alteration of all subsequent blocks, which requires "secret agreement" of the network majority.

Still confused ???
Enough of the non-sense theory and articles explaining blockchain.
Just see this 2 minute video from BBC


Blockchain explained for non techies

In my previous article on "How to setup unauthenticated mail server configuration using mail forwarding with authentication", I've tried to collate all the steps required to setup Postfix server on RHEL7. Here, I simply automate it.
Why do it manually, when you could do the same with automation for next 5 years.
-- Random Thoughts


# This script will enable the linux postfix setup.
# Ensure that mail, postfix and openssl are installed.
# @author



  local line=$3
  [ ! -z $3 ] && line=`expr $line - 1` || line=""
  echo "$1, RC=$2, ERROR on line: $line"
  exit $2

  echo "$1"

  echo "${1} = ${2}" >> ${3}
  if [ $? -eq 0 ]
    INFO "Property \"$1\" configured with value \"$2\""
    ERROR "Failed to add property \"$1\"" 2 ${LINENO}

  sed -i "s|^$1[[:space:]]*=.*|$1 = $2|g" ${3}
  if [ $? -eq 0 ]
    INFO "Property \"$1\" configured with value \"$2\"" 
    ERROR "Failed to update property \"$1\"" 2 ${LINENO}

  [ -z ${KEY} ] && ERROR "Invalid or empty property name \"${KEY}\"." 1 ${LINENO}
  [ -z ${FILE} -o ! -f $FILE ] && ERROR "No such file, $FILE" 1 
  grep -q "^[[:space:]]*${KEY}[[:space:]]*=" ${FILE} >/dev/null 2>&1
  if [ $rc -eq 0 ]
    updateProperty "${KEY}" "${VALUE}" "${FILE}" 
    addProperty "${KEY}" "${VALUE}" "${FILE}"

   echo "${1}:${2}    ${3}:${4}" > ${5}
   postmap ${5}
   chown root:root "${5}" "${5}.db"
   chmod 0600 "${5}" "${5}.db"

  echo "$1:$2 encrypt" > ${3}
  postmap ${3}

  local domain=`echo "$1" | awk -F'.' '{print $(NF-1)"." $NF}'`
  echo -n | openssl s_client -connect $domain:443 | \
  sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' >/tmp/cacertbundle.pem

  if [ $? -eq 0 ]
    cp /tmp/cacertbundle.pem ${POSTFIX_DIR} 
    INFO "WARNING: Failed to retrieve certificate from host $domain:443"

  echo "${TEST_MAILBODY}" | mail -s "$TEST_MAILSUB" "$TEST_MAILID"
  echo ""
  echo "Test email has been sent to $TEST_MAILID with subject, \"$TEST_MAILSUB\""

  SCRIPTNAME=`basename $0`
  echo ""
  echo "Usage:"
  echo "$SCRIPTNAME    "
  echo ""
  echo "For example: $SCRIPTNAME '' '587' '' 'passpass'"
  echo ""
  exit 1

[ $# -lt 4 ] && USAGE

encryptAndStoreSMTPCredentials "${SMTP_HOST}" "${SMTP_PORT}" \
"${SMTP_USERNAME}" "${SMTP_PASSWORD}" "${POSTFIX_DIR}/sasl_password"

setupTLSPolicy "${SMTP_HOST}" "${SMTP_PORT}" "${POSTFIX_DIR}/tls_policy"

configure "myhostname" "$HOSTNAME" "${MAIN_CF}"
configure "relayhost" "${SMTP_HOST}:${SMTP_PORT}" "${MAIN_CF}"
echo "/^From:.*/ REPLACE From:$SMTP_USERNAME" > ${POSTFIX_DIR}/smtp_header_checks
configure "smtp_header_checks" "pcre:${POSTFIX_DIR}/smtp_header_checks" "${MAIN_CF}"
configure "smtp_sasl_auth_enable" "yes" "${MAIN_CF}"
configure "smtp_sasl_security_options" "noanonymous" "${MAIN_CF}"
configure "smtp_sasl_tls_security_options" "noanonymous" "${MAIN_CF}"
configure "smtp_sasl_password_maps" "hash:${POSTFIX_DIR}/sasl_password" "${MAIN_CF}"
configure "smtp_tls_policy_maps" "hash:${POSTFIX_DIR}/tls_policy" "${MAIN_CF}"
configure "smtpd_use_tls" "yes" "${MAIN_CF}"
getSignerCert "${SMTP_HOST}"
configure "smtp_tls_CAfile" "${POSTFIX_DIR}/cacertbundle.pem" "${MAIN_CF}"
service postfix restart
sleep 2
sendTestMail "${SMTP_USERNAME}" \
"Test email: Postfix configuration for $HOSTNAME on `date`" \
"This is a test mail generated by the configuration script from ''"

./ "" "587" "" "gmail_password_of_someadmin"

Automated linux shell script to setup Postfix on RHEL7 with Gmail

We want to send mails which are authenticated by a mail server, but the application can only send mails to a unauthenticated mail server. We achieve this end result of authenticated mails, by using postfix (a mail relay/forwarding tool ).

What is Postfix? Its a mail server program written by "Wietse Zweitze Venema" that started life at IBM Research as an alternative to the most popular linux "Sendmail" tool. Now he works at Google,  and continues to support Postfix.

Postfix attempts to be fast, easy to setup, and secure. After using postfix you may feel that it's just a sendmail tool, but from inside its completely different. Most Linux OS would have postfix installed by default, if not you can install it using yum or apt-get based on your OS. Before setting up postfix make sure you also have the mail tool installed. Lets try to go through the flow we want to achieve.

Configuration Steps (RedHat):
1. Install the tools
yum update && yum install postfix mailx

2. Setup Gmail Authentication
Create or modify a credentials file which will be used by Postfix to establish authentication with GMail or any other mail server.
Add your credentials to a file (these are the details of the "mail from" user)
echo  "[]:587" >/etc/postfix/sasl_passwd

3. Secure this credentials file
chmod 600 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd

4. Create the security configurations file and secure it
echo "   encrypt" >/etc/postfix/tls_policy
postmap /etc/postfix/tls_policy

5. Fetch the remote server's signer certificate
Note: Its a one line command
echo  -n | openssl  s_client  -connect | \
sed  -ne  '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'  >/etc/postfix/cacert.pem

6. Create the mail re-write policy file
The file /etc/postfix/smtp_header_checks contains rules to be used to rewrite the headers of the emails about to be sent.
This is the most important file in our case as it rewrites the sender so that it always matches our GMail account,
No more 'Relaying disallowed' errors!
echo  "/^From:.*/   REPLACE" > /etc/postfix/smtp_header_checks

7. Configure postfix
Edit postfix configuration file /etc/postfix/ with following values:
relayhost =
smtp_header_checks = pcre:/etc/postfix/smtp_header_checks
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_password
smtp_tls_policy_maps = hash:/etc/postfix/tls_policy
smtpd_use_tls = yes
smtp_tls_CAfile = /etc/postfix/cacert.pem

8. Enable IMAP for gmail user's (eg: account.

9. Restart postfix service and send a sample mail
service restart postfix

10. Enable "Less Secure Apps" In GMail
By default, only the most secure sign-ins, such as logging in to GMail on the web, are allowed for your GMail account. To permit relay requests, log in to your GMail account and turn on Allow less secure apps. For more information, review the Google Support document
"Allowing less secure apps to access your account."

11. All DONE
Let's send a test mail to confirm all our efforts...
echo  "This mail is generated by the configuration steps from ''"  |  mail  -s  "Postfix configuration" ""

For postfix log, see /var/log/maillog

Still not working, try this ...
In the file /etc/postfix/, I uncommented this line:
smtps  inet  n  -  -  -  -  smtpd

Thinking to automate this ?
Here's a script to do all the above steps in a ONE single script !

How to setup unauthenticated mail server configuration using mail forwarding with authentication

Where Good Ideas come from ? ... by Steven Jhonson.
A Fantastic video filled with loads of real life examples and inspirational thoughts....

Good Ideas by Smarter people for a Smarter planet

Archives are the heart and history of any blog. While the recent blog posts appear on the blog's home page, older posts are harder to find. Thanks to the archiving feature, so that the older posts can be found online at any time in the future...


In WEB based applications RESTful APIs are used to manipulate data between the browser and the server. User may enter some data which is formatted to a valid JSON object (Java Script Object Notation) before sending it to the server for processing. However at the server side this data may be stored in an XML format, which means we need to now convert this data from JSON to XML and vice-versa while sending data from server to the browser.

Below is a screenshot showing a sample XML on the left side and the generated JSON on the right side:

Conversion of JSON to XML isn't straight forward since they don't share common aspects about their structure. One may write a parser to read JSON elements one by one and then generate an XML but its a tedious job. Thanks to the power of Java Architecture for XML Binding (JAXB) which can easily parsing of XML (based on its dtd - document type definition) ie: convert an XML string into a java object and vice-versa. However pure jaxb implemetation isin't sufficient to solve our problem. We need a third party library named eclipselink.jar (download from here) which has the jaxb implementation to convert XML to JSON and vice versa. Alternatively we can use 3 sub jars from the eclipselink.jar - org.eclipse.persistence.core-2.5.1.jar, org.eclipse.persistence.antlr-2.5.1.jar, org.eclipse.persistence.moxy-2.5.1.jar

1. Generate java classes using the dtd file of the XML
-dtd /home/cubicrace/workspace/json2xml/samples/DatabaseInventory.dtd
-d /home/cubicrace/workspace/json2xml/src/
-p com.utilities.jaxb

parsing a schema...
compiling a schema...

2. Create a file with the below content:

Ensure that this file exists at the same location as the .class files of the above generated java files.

3. Add the eclipselink.jar to the classpath or project's java build path

4. Write a simple class to perform the conversion.
Below is a working standlone class:

package com.utilities;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;

import com.utilities.jaxb.DatabaseInventory;

public class JsonXmlConverter {

 public static final String base = "/home/cubicrace/workspace";
 static String srcJsonFile = base + "/json2xml/samples/dbInventory.json";
 static String srcXmlFile = base +  "/json2xml/samples/dbInventory.xml";    
 static String convertedJsonFile = base + "/json2xml/samples/convertedFile.json";
 static String convertedXmlFile = base + "/json2xml/samples/convertedFile.xml";
 public boolean createFile(String filePath) {
  File file = new File(filePath);
  try {
   return file.createNewFile();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   return false;
 private void jsonToXml(String jsonFile) throws Exception{
  File jsonObj = new File(jsonFile);
  JAXBContext jc = JAXBContext.newInstance(DatabaseInventory.class);
  Unmarshaller unmarshaller = jc.createUnmarshaller();
  unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
  DatabaseInventory activity = (DatabaseInventory) unmarshaller.unmarshal(jsonObj);
  Marshaller xmlM = jc.createMarshaller();
  xmlM.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  FileOutputStream fos = new FileOutputStream(new File(convertedXmlFile));
  xmlM.marshal(activity, fos);
 private void xmlToJson(String xmlFile) throws Exception{
  File xml = new File(xmlFile);
  JAXBContext jc = JAXBContext.newInstance(DatabaseInventory.class);
  Unmarshaller unmarshaller = jc.createUnmarshaller();
  DatabaseInventory activity = (DatabaseInventory) unmarshaller.unmarshal(xml);

  Marshaller marshaller = jc.createMarshaller();
  marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true);
  marshaller.setProperty( MarshallerProperties.MEDIA_TYPE, "application/json");
  marshaller.setProperty( MarshallerProperties.JSON_INCLUDE_ROOT, true);
  FileOutputStream fos = new FileOutputStream(new File(convertedJsonFile));
  marshaller.marshal(activity, fos);
 public static void main(String[] args) {

  JsonXmlConverter jxc = new JsonXmlConverter();
  try {
   System.out.println("Source XML: " +  srcXmlFile);
   System.out.println("Converted Output JSON: " + convertedJsonFile);
   System.out.println("Source JSON: " + srcJsonFile);
   System.out.println("Converted Output XML: " + convertedXmlFile);
  } catch (Exception e) {
   // TODO Auto-generated catch block


Note: Ensure to update the workspace variable in the above java class before executing in your local environment.

If you see the below error, it means the file (step2 above) is missing at the expected location or has incorrect property value pair.
javax.xml.bind.PropertyException: name: value: application/json
at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(

Download the dtd, sample files, eclipselink.jar and source code as single zip: here

How to convert a given XML snippet to an equivalent JSON format and vice-versa