summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/cms/servlet/csadmin/GetCookie.java
blob: 082c43b42f34296b5ec4f828d175c734518b275a (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
// --- 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 ---
package com.netscape.cms.servlet.csadmin;

import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Locale;
import java.util.Random;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.netscape.certsrv.apps.CMS;
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.cms.servlet.base.CMSServlet;
import com.netscape.cms.servlet.base.UserInfo;
import com.netscape.cms.servlet.common.CMSRequest;
import com.netscape.cms.servlet.common.CMSTemplate;
import com.netscape.cms.servlet.common.CMSTemplateParams;
import com.netscape.cms.servlet.common.ECMSGWException;

public class GetCookie extends CMSServlet {

    /**
     *
     */
    private static final long serialVersionUID = 2466968231929541707L;
    private static Random mRandom = null;
    private String mErrorFormPath = null;
    private String mFormPath = null;

    private final static String LOGGING_SIGNED_AUDIT_SECURITY_DOMAIN_UPDATE =
            "LOGGING_SIGNED_AUDIT_SECURITY_DOMAIN_UPDATE_1";
    private final static String LOGGING_SIGNED_AUDIT_ROLE_ASSUME =
            "LOGGING_SIGNED_AUDIT_ROLE_ASSUME_3";

    public GetCookie() {
        super();
    }

    /**
     * initialize the servlet.
     *
     * @param sc servlet configuration, read from the web.xml file
     */
    public void init(ServletConfig sc) throws ServletException {
        super.init(sc);

        CMS.debug("GetCookie init");
        mTemplates.remove(CMSRequest.SUCCESS);
        mRandom = new Random();
        mErrorFormPath = sc.getInitParameter("errorTemplatePath");
        if (mOutputTemplatePath != null) {
            mFormPath = mOutputTemplatePath;
        }
    }

    /**
     * Process the HTTP request.
     *
     * @param cmsReq the object holding the request and response information
     */
    protected void process(CMSRequest cmsReq) throws EBaseException {
        HttpServletRequest httpReq = cmsReq.getHttpReq();
        HttpServletResponse httpResp = cmsReq.getHttpResp();

        CMS.debug("GetCookie start");
        IAuthToken authToken = null;
        IConfigStore cs = CMS.getConfigStore();

        IArgBlock header = CMS.createArgBlock();
        IArgBlock ctx = CMS.createArgBlock();
        CMSTemplateParams argSet = new CMSTemplateParams(header, ctx);

        CMSTemplate form = null;
        Locale[] locale = new Locale[1];

        String url = httpReq.getParameter("url");
        CMS.debug("GetCookie before auth, url =" + url);
        String url_e = "";
        URL u = null;
        try {
            url_e = URLDecoder.decode(url, "UTF-8");
            u = new URL(url_e);
        } catch (Exception eee) {
            throw new ECMSGWException(
                    "GetCookie missing parameter: url");
        }

        int index2 = url_e.indexOf("subsystem=");
        String subsystem = "";
        if (index2 > 0) {
            subsystem = url.substring(index2 + 10);
            int index1 = subsystem.indexOf("&");
            if (index1 > 0)
                subsystem = subsystem.substring(0, index1);
        }

        try {
            authToken = authenticate(cmsReq);
        } catch (Exception e) {
            CMS.debug("GetCookie authentication failed");
            log(ILogger.LL_FAILURE,
                    CMS.getLogMessage("CMSGW_ERR_BAD_SERV_OUT_STREAM", "",
                            e.toString()));
            header.addStringValue("sd_uid", "");
            header.addStringValue("sd_pwd", "");
            header.addStringValue("host", u.getHost());
            header.addStringValue("sdhost", CMS.getEESSLHost());
            header.addStringValue("subsystem", subsystem);
            header.addStringValue("url", url_e);
            header.addStringValue("errorString", "Failed Authentication");
            String sdname = cs.getString("securitydomain.name", "");
            header.addStringValue("sdname", sdname);

            CMS.debug("mErrorFormPath=" + mErrorFormPath);
            try {
                form = getTemplate(mErrorFormPath, httpReq, locale);
            } catch (IOException eee) {
                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"));
                */
            }

            if (form == null) {
                CMS.debug("GetCookie::process() - form is null!");
                throw new EBaseException("form is null");
            }

            try {
                ServletOutputStream out = httpResp.getOutputStream();

                cmsReq.setStatus(CMSRequest.SUCCESS);
                httpResp.setContentType("text/html");
                form.renderOutput(out, argSet);
            } catch (IOException ee) {
                log(ILogger.LL_FAILURE,
                        CMS.getLogMessage("CMSGW_ERR_OUT_STREAM_TEMPLATE", ee.toString()));
                throw new ECMSGWException(
                        CMS.getUserMessage("CMS_GW_DISPLAY_TEMPLATE_ERROR"));
            }
            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);
                }

                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);

                        try {
                            ServletOutputStream out = httpResp.getOutputStream();

                            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 (Exception e) {
                }
            } else {
                auditMessage = CMS.getLogMessage(
                                   LOGGING_SIGNED_AUDIT_ROLE_ASSUME,
                                   uid,
                                   ILogger.FAILURE,
                                   "Enterprise " + subsystem + " Administrators");
                audit(auditMessage);
            }
        }
    }

    /**
     * Retrieves locale based on the request.
     */
    protected Locale getLocale(HttpServletRequest req) {
        Locale locale = null;
        String lang = req.getHeader("accept-language");

        if (lang == null) {
            // use server locale
            locale = Locale.getDefault();
        } else {
            locale = new Locale(UserInfo.getUserLanguage(lang),
                    UserInfo.getUserCountry(lang));
        }
        return locale;
    }
}