ldb: lay foundation for proper utc/generalized time handling
authorMatthieu Patou <mat@matws.net>
Fri, 18 May 2012 06:57:55 +0000 (23:57 -0700)
committerMatthieu Patou <mat@matws.net>
Sat, 23 Jun 2012 06:22:03 +0000 (23:22 -0700)
We use to handle UTCtime and generalized time the same way. The thing is
that it's not the case, they are different in the way they are set (most
of the time) with different format and also stored and return in
different format too.

lib/ldb/common/attrib_handlers.c
lib/ldb/common/ldb_msg.c
lib/ldb/include/ldb.h

index 73e1705593bf1d9db2bb3b88f3a3e82723855696..daeb422aca3c6c54a43dfb6367fb106048724b87 100644 (file)
@@ -380,6 +380,27 @@ static int ldb_comparison_utctime(struct ldb_context *ldb, void *mem_ctx,
 */
 static int ldb_canonicalise_utctime(struct ldb_context *ldb, void *mem_ctx,
                                    const struct ldb_val *in, struct ldb_val *out)
+{
+       time_t t;
+       int ret;
+       ret = ldb_val_to_time(in, &t);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+       out->data = (uint8_t *)ldb_timestring_utc(mem_ctx, t);
+       if (out->data == NULL) {
+               ldb_oom(ldb);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       out->length = strlen((char *)out->data);
+       return 0;
+}
+
+/*
+  canonicalise a generalized time
+*/
+static int ldb_canonicalise_generalizedtime(struct ldb_context *ldb, void *mem_ctx,
+                                       const struct ldb_val *in, struct ldb_val *out)
 {
        time_t t;
        int ret;
@@ -443,6 +464,13 @@ static const struct ldb_schema_syntax ldb_standard_syntaxes[] = {
                .comparison_fn   = ldb_comparison_utctime
        },
        { 
+               .name            = LDB_SYNTAX_GENERALIZED_TIME,
+               .ldif_read_fn    = ldb_handler_copy,
+               .ldif_write_fn   = ldb_handler_copy,
+               .canonicalise_fn = ldb_canonicalise_generalizedtime,
+               .comparison_fn   = ldb_comparison_utctime
+       },
+       {
                .name            = LDB_SYNTAX_BOOLEAN,
                .ldif_read_fn    = ldb_handler_copy,
                .ldif_write_fn   = ldb_handler_copy,
index 35c568a077b7278dcbc6608b89197e222efcb972..809e3af8193a9b7def5356db04220f5c5603a20f 100644 (file)
@@ -1092,16 +1092,24 @@ int ldb_val_to_time(const struct ldb_val *v, time_t *t)
 {
        struct tm tm;
 
-       if (v == NULL || !v->data || v->length < 17) {
+       if (v == NULL || !v->data || (v->length != 17 && v->length != 13)) {
                return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
        }
 
        memset(&tm, 0, sizeof(tm));
 
-       if (sscanf((char *)v->data, "%04u%02u%02u%02u%02u%02u.0Z",
-                  &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
-                  &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
-               return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
+       if (v->length == 13) {
+               if (sscanf((char *)v->data, "%02u%02u%02u%02u%02u%02uZ",
+                       &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
+                       &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
+                       return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
+               }
+       } else {
+               if (sscanf((char *)v->data, "%04u%02u%02u%02u%02u%02u.0Z",
+                       &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
+                       &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
+                       return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
+               }
        }
        tm.tm_year -= 1900;
        tm.tm_mon -= 1;
index d3a20c5ff8e22c4c3e6bcad586a8ba28717fdc76..b60fc9b5ebb94096fa823fe8981aa57998a27bea 100644 (file)
@@ -475,6 +475,7 @@ const struct ldb_dn_extended_syntax *ldb_dn_extended_syntax_by_name(struct ldb_c
   See <a href="http://www.ietf.org/rfc/rfc2252.txt">RFC 2252</a>, Section 4.3.2 
 */
 #define LDB_SYNTAX_UTC_TIME             "1.3.6.1.4.1.1466.115.121.1.53"
+#define LDB_SYNTAX_GENERALIZED_TIME     "1.3.6.1.4.1.1466.115.121.1.24"
 
 #define LDB_SYNTAX_OBJECTCLASS          "LDB_SYNTAX_OBJECTCLASS"