James Moger
2013-11-24 04a98505a4ab8f48aee22800fcac193d9367d0ae
src/main/java/com/gitblit/auth/PAMAuthProvider.java
File was renamed from src/main/java/com/gitblit/PAMUserService.java
@@ -13,47 +13,35 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit;
package com.gitblit.auth;
import java.io.File;
import org.jvnet.libpam.PAM;
import org.jvnet.libpam.PAMException;
import org.jvnet.libpam.impl.CLibrary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.gitblit.Constants;
import com.gitblit.Constants.AccountType;
import com.gitblit.manager.IRuntimeManager;
import com.gitblit.Keys;
import com.gitblit.auth.AuthenticationProvider.UsernamePasswordAuthenticationProvider;
import com.gitblit.models.UserModel;
import com.gitblit.utils.ArrayUtils;
import com.gitblit.utils.StringUtils;
/**
 * Implementation of a PAM user service for Linux/Unix/MacOSX.
 * Implementation of PAM authentication for Linux/Unix/MacOSX.
 *
 * @author James Moger
 */
public class PAMUserService extends GitblitUserService {
public class PAMAuthProvider extends UsernamePasswordAuthenticationProvider {
    private final Logger logger = LoggerFactory.getLogger(PAMUserService.class);
    private IStoredSettings settings;
    public PAMUserService() {
        super();
    public PAMAuthProvider() {
        super("pam");
    }
    @Override
    public void setup(IRuntimeManager runtimeManager) {
        this.settings = runtimeManager.getSettings();
        String file = settings.getString(Keys.realm.pam.backingUserService, "${baseFolder}/users.conf");
        File realmFile = runtimeManager.getFileOrFolder(file);
        serviceImpl = createUserService(realmFile);
        logger.info("PAM User Service backed by " + serviceImpl.toString());
    public void setup() {
        // Try to identify the passwd database
        String [] files = { "/etc/shadow", "/etc/master.passwd" };
      File passwdFile = null;
@@ -65,9 +53,9 @@
         }
      }
      if (passwdFile == null) {
         logger.error("PAM User Service could not find a passwd database!");
         logger.error("PAM Authentication could not find a passwd database!");
      } else if (!passwdFile.canRead()) {
         logger.error("PAM User Service can not read passwd database {}! PAM authentications may fail!", passwdFile);
         logger.error("PAM Authentication can not read passwd database {}! PAM authentications may fail!", passwdFile);
      }
    }
@@ -98,11 +86,6 @@
    @Override
    public UserModel authenticate(String username, char[] password) {
      if (isLocalAccount(username)) {
         // local account, bypass PAM authentication
         return super.authenticate(username, password);
      }
      if (CLibrary.libc.getpwnam(username) == null) {
         logger.warn("Can not get PAM passwd for " + username);
         return null;
@@ -120,7 +103,7 @@
         pam.dispose();
      }
        UserModel user = getUserModel(username);
        UserModel user = userManager.getUserModel(username);
        if (user == null)   // create user object for new authenticated user
           user = new UserModel(username.toLowerCase());
@@ -136,7 +119,7 @@
        // TODO consider mapping PAM groups to teams
        // push the changes to the backing user service
        super.updateUserModel(user);
        updateUser(user);
        return user;
    }