Merge branch 'HostServ' of ssh://git.pk910.de:16110/srvx into HostServ
[srvx.git] / src / mod-hostserv.c
index 88f55d45ce93b2c052ce5ee303c8403c8621fd3f..fbba68cb27893f073f10ab5f17c20b3a4cebfe61 100644 (file)
@@ -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.
  *
@@ -27,6 +27,9 @@
  *     };
  *  };
  *
+ * After you started srvx make the bot active:
+ /msg opserv bind hostserv * hostserv.*
+ /msg opserv bind hostserv help *modcmd.help
  */
 
 #include "chanserv.h"
 #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"
 
 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_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_UNASSIGN_SUCCESS", "Group $b%s.%s$b was unassigned successfully." },
     
     { NULL, NULL }
 };
@@ -74,6 +90,9 @@ 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;
 } hostserv_conf;
 
 const char *hostserv_module_deps[] = { NULL };
@@ -81,9 +100,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 +131,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 */
@@ -169,7 +186,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 +206,22 @@ 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 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);
@@ -301,6 +329,35 @@ static void hs_del_manager(struct hs_manager *manager, int remove_from_object) {
     free(manager);
 }
 
+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 = assignment->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(assignment->user->hi, NULL);
+}
+
 static struct hs_assignment *hs_add_assignment(struct hs_secondlevel *slfh, struct hs_user *user) {
     struct hs_assignment *assignment = calloc(1, sizeof(*assignment));
     assignment->secondlevel = slfh;
@@ -312,7 +369,7 @@ static struct hs_assignment *hs_add_assignment(struct hs_secondlevel *slfh, stru
     assignment->unext = user->assignments;
     user->assignments = assignment;
     if(assignment->active) {
-        /* use this assignment */
+        hs_activate_assignment(user, assignment);
     }
     return assignment;
 }
@@ -333,6 +390,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 +401,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 +481,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 +495,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 +564,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], '.')) {
@@ -565,7 +646,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 +683,185 @@ 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 NULL;
+        }
+        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;
+        }
+        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_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, '.');
+        *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 +878,12 @@ static MODCMD_FUNC(cmd_delmanager) {
     }
     char *slfh_name = fakehost;
     char *tlfh_name = strchr(fakehost, '.');
-    *tlfh_name = '\0';
+    if(tlfh_name) {
+        *tlfh_name = '\0';
+    } else {
+        reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, "");
+        return 0;
+    }
     tlfh_name++;
     if(strchr(tlfh_name, '.')) {
         reply("HSMSG_UNKNOWN_FAKEHOST", slfh_name, tlfh_name);
@@ -701,17 +900,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 +943,194 @@ 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_assignment *assgn;
+    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 {
+        if(!strchr(argv[1], '.')) {
+            
+        }
+        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) {
+        *tlfh_name = '\0';
+    } else {
+        reply("HSMSG_UNKNOWN_FAKEHOST", 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;
+                    }
+                    hs_add_assignment(slfh, hs_user);
+                    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) {
+        *tlfh_name = '\0';
+    } else {
+        reply("HSMSG_UNKNOWN_FAKEHOST", 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, "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 +1156,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 +1189,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;
             }
         }
         
@@ -859,7 +1250,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 +1264,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 +1296,35 @@ 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);
+    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 +1338,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