source: trunk/src/sh_tools.c@ 25

Last change on this file since 25 was 22, checked in by rainer, 19 years ago

Minor code revisions.

File size: 39.3 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
91#undef FIL__
92#define FIL__ _("sh_tools.c")
93
94#ifdef SH_ENCRYPT
95#include "rijndael-api-fst.h"
96char * errorExplain (int err_num)
97{
98 if (err_num == BAD_KEY_DIR)
99 return (_("Key direction is invalid"));
100 else if (err_num == BAD_KEY_MAT)
101 return (_("Key material not of correct length"));
102 else if (err_num == BAD_KEY_INSTANCE)
103 return (_("Key passed is not valid"));
104 else if (err_num == BAD_CIPHER_MODE)
105 return (_("Params struct passed to cipherInit invalid"));
106 else if (err_num == BAD_CIPHER_STATE)
107 return (_("Cipher in wrong state"));
108 else if (err_num == BAD_BLOCK_LENGTH)
109 return (_("Bad block length"));
110 else if (err_num == BAD_CIPHER_INSTANCE)
111 return (_("Bad cipher instance"));
112 else if (err_num == BAD_DATA)
113 return (_("Data contents are invalid"));
114 else
115 return (_("Unknown error"));
116}
117
118#endif
119
120/* --- recode all \blah escapes to '=XX' format, and also code all
121 * remaining unprintable chars ---
122 */
123#define SH_PUT_4(p, a, b, c) (p)[0] = (a); (p)[1] = (b); (p)[2] = (c);
124
125char * sh_tools_safe_name (const char * instr, int flag)
126{
127 unsigned char c, d;
128 const char * p;
129 char *q;
130 char tmp[4];
131 char * outstr;
132 int len = 1;
133 int i = 0;
134 unsigned char val_octal = '\0';
135 static char ctable[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
136 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
137
138 SL_ENTER(_("sh_tools_safe_name"));
139
140 if (instr)
141 len = (3 * strlen(instr)) + 4;
142
143 outstr = SH_ALLOC(len);
144
145 outstr[0] = '\0';
146 tmp[3] = '\0';
147
148 p = instr;
149 q = outstr;
150
151#if !defined(SH_USE_XML)
152 (void) flag; /* fix compiler warning */
153#endif
154
155 if (!p)
156 goto end;
157
158 while (*p)
159 {
160 c = *p;
161
162 if (*p == '\n')
163 {
164 outstr[i] = ' '; ++i; ++p;
165 continue;
166 }
167
168#ifdef SH_USE_XML
169 if (flag == 1)
170 {
171 if ((*p) == '"')
172 {
173 SH_PUT_4(&outstr[i], '=', '2', '2');
174 i+=3; ++p;
175 continue;
176 }
177 else if ((*p) == '&')
178 {
179 SH_PUT_4(&outstr[i], '=', '2', '6');
180 i+=3; ++p;
181 continue;
182 }
183 else if ((*p) == '<')
184 { /* left angle */
185 SH_PUT_4(&outstr[i], '=', '3', 'c');
186 i+=3; ++p;
187 continue;
188 }
189 else if ((*p) == '>')
190 { /* right angle */
191 SH_PUT_4(&outstr[i], '=', '3', 'e');
192 i+=3; ++p;
193 continue;
194 }
195 }
196#endif
197
198 if ( (*p) != '\\' && (*p) != '&' && (*p) != '=' && (*p) != '\'')
199 {
200 outstr[i] = *p; ++i;
201 ++p;
202
203 if (c < 31 || c > 126)
204 {
205 --i;
206 d = c % 16; c = c / 16;
207 outstr[i] = '='; ++i;
208 outstr[i] = ctable[c]; ++i;
209 outstr[i] = ctable[d]; ++i;
210 }
211
212 continue;
213 }
214 else if ((*p) == '\'')
215 {
216 SH_PUT_4(&outstr[i], '=', '2', '7');
217 i+=3; ++p;
218 }
219 else if (*p == '=')
220 {
221 if (p[1] != '"' && p[1] != '<')
222 {
223 SH_PUT_4(&outstr[i], '=', '3', 'd');
224 i+=3; ++p;
225 }
226 else
227 { outstr[i] = *p; ++i; ++p; }
228 }
229 else if (*p == '\\')
230 {
231 ++p;
232 if (!p)
233 break;
234 if (!(*p))
235 break;
236
237 c = *p;
238
239 switch (*p) {
240 case '\\':
241 SH_PUT_4(&outstr[i], '=', '5', 'c');
242 i+=3; ++p;
243 break;
244 case 'n':
245 SH_PUT_4(&outstr[i], '=', '0', 'a');
246 i+=3; ++p;
247 break;
248 case 'b':
249 SH_PUT_4(&outstr[i], '=', '0', '8');
250 i+=3; ++p;
251 break;
252 case 'r':
253 SH_PUT_4(&outstr[i], '=', '0', 'd');
254 i+=3; ++p;
255 break;
256 case 't':
257 SH_PUT_4(&outstr[i], '=', '0', '9');
258 i+=3; ++p;
259 break;
260 case 'v':
261 SH_PUT_4(&outstr[i], '=', '0', 'b');
262 i+=3; ++p;
263 break;
264 case 'f':
265 SH_PUT_4(&outstr[i], '=', '0', 'c');
266 i+=3; ++p;
267 break;
268 case '\'':
269 SH_PUT_4(&outstr[i], '=', '2', '7');
270 i+=3; ++p;
271 break;
272 case '"': /* also encode quoted '"' */
273 SH_PUT_4(&outstr[i], '=', '2', '2');
274 i+=3; ++p;
275 break;
276 case ' ':
277 SH_PUT_4(&outstr[i], '=', '2', '0');
278 i+=3; ++p;
279 break;
280 default:
281 if (strlen(p) < 3) /* certainly not an octal number, skip */
282 {
283 p += strlen(p);
284 }
285 else
286 {
287 tmp[0] = p[0]; tmp[1] = p[1]; tmp[2] = p[2];
288 val_octal = (unsigned char) strtoul(tmp, (char **)NULL, 8);
289 if (val_octal != '\0') {
290 c = val_octal;
291 d = c % 16; c = c / 16;
292 outstr[i] = '='; ++i;
293 outstr[i] = ctable[c]; ++i;
294 outstr[i] = ctable[d]; ++i;
295 }
296 p += 3;
297 }
298 }
299 }
300 else if (*p == '&')
301 {
302 ++p;
303 if (!p || !(*p))
304 {
305 outstr[i] = '&'; ++i;
306 break;
307 }
308
309 if (p[0] == 'a' && p[1] == 'm' && p[2] == 'p' && p[3] == ';')
310 {
311 SH_PUT_4(&outstr[i], '=', '2', '6');
312 i+=3; p += 4;
313 }
314 else if (p[0] == 'q' && p[1] == 'u' && p[2] == 'o' && p[3] == 't' &&
315 p[4] == ';')
316 {
317 SH_PUT_4(&outstr[i], '=', '2', '2');
318 i+=3; p += 5;
319 }
320 else if (p[0] == 'l' && p[1] == 't' && p[2] == ';')
321 {
322 SH_PUT_4(&outstr[i], '=', '3', 'c');
323 i+=3; p += 3;
324 }
325 else if (p[0] == 'g' && p[1] == 't' && p[2] == ';')
326 {
327 SH_PUT_4(&outstr[i], '=', '3', 'e');
328 i+=3; p += 3;
329 }
330 else /* conserve the '&' */
331 {
332 outstr[i] = '&'; ++i;
333 }
334 }
335 else
336 {
337 outstr[i] = *p; ++i;
338 ++p;
339 }
340 } /* while (p && *p) */
341
342 end:
343
344 outstr[i] = '\0';
345 SL_RETURN( outstr, _("sh_tools_safe_name"));
346}
347
348
349/* extern int h_errno; */
350
351char * sh_tools_errmessage (int tellme)
352{
353
354#ifdef HOST_NOT_FOUND
355 if (tellme == HOST_NOT_FOUND)
356 return _("The specified host is unknown: ");
357#endif
358#ifdef NO_ADDRESS
359 if (tellme == NO_ADDRESS)
360 return _("The requested name is valid but does not have an IP address: ");
361#endif
362#ifdef NO_RECOVERY
363 if (tellme == NO_RECOVERY)
364 return _("A non-recoverable name server error occurred: ");
365#endif
366#ifdef TRY_AGAIN
367 if (tellme == TRY_AGAIN)
368 return _("A temporary error occurred on an authoritative name server. The specified host is unknown: ");
369#endif
370 return _("Unknown error");
371}
372
373int is_numeric (const char * address)
374{
375 int j;
376 int len = sl_strlen(address);
377
378 for (j = 0; j < len; ++j)
379 if ( (address[j] < '0' || address[j] > '9') && address[j] != '.')
380 return (1 == 0);
381 return (1 == 1);
382}
383
384#if defined (SH_WITH_SERVER)
385
386int get_open_max ()
387{
388 int value;
389
390#ifdef _SC_OPEN_MAX
391 value = sysconf (_SC_OPEN_MAX);
392#else
393#ifdef OPEN_MAX
394 value = OPEN_MAX;
395#else
396 value = _POSIX_OPEN_MAX;
397#endif
398#endif
399
400 if (value < 0)
401 value = 8; /* POSIX lower limit */
402
403 if (value > 4096)
404 value = 4096;
405
406 return value;
407}
408
409#endif
410
411typedef struct _sin_cache {
412 char * address;
413 struct sockaddr_in sin;
414 struct _sin_cache * next;
415} sin_cache;
416
417static sin_cache * conn_cache = NULL;
418static int cached_addr = 0;
419
420void delete_cache()
421{
422 sin_cache * check_cache = conn_cache;
423 sin_cache * old_entry = conn_cache;
424
425 SL_ENTER(_("delete_cache"));
426
427 while (check_cache != NULL)
428 {
429 old_entry = check_cache;
430 check_cache = check_cache->next;
431 SH_FREE(old_entry->address);
432 SH_FREE(old_entry);
433 }
434
435 cached_addr = 0;
436
437 conn_cache = NULL;
438 SL_RET0(_("delete_cache"));
439}
440
441int DoReverseLookup = S_TRUE;
442
443int set_reverse_lookup (const char * c)
444{
445 return sh_util_flagval(c, &DoReverseLookup);
446}
447
448int connect_port (char * address, int port,
449 char * ecall, int * errnum, char * errmsg, int errsiz)
450{
451 struct in_addr haddr; /* host address from numeric */
452 /* host details returned by the DNS */
453 struct hostent *host_entry = NULL;
454 struct sockaddr_in sinr; /* socket to the remote host */
455
456 char * host_name;
457
458 int fd = (-1);
459 int status;
460 int fail = 0;
461 int cached = 0;
462
463 int retval;
464 size_t len;
465
466 sin_cache * check_cache = conn_cache;
467
468 SL_ENTER(_("connect_port"));
469
470 /* paranoia -- should not happen
471 */
472 if (cached_addr > 128)
473 delete_cache();
474
475 if (check_cache != NULL)
476 {
477 while (check_cache && check_cache->address)
478 {
479 if ( 0 == sl_strncmp(check_cache->address,
480 address, sl_strlen(address)))
481 {
482 memcpy (&sinr, &(check_cache->sin), sizeof(struct sockaddr_in));
483 sinr.sin_family = AF_INET;
484 sinr.sin_port = htons (port);
485 cached = 1;
486 break;
487 }
488 if (check_cache->next)
489 check_cache = check_cache->next;
490 else
491 check_cache = NULL;
492 }
493 }
494
495 /* only use gethostbyname() if neccessary
496 */
497 if (cached == 0)
498 {
499#ifdef HAVE_INET_ATON
500 if (0 == inet_aton(address, &haddr))
501#else
502 if ((unsigned long)-1 == (haddr.s_addr = inet_addr(address)))
503#endif
504 {
505
506 host_entry = sh_gethostbyname(address);
507
508 if (host_entry == NULL || host_entry->h_addr == NULL)
509 {
510 sl_strlcpy(ecall, _("gethostbyname"), SH_MINIBUF);
511#ifndef NO_H_ERRNO
512 *errnum = h_errno;
513#else
514 *errnum = 666;
515#endif
516 sl_strlcpy(errmsg, sh_tools_errmessage (*errnum), errsiz);
517 sl_strlcat(errmsg, address, errsiz);
518 fail = (-1);
519 }
520 else
521 {
522 sinr.sin_family = AF_INET;
523 sinr.sin_port = htons (port);
524 sinr.sin_addr = *(struct in_addr *) host_entry->h_addr;
525
526
527 /* reverse DNS lookup
528 */
529 if (DoReverseLookup == S_TRUE)
530 {
531 if (host_entry->h_name == NULL)
532 {
533 host_name = SH_ALLOC(1);
534 host_name[0] = '\0';
535 }
536 else
537 {
538 len = sl_strlen(host_entry->h_name) + 1;
539 host_name = SH_ALLOC(len);
540 if (len > 1)
541 sl_strlcpy(host_name, host_entry->h_name, len);
542 else
543 host_name[0] = '\0';
544 }
545
546 host_entry = sh_gethostbyaddr ((char *) &sinr.sin_addr,
547 sizeof(struct in_addr),
548 AF_INET);
549 if (host_entry == NULL || host_entry->h_name == NULL)
550 {
551 sl_strlcpy(ecall, _("gethostbyaddr"), SH_MINIBUF);
552#ifndef NO_H_ERRNO
553 *errnum = h_errno;
554#else
555 *errnum = 666;
556#endif
557 sl_strlcpy(errmsg,
558 sh_tools_errmessage (*errnum), errsiz);
559 sl_strlcat(errmsg,
560 inet_ntoa (*(struct in_addr *) &(sinr.sin_addr)),
561 errsiz);
562 fail = (-1);
563 }
564 else
565 {
566 *errnum = 0;
567 if (sl_strlen(host_entry->h_name) == 0 ||
568 (*errnum = sl_strcmp(host_name,host_entry->h_name)) != 0)
569 {
570 if (*errnum)
571 sl_strlcpy(ecall, _("strcmp"), SH_MINIBUF);
572 else
573 sl_strlcpy(ecall, _("strlen"), SH_MINIBUF);
574 sl_strlcpy(errmsg, _("Reverse lookup failed."),
575 errsiz);
576 sl_strlcat(errmsg, address, errsiz);
577 sl_strlcat(errmsg, _(" vs "), errsiz);
578 sl_strlcat(errmsg,
579 inet_ntoa (*(struct in_addr *) &(sinr.sin_addr)),
580 errsiz);
581 fail = -1;
582 }
583 }
584
585 SH_FREE(host_name);
586 }
587 }
588 }
589
590 else /* address was numeric */
591 {
592 sinr.sin_family = AF_INET;
593 sinr.sin_port = htons (port);
594 sinr.sin_addr = haddr;
595 }
596
597
598 if (fail != -1)
599 {
600 /* put it into the cache
601 */
602 check_cache = SH_ALLOC(sizeof(sin_cache));
603 check_cache->address = SH_ALLOC(sl_strlen(address) + 1);
604 sl_strlcpy (check_cache->address, address, sl_strlen(address) + 1);
605 memcpy(&(check_cache->sin), &sinr, sizeof(struct sockaddr_in));
606 ++cached_addr;
607
608 if (conn_cache)
609 {
610 if (conn_cache->next)
611 check_cache->next = conn_cache->next;
612 else
613 check_cache->next = NULL;
614 conn_cache->next = check_cache;
615 }
616 else
617 {
618 check_cache->next = NULL;
619 conn_cache = check_cache;
620 }
621 }
622 }
623
624
625 if (fail != (-1))
626 {
627 fd = socket(AF_INET, SOCK_STREAM, 0);
628 if (fd < 0) {
629 fail = (-1);
630 status = errno;
631 sl_strlcpy(ecall, _("socket"), SH_MINIBUF);
632 *errnum = status;
633 sl_strlcpy(errmsg, sh_error_message (status), errsiz);
634 sl_strlcat(errmsg, _(", address "), errsiz);
635 sl_strlcat(errmsg, address, errsiz);
636 }
637 }
638
639 if (fail != (-1)) {
640
641 if ( retry_connect(FIL__, __LINE__, fd,
642 (struct sockaddr *) &sinr, sizeof(sinr)) < 0)
643 {
644 status = errno;
645 sl_strlcpy(ecall, _("connect"), SH_MINIBUF);
646 *errnum = status;
647 sl_strlcpy(errmsg, sh_error_message (status), errsiz);
648 sl_strlcat(errmsg, _(", address "), errsiz);
649 sl_strlcat(errmsg, address, errsiz);
650 close(fd);
651 fail = (-1);
652 }
653 }
654
655 retval = (fail < 0) ? (-1) : fd;
656 SL_RETURN(retval, _("connect_port"));
657}
658
659int connect_port_2 (char * address1, char * address2, int port,
660 char * ecall, int * errnum, char * errmsg, int errsiz)
661{
662 int retval = (-1);
663
664 SL_ENTER(_("connect_port_2"));
665
666 errmsg[0] = '\0';
667 *errnum = 0;
668
669 if (address1 != NULL && address1[0] != '\0')
670 retval = connect_port (address1, port,
671 ecall, errnum,
672 errmsg, errsiz);
673
674 if (retval < 0 && address2 != NULL && address2[0] != '\0')
675 {
676 /* can't use sh_error_handle here, as this would cause an infinite
677 * loop if called from sh_unix_time
678 */
679 TPT(( 0, FIL__, __LINE__, _("msg=<Using alternative server %s.>\n"),
680 address2));
681 retval = connect_port (address2, port,
682 ecall, errnum,
683 errmsg, errsiz);
684 }
685
686 if ((retval < 0) &&
687 (address1 == NULL || address1[0] == '\0') &&
688 (address1 == NULL || address1[0] == '\0'))
689 {
690 sl_strlcpy(ecall, _("connect_port_2"), SH_MINIBUF);
691 sl_strlcpy(errmsg, _("No server address known"), errsiz);
692 }
693 SL_RETURN(retval, _("connect_port_2"));
694 /* return retval; */
695}
696
697#if defined(HAVE_NTIME) || defined(SH_WITH_CLIENT)
698static
699int sh_write_select(int type, int sockfd,
700 char *buf, int nbytes,
701 int * w_error, int timeout)
702{
703 int countbytes, count;
704 fd_set fds;
705 struct timeval tv;
706 int select_now;
707 int num_sel;
708
709 struct sigaction new_act;
710 struct sigaction old_act;
711
712 SL_ENTER(_("sh_write_select"));
713
714 /* ignore SIGPIPE (instead get EPIPE if connection is closed)
715 */
716 new_act.sa_handler = SIG_IGN;
717 sigemptyset( &new_act.sa_mask ); /* set an empty mask */
718 new_act.sa_flags = 0; /* init sa_flags */
719 sigaction (SIGPIPE, &new_act, &old_act);
720
721 FD_ZERO(&fds);
722 FD_SET(sockfd, &fds);
723
724 countbytes = 0;
725 tv.tv_sec = 1;
726 tv.tv_usec = 0;
727 select_now = 0;
728
729 *w_error = 0;
730
731 while ( countbytes < nbytes ) {
732
733 FD_ZERO(&fds);
734 FD_SET(sockfd, &fds);
735
736 if (type == SH_DO_WRITE)
737 {
738 if ( (num_sel = select (sockfd+1, NULL, &fds, NULL, &tv)) == -1)
739 {
740 if (sig_raised == 1)
741 {
742 sig_raised = 2;
743 continue;
744 }
745 if ( errno == EINTR) /* try again */
746 continue;
747 *w_error = errno;
748 TPT(( 0, FIL__, __LINE__, _("msg=<select: %s>\n"),
749 sh_error_message(*w_error)));
750 sigaction (SIGPIPE, &old_act, NULL);
751 SL_RETURN( countbytes, _("sh_write_select"));
752 }
753 }
754 else
755 {
756 if ( (num_sel = select (sockfd+1, &fds, NULL, NULL, &tv)) == -1)
757 {
758 if (sig_raised == 1)
759 {
760 sig_raised = 2;
761 continue;
762 }
763 if ( errno == EINTR ) /* try again */
764 continue;
765 *w_error = errno;
766 TPT(( 0, FIL__, __LINE__, _("msg=<select: %s>\n"),
767 sh_error_message(*w_error)));
768 sigaction (SIGPIPE, &old_act, NULL);
769 SL_RETURN( countbytes, _("sh_write_select"));
770 }
771 }
772
773 /* on Linux, timeout is modified to reflect the amount of
774 * time not slept
775 */
776 tv.tv_sec = 1;
777 tv.tv_usec = 0;
778
779
780 /* let's not hang on forever
781 */
782 if (num_sel == 0)
783 {
784 ++select_now; /* timeout */
785 if ( select_now > timeout ) /* 5 minutes */
786 {
787#ifdef ETIMEDOUT
788 *w_error = ETIMEDOUT;
789#else
790 *w_error = 0;
791#endif
792 sigaction (SIGPIPE, &old_act, NULL);
793 TPT(( 0, FIL__, __LINE__, _("msg=<Timeout>\n")));
794 SL_RETURN( countbytes, _("sh_write_select"));
795 }
796 }
797
798 if ( FD_ISSET (sockfd, &fds) )
799 {
800 if (type == SH_DO_WRITE)
801 count = write (sockfd, buf, nbytes-countbytes);
802 else
803 count = read (sockfd, buf, nbytes-countbytes);
804
805 if (count > 0)
806 {
807 countbytes += count;
808 buf += count; /* move buffer pointer forward */
809 if (countbytes < nbytes) FD_SET( sockfd, &fds );
810 }
811 else if (count < 0 && errno == EINTR)
812 {
813 FD_SET( sockfd, &fds );
814 }
815 else if (count < 0)
816 {
817 *w_error = errno;
818 sigaction (SIGPIPE, &old_act, NULL);
819 TPT(( 0, FIL__, __LINE__, _("msg=<count < 0>\n")));
820 SL_RETURN( countbytes, _("sh_write_select"));
821 }
822 else /* count == 0 */
823 {
824 *w_error = errno;
825 sigaction (SIGPIPE, &old_act, NULL);
826 TPT(( 0, FIL__, __LINE__, _("msg=<count == 0>\n")));
827 SL_RETURN( countbytes, _("sh_write_select"));
828 }
829 }
830 }
831
832
833 /* restore signal handler
834 */
835 sigaction (SIGPIPE, &old_act, NULL);
836
837 *w_error = 0;
838
839 TPT(( 0, FIL__, __LINE__, _("msg=<count = %d>\n"), countbytes));
840 SL_RETURN( countbytes, _("sh_write_select"));
841}
842#endif
843
844#if defined (SH_WITH_CLIENT)
845unsigned long write_port (int sockfd, char *buf, unsigned long nbytes,
846 int * w_error, int timeout)
847{
848 unsigned long bytes;
849 char errmsg[256];
850
851 SL_ENTER(_("write_port"));
852
853 bytes = sh_write_select(SH_DO_WRITE, sockfd, buf, nbytes, w_error, timeout);
854 if (*w_error != 0)
855 {
856 sl_strlcpy(errmsg, sh_error_message (*w_error), sizeof(errmsg));
857 sh_error_handle((-1), FIL__, __LINE__, *w_error, MSG_TCP_NETRP,
858 errmsg, (long) sockfd, _("write_port"));
859 }
860 SL_RETURN( bytes, _("write_port"));
861}
862#endif
863
864#if defined(HAVE_NTIME) || defined(SH_WITH_CLIENT)
865
866unsigned long read_port (int sockfd, char *buf, unsigned long nbytes,
867 int * w_error, int timeout)
868{
869 unsigned long bytes;
870 char errmsg[256];
871
872 SL_ENTER(_("read_port"));
873
874 bytes = sh_write_select(SH_DO_READ, sockfd, buf, nbytes, w_error, timeout);
875 if (*w_error != 0)
876 {
877 sl_strlcpy(errmsg, sh_error_message (*w_error), sizeof(errmsg));
878 sh_error_handle((-1), FIL__, __LINE__, *w_error, MSG_TCP_NETRP,
879 errmsg, (long) sockfd, _("read_port"));
880 }
881 SL_RETURN( bytes, _("read_port"));
882}
883#endif
884
885#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
886
887int check_request_nerr (char * have, char * need)
888{
889 SL_ENTER(_("check_request_nerr"));
890 ASSERT_RET((have != NULL && need != NULL),
891 _("have != NULL && need != NULL"), (-1))
892
893 if ( (have[0] == need[0]) && (have[1] == need[1]) &&
894 (have[2] == need[2]) && (have[3] == need[3]))
895 SL_RETURN(0, _("check_request_nerr"));
896 SL_RETURN((-1), _("check_request_nerr"));
897}
898#endif
899
900#if defined (SH_WITH_CLIENT)
901
902int check_request (char * have, char * need)
903{
904 char first[21], second[5];
905 int i;
906
907 SL_ENTER(_("check_request"));
908 i = check_request_nerr (have, need);
909
910 if (i == 0)
911 SL_RETURN(0, _("check_request"));
912
913 for (i = 0; i < 4; ++i)
914 {
915 second[i] = need[i];
916 sprintf(&first[i*4], _("%c%03o"), /* known to fit */
917 '\\', (unsigned char) have[i]);
918 }
919
920 first[20] = '\0'; second[4] = '\0';
921
922 sh_error_handle((-1), FIL__, __LINE__, EINVAL, MSG_E_NETST,
923 second, first);
924 SL_RETURN((-1), _("check_request"));
925}
926#endif
927
928#if defined (SH_WITH_SERVER)
929
930int check_request_s (char * have, char * need, char * clt)
931{
932 char first[21], second[5];
933 int i;
934
935 SL_ENTER(_("check_request_s"));
936 i = check_request_nerr (have, need);
937
938 if (i == 0)
939 SL_RETURN( (0), _("check_request_s"));
940
941 for (i = 0; i < 4; ++i)
942 {
943 second[i] = need[i];
944 sprintf(&first[i*4], _("%c%03o"), /* known to fit */
945 '\\', (unsigned char) have[i]);
946 }
947 first[20] = '\0'; second[4] = '\0';
948 sh_error_handle((-1), FIL__, __LINE__, EINVAL, MSG_E_NETST1,
949 second, first, clt);
950 SL_RETURN( (-1), _("check_request_s"));
951}
952#endif
953
954#if defined (SH_WITH_CLIENT)
955
956void get_header (unsigned char * head, unsigned long * bytes, char * u)
957{
958 SL_ENTER(_("get_header"));
959
960 *bytes =
961 (256 * (unsigned int)head[1] + (unsigned int)head[2]);
962
963 if (u != NULL)
964 {
965 u[0] = head[3];
966 u[1] = head[4];
967 u[2] = head[5];
968 u[3] = head[6];
969 u[4] = '\0';
970 }
971
972 SL_RET0(_("get_header"));
973}
974#endif
975
976#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
977
978#ifdef SH_ENCRYPT_2
979#define TRANS_BYTES 65120
980#else
981#define TRANS_BYTES 65280
982#endif
983
984void put_header (unsigned char * head, int protocol,
985 unsigned long * length, char * u)
986{
987
988 /* static long transfer_limit = (8 * SH_BUFSIZE); V0.8 */
989 static unsigned long transfer_limit = TRANS_BYTES + 6 + KEY_LEN;
990
991 SL_ENTER(_("put_header"));
992
993 head[0] = protocol;
994
995 ASSERT((*length < transfer_limit), _("*length < transfer_limit"))
996
997 if (*length > transfer_limit)
998 *length = transfer_limit;
999
1000 head[1] = (unsigned int)(*length/256);
1001 head[2] = (unsigned int)(*length-256 * head[1]);
1002 if (u == NULL)
1003 {
1004 head[3] = 0x01;
1005 head[4] = 0x01;
1006 head[5] = 0x01;
1007 head[6] = 0x01;
1008 }
1009 else
1010 {
1011 head[3] = u[0];
1012 head[4] = u[1];
1013 head[5] = u[2];
1014 head[6] = u[3];
1015 }
1016
1017 SL_RET0(_("put_header"));
1018}
1019#endif
1020
1021/* ------------------------------------------
1022 *
1023 * version 2 client/server protocol
1024 *
1025 * ------------------------------------------
1026 *
1027 * header : flag size[2]
1028 *
1029 * payload: random_pad[8] protocol[4] size[4] payload[payload_size] padding
1030 *
1031 * full_size <= 8192; payload_size <= 8176 (511*16); msg_size <= 8128 (508*16)
1032 * (msg_size = payload_size - key_len = payload_size - 48)
1033 */
1034#ifdef SH_WITH_SERVER
1035#define SH_V2_FULLSIZE 240
1036#define SH_V2_PAYLOAD 224
1037#define SH_V2_MESSAGE 176
1038#else
1039#define SH_V2_FULLSIZE 1024
1040#define SH_V2_PAYLOAD 1008
1041#define SH_V2_MESSAGE 960
1042#endif
1043
1044#ifdef SH_ENCRYPT
1045#include "rijndael-api-fst.h"
1046#endif
1047
1048void sh_tools_show_header (unsigned char * head, char sign)
1049{
1050#define SH_IS_ASCII(c) (((c) & ~0x7f) == 0)
1051
1052
1053 int msg_size = (256 * (unsigned int)head[1] + (unsigned int)head[2]);
1054 char code[32];
1055 char * p = &code[0];
1056
1057 memset (code, ' ', 32); /* space */
1058
1059 if ((head[0] & SH_PROTO_SRP) != 0) { p[0]='S';p[1]='R';p[2]='P';}
1060 p += 4;
1061 if ((head[0] & SH_PROTO_MSG) != 0) { p[0]='M';p[1]='S';p[2]='G';}
1062 p += 4;
1063 if ((head[0] & SH_PROTO_BIG) != 0) { p[0]='B';p[1]='I';p[2]='G';}
1064 p += 4;
1065 if ((head[0] & SH_PROTO_END) != 0) { p[0]='E';p[1]='N';p[2]='D';}
1066 p += 4;
1067 if ((head[0] & SH_PROTO_ENC) != 0) { p[0]='E';p[1]='N';p[2]='C';}
1068 p += 4;
1069 if ((head[0] & SH_PROTO_EN2) != 0) { p[0]='E';p[1]='N';p[2]='2';}
1070 code[23] = '\0';
1071
1072 if (SH_IS_ASCII(head[3]) && isalpha(head[3]) &&
1073 SH_IS_ASCII(head[4]) && isalpha(head[4]) &&
1074 SH_IS_ASCII(head[5]) && isalpha(head[5]) &&
1075 SH_IS_ASCII(head[6]) && isalpha(head[6])) {
1076 fprintf(stderr, "%c %3o %s %5d %c %c %c %c\n", sign,
1077 head[0], code, msg_size, head[3], head[4], head[5], head[6]);
1078 } else {
1079 fprintf(stderr, "%c %3o %s %5d %2X %2X %2X %2X\n", sign,
1080 head[0], code, msg_size, head[3], head[4], head[5], head[6]);
1081 }
1082 return;
1083}
1084
1085#ifdef SH_ENCRYPT
1086/*
1087 * #define DEBUG_EN2
1088 *
1089 * ingest version 1 7-byte header and payload, return version2 header/payload
1090 * last 4 bytes of outgoing header are set to dummy value
1091 */
1092char * sh_tools_makePack (unsigned char * header,
1093 char * payload, int payload_size,
1094 keyInstance * keyInstE)
1095{
1096 UINT32 rpad[3];
1097 unsigned char head[16];
1098 double epad;
1099 int i_epad = 0;
1100 int i_blk = payload_size / 16;
1101 int i_blkmax = SH_V2_FULLSIZE / 16;
1102 int pads = 0;
1103 int full_size;
1104 char * full_ret;
1105
1106 char * p;
1107 RIJ_BYTE inBlock[B_SIZ];
1108 RIJ_BYTE outBlock[B_SIZ];
1109 int j;
1110 cipherInstance cipherInst;
1111 int err_num;
1112 int blkfac;
1113
1114 /*
1115 SL_REQUIRE (i_blk*16 == payload_size, _("payload_size % 16 != 0"));
1116 */
1117 if ((i_blk * 16) != payload_size) ++i_blk;
1118#ifdef DEBUG_EN2
1119 fprintf(stderr, "SEND <%d> blocks <%d>\n", payload_size, i_blk);
1120#endif
1121 /* random_pad
1122 */
1123 rpad[1] = taus_get (&(skey->rng0[0]), &(skey->rng1[0]), &(skey->rng2[0]));
1124 memcpy (head, &rpad[1], 4);
1125 rpad[0] = taus_get (&(skey->rng0[0]), &(skey->rng1[0]), &(skey->rng2[0]));
1126 memcpy (&head[4], &rpad[0], 4);
1127 rpad[2] = taus_get (&(skey->rng0[0]), &(skey->rng1[0]), &(skey->rng2[0]));
1128 memcpy (&head[8], &rpad[2], 4);
1129
1130 /* protocol
1131 */
1132 /* memcpy (&head[8], &header[3], 4); */
1133
1134 /* size (payload)
1135 */
1136 head[12] = header[1];
1137 head[13] = header[2];
1138 head[14] = '\0';
1139 head[15] = '\0';
1140
1141 if (i_blk < i_blkmax)
1142 {
1143 pads = i_blkmax - i_blk;
1144 /* memcpy((char *) &rpad[2], &head[12], 4); */
1145 epad = taus_get_double (&rpad);
1146#ifdef DEBUG_EN2
1147 fprintf(stderr, "PAD1 <%d> <%f>\n", pads, epad);
1148#endif
1149 i_epad = (int) (pads * epad);
1150#ifdef DEBUG_EN2
1151 fprintf(stderr, "PAD2 <%d> <%d>\n", i_epad, (i_epad*16));
1152#endif
1153 }
1154
1155 full_size =
1156 /* head */ 16 +
1157 /* payload */ (i_blk*16) + /* payload_size + */
1158 /* pad */ (i_epad * 16);
1159
1160 full_ret = SH_ALLOC(full_size);
1161 memcpy(full_ret, head, 16);
1162 if (payload != NULL)
1163 {
1164 memcpy(&full_ret[16], payload, payload_size);
1165 }
1166 if ((i_blk*16) > payload_size)
1167 {
1168#ifdef DEBUG_EN2
1169 fprintf(stderr, "SEN2 <%d>\n", (i_blk*16) - payload_size);
1170#endif
1171 memset(&full_ret[16+payload_size], '\0', (i_blk*16) - payload_size);
1172 payload_size = i_blk * 16;
1173 }
1174 memset(&full_ret[16+payload_size], '\0', i_epad*16);
1175#ifdef DEBUG_EN2
1176 fprintf(stderr, "SEN3 <%d> <%d>\n", full_size, i_epad*16);
1177#endif
1178
1179 /* rewrite header
1180 */
1181 header[1] = (unsigned int)(full_size/256);
1182 header[2] = (unsigned int)(full_size - (256 * header[1]));
1183 /* don't erase protocol from header
1184 memset(&header[3], '\0', 4);
1185 */
1186 p = full_ret; blkfac = full_size / 16;
1187
1188 err_num = cipherInit (&cipherInst, MODE_CBC, NULL);
1189
1190 if (err_num < 0)
1191 {
1192 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
1193 errorExplain(err_num),
1194 _("sh_tools_makePack: cipherInit"));
1195 }
1196 for (j = 0; j < blkfac; ++j)
1197 {
1198 memcpy(inBlock, p, B_SIZ);
1199 err_num = blockEncrypt(&cipherInst, keyInstE,
1200 inBlock, 128 * BNUM, outBlock);
1201 if (err_num < 0)
1202 {
1203 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
1204 errorExplain(err_num),
1205 _("sh_tools_makePack: blockEncrypt"));
1206 }
1207 memcpy(p, outBlock, B_SIZ);
1208 p += B_SIZ;
1209 }
1210
1211 return full_ret;
1212}
1213
1214/* write a 7-byte header and return payload as expected by version 1
1215 * last 4 bytes of incoming header are dummy
1216 */
1217char * sh_tools_revertPack (unsigned char * header, char * message,
1218 keyInstance * keyInstD,
1219 unsigned long message_size)
1220{
1221 unsigned long msg_size;
1222 char * msg_ret;
1223
1224 char * p;
1225 RIJ_BYTE inBlock[B_SIZ];
1226 RIJ_BYTE outBlock[B_SIZ];
1227 int j;
1228 cipherInstance cipherInst;
1229 int err_num;
1230 int blkfac;
1231
1232 msg_size = (256 * (unsigned int)header[1] + (unsigned int)header[2]);
1233#ifdef DEBUG_EN2
1234 fprintf(stderr, "RECV <%lu>\n", msg_size);
1235#endif
1236 if (msg_size > message_size) {
1237 msg_size = message_size;
1238#ifdef DEBUG_EN2
1239 fprintf(stderr, "RECV TRUNC1 <%lu>\n", msg_size);
1240#endif
1241 }
1242
1243 p = message; blkfac = msg_size / 16;
1244
1245 err_num = cipherInit (&cipherInst, MODE_CBC, NULL);
1246
1247 if (err_num < 0)
1248 {
1249 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
1250 errorExplain(err_num),
1251 _("sh_tools_revertPack: cipherInit"));
1252 }
1253 for (j = 0; j < blkfac; ++j)
1254 {
1255 memcpy(inBlock, p, B_SIZ);
1256 err_num = blockDecrypt(&cipherInst, keyInstD,
1257 inBlock, 128 * BNUM, outBlock);
1258 if (err_num < 0)
1259 {
1260 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
1261 errorExplain(err_num),
1262 _("sh_tools_revertPack: blockDecrypt"));
1263 }
1264 memcpy(p, outBlock, B_SIZ);
1265 p += B_SIZ;
1266 }
1267
1268 /* rewrite size in header
1269 */
1270 header[1] = message[12];
1271 header[2] = message[13];
1272 msg_size = (256 * (unsigned int)header[1] + (unsigned int)header[2]);
1273
1274 if (msg_size > (message_size-16))
1275 {
1276 msg_size = message_size-16;
1277 header[1] = (unsigned int)(msg_size/256);
1278 header[2] = (unsigned int)(msg_size - (256 * header[1]));
1279#ifdef DEBUG_EN2
1280 fprintf(stderr, "RECV TRUNC2 <%lu>\n", msg_size);
1281#endif
1282 }
1283#ifdef DEBUG_EN2
1284 fprintf(stderr, "REC2 <%lu>\n", msg_size);
1285#endif
1286 /* protocol
1287 */
1288 /* memcpy(&header[3], &message[8], 4); */
1289
1290 /* payload
1291 */
1292 msg_ret = SH_ALLOC(msg_size+1);
1293 if (msg_size > 0)
1294 {
1295 memcpy(msg_ret, &message[16], msg_size);
1296 }
1297 msg_ret[msg_size] = '\0';
1298#ifdef DEBUG_EN2
1299 fprintf(stderr, "REC3 <%lu>\n", msg_size);
1300#endif
1301 SH_FREE(message);
1302
1303 return msg_ret;
1304}
1305#endif
1306
1307int sh_tools_hash_add(char * key, char * buf, int buflen)
1308{
1309 char * theSig;
1310
1311 SL_ENTER(_("sh_tools_hash_add"));
1312
1313 theSig = sh_util_siggen (key, buf, buflen);
1314 sl_strlcat(buf, theSig, buflen + KEY_LEN + 1);
1315
1316 SL_RETURN((0), _("sh_tools_hash_add"));
1317}
1318
1319
1320/* return 0 (== FALSE) if no match, else 1 (== TRUE)
1321 */
1322int sh_tools_hash_vfy(char * key, char * buf, int buflen)
1323{
1324 char hash[KEY_LEN+1];
1325 register int i;
1326 char * theSig;
1327
1328 SL_ENTER(_("sh_tools_hash_vfy"));
1329
1330 theSig = sh_util_siggen (key, buf, buflen);
1331 sl_strlcpy(hash, theSig, KEY_LEN+1);
1332
1333 for (i = 0; i < KEY_LEN; ++i)
1334 {
1335 if (buf[buflen + i] != hash[i])
1336 SL_RETURN((0), _("sh_tools_hash_vfy"));
1337 }
1338
1339 SL_RETURN((1), _("sh_tools_hash_vfy"));
1340}
1341
1342/* ------------------------------------------ */
1343
1344#if defined (SH_WITH_SERVER)
1345
1346/* add a checksum to a buffer; put checksum in front
1347 */
1348char * hash_me (char * key, char * buf, int buflen)
1349{
1350 char hash[KEY_LEN+1];
1351 char * temp;
1352 register int i;
1353 int total = 0;
1354 char * theSig;
1355
1356
1357 SL_ENTER(_("hash_me"));
1358
1359#ifdef DEBUG_EN2
1360 fprintf(stderr, "hash_me <%s> <%d>\n",
1361 (key == NULL) ? "NULL" : key, buflen);
1362#endif
1363 /* key = H(NSRV,NCLT,SK)
1364 */
1365 ASSERT_RET((key != NULL), _("key != NULL"), (NULL));
1366 ASSERT_RET((buflen >= 0), _("buflen >= 0"), (NULL));
1367
1368 theSig = sh_util_siggen (key, buf, buflen);
1369 sl_strlcpy(hash, theSig, KEY_LEN+1);
1370
1371
1372 total = KEY_LEN + buflen;
1373 temp = SH_ALLOC (total);
1374
1375 for (i = 0; i < KEY_LEN; ++i)
1376 temp[i] = hash[i];
1377
1378 for (i = 0; i < buflen; ++i)
1379 temp[i+KEY_LEN] = buf[i];
1380
1381 SL_RETURN(temp, _("hash_me"));
1382}
1383#endif
1384
1385#if defined (SH_WITH_CLIENT)
1386
1387/* verify the checksum of a buffer; checksum comes first
1388 */
1389int hash_check(char * key,
1390 char * buf, int buflen)
1391{
1392 char hash[KEY_LEN+1];
1393 register int i;
1394 char * theSig;
1395
1396 SL_ENTER(_("hash_check"));
1397
1398#ifdef DEBUG_EN2
1399 fprintf(stderr, "hash_check <%s> <%d>\n",
1400 (key == NULL) ? "NULL" : key, buflen);
1401#endif
1402 theSig = sh_util_siggen (key, &buf[KEY_LEN], buflen-KEY_LEN);
1403 sl_strlcpy(hash, theSig, KEY_LEN+1);
1404
1405 for (i = 0; i < KEY_LEN; ++i)
1406 {
1407 if (buf[i] != hash[i])
1408 SL_RETURN((-1), _("hash_check"));
1409 }
1410 SL_RETURN((0), _("hash_check"));
1411}
1412
1413#endif
1414
1415#if defined (SH_WITH_SERVER)
1416
1417char * get_client_conf_file (char * peer, unsigned long * length)
1418{
1419 char * ret;
1420 int size, status;
1421 struct stat buf;
1422 char * base;
1423
1424 SL_ENTER(_("get_client_conf_file"));
1425
1426 size = sl_strlen(DEFAULT_DATAROOT);
1427 base = SH_ALLOC(size + 1);
1428 sl_strlcpy(base, DEFAULT_DATAROOT, size + 1);
1429
1430 size = sl_strlen(base) + sl_strlen(peer) + 5;
1431 ++size;
1432
1433 ret = SH_ALLOC(size);
1434 sl_strlcpy(ret, base, size);
1435 sl_strlcat(ret, _("/rc."), size);
1436 sl_strlcat(ret, peer, size);
1437
1438 status = retry_stat (FIL__, __LINE__, ret, &buf);
1439
1440 if (status == 0)
1441 goto lab_end;
1442 else
1443 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_ACCESS,
1444 (long) sh.effective.uid, ret);
1445
1446 sl_strlcpy(ret, base, size);
1447 sl_strlcat(ret, "/rc", size);
1448
1449 status = retry_stat (FIL__, __LINE__, ret, &buf);
1450
1451 if (status == 0)
1452 goto lab_end;
1453 else
1454 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_ACCESS,
1455 (long) sh.effective.uid, ret);
1456
1457 SH_FREE(base);
1458 *length=0;
1459 SL_RETURN(NULL, _("get_client_conf_file"));
1460
1461 lab_end:
1462 if (buf.st_size > 0x7fffffff)
1463 {
1464 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN,
1465 _("File too large"), _("get_client_conf_file"));
1466 SH_FREE(base);
1467 SL_RETURN(NULL, _("get_client_conf_file"));
1468 }
1469 *length = (unsigned long) buf.st_size;
1470 SH_FREE(base);
1471 SL_RETURN(ret, _("get_client_conf_file"));
1472}
1473
1474char * get_client_data_file (char * peer, unsigned long * length)
1475{
1476 char * ret;
1477 int size, status;
1478 struct stat buf;
1479
1480 char * base;
1481
1482 SL_ENTER(_("get_client_data_file"));
1483
1484 size = sl_strlen(DEFAULT_DATAROOT);
1485 base = SH_ALLOC(size + 1);
1486 sl_strlcpy(base, DEFAULT_DATAROOT, size + 1);
1487
1488 size = sl_strlen(base) + sl_strlen(peer) + 7;
1489
1490 ++size;
1491
1492 ret = SH_ALLOC(size);
1493 sl_strlcpy(ret, base, size);
1494 sl_strlcat(ret, _("/file."), size);
1495 sl_strlcat(ret, peer, size);
1496
1497 status = retry_stat (FIL__, __LINE__, ret, &buf);
1498
1499 if (status == 0)
1500 goto lab1_end;
1501 else
1502 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_ACCESS,
1503 (long) sh.effective.uid, ret);
1504
1505
1506 sl_strlcpy(ret, base, size);
1507 sl_strlcat(ret, _("/file"), size);
1508
1509 status = retry_stat (FIL__, __LINE__, ret, &buf);
1510
1511 if (status == 0)
1512 goto lab1_end;
1513 else
1514 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, status, MSG_E_ACCESS,
1515 (long) sh.effective.uid, ret);
1516
1517
1518 *length = 0;
1519 SH_FREE(base);
1520 SL_RETURN(NULL, _("get_client_data_file"));
1521
1522 lab1_end:
1523 if (buf.st_size > 0x7fffffff)
1524 {
1525 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGEN,
1526 _("File too large"), _("get_client_data_file"));
1527 SH_FREE(base);
1528 SL_RETURN(NULL, _("get_client_data_file"));
1529 }
1530 *length = (unsigned long) buf.st_size;
1531 SH_FREE(base);
1532 SL_RETURN(ret, _("get_client_data_file"));
1533
1534}
1535#endif
1536
1537#if defined(SH_WITH_CLIENT) || defined(SH_STEALTH) || defined(WITH_GPG) || defined(WITH_PGP)
1538
1539/* --------- secure temporary file ------------ */
1540
1541SL_TICKET open_tmp ()
1542{
1543 SL_TICKET fd;
1544 UINT32 ticks;
1545 char * file;
1546 struct stat buf;
1547 int error;
1548 int status = BAD;
1549 char * my_tmp_dir;
1550 int len;
1551
1552 SL_ENTER(_("open_tmp"));
1553
1554#if defined(SH_TMPDIR)
1555 len = sl_strlen(SH_TMPDIR) + 1;
1556 my_tmp_dir = SH_ALLOC(len);
1557 sl_strlcpy(my_tmp_dir, SH_TMPDIR, len);
1558#else
1559#if defined(SH_WITH_SERVER)
1560 len = sl_strlen(DEFAULT_LOGDIR) + 1;
1561 my_tmp_dir = SH_ALLOC(len);
1562 sl_strlcpy(my_tmp_dir, DEFAULT_LOGDIR, len);
1563#else
1564 len = sl_strlen(sh.effective.home) + 1;
1565 my_tmp_dir = SH_ALLOC(len);
1566 sl_strlcpy(my_tmp_dir, sh.effective.home, len);
1567#endif
1568#endif
1569
1570 if (0 != tf_trust_check (my_tmp_dir, SL_YESPRIV))
1571 {
1572 dlog(1, FIL__, __LINE__,
1573 _("The directory for temporary files: %s is untrusted, i.e. an\nuntrusted user owns or can write to some directory in the path.\n"),
1574 my_tmp_dir);
1575 sh_error_handle ((-1), FIL__, __LINE__, EACCES, MSG_TRUST,
1576 (long) sh.effective.uid,
1577 my_tmp_dir);
1578 SH_FREE(my_tmp_dir);
1579 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
1580 }
1581
1582 do {
1583
1584 /* create random filename in effective users home directory
1585 */
1586 ticks = taus_get (&(skey->rng0[0]), &(skey->rng1[0]), &(skey->rng2[0]));
1587 if (my_tmp_dir[0] == '/' && my_tmp_dir[1] == '\0')
1588 file = sh_util_strconcat (my_tmp_dir,
1589 sh_tiger_hash( (char *) &ticks, TIGER_DATA, 4),
1590 NULL);
1591 else
1592 file = sh_util_strconcat (my_tmp_dir,
1593 "/",
1594 sh_tiger_hash( (char *) &ticks, TIGER_DATA, 4),
1595 NULL);
1596
1597 /* check whether it already exists (paranoia)
1598 */
1599 errno = 0;
1600 status = retry_lstat(FIL__, __LINE__, file, &buf);
1601 error = errno;
1602
1603 if ( (status < 0) && (error == ENOENT) ) /* file does not exist */
1604 status = GOOD;
1605 else if (status < 0) /* unexpected error condition */
1606 {
1607 SH_FREE (file);
1608 SH_FREE(my_tmp_dir);
1609 TPT(( 0, FIL__, __LINE__, _("msg=<Unexpected error %d>\n"), error));
1610 SL_RETURN((-1), _("open_tmp"));
1611 }
1612 else /* file exists */
1613 {
1614 status = BAD;
1615 TPT(( 0, FIL__, __LINE__, _("msg=<Temporary file exists already>\n")));
1616 }
1617
1618 if (status == GOOD)
1619 {
1620 if (0 == tf_trust_check (file, SL_YESPRIV))
1621 status = GOOD;
1622 else
1623 {
1624 status = BAD;
1625 TPT(( 0, FIL__, __LINE__, _("msg=<Temporary file untrusted>\n")));
1626 }
1627 }
1628
1629 if (status == BAD)
1630 SH_FREE (file);
1631
1632 } while (status == BAD);
1633
1634 fd = sl_open_safe_rdwr (file, SL_YESPRIV);
1635 if (SL_ISERROR(fd))
1636 {
1637 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, fd, MSG_E_SUBGEN,
1638 _("Error opening temporary file"), _("open_tmp"));
1639 TPT(( 0, FIL__, __LINE__, _("msg=<Error %d temporary file %s>\n"),
1640 fd, file));
1641 }
1642
1643
1644 SH_FREE (file);
1645 SH_FREE(my_tmp_dir);
1646
1647 if (!SL_ISERROR(fd)) {
1648 sl_unlink(fd);
1649 }
1650
1651 if (!SL_ISERROR(fd))
1652 SL_RETURN((fd), _("open_tmp"));
1653 else
1654 SL_RETURN((-1), _("open_tmp"));
1655}
1656
1657
1658int close_tmp (SL_TICKET fd)
1659{
1660 SL_ENTER(_("close_tmp"));
1661
1662 if (SL_ISERROR(sl_close (fd)))
1663 SL_RETURN((-1), _("close_tmp"));
1664 SL_RETURN((0), _("close_tmp"));
1665}
1666
1667int rewind_tmp (SL_TICKET fd)
1668{
1669 SL_ENTER(_("rewind_tmp"));
1670
1671 if (SL_ISERROR(sl_rewind (fd)))
1672 SL_RETURN((-1), _("rewind_tmp"));
1673 SL_RETURN((0), _("rewind_tmp"));
1674}
1675#endif
Note: See TracBrowser for help on using the repository browser.