001: /*_############################################################################
002: _##
003: _## SNMP4J - UsmUserEntry.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.security;
022:
023: import java.io.Serializable;
024: import org.snmp4j.smi.OctetString;
025: import org.snmp4j.smi.OID;
026:
027: /**
028: * The <code>UsmUserEntry</code> class represents a user in the
029: * Local Configuration Datastore (LCD).
030: *
031: * @author Frank Fock
032: * @version 1.1
033: */
034: public class UsmUserEntry implements Serializable, Comparable {
035:
036: private static final long serialVersionUID = -3021438367015187166L;
037:
038: private OctetString engineID;
039: private OctetString userName;
040: private UsmUser usmUser;
041: private byte[] authenticationKey;
042: private byte[] privacyKey;
043:
044: /**
045: * Creates a new user entry with empty engine ID and empty user.
046: */
047: public UsmUserEntry() {
048: engineID = new OctetString();
049: userName = new OctetString();
050: usmUser = new UsmUser(new OctetString(), null, null, null, null);
051: }
052:
053: /**
054: * Creates a user with user name and associated {@link UsmUser}.
055: * @param userName
056: * the user name of the new entry.
057: * @param user
058: * the <code>UsmUser</code> representing the security information of the
059: * user.
060: */
061: public UsmUserEntry(OctetString userName, UsmUser user) {
062: this .userName = userName;
063: this .usmUser = user;
064: if (user.isLocalized()) {
065: if ((user.getAuthenticationProtocol() != null)
066: && (user.getAuthenticationPassphrase() != null)) {
067: authenticationKey = user.getAuthenticationPassphrase()
068: .getValue();
069: if ((user.getPrivacyProtocol() != null)
070: && (user.getPrivacyPassphrase() != null)) {
071: privacyKey = user.getPrivacyPassphrase().getValue();
072: }
073: }
074: }
075: }
076:
077: /**
078: * Creates a user with user name and associated {@link UsmUser}.
079: * @param userName
080: * the user name of the new entry.
081: * @param engineID
082: * the authoritative engine ID associated with the user.
083: * @param user
084: * the <code>UsmUser</code> representing the security information of the
085: * user.
086: */
087: public UsmUserEntry(OctetString userName, OctetString engineID,
088: UsmUser user) {
089: this (userName, user);
090: this .engineID = engineID;
091: }
092:
093: /**
094: * Creates a localized user entry.
095: * @param engineID
096: * the engine ID for which the users has bee localized.
097: * @param securityName
098: * the user and security name of the new entry.
099: * @param authProtocol
100: * the authentication protocol ID.
101: * @param authKey
102: * the authentication key.
103: * @param privProtocol
104: * the privacy protocol ID.
105: * @param privKey
106: * the privacy key.
107: */
108: public UsmUserEntry(byte[] engineID, OctetString securityName,
109: OID authProtocol, byte[] authKey, OID privProtocol,
110: byte[] privKey) {
111: this .engineID = (engineID == null) ? null : new OctetString(
112: engineID);
113: this .userName = securityName;
114: this .authenticationKey = authKey;
115: this .privacyKey = privKey;
116: this .usmUser = new UsmUser(userName, authProtocol,
117: ((authenticationKey != null) ? new OctetString(
118: authenticationKey) : null), privProtocol,
119: ((privacyKey != null) ? new OctetString(privacyKey)
120: : null), this .engineID);
121: }
122:
123: public OctetString getEngineID() {
124: return engineID;
125: }
126:
127: public void setEngineID(OctetString engineID) {
128: this .engineID = engineID;
129: }
130:
131: public void setUserName(OctetString userName) {
132: this .userName = userName;
133: }
134:
135: public OctetString getUserName() {
136: return userName;
137: }
138:
139: public void setUsmUser(UsmUser usmUser) {
140: this .usmUser = usmUser;
141: }
142:
143: public UsmUser getUsmUser() {
144: return usmUser;
145: }
146:
147: public void setAuthenticationKey(byte[] authenticationKey) {
148: this .authenticationKey = authenticationKey;
149: }
150:
151: public byte[] getAuthenticationKey() {
152: return authenticationKey;
153: }
154:
155: public void setPrivacyKey(byte[] privacyKey) {
156: this .privacyKey = privacyKey;
157: }
158:
159: public byte[] getPrivacyKey() {
160: return privacyKey;
161: }
162:
163: /**
164: * Compares this user entry with another one by engine ID then by their user
165: * names.
166: *
167: * @param o
168: * a <code>UsmUserEntry</code> instance.
169: * @return
170: * a negative integer, zero, or a positive integer as this object is
171: * less than, equal to, or greater than the specified object.
172: */
173: public int compareTo(Object o) {
174: UsmUserEntry other = (UsmUserEntry) o;
175: int result = 0;
176: if ((engineID != null) && (other.engineID != null)) {
177: result = engineID.compareTo(other.engineID);
178: } else if ((engineID != null) && (other.engineID == null)) {
179: result = 1;
180: } else if ((engineID == null) && (other.engineID != null)) {
181: result = -1;
182: }
183: if (result == 0) {
184: result = userName.compareTo(other.userName);
185: if (result == 0) {
186: result = usmUser.compareTo(other.usmUser);
187: }
188: }
189: return result;
190: }
191:
192: public String toString() {
193: return "UsmUserEntry[userName=" + userName + ",usmUser="
194: + usmUser + "]";
195: }
196:
197: }
|