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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
/*
* Copyright (c) 2005 William Pitcock <nenolod -at- nenolod.net>
* Rights to this code are as documented in doc/LICENSE.
*
* Allows opers to offer vhosts to users
*
*/
#include "atheme.h"
#include "hostserv.h"
#include "../groupserv/groupserv.h"
DECLARE_MODULE_V1
(
"hostserv/offer", true, _modinit, _moddeinit,
PACKAGE_STRING,
"Atheme Development Group <http://www.atheme.net>"
);
static void hs_cmd_offer(sourceinfo_t *si, int parc, char *parv[]);
static void hs_cmd_unoffer(sourceinfo_t *si, int parc, char *parv[]);
static void hs_cmd_offerlist(sourceinfo_t *si, int parc, char *parv[]);
static void hs_cmd_take(sourceinfo_t *si, int parc, char *parv[]);
static void write_hsofferdb(database_handle_t *db);
static void db_h_ho(database_handle_t *db, const char *type);
static void remove_group_offered_hosts(mygroup_t *mg);
command_t hs_offer = { "OFFER", N_("Sets vhosts available for users to take."), PRIV_USER_VHOST, 2, hs_cmd_offer, { .path = "hostserv/offer" } };
command_t hs_unoffer = { "UNOFFER", N_("Removes a vhost from the list that users can take."), PRIV_USER_VHOST, 2, hs_cmd_unoffer, { .path = "hostserv/unoffer" } };
command_t hs_offerlist = { "OFFERLIST", N_("Lists all available vhosts."), AC_NONE, 1, hs_cmd_offerlist, { .path = "hostserv/offerlist" } };
command_t hs_take = { "TAKE", N_("Take an offered vhost for use."), AC_AUTHENTICATED, 2, hs_cmd_take, { .path = "hostserv/take" } };
struct hsoffered_ {
char *vhost;
time_t vhost_ts;
stringref creator;
myentity_t *group;
mowgli_node_t node;
};
typedef struct hsoffered_ hsoffered_t;
mowgli_list_t hs_offeredlist;
void _modinit(module_t *m)
{
if (!module_find_published("backend/opensex"))
{
slog(LG_INFO, "Module %s requires use of the OpenSEX database backend, refusing to load.", m->name);
m->mflags = MODTYPE_FAIL;
return;
}
hook_add_db_write(write_hsofferdb);
db_register_type_handler("HO", db_h_ho);
hook_add_event("group_drop");
hook_add_group_drop(remove_group_offered_hosts);
service_named_bind_command("hostserv", &hs_offer);
service_named_bind_command("hostserv", &hs_unoffer);
service_named_bind_command("hostserv", &hs_offerlist);
service_named_bind_command("hostserv", &hs_take);
}
void _moddeinit(module_unload_intent_t intent)
{
hook_del_db_write(write_hsofferdb);
db_unregister_type_handler("HO");
hook_del_group_drop(remove_group_offered_hosts);
service_named_unbind_command("hostserv", &hs_offer);
service_named_unbind_command("hostserv", &hs_unoffer);
service_named_unbind_command("hostserv", &hs_offerlist);
service_named_unbind_command("hostserv", &hs_take);
}
static void write_hsofferdb(database_handle_t *db)
{
mowgli_node_t *n;
MOWGLI_ITER_FOREACH(n, hs_offeredlist.head)
{
hsoffered_t *l = n->data;
db_start_row(db, "HO");
if (l->group != NULL)
db_write_word(db, l->group->name);
db_write_word(db, l->vhost);
db_write_time(db, l->vhost_ts);
db_write_word(db, l->creator);
db_commit_row(db);
}
}
static void db_h_ho(database_handle_t *db, const char *type)
{
hsoffered_t *l;
const char *buf;
time_t vhost_ts;
const char *creator;
myentity_t *mt = NULL;
buf = db_sread_word(db);
if (*buf == '!')
{
mt = myentity_find(buf);
buf = db_sread_word(db);
}
vhost_ts = db_sread_time(db);
creator = db_sread_word(db);
l = smalloc(sizeof(hsoffered_t));
l->group = mt;
l->vhost = sstrdup(buf);
l->vhost_ts = vhost_ts;
l->creator = strshare_get(creator);
mowgli_node_add(l, &l->node, &hs_offeredlist);
}
static inline hsoffered_t *hs_offer_find(const char *host, myentity_t *mt)
{
mowgli_node_t *n;
hsoffered_t *l;
MOWGLI_ITER_FOREACH(n, hs_offeredlist.head)
{
l = n->data;
if (!irccasecmp(l->vhost, host) && (l->group == mt || mt == NULL))
return l;
}
return NULL;
}
static void remove_group_offered_hosts(mygroup_t *mg)
{
return_if_fail(mg != NULL);
myentity_t *mt = entity(mg);
mowgli_node_t *n, *tn;
hsoffered_t *l;
MOWGLI_ITER_FOREACH_SAFE(n, tn, hs_offeredlist.head)
{
l = n->data;
if (l->group != NULL && l->group == mt)
{
slog(LG_VERBOSE, "remove_group_offered_hosts(): removing %s (group %s)", l->vhost, l->group->name);
mowgli_node_delete(n, &hs_offeredlist);
strshare_unref(l->creator);
free(l->vhost);
free(l);
}
}
}
/* OFFER <host> */
static void hs_cmd_offer(sourceinfo_t *si, int parc, char *parv[])
{
char *group = parv[0];
char *host;
hsoffered_t *l;
myentity_t *mt = NULL;
if (!group)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "OFFER");
command_fail(si, fault_needmoreparams, _("Syntax: OFFER [!group] <vhost>"));
return;
}
if (*group != '!')
{
host = group;
group = NULL;
}
else
host = parv[1];
if (!host)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "OFFER");
command_fail(si, fault_needmoreparams, _("Syntax: OFFER [!group] <vhost>"));
return;
}
if (si->smu == NULL)
{
command_fail(si, fault_noprivs, _("You are not logged in."));
return;
}
if (group != NULL)
{
mt = myentity_find(group);
if (mt == NULL)
{
command_fail(si, fault_badparams, _("The requested group does not exist."));
return;
}
}
l = hs_offer_find(host, mt);
if (l != NULL)
{
command_fail(si, fault_badparams, _("The requested offer already exists."));
return;
}
l = smalloc(sizeof(hsoffered_t));
l->group = mt;
l->vhost = sstrdup(host);
l->vhost_ts = CURRTIME;;
l->creator = strshare_ref(entity(si->smu)->name);
mowgli_node_add(l, &l->node, &hs_offeredlist);
if (mt != NULL)
{
command_success_nodata(si, _("You have offered vhost \2%s\2 to group \2%s\2."), host, group);
logcommand(si, CMDLOG_ADMIN, "OFFER: \2%s\2 to \2%s\2", host, group);
}
else
{
command_success_nodata(si, _("You have offered vhost \2%s\2."), host);
logcommand(si, CMDLOG_ADMIN, "OFFER: \2%s\2", host);
}
return;
}
/* UNOFFER <vhost> */
static void hs_cmd_unoffer(sourceinfo_t *si, int parc, char *parv[])
{
char *host = parv[0];
hsoffered_t *l;
mowgli_node_t *n;
if (!host)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "UNOFFER");
command_fail(si, fault_needmoreparams, _("Syntax: UNOFFER <vhost>"));
return;
}
l = hs_offer_find(host, NULL);
if (l == NULL)
{
command_fail(si, fault_nosuch_target, _("vhost \2%s\2 not found in vhost offer database."), host);
return;
}
logcommand(si, CMDLOG_ADMIN, "UNOFFER: \2%s\2", host);
while (l != NULL)
{
mowgli_node_delete(&l->node, &hs_offeredlist);
strshare_unref(l->creator);
free(l->vhost);
free(l);
l = hs_offer_find(host, NULL);
}
command_success_nodata(si, _("You have unoffered vhost \2%s\2."), host);
}
static bool myuser_is_in_group(myuser_t *mu, myentity_t *mt)
{
mygroup_t *mg;
mowgli_node_t *n;
return_val_if_fail(mt != NULL, false);
return_val_if_fail((mg = group(mt)) != NULL, false);
if (!mu) /* NULL users can't be in groups! :o */
return false;
MOWGLI_ITER_FOREACH(n, mg->acs.head)
{
groupacs_t *ga = n->data;
if (ga->mt == entity(mu) && ga->flags & GA_VHOST)
return true;
}
return false;
}
/* TAKE <vhost> */
static void hs_cmd_take(sourceinfo_t *si, int parc, char *parv[])
{
char *host = parv[0];
hsoffered_t *l;
mowgli_node_t *n;
if (!host)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "TAKE");
command_fail(si, fault_needmoreparams, _("Syntax: TAKE <vhost>"));
return;
}
if (si->smu == NULL)
{
command_fail(si, fault_nochange, _("You can't take vhosts when not logged in"));
return;
}
if (metadata_find(si->smu, "private:restrict:setter"))
{
command_fail(si, fault_noprivs, _("You have been restricted from taking vhosts by network staff"));
return;
}
MOWGLI_ITER_FOREACH(n, hs_offeredlist.head)
{
l = n->data;
if (l->group != NULL && !myuser_is_in_group(si->smu, l->group))
continue;
if (!irccasecmp(l->vhost, host))
{
if (strstr(host, "$account"))
replace(host, BUFSIZE, "$account", entity(si->smu)->name);
if (!check_vhost_validity(si, host))
return;
logcommand(si, CMDLOG_GET, "TAKE: \2%s\2 for \2%s\2", host, entity(si->smu)->name);
command_success_nodata(si, _("You have taken vhost \2%s\2."), host);
hs_sethost_all(si->smu, host, get_source_name(si));
do_sethost_all(si->smu, host);
return;
}
}
command_success_nodata(si, _("vhost \2%s\2 not found in vhost offer database."), host);
}
/* OFFERLIST */
static void hs_cmd_offerlist(sourceinfo_t *si, int parc, char *parv[])
{
hsoffered_t *l;
mowgli_node_t *n;
char buf[BUFSIZE];
struct tm tm;
MOWGLI_ITER_FOREACH(n, hs_offeredlist.head)
{
l = n->data;
if (l->group != NULL && !myuser_is_in_group(si->smu, l->group) && !has_priv(si, PRIV_GROUP_ADMIN))
continue;
tm = *localtime(&l->vhost_ts);
strftime(buf, BUFSIZE, TIME_FORMAT, &tm);
if(l->group != NULL)
command_success_nodata(si, "vhost:\2%s\2, group:\2%s\2 creator:\2%s\2 (%s)",
l->vhost, entity(l->group)->name, l->creator, buf);
else
command_success_nodata(si, "vhost:\2%s\2, creator:\2%s\2 (%s)",
l->vhost, l->creator, buf);
}
command_success_nodata(si, "End of list.");
logcommand(si, CMDLOG_GET, "OFFERLIST");
}
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
* vim:ts=8
* vim:sw=8
* vim:noexpandtab
*/
|