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 size_t 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);
104 insert("now", ctime(&now));
108 const char* line = is.c_str();
109 const char* lastpos = line;
112 m_priv->log->debug("Processing string");
115 // Search for SHIBMLP tags. These are of the form:
117 // <shibmlpif key> stuff </shibmlpif>
118 // <shibmlpifnot key> stuff </shibmlpifnot>
119 // Note that there MUST be white-space after "<shibmlp" but
120 // there does not need to be white space between the key and
123 while ((thispos = strchr(lastpos, '<')) != NULL) {
124 // save the string up to this token
125 *output += is.substr(lastpos-line, thispos-lastpos);
127 // Make sure this token matches our tokens.
128 #ifdef HAVE_STRCASECMP
129 if (!strncasecmp(thispos, "<shibmlp ", 9))
131 if (!_strnicmp(thispos, "<shibmlp ", 9))
134 // Save this position off.
135 lastpos = thispos + 9; // strlen("<shibmlp ")
137 // search for the end-tag
138 if ((thispos = strstr(lastpos, "/>")) != NULL) {
139 string key = is.substr(lastpos-line, thispos-lastpos);
142 map<string,string>::const_iterator i=m_map.find(key);
143 if (i != m_map.end()) {
144 m_priv->html_encode(*output,i->second.c_str());
147 pair<bool,const char*> p=props ? props->getString(key.c_str()) : pair<bool,const char*>(false,NULL);
149 m_priv->html_encode(*output,p.second);
152 static const char* s1 = "<!-- Unknown SHIBMLP key: ";
153 static const char* s2 = "/>";
158 lastpos = thispos + 2; // strlen("/>")
161 #ifdef HAVE_STRCASECMP
162 else if (!strncasecmp(thispos, "<shibmlpif ", 11))
164 else if (!_strnicmp(thispos, "<shibmlpif ", 11))
167 // Save this position off.
168 lastpos = thispos + 11; // strlen("<shibmlpif ")
170 // search for the end of this tag
171 if ((thispos = strchr(lastpos, '>')) != NULL) {
172 string key = is.substr(lastpos-line, thispos-lastpos);
175 map<string,string>::const_iterator i=m_map.find(key);
176 if (i != m_map.end() && !i->second.empty()) {
180 pair<bool,const char*> p=props ? props->getString(key.c_str()) : pair<bool,const char*>(false,NULL);
185 lastpos = thispos + 1; // strlen(">")
187 // Search for the closing tag.
188 const char* frontpos=lastpos;
189 while ((thispos = strstr(lastpos, "</")) != NULL) {
190 #ifdef HAVE_STRCASECMP
191 if (!strncasecmp(thispos, "</shibmlpif>", 12))
193 if (!_strnicmp(thispos, "</shibmlpif>", 12))
196 // We found our terminator. Process the string in between.
198 run(is.substr(frontpos-line, thispos-frontpos),props,&segment);
201 lastpos = thispos + 12; // strlen("</shibmlpif>")
206 lastpos = thispos + 2;
211 #ifdef HAVE_STRCASECMP
212 else if (!strncasecmp(thispos, "<shibmlpifnot ", 14))
214 else if (!_strnicmp(thispos, "<shibmlpifnot ", 14))
217 // Save this position off.
218 lastpos = thispos + 14; // strlen("<shibmlpifnot ")
220 // search for the end of this tag
221 if ((thispos = strchr(lastpos, '>')) != NULL) {
222 string key = is.substr(lastpos-line, thispos-lastpos);
225 map<string,string>::const_iterator i=m_map.find(key);
226 if (i != m_map.end() && !i->second.empty()) {
230 pair<bool,const char*> p=props ? props->getString(key.c_str()) : pair<bool,const char*>(false,NULL);
235 lastpos = thispos + 1; // strlen(">")
237 // Search for the closing tag.
238 const char* frontpos=lastpos;
239 while ((thispos = strstr(lastpos, "</")) != NULL) {
240 #ifdef HAVE_STRCASECMP
241 if (!strncasecmp(thispos, "</shibmlpifnot>", 15))
243 if (!_strnicmp(thispos, "</shibmlpifnot>", 15))
246 // We found our terminator. Process the string in between.
248 run(is.substr(frontpos-line, thispos-frontpos),props,&segment);
251 lastpos = thispos + 15; // strlen("</shibmlpifnot>")
256 lastpos = thispos + 2;
264 lastpos = thispos + 1;
267 *output += is.substr(lastpos-line);
269 return output->c_str();
272 const char* ShibMLP::run(istream& is, const IPropertySet* props, std::string* output)
274 static string eol = "\r\n";
277 m_priv->log->debug("processing stream");
279 while (getline(is, line))
282 return run(str,props,output);
285 void ShibMLP::insert(SAMLException& e)
287 insert("errorType", e.classname());
288 if (typeid(e)==typeid(ContentTypeException))
289 insert("errorText", "A problem was detected with your identity provider's software configuration.");
291 insert("errorText", e.getMessage() ? e.getMessage() : "No Message");
292 if (e.getProperty("errorURL"))
293 insert("originErrorURL", e.getProperty("errorURL"));
294 if (e.getProperty("contactName"))
295 insert("originContactName", e.getProperty("contactName"));
296 const char* email=e.getProperty("contactEmail");
298 if (!strncmp(email,"mailto:",7) && strlen(email)>7)
299 insert("originContactEmail", email+7);
301 insert("originContactEmail", email);
305 void ShibMLP::insert (const std::string& key, const std::string& value)
307 m_priv->log->debug("inserting %s -> %s", key.c_str(), value.c_str());