Reorg projects a bit, created new ODBC extension project.
[shibboleth/cpp-sp.git] / shib-target / shib-mlp.cpp
1 /*
2  *  Copyright 2001-2005 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
18  * shib-mlp.cpp -- The ShibTarget Markup Language processor
19  *
20  * Created by:  Derek Atkins <derek@ihtfp.com>
21  *
22  * $Id$
23  */
24
25 #include "internal.h"
26
27 #include <sstream>
28 #include <ctype.h>
29 #include <xercesc/util/XercesDefs.hpp>
30 #include <log4cpp/Category.hh>
31
32 using namespace std;
33 using namespace log4cpp;
34 using namespace saml;
35 using namespace shibboleth;
36 using namespace shibtarget;
37
38 class shibtarget::ShibMLPPriv {
39 public:
40   ShibMLPPriv();
41   ~ShibMLPPriv() {}
42   log4cpp::Category *log;
43
44   static void html_encode(string& os, const char* start);
45 };  
46
47
48 void ShibMLPPriv::html_encode(string& os, const char* start)
49 {
50     while (start && *start) {
51         switch (*start) {
52             case '<':   os += "&lt;";       break;
53             case '>':   os += "&gt;";       break;
54             case '"':   os += "&quot;";     break;
55             case '#':   os += "&#35;";      break;
56             case '%':   os += "&#37;";      break;
57             case '&':   os += "&#38;";      break;
58             case '\'':  os += "&#39;";      break;
59             case '(':   os += "&#40;";      break;
60             case ')':   os += "&#41;";      break;
61             case ':':   os += "&#58;";      break;
62             case '[':   os += "&#91;";      break;
63             case '\\':  os += "&#92;";      break;
64             case ']':   os += "&#93;";      break;
65             case '`':   os += "&#96;";      break;
66             case '{':   os += "&#123;";     break;
67             case '}':   os += "&#125;";     break;
68             default:    os += *start;
69         }
70         start++;
71     }
72 }
73
74 ShibMLPPriv::ShibMLPPriv() : log(&(log4cpp::Category::getInstance("shibtarget.ShibMLP"))) {}
75
76 static void trimspace (string& s)
77 {
78   size_t end = s.size() - 1, start = 0;
79
80   // Trim stuff on right.
81   while (end > 0 && !isgraph(s[end])) end--;
82
83   // Trim stuff on left.
84   while (start < end && !isgraph(s[start])) start++;
85
86   // Modify the string.
87   s = s.substr(start, end - start + 1);
88 }
89
90 ShibMLP::ShibMLP()
91 {
92   m_priv = new ShibMLPPriv ();
93 }
94
95 ShibMLP::~ShibMLP ()
96 {
97   delete m_priv;
98 }
99
100 const char* ShibMLP::run(const string& is, const IPropertySet* props, std::string* output)
101 {
102   // Create a timestamp
103   time_t now = time(NULL);
104   insert("now", ctime(&now));
105
106   if (!output)
107     output=&m_generated;
108   const char* line = is.c_str();
109   const char* lastpos = line;
110   const char* thispos;
111
112   m_priv->log->debug("Processing string");
113
114   //
115   // Search for SHIBMLP tags.  These are of the form:
116   //    <shibmlp key/>
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
121   // the close-tag.
122   //
123   while ((thispos = strchr(lastpos, '<')) != NULL) {
124     // save the string up to this token
125     *output += is.substr(lastpos-line, thispos-lastpos);
126
127     // Make sure this token matches our tokens.
128 #ifdef HAVE_STRCASECMP
129     if (!strncasecmp(thispos, "<shibmlp ", 9))
130 #else
131     if (!_strnicmp(thispos, "<shibmlp ", 9))
132 #endif
133     {
134         // Save this position off.
135         lastpos = thispos + 9;  // strlen("<shibmlp ")
136     
137         // search for the end-tag
138         if ((thispos = strstr(lastpos, "/>")) != NULL) {
139             string key = is.substr(lastpos-line, thispos-lastpos);
140             trimspace(key);
141     
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());
145             }
146             else {
147                 pair<bool,const char*> p=props ? props->getString(key.c_str()) : pair<bool,const char*>(false,NULL);
148                 if (p.first) {
149                     m_priv->html_encode(*output,p.second);
150                 }
151                 else {
152                     static const char* s1 = "<!-- Unknown SHIBMLP key: ";
153                     static const char* s2 = "/>";
154                     *output += s1;
155                     *output += key + s2;
156                 }
157             }
158             lastpos = thispos + 2; // strlen("/>")
159         }
160     }
161 #ifdef HAVE_STRCASECMP
162     else if (!strncasecmp(thispos, "<shibmlpif ", 11))
163 #else
164     else if (!_strnicmp(thispos, "<shibmlpif ", 11))
165 #endif
166     {
167         // Save this position off.
168         lastpos = thispos + 11;  // strlen("<shibmlpif ")
169
170         // search for the end of this tag
171         if ((thispos = strchr(lastpos, '>')) != NULL) {
172             string key = is.substr(lastpos-line, thispos-lastpos);
173             trimspace(key);
174             bool eval=false;
175             map<string,string>::const_iterator i=m_map.find(key);
176             if (i != m_map.end() && !i->second.empty()) {
177                 eval=true;
178             }
179             else {
180                 pair<bool,const char*> p=props ? props->getString(key.c_str()) : pair<bool,const char*>(false,NULL);
181                 if (p.first) {
182                     eval=true;
183                 }
184             }
185             lastpos = thispos + 1; // strlen(">")
186             
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))
192 #else
193                 if (!_strnicmp(thispos, "</shibmlpif>", 12))
194 #endif
195                 {
196                     // We found our terminator. Process the string in between.
197                     string segment;
198                     run(is.substr(frontpos-line, thispos-frontpos),props,&segment);
199                     if (eval)
200                         *output += segment;
201                     lastpos = thispos + 12; // strlen("</shibmlpif>")
202                     break;
203                 }
204                 else {
205                     // Skip it.
206                     lastpos = thispos + 2;
207                 }
208             }
209         }
210     }
211 #ifdef HAVE_STRCASECMP
212     else if (!strncasecmp(thispos, "<shibmlpifnot ", 14))
213 #else
214     else if (!_strnicmp(thispos, "<shibmlpifnot ", 14))
215 #endif
216     {
217         // Save this position off.
218         lastpos = thispos + 14;  // strlen("<shibmlpifnot ")
219
220         // search for the end of this tag
221         if ((thispos = strchr(lastpos, '>')) != NULL) {
222             string key = is.substr(lastpos-line, thispos-lastpos);
223             trimspace(key);
224             bool eval=false;
225             map<string,string>::const_iterator i=m_map.find(key);
226             if (i != m_map.end() && !i->second.empty()) {
227                 eval=true;
228             }
229             else {
230                 pair<bool,const char*> p=props ? props->getString(key.c_str()) : pair<bool,const char*>(false,NULL);
231                 if (p.first) {
232                     eval=true;
233                 }
234             }
235             lastpos = thispos + 1; // strlen(">")
236             
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))
242 #else
243                 if (!_strnicmp(thispos, "</shibmlpifnot>", 15))
244 #endif
245                 {
246                     // We found our terminator. Process the string in between.
247                     string segment;
248                     run(is.substr(frontpos-line, thispos-frontpos),props,&segment);
249                     if (!eval)
250                         *output += segment;
251                     lastpos = thispos + 15; // strlen("</shibmlpifnot>")
252                     break;
253                 }
254                 else {
255                     // Skip it.
256                     lastpos = thispos + 2;
257                 }
258             }
259         }
260     }
261     else {
262       // Skip it.
263       *output += "<";
264       lastpos = thispos + 1;
265     }
266   }
267   *output += is.substr(lastpos-line);
268
269   return output->c_str();
270 }
271
272 const char* ShibMLP::run(istream& is, const IPropertySet* props, std::string* output)
273 {
274   static string eol = "\r\n";
275   string str, line;
276
277   m_priv->log->debug("processing stream");
278
279   while (getline(is, line))
280     str += line + eol;
281
282   return run(str,props,output);
283 }
284
285 void ShibMLP::insert(SAMLException& e)
286 {
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.");
290     else
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");
297     if (email) {
298         if (!strncmp(email,"mailto:",7) && strlen(email)>7)
299             insert("originContactEmail", email+7);
300         else
301             insert("originContactEmail", email);
302     }
303 }
304
305 void ShibMLP::insert (const std::string& key, const std::string& value)
306 {
307   m_priv->log->debug("inserting %s -> %s", key.c_str(), value.c_str());
308   m_map[key] = value;
309 }