From d46119b71bbeda093c6b421698bbdd585a000861 Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Mon, 15 Oct 2012 16:44:14 -0500
Subject: [PATCH] Refactored GetCookie servlet.

The GetCookie servlet has been refactored to use the new
SecurityDomainProcessor.

Ticket #309
---
 .../netscape/cms/servlet/csadmin/GetCookie.java    | 133 ++++++++-------------
 1 file changed, 48 insertions(+), 85 deletions(-)

diff --git a/base/common/src/com/netscape/cms/servlet/csadmin/GetCookie.java b/base/common/src/com/netscape/cms/servlet/csadmin/GetCookie.java
index 082c43b42f34296b5ec4f828d175c734518b275a..efc356f8ae11eedc016fbb4ae3bcd27836b5bc55 100644
--- a/base/common/src/com/netscape/cms/servlet/csadmin/GetCookie.java
+++ b/base/common/src/com/netscape/cms/servlet/csadmin/GetCookie.java
@@ -35,8 +35,8 @@ import com.netscape.certsrv.authentication.IAuthToken;
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.base.IArgBlock;
 import com.netscape.certsrv.base.IConfigStore;
-import com.netscape.certsrv.base.ISecurityDomainSessionTable;
 import com.netscape.certsrv.logging.ILogger;
+import com.netscape.certsrv.system.InstallToken;
 import com.netscape.cms.servlet.base.CMSServlet;
 import com.netscape.cms.servlet.base.UserInfo;
 import com.netscape.cms.servlet.common.CMSRequest;
@@ -171,99 +171,62 @@ public class GetCookie extends CMSServlet {
             return;
         }
 
-        String cookie = "";
-        String auditMessage = "";
-
         if (authToken != null) {
             String uid = authToken.getInString("uid");
-            String groupname = ConfigurationUtils.getGroupName(uid, subsystem);
 
-            if (groupname != null) {
-
-                auditMessage = CMS.getLogMessage(
-                                   LOGGING_SIGNED_AUDIT_ROLE_ASSUME,
-                                   uid,
-                                   ILogger.SUCCESS,
-                                   groupname);
-                audit(auditMessage);
-
-                // assign cookie
-                long num = mRandom.nextLong();
-                cookie = num + "";
-                ISecurityDomainSessionTable ctable = CMS.getSecurityDomainSessionTable();
-                String addr = "";
-                try {
-                    addr = u.getHost();
-                } catch (Exception e) {
-                }
-                String ip = "";
-                try {
-                    ip = InetAddress.getByName(addr).toString();
-                    int index = ip.indexOf("/");
-                    if (index > 0)
-                        ip = ip.substring(index + 1);
-                } catch (Exception e) {
-                }
-
-                String auditParams = "operation;;issue_token+token;;" + cookie + "+ip;;" + ip +
-                              "+uid;;" + uid + "+groupname;;" + groupname;
-
-                int status = ctable.addEntry(cookie, ip, uid, groupname);
-                if (status == ISecurityDomainSessionTable.SUCCESS) {
-                    auditMessage = CMS.getLogMessage(
-                                       LOGGING_SIGNED_AUDIT_SECURITY_DOMAIN_UPDATE,
-                                       uid,
-                                       ILogger.SUCCESS,
-                                       auditParams);
-                    audit(auditMessage);
-                } else {
-                    auditMessage = CMS.getLogMessage(
-                                       LOGGING_SIGNED_AUDIT_SECURITY_DOMAIN_UPDATE,
-                                       uid,
-                                       ILogger.FAILURE,
-                                       auditParams);
-                    audit(auditMessage);
-                }
+            String addr = "";
+            try {
+                addr = u.getHost();
+            } catch (Exception e) {
+            }
+            String ip = "";
+            try {
+                ip = InetAddress.getByName(addr).toString();
+                int index = ip.indexOf("/");
+                if (index > 0)
+                    ip = ip.substring(index + 1);
+            } catch (Exception e) {
+            }
+
+            try {
+                SecurityDomainProcessor processor = new SecurityDomainProcessor(getLocale(httpReq));
+
+                InstallToken installToken = processor.getInstallToken(uid, subsystem, ip);
+                String cookie = installToken.getToken();
+
+                if (!url.startsWith("$")) {
+                    try {
+                        form = getTemplate(mFormPath, httpReq, locale);
+                    } catch (IOException e) {
+                        CMS.debug("GetCookie process: cant locate the form");
+                        /*
+                        log(ILogger.LL_FAILURE,
+                          CMS.getLogMessage("CMSGW_ERR_GET_TEMPLATE", e.toString()));
+                        throw new ECMSGWException(
+                          CMS.getUserMessage("CMS_GW_DISPLAY_TEMPLATE_ERROR"));
+                        */
+                    }
 
-                try {
-                    if (!url.startsWith("$")) {
-                        try {
-                            form = getTemplate(mFormPath, httpReq, locale);
-                        } catch (IOException e) {
-                            CMS.debug("GetCookie process: cant locate the form");
-                            /*
-                                                        log(ILogger.LL_FAILURE,
-                                                          CMS.getLogMessage("CMSGW_ERR_GET_TEMPLATE", e.toString()));
-                                                        throw new ECMSGWException(
-                                                          CMS.getUserMessage("CMS_GW_DISPLAY_TEMPLATE_ERROR"));
-                            */
-                        }
+                    header.addStringValue("url", url);
+                    header.addStringValue("session_id", cookie);
 
-                        header.addStringValue("url", url);
-                        header.addStringValue("session_id", cookie);
+                    try {
+                        ServletOutputStream out = httpResp.getOutputStream();
 
-                        try {
-                            ServletOutputStream out = httpResp.getOutputStream();
+                        cmsReq.setStatus(CMSRequest.SUCCESS);
+                        httpResp.setContentType("text/html");
+                        form.renderOutput(out, argSet);
 
-                            cmsReq.setStatus(CMSRequest.SUCCESS);
-                            httpResp.setContentType("text/html");
-                            form.renderOutput(out, argSet);
-                        } catch (IOException e) {
-                            log(ILogger.LL_FAILURE,
-                                    CMS.getLogMessage("CMSGW_ERR_OUT_STREAM_TEMPLATE", e.toString()));
-                            throw new ECMSGWException(
-                                    CMS.getUserMessage("CMS_GW_DISPLAY_TEMPLATE_ERROR"));
-                        }
+                    } catch (IOException e) {
+                        log(ILogger.LL_FAILURE,
+                                CMS.getLogMessage("CMSGW_ERR_OUT_STREAM_TEMPLATE", e.toString()));
+                        throw new ECMSGWException(
+                                CMS.getUserMessage("CMS_GW_DISPLAY_TEMPLATE_ERROR"));
                     }
-                } catch (Exception e) {
                 }
-            } else {
-                auditMessage = CMS.getLogMessage(
-                                   LOGGING_SIGNED_AUDIT_ROLE_ASSUME,
-                                   uid,
-                                   ILogger.FAILURE,
-                                   "Enterprise " + subsystem + " Administrators");
-                audit(auditMessage);
+
+            } catch (Exception e) {
+                e.printStackTrace();
             }
         }
     }
-- 
1.7.11.4

