Generation of entangled states of two atoms inside a leaky cavity