mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noël Rouvignac
22.56.2016 83f86f11c27378d650c3bfc7e55397687062edbc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008 Sun Microsystems, Inc.
 * Portions copyright 2013-2016 ForgeRock AS.
 */
package org.opends.server.snmp;
 
import com.sun.management.snmp.InetAddressAcl;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.Vector;
 
import org.forgerock.opendj.server.config.server.SNMPConnectionHandlerCfg;
 
/**
 * This class allows to manage the IP-ACL based access rights
 * for SNMP v1/v2c.
 */
public class SNMPInetAddressAcl implements InetAddressAcl {
 
    /**
     * Current Security Configuration for the SNMP Connection Handler.
     */
    private SNMPConnectionHandlerCfg currentConfig;
    /**
     * If * then all the users are allowed to access in read.
     */
    private static final String ALL_MANAGERS_ALLOWED = "*";
 
    private Set<InetAddress> hostsList;
    private boolean allManagers = false;
 
    private SortedSet<String> trapsDestinations;
    private String trapsCommunity;
 
    private String communities;
 
 
    /**
     * Creates an IP-Based ACL controller.
     * @param configuration of the Configuration
     */
    public SNMPInetAddressAcl(SNMPConnectionHandlerCfg configuration) {
        super();
        // Get the current configuration
        this.currentConfig = configuration;
 
        // hostsList
        SortedSet<String> tmp = this.currentConfig.getAllowedManager();
        if (tmp.contains(ALL_MANAGERS_ALLOWED)) {
            this.allManagers=true;
        }
        this.hostsList = new HashSet<InetAddress>();
        // Transform the String list into InetAddress List
        for (String dest : tmp) {
            try {
                this.hostsList.add(InetAddress.getByName(dest));
            } catch (UnknownHostException ignore) {
            }
        }
 
        // Get the list of trap destinations
        this.trapsDestinations = this.currentConfig.getTrapsDestination();
        // Get the community string to accept
        this.communities = this.currentConfig.getCommunity();
        // Get the community string to set in the traps
        this.trapsCommunity = this.currentConfig.getTrapsCommunity();
    }
 
    /**
     * Gets the name of the acl.
     * @return the name of the acl as a String
     */
    public String getName() {
        return "OpenDS";
    }
 
    /**
     * {@inheritDoc}
     */
    public boolean checkReadPermission(InetAddress address) {
        if (this.allManagers) {
            return true;
        }
 
        if ((this.hostsList==null) || (this.hostsList.isEmpty())) {
            return false;
        }
 
        // check the address is in the configured allowed managers
        return this.hostsList.contains(address);
    }
 
    /**
     * {@inheritDoc}
     */
    public boolean checkReadPermission(InetAddress address, String community) {
        if ((this.checkReadPermission(address)) &&
                (this.checkCommunity(community))) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * {@inheritDoc}
     */
    public boolean checkCommunity(String community) {
        return this.communities.equals(community);
    }
 
    /**
     * {@inheritDoc}
     */
    public boolean checkWritePermission(InetAddress address) {
        // WRITE Access are always denied
        return false;
    }
 
    /**
     * {@inheritDoc}
     */
    public boolean checkWritePermission(InetAddress address, String community) {
        // WRITE Access are always denied
        return false;
    }
 
    /**
     * {@inheritDoc}
     * @return the list of traps destinations
     */
    public Enumeration getTrapDestinations() {
        Vector<InetAddress> tempDests = new Vector<InetAddress>();
        for (String dest : this.trapsDestinations) {
            try {
                tempDests.add(InetAddress.getByName(dest));
            } catch (UnknownHostException ignore) {
            }
        }
        return tempDests.elements();
    }
 
    /**
     * {@inheritDoc}
     * @param address
     * @return the list of communities
     */
    public Enumeration getTrapCommunities(InetAddress address) {
        Vector<String> trapCommunities = new Vector<String>();
        trapCommunities.add(this.trapsCommunity);
        return trapCommunities.elements();
    }
 
    /**
     * {@inheritDoc}
     * @return an empty enumeration
     */
    public Enumeration getInformDestinations() {
        Vector<String> informDests = new Vector<String>();
        return informDests.elements();
    }
 
    /**
     * {@inheritDoc}
     * @param address
     * @return an empty enumeration
     */
    public Enumeration getInformCommunities(InetAddress address) {
        Vector<String> informCommunities = new Vector<String>();
        return informCommunities.elements();
    }
 
}