+static void die(std::string s) { throw mp::filter::FilterException(s); }
+
+
+// Read XML config.. Put config info in m_p.
+void mp::filter::AuthSimple::configure(const xmlNode * ptr, bool test_only)
+{
+ std::string userRegisterName;
+ std::string targetRegisterName;
+
+ for (ptr = ptr->children; ptr != 0; ptr = ptr->next) {
+ if (ptr->type != XML_ELEMENT_NODE)
+ continue;
+ if (!strcmp((const char *) ptr->name, "userRegister")) {
+ userRegisterName = mp::xml::get_text(ptr);
+ m_p->got_userRegister = true;
+ } else if (!strcmp((const char *) ptr->name, "targetRegister")) {
+ targetRegisterName = mp::xml::get_text(ptr);
+ m_p->got_targetRegister = true;
+ } else if (!strcmp((const char *) ptr->name,
+ "discardUnauthorisedTargets")) {
+ m_p->discardUnauthorisedTargets = true;
+ } else {
+ die("Bad element in auth_simple: <"
+ + std::string((const char *) ptr->name) + ">");
+ }
+ }
+
+ if (!m_p->got_userRegister && !m_p->got_targetRegister)
+ die("auth_simple: no user-register or target-register "
+ "filename specified");
+
+ if (m_p->got_userRegister)
+ config_userRegister(userRegisterName);
+ if (m_p->got_targetRegister)
+ config_targetRegister(targetRegisterName);
+}
+
+
+void mp::filter::AuthSimple::config_userRegister(std::string filename)
+{
+ FILE *fp = fopen(filename.c_str(), "r");
+ if (fp == 0)
+ die("can't open auth_simple user-register '" + filename + "': " +
+ strerror(errno));
+
+ char buf[1000];
+ while (fgets(buf, sizeof buf, fp))
+ {
+ if (*buf == '\n' || *buf == '#')
+ continue;
+ buf[strlen(buf)-1] = 0;
+ char *passwdp = strchr(buf, ':');
+ if (passwdp == 0)
+ die("auth_simple user-register '" + filename + "': " +
+ "no password on line: '" + buf + "'");
+ *passwdp++ = 0;
+ char *databasesp = strchr(passwdp, ':');
+ if (databasesp == 0)
+ die("auth_simple user-register '" + filename + "': " +
+ "no databases on line: '" + buf + ":" + passwdp + "'");
+ *databasesp++ = 0;
+ yf::AuthSimple::Rep::PasswordAndDBs tmp(passwdp);
+ boost::split(tmp.dbs, databasesp, boost::is_any_of(","));
+ m_p->userRegister[buf] = tmp;
+
+ if (0)
+ { // debugging
+ printf("Added user '%s' -> password '%s'\n", buf, passwdp);
+ std::list<std::string>::const_iterator i;
+ for (i = tmp.dbs.begin(); i != tmp.dbs.end(); i++)
+ printf("db '%s'\n", (*i).c_str());
+ }
+ }
+ fclose(fp);
+}
+
+
+// I feel a little bad about the duplication of code between this and
+// config_userRegister(). But not bad enough to refactor.
+//
+void mp::filter::AuthSimple::config_targetRegister(std::string filename)
+{
+ FILE *fp = fopen(filename.c_str(), "r");
+ if (fp == 0)
+ die("can't open auth_simple target-register '" + filename + "': " +
+ strerror(errno));
+
+ char buf[1000];
+ while (fgets(buf, sizeof buf, fp)) {
+ if (*buf == '\n' || *buf == '#')
+ continue;
+ buf[strlen(buf)-1] = 0;
+ char *targetsp = strchr(buf, ':');
+ if (targetsp == 0)
+ die("auth_simple target-register '" + filename + "': " +
+ "no targets on line: '" + buf + "'");
+ *targetsp++ = 0;
+ std::list<std::string> tmp;
+ boost::split(tmp, targetsp, boost::is_any_of(","));
+ m_p->targetsByUser[buf] = tmp;
+
+ if (0) { // debugging
+ printf("Added user '%s' with targets:\n", buf);
+ std::list<std::string>::const_iterator i;
+ for (i = tmp.begin(); i != tmp.end(); i++) {
+ printf("\t%s\n", (*i).c_str());
+ }
+ }
+ }