s4-ldb: added ldb_val_to_time()
authorAndrew Tridgell <tridge@samba.org>
Tue, 29 Dec 2009 00:36:37 +0000 (11:36 +1100)
committerAndrew Tridgell <tridge@samba.org>
Fri, 1 Jan 2010 21:16:55 +0000 (08:16 +1100)
This is intended as a replacement for ldb_string_to_time() for ldb_val
inputs. This ensures it is length limited and includes additional
validity checks

source4/lib/ldb/common/ldb_msg.c

index fbf49fbb237583c85f30ed3c6096fb870d23db1b..9b33d7e3519c3e088ee610a6f819894a73e0f937 100644 (file)
@@ -831,6 +831,33 @@ time_t ldb_string_to_time(const char *s)
        return timegm(&tm);
 }
 
+/*
+  convert a LDAP GeneralizedTime string in ldb_val format to a
+  time_t.
+*/
+int ldb_val_to_time(const struct ldb_val *v, time_t *t)
+{
+       struct tm tm;
+
+       if (v == NULL || !v->data || v->length < 14) {
+               return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
+       }
+
+       memset(&tm, 0, sizeof(tm));
+
+       if (sscanf((char *)v->data, "%04u%02u%02u%02u%02u%02u",
+                  &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;
+
+       *t = timegm(&tm);
+
+       return LDB_SUCCESS;
+}
+
 /*
   return a LDAP formatted UTCTime string
 */