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"
|
---|
47 | #include "sh_pthread.h"
|
---|
48 |
|
---|
49 | #undef FIL__
|
---|
50 | #define FIL__ _("sh_utils.c")
|
---|
51 |
|
---|
52 | UINT32 ErrFlag[2];
|
---|
53 |
|
---|
54 | int sh_util_flagval(const char * c, int * fval)
|
---|
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 |
|
---|
74 | int 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 |
|
---|
102 | static int sh_ask_update = S_FALSE;
|
---|
103 |
|
---|
104 | int sh_util_set_interactive(const char * str)
|
---|
105 | {
|
---|
106 | (void) str;
|
---|
107 |
|
---|
108 | sh_ask_update = S_TRUE;
|
---|
109 |
|
---|
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 |
|
---|
122 | int sh_util_ask_update(const 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 | {
|
---|
183 | fprintf(stderr, "%s", _("Please answer y(es) or n(o)\n"));
|
---|
184 | }
|
---|
185 | /*@-charintliteral@*/
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | SL_RETURN(i, _("sh_util_ask_update"));
|
---|
190 | }
|
---|
191 |
|
---|
192 | int sh_util_hidesetup(const char * c)
|
---|
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 | }
|
---|
200 |
|
---|
201 | char * 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 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | char * 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);
|
---|
285 | (void) memcpy (p, str, len+1);
|
---|
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 |
|
---|
294 | char * sh_util_strdup (const char * str)
|
---|
295 | {
|
---|
296 | char * p = NULL;
|
---|
297 | size_t len;
|
---|
298 |
|
---|
299 | SL_ENTER(_("sh_util_strdup"));
|
---|
300 |
|
---|
301 | SH_VALIDATE_NE(str, NULL);
|
---|
302 |
|
---|
303 | len = sl_strlen(str);
|
---|
304 | p = SH_ALLOC (len + 1);
|
---|
305 | (void) memcpy (p, str, len+1);
|
---|
306 |
|
---|
307 | SL_RETURN( p, _("sh_util_strdup"));
|
---|
308 | }
|
---|
309 |
|
---|
310 | /* by the eircom.net computer incident
|
---|
311 | * response team
|
---|
312 | */
|
---|
313 | char * sh_util_strsep (char **str, const char *delim)
|
---|
314 | {
|
---|
315 | char *ret, *c;
|
---|
316 | const char *d;
|
---|
317 |
|
---|
318 | SL_ENTER(_("sh_util_strsep"));
|
---|
319 | ret = *str;
|
---|
320 |
|
---|
321 | SH_VALIDATE_NE(ret, NULL);
|
---|
322 |
|
---|
323 | for (c = *str; *c != '\0'; c++) {
|
---|
324 | for (d = delim; *d != '\0'; d++) {
|
---|
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 |
|
---|
339 | /* returned string must be free'd by caller.
|
---|
340 | */
|
---|
341 | char * 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 |
|
---|
354 | char * clist[16] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
|
---|
355 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
|
---|
356 | int nn = 0;
|
---|
357 |
|
---|
358 | SL_ENTER(_("sh_util_formatted"));
|
---|
359 |
|
---|
360 | if (formatt == NULL || ftab == NULL || *formatt == '\0')
|
---|
361 | SL_RETURN(NULL, _("sh_util_formatted"));
|
---|
362 |
|
---|
363 | /* -- save the format (we overwrite it !!) --
|
---|
364 | */
|
---|
365 | size = sl_strlen(formatt);
|
---|
366 |
|
---|
367 | if (!sl_ok_adds(size, 1))
|
---|
368 | SL_RETURN(NULL, _("sh_util_formatted"));
|
---|
369 |
|
---|
370 | ++size;
|
---|
371 | fmt = SH_ALLOC(size);
|
---|
372 | (void) sl_strlcpy(fmt, formatt, size);
|
---|
373 |
|
---|
374 | p = fmt;
|
---|
375 |
|
---|
376 | j = 0;
|
---|
377 | while (ftab[j].fchar != '\0') {
|
---|
378 | if (ftab[j].type != S_FMT_STRING)
|
---|
379 | ftab[j].data_str = NULL;
|
---|
380 | ++j;
|
---|
381 | }
|
---|
382 |
|
---|
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 |
|
---|
415 | switch(ftab[j].type) {
|
---|
416 |
|
---|
417 | case S_FMT_STRING:
|
---|
418 | {
|
---|
419 | isiz = sl_strlen(ftab[j].data_str);
|
---|
420 | if (isiz > 0 && sl_ok_adds(size, isiz))
|
---|
421 | {
|
---|
422 | size += isiz;
|
---|
423 | clist[nn] = ftab[j].data_str;
|
---|
424 | ++nn;
|
---|
425 | }
|
---|
426 | else
|
---|
427 | *q = '%';
|
---|
428 | goto endsrch;
|
---|
429 | }
|
---|
430 | break;
|
---|
431 |
|
---|
432 | case S_FMT_ULONG:
|
---|
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);
|
---|
440 | if (isiz > 0 && sl_ok_adds(size, isiz))
|
---|
441 | {
|
---|
442 | size += isiz;
|
---|
443 | clist[nn] = ftab[j].data_str;
|
---|
444 | ++nn;
|
---|
445 | }
|
---|
446 | else
|
---|
447 | *q = '%';
|
---|
448 | goto endsrch;
|
---|
449 | }
|
---|
450 | break;
|
---|
451 |
|
---|
452 | case S_FMT_LONG:
|
---|
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);
|
---|
460 | if (isiz > 0 && sl_ok_adds(size, isiz))
|
---|
461 | {
|
---|
462 | size += isiz;
|
---|
463 | clist[nn] = ftab[j].data_str;
|
---|
464 | ++nn;
|
---|
465 | }
|
---|
466 | else
|
---|
467 | *q = '%';
|
---|
468 | goto endsrch;
|
---|
469 | }
|
---|
470 | break;
|
---|
471 |
|
---|
472 | case S_FMT_TIME:
|
---|
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);
|
---|
492 | if (isiz > 0 && sl_ok_adds(size, isiz))
|
---|
493 | {
|
---|
494 | size += isiz;
|
---|
495 | clist[nn] = ftab[j].data_str;
|
---|
496 | ++nn;
|
---|
497 | }
|
---|
498 | else
|
---|
499 | *q = '%';
|
---|
500 | goto endsrch;
|
---|
501 | }
|
---|
502 | break;
|
---|
503 |
|
---|
504 | default:
|
---|
505 | /* do nothing */;
|
---|
506 | }
|
---|
507 |
|
---|
508 | }
|
---|
509 | ++j;
|
---|
510 | }
|
---|
511 |
|
---|
512 | endsrch:
|
---|
513 |
|
---|
514 | p = q;
|
---|
515 |
|
---|
516 | /* -- not found -- */
|
---|
517 | if (i == 0)
|
---|
518 | {
|
---|
519 | *q = '%';
|
---|
520 | ++p;
|
---|
521 | }
|
---|
522 |
|
---|
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 | */
|
---|
532 | if (sl_ok_adds(size, 1))
|
---|
533 | size++;
|
---|
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]);
|
---|
543 | outstr[size-1] = '\0';
|
---|
544 |
|
---|
545 | /* -- cleanup --
|
---|
546 | */
|
---|
547 | j = 0;
|
---|
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 | }
|
---|
553 | SH_FREE(fmt);
|
---|
554 |
|
---|
555 | SL_RETURN(outstr, _("sh_util_formatted"));
|
---|
556 | }
|
---|
557 |
|
---|
558 | /* read a hexchar, return int value (0-15)
|
---|
559 | * can't inline (AIX)
|
---|
560 | */
|
---|
561 | int sh_util_hexchar( const char c )
|
---|
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 |
|
---|
574 | char * sh_util_charhex( unsigned char i , char * i2h)
|
---|
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 |
|
---|
590 | /* read a hexadecimal key, convert to binary
|
---|
591 | */
|
---|
592 | int sh_util_hextobinary (char * binary, const char * hex, int bytes)
|
---|
593 | {
|
---|
594 | int i = 0, j, k, l = 0;
|
---|
595 | char c;
|
---|
596 |
|
---|
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 |
|
---|
609 | SL_ENTER(_("sh_util_hextobinary"));
|
---|
610 |
|
---|
611 | if (bytes < 2)
|
---|
612 | SL_RETURN((-1), _("sh_util_hextobinary"));
|
---|
613 |
|
---|
614 | while (i < (bytes-1))
|
---|
615 | {
|
---|
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;
|
---|
621 | }
|
---|
622 |
|
---|
623 | SL_RETURN((0), _("sh_util_hextobinary"));
|
---|
624 | }
|
---|
625 |
|
---|
626 | static 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 | */
|
---|
644 | static char * sh_util_hmac_tiger (char * hexkey,
|
---|
645 | char * text, size_t textlen,
|
---|
646 | char * res, size_t len)
|
---|
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 | };
|
---|
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 | };
|
---|
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];
|
---|
672 | UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
|
---|
673 | char hashbuf[KEYBUF_SIZE];
|
---|
674 |
|
---|
675 |
|
---|
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 | {
|
---|
683 | (void) sh_tiger_hash (NULL, TIGER_DATA, 0, hashbuf, sizeof(hashbuf));
|
---|
684 | sl_strlcpy(res, hashbuf, len);
|
---|
685 | SL_RETURN(res, _("sh_util_hmac_tiger"));
|
---|
686 | }
|
---|
687 |
|
---|
688 | memcpy (K, zap, KEY_BLOCK);
|
---|
689 |
|
---|
690 | if (sh_util_hextobinary (K, hexkey, KEY_LEN) < 0)
|
---|
691 | {
|
---|
692 | (void) sh_tiger_hash (NULL, TIGER_DATA, 0, hashbuf, sizeof(hashbuf));
|
---|
693 | sl_strlcpy(res, hashbuf, len);
|
---|
694 | SL_RETURN(res, _("sh_util_hmac_tiger"));
|
---|
695 | }
|
---|
696 |
|
---|
697 | if (sl_ok_adds(textlen, KEY_BLOCK))
|
---|
698 | {
|
---|
699 | inner = (char *) SH_ALLOC (textlen + KEY_BLOCK);
|
---|
700 |
|
---|
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 | }
|
---|
710 | }
|
---|
711 | else
|
---|
712 | {
|
---|
713 | sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
|
---|
714 | _("integer overflow"),
|
---|
715 | _("sh_util_hmac_tiger"));
|
---|
716 | (void) sh_tiger_hash (NULL, TIGER_DATA, 0, hashbuf, sizeof(hashbuf));
|
---|
717 | sl_strlcpy(res, hashbuf, len);
|
---|
718 | SL_RETURN(res, _("sh_util_hmac_tiger"));
|
---|
719 | }
|
---|
720 |
|
---|
721 | /* now compute the hash
|
---|
722 | */
|
---|
723 | h1 = sh_tiger_hash_uint32 ( outer,
|
---|
724 | TIGER_DATA,
|
---|
725 | KEY_BLOCK,
|
---|
726 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
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,
|
---|
735 | (unsigned long) KEY_BLOCK+textlen,
|
---|
736 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
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 |
|
---|
744 | (void) sh_tiger_hash ((char *) &cc[0],
|
---|
745 | TIGER_DATA,
|
---|
746 | (unsigned long) (KEY_LEN/4 * sizeof(UINT32)),
|
---|
747 | hashbuf, sizeof(hashbuf));
|
---|
748 |
|
---|
749 | sl_strlcpy(res, hashbuf, len);
|
---|
750 | SL_RETURN(res, _("sh_util_hmac_tiger"));
|
---|
751 | }
|
---|
752 |
|
---|
753 | static char * sh_util_hash_tiger ( char * hexkey,
|
---|
754 | char * text, size_t textlen,
|
---|
755 | char * res, size_t len)
|
---|
756 | {
|
---|
757 | char h2[2*KEY_LEN+1];
|
---|
758 | char hashbuf[KEYBUF_SIZE];
|
---|
759 |
|
---|
760 | SL_ENTER(_("sh_util_hash_tiger"));
|
---|
761 |
|
---|
762 | (void) sl_strlcpy(h2, hexkey, KEY_LEN+1);
|
---|
763 | (void) sl_strlcat(h2,
|
---|
764 | sh_tiger_hash(text, TIGER_DATA,
|
---|
765 | (unsigned long) textlen,
|
---|
766 | hashbuf, sizeof(hashbuf)),
|
---|
767 | 2*KEY_LEN+1
|
---|
768 | );
|
---|
769 |
|
---|
770 | (void) sh_tiger_hash(h2, TIGER_DATA, 2*KEY_LEN, hashbuf, sizeof(hashbuf));
|
---|
771 |
|
---|
772 | sl_strlcpy(res, hashbuf, len);
|
---|
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 |
|
---|
781 | static int sigtype = TYPE_HMAC;
|
---|
782 |
|
---|
783 | int sh_util_sigtype (const char * c)
|
---|
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 |
|
---|
799 | char * sh_util_siggen (char * hexkey,
|
---|
800 | char * text, size_t textlen,
|
---|
801 | char * res, size_t len)
|
---|
802 | {
|
---|
803 | char * p;
|
---|
804 |
|
---|
805 | SL_ENTER(_("sh_util_siggen"));
|
---|
806 | if (sigtype == TYPE_HMAC)
|
---|
807 | p = sh_util_hmac_tiger (hexkey,
|
---|
808 | text, textlen, res, len);
|
---|
809 | else
|
---|
810 | p = sh_util_hash_tiger (hexkey,
|
---|
811 | text, textlen, res, len);
|
---|
812 | SL_RETURN(p, _("sh_util_siggen"));
|
---|
813 | }
|
---|
814 |
|
---|
815 |
|
---|
816 | /* a simple compressor
|
---|
817 | */
|
---|
818 | size_t sh_util_compress (char * dest, char * src, size_t dest_size)
|
---|
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 */
|
---|
859 | SL_RETURN((count), _("sh_util_compress")); /* no of chars copied */
|
---|
860 | }
|
---|
861 |
|
---|
862 |
|
---|
863 | /* copy the four least significant bytes
|
---|
864 | */
|
---|
865 | void 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 | #ifdef WORDS_BIGENDIAN
|
---|
874 | unsigned char swap;
|
---|
875 | unsigned char * ii = (unsigned char *) dest;
|
---|
876 | #endif
|
---|
877 |
|
---|
878 | SL_ENTER(_("sh_util_cpylong"));
|
---|
879 |
|
---|
880 | u.l = 1;
|
---|
881 |
|
---|
882 | /* MSB is first
|
---|
883 | */
|
---|
884 | if (sizeof(long)>4 &&/*@+charint@*/(u.c[sizeof(long)-1] == 1)/*@-charint@*/)
|
---|
885 | {
|
---|
886 | j = (int) (sizeof(long)-4);
|
---|
887 | for (i = 0; i < j; ++i) ++src;
|
---|
888 | }
|
---|
889 |
|
---|
890 | i = 0;
|
---|
891 |
|
---|
892 | while (i < 4)
|
---|
893 | {
|
---|
894 | *dest = (*src);
|
---|
895 | ++dest; ++src;
|
---|
896 | if (i == (len-1)) break;
|
---|
897 | ++i;
|
---|
898 | }
|
---|
899 | #ifdef WORDS_BIGENDIAN
|
---|
900 | swap = ii[0]; ii[0] = ii[3]; ii[3] = swap;
|
---|
901 | swap = ii[1]; ii[1] = ii[2]; ii[2] = swap;
|
---|
902 | #endif
|
---|
903 | SL_RET0(_("sh_util_cpylong"));
|
---|
904 | }
|
---|
905 |
|
---|
906 | /* This is a maximally equidistributed combined Tausworthe
|
---|
907 | * generator. The sequence is,
|
---|
908 | *
|
---|
909 | * x_n = (s1_n ^ s2_n ^ s3_n)
|
---|
910 | *
|
---|
911 | * s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
|
---|
912 | * s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
|
---|
913 | * s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
|
---|
914 | *
|
---|
915 | * computed modulo 2^32. In the three formulas above '^' means
|
---|
916 | * exclusive-or (C-notation), not exponentiation. Note that the
|
---|
917 | * algorithm relies on the properties of 32-bit unsigned integers (it
|
---|
918 | * is formally defined on bit-vectors of length 32).
|
---|
919 | *
|
---|
920 | * Stolen from GSL (GNU scientific library) and modified somewhat.
|
---|
921 | * I am using UINT32, which is guaranteed to be 32 bits. Also made
|
---|
922 | * sure that the initialization vector is valid.
|
---|
923 | */
|
---|
924 |
|
---|
925 |
|
---|
926 | /* interval [0, 4294967296]
|
---|
927 | */
|
---|
928 | static UINT32 taus_get_long (void *vstate)
|
---|
929 | {
|
---|
930 | UINT32 * state = (UINT32 *) vstate;
|
---|
931 |
|
---|
932 | /*
|
---|
933 | if (skey->rngI == BAD)
|
---|
934 | (void)taus_seed();
|
---|
935 | */
|
---|
936 |
|
---|
937 | #define TAUSWORTHE(s,a,b,c,d) ((s &c) <<d) ^ (((s <<a) ^s) >>b)
|
---|
938 | /*@+ignorequals@*/
|
---|
939 | state[0] = TAUSWORTHE (state[0], 13, 19, 4294967294UL, 12);
|
---|
940 | state[1] = TAUSWORTHE (state[1], 2, 25, 4294967288UL, 4);
|
---|
941 | state[2] = TAUSWORTHE (state[2], 3, 11, 4294967280UL, 17);
|
---|
942 | /*@-ignorequals@*/
|
---|
943 | return (state[0] ^ state[1] ^ state[2]);
|
---|
944 | }
|
---|
945 |
|
---|
946 | /* Hide the internal state of the PRNG by using its output as
|
---|
947 | * input for a one-way hash function.
|
---|
948 | */
|
---|
949 |
|
---|
950 | UINT32 taus_get ()
|
---|
951 | {
|
---|
952 | #define TAUS_SAMPLE 12
|
---|
953 |
|
---|
954 | UINT32 taus_svec[TAUS_SAMPLE];
|
---|
955 | UINT32 retval;
|
---|
956 | UINT32 * res;
|
---|
957 | UINT32 * res_vec = &(skey->res_vec[0]);
|
---|
958 | static int res_num = 0;
|
---|
959 | register int i;
|
---|
960 | UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
|
---|
961 |
|
---|
962 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
963 | if (res_num > 0)
|
---|
964 | {
|
---|
965 | retval = res_vec[res_num];
|
---|
966 | res_num = (res_num == 5) ? 0 : (res_num + 1);
|
---|
967 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey); /* alternative path */
|
---|
968 | return retval;
|
---|
969 | }
|
---|
970 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
971 |
|
---|
972 | (void)taus_seed();
|
---|
973 |
|
---|
974 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
975 | for (i = 0; i < (TAUS_SAMPLE/3); ++i)
|
---|
976 | {
|
---|
977 | taus_svec[i*3] = taus_get_long (&(skey->rng0[0]));
|
---|
978 | taus_svec[i*3+1] = taus_get_long (&(skey->rng1[0]));
|
---|
979 | taus_svec[i*3+2] = taus_get_long (&(skey->rng2[0]));
|
---|
980 | }
|
---|
981 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
982 |
|
---|
983 | res = sh_tiger_hash_uint32 ( (char *) &taus_svec[0],
|
---|
984 | TIGER_DATA,
|
---|
985 | (unsigned long)(TAUS_SAMPLE * sizeof(UINT32)),
|
---|
986 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
987 |
|
---|
988 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
989 | for (i = 1; i < 6; ++i)
|
---|
990 | {
|
---|
991 | res_vec[i] = res[i];
|
---|
992 | }
|
---|
993 | retval = res[0];
|
---|
994 | res_num = 1;
|
---|
995 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
996 |
|
---|
997 | memset(taus_svec, '\0', TAUS_SAMPLE * sizeof(UINT32));
|
---|
998 |
|
---|
999 | return retval;
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | /* interval [0,1)
|
---|
1003 | */
|
---|
1004 | double taus_get_double (void *vstate)
|
---|
1005 | {
|
---|
1006 | return taus_get_long (vstate) / (4294967296.0 + 1.0) ;
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | #define LCG(n) ((69069 * n) & 0xffffffffUL)
|
---|
1010 |
|
---|
1011 | /* TAKE CARE: state[0], state[1], state[2] must be > 2,8,16, respectively
|
---|
1012 | */
|
---|
1013 | static void taus_set_from_ulong (void *vstate, unsigned long int s)
|
---|
1014 | {
|
---|
1015 | UINT32 *state = (UINT32 *) vstate;
|
---|
1016 |
|
---|
1017 | if (s == 0)
|
---|
1018 | s = 1; /* default seed is 1 */
|
---|
1019 |
|
---|
1020 | state[0] = (UINT32)(LCG (s) | (UINT32) 0x03);
|
---|
1021 | state[1] = (UINT32)(LCG (state[0]) | (UINT32) 0x09);
|
---|
1022 | state[2] = (UINT32)(LCG (state[1]) | (UINT32) 0x17);
|
---|
1023 |
|
---|
1024 | /* 'warm up'
|
---|
1025 | */
|
---|
1026 | (void) taus_get_long (state);
|
---|
1027 | (void) taus_get_long (state);
|
---|
1028 | (void) taus_get_long (state);
|
---|
1029 | (void) taus_get_long (state);
|
---|
1030 | (void) taus_get_long (state);
|
---|
1031 | (void) taus_get_long (state);
|
---|
1032 |
|
---|
1033 | return;
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | static void taus_set_from_state (void *vstate, void *init_state)
|
---|
1037 | {
|
---|
1038 | UINT32 *state = (UINT32 *) vstate;
|
---|
1039 | UINT32 *state0 = (UINT32 *) init_state;
|
---|
1040 |
|
---|
1041 | state[0] = state0[0] | (UINT32) 0x03;
|
---|
1042 | state[1] = state0[1] | (UINT32) 0x09;
|
---|
1043 | state[2] = state0[2] | (UINT32) 0x17;
|
---|
1044 |
|
---|
1045 | return;
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 |
|
---|
1049 | int taus_seed ()
|
---|
1050 | {
|
---|
1051 | char bufx[9 * sizeof(UINT32) + 1];
|
---|
1052 | int status;
|
---|
1053 | static unsigned long seed_time = 0;
|
---|
1054 | unsigned long gtime;
|
---|
1055 |
|
---|
1056 | SL_ENTER(_("taus_seed"));
|
---|
1057 |
|
---|
1058 | if (skey->rngI == GOOD)
|
---|
1059 | {
|
---|
1060 | if ( (sh_unix_longtime () - seed_time) < 7200)
|
---|
1061 | SL_RETURN( (0), _("taus_seed"));
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | seed_time = sh_unix_longtime ();
|
---|
1065 |
|
---|
1066 | status = sh_entropy (24, bufx);
|
---|
1067 |
|
---|
1068 | if (!SL_ISERROR(status))
|
---|
1069 | {
|
---|
1070 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
1071 | memcpy (&skey->rng0[0], &bufx[0], 2*sizeof(UINT32));
|
---|
1072 | memcpy (&skey->rng1[0], &bufx[2*sizeof(UINT32)], 2*sizeof(UINT32));
|
---|
1073 | memcpy (&skey->rng2[0], &bufx[4*sizeof(UINT32)], 2*sizeof(UINT32));
|
---|
1074 | memset (bufx, 0, 9 * sizeof(UINT32) + 1);
|
---|
1075 |
|
---|
1076 | skey->rng0[2] = 0;
|
---|
1077 | skey->rng1[2] = 0;
|
---|
1078 | skey->rng2[2] = 0;
|
---|
1079 |
|
---|
1080 | taus_set_from_state( &(skey->rng0[0]), &(skey->rng0[0]));
|
---|
1081 | taus_set_from_state( &(skey->rng1[0]), &(skey->rng1[0]));
|
---|
1082 | taus_set_from_state( &(skey->rng2[0]), &(skey->rng2[0]));
|
---|
1083 |
|
---|
1084 | skey->rngI = GOOD;
|
---|
1085 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
1086 | SL_RETURN( (0), _("taus_seed"));
|
---|
1087 | }
|
---|
1088 |
|
---|
1089 | sh_error_handle ((-1), FIL__, __LINE__, status, MSG_ES_ENT,
|
---|
1090 | _("sh_entropy"));
|
---|
1091 |
|
---|
1092 | /* emergency backup - unsafe !
|
---|
1093 | */
|
---|
1094 | #ifdef HAVE_GETTIMEOFDAY
|
---|
1095 | gtime = sh_unix_notime();
|
---|
1096 | #else
|
---|
1097 | gtime = seed_time;
|
---|
1098 | #endif
|
---|
1099 |
|
---|
1100 | SH_MUTEX_LOCK_UNSAFE(mutex_skey);
|
---|
1101 | taus_set_from_ulong ( &(skey->rng0[0]), LCG (gtime) );
|
---|
1102 | taus_set_from_ulong ( &(skey->rng1[0]), LCG (skey->rng0[0]) );
|
---|
1103 | taus_set_from_ulong ( &(skey->rng2[0]), LCG (skey->rng1[0]) );
|
---|
1104 | skey->rngI = BAD;
|
---|
1105 | SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
|
---|
1106 |
|
---|
1107 | SL_RETURN( (-1), _("taus_seed"));
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | /*@+charint@*/
|
---|
1111 | static unsigned char new_key[] = { 0xA7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xA7 };
|
---|
1112 | /*@-charint@*/
|
---|
1113 | static void copy_four (unsigned char * dest, UINT32 in);
|
---|
1114 |
|
---|
1115 | int sh_util_set_newkey (const char * new_in)
|
---|
1116 | {
|
---|
1117 | size_t i, j = 0;
|
---|
1118 | size_t len;
|
---|
1119 | SL_TICKET fp;
|
---|
1120 | SL_TICKET fout;
|
---|
1121 | char * key;
|
---|
1122 | char * path;
|
---|
1123 | char * outpath = NULL;
|
---|
1124 | unsigned char * image = NULL;
|
---|
1125 | long s = 0;
|
---|
1126 | long ilen = 0;
|
---|
1127 | long ii, k = 0;
|
---|
1128 | UINT32 * h1;
|
---|
1129 | char * new = NULL;
|
---|
1130 |
|
---|
1131 | if (0 != sl_is_suid())
|
---|
1132 | {
|
---|
1133 | fprintf(stderr, "%s", _("ERROR: insufficient privilege\n"));
|
---|
1134 | _exit (EXIT_FAILURE);
|
---|
1135 | /*@notreached@*/
|
---|
1136 | return -1; /* braindead MAC OSX compiler needs this */
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | if (new_in == NULL || new_in[0] == '\0')
|
---|
1140 | {
|
---|
1141 | fprintf(stderr, "%s",
|
---|
1142 | _("ERROR: no key given\n Argument must be 'key@path'\n"));
|
---|
1143 | _exit (EXIT_FAILURE);
|
---|
1144 | /*@notreached@*/
|
---|
1145 | return -1;
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | if (NULL == (new = malloc(strlen(new_in) + 1)))
|
---|
1149 | goto bail_mem;
|
---|
1150 | sl_strncpy(new, new_in, strlen(new_in) + 1);
|
---|
1151 |
|
---|
1152 | key = new;
|
---|
1153 | len = strlen(new);
|
---|
1154 | for (i = 1; i < (len-2); ++i)
|
---|
1155 | {
|
---|
1156 | if (new[i] == '@' && new[i+1] == '/')
|
---|
1157 | {
|
---|
1158 | j = i+1; new[i] = '\0'; break;
|
---|
1159 | }
|
---|
1160 | }
|
---|
1161 | if (j == 0)
|
---|
1162 | {
|
---|
1163 | fprintf(stderr, "%s",
|
---|
1164 | _("ERROR: no path to executable given\n Argument must be 'key@path'\n"));
|
---|
1165 | free(new);
|
---|
1166 | _exit (EXIT_FAILURE);
|
---|
1167 | /*@notreached@*/
|
---|
1168 | return -1;
|
---|
1169 | }
|
---|
1170 | else
|
---|
1171 | path = &new[j];
|
---|
1172 |
|
---|
1173 | len = strlen(path) + 1 + 4;
|
---|
1174 | /*@-usedef@*/
|
---|
1175 | if (NULL == (outpath = malloc(len)))
|
---|
1176 | goto bail_mem;
|
---|
1177 | /*@-usedef@*/
|
---|
1178 | sl_snprintf (outpath, len, _("%s.out"), path);
|
---|
1179 |
|
---|
1180 | fp = sl_open_read(FIL__, __LINE__, path, SL_NOPRIV);
|
---|
1181 | if (SL_ISERROR(fp))
|
---|
1182 | {
|
---|
1183 | fprintf(stderr,
|
---|
1184 | _("ERROR: cannot open %s for read (errnum = %ld)\n"), path, fp);
|
---|
1185 | free(new); free (outpath);
|
---|
1186 | _exit (EXIT_FAILURE);
|
---|
1187 | /*@notreached@*/
|
---|
1188 | return -1;
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | fout = sl_open_write(FIL__, __LINE__, outpath, SL_NOPRIV);
|
---|
1192 | if (SL_ISERROR(fout))
|
---|
1193 | {
|
---|
1194 | fprintf(stderr,
|
---|
1195 | _("ERROR: cannot open %s (errnum = %ld)\n"), outpath, fout);
|
---|
1196 | free(new); free (outpath);
|
---|
1197 | _exit (EXIT_FAILURE);
|
---|
1198 | /*@notreached@*/
|
---|
1199 | return -1;
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 |
|
---|
1203 | image = malloc (4096);
|
---|
1204 | if (!image)
|
---|
1205 | goto bail_mem;
|
---|
1206 | while (0 < (ii = sl_read (fp, &image[s], 4096)))
|
---|
1207 | {
|
---|
1208 | ilen += ii;
|
---|
1209 | s += 4096;
|
---|
1210 | image = realloc (image, (size_t) (4096 + s));
|
---|
1211 | if (!image)
|
---|
1212 | goto bail_mem;
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 | printf(_("%ld bytes read\n"), ilen);
|
---|
1216 |
|
---|
1217 |
|
---|
1218 | for (k = 0; k < (ilen - 8); ++k)
|
---|
1219 | {
|
---|
1220 | if (image[k] == new_key[0] &&
|
---|
1221 | image[k+1] == new_key[1] &&
|
---|
1222 | image[k+2] == new_key[2] &&
|
---|
1223 | image[k+3] == new_key[3] &&
|
---|
1224 | image[k+4] == new_key[4] &&
|
---|
1225 | image[k+5] == new_key[5] &&
|
---|
1226 | image[k+6] == new_key[6] &&
|
---|
1227 | image[k+7] == new_key[7])
|
---|
1228 | {
|
---|
1229 | UINT32 kbuf[KEY_BYT/sizeof(UINT32)];
|
---|
1230 |
|
---|
1231 | printf("%s", _("old key found\n"));
|
---|
1232 | h1 = sh_tiger_hash_uint32 (key, TIGER_DATA,
|
---|
1233 | (unsigned long)strlen(key),
|
---|
1234 | kbuf, KEY_BYT/sizeof(UINT32));
|
---|
1235 | copy_four( (unsigned char *) &(image[k]), h1[0]);
|
---|
1236 | copy_four( (unsigned char *) &(image[k+4]), h1[1]);
|
---|
1237 | (void) sl_write (fout, image, ilen);
|
---|
1238 | (void) sl_close (fout);
|
---|
1239 | printf(_("new file %s written\n"), outpath);
|
---|
1240 | free(new); free (outpath); free(image);
|
---|
1241 | _exit (EXIT_SUCCESS);
|
---|
1242 | /*@notreached@*/
|
---|
1243 | return 0;
|
---|
1244 | }
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | fprintf(stderr, "%s",
|
---|
1248 | _("ERROR: old key not found\n"));
|
---|
1249 | free(new); free (outpath); free(image);
|
---|
1250 | _exit (EXIT_FAILURE);
|
---|
1251 | /*@notreached@*/
|
---|
1252 | return -1;
|
---|
1253 |
|
---|
1254 |
|
---|
1255 | bail_mem:
|
---|
1256 | fprintf(stderr, "%s",
|
---|
1257 | _("ERROR: out of memory\n"));
|
---|
1258 | if (new) free(new);
|
---|
1259 | if (outpath) free (outpath);
|
---|
1260 | if (image) free (image);
|
---|
1261 | _exit (EXIT_FAILURE);
|
---|
1262 | /*@notreached@*/
|
---|
1263 | return -1;
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 |
|
---|
1267 |
|
---|
1268 |
|
---|
1269 | /* A simple en-/decoder, based on Vernam cipher. We use the
|
---|
1270 | * message as salt to hide the key by obtaining a different one-time
|
---|
1271 | * pad each time.
|
---|
1272 | * Should be safe against a listener on the network, but not against someone
|
---|
1273 | * with read access to the binary.
|
---|
1274 | */
|
---|
1275 | void sh_util_encode (char * data, char * salt, int mode, char fill)
|
---|
1276 | {
|
---|
1277 | static char cc1[17] = N_("0123456789ABCDEF");
|
---|
1278 | char cc[17] = "\0";
|
---|
1279 | register int i, j, j1 = 0, j2 = 0, j3;
|
---|
1280 | char * dez;
|
---|
1281 | char hashbuf[KEYBUF_SIZE];
|
---|
1282 |
|
---|
1283 | SL_ENTER(_("sh_util_encode"));
|
---|
1284 |
|
---|
1285 | /* init
|
---|
1286 | */
|
---|
1287 | (void) sl_strlcpy( cc, _(cc1), sizeof(cc));
|
---|
1288 |
|
---|
1289 | /* max 128 bits keyspace
|
---|
1290 | */
|
---|
1291 | memset (skey->vernam, (int)fill, KEY_LEN+1);
|
---|
1292 |
|
---|
1293 | dez = (char *) &(skey->ErrFlag[0]);
|
---|
1294 | sh_util_cpylong (skey->vernam, dez, 4);
|
---|
1295 | dez = (char *) &(skey->ErrFlag[1]);
|
---|
1296 | sh_util_cpylong (&skey->vernam[4], dez, 4);
|
---|
1297 |
|
---|
1298 | skey->vernam[KEY_LEN] = '\0';
|
---|
1299 |
|
---|
1300 | (void) sl_strlcpy(skey->vernam,
|
---|
1301 | sh_tiger_hash(skey->vernam, TIGER_DATA, KEY_LEN,
|
---|
1302 | hashbuf, sizeof(hashbuf)),
|
---|
1303 | KEY_LEN+1);
|
---|
1304 |
|
---|
1305 | (void) sl_strlcpy(skey->vernam,
|
---|
1306 | sh_util_hmac_tiger (skey->vernam, salt, strlen(salt),
|
---|
1307 | hashbuf, sizeof(hashbuf)),
|
---|
1308 | KEY_LEN+1);
|
---|
1309 |
|
---|
1310 | (void) sl_strlcpy(skey->vernam,
|
---|
1311 | sh_util_hmac_tiger (skey->vernam, (char*) new_key, 8,
|
---|
1312 | hashbuf, sizeof(hashbuf)),
|
---|
1313 | KEY_LEN+1);
|
---|
1314 |
|
---|
1315 | /* The following routine adds/subtracts data[j] and vernam[j] mod 16.
|
---|
1316 | */
|
---|
1317 | j = 0;
|
---|
1318 | while (j < KEY_LEN)
|
---|
1319 | {
|
---|
1320 | for (i = 0; i < 16; ++i)
|
---|
1321 | {
|
---|
1322 | if (cc[i] == data[j]) j1 = i;
|
---|
1323 | if (cc[i] == skey->vernam[j]) j2 = i;
|
---|
1324 | }
|
---|
1325 | if (mode == 0)
|
---|
1326 | {
|
---|
1327 | j3 = j1 + j2;
|
---|
1328 | if (j3 > 15) j3 -= 16;
|
---|
1329 | data[j] = cc[j3];
|
---|
1330 | }
|
---|
1331 | else
|
---|
1332 | {
|
---|
1333 | j3 = j1 - j2;
|
---|
1334 | if (j3 < 0) j3 += 16;
|
---|
1335 | data[j] = cc[j3];
|
---|
1336 | }
|
---|
1337 | ++j;
|
---|
1338 | }
|
---|
1339 | SL_RET0(_("sh_util_encode"));
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | /* server mode
|
---|
1343 | */
|
---|
1344 | int sh_util_setserver (const char * dummy)
|
---|
1345 | {
|
---|
1346 | SL_ENTER(_("sh_util_setserver"));
|
---|
1347 |
|
---|
1348 | (void) dummy;
|
---|
1349 | sh.flag.isserver = GOOD;
|
---|
1350 | SL_RETURN((0),_("sh_util_setserver"));
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 |
|
---|
1354 | int sh_util_setlooptime (const char * str)
|
---|
1355 | {
|
---|
1356 | int i = atoi (str);
|
---|
1357 |
|
---|
1358 | SL_ENTER(_("sh_util_setlooptime"));
|
---|
1359 |
|
---|
1360 | if (i >= 0 && i < INT_MAX) {
|
---|
1361 | sh.looptime = i;
|
---|
1362 | SL_RETURN((0),_("sh_util_setlooptime"));
|
---|
1363 | } else {
|
---|
1364 | sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
|
---|
1365 | _("loop time"), str);
|
---|
1366 | SL_RETURN((-1),_("sh_util_setlooptime"));
|
---|
1367 | }
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | #if defined (SH_WITH_CLIENT) || defined (SH_STANDALONE)
|
---|
1371 | int sh_util_setchecksum (const char * str)
|
---|
1372 | {
|
---|
1373 | static int reject = 0;
|
---|
1374 |
|
---|
1375 | SL_ENTER(_("sh_util_setchecksum"));
|
---|
1376 |
|
---|
1377 | if (reject == 1)
|
---|
1378 | SL_RETURN((0), _("sh_util_setchecksum"));
|
---|
1379 | reject = 1;
|
---|
1380 |
|
---|
1381 | if (sl_strncmp (str, _("init"), sizeof("init")-1) == 0)
|
---|
1382 | {
|
---|
1383 | sh.flag.checkSum = SH_CHECK_INIT;
|
---|
1384 | }
|
---|
1385 | else if (sl_strncmp (str, _("update"), sizeof("update")-1) == 0)
|
---|
1386 | {
|
---|
1387 | if (S_TRUE == file_is_remote())
|
---|
1388 | {
|
---|
1389 | sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
|
---|
1390 | _("checksum testing"), str);
|
---|
1391 | SL_RETURN((-1), _("sh_util_setchecksum"));
|
---|
1392 | }
|
---|
1393 | else
|
---|
1394 | {
|
---|
1395 | sh.flag.checkSum = SH_CHECK_CHECK;
|
---|
1396 | sh.flag.update = S_TRUE;
|
---|
1397 | }
|
---|
1398 | }
|
---|
1399 | else if (sl_strncmp (str, _("check"), sizeof("check")-1) == 0)
|
---|
1400 | {
|
---|
1401 | sh.flag.checkSum = SH_CHECK_CHECK;
|
---|
1402 | }
|
---|
1403 | /*
|
---|
1404 | else if (sl_strncmp (str, _("update"), sizeof("update")-1) == 0)
|
---|
1405 | {
|
---|
1406 | sh.flag.checkSum = SH_CHECK_INIT;
|
---|
1407 | sh.flag.update = S_TRUE;
|
---|
1408 | }
|
---|
1409 | */
|
---|
1410 | else if (sl_strncmp (str, _("none"), sizeof("none")-1) == 0)
|
---|
1411 | {
|
---|
1412 | sh.flag.checkSum = SH_CHECK_NONE;
|
---|
1413 | }
|
---|
1414 | else
|
---|
1415 | {
|
---|
1416 | sh_error_handle ((-1), FIL__, __LINE__, EINVAL, MSG_EINVALS,
|
---|
1417 | _("checksum testing"), str);
|
---|
1418 | SL_RETURN((-1), _("sh_util_setchecksum"));
|
---|
1419 | }
|
---|
1420 | SL_RETURN((0), _("sh_util_setchecksum"));
|
---|
1421 | }
|
---|
1422 | #endif
|
---|
1423 |
|
---|
1424 | /*@+charint@*/
|
---|
1425 | unsigned char TcpFlag[8][PW_LEN+1] = {
|
---|
1426 | #if (POS_TF == 1)
|
---|
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 == 2)
|
---|
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 == 3)
|
---|
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 == 4)
|
---|
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 == 5)
|
---|
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 == 6)
|
---|
1447 | { 0xF7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xF7,0x00 },
|
---|
1448 | #endif
|
---|
1449 | { 0xFF,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xFF,0x00 },
|
---|
1450 | #if (POS_TF == 7)
|
---|
1451 | { 0xF7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xF7,0x00 },
|
---|
1452 | #endif
|
---|
1453 | { 0xFF,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xFF,0x00 },
|
---|
1454 | #if (POS_TF == 8)
|
---|
1455 | { 0xF7,0xC3,0x12,0xAA,0xAA,0x12,0xC3,0xF7,0x00 },
|
---|
1456 | #endif
|
---|
1457 | };
|
---|
1458 | /*@-charint@*/
|
---|
1459 |
|
---|
1460 | /* initialize a key to a random value
|
---|
1461 | * rev 0.8
|
---|
1462 | */
|
---|
1463 | int sh_util_keyinit (char * buf, long size)
|
---|
1464 | {
|
---|
1465 | UINT32 bufy[6];
|
---|
1466 | int i;
|
---|
1467 | int status = 0;
|
---|
1468 | char * p;
|
---|
1469 | char hashbuf[KEYBUF_SIZE];
|
---|
1470 |
|
---|
1471 | SL_ENTER(_("sh_util_keyinit"));
|
---|
1472 |
|
---|
1473 | ASSERT((size <= KEY_LEN+1), _("size <= KEY_LEN+1"))
|
---|
1474 |
|
---|
1475 | if (size > KEY_LEN+1)
|
---|
1476 | size = KEY_LEN+1;
|
---|
1477 |
|
---|
1478 | /* seed / re-seed the PRNG if required
|
---|
1479 | */
|
---|
1480 | status = taus_seed ();
|
---|
1481 |
|
---|
1482 | if (status == -1)
|
---|
1483 | sh_error_handle ((-1), FIL__, __LINE__, -1, MSG_ES_KEY1,
|
---|
1484 | _("taus_seed"));
|
---|
1485 |
|
---|
1486 | for (i = 0; i < 6; ++i)
|
---|
1487 | bufy[i] = taus_get();
|
---|
1488 |
|
---|
1489 | p = sh_tiger_hash ((char *) bufy, TIGER_DATA,
|
---|
1490 | (unsigned long)(6*sizeof(UINT32)),
|
---|
1491 | hashbuf, sizeof(hashbuf));
|
---|
1492 |
|
---|
1493 | i = sl_strlcpy(buf, p, (size_t)size);
|
---|
1494 |
|
---|
1495 | memset (bufy, 0, 6*sizeof(UINT32));
|
---|
1496 |
|
---|
1497 | if ((status == 0) && (!SL_ISERROR(i)) )
|
---|
1498 | SL_RETURN((0),_("sh_util_keyinit"));
|
---|
1499 |
|
---|
1500 | if (SL_ISERROR(i))
|
---|
1501 | sh_error_handle ((-1), FIL__, __LINE__, i, MSG_ES_KEY2,
|
---|
1502 | _("sl_strlcpy"));
|
---|
1503 |
|
---|
1504 | SL_RETURN((-1),_("sh_util_keyinit"));
|
---|
1505 | }
|
---|
1506 |
|
---|
1507 | #if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
|
---|
1508 |
|
---|
1509 | static unsigned char sh_obscure_index[256];
|
---|
1510 | static int sh_obscure_no_check = S_FALSE;
|
---|
1511 |
|
---|
1512 | int sh_util_valid_utf8 (const unsigned char * str)
|
---|
1513 | {
|
---|
1514 | const int sh_val_utf8_1 = 1;
|
---|
1515 | const int sh_val_utf8_2 = 2;
|
---|
1516 | const int sh_val_utf8_3 = 3;
|
---|
1517 | const int sh_val_utf8_4 = 4;
|
---|
1518 |
|
---|
1519 | size_t len = strlen((const char *)str);
|
---|
1520 | size_t l = 0;
|
---|
1521 | int typ = 0;
|
---|
1522 | unsigned char c = '\0';
|
---|
1523 | unsigned char c2[2] = { 0x00, 0x00 };
|
---|
1524 | unsigned char c3[3] = { 0x00, 0x00, 0x00 };
|
---|
1525 |
|
---|
1526 |
|
---|
1527 | #define SH_VAL_UTF8_1 ((c != '\0') && ((c & 0x80) == 0x00))
|
---|
1528 | #define SH_VAL_UTF8_2 ((c != '\0') && ((c & 0xE0) == 0xC0)) /* 110x xxxx */
|
---|
1529 | #define SH_VAL_UTF8_3 ((c != '\0') && ((c & 0xF0) == 0xE0)) /* 1110 xxxx */
|
---|
1530 | #define SH_VAL_UTF8_4 ((c != '\0') && ((c & 0xF8) == 0xF0)) /* 1111 0xxx */
|
---|
1531 | #define SH_VAL_UTF8_N ((c != '\0') && ((c & 0xC0) == 0x80)) /* 10xx xxxx */
|
---|
1532 | #define SH_VAL_BAD ((c == '"') || (c == '\t') || (c == '\b') || \
|
---|
1533 | (c == '\f') || (c == '\n') || \
|
---|
1534 | (c == '\r') || (c == '\v') || iscntrl((int) c) || \
|
---|
1535 | (c != ' ' && !isgraph ((int) c)))
|
---|
1536 |
|
---|
1537 | while(l < len)
|
---|
1538 | {
|
---|
1539 | c = str[l];
|
---|
1540 |
|
---|
1541 | if (SH_VAL_UTF8_1)
|
---|
1542 | {
|
---|
1543 | if (!(SH_VAL_BAD && (sh_obscure_index[c] != 1)))
|
---|
1544 | {
|
---|
1545 | typ = sh_val_utf8_1;
|
---|
1546 | ++l; continue;
|
---|
1547 | }
|
---|
1548 | else
|
---|
1549 | {
|
---|
1550 | return S_FALSE;
|
---|
1551 | }
|
---|
1552 | }
|
---|
1553 | else if (SH_VAL_UTF8_2)
|
---|
1554 | {
|
---|
1555 | typ = sh_val_utf8_2;
|
---|
1556 | c2[0] = c;
|
---|
1557 | if ((c & 0x3e) != 0x00) /* !(overlong 2-byte seq.) */
|
---|
1558 | {
|
---|
1559 | ++l;
|
---|
1560 | if (l != len) {
|
---|
1561 | c = str[l];
|
---|
1562 | if(SH_VAL_UTF8_N) {
|
---|
1563 | c2[1] = c;
|
---|
1564 | ++l; continue;
|
---|
1565 | }
|
---|
1566 | else {
|
---|
1567 | return S_FALSE;
|
---|
1568 | }
|
---|
1569 | }
|
---|
1570 | else {
|
---|
1571 | return S_FALSE;
|
---|
1572 | }
|
---|
1573 | }
|
---|
1574 | else
|
---|
1575 | {
|
---|
1576 | return S_FALSE; /* overlong 2-byte seq. */
|
---|
1577 | }
|
---|
1578 | }
|
---|
1579 | else if (SH_VAL_UTF8_3)
|
---|
1580 | {
|
---|
1581 | typ = sh_val_utf8_3;
|
---|
1582 | c3[0] = c;
|
---|
1583 | ++l; if (l == len) return S_FALSE; c = str[l];
|
---|
1584 | if(!SH_VAL_UTF8_N) return S_FALSE;
|
---|
1585 | if (((str[l-1] & 0x1F) == 0x00) && ((c & 0x60) == 0x00))
|
---|
1586 | return S_FALSE; /* overlong 3-byte seq. */
|
---|
1587 | c3[1] = c;
|
---|
1588 | ++l; if (l == len) return S_FALSE; c = str[l];
|
---|
1589 | if(!SH_VAL_UTF8_N) return S_FALSE;
|
---|
1590 | c3[2] = c;
|
---|
1591 | ++l; continue;
|
---|
1592 | }
|
---|
1593 | else if (SH_VAL_UTF8_4)
|
---|
1594 | {
|
---|
1595 | typ = sh_val_utf8_4;
|
---|
1596 | ++l; if (l == len) return S_FALSE; c = str[l];
|
---|
1597 | if(!SH_VAL_UTF8_N) return S_FALSE;
|
---|
1598 | if (((str[l-1] & 0x0F) == 0x00) && ((c & 0x70) == 0x00))
|
---|
1599 | return S_FALSE; /* overlong 4-byte seq. */
|
---|
1600 | ++l; if (l == len) return S_FALSE; c = str[l];
|
---|
1601 | if(!SH_VAL_UTF8_N) return S_FALSE;
|
---|
1602 | ++l; if (l == len) return S_FALSE; c = str[l];
|
---|
1603 | if(!SH_VAL_UTF8_N) return S_FALSE;
|
---|
1604 | ++l; continue;
|
---|
1605 | }
|
---|
1606 | return S_FALSE;
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | /* last character is invisible (space or else)
|
---|
1610 | */
|
---|
1611 | if (typ == sh_val_utf8_1)
|
---|
1612 | {
|
---|
1613 | if (c != ' ')
|
---|
1614 | return S_TRUE;
|
---|
1615 | else
|
---|
1616 | return S_FALSE;
|
---|
1617 | }
|
---|
1618 | else if (typ == sh_val_utf8_2)
|
---|
1619 | {
|
---|
1620 | if (c2[0] == 0xC2 && c2[1] == 0xA0) /* nbsp */
|
---|
1621 | return S_FALSE;
|
---|
1622 | else
|
---|
1623 | return S_TRUE;
|
---|
1624 | }
|
---|
1625 | else if (typ == sh_val_utf8_3)
|
---|
1626 | {
|
---|
1627 | if (c3[0] == 0xE2)
|
---|
1628 | {
|
---|
1629 | if (c3[1] == 0x80 && c3[2] >= 0x80 && c3[2] <= 0x8F)
|
---|
1630 | return S_FALSE; /* various spaces, left-to-right, right-to-left */
|
---|
1631 | else if (c3[1] == 0x80 && (c3[2] == 0xA8 || c3[2] == 0xA9 ||
|
---|
1632 | c3[2] == 0xAD || c3[2] == 0xAF))
|
---|
1633 | return S_FALSE; /* line sep, para sep, zw word joiner, nnbsp */
|
---|
1634 | else if (c3[1] == 0x81 && (c3[2] == 0xA0 || c3[2] == 0xA1 ||
|
---|
1635 | c3[2] == 0x9F))
|
---|
1636 | return S_FALSE; /* word joiner, function app, math space */
|
---|
1637 | else
|
---|
1638 | return S_TRUE;
|
---|
1639 | }
|
---|
1640 | else if (c3[0] == 0xE3 && c3[1] == 0x80 && c3[2] == 0x80)
|
---|
1641 | {
|
---|
1642 | return S_FALSE; /* ideographic space */
|
---|
1643 | }
|
---|
1644 | else if (c3[0] == 0xEF && c3[1] == 0xBB && c3[2] == 0xBF)
|
---|
1645 | {
|
---|
1646 | return S_FALSE; /* zwnbsp */
|
---|
1647 | }
|
---|
1648 | else
|
---|
1649 | {
|
---|
1650 | return S_TRUE;
|
---|
1651 | }
|
---|
1652 | }
|
---|
1653 | else
|
---|
1654 | {
|
---|
1655 | return S_TRUE;
|
---|
1656 | }
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 |
|
---|
1660 | int sh_util_obscure_ok (const char * str)
|
---|
1661 | {
|
---|
1662 | unsigned long i;
|
---|
1663 | char * endptr = NULL;
|
---|
1664 |
|
---|
1665 | SL_ENTER(_("sh_util_obscure_ok"));
|
---|
1666 |
|
---|
1667 | if (0 == sl_strncmp("all", str, 3))
|
---|
1668 | {
|
---|
1669 | for (i = 0; i < 255; ++i)
|
---|
1670 | {
|
---|
1671 | sh_obscure_index[i] = (unsigned char)1;
|
---|
1672 | }
|
---|
1673 | sh_obscure_no_check = S_TRUE;
|
---|
1674 | SL_RETURN(0, _("sh_util_obscure_ok"));
|
---|
1675 | }
|
---|
1676 |
|
---|
1677 | sh_obscure_no_check = S_FALSE;
|
---|
1678 |
|
---|
1679 | for (i = 0; i < 255; ++i)
|
---|
1680 | {
|
---|
1681 | sh_obscure_index[i] = (unsigned char)0;
|
---|
1682 | }
|
---|
1683 |
|
---|
1684 | i = strtoul (str, &endptr, 0);
|
---|
1685 | if (i > 255)
|
---|
1686 | {
|
---|
1687 | SL_RETURN(-1, _("sh_util_obscure_ok"));
|
---|
1688 | }
|
---|
1689 | sh_obscure_index[i] = (unsigned char)1;
|
---|
1690 | if (*endptr == ',')
|
---|
1691 | ++endptr;
|
---|
1692 |
|
---|
1693 | while (*endptr != '\0')
|
---|
1694 | {
|
---|
1695 | i = strtoul (endptr, &endptr, 0);
|
---|
1696 | if (i > 255)
|
---|
1697 | {
|
---|
1698 | SL_RETURN(-1, _("sh_util_obscure_ok"));
|
---|
1699 | }
|
---|
1700 | sh_obscure_index[i] = (unsigned char)1;
|
---|
1701 | if (*endptr == ',')
|
---|
1702 | ++endptr;
|
---|
1703 | }
|
---|
1704 | SL_RETURN(0, _("sh_util_obscure_ok"));
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | static int sh_obscure_check_utf8 = S_FALSE;
|
---|
1708 |
|
---|
1709 | int sh_util_obscure_utf8 (const char * c)
|
---|
1710 | {
|
---|
1711 | int i;
|
---|
1712 | SL_ENTER(_("sh_util_obscure_utf8"));
|
---|
1713 | i = sh_util_flagval(c, &(sh_obscure_check_utf8));
|
---|
1714 | if (sh_obscure_check_utf8 == S_TRUE)
|
---|
1715 | sh_obscure_no_check = S_FALSE;
|
---|
1716 | SL_RETURN(i, _("sh_util_obscure_utf8"));
|
---|
1717 | }
|
---|
1718 |
|
---|
1719 |
|
---|
1720 | int sh_util_obscurename (ShErrLevel level, char * name_orig, int flag)
|
---|
1721 | {
|
---|
1722 | unsigned char * name = (unsigned char *) name_orig;
|
---|
1723 | char * safe;
|
---|
1724 | unsigned int i;
|
---|
1725 | size_t len = 0;
|
---|
1726 |
|
---|
1727 | SL_ENTER(_("sh_util_obscurename"));
|
---|
1728 |
|
---|
1729 | ASSERT_RET((name != NULL), _("name != NULL"), (0))
|
---|
1730 |
|
---|
1731 | if (sh_obscure_no_check == S_FALSE)
|
---|
1732 | {
|
---|
1733 | if (sh_obscure_check_utf8 != S_TRUE)
|
---|
1734 | {
|
---|
1735 | /* -- Check name. --
|
---|
1736 | */
|
---|
1737 | while (*name != '\0')
|
---|
1738 | {
|
---|
1739 | if ( (*name) > 0x7F || (*name) == '"' || (*name) == '\t' ||
|
---|
1740 | (*name) == '\b' || (*name) == '\f' ||
|
---|
1741 | (*name) == '\n' || (*name) == '\r' ||
|
---|
1742 | (*name) == '\v' || iscntrl((int) *name) ||
|
---|
1743 | ((*name) != ' ' && !isgraph ((int) *name)) )
|
---|
1744 | {
|
---|
1745 | i = (unsigned char) *name;
|
---|
1746 | if (sh_obscure_index[i] != (unsigned char)1)
|
---|
1747 | {
|
---|
1748 | goto err;
|
---|
1749 | }
|
---|
1750 | }
|
---|
1751 | name++; ++len;
|
---|
1752 | }
|
---|
1753 |
|
---|
1754 | /* Check for blank at end of name
|
---|
1755 | */
|
---|
1756 | if ((len > 0) && (name_orig[len-1] == ' '))
|
---|
1757 | {
|
---|
1758 | goto err;
|
---|
1759 | }
|
---|
1760 | }
|
---|
1761 | else
|
---|
1762 | {
|
---|
1763 | if (S_FALSE == sh_util_valid_utf8(name))
|
---|
1764 | {
|
---|
1765 | goto err;
|
---|
1766 | }
|
---|
1767 | SL_RETURN((0),_("sh_util_obscurename"));
|
---|
1768 | }
|
---|
1769 | }
|
---|
1770 |
|
---|
1771 | SL_RETURN((0),_("sh_util_obscurename"));
|
---|
1772 |
|
---|
1773 | err:
|
---|
1774 |
|
---|
1775 | if (flag == S_TRUE)
|
---|
1776 | {
|
---|
1777 | safe = sh_util_safe_name (name_orig);
|
---|
1778 | sh_error_handle (level, FIL__, __LINE__, 0, MSG_FI_OBSC,
|
---|
1779 | safe);
|
---|
1780 | SH_FREE(safe);
|
---|
1781 | }
|
---|
1782 | SL_RETURN((-1),_("sh_util_obscurename"));
|
---|
1783 | }
|
---|
1784 |
|
---|
1785 | #endif
|
---|
1786 |
|
---|
1787 | /* returns freshly allocated memory, return value should be free'd
|
---|
1788 | */
|
---|
1789 | char * sh_util_dirname(const char * fullpath)
|
---|
1790 | {
|
---|
1791 | char * retval;
|
---|
1792 | size_t len;
|
---|
1793 | char * tmp;
|
---|
1794 |
|
---|
1795 | SL_ENTER(_("sh_util_dirname"));
|
---|
1796 |
|
---|
1797 | ASSERT_RET ((fullpath != NULL), _("fullpath != NULL"), (NULL))
|
---|
1798 | ASSERT_RET ((*fullpath == '/'), _("*fullpath == '/'"), (NULL))
|
---|
1799 |
|
---|
1800 | retval = sh_util_strdup(fullpath);
|
---|
1801 |
|
---|
1802 | tmp = retval;
|
---|
1803 | while (*tmp == '/') ++tmp;
|
---|
1804 |
|
---|
1805 | /* (1) only leading slashes -- return exact copy
|
---|
1806 | */
|
---|
1807 | if (*tmp == '\0')
|
---|
1808 | {
|
---|
1809 | SL_RETURN(retval, _("sh_util_dirname"));
|
---|
1810 | }
|
---|
1811 |
|
---|
1812 | /* (2) there are non-slash characters, so delete trailing slashes
|
---|
1813 | */
|
---|
1814 | len = sl_strlen (retval); /* retval[len] is terminating '\0' */
|
---|
1815 |
|
---|
1816 | while (len > 1 && retval[len-1] == '/') /* delete trailing slash */
|
---|
1817 | {
|
---|
1818 | retval[len-1] = '\0';
|
---|
1819 | --len;
|
---|
1820 | }
|
---|
1821 |
|
---|
1822 | /* (3) now delete all non-slash characters up to the preceding slash
|
---|
1823 | */
|
---|
1824 | while (len > 1 && retval[len-1] != '/') {
|
---|
1825 | retval[len-1] = '\0';
|
---|
1826 | --len;
|
---|
1827 | }
|
---|
1828 |
|
---|
1829 | /* (4a) only leading slashes left, so return this
|
---|
1830 | */
|
---|
1831 | if (&(retval[len]) == tmp)
|
---|
1832 | {
|
---|
1833 | SL_RETURN(retval, _("sh_util_dirname"));
|
---|
1834 | }
|
---|
1835 |
|
---|
1836 | /* (4b) strip trailing slash(es) of parent directory
|
---|
1837 | */
|
---|
1838 | while (len > 1 && retval[len-1] == '/') {
|
---|
1839 | retval[len-1] = '\0';
|
---|
1840 | --len;
|
---|
1841 | }
|
---|
1842 | SL_RETURN(retval, _("sh_util_dirname"));
|
---|
1843 |
|
---|
1844 | }
|
---|
1845 |
|
---|
1846 | /* returns freshly allocated memory, return value should be free'd
|
---|
1847 | */
|
---|
1848 | char * sh_util_basename(const char * fullpath)
|
---|
1849 | {
|
---|
1850 | char * retval = NULL;
|
---|
1851 | const char * tmp;
|
---|
1852 | char * tmp2;
|
---|
1853 | char * c;
|
---|
1854 | size_t len;
|
---|
1855 |
|
---|
1856 | SL_ENTER(_("sh_util_basename"));
|
---|
1857 |
|
---|
1858 | ASSERT_RET ((fullpath != NULL), _("fullpath != NULL"), (NULL))
|
---|
1859 |
|
---|
1860 | tmp = fullpath; while (*tmp == '/') ++tmp;
|
---|
1861 | if (*tmp == '\0')
|
---|
1862 | {
|
---|
1863 | retval = sh_util_strdup(fullpath);
|
---|
1864 | }
|
---|
1865 | else
|
---|
1866 | {
|
---|
1867 | tmp2 = sh_util_strdup(tmp);
|
---|
1868 | len = sl_strlen (tmp2);
|
---|
1869 |
|
---|
1870 | while (len > 1 && tmp2[len-1] == '/')
|
---|
1871 | {
|
---|
1872 | tmp2[len-1] = '\0';
|
---|
1873 | --len;
|
---|
1874 | }
|
---|
1875 |
|
---|
1876 | c = strrchr(tmp2, '/');
|
---|
1877 | if (c)
|
---|
1878 | {
|
---|
1879 | retval = sh_util_strdup(++c);
|
---|
1880 | SH_FREE(tmp2);
|
---|
1881 | }
|
---|
1882 | else
|
---|
1883 | {
|
---|
1884 | retval = tmp2;
|
---|
1885 | }
|
---|
1886 | }
|
---|
1887 |
|
---|
1888 | SL_RETURN(retval, _("sh_util_basename"));
|
---|
1889 | }
|
---|
1890 |
|
---|
1891 | #define SH_ESCAPE_SPACE 1
|
---|
1892 | #define SH_DONT_ESCAPE_SPACE 0
|
---|
1893 | char * sh_util_safe_name_int (const char * name, int escape_space);
|
---|
1894 |
|
---|
1895 | char * sh_util_safe_name (const char * name)
|
---|
1896 | {
|
---|
1897 | return sh_util_safe_name_int (name, SH_ESCAPE_SPACE);
|
---|
1898 | }
|
---|
1899 |
|
---|
1900 | char * sh_util_safe_name_keepspace (const char * name)
|
---|
1901 | {
|
---|
1902 | return sh_util_safe_name_int (name, SH_DONT_ESCAPE_SPACE);
|
---|
1903 | }
|
---|
1904 |
|
---|
1905 | /* returns freshly allocated memory, return value should be free'd
|
---|
1906 | */
|
---|
1907 | char * sh_util_safe_name_int (const char * name, int escape_space)
|
---|
1908 | {
|
---|
1909 | register int i = 0;
|
---|
1910 | const char * p;
|
---|
1911 | char * retval;
|
---|
1912 | char oct[32];
|
---|
1913 | char format[16];
|
---|
1914 | size_t len;
|
---|
1915 |
|
---|
1916 | SL_ENTER(_("sh_util_safe_name"));
|
---|
1917 |
|
---|
1918 | if (name == NULL)
|
---|
1919 | {
|
---|
1920 | /* return an allocated array
|
---|
1921 | */
|
---|
1922 | retval = SH_ALLOC(7);
|
---|
1923 | (void) sl_strlcpy(retval, _("(null)"), 7);
|
---|
1924 | SL_RETURN(retval, _("sh_util_safe_name"));
|
---|
1925 | }
|
---|
1926 |
|
---|
1927 | /*
|
---|
1928 | ASSERT_RET ((name != NULL), _("name != NULL"), _("NULL"))
|
---|
1929 | */
|
---|
1930 |
|
---|
1931 | len = sl_strlen(name);
|
---|
1932 | p = name;
|
---|
1933 |
|
---|
1934 | #ifdef SH_USE_XML
|
---|
1935 | if (sl_ok_muls (6, len) && sl_ok_adds ((6*len), 2))
|
---|
1936 | { retval = SH_ALLOC(6 * len + 2); }
|
---|
1937 | else
|
---|
1938 | {
|
---|
1939 | /* return an allocated array
|
---|
1940 | */
|
---|
1941 | retval = SH_ALLOC(11);
|
---|
1942 | (void) sl_strlcpy(retval, _("(overflow)"), 11);
|
---|
1943 | SL_RETURN(retval, _("sh_util_safe_name"));
|
---|
1944 | }
|
---|
1945 | #else
|
---|
1946 | if (sl_ok_muls (4, len) && sl_ok_adds ((4*len), 2))
|
---|
1947 | { retval = SH_ALLOC(4 * len + 2); }
|
---|
1948 | else
|
---|
1949 | {
|
---|
1950 | /* return an allocated array
|
---|
1951 | */
|
---|
1952 | retval = SH_ALLOC(11);
|
---|
1953 | (void) sl_strlcpy(retval, _("(overflow)"), 11);
|
---|
1954 | SL_RETURN(retval, _("sh_util_safe_name"));
|
---|
1955 | }
|
---|
1956 | #endif
|
---|
1957 |
|
---|
1958 | (void) sl_strncpy(format, _("%c%03o"), 16);
|
---|
1959 |
|
---|
1960 | while (*p != '\0') {
|
---|
1961 | /* Most frequent cases first
|
---|
1962 | */
|
---|
1963 | if ( ((*p) >= 'a' && (*p) <= 'z') || ((*p) == '/') || ((*p) == '.') ||
|
---|
1964 | ((*p) >= '0' && (*p) <= '9') ||
|
---|
1965 | ((*p) >= 'A' && (*p) <= 'Z')) {
|
---|
1966 | retval[i] = *p;
|
---|
1967 | } else if ( (*p) == '\\') { /* backslash */
|
---|
1968 | retval[i] = '\\'; ++i;
|
---|
1969 | retval[i] = '\\';
|
---|
1970 | } else if ( (*p) == '\n') { /* newline */
|
---|
1971 | retval[i] = '\\'; ++i;
|
---|
1972 | retval[i] = 'n';
|
---|
1973 | } else if ( (*p) == '\b') { /* backspace */
|
---|
1974 | retval[i] = '\\'; ++i;
|
---|
1975 | retval[i] = 'b';
|
---|
1976 | } else if ( (*p) == '\r') { /* carriage return */
|
---|
1977 | retval[i] = '\\'; ++i;
|
---|
1978 | retval[i] = 'r';
|
---|
1979 | } else if ( (*p) == '\t') { /* horizontal tab */
|
---|
1980 | retval[i] = '\\'; ++i;
|
---|
1981 | retval[i] = 't';
|
---|
1982 | } else if ( (*p) == '\v') { /* vertical tab */
|
---|
1983 | retval[i] = '\\'; ++i;
|
---|
1984 | retval[i] = 'v';
|
---|
1985 | } else if ( (*p) == '\f') { /* form-feed */
|
---|
1986 | retval[i] = '\\'; ++i;
|
---|
1987 | retval[i] = 'f';
|
---|
1988 | #ifdef WITH_DATABASE
|
---|
1989 | } else if ( (*p) == '\'') { /* single quote */
|
---|
1990 | retval[i] = '\\'; ++i;
|
---|
1991 | retval[i] = '\'';
|
---|
1992 | #endif
|
---|
1993 | } else if ( (*p) == ' ') { /* space */
|
---|
1994 | if (escape_space) {
|
---|
1995 | retval[i] = '\\'; ++i;
|
---|
1996 | retval[i] = ' ';
|
---|
1997 | }
|
---|
1998 | else {
|
---|
1999 | retval[i] = *p;
|
---|
2000 | }
|
---|
2001 | #ifdef SH_USE_XML
|
---|
2002 | } else if ( (*p) == '"') { /* double quote */
|
---|
2003 | retval[i] = '&'; ++i;
|
---|
2004 | retval[i] = 'q'; ++i;
|
---|
2005 | retval[i] = 'u'; ++i;
|
---|
2006 | retval[i] = 'o'; ++i;
|
---|
2007 | retval[i] = 't'; ++i;
|
---|
2008 | retval[i] = ';';
|
---|
2009 | } else if ( (*p) == '&') { /* ampersand */
|
---|
2010 | retval[i] = '&'; ++i;
|
---|
2011 | retval[i] = 'a'; ++i;
|
---|
2012 | retval[i] = 'm'; ++i;
|
---|
2013 | retval[i] = 'p'; ++i;
|
---|
2014 | retval[i] = ';';
|
---|
2015 | } else if ( (*p) == '<') { /* left angle */
|
---|
2016 | retval[i] = '&'; ++i;
|
---|
2017 | retval[i] = 'l'; ++i;
|
---|
2018 | retval[i] = 't'; ++i;
|
---|
2019 | retval[i] = ';';
|
---|
2020 | } else if ( (*p) == '>') { /* right angle */
|
---|
2021 | retval[i] = '&'; ++i;
|
---|
2022 | retval[i] = 'g'; ++i;
|
---|
2023 | retval[i] = 't'; ++i;
|
---|
2024 | retval[i] = ';';
|
---|
2025 | #else
|
---|
2026 | } else if ( (*p) == '"') { /* double quote */
|
---|
2027 | retval[i] = '\\'; ++i;
|
---|
2028 | retval[i] = '\"';
|
---|
2029 | #endif
|
---|
2030 | } else if (!isgraph ((int) *p)) { /* not printable */
|
---|
2031 | /*@-bufferoverflowhigh -formatconst@*/
|
---|
2032 | /* flawfinder: ignore */
|
---|
2033 | sprintf(oct, format, '\\', /* known to fit */
|
---|
2034 | (unsigned char) *p);
|
---|
2035 | /*@+bufferoverflowhigh +formatconst@*/
|
---|
2036 | retval[i] = oct[0]; ++i;
|
---|
2037 | retval[i] = oct[1]; ++i;
|
---|
2038 | retval[i] = oct[2]; ++i;
|
---|
2039 | retval[i] = oct[3];
|
---|
2040 | } else {
|
---|
2041 | retval[i] = *p;
|
---|
2042 | }
|
---|
2043 | ++p;
|
---|
2044 | ++i;
|
---|
2045 | }
|
---|
2046 | retval[i] = '\0';
|
---|
2047 | SL_RETURN(retval, _("sh_util_safe_name"));
|
---|
2048 | }
|
---|
2049 |
|
---|
2050 | int sh_util_isnum (const char *str)
|
---|
2051 | {
|
---|
2052 | const char *p = str;
|
---|
2053 |
|
---|
2054 | SL_ENTER(_("sh_util_isnum"));
|
---|
2055 |
|
---|
2056 | ASSERT_RET ((str != NULL), _("str != NULL"), (-1))
|
---|
2057 |
|
---|
2058 | while (p) {
|
---|
2059 | if (!isdigit((int) *p) )
|
---|
2060 | SL_RETURN((-1), _("sh_util_isnum"));
|
---|
2061 | ++p;
|
---|
2062 | }
|
---|
2063 | SL_RETURN((0), _("sh_util_isnum"));
|
---|
2064 | }
|
---|
2065 |
|
---|
2066 | char * sh_util_strconcat (const char * arg1, ...)
|
---|
2067 | {
|
---|
2068 | size_t length, l2;
|
---|
2069 | char * s;
|
---|
2070 | char * strnew;
|
---|
2071 | va_list vl;
|
---|
2072 |
|
---|
2073 | SL_ENTER(_("sh_util_strconcat"));
|
---|
2074 |
|
---|
2075 | ASSERT_RET ((arg1 != NULL), _("arg1 != NULL"), (NULL))
|
---|
2076 |
|
---|
2077 | length = sl_strlen (arg1) + 1;
|
---|
2078 |
|
---|
2079 | va_start (vl, arg1);
|
---|
2080 | s = va_arg (vl, char * );
|
---|
2081 | while (s != NULL)
|
---|
2082 | {
|
---|
2083 | l2 = sl_strlen (s);
|
---|
2084 | if (sl_ok_adds(length, l2))
|
---|
2085 | length += l2;
|
---|
2086 | else
|
---|
2087 | SL_RETURN(NULL, _("sh_util_strconcat"));
|
---|
2088 | s = va_arg (vl, char * );
|
---|
2089 | }
|
---|
2090 | va_end (vl);
|
---|
2091 |
|
---|
2092 | if (sl_ok_adds(length, 2))
|
---|
2093 | strnew = SH_ALLOC( length + 2 );
|
---|
2094 | else
|
---|
2095 | SL_RETURN(NULL, _("sh_util_strconcat"));
|
---|
2096 |
|
---|
2097 | strnew[0] = '\0';
|
---|
2098 |
|
---|
2099 | (void) sl_strlcpy (strnew, arg1, length + 2);
|
---|
2100 |
|
---|
2101 | va_start (vl, arg1);
|
---|
2102 | s = va_arg (vl, char * );
|
---|
2103 | while (s)
|
---|
2104 | {
|
---|
2105 | (void) sl_strlcat (strnew, s, length + 2);
|
---|
2106 | s = va_arg (vl, char * );
|
---|
2107 | }
|
---|
2108 | va_end (vl);
|
---|
2109 |
|
---|
2110 | SL_RETURN(strnew, _("sh_util_strconcat"));
|
---|
2111 | }
|
---|
2112 |
|
---|
2113 | static const char bto64_0[] = N_("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789()");
|
---|
2114 | static char bto64[65] = { '\0' };
|
---|
2115 |
|
---|
2116 |
|
---|
2117 | size_t sh_util_base64_enc (unsigned char * out,
|
---|
2118 | const unsigned char * instr,
|
---|
2119 | size_t lin)
|
---|
2120 | {
|
---|
2121 | int ll;
|
---|
2122 | unsigned char a, b, c;
|
---|
2123 | size_t len = 0;
|
---|
2124 | size_t j = 0;
|
---|
2125 |
|
---|
2126 | start:
|
---|
2127 | if (bto64[0] != '\0')
|
---|
2128 | {
|
---|
2129 | if (instr && *instr)
|
---|
2130 | {
|
---|
2131 | if (lin == 0)
|
---|
2132 | lin = strlen((const char *)instr);
|
---|
2133 |
|
---|
2134 | do {
|
---|
2135 | ll = 0;
|
---|
2136 |
|
---|
2137 | if (len < lin)
|
---|
2138 | { a = *instr; ++instr; ++len; ++ll; }
|
---|
2139 | else
|
---|
2140 | { a = 0; }
|
---|
2141 | if (len < lin)
|
---|
2142 | { b = *instr; ++instr; ++len; ++ll; }
|
---|
2143 | else
|
---|
2144 | { b = 0; }
|
---|
2145 | if (len < lin)
|
---|
2146 | { c = *instr; ++instr; ++len; ++ll; }
|
---|
2147 | else
|
---|
2148 | { c = 0; }
|
---|
2149 |
|
---|
2150 | *out = bto64[ a >> 2 ];
|
---|
2151 | ++j; ++out;
|
---|
2152 | *out = bto64[ ((a & 0x03) << 4) | ((b & 0xf0) >> 4) ];
|
---|
2153 | ++j; ++out;
|
---|
2154 | *out = (unsigned char) (ll > 1 ? bto64[ ((b & 0x0f) << 2) | ((c & 0xc0) >> 6) ] : '?');
|
---|
2155 | ++j; ++out;
|
---|
2156 | *out = (unsigned char) (ll > 2 ? bto64[ c & 0x3f ] : '?');
|
---|
2157 | ++j; ++out;
|
---|
2158 | } while (len < lin);
|
---|
2159 | }
|
---|
2160 | *out = '\0';
|
---|
2161 | return j;
|
---|
2162 | }
|
---|
2163 |
|
---|
2164 | memcpy(bto64, _(bto64_0), 65);
|
---|
2165 | goto start;
|
---|
2166 | }
|
---|
2167 |
|
---|
2168 | size_t sh_util_base64_enc_alloc (char **out, const char *in, size_t inlen)
|
---|
2169 | {
|
---|
2170 | size_t outlen = SH_B64_SIZ(inlen);
|
---|
2171 |
|
---|
2172 | if (inlen > outlen) /* overflow */
|
---|
2173 | {
|
---|
2174 | *out = NULL;
|
---|
2175 | return 0;
|
---|
2176 | }
|
---|
2177 |
|
---|
2178 | *out = SH_ALLOC(outlen);
|
---|
2179 | return sh_util_base64_enc((unsigned char *)*out, (const unsigned char *)in, inlen);
|
---|
2180 | }
|
---|
2181 |
|
---|
2182 | size_t sh_util_base64_dec (unsigned char *out,
|
---|
2183 | const unsigned char *in,
|
---|
2184 | size_t lin)
|
---|
2185 | {
|
---|
2186 | size_t i;
|
---|
2187 | unsigned char c;
|
---|
2188 | unsigned char b;
|
---|
2189 | size_t lout = 0;
|
---|
2190 | int w = 0;
|
---|
2191 |
|
---|
2192 | if (out && in)
|
---|
2193 | {
|
---|
2194 | if (lin == 0)
|
---|
2195 | lin = strlen((const char *)in);
|
---|
2196 |
|
---|
2197 | for (i = 0; i < lin; i++)
|
---|
2198 | {
|
---|
2199 | c = *in; ++in;
|
---|
2200 | b = 0;
|
---|
2201 |
|
---|
2202 | if ((c >= 'A') && (c <= 'Z'))
|
---|
2203 | {
|
---|
2204 | b = (c - 'A');
|
---|
2205 | }
|
---|
2206 | else if ((c >= 'a') && (c <= 'z'))
|
---|
2207 | {
|
---|
2208 | b = (c - 'a' + 26);
|
---|
2209 | }
|
---|
2210 | else if ((c >= '0') && (c <= '9'))
|
---|
2211 | {
|
---|
2212 | b = (c - '0' + 52);
|
---|
2213 | }
|
---|
2214 | else if (c == '(' || c == '+')
|
---|
2215 | {
|
---|
2216 | b = 62;
|
---|
2217 | }
|
---|
2218 | else if (c == ')' || c == '/')
|
---|
2219 | {
|
---|
2220 | b = 63;
|
---|
2221 | }
|
---|
2222 | else if (c == '?' || c == '=')
|
---|
2223 | {
|
---|
2224 | /* last byte was written to, but will now get
|
---|
2225 | * truncated
|
---|
2226 | */
|
---|
2227 | if (lout > 0) --lout;
|
---|
2228 | break;
|
---|
2229 | }
|
---|
2230 |
|
---|
2231 | if (w == 0)
|
---|
2232 | {
|
---|
2233 | *out = (b << 2) & 0xfc;
|
---|
2234 | ++lout;
|
---|
2235 | }
|
---|
2236 | else if (w == 1)
|
---|
2237 | {
|
---|
2238 | *out |= (b >> 4) & 0x03;
|
---|
2239 | ++out;
|
---|
2240 | *out = (b << 4) & 0xf0;
|
---|
2241 | ++lout;
|
---|
2242 | }
|
---|
2243 | else if (w == 2)
|
---|
2244 | {
|
---|
2245 | *out |= (b >> 2) & 0x0f;
|
---|
2246 | ++out;
|
---|
2247 | *out = (b << 6) & 0xc0;
|
---|
2248 | ++lout;
|
---|
2249 | }
|
---|
2250 | else if (w == 3)
|
---|
2251 | {
|
---|
2252 | *out |= b & 0x3f;
|
---|
2253 | ++out;
|
---|
2254 | }
|
---|
2255 |
|
---|
2256 | ++w;
|
---|
2257 |
|
---|
2258 | if (w == 4)
|
---|
2259 | {
|
---|
2260 | w = 0;
|
---|
2261 | }
|
---|
2262 | }
|
---|
2263 | *out = '\0';
|
---|
2264 | }
|
---|
2265 | return lout;
|
---|
2266 | }
|
---|
2267 |
|
---|
2268 | size_t sh_util_base64_dec_alloc (unsigned char **out, const unsigned char *in,
|
---|
2269 | size_t lin)
|
---|
2270 | {
|
---|
2271 | size_t lout = 3 * (lin / 4) + 2;
|
---|
2272 |
|
---|
2273 | *out = SH_ALLOC(lout);
|
---|
2274 |
|
---|
2275 | return sh_util_base64_dec (*out, in, lin);
|
---|
2276 | }
|
---|
2277 |
|
---|
2278 |
|
---|
2279 | #ifdef HAVE_REGEX_H
|
---|
2280 |
|
---|
2281 | #include <regex.h>
|
---|
2282 |
|
---|
2283 | int sh_util_regcmp (char * regex_str, char * in_str)
|
---|
2284 | {
|
---|
2285 | #if defined(REG_ESPACE)
|
---|
2286 | int status = REG_ESPACE;
|
---|
2287 | #else
|
---|
2288 | int status = -1;
|
---|
2289 | #endif
|
---|
2290 | regex_t preg;
|
---|
2291 | char * errbuf;
|
---|
2292 |
|
---|
2293 | SL_ENTER(_("sh_util_regcmp"));
|
---|
2294 |
|
---|
2295 | status = regcomp(&preg, regex_str, REG_NOSUB|REG_EXTENDED);
|
---|
2296 |
|
---|
2297 | if (status == 0)
|
---|
2298 | {
|
---|
2299 | if ((status = regexec(&preg, in_str, 0, NULL, 0)) == 0)
|
---|
2300 | {
|
---|
2301 | regfree (&preg);
|
---|
2302 | SL_RETURN((0), _("sh_util_regcmp"));
|
---|
2303 | }
|
---|
2304 | }
|
---|
2305 |
|
---|
2306 | if (status != 0 && status != REG_NOMATCH)
|
---|
2307 | {
|
---|
2308 | errbuf = SH_ALLOC(BUFSIZ);
|
---|
2309 | (void) regerror(status, &preg, errbuf, BUFSIZ);
|
---|
2310 | errbuf[BUFSIZ-1] = '\0';
|
---|
2311 | sh_error_handle ((-1), FIL__, __LINE__, status, MSG_E_REGEX,
|
---|
2312 | errbuf, regex_str);
|
---|
2313 | SH_FREE(errbuf);
|
---|
2314 | }
|
---|
2315 |
|
---|
2316 | regfree (&preg);
|
---|
2317 | SL_RETURN((-1), _("sh_util_regcmp"));
|
---|
2318 | }
|
---|
2319 |
|
---|
2320 | #endif
|
---|
2321 |
|
---|
2322 |
|
---|
2323 |
|
---|
2324 |
|
---|
2325 |
|
---|
2326 |
|
---|
2327 |
|
---|
2328 |
|
---|