summaryrefslogtreecommitdiffstats
path: root/base/silent/src/com/netscape/pkisilent/common/ServerInfo.java
blob: 706d4ccee47568f100b705fb99b824d6ddbd94d8 (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
package com.netscape.pkisilent.common;

// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.net.InetAddress;
import java.util.StringTokenizer;

/**
 * CMS Test framework .
 * This class fetches all the necssary ServerInformation to run the test . For example AdminServer information linke
 * port , hsotname, Config LDAP server port, CMS servers Agentport,AdminPort, EESSL port, EE port etc..
 */

public class ServerInfo {

    public String serverRoot, instanceRoot, instanceID;
    public String ldapPort, ldapHost, ldapSSLPort, ldapBaseSuffix, adminPort, admDN, admDNPW, singleSignOnPWD, domain;
    public String caSigningCertNickName, raSigningCertNickName, ocspSigningCertNickName, kraTransportCertNickName;
    public String ServerCertNickName, CertAuthority;
    public String CMSAgentPort, CMSEESSLPort, CMSEEPort, CMSAdminPort, IDBPort;

    public static CMSProperties props = null;
    public static CMSProperties CMSprops = null;

    // Private variables
    public String CMSConfigFile, AdminConfigFile;

    public ServerInfo() {
    }

    /**
     * Constructor. Takes Server root as parameter for example ( /export/qa). Reads and collects information about
     * adminserver and Config LDAP server.
     */
    public ServerInfo(String sroot) {
        serverRoot = sroot;
        AdminConfigFile = serverRoot + "/admin-serv/config/adm.conf";
        readAdminConfig();
        SystemInfo();
    }

    /**
     * Constructor. Takes Serverroot ( /export/qa) and instanceRoot (/export/qa/cert-jupiter2) as parameters . Reads and
     * collects information about Admin Server , Config LDAP server and CMS server .
     */

    public ServerInfo(String sroot, String instRoot) {
        serverRoot = sroot;
        instanceRoot = instRoot;
        CMSConfigFile = instanceRoot + "/config/CS.cfg";
        AdminConfigFile = serverRoot + "/admin-serv/config/adm.conf";
        instanceID = instanceRoot.substring(instanceRoot.indexOf("cert-") + 5);
        readAdminConfig();
        SystemInfo();
        parseServerXML();
        readCMSConfig();
    }

    public String GetAdminPort() {
        return adminPort;
    }

    public String GetConfigLDAPPort() {
        return ldapPort;
    }

    public String GetHostName() {
        if (domain.indexOf(".") > 0) {
            return domain.substring(0, domain.indexOf("."));
        } else {
            return domain;
        }
    }

    public String GetInstanceID() {
        return instanceID;
    }

    public String GetCMSConfigFileName() {
        return CMSConfigFile;
    }

    public String GetDomainName() {
        return ldapHost.substring(ldapHost.indexOf(".") + 1);
    }

    public String GetAgentPort() {
        return CMSAgentPort;
    }

    public String GetEESSLPort() {
        return CMSEESSLPort;
    }

    public String GetEEPort() {
        return CMSEEPort;
    }

    public String GetCMSAdminPort() {
        return CMSAdminPort;
    }

    public String GetInternalDBPort() {
        return IDBPort;
    }

    public String GetCertAuthority() {
        return CertAuthority;
    }

    public String GetCASigningCert() {
        return caSigningCertNickName;
    }

    public String GetRASigningCert() {
        return raSigningCertNickName;
    }

    public String GetServerCertNickName() {
        return ServerCertNickName;
    }

    public void setInstanceRoot(String instRoot) {
        instanceRoot = instRoot;
        CMSConfigFile = instanceRoot + "/config/CS.cfg";
        AdminConfigFile = serverRoot + "/admin-serv/config/adm.conf";
        instanceID = instanceRoot.substring(instanceRoot.indexOf("cert-") + 5);
        SystemInfo();
        parseServerXML();
        readCMSConfig();
    }

    // Private functions
    private void SystemInfo() {
        try {
            domain = InetAddress.getLocalHost().getHostName();
            System.out.println("Debu:SystemInfo " + domain);
        } catch (Exception e) {
            System.out.println("Exception InetAddress : " + e.getMessage());
        }

    }

    private void parseServerXML() {
        int AGENT = 1;
        int ADMIN = 2;
        int EE_SSL = 3;
        int EE_NON_SSL = 4;
        int IP = 5;
        int PORT = 6;
        BufferedReader in = null;

        try {
            String xmlFilePath = instanceRoot + "/config/server.xml";

            in = new BufferedReader(new FileReader(xmlFilePath));
            String s = in.readLine();

            while (s != null) {
                // <LS id="agent" ip="0.0.0.0" port="8101" security="on"
                // acceptorthreads="1" blocking="no">
                if (s.startsWith("<LS id=")) {
                    StringTokenizer st = new StringTokenizer(s, "\"");
                    int index1 = 5, index2 = 3;

                    while (st.hasMoreTokens()) {
                        String token = st.nextToken();

                        if (token.equalsIgnoreCase("agent")) {
                            index1 = AGENT;
                        } else if (token.equalsIgnoreCase("admin")) {
                            index1 = ADMIN;
                        } else if (token.equalsIgnoreCase("eeSSL")) {
                            index1 = EE_SSL;
                        } else if (token.equalsIgnoreCase("ee_nonSSL")) {
                            index1 = EE_NON_SSL;
                        } else if (token.equals(" ip=")) {
                            index2 = IP;
                        } else if (token.equals(" port=")) {
                            index2 = PORT;
                        }

                        if (index1 != 5 && index2 == IP && !token.equals(" ip=")) {
                            // token contains the ip value
                        } else if (index2 == PORT && !token.equals(" port=")) {

                            switch (index1) {
                            case 1:
                                CMSAgentPort = token;
                                break;

                            case 2:
                                CMSAdminPort = token;
                                break;

                            case 3:
                                CMSEESSLPort = token;
                                break;

                            case 4:
                                CMSEEPort = token;
                                break;

                            default:
                                break;

                            }

                            break;
                        }
                    } // while token
                } // if LS
                s = in.readLine();
            } // while file no end
            in.close();
        } catch (Exception e) {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception ex) {
                }
            }
        }
    }

    private String stripSpace(String s) {

        StringBuffer val = new StringBuffer();

        for (int i = 0; i < s.length(); i++) {
            if ((s.charAt(i) == ' ')) {
                i++;
                continue;
            } else {
                val.append(s.charAt(i));
            }
        }
        return val.toString();
    }

    private void readAdminConfig() {
        String ldapHostStr = "ldapHost:";
        String ldapPortStr = "ldapPort:";
        String adminPortStr = "port:";

        try {
            FileInputStream fis = new FileInputStream(AdminConfigFile);
            int size = fis.available();
            byte b[] = new byte[size];

            if (fis.read(b) != b.length) {
                System.out.println("Could not read ");

            } else {
                String tmpstr = new String(b, 0, b.length);
                int ret;

                if ((ret = tmpstr.indexOf(ldapHostStr)) > -1) {
                    ldapHost = tmpstr.substring(ret + ldapHostStr.length() + 1,
                            tmpstr.indexOf("ldapPort", ret) - 1);
                    ldapHost = stripSpace(ldapHost);
                    // System.out.println(ldapPort);
                }

                if ((ret = tmpstr.indexOf(ldapPortStr)) > -1) {
                    ldapPort = tmpstr.substring(ret + ldapPortStr.length() + 1,
                            tmpstr.indexOf("sie", ret) - 1);
                    ldapPort = stripSpace(ldapPort);
                    // System.out.println(ldapPort);
                }
                if ((ret = tmpstr.indexOf(adminPortStr)) > -1) {
                    adminPort = tmpstr.substring(ret + adminPortStr.length() + 1,
                            tmpstr.indexOf("ldapStart", ret) - 1);
                    adminPort = stripSpace(adminPort);
                    // System.out.println(adminPort);
                }

            }

            fis.close();
        } catch (Exception e) {
            System.out.println("exception " + e.getMessage());
        }

    }

    private void readCMSConfig() {

        try {
            FileInputStream fis = new FileInputStream(CMSConfigFile);

            CMSprops = new CMSProperties();
            CMSprops.load(fis);
            System.out.println("Reading CMS Config file successful");
            CertAuthority = CMSprops.getProperty("subsystem.0.id");
            if (CertAuthority.equals("ca")) {
                caSigningCertNickName = CMSprops.getProperty(
                        "ca.signing.cacertnickname");
                ServerCertNickName = "Server-Cert cert-" + instanceID;
            }
            if (CertAuthority.equals("ra")) {
                raSigningCertNickName = CMSprops.getProperty(
                        "ra.signing.cacertnickname");
                ServerCertNickName = "Server-Cert cert-" + instanceID;
            }
            IDBPort = CMSprops.getProperty("internaldb.ldapconn.port");

            fis.close();
        } catch (Exception e) {
            System.out.println("exception " + e.getMessage());
        }

    }

    public static void main(String args[]) {
        ServerInfo s = new ServerInfo("Test", "Test");

        System.out.println(" Admin Port : " + s.GetAdminPort());
        System.out.println(" LDAP Port : " + s.GetConfigLDAPPort());
        System.out.println("Hostname " + s.GetHostName());
        System.out.println("InstanceID" + s.GetInstanceID());
        System.out.println(" doamin name : " + s.GetDomainName());
        System.out.println("AgentPort " + s.GetAgentPort());
        System.out.println("EESSLPort " + s.GetEESSLPort());
        System.out.println("EEPort " + s.GetEEPort());
        System.out.println("CMSAdminPort :" + s.GetCMSAdminPort());
        System.out.println("CAAuthority : " + s.GetCertAuthority());
        System.out.println("CASigningCert:" + s.GetCASigningCert());
        System.out.println("RASigningCert:" + s.GetRASigningCert());
        System.out.println("ServerCert" + s.GetServerCertNickName());

    }// end of function main

} // end of class