View Javadoc
1   /**
2    * Waffle (https://github.com/dblock/waffle)
3    *
4    * Copyright (c) 2010 - 2015 Application Security, Inc.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/epl-v10.html
10   *
11   * Contributors:
12   *     Application Security, Inc.
13   */
14  package waffle.windows.auth.impl;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import waffle.windows.auth.IWindowsComputer;
20  
21  import com.sun.jna.platform.win32.LMJoin;
22  import com.sun.jna.platform.win32.Netapi32Util;
23  import com.sun.jna.platform.win32.Netapi32Util.LocalGroup;
24  
25  /**
26   * Windows Computer.
27   * 
28   * @author dblock[at]dblock[dot]org
29   */
30  public class WindowsComputerImpl implements IWindowsComputer {
31  
32      /** The computer name. */
33      private final String computerName;
34      
35      /** The domain name. */
36      private final String domainName;
37  
38      /**
39       * Instantiates a new windows computer impl.
40       *
41       * @param newComputerName
42       *            the new computer name
43       */
44      public WindowsComputerImpl(final String newComputerName) {
45          this.computerName = newComputerName;
46          this.domainName = Netapi32Util.getDomainName(newComputerName);
47      }
48  
49      /* (non-Javadoc)
50       * @see waffle.windows.auth.IWindowsComputer#getComputerName()
51       */
52      @Override
53      public String getComputerName() {
54          return this.computerName;
55      }
56  
57      /* (non-Javadoc)
58       * @see waffle.windows.auth.IWindowsComputer#getGroups()
59       */
60      @Override
61      public String[] getGroups() {
62          final List<String> groupNames = new ArrayList<String>();
63          final LocalGroup[] groups = Netapi32Util.getLocalGroups(this.computerName);
64          for (final LocalGroup group : groups) {
65              groupNames.add(group.name);
66          }
67          return groupNames.toArray(new String[0]);
68      }
69  
70      /* (non-Javadoc)
71       * @see waffle.windows.auth.IWindowsComputer#getJoinStatus()
72       */
73      @Override
74      public String getJoinStatus() {
75          final int joinStatus = Netapi32Util.getJoinStatus(this.computerName);
76          switch (joinStatus) {
77              case LMJoin.NETSETUP_JOIN_STATUS.NetSetupDomainName:
78                  return "NetSetupDomainName";
79              case LMJoin.NETSETUP_JOIN_STATUS.NetSetupUnjoined:
80                  return "NetSetupUnjoined";
81              case LMJoin.NETSETUP_JOIN_STATUS.NetSetupWorkgroupName:
82                  return "NetSetupWorkgroupName";
83              case LMJoin.NETSETUP_JOIN_STATUS.NetSetupUnknownStatus:
84                  return "NetSetupUnknownStatus";
85              default:
86                  throw new RuntimeException("Unsupported join status: " + joinStatus);
87          }
88      }
89  
90      /* (non-Javadoc)
91       * @see waffle.windows.auth.IWindowsComputer#getMemberOf()
92       */
93      @Override
94      public String getMemberOf() {
95          return this.domainName;
96      }
97  
98  }