You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PAMAuthProvider.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2013 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.auth;
  17. import java.io.File;
  18. import org.jvnet.libpam.PAM;
  19. import org.jvnet.libpam.PAMException;
  20. import org.jvnet.libpam.impl.CLibrary;
  21. import com.gitblit.Constants;
  22. import com.gitblit.Constants.AccountType;
  23. import com.gitblit.Keys;
  24. import com.gitblit.auth.AuthenticationProvider.UsernamePasswordAuthenticationProvider;
  25. import com.gitblit.models.UserModel;
  26. import com.gitblit.utils.ArrayUtils;
  27. import com.gitblit.utils.StringUtils;
  28. /**
  29. * Implementation of PAM authentication for Linux/Unix/MacOSX.
  30. *
  31. * @author James Moger
  32. */
  33. public class PAMAuthProvider extends UsernamePasswordAuthenticationProvider {
  34. public PAMAuthProvider() {
  35. super("pam");
  36. }
  37. @Override
  38. public void setup() {
  39. // Try to identify the passwd database
  40. String [] files = { "/etc/shadow", "/etc/master.passwd" };
  41. File passwdFile = null;
  42. for (String name : files) {
  43. File f = new File(name);
  44. if (f.exists()) {
  45. passwdFile = f;
  46. break;
  47. }
  48. }
  49. if (passwdFile == null) {
  50. logger.error("PAM Authentication could not find a passwd database!");
  51. } else if (!passwdFile.canRead()) {
  52. logger.error("PAM Authentication can not read passwd database {}! PAM authentications may fail!", passwdFile);
  53. }
  54. }
  55. @Override
  56. public boolean supportsCredentialChanges() {
  57. return false;
  58. }
  59. @Override
  60. public boolean supportsDisplayNameChanges() {
  61. return true;
  62. }
  63. @Override
  64. public boolean supportsEmailAddressChanges() {
  65. return true;
  66. }
  67. @Override
  68. public boolean supportsTeamMembershipChanges() {
  69. return true;
  70. }
  71. @Override
  72. public AccountType getAccountType() {
  73. return AccountType.PAM;
  74. }
  75. @Override
  76. public UserModel authenticate(String username, char[] password) {
  77. if (CLibrary.libc.getpwnam(username) == null) {
  78. logger.warn("Can not get PAM passwd for " + username);
  79. return null;
  80. }
  81. PAM pam = null;
  82. try {
  83. String serviceName = settings.getString(Keys.realm.pam.serviceName, "system-auth");
  84. pam = new PAM(serviceName);
  85. pam.authenticate(username, new String(password));
  86. } catch (PAMException e) {
  87. logger.error(e.getMessage());
  88. return null;
  89. } finally {
  90. pam.dispose();
  91. }
  92. UserModel user = userManager.getUserModel(username);
  93. if (user == null) // create user object for new authenticated user
  94. user = new UserModel(username.toLowerCase());
  95. // create a user cookie
  96. if (StringUtils.isEmpty(user.cookie) && !ArrayUtils.isEmpty(password)) {
  97. user.cookie = StringUtils.getSHA1(user.username + new String(password));
  98. }
  99. // update user attributes from UnixUser
  100. user.accountType = getAccountType();
  101. user.password = Constants.EXTERNAL_ACCOUNT;
  102. // TODO consider mapping PAM groups to teams
  103. // push the changes to the backing user service
  104. updateUser(user);
  105. return user;
  106. }
  107. }