#include "k5-int.h"
#include <ctype.h>
struct salttype_lookup_entry {
krb5_int32 stt_enctype;
const char * stt_name;
};
#include "kdb.h"
static const struct salttype_lookup_entry salttype_table[] = {
{ KRB5_KDB_SALTTYPE_NORMAL, "normal" },
{ KRB5_KDB_SALTTYPE_NOREALM, "norealm", },
{ KRB5_KDB_SALTTYPE_ONLYREALM, "onlyrealm", },
{ KRB5_KDB_SALTTYPE_SPECIAL, "special", },
};
static const int salttype_table_nents = sizeof(salttype_table)/
sizeof(salttype_table[0]);
krb5_error_code KRB5_CALLCONV
krb5_string_to_salttype(char *string, krb5_int32 *salttypep)
{
int i;
int found;
found = 0;
for (i=0; i<salttype_table_nents; i++) {
if (!strcasecmp(string, salttype_table[i].stt_name)) {
found = 1;
*salttypep = salttype_table[i].stt_enctype;
break;
}
}
return((found) ? 0 : EINVAL);
}
krb5_error_code KRB5_CALLCONV
krb5_salttype_to_string(krb5_int32 salttype, char *buffer, size_t buflen)
{
int i;
const char *out;
out = (char *) NULL;
for (i=0; i<salttype_table_nents; i++) {
if (salttype == salttype_table[i].stt_enctype) {
out = salttype_table[i].stt_name;
break;
}
}
if (out) {
if (strlcpy(buffer, out, buflen) >= buflen)
return(ENOMEM);
return(0);
}
else
return(EINVAL);
}
#ifdef HAVE_STRPTIME
#ifdef NEED_STRPTIME_PROTO
extern char *strptime (const char *, const char *,
struct tm *)
#ifdef __cplusplus
throw()
#endif
;
#endif
#else
#undef strptime
#define strptime my_strptime
static char *strptime (const char *, const char *, struct tm *);
#endif
#ifndef HAVE_LOCALTIME_R
static inline struct tm *
localtime_r(const time_t *t, struct tm *buf)
{
struct tm *tm = localtime(t);
if (tm == NULL)
return NULL;
*buf = *tm;
return buf;
}
#endif
krb5_error_code KRB5_CALLCONV
krb5_string_to_timestamp(char *string, krb5_timestamp *timestampp)
{
int i;
struct tm timebuf, timebuf2;
time_t now, ret_time;
char *s;
static const char * const atime_format_table[] = {
"%Y%m%d%H%M%S",
"%Y.%m.%d.%H.%M.%S",
"%y%m%d%H%M%S",
"%y.%m.%d.%H.%M.%S",
"%y%m%d%H%M",
"%H%M%S",
"%H%M",
"%T",
"%R",
"%x:%X",
"%d-%b-%Y:%T",
"%d-%b-%Y:%R"
};
static const int atime_format_table_nents =
sizeof(atime_format_table)/sizeof(atime_format_table[0]);
now = time((time_t *) NULL);
if (localtime_r(&now, &timebuf2) == NULL)
return EINVAL;
for (i=0; i<atime_format_table_nents; i++) {
timebuf = timebuf2;
if ((s = strptime(string, atime_format_table[i], &timebuf))
&& (s != string)) {
while(*s != 0 && isspace((int) *s)) s++;
if (*s != 0)
continue;
if (timebuf.tm_year <= 0)
continue;
ret_time = mktime(&timebuf);
if (ret_time == (time_t) -1)
continue;
*timestampp = (krb5_timestamp) ret_time;
return 0;
}
}
return(EINVAL);
}
krb5_error_code KRB5_CALLCONV
krb5_timestamp_to_string(krb5_timestamp timestamp, char *buffer, size_t buflen)
{
size_t ret;
time_t timestamp2 = ts2tt(timestamp);
struct tm tmbuf;
const char *fmt = "%c";
if (localtime_r(×tamp2, &tmbuf) == NULL)
return(ENOMEM);
ret = strftime(buffer, buflen, fmt, &tmbuf);
if (ret == 0 || ret == buflen)
return(ENOMEM);
return(0);
}
krb5_error_code KRB5_CALLCONV
krb5_timestamp_to_sfstring(krb5_timestamp timestamp, char *buffer, size_t buflen, char *pad)
{
struct tm *tmp;
size_t i;
size_t ndone;
time_t timestamp2 = ts2tt(timestamp);
struct tm tmbuf;
static const char * const sftime_format_table[] = {
"%c",
"%d %b %Y %T",
"%x %X",
"%x %T",
"%x %R",
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%dT%H:%M",
"%Y%m%d%H%M%S",
"%Y%m%d%H%M"
};
static const unsigned int sftime_format_table_nents =
sizeof(sftime_format_table)/sizeof(sftime_format_table[0]);
tmp = localtime_r(×tamp2, &tmbuf);
if (tmp == NULL)
return errno;
ndone = 0;
for (i=0; i<sftime_format_table_nents; i++) {
if ((ndone = strftime(buffer, buflen, sftime_format_table[i], tmp)))
break;
}
if (ndone && pad) {
for (i=ndone; i<buflen-1; i++)
buffer[i] = *pad;
buffer[buflen-1] = '\0';
}
return((ndone) ? 0 : ENOMEM);
}
krb5_error_code KRB5_CALLCONV
krb5_deltat_to_string(krb5_deltat deltat, char *buffer, size_t buflen)
{
int days, hours, minutes, seconds;
krb5_deltat dt;
days = (int) (deltat / (24*3600L));
dt = deltat % (24*3600L);
hours = (int) (dt / 3600);
dt %= 3600;
minutes = (int) (dt / 60);
seconds = (int) (dt % 60);
if (days == 0)
snprintf(buffer, buflen, "%d:%02d:%02d", hours, minutes, seconds);
else if (hours || minutes || seconds)
snprintf(buffer, buflen, "%d %s %02d:%02d:%02d", days,
(days > 1) ? "days" : "day",
hours, minutes, seconds);
else
snprintf(buffer, buflen, "%d %s", days,
(days > 1) ? "days" : "day");
return 0;
}
#undef __P
#define __P(X) X
#ifndef HAVE_STRPTIME
#undef _CurrentTimeLocale
#define _CurrentTimeLocale (&dummy_locale_info)
struct dummy_locale_info_t {
char d_t_fmt[15];
char t_fmt_ampm[12];
char t_fmt[9];
char d_fmt[9];
char day[7][10];
char abday[7][4];
char mon[12][10];
char abmon[12][4];
char am_pm[2][3];
};
static const struct dummy_locale_info_t dummy_locale_info = {
"%a %b %d %X %Y",
"%I:%M:%S %p",
"%H:%M:%S",
"%m/%d/%y",
{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday" },
{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" },
{ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" },
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" },
{ "AM", "PM" },
};
#undef TM_YEAR_BASE
#define TM_YEAR_BASE 1900
#include "strptime.c"
#endif