Write passwd atomically

Also error checking on input parameters.