X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2Fmod-hostserv.c;h=a2e50d30aa8ee1c860ae8d0d9b2215198a8361d5;hb=9a7baf0638dac261b1aea690fae5f52caf674812;hp=88f55d45ce93b2c052ce5ee303c8403c8621fd3f;hpb=953be72cfd954be6a2521ac2b5b561f4dca5ac4e;p=srvx.git diff --git a/src/mod-hostserv.c b/src/mod-hostserv.c index 88f55d4..a2e50d3 100644 --- a/src/mod-hostserv.c +++ b/src/mod-hostserv.c @@ -1,5 +1,5 @@ /* mod-hostserv.c - HostServ module for srvx - * Copyright 2003-2004 Martijn Smit and srvx Development Team + * Copyright 2012-2013 pk910, Stricted, NurPech * * This file is part of srvx. * @@ -23,10 +23,18 @@ * "hostserv" { * "nick" "HostServ"; * "modes" "+iok"; - "toplevel_access" "600"; + * "toplevel_access" "600"; + * "fallback_other_assignment" "1"; //fall back to another assignment when active assignment gets removed + * "manager_can_rename_toplevel" "0"; //managers of a toplevel group may rename the whole group + * "manager_can_del_toplevel" "0"; //managers of a toplevel group may delete the whole group + * "manager_can_rename_secondlevel" "0"; //managers of a secondlevel group may rename the whole group + * "manager_can_del_secondlevel" "0"; //managers of a secondlevel group may delete the whole group * }; * }; * + * After you started srvx make the bot active: + /msg opserv bind hostserv * hostserv.* + /msg opserv bind hostserv help *modcmd.help */ #include "chanserv.h" @@ -36,36 +44,55 @@ #include "modcmd.h" #include "saxdb.h" #include "timeq.h" -#include "gline.h" -#define KEY_TOPLEVEL "toplevel" -#define KEY_SECONDLEVEL "secondlevel" -#define KEY_MANAGERS "managers" -#define KEY_FAKEHOST "fakehost" -#define KEY_ASSIGNMENTS "assignments" +#define KEY_TOPLEVEL "TopLevel" +#define KEY_SECONDLEVEL "SecondLevel" +#define KEY_MANAGERS "Manager" +#define KEY_ASSIGNMENTS "Assignments" +#define KEY_ACTIVE "active" + +#define HS_ASSIGNMENTSTATE_AUTO -1 +#define HS_ASSIGNMENTSTATE_OFF 0 +#define HS_ASSIGNMENTSTATE_ON 1 static const struct message_entry msgtab[] = { + { "HSMSG_ACCESS_DENIED", "Access denied." }, { "HSMSG_ASSIGNED_FAKEHOSTS", "Assigned Fakehosts for User $b%s$b:" }, { "HSMSG_ASSIGNED_FAKEHOST", " $b%s.%s$b" }, { "HSMSG_ASSIGNED_FAKEHOST_ACTTIVE", " $b%s.%s$b (active)" }, - { "HSMSG_ASSIGN_HOWTO", "Use $bassign xxx.yyy$b to activate one of the listed fakehosts or $bassign *$b to use the default fakehost." }, + { "HSMSG_ASSIGNED_FAKEHOST_NOT_ACTIVE", "Fakehost $b%s.%s.$b is not active." }, + { "HSMSG_ASSIGN_HOWTO", "Use $bset xxx.yyy$b to activate one of the listed fakehosts or $bset *$b to use the default fakehost." }, { "HSMSG_ASSIGNED_NONE", " None." }, { "HSMSG_MANAGED_FAKEHOSTS", "Fakehosts managed by User $b%s$b:" }, { "HSMSG_MANAGED_TOPLEVEL", " $b*.%s$b fakehosts: %d assignments: %d" }, + { "HSMSG_MANAGED_TOPLEVEL_OWN", " $b*.%s$b fakehosts: %d assignments: %d (active)" }, { "HSMSG_MANAGED_FAKEHOST", " $b%s.%s$b assignments: %d" }, { "HSMSG_MANAGE_HOWTO", "Use $bview xxx.yyy$b to view more information about a fakehost group." }, { "HSMSG_UNKNOWN_FAKEHOST", "Fakehost $b%s.%s$b is unknown or you have no access to manage it." }, { "HSMSG_TOPLEVEL_FAKEHOSTS", "Fakehosts in group $b*.%s$b:" }, { "HSMSG_TOPLEVEL_FAKEHOST", " $b%s.%s$b assignments: %d managers: %d" }, + { "HSMSG_TOPLEVEL_INVALID", "The name of the group you entered is invalid ($b%s$b)" }, { "HSMSG_MANAGERS_TOPLEVEL", "Managers of group $b*.%s$b:" }, { "HSMSG_MANAGERS_FAKEHOST", "Managers of group $b%s.%s$b:" }, { "HSMSG_MANAGERS_MANAGERS", " %s" }, { "HSMSG_FAKEHOST_ASSIGNMENTS", "Assignments in group $b%s.%s$b:" }, { "HSMSG_FAKEHOST_ASSIGNMENT", " $b%s$b (%s.%s.%s)" }, { "HSMSG_FAKEHOST_ASSIGNMENT_ACTIVE", " $b%s$b (%s.%s.%s) active" }, + { "HSMSG_FAKEHOST_SET_SUCCESS", "$b%s.%s$b where set successfully." }, + { "HSMSG_FAKEHOST_TOPLEVEL_ADDED", "Group $b%s$b successfully added." }, + { "HSMSG_FAKEHOST_TOPLEVEL_ALREADY_EXISTS", "Group $b%s$b already exists." }, + { "HSMSG_FAKEHOST_TOPLEVEL_DELETED", "Group $b%s$b successfully deleted." }, + { "HSMSG_FAKEHOST_SECONDLEVEL_ADDED", "Group $b%s.%s$b successfully added." }, + { "HSMSG_FAKEHOST_SECONDLEVEL_ALREADY_EXISTS", "Group $b%s.%s$b already exists." }, + { "HSMSG_FAKEHOST_SECONDLEVEL_DELETED", "Group $b%s.%s$b successfully deleted." }, + { "HSMSG_FAKEHOST_RENAMED", "Group $b%s.%s$b renamed to $b%s.%s$b." }, { "HSMSG_MANAGER_ALREADY", "$b%s$b is already a manager of %s.%s" }, { "HSMSG_MANAGER_ADDED", "$b%s$b is now a manager of %s.%s" }, { "HSMSG_MANAGER_NOT", "$b%s$b is not a manager of %s.%s" }, + { "HSMSG_MANAGER_DELETED", "$b%s$b is no longer a manager of %s.%s" }, + { "HSMSG_FAKEHOST_ASSIGN_SUCCESS", "Group $b%s.%s$b was assigned successfully." }, + { "HSMSG_FAKEHOST_ASSIGNED", "Group $b%s.%s$b is already assigned to the user." }, + { "HSMSG_FAKEHOST_UNASSIGN_SUCCESS", "Group $b%s.%s$b was unassigned successfully." }, { NULL, NULL } }; @@ -74,6 +101,11 @@ static struct { const char *nick; const char *modes; int toplevel_access; + int fallback_other_assignment : 1; + int manager_can_del_toplevel : 1; + int manager_can_del_secondlevel : 1; + int manager_can_rename_toplevel : 1; + int manager_can_rename_secondlevel : 1; } hostserv_conf; const char *hostserv_module_deps[] = { NULL }; @@ -81,9 +113,6 @@ struct userNode *hostserv; struct helpfile *hostserv_helpfile; static struct module *hostserv_module; static struct service *hostserv_service; -static dict_t hostserv_func_dict; -static int shutting_down; -static FILE *reqlog_f; static struct log_type *HS_LOG; static struct hs_toplevel *toplevels = NULL; static struct hs_user *hostserv_users = NULL; @@ -115,6 +144,7 @@ struct hs_assignment { struct hs_manager { char type; void *object; + int active; struct hs_user *user; struct hs_manager *next; struct hs_manager *unext; /* for hs_user */ @@ -132,6 +162,7 @@ static void hs_del_secondlevel(struct hs_secondlevel *slfh, int remove_from_tlfh static void hs_del_manager(struct hs_manager *manager, int remove_from_object); static void hs_del_assignment(struct hs_assignment *assignment, int remove_from_slfh); static void hs_del_user(struct hs_user *user); +static void hs_activate_assignment(struct hs_user *user, struct hs_assignment *assignment); static void hs_free_all() { struct hs_toplevel *tlfh, *next_tlfh; @@ -147,7 +178,7 @@ static void hs_free_all() { } for(slfh = tlfh->secondlevel; slfh; slfh = next_slfh) { next_slfh = slfh->next; - for(manager = tlfh->managers; manager; manager = next_manager) { + for(manager = slfh->managers; manager; manager = next_manager) { next_manager = manager->next; free(manager); } @@ -169,7 +200,7 @@ static void hs_free_all() { hostserv_users = NULL; } -static struct hs_toplevel *hs_add_toplevel(char *name) { +static struct hs_toplevel *hs_add_toplevel(const char *name) { struct hs_toplevel *tlfh = calloc(1, sizeof(*tlfh)); tlfh->fakehost = strdup(name); tlfh->next = toplevels; @@ -189,11 +220,39 @@ static void hs_del_toplevel(struct hs_toplevel *tlfh) { next_slfh = slfh->next; hs_del_secondlevel(slfh, 0); } + + struct hs_toplevel *ctlfh, *last_tlfh = NULL; + for(ctlfh = toplevels; ctlfh; ctlfh = ctlfh->next) { + if(ctlfh == tlfh) { + if(last_tlfh) + last_tlfh->next = ctlfh->next; + else + toplevels = ctlfh->next; + } else + last_tlfh = ctlfh; + } free(tlfh->fakehost); free(tlfh); } -static struct hs_secondlevel *hs_add_secondlevel(struct hs_toplevel *tlfh, char *name) { +static void hs_rename_toplevel(struct hs_toplevel *tlfh, const char *name) { + struct hs_secondlevel *slfh; + struct hs_assignment *assng; + + free(tlfh->fakehost); + tlfh->fakehost = strdup(name); + + //trigger rename for all assignments + for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) { + for(assng = slfh->assignments; assng; assng = assng->next) { + if(assng->active) { + hs_activate_assignment(assng->user, assng); + } + } + } +} + +static struct hs_secondlevel *hs_add_secondlevel(struct hs_toplevel *tlfh, const char *name) { struct hs_secondlevel *slfh = calloc(1, sizeof(*slfh)); slfh->toplevel = tlfh; slfh->fakehost = strdup(name); @@ -230,6 +289,20 @@ static void hs_del_secondlevel(struct hs_secondlevel *slfh, int remove_from_tlfh free(slfh); } +static void hs_rename_secondlevel(struct hs_secondlevel *slfh, const char *name) { + struct hs_assignment *assng; + + free(slfh->fakehost); + slfh->fakehost = strdup(name); + + //trigger rename for all assignments + for(assng = slfh->assignments; assng; assng = assng->next) { + if(assng->active) { + hs_activate_assignment(assng->user, assng); + } + } +} + static struct hs_manager *hs_add_manager_toplevel(struct hs_toplevel *tlfh, struct hs_user *user) { struct hs_manager *manager = calloc(1, sizeof(*manager)); manager->user = user; @@ -301,18 +374,49 @@ static void hs_del_manager(struct hs_manager *manager, int remove_from_object) { free(manager); } -static struct hs_assignment *hs_add_assignment(struct hs_secondlevel *slfh, struct hs_user *user) { +static void hs_activate_assignment(struct hs_user *user, struct hs_assignment *assignment) { + struct hs_toplevel *tlfh; + struct hs_secondlevel *slfh; + struct hs_assignment *assgn; + char fakehost[HOSTLEN]; + + assert((!assignment || (assignment->user == user))); + + if(user->assignments) { + for(assgn = user->assignments; assgn; assgn = assgn->unext) + assgn->active = 0; + } + + if(user->hi->fakehost) { + free(user->hi->fakehost); + user->hi->fakehost = NULL; + } + + if(assignment) { + slfh = assignment->secondlevel; + tlfh = slfh->toplevel; + snprintf(fakehost, sizeof(fakehost), "$.%s.%s", slfh->fakehost, tlfh->fakehost); + user->hi->fakehost = strdup(fakehost); + assignment->active = 1; + } + + apply_fakehost(user->hi, NULL); +} + +static struct hs_assignment *hs_add_assignment(struct hs_secondlevel *slfh, struct hs_user *user, int active) { struct hs_assignment *assignment = calloc(1, sizeof(*assignment)); assignment->secondlevel = slfh; assignment->user = user; - if(user->assignments == NULL) - assignment->active = 1; + if(active == HS_ASSIGNMENTSTATE_AUTO) + assignment->active = (user->assignments == NULL ? 1 : 0); + else + assignment->active = (active == HS_ASSIGNMENTSTATE_ON ? 1 : 0); assignment->next = slfh->assignments; slfh->assignments = assignment; assignment->unext = user->assignments; user->assignments = assignment; if(assignment->active) { - /* use this assignment */ + hs_activate_assignment(user, assignment); } return assignment; } @@ -333,6 +437,7 @@ static void hs_del_assignment(struct hs_assignment *assignment, int remove_from_ prev_assignment = NULL; } if(remove_from_slfh != 2) { + prev_assignment = NULL; for(cassignment = assignment->user->assignments; cassignment; cassignment = cassignment->unext) { if(cassignment == assignment) { if(prev_assignment) @@ -343,15 +448,38 @@ static void hs_del_assignment(struct hs_assignment *assignment, int remove_from_ } else prev_assignment = cassignment; } - if(assignment->user->managements == NULL && assignment->user->assignments == NULL) - hs_del_user(assignment->user); + if(assignment->active) { - /* use other assignment */ + /* use another assignment - or fall back to default user host? */ + cassignment = NULL; + if(hostserv_conf.fallback_other_assignment && assignment->user->assignments) { + /* try to find another assignment from same slfh first */ + for(cassignment = assignment->secondlevel->assignments; cassignment; cassignment = cassignment->next) { + if(cassignment != assignment && cassignment->user == assignment->user) + break; + } + /* use another tlfh assignment */ + if(!cassignment) + cassignment = assignment->user->assignments; + } + hs_activate_assignment(assignment->user, cassignment); } + + if(assignment->user->managements == NULL && assignment->user->assignments == NULL) + hs_del_user(assignment->user); } free(assignment); } +static struct hs_assignment *hs_get_assignment(struct hs_secondlevel *slfh, struct hs_user *user) { + struct hs_assignment *cassignment; + for(cassignment = slfh->assignments; cassignment; cassignment = cassignment->next) { + if(cassignment->user == user) + return cassignment; + } + return NULL; +} + static struct hs_user *hs_get_user(struct handle_info *hi, int create) { struct hs_user *cuser; for(cuser = hostserv_users; cuser; cuser = cuser->next) { @@ -400,8 +528,7 @@ static void hs_del_user(struct hs_user *user) { /* END OF MANAGEMENT FUNCTIONS */ static int check_management_access(struct handle_info *hi, struct hs_toplevel *tlfh, struct hs_secondlevel *slfh) { - tlfh = NULL; - if(!hi) + if(!hi) return 0; if(hi->opserv_level >= hostserv_conf.toplevel_access) return 1; @@ -415,9 +542,11 @@ static int check_management_access(struct handle_info *hi, struct hs_toplevel *t return 1; } } - for(manager = user->managements; manager; manager = manager->next) { - if(manager->type == 1 && manager->object == slfh) - return 1; + if(tlfh) { + for(manager = user->managements; manager; manager = manager->next) { + if(manager->type == 1 && manager->object == tlfh) + return 1; + } } return 0; } @@ -482,7 +611,6 @@ static void cmd_view_secondlevel_information(UNUSED_ARG(struct userNode *user), } } - static MODCMD_FUNC(cmd_view) { struct handle_info *hi; if(argc >= 2 && !strchr(argv[1], '.')) { @@ -495,6 +623,10 @@ static MODCMD_FUNC(cmd_view) { } char *slfh_name = argv[1]; char *tlfh_name = strchr(argv[1], '.'); + if(!tlfh_name) { + reply("HSMSG_TOPLEVEL_INVALID", slfh_name); + return 0; + } *tlfh_name = '\0'; tlfh_name++; if(strchr(tlfh_name, '.')) { @@ -565,7 +697,7 @@ static MODCMD_FUNC(cmd_view) { } else if(huser && huser->managements) { reply("HSMSG_MANAGED_FAKEHOSTS", hi->handle); struct hs_manager *manager; - for(manager = huser->managements; manager; manager = manager->next) { + for(manager = huser->managements; manager; manager = manager->unext) { if(manager->type == 1) { struct hs_toplevel *tlfh = manager->object; struct hs_secondlevel *slfh; @@ -602,72 +734,296 @@ static MODCMD_FUNC(cmd_view) { return 1; } +static MODCMD_FUNC(cmd_addhost) { + struct handle_info *hi; + struct hs_toplevel *tlfh; + struct hs_secondlevel *slfh; + if (!(hi = user->handle_info)) { + reply("NSMSG_MUST_AUTH"); + return 0; + } + char *slfh_name = argv[1]; + char *tlfh_name = strchr(argv[1], '.'); + if(!tlfh_name) { + reply("HSMSG_TOPLEVEL_INVALID", slfh_name); + return 0; + } + *tlfh_name = '\0'; + tlfh_name++; + if(strchr(tlfh_name, '.')) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + if(!irccasecmp(slfh_name, "*")) { + if(!check_management_access(hi, NULL, NULL)) { + reply("HSMSG_ACCESS_DENIED"); + return 0; + } + for(tlfh = toplevels; tlfh; tlfh = tlfh->next) { + if(!irccasecmp(tlfh->fakehost, tlfh_name)) break; + } + if(tlfh) { + reply("HSMSG_FAKEHOST_TOPLEVEL_ALREADY_EXISTS", slfh_name, tlfh_name); + return 0; + } + if(irccasecmp(tlfh_name, "*")) { + reply("HSMSG_ACCESS_DENIED"); + return 0; + } + hs_add_toplevel(tlfh_name); + reply("HSMSG_FAKEHOST_TOPLEVEL_ADDED", tlfh_name); + } else { + for(tlfh = toplevels; tlfh; tlfh = tlfh->next) { + if(!irccasecmp(tlfh->fakehost, tlfh_name)) break; + } + if(!tlfh) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + if(!check_management_access(hi, tlfh, NULL)) { + reply("HSMSG_ACCESS_DENIED"); + return 0; + } + for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) { + if(!irccasecmp(slfh->fakehost, slfh_name)) break; + } + if(slfh) { + reply("HSMSG_FAKEHOST_SECONDLEVEL_ALREADY_EXISTS", slfh_name, tlfh_name); + return 0; + } + hs_add_secondlevel(tlfh, slfh_name); + reply("HSMSG_FAKEHOST_SECONDLEVEL_ADDED", slfh_name, tlfh_name); + } + return 1; +} + +static MODCMD_FUNC(cmd_delhost) { + struct handle_info *hi; + if (!(hi = user->handle_info)) { + reply("NSMSG_MUST_AUTH"); + return 0; + } + char *slfh_name = argv[1]; + char *tlfh_name = strchr(argv[1], '.'); + *tlfh_name = '\0'; + tlfh_name++; + if(strchr(tlfh_name, '.')) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + struct hs_toplevel *tlfh; + for(tlfh = toplevels; tlfh; tlfh = tlfh->next) { + if(!irccasecmp(tlfh->fakehost, tlfh_name)) break; + } + if(!tlfh) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + if(!irccasecmp(slfh_name, "*")) { + if(!check_management_access(hi, (hostserv_conf.manager_can_del_toplevel ? tlfh : NULL), NULL)) { /* manager access is enough to delete whole toplevel? */ + reply("HSMSG_ACCESS_DENIED"); + return 0; + } + hs_del_toplevel(tlfh); + reply("HSMSG_FAKEHOST_TOPLEVEL_DELETED", tlfh_name); + } else { + struct hs_secondlevel *slfh; + for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) { + if(!irccasecmp(slfh->fakehost, slfh_name)) break; + } + if(!slfh) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + if(!check_management_access(hi, tlfh, (hostserv_conf.manager_can_del_secondlevel ? slfh : NULL))) { + reply("HSMSG_ACCESS_DENIED"); + return 0; + } + hs_del_secondlevel(slfh, 1); + reply("HSMSG_FAKEHOST_SECONDLEVEL_DELETED", slfh_name, tlfh_name); + } + return 1; +} + +static MODCMD_FUNC(cmd_renamehost) { + struct handle_info *hi; + if (!(hi = user->handle_info)) { + reply("NSMSG_MUST_AUTH"); + return 0; + } + //old fakehost name + char *slfh_name = argv[1]; + char *tlfh_name = strchr(argv[1], '.'); + if(!tlfh_name) { + reply("HSMSG_TOPLEVEL_INVALID", slfh_name); + return 0; + } + *tlfh_name = '\0'; + tlfh_name++; + if(strchr(tlfh_name, '.')) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + struct hs_toplevel *tlfh; + for(tlfh = toplevels; tlfh; tlfh = tlfh->next) { + if(!irccasecmp(tlfh->fakehost, tlfh_name)) break; + } + if(!tlfh) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + //new fakehost name + char *new_slfh_name = argv[2]; + char *new_tlfh_name = strchr(argv[2], '.'); + if(!new_tlfh_name) { + reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name); + return 0; + } + *new_tlfh_name = '\0'; + new_tlfh_name++; + if(strchr(new_tlfh_name, '.')) { + new_tlfh_name--; + *new_tlfh_name = '.'; + reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name); + return 0; + } + if(!irccasecmp(slfh_name, "*")) { + if(!check_management_access(hi, (hostserv_conf.manager_can_rename_toplevel ? tlfh : NULL), NULL)) { /* manager access is enough to delete whole toplevel? */ + reply("HSMSG_ACCESS_DENIED"); + return 0; + } + if(irccasecmp(new_slfh_name, "*")) { + //can't rename toplevel into secondlevel fakehost! + new_tlfh_name--; + *new_tlfh_name = '.'; + reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name); + return 0; + } + struct hs_toplevel *ctlfh; + for(ctlfh = toplevels; ctlfh; ctlfh = ctlfh->next) { + if(!irccasecmp(ctlfh->fakehost, new_tlfh_name)) break; + } + if(ctlfh) { + reply("HSMSG_FAKEHOST_TOPLEVEL_ALREADY_EXISTS", new_slfh_name, new_tlfh_name); + return 0; + } + if(irccasecmp(new_tlfh_name, "*")) { + reply("HSMSG_ACCESS_DENIED"); + return 0; + } + if(strcmp(new_tlfh_name, tlfh->fakehost)) + hs_rename_toplevel(tlfh, new_tlfh_name); + reply("HSMSG_FAKEHOST_RENAMED", slfh_name, tlfh_name, new_slfh_name, new_tlfh_name); + } else { + struct hs_secondlevel *slfh; + for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) { + if(!irccasecmp(slfh->fakehost, slfh_name)) break; + } + if(!slfh) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + if(!check_management_access(hi, tlfh, (hostserv_conf.manager_can_rename_secondlevel ? slfh : NULL))) { + reply("HSMSG_ACCESS_DENIED"); + return 0; + } + if(irccasecmp(new_tlfh_name, tlfh_name)) { + //can't rename toplevel and secondlevel fakehost with one command! + new_tlfh_name--; + *new_tlfh_name = '.'; + reply("HSMSG_TOPLEVEL_INVALID", new_slfh_name); + } + struct hs_secondlevel *cslfh; + for(cslfh = tlfh->secondlevel; cslfh; cslfh = cslfh->next) { + if(!irccasecmp(cslfh->fakehost, new_slfh_name)) break; + } + if(cslfh) { + reply("HSMSG_FAKEHOST_SECONDLEVEL_ALREADY_EXISTS", new_slfh_name, new_tlfh_name); + return 0; + } + if(strcmp(new_slfh_name, slfh->fakehost)) + hs_rename_secondlevel(slfh, new_slfh_name); + reply("HSMSG_FAKEHOST_RENAMED", slfh_name, tlfh_name, new_slfh_name, new_tlfh_name); + } + return 1; +} + static MODCMD_FUNC(cmd_addmanager) { struct handle_info *hi; char *fakehost; - if(argc >= 2) { - if(!strchr(argv[1], '.')) { - if (!(hi = modcmd_get_handle_info(user, argv[1]))) - return 0; - fakehost = argv[2]; - } else { - if (!(hi = modcmd_get_handle_info(user, argv[2]))) - return 0; - fakehost = argv[1]; - } - char *slfh_name = fakehost; - char *tlfh_name = strchr(fakehost, '.'); - *tlfh_name = '\0'; - tlfh_name++; - if(strchr(tlfh_name, '.')) { - reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); - return 0; - } - struct hs_toplevel *tlfh; - struct hs_secondlevel *slfh = NULL; - for(tlfh = toplevels; tlfh; tlfh = tlfh->next) { - if(!irccasecmp(tlfh->fakehost, tlfh_name)) break; - } - if(!tlfh) { - reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); - return 0; - } - if(!irccasecmp(slfh_name, "*")) { - if(!check_management_access(user->handle_info, tlfh, NULL)) { - reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); - return 0; - } - } else { - for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) { - if(!irccasecmp(slfh->fakehost, slfh_name)) break; - } - if(!slfh || !check_management_access(user->handle_info, tlfh, slfh)) { - reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); - return 0; - } - } - struct hs_user *huser = hs_get_user(hi, 1); - struct hs_manager *manager; - if(slfh) { - for(manager = huser->managements; manager; manager = manager->next) { - if(manager->type == 2 && manager->object == slfh) { - reply("HSMSG_MANAGER_ALREADY", hi->handle, slfh_name, tlfh_name); - return 0; - } - } - } - for(manager = huser->managements; manager; manager = manager->next) { - if(manager->type == 1 && manager->object == tlfh) { - reply("HSMSG_MANAGER_ALREADY", hi->handle, "*", tlfh_name); - return 0; - } - } - if(slfh) - hs_add_manager_secondlevel(slfh, huser); - else - hs_add_manager_toplevel(tlfh, huser); - reply("HSMSG_MANAGER_ADDED", hi->handle, slfh_name, tlfh_name); + if(argc >= 3) { + if(!strchr(argv[1], '.')) { + if (!(hi = modcmd_get_handle_info(user, argv[1]))) + return 0; + fakehost = argv[2]; + } else { + if (!(hi = modcmd_get_handle_info(user, argv[2]))) + return 0; + fakehost = argv[1]; + } + char *slfh_name = fakehost; + char *tlfh_name = strchr(fakehost, '.'); + if(!tlfh_name) { + reply("HSMSG_TOPLEVEL_INVALID", slfh_name); + return 0; + } + *tlfh_name = '\0'; + tlfh_name++; + if(strchr(tlfh_name, '.')) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + struct hs_toplevel *tlfh; + struct hs_secondlevel *slfh = NULL; + for(tlfh = toplevels; tlfh; tlfh = tlfh->next) { + if(!irccasecmp(tlfh->fakehost, tlfh_name)) break; + } + if(!tlfh) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + if(!irccasecmp(slfh_name, "*")) { + if(!check_management_access(user->handle_info, tlfh, NULL)) { + reply("HSMSG_ACCESS_DENIED"); + return 0; + } + } else { + for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) { + if(!irccasecmp(slfh->fakehost, slfh_name)) break; + } + if(!slfh) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + if(!check_management_access(user->handle_info, tlfh, slfh)) { + reply("HSMSG_ACCESS_DENIED"); + return 0; + } + } + struct hs_user *huser = hs_get_user(hi, 1); + struct hs_manager *manager; + if(slfh) { + for(manager = huser->managements; manager; manager = manager->next) { + if(manager->type == 2 && manager->object == slfh) { + reply("HSMSG_MANAGER_ALREADY", hi->handle, slfh_name, tlfh_name); + return 0; + } + } + } + for(manager = huser->managements; manager; manager = manager->next) { + if(manager->type == 1 && manager->object == tlfh) { + reply("HSMSG_MANAGER_ALREADY", hi->handle, "*", tlfh_name); + return 0; + } + } + if(slfh) + hs_add_manager_secondlevel(slfh, huser); + else + hs_add_manager_toplevel(tlfh, huser); + reply("HSMSG_MANAGER_ADDED", hi->handle, slfh_name, tlfh_name); + return 1; } + return 0; } static MODCMD_FUNC(cmd_delmanager) { @@ -684,7 +1040,10 @@ static MODCMD_FUNC(cmd_delmanager) { } char *slfh_name = fakehost; char *tlfh_name = strchr(fakehost, '.'); - *tlfh_name = '\0'; + if(!tlfh_name) { + reply("HSMSG_TOPLEVEL_INVALID", slfh_name); + return 0; + } tlfh_name++; if(strchr(tlfh_name, '.')) { reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); @@ -701,17 +1060,21 @@ static MODCMD_FUNC(cmd_delmanager) { } if(!irccasecmp(slfh_name, "*")) { if(!check_management_access(user->handle_info, tlfh, NULL)) { - reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + reply("HSMSG_ACCESS_DENIED"); return 0; } } else { for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) { if(!irccasecmp(slfh->fakehost, slfh_name)) break; } - if(!slfh || !check_management_access(user->handle_info, tlfh, slfh)) { + if(!slfh) { reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); return 0; } + if(!check_management_access(user->handle_info, tlfh, slfh)) { + reply("HSMSG_ACCESS_DENIED"); + return 0; + } } struct hs_user *huser = hs_get_user(hi, 0); struct hs_manager *manager; @@ -740,6 +1103,196 @@ static MODCMD_FUNC(cmd_delmanager) { } hs_del_manager(manager, 1); reply("HSMSG_MANAGER_DELETED", hi->handle, slfh_name, tlfh_name); + return 1; +} + +static MODCMD_FUNC(cmd_set) { + struct handle_info *hi; + struct hs_user *hs_user; + struct hs_assignment *assignment; + struct hs_toplevel *tlfh; + struct hs_secondlevel *slfh; + char *fakehost; + + if (!(hi = user->handle_info)) { + reply("NSMSG_MUST_AUTH"); + return 0; + } + hs_user = hs_get_user(hi, 0); + if(!hs_user) + return 0; //nothing to do here + if(!strcmp(argv[1], "*")) { + hs_activate_assignment(hs_user, NULL); + return 1; + } else { + fakehost = argv[1]; + char *slfh_name = fakehost; + char *tlfh_name = strchr(fakehost, '.'); + if(tlfh_name) { + *tlfh_name = '\0'; + tlfh_name++; + } + if(!tlfh_name || strchr(tlfh_name, '.')) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, (tlfh_name ? tlfh_name : "")); + return 0; + } + for(assignment = hs_user->assignments; assignment; assignment = assignment->unext) { + slfh = assignment->secondlevel; + tlfh = slfh->toplevel; + if(!irccasecmp(tlfh_name, tlfh->fakehost) && !irccasecmp(slfh_name, slfh->fakehost)) { + hs_activate_assignment(hs_user, assignment); + reply("HSMSG_FAKEHOST_SET_SUCCESS", slfh->fakehost, tlfh->fakehost); + return 1; + } + } + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } +} + +static MODCMD_FUNC(cmd_assign) { + struct handle_info *hi; + char *fakehost; + if(!strchr(argv[1], '.')) { + if (!(hi = modcmd_get_handle_info(user, argv[1]))) + return 0; + fakehost = argv[2]; + } else { + if (!(hi = modcmd_get_handle_info(user, argv[2]))) + return 0; + fakehost = argv[1]; + } + if (!user->handle_info) { + reply("NSMSG_MUST_AUTH"); + return 0; + } + char *slfh_name = fakehost; + char *tlfh_name = strchr(fakehost, '.'); + if(!tlfh_name) { + reply("HSMSG_TOPLEVEL_INVALID", slfh_name); + return 0; + } + tlfh_name++; + if(strchr(tlfh_name, '.')) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + struct hs_toplevel *tlfh; + struct hs_secondlevel *slfh; + struct hs_user *hs_user = hs_get_user(hi, 1); + for(tlfh = toplevels; tlfh; tlfh = tlfh->next) { + if(!irccasecmp(tlfh_name, tlfh->fakehost)) { + for(slfh = tlfh->secondlevel; slfh; slfh = slfh->next) { + if(!irccasecmp(slfh_name, slfh->fakehost)) { + if(!check_management_access(user->handle_info, tlfh, slfh)) { + reply("HSMSG_ACCESS_DENIED"); + return 0; + } + if(hs_get_assignment(slfh, hs_user)) { + reply("HSMSG_FAKEHOST_ASSIGNED", slfh_name, tlfh_name); + return 0; + } + hs_add_assignment(slfh, hs_user, HS_ASSIGNMENTSTATE_AUTO); + reply("HSMSG_FAKEHOST_ASSIGN_SUCCESS", slfh_name, tlfh_name); + return 1; + } + } + } + } + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; +} + +static MODCMD_FUNC(cmd_unassign) { + struct handle_info *hi; + char *fakehost; + if(!strchr(argv[1], '.')) { + if (!(hi = modcmd_get_handle_info(user, argv[1]))) + return 0; + fakehost = argv[2]; + } else { + if (!(hi = modcmd_get_handle_info(user, argv[2]))) + return 0; + fakehost = argv[1]; + } + if (!user->handle_info) { + reply("NSMSG_MUST_AUTH"); + return 0; + } + char *slfh_name = fakehost; + char *tlfh_name = strchr(fakehost, '.'); + if(!tlfh_name) { + reply("HSMSG_TOPLEVEL_INVALID", slfh_name); + return 0; + } + tlfh_name++; + if(strchr(tlfh_name, '.')) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + struct hs_assignment *assignment; + struct hs_user *hs_user = hs_get_user(hi, 0); + if(!hs_user) { + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; + } + for(assignment = hs_user->assignments; assignment; assignment = assignment->unext) { + if(!irccasecmp(slfh_name, assignment->secondlevel->fakehost)) { + if(!irccasecmp(tlfh_name, assignment->secondlevel->toplevel->fakehost)) { + if(!check_management_access(user->handle_info, assignment->secondlevel->toplevel, assignment->secondlevel)) { + reply("HSMSG_ACCESS_DENIED"); + return 0; + } + hs_del_assignment(assignment, 1); + reply("HSMSG_FAKEHOST_UNASSIGN_SUCCESS", slfh_name, tlfh_name); + return 1; + } + } + } + reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name); + return 0; +} + +static void hostserv_conf_read(void) { + dict_t conf_node; + const char *str; + + str = "modules/hostserv"; + if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) { + log_module(HS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str); + return; + } + + str = database_get_data(conf_node, "nick", RECDB_QSTRING); + if(hostserv_conf.nick && strcmp(hostserv_conf.nick, str)) { + //nick changed + } + hostserv_conf.nick = str; + + str = database_get_data(conf_node, "modes", RECDB_QSTRING); + hostserv_conf.modes = (str ? str : NULL); + + str = database_get_data(conf_node, "toplevel_access", RECDB_QSTRING); + unsigned int toplevel_access = atoi(str); + hostserv_conf.toplevel_access = (toplevel_access ? toplevel_access : 600); + + str = database_get_data(conf_node, "fallback_other_assignment", RECDB_QSTRING); + hostserv_conf.fallback_other_assignment = (atoi(str) ? 1 : 0); + + str = database_get_data(conf_node, "manager_can_del_toplevel", RECDB_QSTRING); + hostserv_conf.manager_can_del_toplevel = (atoi(str) ? 1 : 0); + + str = database_get_data(conf_node, "manager_can_del_secondlevel", RECDB_QSTRING); + hostserv_conf.manager_can_del_secondlevel = (atoi(str) ? 1 : 0); + + str = database_get_data(conf_node, "manager_can_rename_toplevel", RECDB_QSTRING); + hostserv_conf.manager_can_rename_toplevel = (atoi(str) ? 1 : 0); + + str = database_get_data(conf_node, "manager_can_rename_secondlevel", RECDB_QSTRING); + hostserv_conf.manager_can_rename_secondlevel = (atoi(str) ? 1 : 0); + + /*str = database_get_data(conf_node, "description", RECDB_QSTRING); + hostserv_conf.description = (str ? str : NULL);*/ } static int hostserv_saxdb_read_secondlevel(const char *name, void *data, UNUSED_ARG(void *extra)); @@ -765,9 +1318,9 @@ static int hostserv_saxdb_read_toplevel(const char *name, void *data, UNUSED_ARG /* nothing in here, yet */ managerTL = hs_add_manager_toplevel(tlfh, user); if (database_get_data(db, KEY_ACTIVE, RECDB_QSTRING)) - managerTL->active = 1; + managerTL->active = 1; else - managerTL->active = 0; + managerTL->active = 0; } } @@ -798,9 +1351,9 @@ static int hostserv_saxdb_read_secondlevel(const char *name, void *data, UNUSED_ /* nothing in here, yet */ managerSL = hs_add_manager_secondlevel(slfh, user); if (database_get_data(db, KEY_ACTIVE, RECDB_QSTRING)) - managerSL->active = 1; + managerSL->active = 1; else - managerSL->active = 0; + managerSL->active = 0; } } @@ -814,18 +1367,15 @@ static int hostserv_saxdb_read_assignments(const char *name, void *data, UNUSED_ struct record_data *rd = data; struct hs_secondlevel *slfh = extra; struct hs_user *user; - struct hs_assignment *assng; + int active; if (rd->type == RECDB_OBJECT) { dict_t db = GET_RECORD_OBJECT(rd); user = hs_get_user(get_handle_info(name), 1); - assng = hs_add_assignment(slfh, user); + active = (database_get_data(db, KEY_ACTIVE, RECDB_QSTRING) ? HS_ASSIGNMENTSTATE_ON : HS_ASSIGNMENTSTATE_OFF); - if (database_get_data(db, KEY_ACTIVE, RECDB_QSTRING)) - assng->active = 1; - else - assng->active = 0; + hs_add_assignment(slfh, user, active); } return 0; @@ -859,7 +1409,7 @@ hostserv_saxdb_write(struct saxdb_context *ctx) saxdb_start_record(ctx, manager->user->hi->handle, 0); //additional manager information? if(manager->active) - saxdb_write_int(ctx, KEY_ACTIVE, manager->active); + saxdb_write_int(ctx, KEY_ACTIVE, manager->active); saxdb_end_record(ctx); } saxdb_end_record(ctx); @@ -873,7 +1423,7 @@ hostserv_saxdb_write(struct saxdb_context *ctx) saxdb_start_record(ctx, manager->user->hi->handle, 0); //additional manager information? if(manager->active) - saxdb_write_int(ctx, KEY_ACTIVE, manager->active); + saxdb_write_int(ctx, KEY_ACTIVE, manager->active); saxdb_end_record(ctx); } saxdb_end_record(ctx); @@ -905,30 +1455,36 @@ static void hostserv_db_cleanup(void) { } int hostserv_init() { - HS_LOG = log_register_type("HostServ", "file:hostserv.log"); + HS_LOG = log_register_type("HostServ", "file:hostserv.log"); - const char *nick, *modes; - if((nick = conf_get_data("modules/hostserv/nick", RECDB_QSTRING))) { - modes = conf_get_data("modules/hostserv/modes", RECDB_QSTRING); - hostserv = AddLocalUser(nick, nick, NULL, "Host Service", modes); - hostserv_service = service_register(hostserv); - hostserv_service->trigger = '*'; - } - - conf_register_reload(hostserv_conf_read); - reg_exit_func(hostserv_db_cleanup); - saxdb_register("HostServ", hostserv_saxdb_read, hostserv_saxdb_write); - hostserv_module = module_register("HostServ", HS_LOG, "mod-hostserv.help", NULL); - modcmd_register(hostserv_module, "view", cmd_view, 0, MODCMD_REQUIRE_AUTHED, NULL); - modcmd_register(hostserv_module, "addmanager", cmd_addmanager, 3, MODCMD_REQUIRE_AUTHED, NULL); - modcmd_register(hostserv_module, "delmanager", cmd_delmanager, 3, MODCMD_REQUIRE_AUTHED, NULL); - message_register_table(msgtab); - return 1; + const char *nick, *modes; + if((nick = conf_get_data("modules/hostserv/nick", RECDB_QSTRING))) { + modes = conf_get_data("modules/hostserv/modes", RECDB_QSTRING); + hostserv = AddLocalUser(nick, nick, NULL, "Host Service", modes); + hostserv_service = service_register(hostserv); + hostserv_service->trigger = '*'; + } + + conf_register_reload(hostserv_conf_read); + reg_exit_func(hostserv_db_cleanup); + saxdb_register("HostServ", hostserv_saxdb_read, hostserv_saxdb_write); + hostserv_module = module_register("HostServ", HS_LOG, "mod-hostserv.help", NULL); + modcmd_register(hostserv_module, "view", cmd_view, 0, MODCMD_REQUIRE_AUTHED, NULL); + modcmd_register(hostserv_module, "addmanager", cmd_addmanager, 3, MODCMD_REQUIRE_AUTHED, NULL); + modcmd_register(hostserv_module, "delmanager", cmd_delmanager, 3, MODCMD_REQUIRE_AUTHED, NULL); + modcmd_register(hostserv_module, "set", cmd_set, 2, MODCMD_REQUIRE_AUTHED, NULL); + modcmd_register(hostserv_module, "assign", cmd_assign, 3, MODCMD_REQUIRE_AUTHED, NULL); + modcmd_register(hostserv_module, "unassign", cmd_unassign, 3, MODCMD_REQUIRE_AUTHED, NULL); + modcmd_register(hostserv_module, "addhost", cmd_addhost, 2, MODCMD_REQUIRE_AUTHED, NULL); + modcmd_register(hostserv_module, "delhost", cmd_delhost, 2, MODCMD_REQUIRE_AUTHED, NULL); + modcmd_register(hostserv_module, "renamehost", cmd_renamehost, 3, MODCMD_REQUIRE_AUTHED, NULL); + message_register_table(msgtab); + return 1; } int hostserv_finalize(void) { - dict_t conf_node; - const char *str; + dict_t conf_node; + const char *str; str = "modules/hostserv"; if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) { @@ -942,4 +1498,4 @@ int hostserv_finalize(void) { str = database_get_data(conf_node, "modes", RECDB_QSTRING); if (str) hostserv_conf.modes = str; return 1; -} +} \ No newline at end of file