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>
33 using namespace shibboleth;
34 using namespace shibtarget;
36 void ShibMLP::html_encode(string& os, const char* start)
38 while (start && *start) {
40 case '<': os += "<"; break;
41 case '>': os += ">"; break;
42 case '"': os += """; break;
43 case '#': os += "#"; break;
44 case '%': os += "%"; break;
45 case '&': os += "&"; break;
46 case '\'': os += "'"; break;
47 case '(': os += "("; break;
48 case ')': os += ")"; break;
49 case ':': os += ":"; break;
50 case '[': os += "["; break;
51 case '\\': os += "\"; break;
52 case ']': os += "]"; break;
53 case '`': os += "`"; break;
54 case '{': os += "{"; break;
55 case '}': os += "}"; break;
56 default: os += *start;
62 static void trimspace (string& s)
64 int end = s.size() - 1, start = 0;
66 // Trim stuff on right.
67 while (end > 0 && !isgraph(s[end])) end--;
69 // Trim stuff on left.
70 while (start < end && !isgraph(s[start])) start++;
73 s = s.substr(start, end - start + 1);
76 const char* ShibMLP::run(const string& is, const IPropertySet* props, std::string* output)
79 time_t now = time(NULL);
82 insert("now", ctime_r(&now,timebuf,sizeof(timebuf)));
84 insert("now", ctime(&now));
89 const char* line = is.c_str();
90 const char* lastpos = line;
94 // Search for SHIBMLP tags. These are of the form:
96 // <shibmlpif key> stuff </shibmlpif>
97 // <shibmlpifnot key> stuff </shibmlpifnot>
98 // Note that there MUST be white-space after "<shibmlp" but
99 // there does not need to be white space between the key and
102 while ((thispos = strchr(lastpos, '<')) != NULL) {
103 // save the string up to this token
104 *output += is.substr(lastpos-line, thispos-lastpos);
106 // Make sure this token matches our tokens.
107 #ifdef HAVE_STRCASECMP
108 if (!strncasecmp(thispos, "<shibmlp ", 9))
110 if (!strnicmp(thispos, "<shibmlp ", 9))
113 // Save this position off.
114 lastpos = thispos + 9; // strlen("<shibmlp ")
116 // search for the end-tag
117 if ((thispos = strstr(lastpos, "/>")) != NULL) {
118 string key = is.substr(lastpos-line, thispos-lastpos);
121 map<string,string>::const_iterator i=m_map.find(key);
122 if (i != m_map.end()) {
123 html_encode(*output,i->second.c_str());
126 pair<bool,const char*> p=props ? props->getString(key.c_str()) : pair<bool,const char*>(false,NULL);
128 html_encode(*output,p.second);
131 static const char* s1 = "<!-- Unknown SHIBMLP key: ";
132 static const char* s2 = "/>";
137 lastpos = thispos + 2; // strlen("/>")
140 #ifdef HAVE_STRCASECMP
141 else if (!strncasecmp(thispos, "<shibmlpif ", 11))
143 else if (!strnicmp(thispos, "<shibmlpif ", 11))
146 // Save this position off.
147 lastpos = thispos + 11; // strlen("<shibmlpif ")
149 // search for the end of this tag
150 if ((thispos = strchr(lastpos, '>')) != NULL) {
151 string key = is.substr(lastpos-line, thispos-lastpos);
154 map<string,string>::const_iterator i=m_map.find(key);
155 if (i != m_map.end() && !i->second.empty()) {
159 pair<bool,const char*> p=props ? props->getString(key.c_str()) : pair<bool,const char*>(false,NULL);
164 lastpos = thispos + 1; // strlen(">")
166 // Search for the closing tag.
167 const char* frontpos=lastpos;
168 while ((thispos = strstr(lastpos, "</")) != NULL) {
169 #ifdef HAVE_STRCASECMP
170 if (!strncasecmp(thispos, "</shibmlpif>", 12))
172 if (!strnicmp(thispos, "</shibmlpif>", 12))
175 // We found our terminator. Process the string in between.
177 run(is.substr(frontpos-line, thispos-frontpos),props,&segment);
180 lastpos = thispos + 12; // strlen("</shibmlpif>")
185 lastpos = thispos + 2;
190 #ifdef HAVE_STRCASECMP
191 else if (!strncasecmp(thispos, "<shibmlpifnot ", 14))
193 else if (!strnicmp(thispos, "<shibmlpifnot ", 14))
196 // Save this position off.
197 lastpos = thispos + 14; // strlen("<shibmlpifnot ")
199 // search for the end of this tag
200 if ((thispos = strchr(lastpos, '>')) != NULL) {
201 string key = is.substr(lastpos-line, thispos-lastpos);
204 map<string,string>::const_iterator i=m_map.find(key);
205 if (i != m_map.end() && !i->second.empty()) {
209 pair<bool,const char*> p=props ? props->getString(key.c_str()) : pair<bool,const char*>(false,NULL);
214 lastpos = thispos + 1; // strlen(">")
216 // Search for the closing tag.
217 const char* frontpos=lastpos;
218 while ((thispos = strstr(lastpos, "</")) != NULL) {
219 #ifdef HAVE_STRCASECMP
220 if (!strncasecmp(thispos, "</shibmlpifnot>", 15))
222 if (!strnicmp(thispos, "</shibmlpifnot>", 15))
225 // We found our terminator. Process the string in between.
227 run(is.substr(frontpos-line, thispos-frontpos),props,&segment);
230 lastpos = thispos + 15; // strlen("</shibmlpifnot>")
235 lastpos = thispos + 2;
243 lastpos = thispos + 1;
246 *output += is.substr(lastpos-line);
248 return output->c_str();
251 const char* ShibMLP::run(istream& is, const IPropertySet* props, std::string* output)
253 static string eol = "\r\n";
256 while (getline(is, line))
259 return run(str,props,output);
262 void ShibMLP::insert(SAMLException& e)
264 insert("errorType", e.classname());
265 if (typeid(e)==typeid(ContentTypeException))
266 insert("errorText", "A problem was detected with your identity provider's software configuration.");
268 insert("errorText", e.getMessage() ? e.getMessage() : "No Message");
269 if (e.getProperty("errorURL"))
270 insert("originErrorURL", e.getProperty("errorURL"));
271 if (e.getProperty("contactName"))
272 insert("originContactName", e.getProperty("contactName"));
273 const char* email=e.getProperty("contactEmail");
275 if (!strncmp(email,"mailto:",7) && strlen(email)>7)
276 insert("originContactEmail", email+7);
278 insert("originContactEmail", email);