- ((PHTTP_FILTER_LOG)pvNotification)->pszClientUserName=static_cast<LPCSTR>(pfc->pFilterContext);
- return SF_STATUS_REQ_NEXT_NOTIFICATION;
- }
-
- PHTTP_FILTER_PREPROC_HEADERS pn=(PHTTP_FILTER_PREPROC_HEADERS)pvNotification;
- try
- {
- // Determine web site number. This can't really fail, I don't think.
- dynabuf buf(128);
- GetServerVariable(pfc,"INSTANCE_ID",buf,10);
-
- // Match site instance to host name, skip if no match.
- map<string,site_t>::const_iterator map_i=g_Sites.find(static_cast<char*>(buf));
- if (map_i==g_Sites.end())
- return SF_STATUS_REQ_NEXT_NOTIFICATION;
-
- ostringstream threadid;
- threadid << "[" << getpid() << "] isapi_shib" << '\0';
- saml::NDC ndc(threadid.str().c_str());
-
- // We lock the configuration system for the duration.
- IConfig* conf=g_Config->getINI();
- Locker locker(conf);
-
- // Map request to application and content settings.
- string targeturl;
- IRequestMapper* mapper=conf->getRequestMapper();
- Locker locker2(mapper);
- IRequestMapper::Settings settings=map_request(pfc,pn,mapper,map_i->second,targeturl);
- pair<bool,const char*> application_id=settings.first->getString("applicationId");
- const IApplication* application=conf->getApplication(application_id.second);
- if (!application)
- return WriteClientError(pfc,"Unable to map request to application settings, check configuration.");
-
- // Declare SHIRE object for this request.
- SHIRE shire(application);
-
- const char* shireURL=shire.getShireURL(targeturl.c_str());
- if (!shireURL)
- return WriteClientError(pfc,"Unable to map request to proper shireURL setting, check configuration.");
-
- // If the user is accessing the SHIRE acceptance point, pass it on.
- if (targeturl.find(shireURL)!=string::npos)
- return SF_STATUS_REQ_NEXT_NOTIFICATION;
-
- // Now check the policy for this request.
- pair<bool,bool> requireSession=settings.first->getBool("requireSession");
- pair<const char*,const char*> shib_cookie=shire.getCookieNameProps();
- pair<bool,bool> httpRedirects=application->getPropertySet("Sessions")->getBool("httpRedirects");
- pair<bool,const char*> redirectPage=application->getPropertySet("Sessions")->getString("redirectPage");
- if (httpRedirects.first && !httpRedirects.second && !redirectPage.first)
- return WriteClientError(pfc,"HTML-based redirection requires a redirectPage property.");
-
- // Check for session cookie.
- const char* session_id=NULL;
- GetHeader(pn,pfc,"Cookie:",buf,128,false);
- Category::getInstance("isapi_shib.HttpFilterProc").debug("cookie header is {%s}",(const char*)buf);
- if (!buf.empty() && (session_id=strstr(buf,shib_cookie.first))) {
- session_id+=strlen(shib_cookie.first) + 1; /* Skip over the '=' */
- char* cookieend=strchr(session_id,';');
- if (cookieend)
- *cookieend = '\0'; /* Ignore anyting after a ; */
- }
-
- if (!session_id || !*session_id) {
- // If no session required, bail now.
- if (!requireSession.second)
- return SF_STATUS_REQ_NEXT_NOTIFICATION;
-
- // No acceptable cookie, and we require a session. Generate an AuthnRequest.
- const char* areq = shire.getAuthnRequest(targeturl.c_str());
- if (!httpRedirects.first || httpRedirects.second) {
- string hdrs=string("Location: ") + areq + "\r\n"
- "Content-Type: text/html\r\n"
- "Content-Length: 40\r\n"
- "Expires: 01-Jan-1997 12:00:00 GMT\r\n"
- "Cache-Control: private,no-store,no-cache\r\n\r\n";
- pfc->ServerSupportFunction(pfc,SF_REQ_SEND_RESPONSE_HEADER,"302 Please Wait",(DWORD)hdrs.c_str(),0);
- static const char* redmsg="<HTML><BODY>Redirecting...</BODY></HTML>";
- DWORD resplen=40;
- pfc->WriteClient(pfc,(LPVOID)redmsg,&resplen,0);
- return SF_STATUS_REQ_FINISHED;
- }
- else {
- ShibMLP markupProcessor;
- markupProcessor.insert("requestURL",areq);
- return WriteRedirectPage(pfc, application, redirectPage.second, markupProcessor);
- }
- }
-
- // Make sure this session is still valid.
- RPCError* status = NULL;
- ShibMLP markupProcessor;
- markupProcessor.insert("requestURL", targeturl);
-
- dynabuf abuf(16);
- GetServerVariable(pfc,"REMOTE_ADDR",abuf,16);
- try {
- status = shire.sessionIsValid(session_id, abuf);
- }
- catch (ShibTargetException &e) {
- markupProcessor.insert("errorType", "Session Processing Error");
- markupProcessor.insert("errorText", e.what());
- markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
- return WriteClientError(pfc, application, "shire", markupProcessor);
- }
-#ifndef _DEBUG
- catch (...) {
- markupProcessor.insert("errorType", "Session Processing Error");
- markupProcessor.insert("errorText", "Unexpected Exception");
- markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
- return WriteClientError(pfc, application, "shire", markupProcessor);
- }
-#endif
-
- // Check the status
- if (status->isError()) {
- if (!requireSession.second)
- return SF_STATUS_REQ_NEXT_NOTIFICATION;
- else if (status->isRetryable()) {
- // Oops, session is invalid. Generate AuthnRequest.
- delete status;
- const char* areq = shire.getAuthnRequest(targeturl.c_str());
- if (!httpRedirects.first || httpRedirects.second) {
- string hdrs=string("Location: ") + areq + "\r\n"
- "Content-Type: text/html\r\n"
- "Content-Length: 40\r\n"
- "Expires: 01-Jan-1997 12:00:00 GMT\r\n"
- "Cache-Control: private,no-store,no-cache\r\n\r\n";
- pfc->ServerSupportFunction(pfc,SF_REQ_SEND_RESPONSE_HEADER,"302 Please Wait",(DWORD)hdrs.c_str(),0);
- static const char* redmsg="<HTML><BODY>Redirecting...</BODY></HTML>";
- DWORD resplen=40;
- pfc->WriteClient(pfc,(LPVOID)redmsg,&resplen,0);
- return SF_STATUS_REQ_FINISHED;
- }
- else {
- markupProcessor.insert("requestURL",areq);
- return WriteRedirectPage(pfc, application, redirectPage.second, markupProcessor);
- }
- }
- else {
- // return the error page to the user
- markupProcessor.insert(*status);
- delete status;
- return WriteClientError(pfc, application, "shire", markupProcessor);
- }
- }
- delete status;
-
- // Move to RM phase.
- RM rm(application);
- vector<SAMLAssertion*> assertions;
- SAMLAuthenticationStatement* sso_statement=NULL;
-
- try {
- status = rm.getAssertions(session_id, abuf, assertions, &sso_statement);
- }
- catch (ShibTargetException &e) {
- markupProcessor.insert("errorType", "Attribute Processing Error");
- markupProcessor.insert("errorText", e.what());
- markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
- return WriteClientError(pfc, application, "rm", markupProcessor);
- }
- #ifndef _DEBUG
- catch (...) {
- markupProcessor.insert("errorType", "Attribute Processing Error");
- markupProcessor.insert("errorText", "Unexpected Exception");
- markupProcessor.insert("errorDesc", "An error occurred while processing your request.");
- return WriteClientError(pfc, application, "rm", markupProcessor);
- }
- #endif
-
- if (status->isError()) {
- markupProcessor.insert(*status);
- delete status;
- return WriteClientError(pfc, application, "rm", markupProcessor);
- }
- delete status;
-
- // Do we have an access control plugin?
- if (settings.second) {
- Locker acllock(settings.second);
- if (!settings.second->authorized(*sso_statement,assertions)) {
- for (int k = 0; k < assertions.size(); k++)
- delete assertions[k];
- delete sso_statement;
- return WriteClientError(pfc, application, "access", markupProcessor);
- }
- }