2 * Copyright 2001-2005 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * shib-mlp.cpp -- The ShibTarget Markup Language processor
20 * Created by: Derek Atkins <derek@ihtfp.com>
29 #include <xercesc/util/XercesDefs.hpp>
30 #include <log4cpp/Category.hh>
33 using namespace log4cpp;
35 using namespace shibboleth;
36 using namespace shibtarget;
38 class shibtarget::ShibMLPPriv {
42 log4cpp::Category *log;
44 static void html_encode(string& os, const char* start);
48 void ShibMLPPriv::html_encode(string& os, const char* start)
50 while (start && *start) {
52 case '<': os += "<"; break;
53 case '>': os += ">"; break;
54 case '"': os += """; break;
55 case '#': os += "#"; break;
56 case '%': os += "%"; break;
57 case '&': os += "&"; break;
58 case '\'': os += "'"; break;
59 case '(': os += "("; break;
60 case ')': os += ")"; break;
61 case ':': os += ":"; break;
62 case '[': os += "["; break;
63 case '\\': os += "\"; break;
64 case ']': os += "]"; break;
65 case '`': os += "`"; break;
66 case '{': os += "{"; break;
67 case '}': os += "}"; break;
68 default: os += *start;
74 ShibMLPPriv::ShibMLPPriv() : log(&(log4cpp::Category::getInstance("shibtarget.ShibMLP"))) {}
76 static void trimspace (string& s)
78 int end = s.size() - 1, start = 0;
80 // Trim stuff on right.
81 while (end > 0 && !isgraph(s[end])) end--;
83 // Trim stuff on left.
84 while (start < end && !isgraph(s[start])) start++;
87 s = s.substr(start, end - start + 1);
92 m_priv = new ShibMLPPriv ();
100 const char* ShibMLP::run(const string& is, const IPropertySet* props, std::string* output)
102 // Create a timestamp
103 time_t now = time(NULL);
106 insert("now", ctime_r(&now,timebuf));
108 insert("now", ctime(&now));
113 const char* line = is.c_str();
114 const char* lastpos = line;
117 m_priv->log->debug("Processing string");
120 // Search for SHIBMLP tags. These are of the form:
122 // <shibmlpif key> stuff </shibmlpif>
123 // <shibmlpifnot key> stuff </shibmlpifnot>
124 // Note that there MUST be white-space after "<shibmlp" but
125 // there does not need to be white space between the key and
128 while ((thispos = strchr(lastpos, '<')) != NULL) {
129 // save the string up to this token
130 *output += is.substr(lastpos-line, thispos-lastpos);
132 // Make sure this token matches our tokens.
133 #ifdef HAVE_STRCASECMP
134 if (!strncasecmp(thispos, "<shibmlp ", 9))
136 if (!strnicmp(thispos, "<shibmlp ", 9))
139 // Save this position off.
140 lastpos = thispos + 9; // strlen("<shibmlp ")
142 // search for the end-tag
143 if ((thispos = strstr(lastpos, "/>")) != NULL) {
144 string key = is.substr(lastpos-line, thispos-lastpos);
147 map<string,string>::const_iterator i=m_map.find(key);
148 if (i != m_map.end()) {
149 m_priv->html_encode(*output,i->second.c_str());
152 pair<bool,const char*> p=props ? props->getString(key.c_str()) : pair<bool,const char*>(false,NULL);
154 m_priv->html_encode(*output,p.second);
157 static const char* s1 = "<!-- Unknown SHIBMLP key: ";
158 static const char* s2 = "/>";
163 lastpos = thispos + 2; // strlen("/>")
166 #ifdef HAVE_STRCASECMP
167 else if (!strncasecmp(thispos, "<shibmlpif ", 11))
169 else if (!strnicmp(thispos, "<shibmlpif ", 11))
172 // Save this position off.
173 lastpos = thispos + 11; // strlen("<shibmlpif ")
175 // search for the end of this tag
176 if ((thispos = strchr(lastpos, '>')) != NULL) {
177 string key = is.substr(lastpos-line, thispos-lastpos);
180 map<string,string>::const_iterator i=m_map.find(key);
181 if (i != m_map.end() && !i->second.empty()) {
185 pair<bool,const char*> p=props ? props->getString(key.c_str()) : pair<bool,const char*>(false,NULL);
190 lastpos = thispos + 1; // strlen(">")
192 // Search for the closing tag.
193 const char* frontpos=lastpos;
194 while ((thispos = strstr(lastpos, "</")) != NULL) {
195 #ifdef HAVE_STRCASECMP
196 if (!strncasecmp(thispos, "</shibmlpif>", 12))
198 if (!strnicmp(thispos, "</shibmlpif>", 12))
201 // We found our terminator. Process the string in between.
203 run(is.substr(frontpos-line, thispos-frontpos),props,&segment);
206 lastpos = thispos + 12; // strlen("</shibmlpif>")
211 lastpos = thispos + 2;
216 #ifdef HAVE_STRCASECMP
217 else if (!strncasecmp(thispos, "<shibmlpifnot ", 14))
219 else if (!strnicmp(thispos, "<shibmlpifnot ", 14))
222 // Save this position off.
223 lastpos = thispos + 14; // strlen("<shibmlpifnot ")
225 // search for the end of this tag
226 if ((thispos = strchr(lastpos, '>')) != NULL) {
227 string key = is.substr(lastpos-line, thispos-lastpos);
230 map<string,string>::const_iterator i=m_map.find(key);
231 if (i != m_map.end() && !i->second.empty()) {
235 pair<bool,const char*> p=props ? props->getString(key.c_str()) : pair<bool,const char*>(false,NULL);
240 lastpos = thispos + 1; // strlen(">")
242 // Search for the closing tag.
243 const char* frontpos=lastpos;
244 while ((thispos = strstr(lastpos, "</")) != NULL) {
245 #ifdef HAVE_STRCASECMP
246 if (!strncasecmp(thispos, "</shibmlpifnot>", 15))
248 if (!strnicmp(thispos, "</shibmlpifnot>", 15))
251 // We found our terminator. Process the string in between.
253 run(is.substr(frontpos-line, thispos-frontpos),props,&segment);
256 lastpos = thispos + 15; // strlen("</shibmlpifnot>")
261 lastpos = thispos + 2;
269 lastpos = thispos + 1;
272 *output += is.substr(lastpos-line);
274 return output->c_str();
277 const char* ShibMLP::run(istream& is, const IPropertySet* props, std::string* output)
279 static string eol = "\r\n";
282 m_priv->log->debug("processing stream");
284 while (getline(is, line))
287 return run(str,props,output);
290 void ShibMLP::insert(SAMLException& e)
292 insert("errorType", e.classname());
293 if (typeid(e)==typeid(ContentTypeException))
294 insert("errorText", "A problem was detected with your identity provider's software configuration.");
296 insert("errorText", e.getMessage() ? e.getMessage() : "No Message");
297 if (e.getProperty("errorURL"))
298 insert("originErrorURL", e.getProperty("errorURL"));
299 if (e.getProperty("contactName"))
300 insert("originContactName", e.getProperty("contactName"));
301 const char* email=e.getProperty("contactEmail");
303 if (!strncmp(email,"mailto:",7) && strlen(email)>7)
304 insert("originContactEmail", email+7);
306 insert("originContactEmail", email);
310 void ShibMLP::insert (const std::string& key, const std::string& value)
312 m_priv->log->debug("inserting %s -> %s", key.c_str(), value.c_str());