Fix allocate_find query. Addresses #786
authorAlan T. DeKok <aland@freeradius.org>
Wed, 10 Sep 2014 12:49:08 +0000 (08:49 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Wed, 10 Sep 2014 12:49:08 +0000 (08:49 -0400)
raddb/mods-config/sql/ippool-dhcp/mysql/queries.conf

index ada0018..0a5d7ef 100644 (file)
@@ -35,17 +35,24 @@ allocate_clear = "\
        AND nasipaddress = '%{Nas-IP-Address}'"
 
 #
-#  The ORDER BY clause of this query tries to allocate the same IP-address
-#  which user had last session...
+#  Search for the SAME Calling-Station-ID as last time, OR an entry
+#  where there is no Calling-Station-Id.
+#
+#  If the lease was expired, the allocate_clear above will reset the
+#  Calling-Station-Id to ''.
+#
+#  If the lease wasn't expired, we want to give the user the same IP
+#  as last time.
+#
+#  Then, we order by expiry_time, so that when there is no existing
+#  entry for the Calling-Station-Id, it picks the OLDEST expired IP.
 #
 allocate_find = "\
        SELECT framedipaddress \
        FROM ${ippool_table} \
        WHERE pool_name = '%{control:Pool-Name}' \
-       AND (expiry_time < NOW() OR expiry_time IS NULL) \
+       AND (callingstationid = '%{Calling-Station-Id}' or callingstationid = '') \
        ORDER BY \
-               (username <> '%{User-Name}'), \
-               (callingstationid <> '%{Calling-Station-Id}'), \
                expiry_time \
        LIMIT 1 \
        FOR UPDATE"