001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander V. Astapchuk
020: * @version $Revision$
021: */package org.apache.harmony.auth.module;
022:
023: import java.util.Map;
024:
025: import org.apache.harmony.auth.NTSidGroupPrincipal;
026: import org.apache.harmony.auth.NTSidPrimaryGroupPrincipal;
027: import org.apache.harmony.auth.NTSidUserPrincipal;
028:
029: /**
030: * A helper class which queries information about the current NT user.
031: */
032: public final class NTSystem {
033:
034: // Shows whether the hyauth library was loaded or not
035: private static boolean loadLibDone;
036:
037: // User's sid, domain and name
038: private NTSidUserPrincipal user;
039:
040: // User's domain sid
041: private String domainSid;
042:
043: // User's primary group
044: /**/NTSidPrimaryGroupPrincipal mainGroup;
045:
046: // A list of groups the user belongs to
047: /**/NTSidGroupPrincipal[] groups;
048:
049: // Impersonation token
050: private long token;
051:
052: // May be used to trace the native library execution
053: @SuppressWarnings("unused")
054: private boolean debugNative;
055:
056: /**
057: * The default constructor. Loads hyauth library if necessary.
058: * @throws UnsatisfiedLinkError if library hyauth not found
059: */
060: public NTSystem() {
061: super ();
062: synchronized (NTSystem.class) {
063: if (!loadLibDone) {
064: System.loadLibrary("hyauth"); //$NON-NLS-1$
065: initNatives();
066: loadLibDone = true;
067: }
068: }
069: }
070:
071: /**
072: * Constructs an instance with options.
073: * @param options
074: */
075: public NTSystem(Map<String, ?> options) {
076: this ();
077: debugNative = "true".equalsIgnoreCase((String) options //$NON-NLS-1$
078: .get("debugNative")); //$NON-NLS-1$
079: }
080:
081: /**
082: * Initializes internal data.
083: */
084: private static native void initNatives();
085:
086: /**
087: * Load the security information about user.
088: */
089: public native void load();
090:
091: /**
092: * Frees internal data stored during login().
093: */
094: public native void free();
095:
096: /**
097: * Returns name of user's domain
098: */
099: public String getDomain() {
100: return user.getObjectDomain();
101: }
102:
103: /**
104: * Returns String representation of SID of user's domain
105: */
106: public String getDomainSID() {
107: return domainSid;
108: }
109:
110: /**
111: * Returns array of SIDs of groups the user belongs to
112: */
113: public String[] getGroupIDs() {
114: if (groups == null || groups.length == 0) {
115: return null;
116: }
117: String[] gids = new String[groups.length];
118: for (int i = 0; i < groups.length; i++) {
119: gids[i] = groups[i].getName();
120: }
121: return gids;
122: }
123:
124: /**
125: * Returns implementation token
126: */
127: public long getImpersonationToken() {
128: return token;
129: }
130:
131: /**
132: * Returns user name
133: */
134: public String getName() {
135: return user.getObjectName();
136: }
137:
138: /**
139: * Returns a SID of user's main group
140: */
141: public String getPrimaryGroupID() {
142: return mainGroup.getSid();
143: }
144:
145: /**
146: * Returns user's SID
147: */
148: public String getUserSID() {
149: return user.getSid();
150: }
151:
152: /**
153: * Returns a String representation of this object.
154: */
155: @Override
156: public String toString() {
157: String s = "NTSystem:\n"; //$NON-NLS-1$
158: s += " user : " + user + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
159: s += " domainSid : " + domainSid + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
160: s += " mainGroup : " + mainGroup + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
161: s += " token : " + token + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
162: s += " groups count : " + (groups == null ? 0 : groups.length); //$NON-NLS-1$
163: if (groups != null) {
164: s += "\n"; //$NON-NLS-1$
165: for (int i = 0; i < groups.length; i++) {
166: s += " " + i + "] " + groups[i] + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
167: }
168: }
169: return s;
170: }
171:
172: /**
173: * Returns an array of groups the user belongs to
174: */
175: public NTSidGroupPrincipal[] getGroups() {
176: if (groups == null) {
177: return null;
178: }
179: NTSidGroupPrincipal[] tmp = new NTSidGroupPrincipal[groups.length];
180: System.arraycopy(groups, 0, tmp, 0, groups.length);
181: return tmp;
182: }
183: }
|