aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/dnsconfig/DnssecPanel.java
blob: 0500c07dd6598e9ed31de8cb3e1a9509066346b2 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.plugin.dnsconfig;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.table.*;

import net.java.sip.communicator.impl.dns.*;
import net.java.sip.communicator.plugin.desktoputil.*;
import net.java.sip.communicator.service.dns.*;
import net.java.sip.communicator.util.*;

import org.jitsi.service.configuration.*;
import org.jitsi.service.fileaccess.*;
import org.jitsi.service.resources.*;
import org.osgi.framework.*;

/**
 * Configuration of the DNSSEC validating resolver.
 *
 * @author Ingo Bauersachs
 */
public class DnssecPanel
    extends TransparentPanel
    implements ActionListener, FocusListener
{
    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 0L;

    private final static Logger logger = Logger.getLogger(DnssecPanel.class);

    //UI Controls
    private JComboBox cboDefault;
    private JCheckBox chkEnabled;
    private JCheckBox chkAbsolute;
    private JTable tblDomains;
    private JTextField txtNameservers;

    //service references
    private ResourceManagementService R;
    private ConfigurationService config;
    private TableModel data = new DnssecTableModel();
    private final ParallelDnsPanel parallelDnsPanel;

    /**
     * Create a new instance of this class.
     * @param parallelDnsPanel the panel configuring the parallel resolver
     */
    public DnssecPanel(ParallelDnsPanel parallelDnsPanel)
    {
        this.parallelDnsPanel = parallelDnsPanel;
        initServices();
        initComponents();
        loadData();
        updateState();
        chkAbsolute.addActionListener(this);
        chkEnabled.addActionListener(this);
        cboDefault.addActionListener(this);
        txtNameservers.addFocusListener(this);
    }

    /**
     * Loads all service references
     */
    private void initServices()
    {
        BundleContext bc = DnsConfigActivator.bundleContext;
        R = ServiceUtils.getService(bc, ResourceManagementService.class);
        config = ServiceUtils.getService(bc, ConfigurationService.class);
    }

    /**
     * Create the UI components
     */
    private void initComponents()
    {
        setLayout(new BorderLayout(0, 10));
        setBorder(BorderFactory.createEmptyBorder(10, 5, 0, 0));

        JPanel pnlCommon = new TransparentPanel(new GridBagLayout());
        pnlCommon.setAlignmentX(LEFT_ALIGNMENT);
        GridBagConstraints cl = new GridBagConstraints();
        GridBagConstraints cr = new GridBagConstraints();
        cl.gridy = cr.gridy = 0;
        cl.anchor = cr.anchor = GridBagConstraints.LINE_START;
        cl.gridx = 0;
        cl.fill = GridBagConstraints.HORIZONTAL;
        cl.weightx = 0;
        cl.insets = new Insets(0, 0, 0, 10);
        cr.gridx = 1;
        cr.fill = GridBagConstraints.HORIZONTAL;
        cr.gridwidth = GridBagConstraints.REMAINDER;
        cr.weightx = 1;
        add(pnlCommon, BorderLayout.NORTH);


        //always use absolute names
        chkAbsolute = new SIPCommCheckBox(
            R.getI18NString("plugin.dnsconfig.dnssec.chkAbsolute"));
        cl.gridwidth = 2;
        pnlCommon.add(chkAbsolute, cl);
        cl.gridwidth = 1;

        //dnssec enable/disable
        cl.gridy = ++cr.gridy;
        chkEnabled = new SIPCommCheckBox(
            R.getI18NString("plugin.dnsconfig.dnssec.chkEnabled"));
        cl.gridwidth = 2;
        pnlCommon.add(chkEnabled, cl);
        cl.gridwidth = 1;

        cl.gridy = ++cr.gridy;
        JLabel lblRestart = new JLabel(
            R.getI18NString("plugin.dnsconfig.dnssec.RESTART_WARNING",
            new String[]{
                R.getSettingsString("service.gui.APPLICATION_NAME")
            }));
        lblRestart.setBorder(BorderFactory.createEmptyBorder(0, 22, 10, 0));
        cl.gridwidth = GridBagConstraints.REMAINDER;
        pnlCommon.add(lblRestart, cl);
        cl.gridwidth = 1;

        //custom nameservers
        cl.gridy = ++cr.gridy;
        JLabel lblNameserver = new JLabel(
            R.getI18NString("plugin.dnsconfig.dnssec.lblNameservers"));
        lblNameserver.setBorder(BorderFactory.createEmptyBorder(0, 22, 0, 0));
        pnlCommon.add(lblNameserver, cl);

        txtNameservers = new JTextField();
        pnlCommon.add(txtNameservers, cr);
        cl.gridy = ++cr.gridy;
        JLabel lblNsHint = new JLabel(
            R.getI18NString("plugin.dnsconfig.dnssec.lblNameserversHint"));
        lblNsHint.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        pnlCommon.add(lblNsHint, cr);

        //default dnssec handling
        cl.gridy = ++cr.gridy;
        JLabel lblDefault = new JLabel(
            R.getI18NString("plugin.dnsconfig.dnssec.lblDefault"));
        lblDefault.setBorder(BorderFactory.createEmptyBorder(0, 22, 0, 0));
        pnlCommon.add(lblDefault, cl);
        cboDefault = new JComboBox(SecureResolveMode.values());
        cboDefault.setRenderer(getResolveModeRenderer());
        pnlCommon.add(cboDefault, cr);

        //domain list table
        tblDomains = new JTable(data);
        tblDomains.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tblDomains.setRowHeight(20);
        tblDomains.getColumnModel().getColumn(1).setCellRenderer(
            new DefaultTableCellRenderer()
            {
                /**
                 * Serial version UID.
                 */
                private static final long serialVersionUID = 0L;

                @Override
                protected void setValue(Object value)
                {
                    if (value instanceof SecureResolveMode)
                        setText(R.getI18NString(
                            "net.java.sip.communicator.util.dns."
                            + SecureResolveMode.class.getSimpleName()
                            + "."
                            + ((SecureResolveMode) value).name()));
                    else
                        super.setValue(value);
                }
            }
        );
        JComboBox cboTblModeEditor = new JComboBox(SecureResolveMode.values());
        cboTblModeEditor.setRenderer(getResolveModeRenderer());
        tblDomains.getColumnModel().getColumn(1).setCellEditor(
            new DefaultCellEditor(cboTblModeEditor));
        JScrollPane pnlScroller = new JScrollPane(tblDomains);
        pnlScroller.setBorder(BorderFactory.createEmptyBorder(0, 22, 0, 0));
        pnlScroller.setOpaque(false);
        add(pnlScroller, BorderLayout.CENTER);
    }

    /**
     * Reads the configured properties or their defaults into the UI controls.
     */
    private void loadData()
    {
        cboDefault.setSelectedItem(Enum.valueOf(SecureResolveMode.class,
            config.getString(
                ConfigurableDnssecResolver.PNAME_DNSSEC_VALIDATION_MODE,
                SecureResolveMode.WarnIfBogus.name())
            )
        );
        chkEnabled.setSelected(config.getBoolean(
            CustomResolver.PNAME_DNSSEC_RESOLVER_ENABLED,
            CustomResolver.PDEFAULT_DNSSEC_RESOLVER_ENABLED
        ));
        chkAbsolute.setSelected(config.getBoolean(
            NetworkUtils.PNAME_DNS_ALWAYS_ABSOLUTE,
            NetworkUtils.PDEFAULT_DNS_ALWAYS_ABSOLUTE
        ));
        txtNameservers.setText(
            config.getString(DnsUtilActivator.PNAME_DNSSEC_NAMESERVERS));
    }

    /**
     * Action has occurred in the config form.
     * @param e the action event
     */
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == cboDefault)
        {
            if(cboDefault.getSelectedItem() == null)
                return;

            SecureResolveMode oldMode = Enum.valueOf(SecureResolveMode.class,
                config.getString(
                    ConfigurableDnssecResolver.PNAME_DNSSEC_VALIDATION_MODE,
                    SecureResolveMode.WarnIfBogus.name())
                );
            config.setProperty(
                ConfigurableDnssecResolver.PNAME_DNSSEC_VALIDATION_MODE,
                ((SecureResolveMode)cboDefault.getSelectedItem()).name());

            //update all values that had the default to the new default
            for(int i = 0; i < tblDomains.getModel().getRowCount(); i++)
            {
                SecureResolveMode m = (SecureResolveMode)data.getValueAt(i, 1);
                if(m == oldMode)
                    data.setValueAt(cboDefault.getSelectedItem(), i, 1);
            }
            tblDomains.repaint();
            return;
        }
        if(e.getSource() == chkEnabled)
        {
            File f;
            try
            {
                f = DnsConfigActivator.getFileAccessService()
                    .getPrivatePersistentFile(".usednsjava",
                        FileCategory.PROFILE);
                if(chkEnabled.isSelected())
                {
                    if(!f.createNewFile() && !f.exists())
                        chkEnabled.setSelected(UnboundApi.isAvailable());
                }
                else
                {
                    if(!f.delete() && f.exists())
                        chkEnabled.setSelected(true);
                }
                config.setProperty(
                    CustomResolver.PNAME_DNSSEC_RESOLVER_ENABLED,
                    chkEnabled.isSelected());
            }
            catch (Exception ex)
            {
                logger.error("failed to enable DNSSEC", ex);
                ErrorDialog ed = new ErrorDialog(
                    null,
                    R.getI18NString("plugin.dnsconfig.dnssec.ENABLE_FAILED"),
                    R.getI18NString("plugin.dnsconfig.dnssec.ENABLE_FAILED_MSG"),
                    ex);
                ed.showDialog();
            }
            updateState();
        }
        if(e.getSource() == chkAbsolute)
        {
            config.setProperty(
                NetworkUtils.PNAME_DNS_ALWAYS_ABSOLUTE,
                chkAbsolute.isSelected());
            updateState();
        }
    }

    /**
     * Updates the form behavior when the resolver is enabled or disabled.
     */
    private void updateState()
    {
        cboDefault.setEnabled(chkEnabled.isSelected());
        txtNameservers.setEnabled(chkEnabled.isSelected());
        tblDomains.setEnabled(chkEnabled.isSelected());
        parallelDnsPanel.updateDnssecState();
    }

    /**
     * Creates a ComboBox renderer for the resolve mode column. The non-edit
     * text is based on the selected value of the row for which the renderer is
     * created.
     *
     * @return ComboBox render for the SecureResolveMode enum.
     */
    private BasicComboBoxRenderer getResolveModeRenderer()
    {
        return new BasicComboBoxRenderer()
        {
            /**
             * Serial version UID.
             */
            private static final long serialVersionUID = 0L;

            @Override
            public Component getListCellRendererComponent(JList list,
                Object value, int index, boolean isSelected,
                boolean cellHasFocus)
            {
                Component c =
                    super.getListCellRendererComponent(list, value, index,
                        isSelected, cellHasFocus);
                setText(R.getI18NString("net.java.sip.communicator.util.dns."
                    + SecureResolveMode.class.getSimpleName()
                    + "."
                    + value));
                return c;
            }
        };
    }

    /**
     * A text field has lost the focus in the config form.
     * @param e the action event
     */
    public void focusLost(FocusEvent e)
    {
        if(e.getSource() == txtNameservers)
        {
            config.setProperty(
                DnsUtilActivator.PNAME_DNSSEC_NAMESERVERS,
                txtNameservers.getText());
            DnsUtilActivator.reloadDnsResolverConfig();
        }
    }

    public void focusGained(FocusEvent e)
    {}
}