#include "auditr.h"
#include <locale.h>
#include <libintl.h>
int derive_date(char *, struct tm *);
void derive_str(time_t, char *);
int parse_time(char *, int);
time_t tm_to_secs(struct tm *);
static int check_time(struct tm *);
static int days_in_year(int);
static char *do_invalid(void);
static time_t local_to_gm(struct tm *);
static char *invalid_inter = NULL;
static int days_month[] = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
char *
do_invalid(void)
{
if (invalid_inter == NULL)
invalid_inter = gettext("invalid date/time format -");
return (invalid_inter);
}
time_t
local_to_gm(struct tm *tme)
{
time_t secs, gsecs, lsecs, save_gsecs;
time_t r1secs, r2secs;
struct tm ltime, gtime;
r1secs = secs = tm_to_secs(tme);
(void) memcpy((void *)>ime, (void *)gmtime(&secs), sizeof (gtime));
(void) memcpy((void *)<ime, (void *)localtime(&secs), sizeof (ltime));
gsecs = tm_to_secs(>ime);
lsecs = tm_to_secs(<ime);
secs = lsecs - gsecs;
gsecs -= secs;
(void) memcpy((void *)<ime, (void *)localtime(&gsecs),
sizeof (ltime));
save_gsecs = gsecs;
r2secs = tm_to_secs(<ime);
if (r1secs != r2secs) {
if (r2secs > r1secs) {
gsecs -= r2secs - r1secs;
} else {
gsecs += r1secs - r2secs;
}
(void) memcpy((void *)<ime, (void *)localtime(&gsecs),
sizeof (ltime));
r2secs = tm_to_secs(<ime);
if (r1secs != r2secs) {
gsecs = save_gsecs;
}
}
return (gsecs);
}
time_t
tm_to_secs(struct tm *tme)
{
int leap_year = FALSE;
int days = 0;
time_t num_sec = 0;
int sec = tme->tm_sec;
int min = tme->tm_min;
int hour = tme->tm_hour;
int day = tme->tm_mday;
int month = tme->tm_mon;
int year = tme->tm_year + 1900;
if (days_in_year(year) == 366)
leap_year = TRUE;
while (year > 1970) {
num_sec += days_in_year(--year) * 24 * 60 * 60;
}
while (month > 0) {
days = days_month[--month];
if (leap_year && month == 1) {
days++;
}
num_sec += days * 24 * 60 * 60;
}
num_sec += --day * 24 * 60 * 60;
num_sec += hour * 60 * 60;
num_sec += min * 60;
num_sec += sec;
return (num_sec);
}
int
check_time(struct tm *tme)
{
error_str = NULL;
if (tme->tm_sec < 0 || tme->tm_sec > 59) {
(void) sprintf(errbuf,
gettext("seconds out of range (%d)"), tme->tm_sec + 1);
error_str = errbuf;
} else if (tme->tm_min < 0 || tme->tm_min > 59) {
(void) sprintf(errbuf,
gettext("minutes out of range (%d)"), tme->tm_min + 1);
error_str = errbuf;
} else if (tme->tm_hour < 0 || tme->tm_hour > 23) {
(void) sprintf(errbuf,
gettext("hours out of range (%d)"), tme->tm_hour + 1);
error_str = errbuf;
} else if (tme->tm_mon < 0 || tme->tm_mon > 11) {
(void) sprintf(errbuf,
gettext("months out of range (%d)"), tme->tm_mon + 1);
error_str = errbuf;
} else if (tme->tm_year < 0) {
(void) sprintf(errbuf,
gettext("years out of range (%d)"), tme->tm_year);
error_str = errbuf;
} else if (tme->tm_mday < 1 || tme->tm_mday > days_month[tme->tm_mon]) {
if (!(days_in_year(tme->tm_year + 1900) == 366 &&
tme->tm_mon == 1 &&
tme->tm_mday == 29)) {
(void) sprintf(errbuf,
gettext("days out of range (%d)"), tme->tm_mday);
error_str = errbuf;
}
} else if (tme->tm_wday < 0 || tme->tm_wday > 6) {
(void) sprintf(errbuf,
gettext("weekday out of range (%d)"), tme->tm_wday);
error_str = errbuf;
} else if (tme->tm_yday < 0 || tme->tm_yday > 365) {
(void) sprintf(errbuf,
gettext("day of year out of range (%d)"), tme->tm_yday);
error_str = errbuf;
}
if (error_str == NULL)
return (0);
else
return (-1);
}
int
parse_time(char *str, int opt)
{
int ret, len, factor;
char *strxx;
long lnum;
struct tm thentime;
len = strlen(str);
if (len < 6) {
if (*str++ != '+') {
(void) sprintf(errbuf, gettext("%s needs '+' (%s)"),
do_invalid(), str);
error_str = errbuf;
return (-1);
}
if (opt != 'b') {
(void) sprintf(errbuf,
gettext("%s only allowed with 'b' option (%s)"),
do_invalid(), str);
error_str = errbuf;
return (-1);
}
if (m_after == 0) {
(void) sprintf(errbuf,
gettext("must have -a to use -b +nx form (%s)"),
str);
error_str = errbuf;
return (-1);
}
if ((strxx = strpbrk(str, "dhms")) == NULL) {
(void) sprintf(errbuf,
gettext("%s needs 'd', 'h', 'm', or 's' (%s)"),
do_invalid(), str);
error_str = errbuf;
return (-1);
} else {
ret = *strxx;
*strxx = '\0';
}
if (strlen(str) != strspn(str, "0123456789")) {
(void) sprintf(errbuf,
gettext("%s non-numeric offset (%s)"),
do_invalid(), str);
error_str = errbuf;
return (-1);
}
factor = 1;
if (ret == 'd')
factor = 24 * 60 * 60;
else if (ret == 'h')
factor = 60 * 60;
else if (ret == 'm')
factor = 60;
lnum = atol(str);
m_before = m_after + (lnum * factor);
return (0);
}
if (derive_date(str, &thentime))
return (-1);
if (opt == 'd') {
thentime.tm_sec = 0;
thentime.tm_min = 0;
thentime.tm_hour = 0;
m_after = local_to_gm(&thentime);
m_before = m_after + (24 * 60 * 60);
} else if (opt == 'a') {
m_after = local_to_gm(&thentime);
} else if (opt == 'b') {
m_before = local_to_gm(&thentime);
}
return (0);
}
int
derive_date(char *str, struct tm *tme)
{
char *strs;
char *digits = "0123456789";
size_t len;
struct tm nowtime;
len = strlen(str);
if (len != strspn(str, digits)) {
(void) sprintf(errbuf, gettext("%s not all digits (%s)"),
do_invalid(), str);
error_str = errbuf;
return (-1);
}
if (len % 2) {
(void) sprintf(errbuf, gettext("%s odd number of digits (%s)"),
do_invalid(), str);
error_str = errbuf;
return (-1);
}
strs = (char *)a_calloc(1, len + 4);
(void) memcpy((char *)&nowtime, (char *)gmtime(&time_now),
sizeof (nowtime));
if ((str[0] != '1' || str[1] != '9') &&
(str[0] != '2' || str[1] != '0')) {
(void) sprintf(errbuf, gettext("invalid year (%c%c%c%c)"),
str[0], str[1], str[2], str[3]);
error_str = errbuf;
free(strs);
return (-1);
}
len = strlen(str);
if (len < 8 || len > 14) {
(void) sprintf(errbuf,
gettext("invalid date/time length (%s)"), str);
error_str = errbuf;
free(strs);
return (-1);
}
(void) memset((void *) tme, 0, (size_t)sizeof (*tme));
(void) strncpy(strs, str, 4);
strs[4] = '\0';
tme->tm_year = atoi(strs) - 1900;
(void) strncpy(strs, str + 4, 2);
strs[2] = '\0';
tme->tm_mon = atoi(strs) - 1;
(void) strncpy(strs, str + 6, 2);
strs[2] = '\0';
tme->tm_mday = atoi(strs);
if (len >= 10) {
(void) strncpy(strs, str + 8, 2);
strs[2] = '\0';
tme->tm_hour = atoi(strs);
}
if (len >= 12) {
(void) strncpy(strs, str + 10, 2);
strs[2] = '\0';
tme->tm_min = atoi(strs);
}
if (len >= 14) {
(void) strncpy(strs, str + 12, 2);
strs[2] = '\0';
tme->tm_sec = atoi(strs);
}
free(strs);
return (check_time(tme));
}
void
derive_str(time_t clock, char *buf)
{
struct tm gtime;
(void) memcpy((void *) & gtime, (void *)gmtime(&clock), sizeof (gtime));
(void) sprintf(buf, "%4d", gtime.tm_year + 1900);
(void) sprintf(buf + 4, "%.2d", gtime.tm_mon + 1);
(void) sprintf(buf + 6, "%.2d", gtime.tm_mday);
(void) sprintf(buf + 8, "%.2d", gtime.tm_hour);
(void) sprintf(buf + 10, "%.2d", gtime.tm_min);
(void) sprintf(buf + 12, "%.2d", gtime.tm_sec);
buf[14] = '\0';
}
int
days_in_year(int year)
{
if (isleap(year))
return (366);
return (365);
}