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.

WindowsUserService.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 java.util.Set;
  19. import java.util.TreeSet;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import waffle.windows.auth.IWindowsAccount;
  23. import waffle.windows.auth.IWindowsAuthProvider;
  24. import waffle.windows.auth.IWindowsComputer;
  25. import waffle.windows.auth.IWindowsIdentity;
  26. import waffle.windows.auth.impl.WindowsAuthProviderImpl;
  27. import com.gitblit.Constants.AccountType;
  28. import com.gitblit.models.UserModel;
  29. import com.gitblit.utils.ArrayUtils;
  30. import com.gitblit.utils.StringUtils;
  31. import com.sun.jna.platform.win32.Win32Exception;
  32. /**
  33. * Implementation of a Windows user service.
  34. *
  35. * @author James Moger
  36. */
  37. public class WindowsUserService extends GitblitUserService {
  38. private final Logger logger = LoggerFactory.getLogger(WindowsUserService.class);
  39. private IStoredSettings settings;
  40. private IWindowsAuthProvider waffle;
  41. public WindowsUserService() {
  42. super();
  43. }
  44. @Override
  45. public void setup(IStoredSettings settings) {
  46. this.settings = settings;
  47. String file = settings.getString(Keys.realm.windows.backingUserService, "${baseFolder}/users.conf");
  48. File realmFile = GitBlit.getFileOrFolder(file);
  49. serviceImpl = createUserService(realmFile);
  50. logger.info("Windows User Service backed by " + serviceImpl.toString());
  51. waffle = new WindowsAuthProviderImpl();
  52. IWindowsComputer computer = waffle.getCurrentComputer();
  53. logger.info(" name = " + computer.getComputerName());
  54. logger.info(" status = " + describeJoinStatus(computer.getJoinStatus()));
  55. logger.info(" memberOf = " + computer.getMemberOf());
  56. //logger.info(" groups = " + Arrays.asList(computer.getGroups()));
  57. }
  58. protected String describeJoinStatus(String value) {
  59. if ("NetSetupUnknownStatus".equals(value)) {
  60. return "unknown";
  61. } else if ("NetSetupUnjoined".equals(value)) {
  62. return "not joined";
  63. } else if ("NetSetupWorkgroupName".equals(value)) {
  64. return "joined to a workgroup";
  65. } else if ("NetSetupDomainName".equals(value)) {
  66. return "joined to a domain";
  67. }
  68. return value;
  69. }
  70. @Override
  71. public boolean supportsCredentialChanges() {
  72. return false;
  73. }
  74. @Override
  75. public boolean supportsDisplayNameChanges() {
  76. return false;
  77. }
  78. @Override
  79. public boolean supportsEmailAddressChanges() {
  80. return true;
  81. }
  82. @Override
  83. public boolean supportsTeamMembershipChanges() {
  84. return true;
  85. }
  86. @Override
  87. protected AccountType getAccountType() {
  88. return AccountType.WINDOWS;
  89. }
  90. @Override
  91. public UserModel authenticate(String username, char[] password) {
  92. if (isLocalAccount(username)) {
  93. // local account, bypass Windows authentication
  94. return super.authenticate(username, password);
  95. }
  96. String defaultDomain = settings.getString(Keys.realm.windows.defaultDomain, null);
  97. if (StringUtils.isEmpty(defaultDomain)) {
  98. // ensure that default domain is null
  99. defaultDomain = null;
  100. }
  101. if (defaultDomain != null) {
  102. // sanitize username
  103. if (username.startsWith(defaultDomain + "\\")) {
  104. // strip default domain from domain\ username
  105. username = username.substring(defaultDomain.length() + 1);
  106. } else if (username.endsWith("@" + defaultDomain)) {
  107. // strip default domain from username@domain
  108. username = username.substring(0, username.lastIndexOf('@'));
  109. }
  110. }
  111. IWindowsIdentity identity = null;
  112. try {
  113. if (username.indexOf('@') > -1 || username.indexOf('\\') > -1) {
  114. // manually specified domain
  115. identity = waffle.logonUser(username, new String(password));
  116. } else {
  117. // no domain specified, use default domain
  118. identity = waffle.logonDomainUser(username, defaultDomain, new String(password));
  119. }
  120. } catch (Win32Exception e) {
  121. logger.error(e.getMessage());
  122. return null;
  123. }
  124. if (identity.isGuest() && !settings.getBoolean(Keys.realm.windows.allowGuests, false)) {
  125. logger.warn("Guest account access is disabled");
  126. identity.dispose();
  127. return null;
  128. }
  129. UserModel user = getUserModel(username);
  130. if (user == null) // create user object for new authenticated user
  131. user = new UserModel(username.toLowerCase());
  132. // create a user cookie
  133. if (StringUtils.isEmpty(user.cookie) && !ArrayUtils.isEmpty(password)) {
  134. user.cookie = StringUtils.getSHA1(user.username + new String(password));
  135. }
  136. // update user attributes from Windows identity
  137. user.accountType = getAccountType();
  138. String fqn = identity.getFqn();
  139. if (fqn.indexOf('\\') > -1) {
  140. user.displayName = fqn.substring(fqn.lastIndexOf('\\') + 1);
  141. } else {
  142. user.displayName = fqn;
  143. }
  144. user.password = Constants.EXTERNAL_ACCOUNT;
  145. Set<String> groupNames = new TreeSet<String>();
  146. for (IWindowsAccount group : identity.getGroups()) {
  147. groupNames.add(group.getFqn());
  148. }
  149. if (groupNames.contains("BUILTIN\\Administrators")) {
  150. // local administrator
  151. user.canAdmin = true;
  152. }
  153. // TODO consider mapping Windows groups to teams
  154. // push the changes to the backing user service
  155. super.updateUserModel(user);
  156. // cleanup resources
  157. identity.dispose();
  158. return user;
  159. }
  160. }