Allow utc. Patch from Peter Lambrechtsen
[freeradius.git] / src / modules / rlm_date / rlm_date.c
index c155a31..86a7d62 100644 (file)
@@ -1,7 +1,8 @@
 /*
  *   This program is is free software; you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License, version 2 if the
- *   License as published by the Free Software Foundation.
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or (at
+ *   your option) any later version.
  *
  *   This program is distributed in the hope that it will be useful,
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 #include <freeradius-devel/radiusd.h>
 #include <freeradius-devel/modules.h>
+#include <ctype.h>
 #include <time.h>
 
 typedef struct rlm_date_t {
        char const *xlat_name;
        char const *fmt;
+       bool utc;
 } rlm_date_t;
 
 static const CONF_PARSER module_config[] = {
-       {"format", PW_TYPE_STRING_PTR, offsetof(rlm_date_t, fmt), NULL, "%b %e %Y %H:%M:%S %Z"},
-       {NULL, -1, 0, NULL, NULL}
+       { "format", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_date_t, fmt), "%b %e %Y %H:%M:%S %Z" },
+       { "utc", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_date_t, utc), "no" },
+       CONF_PARSER_TERMINATOR
 };
 
 DIAG_OFF(format-nonliteral)
-static ssize_t xlat_date_convert(void *instance, UNUSED REQUEST *request, char const *fmt, char *out, size_t outlen)
+static ssize_t xlat_date_convert(void *instance, REQUEST *request, char const *fmt, char *out, size_t outlen)
 {
        rlm_date_t *inst = instance;
        time_t date = 0;
        struct tm tminfo;
        VALUE_PAIR *vp;
 
+       memset(&tminfo, 0, sizeof(tminfo));
+
        if ((radius_get_vp(&vp, request, fmt) < 0) || !vp) {
                *out = '\0';
                return 0;
@@ -65,9 +71,16 @@ static ssize_t xlat_date_convert(void *instance, UNUSED REQUEST *request, char c
                date = (time_t) vp->vp_integer;
 
        encode:
-               if (localtime_r(&date, &tminfo) == NULL) {
-                       REDEBUG("Failed converting time string to localtime");
-                       goto error;
+               if (!inst->utc) {
+                       if (localtime_r(&date, &tminfo) == NULL) {
+                               REDEBUG("Failed converting time string to localtime");
+                               goto error;
+                       }
+               } else {
+                       if (gmtime_r(&date, &tminfo) == NULL) {
+                               REDEBUG("Failed converting time string to gmtime");
+                               goto error;
+                       }
                }
                return strftime(out, outlen, inst->fmt, &tminfo);
 
@@ -99,7 +112,7 @@ static ssize_t xlat_date_convert(void *instance, UNUSED REQUEST *request, char c
 }
 DIAG_ON(format-nonliteral)
 
-static int mod_instantiate(CONF_SECTION *conf, void *instance)
+static int mod_bootstrap(CONF_SECTION *conf, void *instance)
 {
        rlm_date_t *inst = instance;
 
@@ -113,19 +126,12 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
        return 0;
 }
 
+extern module_t rlm_date;
 module_t rlm_date = {
-       RLM_MODULE_INIT,
-       "date",                         /* Name */
-       RLM_TYPE_CHECK_CONFIG_SAFE,     /* type */
-       sizeof(rlm_date_t),
-       module_config,
-       mod_instantiate,                /* instantiation */
-       NULL,                           /* detach */
-       {
-               NULL,                   /* authentication */
-               NULL,                   /* authorization */
-               NULL,                   /* pre-accounting */
-               NULL                    /* accounting */
-       },
+       .magic          = RLM_MODULE_INIT,
+       .name           = "date",
+       .inst_size      = sizeof(rlm_date_t),
+       .config         = module_config,
+       .bootstrap      = mod_bootstrap
 };