2
|
1 |
/*
|
23711
|
2 |
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
|
|
27 |
package com.sun.jmx.snmp.IPAcl;
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
// java import
|
|
32 |
//
|
|
33 |
import java.io.Serializable;
|
|
34 |
import java.net.InetAddress;
|
|
35 |
import java.net.UnknownHostException;
|
|
36 |
import java.util.Hashtable;
|
|
37 |
import java.util.logging.Level;
|
|
38 |
import java.util.Vector;
|
|
39 |
import java.security.acl.NotOwnerException;
|
|
40 |
|
|
41 |
import static com.sun.jmx.defaults.JmxProperties.SNMP_LOGGER;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* The class defines an abstract representation of a host.
|
|
45 |
*
|
|
46 |
*/
|
23711
|
47 |
@SuppressWarnings("serial") // JDK implementation class
|
2
|
48 |
abstract class Host extends SimpleNode implements Serializable {
|
|
49 |
|
|
50 |
public Host(int id) {
|
|
51 |
super(id);
|
|
52 |
}
|
|
53 |
|
|
54 |
public Host(Parser p, int id) {
|
|
55 |
super(p, id);
|
|
56 |
}
|
|
57 |
|
|
58 |
protected abstract PrincipalImpl createAssociatedPrincipal()
|
|
59 |
throws UnknownHostException;
|
|
60 |
|
|
61 |
protected abstract String getHname();
|
|
62 |
|
|
63 |
public void buildAclEntries(PrincipalImpl owner, AclImpl acl) {
|
|
64 |
// Create a principal
|
|
65 |
//
|
|
66 |
PrincipalImpl p=null;
|
|
67 |
try {
|
|
68 |
p = createAssociatedPrincipal();
|
|
69 |
} catch(UnknownHostException e) {
|
|
70 |
if (SNMP_LOGGER.isLoggable(Level.FINEST)) {
|
|
71 |
SNMP_LOGGER.logp(Level.FINEST, Host.class.getName(),
|
|
72 |
"buildAclEntries",
|
|
73 |
"Cannot create ACL entry; got exception", e);
|
|
74 |
}
|
|
75 |
throw new IllegalArgumentException("Cannot create ACL entry for " + e.getMessage());
|
|
76 |
}
|
|
77 |
|
|
78 |
// Create an AclEntry
|
|
79 |
//
|
|
80 |
AclEntryImpl entry= null;
|
|
81 |
try {
|
|
82 |
entry = new AclEntryImpl(p);
|
|
83 |
// Add permission
|
|
84 |
//
|
|
85 |
registerPermission(entry);
|
|
86 |
acl.addEntry(owner, entry);
|
|
87 |
} catch(UnknownHostException e) {
|
|
88 |
if (SNMP_LOGGER.isLoggable(Level.FINEST)) {
|
|
89 |
SNMP_LOGGER.logp(Level.FINEST, Host.class.getName(),
|
|
90 |
"buildAclEntries",
|
|
91 |
"Cannot create ACL entry; got exception", e);
|
|
92 |
}
|
|
93 |
return;
|
|
94 |
} catch(NotOwnerException a) {
|
|
95 |
if (SNMP_LOGGER.isLoggable(Level.FINEST)) {
|
|
96 |
SNMP_LOGGER.logp(Level.FINEST, Host.class.getName(),
|
|
97 |
"buildAclEntries",
|
|
98 |
"Cannot create ACL entry; got exception", a);
|
|
99 |
}
|
|
100 |
return;
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
private void registerPermission(AclEntryImpl entry) {
|
|
105 |
JDMHost host= (JDMHost) jjtGetParent();
|
|
106 |
JDMManagers manager= (JDMManagers) host.jjtGetParent();
|
|
107 |
JDMAclItem acl= (JDMAclItem) manager.jjtGetParent();
|
|
108 |
JDMAccess access= acl.getAccess();
|
|
109 |
access.putPermission(entry);
|
|
110 |
JDMCommunities comm= acl.getCommunities();
|
|
111 |
comm.buildCommunities(entry);
|
|
112 |
}
|
|
113 |
|
|
114 |
public void buildTrapEntries(Hashtable<InetAddress, Vector<String>> dest) {
|
|
115 |
|
|
116 |
JDMHostTrap host= (JDMHostTrap) jjtGetParent();
|
|
117 |
JDMTrapInterestedHost hosts= (JDMTrapInterestedHost) host.jjtGetParent();
|
|
118 |
JDMTrapItem trap = (JDMTrapItem) hosts.jjtGetParent();
|
|
119 |
JDMTrapCommunity community = trap.getCommunity();
|
|
120 |
String comm = community.getCommunity();
|
|
121 |
|
|
122 |
InetAddress add = null;
|
|
123 |
try {
|
|
124 |
add = java.net.InetAddress.getByName(getHname());
|
|
125 |
} catch(UnknownHostException e) {
|
|
126 |
if (SNMP_LOGGER.isLoggable(Level.FINEST)) {
|
|
127 |
SNMP_LOGGER.logp(Level.FINEST, Host.class.getName(),
|
|
128 |
"buildTrapEntries",
|
|
129 |
"Cannot create TRAP entry; got exception", e);
|
|
130 |
}
|
|
131 |
return;
|
|
132 |
}
|
|
133 |
|
|
134 |
Vector<String> list = null;
|
|
135 |
if (dest.containsKey(add)){
|
|
136 |
list = dest.get(add);
|
|
137 |
if (!list.contains(comm)){
|
|
138 |
list.addElement(comm);
|
|
139 |
}
|
|
140 |
} else {
|
|
141 |
list = new Vector<String>();
|
|
142 |
list.addElement(comm);
|
|
143 |
dest.put(add,list);
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
public void buildInformEntries(Hashtable<InetAddress, Vector<String>> dest) {
|
|
148 |
|
|
149 |
JDMHostInform host= (JDMHostInform) jjtGetParent();
|
|
150 |
JDMInformInterestedHost hosts= (JDMInformInterestedHost) host.jjtGetParent();
|
|
151 |
JDMInformItem inform = (JDMInformItem) hosts.jjtGetParent();
|
|
152 |
JDMInformCommunity community = inform.getCommunity();
|
|
153 |
String comm = community.getCommunity();
|
|
154 |
|
|
155 |
InetAddress add = null;
|
|
156 |
try {
|
|
157 |
add = java.net.InetAddress.getByName(getHname());
|
|
158 |
} catch(UnknownHostException e) {
|
|
159 |
if (SNMP_LOGGER.isLoggable(Level.FINEST)) {
|
|
160 |
SNMP_LOGGER.logp(Level.FINEST, Host.class.getName(),
|
|
161 |
"buildTrapEntries",
|
|
162 |
"Cannot create INFORM entry; got exception", e);
|
|
163 |
}
|
|
164 |
return;
|
|
165 |
}
|
|
166 |
|
|
167 |
Vector<String> list = null;
|
|
168 |
if (dest.containsKey(add)){
|
|
169 |
list = dest.get(add);
|
|
170 |
if (!list.contains(comm)){
|
|
171 |
list.addElement(comm);
|
|
172 |
}
|
|
173 |
} else {
|
|
174 |
list = new Vector<String>();
|
|
175 |
list.addElement(comm);
|
|
176 |
dest.put(add,list);
|
|
177 |
}
|
|
178 |
}
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
}
|