]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
move timestr_match() to better API
authorAlan T. DeKok <aland@freeradius.org>
Mon, 11 Oct 2021 16:41:23 +0000 (12:41 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Mon, 11 Oct 2021 17:03:34 +0000 (13:03 -0400)
src/modules/rlm_logintime/rlm_logintime.c
src/modules/rlm_logintime/timestr.c

index d70e5fbb4b2eedf794638588d4e7f1f6672c7382..2095436412ff2eddfc62680fdc442e61b711bd06 100644 (file)
@@ -30,7 +30,7 @@ RCSID("$Id$")
 #include <ctype.h>
 
 /* timestr.c */
-fr_time_delta_t        timestr_match(char const *, fr_time_t);
+int timestr_match(fr_time_delta_t *out, char const *tmstr, fr_time_t when);
 
 /*
  *     Define a structure for our module configuration.
@@ -80,10 +80,14 @@ fr_dict_attr_autoload_t rlm_logintime_dict_attr[] = {
  */
 static int timecmp(UNUSED void *instance, request_t *request, UNUSED fr_pair_list_t *request_list, fr_pair_t const *check)
 {
+       fr_time_delta_t left;
+
+       if (timestr_match(&left, check->vp_strvalue, request->packet->timestamp) < 0) return -1;
+
        /*
-        *      If there's a request, use that timestamp.
+        *      0 is a special case meaning "allowed".
         */
-       if (fr_time_delta_gteq(timestr_match(check->vp_strvalue, request->packet->timestamp), fr_time_delta_wrap(0))) return 0;
+       if (fr_time_delta_gteq(left, fr_time_delta_wrap(0))) return 0;
 
        return -1;
 }
@@ -166,11 +170,12 @@ static unlang_action_t CC_HINT(nonnull) mod_authorize(rlm_rcode_t *p_result, mod
        /*
         *      Compare the time the request was received with the current Login-Time value
         */
-       left = timestr_match(ends->vp_strvalue, request->packet->timestamp);
-       if (fr_time_delta_isneg(left)) RETURN_MODULE_DISALLOW; /* outside of the allowed time */
+       if (timestr_match(&left, ends->vp_strvalue, request->packet->timestamp) < 0) {
+               RETURN_MODULE_DISALLOW; /* outside of the allowed time */
+       }
 
        /*
-        *      Do nothing, login time is not controlled (unendsed).
+        *      Do nothing, login time is not controlled (unended).
         */
        if (fr_time_delta_eq(left, fr_time_delta_wrap(0))) RETURN_MODULE_OK;
 
@@ -190,7 +195,7 @@ static unlang_action_t CC_HINT(nonnull) mod_authorize(rlm_rcode_t *p_result, mod
        /* else left > inst->min_time */
 
        /*
-        *      There's time left in the users session, inform the NAS by including a Session-vp
+        *      There's time left in the users session, inform the NAS by including a Session-Timeout
         *      attribute in the reply, or modifying the existing one.
         */
        RDEBUG2("Login within allowed time-slot, %d seconds left in this session", (int) fr_time_delta_to_sec(left));
index 6ff54ed05302b0cc5d6f2df9468b88d143dbe0e7..009ac71271b4e73ffd2dc2bb2bb7915c20524c2d 100644 (file)
@@ -27,7 +27,7 @@ RCSID("$Id$")
 
 #include <ctype.h>
 
-fr_time_delta_t        timestr_match(char const *, fr_time_t);
+int timestr_match(fr_time_delta_t *out, char const *tmstr, fr_time_t when);
 
 static char const *days[] =
        { "su", "mo", "tu", "we", "th", "fr", "sa", "wk", "any", "al" };
@@ -193,14 +193,14 @@ static int week_fill(char *bitmap, char const *tm)
 }
 
 /*
- *     Match a timestring and return time left.
- *     -1 for no match, 0 for unlimited.
+ *     Match a time string, and return time left in `out`.
+ *     -1 for no match
  */
-fr_time_delta_t timestr_match(char const *tmstr, fr_time_t when)
+int timestr_match(fr_time_delta_t *out, char const *tmstr, fr_time_t when)
 {
        struct tm *tm, s_tm;
        char bitmap[WEEKMIN / 8];
-       int now, tot, i;
+       int64_t now, tot, i;
        int byte, bit;
 #ifdef do_timestr_debug
        int y;
@@ -245,11 +245,15 @@ fr_time_delta_t timestr_match(char const *tmstr, fr_time_t when)
                        break;
        }
 
-       if (!tot) return fr_time_delta_wrap(-1);
+       if (!tot) return -1;
 
-       if (i == now) return fr_time_delta_wrap(0);
+       if (i == now) {
+               *out = fr_time_delta_wrap(0);
+               return 0;
+       }
 
-       return fr_time_delta_from_sec(tot);
+       *out = fr_time_delta_wrap(tot);
+       return 0;
 }
 
 #ifdef STANDALONE