source: trunk/src/sh_forward.c@ 164

Last change on this file since 164 was 137, checked in by rainer, 17 years ago

Fix compile errors.

File size: 144.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#include <string.h>
23#include <stdio.h>
24#include <stdlib.h>
25
26
27/* Must be early on FreeBSD
28 */
29#include <sys/types.h>
30
31/* must be .le. than (1020 * 64)
32 * (see sh_tools.c -- put_header)
33 *
34 * also: must be (N * 16), otherwise
35 * binary files cannot be transferred encrypted
36 *
37 * 65280 = (1020*64)
38 * #define TRANS_BYTES 8000 V0.8
39 */
40#ifdef SH_ENCRYPT_2
41#define TRANS_BYTES 65120
42#else
43#define TRANS_BYTES 65280
44#endif
45
46/* timeout for session key
47 */
48#define TIMEOUT_KEY 7200
49
50/* max time between connection attempts
51 */
52#define TIMEOUT_CON 2048
53
54/* #undef SRP_DEBUG */
55/* #define SRP_DEBUG */
56
57#ifdef HAVE_MEMORY_H
58#include <memory.h>
59#endif
60
61#if TIME_WITH_SYS_TIME
62#include <sys/time.h>
63#include <time.h>
64#else
65#if HAVE_SYS_TIME_H
66#include <sys/time.h>
67#else
68#include <time.h>
69#endif
70#endif
71
72/*
73#ifdef TM_IN_SYS_TIME
74#include <sys/time.h>
75#else
76#include <time.h>
77#endif
78*/
79
80#ifdef HAVE_SYS_SELECT_H
81#include <sys/select.h>
82#endif
83
84#ifdef HAVE_UNISTD_H
85#include <errno.h>
86#include <signal.h>
87#include <setjmp.h>
88#include <pwd.h>
89#include <grp.h>
90#include <sys/stat.h>
91#include <sys/resource.h>
92#include <fcntl.h>
93#include <sys/wait.h>
94#include <unistd.h>
95#endif
96
97#ifndef FD_SET
98#define NFDBITS 32
99#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
100#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
101#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
102#endif /* !FD_SET */
103#ifndef FD_SETSIZE
104#define FD_SETSIZE 32
105#endif
106#ifndef FD_ZERO
107#define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
108#endif
109
110#if defined(HAVE_MLOCK) && !defined(HAVE_BROKEN_MLOCK)
111#include <sys/mman.h>
112#endif
113
114
115#include <netdb.h>
116#include <sys/types.h>
117#include <netinet/in.h>
118#include <sys/socket.h>
119#ifndef S_SPLINT_S
120#include <arpa/inet.h>
121#endif
122
123#include "samhain.h"
124#include "sh_tiger.h"
125#include "sh_utils.h"
126#include "sh_unix.h"
127#include "sh_forward.h"
128#include "sh_srp.h"
129#include "sh_fifo.h"
130#include "sh_tools.h"
131#include "sh_entropy.h"
132#include "sh_html.h"
133#include "sh_mail.h"
134#include "sh_socket.h"
135#define SH_NEED_GETHOSTBYXXX
136#include "sh_static.h"
137
138#ifdef SH_ENCRYPT
139#include "rijndael-api-fst.h"
140char * sh_tools_makePack (unsigned char * header,
141 char * payload, unsigned long payload_size,
142 keyInstance * keyInstE);
143char * sh_tools_revertPack (unsigned char * header, char * message,
144 keyInstance * keyInstE,
145 unsigned long message_size);
146#endif
147
148/* define this if you want to debug the client/server communication */
149/* #define SH_DBG_PROT 1 */
150
151#ifdef SH_DBG_PROT
152#define SH_SHOWPROT(c,d) sh_tools_show_header((c), (d))
153#else
154#define SH_SHOWPROT(c,d)
155#endif
156
157/* the port client will be connecting to
158 */
159#ifndef SH_DEFAULT_PORT
160#define SH_DEFAULT_PORT 49777
161#endif
162
163#ifndef SH_SELECT_REPEAT
164#define SH_SELECT_REPEAT 60
165#endif
166
167#ifndef SH_HEADER_SIZE
168#define SH_HEADER_SIZE 7
169#endif
170
171#ifndef SH_CHALLENGE_SIZE
172#define SH_CHALLENGE_SIZE 9
173#endif
174
175#undef FIL__
176#define FIL__ _("sh_forward.c")
177
178int clt_class = (-1);
179
180extern int flag_err_debug;
181extern int flag_err_info;
182
183#ifndef SH_STANDALONE
184
185#if defined(WITH_TRACE) || defined(WITH_TPT)
186char * hu_trans(const char * ihu)
187{
188 static char ohu[17];
189 sprintf(ohu, _("%c%03o"), '\\', /* known to fit */
190 (unsigned char) ihu[0]);
191 sprintf(&(ohu[4]), _("%c%03o"), '\\', /* known to fit */
192 (unsigned char) ihu[1]);
193 sprintf(&(ohu[8]), _("%c%03o"), '\\', /* known to fit */
194 (unsigned char) ihu[2]);
195 sprintf(&(ohu[12]), _("%c%03o"), '\\', /* known to fit */
196 (unsigned char) ihu[3]);
197 ohu[16] = '\0';
198 return ohu;
199}
200#endif
201
202static int StripDomain = S_TRUE;
203
204int sh_forward_set_strip (const char * str)
205{
206 static int fromcl = 0;
207 char dummy[2] = "F";
208
209 if (fromcl == 1)
210 return 0;
211
212 if (str == NULL)
213 {
214 fromcl = 1;
215 return (sh_util_flagval(dummy, &StripDomain));
216 }
217 else
218 return (sh_util_flagval(str, &StripDomain));
219}
220
221#include <ctype.h>
222
223const char * sh_strip_domain (char *name)
224{
225 char * first;
226 static char name_2[SH_MINIBUF+1];
227 register int i = 0;
228
229 SL_ENTER(_("sh_strip_domain"));
230
231 if (StripDomain == S_FALSE || (first = strchr(name, '.')) == NULL)
232 {
233 SL_RETURN( name, _("sh_strip_domain"));
234 }
235 else
236 {
237
238 /* check whether it is in dotted number format
239 * --> last part must be kept
240 */
241 if (0 != is_numeric(name))
242 {
243 SL_RETURN( name, _("sh_strip_domain"));
244 /*
245 i = sl_strlen(name) - 1;
246 while (name[i] != '.' && i >= 0)
247 --i;
248 if (name[i] == '.') ++i;
249 sl_strlcpy( name_2, &name[i], SH_MINIBUF +1 );
250 */
251 }
252 else
253 {
254 first = name;
255 while (i < SH_MINIBUF && *first != '.' && *first != '\0')
256 {
257 name_2[i] = *first;
258 ++first; ++i;
259 }
260 name_2[i] = '\0';
261 }
262 }
263
264 SL_RETURN( name_2, _("sh_strip_domain"));
265}
266
267/* #ifndef SH_STANDALONE */
268#endif
269
270#ifndef USE_SRP_PROTOCOL
271static
272void sh_passwd (char * salt, char * password, char * nounce, char *hash)
273{
274
275 char *combi;
276 size_t len;
277 register int i;
278 unsigned char * dez = NULL;
279 char hashbuf[KEYBUF_SIZE];
280
281 if (password == NULL)
282 dez = (unsigned char *) &(skey->pw[0]);
283 else if (sl_strlen(password) < PW_LEN)
284 {
285 fprintf(stderr, _("Password has less than %d chars !\n"),
286 PW_LEN);
287 _exit(EXIT_FAILURE);
288 }
289
290 if (password == NULL)
291 {
292 /* --- copy password ---
293 */
294 for (i = 0; i < PW_LEN; ++i)
295 {
296 skey->vernam[i] = (char)(*dez);
297 ++dez;
298 }
299 (void) sl_strlcpy (skey->vernam,
300 sh_tiger_hash(skey->vernam, TIGER_DATA, PW_LEN,
301 hashbuf, sizeof(hashbuf)),
302 KEY_LEN+1);
303 }
304 else
305 {
306 (void) sl_strlcpy (skey->vernam, password, KEY_LEN+1);
307 }
308
309 len = sl_strlen(salt) + 1;
310 if (sl_ok_adds(len, sl_strlen(skey->vernam)))
311 len += sl_strlen(skey->vernam);
312 if (nounce != NULL && sl_ok_adds(len, sl_strlen(nounce)))
313 len += sl_strlen(nounce);
314
315 /* H(s,P)
316 */
317 combi = SH_ALLOC(len);
318 (void) sl_strlcpy (combi, salt, len);
319 (void) sl_strlcat (combi, skey->vernam, len);
320 if (nounce != NULL)
321 (void) sl_strlcat (combi, nounce, len);
322 (void) sl_strlcpy (hash,
323 sh_tiger_hash(combi, TIGER_DATA,
324 (unsigned long) sl_strlen(combi),
325 hashbuf, sizeof(hashbuf)),
326 KEY_LEN+1);
327
328 /*
329 fprintf(stderr, "DD: A: <%s>\n", salt);
330 fprintf(stderr, "DD: P: <%s>\n", skey->pw);
331 fprintf(stderr, "DD: V: <%s>\n", skey->vernam);
332 fprintf(stderr, "DD: C: <%s>\n", combi);
333 fprintf(stderr, "DD: H: <%s>\n", hash);
334 */
335
336 SH_FREE (combi);
337 hash[KEY_LEN] = '\0';
338 return;
339}
340#endif
341
342#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
343
344static int count_dev_server = 0;
345
346void reset_count_dev_server(void)
347{
348 count_dev_server = 0;
349 return;
350}
351
352int sh_forward_setlogserver (const char * address)
353{
354 SL_ENTER(_("sh_forward_setlogserver"));
355
356 if (address != NULL && count_dev_server < 2
357 && sl_strlen(address) < SH_PATHBUF && sl_strlen(address) > 0)
358 {
359 if (count_dev_server == 0)
360 (void) sl_strlcpy (sh.srvexport.name, address, SH_PATHBUF);
361 else
362 (void) sl_strlcpy (sh.srvexport.alt, address, SH_PATHBUF);
363
364 ++count_dev_server;
365 SL_RETURN (0, _("sh_forward_setlogserver"));
366 }
367 SL_RETURN (-1, _("sh_forward_setlogserver"));
368}
369
370static
371int sh_forward_send_intern (int mysocket, char protocol, char * micro,
372 char * msgbuf, unsigned long length, int docrypt)
373{
374 unsigned long numbytes, countbytes;
375 int flag_err = 0;
376 unsigned char head[SH_HEADER_SIZE];
377 char * outbuf;
378
379#ifdef SH_ENCRYPT
380
381 unsigned long blkfac;
382 int rem;
383 unsigned long length2;
384 char * msg2buf = NULL;
385 char * p, * q;
386 RIJ_BYTE inBlock[B_SIZ];
387 RIJ_BYTE outBlock[B_SIZ];
388 unsigned long j;
389 cipherInstance cipherInst;
390 int err_num;
391 char expbuf[SH_ERRBUF_SIZE];
392#else
393 docrypt = SL_FALSE; /* dummy to fix compiler warning */
394#endif
395
396 SL_ENTER(_("sh_forward_send_intern"));
397
398#ifdef SH_ENCRYPT
399 if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_EN2) != (char)0))
400 {
401 put_header (head, (int)protocol, &length, micro);
402 msg2buf = sh_tools_makePack (head, msgbuf, length,
403 &(skey->keyInstE));
404 /*@-usedef@*/
405 length = (unsigned long) (256 * (unsigned int)head[1] +
406 (unsigned int)head[2]);
407 /*@+usedef@*/
408 outbuf = msg2buf;
409 }
410 else if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_ENC) != (char)0))
411 {
412 blkfac = length/B_SIZ;
413 rem = (int) (length - (B_SIZ * blkfac));
414 length2 = (B_SIZ * blkfac);
415 if ((rem > 0) && (length2+B_SIZ) > length2)
416 length2 += B_SIZ;
417 else
418 rem = 0;
419
420 msg2buf = SH_ALLOC((size_t)length2);
421 p = msgbuf;
422 q = msg2buf;
423
424 err_num = cipherInit (&cipherInst, (RIJ_BYTE)MODE_CBC, NULL);
425
426 if (err_num < 0)
427 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
428 errorExplain(err_num, expbuf, sizeof(expbuf)),
429 _("sh_forward_send_intern: cipherInit"));
430
431
432 for (j = 0; j < blkfac; ++j)
433 {
434 memcpy(inBlock, p, B_SIZ);
435 err_num = blockEncrypt(&cipherInst, &(skey->keyInstE),
436 inBlock, 128 * BNUM, outBlock);
437 if (err_num < 0)
438 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
439 errorExplain(err_num, expbuf, sizeof(expbuf)),
440 _("sh_forward_send_intern: blockEncrypt"));
441 memcpy(q, outBlock, B_SIZ);
442 p += B_SIZ;
443 q += B_SIZ;
444 }
445 if (rem > 0)
446 {
447 memset(inBlock, 0, B_SIZ);
448 memcpy(inBlock, p, (size_t)rem);
449 err_num = blockEncrypt(&cipherInst, &(skey->keyInstE),
450 inBlock, 128 * BNUM, outBlock);
451 if (err_num < 0)
452 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
453 errorExplain(err_num, expbuf, sizeof(expbuf)),
454 _("sh_forward_send_intern: blockEncrypt"));
455 memcpy(q, outBlock, B_SIZ);
456 q += B_SIZ;
457 }
458
459 outbuf = msg2buf;
460 length = length2;
461 put_header (head, (int)protocol, &length, micro);
462 }
463 else
464 {
465 outbuf = msgbuf;
466 put_header (head, (int)protocol, &length, micro);
467 }
468#else
469 outbuf = msgbuf;
470 put_header (head, (int)protocol, &length, micro);
471#endif
472
473 SH_SHOWPROT(head,'>');
474
475 numbytes = SH_HEADER_SIZE;
476 countbytes = write_port (mysocket,
477 (char *) head, numbytes,
478 &flag_err, 300);
479
480 if (countbytes == numbytes && outbuf != NULL)
481 {
482 numbytes = (length);
483 countbytes = write_port (mysocket,
484 outbuf, numbytes,
485 &flag_err, 300);
486 }
487
488#ifdef SH_ENCRYPT
489 /*@-usedef@*/
490 if (msg2buf != NULL)
491 SH_FREE(msg2buf);
492 /*@+usedef@*/
493#endif
494
495 if (countbytes == numbytes)
496 {
497 SL_RETURN( 0, _("sh_forward_send_intern"));
498 }
499 else
500 {
501 SL_RETURN( flag_err, _("sh_forward_send_intern"));
502 }
503}
504static
505int sh_forward_send (int mysocket, char protocol, char * micro,
506 char * msgbuf, unsigned long length)
507{
508 int i;
509 SL_ENTER(_("sh_forward_send"));
510 TPT(( 0, FIL__, __LINE__, _("msg=<Send.>\n")));
511 i = sh_forward_send_intern (mysocket, protocol, micro,
512 msgbuf, length, S_FALSE);
513 SL_RETURN(i, _("sh_forward_send"));
514}
515static
516int sh_forward_send_crypt (int mysocket, char protocol, char * micro,
517 char * msgbuf, unsigned long length)
518{
519 int i;
520 SL_ENTER(_("sh_forward_send_crypt"));
521#ifdef SH_ENCRYPT
522 TPT(( 0, FIL__, __LINE__, _("msg=<Send encrypted.>\n")));
523#else
524 TPT(( 0, FIL__, __LINE__, _("msg=<Send.>\n")));
525#endif
526 i = sh_forward_send_intern (mysocket, protocol, micro,
527 msgbuf, length, S_TRUE);
528 SL_RETURN(i, _("sh_forward_send_crypt"));
529}
530
531
532/* receive answer, add a trailing NULL to terminate string
533 * rev 0.8
534 */
535static
536long sh_forward_receive_intern (int mysocket, char protocol, char * micro,
537 char * msgbuf, unsigned long length,
538 int docrypt)
539{
540 unsigned long numbytes, countbytes;
541 int flag_err = -1;
542 unsigned char head[SH_HEADER_SIZE];
543
544#ifdef SH_ENCRYPT
545
546 unsigned long head_length;
547 unsigned long blkfac;
548 /* unsigned long length2; */
549 char * p, * q, * tmp;
550 RIJ_BYTE inBlock[B_SIZ];
551 RIJ_BYTE outBlock[B_SIZ];
552 unsigned long j;
553 cipherInstance cipherInst;
554 int err_num;
555 char expbuf[SH_ERRBUF_SIZE];
556#else
557 docrypt = SL_FALSE; /* dummy to fix compiler warning */
558#endif
559
560 SL_ENTER(_("sh_forward_receive_intern"));
561
562#ifdef SH_ENCRYPT
563 /* make sure length is not multiple of B_SIZ, see below
564 */
565 ASSERT_RET((length % B_SIZ != 0), _("length % 16 != 0"), flag_err);
566#endif
567
568 if (micro != NULL)
569 micro[4] = '\0';
570
571 if (msgbuf != NULL)
572 msgbuf[0] = '\0';
573
574 numbytes = SH_HEADER_SIZE;
575 countbytes = read_port (mysocket,
576 (char *) head, numbytes,
577 &flag_err, 300);
578
579 if (countbytes != numbytes)
580 {
581 TPT(( 0, FIL__, __LINE__, _("msg=<countbytes != numbytes>\n")));
582 SL_RETURN(flag_err, _("sh_forward_receive_intern"));
583 }
584 /*@-usedef +ignoresigns@*/
585 else if (head[0] != protocol &&
586 (head[0] & SH_PROTO_SRP) == (char)0 /* not set */)
587 {
588 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISMATCH);
589 SL_RETURN((-1), _("sh_forward_receive_intern"));
590 }
591 /*@+usedef -ignoresigns@*/
592 else
593 {
594 get_header (head, &numbytes, micro);
595 SH_SHOWPROT(head, '<');
596
597 if (numbytes > 0)
598 {
599 numbytes = (numbytes > length ? length : numbytes);
600
601 countbytes = read_port (mysocket,
602 msgbuf, numbytes,
603 &flag_err, 300);
604
605 if (countbytes < length)
606 msgbuf[countbytes] = '\0';
607 else
608 msgbuf[length-1] = '\0';
609
610 if (flag_err != 0)
611 {
612 TPT(( 0, FIL__, __LINE__, _("msg=<read error>\n")));
613 SL_RETURN((-1), _("sh_forward_receive_intern"));
614 }
615 }
616 }
617
618#ifdef SH_ENCRYPT
619 if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_EN2) != (char)0))
620 {
621 tmp = SH_ALLOC((size_t)length);
622 memcpy(tmp, msgbuf, (size_t)length);
623 tmp = sh_tools_revertPack (head, tmp, &(skey->keyInstD), countbytes);
624
625 head_length = (unsigned long) (256 * (unsigned int)head[1] +
626 (unsigned int)head[2]);
627
628 /*
629 * revertPack returns header with length <= (original_length-16), so
630 * the following msgbuf[length] = '\0' is always safe.
631 * Nevertheless, check for proper length.
632 */
633 if (head_length <= (length-1))
634 length = head_length;
635 else
636 --length;
637
638 memcpy(msgbuf, tmp, (size_t)length);
639 msgbuf[length] = '\0';
640 SH_FREE(tmp);
641 if (countbytes == numbytes)
642 {
643 countbytes = length; /* to avoid error on return, see below */
644 }
645 numbytes = length;
646 }
647 else if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_ENC) != (char)0))
648 {
649 /* Decrypt only complete blocks. If there is an incomplete block,
650 * something is wrong anyway.
651 * Decrypt in place.
652 */
653 blkfac = countbytes/B_SIZ;
654
655 p = msgbuf;
656 q = msgbuf;
657
658 err_num = cipherInit (&cipherInst, (RIJ_BYTE)MODE_CBC, NULL);
659
660 if (err_num < 0)
661 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
662 errorExplain(err_num, expbuf, sizeof(expbuf)),
663 _("sh_forward_receive_intern: cipherInit"));
664
665 /* here we want to have (length % B_SIZ != 0), such that the
666 * terminating '\0' cannot be overwritten
667 */
668 for (j = 0; j < blkfac; ++j)
669 {
670 memcpy(inBlock, p, B_SIZ);
671 err_num = blockDecrypt(&cipherInst, &(skey->keyInstD),
672 inBlock, 128 * BNUM, outBlock);
673 if (err_num < 0)
674 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
675 errorExplain(err_num, expbuf, sizeof(expbuf)),
676 _("sh_forward_receive_intern: blockDecrypt"));
677 memcpy(q, outBlock, B_SIZ);
678 p += B_SIZ;
679 q += B_SIZ;
680 }
681 }
682#endif
683
684 if (countbytes == numbytes)
685 {
686 SL_RETURN(((long)numbytes), _("sh_forward_receive_intern"));
687 }
688 else
689 {
690 TPT(( 0, FIL__, __LINE__, _("msg=<short read>\n")));
691 SL_RETURN(flag_err, _("sh_forward_receive_intern"));
692 }
693}
694
695static
696long sh_forward_receive (int mysocket, char protocol, char * micro,
697 char * msgbuf, unsigned long length)
698{
699 long i;
700 SL_ENTER(_("sh_forward_receive"));
701 TPT(( 0, FIL__, __LINE__, _("msg=<Receive.>\n")));
702 i = sh_forward_receive_intern (mysocket, protocol, micro,
703 msgbuf, length, S_FALSE);
704 SL_RETURN(i, _("sh_forward_receive"));
705}
706
707static
708long sh_forward_receive_crypt (int mysocket, char protocol, char * micro,
709 char * msgbuf, unsigned long length)
710{
711 long i;
712 SL_ENTER(_("sh_forward_receive_crypt"));
713#ifdef SH_ENCRYPT
714 TPT(( 0, FIL__, __LINE__, _("msg=<Receive encrypted.>\n")));
715#else
716 TPT(( 0, FIL__, __LINE__, _("msg=<Receive.>\n")));
717#endif
718 i = sh_forward_receive_intern (mysocket, protocol, micro,
719 msgbuf, length, S_TRUE);
720 SL_RETURN(i, _("sh_forward_receive"));
721}
722
723/**************************************************
724 *
725 *
726 * C L I E N T
727 *
728 *
729 ***************************************************/
730
731
732#include <time.h>
733
734static SH_FIFO * fifo = NULL;
735
736static long sh_forward_try (char * errmsg);
737
738static unsigned int ServerPort = SH_DEFAULT_PORT;
739
740int sh_forward_server_port (const char * str)
741{
742 unsigned long l;
743 char * endptr;
744
745 SL_ENTER(_("sh_forward_server_port"));
746
747 l = strtoul (str, &endptr, 0);
748 if (l > 65535 || endptr == str)
749 {
750 SL_RETURN (-1, _("sh_forward_server_port"));
751 }
752 ServerPort = (unsigned int) l;
753 SL_RETURN (0, _("sh_forward_server_port"));
754}
755
756long sh_forward (char * errmsg)
757{
758 static int have_server = GOOD;
759 long status;
760 char * popmsg;
761 static int failed = GOOD;
762
763 SL_ENTER(_("sh_forward"));
764
765 /* --- No log server available. ---
766 */
767 if (have_server == GOOD && sh.srvexport.name[0] == '\0')
768 {
769 have_server = BAD;
770 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NONAME);
771 SL_RETURN (-1, _("sh_forward"));
772 }
773 else if (have_server == BAD)
774 {
775 SL_RETURN (-1, _("sh_forward"));
776 }
777
778 /* --- Allocate fifo. ---
779 */
780 if (fifo == NULL)
781 {
782 fifo = SH_ALLOC(sizeof(SH_FIFO));
783 fifo_init(fifo);
784 }
785
786 /* --- Check for messages on the queue, and send them first. ---
787 */
788 while (NULL != (popmsg = pop_list(fifo)) )
789 {
790 status = sh_forward_try (popmsg);
791 if (status != 0)
792 {
793 (void) push_tail_list (fifo, popmsg);
794 SH_FREE(popmsg);
795 if (SH_FIFO_MAX == push_list (fifo, errmsg))
796 {
797 SL_RETURN (-2, _("sh_forward"));
798 }
799 SL_RETURN (-1, _("sh_forward"));
800 }
801 SH_FREE(popmsg);
802 }
803
804 /* --- Now send the error message. ---
805 */
806 status = sh_forward_try (errmsg);
807 if (status != 0)
808 {
809 if (failed == GOOD)
810 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_SRV_FAIL,
811 _("log server"),
812 sh.srvexport.name);
813 failed = BAD;
814 if (SH_FIFO_MAX == push_list (fifo, errmsg)) /* push message on stack */
815 {
816 SL_RETURN (-2, _("sh_forward"));
817 }
818 SL_RETURN (-1, _("sh_forward"));
819 }
820
821 failed = GOOD;
822 SL_RETURN (0, _("sh_forward"));
823}
824
825static long sh_forward_try_impl (char * errmsg, char what);
826
827static long sh_forward_try (char * errmsg)
828{
829 long i;
830 SL_ENTER(_("sh_forward_try"));
831 i = sh_forward_try_impl (errmsg, (char)SH_PROTO_MSG);
832 SL_RETURN(i, _("sh_forward_try"));
833}
834
835long sh_forward_req_file (char * file)
836{
837 long i;
838 char tmp_file[8];
839 SL_ENTER(_("sh_forward_req_file"));
840 (void) sl_strlcpy(tmp_file, file, 8);
841 i = sh_forward_try_impl (tmp_file, (char)SH_PROTO_BIG);
842 SL_RETURN(i, _("sh_forward_req_file"));
843}
844
845static long sh_forward_try_impl (char * errmsg, char what)
846{
847 static int initialized = BAD;
848 static int conn_state = GOOD;
849 int sockfd;
850 int flag_err;
851 char * answer;
852
853 unsigned char theProto;
854
855 char hash[KEY_LEN+1];
856 size_t len;
857 char * buffer;
858#ifdef SH_ENCRYPT_2
859 size_t pos; /* for the server command */
860#endif
861 char head_u[5];
862
863 char nsrv[KEY_LEN+1];
864 char nclt[KEY_LEN+1];
865 SL_TICKET sfd = -1;
866 int transfercount;
867
868 char foo_M1[KEY_LEN+1];
869 UINT32 ticks;
870
871 char error_msg[256];
872 char error_call[SH_MINIBUF];
873 int error_num = 0;
874
875#ifdef USE_SRP_PROTOCOL
876 char u_real[SH_CHALLENGE_SIZE];
877 char * foo_A;
878 char * foo_Sc;
879 char * M;
880#else
881 char nounce[KEY_LEN+1];
882 char temp[2*KEY_LEN+1];
883 char nonce_u[KEY_LEN+1];
884#endif
885
886#ifdef SH_ENCRYPT
887 int err_num;
888 char expbuf[SH_ERRBUF_SIZE];
889#endif
890
891 static time_t time_now = 1200;
892 static time_t time_last = 0;
893
894 static time_t timeout_val = 1;
895 char hashbuf[KEYBUF_SIZE];
896 char sigbuf[KEYBUF_SIZE];
897
898 SL_ENTER(_("sh_forward_try_impl"));
899
900 /* --- No message to transmit. ---
901 */
902 if (errmsg == NULL && initialized == GOOD)
903 SL_RETURN( 0, _("sh_forward_try_impl"));
904
905 /* --- Connection in bad state. ---
906 */
907 if (initialized == BAD || conn_state == BAD)
908 {
909 timeout_val =
910 (time_t)((timeout_val > TIMEOUT_CON) ? TIMEOUT_CON : timeout_val);
911
912 /* --- Retry bad attempt only after some time. ---
913 */
914 time_now = time (NULL);
915 if ((time_now - time_last) < timeout_val)
916 {
917 TPT(( 0, FIL__, __LINE__, _("msg=<Within deadtime, no retry.>\n")));
918 SL_RETURN( (-1), _("sh_forward_try_impl"));
919 }
920 TPT(( 0, FIL__, __LINE__, _("msg=<Retry.>\n")));
921 }
922 time_last = time (NULL);
923
924
925 /* --- Try to connect to log server. ---
926 */
927 error_call[0] = '\0';
928
929 sockfd = connect_port_2 (sh.srvexport.name, sh.srvexport.alt,
930 ServerPort,
931 error_call, &error_num, error_msg, 256);
932
933 if (sockfd < 0)
934 {
935 conn_state = BAD;
936 timeout_val *= 2;
937 sh_error_handle ((-1), FIL__, __LINE__, error_num,
938 MSG_E_NET, error_msg, error_call,
939 _("export"), sh.srvexport.name);
940 SL_RETURN( (-1), _("sh_forward_try_impl"));
941 }
942
943 conn_state = GOOD;
944
945 /*************************
946 *
947 * initialization
948 *
949 */
950
951 flag_err = 0;
952 answer = SH_ALLOC(512);
953 MLOCK(answer, 512);
954
955
956#ifndef USE_SRP_PROTOCOL
957
958 /**************************************************
959 *
960 * --- challenge/response authentication ---
961 *
962 **************************************************/
963
964 if (initialized == BAD)
965 {
966 theProto = (unsigned char) SH_PROTO_SRP;
967
968 TPT(( 0, FIL__, __LINE__, _("msg=<c/r: entry>\n")));
969
970 (void) sl_strlcpy (answer, sh.host.name, 512);
971
972 flag_err = sh_forward_send (sockfd, (char) theProto, _("SALT"),
973 answer, (unsigned long)sl_strlen(answer));
974
975 TPT(( 0, FIL__, __LINE__, _("msg=<c/r: sent SALT, flag_err = %d>\n"),
976 flag_err));
977
978 /* get nonce from server
979 */
980 if (flag_err == 0)
981 {
982 flag_err = (int) sh_forward_receive (sockfd, (char)theProto, head_u,
983 answer, 511);
984 flag_err = (flag_err < 0) ? flag_err : 0;
985 TPT(( 0, FIL__, __LINE__,
986 _("msg=<c/r: rcvt nonce, flag_err = %d>\n"),
987 flag_err));
988 }
989
990 /* entry point for jump from message forward if session key must
991 * be re-initialized
992 */
993 initBlock:
994
995 if (0 == check_request (head_u, _("INIT")) &&
996 flag_err == 0 &&
997 sl_strlen(answer) > KEY_LEN )
998 (void) sl_strlcpy(nounce, &answer[KEY_LEN], KEY_LEN+1);
999 else
1000 flag_err = (-1);
1001
1002 TPT(( 0, FIL__, __LINE__, _("msg=<c/r: rcvt INIT, flag_err = %d>\n"),
1003 flag_err));
1004
1005 /* verify random nonce v from server H(v, P)v
1006 */
1007 sh_passwd (nounce, NULL, NULL, temp);
1008 if ( 0 != sl_strncmp(temp, answer, KEY_LEN))
1009 flag_err = (-1);
1010
1011 TPT(( 0, FIL__, __LINE__, _("msg=<c/r: vrfy nonce, flag_err = %d>\n"),
1012 flag_err));
1013
1014
1015 /* --- Create own nonce. ---
1016 */
1017 ticks = (UINT32) taus_get ();
1018
1019 (void) sl_strlcpy(nonce_u,
1020 sh_tiger_hash((char *) &ticks,
1021 TIGER_DATA,
1022 (unsigned long)sizeof(UINT32),
1023 hashbuf, sizeof(hashbuf)),
1024 KEY_LEN+1);
1025
1026 /* --- Form the message H(H(u,v),P)u ---
1027 */
1028 (void) sl_strlcpy(temp, nonce_u, 2*KEY_LEN+1);
1029 (void) sl_strlcat(temp, nounce, 2*KEY_LEN+1);
1030 (void) sl_strlcpy(temp,
1031 sh_tiger_hash(temp,
1032 TIGER_DATA,
1033 (unsigned long)sl_strlen(temp),
1034 hashbuf, sizeof(hashbuf)),
1035 KEY_LEN+1);
1036 sh_passwd (temp, NULL, NULL, foo_M1);
1037 (void) sl_strlcpy(temp, foo_M1, 2*KEY_LEN+1);
1038 (void) sl_strlcat(temp, nonce_u, 2*KEY_LEN+1);
1039
1040 /* --- Send it to server. ---
1041 */
1042 if (flag_err == 0)
1043 {
1044 flag_err = (int) sh_forward_send (sockfd,
1045 (char)(theProto|SH_PROTO_SRP),
1046 _("PASS"), temp,
1047 (unsigned long)sl_strlen(temp));
1048 TPT(( 0, FIL__, __LINE__, _("msg=<c/r: sent PASS, flag_err = %d>\n"),
1049 flag_err));
1050 }
1051
1052 if (flag_err == 0)
1053 {
1054 flag_err = (int)sh_forward_receive (sockfd,
1055 (char)(theProto|SH_PROTO_SRP),
1056 head_u, answer, 511);
1057 sh_passwd (nounce, NULL, nonce_u, foo_M1);
1058 (void) sl_strlcpy (skey->session, foo_M1, KEY_LEN+1);
1059#ifdef SH_ENCRYPT
1060 err_num = makeKey(&(skey->keyInstE),
1061 (RIJ_BYTE)DIR_ENCRYPT, 192, skey->session);
1062 if (err_num < 0)
1063 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
1064 errorExplain(err_num, expbuf, sizeof(expbuf)),
1065 _("sh_forward_try_impl: makeKey"));
1066
1067 err_num = makeKey(&(skey->keyInstD),
1068 (RIJ_BYTE)DIR_DECRYPT, 192, skey->session);
1069 if (err_num < 0)
1070 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
1071 errorExplain(err_num, expbuf, sizeof(expbuf)),
1072 _("sh_forward_try_impl: make_key"));
1073#endif
1074 initialized = GOOD;
1075 }
1076
1077 if (initialized == BAD)
1078 {
1079 timeout_val *= 2;
1080 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOAUTH);
1081 memset(answer, 0, 512);
1082 MUNLOCK(answer, 512);
1083 SH_FREE(answer);
1084 SL_RETURN( (-1), _("sh_forward_try_impl"));
1085 }
1086 else
1087 {
1088 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_AUTH);
1089 }
1090 }
1091
1092#else
1093
1094
1095 /* This is the SRP authenticated key exchange protocol.
1096 * Produces a session key skey->session.
1097 */
1098 if (initialized == BAD)
1099 {
1100 TPT(( 0, FIL__, __LINE__, _("msg=<srp: entry>\n")));
1101
1102 theProto = SH_PROTO_SRP;
1103
1104 sl_strlcpy (answer, sh.host.name, 512);
1105 flag_err = sh_forward_send (sockfd, theProto, _("SALT "),
1106 answer, sl_strlen(answer));
1107
1108 TPT(( 0, FIL__, __LINE__, _("msg=<srp: sent SALT, flag_err = %d>\n"),
1109 flag_err));
1110
1111 if (flag_err == 0)
1112 {
1113 flag_err = sh_forward_receive (sockfd, theProto, head_u,
1114 answer, 511);
1115 flag_err = (flag_err < 0) ? flag_err : 0;
1116 TPT(( 0, FIL__, __LINE__,
1117 _("msg=<srp: rcvt nonce, flag_err = %d>\n"),
1118 flag_err));
1119 }
1120
1121 /* Entry point for jump from message forward if session key must
1122 * be re-initialized.
1123 */
1124 initBlock:
1125 TPT(( 0, FIL__, __LINE__, _("msg=<srp: INIT>\n")));
1126
1127 if (flag_err == 0 &&
1128 (0 == check_request (head_u, _("INIT"))))
1129 {
1130 if (0 != sh_srp_init())
1131 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_EBGN);
1132 else /* if (0 == sh_srp_init()) */
1133 {
1134 TPT(( 0, FIL__, __LINE__, _("msg=<srp: bignum initialized>\n")));
1135
1136 sh_srp_x (answer, NULL); /* x password */
1137 sh_srp_make_a (); /* a random number */
1138 foo_A = sh_srp_A(); /* g^a */
1139
1140 TPT(( 0, FIL__, __LINE__, _("msg=<srp: A = %s>\n"), foo_A));
1141
1142 if (foo_A == NULL)
1143 flag_err = (-1);
1144
1145 if (flag_err == 0)
1146 flag_err = sh_forward_send (sockfd,
1147 (theProto|SH_PROTO_SRP),
1148 _("PC01"),
1149 foo_A, sl_strlen(foo_A)+1);
1150 if (flag_err == 0)
1151 {
1152 flag_err = sh_forward_receive (sockfd,
1153 (theProto|SH_PROTO_SRP),
1154 head_u,
1155 answer, 511);
1156 flag_err = (flag_err < 0) ? flag_err : 0;
1157 TPT(( 0, FIL__, __LINE__, _("msg=<srp: B = %s>\n"), answer));
1158 TPT(( 0, FIL__, __LINE__, _("msg=<srp: u = %03o-%03o-%03o-%03o>\n"), head_u[0], head_u[1], head_u[2], head_u[3]));
1159 }
1160
1161 /* u nounce */
1162 /* B answer */
1163 /* S = (B-g^x)^(a+ux) */
1164
1165 if (flag_err == 0)
1166 {
1167 if (0 != sh_srp_check_zero (answer))
1168 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_EZERO);
1169 else /* if (0 != sh_srp_check_zero (answer)) */
1170 {
1171 sl_strlcpy(u_real, sh_tiger_hash(head_u, TIGER_DATA, 4,
1172 hashbuf, sizeof(hashbuf)),
1173 SH_CHALLENGE_SIZE);
1174 foo_Sc = sh_srp_S_c (u_real, answer);
1175
1176 TPT(( 0, FIL__, __LINE__, _("msg=<srp: U = %s>\n"),
1177 u_real));
1178 TPT(( 0, FIL__, __LINE__, _("msg=<srp:Sc = %s>\n"),
1179 foo_Sc));
1180
1181 /* --- Now send H(A,B,H(Sc)) and check. ---
1182 */
1183 if (foo_Sc != NULL)
1184 {
1185 sh_srp_M(foo_A,
1186 answer,
1187 sh_tiger_hash(foo_Sc,
1188 TIGER_DATA,
1189 sl_strlen(foo_Sc),
1190 hashbuf, sizeof(hashbuf)),
1191 foo_M1, KEY_LEN+1);
1192
1193
1194 TPT(( 0, FIL__, __LINE__, _("msg=<srp:M1 = %s>\n"),
1195 foo_M1));
1196
1197 flag_err = sh_forward_send(sockfd,
1198 (theProto|SH_PROTO_SRP),
1199 _("PC02"),
1200 foo_M1, KEY_LEN+1);
1201 }
1202 else
1203 {
1204 flag_err = (-1);
1205 }
1206
1207 if (flag_err == 0)
1208 {
1209 flag_err =sh_forward_receive(sockfd,
1210 (theProto|SH_PROTO_SRP),
1211 head_u,
1212 answer, 511);
1213 flag_err = (flag_err < 0) ? flag_err : 0;
1214 TPT(( 0, FIL__, __LINE__, _("msg=<srp: M = %s>\n"),
1215 answer));
1216 }
1217
1218 if (flag_err == 0 &&
1219 (0 == check_request (head_u, _("PARP"))))
1220 {
1221 /* ------ verify M2 = H(A, M1, K) --------
1222 */
1223 char M_buf[KEY_LEN+1];
1224 M = sh_srp_M (foo_A, foo_M1,
1225 sh_tiger_hash(foo_Sc,
1226 TIGER_DATA,
1227 sl_strlen(foo_Sc),
1228 hashbuf, sizeof(hashbuf)),
1229 M_buf, sizeof(M_buf)
1230 );
1231 if (M != NULL &&
1232 0 == sl_strncmp (answer, M, KEY_LEN+1))
1233 {
1234 sl_strlcpy (skey->session,
1235 sh_tiger_hash(foo_Sc,
1236 TIGER_DATA,
1237 sl_strlen(foo_Sc),
1238 hashbuf, sizeof(hashbuf)),
1239 KEY_LEN+1);
1240 TPT(( 0, FIL__, __LINE__,
1241 _("msg=<srp: Key = %s>\n"),
1242 skey->session));
1243
1244#ifdef SH_ENCRYPT
1245 err_num = makeKey(&(skey->keyInstE),
1246 DIR_ENCRYPT,
1247 192, skey->session);
1248 if (err_num < 0)
1249 sh_error_handle((-1), FIL__, __LINE__, -1,
1250 MSG_E_SUBGEN,
1251 errorExplain(err_num, expbuf, sizeof(expbuf)),
1252 _("sh_forward_try_impl: makeKey"));
1253 err_num = makeKey(&(skey->keyInstD),
1254 DIR_DECRYPT,
1255 192, skey->session);
1256 if (err_num < 0)
1257 sh_error_handle((-1), FIL__, __LINE__, -1,
1258 MSG_E_SUBGEN,
1259 errorExplain(err_num, expbuf, sizeof(expbuf)),
1260 _("sh_forward_try_impl: makeKey"));
1261#endif
1262 initialized = GOOD;
1263 }
1264 }
1265 if (foo_Sc != NULL)
1266 SH_FREE(foo_Sc);
1267 }
1268 }
1269 if (foo_A != NULL)
1270 SH_FREE(foo_A);
1271 sh_srp_exit();
1272 }
1273 }
1274
1275 if (initialized == BAD)
1276 {
1277 timeout_val *= 2;
1278 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOAUTH);
1279 memset(answer, '\0', 512);
1280 MUNLOCK(answer, 512);
1281 SH_FREE(answer);
1282 SL_RETURN( (-1), _("sh_forward_try_impl"));
1283 }
1284 else
1285 {
1286 if (flag_err_info == SL_TRUE)
1287 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_AUTH);
1288 }
1289 }
1290
1291#endif
1292
1293 /* no message, just session key negotiated
1294 */
1295 if (errmsg == NULL)
1296 {
1297 timeout_val = 1;
1298 memset(answer, 0, 512);
1299 MUNLOCK(answer, 512);
1300 SH_FREE(answer);
1301 TPT(( 0, FIL__, __LINE__, _("msg=<No message.>\n")));
1302 SL_RETURN( (0), _("sh_forward_try_impl"));
1303 }
1304 else if (what == (char)SH_PROTO_BIG)
1305 {
1306 MUNLOCK(answer, 512);
1307 SH_FREE (answer);
1308 answer = SH_ALLOC(TRANS_BYTES + 256);
1309 MLOCK(answer, TRANS_BYTES + 256);
1310 TPT(( 0, FIL__, __LINE__, _("msg=<File transfer.>\n")));
1311 }
1312
1313
1314 (void) sl_strlcpy (answer, sh_util_siggen(skey->session,
1315 sh.host.name,
1316 sl_strlen(sh.host.name),
1317 sigbuf, sizeof(sigbuf)),
1318 KEY_LEN+1);
1319 TPT((0, FIL__, __LINE__, _("msg=<host %s>\n"), sh.host.name));
1320 TPT((0, FIL__, __LINE__, _("msg=<ckey %s>\n"), skey->session));
1321 TPT((0, FIL__, __LINE__, _("msg=<sign %s>\n"), answer));
1322
1323
1324 (void) sl_strlcat (answer, sh.host.name, 512);
1325
1326 TPT((0, FIL__, __LINE__, _("msg=<mesg %s>\n"), answer));
1327
1328 /***********************************************
1329 *
1330 * send the message
1331 *
1332 */
1333
1334 if (what == (char) SH_PROTO_MSG)
1335 {
1336 theProto = (unsigned char)SH_PROTO_MSG;
1337
1338 /* say HELO
1339 */
1340
1341 flag_err = sh_forward_send (sockfd,
1342 (char)theProto, _("HELO"),
1343 answer,
1344 (unsigned long)sl_strlen(answer));
1345 TPT(( 0, FIL__, __LINE__, _("msg=<Sent %s, status %d.>\n"),
1346 answer, flag_err));
1347 if (flag_err == 0)
1348 {
1349 /* --- Get challenge. ---
1350 */
1351 flag_err = (int) sh_forward_receive (sockfd,
1352 (char)SH_PROTO_MSG, head_u,
1353 answer, 255);
1354 TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s, u %s, status %d.>\n"),
1355 answer, hu_trans(head_u), flag_err));
1356 flag_err = (flag_err < 0) ? flag_err : 0;
1357
1358 if (flag_err == 0)
1359 {
1360
1361 /* --- Re-negotiate key. ---
1362 */
1363 if (0 == check_request_nerr(head_u, _("INIT")))
1364 {
1365 flag_err = 0;
1366 initialized = BAD;
1367 goto initBlock;
1368 }
1369
1370 else if (0 == check_request(head_u, _("TALK")))
1371 {
1372
1373 /* --- Save the challenge. ---
1374 */
1375 (void) sl_strlcpy(nsrv, answer, KEY_LEN + 1);
1376
1377 /* --- Hash(msg,challenge,sessionkey). ---
1378 */
1379 len = sl_strlen(errmsg) + sl_strlen(answer)
1380 + KEY_LEN + 1;
1381 len = (size_t)((len < 256) ? 256 : len);
1382 buffer = SH_ALLOC(len);
1383 MLOCK(buffer, len);
1384 (void) sl_strlcpy(buffer, errmsg, len);
1385 (void) sl_strlcat(buffer, answer, len);
1386 (void) sl_strlcpy(hash,
1387 sh_util_siggen (skey->session,
1388 buffer,
1389 sl_strlen(buffer),
1390 sigbuf, sizeof(sigbuf)),
1391 KEY_LEN+1);
1392 TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
1393 sh_util_siggen(skey->session, buffer,
1394 sl_strlen(buffer), sigbuf, sizeof(sigbuf))));
1395
1396 (void) sl_strlcpy(buffer, errmsg, len);
1397 (void) sl_strlcat(buffer, hash, len);
1398
1399 flag_err =
1400 sh_forward_send_crypt (sockfd,
1401#ifdef SH_ENCRYPT
1402#ifdef SH_ENCRYPT_2
1403 (char)(SH_PROTO_MSG|SH_PROTO_ENC|SH_PROTO_EN2),
1404#else
1405 (char)(SH_PROTO_MSG|SH_PROTO_ENC),
1406#endif
1407#else
1408 (char)(SH_PROTO_MSG),
1409#endif
1410 _("MESG"),
1411 buffer,
1412 (unsigned long)(sl_strlen(buffer)+1));
1413 TPT(( 0, FIL__, __LINE__,
1414 _("msg=<Sent %s, status %d.>\n"),
1415 answer, flag_err));
1416
1417 /* --- Get confirmation. ---
1418 */
1419 if (flag_err == 0)
1420 {
1421 flag_err = (int)
1422 sh_forward_receive_crypt (sockfd,
1423#ifdef SH_ENCRYPT
1424#ifdef SH_ENCRYPT_2
1425 (char)(SH_PROTO_MSG|SH_PROTO_ENC|SH_PROTO_EN2|SH_PROTO_END),
1426#else
1427 (char)(SH_PROTO_MSG|SH_PROTO_ENC|SH_PROTO_END),
1428#endif
1429#else
1430 (char)(SH_PROTO_MSG|SH_PROTO_END),
1431#endif
1432 head_u,
1433 answer, 255);
1434 TPT(( 0, FIL__, __LINE__,
1435 _("msg=<Rcvt %s, u %s, status %d.>\n"),
1436 answer, hu_trans(head_u), flag_err));
1437 flag_err = (flag_err < 0) ? flag_err : 0;
1438 }
1439
1440
1441 /* --- Check confirmation. ---
1442 */
1443 if (flag_err == 0)
1444 {
1445 /* CLIENT CONF RECV
1446 *
1447 * first KEY_LEN bytes must be
1448 * sig(skey->session (errmsg nsrv))
1449 *
1450 */
1451 (void) sl_strlcpy(buffer, errmsg, len);
1452 (void) sl_strlcat(buffer, nsrv, len);
1453 flag_err = sl_strncmp(answer,
1454 sh_util_siggen(skey->session,
1455 buffer,
1456 sl_strlen(buffer),
1457 sigbuf, sizeof(sigbuf)),
1458 KEY_LEN);
1459 TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
1460 sh_util_siggen(skey->session, buffer,
1461 sl_strlen(buffer), sigbuf, sizeof(sigbuf))));
1462
1463 if (flag_err != 0)
1464 {
1465#ifdef ENOMSG
1466 flag_err = ENOMSG;
1467#else
1468 flag_err = EIO;
1469#endif
1470 sh_error_handle((-1), FIL__, __LINE__, flag_err,
1471 MSG_TCP_NOCONF);
1472 }
1473 else
1474 {
1475#ifdef SH_ENCRYPT_2
1476 /* --- SERVER CMD --- */
1477 if (answer[KEY_LEN] != '\0' &&
1478 sl_strlen(answer) > (2*KEY_LEN))
1479 {
1480 pos = sl_strlen(answer) - (2*KEY_LEN);
1481 /*
1482 * buffer is >= 256
1483 * answer has <= 255 bytes
1484 */
1485 (void) sl_strlcpy(buffer, &answer[KEY_LEN],
1486 pos+1);
1487 flag_err =
1488 sl_strncmp(&answer[KEY_LEN+pos],
1489 sh_util_siggen(skey->session,
1490 buffer,
1491 pos,
1492 sigbuf, sizeof(sigbuf)),
1493 KEY_LEN);
1494
1495 TPT((0, FIL__, __LINE__,
1496 _("CONF RECV <%d> <%s>\n"),
1497 flag_err, &answer[KEY_LEN]));
1498
1499 if (flag_err != 0) {
1500 sh_error_handle((-1), FIL__, __LINE__,
1501 flag_err,
1502 MSG_TCP_NOCONF);
1503 }
1504#ifdef SH_WITH_CLIENT
1505 else {
1506 sh_socket_server_cmd(buffer);
1507 }
1508#endif
1509 flag_err = 0;
1510
1511 } else {
1512
1513 TPT((0, FIL__, __LINE__,
1514 _("CONF RECV <0> <[null]>\n")));
1515
1516 }
1517 /* --- SERVER CMD END --- */
1518#endif
1519 if (flag_err_debug == SL_TRUE)
1520 sh_error_handle((-1), FIL__, __LINE__, 0,
1521 MSG_TCP_CONF);
1522 }
1523 }
1524
1525 memset(buffer, 0, len);
1526 MUNLOCK(buffer, len);
1527 SH_FREE(buffer);
1528 }
1529 else
1530 {
1531 /* --- Unexpected reply from server. ---
1532 */
1533 sh_error_handle((-1), FIL__, __LINE__, 0,
1534 MSG_TCP_UNEXP);
1535 flag_err = (-1);
1536 }
1537 }
1538 }
1539 }
1540
1541
1542 else if (what == (char)SH_PROTO_BIG)
1543 {
1544 theProto = (unsigned char) SH_PROTO_BIG;
1545
1546 /* --- Say HELO ---
1547 */
1548 flag_err = sh_forward_send (sockfd, (char) theProto, _("HELO"),
1549 answer, (unsigned long)sl_strlen(answer));
1550 TPT(( 0, FIL__, __LINE__, _("msg=<Sent %s, status %d.>\n"),
1551 answer, flag_err));
1552
1553 if (flag_err == 0)
1554 {
1555 /* --- Get NSRV. ---
1556 */
1557 flag_err = (int) sh_forward_receive (sockfd,
1558 (char)SH_PROTO_BIG, head_u,
1559 answer, 255);
1560 TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s, u %s, status %d.>\n"),
1561 answer, hu_trans(head_u), flag_err));
1562 flag_err = (flag_err < 0) ? flag_err : 0;
1563 }
1564
1565 if (flag_err == 0)
1566 {
1567
1568 /* --- Re-negotiate key. ---
1569 */
1570 if (0 == check_request_nerr(head_u, _("INIT")))
1571 {
1572 flag_err = 0;
1573 initialized = BAD;
1574 goto initBlock;
1575 }
1576
1577
1578 else if (0 == check_request(head_u, _("NSRV")))
1579 {
1580#ifdef SH_ENCRYPT
1581 /* --- Set encryption flag. ---
1582 */
1583#ifdef SH_ENCRYPT_2
1584 theProto =
1585 (unsigned char)(SH_PROTO_BIG | SH_PROTO_ENC | SH_PROTO_EN2);
1586#else
1587 theProto = (unsigned char)(SH_PROTO_BIG | SH_PROTO_ENC);
1588#endif
1589#endif
1590
1591 (void) sl_strlcpy(nsrv, answer, KEY_LEN+1);
1592
1593 /* --- Generate a nonce. ---
1594 */
1595 ticks = (UINT32) taus_get ();
1596
1597 (void) sl_strlcpy(nclt,
1598 sh_tiger_hash((char *) &ticks,
1599 TIGER_DATA,
1600 (unsigned long)sizeof(UINT32),
1601 hashbuf, sizeof(hashbuf)),
1602 KEY_LEN+1);
1603
1604 /* --- Compute H(nsrv, nclt, skey). ---
1605 */
1606 buffer = sh_util_strconcat (nsrv, nclt,
1607 skey->session, NULL);
1608 (void)sl_strlcpy(foo_M1,
1609 sh_tiger_hash(buffer, TIGER_DATA,
1610 (unsigned long)sl_strlen(buffer),
1611 hashbuf, sizeof(hashbuf)),
1612 KEY_LEN+1);
1613 memset (buffer, 0, sl_strlen(buffer));
1614
1615 /* --- Send (nclt, msg) ---
1616 */
1617 (void) sl_strlcpy(buffer, nclt, KEY_LEN+1);
1618 (void) sl_strlcat(buffer, errmsg, KEY_LEN+5);
1619
1620#ifndef SH_ENCRYPT
1621 buffer[KEY_LEN+4] = theProto;
1622 buffer[KEY_LEN+5] = '\0';
1623 sh_tools_hash_add(foo_M1, buffer, KEY_LEN+5);
1624#endif
1625
1626 flag_err =
1627 sh_forward_send_crypt (sockfd, (char) theProto, _("NCLT"),
1628 buffer,
1629 (unsigned long) sl_strlen(buffer));
1630
1631 TPT(( 0, FIL__, __LINE__, _("msg=<Sent %s, status %d.>\n"),
1632 buffer, flag_err));
1633 SH_FREE (buffer);
1634 }
1635 }
1636
1637 if (flag_err == 0)
1638 {
1639 /* --- Receive the file. ---
1640 */
1641
1642 /* --- Open a temporary file. ---
1643 */
1644
1645 if ( (sfd = open_tmp ()) < 0)
1646 {
1647 flag_err = (-1);
1648 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_EFIL);
1649 }
1650 else
1651 {
1652 /* --- Read from socket into tmp file. ---
1653 */
1654 transfercount = 0;
1655 flag_err = 0;
1656
1657 do {
1658 flag_err = (int)
1659 sh_forward_receive_crypt (sockfd,
1660#ifdef SH_ENCRYPT
1661#ifdef SH_ENCRYPT_2
1662 (char)(SH_PROTO_BIG|SH_PROTO_EN2|SH_PROTO_ENC),
1663#else
1664 (char)(SH_PROTO_BIG|SH_PROTO_ENC),
1665#endif
1666#else
1667 (char)(SH_PROTO_BIG),
1668#endif
1669 head_u,
1670 answer,
1671 TRANS_BYTES + 255);
1672
1673 TPT(( 0, FIL__, __LINE__,
1674 _("msg=<Received: %d bytes, marked %s.>\n"),
1675 flag_err, hu_trans(head_u)));
1676
1677 if (flag_err > 0 && 0 == check_request_nerr(head_u, _("FILE")))
1678 {
1679 if (0 == hash_check (foo_M1, answer, flag_err))
1680 {
1681 (void) sl_write(sfd, &answer[KEY_LEN],
1682 flag_err-KEY_LEN);
1683 ++transfercount;
1684 flag_err =
1685 sh_forward_send_crypt (sockfd, (char) theProto,
1686 _("RECV"),
1687 nclt,
1688 (unsigned long)sl_strlen(nclt));
1689
1690 }
1691 else
1692 {
1693 TPT(( 0, FIL__, __LINE__,
1694 _("msg=<File transfer: Hash check failed.>\n")));
1695 break;
1696 }
1697 }
1698 else
1699 {
1700 TPT(( 0, FIL__, __LINE__,
1701 _("msg=<File transfer: No more data.>\n")));
1702 break;
1703 }
1704 } while (transfercount < 32000); /* 64 Mbyte */
1705
1706 if (0 == check_request_nerr(head_u, _("EEOT")) &&
1707 0 < flag_err &&
1708 0 == hash_check (foo_M1, answer, (int)strlen(answer)))
1709 {
1710 flag_err =
1711 sh_forward_send_crypt (sockfd, (char) theProto,
1712 _("EOTE"),
1713 nclt,
1714 (unsigned int) sl_strlen(nclt));
1715
1716 (void) rewind_tmp (sfd);
1717 (void) sl_sync(sfd);
1718 if (flag_err_info == SL_TRUE)
1719 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FOK);
1720 }
1721 else
1722 {
1723 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FBAD);
1724 (void) sl_close (sfd);
1725 sfd = (-1);
1726 }
1727
1728 (void) close (sockfd);
1729 memset(answer, 0, TRANS_BYTES + 256);
1730 MUNLOCK(answer, TRANS_BYTES + 256);
1731 SH_FREE(answer);
1732 timeout_val = 1;
1733
1734 SL_RETURN( (sfd), _("sh_forward_try_impl"));
1735 }
1736 }
1737
1738 (void) close (sockfd);
1739 memset(answer, 0, TRANS_BYTES + 256);
1740 MUNLOCK(answer, TRANS_BYTES + 256);
1741 SH_FREE(answer);
1742 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FBAD);
1743 timeout_val *= 2;
1744
1745 SL_RETURN( (-1), _("sh_forward_try_impl"));
1746 }
1747
1748
1749
1750 (void) close (sockfd);
1751 memset(answer, 0, 512);
1752 MUNLOCK(answer, 512);
1753 SH_FREE(answer);
1754
1755#ifndef EIO
1756#define EIO 5
1757#endif
1758
1759
1760#ifdef SH_ERROR_H
1761 if (flag_err != 0)
1762 {
1763 char errbuf[SH_ERRBUF_SIZE];
1764 conn_state = BAD;
1765 timeout_val *= 2;
1766 if (flag_err < 0 || NULL == sh_error_message(flag_err, errbuf, sizeof(errbuf)))
1767 flag_err = EIO;
1768 sh_error_handle((-1), FIL__, __LINE__, flag_err, MSG_TCP_ECONN,
1769 sh_error_message(flag_err, errbuf, sizeof(errbuf)));
1770 SL_RETURN( (-1), _("sh_forward_try_impl"));
1771 }
1772#endif
1773 timeout_val = 1;
1774
1775 SL_RETURN( (0), _("sh_forward_try_impl"));
1776}
1777
1778/* #ifdef SH_WITH_CLIENT */
1779#endif
1780
1781
1782#if defined (SH_WITH_SERVER)
1783
1784#ifndef USE_SRP_PROTOCOL
1785
1786int sh_forward_make_client (const char * str)
1787{
1788 /* char * safer; */
1789 char key[KEY_LEN+1];
1790 unsigned char in[PW_LEN+1];
1791 int i = 0, j, k, l = 0;
1792 char hashbuf[KEYBUF_SIZE];
1793
1794 if (sl_strlen(str) != (PW_LEN * 2))
1795 {
1796 fprintf(stderr,
1797 _("Input must be a %d digit hexadecimal number"\
1798 " (only 0-9, a-f, A-F allowed in input)\n"),
1799 (PW_LEN * 2));
1800 _exit(EXIT_FAILURE);
1801 }
1802
1803 while (i < (PW_LEN * 2))
1804 {
1805 k = sh_util_hexchar(str[i]); j = sh_util_hexchar(str[i+1]);
1806 if (k != -1 && j != -1)
1807 {
1808 in[l] = (k * 16 + j);
1809 ++l; i+= 2;
1810 }
1811 else
1812 {
1813 fprintf(stderr, _("Invalid char %c\n"), str[i]);
1814 _exit(EXIT_FAILURE);
1815 }
1816 }
1817 in[PW_LEN] = '\0';
1818
1819 sl_strlcpy ((char *)key,
1820 sh_tiger_hash ((char*)in, TIGER_DATA, PW_LEN,
1821 hashbuf, sizeof(hashbuf)),
1822 KEY_LEN+1);
1823 key[KEY_LEN] = '\0';
1824
1825 fprintf(stdout, _("Client entry: Client=HOSTNAME@00000000@%s\n"),
1826 key);
1827 fflush(stdout);
1828
1829 _exit(EXIT_SUCCESS);
1830 return 0;
1831}
1832
1833#else
1834
1835int sh_forward_make_client (const char * str)
1836{
1837 char * foo_v;
1838
1839 char salt[17];
1840 char key[KEY_LEN+1];
1841 char in[PW_LEN];
1842 int i = 0, j, k, l = 0;
1843 char hashbuf[KEYBUF_SIZE];
1844
1845 if (sl_strlen(str) != (PW_LEN*2))
1846 {
1847 fprintf(stderr,
1848 _("Input must be a %d digit hexadecimal number"\
1849 " (only 0-9, a-f, A-F allowed in input)\n"),
1850 (PW_LEN*2));
1851 _exit(EXIT_FAILURE);
1852 }
1853
1854 while (i < (PW_LEN*2))
1855 {
1856 k = sh_util_hexchar(str[i]); j = sh_util_hexchar(str[i+1]);
1857 if (k != -1 && j != -1)
1858 {
1859 in[l] = (k * 16 + j);
1860 ++l; i+= 2;
1861 }
1862 else
1863 {
1864 fprintf(stderr, _("Invalid char %c\n"), str[i]);
1865 _exit(EXIT_FAILURE);
1866 }
1867 }
1868
1869
1870 if (0 == sh_srp_init())
1871 {
1872 sh_util_keyinit(key, KEY_LEN);
1873 sl_strlcpy(salt, sh_tiger_hash(key, TIGER_DATA, KEY_LEN,
1874 hashbuf, sizeof(hashbuf)),
1875 17);
1876 sh_srp_x (salt, in);
1877 foo_v = sh_srp_verifier ();
1878 fprintf(stdout, _("Client=HOSTNAME@%s@%s\n"),
1879 salt, foo_v);
1880 fflush(stdout);
1881 SH_FREE(foo_v);
1882 sh_srp_exit();
1883 _exit(EXIT_SUCCESS);
1884 }
1885 fprintf(stdout, _("ERROR initializing BigNum library.\n"));
1886 fflush (stdout);
1887 _exit(EXIT_FAILURE);
1888 return -1;
1889}
1890#endif
1891
1892
1893int sh_forward_create_password (const char * dummy)
1894{
1895 UINT32 val[2];
1896 char output[KEY_LEN+1];
1897 char hashbuf[KEYBUF_SIZE];
1898
1899 val[0] = taus_get ();
1900 val[1] = taus_get ();
1901
1902 sl_strlcpy (output,
1903 sh_tiger_hash((char *)(&val[0]), TIGER_DATA, 2*sizeof(UINT32),
1904 hashbuf, sizeof(hashbuf)),
1905 KEY_LEN);
1906
1907 output[16] = '\0';
1908
1909 fprintf(stdout, _("%s\n"), output);
1910 fflush (stdout);
1911
1912 if (dummy)
1913 _exit(EXIT_SUCCESS);
1914 else
1915 _exit(EXIT_SUCCESS);
1916 return (0); /* avoid compiler warning */
1917}
1918
1919/* #if defined (SH_WITH_SERVER) */
1920#endif
1921
1922/**************************************************
1923 *
1924 *
1925 * S E R V E R
1926 *
1927 *
1928 ***************************************************/
1929
1930#ifdef SH_WITH_SERVER
1931
1932#include "sh_readconf.h"
1933
1934
1935#define CONN_FREE 0
1936#define CONN_READING 1
1937#define CONN_SENDING 2
1938#define CONN_PAUSE 3
1939#define CONN_BUSY 4
1940
1941char * clt_stat[] = {
1942 N_("Inactive"),
1943 N_("Started"),
1944 N_("ILLEGAL"),
1945 N_("FAILED"),
1946 N_("Exited"),
1947 N_("PANIC"),
1948 N_("POLICY"),
1949 N_("File_transfer"),
1950 N_("Message"),
1951 N_("TIMEOUT_EXCEEDED"),
1952 N_("Suspended"),
1953 N_("Filecheck"),
1954};
1955
1956#include <time.h>
1957
1958/* in sh_html.h:
1959 * typedef struct client_entry {
1960 * } client_t;
1961 */
1962
1963#include "zAVLTree.h"
1964
1965/* Function to return the key for indexing
1966 * the argument
1967 */
1968zAVLKey sh_avl_key (void const * arg)
1969{
1970 const client_t * sa = (const client_t *) arg;
1971 return (zAVLKey) sa->hostname;
1972}
1973
1974zAVLTree * all_clients = NULL;
1975
1976void sh_forward_html_write()
1977{
1978 SL_ENTER(_("sh_forward_html_write"));
1979 sh_html_write(all_clients);
1980 SL_RET0(_("sh_forward_html_write"));
1981}
1982
1983
1984int sh_forward_use_clt_class (const char * c)
1985{
1986 int i;
1987 SL_ENTER(_("sh_forward_use_clt_class"));
1988 i = sh_util_flagval(c, &(sh.flag.client_class));
1989 SL_RETURN(i, _("sh_forward_use_clt_class"));
1990}
1991
1992int sh_forward_use_clt_sev (const char * c)
1993{
1994 int i;
1995 SL_ENTER(_("sh_forward_use_clt_sev"));
1996 i = sh_util_flagval(c, &(sh.flag.client_severity));
1997 SL_RETURN(i, _("sh_forward_use_clt_sev"));
1998}
1999
2000
2001/* the destructor
2002 */
2003void free_client(void * inptr)
2004{
2005 client_t * here;
2006
2007 SL_ENTER(_("free_client"));
2008 if (inptr == NULL)
2009 SL_RET0(_("free_client"));
2010 else
2011 here = (client_t *) inptr;
2012
2013 if (here->hostname != NULL)
2014 SH_FREE(here->hostname);
2015 if (here->salt != NULL)
2016 SH_FREE(here->salt);
2017 if (here->verifier != NULL)
2018 SH_FREE(here->verifier);
2019 SH_FREE(here);
2020 SL_RET0(_("free_client"));
2021}
2022
2023
2024int sh_forward_register_client (const char * str)
2025{
2026 client_t * newclt;
2027 client_t * testclt;
2028
2029 const char * ptr;
2030 int sepnum = 0;
2031 int sep[2];
2032 register int i = 0;
2033 int siz_str = 0;
2034
2035 SL_ENTER(_("sh_forward_register_client"));
2036
2037 ptr = str;
2038 while (*ptr) {
2039 if (*ptr == '@' && sepnum < 2 )
2040 {
2041 sep[sepnum] = i;
2042 ++sepnum;
2043 }
2044 ++ptr; ++i;
2045 }
2046
2047 if (all_clients == NULL)
2048 {
2049 all_clients = zAVLAllocTree (sh_avl_key);
2050 if (all_clients == NULL)
2051 {
2052 (void) safe_logger (0, 0, NULL);
2053 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
2054 }
2055 }
2056
2057 if ((sepnum == 2) && (sep[0] > 0) && (sep[1] > sep[0]))
2058 {
2059 newclt = SH_ALLOC (sizeof(client_t));
2060 newclt->hostname = SH_ALLOC (sep[0]+1);
2061 newclt->salt = SH_ALLOC (sep[1]-sep[0]);
2062 newclt->verifier = SH_ALLOC (sl_strlen(str)-sep[1]+1);
2063 newclt->exit_flag = 0;
2064 newclt->dead_flag = 0;
2065#ifdef SH_ENCRYPT
2066#ifdef SH_ENCRYPT_2
2067 newclt->encf_flag = SH_PROTO_ENC|SH_PROTO_EN2;
2068 newclt->ency_flag = SH_PROTO_ENC|SH_PROTO_EN2;
2069#else
2070 newclt->encf_flag = SH_PROTO_ENC;
2071 newclt->ency_flag = SH_PROTO_ENC;
2072#endif
2073#else
2074 newclt->encf_flag = 0;
2075 newclt->ency_flag = 0;
2076#endif
2077 newclt->session_key[0] = '\0';
2078 newclt->last_connect = (time_t) 0;
2079 newclt->session_key_timer = (time_t) 0;
2080 newclt->status_now = CLT_INACTIVE;
2081 for (i = 0; i < CLT_MAX; ++i)
2082 newclt->status_arr[i] = CLT_INACTIVE;
2083 (void) sh_unix_time(0, newclt->timestamp[CLT_INACTIVE], TIM_MAX);
2084 /* truncate */
2085 sl_strlcpy(newclt->hostname, &str[0], sep[0]+1);
2086 /* truncate */
2087 sl_strlcpy(newclt->salt, &str[sep[0]+1], sep[1]-sep[0]);
2088 sl_strlcpy(newclt->verifier, &str[sep[1]+1], sl_strlen(str)-sep[1]+1);
2089
2090 testclt = (client_t *) zAVLSearch (all_clients, newclt->hostname);
2091
2092 if (testclt != NULL)
2093 {
2094 SH_FREE(testclt->verifier);
2095 siz_str = strlen (newclt->verifier) + 1;
2096 testclt->verifier = SH_ALLOC (siz_str);
2097 sl_strlcpy(testclt->verifier, newclt->verifier, siz_str);
2098
2099 SH_FREE(testclt->salt);
2100 siz_str = strlen (newclt->salt) + 1;
2101 testclt->salt = SH_ALLOC (siz_str);
2102 sl_strlcpy(testclt->salt, newclt->salt, siz_str);
2103
2104 testclt->dead_flag = 0;
2105
2106 free_client(newclt);
2107 SL_RETURN( 0, _("sh_forward_register_client"));
2108 }
2109 else
2110 {
2111 if (0 == zAVLInsert (all_clients, newclt))
2112 {
2113 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_CREG,
2114 newclt->hostname,
2115 newclt->salt, newclt->verifier);
2116 SL_RETURN( 0, _("sh_forward_register_client"));
2117 }
2118 }
2119 }
2120 SL_RETURN (-1, _("sh_forward_register_client"));
2121}
2122
2123typedef struct {
2124 int state;
2125 int fd;
2126 char * buf;
2127 unsigned char head[SH_HEADER_SIZE];
2128 char challenge[SH_CHALLENGE_SIZE];
2129 char peer[SH_MINIBUF+1];
2130 client_t * client_entry;
2131 char * K;
2132 char * M1;
2133 char * A;
2134 int headcount;
2135 unsigned long bytecount;
2136 unsigned long bytes_to_send;
2137 unsigned long bytes_to_get;
2138 int pass;
2139 unsigned long timer;
2140
2141 char * FileName;
2142 unsigned long FileLength;
2143 unsigned long FileSent;
2144 char FileType[5];
2145
2146 struct sockaddr_in addr_peer;
2147} sh_conn_t;
2148
2149
2150static char zap_challenge[SH_CHALLENGE_SIZE] = { 0 };
2151
2152void sh_forward_do_free (sh_conn_t * conn)
2153{
2154 SL_ENTER(_("sh_forward_do_free"));
2155
2156 if (conn->K != NULL)
2157 {
2158 SH_FREE(conn->K);
2159 conn->K = NULL;
2160 }
2161 if (conn->A != NULL)
2162 {
2163 SH_FREE(conn->A);
2164 conn->A = NULL;
2165 }
2166 if (conn->M1 != NULL)
2167 {
2168 SH_FREE(conn->M1);
2169 conn->M1 = NULL;
2170 }
2171 if (conn->buf != NULL)
2172 {
2173 SH_FREE(conn->buf);
2174 conn->buf = NULL;
2175 }
2176 if (conn->fd != (-1))
2177 {
2178 close (conn->fd);
2179 conn->fd = -1;
2180 }
2181 memcpy(conn->challenge, zap_challenge, SH_CHALLENGE_SIZE);
2182 conn->state = CONN_FREE;
2183 conn->headcount = 0;
2184 conn->bytecount = 0;
2185 conn->bytes_to_send = 0;
2186 conn->bytes_to_get = 0;
2187 conn->pass = 0;
2188 conn->timer = 0;
2189 conn->client_entry = NULL;
2190
2191 if (conn->FileName != NULL)
2192 {
2193 SH_FREE(conn->FileName);
2194 conn->FileName = NULL;
2195 }
2196 conn->FileLength = 0;
2197 conn->FileSent = 0;
2198 conn->FileType[0] = '\0';
2199 conn->FileType[1] = '\0';
2200 conn->FileType[2] = '\0';
2201 conn->FileType[3] = '\0';
2202 conn->FileType[4] = '\0';
2203
2204 --server_status.conn_open;
2205
2206 SL_RET0(_("sh_forward_do_free"));
2207}
2208
2209/****************************************
2210 *
2211 * -- Reconfiguration. --
2212 *
2213 * (1) Mark all clients as 'dead'.
2214 * (2) Reload configuration - clients
2215 * in config are non-dead now.
2216 * (3) Remove all clients still
2217 * marked as 'dead'.
2218 */
2219
2220/* -- Mark all clients as dead.
2221 */
2222void sh_forward_mark_dead ()
2223{
2224 zAVLCursor avlcursor;
2225 client_t * item;
2226
2227 SL_ENTER(_("sh_forward_mark_dead"));
2228
2229 for (item = (client_t *) zAVLFirst(&avlcursor, all_clients); item;
2230 item = (client_t *) zAVLNext(&avlcursor))
2231 {
2232 item->dead_flag = 1;
2233 }
2234 SL_RET0(_("sh_forward_mark_dead"));
2235}
2236
2237
2238/* -- Clean tree from dead clients.
2239 */
2240void sh_forward_clean_tree ()
2241{
2242 zAVLCursor avlcursor;
2243 client_t * item;
2244
2245 SL_ENTER(_("sh_forward_clean_tree"));
2246
2247 repeat_search:
2248
2249 for (item = (client_t *) zAVLFirst(&avlcursor, all_clients); item;
2250 item = (client_t *) zAVLNext(&avlcursor))
2251 {
2252 if (item->dead_flag == 1)
2253 {
2254 zAVLDelete (all_clients, item->hostname);
2255 free_client (item);
2256 goto repeat_search;
2257 }
2258 }
2259 SL_RET0(_("sh_forward_clean_tree"));
2260}
2261
2262/*
2263 *
2264 **********************************************/
2265
2266
2267
2268/* -- SERVER SEND FUNKTION. --
2269 */
2270void sh_forward_prep_send_int (sh_conn_t * conn,
2271 char * msg, unsigned long length,
2272 char * u, char protocol,
2273 int docrypt)
2274{
2275 /* register unsigned long i; */
2276 unsigned long length2;
2277
2278#ifdef SH_ENCRYPT
2279 unsigned long blkfac = 0;
2280 int rem = 0;
2281 char * p, * q;
2282 RIJ_BYTE inBlock[B_SIZ];
2283 RIJ_BYTE outBlock[B_SIZ];
2284 unsigned int j;
2285 cipherInstance cipherInst;
2286 int err_num;
2287 char expbuf[SH_ERRBUF_SIZE];
2288#else
2289 (void) docrypt;
2290#endif
2291
2292 SL_ENTER(_("sh_forward_prep_send_int"));
2293
2294 TPT((0, FIL__, __LINE__, _("msg=<%s>, docrypt=<%d>\n"), msg, docrypt ));
2295
2296#ifdef SH_ENCRYPT
2297 if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_EN2) != 0) )
2298 {
2299 length2 = length;
2300 }
2301 else if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_ENC) != 0) )
2302 {
2303 blkfac = length/B_SIZ;
2304 rem = length - (B_SIZ * blkfac);
2305 length2 = (B_SIZ * blkfac);
2306 if (rem > 0 && (length2 + B_SIZ) > length2)
2307 length2 += B_SIZ;
2308 else
2309 rem = 0;
2310 }
2311 else
2312 {
2313 length2 = length;
2314 }
2315#else
2316 length2 = length;
2317#endif
2318
2319 conn->headcount = 0;
2320 conn->bytecount = 0;
2321 conn->bytes_to_send = 0;
2322 conn->bytes_to_get = 0;
2323
2324 if (conn->buf != NULL)
2325 {
2326 SH_FREE(conn->buf);
2327 conn->buf = NULL;
2328 }
2329
2330
2331 put_header (conn->head, protocol, &length2, u);
2332 SH_SHOWPROT(conn->head,'>');
2333
2334 TPT((0, FIL__, __LINE__, _("msg=<put_header done>\n") ));
2335
2336 if (msg == NULL)
2337 length2 = 0;
2338
2339#ifdef SH_ENCRYPT
2340 if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_EN2) != 0))
2341 {
2342 TPT((0, FIL__, __LINE__, _("encrypting (version 2)\n")));
2343
2344 conn->buf = sh_tools_makePack (conn->head, msg, length2,
2345 &(conn->client_entry->keyInstE));
2346 }
2347 else if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_ENC) != 0) &&
2348 ((length2 + 1) > length2))
2349 {
2350 conn->buf = SH_ALLOC(length2 + 1);
2351
2352 p = msg;
2353 q = conn->buf;
2354
2355 TPT((0, FIL__, __LINE__, _("encrypting (version 1)\n")));
2356
2357 err_num = cipherInit (&cipherInst, MODE_CBC, NULL);
2358 if (err_num < 0)
2359 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
2360 errorExplain(err_num, expbuf, sizeof(expbuf)),
2361 _("sh_forward_prep_send_int: cipherInit"));
2362
2363 for (j = 0; j < blkfac; ++j)
2364 {
2365 memcpy(inBlock, p, B_SIZ);
2366 err_num = blockEncrypt(&cipherInst, &(conn->client_entry->keyInstE),
2367 inBlock, 128 * BNUM, outBlock);
2368 if (err_num < 0)
2369 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
2370 errorExplain(err_num, expbuf, sizeof(expbuf)),
2371 _("sh_forward_prep_send_int: blockEncrypt"));
2372 memcpy(q, outBlock, B_SIZ);
2373 p += B_SIZ;
2374 q += B_SIZ;
2375 }
2376 if (rem > 0)
2377 {
2378 /* incomplete block at end
2379 */
2380 memset(inBlock, '\0', B_SIZ);
2381 memcpy(inBlock, p, rem);
2382 err_num = blockEncrypt(&cipherInst, &(conn->client_entry->keyInstE),
2383 inBlock, 128 * BNUM, outBlock);
2384 if (err_num < 0)
2385 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
2386 errorExplain(err_num, expbuf, sizeof(expbuf)),
2387 _("sh_forward_prep_send_int: blockEncrypt"));
2388 memcpy(q, outBlock, B_SIZ);
2389 q += B_SIZ;
2390 }
2391
2392 TPT((0, FIL__, __LINE__, _("msg=<encryption done>\n") ));
2393 }
2394 else
2395 {
2396 if ((length2 + 1) < length2) --length2;
2397 conn->buf = SH_ALLOC(length2 + 1);
2398
2399 memcpy(conn->buf, msg, length2);
2400 /*
2401 for (i = 0; i < length2; ++i)
2402 conn->buf[i] = msg[i];
2403 */
2404 conn->buf[length2] = '\0';
2405 TPT((0, FIL__, __LINE__, _("msg=<no encryption done>\n") ));
2406 }
2407#else
2408 if ((length2 + 1) < length2) --length2;
2409 conn->buf = SH_ALLOC(length2 + 1);
2410
2411 memcpy(conn->buf, msg, length2);
2412 /*
2413 for (i = 0; i < length; ++i)
2414 conn->buf[i] = msg[i];
2415 */
2416 conn->buf[length2] = '\0';
2417 TPT((0, FIL__, __LINE__, _("msg=<no encryption done>\n") ));
2418#endif
2419
2420 conn->state = CONN_SENDING;
2421 SL_RET0(_("sh_forward_prep_send_int"));
2422}
2423
2424/* -- Send/Receive. --
2425 */
2426void sh_forward_prep_send (sh_conn_t * conn,
2427 char * msg, unsigned long length,
2428 char * u, char protocol)
2429{
2430 SL_ENTER(_("sh_forward_prep_send"));
2431 sh_forward_prep_send_int (conn, msg, length, u, protocol, S_FALSE);
2432 SL_RET0(_("sh_forward_prep_send"));
2433}
2434
2435void sh_forward_prep_send_crypt (sh_conn_t * conn,
2436 char * msg, unsigned long length,
2437 char * u, char protocol)
2438{
2439 SL_ENTER(_("sh_forward_prep_send_crypt"));
2440 sh_forward_prep_send_int (conn, msg, length, u, protocol, S_TRUE);
2441 SL_RET0(_("sh_forward_prep_send_crypt"));
2442}
2443
2444/* #include <sys/times.h> */
2445
2446#if defined(WITH_EXTERNAL)
2447#include "sh_extern.h"
2448#endif
2449
2450/* -- Update the client status. --
2451 *
2452 * Update the status array for the client,
2453 * and eventually call external program.
2454 */
2455static void status_update (client_t * conn, int status)
2456{
2457#if defined(WITH_EXTERNAL)
2458 char msg[2 * SH_MINIBUF + TIM_MAX + 3];
2459#endif
2460
2461 SL_ENTER(_("status_update"));
2462
2463 if (conn == NULL ||
2464 status < 0 || status >= CLT_MAX)
2465 SL_RET0(_("status_update"));
2466
2467 conn->status_now = status;
2468 conn->status_arr[status] = status;
2469 (void) sh_unix_time(0, conn->timestamp[status], TIM_MAX);
2470
2471#if defined(WITH_EXTERNAL)
2472 sl_snprintf(msg, sizeof(msg), _("%s %s %s"),
2473 conn->hostname, conn->timestamp[status], _(clt_stat[status]));
2474 sh_ext_execute('s', 'r', 'v', msg, 0);
2475#endif
2476
2477 SL_RET0(_("status_update"));
2478}
2479
2480static time_t time_client_limit = 86400;
2481
2482int sh_forward_set_time_limit (const char * c)
2483{
2484 long val;
2485
2486 SL_ENTER(_("sh_forward_set_time_limit"));
2487
2488 val = strtol (c, (char **)NULL, 10);
2489 if (val <= 0)
2490 SL_RETURN( (-1), _("sh_forward_set_time_limit"));
2491
2492 val = (val < 0 ? 0 : val);
2493
2494 time_client_limit = (time_t) val;
2495 SL_RETURN( (0), _("sh_forward_set_time_limit"));
2496}
2497
2498
2499/* -- Check for time limit exceeded. --
2500 */
2501static int client_time_check()
2502{
2503 zAVLCursor avlcursor;
2504 client_t * item;
2505
2506 SL_ENTER(_("client_time_check"));
2507
2508 if (time_client_limit == (time_t) 0)
2509 SL_RETURN( 0, _("client_time_check"));
2510
2511 for (item = (client_t *) zAVLFirst(&avlcursor, all_clients); item;
2512 item = (client_t *) zAVLNext(&avlcursor))
2513 {
2514 if (item->exit_flag == 0 && item->last_connect != (time_t) 0)
2515 {
2516 if ( (time(NULL) - item->last_connect) > time_client_limit)
2517 {
2518 if (item->status_now != CLT_TOOLONG)
2519 {
2520 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_TIMEXC,
2521 item->hostname);
2522 status_update (item, CLT_TOOLONG);
2523 }
2524 }
2525 }
2526 }
2527 SL_RETURN( 0, _("client_time_check"));
2528}
2529
2530static int lookup_err = SH_ERR_SEVERE;
2531
2532int sh_forward_lookup_level (const char * c)
2533{
2534 int ci = sh_error_convert_level (c);
2535
2536 SL_ENTER(_("sh_forward_lookup_level"));
2537
2538 if (ci >= 0)
2539 {
2540 lookup_err = ci;
2541 SL_RETURN( 0, _("sh_forward_lookup_level"));
2542 }
2543 else
2544 SL_RETURN( (-1), _("sh_forward_lookup_level"));
2545}
2546
2547#ifndef MAXHOSTNAMELEN
2548#define MAXHOSTNAMELEN 127
2549#endif
2550
2551int check_addr (const char * claim, struct sockaddr_in addr_peer)
2552{
2553 char h_name[MAXHOSTNAMELEN + 1];
2554 char h_peer[MAXHOSTNAMELEN + 1];
2555 char h_peer_IP[16];
2556 char tmp_peer_IP[16];
2557 struct hostent * he;
2558 char ** p = NULL;
2559 int i;
2560 int flag = 0;
2561
2562 SL_ENTER(_("check_addr"));
2563
2564 if (claim == NULL)
2565 {
2566 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
2567 _("NULL input"), _("check_addr"));
2568 SL_RETURN ((-1), _("check_addr"));
2569 }
2570
2571 /* Make sure we have the canonical name for the client
2572 */
2573 he = sh_gethostbyname (claim);
2574
2575 if (he != NULL && he->h_name != NULL)
2576 {
2577 if (NULL == strchr(he->h_name, '.') && he->h_addr_list != NULL)
2578 {
2579 he = sh_gethostbyaddr(he->h_addr_list[0],
2580 he->h_length,
2581 he->h_addrtype);
2582 }
2583 }
2584
2585 /* copy canonical name into h_name
2586 */
2587 if (he != NULL && he->h_name != NULL)
2588 sl_strlcpy(h_name, he->h_name, MAXHOSTNAMELEN + 1);
2589 else
2590 {
2591 sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_RESCLT,
2592 claim);
2593 SL_RETURN ((0), _("check_addr"));
2594 }
2595
2596
2597 /* get canonical name of socket peer
2598 */
2599 he = sh_gethostbyaddr ((char *) &(addr_peer.sin_addr),
2600 sizeof(addr_peer.sin_addr),
2601 AF_INET);
2602
2603 if (he != NULL && he->h_name != NULL)
2604 {
2605 if (0 == sl_strcmp(he->h_name, _("localhost")))
2606 sl_strlcpy(h_peer, sh.host.name, MAXHOSTNAMELEN + 1);
2607 else
2608 sl_strlcpy(h_peer, he->h_name, MAXHOSTNAMELEN + 1);
2609 }
2610 else
2611 {
2612 sl_strlcpy(tmp_peer_IP,
2613 inet_ntoa (*(struct in_addr *) &(addr_peer.sin_addr)),
2614 16);
2615 sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_RESPEER,
2616 claim, tmp_peer_IP);
2617 SL_RETURN ((0), _("check_addr"));
2618 }
2619
2620 sl_strlcpy(h_peer_IP,
2621 inet_ntoa (*(struct in_addr *) he->h_addr),
2622 16);
2623
2624#if 0
2625 if (S_FALSE == DoReverseLookup)
2626 {
2627 SL_RETURN ((0), _("check_addr"));
2628 }
2629#endif
2630
2631 /* reverse lookup
2632 */
2633 if (0 != sl_strncmp(_("127."),
2634 inet_ntoa (*(struct in_addr *) &(addr_peer.sin_addr)),
2635 4))
2636 {
2637 he = sh_gethostbyname(h_peer);
2638
2639 if (he != NULL)
2640 {
2641 for (p = he->h_addr_list; *p; ++p)
2642 {
2643 if (0 == memcmp (*p, &(addr_peer.sin_addr),
2644 sizeof(addr_peer.sin_addr)))
2645 break;
2646 ++i;
2647 }
2648 }
2649 if (he == NULL || *p == NULL)
2650 {
2651 sl_strlcpy(tmp_peer_IP,
2652 inet_ntoa (*(struct in_addr *) &(addr_peer.sin_addr)),
2653 16);
2654 sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_LOOKERS,
2655 claim, h_peer, tmp_peer_IP);
2656 SL_RETURN ((0), _("check_addr"));
2657 }
2658 }
2659
2660
2661 if ((0 == sl_strcmp(h_peer, h_name)) || (0 == sl_strcmp(h_peer_IP, h_name)))
2662 {
2663 SL_RETURN ((0), _("check_addr"));
2664 }
2665 else
2666 {
2667 i = 0;
2668 while (he->h_aliases[i] != NULL)
2669 {
2670 if (0 == sl_strcmp(he->h_aliases[i], h_name))
2671 {
2672 flag = 1;
2673 break;
2674 }
2675 ++i;
2676 }
2677 if (flag == 0)
2678 sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_LOOKUP,
2679 claim, h_peer);
2680 }
2681
2682 SL_RETURN ((0), _("check_addr"));
2683}
2684
2685static int UseSocketPeer = S_FALSE;
2686
2687int set_socket_peer (const char * c)
2688{
2689 return sh_util_flagval(c, &UseSocketPeer);
2690}
2691
2692
2693/* -- Search register. --
2694 */
2695client_t * search_register(sh_conn_t * conn, int pos)
2696{
2697 client_t * this_client;
2698 char peer_ip[16];
2699 char peer_name[MAXHOSTNAMELEN+1];
2700 char * search_string;
2701 struct sockaddr_in peer_addr;
2702 struct hostent * he;
2703 char ** p = NULL;
2704
2705 SL_ENTER(_("search_register"));
2706
2707 if (UseSocketPeer == S_TRUE)
2708 {
2709 peer_addr = conn->addr_peer;
2710 sl_strlcpy(peer_ip,
2711 inet_ntoa (*(struct in_addr *) &(peer_addr.sin_addr)), 16);
2712
2713 /* get canonical name of socket peer
2714 */
2715 he = sh_gethostbyaddr ((char *) &(peer_addr.sin_addr),
2716 sizeof(peer_addr.sin_addr),
2717 AF_INET);
2718
2719 if (he != NULL && he->h_name != NULL)
2720 {
2721 if (0 == sl_strcmp(he->h_name, _("localhost")))
2722 sl_strlcpy(peer_name, sh.host.name, MAXHOSTNAMELEN + 1);
2723 else
2724 sl_strlcpy(peer_name, he->h_name, MAXHOSTNAMELEN + 1);
2725
2726 /* Reverse lookup
2727 */
2728 if (0 != sl_strncmp(peer_ip, _("127."), 4))
2729 {
2730 he = sh_gethostbyname(peer_name);
2731
2732 if (he != NULL)
2733 {
2734 for (p = he->h_addr_list; *p; ++p)
2735 {
2736 if (0 == memcmp (*p, &(peer_addr.sin_addr),
2737 sizeof(peer_addr.sin_addr)))
2738 break;
2739 }
2740 }
2741 if (he == NULL || *p == NULL)
2742 {
2743 /*
2744 sh_error_handle(lookup_err, FIL__, __LINE__, 0,
2745 MSG_TCP_LOOKERS,
2746 conn->buf[pos], peer_name, peer_ip);
2747 */
2748 sl_strlcpy(peer_name, peer_ip, MAXHOSTNAMELEN + 1);
2749 }
2750 }
2751 }
2752 else
2753 {
2754 sl_strlcpy(peer_name, peer_ip, MAXHOSTNAMELEN + 1);
2755 }
2756 search_string = peer_name;
2757 }
2758 else
2759 {
2760 search_string = &(conn->buf[pos]);
2761
2762 if (0 != check_addr (search_string, conn->addr_peer))
2763 {
2764 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
2765 _("Reverse lookup failed"), search_string);
2766 sh_forward_do_free (conn);
2767 SL_RETURN( NULL, _("search_register"));
2768 }
2769 }
2770
2771 /* ---- search the register -----
2772 */
2773 this_client = zAVLSearch(all_clients, search_string);
2774
2775 if (this_client == NULL)
2776 {
2777 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
2778 _("Not in client list"), search_string);
2779 sh_forward_do_free (conn);
2780 SL_RETURN( NULL, _("search_register"));
2781 }
2782 if (this_client->exit_flag == 1)
2783 {
2784 TPT((0, FIL__, __LINE__, _("msg=<this_client->exit_flag == 1>\n")));
2785 this_client->session_key_timer = (time_t) 0;
2786 this_client->session_key[0] = '\0';
2787 this_client->exit_flag = 0;
2788 }
2789 TPT((0, FIL__, __LINE__, _("msg=<search_register: client %s>\n"),
2790 this_client->hostname));
2791 TPT((0, FIL__, __LINE__, _("msg=<search_register: key %s>\n"),
2792 this_client->session_key));
2793 SL_RETURN( this_client, _("search_register"));
2794}
2795
2796
2797/************************************************************************
2798 *
2799 * Here we check the message received, and decide on the answer to send
2800 * (if any). The connection is in CONN_PAUSED state, thus we must:
2801 * (i) define the proper reaction
2802 * (ii) reset to CONN_READING or CONN_WRITING or CONN_FREE
2803 * (iii) eventually reset the connection entry
2804 *
2805 *************************************************************************/
2806static
2807void check_protocol(sh_conn_t * conn, int state)
2808{
2809 client_t * this_client;
2810
2811 char * cmd;
2812
2813 char hash[SH_MAXMSGLEN + KEY_LEN + KEY_LEN + 1];
2814 char * buffer;
2815
2816 int clt_sev;
2817 char * ptok;
2818
2819 UINT32 ticks;
2820 size_t len;
2821 int i;
2822 char * test;
2823 char u[5] = "OOOO";
2824
2825 SL_TICKET sfd = -1;
2826 char * read_buf = 0;
2827 char * send_buf;
2828 int bytes;
2829
2830#ifdef SH_ENCRYPT
2831 int blkfac;
2832 int rem;
2833 int send_bytes;
2834 int err_num;
2835 char expbuf[SH_ERRBUF_SIZE];
2836#endif
2837
2838
2839#ifdef USE_SRP_PROTOCOL
2840 char * foo_B;
2841 char * foo_Ss;
2842#endif
2843 char hashbuf[KEYBUF_SIZE];
2844 char sigbuf[KEYBUF_SIZE];
2845
2846 SL_ENTER(_("check_protocol"));
2847
2848 /* seed / re-seed the PRNG if required
2849 */
2850 (void) taus_seed();
2851
2852
2853 /* protocols:
2854 * -- (iii) file transfer
2855 * -- (ii) authenticated message transfer
2856 * -- (i) SRP key exchange
2857 */
2858
2859 /* --------- FILE TRANSFER -----------
2860 */
2861 if ( (conn->head[0] & SH_PROTO_SRP) == 0 &&
2862 (conn->head[0] & SH_PROTO_BIG) != 0 /* is set */ )
2863 {
2864
2865 if (state == SH_DO_READ) /* finished reading */
2866 {
2867 TPT(( 0, FIL__, __LINE__, _("msg=<File transfer - entry.>\n")));
2868
2869 /* -- Client requests challenge. --
2870 */
2871 if (0 == check_request_nerr ((char *) &(conn->head[3]), _("HELO")))
2872 {
2873
2874 TPT(( 0, FIL__, __LINE__,
2875 _("msg=<File transfer - HELO (1).>\n")));
2876
2877 if (conn->buf == NULL || sl_strlen(conn->buf) <= KEY_LEN)
2878 {
2879 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOCLT);
2880 sh_forward_do_free (conn);
2881 SL_RET0(_("check_protocol"));
2882 }
2883
2884 /* ---- search the register -----
2885 */
2886
2887 this_client = search_register (conn, KEY_LEN);
2888 if (this_client == NULL)
2889 SL_RET0(_("check_protocol"));
2890
2891 /* ---- force authentication -----
2892 */
2893
2894 if (this_client->session_key[0] == '\0' ||
2895 (time(NULL) - this_client->session_key_timer)
2896 > (time_t) TIMEOUT_KEY )
2897 {
2898 /* fake an auth request and jump there
2899 */
2900 conn->head[0] = (conn->head[0] | SH_PROTO_SRP);
2901 conn->head[3] = 'S';
2902 conn->head[4] = 'A';
2903 conn->head[5] = 'L';
2904 conn->head[6] = 'T';
2905 if (flag_err_info == SL_TRUE)
2906 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FAUTH,
2907 &(conn->buf[KEY_LEN]));
2908 len = sl_strlen(&(conn->buf[KEY_LEN])) + 1;
2909 /* &(conn->buf[KEY_LEN]) is hostname */
2910 /* may overlap, thus only memmove is correct */
2911 memmove(conn->buf, &(conn->buf[KEY_LEN]), len);
2912 this_client->session_key[0] = '\0';
2913 this_client->session_key_timer = (time_t) 1;
2914 goto servInit;
2915 }
2916
2917 /* --- check whether hostname is properly signed ---
2918 */
2919 if (conn->K != NULL)
2920 {
2921 SH_FREE(conn->K);
2922 conn->K = NULL;
2923 }
2924
2925 /* FIXME
2926 len = sl_strlen(&(conn->buf[KEY_LEN])) + 1;
2927 if (sl_ok_adds(len, KEY_LEN))
2928 len += KEY_LEN;
2929 len = (len < (KEY_LEN+1)) ? (KEY_LEN+1) : len;
2930 */
2931 conn->K = SH_ALLOC(KEY_LEN+1);
2932
2933 sl_strlcpy (conn->K,
2934 sh_util_siggen(this_client->session_key,
2935 &(conn->buf[KEY_LEN]),
2936 sl_strlen(&(conn->buf[KEY_LEN])),
2937 sigbuf, sizeof(sigbuf)),
2938 KEY_LEN+1);
2939 TPT((0, FIL__, __LINE__, _("msg=<host %s>\n"),
2940 &(conn->buf[KEY_LEN])));
2941 TPT((0, FIL__, __LINE__, _("msg=<ckey %s>\n"),
2942 this_client->session_key));
2943 TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
2944 sh_util_siggen(this_client->session_key,
2945 &(conn->buf[KEY_LEN]),
2946 sl_strlen(&(conn->buf[KEY_LEN])),
2947 sigbuf, sizeof(sigbuf))));
2948
2949 if (0 != sl_strncmp(conn->K, conn->buf, KEY_LEN))
2950 {
2951 TPT((0, FIL__, __LINE__, _("msg=<clt %s>\n"), conn->buf));
2952 TPT((0, FIL__, __LINE__, _("msg=<srv %s>\n"), conn->K));
2953 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
2954 _("Signature mismatch"),
2955 &(conn->buf[KEY_LEN]));
2956
2957 this_client->session_key_timer =
2958 time(NULL) - (2*TIMEOUT_KEY);
2959
2960 sh_forward_do_free (conn);
2961 SL_RET0(_("check_protocol"));
2962 }
2963 SH_FREE(conn->K);
2964 conn->K = NULL;
2965
2966 /* --- create and send a nonce ---
2967 */
2968
2969 conn->client_entry = this_client;
2970 sl_strlcpy (conn->peer, &(conn->buf[KEY_LEN]), SH_MINIBUF+1);
2971
2972 ticks = (UINT32) taus_get ();
2973
2974 if (conn->K != NULL)
2975 {
2976 SH_FREE(conn->K);
2977 conn->K = NULL;
2978 }
2979 conn->K = SH_ALLOC(KEY_LEN+1);
2980 sl_strlcpy (conn->K,
2981 sh_tiger_hash ((char *) &ticks,
2982 TIGER_DATA, sizeof(UINT32),
2983 hashbuf, sizeof(hashbuf)),
2984 KEY_LEN+1);
2985
2986 TPT((0, FIL__, __LINE__, _("msg=<send nonce>\n")));
2987 sh_forward_prep_send (conn, conn->K, KEY_LEN+1, _("NSRV"),
2988 SH_PROTO_BIG);
2989 }
2990
2991 /* --- Client has send a message. Check state and message. ---
2992 */
2993 else if (0 == check_request_nerr((char *)&(conn->head[3]), _("NCLT")) &&
2994 conn->client_entry != NULL &&
2995 sl_strlen(conn->buf) > KEY_LEN &&
2996 conn->K != NULL)
2997 {
2998
2999 TPT(( 0, FIL__, __LINE__,
3000 _("msg=<File transfer - NCLT (3).>\n")));
3001
3002 /* --- get client nonce and compute hash ---
3003 */
3004 if (conn->A != NULL)
3005 {
3006 SH_FREE(conn->A);
3007 conn->A = NULL;
3008 }
3009 conn->A = SH_ALLOC(3*KEY_LEN+1);
3010 sl_strlcpy (conn->A, conn->K, KEY_LEN+1);
3011 sl_strlcat(conn->A, conn->buf, /* truncate */
3012 2*KEY_LEN+1);
3013 sl_strlcat(conn->A, conn->client_entry->session_key,
3014 3*KEY_LEN+1);
3015 sl_strlcpy (conn->K, sh_tiger_hash(conn->A,TIGER_DATA,3*KEY_LEN,
3016 hashbuf, sizeof(hashbuf)),
3017 KEY_LEN+1);
3018 SH_FREE(conn->A);
3019 conn->A = NULL;
3020
3021
3022#ifdef SH_ENCRYPT
3023 if ((conn->client_entry->encf_flag != 0) &&
3024 ((conn->head[0] & SH_PROTO_ENC) == 0))
3025 {
3026 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISENC,
3027 _("file download"),
3028#ifdef SH_ENCRYPT_2
3029 _("version2"),
3030#else
3031 _("version1"),
3032#endif
3033 _("none"));
3034 if (sl_strlen(conn->buf) > (KEY_LEN + 5)) {
3035 if (sh_tools_hash_vfy(conn->K, conn->buf, KEY_LEN+5)) {
3036 if (conn->buf[KEY_LEN+4] == conn->head[0]) {
3037 /* conn->client_entry->encf_flag = 0 */ ; /* FIXME */
3038 }
3039 }
3040 }
3041 }
3042 else if ((conn->client_entry->encf_flag != 0) &&
3043 ((conn->head[0] & SH_MASK_ENC) !=
3044 conn->client_entry->encf_flag))
3045 {
3046 sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, 0,
3047 MSG_TCP_MISENC,
3048 _("file download"),
3049#ifdef SH_ENCRYPT_2
3050 _("version2"),
3051#else
3052 _("version1"),
3053#endif
3054 ((conn->head[0] & SH_PROTO_EN2) == SH_PROTO_EN2) ? _("version2") : _("version1")
3055 );
3056 conn->client_entry->encf_flag =
3057 (conn->head[0] & SH_MASK_ENC);
3058 }
3059#else
3060 if ((conn->head[0] & SH_PROTO_ENC) != 0)
3061 {
3062 sh_error_handle((-1), FIL__, __LINE__, 0,
3063 MSG_TCP_MISENC,
3064 _("file download"),
3065 _("none"),
3066 ((conn->head[0] & SH_PROTO_EN2) == SH_PROTO_EN2) ? _("version2") : _("version1"));
3067 }
3068#endif
3069
3070
3071 /* ---- K = H(NSRV, NCLT, session_key) -------
3072 */
3073
3074 if (conn->FileName != NULL)
3075 {
3076 SH_FREE(conn->FileName);
3077 conn->FileName = NULL;
3078 }
3079
3080 if (0 == sl_strncmp (_("CONF"), &(conn->buf[KEY_LEN]), 4))
3081 {
3082 strcpy(conn->FileType, _("CONF")); /* known to fit */
3083 conn->FileName = get_client_conf_file(conn->peer,
3084 &(conn->FileLength));
3085 conn->FileSent = 0;
3086 }
3087 else if (0 == sl_strncmp (_("DATA"), &(conn->buf[KEY_LEN]), 4))
3088 {
3089 strcpy(conn->FileType, _("DATA")); /* known to fit */
3090 conn->FileName = get_client_data_file(conn->peer,
3091 &(conn->FileLength));
3092 conn->FileSent = 0;
3093 }
3094 else
3095 {
3096 ptok = sh_util_safe_name(&(conn->buf[KEY_LEN]));
3097 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FFILE,
3098 conn->peer,
3099 ptok);
3100 SH_FREE(ptok);
3101 status_update (conn->client_entry, CLT_FAILED);
3102 sh_forward_do_free (conn);
3103 }
3104
3105 bytes = -1;
3106
3107 if (conn != NULL && conn->FileName != NULL)
3108 {
3109 sfd = sl_open_read(conn->FileName, SL_YESPRIV);
3110 if (!SL_ISERROR(sfd))
3111 {
3112 read_buf = SH_ALLOC(TRANS_BYTES);
3113 bytes = sl_read (sfd, read_buf, TRANS_BYTES);
3114 sl_close(sfd);
3115 }
3116
3117 else
3118 {
3119 sh_error_handle((-1), FIL__, __LINE__, sfd,
3120 MSG_E_ACCESS,
3121 (long) geteuid(),
3122 conn->FileName);
3123 }
3124 if (bytes >= 0)
3125 {
3126#ifdef SH_ENCRYPT
3127 /* need to send N * B_SIZ bytes
3128 */
3129 blkfac = bytes / B_SIZ;
3130 rem = bytes - (blkfac * B_SIZ);
3131 if (rem != 0)
3132 {
3133 memset(&read_buf[bytes], '\n', (B_SIZ-rem));
3134 ++blkfac;
3135 send_bytes = blkfac * B_SIZ;
3136 }
3137 else
3138 send_bytes = bytes;
3139
3140 send_buf = hash_me(conn->K, read_buf,
3141 send_bytes);
3142
3143 sh_forward_prep_send_crypt (conn, send_buf,
3144 send_bytes+KEY_LEN,
3145 _("FILE"),
3146 SH_PROTO_BIG|conn->client_entry->encf_flag);
3147#else
3148 send_buf = hash_me(conn->K, read_buf, bytes);
3149 sh_forward_prep_send_crypt (conn, send_buf,
3150 bytes+KEY_LEN,
3151 _("FILE"), SH_PROTO_BIG);
3152#endif
3153 conn->FileSent += bytes;
3154 if (send_buf != NULL)
3155 {
3156 SH_FREE(send_buf);
3157 }
3158 SH_FREE(read_buf);
3159 }
3160 }
3161
3162 if (conn == NULL || conn->FileName == NULL ||
3163 SL_ISERROR(sfd) || bytes < 0)
3164 {
3165 sh_error_handle((-1), FIL__, __LINE__, sfd, MSG_TCP_NFILE,
3166 conn->peer,
3167 (conn->FileName == NULL) ?
3168 _("(NULL)") : conn->FileName);
3169 status_update (conn->client_entry, CLT_FAILED);
3170 sh_forward_do_free (conn);
3171 }
3172
3173 }
3174
3175 else if (0 == check_request_nerr((char *)&(conn->head[3]),
3176 _("RECV")) &&
3177 conn->client_entry != NULL &&
3178 conn->K != NULL &&
3179 conn->FileName != NULL)
3180 {
3181
3182 TPT(( 0, FIL__, __LINE__,
3183 _("msg=<File transfer - RCVT (5+).>\n")));
3184
3185 if (conn->FileSent == conn->FileLength)
3186 {
3187 send_buf = hash_me(conn->K, conn->peer,
3188 sl_strlen(conn->peer));
3189#ifdef SH_ENCRYPT
3190 sh_forward_prep_send_crypt (conn, send_buf,
3191 sl_strlen(conn->peer)+KEY_LEN,
3192 _("EEOT"),
3193 SH_PROTO_BIG|conn->client_entry->encf_flag);
3194#else
3195 sh_forward_prep_send_crypt (conn, send_buf,
3196 sl_strlen(conn->peer)+KEY_LEN,
3197 _("EEOT"),
3198 SH_PROTO_BIG);
3199#endif
3200 SH_FREE(send_buf);
3201 }
3202 else
3203 {
3204 bytes = -1;
3205 sfd = sl_open_read(conn->FileName, SL_YESPRIV);
3206 if (!SL_ISERROR(sfd))
3207 {
3208 read_buf = SH_ALLOC(TRANS_BYTES);
3209 sl_seek (sfd, (off_t) conn->FileSent);
3210 bytes = sl_read (sfd, read_buf, TRANS_BYTES);
3211 sl_close(sfd);
3212 }
3213 else
3214 {
3215 sh_error_handle((-1), FIL__, __LINE__, sfd,
3216 MSG_E_ACCESS,
3217 (long) geteuid(),
3218 conn->FileName);
3219 }
3220 if (bytes >= 0)
3221 {
3222#ifdef SH_ENCRYPT
3223 /* need to send N * B_SIZ bytes
3224 */
3225 blkfac = bytes / B_SIZ;
3226 rem = bytes - (blkfac * B_SIZ);
3227 if (rem != 0)
3228 {
3229 memset(&read_buf[bytes], '\n', (B_SIZ-rem));
3230 ++blkfac;
3231 send_bytes = blkfac * B_SIZ;
3232 }
3233 else
3234 send_bytes = bytes;
3235
3236 send_buf = hash_me(conn->K, read_buf,
3237 send_bytes);
3238
3239 sh_forward_prep_send_crypt (conn, send_buf,
3240 send_bytes+KEY_LEN,
3241 _("FILE"),
3242 SH_PROTO_BIG|conn->client_entry->encf_flag);
3243#else
3244
3245 send_buf = hash_me(conn->K, read_buf, bytes);
3246 sh_forward_prep_send_crypt (conn, send_buf,
3247 bytes+KEY_LEN,
3248 _("FILE"),
3249 SH_PROTO_BIG);
3250#endif
3251
3252 conn->FileSent += bytes;
3253 SH_FREE(send_buf);
3254 SH_FREE(read_buf);
3255 }
3256 else
3257 {
3258 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NFILE,
3259 conn->peer,
3260 (conn->FileName == NULL) ?
3261 _("(NULL)") : conn->FileName);
3262 status_update (conn->client_entry, CLT_FAILED);
3263 sh_forward_do_free (conn);
3264 }
3265 }
3266 }
3267
3268
3269 else if (0 == check_request_nerr((char *)&(conn->head[3]),
3270 _("EOTE")) &&
3271 conn->client_entry != NULL)
3272 {
3273
3274 TPT(( 0, FIL__, __LINE__,
3275 _("msg=<File transfer - EOTE (7).>\n")));
3276
3277 if (flag_err_info == SL_TRUE)
3278 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_OKFILE,
3279 conn->peer);
3280
3281 if ((conn->client_entry->status_now != CLT_SUSPEND) &&
3282 (conn->client_entry->status_now != CLT_TOOLONG))
3283 { status_update (conn->client_entry, CLT_FILE); }
3284 else
3285 { conn->client_entry->session_key[0] = '\0'; }
3286 conn->client_entry->last_connect = time (NULL);
3287 sh_forward_do_free (conn);
3288 }
3289
3290
3291 /* client does something unexpected
3292 */
3293 else /* ---- ??? ----- */
3294 {
3295 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
3296 1, conn->pass, conn->peer,
3297 '\\', conn->head[3], '\\',conn->head[4],
3298 '\\', conn->head[5], '\\',conn->head[6]);
3299 status_update (conn->client_entry, CLT_FAILED);
3300 sh_forward_do_free (conn);
3301 }
3302 }
3303
3304 else if (state == SH_DO_WRITE) /* finished writing */
3305 {
3306 TPT(( 0, FIL__, __LINE__, _("msg=<File transfer - (wait).>\n")));
3307
3308 /* challenge is sent, now wait for message from client
3309 */
3310 conn->headcount = 0;
3311 conn->bytecount = 0;
3312 conn->bytes_to_send = 0;
3313 conn->bytes_to_get = 0;
3314 if (conn->buf != NULL)
3315 {
3316 SH_FREE(conn->buf);
3317 conn->buf = NULL;
3318 }
3319 conn->state = CONN_READING;
3320 }
3321 SL_RET0(_("check_protocol"));
3322 }
3323
3324 /* --------- message exchange -----------
3325 */
3326 if ((conn->head[0] & SH_PROTO_SRP) == 0 &&
3327 (conn->head[0] & SH_PROTO_MSG) != 0 /* is set */ )
3328 {
3329
3330 if (state == SH_DO_READ) /* finished reading */
3331 {
3332
3333 TPT(( 0, FIL__, __LINE__, _("msg=<Message transfer - entry.>\n")));
3334
3335 /* client requests challenge
3336 */
3337 if (0 == check_request_nerr ((char *)&(conn->head[3]), _("HELO")))
3338 {
3339
3340 TPT(( 0, FIL__, __LINE__,
3341 _("msg=<Message transfer - HELO (1).>\n")));
3342
3343 if (conn->buf == NULL || sl_strlen(conn->buf) <= KEY_LEN )
3344 {
3345 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOCLT);
3346 sh_forward_do_free (conn);
3347 SL_RET0(_("check_protocol"));
3348 }
3349
3350 TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s.>\n"), conn->buf));
3351
3352 /* ---- search the register -----
3353 */
3354 this_client = search_register (conn, KEY_LEN);
3355 if (NULL == this_client)
3356 SL_RET0(_("check_protocol"));
3357
3358 /* ---- force authentication -----
3359 */
3360 if ( (this_client->session_key[0] == '\0') ||
3361 ((time(NULL)-this_client->session_key_timer)
3362 > (time_t) TIMEOUT_KEY)
3363 )
3364 {
3365
3366 /* fake an auth request and jump there
3367 */
3368 conn->head[0] = (conn->head[0] | SH_PROTO_SRP);
3369 conn->head[3] = 'S';
3370 conn->head[4] = 'A';
3371 conn->head[5] = 'L';
3372 conn->head[6] = 'T';
3373 if (flag_err_info == SL_TRUE)
3374 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FAUTH,
3375 &(conn->buf[KEY_LEN]));
3376 len = sl_strlen(&(conn->buf[KEY_LEN])) + 1;
3377 /* &(conn->buf[KEY_LEN]) is hostname */
3378 /* may overlap, thus only memmove is correct */
3379 memmove(conn->buf, &(conn->buf[KEY_LEN]), len);
3380 this_client->session_key[0] = '\0';
3381 this_client->session_key_timer = (time_t) 1;
3382
3383 goto servInit;
3384 }
3385
3386 /* check whether hostname is properly signed
3387 */
3388 if (conn->K != NULL)
3389 {
3390 SH_FREE(conn->K);
3391 conn->K = NULL;
3392 }
3393 /* FIXME len = sl_strlen(&(conn->buf[KEY_LEN])) + KEY_LEN + 1; */
3394 conn->K = SH_ALLOC(KEY_LEN + 1);
3395
3396 sl_strlcpy (conn->K,
3397 sh_util_siggen(this_client->session_key,
3398 &(conn->buf[KEY_LEN]),
3399 sl_strlen(&(conn->buf[KEY_LEN])),
3400 sigbuf, sizeof(sigbuf)),
3401 KEY_LEN+1);
3402 TPT((0, FIL__, __LINE__, _("msg=<host %s>\n"),
3403 &(conn->buf[KEY_LEN])));
3404 TPT((0, FIL__, __LINE__, _("msg=<ckey %s>\n"),
3405 this_client->session_key));
3406 TPT((0, FIL__, __LINE__, _("msg=<sign %s>\n"), conn->K));
3407
3408 if (0 != sl_strncmp(conn->K, conn->buf, KEY_LEN))
3409 {
3410 TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s>\n"), conn->buf));
3411 TPT(( 0, FIL__, __LINE__, _("msg=<Want %s>\n"), conn->K));
3412 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
3413 _("Signature mismatch"),
3414 &(conn->buf[KEY_LEN]));
3415
3416 this_client->session_key_timer =
3417 time(NULL) - (2*TIMEOUT_KEY);
3418
3419 sh_forward_do_free (conn);
3420 SL_RET0(_("check_protocol"));
3421 }
3422 SH_FREE(conn->K);
3423 conn->K = NULL;
3424
3425 /* -- create a nonce and send it --
3426 */
3427
3428 conn->client_entry = this_client;
3429 sl_strlcpy (conn->peer, &(conn->buf[KEY_LEN]), SH_MINIBUF+1);
3430
3431 ticks = (UINT32) taus_get ();
3432
3433 test = (char *) &ticks;
3434 sh_util_cpylong (conn->challenge, test, 4);
3435 conn->challenge[4] = '\0';
3436 for (i = 0; i < 4; ++i)
3437 if (conn->challenge[i] == '\0')
3438 conn->challenge[i] = 0x01;
3439
3440 sh_forward_prep_send (conn, conn->challenge, 5, _("TALK"),
3441 SH_PROTO_MSG);
3442 TPT(( 0, FIL__, __LINE__, _("msg=<Sent %s.>\n"),
3443 hu_trans(conn->challenge)));
3444 }
3445
3446 /* Client has send a message. Check whether we are in proper
3447 * state, and verify message.
3448 */
3449 else if (0 ==
3450 check_request_nerr((char *)&(conn->head[3]), _("MESG")) &&
3451 conn->client_entry != NULL &&
3452 conn->client_entry->session_key[0] != '\0' &&
3453 (len = sl_strlen(conn->buf) - KEY_LEN) > 0 &&
3454 sl_strlen(conn->challenge) == 4)
3455 {
3456 TPT(( 0, FIL__, __LINE__,
3457 _("msg=<Message transfer - MESG (3).>\n")));
3458
3459#ifdef SH_ENCRYPT
3460 if (conn->client_entry->encf_flag == 0) {
3461 conn->client_entry->ency_flag = 0;
3462 }
3463 if ((conn->client_entry->ency_flag != 0) &&
3464 ((conn->head[0] & SH_PROTO_ENC) == 0))
3465 {
3466 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISENC,
3467 _("message transfer"),
3468#ifdef SH_ENCRYPT_2
3469 _("version2"),
3470#else
3471 _("version1"),
3472#endif
3473 _("none"));
3474 /* conn->client_entry->ency_flag = 0; */
3475 }
3476 else if ((conn->client_entry->ency_flag != 0) &&
3477 ((conn->head[0] & SH_MASK_ENC) !=
3478 conn->client_entry->ency_flag))
3479 {
3480 sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, 0,
3481 MSG_TCP_MISENC,
3482 _("message transfer"),
3483#ifdef SH_ENCRYPT_2
3484 _("version2"),
3485#else
3486 _("version1"),
3487#endif
3488 ((conn->head[0] & SH_PROTO_EN2) == SH_PROTO_EN2) ? _("version2") : _("version1"));
3489 conn->client_entry->ency_flag =
3490 (conn->head[0] & SH_MASK_ENC);
3491 }
3492#else
3493 if ((conn->head[0] & SH_PROTO_ENC) != 0)
3494 {
3495 sh_error_handle((-1), FIL__, __LINE__, 0,
3496 MSG_TCP_MISENC,
3497 _("message transfer"),
3498 _("none"),
3499 ((conn->head[0] & SH_PROTO_EN2) == SH_PROTO_EN2) ? _("version2") : _("version1"));
3500 }
3501#endif
3502
3503 TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s.>\n"), conn->buf));
3504 /* get hash from message end, truncate message
3505 */
3506 sl_strlcpy(hash, &(conn->buf[len]), KEY_LEN+1);
3507 conn->buf[len] = '\0';
3508
3509 /* verify hash
3510 */
3511 buffer = sh_util_strconcat(conn->buf, conn->challenge, NULL);
3512 i = sl_strncmp(hash,
3513 sh_util_siggen(conn->client_entry->session_key,
3514 buffer,
3515 sl_strlen(buffer),
3516 sigbuf, sizeof(sigbuf)),
3517 KEY_LEN);
3518 TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
3519 sh_util_siggen(conn->client_entry->session_key,
3520 buffer,
3521 sl_strlen(buffer),
3522 sigbuf, sizeof(sigbuf))));
3523
3524
3525 if (0 != i)
3526 {
3527 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
3528 status_update (conn->client_entry, CLT_FAILED);
3529 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
3530 _("Msg signature mismatch"), conn->peer);
3531 conn->client_entry->session_key_timer =
3532 time(NULL) - (2*TIMEOUT_KEY);
3533 sh_forward_do_free (conn);
3534 SL_RET0(_("check_protocol"));
3535 }
3536 else
3537 {
3538 conn->client_entry->last_connect = time (NULL);
3539
3540 if (NULL != sl_strstr(conn->buf, _("EXIT")))
3541 {
3542 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
3543 conn->client_entry->exit_flag = 1;
3544 status_update (conn->client_entry, CLT_EXITED);
3545 }
3546 else if (NULL != sl_strstr(conn->buf, _("PANIC")))
3547 {
3548 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
3549 status_update (conn->client_entry, CLT_PANIC);
3550 }
3551 else if (NULL != sl_strstr(conn->buf, _("SUSPEND")))
3552 {
3553 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
3554 status_update (conn->client_entry, CLT_SUSPEND);
3555 }
3556 else if (NULL != sl_strstr(conn->buf, _("POLICY")))
3557 {
3558 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
3559 status_update (conn->client_entry, CLT_POLICY);
3560 }
3561 else if (NULL != sl_strstr(conn->buf,
3562 _("File check completed")))
3563 {
3564 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
3565 status_update (conn->client_entry, CLT_CHECK);
3566 }
3567 else if (NULL != sl_strstr(conn->buf, _("START")))
3568 {
3569 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
3570 sh_socket_add2reload (conn->client_entry->hostname);
3571 if (conn->client_entry->status_now == CLT_SUSPEND) {
3572 status_update (conn->client_entry, CLT_ILLEGAL);
3573 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_ILL,
3574 conn->peer);
3575 }
3576 else
3577 status_update (conn->client_entry, CLT_STARTED);
3578 }
3579 else
3580 {
3581 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
3582 if (0 != sl_strcmp(conn->buf,
3583 _("Runtime configuration reloaded")))
3584 {
3585 sh_socket_add2reload (conn->client_entry->hostname);
3586 }
3587 status_update (conn->client_entry, CLT_MSG);
3588 }
3589
3590 TPT((0, FIL__, __LINE__, _("msg=<status updated>\n")));
3591 clt_sev = atoi(conn->buf);
3592 clt_class = (-1);
3593 ptok = strchr(conn->buf, '?');
3594 if (ptok != NULL)
3595 {
3596 ++ptok;
3597 if (ptok != NULL && sh.flag.client_class == S_TRUE)
3598 clt_class = atoi(ptok); /* is a global */
3599 ptok = strchr(ptok, '?');
3600 if (ptok != NULL)
3601 ++ptok;
3602 }
3603 if (sh.flag.client_severity == S_FALSE)
3604 clt_sev = (-1);
3605
3606 /* here we expect an xml formatted message, thus we don't
3607 escape xml special chars (flag == 0) */
3608 ptok =
3609 sh_tools_safe_name ((ptok!=NULL) ? ptok : conn->buf, 0);
3610
3611 /* push client name to error routine
3612 */
3613 sh_error_set_peer(sh_strip_domain (conn->peer));
3614 sh_error_handle(clt_sev, FIL__, __LINE__, 0, MSG_TCP_MSG,
3615 sh_strip_domain (conn->peer),
3616 ptok);
3617 sh_error_set_peer(NULL);
3618
3619 TPT((0, FIL__, __LINE__, _("msg=<%s>\n"), ptok));
3620 SH_FREE(ptok);
3621 clt_class = (-1);
3622 }
3623 memset(buffer, '\0', sl_strlen(buffer));
3624 SH_FREE(buffer);
3625
3626 /* SERVER CONF SEND
3627 */
3628 buffer = sh_util_strconcat(conn->buf,
3629 conn->challenge,
3630 NULL);
3631 sl_strlcpy(hash,
3632 sh_util_siggen ( conn->client_entry->session_key,
3633 buffer,
3634 sl_strlen(buffer),
3635 sigbuf, sizeof(sigbuf)),
3636 KEY_LEN+1);
3637
3638 /* --- SERVER CMD --- */
3639 cmd = sh_socket_check (conn->peer);
3640
3641 if (cmd != NULL)
3642 {
3643 /* max cmd size is SH_MAXMSGLEN bytes
3644 */
3645 sl_strlcpy(&hash[KEY_LEN], cmd, SH_MAXMSGLEN);
3646 sl_strlcat(&hash[KEY_LEN],
3647 sh_util_siggen ( conn->client_entry->session_key,
3648 &hash[KEY_LEN],
3649 sl_strlen(&hash[KEY_LEN]),
3650 sigbuf, sizeof(sigbuf)),
3651 SH_MAXMSGLEN+KEY_LEN+1);
3652
3653 TPT((0, FIL__, __LINE__, _("CONF SEND <0> <%s>\n"),
3654 &hash[KEY_LEN]));
3655
3656 } else {
3657
3658 TPT((0, FIL__, __LINE__, _("CONF SEND <0> <[NULL]>\n")));
3659
3660 }
3661 /* --- SERVER CMD END --- */
3662
3663 TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
3664 sh_util_siggen(conn->client_entry->session_key,
3665 buffer,
3666 sl_strlen(buffer),
3667 sigbuf, sizeof(sigbuf))));
3668
3669#ifdef SH_ENCRYPT
3670 sh_forward_prep_send_crypt (conn, hash,
3671 sl_strlen(hash) /* KEY_LEN */,
3672 _("CONF"),
3673 SH_PROTO_MSG|SH_PROTO_END|conn->client_entry->ency_flag);
3674#else
3675 sh_forward_prep_send_crypt (conn, hash,
3676 sl_strlen(hash) /* KEY_LEN */,
3677 _("CONF"),
3678 SH_PROTO_MSG|SH_PROTO_END);
3679#endif
3680
3681 memset(buffer, '\0', sl_strlen(buffer));
3682 SH_FREE(buffer);
3683
3684 /* sh_forward_do_free (conn); */
3685 }
3686
3687 /* client does something unexpected
3688 */
3689 else /* ---- ??? ----- */
3690 {
3691 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
3692 2, conn->pass, conn->peer,
3693 '\\', conn->head[3], '\\',conn->head[4],
3694 '\\', conn->head[5], '\\',conn->head[6]);
3695 status_update (conn->client_entry, CLT_FAILED);
3696 conn->client_entry->session_key_timer =
3697 time(NULL) - (2*TIMEOUT_KEY);
3698 sh_forward_do_free (conn);
3699 }
3700 }
3701 else if (state == SH_DO_WRITE) /* finished writing */
3702 {
3703 if (0 != (conn->head[0] & SH_PROTO_END))
3704 {
3705 if (flag_err_debug == SL_TRUE)
3706 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_OKMSG,
3707 sh_strip_domain (conn->peer));
3708 sh_forward_do_free (conn);
3709 SL_RET0(_("check_protocol"));
3710 }
3711
3712 TPT(( 0, FIL__, __LINE__, _("msg=<Msg transfer - (wait).>\n")));
3713
3714 /* challenge is sent, now wait for message from client
3715 */
3716 conn->headcount = 0;
3717 conn->bytecount = 0;
3718 conn->bytes_to_send = 0;
3719 conn->bytes_to_get = 0;
3720 if (conn->buf != NULL)
3721 {
3722 SH_FREE(conn->buf);
3723 conn->buf = NULL;
3724 }
3725 conn->state = CONN_READING;
3726 }
3727 TPT((0, FIL__, __LINE__, _("msg=<return>\n") ));
3728 SL_RET0(_("check_protocol"));
3729 }
3730
3731 /* --------- authentication -----------
3732 */
3733
3734 /* entry point for jump from message forward if session key must
3735 * be re-initialized
3736 */
3737 servInit:
3738
3739 if ( (conn->head[0] & SH_PROTO_SRP) != 0 /* is set */ )
3740 {
3741
3742#ifndef USE_SRP_PROTOCOL
3743
3744 if (state == SH_DO_READ) /* finished reading */
3745 {
3746 TPT((0, FIL__, __LINE__, _("msg=<Authentication - entry.>\n")));
3747
3748 /* first pass -- client request salt
3749 */
3750 if (conn->pass == 1)
3751 {
3752
3753 TPT((0, FIL__, __LINE__,
3754 _("msg=<Authentication - SALT (1).>\n")));
3755
3756 if (conn->buf == NULL || sl_strlen(conn->buf) == 0)
3757 {
3758 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOCLT);
3759 sh_forward_do_free (conn);
3760 SL_RET0(_("check_protocol"));
3761 }
3762
3763
3764 /* search the register
3765 */
3766
3767 this_client = search_register (conn, 0);
3768 if (NULL == this_client)
3769 SL_RET0(_("check_protocol"));
3770
3771
3772 conn->client_entry = this_client;
3773 sl_strlcpy (conn->peer, conn->buf, SH_MINIBUF+1);
3774
3775 if (0 != check_request_s((char *)&(conn->head[3]),
3776 _("SALT"),conn->peer))
3777 {
3778 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
3779 _("No salt requested"), conn->peer);
3780 status_update (conn->client_entry, CLT_FAILED);
3781 conn->client_entry->session_key_timer =
3782 time(NULL) - (2*TIMEOUT_KEY);
3783 sh_forward_do_free (conn);
3784 SL_RET0(_("check_protocol"));
3785 }
3786
3787
3788 /* -- create server nounce v --
3789 */
3790 ticks = (UINT32) taus_get ();
3791
3792 if (conn->A != NULL)
3793 {
3794 SH_FREE(conn->A);
3795 conn->A = NULL;
3796 }
3797 conn->A = SH_ALLOC(KEY_LEN+1);
3798
3799 sl_strlcpy(conn->A,
3800 sh_tiger_hash((char *) &ticks,
3801 TIGER_DATA, sizeof(UINT32),
3802 hashbuf, sizeof(hashbuf)),
3803 KEY_LEN+1);
3804 u[0] = 'I'; u[1] = 'N'; u[2] = 'I'; u[3] = 'T'; u[4] = '\0';
3805
3806 if (conn->M1 != NULL)
3807 {
3808 SH_FREE(conn->M1);
3809 conn->M1 = NULL;
3810 }
3811 conn->M1 = SH_ALLOC(2*KEY_LEN+1);
3812
3813 /* compute hash key H(v(server), P)v(server)
3814 */
3815 sh_passwd (conn->A, conn->client_entry->verifier,
3816 NULL, conn->M1);
3817
3818 sl_strlcat(conn->M1, conn->A, 2*KEY_LEN+1);
3819
3820
3821 /* --- send H(v(server), P)v(server) ----
3822 */
3823 sh_forward_prep_send (conn,
3824 conn->M1,
3825 sl_strlen(conn->M1),
3826 u,
3827 (conn->head[0]|SH_PROTO_SRP));
3828
3829 SH_FREE(conn->M1);
3830 conn->M1 = NULL;
3831 }
3832
3833 /* client -- third pass
3834 * Message is H(H(u,v),P)u
3835 *
3836 * A := v, verifier := H(password),
3837 */
3838 else if (conn->pass == 3 &&
3839 conn->client_entry != NULL)
3840 {
3841
3842 TPT((0, FIL__, __LINE__,
3843 _("msg=<Authentication - PASS (3).>\n")));
3844
3845 if (0 != check_request_s((char *) &(conn->head[3]), _("PASS"),
3846 conn->peer) ||
3847 sl_strlen(conn->buf) <= KEY_LEN ||
3848 conn->A == NULL)
3849 {
3850 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
3851 _("Invalid client request"), conn->peer);
3852 status_update (conn->client_entry, CLT_FAILED);
3853 conn->client_entry->session_key_timer =
3854 time(NULL) - (2*TIMEOUT_KEY);
3855 sh_forward_do_free (conn);
3856 SL_RET0(_("check_protocol"));
3857 }
3858
3859 /* store random nonce u from client
3860 */
3861 if (conn->K != NULL)
3862 {
3863 SH_FREE(conn->K);
3864 conn->K = NULL;
3865 }
3866 conn->K = SH_ALLOC(KEY_LEN+1);
3867 sl_strlcpy(conn->K, &(conn->buf[KEY_LEN]), KEY_LEN+1);
3868
3869 /* verify random nonce u from client
3870 */
3871 if (conn->M1 != NULL)
3872 {
3873 SH_FREE(conn->M1);
3874 conn->M1 = NULL;
3875 }
3876 conn->M1 = sh_util_strconcat(conn->K, conn->A, NULL);
3877
3878 TPT((0, FIL__, __LINE__, _("msg=<c/r: K = %s>\n"), conn->K));
3879 TPT((0, FIL__, __LINE__, _("msg=<c/r: A = %s>\n"), conn->A));
3880 TPT((0, FIL__, __LINE__, _("msg=<c/r: M = %s>\n"), conn->M1));
3881
3882 sl_strlcpy(hash, sh_tiger_hash (conn->M1,
3883 TIGER_DATA,
3884 sl_strlen(conn->M1),
3885 hashbuf, sizeof(hashbuf)),
3886 KEY_LEN+1);
3887 sh_passwd (hash, conn->client_entry->verifier, NULL, conn->M1);
3888
3889 TPT((0, FIL__, __LINE__, _("msg=<c/r: H = %s>\n"), hash));
3890 TPT((0, FIL__, __LINE__, _("msg=<c/r: P = %s>\n"), conn->M1));
3891
3892 if ( 0 != sl_strncmp(conn->M1, conn->buf, KEY_LEN))
3893 {
3894 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
3895 _("Session key mismatch"), conn->peer);
3896 status_update (conn->client_entry, CLT_FAILED);
3897 conn->client_entry->session_key_timer =
3898 time(NULL) - (2*TIMEOUT_KEY);
3899 sh_forward_do_free (conn);
3900 SL_RET0(_("check_protocol"));
3901 }
3902
3903
3904 /* ---- compute hash key H(v, P, u) ----
3905 */
3906
3907 sh_passwd (conn->A, conn->client_entry->verifier, conn->K,
3908 conn->M1);
3909
3910 sl_strlcpy(conn->client_entry->session_key,
3911 conn->M1, KEY_LEN+1);
3912 TPT((0, FIL__, __LINE__, _("msg=<c/r: Key = %s>\n"),
3913 conn->client_entry->session_key));
3914
3915#ifdef SH_ENCRYPT
3916 err_num = makeKey(&(conn->client_entry->keyInstE),
3917 DIR_ENCRYPT, 192,
3918 conn->client_entry->session_key);
3919 if (err_num < 0)
3920 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
3921 errorExplain(err_num, expbuf, sizeof(expbuf)),
3922 _("check_protocol: makeKey"));
3923 err_num = makeKey(&(conn->client_entry->keyInstD),
3924 DIR_DECRYPT, 192,
3925 conn->client_entry->session_key);
3926 if (err_num < 0)
3927 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
3928 errorExplain(err_num, expbuf, sizeof(expbuf)),
3929 _("check_protocol: makeKey"));
3930#endif
3931
3932 if (conn->K != NULL) SH_FREE (conn->K);
3933 conn->K = NULL;
3934 if (conn->A != NULL) SH_FREE (conn->A);
3935 conn->A = NULL;
3936 if (conn->M1 != NULL) SH_FREE (conn->M1);
3937 conn->M1 = NULL;
3938
3939 /* if (conn->client_entry->status_now == CLT_STARTED */
3940 if (((conn->client_entry->status_now != CLT_INACTIVE) &&
3941 (conn->client_entry->status_now != CLT_EXITED) &&
3942 (conn->client_entry->status_now != CLT_SUSPEND))
3943 && conn->client_entry->session_key_timer > (time_t) 1)
3944 {
3945 status_update (conn->client_entry, CLT_ILLEGAL);
3946 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_ILL,
3947 conn->peer);
3948 }
3949 else if (conn->client_entry->session_key_timer == (time_t) 0)
3950 {
3951 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NEW,
3952 conn->peer);
3953 if (conn->client_entry->status_now != CLT_SUSPEND)
3954 status_update (conn->client_entry, CLT_STARTED);
3955 }
3956
3957 conn->client_entry->session_key_timer = time (NULL);
3958 conn->client_entry->last_connect = time (NULL);
3959
3960 /* put in read state
3961 */
3962 sh_forward_prep_send (conn,
3963 _("AUTH"),
3964 5,
3965 _("AUTH"),
3966 (conn->head[0]|SH_PROTO_SRP));
3967
3968 }
3969 else
3970 {
3971 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
3972 3, conn->pass, conn->peer,
3973 '\\', conn->head[3], '\\', conn->head[4],
3974 '\\', conn->head[5], '\\', conn->head[6]);
3975 sh_forward_do_free (conn);
3976 }
3977 }
3978
3979#else
3980 /* use SRP */
3981
3982
3983 if (state == SH_DO_READ) /* finished reading */
3984 {
3985
3986 TPT((0, FIL__, __LINE__, _("msg=<Authentication - entry.>\n")));
3987
3988 /* first pass -- client request salt
3989 */
3990 if (conn->pass == 1)
3991 {
3992 TPT((0, FIL__, __LINE__,
3993 _("msg=<Authentication - SALT (1).>\n")));
3994
3995 if (conn->buf == NULL)
3996 {
3997 sh_error_handle( (-1), FIL__, __LINE__, 0, MSG_TCP_NOCLT);
3998 sh_forward_do_free (conn);
3999 SL_RET0(_("check_protocol"));
4000 }
4001
4002 /* search the register
4003 */
4004 this_client = search_register(conn, 0);
4005 if (NULL == this_client)
4006 SL_RET0(_("check_protocol"));
4007
4008 conn->client_entry = this_client;
4009 sl_strlcpy (conn->peer, conn->buf, SH_MINIBUF+1);
4010
4011 if (0 != check_request_s((char *)&(conn->head[3]), _("SALT"),
4012 conn->peer))
4013 {
4014 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
4015 _("No salt requested"), conn->peer);
4016 status_update (conn->client_entry, CLT_FAILED);
4017 conn->client_entry->session_key_timer =
4018 time(NULL) - (2*TIMEOUT_KEY);
4019 sh_forward_do_free (conn);
4020 SL_RET0(_("check_protocol"));
4021 }
4022
4023
4024 u[0] = 'I'; u[1] = 'N'; u[2] = 'I'; u[3] = 'T'; u[4] = '\0';
4025
4026 sh_forward_prep_send (conn,
4027 conn->client_entry->salt,
4028 sl_strlen(conn->client_entry->salt),
4029 u,
4030 (conn->head[0]|SH_PROTO_SRP));
4031 }
4032
4033 /* client has sent A -- third pass
4034 */
4035 else if (conn->pass == 3 &&
4036 conn->client_entry != NULL)
4037 {
4038
4039 TPT((0, FIL__, __LINE__,
4040 _("msg=<Authentication - PC01 (3).>\n")));
4041
4042 if (0 != check_request_s((char *)&(conn->head[3]),_("PC01"),conn->peer)||
4043 conn->buf == NULL
4044 )
4045 {
4046 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
4047 _("Invalid client request"), conn->peer);
4048 status_update (conn->client_entry, CLT_FAILED);
4049 conn->client_entry->session_key_timer =
4050 time(NULL) - (2*TIMEOUT_KEY);
4051 sh_forward_do_free (conn);
4052 SL_RET0(_("check_protocol"));
4053 }
4054
4055 if (0 != sh_srp_init())
4056 {
4057 status_update (conn->client_entry, CLT_FAILED);
4058 sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0,
4059 MSG_TCP_EBGN);
4060 sh_forward_do_free (conn);
4061 SL_RET0(_("check_protocol"));
4062 }
4063
4064
4065 /* check A, only send B if correct
4066 */
4067 if ( sl_strlen(conn->buf) < SH_BUFSIZE &&
4068 0 == sh_srp_check_zero (conn->buf) )
4069 {
4070 len = sl_strlen(conn->buf)+1;
4071
4072 if (conn->A != NULL)
4073 {
4074 SH_FREE(conn->A);
4075 conn->A = NULL;
4076 }
4077 conn->A = SH_ALLOC(len);
4078 sl_strlcpy (conn->A, conn->buf, len);
4079
4080 /*
4081 * compute B
4082 */
4083 if (0 != sh_srp_make_a ()) /* b random number */
4084 {
4085 status_update (conn->client_entry, CLT_FAILED);
4086
4087 sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0,
4088 MSG_TCP_EBGN);
4089 sh_srp_exit();
4090 sh_forward_do_free (conn);
4091 SL_RET0(_("check_protocol"));
4092 }
4093
4094 foo_B = sh_srp_B /* B = v + g^b */
4095 (conn->client_entry->verifier);
4096
4097 if (foo_B == NULL)
4098 {
4099 status_update (conn->client_entry, CLT_FAILED);
4100
4101 sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0,
4102 MSG_TCP_EBGN);
4103 sh_srp_exit();
4104 sh_forward_do_free (conn);
4105 SL_RET0(_("check_protocol"));
4106 }
4107
4108 TPT((0, FIL__, __LINE__, _("msg=<srp: A = %s>\n"), conn->A));
4109 TPT((0, FIL__, __LINE__, _("msg=<srp: B = %s>\n"), foo_B));
4110
4111 /*
4112 * create nonce u
4113 */
4114 ticks = (UINT32) taus_get ();
4115
4116 test = (char *) &ticks;
4117 sh_util_cpylong (u, test, 4); /* u nounce */
4118 u[4] = '\0';
4119 sl_strlcpy(conn->challenge,
4120 sh_tiger_hash(u, TIGER_DATA, 4, hashbuf, sizeof(hashbuf)),
4121 SH_CHALLENGE_SIZE);
4122
4123 TPT((0, FIL__, __LINE__, _("msg=<srp: u = %03o-%03o-%03o-%03o>\n"), u[0], u[1], u[2], u[3]));
4124 TPT((0, FIL__, __LINE__, _("msg=<srp: U = %s>\n"),
4125 conn->challenge));
4126
4127 /*
4128 * compute the session key K and M1 = Hash(A,B,K)
4129 */
4130 foo_Ss = sh_srp_S_s (conn->challenge,
4131 conn->A,
4132 conn->client_entry->verifier);
4133 if (foo_Ss == NULL)
4134 {
4135 status_update (conn->client_entry, CLT_FAILED);
4136
4137 sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0,
4138 MSG_TCP_EBGN);
4139 sh_srp_exit();
4140 sh_forward_do_free (conn);
4141 SL_RET0(_("check_protocol"));
4142 }
4143
4144 if (conn->K != NULL)
4145 {
4146 SH_FREE(conn->K);
4147 conn->K = NULL;
4148 }
4149 conn->K = SH_ALLOC(KEY_LEN+1);
4150 sl_strlcpy(conn->K,
4151 sh_tiger_hash(foo_Ss, TIGER_DATA,
4152 sl_strlen(foo_Ss),
4153 hashbuf, sizeof(hashbuf)),
4154 KEY_LEN+1);
4155
4156 if (conn->M1 != NULL)
4157 {
4158 SH_FREE(conn->M1);
4159 conn->M1 = NULL;
4160 }
4161 conn->M1 = SH_ALLOC(KEY_LEN+1);
4162 sh_srp_M (conn->A, foo_B, conn->K, conn->M1, KEY_LEN+1);
4163
4164 TPT((0, FIL__, __LINE__, _("msg=<srp:Ss = %s>\n"), foo_Ss));
4165 TPT((0, FIL__, __LINE__, _("msg=<srp: K = %s>\n"), conn->K));
4166 TPT((0, FIL__, __LINE__, _("msg=<srp:M1 = %s>\n"),conn->M1));
4167
4168 /*
4169 * send B
4170 */
4171 sh_forward_prep_send (conn,
4172 foo_B,
4173 sl_strlen(foo_B)+1,
4174 u,
4175 (conn->head[0]|SH_PROTO_SRP));
4176 if (foo_Ss != NULL)
4177 {
4178 SH_FREE(foo_Ss);
4179 foo_Ss = NULL;
4180 }
4181 if (foo_B != NULL)
4182 {
4183 SH_FREE(foo_B);
4184 foo_B = NULL;
4185 }
4186 }
4187 else
4188 {
4189 status_update (conn->client_entry, CLT_FAILED);
4190
4191 sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0,
4192 MSG_TCP_EZERO);
4193 sh_forward_do_free (conn);
4194 }
4195
4196 sh_srp_exit();
4197 }
4198
4199 /* client has sent M1 -- fifth pass
4200 */
4201 else if (conn->pass == 5 &&
4202 conn->client_entry != NULL)
4203 {
4204 TPT((0, FIL__, __LINE__,
4205 _("msg=<Authentication - PC02 (5).>\n")));
4206
4207 /* check that the state is valid
4208 */
4209 if (0 != check_request_s((char *)&(conn->head[3]), _("PC02"),
4210 conn->peer) ||
4211 conn->A == NULL || conn->K == NULL || conn->M1 == NULL)
4212 {
4213 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
4214 _("Invalid client request"), conn->peer);
4215 status_update (conn->client_entry, CLT_FAILED);
4216 conn->client_entry->session_key_timer =
4217 time(NULL) - (2*TIMEOUT_KEY);
4218 sh_forward_do_free (conn);
4219 SL_RET0(_("check_protocol"));
4220 }
4221
4222 /* ------ verify M1 = H(A, B, K) -------
4223 * ----- send M2 = H(A, M1, K) -------
4224 */
4225 if (conn->buf != NULL &&
4226 sl_strncmp(conn->buf, conn->M1, KEY_LEN) == 0)
4227 {
4228 /*
4229 * send M2
4230 */
4231 char M_buf[KEY_LEN+1];
4232 sh_forward_prep_send (conn,
4233 sh_srp_M (conn->A, conn->M1, conn->K,
4234 M_buf, sizeof(M_buf)),
4235 KEY_LEN+1,
4236 _("PARP"),
4237 (conn->head[0]|SH_PROTO_SRP));
4238
4239 if (conn->A != NULL) SH_FREE(conn->A); conn->A = NULL;
4240 if (conn->M1 != NULL) SH_FREE(conn->M1); conn->M1 = NULL;
4241 sl_strlcpy(conn->client_entry->session_key,
4242 conn->K, KEY_LEN+1);
4243 TPT((0, FIL__, __LINE__, _("msg=<key %s>\n"),
4244 conn->client_entry->session_key));
4245
4246#ifdef SH_ENCRYPT
4247 err_num = makeKey(&(conn->client_entry->keyInstE),
4248 DIR_ENCRYPT, 192,
4249 conn->client_entry->session_key);
4250 if (err_num < 0)
4251 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
4252 errorExplain(err_num, expbuf, sizeof(expbuf)),
4253 _("sh_forward_prep_send_int: makeKey"));
4254 err_num = makeKey(&(conn->client_entry->keyInstD),
4255 DIR_DECRYPT, 192,
4256 conn->client_entry->session_key);
4257 if (err_num < 0)
4258 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
4259 errorExplain(err_num, expbuf, sizeof(expbuf)),
4260 _("sh_forward_prep_send_int: makeKey"));
4261#endif
4262
4263 if (conn->K != NULL) SH_FREE(conn->K); conn->K = NULL;
4264
4265 conn->client_entry->last_connect = time (NULL);
4266
4267 if (((conn->client_entry->status_now != CLT_INACTIVE) &&
4268 (conn->client_entry->status_now != CLT_EXITED) &&
4269 (conn->client_entry->status_now != CLT_SUSPEND))
4270 && conn->client_entry->session_key_timer > (time_t) 1)
4271 {
4272 status_update (conn->client_entry, CLT_ILLEGAL);
4273
4274 sh_error_handle((-1), FIL__, __LINE__, 0,
4275 MSG_TCP_ILL,
4276 conn->peer);
4277 }
4278 else if (conn->client_entry->session_key_timer == (time_t) 0)
4279 {
4280 sh_error_handle((-1), FIL__, __LINE__, 0,
4281 MSG_TCP_NEW,
4282 conn->peer);
4283 if (conn->client_entry->status_now != CLT_SUSPEND)
4284 status_update (conn->client_entry, CLT_STARTED);
4285 }
4286 conn->client_entry->session_key_timer = time (NULL);
4287
4288 }
4289 else
4290 {
4291 status_update (conn->client_entry, CLT_FAILED);
4292 conn->client_entry->session_key_timer =
4293 time(NULL) - (2*TIMEOUT_KEY);
4294
4295 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
4296 _("Session key mismatch"), conn->peer);
4297 sh_forward_do_free (conn);
4298 }
4299 }
4300
4301 else
4302 {
4303 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
4304 4, conn->pass, conn->peer,
4305 '\\', conn->head[3], '\\', conn->head[4],
4306 '\\', conn->head[5], '\\', conn->head[6]);
4307 sh_forward_do_free (conn);
4308 }
4309 }
4310
4311#endif
4312
4313 else if (state == SH_DO_WRITE) /* finished writing */
4314 {
4315 TPT((0, FIL__, __LINE__, _("msg=<Authentication -- (wait).>\n")));
4316
4317 conn->headcount = 0;
4318 conn->bytecount = 0;
4319 conn->bytes_to_send = 0;
4320 conn->bytes_to_get = 0;
4321 if (conn->buf != NULL)
4322 {
4323 SH_FREE(conn->buf);
4324 conn->buf = NULL;
4325 }
4326 conn->state = CONN_READING;
4327 }
4328 }
4329 SL_RET0(_("check_protocol"));
4330}
4331
4332
4333/***********************************************************
4334 *
4335 * SERVER RECEIVE FUNCTION
4336 *
4337 ***********************************************************
4338 */
4339int sh_forward_do_read (sh_conn_t * conn)
4340{
4341 unsigned long byteread; /* bytes read */
4342
4343#ifdef SH_ENCRYPT
4344
4345 unsigned long blkfac = 0;
4346 /* unsigned long length2; */
4347 char * p = NULL, * q = NULL;
4348 RIJ_BYTE inBlock[B_SIZ];
4349 RIJ_BYTE outBlock[B_SIZ];
4350 unsigned int j;
4351 cipherInstance cipherInst;
4352 int err_num;
4353 char expbuf[SH_ERRBUF_SIZE];
4354#endif
4355
4356 SL_ENTER(_("sh_forward_do_read"));
4357
4358 if (conn->state == CONN_SENDING)
4359 {
4360 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_SYNC,
4361 conn->peer);
4362 SL_RETURN( (-1), _("sh_forward_do_read"));
4363 }
4364
4365 if (conn->headcount < SH_HEADER_SIZE)
4366 {
4367 conn->bytes_to_get = SH_HEADER_SIZE - conn->headcount;
4368 byteread = read (conn->fd, &(conn->head[conn->headcount]),
4369 conn->bytes_to_get);
4370 if (byteread > 0 || errno == EINTR)
4371 {
4372 if (byteread > 0)
4373 conn->headcount += byteread;
4374 if (conn->headcount == SH_HEADER_SIZE)
4375 {
4376 conn->bytes_to_get =
4377 (256 * (unsigned int)conn->head[1] +
4378 (unsigned int)conn->head[2]);
4379 SH_SHOWPROT(conn->head, '<');
4380 conn->bytecount = 0;
4381 }
4382 }
4383 else
4384 {
4385 goto conn_reset;
4386 }
4387 SL_RETURN( (0), _("sh_forward_do_read"));
4388 }
4389
4390
4391 /* limit message size
4392 */
4393 /*
4394 conn->bytes_to_get =
4395 (conn->bytes_to_get > (16*SH_BUFSIZE - 1)) ?
4396 (16*SH_BUFSIZE - 1) : conn->bytes_to_get;
4397 */
4398 conn->bytes_to_get = (conn->bytes_to_get > TRANS_BYTES) ?
4399 TRANS_BYTES : conn->bytes_to_get;
4400
4401 if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_get > 0)
4402 {
4403 if ((conn->bytecount > 0) && (conn->bytes_to_get > conn->bytecount))
4404 {
4405 /* do nothing */;
4406 }
4407 else
4408 {
4409 if (conn->buf != NULL)
4410 SH_FREE (conn->buf);
4411 conn->buf = SH_ALLOC(conn->bytes_to_get + 1); /* <= TRANS_BYTES+1 */
4412 conn->bytecount = 0;
4413 }
4414
4415 byteread = read (conn->fd, &(conn->buf[conn->bytecount]),
4416 conn->bytes_to_get - conn->bytecount);
4417 if (byteread > 0 || errno == EINTR)
4418 {
4419 if (byteread > 0)
4420 conn->bytecount += byteread;
4421 if (conn->bytecount == conn->bytes_to_get)
4422 {
4423 ++conn->pass;
4424 /* always terminate with NULL - we might use sl_strcmp()
4425 */
4426 conn->buf[conn->bytecount] = '\0';
4427 conn->state = CONN_PAUSE;
4428
4429#ifdef SH_ENCRYPT
4430 if ((conn->head[0] & SH_PROTO_EN2) != 0) /* if encrypted */
4431 {
4432 conn->buf =
4433 sh_tools_revertPack (conn->head, conn->buf,
4434 &(conn->client_entry->keyInstD),
4435 conn->bytecount);
4436 }
4437 else if ((conn->head[0] & SH_PROTO_ENC) != 0) /* if encrypted */
4438 {
4439 /* Decrypt only complete blocks.
4440 * If there is an incomplete block,
4441 * something is wrong anyway.
4442 * Decrypt in place.
4443 */
4444 blkfac = conn->bytecount / B_SIZ;
4445 /* length2 = (B_SIZ * blkfac); */
4446 p = conn->buf;
4447 q = conn->buf;
4448
4449 err_num = cipherInit (&cipherInst, MODE_CBC, NULL);
4450 if (err_num < 0)
4451 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
4452 errorExplain(err_num, expbuf, sizeof(expbuf)),
4453 _("sh_forward_do_read: cipherInit"));
4454
4455 for (j = 0; j < blkfac; ++j)
4456 {
4457 memcpy(inBlock, p, B_SIZ);
4458 err_num = blockDecrypt(&cipherInst,
4459 &(conn->client_entry->keyInstD),
4460 inBlock, 128 * BNUM, outBlock);
4461 if (err_num < 0)
4462 sh_error_handle((-1), FIL__, __LINE__, -1,
4463 MSG_E_SUBGEN,
4464 errorExplain(err_num, expbuf, sizeof(expbuf)),
4465 _("sh_forward_do_read: blockDecrypt"));
4466 memcpy(q, outBlock, B_SIZ);
4467 p += 16;
4468 q += 16;
4469 }
4470 }
4471#endif
4472
4473 /* ------ HERE CALL check_protocol(conn) ------- */
4474 check_protocol(conn, SH_DO_READ);
4475 }
4476 }
4477 else
4478 {
4479 goto conn_reset;
4480 }
4481 }
4482
4483 else if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_get == 0)
4484 {
4485 if (conn->buf != NULL)
4486 SH_FREE (conn->buf);
4487 conn->buf = NULL;
4488 conn->bytecount = 0;
4489 ++conn->pass;
4490 conn->state = CONN_PAUSE;
4491 /* fprintf(stderr, "\n**** FIXME null read ****\n\n"); */
4492 /* ------ HERE CALL check_protocol(conn) ------- */
4493 check_protocol(conn, SH_DO_READ);
4494 }
4495
4496 SL_RETURN( (0), _("sh_forward_do_read"));
4497
4498 conn_reset:
4499 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_RESET,
4500 conn->peer);
4501 sh_forward_do_free ( conn );
4502 SL_RETURN( (-1), _("sh_forward_do_read"));
4503}
4504
4505#if !defined(O_NONBLOCK)
4506#if defined(O_NDELAY)
4507#define O_NONBLOCK O_NDELAY
4508#else
4509#define O_NONBLOCK 0
4510#endif
4511#endif
4512
4513/* send to the client
4514 */
4515int sh_forward_do_write (sh_conn_t * conn)
4516{
4517 int flags;
4518 long arg = 0;
4519 long bytesent; /* bytes read */
4520
4521 SL_ENTER(_("sh_forward_do_write"));
4522
4523 /* ---- consistency check ------
4524 */
4525 if (conn->state == CONN_READING)
4526 {
4527 sh_error_handle( (-1), FIL__, __LINE__, 0, MSG_TCP_SYNC,
4528 conn->peer);
4529 SL_RETURN( (-1), _("sh_forward_do_write"));
4530 }
4531
4532
4533 flags = retry_fcntl (FIL__, __LINE__, conn->fd, F_GETFL, arg);
4534 retry_fcntl (FIL__, __LINE__, conn->fd, F_SETFL, flags|O_NONBLOCK);
4535
4536 /* ---- send the header ------
4537 */
4538 if (conn->headcount < SH_HEADER_SIZE)
4539 {
4540 conn->bytes_to_send = SH_HEADER_SIZE - conn->headcount;
4541 bytesent = write (conn->fd,
4542 &(conn->head[conn->headcount]),
4543 conn->bytes_to_send);
4544 if (bytesent >= 0 || errno == EINTR || errno == EAGAIN)
4545 {
4546 if (bytesent > 0)
4547 conn->headcount += bytesent;
4548 if (conn->headcount == SH_HEADER_SIZE)
4549 {
4550 conn->bytes_to_send =
4551 (256 * (int)conn->head[1] + (int)conn->head[2]);
4552 }
4553 }
4554 else
4555 {
4556 goto conn_reset_w;
4557 }
4558 if (conn->fd >= 0)
4559 retry_fcntl (FIL__, __LINE__, conn->fd, F_SETFL, flags);
4560 SL_RETURN( (0), _("sh_forward_do_write"));
4561 }
4562
4563
4564 /* ---- send the body ------
4565 */
4566
4567 if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_send > 0 &&
4568 conn->buf != NULL)
4569 {
4570 bytesent = write (conn->fd, &(conn->buf[conn->bytecount]),
4571 conn->bytes_to_send - conn->bytecount);
4572 if (bytesent >= 0 || errno == EINTR || errno == EAGAIN)
4573 {
4574 if (bytesent > 0)
4575 conn->bytecount += bytesent;
4576 if (conn->bytecount == conn->bytes_to_send)
4577 {
4578 ++conn->pass;
4579 conn->state = CONN_PAUSE;
4580 /* ------ HERE CALL check_protocol(conn) ------- */
4581 check_protocol(conn, SH_DO_WRITE);
4582 }
4583 }
4584 else
4585 {
4586 goto conn_reset_w;
4587 }
4588 }
4589
4590 else if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_send == 0)
4591 {
4592 ++conn->pass;
4593 conn->state = CONN_PAUSE;
4594 /* fprintf(stderr, "\n**** FIXME null write ****\n\n"); */
4595 /* ------ HERE CALL check_protocol(conn) ------- */
4596 check_protocol(conn, SH_DO_WRITE);
4597 }
4598
4599 if (conn->fd >= 0)
4600 retry_fcntl (FIL__, __LINE__, conn->fd, F_SETFL, flags);
4601 SL_RETURN( (0), _("sh_forward_do_write"));
4602
4603 conn_reset_w:
4604 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_RESET,
4605 conn->peer);
4606 sh_forward_do_free ( conn );
4607 SL_RETURN( (-1), _("sh_forward_do_write"));
4608}
4609
4610/* accept a connection from a client
4611 */
4612#include <syslog.h>
4613#ifdef SH_USE_LIBWRAP
4614#include <tcpd.h>
4615
4616#ifndef ALLOW_SEVERITY
4617#define ALLOW_SEVERITY LOG_INFO
4618#define DENY_SEVERITY LOG_WARNING
4619#endif
4620
4621int allow_severity;
4622int deny_severity;
4623#endif
4624
4625int sh_forward_accept (int sock, sh_conn_t * newconn)
4626{
4627 int errflag;
4628 int rc;
4629 struct sockaddr_in addr;
4630#ifdef SH_USE_LIBWRAP
4631 struct request_info request;
4632 char errbuf[128];
4633 char daemon[128];
4634#endif
4635
4636 /* handle AIX (size_t addrlen) in wrapper
4637 */
4638 int addrlen = sizeof(addr);
4639
4640 SL_ENTER(_("sh_forward_accept"));
4641
4642 rc = retry_accept(FIL__, __LINE__, sock,
4643 (struct sockaddr *) &addr, &addrlen);
4644
4645 if (rc >= 0)
4646 {
4647
4648 if (addrlen == 0)
4649 {
4650 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
4651 _("Connecting entity unknown"), _("accept"));
4652 newconn->fd = -1;
4653 newconn->state = CONN_FREE;
4654 close(rc);
4655 SL_RETURN( (-1), _("sh_forward_accept"));
4656 }
4657
4658#ifdef SH_USE_LIBWRAP
4659 sl_strlcpy(daemon, SH_INSTALL_NAME, 128);
4660 request_init(&request, RQ_DAEMON, daemon, RQ_FILE, rc, 0);
4661 fromhost(&request);
4662 if (!hosts_access(&request))
4663 {
4664 sl_strlcpy(errbuf, _("Refused connection from "), 128);
4665 sl_strlcat(errbuf, eval_client(&request), 128);
4666
4667 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
4668 errbuf, _("libwrap"));
4669 newconn->fd = -1;
4670 newconn->state = CONN_FREE;
4671 close(rc);
4672 SL_RETURN( (-1), _("sh_forward_accept"));
4673 }
4674#endif
4675
4676 memcpy (&(newconn->addr_peer), &addr, sizeof(struct sockaddr_in));
4677
4678 /* prepare for usage of connection
4679 */
4680 (void) retry_fcntl( FIL__, __LINE__, rc, F_SETFD, 1 );
4681 newconn->fd = rc;
4682 newconn->state = CONN_READING;
4683 newconn->timer = (unsigned long) time (NULL);
4684
4685 if (flag_err_info == SL_TRUE)
4686 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_CNEW, newconn->fd);
4687
4688 SL_RETURN( (0), _("sh_forward_accept"));
4689 }
4690 else
4691 {
4692 char err_buf[SH_ERRBUF_SIZE];
4693 errflag = errno;
4694 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
4695 sh_error_message(errflag,err_buf, sizeof(err_buf)), _("accept"));
4696 newconn->fd = -1;
4697 newconn->state = CONN_FREE;
4698 SL_RETURN( (-1), _("sh_forward_accept"));
4699 }
4700}
4701
4702extern char sh_sig_msg[64]; /* defined in sh_unix.c */
4703
4704/* ------------ port and interface -------
4705 */
4706static unsigned int server_port = SH_DEFAULT_PORT;
4707
4708int sh_forward_set_port (const char * str)
4709{
4710 int retval = 0;
4711 unsigned long i;
4712 char * endptr;
4713
4714 SL_ENTER(_("sh_forward_set_port"));
4715 i = strtoul (str, &endptr, 0);
4716 if (endptr == str) {
4717 retval = -1;
4718 } else if (i > 65535) {
4719 retval = -1;
4720 } else {
4721 server_port = i;
4722 }
4723 SL_RETURN( (retval), _("sh_forward_set_port"));
4724}
4725
4726static struct in_addr server_interface;
4727static int use_server_interface = 0;
4728
4729int sh_forward_set_interface (const char * str)
4730{
4731 if (0 == strcmp(str, _("INADDR_ANY")))
4732 {
4733 use_server_interface = 0;
4734 return 0;
4735 }
4736 if (0 == /*@-unrecog@*/inet_aton(str, &server_interface)/*@+unrecog@*/)
4737 {
4738 use_server_interface = 0;
4739 return -1;
4740 }
4741 use_server_interface = 1;
4742 return 0;
4743}
4744
4745/* ------------ print error --------------
4746 */
4747struct sock_err_st {
4748 char msg[128];
4749 int errnum;
4750 int port;
4751 int line;
4752 int euid;
4753};
4754
4755static struct sock_err_st sock_err[2];
4756
4757void sh_forward_printerr(char * str, int errnum, unsigned int port, int line)
4758{
4759 int slot = 0;
4760
4761 if (port != server_port)
4762 slot = 1;
4763 if (str == NULL)
4764 sock_err[slot].msg[0] = '\0';
4765 else
4766 sl_strlcpy(sock_err[slot].msg, str, 128);
4767 sock_err[slot].errnum = errnum;
4768 sock_err[slot].port = port;
4769 sock_err[slot].line = line;
4770 sock_err[slot].euid = (int) geteuid();
4771}
4772
4773int sh_forward_printerr_final(int slot)
4774{
4775 char errbuf[SH_ERRBUF_SIZE];
4776
4777 SL_ENTER(_("sh_forward_printerr_final"));
4778 if (sock_err[slot].msg[0] != '\0')
4779 {
4780 dlog(1, FIL__, __LINE__,
4781 _("Could not set up the listening socket for the server because of the\nfollowing error: %s\nPossible reasons include:\n - insufficient privilege for UID %d, or\n - the port %d is already used by another program.\n"),
4782 sh_error_message(sock_err[slot].errnum, errbuf, sizeof(errbuf)),
4783 sock_err[slot].euid,
4784 sock_err[slot].port);
4785 sh_error_handle((-1), FIL__, sock_err[slot].line,
4786 sock_err[slot].errnum, MSG_EXIT_ABORTS,
4787 sh_error_message(sock_err[slot].errnum, errbuf, sizeof(errbuf)),
4788 sh.prg_name,
4789 sock_err[slot].msg);
4790 SL_RETURN((-1), _("sh_forward_printerr_final"));
4791 }
4792 SL_RETURN(0, _("sh_forward_printerr_final"));
4793}
4794
4795static sh_conn_t * conns = NULL;
4796#define TIME_OUT_DEF 300
4797static int maxconn = 0; /* maximum number of simultaneous connections */
4798
4799
4800#ifdef INET_SYSLOG
4801#define INET_SUSPEND_TIME 180 /* equal to 3 minutes */
4802#define SH_MINSOCK 3
4803int create_syslog_socket (int flag);
4804static int recv_syslog_socket (int fd);
4805static int syslog_sock = -1;
4806#else
4807#define SH_MINSOCK 2
4808#endif
4809
4810extern int pf_unix_fd;
4811
4812/* the tcp socket, and the function to establish it
4813 */
4814static int sh_tcp_sock = -1;
4815
4816int sh_create_tcp_socket ()
4817{
4818 struct sockaddr_in addr;
4819 int addrlen = sizeof(addr);
4820
4821 int sock = -1;
4822 int errnum = 0;
4823 int flag = 1; /* non-zero to enable an option */
4824
4825 SL_ENTER(_("sh_create_tcp_socket"));
4826
4827 sh_forward_printerr (NULL, 0, server_port, __LINE__);
4828
4829 /* create the socket, bind() it and listen()
4830 */
4831 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
4832 {
4833 errnum = errno;
4834 sh_forward_printerr (_("socket"), errnum, server_port, __LINE__);
4835 SL_RETURN((-1), _("sl_create_tcp_socket"));
4836 }
4837 (void) retry_fcntl( FIL__, __LINE__, sock, F_SETFD, 1 );
4838
4839 if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
4840 (void *) &flag, sizeof(flag)) < 0 )
4841 {
4842 errnum = errno;
4843 sh_forward_printerr (_("setsockopt"), errnum, server_port, __LINE__);
4844 SL_RETURN((-1), _("sl_create_tcp_socket"));
4845 }
4846
4847 addr.sin_family = AF_INET;
4848 addr.sin_port = htons(server_port);
4849 if (use_server_interface == 0)
4850 addr.sin_addr.s_addr = INADDR_ANY;
4851 else
4852 memcpy(&addr.sin_addr, &server_interface, sizeof(struct in_addr));
4853
4854 if ( bind(sock, (struct sockaddr *) &addr, addrlen) < 0)
4855 {
4856 errnum = errno;
4857 sh_forward_printerr (_("bind"), errnum, server_port, __LINE__);
4858 SL_RETURN((-1), _("sl_create_tcp_socket"));
4859 }
4860
4861 if ( retry_fcntl( FIL__, __LINE__, sock, F_SETFL, O_NONBLOCK ) < 0 )
4862 {
4863 errnum = errno;
4864 sh_forward_printerr (_("fcntl"), errnum, server_port, __LINE__);
4865 SL_RETURN((-1), _("sl_create_tcp_socket"));
4866 }
4867
4868 if ( listen(sock, 5) < 0)
4869 {
4870 errnum = errno;
4871 sh_forward_printerr (_("listen"), errnum, server_port, __LINE__);
4872 SL_RETURN((-1), _("sl_create_tcp_socket"));
4873 }
4874
4875 sh_tcp_sock = sock;
4876
4877 SL_RETURN((sock), _("sl_create_tcp_socket"));
4878}
4879
4880/*****************************************
4881 *
4882 * This is the server main loop.
4883 *
4884 * The server is set up for listening, and
4885 * and starts a select() loop.
4886 *
4887 *****************************************/
4888
4889void sh_receive()
4890{
4891#ifdef SH_USE_XML
4892 extern int sh_log_file (char * message, char * inet_peer);
4893#endif
4894
4895 int sock = -1;
4896 sh_conn_t * cx;
4897 fd_set readset;
4898 fd_set writeset;
4899 struct timeval tv;
4900 int num_sel;
4901 int errnum;
4902 int nowconn;
4903 int status;
4904 int high_fd;
4905 register int i;
4906 long dummy = 0;
4907 unsigned long time_now;
4908 unsigned long time_last = 0;
4909 unsigned long time_out = TIME_OUT_DEF;
4910
4911 time_t told;
4912 time_t tcurrent;
4913
4914 unsigned long tchkold;
4915
4916 struct sigaction new_act;
4917 struct sigaction old_act;
4918
4919 SL_ENTER(_("sh_receive"));
4920
4921 /* ignore SIGPIPE (instead get EPIPE if connection is closed)
4922 * --- we have called sh_unix_init() already ---
4923 */
4924 new_act.sa_handler = SIG_IGN;
4925 sigemptyset( &new_act.sa_mask ); /* set an empty mask */
4926 new_act.sa_flags = 0; /* init sa_flags */
4927 retry_sigaction (FIL__, __LINE__, SIGPIPE, &new_act, &old_act);
4928
4929 if ( sh_forward_printerr_final(0) < 0)
4930 {
4931 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
4932 }
4933 sock = sh_tcp_sock;
4934
4935 /* ****************************************************************
4936 *
4937 * This is a non-forking server. We use select() on the listen()
4938 * socket to watch for new connections. For new connections, accept()
4939 * will return a new socket that is put in the read/write filesets.
4940 * Data about active connections are kept in the 'conns' table.
4941 *
4942 ******************************************************************/
4943
4944 /* The table to hold info on sockets.
4945 * We reserve 6 file descriptors for misc. use.
4946 * The POSIX lower limit on open files seems to be eight.
4947 */
4948 maxconn = get_open_max() - 6;
4949 maxconn = (((int)FD_SETSIZE) < maxconn) ? FD_SETSIZE : maxconn;
4950
4951 if (maxconn < 0 || !sl_ok_muls(maxconn, sizeof(sh_conn_t)))
4952 {
4953 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_START_SRV,
4954 0, sock);
4955 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
4956 }
4957 conns = SH_ALLOC (sizeof(sh_conn_t) * maxconn);
4958
4959 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_START_SRV,
4960 (maxconn-1), sock);
4961
4962 /* timer
4963 */
4964 tcurrent = (unsigned long) time (NULL);
4965 told = tcurrent;
4966
4967 tchkold = tcurrent;
4968
4969 for (i = SH_MINSOCK; i < maxconn; ++i)
4970 {
4971 conns[i].buf = NULL;
4972 conns[i].K = NULL;
4973 conns[i].A = NULL;
4974 conns[i].M1 = NULL;
4975 conns[i].FileName = NULL;
4976 conns[i].fd = -1;
4977 sh_forward_do_free ( &conns[i]);
4978 }
4979
4980 /* status init
4981 */
4982 server_status.conn_open = 0;
4983 server_status.conn_total = 0;
4984 server_status.conn_max = maxconn-1;
4985 server_status.start = time (NULL);
4986 server_status.last = (time_t) 0;
4987
4988 nowconn = 1;
4989 tv.tv_sec = 5;
4990 tv.tv_usec = 0;
4991
4992 /* conns[0] is the listen() socket. Always in read mode.
4993 */
4994 conns[0].fd = sock;
4995 conns[0].state = CONN_READING;
4996 high_fd = sock;
4997
4998 conns[1].fd = pf_unix_fd;
4999 conns[1].state = CONN_READING;
5000 high_fd = (pf_unix_fd > high_fd) ? pf_unix_fd : high_fd;
5001
5002#ifdef INET_SYSLOG
5003 conns[2].fd = -1;
5004 if ( sh_forward_printerr_final(1) < 0)
5005 {
5006 SH_FREE(conns);
5007 conns = NULL;
5008 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
5009 }
5010 sock = syslog_sock;
5011
5012 if (sock >= 0)
5013 {
5014 conns[2].fd = sock;
5015 conns[2].state = CONN_READING;
5016 high_fd = (high_fd > conns[2].fd) ? high_fd : conns[2].fd;
5017 }
5018#endif
5019
5020 sh_html_write(all_clients);
5021
5022 /* This is the select() loop.
5023 */
5024 while (1 == 1)
5025 {
5026
5027 if (sig_raised > 0)
5028 {
5029 TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
5030
5031 if (sig_termfast == 1) /* SIGTERM */
5032 {
5033 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
5034 strncpy (sh_sig_msg, _("SIGTERM"), 20);
5035 --sig_raised; --sig_urgent;
5036 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
5037 }
5038
5039 if (sig_config_read_again == 1)
5040 {
5041 TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")));
5042 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
5043
5044
5045 /* -- Delete the name server cache. --
5046 */
5047
5048 delete_cache();
5049#if defined(WITH_EXTERNAL)
5050 /* -- Delete list of external tasks. --
5051 */
5052 (void) sh_ext_cleanup();
5053#endif
5054 /* - mark all clients dead
5055 * - read configuration file
5056 * - remove clients still dead
5057 */
5058 sh_forward_mark_dead ();
5059
5060#if defined(SH_WITH_MAIL)
5061 reset_count_dev_mail();
5062#endif
5063 reset_count_dev_console();
5064 reset_count_dev_time();
5065 sl_trust_purge_user();
5066
5067 (void) sh_readconf_read ();
5068 for (i = SH_MINSOCK; i < maxconn; ++i)
5069 if (conns[i].state != CONN_FREE &&
5070 conns[i].client_entry != NULL &&
5071 conns[i].client_entry->dead_flag == 1)
5072 sh_forward_do_free ( &conns[i]);
5073 sh_forward_clean_tree ();
5074
5075 sig_config_read_again = 0;
5076 --sig_raised;
5077 }
5078
5079 if (sig_fresh_trail == 1) /* SIGIOT */
5080 {
5081 /* Logfile access
5082 */
5083#ifdef SH_USE_XML
5084 sh_log_file (NULL, NULL);
5085#endif
5086 TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
5087 sh_error_only_stderr (S_TRUE);
5088 sh_unix_rm_lock_file(sh.srvlog.name);
5089 retry_msleep(3, 0);
5090 sh.flag.log_start = S_TRUE;
5091 sh_error_only_stderr (S_FALSE);
5092 sig_fresh_trail = 0;
5093 --sig_raised;
5094 }
5095
5096
5097 if (sig_terminate == 1 && nowconn < 2) /* SIGQUIT */
5098 {
5099 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
5100 strncpy (sh_sig_msg, _("SIGQUIT"), 20);
5101 --sig_raised; --sig_urgent;
5102 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
5103 }
5104
5105
5106 if (sig_debug_switch == 1) /* SIGUSR1 */
5107 {
5108 TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
5109 sh_error_dbg_switch();
5110 sig_debug_switch = 0;
5111 --sig_raised;
5112 }
5113
5114 if (sig_suspend_switch > 0) /* SIGUSR2 */
5115 {
5116 TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
5117 if (sh_global_suspend_flag == 1) {
5118 sh_global_suspend_flag = 0;
5119 } else {
5120 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND,
5121 sh.prg_name);
5122 sh_global_suspend_flag = 1;
5123 }
5124 --sig_suspend_switch;
5125 --sig_raised; --sig_urgent;
5126 }
5127
5128 sig_raised = (sig_raised < 0) ? 0 : sig_raised;
5129 sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
5130 TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
5131 }
5132
5133 if (sh_global_suspend_flag == 1)
5134 {
5135 (void) retry_msleep (1, 0);
5136 continue;
5137 }
5138
5139 /* Recompute the descriptor set. select() modifies it,
5140 * thus we update it using the info from the connection table.
5141 * Also recompute the number of open connections.
5142 */
5143 FD_ZERO( &readset );
5144 FD_ZERO( &writeset );
5145 FD_SET(conns[0].fd, &readset );
5146 high_fd = conns[0].fd;
5147
5148 if (conns[1].fd > -1)
5149 {
5150 FD_SET(conns[1].fd, &readset );
5151 high_fd = (high_fd > conns[1].fd) ? high_fd : conns[1].fd;
5152 }
5153
5154#ifdef INET_SYSLOG
5155 if (conns[2].fd > -1)
5156 {
5157 FD_SET(conns[2].fd, &readset );
5158 high_fd = (high_fd > conns[2].fd) ? high_fd : conns[2].fd;
5159 }
5160#endif
5161
5162 time_now = (unsigned long) time (NULL);
5163 nowconn = 1;
5164
5165 for (i = SH_MINSOCK; i < maxconn; ++i)
5166 {
5167 /* eliminate timed out connections
5168 */
5169 if (conns[i].state != CONN_FREE)
5170 {
5171 if (time_now-conns[i].timer > time_out)
5172 {
5173 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_TIMOUT,
5174 conns[i].peer);
5175 sh_forward_do_free ( &conns[i]);
5176 }
5177 else
5178 ++nowconn;
5179 }
5180
5181
5182 if (conns[i].state == CONN_READING)
5183 {
5184 FD_SET(conns[i].fd, &readset);
5185 high_fd = (high_fd < conns[i].fd ? conns[i].fd : high_fd);
5186 }
5187 else if (conns[i].state == CONN_SENDING)
5188 {
5189 FD_SET(conns[i].fd, &writeset);
5190 high_fd = (high_fd < conns[i].fd ? conns[i].fd : high_fd);
5191 }
5192 }
5193
5194 /* -- Exponentially reduce timeout limit if more than 1/2 full. --
5195 */
5196 if (nowconn > (maxconn/2))
5197 time_out = ( (time_out/2) > 1) ? (time_out/2) : 1;
5198 else
5199 time_out = TIME_OUT_DEF;
5200
5201
5202
5203 /* -- Do the select(). --
5204 */
5205 num_sel = select(high_fd+1, &readset, &writeset, NULL, &tv);
5206 errnum = errno;
5207
5208 /* reset timeout - modified by select() on some systems
5209 */
5210 tv.tv_sec = 5;
5211 tv.tv_usec = 0;
5212
5213
5214 if ( (time_now - time_last) > 2L)
5215 {
5216 time_last = time_now;
5217 if (sh_html_write(all_clients) < 0)
5218 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_HTML);
5219 }
5220
5221
5222 /* Error handling.
5223 */
5224 if ( num_sel < 0 ) /* some error */
5225 {
5226 char errbuf[SH_ERRBUF_SIZE];
5227
5228 if (sig_raised == 1)
5229 {
5230 sig_raised = 2;
5231 continue;
5232 }
5233
5234 if ( errnum == EINTR)
5235 continue; /* try again */
5236
5237 if ( errnum == EBADF)
5238 {
5239 /* seek and destroy the bad fd
5240 */
5241 for (i = SH_MINSOCK; i < high_fd; ++i)
5242 {
5243 if ((conns[i].state == CONN_READING) ||
5244 (conns[i].state == CONN_SENDING))
5245 {
5246 if (-1 == retry_fcntl(FIL__, __LINE__,
5247 conns[i].fd, F_GETFL, dummy))
5248 sh_forward_do_free ( &conns[i]);
5249 }
5250 }
5251 continue;
5252 }
5253
5254 sh_error_handle((-1), FIL__, __LINE__, errnum, MSG_EXIT_ABORTS,
5255 sh_error_message(errnum, errbuf, sizeof(errbuf)),
5256 sh.prg_name,
5257 _("select"));
5258 aud_exit(FIL__, __LINE__, EXIT_FAILURE );
5259 }
5260
5261
5262 /* log the timestamp
5263 */
5264 if ((tcurrent - told) > sh.looptime )
5265 {
5266 told = tcurrent;
5267#ifdef MEM_DEBUG
5268 sh_mem_check();
5269 sh_unix_count_mlock();
5270#else
5271 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
5272#endif
5273 }
5274
5275#if defined(SH_WITH_MAIL)
5276 /*
5277 * flush the mail queue
5278 */
5279 if (tcurrent - sh.mailTime.alarm_last > sh.mailTime.alarm_interval)
5280 {
5281 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
5282 (void) sh_mail_msg (NULL);
5283 sh.mailTime.alarm_last = tcurrent;
5284 }
5285#endif
5286#ifdef MEM_DEBUG
5287 sh_mem_dump();
5288#endif
5289
5290 tcurrent = (unsigned long) time (NULL);
5291
5292 /* check for time limit exceeded
5293 */
5294 if ((tcurrent - tchkold) > (unsigned int) 3 )
5295 {
5296 tchkold = tcurrent;
5297 client_time_check(/* all_clients */);
5298 }
5299
5300 /* seed / re-seed the PRNG if required
5301 */
5302 (void) taus_seed();
5303
5304 /* select() timeout handling.
5305 */
5306 if ( num_sel == 0 ) /* timeout - no connection */
5307 {
5308 if (sh_html_write(all_clients) < 0)
5309 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_HTML);
5310 continue;
5311 }
5312
5313 /* New connection.
5314 */
5315 if ( FD_ISSET(conns[0].fd , &readset )) /* a new connection */
5316 {
5317 --num_sel;
5318 status = 0;
5319 if (nowconn < maxconn && sig_terminate == 0 && sig_termfast == 0)
5320 {
5321 i = SH_MINSOCK;
5322 while (i < maxconn)
5323 {
5324 if (conns[i].state == CONN_FREE)
5325 {
5326 status = sh_forward_accept (conns[0].fd, &conns[i]);
5327 if (status == 0)
5328 {
5329 high_fd =
5330 (high_fd > conns[i].fd ? high_fd : conns[i].fd);
5331 ++server_status.conn_open;
5332 ++server_status.conn_total;
5333 server_status.last = time (NULL);
5334 }
5335 break;
5336 }
5337 ++i;
5338 }
5339 }
5340 if (status == 0)
5341 continue;
5342 }
5343
5344 /* check for commands on the socket
5345 */
5346 if (conns[1].fd > (-1) && FD_ISSET(conns[1].fd , &readset ))
5347 {
5348 sh_socket_poll();
5349 }
5350
5351#ifdef INET_SYSLOG
5352 if (conns[2].fd > (-1) && FD_ISSET(conns[2].fd , &readset ))
5353 {
5354 recv_syslog_socket (conns[2].fd);
5355 }
5356#endif
5357
5358 /* Check for pending read/write on the rest of the sockets.
5359 */
5360 for ( i = SH_MINSOCK; num_sel > 0 && i < maxconn; ++i )
5361 {
5362 if (sig_termfast == 1)
5363 break;
5364
5365 cx = &conns[i];
5366 if ( cx->state == CONN_READING &&
5367 FD_ISSET( cx->fd, &readset ) )
5368 {
5369 --num_sel;
5370 sh_forward_do_read ( cx );
5371 }
5372 else if ( cx->state == CONN_SENDING &&
5373 FD_ISSET( cx->fd, &writeset ) )
5374 {
5375 --num_sel;
5376 sh_forward_do_write ( cx );
5377 }
5378 }
5379 /* continue */
5380 }
5381 /* notreached */
5382}
5383
5384void free_client_tree ()
5385{
5386 SL_ENTER(_("free_client_tree"));
5387 zAVLFreeTree (all_clients, free_client);
5388 SL_RET0(_("free_client_tree"));
5389}
5390
5391void sh_forward_free_all ()
5392{
5393 register int i;
5394
5395 SL_ENTER(_("sh_forward_free_all"));
5396
5397 if (conns != NULL)
5398 for (i = SH_MINSOCK; i < maxconn; ++i)
5399 {
5400 sh_forward_do_free ( &conns[i]);
5401 }
5402
5403
5404 free_client_tree ();
5405
5406 if (conns != NULL)
5407 SH_FREE (conns);
5408
5409 SL_RET0(_("sh_forward_free_all"));
5410}
5411
5412#ifdef INET_SYSLOG
5413
5414#ifdef HAVE_INET_ATON
5415static char * my_inet_ntoa(struct in_addr in)
5416{
5417 return inet_ntoa(in);
5418}
5419#else
5420static char * my_inet_ntoa(struct in_addr in)
5421{
5422 unsigned char a, b, c, d;
5423 static char foo[16];
5424 char bar[4];
5425 memcpy (bar, &(in.s_addr), 4); /* memory alignment (?) */
5426 memcpy (&a, &bar[0], 1);
5427 memcpy (&b, &bar[1], 1);
5428 memcpy (&c, &bar[2], 1);
5429 memcpy (&d, &bar[3], 1);
5430 sprintf(foo, "%d.%d.%d.%d", /* known to fit */
5431 (int) a, (int) b, (int) c, (int) d);
5432 return foo;
5433}
5434#endif
5435
5436
5437/* Unlike Linux / FreeBSD, most systems don't define the stuff below
5438 * in syslog.h
5439 */
5440
5441#ifndef LOG_FAC
5442#define LOG_FAC(p) (((p) & LOG_FACMASK) >> 3)
5443#endif
5444
5445#ifndef LOG_PRI
5446#define LOG_PRI(p) ((p) & LOG_PRIMASK)
5447#endif
5448
5449typedef struct sh_code {
5450 char *c_name;
5451 int c_val;
5452} SH_CODE;
5453
5454SH_CODE sh_facilitynames[] =
5455{
5456#ifdef LOG_AUTH
5457 { N_("auth"), LOG_AUTH },
5458#endif
5459#ifdef LOG_AUTHPRIV
5460 { N_("authpriv"), LOG_AUTHPRIV },
5461#endif
5462#ifdef LOG_CRON
5463 { N_("cron"), LOG_CRON },
5464#endif
5465#ifdef LOG_DAEMON
5466 { N_("daemon"), LOG_DAEMON },
5467#endif
5468#ifdef LOG_FTP
5469 { N_("ftp"), LOG_FTP },
5470#endif
5471#ifdef LOG_KERN
5472 { N_("kern"), LOG_KERN },
5473#endif
5474#ifdef LOG_LPR
5475 { N_("lpr"), LOG_LPR },
5476#endif
5477#ifdef LOG_MAIL
5478 { N_("mail"), LOG_MAIL },
5479#endif
5480#ifdef INTERNAL_MARK
5481 { N_("mark"), INTERNAL_MARK }, /* INTERNAL */
5482#endif
5483#ifdef LOG_NEWS
5484 { N_("news"), LOG_NEWS },
5485#endif
5486#ifdef LOG_AUTH
5487 { N_("security"), LOG_AUTH }, /* DEPRECATED */
5488#endif
5489#ifdef LOG_SYSLOG
5490 { N_("syslog"), LOG_SYSLOG },
5491#endif
5492#ifdef LOG_USER
5493 { N_("user"), LOG_USER },
5494#endif
5495#ifdef LOG_UUCP
5496 { N_("uucp"), LOG_UUCP },
5497#endif
5498#ifdef LOG_LOCAL0
5499 { N_("local0"), LOG_LOCAL0 },
5500#endif
5501#ifdef LOG_LOCAL1
5502 { N_("local1"), LOG_LOCAL1 },
5503#endif
5504#ifdef LOG_LOCAL2
5505 { N_("local2"), LOG_LOCAL2 },
5506#endif
5507#ifdef LOG_LOCAL3
5508 { N_("local3"), LOG_LOCAL3 },
5509#endif
5510#ifdef LOG_LOCAL4
5511 { N_("local4"), LOG_LOCAL4 },
5512#endif
5513#ifdef LOG_LOCAL5
5514 { N_("local5"), LOG_LOCAL5 },
5515#endif
5516#ifdef LOG_LOCAL6
5517 { N_("local6"), LOG_LOCAL6 },
5518#endif
5519#ifdef LOG_LOCAL7
5520 { N_("local7"), LOG_LOCAL7 },
5521#endif
5522 { NULL, -1 }
5523};
5524
5525
5526SH_CODE sh_prioritynames[] =
5527{
5528#ifdef LOG_ALERT
5529 { N_("alert"), LOG_ALERT },
5530#endif
5531#ifdef LOG_CRIT
5532 { N_("crit"), LOG_CRIT },
5533#endif
5534#ifdef LOG_DEBUG
5535 { N_("debug"), LOG_DEBUG },
5536#endif
5537#ifdef LOG_EMERG
5538 { N_("emerg"), LOG_EMERG },
5539#endif
5540#ifdef LOG_ERR
5541 { N_("err"), LOG_ERR },
5542#endif
5543#ifdef LOG_ERR
5544 { N_("error"), LOG_ERR }, /* DEPRECATED */
5545#endif
5546#ifdef LOG_INFO
5547 { N_("info"), LOG_INFO },
5548#endif
5549#ifdef INTERNAL_NOPRI
5550 { N_("none"), INTERNAL_NOPRI }, /* INTERNAL */
5551#endif
5552#ifdef LOG_NOTICE
5553 { N_("notice"), LOG_NOTICE },
5554#endif
5555#ifdef LOG_EMERG
5556 { N_("panic"), LOG_EMERG }, /* DEPRECATED */
5557#endif
5558#ifdef LOG_WARNING
5559 { N_("warn"), LOG_WARNING }, /* DEPRECATED */
5560#endif
5561#ifdef LOG_WARNING
5562 { N_("warning"), LOG_WARNING },
5563#endif
5564 { NULL, -1 }
5565};
5566
5567static int enable_syslog_socket = S_FALSE;
5568
5569static int recv_syslog_socket (int fd)
5570{
5571 static time_t return_next = 0;
5572 int priority = 0;
5573 int fac, pri;
5574 int i;
5575 char * cfac = NULL;
5576 char * cpri = NULL;
5577 int res;
5578 char * tmp;
5579 char * bptr;
5580 char * ptr = NULL;
5581 char buf[1048];
5582 struct sockaddr_in from;
5583 char errbuf[SH_ERRBUF_SIZE];
5584
5585 /* The 6th argument in recvfrom is *socklen_t in Linux and *BSD,
5586 * but *int everywhere else. Because socklen_t is unsigned int, there
5587 * should be no problem as long as sizeof(struct sockaddr_in) < INT_MAX ...
5588 */
5589 int fromlen = sizeof(from);
5590
5591 if (enable_syslog_socket == S_FALSE)
5592 return 0;
5593
5594 SL_ENTER(_("recv_syslog_socket"));
5595
5596 if (return_next > 0)
5597 {
5598 if ( (time(NULL) - return_next) < 2)
5599 SL_RETURN( 0, _("recv_syslog_socket"));
5600 else
5601 return_next = 0;
5602 }
5603
5604 res = recvfrom(fd, buf, 1047, 0, (struct sockaddr *) &from, &fromlen);
5605
5606 if (res > 0)
5607 {
5608 res = (res < 1047) ? res : 1047;
5609 buf[res] = '\0';
5610 if (res > 1 && buf[res-1] == '\n')
5611 buf[res-1] = '\0';
5612
5613 /* here we expect an xml formatted message, thus we don't
5614 escape xml special chars (flag == 0) */
5615 /* commented out to not escape twice */
5616 /* bptr = sh_tools_safe_name(buf, 0); */
5617 bptr = buf;
5618
5619 if (!bptr || !(*bptr))
5620 {
5621 res = errno;
5622 TPT(( 0, FIL__, __LINE__, _("msg=<UDP error: %d>\n"), res));
5623 sh_error_handle((-1), FIL__, __LINE__, res, MSG_ERR_SYSLOG,
5624 sh_error_message(res, errbuf, sizeof(errbuf)),
5625 my_inet_ntoa(from.sin_addr));
5626 SL_RETURN( (-1), _("recv_syslog_socket"));
5627 }
5628
5629 TPT(( 0, FIL__, __LINE__, _("msg=<UDP message from %s>\n"),
5630 my_inet_ntoa(from.sin_addr)));
5631 ptr = bptr;
5632 i = 0;
5633 if (*ptr == '<')
5634 {
5635 ++ptr; ++i;
5636 while (i < res &&
5637 (unsigned char) *ptr > 47 && (unsigned char) *ptr < 58)
5638 {
5639 priority = 10 * priority + (*ptr - '0');
5640 ++ptr;
5641 ++i;
5642 }
5643 if (*ptr == '>')
5644 ++ptr;
5645 }
5646 fac = LOG_FAC(priority);
5647 i = 0;
5648 while (sh_facilitynames[i].c_name != NULL)
5649 {
5650 if (sh_facilitynames[i].c_val == (fac<<3))
5651 { cfac = sh_util_strdup(_(sh_facilitynames[i].c_name)); break; }
5652 ++i;
5653 }
5654 pri = LOG_PRI(priority);
5655 i = 0;
5656 while (sh_prioritynames[i].c_name != NULL)
5657 {
5658 if (sh_prioritynames[i].c_val == pri)
5659 { cpri = sh_util_strdup(_(sh_prioritynames[i].c_name)); break; }
5660 ++i;
5661 }
5662
5663 /* here we do not expect an xml formatted message, thus we escape
5664 xml special chars (flag == 1) */
5665 tmp = sh_tools_safe_name (ptr, 1);
5666 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_INET_SYSLOG,
5667 my_inet_ntoa(from.sin_addr),
5668 (cfac == NULL) ? _("none") : cfac,
5669 (cpri == NULL) ? _("none") : cpri,
5670 (ptr == NULL) ? _("none") : ptr);
5671 if (cfac != NULL)
5672 SH_FREE(cfac);
5673 if (cpri != NULL)
5674 SH_FREE(cpri);
5675 SH_FREE(tmp);
5676 /* SH_FREE(bptr); */
5677 }
5678
5679 else if (res < 0 && errno != EINTR)
5680 {
5681 res = errno;
5682 TPT(( 0, FIL__, __LINE__, _("msg=<UDP error: %d>\n"), res));
5683 sh_error_handle((-1), FIL__, __LINE__, res, MSG_ERR_SYSLOG,
5684 sh_error_message(res, errbuf, sizeof(errbuf)),
5685 my_inet_ntoa(from.sin_addr));
5686
5687 /* don't accept anything the next 2 seconds
5688 */
5689 return_next = time(NULL);
5690 SL_RETURN( (-1), _("recv_syslog_socket"));
5691 }
5692 SL_RETURN( (0), _("recv_syslog_socket"));
5693}
5694
5695int set_syslog_active(const char * c)
5696{
5697 return sh_util_flagval(c, &enable_syslog_socket);
5698}
5699
5700/* callerFlag == S_TRUE means override the enable_syslog_socket flag
5701 */
5702int create_syslog_socket (int callerFlag)
5703{
5704 int flag = 1; /* non-zero to enable an option */
5705 int sock;
5706 int errnum;
5707 int res;
5708 struct sockaddr_in addr;
5709 int addrlen = sizeof(addr);
5710
5711 SL_ENTER(_("create_syslog_socket"));
5712
5713 if (callerFlag == S_FALSE)
5714 {
5715 if (enable_syslog_socket == S_FALSE && syslog_sock >= 0)
5716 {
5717 /* user does not wish to use this facility
5718 */
5719 TPT(( 0, FIL__, __LINE__, _("msg=<close syslog socket>\n")));
5720 close(syslog_sock);
5721 syslog_sock = -1;
5722 }
5723 SL_RETURN((-1), _("create_syslog_socket"));
5724 }
5725
5726 sh_forward_printerr (NULL, 0, 514, __LINE__);
5727
5728 /* create the socket, bind() it and listen()
5729 */
5730 sock = socket(AF_INET, SOCK_DGRAM, 0);
5731
5732 if (sock < 0)
5733 {
5734 errnum = errno;
5735 sh_forward_printerr (_("syslog socket"), errnum, 514, __LINE__);
5736 SL_RETURN((-1), _("create_syslog_socket"));
5737 }
5738 (void) retry_fcntl( FIL__, __LINE__, sock, F_SETFD, 1 );
5739
5740 if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
5741 (void *) &flag, sizeof(flag)) < 0 )
5742 {
5743 errnum = errno;
5744 sh_forward_printerr (_("syslog setsockopt SO_REUSEADDR"),
5745 errnum, 514, __LINE__);
5746 SL_RETURN((-1), _("create_syslog_socket"));
5747 }
5748
5749#if defined(SO_BSDCOMPAT)
5750 if ( setsockopt(sock, SOL_SOCKET, SO_BSDCOMPAT,
5751 (void *) &flag, sizeof(flag)) < 0 )
5752 {
5753 errnum = errno;
5754 sh_forward_printerr (_("syslog setsockopt SO_BSDCOMPAT"),
5755 errnum, 514, __LINE__);
5756 SL_RETURN((-1), _("create_syslog_socket"));
5757 }
5758#endif
5759
5760 memset(&addr, 0, sizeof(addr));
5761 addr.sin_family = AF_INET;
5762 addr.sin_port = htons(514);
5763
5764 res = bind(sock, (struct sockaddr *) &addr, addrlen);
5765
5766 if ( res < 0)
5767 {
5768 errnum = errno;
5769 sh_forward_printerr (_("syslog bind"), errnum, 514, __LINE__);
5770 close(sock);
5771 SL_RETURN((-1), _("create_syslog_socket"));
5772 }
5773
5774 syslog_sock = sock;
5775
5776 SL_RETURN((sock), _("create_syslog_socket"));
5777}
5778/* #ifdef INET_SYSLOG */
5779#endif
5780
5781
5782
5783/* #ifdef SH_WITH_SERVER */
5784#endif
5785
5786
5787
5788
5789
5790
Note: See TracBrowser for help on using the repository browser.