source: trunk/src/sh_tools.c@ 558

Last change on this file since 558 was 550, checked in by katerina, 5 years ago

Fix for ticket #442 (support for OpenBSD signify).

File size: 54.0 KB
Line 
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 <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <ctype.h>
27
28/* Must be early on FreeBSD
29 */
30#include <sys/types.h>
31
32#ifdef HAVE_MEMORY_H
33#include <memory.h>
34#endif
35
36#ifdef HAVE_SYS_SELECT_H
37#include <sys/select.h>
38#endif
39
40#ifdef HAVE_UNISTD_H
41#include <errno.h>
42#include <signal.h>
43#include <setjmp.h>
44#include <pwd.h>
45#include <grp.h>
46#include <sys/stat.h>
47#include <sys/resource.h>
48#include <fcntl.h>
49#include <sys/wait.h>
50#include <unistd.h>
51#endif
52
53#include <sys/socket.h>
54
55#ifdef HOST_IS_HPUX
56#define _XOPEN_SOURCE_EXTENDED
57#endif
58#include <netinet/in.h>
59#include <arpa/inet.h>
60#include <netdb.h>
61
62#ifndef FD_SET
63#define NFDBITS 32
64#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
65#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
66#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
67#endif /* !FD_SET */
68#ifndef FD_SETSIZE
69#define FD_SETSIZE 32
70#endif
71#ifndef FD_ZERO
72#define FD_ZERO(p) memset((char *)(p), 0, sizeof(*(p)))
73#endif
74
75
76#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
77#include <sys/mman.h>
78#endif
79
80#define SH_REAL_SET
81
82#include "samhain.h"
83#include "sh_mem.h"
84#include "sh_error.h"
85#include "sh_tools.h"
86#include "sh_utils.h"
87#include "sh_tiger.h"
88#define SH_NEED_GETHOSTBYXXX
89#include "sh_static.h"
90#include "sh_pthread.h"
91#include "sh_ipvx.h"
92
93#undef FIL__
94#define FIL__ _("sh_tools.c")
95
96static int tools_debug = 0;
97
98#ifdef SH_ENCRYPT
99#include "rijndael-api-fst.h"
100char * errorExplain (int err_num, char * buffer, size_t len)
101{
102 char * p;
103
104 if (err_num == BAD_KEY_DIR)
105 p = (_("Key direction is invalid"));
106 else if (err_num == BAD_KEY_MAT)
107 p = (_("Key material not of correct length"));
108 else if (err_num == BAD_KEY_INSTANCE)
109 p = (_("Key passed is not valid"));
110 else if (err_num == BAD_CIPHER_MODE)
111 p = (_("Params struct passed to rijndael_cipherInit invalid"));
112 else if (err_num == BAD_CIPHER_STATE)
113 p = (_("Cipher in wrong state"));
114 else if (err_num == BAD_BLOCK_LENGTH)
115 p = (_("Bad block length"));
116 else if (err_num == BAD_CIPHER_INSTANCE)
117 p = (_("Bad cipher instance"));
118 else if (err_num == BAD_DATA)
119 p = (_("Data contents are invalid"));
120 else
121 p = (_("Unknown error"));
122 sl_strlcpy (buffer, p, len);
123 return buffer;
124}
125
126#endif
127
128/* --- check for an interface ---
129 */
130int sh_tools_iface_is_present(char *str)
131{
132#if defined(USE_IPVX)
133 struct addrinfo *ai;
134 struct addrinfo hints;
135 int res;
136
137 memset (&hints, 0, sizeof (hints));
138 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
139 hints.ai_socktype = SOCK_STREAM;
140 res = getaddrinfo (str, _("2543"), &hints, &ai);
141
142 if (res == 0)
143 {
144 struct addrinfo *p = ai;
145 while (p != NULL)
146 {
147 int fd = socket (p->ai_family, p->ai_socktype,
148 p->ai_protocol);
149
150 if (fd < 0)
151 {
152 freeaddrinfo (ai);
153 return 0;
154 }
155
156 if (bind (fd, p->ai_addr, p->ai_addrlen) != 0)
157 {
158 /* bind() fails for access reasons, iface exists
159 */
160 if (errno == EACCES || errno == EADDRINUSE)
161 {
162 sl_close_fd (FIL__, __LINE__, fd);
163 freeaddrinfo (ai);
164 return 1;
165 }
166
167 sl_close_fd (FIL__, __LINE__, fd);
168 freeaddrinfo (ai);
169 return 0;
170 }
171
172 sl_close_fd (FIL__, __LINE__, fd);
173 freeaddrinfo (ai);
174 return 1;
175 /* p = p->ai_next; */
176 }
177 }
178#else
179 struct sockaddr_in sin;
180 int sd;
181
182 memset(&sin, 0, sizeof(sin));
183 sin.sin_family = AF_INET;
184 if (inet_aton(str, &(sin.sin_addr)))
185 {
186 sin.sin_port = htons(2543);
187
188 if (-1 == (sd = socket(AF_INET, SOCK_STREAM, 0)))
189 {
190 /* cppcheck-suppress resourceLeak */
191 return 0;
192 }
193
194 if (-1 == bind(sd, (struct sockaddr *)&sin, sizeof(sin)))
195 {
196 int retval = 0;
197
198 /* bind() fails for access reasons, iface exists
199 */
200 if (errno == EACCES || errno == EADDRINUSE)
201 retval = 1;
202 sl_close_fd (FIL__, __LINE__, sd);
203 return retval;
204 }
205
206 /* bind() succeeds, iface exists
207 */
208 sl_close_fd(FIL__, __LINE__, sd);
209 return 1;
210 }
211#endif
212 return 0;
213}
214
215/* --- recode all \blah escapes to qp (quoted printable) '=XX' format, and
216 * also code all remaining unprintable chars ---
217 */
218#define SH_PUT_4(p, a, b, c) (p)[0] = (a); (p)[1] = (b); (p)[2] = (c);
219
220char * sh_tools_safe_name (const char * instr, int flag)
221{
222 unsigned char c, d;
223 const char * p;
224 char tmp[4];
225 char * outstr;
226 size_t len = 1;
227 int i = 0;
228 unsigned char val_octal = '\0';
229 static char ctable[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
230 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
231
232 SL_ENTER(_("sh_tools_safe_name"));
233
234 if (instr)
235 {
236 len = strlen(instr);
237 if (sl_ok_muls (3, len) && sl_ok_adds ((3*len), 4))
238 {
239 len = (3 * len) + 4;
240 p = instr;
241 }
242 else
243 {
244 len = 1;
245 p = NULL;
246 }
247 }
248 else
249 {
250 p = NULL;
251 }
252
253 outstr = SH_ALLOC(len);
254
255 outstr[0] = '\0';
256 tmp[3] = '\0';
257
258#if !defined(SH_USE_XML)
259 (void) flag; /* fix compiler warning */
260#endif
261
262 if (!p)
263 goto end;
264
265 while (*p)
266 {
267 c = *p;
268
269 if (*p == '\n')
270 {
271 outstr[i] = ' '; ++i; ++p;
272 continue;
273 }
274
275#ifdef SH_USE_XML
276 if (flag == 1)
277 {
278 if ((*p) == '"')
279 {
280 SH_PUT_4(&outstr[i], '=', '2', '2');
281 i+=3; ++p;
282 continue;
283 }
284 else if ((*p) == '&')
285 {
286 SH_PUT_4(&outstr[i], '=', '2', '6');
287 i+=3; ++p;
288 continue;
289 }
290 else if ((*p) == '<')
291 { /* left angle */
292 SH_PUT_4(&outstr[i], '=', '3', 'c');
293 i+=3; ++p;
294 continue;
295 }
296 else if ((*p) == '>')
297 { /* right angle */
298 SH_PUT_4(&outstr[i], '=', '3', 'e');
299 i+=3; ++p;
300 continue;
301 }
302 }
303#endif
304
305 if ( (*p) != '\\' && (*p) != '&' && (*p) != '=' && (*p) != '\'')
306 {
307 outstr[i] = *p; ++i;
308 ++p;
309
310 if (c < 32 || c > 126)
311 {
312 --i;
313 d = c % 16; c = c / 16;
314 outstr[i] = '='; ++i;
315 outstr[i] = ctable[c]; ++i;
316 outstr[i] = ctable[d]; ++i;
317 }
318
319 continue;
320 }
321 else if ((*p) == '\'')
322 {
323 SH_PUT_4(&outstr[i], '=', '2', '7');
324 i+=3; ++p;
325 }
326 else if (*p == '=')
327 {
328 if (p[1] != '"' && p[1] != '<')
329 {
330 SH_PUT_4(&outstr[i], '=', '3', 'd');
331 i+=3; ++p;
332 }
333 else
334 { outstr[i] = *p; ++i; ++p; }
335 }
336 else if (*p == '\\')
337 {
338 ++p;
339 if (!p)
340 break;
341 if (!(*p))
342 break;
343
344
345
346 switch (*p) {
347 case '\\':
348 SH_PUT_4(&outstr[i], '=', '5', 'c');
349 i+=3; ++p;
350 break;
351 case 'n':
352 SH_PUT_4(&outstr[i], '=', '0', 'a');
353 i+=3; ++p;
354 break;
355 case 'b':
356 SH_PUT_4(&outstr[i], '=', '0', '8');
357 i+=3; ++p;
358 break;
359 case 'r':
360 SH_PUT_4(&outstr[i], '=', '0', 'd');
361 i+=3; ++p;
362 break;
363 case 't':
364 SH_PUT_4(&outstr[i], '=', '0', '9');
365 i+=3; ++p;
366 break;
367 case 'v':
368 SH_PUT_4(&outstr[i], '=', '0', 'b');
369 i+=3; ++p;
370 break;
371 case 'f':
372 SH_PUT_4(&outstr[i], '=', '0', 'c');
373 i+=3; ++p;
374 break;
375 case '\'':
376 SH_PUT_4(&outstr[i], '=', '2', '7');
377 i+=3; ++p;
378 break;
379 case '"': /* also encode quoted '"' */
380 SH_PUT_4(&outstr[i], '=', '2', '2');
381 i+=3; ++p;
382 break;
383 case ' ':
384 SH_PUT_4(&outstr[i], '=', '2', '0');
385 i+=3; ++p;
386 break;
387 default:
388 if (strlen(p) < 3) /* certainly not an octal number, skip */
389 {
390 p += strlen(p);
391 }
392 else
393 {
394 tmp[0] = p[0]; tmp[1] = p[1]; tmp[2] = p[2];
395 val_octal = (unsigned char) strtoul(tmp, (char **)NULL, 8);
396 if (val_octal != '\0') {
397 c = val_octal;
398 d = c % 16; c = c / 16;
399 outstr[i] = '='; ++i;
400 outstr[i] = ctable[c]; ++i;
401 outstr[i] = ctable[d]; ++i;
402 }
403 p += 3;
404 }
405 }
406 }
407 else if (*p == '&')
408 {
409 ++p;
410 if (!p || !(*p))
411 {
412 outstr[i] = '&'; ++i;
413 break;
414 }
415
416 if (p[0] == 'a' && p[1] == 'm' && p[2] == 'p' && p[3] == ';')
417 {
418 SH_PUT_4(&outstr[i], '=', '2', '6');
419 i+=3; p += 4;
420 }
421 else if (p[0] == 'q' && p[1] == 'u' && p[2] == 'o' && p[3] == 't' &&
422 p[4] == ';')
423 {
424 SH_PUT_4(&outstr[i], '=', '2', '2');
425 i+=3; p += 5;
426 }
427 else if (p[0] == 'l' && p[1] == 't' && p[2] == ';')
428 {
429 SH_PUT_4(&outstr[i], '=', '3', 'c');
430 i+=3; p += 3;
431 }
432 else if (p[0] == 'g' && p[1] == 't' && p[2] == ';')
433 {
434 SH_PUT_4(&outstr[i], '=', '3', 'e');
435 i+=3; p += 3;
436 }
437 else /* conserve the '&' */
438 {
439 outstr[i] = '&'; ++i;
440 }
441 }
442 else
443 {
444 outstr[i] = *p; ++i;
445 ++p;
446 }
447 } /* while (p && *p) */
448
449 end:
450
451 outstr[i] = '\0';
452 SL_RETURN( outstr, _("sh_tools_safe_name"));
453}
454
455
456/* extern int h_errno; */
457
458char * sh_tools_errmessage (int tellme, char * errbuf, size_t len)
459{
460 char * p = NULL;
461#ifdef HOST_NOT_FOUND
462 if (tellme == HOST_NOT_FOUND)
463 p = _("The specified host is unknown: ");
464#endif
465#ifdef NO_ADDRESS
466 if (tellme == NO_ADDRESS)
467 p = _("The requested name is valid but does not have an IP address: ");
468#endif
469#ifdef NO_RECOVERY
470 if (tellme == NO_RECOVERY)
471 p = _("A non-recoverable name server error occurred: ");
472#endif
473#ifdef TRY_AGAIN
474 if (tellme == TRY_AGAIN)
475 p = _("A temporary error occurred on an authoritative name server. The specified host is unknown: ");
476#endif
477 if (!p) p = _("Unknown error");
478 sl_strlcpy(errbuf, p, len);
479 return errbuf;
480}
481
482#if defined (SH_WITH_SERVER)
483
484int get_open_max ()
485{
486 int value;
487
488#ifdef _SC_OPEN_MAX
489 value = sysconf (_SC_OPEN_MAX);
490#else
491#ifdef OPEN_MAX
492 value = OPEN_MAX;
493#else
494 value = _POSIX_OPEN_MAX;
495#endif
496#endif
497
498 if (value < 0)
499 value = 8; /* POSIX lower limit */
500
501 if (value > 4096)
502 value = 4096;
503
504 return value;
505}
506
507#endif
508
509typedef struct _sin_cache {
510 char * address;
511 struct sh_sockaddr saddr;
512 struct _sin_cache * next;
513} sin_cache;
514
515static sin_cache * conn_cache = NULL;
516static int cached_addr = 0;
517
518void delete_cache()
519{
520 sin_cache * check_cache = conn_cache;
521 sin_cache * old_entry;
522
523 SL_ENTER(_("delete_cache"));
524
525 while (check_cache != NULL)
526 {
527 old_entry = check_cache;
528 check_cache = check_cache->next;
529 SH_FREE(old_entry->address);
530 SH_FREE(old_entry);
531 }
532
533 cached_addr = 0;
534
535 conn_cache = NULL;
536 SL_RET0(_("delete_cache"));
537}
538
539int DoReverseLookup = S_TRUE;
540
541int set_reverse_lookup (const char * c)
542{
543 return sh_util_flagval(c, &DoReverseLookup);
544}
545
546#if !defined(USE_IPVX)
547int connect_port (char * address, int port,
548 char * ecall, int * errnum, char * errmsg, int errsiz)
549{
550 struct in_addr haddr; /* host address from numeric */
551 /* host details returned by the DNS */
552 struct hostent *host_entry = NULL;
553 struct sockaddr_in sinr; /* socket to the remote host */
554
555 char * host_name;
556
557 volatile int fd = (-1);
558 int status;
559 volatile int fail = 0;
560 int cached = 0;
561
562 int retval;
563 char errbuf[SH_ERRBUF_SIZE];
564
565 sin_cache * check_cache = conn_cache;
566
567 SL_ENTER(_("connect_port"));
568
569 if (tools_debug)
570 fprintf(stderr, _("-00- <%s> <%d> no IPv6 support\n"), address, port);
571
572 if (errsiz > 0) errmsg[0] = '\0';
573
574 /* paranoia -- should not happen
575 */
576 if (cached_addr > 128)
577 delete_cache();
578
579 if (check_cache != NULL)
580 {
581 while (check_cache && check_cache->address)
582 {
583 if (tools_debug)
584 fprintf(stderr, _("-01- <%s> <%s>\n"),
585 address, check_cache->address);
586
587 if ( 0 == sl_strncmp(check_cache->address,
588 address, sl_strlen(address)) )
589 {
590 memcpy (&sinr, &((check_cache->saddr).sin), sizeof(struct sockaddr_in));
591 sinr.sin_family = AF_INET;
592 sinr.sin_port = htons (port);
593 cached = 1;
594 break;
595 }
596 if (tools_debug)
597 {
598 char eaddr[SH_IP_BUF];
599 sl_strlcpy(eaddr,
600 inet_ntoa(*(struct in_addr *) &(sinr.sin_addr)),
601 sizeof(eaddr));
602 fprintf(stderr, _("-02- <AF_INET> <%s> <%d> <%d>\n"),
603 eaddr,
604 port, cached);
605 }
606 if (check_cache->next)
607 check_cache = check_cache->next;
608 else
609 check_cache = NULL;
610 }
611 }
612
613 /* only use gethostbyname() if neccessary
614 */
615 if (cached == 0)
616 {
617 if (tools_debug)
618 fputs(_("-03- not cached\n"), stderr);
619#ifdef HAVE_INET_ATON
620 if (0 == inet_aton(address, &haddr))
621#else
622 if ((unsigned long)-1 == (haddr.s_addr = inet_addr(address)))
623#endif
624 {
625 SH_MUTEX_LOCK(mutex_resolv);
626
627 host_name = NULL;
628
629 host_entry = sh_gethostbyname(address);
630
631 if (host_entry == NULL || host_entry->h_addr == NULL)
632 {
633 sl_strlcpy(ecall, _("gethostbyname"), SH_MINIBUF);
634#ifndef NO_H_ERRNO
635 *errnum = h_errno;
636#else
637 *errnum = 666;
638#endif
639 (void) sh_tools_errmessage (*errnum, errmsg, errsiz);
640 sl_strlcat(errmsg, address, errsiz);
641 fail = (-1);
642 }
643 else
644 {
645 sinr.sin_family = AF_INET;
646 sinr.sin_port = htons (port);
647 sinr.sin_addr = *(struct in_addr *) host_entry->h_addr;
648
649 if (tools_debug)
650 fprintf(stderr,
651 _("-04- <%s> <%s> hostent->h_name %s <%s> hostent->h_addr\n"),
652 address,
653 (host_entry->h_name == NULL) ? _("NULL") : host_entry->h_name,
654 (host_entry->h_addrtype == AF_INET) ? _("AF_INET") : _("AF_INET6"),
655 inet_ntoa(*(struct in_addr *) &(sinr.sin_addr)));
656
657 /* reverse DNS lookup
658 */
659 if (DoReverseLookup == S_TRUE)
660 {
661 if (host_entry->h_name == NULL)
662 {
663 host_name = SH_ALLOC(1);
664 host_name[0] = '\0';
665 }
666 else
667 {
668 host_name = sh_util_strdup(host_entry->h_name);
669 }
670
671 host_entry = sh_gethostbyaddr ((char *) &sinr.sin_addr,
672 sizeof(struct in_addr),
673 AF_INET);
674 if (host_entry == NULL || host_entry->h_name == NULL)
675 {
676 sl_strlcpy(ecall, _("gethostbyaddr"), SH_MINIBUF);
677#ifndef NO_H_ERRNO
678 *errnum = h_errno;
679#else
680 *errnum = 666;
681#endif
682 (void) sh_tools_errmessage (*errnum, errmsg, errsiz);
683 sl_strlcat(errmsg,
684 inet_ntoa (*(struct in_addr *) &(sinr.sin_addr)),
685 errsiz);
686 fail = (-1);
687 }
688 else
689 {
690 *errnum = 0;
691 if (sl_strlen(host_entry->h_name) == 0 ||
692 (*errnum = sl_strcasecmp(host_name,host_entry->h_name)) != 0)
693 {
694 if (*errnum)
695 sl_strlcpy(ecall, _("strcmp"), SH_MINIBUF);
696 else
697 sl_strlcpy(ecall, _("strlen"), SH_MINIBUF);
698 sl_strlcpy(errmsg, _("Reverse lookup failed: "),
699 errsiz);
700 sl_strlcat(errmsg, address, errsiz);
701 sl_strlcat(errmsg, _(" vs "), errsiz);
702 sl_strlcat(errmsg,
703 inet_ntoa (*(struct in_addr *) &(sinr.sin_addr)),
704 errsiz);
705 fail = -1;
706 }
707 }
708 }
709 }
710 SH_MUTEX_UNLOCK(mutex_resolv);
711 if (host_name) SH_FREE(host_name);
712 }
713
714 else /* address was numeric */
715 {
716 sinr.sin_family = AF_INET;
717 sinr.sin_port = htons (port);
718 sinr.sin_addr = haddr;
719
720 if (tools_debug)
721 fprintf(stderr,
722 _("-04- <%s> is_numeric AF_INET <%s> \n"),
723 address,
724 inet_ntoa(*(struct in_addr *) &(sinr.sin_addr)));
725 }
726
727
728 if (fail != -1)
729 {
730 /* put it into the cache
731 */
732 check_cache = SH_ALLOC(sizeof(sin_cache));
733 check_cache->address = SH_ALLOC(sl_strlen(address) + 1);
734 sl_strlcpy (check_cache->address, address, sl_strlen(address) + 1);
735
736 sh_ipvx_save(&(check_cache->saddr), AF_INET, (struct sockaddr *) &sinr);
737
738 ++cached_addr;
739
740 if (conn_cache)
741 {
742 if (conn_cache->next)
743 check_cache->next = conn_cache->next;
744 else
745 check_cache->next = NULL;
746 conn_cache->next = check_cache;
747 }
748 else
749 {
750 check_cache->next = NULL;
751 conn_cache = check_cache;
752 }
753 }
754 }
755
756
757 if (fail != (-1))
758 {
759 fd = socket(AF_INET, SOCK_STREAM, 0);
760 if (fd < 0) {
761 fail = (-1);
762 status = errno;
763 sl_strlcpy(ecall, _("socket"), SH_MINIBUF);
764 *errnum = status;
765 sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
766 sl_strlcat(errmsg, _(", address "), errsiz);
767 sl_strlcat(errmsg, address, errsiz);
768 }
769 }
770
771 if (fail != (-1)) {
772
773 if ( retry_connect(FIL__, __LINE__, fd,
774 (struct sockaddr *) &sinr, sizeof(sinr)) < 0)
775 {
776 status = errno;
777 sl_strlcpy(ecall, _("connect"), SH_MINIBUF);
778 *errnum = status;
779 sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
780 sl_strlcat(errmsg,
781 (sinr.sin_family == AF_INET) ? _(", AF_INET ") : _(", AF_INET6 "),
782 errsiz);
783 sl_strlcat(errmsg, _(", address "), errsiz);
784 sl_strlcat(errmsg, address, errsiz);
785 sl_close_fd(FIL__, __LINE__, fd);
786 fail = (-1);
787 }
788 }
789
790 retval = (fail < 0) ? (-1) : fd;
791 SL_RETURN(retval, _("connect_port"));
792}
793#else
794int connect_port (char * address, int port,
795 char * ecall, int * errnum, char * errmsg, int errsiz)
796{
797 struct sockaddr_in *sin;
798 struct sockaddr_in6 *sin6;
799 struct sh_sockaddr ss;
800 sin_cache * check_cache = conn_cache;
801 int cached = 0;
802 int fail = 0;
803 int fd = -1;
804 int status = 0;
805
806 int retval;
807 char errbuf[SH_ERRBUF_SIZE];
808
809 SL_ENTER(_("connect_port"));
810
811 /* paranoia -- should not happen
812 */
813 if (cached_addr > 128)
814 delete_cache();
815
816 if (tools_debug)
817 fprintf(stderr, _("-00- <%s> <%d>\n"), address, port);
818
819 if (check_cache != NULL)
820 {
821 while (check_cache && check_cache->address)
822 {
823 if (tools_debug)
824 fprintf(stderr, _("-01- <%s> <%s>\n"),
825 address, check_cache->address);
826
827 if ( 0 == sl_strcmp(check_cache->address, address) )
828 {
829 memcpy (&ss, &(check_cache->saddr), sizeof(struct sh_sockaddr));
830 switch (ss.ss_family)
831 {
832 case AF_INET:
833 sin = &(ss.sin);
834 sin->sin_port = htons (port);
835 cached = 1;
836 break;
837 case AF_INET6:
838 sin6 = &(ss.sin6);
839 sin6->sin6_port = htons (port);
840 cached = 1;
841 break;
842 default:
843 break;
844 }
845 if (tools_debug)
846 {
847 char eaddr[SH_IP_BUF];
848 sh_ipvx_ntoa(eaddr, sizeof(eaddr), &ss);
849 fprintf(stderr, _("-02- <%s> <%s> <%d> <%d>\n"),
850 (ss.ss_family == AF_INET) ? _("AF_INET") : _("AF_INET6"),
851 eaddr,
852 port, cached);
853 }
854 break;
855 }
856 if (check_cache->next)
857 check_cache = check_cache->next;
858 else
859 check_cache = NULL;
860 }
861 }
862
863 if (cached != 0)
864 {
865 if (tools_debug)
866 fputs(_("-03- cached\n"), stderr);
867 /* cppcheck-suppress uninitStructMember */
868 fd = socket(ss.ss_family, SOCK_STREAM, 0);
869 if (fd < 0)
870 {
871 status = errno;
872 fail = (-1);
873 sl_strlcpy(ecall, _("socket"), SH_MINIBUF);
874 *errnum = status;
875 sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
876 sl_strlcat(errmsg, _(", address "), errsiz);
877 sl_strlcat(errmsg, address, errsiz);
878 }
879
880
881 if (fail != (-1))
882 {
883 /* cppcheck-suppress uninitStructMember */
884 int addrlen = SH_SS_LEN(ss);
885
886 if ( retry_connect(FIL__, __LINE__, fd,
887 sh_ipvx_sockaddr_cast(&ss), addrlen) < 0)
888 {
889 status = errno;
890 sl_strlcpy(ecall, _("connect"), SH_MINIBUF);
891 *errnum = status;
892 sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
893 sl_strlcat(errmsg, _(", address "), errsiz);
894 sl_strlcat(errmsg, address, errsiz);
895 sl_close_fd(FIL__, __LINE__, fd);
896 fail = (-1);
897 }
898 }
899
900 if (fail != 0)
901 {
902 delete_cache();
903 cached = 0;
904 }
905 }
906
907 if (cached == 0)
908 {
909 int res;
910 char sport[32];
911 struct addrinfo *ai;
912 struct addrinfo hints;
913
914 if (tools_debug)
915 fputs(_("-03- not cached\n"), stderr);
916
917 memset (&hints, 0, sizeof (hints));
918 hints.ai_flags = AI_ADDRCONFIG;
919#if defined(AI_CANONNAME)
920 hints.ai_flags |= AI_CANONNAME;
921#endif
922 hints.ai_family = AF_UNSPEC;
923 hints.ai_socktype = SOCK_STREAM;
924 sl_snprintf(sport, sizeof(sport), "%d", port);
925
926 res = getaddrinfo (address, sport, &hints, &ai);
927 if (res != 0)
928 {
929 fail = (-1);
930 status = errno;
931 sl_strlcpy(ecall, _("getaddrinfo"), SH_MINIBUF);
932 *errnum = status;
933 sl_strlcpy(errmsg, gai_strerror (res), errsiz);
934 sl_strlcat(errmsg, _(", address "), errsiz);
935 sl_strlcat(errmsg, address, errsiz);
936 }
937
938 if (fail != (-1) && (DoReverseLookup == S_TRUE) && !sh_ipvx_is_numeric(address))
939 {
940 struct addrinfo *p = ai;
941 int success = 0;
942 char hostname[SH_BUFSIZE];
943 const char * canonical;
944
945
946#if defined(AI_CANONNAME)
947 if (ai->ai_canonname && strlen(ai->ai_canonname) > 0)
948 {
949 canonical = ai->ai_canonname;
950 if (tools_debug)
951 fprintf(stderr, _("-04- <%s> <%s> ai->ai_canonname\n"),
952 address, canonical);
953 }
954 else
955 {
956 canonical = address;
957 if (tools_debug)
958 fprintf(stderr, _("-04- <%s> <%s> defined ai_canonname\n"),
959 address, canonical);
960 }
961#else
962 canonical = address;
963 if (tools_debug)
964 fprintf(stderr, _("-04- <%s> <%s> not defined ai_canonname\n"),
965 address, canonical);
966#endif
967
968 while (p != NULL)
969 {
970 int e = getnameinfo (p->ai_addr, p->ai_addrlen,
971 hostname, sizeof(hostname),
972 NULL, 0, NI_NAMEREQD);
973
974 if (e == 0)
975 {
976 if (tools_debug)
977 {
978 fprintf(stderr, _("-05- <%s> <%s> <%s>\n"),
979 (p->ai_family == AF_INET) ? _("AF_INET") : _("AF_INET6"),
980 sh_ipvx_print_sockaddr (p->ai_addr, p->ai_family),
981 hostname);
982 }
983
984 if (sl_strcasecmp(hostname, canonical) == 0)
985 {
986 if (tools_debug)
987 fprintf(stderr, _("-06- <%s> <%s> match\n"),
988 hostname, canonical);
989 success = 1;
990 break;
991 }
992
993 }
994
995 p = p->ai_next;
996 }
997
998 if (success == 0)
999 {
1000 sl_strlcpy(ecall, _("strcmp"), SH_MINIBUF);
1001 sl_strlcpy(errmsg, _("Reverse lookup failed: "),
1002 errsiz);
1003 sl_strlcat(errmsg, address, errsiz);
1004 fail = -1;
1005 freeaddrinfo (ai);
1006 }
1007 }
1008
1009 if (fail != (-1))
1010 {
1011 struct addrinfo *p = ai;
1012
1013 while (p != NULL)
1014 {
1015 if ( (SOCK_STREAM == p->ai_socktype) &&
1016 ((p->ai_family == AF_INET) || (p->ai_family == AF_INET6)) )
1017 {
1018
1019 fd = socket(p->ai_family, SOCK_STREAM, 0);
1020
1021 if (fd != (-1))
1022 {
1023 if (retry_connect(FIL__, __LINE__, fd,
1024 p->ai_addr, p->ai_addrlen) >= 0)
1025 {
1026 /* put it into the cache
1027 */
1028 check_cache = SH_ALLOC(sizeof(sin_cache));
1029 check_cache->address = SH_ALLOC(sl_strlen(address) + 1);
1030 sl_strlcpy (check_cache->address, address, sl_strlen(address) + 1);
1031
1032 sh_ipvx_save(&(check_cache->saddr), p->ai_family, p->ai_addr);
1033
1034 ++cached_addr;
1035
1036 if (conn_cache)
1037 {
1038 if (conn_cache->next)
1039 check_cache->next = conn_cache->next;
1040 else
1041 check_cache->next = NULL;
1042 conn_cache->next = check_cache;
1043 }
1044 else
1045 {
1046 check_cache->next = NULL;
1047 conn_cache = check_cache;
1048 }
1049
1050 freeaddrinfo (ai);
1051 goto end;
1052 }
1053 status = errno;
1054 sl_close_fd(FIL__, __LINE__, fd);
1055 }
1056 else
1057 {
1058 status = errno;
1059 }
1060 }
1061 p = p->ai_next;
1062 }
1063 fail = (-1);
1064 freeaddrinfo (ai);
1065
1066 sl_strlcpy(ecall, _("connect"), SH_MINIBUF);
1067 *errnum = status;
1068 sl_strlcpy(errmsg, sh_error_message (status, errbuf, sizeof(errbuf)), errsiz);
1069 sl_strlcat(errmsg, _(", address "), errsiz);
1070 sl_strlcat(errmsg, address, errsiz);
1071 }
1072 }
1073
1074 end:
1075 retval = (fail < 0) ? (-1) : fd;
1076 SL_RETURN(retval, _("connect_port"));
1077
1078}
1079#endif
1080
1081int connect_port_2 (char * address1, char * address2, int port,
1082 char * ecall, int * errnum, char * errmsg, int errsiz)
1083{
1084 int retval = (-1);
1085
1086 SL_ENTER(_("connect_port_2"));
1087
1088 errmsg[0] = '\0';
1089 *errnum = 0;
1090
1091 if (address1 != NULL && address1[0] != '\0')
1092 retval = connect_port (address1, port,
1093 ecall, errnum,
1094 errmsg, errsiz);
1095
1096 if (retval < 0 && address2 != NULL && address2[0] != '\0')
1097 {
1098 /* can't use sh_error_handle here, as this would cause an infinite
1099 * loop if called from sh_unix_time
1100 */
1101 TPT(( 0, FIL__, __LINE__, _("msg=<Using alternative server %s.>\n"),
1102 address2));
1103 retval = connect_port (address2, port,
1104 ecall, errnum,
1105 errmsg, errsiz);
1106 }
1107
1108 if ((retval < 0) &&
1109 (address1 == NULL || address1[0] == '\0') &&
1110 (address1 == NULL || address1[0] == '\0'))
1111 {
1112 sl_strlcpy(ecall, _("connect_port_2"), SH_MINIBUF);
1113 sl_strlcpy(errmsg, _("No server address known"), errsiz);
1114 }
1115 SL_RETURN(retval, _("connect_port_2"));
1116 /* return retval; */
1117}
1118
1119#if defined(HAVE_NTIME) || defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
1120static
1121int sh_write_select(int type, int sockfd,
1122 char *buf, int nbytes,
1123 int * w_error, int timeout)
1124{
1125 int countbytes, count;
1126 fd_set fds;
1127 struct timeval tv;
1128 int select_now;
1129 int num_sel;
1130
1131 char errbuf[SH_ERRBUF_SIZE];
1132
1133 SL_ENTER(_("sh_write_select"));
1134
1135 FD_ZERO(&fds);
1136 FD_SET(sockfd, &fds);
1137
1138 countbytes = 0;
1139 tv.tv_sec = 1;
1140 tv.tv_usec = 0;
1141 select_now = 0;
1142
1143 *w_error = 0;
1144
1145 while ( countbytes < nbytes ) {
1146
1147 FD_ZERO(&fds);
1148 FD_SET(sockfd, &fds);
1149
1150 if (type == SH_DO_WRITE)
1151 {
1152 if ( (num_sel = select (sockfd+1, NULL, &fds, NULL, &tv)) == -1)
1153 {
1154 if (sig_raised == 1)
1155 {
1156 sig_raised = 2;
1157 continue;
1158 }
1159 if ( errno == EINTR || errno == EINPROGRESS ) /* try again */
1160 continue;
1161 *w_error = errno;
1162
1163 sh_error_message(*w_error, errbuf, sizeof(errbuf));
1164 sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, errno, MSG_E_SUBGEN,
1165 errbuf,
1166 _("sh_write_select (ws)") );
1167 TPT(( 0, FIL__, __LINE__, _("msg=<select: %s>\n"), errbuf ));
1168 SL_RETURN( countbytes, _("sh_write_select"));
1169 }
1170 }
1171 else
1172 {
1173 if ( (num_sel = select (sockfd+1, &fds, NULL, NULL, &tv)) == -1)
1174 {
1175 if (sig_raised == 1)
1176 {
1177 sig_raised = 2;
1178 continue;
1179 }
1180 if ( errno == EINTR || errno == EINPROGRESS ) /* try again */
1181 continue;
1182 *w_error = errno;
1183
1184 sh_error_message(*w_error, errbuf, sizeof(errbuf));
1185 sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, errno, MSG_E_SUBGEN,
1186 errbuf,
1187 _("sh_write_select (rs)") );
1188 TPT(( 0, FIL__, __LINE__, _("msg=<select: %s>\n"), errbuf ));
1189 SL_RETURN( countbytes, _("sh_write_select"));
1190 }
1191 }
1192
1193 /* on Linux, timeout is modified to reflect the amount of
1194 * time not slept
1195 */
1196 tv.tv_sec = 1;
1197 tv.tv_usec = 0;
1198
1199
1200 /* let's not hang on forever
1201 */
1202 if (num_sel == 0)
1203 {
1204 ++select_now; /* timeout */
1205 if ( select_now > timeout ) /* 5 minutes */
1206 {
1207#ifdef ETIMEDOUT
1208 *w_error = ETIMEDOUT;
1209#else
1210 *w_error = 0;
1211#endif
1212
1213 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout>\n")));
1214 SL_RETURN( countbytes, _("sh_write_select"));
1215 }
1216 }
1217
1218 if ( FD_ISSET (sockfd, &fds) )
1219 {
1220 if (type == SH_DO_WRITE)
1221 count = write (sockfd, buf, nbytes-countbytes);
1222 else
1223 count = read (sockfd, buf, nbytes-countbytes);
1224
1225 if (count > 0)
1226 {
1227 countbytes += count;
1228 buf += count; /* move buffer pointer forward */
1229 if (countbytes < nbytes) FD_SET( sockfd, &fds );
1230 }
1231 else if (count < 0 && errno == EINTR)
1232 {
1233 FD_SET( sockfd, &fds );
1234 }
1235 else if (count < 0)
1236 {
1237 *w_error = errno;
1238
1239 sh_error_message(*w_error, errbuf, sizeof(errbuf));
1240 sh_error_handle (SH_ERR_INFO, FIL__, __LINE__, errno, MSG_E_SUBGEN,
1241 errbuf,
1242 (type == SH_DO_WRITE) ?
1243 _("sh_write_select (w)") : _("sh_write_select (r)"));
1244 TPT(( 0, FIL__, __LINE__, _("msg=<count < 0>\n")));
1245 SL_RETURN( countbytes, _("sh_write_select"));
1246 }
1247 else /* count == 0 */
1248 {
1249 *w_error = errno;
1250
1251 TPT(( 0, FIL__, __LINE__, _("msg=<count == 0>\n")));
1252 SL_RETURN( countbytes, _("sh_write_select"));
1253 }
1254 }
1255 }
1256
1257 *w_error = 0;
1258
1259 TPT(( 0, FIL__, __LINE__, _("msg=<count = %d>\n"), countbytes));
1260 SL_RETURN( countbytes, _("sh_write_select"));
1261}
1262#endif
1263
1264#if defined (SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
1265unsigned long write_port (int sockfd, char *buf, unsigned long nbytes,
1266 int * w_error, int timeout)
1267{
1268 unsigned long bytes;
1269
1270 SL_ENTER(_("write_port"));
1271
1272 bytes = sh_write_select(SH_DO_WRITE, sockfd, buf, nbytes, w_error, timeout);
1273 if (*w_error != 0)
1274 {
1275 char errbuf[SH_ERRBUF_SIZE];
1276 sh_error_handle((-1), FIL__, __LINE__, *w_error, MSG_TCP_NETRP,
1277 sh_error_message (*w_error, errbuf, sizeof(errbuf)),
1278 (long) sockfd, _("write_port"));
1279 }
1280 SL_RETURN( bytes, _("write_port"));
1281}
1282#endif
1283
1284#if defined(HAVE_NTIME) || defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
1285
1286unsigned long read_port (int sockfd, char *buf, unsigned long nbytes,
1287 int * w_error, int timeout)
1288{
1289 unsigned long bytes;
1290
1291 SL_ENTER(_("read_port"));
1292
1293 bytes = sh_write_select(SH_DO_READ, sockfd, buf, nbytes, w_error, timeout);
1294 if (*w_error != 0)
1295 {
1296 char errbuf[SH_ERRBUF_SIZE];
1297 sh_error_handle((-1), FIL__, __LINE__, *w_error, MSG_TCP_NETRP,
1298 sh_error_message (*w_error, errbuf, sizeof(errbuf)),
1299 (long) sockfd, _("read_port"));
1300 }
1301 SL_RETURN( bytes, _("read_port"));
1302}
1303#endif
1304
1305#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
1306
1307int check_request_nerr (char * have, char * need)
1308{
1309 SL_ENTER(_("check_request_nerr"));
1310 ASSERT_RET((have != NULL && need != NULL),
1311 _("have != NULL && need != NULL"), (-1))
1312
1313 if ( (have[0] == need[0]) && (have[1] == need[1]) &&
1314 (have[2] == need[2]) && (have[3] == need[3]))
1315 SL_RETURN(0, _("check_request_nerr"));
1316 SL_RETURN((-1), _("check_request_nerr"));
1317}
1318#endif
1319
1320#if defined (SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
1321
1322int check_request (char * have, char * need)
1323{
1324 char first[21], second[5];
1325 int i;
1326
1327 SL_ENTER(_("check_request"));
1328 i = check_request_nerr (have, need);
1329
1330 if (i == 0)
1331 SL_RETURN(0, _("check_request"));
1332
1333 for (i = 0; i < 4; ++i)
1334 {
1335 second[i] = need[i];
1336 sprintf(&first[i*4], _("%c%03o"), /* known to fit */
1337 '\\', (unsigned char) have[i]);
1338 }
1339
1340 first[20] = '\0'; second[4] = '\0';
1341
1342 sh_error_handle((-1), FIL__, __LINE__, EINVAL, MSG_E_NETST,
1343 second, first);
1344 SL_RETURN((-1), _("check_request"));
1345}
1346#endif
1347
1348#if defined (SH_WITH_SERVER)
1349
1350int check_request_s (char * have, char * need, char * clt)
1351{
1352 char first[21], second[5];
1353 int i;
1354
1355 SL_ENTER(_("check_request_s"));
1356 i = check_request_nerr (have, need);
1357
1358 if (i == 0)
1359 SL_RETURN( (0), _("check_request_s"));
1360
1361 for (i = 0; i < 4; ++i)
1362 {
1363 second[i] = need[i];
1364 sprintf(&first[i*4], _("%c%03o"), /* known to fit */
1365 '\\', (unsigned char) have[i]);
1366 }
1367 first[20] = '\0'; second[4] = '\0';
1368 sh_error_handle((-1), FIL__, __LINE__, EINVAL, MSG_E_NETST1,
1369 second, first, clt);
1370 SL_RETURN( (-1), _("check_request_s"));
1371}
1372#endif
1373
1374#if defined (SH_WITH_CLIENT) || defined (SH_WITH_SERVER)
1375
1376#if defined (SH_WITH_CLIENT)
1377
1378static int probe_done = S_FALSE;
1379static unsigned char probe_flag = '\0';
1380
1381void sh_tools_probe_reset()
1382{
1383 probe_done = S_FALSE;
1384 probe_flag = '\0';
1385 return;
1386}
1387
1388static int probe_ok(int flag)
1389{
1390 (void) flag;
1391 if ((probe_flag & SH_PROTO_IVA) != 0)
1392 return S_TRUE;
1393 return S_FALSE;
1394}
1395
1396static unsigned char probe_header_set(unsigned char protocol)
1397{
1398 if (probe_done || (protocol & SH_PROTO_SRP) == 0)
1399 return 0;
1400
1401 return (char) SH_PROTO_IVA;
1402}
1403
1404static void probe_header_get(unsigned char protocol)
1405{
1406 if (probe_done || (protocol & SH_PROTO_SRP) == 0)
1407 return;
1408
1409 /* If the server doesn't know about it,
1410 * it will simply mirror it back. */
1411
1412 if ((protocol & SH_PROTO_IVA) != 0)
1413 {
1414 /* probe was mirrored */;
1415 }
1416 else
1417 {
1418 /* probe was UNset */
1419 probe_flag |= SH_PROTO_IVA;
1420 }
1421 probe_done = S_TRUE;
1422 return;
1423}
1424
1425#else
1426static unsigned char probe_header_set(unsigned char protocol) {
1427 (void) protocol; return 0; }
1428static void probe_header_get(unsigned char protocol) {
1429 (void) protocol; return; }
1430void sh_tools_probe_reset() { return; }
1431
1432unsigned char sh_tools_probe_store(unsigned char protocol, int * probe_flag)
1433{
1434 if ((protocol & SH_PROTO_SRP) == 0)
1435 return protocol;
1436
1437 if ((protocol & SH_PROTO_IVA) != 0)
1438 {
1439 /* probe received */
1440 *probe_flag |= SH_PROTO_IVA;
1441 protocol &= ~SH_PROTO_IVA;
1442 }
1443 return protocol;
1444}
1445
1446static int probe_ok(int flag)
1447{
1448 if ((flag & SH_PROTO_IVA) != 0)
1449 return S_TRUE;
1450 return S_FALSE;
1451}
1452#endif
1453
1454
1455void get_header (unsigned char * head, unsigned long * bytes, char * u)
1456{
1457 SL_ENTER(_("get_header"));
1458
1459 probe_header_get(head[0]);
1460
1461 *bytes =
1462 (256 * (unsigned int)head[1] + (unsigned int)head[2]);
1463
1464 if (u != NULL)
1465 {
1466 u[0] = head[3];
1467 u[1] = head[4];
1468 u[2] = head[5];
1469 u[3] = head[6];
1470 u[4] = '\0';
1471 }
1472
1473 SL_RET0(_("get_header"));
1474}
1475#endif
1476
1477#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
1478
1479#ifdef SH_ENCRYPT
1480#define TRANS_BYTES 65120
1481#else
1482#define TRANS_BYTES 65280
1483#endif
1484
1485void put_header (unsigned char * head, const int protocol,
1486 unsigned long * length, char * u)
1487{
1488 unsigned char probe = probe_header_set(protocol);
1489
1490 /* static long transfer_limit = (8 * SH_BUFSIZE); V0.8 */
1491 static unsigned long transfer_limit = TRANS_BYTES + 6 + KEY_LEN;
1492
1493 SL_ENTER(_("put_header"));
1494
1495 head[0] = protocol|probe;
1496
1497 ASSERT((*length < transfer_limit), _("*length < transfer_limit"))
1498
1499 if (*length > transfer_limit)
1500 *length = transfer_limit;
1501
1502 head[1] = (unsigned int)(*length/256);
1503 head[2] = (unsigned int)(*length-256 * head[1]);
1504 if (u == NULL)
1505 {
1506 head[3] = 0x01;
1507 head[4] = 0x01;
1508 head[5] = 0x01;
1509 head[6] = 0x01;
1510 }
1511 else
1512 {
1513 head[3] = u[0];
1514 head[4] = u[1];
1515 head[5] = u[2];
1516 head[6] = u[3];
1517 }
1518
1519 SL_RET0(_("put_header"));
1520}
1521
1522/* ------------------------------------------
1523 *
1524 * version 2 client/server protocol
1525 *
1526 * ------------------------------------------
1527 *
1528 * header : flag size[2]
1529 *
1530 * payload: random_pad[8] protocol[4] size[4] payload[payload_size] padding
1531 *
1532 * full_size <= 8192; payload_size <= 8176 (511*16); msg_size <= 8128 (508*16)
1533 * (msg_size = payload_size - key_len = payload_size - 48)
1534 */
1535
1536/*
1537 * only SH_V2_FULLSIZE is used, and only once
1538 */
1539
1540#define SH_V2_FULLSIZE 1024
1541
1542#ifdef SH_ENCRYPT
1543#include "rijndael-api-fst.h"
1544#endif
1545
1546void sh_tools_show_header (unsigned char * head, char sign)
1547{
1548#define SH_IS_ASCII(c) (((c) & ~0x7f) == 0)
1549
1550
1551 int msg_size = (256 * (unsigned int)head[1] + (unsigned int)head[2]);
1552 char code[32];
1553 char * p = &code[0];
1554
1555 memset (code, ' ', 32); /* space */
1556
1557 if ((head[0] & SH_PROTO_SRP) != 0) { p[0]='S';p[1]='R';p[2]='P';}
1558 p += 4;
1559 if ((head[0] & SH_PROTO_MSG) != 0) { p[0]='M';p[1]='S';p[2]='G';}
1560 p += 4;
1561 if ((head[0] & SH_PROTO_BIG) != 0) { p[0]='B';p[1]='I';p[2]='G';}
1562 p += 4;
1563 if ((head[0] & SH_PROTO_END) != 0) { p[0]='E';p[1]='N';p[2]='D';}
1564 p += 4;
1565 if ((head[0] & SH_PROTO_ENC) != 0) { p[0]='E';p[1]='N';p[2]='C';}
1566 p += 4;
1567 if ((head[0] & SH_PROTO_EN2) != 0) { p[0]='E';p[1]='N';p[2]='2';}
1568 code[23] = '\0';
1569
1570 if (SH_IS_ASCII(head[3]) && isalpha(head[3]) &&
1571 SH_IS_ASCII(head[4]) && isalpha(head[4]) &&
1572 SH_IS_ASCII(head[5]) && isalpha(head[5]) &&
1573 SH_IS_ASCII(head[6]) && isalpha(head[6])) {
1574 fprintf(stderr, _("%c %3o %s %5d %c %c %c %c\n"), sign,
1575 head[0], code, msg_size, head[3], head[4], head[5], head[6]);
1576 } else {
1577 fprintf(stderr, _("%c %3o %s %5d %2X %2X %2X %2X\n"), sign,
1578 head[0], code, msg_size, head[3], head[4], head[5], head[6]);
1579 }
1580 return;
1581}
1582
1583#ifdef SH_ENCRYPT
1584
1585/*
1586 * #define DEBUG_EN2
1587 *
1588 * ingest version 1 7-byte header and payload, return version2 header/payload
1589 * last 4 bytes of outgoing header are set to dummy value
1590 */
1591char * sh_tools_makePack (unsigned char * header, int flag,
1592 char * payload, unsigned long payload_size,
1593 keyInstance * keyInstE)
1594{
1595 BYTE inBlock[B_SIZ];
1596 BYTE outBlock[B_SIZ];
1597 char ivBlock[B_SIZ];
1598
1599 UINT32 rpad[3];
1600 unsigned char head[16];
1601 double epad;
1602 unsigned long i_epad = 0;
1603 unsigned long i_blk = payload_size / 16;
1604 unsigned long i_blkmax = SH_V2_FULLSIZE / 16;
1605 unsigned long pads = 0;
1606 size_t full_size;
1607 char * full_ret;
1608
1609 unsigned char * p;
1610 int j;
1611 cipherInstance cipherInst;
1612 int err_num;
1613 int blkfac;
1614 int oflow = 0;
1615 char expbuf[SH_ERRBUF_SIZE];
1616
1617 /*
1618 SL_REQUIRE (i_blk*16 == payload_size, _("payload_size % 16 != 0"));
1619 */
1620 if ((i_blk * 16) != payload_size) ++i_blk;
1621
1622 /* random_pad
1623 */
1624 rpad[1] = taus_get ();
1625 memcpy (head, &rpad[1], 4);
1626 rpad[0] = taus_get ();
1627 memcpy (&head[4], &rpad[0], 4);
1628 rpad[2] = taus_get ();
1629 memcpy (&head[8], &rpad[2], 4);
1630
1631 /* size (payload)
1632 */
1633 head[12] = header[1];
1634 head[13] = header[2];
1635 head[14] = '\0';
1636 head[15] = '\0';
1637
1638 if (i_blk < i_blkmax)
1639 {
1640 pads = i_blkmax - i_blk;
1641 epad = taus_get_double (&rpad);
1642 i_epad = (unsigned long) (pads * epad);
1643 }
1644
1645 full_size = 16; /* head */
1646 if (sl_ok_muls(i_blk, 16) && sl_ok_adds(full_size, (i_blk*16)))
1647 full_size = full_size + (i_blk*16); /* payload */
1648 else
1649 oflow = 1;
1650 if (sl_ok_adds(full_size, (i_epad*16)))
1651 full_size = full_size + (i_epad*16); /* pad */
1652 else
1653 i_epad = 0;
1654
1655 if (oflow)
1656 {
1657 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
1658 _("integer overflow"),
1659 _("sh_tools_makePack"));
1660 }
1661
1662 full_ret = SH_ALLOC(full_size);
1663
1664 memcpy(full_ret, head, 16);
1665 if (payload != NULL && !oflow)
1666 memcpy(&full_ret[16], payload, payload_size);
1667
1668 if ((i_blk*16) > payload_size && !oflow)
1669 {
1670 memset(&full_ret[16+payload_size], 0, (i_blk*16) - payload_size);
1671 payload_size = i_blk * 16;
1672 }
1673 memset(&full_ret[16+payload_size], 0, i_epad*16);
1674
1675 /* rewrite header
1676 */
1677 header[1] = (unsigned int)(full_size/256);
1678 header[2] = (unsigned int)(full_size - (256 * header[1]));
1679
1680 p = (unsigned char *) full_ret;
1681 blkfac = full_size / B_SIZ;
1682
1683 err_num = rijndael_cipherInit (&cipherInst, MODE_CBC, NULL);
1684
1685 if (err_num < 0) {
1686 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
1687 errorExplain(err_num, expbuf, sizeof(expbuf)),
1688 _("sh_tools_makePack: rijndael_cipherInit")); }
1689
1690 if (probe_ok(flag)) {
1691 memcpy(inBlock, p, B_SIZ);
1692 err_num = rijndael_blockEncrypt(&cipherInst, keyInstE,
1693 inBlock, 128, outBlock);
1694 if (err_num >= 0) {
1695 memcpy(p, outBlock, B_SIZ); p += B_SIZ;
1696 memcpy(ivBlock, outBlock, sizeof(ivBlock));
1697 err_num = rijndael_cipherInit (&cipherInst, MODE_CBC, ivBlock);
1698 if (err_num >= 0) {
1699 err_num = rijndael_blockEncrypt(&cipherInst, keyInstE,
1700 p, 128*(blkfac-1), p);
1701 }
1702 }
1703 }
1704
1705 else {
1706 for (j = 0; j < blkfac; ++j) {
1707 memcpy(inBlock, p, B_SIZ);
1708 err_num = rijndael_blockEncrypt(&cipherInst, keyInstE,
1709 inBlock, 128, outBlock);
1710
1711 if (err_num < 0) {
1712 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
1713 errorExplain(err_num, expbuf, sizeof(expbuf)),
1714 _("sh_tools_makePack: rijndael_blockEncrypt"));
1715 break; }
1716
1717 memcpy(p, outBlock, B_SIZ);
1718 p += B_SIZ;
1719 }
1720 }
1721
1722 return full_ret;
1723}
1724
1725/* write a 7-byte header and return payload as expected by version 1
1726 * last 4 bytes of incoming header are dummy
1727 */
1728char * sh_tools_revertPack (unsigned char * header, int flag, char * message,
1729 keyInstance * keyInstD,
1730 unsigned long message_size)
1731{
1732 BYTE inBlock[B_SIZ];
1733 BYTE outBlock[B_SIZ];
1734 char ivBlock[B_SIZ];
1735 unsigned long msg_size;
1736 char * msg_ret;
1737
1738 unsigned char * p;
1739 int j;
1740 cipherInstance cipherInst;
1741 int err_num;
1742 int blkfac;
1743 char expbuf[SH_ERRBUF_SIZE];
1744
1745 msg_size = (256 * (unsigned int)header[1] + (unsigned int)header[2]);
1746 if (msg_size > message_size)
1747 msg_size = message_size;
1748
1749 p = (unsigned char *) message; blkfac = msg_size / 16;
1750
1751 err_num = rijndael_cipherInit (&cipherInst, MODE_CBC, NULL);
1752
1753 if (err_num < 0)
1754 {
1755 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
1756 errorExplain(err_num, expbuf, sizeof(expbuf)),
1757 _("sh_tools_revertPack: rijndael_cipherInit"));
1758 }
1759
1760 if (probe_ok(flag)) {
1761 memcpy(inBlock, p, B_SIZ);
1762 err_num = rijndael_blockDecrypt(&cipherInst, keyInstD,
1763 inBlock, 128, outBlock);
1764 if (err_num >= 0) {
1765 memcpy(p, outBlock, B_SIZ); p += B_SIZ;
1766 memcpy(ivBlock, inBlock, sizeof(ivBlock));
1767 err_num = rijndael_cipherInit (&cipherInst, MODE_CBC, ivBlock);
1768 if (err_num >= 0) {
1769 err_num = rijndael_blockDecrypt(&cipherInst, keyInstD,
1770 p, 128*(blkfac-1), p);
1771 }
1772 }
1773 }
1774
1775 else {
1776 for (j = 0; j < blkfac; ++j) {
1777 memcpy(inBlock, p, B_SIZ);
1778 err_num = rijndael_blockDecrypt(&cipherInst, keyInstD,
1779 inBlock, 128, outBlock);
1780
1781 if (err_num < 0) {
1782 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
1783 errorExplain(err_num, expbuf, sizeof(expbuf)),
1784 _("sh_tools_revertPack: rijndael_blockDecrypt"));
1785 break; }
1786
1787 memcpy(p, outBlock, B_SIZ);
1788 p += B_SIZ;
1789 }
1790 }
1791
1792 /* rewrite size in header
1793 */
1794 header[1] = message[12];
1795 header[2] = message[13];
1796 msg_size = (256 * (unsigned int)header[1] + (unsigned int)header[2]);
1797
1798 if (msg_size > (message_size-16))
1799 {
1800 msg_size = message_size-16;
1801 header[1] = (unsigned int)(msg_size/256);
1802 header[2] = (unsigned int)(msg_size - (256 * header[1]));
1803 }
1804
1805 /* payload
1806 */
1807 msg_ret = SH_ALLOC(msg_size+1);
1808 if (msg_size > 0)
1809 memcpy(msg_ret, &message[16], msg_size);
1810 msg_ret[msg_size] = '\0';
1811
1812 SH_FREE(message);
1813 return msg_ret;
1814}
1815#endif /* #ifdef SH_ENCRYPT */
1816
1817int sh_tools_hash_add(char * key, char * buf, int buflen)
1818{
1819 char * theSig;
1820 char sigbuf[KEYBUF_SIZE];
1821
1822 SL_ENTER(_("sh_tools_hash_add"));
1823
1824 theSig = sh_util_siggen (key, buf, buflen, sigbuf, sizeof(sigbuf));
1825 sl_strlcat(buf, theSig, buflen + KEY_LEN + 1);
1826
1827 SL_RETURN((0), _("sh_tools_hash_add"));
1828}
1829
1830
1831/* return 0 (== FALSE) if no match, else 1 (== TRUE)
1832 */
1833int sh_tools_hash_vfy(char * key, char * buf, int buflen)
1834{
1835 char hash[KEY_LEN+1];
1836 register int i;
1837 char * theSig;
1838 char sigbuf[KEYBUF_SIZE];
1839
1840 SL_ENTER(_("sh_tools_hash_vfy"));
1841
1842 theSig = sh_util_siggen (key, buf, buflen, sigbuf, sizeof(sigbuf));
1843 sl_strlcpy(hash, theSig, KEY_LEN+1);
1844
1845 for (i = 0; i < KEY_LEN; ++i)
1846 {
1847 if (buf[buflen + i] != hash[i])
1848 SL_RETURN((0), _("sh_tools_hash_vfy"));
1849 }
1850
1851 SL_RETURN((1), _("sh_tools_hash_vfy"));
1852}
1853
1854#endif /* defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER) */
1855
1856
1857/* ------------------------------------------ */
1858
1859#if defined (SH_WITH_SERVER)
1860
1861/* add a checksum to a buffer; put checksum in front
1862 */
1863char * hash_me (char * key, char * buf, int buflen)
1864{
1865 char hash[KEY_LEN+1];
1866 char * temp = NULL;
1867 register int i;
1868 int total = 0;
1869 char * theSig;
1870 char sigbuf[KEYBUF_SIZE];
1871
1872
1873 SL_ENTER(_("hash_me"));
1874
1875#ifdef DEBUG_EN2
1876 fprintf(stderr, "hash_me <%s> <%d>\n",
1877 (key == NULL) ? "NULL" : key, buflen);
1878#endif
1879 /* key = H(NSRV,NCLT,SK)
1880 */
1881 ASSERT_RET((key != NULL), _("key != NULL"), (NULL));
1882 ASSERT_RET((buflen >= 0), _("buflen >= 0"), (NULL));
1883
1884 theSig = sh_util_siggen (key, buf, buflen, sigbuf, sizeof(sigbuf));
1885 sl_strlcpy(hash, theSig, KEY_LEN+1);
1886
1887 if (sl_ok_adds(buflen, KEY_LEN))
1888 {
1889 total = KEY_LEN + buflen;
1890 temp = SH_ALLOC (total);
1891
1892 for (i = 0; i < KEY_LEN; ++i)
1893 temp[i] = hash[i];
1894
1895 for (i = 0; i < buflen; ++i)
1896 temp[i+KEY_LEN] = buf[i];
1897 }
1898 else
1899 {
1900 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
1901 _("integer overflow"),
1902 _("hash_me"));
1903 temp = sh_util_strdup(buf);
1904 }
1905 SL_RETURN(temp, _("hash_me"));
1906}
1907#endif
1908
1909#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
1910
1911/* verify the checksum of a buffer; checksum comes first
1912 */
1913int hash_check(char * key,
1914 char * buf, int buflen)
1915{
1916 char hash[KEY_LEN+1];
1917 register int i;
1918 char * theSig;
1919 char sigbuf[KEYBUF_SIZE];
1920
1921 SL_ENTER(_("hash_check"));
1922
1923#ifdef DEBUG_EN2
1924 fprintf(stderr, "hash_check <%s> <%d>\n",
1925 (key == NULL) ? "NULL" : key, buflen);
1926#endif
1927 theSig = sh_util_siggen (key, &buf[KEY_LEN], buflen-KEY_LEN,
1928 sigbuf, sizeof(sigbuf));
1929 sl_strlcpy(hash, theSig, KEY_LEN+1);
1930
1931 for (i = 0; i < KEY_LEN; ++i)
1932 {
1933 if (buf[i] != hash[i])
1934 SL_RETURN((-1), _("hash_check"));
1935 }
1936 SL_RETURN((0), _("hash_check"));
1937}
1938
1939#endif
1940
1941#if defined (SH_WITH_SERVER)
1942
1943char * get_client_conf_file (const char * peer, unsigned long * length)
1944{
1945 char * ret;
1946 int status;
1947 struct stat buf;
1948 char * base;
1949
1950 SL_ENTER(_("get_client_conf_file"));
1951
1952 base = sh_util_strdup(DEFAULT_DATAROOT);
1953 ret = sh_util_strconcat(base, _("/rc."), peer, NULL);
1954 if (!ret)
1955 { SH_FREE(base); *length = 0; SL_RETURN(NULL, _("get_client_conf_file")); }
1956
1957 status = retry_stat (FIL__, __LINE__, ret, &buf);
1958 if (status == 0)
1959 goto lab_end;
1960 else
1961 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_ACCESS,
1962 (long) sh.effective.uid, ret);
1963
1964 SH_FREE(ret); ret = sh_util_strconcat(base, _("/rc"), NULL);
1965 if (!ret)
1966 { SH_FREE(base); *length = 0; SL_RETURN(NULL, _("get_client_conf_file")); }
1967
1968 status = retry_stat (FIL__, __LINE__, ret, &buf);
1969 if (status == 0)
1970 goto lab_end;
1971 else
1972 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_ACCESS,
1973 (long) sh.effective.uid, ret);
1974
1975 SH_FREE(base); SH_FREE(ret); *length=0;
1976 SL_RETURN(NULL, _("get_client_conf_file"));
1977
1978 lab_end:
1979 if (buf.st_size > 0x7fffffff)
1980 {
1981 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN,
1982 _("File too large"), _("get_client_conf_file"));
1983 SH_FREE(base); SH_FREE(ret); *length = 0;
1984 SL_RETURN(NULL, _("get_client_conf_file"));
1985 }
1986
1987 SH_FREE(base); *length = (unsigned long) buf.st_size;
1988 SL_RETURN(ret, _("get_client_conf_file"));
1989}
1990
1991char * get_client_data_file (const char * peer, unsigned long * length)
1992{
1993 char * ret;
1994 int status;
1995 struct stat buf;
1996 char * base;
1997
1998 SL_ENTER(_("get_client_data_file"));
1999
2000 base = sh_util_strdup(DEFAULT_DATAROOT);
2001 ret = sh_util_strconcat(base, _("/file."), peer, NULL);
2002 if (!ret)
2003 { SH_FREE(base); *length = 0; SL_RETURN(NULL, _("get_client_data_file")); }
2004
2005 status = retry_stat (FIL__, __LINE__, ret, &buf);
2006 if (status == 0)
2007 goto lab1_end;
2008 else
2009 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_ACCESS,
2010 (long) sh.effective.uid, ret);
2011
2012 SH_FREE(ret);
2013 ret = sh_util_strconcat(base, _("/file"), NULL);
2014 if (!ret)
2015 { SH_FREE(base); *length = 0; SL_RETURN(NULL, _("get_client_data_file")); }
2016
2017 status = retry_stat (FIL__, __LINE__, ret, &buf);
2018 if (status == 0)
2019 goto lab1_end;
2020 else
2021 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_ACCESS,
2022 (long) sh.effective.uid, ret);
2023
2024 *length = 0; SH_FREE(base); SH_FREE(ret);
2025 SL_RETURN(NULL, _("get_client_data_file"));
2026
2027 lab1_end:
2028 if (buf.st_size > 0x7fffffff)
2029 {
2030 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN,
2031 _("File too large"), _("get_client_data_file"));
2032 SH_FREE(base); SH_FREE(ret); *length = 0;
2033 SL_RETURN(NULL, _("get_client_data_file"));
2034 }
2035
2036 *length = (unsigned long) buf.st_size; SH_FREE(base);
2037 SL_RETURN(ret, _("get_client_data_file"));
2038}
2039
2040char * get_client_uuid_file (const char * peer, unsigned long * length, const char * uuid)
2041{
2042 char * ret;
2043 int status;
2044 struct stat buf;
2045 char * base;
2046
2047 SL_ENTER(_("get_client_uuid_file"));
2048
2049 base = sh_util_strdup(DEFAULT_DATAROOT);
2050 ret = sh_util_strconcat(base, _("/file."), peer, ".", uuid, NULL);
2051 SH_FREE(base);
2052 if (!ret)
2053 { *length = 0; SL_RETURN(NULL, _("get_client_uuid_file")); }
2054
2055 status = retry_stat (FIL__, __LINE__, ret, &buf);
2056 if (status != 0)
2057 {
2058 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_ACCESS,
2059 (long) sh.effective.uid, ret);
2060 SH_FREE(ret); *length = 0;
2061 SL_RETURN(NULL, _("get_client_uuid_file"));
2062 }
2063 else if (buf.st_size > 0x7fffffff)
2064 {
2065 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN,
2066 _("File too large"), _("get_client_uuid_file"));
2067 SH_FREE(ret); *length = 0;
2068 SL_RETURN(NULL, _("get_client_data_file"));
2069 }
2070
2071 *length = (unsigned long) buf.st_size;
2072 SL_RETURN(ret, _("get_client_uuid_file"));
2073}
2074
2075#endif
2076
2077#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER) || defined(SH_STEALTH) || defined(WITH_SIG)
2078
2079/* --------- secure temporary file ------------ */
2080
2081SL_TICKET open_tmp ()
2082{
2083 SL_TICKET fd;
2084 UINT32 ticks;
2085 char * file;
2086 struct stat buf;
2087 int error;
2088 int status = BAD;
2089 char * my_tmp_dir;
2090 char hashbuf[KEYBUF_SIZE];
2091
2092 SL_ENTER(_("open_tmp"));
2093
2094#if defined(SH_TMPDIR)
2095 my_tmp_dir = sh_util_strdup(SH_TMPDIR);
2096#else
2097#if defined(SH_WITH_SERVER)
2098 my_tmp_dir = sh_util_strdup(DEFAULT_LOGDIR);
2099#else
2100 my_tmp_dir = sh_util_strdup(sh.effective.home);
2101#endif
2102#endif
2103
2104 if (0 != tf_trust_check (my_tmp_dir, SL_YESPRIV))
2105 {
2106 dlog(1, FIL__, __LINE__,
2107 _("The directory for temporary files: %s is untrusted, i.e. an\nuntrusted user owns or can write to some directory in the path.\n"),
2108 my_tmp_dir);
2109 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_TRUST,
2110 (long) sh.effective.uid,
2111 my_tmp_dir);
2112 SH_FREE(my_tmp_dir);
2113 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
2114 }
2115
2116 do {
2117
2118 /* create random filename in effective users home directory
2119 */
2120 ticks = taus_get ();
2121 if (my_tmp_dir[0] == '/' && my_tmp_dir[1] == '\0')
2122 file = sh_util_strconcat (my_tmp_dir,
2123 sh_tiger_hash( (char *) &ticks, TIGER_DATA, 4,
2124 hashbuf, sizeof(hashbuf)),
2125 NULL);
2126 else
2127 file = sh_util_strconcat (my_tmp_dir,
2128 "/",
2129 sh_tiger_hash( (char *) &ticks, TIGER_DATA, 4,
2130 hashbuf, sizeof(hashbuf)),
2131 NULL);
2132
2133 /* check whether it already exists (paranoia)
2134 */
2135 errno = 0;
2136 status = retry_lstat(FIL__, __LINE__, file, &buf);
2137 error = errno;
2138
2139 if ( (status < 0) && (error == ENOENT) ) /* file does not exist */
2140 status = GOOD;
2141 else if (status < 0) /* unexpected error condition */
2142 {
2143 SH_FREE (file);
2144 SH_FREE(my_tmp_dir);
2145 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
2146 _("Error (lstat) while opening temporary file"), _("open_tmp"));
2147 TPT(( 0, FIL__, __LINE__, _("msg=<Unexpected error %d>\n"), error));
2148 SL_RETURN((-1), _("open_tmp"));
2149 }
2150 else /* file exists */
2151 {
2152 status = BAD;
2153 TPT(( 0, FIL__, __LINE__, _("msg=<Temporary file exists already>\n")));
2154 }
2155
2156 if (status == GOOD)
2157 {
2158 if (0 == tf_trust_check (file, SL_YESPRIV))
2159 status = GOOD;
2160 else
2161 {
2162 status = BAD;
2163 TPT(( 0, FIL__, __LINE__, _("msg=<Temporary file untrusted>\n")));
2164 }
2165 }
2166
2167 if (status == BAD)
2168 SH_FREE (file);
2169
2170 } while (status == BAD);
2171
2172 fd = sl_open_safe_rdwr (FIL__, __LINE__, file, SL_YESPRIV);
2173 if (SL_ISERROR(fd))
2174 {
2175 sh_error_handle((-1), FIL__, __LINE__, fd, MSG_E_SUBGEN,
2176 _("Error opening temporary file"), _("open_tmp"));
2177 TPT(( 0, FIL__, __LINE__, _("msg=<Error %d temporary file %s>\n"),
2178 fd, file));
2179 }
2180
2181 SH_FREE (file);
2182 SH_FREE(my_tmp_dir);
2183
2184 if (!SL_ISERROR(fd)) {
2185 sl_unlink(fd);
2186 }
2187
2188 if (!SL_ISERROR(fd))
2189 SL_RETURN((fd), _("open_tmp"));
2190 else
2191 SL_RETURN((-1), _("open_tmp"));
2192}
2193
2194
2195int close_tmp (SL_TICKET fd)
2196{
2197 SL_ENTER(_("close_tmp"));
2198
2199 if (SL_ISERROR(sl_close (fd)))
2200 SL_RETURN((-1), _("close_tmp"));
2201 SL_RETURN((0), _("close_tmp"));
2202}
2203
2204int rewind_tmp (SL_TICKET fd)
2205{
2206 SL_ENTER(_("rewind_tmp"));
2207
2208 if (SL_ISERROR(sl_rewind (fd)))
2209 SL_RETURN((-1), _("rewind_tmp"));
2210 SL_RETURN((0), _("rewind_tmp"));
2211}
2212#endif
2213
2214/********************************************************
2215 * Search rotated logfile
2216 */
2217#include <unistd.h>
2218#include <libgen.h>
2219#include <dirent.h>
2220
2221char * sh_rotated_log_search(const char * path, struct stat * buf)
2222{
2223
2224 size_t size;
2225 int i;
2226 char * searchpath;
2227 struct stat sbuf;
2228 DIR * dp;
2229 char * dname;
2230 char * bname;
2231
2232 dname = sh_util_dirname(path);
2233 bname = sh_util_basename(path);
2234
2235 size = strlen(dname) + strlen(bname) + 4;
2236 searchpath = SH_ALLOC(size);
2237
2238 for (i = 0; i < 2; ++i)
2239 {
2240 snprintf(searchpath, size, "%s/%s.%1d", dname, bname, i);
2241 if (0 == stat(searchpath, &sbuf) && sbuf.st_ino == buf->st_ino)
2242 {
2243 SH_FREE(dname);
2244 SH_FREE(bname);
2245 return searchpath;
2246 }
2247 }
2248
2249 SH_FREE(searchpath);
2250
2251 if (NULL != (dp = opendir(dname)))
2252 {
2253 struct dirent * de;
2254
2255 while (NULL != (de = readdir(dp)))
2256 {
2257 if (0 == strcmp(de->d_name, ".") || 0 == strcmp(de->d_name, ".."))
2258 continue;
2259
2260 size = strlen(dname) + strlen(de->d_name) + 2;
2261 searchpath = SH_ALLOC(size);
2262 snprintf(searchpath, size, "%s/%s", dname, de->d_name);
2263
2264 if (0 == stat(searchpath, &sbuf) && sbuf.st_ino == buf->st_ino)
2265 {
2266 SH_FREE(dname);
2267 SH_FREE(bname);
2268 closedir(dp);
2269 return searchpath;
2270 }
2271
2272 SH_FREE(searchpath);
2273 }
2274 closedir(dp);
2275 }
2276
2277 SH_FREE(dname);
2278 SH_FREE(bname);
2279
2280 return NULL;
2281}
2282
Note: See TracBrowser for help on using the repository browser.