Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PAMUserService.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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;
  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 org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import com.gitblit.Constants.AccountType;
  24. import com.gitblit.manager.IRuntimeManager;
  25. import com.gitblit.models.UserModel;
  26. import com.gitblit.utils.ArrayUtils;
  27. import com.gitblit.utils.StringUtils;
  28. /**
  29. * Implementation of a PAM user service for Linux/Unix/MacOSX.
  30. *
  31. * @author James Moger
  32. */
  33. public class PAMUserService extends GitblitUserService {
  34. private final Logger logger = LoggerFactory.getLogger(PAMUserService.class);
  35. private IStoredSettings settings;
  36. public PAMUserService() {
  37. super();
  38. }
  39. @Override
  40. public void setup(IStoredSettings settings) {
  41. this.settings = settings;
  42. String file = settings.getString(Keys.realm.pam.backingUserService, "${baseFolder}/users.conf");
  43. IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);
  44. File realmFile = runtimeManager.getFileOrFolder(file);
  45. serviceImpl = createUserService(realmFile);
  46. logger.info("PAM User Service backed by " + serviceImpl.toString());
  47. // Try to identify the passwd database
  48. String [] files = { "/etc/shadow", "/etc/master.passwd" };
  49. File passwdFile = null;
  50. for (String name : files) {
  51. File f = new File(name);
  52. if (f.exists()) {
  53. passwdFile = f;
  54. break;
  55. }
  56. }
  57. if (passwdFile == null) {
  58. logger.error("PAM User Service could not find a passwd database!");
  59. } else if (!passwdFile.canRead()) {
  60. logger.error("PAM User Service can not read passwd database {}! PAM authentications may fail!", passwdFile);
  61. }
  62. }
  63. @Override
  64. public boolean supportsCredentialChanges() {
  65. return false;
  66. }
  67. @Override
  68. public boolean supportsDisplayNameChanges() {
  69. return true;
  70. }
  71. @Override
  72. public boolean supportsEmailAddressChanges() {
  73. return true;
  74. }
  75. @Override
  76. public boolean supportsTeamMembershipChanges() {
  77. return true;
  78. }
  79. @Override
  80. protected AccountType getAccountType() {
  81. return AccountType.PAM;
  82. }
  83. @Override
  84. public UserModel authenticate(String username, char[] password) {
  85. if (isLocalAccount(username)) {
  86. // local account, bypass PAM authentication
  87. return super.authenticate(username, password);
  88. }
  89. if (CLibrary.libc.getpwnam(username) == null) {
  90. logger.warn("Can not get PAM passwd for " + username);
  91. return null;
  92. }
  93. PAM pam = null;
  94. try {
  95. String serviceName = settings.getString(Keys.realm.pam.serviceName, "system-auth");
  96. pam = new PAM(serviceName);
  97. pam.authenticate(username, new String(password));
  98. } catch (PAMException e) {
  99. logger.error(e.getMessage());
  100. return null;
  101. } finally {
  102. pam.dispose();
  103. }
  104. UserModel user = getUserModel(username);
  105. if (user == null) // create user object for new authenticated user
  106. user = new UserModel(username.toLowerCase());
  107. // create a user cookie
  108. if (StringUtils.isEmpty(user.cookie) && !ArrayUtils.isEmpty(password)) {
  109. user.cookie = StringUtils.getSHA1(user.username + new String(password));
  110. }
  111. // update user attributes from UnixUser
  112. user.accountType = getAccountType();
  113. user.password = Constants.EXTERNAL_ACCOUNT;
  114. // TODO consider mapping PAM groups to teams
  115. // push the changes to the backing user service
  116. super.updateUserModel(user);
  117. return user;
  118. }
  119. }