1 /* 2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 #pragma ident "%Z%%M% %I% %E% SMI" 7 8 /* 9 * lib/kadm/str_conv.c 10 * 11 * Copyright 1995, 1999 by the Massachusetts Institute of Technology. 12 * All Rights Reserved. 13 * 14 * Export of this software from the United States of America may 15 * require a specific license from the United States Government. 16 * It is the responsibility of any person or organization contemplating 17 * export to obtain such a license before exporting. 18 * 19 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 20 * distribute this software and its documentation for any purpose and 21 * without fee is hereby granted, provided that the above copyright 22 * notice appear in all copies and that both that copyright notice and 23 * this permission notice appear in supporting documentation, and that 24 * the name of M.I.T. not be used in advertising or publicity pertaining 25 * to distribution of the software without specific, written prior 26 * permission. Furthermore if you modify this software you must label 27 * your software as modified software and not distribute it in such a 28 * fashion that it might be confused with the original M.I.T. software. 29 * M.I.T. makes no representations about the suitability of 30 * this software for any purpose. It is provided "as is" without express 31 * or implied warranty. 32 * 33 */ 34 35 /* 36 * str_conv.c - Convert between strings and Kerberos internal data. 37 */ 38 39 /* 40 * Table of contents: 41 * 42 * String decoding: 43 * ---------------- 44 * krb5_string_to_salttype() - Convert string to salttype (krb5_int32) 45 * krb5_string_to_timestamp() - Convert string to krb5_timestamp. 46 * krb5_string_to_deltat() - Convert string to krb5_deltat. 47 * 48 * String encoding: 49 * ---------------- 50 * krb5_salttype_to_string() - Convert salttype (krb5_int32) to string. 51 * krb5_timestamp_to_string() - Convert krb5_timestamp to string. 52 * krb5_timestamp_to_sfstring() - Convert krb5_timestamp to short filled string 53 * krb5_deltat_to_string() - Convert krb5_deltat to string. 54 */ 55 56 #include "k5-int.h" 57 #include <ctype.h> 58 59 /* Salt type conversions */ 60 61 /* 62 * Local data structures. 63 */ 64 struct salttype_lookup_entry { 65 krb5_int32 stt_enctype; /* Salt type */ 66 const char * stt_specifier; /* How to recognize it */ 67 const char * stt_output; /* How to spit it out */ 68 }; 69 70 /* 71 * Lookup tables. 72 */ 73 74 static const struct salttype_lookup_entry salttype_table[] = { 75 /* salt type input specifier output string */ 76 /*----------------------------- --------------- ---------------*/ 77 { KRB5_KDB_SALTTYPE_NORMAL, "normal", "Version 5" }, 78 { KRB5_KDB_SALTTYPE_V4, "v4", "Version 4" }, 79 { KRB5_KDB_SALTTYPE_NOREALM, "norealm", "Version 5 - No Realm" }, 80 { KRB5_KDB_SALTTYPE_ONLYREALM, "onlyrealm", "Version 5 - Realm Only" }, 81 { KRB5_KDB_SALTTYPE_SPECIAL, "special", "Special" }, 82 { KRB5_KDB_SALTTYPE_AFS3, "afs3", "AFS version 3" } 83 }; 84 static const int salttype_table_nents = sizeof(salttype_table)/ 85 sizeof(salttype_table[0]); 86 87 krb5_error_code KRB5_CALLCONV 88 krb5_string_to_salttype(char *string, krb5_int32 *salttypep) 89 { 90 int i; 91 int found; 92 93 found = 0; 94 for (i=0; i<salttype_table_nents; i++) { 95 if (!strcasecmp(string, salttype_table[i].stt_specifier)) { 96 found = 1; 97 *salttypep = salttype_table[i].stt_enctype; 98 break; 99 } 100 } 101 return((found) ? 0 : EINVAL); 102 } 103 104 /* 105 * Internal datatype to string routines. 106 * 107 * These routines return 0 for success, EINVAL for invalid parameter, ENOMEM 108 * if the supplied buffer/length will not contain the output. 109 */ 110 krb5_error_code KRB5_CALLCONV 111 krb5_salttype_to_string(krb5_int32 salttype, char *buffer, size_t buflen) 112 { 113 int i; 114 const char *out; 115 116 out = (char *) NULL; 117 for (i=0; i<salttype_table_nents; i++) { 118 if (salttype == salttype_table[i].stt_enctype) { 119 out = salttype_table[i].stt_output; 120 break; 121 } 122 } 123 if (out) { 124 if (buflen > strlen(out)) 125 strcpy(buffer, out); 126 else 127 out = (char *) NULL; 128 return((out) ? 0 : ENOMEM); 129 } 130 else 131 return(EINVAL); 132 } 133 134 /* (absolute) time conversions */ 135 136 #ifndef HAVE_STRFTIME 137 #undef strftime 138 #define strftime my_strftime 139 static size_t strftime (char *, size_t, const char *, const struct tm *); 140 #endif 141 142 #ifdef HAVE_STRPTIME 143 #ifdef NEED_STRPTIME_PROTO 144 extern char *strptime (const char *, const char *, 145 struct tm *) 146 #ifdef __cplusplus 147 throw() 148 #endif 149 ; 150 #endif 151 #else /* HAVE_STRPTIME */ 152 #undef strptime 153 #define strptime my_strptime 154 static char *strptime (const char *, const char *, struct tm *); 155 #endif 156 157 krb5_error_code KRB5_CALLCONV 158 krb5_string_to_timestamp(char *string, krb5_timestamp *timestampp) 159 { 160 int i; 161 struct tm timebuf; 162 time_t now, ret_time; 163 char *s; 164 /* 165 * Solaris Kerberos: 166 * The format strings shouldn't conflict with SCCS keywords 167 */ 168 static const char * const atime_format_table[] = { 169 "%Y" "%m" "%d" "%H" "%M" "%S", /* yyyymmddhhmmss */ 170 "%Y.%m.%d.%H.%M.%S", /* yyyy.mm.dd.hh.mm.ss */ 171 "%y" "%m" "%d" "%H" "%M" "%S", /* yymmddhhmmss */ 172 "%y.%m.%d.%H.%M.%S", /* yy.mm.dd.hh.mm.ss */ 173 "%y" "%m" "%d" "%H" "%M", /* yymmddhhmm */ 174 "%H" "%M" "%S", /* hhmmss */ 175 "%H" "%M", /* hhmm */ 176 "%T", /* hh:mm:ss */ 177 "%R", /* hh:mm */ 178 /* The following not really supported unless native strptime present */ 179 "%x:%X", /* locale-dependent short format */ 180 "%d-%b-%Y:%T", /* dd-month-yyyy:hh:mm:ss */ 181 "%d-%b-%Y:%R" /* dd-month-yyyy:hh:mm */ 182 }; 183 static const int atime_format_table_nents = 184 sizeof(atime_format_table)/sizeof(atime_format_table[0]); 185 186 187 now = time((time_t *) NULL); 188 for (i=0; i<atime_format_table_nents; i++) { 189 /* We reset every time throughout the loop as the manual page 190 * indicated that no guarantees are made as to preserving timebuf 191 * when parsing fails 192 */ 193 #ifdef HAVE_LOCALTIME_R 194 (void) localtime_r(&now, &timebuf); 195 #else 196 memcpy(&timebuf, localtime(&now), sizeof(timebuf)); 197 #endif 198 if ((s = strptime(string, atime_format_table[i], &timebuf)) 199 && (s != string)) { 200 /* See if at end of buffer - otherwise partial processing */ 201 while(*s != 0 && isspace((int) *s)) s++; 202 if (*s != 0) 203 continue; 204 if (timebuf.tm_year <= 0) 205 continue; /* clearly confused */ 206 ret_time = mktime(&timebuf); 207 if (ret_time == (time_t) -1) 208 continue; /* clearly confused */ 209 *timestampp = (krb5_timestamp) ret_time; 210 return 0; 211 } 212 } 213 return(EINVAL); 214 } 215 216 krb5_error_code KRB5_CALLCONV 217 krb5_timestamp_to_string(krb5_timestamp timestamp, char *buffer, size_t buflen) 218 { 219 int ret; 220 time_t timestamp2 = timestamp; 221 struct tm tmbuf; 222 const char *fmt = "%c"; /* This is to get around gcc -Wall warning that 223 the year returned might be two digits */ 224 225 #ifdef HAVE_LOCALTIME_R 226 (void) localtime_r(×tamp2, &tmbuf); 227 #else 228 memcpy(&tmbuf, localtime(×tamp2), sizeof(tmbuf)); 229 #endif 230 ret = strftime(buffer, buflen, fmt, &tmbuf); 231 if (ret == 0 || ret == buflen) 232 return(ENOMEM); 233 return(0); 234 } 235 236 krb5_error_code KRB5_CALLCONV 237 krb5_timestamp_to_sfstring(krb5_timestamp timestamp, char *buffer, size_t buflen, char *pad) 238 { 239 struct tm *tmp; 240 size_t i; 241 size_t ndone; 242 time_t timestamp2 = timestamp; 243 struct tm tmbuf; 244 245 static const char * const sftime_format_table[] = { 246 "%c", /* Default locale-dependent date and time */ 247 "%d %b %Y %T", /* dd mon yyyy hh:mm:ss */ 248 "%x %X", /* locale-dependent short format */ 249 "%d/%m/%Y %R" /* dd/mm/yyyy hh:mm */ 250 }; 251 static const int sftime_format_table_nents = 252 sizeof(sftime_format_table)/sizeof(sftime_format_table[0]); 253 254 #ifdef HAVE_LOCALTIME_R 255 tmp = localtime_r(×tamp2, &tmbuf); 256 #else 257 memcpy((tmp = &tmbuf), localtime(×tamp2), sizeof(tmbuf)); 258 #endif 259 ndone = 0; 260 for (i=0; i<sftime_format_table_nents; i++) { 261 if ((ndone = strftime(buffer, buflen, sftime_format_table[i], tmp))) 262 break; 263 } 264 if (!ndone) { 265 #define sftime_default_len 2+1+2+1+4+1+2+1+2+1 266 if (buflen >= sftime_default_len) { 267 sprintf(buffer, "%02d/%02d/%4d %02d:%02d", 268 tmp->tm_mday, tmp->tm_mon+1, 1900+tmp->tm_year, 269 tmp->tm_hour, tmp->tm_min); 270 ndone = strlen(buffer); 271 } 272 } 273 if (ndone && pad) { 274 for (i=ndone; i<buflen-1; i++) 275 buffer[i] = *pad; 276 buffer[buflen-1] = '\0'; 277 } 278 return((ndone) ? 0 : ENOMEM); 279 } 280 281 #ifdef SUNW_INC_DEAD_CODE 282 /* relative time (delta-t) conversions */ 283 284 /* string->deltat is in deltat.y */ 285 286 krb5_error_code KRB5_CALLCONV 287 krb5_deltat_to_string(krb5_deltat deltat, char *buffer, size_t buflen) 288 { 289 int days, hours, minutes, seconds; 290 krb5_deltat dt; 291 292 /* 293 * We want something like ceil(log10(2**(nbits-1))) + 1. That log 294 * value is log10(2)*(nbits-1) or log10(2**8)*(nbits-1)/8. So, 295 * 2.4... is log10(256), rounded up. Add one to handle leading 296 * minus, and one more to force int cast to round the value up. 297 * This doesn't include room for a trailing nul. 298 * 299 * This will break if bytes are more than 8 bits. 300 */ 301 #define MAX_CHARS_FOR_INT_TYPE(TYPE) ((int) (2 + 2.408241 * sizeof (TYPE))) 302 char tmpbuf[MAX_CHARS_FOR_INT_TYPE(int) * 4 + 8]; 303 304 days = (int) (deltat / (24*3600L)); 305 dt = deltat % (24*3600L); 306 hours = (int) (dt / 3600); 307 dt %= 3600; 308 minutes = (int) (dt / 60); 309 seconds = (int) (dt % 60); 310 311 memset (tmpbuf, 0, sizeof (tmpbuf)); 312 if (days == 0) 313 sprintf(buffer, "%d:%02d:%02d", hours, minutes, seconds); 314 else if (hours || minutes || seconds) 315 sprintf(buffer, "%d %s %02d:%02d:%02d", days, 316 (days > 1) ? "days" : "day", 317 hours, minutes, seconds); 318 else 319 sprintf(buffer, "%d %s", days, 320 (days > 1) ? "days" : "day"); 321 if (tmpbuf[sizeof(tmpbuf)-1] != 0) 322 /* Something must be very wrong with my math above, or the 323 assumptions going into it... */ 324 abort (); 325 if (strlen (tmpbuf) > buflen) 326 return ENOMEM; 327 else 328 strncpy (buffer, tmpbuf, buflen); 329 return 0; 330 } 331 #endif /* SUNW_INC_DEAD_CODE */ 332 333 #undef __P 334 #define __P(X) X 335 336 #if !defined (HAVE_STRFTIME) || !defined (HAVE_STRPTIME) 337 #undef _CurrentTimeLocale 338 #define _CurrentTimeLocale (&dummy_locale_info) 339 340 struct dummy_locale_info_t { 341 char d_t_fmt[15]; 342 char t_fmt_ampm[12]; 343 char t_fmt[9]; 344 char d_fmt[9]; 345 char day[7][10]; 346 char abday[7][4]; 347 char mon[12][10]; 348 char abmon[12][4]; 349 char am_pm[2][3]; 350 }; 351 static const struct dummy_locale_info_t dummy_locale_info = { 352 "%a %b %d %X %Y", /* %c */ 353 "%I:%M:%S %p", /* %r */ 354 "%H:%M:%S", /* %X */ 355 "%m/%d/%y", /* %x */ 356 { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 357 "Saturday" }, 358 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }, 359 { "January", "February", "March", "April", "May", "June", 360 "July", "August", "September", "October", "November", "December" }, 361 { "Jan", "Feb", "Mar", "Apr", "May", "Jun", 362 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }, 363 { "AM", "PM" }, 364 }; 365 #undef TM_YEAR_BASE 366 #define TM_YEAR_BASE 1900 367 #endif 368 369 #ifndef HAVE_STRFTIME 370 #undef DAYSPERLYEAR 371 #define DAYSPERLYEAR 366 372 #undef DAYSPERNYEAR 373 #define DAYSPERNYEAR 365 374 #undef DAYSPERWEEK 375 #define DAYSPERWEEK 7 376 #undef isleap 377 #define isleap(N) ((N % 4) == 0 && (N % 100 != 0 || N % 400 == 0)) 378 #undef tzname 379 #define tzname my_tzname 380 static const char *const tzname[2] = { 0, 0 }; 381 #undef tzset 382 #define tzset() 383 384 #include "strftime.c" 385 #endif 386 387 #ifndef HAVE_STRPTIME 388 #include "strptime.c" 389 #endif 390