View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2024 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  package waffle.windows.auth.impl;
25  
26  import com.sun.jna.platform.win32.LMJoin;
27  import com.sun.jna.platform.win32.Netapi32Util;
28  import com.sun.jna.platform.win32.Netapi32Util.LocalGroup;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import waffle.windows.auth.IWindowsComputer;
34  
35  /**
36   * Windows Computer.
37   */
38  public class WindowsComputerImpl implements IWindowsComputer {
39  
40      /** The computer name. */
41      private final String computerName;
42  
43      /** The domain name. */
44      private final String domainName;
45  
46      /**
47       * Instantiates a new windows computer impl.
48       *
49       * @param newComputerName
50       *            the new computer name
51       */
52      public WindowsComputerImpl(final String newComputerName) {
53          this.computerName = newComputerName;
54          this.domainName = Netapi32Util.getDomainName(newComputerName);
55      }
56  
57      @Override
58      public String getComputerName() {
59          return this.computerName;
60      }
61  
62      @Override
63      public String[] getGroups() {
64          final List<String> groupNames = new ArrayList<>();
65          final LocalGroup[] groups = Netapi32Util.getLocalGroups(this.computerName);
66          for (final LocalGroup group : groups) {
67              groupNames.add(group.name);
68          }
69          return groupNames.toArray(new String[0]);
70      }
71  
72      @Override
73      public String getJoinStatus() {
74          final int joinStatus = Netapi32Util.getJoinStatus(this.computerName);
75          switch (joinStatus) {
76              case LMJoin.NETSETUP_JOIN_STATUS.NetSetupDomainName:
77                  return "NetSetupDomainName";
78              case LMJoin.NETSETUP_JOIN_STATUS.NetSetupUnjoined:
79                  return "NetSetupUnjoined";
80              case LMJoin.NETSETUP_JOIN_STATUS.NetSetupWorkgroupName:
81                  return "NetSetupWorkgroupName";
82              case LMJoin.NETSETUP_JOIN_STATUS.NetSetupUnknownStatus:
83                  return "NetSetupUnknownStatus";
84              default:
85                  throw new RuntimeException("Unsupported join status: " + joinStatus);
86          }
87      }
88  
89      @Override
90      public String getMemberOf() {
91          return this.domainName;
92      }
93  
94  }