source: trunk/src/sh_utils.c@ 287

Last change on this file since 287 was 248, checked in by katerina, 15 years ago

Code to track down originating site for ticket #163.

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