Merge PAM support, submitted by Jason A. Donenfeld
slight cosmethic changes and possible memleak fixed by me