1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*
** Copyright 2016 Double Precision, Inc. See COPYING for
** distribution information.
*/
#if HAVE_CONFIG_H
#include "courier_auth_config.h"
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
extern "C" {
#include "authldap.h"
#include "auth.h"
#include "authldaprc.h"
#include "courierauthdebug.h"
};
#include <algorithm>
#include <fstream>
#include <sstream>
#include "authconfigfile.h"
courier::auth::config_file::config_file(const char *filenameArg)
: filename(filenameArg), loaded(false)
{
}
bool courier::auth::config_file::load(bool reload)
{
struct stat stat_buf;
if (stat(filename, &stat_buf) < 0)
{
courier_auth_err("stat(%s) failed", filename);
return false;
}
if (loaded)
{
if (stat_buf.st_mtime != config_timestamp)
do_reload();
return true;
}
loaded=open_and_load_file(reload);
if (loaded)
config_timestamp=stat_buf.st_mtime;
return loaded;
}
class courier::auth::config_file::isspace {
public:
bool operator()(char c)
{
return std::isspace(c);
}
};
class courier::auth::config_file::not_isspace : public isspace {
public:
bool operator()(char c)
{
return !isspace::operator()(c);
}
};
bool courier::auth::config_file::open_and_load_file(bool reload)
{
std::ifstream f(filename);
if (!f.is_open())
{
courier_auth_err("Cannot open %s", filename);
return false;
}
std::string s;
bool seen_marker=false;
while (s.clear(), !std::getline(f, s).eof() || !s.empty())
{
std::string::iterator e=s.end();
std::string::iterator p=
std::find_if(s.begin(), e, not_isspace());
if (p == s.end() || *p == '#')
{
static const char marker[]="##NAME: MARKER:";
if (s.substr(0, sizeof(marker)-1) == marker)
seen_marker=true;
continue;
}
std::string::iterator q=std::find_if(p, e, isspace());
std::string name(p, q);
std::string setting;
while (1)
{
q=std::find_if(q, e, not_isspace());
while (q != e && isspace()(e[-1]))
--e;
if (q == e)
break;
bool continuing=false;
if (e[-1] == '\\')
{
continuing=true;
e[-1]=' ';
}
setting.insert(setting.end(), q, e);
if (!continuing)
break;
std::getline(f, s);
q=s.begin();
e=s.end();
}
parsed_config.insert(std::make_pair(name, setting));
}
if (!seen_marker)
{
courier_auth_err((reload
? "marker line not found in %s will try again later"
: "marker line not found in %s (probably forgot to run sysconftool after an upgrade)"), filename);
return false;
}
return do_load();
}
bool courier::auth::config_file::getconfig(const char *name,
std::string &value,
bool required,
const char *default_value) const
{
std::map<std::string, std::string>::const_iterator
iter=parsed_config.find(name);
if (iter != parsed_config.end())
{
value=iter->second;
return true;
}
if (required)
{
courier_auth_err("%s not found in %s",
name, filename);
return false;
}
value.clear();
if (default_value)
value=default_value;
return true;
}
template<>
bool courier::auth::config_file::config(const char *name,
std::string &value,
bool required,
const char *default_value) const
{
return getconfig(name, value, required, default_value);
}
std::string courier::auth::config_file::config(const char *name) const
{
return config(name, 0);
}
std::string courier::auth::config_file::config(const char *name,
const char *default_value) const
{
std::string retval;
config(name, retval, false, default_value);
return retval;
}
std::string
courier::auth::config_file::expand_string(const std::string &s,
const std::map<std::string,
std::string> ¶meters)
{
std::ostringstream o;
std::string::const_iterator b=s.begin(), e=s.end(), p;
std::map<std::string, std::string>::const_iterator p_iter;
while (b != e)
{
p=std::find(b, e, '$');
o << std::string(b, p);
b=p;
if (b == e)
continue;
if (*++b != '(')
{
o << '$';
continue;
}
p=std::find(++b, e, ')');
p_iter=parameters.find(std::string(b, p));
b=p;
if (b != e)
++b;
if (p_iter != parameters.end())
o << p_iter->second;
}
return o.str();
}
std::string
courier::auth::config_file::parse_custom_query(const std::string &s,
const std::string &login,
const std::string &defdomain,
std::map<std::string,
std::string> ¶meters)
{
std::string::const_iterator b=login.begin(),
e=login.end(),
p=std::find(b, e, '@');
parameters["local_part"]=std::string(b, p);
parameters["domain"]=p == e ? defdomain:std::string(p+1, e);
return expand_string(s, parameters);
}
|