[1] | 1 | /* SAMHAIN file system integrity testing */
|
---|
| 2 | /* Copyright (C) 1999, 2000 Rainer Wichmann */
|
---|
| 3 | /* */
|
---|
| 4 | /* This program is free software; you can redistribute it */
|
---|
| 5 | /* and/or modify */
|
---|
| 6 | /* it under the terms of the GNU General Public License as */
|
---|
| 7 | /* published by */
|
---|
| 8 | /* the Free Software Foundation; either version 2 of the License, or */
|
---|
| 9 | /* (at your option) any later version. */
|
---|
| 10 | /* */
|
---|
| 11 | /* This program is distributed in the hope that it will be useful, */
|
---|
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
---|
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
---|
| 14 | /* GNU General Public License for more details. */
|
---|
| 15 | /* */
|
---|
| 16 | /* You should have received a copy of the GNU General Public License */
|
---|
| 17 | /* along with this program; if not, write to the Free Software */
|
---|
| 18 | /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
---|
| 19 |
|
---|
| 20 | #include "config_xor.h"
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | #include <stdlib.h>
|
---|
| 24 | #include <stdio.h>
|
---|
| 25 | #include <string.h>
|
---|
| 26 | #include <ctype.h>
|
---|
| 27 | #include <unistd.h>
|
---|
| 28 |
|
---|
| 29 | #if HAVE_SYS_TIME_H
|
---|
| 30 | #include <sys/time.h>
|
---|
[583] | 31 | #endif
|
---|
[1] | 32 | #include <time.h>
|
---|
| 33 |
|
---|
| 34 | #include "samhain.h"
|
---|
| 35 | #include "sh_error.h"
|
---|
| 36 | #include "sh_utils.h"
|
---|
| 37 | #include "sh_unix.h"
|
---|
| 38 | #include "sh_tiger.h"
|
---|
| 39 | #include "sh_entropy.h"
|
---|
[137] | 40 | #include "sh_pthread.h"
|
---|
[1] | 41 |
|
---|
| 42 | #undef FIL__
|
---|
| 43 | #define FIL__ _("sh_utils.c")
|
---|
| 44 |
|
---|
| 45 | UINT32 ErrFlag[2];
|
---|
| 46 |
|
---|
[20] | 47 | int sh_util_flagval(const char * c, int * fval)
|
---|
[1] | 48 | {
|
---|
| 49 | SL_ENTER(_("sh_util_flagval"));
|
---|
| 50 | if (c == NULL)
|
---|
| 51 | SL_RETURN( (-1), _("sh_util_flagval"));
|
---|
| 52 | if ( c[0] == '1' || c[0] == 'y' || c[0] == 'Y' ||
|
---|
| 53 | c[0] == 't' || c[0] == 'T')
|
---|
| 54 | {
|
---|
| 55 | *fval = S_TRUE;
|
---|
| 56 | SL_RETURN( (0), _("sh_util_flagval"));
|
---|
| 57 | }
|
---|
| 58 | if ( c[0] == '0' || c[0] == 'n' || c[0] == 'N' ||
|
---|
| 59 | c[0] == 'f' || c[0] == 'F')
|
---|
| 60 | {
|
---|
| 61 | *fval = S_FALSE;
|
---|
| 62 | SL_RETURN( (0), _("sh_util_flagval"));
|
---|
| 63 | }
|
---|
| 64 | SL_RETURN( (-1), _("sh_util_flagval"));
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | int sh_util_timeout_check (SH_TIMEOUT * sh_timer)
|
---|
| 68 | {
|
---|
| 69 | UINT64 now = (UINT64) time(NULL);
|
---|
| 70 | UINT64 dif;
|
---|
| 71 |
|
---|
| 72 | if (sh_timer->flag_ok == S_FALSE)
|
---|
| 73 | {
|
---|
| 74 | /* first time
|
---|
| 75 | */
|
---|
| 76 | if (sh_timer->time_last == 0)
|
---|
| 77 | {
|
---|
| 78 | sh_timer->time_last = now;
|
---|
| 79 | return S_TRUE;
|
---|
| 80 | }
|
---|
| 81 | /* later on
|
---|
| 82 | */
|
---|
| 83 | dif = now - sh_timer->time_last;
|
---|
| 84 | if (dif < sh_timer->time_dist)
|
---|
| 85 | {
|
---|
| 86 | return S_FALSE;
|
---|
| 87 | }
|
---|
| 88 | sh_timer->time_last = now;
|
---|
| 89 | return S_TRUE;
|
---|
| 90 | }
|
---|
| 91 | sh_timer->time_last = now;
|
---|
| 92 | return S_FALSE;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | static int sh_ask_update = S_FALSE;
|
---|
| 96 |
|
---|
[20] | 97 | int sh_util_set_interactive(const char * str)
|
---|
[1] | 98 | {
|
---|
[20] | 99 | (void) str;
|
---|
[1] | 100 |
|
---|
[20] | 101 | sh_ask_update = S_TRUE;
|
---|
[1] | 102 | sh_unix_setnodeamon(NULL);
|
---|
| 103 |
|
---|
| 104 | return 0;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[355] | 107 | static char * sh_update_file = NULL;
|
---|
| 108 |
|
---|
| 109 | int sh_util_update_file (const char * str)
|
---|
| 110 | {
|
---|
| 111 | if (str)
|
---|
| 112 | {
|
---|
| 113 | if (0 == access(str, R_OK)) /* flawfinder: ignore */
|
---|
| 114 | {
|
---|
| 115 | if (NULL != sh_update_file)
|
---|
| 116 | SH_FREE(sh_update_file);
|
---|
| 117 | sh_update_file = sh_util_strdup(str);
|
---|
| 118 | sh_ask_update = S_TRUE;
|
---|
| 119 | sh_unix_setnodeamon(NULL);
|
---|
| 120 | return 0;
|
---|
| 121 | }
|
---|
| 122 | else
|
---|
| 123 | {
|
---|
| 124 | char ebuf[SH_ERRBUF_SIZE];
|
---|
| 125 | int errnum = errno;
|
---|
| 126 |
|
---|
| 127 | sh_error_message(errnum, ebuf, sizeof(ebuf));
|
---|
| 128 | sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, errnum, MSG_E_SUBGEN,
|
---|
| 129 | ebuf, _("sh_util_update_file") );
|
---|
| 130 |
|
---|
| 131 | return -1;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | return -1;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
[1] | 139 | #if !defined(STDIN_FILENO)
|
---|
| 140 | #define STDIN_FILENO 0
|
---|
| 141 | #endif
|
---|
| 142 | #if !defined(STDERR_FILENO)
|
---|
| 143 | #define STDERR_FILENO 0
|
---|
| 144 | #endif
|
---|
| 145 |
|
---|
[355] | 146 | /* Returns S_FALSE if no update desired
|
---|
| 147 | */
|
---|
| 148 | int sh_util_update_checkfile(const char * path)
|
---|
| 149 | {
|
---|
| 150 | FILE * fd = fopen(sh_update_file, "r");
|
---|
| 151 | char * line;
|
---|
| 152 |
|
---|
| 153 | if (!fd)
|
---|
| 154 | {
|
---|
| 155 | uid_t euid;
|
---|
| 156 | int errnum = errno;
|
---|
| 157 | sl_get_euid(&euid);
|
---|
| 158 | sh_error_handle (SH_ERR_ERR, FIL__, __LINE__, errnum, MSG_NOACCESS,
|
---|
| 159 | (long) euid, sh_update_file);
|
---|
| 160 | aud_exit (FIL__, __LINE__, EXIT_FAILURE);
|
---|
| 161 | return S_FALSE;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | line = SH_ALLOC(8192);
|
---|
| 165 |
|
---|
| 166 | while (NULL != fgets(line, 8192, fd))
|
---|
| 167 | {
|
---|
| 168 | char * nl = strrchr(line, '\n');
|
---|
| 169 |
|
---|
| 170 | if (nl)
|
---|
| 171 | {
|
---|
| 172 | *nl = '\0';
|
---|
| 173 |
|
---|
| 174 | /* Check for MS Windows line terminator
|
---|
| 175 | */
|
---|
| 176 | if (nl > line) --nl;
|
---|
| 177 | if (*nl == '\r')
|
---|
| 178 | *nl = '\0';
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | if (0 == sl_strcmp(line, path))
|
---|
| 182 | {
|
---|
| 183 | SH_FREE(line);
|
---|
| 184 | fclose(fd);
|
---|
| 185 | return S_TRUE;
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | SH_FREE(line);
|
---|
| 189 | fclose(fd);
|
---|
| 190 | return S_FALSE;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /* Returns S_FALSE if no update desired
|
---|
| 194 | */
|
---|
[305] | 195 | int sh_util_ask_update(const char * path)
|
---|
[1] | 196 | {
|
---|
| 197 | int inchar, c;
|
---|
| 198 | int i = S_TRUE;
|
---|
| 199 | char * tmp = NULL;
|
---|
| 200 |
|
---|
| 201 | SL_ENTER(_("sh_util_ask_update"));
|
---|
| 202 |
|
---|
| 203 | if (sh_ask_update != S_TRUE)
|
---|
| 204 | {
|
---|
| 205 | SL_RETURN(i, _("sh_util_ask_update"));
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[355] | 208 | if (sh_update_file)
|
---|
| 209 | {
|
---|
| 210 | i = sh_util_update_checkfile(path);
|
---|
| 211 | SL_RETURN(i, _("sh_util_ask_update"));
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[1] | 214 | #ifdef HAVE_TTYNAME
|
---|
| 215 | if (!ttyname(STDIN_FILENO))
|
---|
| 216 | {
|
---|
| 217 | if (NULL != ttyname(STDERR_FILENO))
|
---|
| 218 | {
|
---|
[504] | 219 | /* cppcheck-suppress leakReturnValNotUsed */
|
---|
[1] | 220 | if (NULL == freopen(ttyname(STDERR_FILENO), "r", stdin))
|
---|
| 221 | {
|
---|
| 222 | sh_error_handle ((-1), FIL__, __LINE__, 0,
|
---|
| 223 | MSG_E_SUBGEN,
|
---|
| 224 | _("Cannot continue: stdin is not a terminal"),
|
---|
| 225 | _("sh_util_ask_update"));
|
---|
| 226 | exit(EXIT_FAILURE);
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | else
|
---|
| 230 | {
|
---|
| 231 | sh_error_handle ((-1), FIL__, __LINE__, 0,
|
---|
| 232 | MSG_E_SUBGEN,
|
---|
| 233 | _("Cannot continue: stdin is not a terminal"),
|
---|
| 234 | _("sh_util_ask_update"));
|
---|
| 235 | exit(EXIT_FAILURE);
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | #endif
|
---|
| 239 |
|
---|
| 240 | if (sh_ask_update == S_TRUE)
|
---|
| 241 | {
|
---|
| 242 | tmp = sh_util_safe_name (path);
|
---|
| 243 | fprintf (stderr, _("Update %s [Y/n] ? "), tmp);
|
---|
| 244 | SH_FREE(tmp);
|
---|
| 245 | while (1 == 1)
|
---|
| 246 | {
|
---|
| 247 | c = fgetc(stdin); inchar = c;
|
---|
| 248 | /*@+charintliteral@*/
|
---|
| 249 | while (c != '\n' && c != EOF)
|
---|
| 250 | c = fgetc(stdin);
|
---|
| 251 | /* fprintf(stderr, "CHAR (1): %c\n", inchar); */
|
---|
| 252 | if (inchar == 'Y' || inchar == 'y' || inchar == '\n')
|
---|
| 253 | {
|
---|
| 254 | break;
|
---|
| 255 | }
|
---|
| 256 | else if (inchar == 'n' || inchar == 'N')
|
---|
| 257 | {
|
---|
| 258 | i = S_FALSE;
|
---|
| 259 | break;
|
---|
| 260 | }
|
---|
| 261 | else
|
---|
| 262 | {
|
---|
[210] | 263 | fprintf(stderr, "%s", _("Please answer y(es) or n(o)\n"));
|
---|
[1] | 264 | }
|
---|
| 265 | /*@-charintliteral@*/
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | SL_RETURN(i, _("sh_util_ask_update"));
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[22] | 272 | int sh_util_hidesetup(const char * c)
|
---|
[1] | 273 | {
|
---|
| 274 | int i;
|
---|
| 275 | SL_ENTER(_("sh_util_hidesetup"));
|
---|
| 276 | i = sh_util_flagval(c, &(sh.flag.hidefile));
|
---|
| 277 |
|
---|
| 278 | SL_RETURN(i, _("sh_util_hidesetup"));
|
---|
| 279 | }
|
---|
[68] | 280 |
|
---|
| 281 | char * sh_util_acl_compact(char * buf, ssize_t len)
|
---|
| 282 | {
|
---|
| 283 | unsigned char * p = (unsigned char *) buf;
|
---|
| 284 | int state = 0;
|
---|
| 285 | ssize_t rem = 0;
|
---|
| 286 | char * out;
|
---|
| 287 |
|
---|
| 288 | SH_VALIDATE_NE(buf, NULL);
|
---|
| 289 | SH_VALIDATE_GE(len, 0);
|
---|
| 290 |
|
---|
| 291 | out = SH_ALLOC(len + 1);
|
---|
| 292 |
|
---|
| 293 | while (*p != '\0') {
|
---|
| 294 |
|
---|
| 295 | /* -- not at start or after newline
|
---|
| 296 | */
|
---|
| 297 | if (state == 1) {
|
---|
| 298 | if (*p == '\n' || *p == ' ' || *p == '\t' || *p == '#') {
|
---|
| 299 | while (*p != '\n') {
|
---|
| 300 | ++p;
|
---|
| 301 | if (*p == '\0') {
|
---|
| 302 | goto exit_it;
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
[481] | 305 | out[rem] = ','; ++rem; /* <-- ensures (rem > 0) is true */
|
---|
| 306 | while (p[1] == '\n') ++p; /* scan over consecutive newlines */
|
---|
[68] | 307 | state = 0;
|
---|
| 308 | if (p[1] == '\0') {
|
---|
[481] | 309 | out[rem-1] = '\0'; /* rem > 0 because of 4 lines above */
|
---|
[68] | 310 | break;
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
| 313 | else {
|
---|
| 314 | if (*p <= 0x7F && isgraph((int) *p)) {
|
---|
| 315 | out[rem] = (char) *p; ++rem;
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | /* -- at start or after newline
|
---|
| 321 | */
|
---|
| 322 | else /* if (state == 0) */ {
|
---|
| 323 | if (0 == strncmp((char *) p, "user", 4)) {
|
---|
| 324 | out[rem] = 'u'; ++rem;
|
---|
[382] | 325 | p += 3;
|
---|
[68] | 326 | } else if (0 == strncmp((char *) p, "group", 5)) {
|
---|
| 327 | out[rem] = 'g'; ++rem;
|
---|
[382] | 328 | p += 4;
|
---|
[68] | 329 | } else if (0 == strncmp((char *) p, "mask", 4)) {
|
---|
| 330 | out[rem] = 'm'; ++rem;
|
---|
[382] | 331 | p += 3;
|
---|
[68] | 332 | } else if (0 == strncmp((char *) p, "other", 5)) {
|
---|
| 333 | out[rem] = 'o';
|
---|
[382] | 334 | p += 4; ++rem;
|
---|
[68] | 335 | } else if (*p == '\0') {
|
---|
| 336 | if (rem > 0) { out[rem-1] = '\0'; }
|
---|
| 337 | break;
|
---|
| 338 | } else {
|
---|
| 339 | if (*p <= 0x7F && isprint((int) *p)) {
|
---|
| 340 | out[rem] = (char) *p; ++rem;
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 | state = 1;
|
---|
| 344 | }
|
---|
| 345 | ++p;
|
---|
| 346 | }
|
---|
| 347 | exit_it:
|
---|
| 348 | out[rem] = '\0';
|
---|
| 349 | return out;
|
---|
| 350 | }
|
---|
[76] | 351 |
|
---|
| 352 |
|
---|
| 353 | char * sh_util_strdup_l (const char * str, size_t len)
|
---|
| 354 | {
|
---|
| 355 | char * p = NULL;
|
---|
| 356 |
|
---|
| 357 | SL_ENTER(_("sh_util_strdup_l"));
|
---|
| 358 |
|
---|
| 359 | SH_VALIDATE_NE(str, NULL);
|
---|
| 360 | SH_VALIDATE_NE(len, 0);
|
---|
| 361 |
|
---|
[382] | 362 | if (str && sl_ok_adds (len, 1))
|
---|
[76] | 363 | {
|
---|
| 364 | p = SH_ALLOC (len + 1);
|
---|
[167] | 365 | (void) memcpy (p, str, len+1);
|
---|
[76] | 366 | }
|
---|
| 367 | else
|
---|
| 368 | {
|
---|
| 369 | safe_fatal(_("integer overflow in sh_util_strdup_l"), FIL__, __LINE__);
|
---|
| 370 | }
|
---|
| 371 | SL_RETURN( p, _("sh_util_strdup_l"));
|
---|
| 372 | }
|
---|
| 373 |
|
---|
[1] | 374 | char * sh_util_strdup (const char * str)
|
---|
| 375 | {
|
---|
| 376 | char * p = NULL;
|
---|
| 377 | size_t len;
|
---|
| 378 |
|
---|
| 379 | SL_ENTER(_("sh_util_strdup"));
|
---|
| 380 |
|
---|
[25] | 381 | SH_VALIDATE_NE(str, NULL);
|
---|
| 382 |
|
---|
[382] | 383 | if (str)
|
---|
| 384 | {
|
---|
| 385 | len = sl_strlen(str);
|
---|
| 386 | p = SH_ALLOC (len + 1);
|
---|
| 387 | (void) memcpy (p, str, len+1);
|
---|
| 388 | }
|
---|
[1] | 389 | SL_RETURN( p, _("sh_util_strdup"));
|
---|
| 390 | }
|
---|
| 391 |
|
---|
[382] | 392 | char * sh_util_strdup_track (const char * str, char * file, int line)
|
---|
| 393 | {
|
---|
| 394 | char * p = NULL;
|
---|
| 395 | size_t len;
|
---|
| 396 |
|
---|
| 397 | SL_ENTER(_("sh_util_strdup_track"));
|
---|
| 398 |
|
---|
| 399 | SH_VALIDATE_NE(str, NULL);
|
---|
| 400 |
|
---|
| 401 | if (str)
|
---|
| 402 | {
|
---|
| 403 | len = sl_strlen(str);
|
---|
| 404 | p = SH_OALLOC (len + 1, file, line);
|
---|
| 405 | (void) memcpy (p, str, len+1);
|
---|
| 406 | }
|
---|
| 407 | SL_RETURN( p, _("sh_util_strdup_track"));
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[1] | 410 | /* by the eircom.net computer incident
|
---|
| 411 | * response team
|
---|
| 412 | */
|
---|
| 413 | char * sh_util_strsep (char **str, const char *delim)
|
---|
| 414 | {
|
---|
[170] | 415 | char *ret, *c;
|
---|
| 416 | const char *d;
|
---|
[1] | 417 |
|
---|
| 418 | SL_ENTER(_("sh_util_strsep"));
|
---|
| 419 | ret = *str;
|
---|
| 420 |
|
---|
[25] | 421 | SH_VALIDATE_NE(ret, NULL);
|
---|
[1] | 422 |
|
---|
[382] | 423 | if (*str)
|
---|
| 424 | {
|
---|
| 425 | for (c = *str; *c != '\0'; c++) {
|
---|
| 426 | for (d = delim; *d != '\0'; d++) {
|
---|
| 427 | if (*c == *d) {
|
---|
| 428 | *c = '\0';
|
---|
| 429 | *str = c + 1;
|
---|
| 430 | SL_RETURN(ret, _("sh_util_strsep"));
|
---|
| 431 | }
|
---|
| 432 | }
|
---|
[1] | 433 | }
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | /* If we get to here, there's no delimiters in the string */
|
---|
| 437 | *str = NULL;
|
---|
| 438 | SL_RETURN(ret, _("sh_util_strsep"));
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 |
|
---|
[13] | 442 | /* returned string must be free'd by caller.
|
---|
[1] | 443 | */
|
---|
| 444 | char * sh_util_formatted (const char * formatt, st_format * ftab)
|
---|
| 445 | {
|
---|
| 446 | struct tm * time_ptr;
|
---|
| 447 | size_t size;
|
---|
| 448 | size_t isiz;
|
---|
| 449 | char * fmt = NULL;
|
---|
| 450 | char * p;
|
---|
| 451 | char * q;
|
---|
| 452 | char * outstr;
|
---|
| 453 | int i;
|
---|
| 454 | int j;
|
---|
| 455 | time_t inpp;
|
---|
| 456 |
|
---|
[13] | 457 | char * clist[16] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
---|
| 458 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
|
---|
[1] | 459 | int nn = 0;
|
---|
| 460 |
|
---|
| 461 | SL_ENTER(_("sh_util_formatted"));
|
---|
| 462 |
|
---|
[13] | 463 | if (formatt == NULL || ftab == NULL || *formatt == '\0')
|
---|
[1] | 464 | SL_RETURN(NULL, _("sh_util_formatted"));
|
---|
| 465 |
|
---|
| 466 | /* -- save the format (we overwrite it !!) --
|
---|
| 467 | */
|
---|
| 468 | size = sl_strlen(formatt);
|
---|
| 469 |
|
---|
[34] | 470 | if (!sl_ok_adds(size, 1))
|
---|
| 471 | SL_RETURN(NULL, _("sh_util_formatted"));
|
---|
[13] | 472 |
|
---|
[34] | 473 | ++size;
|
---|
| 474 | fmt = SH_ALLOC(size);
|
---|
| 475 | (void) sl_strlcpy(fmt, formatt, size);
|
---|
| 476 |
|
---|
[1] | 477 | p = fmt;
|
---|
| 478 |
|
---|
| 479 | j = 0;
|
---|
[13] | 480 | while (ftab[j].fchar != '\0') {
|
---|
| 481 | if (ftab[j].type != S_FMT_STRING)
|
---|
| 482 | ftab[j].data_str = NULL;
|
---|
| 483 | ++j;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
[1] | 486 | while (p != NULL && *p != '\0' && NULL != (q = strchr(p, '%')))
|
---|
| 487 | {
|
---|
| 488 | ++q;
|
---|
| 489 |
|
---|
| 490 | /* fprintf(stderr, "p == %s q == %s\n", p, q); */
|
---|
| 491 |
|
---|
| 492 | /* -- end of string is a '%' --
|
---|
| 493 | */
|
---|
| 494 | if (*q == '\0')
|
---|
| 495 | {
|
---|
| 496 | --q;
|
---|
| 497 | *q = '\0';
|
---|
| 498 | break;
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | i = 0;
|
---|
| 502 | j = 0;
|
---|
| 503 |
|
---|
| 504 | /* -- search the format char in input table --
|
---|
| 505 | * put (nn < 16) here -> all remaining %foo will be
|
---|
| 506 | * converted to %%
|
---|
| 507 | */
|
---|
| 508 | while (ftab[j].fchar != '\0' && nn < 16)
|
---|
| 509 | {
|
---|
| 510 | if (ftab[j].fchar == *q)
|
---|
| 511 | {
|
---|
| 512 | /* -- Convert it to a string format (%s). --
|
---|
| 513 | */
|
---|
| 514 | *q = 's'
|
---|
| 515 | ;
|
---|
| 516 | i = 1;
|
---|
| 517 |
|
---|
[13] | 518 | switch(ftab[j].type) {
|
---|
| 519 |
|
---|
| 520 | case S_FMT_STRING:
|
---|
[1] | 521 | {
|
---|
| 522 | isiz = sl_strlen(ftab[j].data_str);
|
---|
[34] | 523 | if (isiz > 0 && sl_ok_adds(size, isiz))
|
---|
[1] | 524 | {
|
---|
| 525 | size += isiz;
|
---|
| 526 | clist[nn] = ftab[j].data_str;
|
---|
| 527 | ++nn;
|
---|
| 528 | }
|
---|
| 529 | else
|
---|
| 530 | *q = '%';
|
---|
[13] | 531 | goto endsrch;
|
---|
[1] | 532 | }
|
---|
[13] | 533 | break;
|
---|
| 534 |
|
---|
| 535 | case S_FMT_ULONG:
|
---|
[1] | 536 | {
|
---|
| 537 | ftab[j].data_str = (char *) SH_ALLOC(64);
|
---|
| 538 | /*@-bufferoverflowhigh@*/
|
---|
| 539 | sprintf (ftab[j].data_str, "%lu", /* known to fit */
|
---|
| 540 | ftab[j].data_ulong);
|
---|
| 541 | /*@+bufferoverflowhigh@*/
|
---|
| 542 | isiz = sl_strlen(ftab[j].data_str);
|
---|
[34] | 543 | if (isiz > 0 && sl_ok_adds(size, isiz))
|
---|
[1] | 544 | {
|
---|
| 545 | size += isiz;
|
---|
| 546 | clist[nn] = ftab[j].data_str;
|
---|
| 547 | ++nn;
|
---|
| 548 | }
|
---|
| 549 | else
|
---|
| 550 | *q = '%';
|
---|
[13] | 551 | goto endsrch;
|
---|
[1] | 552 | }
|
---|
[13] | 553 | break;
|
---|
| 554 |
|
---|
| 555 | case S_FMT_LONG:
|
---|
[1] | 556 | {
|
---|
| 557 | ftab[j].data_str = (char *) SH_ALLOC(64);
|
---|
| 558 | /*@-bufferoverflowhigh@*/
|
---|
| 559 | sprintf (ftab[j].data_str, "%ld", /* known to fit */
|
---|
| 560 | ftab[j].data_long);
|
---|
| 561 | /*@+bufferoverflowhigh@*/
|
---|
| 562 | isiz = sl_strlen(ftab[j].data_str);
|
---|
[34] | 563 | if (isiz > 0 && sl_ok_adds(size, isiz))
|
---|
[1] | 564 | {
|
---|
| 565 | size += isiz;
|
---|
| 566 | clist[nn] = ftab[j].data_str;
|
---|
| 567 | ++nn;
|
---|
| 568 | }
|
---|
| 569 | else
|
---|
| 570 | *q = '%';
|
---|
[13] | 571 | goto endsrch;
|
---|
[1] | 572 | }
|
---|
[13] | 573 | break;
|
---|
| 574 |
|
---|
| 575 | case S_FMT_TIME:
|
---|
[1] | 576 | {
|
---|
| 577 | ftab[j].data_str = (char *) SH_ALLOC(64);
|
---|
| 578 | inpp = (time_t)ftab[j].data_ulong;
|
---|
| 579 | if (inpp != 0)
|
---|
| 580 | {
|
---|
| 581 | time_ptr = localtime (&(inpp));
|
---|
| 582 | if (time_ptr != NULL)
|
---|
| 583 | (void) strftime(ftab[j].data_str, 64,
|
---|
| 584 | _("%d-%m-%Y %H:%M:%S"), time_ptr);
|
---|
| 585 | else
|
---|
| 586 | (void) sl_strlcpy(ftab[j].data_str,
|
---|
| 587 | _("00-00-0000 00:00:00"), 64);
|
---|
| 588 | }
|
---|
| 589 | else
|
---|
| 590 | {
|
---|
| 591 | (void) sl_strlcpy(ftab[j].data_str,
|
---|
| 592 | _("(None)"), 64);
|
---|
| 593 | }
|
---|
| 594 | isiz = sl_strlen(ftab[j].data_str);
|
---|
[34] | 595 | if (isiz > 0 && sl_ok_adds(size, isiz))
|
---|
[1] | 596 | {
|
---|
| 597 | size += isiz;
|
---|
| 598 | clist[nn] = ftab[j].data_str;
|
---|
| 599 | ++nn;
|
---|
| 600 | }
|
---|
| 601 | else
|
---|
| 602 | *q = '%';
|
---|
[13] | 603 | goto endsrch;
|
---|
[1] | 604 | }
|
---|
[13] | 605 | break;
|
---|
[1] | 606 |
|
---|
[13] | 607 | default:
|
---|
| 608 | /* do nothing */;
|
---|
| 609 | }
|
---|
| 610 |
|
---|
[1] | 611 | }
|
---|
[13] | 612 | ++j;
|
---|
[1] | 613 | }
|
---|
| 614 |
|
---|
[13] | 615 | endsrch:
|
---|
| 616 |
|
---|
| 617 | p = q;
|
---|
| 618 |
|
---|
[1] | 619 | /* -- not found -- */
|
---|
| 620 | if (i == 0)
|
---|
| 621 | {
|
---|
| 622 | *q = '%';
|
---|
| 623 | ++p;
|
---|
| 624 | }
|
---|
[13] | 625 |
|
---|
[1] | 626 | }
|
---|
| 627 |
|
---|
| 628 | /* -- Format string evaluated.
|
---|
| 629 | clist[] List of strings
|
---|
| 630 | size Total size of format string + clist[] strings
|
---|
| 631 | -- */
|
---|
| 632 |
|
---|
| 633 | /* -- closing '\0' --
|
---|
| 634 | */
|
---|
[34] | 635 | if (sl_ok_adds(size, 1))
|
---|
| 636 | size++;
|
---|
[1] | 637 | outstr = (char *) SH_ALLOC(size);
|
---|
| 638 |
|
---|
| 639 | /* -- print it --
|
---|
| 640 | */
|
---|
| 641 | (void) sl_snprintf( outstr, size, fmt,
|
---|
| 642 | clist[0], clist[1], clist[2], clist[3],
|
---|
| 643 | clist[4], clist[5], clist[6], clist[7],
|
---|
| 644 | clist[8], clist[9], clist[10], clist[11],
|
---|
| 645 | clist[12], clist[13], clist[14], clist[15]);
|
---|
[34] | 646 | outstr[size-1] = '\0';
|
---|
| 647 |
|
---|
[1] | 648 | /* -- cleanup --
|
---|
| 649 | */
|
---|
| 650 | j = 0;
|
---|
[13] | 651 | while (ftab[j].fchar != '\0') {
|
---|
| 652 | if (ftab[j].type != S_FMT_STRING && ftab[j].data_str != NULL)
|
---|
| 653 | SH_FREE(ftab[j].data_str);
|
---|
| 654 | ++j;
|
---|
| 655 | }
|
---|
[1] | 656 | SH_FREE(fmt);
|
---|
| 657 |
|
---|
| 658 | SL_RETURN(outstr, _("sh_util_formatted"));
|
---|
| 659 | }
|
---|
| 660 |
|
---|
[93] | 661 | /* read a hexchar, return int value (0-15)
|
---|
| 662 | * can't inline (AIX)
|
---|
[1] | 663 | */
|
---|
[12] | 664 | int sh_util_hexchar( const char c )
|
---|
[1] | 665 | {
|
---|
| 666 | /*@+charint@*/
|
---|
| 667 | if ( c >= '0' && c <= '9' )
|
---|
| 668 | return c - '0';
|
---|
| 669 | else if ( c >= 'a' && c <= 'f' )
|
---|
| 670 | return c - 'a' + 10;
|
---|
| 671 | else if ( c >= 'A' && c <= 'F' )
|
---|
| 672 | return c - 'A' + 10;
|
---|
| 673 | else return -1;
|
---|
| 674 | /*@-charint@*/
|
---|
| 675 | }
|
---|
| 676 |
|
---|
[137] | 677 | char * sh_util_charhex( unsigned char i , char * i2h)
|
---|
[93] | 678 | {
|
---|
| 679 | int j, k;
|
---|
| 680 |
|
---|
| 681 | j = i / 16;
|
---|
| 682 | k = i - (j*16);
|
---|
| 683 |
|
---|
| 684 | if (j < 10) i2h[0] = '0'+j;
|
---|
| 685 | else i2h[0] = 'A'+(j-10);
|
---|
| 686 |
|
---|
| 687 | if (k < 10) i2h[1] = '0'+k;
|
---|
| 688 | else i2h[1] = 'A'+(k-10);
|
---|
| 689 |
|
---|
| 690 | return i2h;
|
---|
| 691 | }
|
---|
| 692 |
|
---|
[1] | 693 | /* read a hexadecimal key, convert to binary
|
---|
| 694 | */
|
---|
[12] | 695 | int sh_util_hextobinary (char * binary, const char * hex, int bytes)
|
---|
[1] | 696 | {
|
---|
| 697 | int i = 0, j, k, l = 0;
|
---|
[13] | 698 | char c;
|
---|
[1] | 699 |
|
---|
[13] | 700 | #define SH_HEXCHAR(x, y) \
|
---|
| 701 | c = (x); \
|
---|
| 702 | if ( c >= '0' && c <= '9' ) \
|
---|
| 703 | y = c - '0'; \
|
---|
| 704 | else if ( c >= 'a' && c <= 'f' ) \
|
---|
| 705 | y = c - 'a' + 10; \
|
---|
| 706 | else if ( c >= 'A' && c <= 'F' ) \
|
---|
| 707 | y = c - 'A' + 10; \
|
---|
| 708 | else \
|
---|
| 709 | SL_RETURN((-1), _("sh_util_hextobinary"))
|
---|
| 710 |
|
---|
| 711 |
|
---|
[1] | 712 | SL_ENTER(_("sh_util_hextobinary"));
|
---|
| 713 |
|
---|
[34] | 714 | if (bytes < 2)
|
---|
| 715 | SL_RETURN((-1), _("sh_util_hextobinary"));
|
---|
| 716 |
|
---|
| 717 | while (i < (bytes-1))
|
---|
[1] | 718 | {
|
---|
[13] | 719 | SH_HEXCHAR(hex[i], k);
|
---|
| 720 | SH_HEXCHAR(hex[i+1], j);
|
---|
| 721 |
|
---|
| 722 | binary[l] = (char)(k * 16 + j);
|
---|
| 723 | ++l; i+= 2;
|
---|
[1] | 724 | }
|
---|
| 725 |
|
---|
| 726 | SL_RETURN((0), _("sh_util_hextobinary"));
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | static void copy_four (unsigned char * dest, UINT32 in)
|
---|
| 730 | {
|
---|
| 731 | UINT32 i, j;
|
---|
| 732 | int count;
|
---|
| 733 |
|
---|
| 734 | SL_ENTER(_("copy_four"));
|
---|
| 735 | for (count = 0; count < 4; ++count)
|
---|
| 736 | {
|
---|
| 737 | i = in / 256;
|
---|
| 738 | j = in - (i*256);
|
---|
| 739 | dest[count] = (unsigned char) j;
|
---|
| 740 | in = i;
|
---|
| 741 | }
|
---|
| 742 | SL_RET0(_("copy_four"));
|
---|
| 743 | }
|
---|
| 744 |
|
---|
| 745 | /* compute HMAC-TIGER
|
---|
| 746 | */
|
---|
| 747 | static char * sh_util_hmac_tiger (char * hexkey,
|
---|
[133] | 748 | char * text, size_t textlen,
|
---|
| 749 | char * res, size_t len)
|
---|
[1] | 750 | {
|
---|
| 751 | static char opad[KEY_BLOCK] = {
|
---|
| 752 | (char)0x5C, (char)0x5C, (char)0x5C, (char)0x5C, (char)0x5C, (char)0x5C,
|
---|
| 753 | (char)0x5C, (char)0x5C, (char)0x5C, (char)0x5C, (char)0x5C, (char)0x5C,
|
---|
| 754 | (char)0x5C, (char)0x5C, (char)0x5C, (char)0x5C, (char)0x5C, (char)0x5C,
|
---|
| 755 | (char)0x5C, (char)0x5C, (char)0x5C, (char)0x5C, (char)0x5C, (char)0x5C
|
---|
| 756 | };
|
---|
| 757 | static char ipad[KEY_BLOCK] = {
|
---|
| 758 | (char)0x36, (char)0x36, (char)0x36, (char)0x36, (char)0x36, (char)0x36,
|
---|
| 759 | (char)0x36, (char)0x36, (char)0x36, (char)0x36, (char)0x36, (char)0x36,
|
---|
| 760 | (char)0x36, (char)0x36, (char)0x36, (char)0x36, (char)0x36, (char)0x36,
|
---|
| 761 | (char)0x36, (char)0x36, (char)0x36, (char)0x36, (char)0x36, (char)0x36
|
---|
| 762 | };
|
---|
[11] | 763 | static char zap[KEY_BLOCK] = {
|
---|
| 764 | (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00,
|
---|
| 765 | (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00,
|
---|
| 766 | (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00,
|
---|
| 767 | (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00, (char)0x00
|
---|
| 768 | };
|
---|
[1] | 769 | char K[KEY_BLOCK];
|
---|
| 770 | char outer[KEY_BLOCK];
|
---|
| 771 | char * inner;
|
---|
| 772 | UINT32 * h1;
|
---|
| 773 | UINT32 * h2;
|
---|
| 774 | UINT32 cc[KEY_LEN/4];
|
---|
[133] | 775 | UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
|
---|
| 776 | char hashbuf[KEYBUF_SIZE];
|
---|
[1] | 777 |
|
---|
[481] | 778 | int result;
|
---|
[1] | 779 | size_t i;
|
---|
| 780 |
|
---|
| 781 | SL_ENTER(_("sh_util_hmac_tiger"));
|
---|
| 782 |
|
---|
[481] | 783 | ASSERT((KEY_BLOCK <= (KEY_LEN/2)), _("KEY_BLOCK <= (KEY_LEN/2)"));
|
---|
[1] | 784 |
|
---|
[11] | 785 | memcpy (K, zap, KEY_BLOCK);
|
---|
[1] | 786 |
|
---|
[481] | 787 | result = sh_util_hextobinary (K, hexkey, KEY_LEN);
|
---|
[1] | 788 |
|
---|
[481] | 789 | ASSERT((result >= 0), _("result >= 0"));
|
---|
| 790 |
|
---|
| 791 | if ((result >= 0) && sl_ok_adds(textlen, KEY_BLOCK))
|
---|
[34] | 792 | {
|
---|
| 793 | inner = (char *) SH_ALLOC (textlen + KEY_BLOCK);
|
---|
[1] | 794 |
|
---|
[34] | 795 | for (i = 0; i < KEY_BLOCK; ++i)
|
---|
| 796 | {
|
---|
| 797 | outer[i] = K[i] ^ opad[i];
|
---|
| 798 | inner[i] = K[i] ^ ipad[i];
|
---|
| 799 | }
|
---|
| 800 | for (i = KEY_BLOCK; i < (KEY_BLOCK+textlen); ++i)
|
---|
| 801 | {
|
---|
| 802 | inner[i] = text[i - KEY_BLOCK];
|
---|
| 803 | }
|
---|
[1] | 804 | }
|
---|
[34] | 805 | else
|
---|
[1] | 806 | {
|
---|
[34] | 807 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
| 808 | _("integer overflow"),
|
---|
| 809 | _("sh_util_hmac_tiger"));
|
---|
[137] | 810 | (void) sh_tiger_hash (NULL, TIGER_DATA, 0, hashbuf, sizeof(hashbuf));
|
---|
| 811 | sl_strlcpy(res, hashbuf, len);
|
---|
[34] | 812 | SL_RETURN(res, _("sh_util_hmac_tiger"));
|
---|
[1] | 813 | }
|
---|
| 814 |
|
---|
| 815 | /* now compute the hash
|
---|
| 816 | */
|
---|
[481] | 817 | h1 = sh_tiger_hash_uint32 ( outer, TIGER_DATA, KEY_BLOCK,
|
---|
[133] | 818 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
[1] | 819 | for (i = 0; i < (KEY_LEN/8); ++i)
|
---|
[481] | 820 | copy_four ( (unsigned char *) &(cc[i]), h1[i]);
|
---|
[1] | 821 |
|
---|
[560] | 822 | /* cppcheck-suppress uninitvar */
|
---|
[481] | 823 | h2 = sh_tiger_hash_uint32 ( inner, TIGER_DATA,
|
---|
[133] | 824 | (unsigned long) KEY_BLOCK+textlen,
|
---|
| 825 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
[1] | 826 | for (i = KEY_LEN/8; i < (KEY_LEN/4); ++i)
|
---|
[481] | 827 | copy_four ( (unsigned char *) &(cc[i]), h2[i - (KEY_LEN/8)]);
|
---|
| 828 |
|
---|
[560] | 829 | /* cppcheck-suppress uninitvar */
|
---|
[1] | 830 | SH_FREE(inner);
|
---|
| 831 |
|
---|
[133] | 832 | (void) sh_tiger_hash ((char *) &cc[0],
|
---|
| 833 | TIGER_DATA,
|
---|
| 834 | (unsigned long) (KEY_LEN/4 * sizeof(UINT32)),
|
---|
| 835 | hashbuf, sizeof(hashbuf));
|
---|
[1] | 836 |
|
---|
[133] | 837 | sl_strlcpy(res, hashbuf, len);
|
---|
[1] | 838 | SL_RETURN(res, _("sh_util_hmac_tiger"));
|
---|
| 839 | }
|
---|
| 840 |
|
---|
| 841 | static char * sh_util_hash_tiger ( char * hexkey,
|
---|
[137] | 842 | char * text, size_t textlen,
|
---|
[133] | 843 | char * res, size_t len)
|
---|
[1] | 844 | {
|
---|
| 845 | char h2[2*KEY_LEN+1];
|
---|
[137] | 846 | char hashbuf[KEYBUF_SIZE];
|
---|
| 847 |
|
---|
[1] | 848 | SL_ENTER(_("sh_util_hash_tiger"));
|
---|
| 849 |
|
---|
| 850 | (void) sl_strlcpy(h2, hexkey, KEY_LEN+1);
|
---|
[133] | 851 | (void) sl_strlcat(h2,
|
---|
| 852 | sh_tiger_hash(text, TIGER_DATA,
|
---|
| 853 | (unsigned long) textlen,
|
---|
| 854 | hashbuf, sizeof(hashbuf)),
|
---|
[137] | 855 | 2*KEY_LEN+1
|
---|
[133] | 856 | );
|
---|
[1] | 857 |
|
---|
[133] | 858 | (void) sh_tiger_hash(h2, TIGER_DATA, 2*KEY_LEN, hashbuf, sizeof(hashbuf));
|
---|
[1] | 859 |
|
---|
[133] | 860 | sl_strlcpy(res, hashbuf, len);
|
---|
[1] | 861 | SL_RETURN(res, _("sh_util_hash_tiger"));
|
---|
| 862 | }
|
---|
| 863 |
|
---|
| 864 | /* --- compute signature on data ---
|
---|
| 865 | */
|
---|
| 866 | #define TYPE_HMAC 0
|
---|
| 867 | #define TYPE_HASH 1
|
---|
| 868 |
|
---|
| 869 | static int sigtype = TYPE_HMAC;
|
---|
| 870 |
|
---|
[22] | 871 | int sh_util_sigtype (const char * c)
|
---|
[1] | 872 | {
|
---|
| 873 | SL_ENTER(_("sh_util_sigtype"));
|
---|
| 874 | if (c == NULL)
|
---|
| 875 | SL_RETURN( -1, _("sh_util_sigtype"));
|
---|
| 876 |
|
---|
| 877 | if (0 == strcmp(_("HMAC-TIGER"), c))
|
---|
| 878 | sigtype = TYPE_HMAC;
|
---|
| 879 | else if (0 == strcmp(_("HASH-TIGER"), c))
|
---|
| 880 | sigtype = TYPE_HASH;
|
---|
| 881 | else
|
---|
| 882 | SL_RETURN( -1, _("sh_util_sigtype"));
|
---|
| 883 |
|
---|
| 884 | SL_RETURN( 0, _("sh_util_sigtype"));
|
---|
| 885 | }
|
---|
| 886 |
|
---|
| 887 | char * sh_util_siggen (char * hexkey,
|
---|
[133] | 888 | char * text, size_t textlen,
|
---|
| 889 | char * res, size_t len)
|
---|
[1] | 890 | {
|
---|
| 891 | char * p;
|
---|
| 892 |
|
---|
| 893 | SL_ENTER(_("sh_util_siggen"));
|
---|
| 894 | if (sigtype == TYPE_HMAC)
|
---|
| 895 | p = sh_util_hmac_tiger (hexkey,
|
---|
[133] | 896 | text, textlen, res, len);
|
---|
[1] | 897 | else
|
---|
| 898 | p = sh_util_hash_tiger (hexkey,
|
---|
[133] | 899 | text, textlen, res, len);
|
---|
[1] | 900 | SL_RETURN(p, _("sh_util_siggen"));
|
---|
| 901 | }
|
---|
| 902 |
|
---|
| 903 |
|
---|
| 904 | /* a simple compressor
|
---|
| 905 | */
|
---|
[214] | 906 | size_t sh_util_compress (char * dest, char * src, size_t dest_size)
|
---|
[1] | 907 | {
|
---|
| 908 | char * add;
|
---|
| 909 | char * get;
|
---|
| 910 | size_t count = 0;
|
---|
| 911 | size_t dest_end;
|
---|
| 912 |
|
---|
| 913 | SL_ENTER(_("sh_util_compress"));
|
---|
| 914 |
|
---|
| 915 | if (dest_size == 0)
|
---|
| 916 | SL_RETURN((0), _("sh_util_compress"));
|
---|
| 917 |
|
---|
| 918 | if ((dest == NULL) || (src == NULL))
|
---|
| 919 | SL_RETURN((0), _("sh_util_compress"));
|
---|
| 920 |
|
---|
| 921 | dest_end = sl_strlen(dest);
|
---|
| 922 |
|
---|
| 923 | if (dest_end > dest_size)
|
---|
| 924 | SL_RETURN((0), _("sh_util_compress"));
|
---|
| 925 |
|
---|
| 926 | add = &dest[dest_end];
|
---|
| 927 | get = src;
|
---|
| 928 |
|
---|
| 929 | while (count < (dest_size-dest_end))
|
---|
| 930 | {
|
---|
| 931 | if (isalnum((int) *get))
|
---|
| 932 | {
|
---|
| 933 | *add = *get;
|
---|
| 934 | ++add;
|
---|
| 935 | ++count;
|
---|
| 936 | }
|
---|
| 937 | ++get;
|
---|
| 938 | if (*get == '\0' && (count < (dest_size-dest_end)))
|
---|
| 939 | /* end of src reached */
|
---|
| 940 | {
|
---|
| 941 | *add = *get; /* copy the '\0' */
|
---|
| 942 | break; /* and stop copying */
|
---|
| 943 | }
|
---|
| 944 | }
|
---|
| 945 |
|
---|
| 946 | dest[dest_size-1] = '\0'; /* paranoia */
|
---|
[214] | 947 | SL_RETURN((count), _("sh_util_compress")); /* no of chars copied */
|
---|
[1] | 948 | }
|
---|
| 949 |
|
---|
| 950 |
|
---|
| 951 | /* copy the four least significant bytes
|
---|
| 952 | */
|
---|
| 953 | void sh_util_cpylong (char * dest, const char * src, int len )
|
---|
| 954 | {
|
---|
| 955 | int i, j;
|
---|
| 956 | union
|
---|
| 957 | {
|
---|
| 958 | long l;
|
---|
| 959 | char c[sizeof(long)];
|
---|
| 960 | } u;
|
---|
[290] | 961 | #ifdef WORDS_BIGENDIAN
|
---|
| 962 | unsigned char swap;
|
---|
| 963 | unsigned char * ii = (unsigned char *) dest;
|
---|
| 964 | #endif
|
---|
[1] | 965 |
|
---|
| 966 | SL_ENTER(_("sh_util_cpylong"));
|
---|
| 967 |
|
---|
| 968 | u.l = 1;
|
---|
| 969 |
|
---|
| 970 | /* MSB is first
|
---|
| 971 | */
|
---|
| 972 | if (sizeof(long)>4 &&/*@+charint@*/(u.c[sizeof(long)-1] == 1)/*@-charint@*/)
|
---|
| 973 | {
|
---|
| 974 | j = (int) (sizeof(long)-4);
|
---|
| 975 | for (i = 0; i < j; ++i) ++src;
|
---|
| 976 | }
|
---|
| 977 |
|
---|
| 978 | i = 0;
|
---|
| 979 |
|
---|
| 980 | while (i < 4)
|
---|
| 981 | {
|
---|
| 982 | *dest = (*src);
|
---|
| 983 | ++dest; ++src;
|
---|
| 984 | if (i == (len-1)) break;
|
---|
| 985 | ++i;
|
---|
| 986 | }
|
---|
[290] | 987 | #ifdef WORDS_BIGENDIAN
|
---|
| 988 | swap = ii[0]; ii[0] = ii[3]; ii[3] = swap;
|
---|
| 989 | swap = ii[1]; ii[1] = ii[2]; ii[2] = swap;
|
---|
| 990 | #endif
|
---|
[1] | 991 | SL_RET0(_("sh_util_cpylong"));
|
---|
| 992 | }
|
---|
| 993 |
|
---|
| 994 | /* This is a maximally equidistributed combined Tausworthe
|
---|
| 995 | * generator. The sequence is,
|
---|
| 996 | *
|
---|
| 997 | * x_n = (s1_n ^ s2_n ^ s3_n)
|
---|
| 998 | *
|
---|
| 999 | * s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
|
---|
| 1000 | * s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
|
---|
| 1001 | * s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
|
---|
| 1002 | *
|
---|
| 1003 | * computed modulo 2^32. In the three formulas above '^' means
|
---|
| 1004 | * exclusive-or (C-notation), not exponentiation. Note that the
|
---|
| 1005 | * algorithm relies on the properties of 32-bit unsigned integers (it
|
---|
| 1006 | * is formally defined on bit-vectors of length 32).
|
---|
| 1007 | *
|
---|
| 1008 | * Stolen from GSL (GNU scientific library) and modified somewhat.
|
---|
| 1009 | * I am using UINT32, which is guaranteed to be 32 bits. Also made
|
---|
| 1010 | * sure that the initialization vector is valid.
|
---|
| 1011 | */
|
---|
| 1012 |
|
---|
| 1013 |
|
---|
[481] | 1014 | /* interval [0, 4294967295]
|
---|
[1] | 1015 | */
|
---|
| 1016 | static UINT32 taus_get_long (void *vstate)
|
---|
| 1017 | {
|
---|
| 1018 | UINT32 * state = (UINT32 *) vstate;
|
---|
| 1019 |
|
---|
[137] | 1020 | /*
|
---|
[1] | 1021 | if (skey->rngI == BAD)
|
---|
| 1022 | (void)taus_seed();
|
---|
[137] | 1023 | */
|
---|
[1] | 1024 |
|
---|
| 1025 | #define TAUSWORTHE(s,a,b,c,d) ((s &c) <<d) ^ (((s <<a) ^s) >>b)
|
---|
| 1026 | /*@+ignorequals@*/
|
---|
| 1027 | state[0] = TAUSWORTHE (state[0], 13, 19, 4294967294UL, 12);
|
---|
| 1028 | state[1] = TAUSWORTHE (state[1], 2, 25, 4294967288UL, 4);
|
---|
| 1029 | state[2] = TAUSWORTHE (state[2], 3, 11, 4294967280UL, 17);
|
---|
| 1030 | /*@-ignorequals@*/
|
---|
| 1031 | return (state[0] ^ state[1] ^ state[2]);
|
---|
| 1032 | }
|
---|
| 1033 |
|
---|
| 1034 | /* Hide the internal state of the PRNG by using its output as
|
---|
| 1035 | * input for a one-way hash function.
|
---|
| 1036 | */
|
---|
[11] | 1037 |
|
---|
[137] | 1038 | UINT32 taus_get ()
|
---|
[1] | 1039 | {
|
---|
[156] | 1040 | #define TAUS_SAMPLE 12
|
---|
| 1041 |
|
---|
| 1042 | UINT32 taus_svec[TAUS_SAMPLE];
|
---|
[1] | 1043 | UINT32 retval;
|
---|
| 1044 | UINT32 * res;
|
---|
[156] | 1045 | UINT32 * res_vec = &(skey->res_vec[0]);
|
---|
[13] | 1046 | static int res_num = 0;
|
---|
[1] | 1047 | register int i;
|
---|
[133] | 1048 | UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
|
---|
[1] | 1049 |
|
---|
[137] | 1050 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
[13] | 1051 | if (res_num > 0)
|
---|
| 1052 | {
|
---|
| 1053 | retval = res_vec[res_num];
|
---|
| 1054 | res_num = (res_num == 5) ? 0 : (res_num + 1);
|
---|
[137] | 1055 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey); /* alternative path */
|
---|
[13] | 1056 | return retval;
|
---|
| 1057 | }
|
---|
[137] | 1058 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
[13] | 1059 |
|
---|
[137] | 1060 | (void)taus_seed();
|
---|
[1] | 1061 |
|
---|
[137] | 1062 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
[156] | 1063 | for (i = 0; i < (TAUS_SAMPLE/3); ++i)
|
---|
| 1064 | {
|
---|
| 1065 | taus_svec[i*3] = taus_get_long (&(skey->rng0[0]));
|
---|
| 1066 | taus_svec[i*3+1] = taus_get_long (&(skey->rng1[0]));
|
---|
| 1067 | taus_svec[i*3+2] = taus_get_long (&(skey->rng2[0]));
|
---|
| 1068 | }
|
---|
[137] | 1069 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
| 1070 |
|
---|
[11] | 1071 | res = sh_tiger_hash_uint32 ( (char *) &taus_svec[0],
|
---|
[1] | 1072 | TIGER_DATA,
|
---|
[156] | 1073 | (unsigned long)(TAUS_SAMPLE * sizeof(UINT32)),
|
---|
[133] | 1074 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
[1] | 1075 |
|
---|
[137] | 1076 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
[13] | 1077 | for (i = 1; i < 6; ++i)
|
---|
[11] | 1078 | {
|
---|
[13] | 1079 | res_vec[i] = res[i];
|
---|
[11] | 1080 | }
|
---|
[156] | 1081 | retval = res[0];
|
---|
[13] | 1082 | res_num = 1;
|
---|
[137] | 1083 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
[1] | 1084 |
|
---|
[541] | 1085 | memset(taus_svec, 0, TAUS_SAMPLE * sizeof(UINT32));
|
---|
[1] | 1086 |
|
---|
| 1087 | return retval;
|
---|
| 1088 | }
|
---|
| 1089 |
|
---|
| 1090 | /* interval [0,1)
|
---|
| 1091 | */
|
---|
| 1092 | double taus_get_double (void *vstate)
|
---|
| 1093 | {
|
---|
| 1094 | return taus_get_long (vstate) / (4294967296.0 + 1.0) ;
|
---|
| 1095 | }
|
---|
| 1096 |
|
---|
| 1097 | #define LCG(n) ((69069 * n) & 0xffffffffUL)
|
---|
| 1098 |
|
---|
| 1099 | /* TAKE CARE: state[0], state[1], state[2] must be > 2,8,16, respectively
|
---|
| 1100 | */
|
---|
| 1101 | static void taus_set_from_ulong (void *vstate, unsigned long int s)
|
---|
| 1102 | {
|
---|
| 1103 | UINT32 *state = (UINT32 *) vstate;
|
---|
| 1104 |
|
---|
| 1105 | if (s == 0)
|
---|
| 1106 | s = 1; /* default seed is 1 */
|
---|
| 1107 |
|
---|
| 1108 | state[0] = (UINT32)(LCG (s) | (UINT32) 0x03);
|
---|
| 1109 | state[1] = (UINT32)(LCG (state[0]) | (UINT32) 0x09);
|
---|
| 1110 | state[2] = (UINT32)(LCG (state[1]) | (UINT32) 0x17);
|
---|
| 1111 |
|
---|
| 1112 | /* 'warm up'
|
---|
| 1113 | */
|
---|
| 1114 | (void) taus_get_long (state);
|
---|
| 1115 | (void) taus_get_long (state);
|
---|
| 1116 | (void) taus_get_long (state);
|
---|
| 1117 | (void) taus_get_long (state);
|
---|
| 1118 | (void) taus_get_long (state);
|
---|
| 1119 | (void) taus_get_long (state);
|
---|
| 1120 |
|
---|
| 1121 | return;
|
---|
| 1122 | }
|
---|
| 1123 |
|
---|
| 1124 | static void taus_set_from_state (void *vstate, void *init_state)
|
---|
| 1125 | {
|
---|
| 1126 | UINT32 *state = (UINT32 *) vstate;
|
---|
| 1127 | UINT32 *state0 = (UINT32 *) init_state;
|
---|
| 1128 |
|
---|
| 1129 | state[0] = state0[0] | (UINT32) 0x03;
|
---|
| 1130 | state[1] = state0[1] | (UINT32) 0x09;
|
---|
| 1131 | state[2] = state0[2] | (UINT32) 0x17;
|
---|
| 1132 |
|
---|
| 1133 | return;
|
---|
| 1134 | }
|
---|
| 1135 |
|
---|
| 1136 |
|
---|
| 1137 | int taus_seed ()
|
---|
| 1138 | {
|
---|
| 1139 | char bufx[9 * sizeof(UINT32) + 1];
|
---|
| 1140 | int status;
|
---|
[481] | 1141 | static unsigned long seed_time = 0;
|
---|
| 1142 | static unsigned long seed_counter = 3000;
|
---|
[137] | 1143 | unsigned long gtime;
|
---|
[1] | 1144 |
|
---|
| 1145 | SL_ENTER(_("taus_seed"));
|
---|
| 1146 |
|
---|
| 1147 | if (skey->rngI == GOOD)
|
---|
| 1148 | {
|
---|
[481] | 1149 | ++seed_counter;
|
---|
| 1150 |
|
---|
| 1151 | if ( ((sh_unix_longtime () - seed_time) < 1800) &&
|
---|
| 1152 | ( seed_counter < 3000))
|
---|
[1] | 1153 | SL_RETURN( (0), _("taus_seed"));
|
---|
| 1154 | }
|
---|
| 1155 |
|
---|
[481] | 1156 | seed_time = sh_unix_longtime ();
|
---|
| 1157 | seed_counter = 0;
|
---|
[1] | 1158 |
|
---|
| 1159 | status = sh_entropy (24, bufx);
|
---|
| 1160 |
|
---|
| 1161 | if (!SL_ISERROR(status))
|
---|
| 1162 | {
|
---|
[137] | 1163 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
[1] | 1164 | memcpy (&skey->rng0[0], &bufx[0], 2*sizeof(UINT32));
|
---|
| 1165 | memcpy (&skey->rng1[0], &bufx[2*sizeof(UINT32)], 2*sizeof(UINT32));
|
---|
| 1166 | memcpy (&skey->rng2[0], &bufx[4*sizeof(UINT32)], 2*sizeof(UINT32));
|
---|
| 1167 | memset (bufx, 0, 9 * sizeof(UINT32) + 1);
|
---|
| 1168 |
|
---|
| 1169 | skey->rng0[2] = 0;
|
---|
| 1170 | skey->rng1[2] = 0;
|
---|
| 1171 | skey->rng2[2] = 0;
|
---|
| 1172 |
|
---|
| 1173 | taus_set_from_state( &(skey->rng0[0]), &(skey->rng0[0]));
|
---|
| 1174 | taus_set_from_state( &(skey->rng1[0]), &(skey->rng1[0]));
|
---|
| 1175 | taus_set_from_state( &(skey->rng2[0]), &(skey->rng2[0]));
|
---|
| 1176 |
|
---|
[137] | 1177 | skey->rngI = GOOD;
|
---|
| 1178 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
[1] | 1179 | SL_RETURN( (0), _("taus_seed"));
|
---|
| 1180 | }
|
---|
| 1181 |
|
---|
| 1182 | sh_error_handle ((-1), FIL__, __LINE__, status, MSG_ES_ENT,
|
---|
| 1183 | _("sh_entropy"));
|
---|
| 1184 |
|
---|
| 1185 | /* emergency backup - unsafe !
|
---|
| 1186 | */
|
---|
| 1187 | #ifdef HAVE_GETTIMEOFDAY
|
---|
[137] | 1188 | gtime = sh_unix_notime();
|
---|
[1] | 1189 | #else
|
---|
[137] | 1190 | gtime = seed_time;
|
---|
[1] | 1191 | #endif
|
---|
[137] | 1192 |
|
---|
| 1193 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
| 1194 | taus_set_from_ulong ( &(skey->rng0[0]), LCG (gtime) );
|
---|
[1] | 1195 | taus_set_from_ulong ( &(skey->rng1[0]), LCG (skey->rng0[0]) );
|
---|
| 1196 | taus_set_from_ulong ( &(skey->rng2[0]), LCG (skey->rng1[0]) );
|
---|
| 1197 | skey->rngI = BAD;
|
---|
[137] | 1198 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
[1] | 1199 |
|
---|
| 1200 | SL_RETURN( (-1), _("taus_seed"));
|
---|
| 1201 | }
|
---|
| 1202 |
|
---|
| 1203 | /*@+charint@*/
|
---|
| 1204 | static unsigned char new_key[] = { 0xA7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xA7 };
|
---|
| 1205 | /*@-charint@*/
|
---|
| 1206 | static void copy_four (unsigned char * dest, UINT32 in);
|
---|
| 1207 |
|
---|
[20] | 1208 | int sh_util_set_newkey (const char * new_in)
|
---|
[1] | 1209 | {
|
---|
| 1210 | size_t i, j = 0;
|
---|
| 1211 | size_t len;
|
---|
| 1212 | SL_TICKET fp;
|
---|
| 1213 | SL_TICKET fout;
|
---|
| 1214 | char * key;
|
---|
[504] | 1215 | char * path = NULL;
|
---|
[22] | 1216 | char * outpath = NULL;
|
---|
[1] | 1217 | unsigned char * image = NULL;
|
---|
| 1218 | long s = 0;
|
---|
| 1219 | long ilen = 0;
|
---|
| 1220 | long ii, k = 0;
|
---|
| 1221 | UINT32 * h1;
|
---|
[22] | 1222 | char * new = NULL;
|
---|
[1] | 1223 |
|
---|
| 1224 | if (0 != sl_is_suid())
|
---|
| 1225 | {
|
---|
[210] | 1226 | fprintf(stderr, "%s", _("ERROR: insufficient privilege\n"));
|
---|
[1] | 1227 | _exit (EXIT_FAILURE);
|
---|
| 1228 | /*@notreached@*/
|
---|
| 1229 | return -1; /* braindead MAC OSX compiler needs this */
|
---|
| 1230 | }
|
---|
| 1231 |
|
---|
[20] | 1232 | if (new_in == NULL || new_in[0] == '\0')
|
---|
[1] | 1233 | {
|
---|
[210] | 1234 | fprintf(stderr, "%s",
|
---|
[1] | 1235 | _("ERROR: no key given\n Argument must be 'key@path'\n"));
|
---|
| 1236 | _exit (EXIT_FAILURE);
|
---|
| 1237 | /*@notreached@*/
|
---|
| 1238 | return -1;
|
---|
| 1239 | }
|
---|
[20] | 1240 |
|
---|
[570] | 1241 | len = strlen(new_in) + 1;
|
---|
| 1242 | if (NULL == (new = calloc(1, len)))
|
---|
[20] | 1243 | goto bail_mem;
|
---|
[570] | 1244 | memcpy(new, new_in, len);
|
---|
[20] | 1245 |
|
---|
[1] | 1246 | key = new;
|
---|
| 1247 | len = strlen(new);
|
---|
| 1248 | for (i = 1; i < (len-2); ++i)
|
---|
| 1249 | {
|
---|
| 1250 | if (new[i] == '@' && new[i+1] == '/')
|
---|
| 1251 | {
|
---|
| 1252 | j = i+1; new[i] = '\0'; break;
|
---|
| 1253 | }
|
---|
| 1254 | }
|
---|
| 1255 | if (j == 0)
|
---|
| 1256 | {
|
---|
[210] | 1257 | fprintf(stderr, "%s",
|
---|
[1] | 1258 | _("ERROR: no path to executable given\n Argument must be 'key@path'\n"));
|
---|
[22] | 1259 | free(new);
|
---|
[1] | 1260 | _exit (EXIT_FAILURE);
|
---|
| 1261 | /*@notreached@*/
|
---|
| 1262 | return -1;
|
---|
| 1263 | }
|
---|
| 1264 | else
|
---|
| 1265 | path = &new[j];
|
---|
[22] | 1266 |
|
---|
| 1267 | len = strlen(path) + 1 + 4;
|
---|
[1] | 1268 | /*@-usedef@*/
|
---|
[454] | 1269 | if (NULL == (outpath = calloc(1,len)))
|
---|
[1] | 1270 | goto bail_mem;
|
---|
| 1271 | /*@-usedef@*/
|
---|
[22] | 1272 | sl_snprintf (outpath, len, _("%s.out"), path);
|
---|
[1] | 1273 |
|
---|
[248] | 1274 | fp = sl_open_read(FIL__, __LINE__, path, SL_NOPRIV);
|
---|
[1] | 1275 | if (SL_ISERROR(fp))
|
---|
| 1276 | {
|
---|
| 1277 | fprintf(stderr,
|
---|
| 1278 | _("ERROR: cannot open %s for read (errnum = %ld)\n"), path, fp);
|
---|
[22] | 1279 | free(new); free (outpath);
|
---|
[1] | 1280 | _exit (EXIT_FAILURE);
|
---|
| 1281 | /*@notreached@*/
|
---|
| 1282 | return -1;
|
---|
| 1283 | }
|
---|
| 1284 |
|
---|
[248] | 1285 | fout = sl_open_write(FIL__, __LINE__, outpath, SL_NOPRIV);
|
---|
[1] | 1286 | if (SL_ISERROR(fout))
|
---|
| 1287 | {
|
---|
| 1288 | fprintf(stderr,
|
---|
| 1289 | _("ERROR: cannot open %s (errnum = %ld)\n"), outpath, fout);
|
---|
[22] | 1290 | free(new); free (outpath);
|
---|
[1] | 1291 | _exit (EXIT_FAILURE);
|
---|
| 1292 | /*@notreached@*/
|
---|
| 1293 | return -1;
|
---|
| 1294 | }
|
---|
| 1295 |
|
---|
| 1296 |
|
---|
[454] | 1297 | image = calloc(1,4096);
|
---|
[1] | 1298 | if (!image)
|
---|
| 1299 | goto bail_mem;
|
---|
| 1300 | while (0 < (ii = sl_read (fp, &image[s], 4096)))
|
---|
| 1301 | {
|
---|
[473] | 1302 | unsigned char * ptr;
|
---|
[1] | 1303 | ilen += ii;
|
---|
| 1304 | s += 4096;
|
---|
[473] | 1305 | ptr = realloc (image, (size_t) (4096 + s));
|
---|
| 1306 | if (ptr)
|
---|
| 1307 | image = ptr;
|
---|
| 1308 | else
|
---|
| 1309 | { free(image); image = NULL; }
|
---|
[1] | 1310 | if (!image)
|
---|
| 1311 | goto bail_mem;
|
---|
| 1312 | }
|
---|
| 1313 |
|
---|
| 1314 | printf(_("%ld bytes read\n"), ilen);
|
---|
| 1315 |
|
---|
| 1316 |
|
---|
| 1317 | for (k = 0; k < (ilen - 8); ++k)
|
---|
| 1318 | {
|
---|
| 1319 | if (image[k] == new_key[0] &&
|
---|
| 1320 | image[k+1] == new_key[1] &&
|
---|
| 1321 | image[k+2] == new_key[2] &&
|
---|
| 1322 | image[k+3] == new_key[3] &&
|
---|
| 1323 | image[k+4] == new_key[4] &&
|
---|
| 1324 | image[k+5] == new_key[5] &&
|
---|
| 1325 | image[k+6] == new_key[6] &&
|
---|
| 1326 | image[k+7] == new_key[7])
|
---|
| 1327 | {
|
---|
[133] | 1328 | UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
|
---|
| 1329 |
|
---|
[210] | 1330 | printf("%s", _("old key found\n"));
|
---|
[1] | 1331 | h1 = sh_tiger_hash_uint32 (key, TIGER_DATA,
|
---|
[133] | 1332 | (unsigned long)strlen(key),
|
---|
| 1333 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
[1] | 1334 | copy_four( (unsigned char *) &(image[k]), h1[0]);
|
---|
| 1335 | copy_four( (unsigned char *) &(image[k+4]), h1[1]);
|
---|
| 1336 | (void) sl_write (fout, image, ilen);
|
---|
| 1337 | (void) sl_close (fout);
|
---|
| 1338 | printf(_("new file %s written\n"), outpath);
|
---|
[22] | 1339 | free(new); free (outpath); free(image);
|
---|
[1] | 1340 | _exit (EXIT_SUCCESS);
|
---|
| 1341 | /*@notreached@*/
|
---|
| 1342 | return 0;
|
---|
| 1343 | }
|
---|
| 1344 | }
|
---|
| 1345 |
|
---|
[210] | 1346 | fprintf(stderr, "%s",
|
---|
[1] | 1347 | _("ERROR: old key not found\n"));
|
---|
[22] | 1348 | free(new); free (outpath); free(image);
|
---|
[1] | 1349 | _exit (EXIT_FAILURE);
|
---|
| 1350 | /*@notreached@*/
|
---|
| 1351 | return -1;
|
---|
| 1352 |
|
---|
| 1353 |
|
---|
| 1354 | bail_mem:
|
---|
[210] | 1355 | fprintf(stderr, "%s",
|
---|
[1] | 1356 | _("ERROR: out of memory\n"));
|
---|
[22] | 1357 | if (new) free(new);
|
---|
| 1358 | if (outpath) free (outpath);
|
---|
| 1359 | if (image) free (image);
|
---|
[1] | 1360 | _exit (EXIT_FAILURE);
|
---|
| 1361 | /*@notreached@*/
|
---|
| 1362 | return -1;
|
---|
| 1363 | }
|
---|
| 1364 |
|
---|
| 1365 |
|
---|
| 1366 |
|
---|
| 1367 |
|
---|
| 1368 | /* A simple en-/decoder, based on Vernam cipher. We use the
|
---|
| 1369 | * message as salt to hide the key by obtaining a different one-time
|
---|
| 1370 | * pad each time.
|
---|
| 1371 | * Should be safe against a listener on the network, but not against someone
|
---|
| 1372 | * with read access to the binary.
|
---|
| 1373 | */
|
---|
| 1374 | void sh_util_encode (char * data, char * salt, int mode, char fill)
|
---|
| 1375 | {
|
---|
| 1376 | static char cc1[17] = N_("0123456789ABCDEF");
|
---|
| 1377 | char cc[17] = "\0";
|
---|
| 1378 | register int i, j, j1 = 0, j2 = 0, j3;
|
---|
| 1379 | char * dez;
|
---|
[133] | 1380 | char hashbuf[KEYBUF_SIZE];
|
---|
[1] | 1381 |
|
---|
| 1382 | SL_ENTER(_("sh_util_encode"));
|
---|
| 1383 |
|
---|
| 1384 | /* init
|
---|
| 1385 | */
|
---|
| 1386 | (void) sl_strlcpy( cc, _(cc1), sizeof(cc));
|
---|
| 1387 |
|
---|
| 1388 | /* max 128 bits keyspace
|
---|
| 1389 | */
|
---|
| 1390 | memset (skey->vernam, (int)fill, KEY_LEN+1);
|
---|
| 1391 |
|
---|
| 1392 | dez = (char *) &(skey->ErrFlag[0]);
|
---|
| 1393 | sh_util_cpylong (skey->vernam, dez, 4);
|
---|
| 1394 | dez = (char *) &(skey->ErrFlag[1]);
|
---|
| 1395 | sh_util_cpylong (&skey->vernam[4], dez, 4);
|
---|
| 1396 |
|
---|
| 1397 | skey->vernam[KEY_LEN] = '\0';
|
---|
| 1398 |
|
---|
| 1399 | (void) sl_strlcpy(skey->vernam,
|
---|
[133] | 1400 | sh_tiger_hash(skey->vernam, TIGER_DATA, KEY_LEN,
|
---|
| 1401 | hashbuf, sizeof(hashbuf)),
|
---|
[1] | 1402 | KEY_LEN+1);
|
---|
| 1403 |
|
---|
| 1404 | (void) sl_strlcpy(skey->vernam,
|
---|
[133] | 1405 | sh_util_hmac_tiger (skey->vernam, salt, strlen(salt),
|
---|
| 1406 | hashbuf, sizeof(hashbuf)),
|
---|
| 1407 | KEY_LEN+1);
|
---|
[1] | 1408 |
|
---|
| 1409 | (void) sl_strlcpy(skey->vernam,
|
---|
[133] | 1410 | sh_util_hmac_tiger (skey->vernam, (char*) new_key, 8,
|
---|
| 1411 | hashbuf, sizeof(hashbuf)),
|
---|
[1] | 1412 | KEY_LEN+1);
|
---|
| 1413 |
|
---|
| 1414 | /* The following routine adds/subtracts data[j] and vernam[j] mod 16.
|
---|
| 1415 | */
|
---|
| 1416 | j = 0;
|
---|
| 1417 | while (j < KEY_LEN)
|
---|
| 1418 | {
|
---|
| 1419 | for (i = 0; i < 16; ++i)
|
---|
| 1420 | {
|
---|
| 1421 | if (cc[i] == data[j]) j1 = i;
|
---|
| 1422 | if (cc[i] == skey->vernam[j]) j2 = i;
|
---|
| 1423 | }
|
---|
| 1424 | if (mode == 0)
|
---|
| 1425 | {
|
---|
| 1426 | j3 = j1 + j2;
|
---|
| 1427 | if (j3 > 15) j3 -= 16;
|
---|
| 1428 | data[j] = cc[j3];
|
---|
| 1429 | }
|
---|
| 1430 | else
|
---|
| 1431 | {
|
---|
| 1432 | j3 = j1 - j2;
|
---|
| 1433 | if (j3 < 0) j3 += 16;
|
---|
| 1434 | data[j] = cc[j3];
|
---|
| 1435 | }
|
---|
| 1436 | ++j;
|
---|
| 1437 | }
|
---|
| 1438 | SL_RET0(_("sh_util_encode"));
|
---|
| 1439 | }
|
---|
| 1440 |
|
---|
| 1441 | /* server mode
|
---|
| 1442 | */
|
---|
[20] | 1443 | int sh_util_setserver (const char * dummy)
|
---|
[1] | 1444 | {
|
---|
| 1445 | SL_ENTER(_("sh_util_setserver"));
|
---|
| 1446 |
|
---|
[20] | 1447 | (void) dummy;
|
---|
| 1448 | sh.flag.isserver = GOOD;
|
---|
[1] | 1449 | SL_RETURN((0),_("sh_util_setserver"));
|
---|
| 1450 | }
|
---|
| 1451 |
|
---|
| 1452 |
|
---|
[20] | 1453 | int sh_util_setlooptime (const char * str)
|
---|
[1] | 1454 | {
|
---|
| 1455 | int i = atoi (str);
|
---|
| 1456 |
|
---|
| 1457 | SL_ENTER(_("sh_util_setlooptime"));
|
---|
| 1458 |
|
---|
| 1459 | if (i >= 0 && i < INT_MAX) {
|
---|
| 1460 | sh.looptime = i;
|
---|
| 1461 | SL_RETURN((0),_("sh_util_setlooptime"));
|
---|
| 1462 | } else {
|
---|
| 1463 | sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
|
---|
| 1464 | _("loop time"), str);
|
---|
| 1465 | SL_RETURN((-1),_("sh_util_setlooptime"));
|
---|
| 1466 | }
|
---|
| 1467 | }
|
---|
| 1468 |
|
---|
| 1469 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
[20] | 1470 | int sh_util_setchecksum (const char * str)
|
---|
[1] | 1471 | {
|
---|
| 1472 | static int reject = 0;
|
---|
| 1473 |
|
---|
| 1474 | SL_ENTER(_("sh_util_setchecksum"));
|
---|
| 1475 |
|
---|
| 1476 | if (reject == 1)
|
---|
| 1477 | SL_RETURN((0), _("sh_util_setchecksum"));
|
---|
| 1478 | reject = 1;
|
---|
| 1479 |
|
---|
| 1480 | if (sl_strncmp (str, _("init"), sizeof("init")-1) == 0)
|
---|
| 1481 | {
|
---|
| 1482 | sh.flag.checkSum = SH_CHECK_INIT;
|
---|
| 1483 | }
|
---|
| 1484 | else if (sl_strncmp (str, _("update"), sizeof("update")-1) == 0)
|
---|
| 1485 | {
|
---|
| 1486 | if (S_TRUE == file_is_remote())
|
---|
| 1487 | {
|
---|
| 1488 | sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
|
---|
| 1489 | _("checksum testing"), str);
|
---|
| 1490 | SL_RETURN((-1), _("sh_util_setchecksum"));
|
---|
| 1491 | }
|
---|
| 1492 | else
|
---|
| 1493 | {
|
---|
| 1494 | sh.flag.checkSum = SH_CHECK_CHECK;
|
---|
| 1495 | sh.flag.update = S_TRUE;
|
---|
| 1496 | }
|
---|
| 1497 | }
|
---|
| 1498 | else if (sl_strncmp (str, _("check"), sizeof("check")-1) == 0)
|
---|
| 1499 | {
|
---|
| 1500 | sh.flag.checkSum = SH_CHECK_CHECK;
|
---|
| 1501 | }
|
---|
| 1502 | /*
|
---|
| 1503 | else if (sl_strncmp (str, _("update"), sizeof("update")-1) == 0)
|
---|
| 1504 | {
|
---|
| 1505 | sh.flag.checkSum = SH_CHECK_INIT;
|
---|
| 1506 | sh.flag.update = S_TRUE;
|
---|
| 1507 | }
|
---|
| 1508 | */
|
---|
| 1509 | else if (sl_strncmp (str, _("none"), sizeof("none")-1) == 0)
|
---|
| 1510 | {
|
---|
| 1511 | sh.flag.checkSum = SH_CHECK_NONE;
|
---|
| 1512 | }
|
---|
| 1513 | else
|
---|
| 1514 | {
|
---|
| 1515 | sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
|
---|
| 1516 | _("checksum testing"), str);
|
---|
| 1517 | SL_RETURN((-1), _("sh_util_setchecksum"));
|
---|
| 1518 | }
|
---|
| 1519 | SL_RETURN((0), _("sh_util_setchecksum"));
|
---|
| 1520 | }
|
---|
| 1521 | #endif
|
---|
| 1522 |
|
---|
| 1523 | /*@+charint@*/
|
---|
| 1524 | unsigned char TcpFlag[8][PW_LEN+1] = {
|
---|
| 1525 | #if (POS_TF == 1)
|
---|
| 1526 | { 0xF7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xF7,0x00 },
|
---|
| 1527 | #endif
|
---|
| 1528 | { 0xFF,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xFF,0x00 },
|
---|
| 1529 | #if (POS_TF == 2)
|
---|
| 1530 | { 0xF7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xF7,0x00 },
|
---|
| 1531 | #endif
|
---|
| 1532 | { 0xFF,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xFF,0x00 },
|
---|
| 1533 | #if (POS_TF == 3)
|
---|
| 1534 | { 0xF7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xF7,0x00 },
|
---|
| 1535 | #endif
|
---|
| 1536 | { 0xFF,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xFF,0x00 },
|
---|
| 1537 | #if (POS_TF == 4)
|
---|
| 1538 | { 0xF7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xF7,0x00 },
|
---|
| 1539 | #endif
|
---|
| 1540 | { 0xFF,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xFF,0x00 },
|
---|
| 1541 | #if (POS_TF == 5)
|
---|
| 1542 | { 0xF7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xF7,0x00 },
|
---|
| 1543 | #endif
|
---|
| 1544 | { 0xFF,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xFF,0x00 },
|
---|
| 1545 | #if (POS_TF == 6)
|
---|
| 1546 | { 0xF7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xF7,0x00 },
|
---|
| 1547 | #endif
|
---|
| 1548 | { 0xFF,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xFF,0x00 },
|
---|
| 1549 | #if (POS_TF == 7)
|
---|
| 1550 | { 0xF7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xF7,0x00 },
|
---|
| 1551 | #endif
|
---|
| 1552 | { 0xFF,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xFF,0x00 },
|
---|
| 1553 | #if (POS_TF == 8)
|
---|
| 1554 | { 0xF7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xF7,0x00 },
|
---|
| 1555 | #endif
|
---|
| 1556 | };
|
---|
| 1557 | /*@-charint@*/
|
---|
| 1558 |
|
---|
| 1559 | /* initialize a key to a random value
|
---|
| 1560 | * rev 0.8
|
---|
| 1561 | */
|
---|
| 1562 | int sh_util_keyinit (char * buf, long size)
|
---|
| 1563 | {
|
---|
| 1564 | UINT32 bufy[6];
|
---|
| 1565 | int i;
|
---|
| 1566 | int status = 0;
|
---|
| 1567 | char * p;
|
---|
[133] | 1568 | char hashbuf[KEYBUF_SIZE];
|
---|
[1] | 1569 |
|
---|
| 1570 | SL_ENTER(_("sh_util_keyinit"));
|
---|
| 1571 |
|
---|
| 1572 | ASSERT((size <= KEY_LEN+1), _("size <= KEY_LEN+1"))
|
---|
| 1573 |
|
---|
| 1574 | if (size > KEY_LEN+1)
|
---|
| 1575 | size = KEY_LEN+1;
|
---|
| 1576 |
|
---|
| 1577 | /* seed / re-seed the PRNG if required
|
---|
| 1578 | */
|
---|
| 1579 | status = taus_seed ();
|
---|
| 1580 |
|
---|
| 1581 | if (status == -1)
|
---|
| 1582 | sh_error_handle ((-1), FIL__, __LINE__, -1, MSG_ES_KEY1,
|
---|
| 1583 | _("taus_seed"));
|
---|
| 1584 |
|
---|
| 1585 | for (i = 0; i < 6; ++i)
|
---|
[170] | 1586 | bufy[i] = taus_get();
|
---|
[1] | 1587 |
|
---|
| 1588 | p = sh_tiger_hash ((char *) bufy, TIGER_DATA,
|
---|
[133] | 1589 | (unsigned long)(6*sizeof(UINT32)),
|
---|
| 1590 | hashbuf, sizeof(hashbuf));
|
---|
[1] | 1591 |
|
---|
| 1592 | i = sl_strlcpy(buf, p, (size_t)size);
|
---|
| 1593 |
|
---|
| 1594 | memset (bufy, 0, 6*sizeof(UINT32));
|
---|
| 1595 |
|
---|
| 1596 | if ((status == 0) && (!SL_ISERROR(i)) )
|
---|
| 1597 | SL_RETURN((0),_("sh_util_keyinit"));
|
---|
| 1598 |
|
---|
| 1599 | if (SL_ISERROR(i))
|
---|
| 1600 | sh_error_handle ((-1), FIL__, __LINE__, i, MSG_ES_KEY2,
|
---|
| 1601 | _("sl_strlcpy"));
|
---|
| 1602 |
|
---|
| 1603 | SL_RETURN((-1),_("sh_util_keyinit"));
|
---|
| 1604 | }
|
---|
| 1605 |
|
---|
| 1606 | #if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
|
---|
| 1607 |
|
---|
| 1608 | static unsigned char sh_obscure_index[256];
|
---|
[76] | 1609 | static int sh_obscure_no_check = S_FALSE;
|
---|
[1] | 1610 |
|
---|
[68] | 1611 | int sh_util_valid_utf8 (const unsigned char * str)
|
---|
| 1612 | {
|
---|
[76] | 1613 | const int sh_val_utf8_1 = 1;
|
---|
| 1614 | const int sh_val_utf8_2 = 2;
|
---|
| 1615 | const int sh_val_utf8_3 = 3;
|
---|
| 1616 | const int sh_val_utf8_4 = 4;
|
---|
| 1617 |
|
---|
[170] | 1618 | size_t len = strlen((const char *)str);
|
---|
[68] | 1619 | size_t l = 0;
|
---|
[76] | 1620 | int typ = 0;
|
---|
| 1621 | unsigned char c = '\0';
|
---|
| 1622 | unsigned char c2[2] = { 0x00, 0x00 };
|
---|
| 1623 | unsigned char c3[3] = { 0x00, 0x00, 0x00 };
|
---|
[68] | 1624 |
|
---|
[76] | 1625 |
|
---|
[68] | 1626 | #define SH_VAL_UTF8_1 ((c != '\0') && ((c & 0x80) == 0x00))
|
---|
| 1627 | #define SH_VAL_UTF8_2 ((c != '\0') && ((c & 0xE0) == 0xC0)) /* 110x xxxx */
|
---|
| 1628 | #define SH_VAL_UTF8_3 ((c != '\0') && ((c & 0xF0) == 0xE0)) /* 1110 xxxx */
|
---|
| 1629 | #define SH_VAL_UTF8_4 ((c != '\0') && ((c & 0xF8) == 0xF0)) /* 1111 0xxx */
|
---|
| 1630 | #define SH_VAL_UTF8_N ((c != '\0') && ((c & 0xC0) == 0x80)) /* 10xx xxxx */
|
---|
[76] | 1631 | #define SH_VAL_BAD ((c == '"') || (c == '\t') || (c == '\b') || \
|
---|
| 1632 | (c == '\f') || (c == '\n') || \
|
---|
[68] | 1633 | (c == '\r') || (c == '\v') || iscntrl((int) c) || \
|
---|
| 1634 | (c != ' ' && !isgraph ((int) c)))
|
---|
| 1635 |
|
---|
| 1636 | while(l < len)
|
---|
| 1637 | {
|
---|
| 1638 | c = str[l];
|
---|
| 1639 |
|
---|
| 1640 | if (SH_VAL_UTF8_1)
|
---|
| 1641 | {
|
---|
[76] | 1642 | if (!(SH_VAL_BAD && (sh_obscure_index[c] != 1)))
|
---|
| 1643 | {
|
---|
| 1644 | typ = sh_val_utf8_1;
|
---|
| 1645 | ++l; continue;
|
---|
| 1646 | }
|
---|
| 1647 | else
|
---|
| 1648 | {
|
---|
| 1649 | return S_FALSE;
|
---|
| 1650 | }
|
---|
[68] | 1651 | }
|
---|
| 1652 | else if (SH_VAL_UTF8_2)
|
---|
| 1653 | {
|
---|
[76] | 1654 | typ = sh_val_utf8_2;
|
---|
| 1655 | c2[0] = c;
|
---|
| 1656 | if ((c & 0x3e) != 0x00) /* !(overlong 2-byte seq.) */
|
---|
| 1657 | {
|
---|
| 1658 | ++l;
|
---|
| 1659 | if (l != len) {
|
---|
| 1660 | c = str[l];
|
---|
| 1661 | if(SH_VAL_UTF8_N) {
|
---|
| 1662 | c2[1] = c;
|
---|
| 1663 | ++l; continue;
|
---|
| 1664 | }
|
---|
| 1665 | else {
|
---|
| 1666 | return S_FALSE;
|
---|
| 1667 | }
|
---|
| 1668 | }
|
---|
| 1669 | else {
|
---|
| 1670 | return S_FALSE;
|
---|
| 1671 | }
|
---|
| 1672 | }
|
---|
| 1673 | else
|
---|
| 1674 | {
|
---|
| 1675 | return S_FALSE; /* overlong 2-byte seq. */
|
---|
| 1676 | }
|
---|
[68] | 1677 | }
|
---|
| 1678 | else if (SH_VAL_UTF8_3)
|
---|
| 1679 | {
|
---|
[76] | 1680 | typ = sh_val_utf8_3;
|
---|
| 1681 | c3[0] = c;
|
---|
[68] | 1682 | ++l; if (l == len) return S_FALSE; c = str[l];
|
---|
| 1683 | if(!SH_VAL_UTF8_N) return S_FALSE;
|
---|
| 1684 | if (((str[l-1] & 0x1F) == 0x00) && ((c & 0x60) == 0x00))
|
---|
| 1685 | return S_FALSE; /* overlong 3-byte seq. */
|
---|
[76] | 1686 | c3[1] = c;
|
---|
[68] | 1687 | ++l; if (l == len) return S_FALSE; c = str[l];
|
---|
| 1688 | if(!SH_VAL_UTF8_N) return S_FALSE;
|
---|
[76] | 1689 | c3[2] = c;
|
---|
[68] | 1690 | ++l; continue;
|
---|
| 1691 | }
|
---|
| 1692 | else if (SH_VAL_UTF8_4)
|
---|
| 1693 | {
|
---|
[76] | 1694 | typ = sh_val_utf8_4;
|
---|
[68] | 1695 | ++l; if (l == len) return S_FALSE; c = str[l];
|
---|
| 1696 | if(!SH_VAL_UTF8_N) return S_FALSE;
|
---|
| 1697 | if (((str[l-1] & 0x0F) == 0x00) && ((c & 0x70) == 0x00))
|
---|
| 1698 | return S_FALSE; /* overlong 4-byte seq. */
|
---|
| 1699 | ++l; if (l == len) return S_FALSE; c = str[l];
|
---|
| 1700 | if(!SH_VAL_UTF8_N) return S_FALSE;
|
---|
| 1701 | ++l; if (l == len) return S_FALSE; c = str[l];
|
---|
| 1702 | if(!SH_VAL_UTF8_N) return S_FALSE;
|
---|
| 1703 | ++l; continue;
|
---|
| 1704 | }
|
---|
| 1705 | return S_FALSE;
|
---|
| 1706 | }
|
---|
[76] | 1707 |
|
---|
| 1708 | /* last character is invisible (space or else)
|
---|
| 1709 | */
|
---|
| 1710 | if (typ == sh_val_utf8_1)
|
---|
| 1711 | {
|
---|
| 1712 | if (c != ' ')
|
---|
| 1713 | return S_TRUE;
|
---|
| 1714 | else
|
---|
| 1715 | return S_FALSE;
|
---|
| 1716 | }
|
---|
| 1717 | else if (typ == sh_val_utf8_2)
|
---|
| 1718 | {
|
---|
| 1719 | if (c2[0] == 0xC2 && c2[1] == 0xA0) /* nbsp */
|
---|
| 1720 | return S_FALSE;
|
---|
| 1721 | else
|
---|
| 1722 | return S_TRUE;
|
---|
| 1723 | }
|
---|
| 1724 | else if (typ == sh_val_utf8_3)
|
---|
| 1725 | {
|
---|
| 1726 | if (c3[0] == 0xE2)
|
---|
| 1727 | {
|
---|
| 1728 | if (c3[1] == 0x80 && c3[2] >= 0x80 && c3[2] <= 0x8F)
|
---|
| 1729 | return S_FALSE; /* various spaces, left-to-right, right-to-left */
|
---|
| 1730 | else if (c3[1] == 0x80 && (c3[2] == 0xA8 || c3[2] == 0xA9 ||
|
---|
| 1731 | c3[2] == 0xAD || c3[2] == 0xAF))
|
---|
| 1732 | return S_FALSE; /* line sep, para sep, zw word joiner, nnbsp */
|
---|
| 1733 | else if (c3[1] == 0x81 && (c3[2] == 0xA0 || c3[2] == 0xA1 ||
|
---|
| 1734 | c3[2] == 0x9F))
|
---|
| 1735 | return S_FALSE; /* word joiner, function app, math space */
|
---|
| 1736 | else
|
---|
| 1737 | return S_TRUE;
|
---|
| 1738 | }
|
---|
| 1739 | else if (c3[0] == 0xE3 && c3[1] == 0x80 && c3[2] == 0x80)
|
---|
| 1740 | {
|
---|
| 1741 | return S_FALSE; /* ideographic space */
|
---|
| 1742 | }
|
---|
| 1743 | else if (c3[0] == 0xEF && c3[1] == 0xBB && c3[2] == 0xBF)
|
---|
| 1744 | {
|
---|
| 1745 | return S_FALSE; /* zwnbsp */
|
---|
| 1746 | }
|
---|
| 1747 | else
|
---|
| 1748 | {
|
---|
| 1749 | return S_TRUE;
|
---|
| 1750 | }
|
---|
| 1751 | }
|
---|
| 1752 | else
|
---|
| 1753 | {
|
---|
| 1754 | return S_TRUE;
|
---|
| 1755 | }
|
---|
[68] | 1756 | }
|
---|
| 1757 |
|
---|
| 1758 |
|
---|
[22] | 1759 | int sh_util_obscure_ok (const char * str)
|
---|
[1] | 1760 | {
|
---|
| 1761 | unsigned long i;
|
---|
[22] | 1762 | char * endptr = NULL;
|
---|
[1] | 1763 |
|
---|
[29] | 1764 | SL_ENTER(_("sh_util_obscure_ok"));
|
---|
[1] | 1765 |
|
---|
| 1766 | if (0 == sl_strncmp("all", str, 3))
|
---|
| 1767 | {
|
---|
| 1768 | for (i = 0; i < 255; ++i)
|
---|
| 1769 | {
|
---|
| 1770 | sh_obscure_index[i] = (unsigned char)1;
|
---|
| 1771 | }
|
---|
[76] | 1772 | sh_obscure_no_check = S_TRUE;
|
---|
[29] | 1773 | SL_RETURN(0, _("sh_util_obscure_ok"));
|
---|
[1] | 1774 | }
|
---|
| 1775 |
|
---|
[76] | 1776 | sh_obscure_no_check = S_FALSE;
|
---|
| 1777 |
|
---|
[1] | 1778 | for (i = 0; i < 255; ++i)
|
---|
| 1779 | {
|
---|
| 1780 | sh_obscure_index[i] = (unsigned char)0;
|
---|
| 1781 | }
|
---|
| 1782 |
|
---|
[22] | 1783 | i = strtoul (str, &endptr, 0);
|
---|
| 1784 | if (i > 255)
|
---|
| 1785 | {
|
---|
[29] | 1786 | SL_RETURN(-1, _("sh_util_obscure_ok"));
|
---|
[22] | 1787 | }
|
---|
| 1788 | sh_obscure_index[i] = (unsigned char)1;
|
---|
| 1789 | if (*endptr == ',')
|
---|
| 1790 | ++endptr;
|
---|
| 1791 |
|
---|
[1] | 1792 | while (*endptr != '\0')
|
---|
| 1793 | {
|
---|
| 1794 | i = strtoul (endptr, &endptr, 0);
|
---|
| 1795 | if (i > 255)
|
---|
| 1796 | {
|
---|
[29] | 1797 | SL_RETURN(-1, _("sh_util_obscure_ok"));
|
---|
[1] | 1798 | }
|
---|
| 1799 | sh_obscure_index[i] = (unsigned char)1;
|
---|
| 1800 | if (*endptr == ',')
|
---|
| 1801 | ++endptr;
|
---|
| 1802 | }
|
---|
[29] | 1803 | SL_RETURN(0, _("sh_util_obscure_ok"));
|
---|
[1] | 1804 | }
|
---|
| 1805 |
|
---|
[68] | 1806 | static int sh_obscure_check_utf8 = S_FALSE;
|
---|
| 1807 |
|
---|
| 1808 | int sh_util_obscure_utf8 (const char * c)
|
---|
| 1809 | {
|
---|
| 1810 | int i;
|
---|
| 1811 | SL_ENTER(_("sh_util_obscure_utf8"));
|
---|
| 1812 | i = sh_util_flagval(c, &(sh_obscure_check_utf8));
|
---|
[76] | 1813 | if (sh_obscure_check_utf8 == S_TRUE)
|
---|
| 1814 | sh_obscure_no_check = S_FALSE;
|
---|
[68] | 1815 | SL_RETURN(i, _("sh_util_obscure_utf8"));
|
---|
| 1816 | }
|
---|
| 1817 |
|
---|
| 1818 |
|
---|
[373] | 1819 | int sh_util_obscurename (ShErrLevel level, const char * name_orig, int flag)
|
---|
[1] | 1820 | {
|
---|
[481] | 1821 | const unsigned char * name = (const unsigned char *) name_orig;
|
---|
[1] | 1822 | char * safe;
|
---|
| 1823 | unsigned int i;
|
---|
[76] | 1824 | size_t len = 0;
|
---|
[1] | 1825 |
|
---|
| 1826 | SL_ENTER(_("sh_util_obscurename"));
|
---|
| 1827 |
|
---|
| 1828 | ASSERT_RET((name != NULL), _("name != NULL"), (0))
|
---|
| 1829 |
|
---|
[76] | 1830 | if (sh_obscure_no_check == S_FALSE)
|
---|
[68] | 1831 | {
|
---|
[76] | 1832 | if (sh_obscure_check_utf8 != S_TRUE)
|
---|
[68] | 1833 | {
|
---|
[76] | 1834 | /* -- Check name. --
|
---|
| 1835 | */
|
---|
| 1836 | while (*name != '\0')
|
---|
| 1837 | {
|
---|
| 1838 | if ( (*name) > 0x7F || (*name) == '"' || (*name) == '\t' ||
|
---|
| 1839 | (*name) == '\b' || (*name) == '\f' ||
|
---|
| 1840 | (*name) == '\n' || (*name) == '\r' ||
|
---|
| 1841 | (*name) == '\v' || iscntrl((int) *name) ||
|
---|
| 1842 | ((*name) != ' ' && !isgraph ((int) *name)) )
|
---|
| 1843 | {
|
---|
| 1844 | i = (unsigned char) *name;
|
---|
| 1845 | if (sh_obscure_index[i] != (unsigned char)1)
|
---|
| 1846 | {
|
---|
| 1847 | goto err;
|
---|
| 1848 | }
|
---|
| 1849 | }
|
---|
| 1850 | name++; ++len;
|
---|
| 1851 | }
|
---|
| 1852 |
|
---|
| 1853 | /* Check for blank at end of name
|
---|
| 1854 | */
|
---|
| 1855 | if ((len > 0) && (name_orig[len-1] == ' '))
|
---|
| 1856 | {
|
---|
| 1857 | goto err;
|
---|
| 1858 | }
|
---|
[68] | 1859 | }
|
---|
[76] | 1860 | else
|
---|
[1] | 1861 | {
|
---|
[76] | 1862 | if (S_FALSE == sh_util_valid_utf8(name))
|
---|
[1] | 1863 | {
|
---|
[68] | 1864 | goto err;
|
---|
[1] | 1865 | }
|
---|
[76] | 1866 | SL_RETURN((0),_("sh_util_obscurename"));
|
---|
[1] | 1867 | }
|
---|
| 1868 | }
|
---|
[76] | 1869 |
|
---|
[1] | 1870 | SL_RETURN((0),_("sh_util_obscurename"));
|
---|
[68] | 1871 |
|
---|
| 1872 | err:
|
---|
[76] | 1873 |
|
---|
[68] | 1874 | if (flag == S_TRUE)
|
---|
| 1875 | {
|
---|
| 1876 | safe = sh_util_safe_name (name_orig);
|
---|
| 1877 | sh_error_handle (level, FIL__, __LINE__, 0, MSG_FI_OBSC,
|
---|
| 1878 | safe);
|
---|
| 1879 | SH_FREE(safe);
|
---|
| 1880 | }
|
---|
| 1881 | SL_RETURN((-1),_("sh_util_obscurename"));
|
---|
[1] | 1882 | }
|
---|
[68] | 1883 |
|
---|
[1] | 1884 | #endif
|
---|
| 1885 |
|
---|
| 1886 | /* returns freshly allocated memory, return value should be free'd
|
---|
| 1887 | */
|
---|
[34] | 1888 | char * sh_util_dirname(const char * fullpath)
|
---|
[1] | 1889 | {
|
---|
| 1890 | char * retval;
|
---|
[34] | 1891 | size_t len;
|
---|
[93] | 1892 | char * tmp;
|
---|
[1] | 1893 |
|
---|
[34] | 1894 | SL_ENTER(_("sh_util_dirname"));
|
---|
[1] | 1895 |
|
---|
| 1896 | ASSERT_RET ((fullpath != NULL), _("fullpath != NULL"), (NULL))
|
---|
[93] | 1897 | ASSERT_RET ((*fullpath == '/'), _("*fullpath == '/'"), (NULL))
|
---|
[1] | 1898 |
|
---|
[93] | 1899 | retval = sh_util_strdup(fullpath);
|
---|
[1] | 1900 |
|
---|
[93] | 1901 | tmp = retval;
|
---|
| 1902 | while (*tmp == '/') ++tmp;
|
---|
[34] | 1903 |
|
---|
[93] | 1904 | /* (1) only leading slashes -- return exact copy
|
---|
| 1905 | */
|
---|
| 1906 | if (*tmp == '\0')
|
---|
| 1907 | {
|
---|
| 1908 | SL_RETURN(retval, _("sh_util_dirname"));
|
---|
| 1909 | }
|
---|
| 1910 |
|
---|
| 1911 | /* (2) there are non-slash characters, so delete trailing slashes
|
---|
| 1912 | */
|
---|
| 1913 | len = sl_strlen (retval); /* retval[len] is terminating '\0' */
|
---|
| 1914 |
|
---|
| 1915 | while (len > 1 && retval[len-1] == '/') /* delete trailing slash */
|
---|
| 1916 | {
|
---|
| 1917 | retval[len-1] = '\0';
|
---|
| 1918 | --len;
|
---|
| 1919 | }
|
---|
| 1920 |
|
---|
| 1921 | /* (3) now delete all non-slash characters up to the preceding slash
|
---|
| 1922 | */
|
---|
| 1923 | while (len > 1 && retval[len-1] != '/') {
|
---|
| 1924 | retval[len-1] = '\0';
|
---|
[34] | 1925 | --len;
|
---|
[1] | 1926 | }
|
---|
| 1927 |
|
---|
[93] | 1928 | /* (4a) only leading slashes left, so return this
|
---|
[1] | 1929 | */
|
---|
[93] | 1930 | if (&(retval[len]) == tmp)
|
---|
| 1931 | {
|
---|
| 1932 | SL_RETURN(retval, _("sh_util_dirname"));
|
---|
[34] | 1933 | }
|
---|
[1] | 1934 |
|
---|
[93] | 1935 | /* (4b) strip trailing slash(es) of parent directory
|
---|
| 1936 | */
|
---|
| 1937 | while (len > 1 && retval[len-1] == '/') {
|
---|
| 1938 | retval[len-1] = '\0';
|
---|
| 1939 | --len;
|
---|
| 1940 | }
|
---|
| 1941 | SL_RETURN(retval, _("sh_util_dirname"));
|
---|
[1] | 1942 |
|
---|
| 1943 | }
|
---|
| 1944 |
|
---|
| 1945 | /* returns freshly allocated memory, return value should be free'd
|
---|
| 1946 | */
|
---|
[34] | 1947 | char * sh_util_basename(const char * fullpath)
|
---|
[1] | 1948 | {
|
---|
[93] | 1949 | char * retval = NULL;
|
---|
| 1950 | const char * tmp;
|
---|
| 1951 | char * tmp2;
|
---|
| 1952 | char * c;
|
---|
| 1953 | size_t len;
|
---|
[1] | 1954 |
|
---|
[34] | 1955 | SL_ENTER(_("sh_util_basename"));
|
---|
[1] | 1956 |
|
---|
| 1957 | ASSERT_RET ((fullpath != NULL), _("fullpath != NULL"), (NULL))
|
---|
| 1958 |
|
---|
[93] | 1959 | tmp = fullpath; while (*tmp == '/') ++tmp;
|
---|
| 1960 | if (*tmp == '\0')
|
---|
[34] | 1961 | {
|
---|
[93] | 1962 | retval = sh_util_strdup(fullpath);
|
---|
[34] | 1963 | }
|
---|
| 1964 | else
|
---|
| 1965 | {
|
---|
[93] | 1966 | tmp2 = sh_util_strdup(tmp);
|
---|
| 1967 | len = sl_strlen (tmp2);
|
---|
| 1968 |
|
---|
| 1969 | while (len > 1 && tmp2[len-1] == '/')
|
---|
[34] | 1970 | {
|
---|
[93] | 1971 | tmp2[len-1] = '\0';
|
---|
| 1972 | --len;
|
---|
| 1973 | }
|
---|
| 1974 |
|
---|
[382] | 1975 | if (tmp2) /* for llvm/clang analyzer */
|
---|
[93] | 1976 | {
|
---|
[382] | 1977 | c = strrchr(tmp2, '/');
|
---|
| 1978 | if (c)
|
---|
| 1979 | {
|
---|
| 1980 | retval = sh_util_strdup(++c);
|
---|
| 1981 | SH_FREE(tmp2);
|
---|
| 1982 | }
|
---|
| 1983 | else
|
---|
| 1984 | {
|
---|
| 1985 | retval = tmp2;
|
---|
| 1986 | }
|
---|
[34] | 1987 | }
|
---|
| 1988 | }
|
---|
[1] | 1989 |
|
---|
[34] | 1990 | SL_RETURN(retval, _("sh_util_basename"));
|
---|
[1] | 1991 | }
|
---|
| 1992 |
|
---|
[185] | 1993 | #define SH_ESCAPE_SPACE 1
|
---|
| 1994 | #define SH_DONT_ESCAPE_SPACE 0
|
---|
| 1995 | char * sh_util_safe_name_int (const char * name, int escape_space);
|
---|
| 1996 |
|
---|
| 1997 | char * sh_util_safe_name (const char * name)
|
---|
| 1998 | {
|
---|
| 1999 | return sh_util_safe_name_int (name, SH_ESCAPE_SPACE);
|
---|
| 2000 | }
|
---|
| 2001 |
|
---|
| 2002 | char * sh_util_safe_name_keepspace (const char * name)
|
---|
| 2003 | {
|
---|
| 2004 | return sh_util_safe_name_int (name, SH_DONT_ESCAPE_SPACE);
|
---|
| 2005 | }
|
---|
| 2006 |
|
---|
[1] | 2007 | /* returns freshly allocated memory, return value should be free'd
|
---|
| 2008 | */
|
---|
[185] | 2009 | char * sh_util_safe_name_int (const char * name, int escape_space)
|
---|
[1] | 2010 | {
|
---|
| 2011 | register int i = 0;
|
---|
| 2012 | const char * p;
|
---|
| 2013 | char * retval;
|
---|
| 2014 | char oct[32];
|
---|
| 2015 | char format[16];
|
---|
[34] | 2016 | size_t len;
|
---|
[1] | 2017 |
|
---|
| 2018 | SL_ENTER(_("sh_util_safe_name"));
|
---|
| 2019 |
|
---|
| 2020 | if (name == NULL)
|
---|
| 2021 | {
|
---|
| 2022 | /* return an allocated array
|
---|
| 2023 | */
|
---|
| 2024 | retval = SH_ALLOC(7);
|
---|
| 2025 | (void) sl_strlcpy(retval, _("(null)"), 7);
|
---|
| 2026 | SL_RETURN(retval, _("sh_util_safe_name"));
|
---|
| 2027 | }
|
---|
| 2028 |
|
---|
| 2029 | /*
|
---|
| 2030 | ASSERT_RET ((name != NULL), _("name != NULL"), _("NULL"))
|
---|
| 2031 | */
|
---|
| 2032 |
|
---|
[34] | 2033 | len = sl_strlen(name);
|
---|
| 2034 | p = name;
|
---|
| 2035 |
|
---|
[1] | 2036 | #ifdef SH_USE_XML
|
---|
[34] | 2037 | if (sl_ok_muls (6, len) && sl_ok_adds ((6*len), 2))
|
---|
| 2038 | { retval = SH_ALLOC(6 * len + 2); }
|
---|
| 2039 | else
|
---|
| 2040 | {
|
---|
| 2041 | /* return an allocated array
|
---|
| 2042 | */
|
---|
| 2043 | retval = SH_ALLOC(11);
|
---|
| 2044 | (void) sl_strlcpy(retval, _("(overflow)"), 11);
|
---|
| 2045 | SL_RETURN(retval, _("sh_util_safe_name"));
|
---|
| 2046 | }
|
---|
[1] | 2047 | #else
|
---|
[34] | 2048 | if (sl_ok_muls (4, len) && sl_ok_adds ((4*len), 2))
|
---|
| 2049 | { retval = SH_ALLOC(4 * len + 2); }
|
---|
| 2050 | else
|
---|
| 2051 | {
|
---|
| 2052 | /* return an allocated array
|
---|
| 2053 | */
|
---|
| 2054 | retval = SH_ALLOC(11);
|
---|
| 2055 | (void) sl_strlcpy(retval, _("(overflow)"), 11);
|
---|
| 2056 | SL_RETURN(retval, _("sh_util_safe_name"));
|
---|
| 2057 | }
|
---|
[1] | 2058 | #endif
|
---|
| 2059 |
|
---|
| 2060 | (void) sl_strncpy(format, _("%c%03o"), 16);
|
---|
| 2061 |
|
---|
| 2062 | while (*p != '\0') {
|
---|
| 2063 | /* Most frequent cases first
|
---|
| 2064 | */
|
---|
| 2065 | if ( ((*p) >= 'a' && (*p) <= 'z') || ((*p) == '/') || ((*p) == '.') ||
|
---|
| 2066 | ((*p) >= '0' && (*p) <= '9') ||
|
---|
| 2067 | ((*p) >= 'A' && (*p) <= 'Z')) {
|
---|
| 2068 | retval[i] = *p;
|
---|
| 2069 | } else if ( (*p) == '\\') { /* backslash */
|
---|
| 2070 | retval[i] = '\\'; ++i;
|
---|
| 2071 | retval[i] = '\\';
|
---|
| 2072 | } else if ( (*p) == '\n') { /* newline */
|
---|
| 2073 | retval[i] = '\\'; ++i;
|
---|
| 2074 | retval[i] = 'n';
|
---|
| 2075 | } else if ( (*p) == '\b') { /* backspace */
|
---|
| 2076 | retval[i] = '\\'; ++i;
|
---|
| 2077 | retval[i] = 'b';
|
---|
| 2078 | } else if ( (*p) == '\r') { /* carriage return */
|
---|
| 2079 | retval[i] = '\\'; ++i;
|
---|
| 2080 | retval[i] = 'r';
|
---|
| 2081 | } else if ( (*p) == '\t') { /* horizontal tab */
|
---|
| 2082 | retval[i] = '\\'; ++i;
|
---|
| 2083 | retval[i] = 't';
|
---|
| 2084 | } else if ( (*p) == '\v') { /* vertical tab */
|
---|
| 2085 | retval[i] = '\\'; ++i;
|
---|
| 2086 | retval[i] = 'v';
|
---|
| 2087 | } else if ( (*p) == '\f') { /* form-feed */
|
---|
| 2088 | retval[i] = '\\'; ++i;
|
---|
| 2089 | retval[i] = 'f';
|
---|
| 2090 | #ifdef WITH_DATABASE
|
---|
| 2091 | } else if ( (*p) == '\'') { /* single quote */
|
---|
| 2092 | retval[i] = '\\'; ++i;
|
---|
| 2093 | retval[i] = '\'';
|
---|
| 2094 | #endif
|
---|
| 2095 | } else if ( (*p) == ' ') { /* space */
|
---|
[185] | 2096 | if (escape_space) {
|
---|
| 2097 | retval[i] = '\\'; ++i;
|
---|
| 2098 | retval[i] = ' ';
|
---|
| 2099 | }
|
---|
| 2100 | else {
|
---|
| 2101 | retval[i] = *p;
|
---|
| 2102 | }
|
---|
[1] | 2103 | #ifdef SH_USE_XML
|
---|
| 2104 | } else if ( (*p) == '"') { /* double quote */
|
---|
| 2105 | retval[i] = '&'; ++i;
|
---|
| 2106 | retval[i] = 'q'; ++i;
|
---|
| 2107 | retval[i] = 'u'; ++i;
|
---|
| 2108 | retval[i] = 'o'; ++i;
|
---|
| 2109 | retval[i] = 't'; ++i;
|
---|
| 2110 | retval[i] = ';';
|
---|
| 2111 | } else if ( (*p) == '&') { /* ampersand */
|
---|
| 2112 | retval[i] = '&'; ++i;
|
---|
| 2113 | retval[i] = 'a'; ++i;
|
---|
| 2114 | retval[i] = 'm'; ++i;
|
---|
| 2115 | retval[i] = 'p'; ++i;
|
---|
| 2116 | retval[i] = ';';
|
---|
| 2117 | } else if ( (*p) == '<') { /* left angle */
|
---|
| 2118 | retval[i] = '&'; ++i;
|
---|
| 2119 | retval[i] = 'l'; ++i;
|
---|
| 2120 | retval[i] = 't'; ++i;
|
---|
| 2121 | retval[i] = ';';
|
---|
| 2122 | } else if ( (*p) == '>') { /* right angle */
|
---|
| 2123 | retval[i] = '&'; ++i;
|
---|
| 2124 | retval[i] = 'g'; ++i;
|
---|
| 2125 | retval[i] = 't'; ++i;
|
---|
| 2126 | retval[i] = ';';
|
---|
| 2127 | #else
|
---|
| 2128 | } else if ( (*p) == '"') { /* double quote */
|
---|
| 2129 | retval[i] = '\\'; ++i;
|
---|
| 2130 | retval[i] = '\"';
|
---|
| 2131 | #endif
|
---|
| 2132 | } else if (!isgraph ((int) *p)) { /* not printable */
|
---|
| 2133 | /*@-bufferoverflowhigh -formatconst@*/
|
---|
[22] | 2134 | /* flawfinder: ignore */
|
---|
[1] | 2135 | sprintf(oct, format, '\\', /* known to fit */
|
---|
| 2136 | (unsigned char) *p);
|
---|
| 2137 | /*@+bufferoverflowhigh +formatconst@*/
|
---|
| 2138 | retval[i] = oct[0]; ++i;
|
---|
| 2139 | retval[i] = oct[1]; ++i;
|
---|
| 2140 | retval[i] = oct[2]; ++i;
|
---|
| 2141 | retval[i] = oct[3];
|
---|
| 2142 | } else {
|
---|
| 2143 | retval[i] = *p;
|
---|
| 2144 | }
|
---|
| 2145 | ++p;
|
---|
| 2146 | ++i;
|
---|
| 2147 | }
|
---|
| 2148 | retval[i] = '\0';
|
---|
| 2149 | SL_RETURN(retval, _("sh_util_safe_name"));
|
---|
| 2150 | }
|
---|
| 2151 |
|
---|
[149] | 2152 | int sh_util_isnum (const char *str)
|
---|
[1] | 2153 | {
|
---|
[149] | 2154 | const char *p = str;
|
---|
[1] | 2155 |
|
---|
| 2156 | SL_ENTER(_("sh_util_isnum"));
|
---|
| 2157 |
|
---|
| 2158 | ASSERT_RET ((str != NULL), _("str != NULL"), (-1))
|
---|
| 2159 |
|
---|
| 2160 | while (p) {
|
---|
| 2161 | if (!isdigit((int) *p) )
|
---|
| 2162 | SL_RETURN((-1), _("sh_util_isnum"));
|
---|
| 2163 | ++p;
|
---|
| 2164 | }
|
---|
| 2165 | SL_RETURN((0), _("sh_util_isnum"));
|
---|
| 2166 | }
|
---|
| 2167 |
|
---|
[149] | 2168 | char * sh_util_strconcat (const char * arg1, ...)
|
---|
[1] | 2169 | {
|
---|
[34] | 2170 | size_t length, l2;
|
---|
[1] | 2171 | char * s;
|
---|
| 2172 | char * strnew;
|
---|
| 2173 | va_list vl;
|
---|
| 2174 |
|
---|
| 2175 | SL_ENTER(_("sh_util_strconcat"));
|
---|
| 2176 |
|
---|
| 2177 | ASSERT_RET ((arg1 != NULL), _("arg1 != NULL"), (NULL))
|
---|
| 2178 |
|
---|
| 2179 | length = sl_strlen (arg1) + 1;
|
---|
| 2180 |
|
---|
| 2181 | va_start (vl, arg1);
|
---|
| 2182 | s = va_arg (vl, char * );
|
---|
| 2183 | while (s != NULL)
|
---|
| 2184 | {
|
---|
[34] | 2185 | l2 = sl_strlen (s);
|
---|
| 2186 | if (sl_ok_adds(length, l2))
|
---|
| 2187 | length += l2;
|
---|
| 2188 | else
|
---|
| 2189 | SL_RETURN(NULL, _("sh_util_strconcat"));
|
---|
[1] | 2190 | s = va_arg (vl, char * );
|
---|
| 2191 | }
|
---|
| 2192 | va_end (vl);
|
---|
| 2193 |
|
---|
[34] | 2194 | if (sl_ok_adds(length, 2))
|
---|
| 2195 | strnew = SH_ALLOC( length + 2 );
|
---|
| 2196 | else
|
---|
| 2197 | SL_RETURN(NULL, _("sh_util_strconcat"));
|
---|
| 2198 |
|
---|
[560] | 2199 | /* cppcheck-suppress uninitvar */
|
---|
[1] | 2200 | strnew[0] = '\0';
|
---|
| 2201 |
|
---|
| 2202 | (void) sl_strlcpy (strnew, arg1, length + 2);
|
---|
| 2203 |
|
---|
| 2204 | va_start (vl, arg1);
|
---|
| 2205 | s = va_arg (vl, char * );
|
---|
| 2206 | while (s)
|
---|
| 2207 | {
|
---|
| 2208 | (void) sl_strlcat (strnew, s, length + 2);
|
---|
| 2209 | s = va_arg (vl, char * );
|
---|
| 2210 | }
|
---|
| 2211 | va_end (vl);
|
---|
| 2212 |
|
---|
| 2213 | SL_RETURN(strnew, _("sh_util_strconcat"));
|
---|
| 2214 | }
|
---|
| 2215 |
|
---|
[167] | 2216 | static const char bto64_0[] = N_("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789()");
|
---|
| 2217 | static char bto64[65] = { '\0' };
|
---|
[1] | 2218 |
|
---|
[167] | 2219 |
|
---|
[226] | 2220 | size_t sh_util_base64_enc (unsigned char * out,
|
---|
| 2221 | const unsigned char * instr,
|
---|
[167] | 2222 | size_t lin)
|
---|
| 2223 | {
|
---|
| 2224 | int ll;
|
---|
| 2225 | unsigned char a, b, c;
|
---|
| 2226 | size_t len = 0;
|
---|
| 2227 | size_t j = 0;
|
---|
| 2228 |
|
---|
| 2229 | start:
|
---|
| 2230 | if (bto64[0] != '\0')
|
---|
| 2231 | {
|
---|
[444] | 2232 | if (instr /* && *instr *//* need to handle binary data */)
|
---|
[167] | 2233 | {
|
---|
| 2234 | if (lin == 0)
|
---|
[170] | 2235 | lin = strlen((const char *)instr);
|
---|
[167] | 2236 |
|
---|
[444] | 2237 | if (lin > 0)
|
---|
| 2238 | {
|
---|
| 2239 | do {
|
---|
| 2240 | ll = 0;
|
---|
| 2241 |
|
---|
| 2242 | if (len < lin)
|
---|
| 2243 | { a = *instr; ++instr; ++len; ++ll; }
|
---|
| 2244 | else
|
---|
| 2245 | { a = 0; }
|
---|
| 2246 | if (len < lin)
|
---|
| 2247 | { b = *instr; ++instr; ++len; ++ll; }
|
---|
| 2248 | else
|
---|
| 2249 | { b = 0; }
|
---|
| 2250 | if (len < lin)
|
---|
| 2251 | { c = *instr; ++instr; ++len; ++ll; }
|
---|
| 2252 | else
|
---|
| 2253 | { c = 0; }
|
---|
| 2254 |
|
---|
| 2255 | *out = bto64[ a >> 2 ];
|
---|
| 2256 | ++j; ++out;
|
---|
| 2257 | *out = bto64[ ((a & 0x03) << 4) | ((b & 0xf0) >> 4) ];
|
---|
| 2258 | ++j; ++out;
|
---|
| 2259 | *out = (unsigned char) (ll > 1 ? bto64[ ((b & 0x0f) << 2) | ((c & 0xc0) >> 6) ] : '?');
|
---|
| 2260 | ++j; ++out;
|
---|
| 2261 | *out = (unsigned char) (ll > 2 ? bto64[ c & 0x3f ] : '?');
|
---|
| 2262 | ++j; ++out;
|
---|
| 2263 | } while (len < lin);
|
---|
| 2264 | }
|
---|
[167] | 2265 | }
|
---|
| 2266 | *out = '\0';
|
---|
| 2267 | return j;
|
---|
| 2268 | }
|
---|
| 2269 |
|
---|
| 2270 | memcpy(bto64, _(bto64_0), 65);
|
---|
| 2271 | goto start;
|
---|
| 2272 | }
|
---|
| 2273 |
|
---|
| 2274 | size_t sh_util_base64_enc_alloc (char **out, const char *in, size_t inlen)
|
---|
| 2275 | {
|
---|
| 2276 | size_t outlen = SH_B64_SIZ(inlen);
|
---|
| 2277 |
|
---|
| 2278 | if (inlen > outlen) /* overflow */
|
---|
| 2279 | {
|
---|
| 2280 | *out = NULL;
|
---|
| 2281 | return 0;
|
---|
| 2282 | }
|
---|
| 2283 |
|
---|
| 2284 | *out = SH_ALLOC(outlen);
|
---|
[170] | 2285 | return sh_util_base64_enc((unsigned char *)*out, (const unsigned char *)in, inlen);
|
---|
[167] | 2286 | }
|
---|
| 2287 |
|
---|
[226] | 2288 | size_t sh_util_base64_dec (unsigned char *out,
|
---|
| 2289 | const unsigned char *in,
|
---|
[167] | 2290 | size_t lin)
|
---|
| 2291 | {
|
---|
| 2292 | size_t i;
|
---|
| 2293 | unsigned char c;
|
---|
| 2294 | unsigned char b;
|
---|
| 2295 | size_t lout = 0;
|
---|
[481] | 2296 | unsigned int w = 0;
|
---|
[167] | 2297 |
|
---|
| 2298 | if (out && in)
|
---|
| 2299 | {
|
---|
| 2300 | if (lin == 0)
|
---|
[170] | 2301 | lin = strlen((const char *)in);
|
---|
[167] | 2302 |
|
---|
| 2303 | for (i = 0; i < lin; i++)
|
---|
| 2304 | {
|
---|
| 2305 | c = *in; ++in;
|
---|
| 2306 | b = 0;
|
---|
| 2307 |
|
---|
| 2308 | if ((c >= 'A') && (c <= 'Z'))
|
---|
| 2309 | {
|
---|
| 2310 | b = (c - 'A');
|
---|
| 2311 | }
|
---|
| 2312 | else if ((c >= 'a') && (c <= 'z'))
|
---|
| 2313 | {
|
---|
| 2314 | b = (c - 'a' + 26);
|
---|
| 2315 | }
|
---|
| 2316 | else if ((c >= '0') && (c <= '9'))
|
---|
| 2317 | {
|
---|
| 2318 | b = (c - '0' + 52);
|
---|
| 2319 | }
|
---|
| 2320 | else if (c == '(' || c == '+')
|
---|
| 2321 | {
|
---|
| 2322 | b = 62;
|
---|
| 2323 | }
|
---|
| 2324 | else if (c == ')' || c == '/')
|
---|
| 2325 | {
|
---|
[170] | 2326 | b = 63;
|
---|
[167] | 2327 | }
|
---|
| 2328 | else if (c == '?' || c == '=')
|
---|
| 2329 | {
|
---|
| 2330 | /* last byte was written to, but will now get
|
---|
| 2331 | * truncated
|
---|
| 2332 | */
|
---|
| 2333 | if (lout > 0) --lout;
|
---|
| 2334 | break;
|
---|
| 2335 | }
|
---|
| 2336 |
|
---|
| 2337 | if (w == 0)
|
---|
| 2338 | {
|
---|
| 2339 | *out = (b << 2) & 0xfc;
|
---|
| 2340 | ++lout;
|
---|
| 2341 | }
|
---|
| 2342 | else if (w == 1)
|
---|
| 2343 | {
|
---|
| 2344 | *out |= (b >> 4) & 0x03;
|
---|
| 2345 | ++out;
|
---|
| 2346 | *out = (b << 4) & 0xf0;
|
---|
| 2347 | ++lout;
|
---|
| 2348 | }
|
---|
| 2349 | else if (w == 2)
|
---|
| 2350 | {
|
---|
| 2351 | *out |= (b >> 2) & 0x0f;
|
---|
| 2352 | ++out;
|
---|
| 2353 | *out = (b << 6) & 0xc0;
|
---|
| 2354 | ++lout;
|
---|
| 2355 | }
|
---|
| 2356 | else if (w == 3)
|
---|
| 2357 | {
|
---|
| 2358 | *out |= b & 0x3f;
|
---|
| 2359 | ++out;
|
---|
| 2360 | }
|
---|
| 2361 |
|
---|
| 2362 | ++w;
|
---|
| 2363 |
|
---|
| 2364 | if (w == 4)
|
---|
| 2365 | {
|
---|
| 2366 | w = 0;
|
---|
| 2367 | }
|
---|
| 2368 | }
|
---|
| 2369 | *out = '\0';
|
---|
| 2370 | }
|
---|
| 2371 | return lout;
|
---|
| 2372 | }
|
---|
| 2373 |
|
---|
| 2374 | size_t sh_util_base64_dec_alloc (unsigned char **out, const unsigned char *in,
|
---|
| 2375 | size_t lin)
|
---|
| 2376 | {
|
---|
| 2377 | size_t lout = 3 * (lin / 4) + 2;
|
---|
| 2378 |
|
---|
| 2379 | *out = SH_ALLOC(lout);
|
---|
| 2380 |
|
---|
| 2381 | return sh_util_base64_dec (*out, in, lin);
|
---|
| 2382 | }
|
---|
| 2383 |
|
---|
| 2384 |
|
---|
[1] | 2385 | #ifdef HAVE_REGEX_H
|
---|
| 2386 |
|
---|
| 2387 | #include <regex.h>
|
---|
| 2388 |
|
---|
| 2389 | int sh_util_regcmp (char * regex_str, char * in_str)
|
---|
| 2390 | {
|
---|
| 2391 | #if defined(REG_ESPACE)
|
---|
| 2392 | int status = REG_ESPACE;
|
---|
| 2393 | #else
|
---|
| 2394 | int status = -1;
|
---|
| 2395 | #endif
|
---|
| 2396 | regex_t preg;
|
---|
| 2397 | char * errbuf;
|
---|
| 2398 |
|
---|
| 2399 | SL_ENTER(_("sh_util_regcmp"));
|
---|
| 2400 |
|
---|
| 2401 | status = regcomp(&preg, regex_str, REG_NOSUB|REG_EXTENDED);
|
---|
| 2402 |
|
---|
| 2403 | if (status == 0)
|
---|
| 2404 | {
|
---|
| 2405 | if ((status = regexec(&preg, in_str, 0, NULL, 0)) == 0)
|
---|
| 2406 | {
|
---|
| 2407 | regfree (&preg);
|
---|
| 2408 | SL_RETURN((0), _("sh_util_regcmp"));
|
---|
| 2409 | }
|
---|
| 2410 | }
|
---|
| 2411 |
|
---|
| 2412 | if (status != 0 && status != REG_NOMATCH)
|
---|
| 2413 | {
|
---|
[34] | 2414 | errbuf = SH_ALLOC(BUFSIZ);
|
---|
[1] | 2415 | (void) regerror(status, &preg, errbuf, BUFSIZ);
|
---|
[34] | 2416 | errbuf[BUFSIZ-1] = '\0';
|
---|
[1] | 2417 | sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_REGEX,
|
---|
| 2418 | errbuf, regex_str);
|
---|
| 2419 | SH_FREE(errbuf);
|
---|
| 2420 | }
|
---|
| 2421 |
|
---|
| 2422 | regfree (&preg);
|
---|
| 2423 | SL_RETURN((-1), _("sh_util_regcmp"));
|
---|
| 2424 | }
|
---|
| 2425 |
|
---|
| 2426 | #endif
|
---|
| 2427 |
|
---|
| 2428 |
|
---|
| 2429 |
|
---|
| 2430 |
|
---|
| 2431 |
|
---|
| 2432 |
|
---|
| 2433 |
|
---|
| 2434 |
|
---|