Add shared db locking.

Also:

  * improve bootstrap sequence:
    - stages are more organized.
    - there are less useless requests to server.
  * improve shared db access:
    - instantiate the shared db only once.
    - also results in less requests to server.
  * move some common functions and global variables to common.
  * improve security of recovery document:
    - access to the recovery document now depends on the user password.
  * improve documentation.