Allow SQL to add clients to virtual servers
[freeradius.git] / src / main / client.c
index d8759fd..79e71c8 100644 (file)
@@ -202,17 +202,51 @@ int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
        DEBUG3("Adding client %s (%s) to prefix tree %i", buffer, client->longname, client->ipaddr.prefix);
 
        /*
-        *      If "clients" is NULL, it means add to the global list.
+        *      If "clients" is NULL, it means add to the global list,
+        *      unless we're trying to add it to a virtual server...
         */
        if (!clients) {
-               /*
-                *      Initialize it, if not done already.
-                */
-               if (!root_clients) {
-                       root_clients = clients_init(NULL);
-                       if (!root_clients) return 0;
+               if (client->server != NULL) {
+                       CONF_SECTION *cs;
+
+                       cs = cf_section_find_name2(main_config.config,
+                                                  "server", client->server);
+                       if (!cs) {
+                               radlog(L_ERR, "Failed to find virtual server %s",
+                                      client->server);
+                               return 0;
+                       }
+
+                       /*
+                        *      If the client list already exists, use that.
+                        *      Otherwise, create a new client list.
+                        */
+                       clients = cf_data_find(cs, "clients");
+                       if (!clients) {
+                               clients = clients_init(cs);
+                               if (!clients) {
+                                       radlog(L_ERR, "Out of memory");
+                                       return 0;
+                               }
+
+                               if (cf_data_add(cs, "clients", clients, (void *) clients_free) < 0) {
+                                       radlog(L_ERR, "Failed to associate clients with virtual server %s",
+                                              client->server);
+                                       clients_free(clients);
+                                       return 0;
+                               }
+                       }
+
+               } else {
+                       /*
+                        *      Initialize the global list, if not done already.
+                        */
+                       if (!root_clients) {
+                               root_clients = clients_init(NULL);
+                               if (!root_clients) return 0;
+                       }
+                       clients = root_clients;
                }
-               clients = root_clients;
        }
 
        /*