fixed #0000071 (first assignment always got active on reload)
[srvx.git] / src / mod-hostserv.c
index 4d16996bfa6d33cd41d85a8908104a5bee100ae6..b865afc52d746851c6b375ed8d25a3c4f9827b44 100644 (file)
 #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:" },
@@ -168,7 +172,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);
             }
@@ -342,7 +346,7 @@ static void hs_activate_assignment(struct hs_user *user, struct hs_assignment *a
     assert((!assignment || (assignment->user == user)));
     
     if(user->assignments) {
-        for(assgn = assignment->user->assignments; assgn; assgn = assgn->unext)
+        for(assgn = user->assignments; assgn; assgn = assgn->unext)
             assgn->active = 0;
     }
     
@@ -359,15 +363,17 @@ static void hs_activate_assignment(struct hs_user *user, struct hs_assignment *a
         assignment->active = 1;
     }
     
-    apply_fakehost(assignment->user->hi, NULL);
+    apply_fakehost(user->hi, NULL);
 }
 
-static struct hs_assignment *hs_add_assignment(struct hs_secondlevel *slfh, struct hs_user *user) {
+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;
@@ -969,9 +975,6 @@ static MODCMD_FUNC(cmd_set) {
         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, '.');
@@ -1041,7 +1044,7 @@ static MODCMD_FUNC(cmd_assign) {
                         reply("HSMSG_FAKEHOST_ASSIGNED", slfh_name, tlfh_name);
                         return 0;
                     }
-                    hs_add_assignment(slfh, hs_user);
+                    hs_add_assignment(slfh, hs_user, HS_ASSIGNMENTSTATE_AUTO);
                     reply("HSMSG_FAKEHOST_ASSIGN_SUCCESS", slfh_name, tlfh_name);
                     return 1;
                 }
@@ -1212,18 +1215,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;