From ae1452644583d38a82a37e86c4240815e0fc4a9b Mon Sep 17 00:00:00 2001
From: Jack Magne <jmagne@dhcp-16-213.sjc.redhat.com>
Date: Mon, 14 Apr 2014 15:39:50 -0700
Subject: [PATCH] PhoneHome feature:

1. Provides an xml file served by TPS to allow the client(esc) to configure itself to contact TPS.
---
 base/server/etc/default.cfg                        |  1 +
 .../python/pki/server/deployment/pkiparser.py      |  6 ++
 .../deployment/scriptlets/slot_substitution.py     |  4 +
 base/tps-tomcat/shared/conf/phoneHome.xml          | 20 +++++
 base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml | 11 +++
 .../src/org/dogtagpki/server/tps/TPSPhoneHome.java | 96 ++++++++++++++++++++++
 6 files changed, 138 insertions(+)
 create mode 100644 base/tps-tomcat/shared/conf/phoneHome.xml
 create mode 100644 base/tps-tomcat/src/org/dogtagpki/server/tps/TPSPhoneHome.java

diff --git a/base/server/etc/default.cfg b/base/server/etc/default.cfg
index 41b3bd3..9f6859b 100644
--- a/base/server/etc/default.cfg
+++ b/base/server/etc/default.cfg
@@ -606,6 +606,7 @@ pki_enable_server_side_keygen=False
 pki_import_shared_secret=False
 pki_share_db=True
 pki_share_dbuser_dn=uid=pkidbuser,ou=people,o=%(pki_instance_name)s-CA
+pki_source_phone_home_xml=/usr/share/pki/%(pki_subsystem_type)s/conf/phoneHome.xml
 
 # Paths
 # These are used in the processing of pkispawn and are not supposed
diff --git a/base/server/python/pki/server/deployment/pkiparser.py b/base/server/python/pki/server/deployment/pkiparser.py
index 8348648..b0e5fbc 100644
--- a/base/server/python/pki/server/deployment/pkiparser.py
+++ b/base/server/python/pki/server/deployment/pkiparser.py
@@ -711,6 +711,12 @@ class PKIConfigParser:
                         os.path.join(self.pki_master_dict\
                                      ['pki_subsystem_configuration_path'],
                                      "transportCert.profile")
+                elif self.pki_master_dict['pki_subsystem'] == "TPS":
+                    self.pki_master_dict['pki_target_phone_home_xml'] = \
+                        os.path.join(
+                            self.pki_master_dict['pki_subsystem_configuration_path'],
+                            "phoneHome.xml")
+
             # Slot assignment name/value pairs
             #     NOTE:  Master key == Slots key; Master value ==> Slots value
             self.pki_master_dict['PKI_INSTANCE_NAME_SLOT'] = \
diff --git a/base/server/python/pki/server/deployment/scriptlets/slot_substitution.py b/base/server/python/pki/server/deployment/scriptlets/slot_substitution.py
index cc54cc4..1c13bdb 100644
--- a/base/server/python/pki/server/deployment/scriptlets/slot_substitution.py
+++ b/base/server/python/pki/server/deployment/scriptlets/slot_substitution.py
@@ -92,6 +92,10 @@ class PkiScriptlet(pkiscriptlet.AbstractBasePkiScriptlet):
                     deployer.master_dict['pki_target_proxy_conf'])
                 deployer.file.apply_slot_substitution(
                     deployer.master_dict['pki_target_profileselect_template'])
+            elif deployer.master_dict['pki_subsystem'] == "TPS":
+                deployer.file.copy_with_slot_substitution(
+                    deployer.master_dict['pki_source_phone_home_xml'],
+                    deployer.master_dict['pki_target_phone_home_xml'])
         return self.rv
 
     def destroy(self, deployer):
diff --git a/base/tps-tomcat/shared/conf/phoneHome.xml b/base/tps-tomcat/shared/conf/phoneHome.xml
new file mode 100644
index 0000000..ea31daa
--- /dev/null
+++ b/base/tps-tomcat/shared/conf/phoneHome.xml
@@ -0,0 +1,20 @@
+<ServiceInfo>
+<IssuerName>
+Fedora Project
+</IssuerName>
+<Services>
+<Operation>
+http://[PKI_HOSTNAME]:[PKI_SECURE_PORT]/tps/tps
+</Operation>
+<UI>
+</UI>
+<EnrolledTokenBrowserURL>
+http://fedoraproject.org
+</EnrolledTokenBrowserURL>
+<EnrolledTokenURL>
+</EnrolledTokenURL>
+<TokenType>
+userKey
+</TokenType>
+</Services>
+</ServiceInfo>
diff --git a/base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml b/base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml
index 17aaf51..b30e8d1 100644
--- a/base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml
+++ b/base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml
@@ -92,6 +92,11 @@
     </servlet>
 
     <servlet>
+     <servlet-name>tpsPhoneHome </servlet-name>
+     <servlet-class> org.dogtagpki.server.tps.TPSPhoneHome</servlet-class>
+    </servlet>
+
+    <servlet>
         <servlet-name>tps</servlet-name>
         <servlet-class>org.dogtagpki.server.tps.TPSServlet</servlet-class>
     </servlet>
@@ -155,6 +160,12 @@
     </servlet-mapping>
 
     <servlet-mapping>
+        <servlet-name>tpsPhoneHome</servlet-name>
+        <url-pattern>/tpsPhoneHome</url-pattern>
+    </servlet-mapping>
+
+
+    <servlet-mapping>
         <servlet-name>tps</servlet-name>
         <url-pattern>/tps</url-pattern>
     </servlet-mapping>
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSPhoneHome.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSPhoneHome.java
new file mode 100644
index 0000000..7c96025
--- /dev/null
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSPhoneHome.java
@@ -0,0 +1,96 @@
+package org.dogtagpki.server.tps;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import com.netscape.certsrv.apps.CMS;
+import com.netscape.cms.servlet.base.CMSStartServlet;
+
+public class TPSPhoneHome extends HttpServlet {
+
+    private static final long serialVersionUID = 1864386666927370987L;
+    private static String phoneHomeName = "phoneHome.xml";
+
+    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
+        //Simply return xml file to the client
+        //In the future we could get this info from elsewhere such as LDAP
+
+        CMS.debug("TPSPhoneHome entering.");
+
+        renderPhoneHome(request, response);
+    }
+
+    private void renderPhoneHome(HttpServletRequest request, HttpServletResponse response) throws ServletException,
+            IOException {
+        ServletOutputStream stream = null;
+        BufferedInputStream buf = null;
+        FileInputStream input = null;
+
+        try {
+
+            stream = response.getOutputStream();
+            response.setContentType("text/xml");
+
+            String confPath = getConfigPath();
+
+            confPath += File.separator + phoneHomeName;
+
+            input  = new FileInputStream(confPath);
+           // InputStream input = ctx.getResourceAsStream(phoneHomeName);
+            buf = new BufferedInputStream(input);
+
+            int readBytes = 0;
+            while ((readBytes = buf.read()) != -1)
+                stream.write(readBytes);
+
+        } catch (IOException e) {
+            CMS.debug("TPSPhoneHome.renderPhoneHome: Error encountered:  " + e);
+            throw new ServletException("TPSPhoneHome.renderPhoneHome: Error encountered:  " + e);
+        } finally {
+            if (stream != null)
+                stream.close();
+            if (buf != null)
+                buf.close();
+            if (input != null)
+                input.close();
+        }
+
+    }
+
+    private String getConfigPath() {
+
+        String path = null;
+        String context = getServletContext().getContextPath();
+
+        // get subsystem name by removing the / prefix from the context
+        String subsystem = context.startsWith("/") ? context.substring(1) : context;
+
+        // get config path from web.xml
+        path = getServletConfig().getInitParameter(CMSStartServlet.PROP_CMS_CFG);
+
+        // if path not specified, use default path
+        if (path == null) {
+            // catalina.base points to instance dir
+            // it's defined as CATALINA_BASE in <instance>/conf/tomcat.conf
+            String instanceDir = System.getProperty("catalina.base");
+
+            // path: <instance>/conf/<subsystem>/CS.cfg
+            path = instanceDir + File.separator + "conf" + File.separator +
+                    subsystem + File.separator;
+        }
+
+        CMS.debug("TPSPhoneHome.getConfigPath: returning: " + path);
+
+        return path;
+
+    }
+
+}
-- 
1.8.3.1

