source: trunk/src/sh_xfer_server.c@ 566

Last change on this file since 566 was 561, checked in by katerina, 3 years ago

Fix for ticket #451 (signify-openbsd in client/server mode not working).

File size: 92.7 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#include <ctype.h>
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
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 "sh_ipvx.h"
124#include "samhain.h"
125#include "sh_tiger.h"
126#include "sh_utils.h"
127#include "sh_unix.h"
128#include "sh_xfer.h"
129#include "sh_srp.h"
130#include "sh_fifo.h"
131#include "sh_tools.h"
132#include "sh_entropy.h"
133#include "sh_html.h"
134#include "sh_nmail.h"
135#include "sh_socket.h"
136#define SH_NEED_GETHOSTBYXXX
137#include "sh_static.h"
138#include "sh_guid.h"
139
140#ifdef SH_ENCRYPT
141#include "rijndael-api-fst.h"
142char * sh_tools_makePack (unsigned char * header, int flag,
143 char * payload, unsigned long payload_size,
144 keyInstance * keyInstE);
145char * sh_tools_revertPack (unsigned char * header, int flag, char * message,
146 keyInstance * keyInstE,
147 unsigned long message_size);
148#endif
149
150/* define this if you want to debug the client/server communication */
151/* #define SH_DBG_PROT 1 */
152
153#ifdef SH_DBG_PROT
154#define SH_SHOWPROT(c,d) sh_tools_show_header((c), (d))
155#else
156#define SH_SHOWPROT(c,d)
157#endif
158
159/* the port client will be connecting to
160 */
161#ifndef SH_DEFAULT_PORT
162#define SH_DEFAULT_PORT 49777
163#endif
164
165#ifndef SH_SELECT_REPEAT
166#define SH_SELECT_REPEAT 60
167#endif
168
169#ifndef SH_HEADER_SIZE
170#define SH_HEADER_SIZE 7
171#endif
172
173#ifndef SH_CHALLENGE_SIZE
174#define SH_CHALLENGE_SIZE 9
175#endif
176
177#undef FIL__
178#define FIL__ _("sh_xfer_server.c")
179
180int clt_class = (-1);
181
182extern int flag_err_debug;
183extern int flag_err_info;
184
185
186#if defined (SH_WITH_SERVER)
187
188#if defined(WITH_TRACE) || defined(WITH_TPT)
189extern char * hu_trans(const char * ihu);
190#endif
191extern unsigned int ServerPort;
192#if !defined(USE_SRP_PROTOCOL)
193extern void sh_passwd (char * salt, char * password, char * nounce, char *hash);
194#endif
195
196static int StripDomain = S_TRUE;
197
198int sh_xfer_set_strip (const char * str)
199{
200 static int fromcl = 0;
201
202 if (fromcl == 1)
203 return 0;
204 else
205 return (sh_util_flagval(str, &StripDomain));
206}
207
208static char * sh_strip_domain (char *name)
209{
210 char * out = NULL;
211
212 SL_ENTER(_("sh_strip_domain"));
213
214 if (StripDomain == S_FALSE || strchr(name, '.') == NULL)
215 {
216 out = sh_util_strdup(name);
217 SL_RETURN( out, _("sh_strip_domain"));
218 }
219 else
220 {
221 /* check whether it is in dotted number format
222 * --> last part must be kept
223 */
224 if (0 != sh_ipvx_is_numeric(name))
225 {
226 out = sh_util_strdup(name);
227 SL_RETURN( out, _("sh_strip_domain"));
228 }
229 else
230 {
231 char * p;
232 out = sh_util_strdup(name);
233 p = strchr(out, '.');
234 if (p) *p = '\0';
235 SL_RETURN( out, _("sh_strip_domain"));
236 }
237 }
238
239 SL_RETURN( out, _("sh_strip_domain"));
240}
241
242#ifndef USE_SRP_PROTOCOL
243
244int sh_xfer_make_client (const char * str)
245{
246 /* char * safer; */
247 char key[KEY_LEN+1];
248 unsigned char in[PW_LEN+1];
249 int i = 0, j, k, l = 0;
250 char hashbuf[KEYBUF_SIZE];
251
252 if (sl_strlen(str) != (PW_LEN * 2))
253 {
254 fprintf(stderr,
255 _("Input must be a %d digit hexadecimal number"\
256 " (only 0-9, a-f, A-F allowed in input)\n"),
257 (PW_LEN * 2));
258 _exit(EXIT_FAILURE);
259 }
260
261 while (i < (PW_LEN * 2))
262 {
263 k = sh_util_hexchar(str[i]); j = sh_util_hexchar(str[i+1]);
264 if (k != -1 && j != -1)
265 {
266 in[l] = (k * 16 + j);
267 ++l; i+= 2;
268 }
269 else
270 {
271 fprintf(stderr, _("Invalid char %c\n"), str[i]);
272 _exit(EXIT_FAILURE);
273 }
274 }
275 in[PW_LEN] = '\0';
276
277 sl_strlcpy ((char *)key,
278 sh_tiger_hash ((char*)in, TIGER_DATA, PW_LEN,
279 hashbuf, sizeof(hashbuf)),
280 KEY_LEN+1);
281 key[KEY_LEN] = '\0';
282
283 fprintf(stdout, _("Client entry: Client=HOSTNAME@00000000@%s\n"),
284 key);
285 fflush(stdout);
286
287 _exit(EXIT_SUCCESS);
288 return 0;
289}
290
291#else
292
293int sh_xfer_make_client (const char * str)
294{
295 char * foo_v;
296
297 char salt[17];
298 char key[KEY_LEN+1];
299 char in[PW_LEN];
300 int i = 0, j, k, l = 0;
301 char hashbuf[KEYBUF_SIZE];
302
303 if (sl_strlen(str) != (PW_LEN*2))
304 {
305 fprintf(stderr,
306 _("Input must be a %d digit hexadecimal number"\
307 " (only 0-9, a-f, A-F allowed in input)\n"),
308 (PW_LEN*2));
309 _exit(EXIT_FAILURE);
310 }
311
312 while (i < (PW_LEN*2))
313 {
314 k = sh_util_hexchar(str[i]); j = sh_util_hexchar(str[i+1]);
315 if (k != -1 && j != -1)
316 {
317 in[l] = (k * 16 + j);
318 ++l; i+= 2;
319 }
320 else
321 {
322 fprintf(stderr, _("Invalid char %c\n"), str[i]);
323 _exit(EXIT_FAILURE);
324 }
325 }
326
327
328 if (0 == sh_srp_init())
329 {
330 sh_util_keyinit(key, KEY_LEN);
331 sl_strlcpy(salt, sh_tiger_hash(key, TIGER_DATA, KEY_LEN,
332 hashbuf, sizeof(hashbuf)),
333 17);
334 sh_srp_x (salt, in);
335 foo_v = sh_srp_verifier ();
336 fprintf(stdout, _("Client=HOSTNAME@%s@%s\n"),
337 salt, foo_v);
338 fflush(stdout);
339 SH_FREE(foo_v);
340 sh_srp_exit();
341 _exit(EXIT_SUCCESS);
342 }
343 fprintf(stdout, "%s",_("ERROR initializing BigNum library.\n"));
344 fflush (stdout);
345 _exit(EXIT_FAILURE);
346 return -1;
347}
348#endif
349
350
351int sh_xfer_create_password (const char * dummy)
352{
353 UINT32 val[2];
354 char output[KEY_LEN+1];
355 char hashbuf[KEYBUF_SIZE];
356
357 val[0] = taus_get ();
358 val[1] = taus_get ();
359
360 sl_strlcpy (output,
361 sh_tiger_hash((char *)(&val[0]), TIGER_DATA, 2*sizeof(UINT32),
362 hashbuf, sizeof(hashbuf)),
363 KEY_LEN);
364
365 output[16] = '\0';
366
367 fprintf(stdout, _("%s\n"), output);
368 fflush (stdout);
369
370 if (dummy)
371 _exit(EXIT_SUCCESS);
372 else
373 _exit(EXIT_SUCCESS);
374 return (0); /* avoid compiler warning */
375}
376
377/* #if defined (SH_WITH_SERVER) */
378#endif
379
380/**************************************************
381 *
382 *
383 * S E R V E R
384 *
385 *
386 ***************************************************/
387
388#ifdef SH_WITH_SERVER
389
390#include "sh_readconf.h"
391
392
393#define CONN_FREE 0
394#define CONN_READING 1
395#define CONN_SENDING 2
396#define CONN_PAUSE 3
397#define CONN_BUSY 4
398
399char * clt_stat[] = {
400 N_("Inactive"),
401 N_("Started"),
402 N_("ILLEGAL"),
403 N_("FAILED"),
404 N_("Exited"),
405 N_("PANIC"),
406 N_("POLICY"),
407 N_("File_transfer"),
408 N_("Message"),
409 N_("TIMEOUT_EXCEEDED"),
410 N_("Suspended"),
411 N_("Filecheck"),
412};
413
414#include <time.h>
415
416/* in sh_html.h:
417 * typedef struct client_entry {
418 * } client_t;
419 */
420
421#include "zAVLTree.h"
422
423static char * sh_tolower (char * s)
424{
425 char * ret = s;
426 if (s)
427 {
428 for (; *s; ++s)
429 {
430 *s = tolower((unsigned char) *s);
431 }
432 }
433 return ret;
434}
435
436/* Function to return the key for indexing
437 * the argument
438 */
439zAVLKey sh_avl_key (void const * arg)
440{
441 const client_t * sa = (const client_t *) arg;
442 return (zAVLKey) sa->hostname;
443}
444
445zAVLTree * all_clients = NULL;
446
447void sh_xfer_html_write()
448{
449 SL_ENTER(_("sh_xfer_html_write"));
450 sh_html_write(all_clients);
451 SL_RET0(_("sh_xfer_html_write"));
452}
453
454
455int sh_xfer_use_clt_class (const char * c)
456{
457 int i;
458 SL_ENTER(_("sh_xfer_use_clt_class"));
459 i = sh_util_flagval(c, &(sh.flag.client_class));
460 SL_RETURN(i, _("sh_xfer_use_clt_class"));
461}
462
463int sh_xfer_use_clt_sev (const char * c)
464{
465 int i;
466 SL_ENTER(_("sh_xfer_use_clt_sev"));
467 i = sh_util_flagval(c, &(sh.flag.client_severity));
468 SL_RETURN(i, _("sh_xfer_use_clt_sev"));
469}
470
471
472/* the destructor
473 */
474void free_client(void * inptr)
475{
476 client_t * here;
477
478 SL_ENTER(_("free_client"));
479 if (inptr == NULL)
480 SL_RET0(_("free_client"));
481 else
482 here = (client_t *) inptr;
483
484 if (here->hostname != NULL)
485 SH_FREE(here->hostname);
486 if (here->salt != NULL)
487 SH_FREE(here->salt);
488 if (here->verifier != NULL)
489 SH_FREE(here->verifier);
490 SH_FREE(here);
491 SL_RET0(_("free_client"));
492}
493
494
495int sh_xfer_register_client (const char * str)
496{
497 client_t * newclt;
498 client_t * testclt;
499
500 const char * ptr;
501 int sepnum = 0;
502 int sep[2];
503 register int i = 0;
504 int siz_str = 0;
505
506 SL_ENTER(_("sh_xfer_register_client"));
507
508 ptr = str;
509 while (*ptr) {
510 if (*ptr == '@' && sepnum < 2 )
511 {
512 sep[sepnum] = i;
513 ++sepnum;
514 }
515 ++ptr; ++i;
516 }
517
518 if (all_clients == NULL)
519 {
520 all_clients = zAVLAllocTree (sh_avl_key, zAVL_KEY_STRING);
521 if (all_clients == NULL)
522 {
523 (void) safe_logger (0, 0, NULL);
524 aud__exit(FIL__, __LINE__, EXIT_FAILURE);
525 }
526 }
527
528 if ((sepnum == 2) && (sep[0] > 0) && (sep[1] > sep[0]))
529 {
530 newclt = SH_ALLOC (sizeof(client_t));
531 newclt->hostname = SH_ALLOC (sep[0]+1);
532 newclt->salt = SH_ALLOC (sep[1]-sep[0]);
533 newclt->verifier = SH_ALLOC (sl_strlen(str)-sep[1]+1);
534 newclt->exit_flag = 0;
535 newclt->dead_flag = 0;
536#ifdef SH_ENCRYPT
537 newclt->encf_flag = SH_PROTO_ENC;
538 newclt->ency_flag = SH_PROTO_ENC;
539#else
540 newclt->encf_flag = 0;
541 newclt->ency_flag = 0;
542#endif
543 newclt->ivst_flag = 0;
544 newclt->session_key[0] = '\0';
545 newclt->last_connect = (time_t) 0;
546 newclt->session_key_timer = (time_t) 0;
547 newclt->status_now = CLT_INACTIVE;
548 for (i = 0; i < CLT_MAX; ++i)
549 newclt->status_arr[i] = CLT_INACTIVE;
550 (void) sh_unix_time(0, newclt->timestamp[CLT_INACTIVE], TIM_MAX);
551
552 /* truncate */
553 sl_strlcpy(newclt->hostname, &str[0], sep[0]+1);
554 sh_tolower(newclt->hostname);
555
556 /* truncate */
557 sl_strlcpy(newclt->salt, &str[sep[0]+1], sep[1]-sep[0]);
558 sl_strlcpy(newclt->verifier, &str[sep[1]+1], sl_strlen(str)-sep[1]+1);
559
560 testclt = (client_t *) zAVLSearch (all_clients, newclt->hostname);
561
562 if (testclt != NULL)
563 {
564 SH_FREE(testclt->verifier);
565 siz_str = strlen (newclt->verifier) + 1;
566 testclt->verifier = SH_ALLOC (siz_str);
567 sl_strlcpy(testclt->verifier, newclt->verifier, siz_str);
568
569 SH_FREE(testclt->salt);
570 siz_str = strlen (newclt->salt) + 1;
571 testclt->salt = SH_ALLOC (siz_str);
572 sl_strlcpy(testclt->salt, newclt->salt, siz_str);
573
574 testclt->dead_flag = 0;
575
576 free_client(newclt);
577 SL_RETURN( 0, _("sh_xfer_register_client"));
578 }
579 else
580 {
581 if (0 == zAVLInsert (all_clients, newclt))
582 {
583 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_CREG,
584 newclt->hostname,
585 newclt->salt, newclt->verifier);
586 SL_RETURN( 0, _("sh_xfer_register_client"));
587 }
588 }
589 }
590 SL_RETURN (-1, _("sh_xfer_register_client"));
591}
592
593typedef struct {
594 int state;
595 int fd;
596 char * buf;
597 unsigned char head[SH_HEADER_SIZE];
598 char challenge[SH_CHALLENGE_SIZE];
599 char peer[SH_MINIBUF+1];
600 client_t * client_entry;
601 char * K;
602 char * M1;
603 char * A;
604 int headcount;
605 unsigned long bytecount;
606 unsigned long bytes_to_send;
607 unsigned long bytes_to_get;
608 int pass;
609 unsigned long timer;
610
611 char * FileName;
612 unsigned long FileLength;
613 unsigned long FileSent;
614 char FileType[5];
615
616 struct sh_sockaddr addr_peer;
617} sh_conn_t;
618
619
620static char zap_challenge[SH_CHALLENGE_SIZE] = { 0 };
621
622void sh_xfer_do_free (sh_conn_t * conn)
623{
624 SL_ENTER(_("sh_xfer_do_free"));
625
626 if (conn->K != NULL)
627 {
628 SH_FREE(conn->K);
629 conn->K = NULL;
630 }
631 if (conn->A != NULL)
632 {
633 SH_FREE(conn->A);
634 conn->A = NULL;
635 }
636 if (conn->M1 != NULL)
637 {
638 SH_FREE(conn->M1);
639 conn->M1 = NULL;
640 }
641 if (conn->buf != NULL)
642 {
643 SH_FREE(conn->buf);
644 conn->buf = NULL;
645 }
646 if (conn->fd != (-1))
647 {
648 sl_close_fd (FIL__, __LINE__, conn->fd);
649 conn->fd = -1;
650 }
651 memcpy(conn->challenge, zap_challenge, SH_CHALLENGE_SIZE);
652 conn->state = CONN_FREE;
653 conn->headcount = 0;
654 conn->bytecount = 0;
655 conn->bytes_to_send = 0;
656 conn->bytes_to_get = 0;
657 conn->pass = 0;
658 conn->timer = 0;
659 conn->client_entry = NULL;
660
661 if (conn->FileName != NULL)
662 {
663 SH_FREE(conn->FileName);
664 conn->FileName = NULL;
665 }
666 conn->FileLength = 0;
667 conn->FileSent = 0;
668 conn->FileType[0] = '\0';
669 conn->FileType[1] = '\0';
670 conn->FileType[2] = '\0';
671 conn->FileType[3] = '\0';
672 conn->FileType[4] = '\0';
673
674 --server_status.conn_open;
675
676 SL_RET0(_("sh_xfer_do_free"));
677}
678
679/****************************************
680 *
681 * -- Reconfiguration. --
682 *
683 * (1) Mark all clients as 'dead'.
684 * (2) Reload configuration - clients
685 * in config are non-dead now.
686 * (3) Remove all clients still
687 * marked as 'dead'.
688 */
689
690/* -- Mark all clients as dead.
691 */
692void sh_xfer_mark_dead (void)
693{
694 zAVLCursor avlcursor;
695 client_t * item;
696
697 SL_ENTER(_("sh_xfer_mark_dead"));
698
699 for (item = (client_t *) zAVLFirst(&avlcursor, all_clients); item;
700 item = (client_t *) zAVLNext(&avlcursor))
701 {
702 item->dead_flag = 1;
703 }
704 SL_RET0(_("sh_xfer_mark_dead"));
705}
706
707
708/* -- Clean tree from dead clients.
709 */
710void sh_xfer_clean_tree (void)
711{
712 zAVLCursor avlcursor;
713 client_t * item;
714
715 SL_ENTER(_("sh_xfer_clean_tree"));
716
717 repeat_search:
718
719 for (item = (client_t *) zAVLFirst(&avlcursor, all_clients); item;
720 item = (client_t *) zAVLNext(&avlcursor))
721 {
722 if (item->dead_flag == 1)
723 {
724 zAVLDelete (all_clients, item->hostname);
725 free_client (item);
726 goto repeat_search;
727 }
728 }
729 SL_RET0(_("sh_xfer_clean_tree"));
730}
731
732/*
733 *
734 **********************************************/
735
736
737
738/* -- SERVER SEND FUNCTION. --
739 */
740void sh_xfer_prep_send_int (sh_conn_t * conn,
741 char * msg, unsigned long length,
742 char * u, char protocol,
743 int docrypt)
744{
745 /* register unsigned long i; */
746 unsigned long length2;
747
748#if !defined(SH_ENCRYPT)
749 (void) docrypt;
750#endif
751
752 SL_ENTER(_("sh_xfer_prep_send_int"));
753
754 TPT((0, FIL__, __LINE__, _("msg=<%s>, docrypt=<%d>\n"), msg, docrypt ));
755
756 length2 = length;
757
758 conn->headcount = 0;
759 conn->bytecount = 0;
760 conn->bytes_to_send = 0;
761 conn->bytes_to_get = 0;
762
763 if (conn->buf != NULL)
764 {
765 SH_FREE(conn->buf);
766 conn->buf = NULL;
767 }
768
769 put_header (conn->head, protocol, &length2, u);
770 SH_SHOWPROT(conn->head,'>');
771
772 TPT((0, FIL__, __LINE__, _("msg=<put_header done>\n") ));
773
774 if (msg == NULL)
775 length2 = 0;
776
777#ifdef SH_ENCRYPT
778 if ((S_TRUE == docrypt) && ((protocol & SH_PROTO_ENC) != 0))
779 {
780 TPT((0, FIL__, __LINE__, _("encrypting (version 2)\n")));
781
782 conn->buf = sh_tools_makePack (conn->head, conn->client_entry->ivst_flag,
783 msg, length2,
784 &(conn->client_entry->keyInstE));
785 }
786 else if (msg == NULL)
787 {
788 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
789 _("msg is NULL"),
790 _("sh_xfer_prep_send_int: cipherInit"));
791 }
792 else
793 {
794 if ((length2 + 1) < length2) --length2;
795 conn->buf = SH_ALLOC(length2 + 1);
796
797 memcpy(conn->buf, msg, length2);
798 conn->buf[length2] = '\0';
799 TPT((0, FIL__, __LINE__, _("msg=<no encryption done>\n") ));
800 }
801#else
802 if ((length2 + 1) < length2) --length2;
803 conn->buf = SH_ALLOC(length2 + 1);
804
805 memcpy(conn->buf, msg, length2);
806 conn->buf[length2] = '\0';
807 TPT((0, FIL__, __LINE__, _("msg=<no encryption done>\n") ));
808#endif
809
810 conn->state = CONN_SENDING;
811 SL_RET0(_("sh_xfer_prep_send_int"));
812}
813
814/* -- Send/Receive. --
815 */
816void sh_xfer_prep_send (sh_conn_t * conn,
817 char * msg, unsigned long length,
818 char * u, char protocol)
819{
820 SL_ENTER(_("sh_xfer_prep_send"));
821 sh_xfer_prep_send_int (conn, msg, length, u, protocol, S_FALSE);
822 SL_RET0(_("sh_xfer_prep_send"));
823}
824
825void sh_xfer_send_crypt (sh_conn_t * conn,
826 char * msg, unsigned long length,
827 char * u, char protocol)
828{
829 SL_ENTER(_("sh_xfer_send_crypt"));
830 sh_xfer_prep_send_int (conn, msg, length, u, protocol, S_TRUE);
831 SL_RET0(_("sh_xfer_send_crypt"));
832}
833
834/* #include <sys/times.h> */
835
836#if defined(WITH_EXTERNAL)
837#include "sh_extern.h"
838#endif
839
840/* -- Update the client status. --
841 *
842 * Update the status array for the client,
843 * and eventually call external program.
844 */
845static void status_update (client_t * conn, int status)
846{
847#if defined(WITH_EXTERNAL)
848 char msg[2 * SH_MINIBUF + TIM_MAX + 3];
849#endif
850
851 SL_ENTER(_("status_update"));
852
853 if (conn == NULL ||
854 status < 0 || status >= CLT_MAX)
855 SL_RET0(_("status_update"));
856
857 conn->status_now = status;
858 conn->status_arr[status] = status;
859 (void) sh_unix_time(0, conn->timestamp[status], TIM_MAX);
860
861#if defined(WITH_EXTERNAL)
862 sl_snprintf(msg, sizeof(msg), _("%s %s %s"),
863 conn->hostname, conn->timestamp[status], _(clt_stat[status]));
864 sh_ext_execute('s', 'r', 'v', msg, 0);
865#endif
866
867 SL_RET0(_("status_update"));
868}
869
870static time_t time_client_limit = 86400;
871
872int sh_xfer_set_time_limit (const char * c)
873{
874 long val;
875
876 SL_ENTER(_("sh_xfer_set_time_limit"));
877
878 val = strtol (c, (char **)NULL, 10);
879 if (val <= 0)
880 SL_RETURN( (-1), _("sh_xfer_set_time_limit"));
881
882 time_client_limit = (time_t) val;
883 SL_RETURN( (0), _("sh_xfer_set_time_limit"));
884}
885
886
887/* -- Check for time limit exceeded. --
888 */
889static int client_time_check(void)
890{
891 zAVLCursor avlcursor;
892 client_t * item;
893
894 SL_ENTER(_("client_time_check"));
895
896 if (time_client_limit == (time_t) 0)
897 SL_RETURN( 0, _("client_time_check"));
898
899 for (item = (client_t *) zAVLFirst(&avlcursor, all_clients); item;
900 item = (client_t *) zAVLNext(&avlcursor))
901 {
902 if (item->exit_flag == 0 && item->last_connect != (time_t) 0)
903 {
904 if ( (time(NULL) - item->last_connect) > time_client_limit)
905 {
906 if (item->status_now != CLT_TOOLONG)
907 {
908 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_TIMEXC,
909 item->hostname);
910 status_update (item, CLT_TOOLONG);
911 }
912 }
913 }
914 }
915 SL_RETURN( 0, _("client_time_check"));
916}
917
918static int lookup_err = SH_ERR_SEVERE;
919
920int sh_xfer_lookup_level (const char * c)
921{
922 int ci = sh_error_convert_level (c);
923
924 SL_ENTER(_("sh_xfer_lookup_level"));
925
926 if (ci >= 0)
927 {
928 lookup_err = ci;
929 SL_RETURN( 0, _("sh_xfer_lookup_level"));
930 }
931 else
932 SL_RETURN( (-1), _("sh_xfer_lookup_level"));
933}
934
935#ifndef MAXHOSTNAMELEN
936#define MAXHOSTNAMELEN 127
937#endif
938
939int check_addr (const char * claim, struct sh_sockaddr * addr_peer)
940{
941 char h_name[MAXHOSTNAMELEN + 1];
942 char h_peer[MAXHOSTNAMELEN + 1];
943 char h_peer_IP[SH_IP_BUF];
944 char tmp_peer_IP[SH_IP_BUF];
945 char * canonical;
946 char numeric[SH_IP_BUF];
947
948 SL_ENTER(_("check_addr"));
949
950 if (claim == NULL)
951 {
952 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
953 _("NULL input"), _("check_addr"));
954 SL_RETURN ((-1), _("check_addr"));
955 }
956
957 /* Make sure we have the canonical name for the client
958 */
959 canonical = sh_ipvx_canonical(claim, numeric, sizeof(numeric));
960
961 /* copy canonical name into h_name
962 */
963 if (canonical != NULL)
964 {
965 sl_strlcpy(h_name, canonical, MAXHOSTNAMELEN + 1);
966 SH_FREE(canonical);
967 }
968 else
969 {
970 sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_RESCLT,
971 claim);
972 SL_RETURN ((0), _("check_addr"));
973 }
974
975
976 /* get canonical name of socket peer
977 */
978 canonical = sh_ipvx_addrtoname(addr_peer);
979
980 if (canonical)
981 {
982 if (0 == sl_strcmp(canonical, _("localhost")))
983 sl_strlcpy(h_peer, sh.host.name, MAXHOSTNAMELEN + 1);
984 else
985 sl_strlcpy(h_peer, canonical, MAXHOSTNAMELEN + 1);
986 SH_FREE(canonical);
987 }
988 else
989 {
990 sh_ipvx_ntoa (tmp_peer_IP, sizeof(tmp_peer_IP), addr_peer);
991 sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_RESPEER,
992 claim, tmp_peer_IP);
993 SL_RETURN ((0), _("check_addr"));
994 }
995
996 sh_ipvx_ntoa (h_peer_IP, sizeof(h_peer_IP), addr_peer);
997
998 /* reverse lookup
999 */
1000 if (0 == sh_ipvx_reverse_check_ok (h_peer, ServerPort, addr_peer))
1001 {
1002 sh_ipvx_ntoa (tmp_peer_IP, sizeof(tmp_peer_IP), addr_peer);
1003
1004 sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_LOOKERS,
1005 claim, h_peer, tmp_peer_IP);
1006 SL_RETURN ((0), _("check_addr"));
1007 }
1008
1009 /* Check whether claim and peer are identical
1010 */
1011 sh_tolower(h_peer); /* Canonical name of what the peer is */
1012 sh_tolower(h_name); /* Canonical name of what the peer claims */
1013
1014 if ((0 == sl_strcmp(h_peer, h_name)) || (0 == sl_strcmp(h_peer_IP, h_name)))
1015 {
1016 SL_RETURN ((0), _("check_addr"));
1017 }
1018#if !defined(USE_IPVX)
1019 else
1020 {
1021 struct hostent * he = sh_gethostbyname(h_peer);
1022 int i = 0;
1023 int flag = 0;
1024
1025 while (he->h_aliases[i] != NULL)
1026 {
1027 if (0 == sl_strcmp(sh_tolower(he->h_aliases[i]), h_name))
1028 {
1029 flag = 1;
1030 break;
1031 }
1032 ++i;
1033 }
1034 if (flag == 0)
1035 sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_LOOKUP,
1036 claim, h_peer);
1037 }
1038#endif
1039
1040 SL_RETURN ((0), _("check_addr"));
1041}
1042
1043static int UseSocketPeer = S_FALSE;
1044
1045int set_socket_peer (const char * c)
1046{
1047 return sh_util_flagval(c, &UseSocketPeer);
1048}
1049
1050
1051/* -- Search register. --
1052 */
1053client_t * search_register(sh_conn_t * conn, int pos)
1054{
1055 client_t * this_client;
1056 char peer_ip[SH_IP_BUF];
1057 char numerical[SH_IP_BUF];
1058 char peer_name[MAXHOSTNAMELEN+1];
1059 char * search_string;
1060
1061 struct sh_sockaddr peer_addr;
1062 char * canonical;
1063
1064 SL_ENTER(_("search_register"));
1065
1066 if (UseSocketPeer == S_TRUE)
1067 {
1068 memcpy(&peer_addr, &(conn->addr_peer), sizeof(struct sh_sockaddr));
1069 sh_ipvx_ntoa (peer_ip, sizeof(peer_ip), &peer_addr);
1070 peer_name[0] = '\0';
1071
1072 /* get canonical name of socket peer
1073 */
1074 canonical = sh_ipvx_canonical(peer_ip, numerical, sizeof(numerical));
1075
1076 if (canonical != NULL)
1077 {
1078 if (0 == sl_strcmp(canonical, _("localhost")))
1079 sl_strlcpy(peer_name, sh.host.name, MAXHOSTNAMELEN + 1);
1080 else
1081 sl_strlcpy(peer_name, canonical, MAXHOSTNAMELEN + 1);
1082 SH_FREE(canonical);
1083 }
1084
1085 if (0 == sh_ipvx_reverse_check_ok (peer_name, ServerPort, &peer_addr))
1086 {
1087 sl_strlcpy(peer_name, peer_ip, MAXHOSTNAMELEN + 1);
1088 }
1089
1090 search_string = peer_name;
1091 }
1092 else
1093 {
1094 search_string = &(conn->buf[pos]);
1095
1096 if (0 != check_addr (search_string, &(conn->addr_peer)))
1097 {
1098 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
1099 _("Reverse lookup failed"), search_string);
1100 sh_xfer_do_free (conn);
1101 SL_RETURN( NULL, _("search_register"));
1102 }
1103 }
1104
1105 sh_tolower(search_string);
1106
1107 /* ---- search the register -----
1108 */
1109 this_client = zAVLSearch(all_clients, search_string);
1110
1111 if (this_client == NULL)
1112 {
1113 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
1114 _("Not in client list"), search_string);
1115 sh_xfer_do_free (conn);
1116 SL_RETURN( NULL, _("search_register"));
1117 }
1118 if (this_client->exit_flag == 1)
1119 {
1120 TPT((0, FIL__, __LINE__, _("msg=<this_client->exit_flag == 1>\n")));
1121 this_client->session_key_timer = (time_t) 0;
1122 this_client->session_key[0] = '\0';
1123 this_client->exit_flag = 0;
1124 }
1125 TPT((0, FIL__, __LINE__, _("msg=<search_register: client %s>\n"),
1126 this_client->hostname));
1127 TPT((0, FIL__, __LINE__, _("msg=<search_register: key %s>\n"),
1128 this_client->session_key));
1129 SL_RETURN( this_client, _("search_register"));
1130}
1131
1132client_t * do_check_client(sh_conn_t * conn, int * retval)
1133{
1134 client_t * this_client = NULL;
1135 char sigbuf[KEYBUF_SIZE];
1136
1137 *retval = 0;
1138
1139 TPT(( 0, FIL__, __LINE__, _("msg=<Client connect - HELO (1).>\n")));
1140
1141 if (conn->buf == NULL || sl_strlen(conn->buf) <= KEY_LEN)
1142 {
1143 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOCLT);
1144 sh_xfer_do_free (conn);
1145 return NULL;
1146 }
1147
1148 /* ---- search the register -----
1149 */
1150
1151 this_client = search_register (conn, KEY_LEN);
1152 if (this_client == NULL)
1153 return NULL;
1154
1155 /* ---- force authentication -----
1156 */
1157
1158 if (this_client->session_key[0] == '\0' ||
1159 (time(NULL) - this_client->session_key_timer)
1160 > (time_t) TIMEOUT_KEY )
1161 {
1162 size_t len;
1163
1164 /* fake an auth request and jump there
1165 */
1166 conn->head[0] = (conn->head[0] | SH_PROTO_SRP);
1167 conn->head[3] = 'S';
1168 conn->head[4] = 'A';
1169 conn->head[5] = 'L';
1170 conn->head[6] = 'T';
1171 if (flag_err_info == S_TRUE)
1172 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FAUTH,
1173 &(conn->buf[KEY_LEN]));
1174 len = sl_strlen(&(conn->buf[KEY_LEN])) + 1;
1175 /* may overlap, thus only memmove is correct */
1176 memmove(conn->buf, &(conn->buf[KEY_LEN]), len);
1177 this_client->session_key[0] = '\0';
1178 this_client->session_key_timer = (time_t) 1;
1179 *retval = -1;
1180 return NULL;
1181 }
1182
1183 /* --- check whether hostname is properly signed ---
1184 */
1185 if (conn->K != NULL)
1186 {
1187 SH_FREE(conn->K);
1188 conn->K = NULL;
1189 }
1190
1191 conn->K = SH_ALLOC(KEY_LEN+1);
1192
1193 sl_strlcpy (conn->K,
1194 sh_util_siggen(this_client->session_key,
1195 &(conn->buf[KEY_LEN]),
1196 sl_strlen(&(conn->buf[KEY_LEN])),
1197 sigbuf, sizeof(sigbuf)),
1198 KEY_LEN+1);
1199
1200 if (0 != sl_ts_strncmp(conn->K, conn->buf, KEY_LEN))
1201 {
1202 TPT((0, FIL__, __LINE__, _("msg=<clt %s>\n"), conn->buf));
1203 TPT((0, FIL__, __LINE__, _("msg=<srv %s>\n"), conn->K));
1204 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
1205 _("Signature mismatch"),
1206 &(conn->buf[KEY_LEN]));
1207
1208 this_client->session_key_timer =
1209 time(NULL) - (2*TIMEOUT_KEY);
1210
1211 sh_xfer_do_free (conn);
1212 return NULL;
1213 }
1214 SH_FREE(conn->K);
1215 conn->K = NULL;
1216
1217 return this_client;
1218}
1219
1220/* ------------------------------------------------------
1221 *
1222 * FILE TRANSFER
1223 *
1224 * ------------------------------------------------------ */
1225
1226static void do_file_send_data(sh_conn_t * conn)
1227{
1228 char * read_buf = 0;
1229 char * send_buf;
1230 int bytes;
1231 SL_TICKET sfd = -1;
1232
1233 if (conn == NULL || conn->FileName == NULL)
1234 {
1235 sh_error_handle((-1), FIL__, __LINE__, sfd, MSG_TCP_NFILE,
1236 conn->peer,
1237 (conn->FileName == NULL) ?
1238 _("(NULL)") : conn->FileName);
1239 status_update (conn->client_entry, CLT_FAILED);
1240 sh_xfer_do_free (conn);
1241 return;
1242 }
1243
1244 if (conn->FileSent == conn->FileLength)
1245 {
1246 send_buf = hash_me(conn->K, conn->peer, sl_strlen(conn->peer));
1247#ifdef SH_ENCRYPT
1248 sh_xfer_send_crypt (conn, send_buf, sl_strlen(conn->peer)+KEY_LEN,
1249 _("EEOT"), SH_PROTO_BIG|conn->client_entry->encf_flag);
1250#else
1251 sh_xfer_send_crypt (conn, send_buf, sl_strlen(conn->peer)+KEY_LEN,
1252 _("EEOT"), SH_PROTO_BIG);
1253#endif
1254 SH_FREE(send_buf);
1255 }
1256 else
1257 {
1258 bytes = -1;
1259
1260 sfd = sl_open_read(FIL__, __LINE__, conn->FileName, SL_YESPRIV);
1261
1262 if (!SL_ISERROR(sfd))
1263 {
1264 read_buf = SH_ALLOC(TRANS_BYTES);
1265 if (conn->FileSent > 0)
1266 sl_seek (sfd, (off_t) conn->FileSent);
1267 bytes = sl_read (sfd, read_buf, TRANS_BYTES);
1268 sl_close(sfd);
1269 }
1270 else
1271 {
1272 sh_error_handle((-1), FIL__, __LINE__, sfd,
1273 MSG_E_ACCESS, (long) geteuid(), conn->FileName);
1274 }
1275
1276 if (bytes >= 0)
1277 {
1278 send_buf = hash_me(conn->K, read_buf, bytes);
1279#ifdef SH_ENCRYPT
1280 sh_xfer_send_crypt (conn, send_buf, bytes+KEY_LEN, _("FILE"),
1281 SH_PROTO_BIG|conn->client_entry->encf_flag);
1282#else
1283 sh_xfer_send_crypt (conn, send_buf, bytes+KEY_LEN, _("FILE"),
1284 SH_PROTO_BIG);
1285#endif
1286 conn->FileSent += bytes;
1287 if (send_buf) /* hash_me() *may* return NULL */
1288 SH_FREE(send_buf);
1289 SH_FREE(read_buf);
1290 }
1291 else
1292 {
1293 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NFILE, conn->peer,
1294 (conn->FileName == NULL) ? _("(NULL)") : conn->FileName);
1295 status_update (conn->client_entry, CLT_FAILED);
1296 sh_xfer_do_free (conn);
1297 }
1298 }
1299 return;
1300}
1301
1302static void do_file_initial(sh_conn_t * conn)
1303{
1304 char * ptok;
1305 char hashbuf[KEYBUF_SIZE];
1306
1307 /* --- get client nonce and compute hash ---
1308 *
1309 * K = H(NSRV, NCLT, session_key)
1310 */
1311 if (conn->A != NULL)
1312 {
1313 SH_FREE(conn->A);
1314 conn->A = NULL;
1315 }
1316 conn->A = SH_ALLOC(3*KEY_LEN+1);
1317 sl_strlcpy (conn->A, conn->K, KEY_LEN+1);
1318 sl_strlcat(conn->A, conn->buf, /* truncate */
1319 2*KEY_LEN+1);
1320 sl_strlcat(conn->A, conn->client_entry->session_key,
1321 3*KEY_LEN+1);
1322 sl_strlcpy (conn->K, sh_tiger_hash(conn->A,TIGER_DATA,3*KEY_LEN,
1323 hashbuf, sizeof(hashbuf)),
1324 KEY_LEN+1);
1325 SH_FREE(conn->A);
1326 conn->A = NULL;
1327
1328
1329 /* Warn about encryption mismatch
1330 */
1331#ifdef SH_ENCRYPT
1332 if ((conn->client_entry->encf_flag != 0) && /* server */
1333 ((conn->head[0] & SH_PROTO_ENC) == 0)) /* client */
1334 {
1335 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISENC,
1336 _("file download"), _("version2"), _("none"));
1337 }
1338
1339 else if ((conn->client_entry->encf_flag != 0) && /* server */
1340 ((conn->head[0] & SH_MASK_ENC) != /* client */
1341 conn->client_entry->encf_flag))
1342 {
1343 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISENC,
1344 _("file download"), _("version2"),
1345 ((conn->head[0] & SH_PROTO_ENC) == SH_PROTO_ENC) ?
1346 _("version2") : _("invalid"));
1347 conn->client_entry->encf_flag = (conn->head[0] & SH_MASK_ENC);
1348 }
1349#else
1350 if ((conn->head[0] & SH_PROTO_ENC) != 0)
1351 {
1352 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISENC,
1353 _("file download"), _("none"),
1354 ((conn->head[0] & SH_PROTO_ENC) == SH_PROTO_ENC) ?
1355 _("version2") : _("invalid"));
1356 }
1357#endif
1358
1359
1360 if (conn->FileName != NULL)
1361 {
1362 SH_FREE(conn->FileName);
1363 conn->FileName = NULL;
1364 }
1365
1366 /* Determine what to send
1367 */
1368 if (0 == sl_strncmp (_("CONF"), &(conn->buf[KEY_LEN]), 4))
1369 {
1370 strcpy(conn->FileType, _("CONF")); /* known to fit */
1371 conn->FileName = get_client_conf_file(conn->peer, &(conn->FileLength));
1372 conn->FileSent = 0;
1373 }
1374 else if (0 == sl_strncmp (_("DATA"), &(conn->buf[KEY_LEN]), 4))
1375 {
1376 strcpy(conn->FileType, _("DATA")); /* known to fit */
1377 conn->FileName = get_client_data_file(conn->peer, &(conn->FileLength));
1378 conn->FileSent = 0;
1379 }
1380 else if (0 == sh_uuid_check(&(conn->buf[KEY_LEN])))
1381 {
1382 char * uuid = &(conn->buf[KEY_LEN]);
1383 strcpy(conn->FileType, _("UUID")); /* known to fit */
1384 conn->FileName = get_client_uuid_file(conn->peer, &(conn->FileLength), uuid);
1385 conn->FileSent = 0;
1386 }
1387 else
1388 {
1389 ptok = sh_util_safe_name(&(conn->buf[KEY_LEN]));
1390 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FFILE,
1391 conn->peer,
1392 ptok);
1393 SH_FREE(ptok);
1394 status_update (conn->client_entry, CLT_FAILED);
1395 sh_xfer_do_free (conn);
1396 }
1397
1398 return;
1399}
1400
1401
1402static int do_file_transfer(sh_conn_t * conn, int state)
1403{
1404 client_t * this_client;
1405 UINT32 ticks;
1406 char hashbuf[KEYBUF_SIZE];
1407
1408 SL_ENTER(_("do_file_transfer"));
1409
1410 if (state == SH_DO_READ) /* finished reading */
1411 {
1412 TPT(( 0, FIL__, __LINE__, _("msg=<File transfer - entry.>\n")));
1413
1414 /* -- Client requests challenge. --
1415 */
1416 if (0 == check_request_nerr ((char *) &(conn->head[3]), _("HELO")))
1417 {
1418 int client_state;
1419
1420 this_client = do_check_client(conn, &client_state);
1421 if (!this_client)
1422 SL_RETURN(client_state, _("do_file_transfer"));
1423
1424 /* --- create and send a nonce ---
1425 */
1426
1427 conn->client_entry = this_client;
1428 sl_strlcpy (conn->peer, &(conn->buf[KEY_LEN]), SH_MINIBUF+1);
1429
1430 ticks = (UINT32) taus_get ();
1431
1432 if (conn->K != NULL)
1433 {
1434 SH_FREE(conn->K);
1435 conn->K = NULL;
1436 }
1437 conn->K = SH_ALLOC(KEY_LEN+1);
1438 sl_strlcpy (conn->K,
1439 sh_tiger_hash ((char *) &ticks,
1440 TIGER_DATA, sizeof(UINT32),
1441 hashbuf, sizeof(hashbuf)),
1442 KEY_LEN+1);
1443
1444 TPT((0, FIL__, __LINE__, _("msg=<send nonce>\n")));
1445 sh_xfer_prep_send (conn, conn->K, KEY_LEN+1, _("NSRV"),
1446 SH_PROTO_BIG);
1447 }
1448
1449 /* --- Client has send a message. Check state and message. ---
1450 */
1451 else if (0 == check_request_nerr((char *)&(conn->head[3]), _("NCLT")) &&
1452 conn->client_entry != NULL &&
1453 sl_strlen(conn->buf) > KEY_LEN &&
1454 conn->K != NULL)
1455 {
1456
1457 TPT(( 0, FIL__, __LINE__,
1458 _("msg=<File transfer - NCLT (3).>\n")));
1459
1460 do_file_initial(conn);
1461 do_file_send_data(conn);
1462 }
1463
1464 else if (0 == check_request_nerr((char *)&(conn->head[3]),
1465 _("RECV")) &&
1466 conn->client_entry != NULL &&
1467 conn->K != NULL &&
1468 conn->FileName != NULL)
1469 {
1470
1471 TPT(( 0, FIL__, __LINE__,
1472 _("msg=<File transfer - RCVT (5+).>\n")));
1473
1474 do_file_send_data(conn);
1475 }
1476
1477
1478 else if (0 == check_request_nerr((char *)&(conn->head[3]),
1479 _("EOTE")) &&
1480 conn->client_entry != NULL)
1481 {
1482
1483 TPT(( 0, FIL__, __LINE__,
1484 _("msg=<File transfer - EOTE (7).>\n")));
1485
1486 if (flag_err_info == S_TRUE)
1487 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_OKFILE,
1488 conn->peer);
1489
1490 if ((conn->client_entry->status_now != CLT_SUSPEND) &&
1491 (conn->client_entry->status_now != CLT_TOOLONG))
1492 { status_update (conn->client_entry, CLT_FILE); }
1493 else
1494 { conn->client_entry->session_key[0] = '\0'; }
1495 conn->client_entry->last_connect = time (NULL);
1496 sh_xfer_do_free (conn);
1497 }
1498
1499
1500 /* client does something unexpected
1501 */
1502 else /* ---- ??? ----- */
1503 {
1504 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
1505 1, conn->pass, conn->peer,
1506 '\\', conn->head[3], '\\',conn->head[4],
1507 '\\', conn->head[5], '\\',conn->head[6]);
1508 status_update (conn->client_entry, CLT_FAILED);
1509 sh_xfer_do_free (conn);
1510 }
1511 }
1512
1513 else if (state == SH_DO_WRITE) /* finished writing */
1514 {
1515 TPT(( 0, FIL__, __LINE__, _("msg=<File transfer - (wait).>\n")));
1516
1517 /* challenge is sent, now wait for message from client
1518 */
1519 conn->headcount = 0;
1520 conn->bytecount = 0;
1521 conn->bytes_to_send = 0;
1522 conn->bytes_to_get = 0;
1523 if (conn->buf != NULL)
1524 {
1525 SH_FREE(conn->buf);
1526 conn->buf = NULL;
1527 }
1528 conn->state = CONN_READING;
1529 }
1530 SL_RETURN(0, _("do_file_transfer"));
1531}
1532
1533/* ------------------------------------------------------
1534 *
1535 * MESSAGE TRANSFER
1536 *
1537 * ------------------------------------------------------ */
1538static int do_message_transfer(sh_conn_t * conn, int state)
1539{
1540 client_t * this_client;
1541 char * cmd;
1542 char hash[SH_MAXMSGLEN + KEY_LEN + KEY_LEN + 1];
1543 char * buffer;
1544 int clt_sev;
1545 char * ptok;
1546 UINT32 ticks;
1547 size_t len;
1548 int i;
1549 char * test;
1550 char sigbuf[KEYBUF_SIZE];
1551
1552 SL_ENTER(_("do_message_transfer"));
1553
1554 if (state == SH_DO_READ) /* finished reading */
1555 {
1556 TPT(( 0, FIL__, __LINE__, _("msg=<File transfer - entry.>\n")));
1557
1558 /* -- Client requests challenge. --
1559 */
1560 if (0 == check_request_nerr ((char *) &(conn->head[3]), _("HELO")))
1561 {
1562 int client_state;
1563
1564 this_client = do_check_client(conn, &client_state);
1565 if (!this_client)
1566 SL_RETURN(client_state, _("do_message_transfer"));
1567
1568
1569 /* -- create a nonce and send it --
1570 */
1571 conn->client_entry = this_client;
1572 sl_strlcpy (conn->peer, &(conn->buf[KEY_LEN]), SH_MINIBUF+1);
1573
1574 ticks = (UINT32) taus_get ();
1575
1576 test = (char *) &ticks;
1577 sh_util_cpylong (conn->challenge, test, 4);
1578 conn->challenge[4] = '\0';
1579 for (i = 0; i < 4; ++i)
1580 if (conn->challenge[i] == '\0')
1581 conn->challenge[i] = 0x01;
1582
1583 sh_xfer_prep_send (conn, conn->challenge, 5, _("TALK"),
1584 SH_PROTO_MSG);
1585 TPT(( 0, FIL__, __LINE__, _("msg=<Sent %s.>\n"),
1586 hu_trans(conn->challenge)));
1587 }
1588
1589 /* Client has send a message. Check whether we are in proper
1590 * state, and verify message.
1591 */
1592 else if (0 ==
1593 check_request_nerr((char *)&(conn->head[3]), _("MESG")) &&
1594 conn->client_entry != NULL &&
1595 conn->client_entry->session_key[0] != '\0' &&
1596 (len = sl_strlen(conn->buf) - KEY_LEN) > 0 &&
1597 sl_strlen(conn->challenge) == 4)
1598 {
1599 TPT(( 0, FIL__, __LINE__,
1600 _("msg=<Message transfer - MESG (3).>\n")));
1601
1602#ifdef SH_ENCRYPT
1603 if (conn->client_entry->encf_flag == 0) {
1604 conn->client_entry->ency_flag = 0;
1605 }
1606 if ((conn->client_entry->ency_flag != 0) &&
1607 ((conn->head[0] & SH_PROTO_ENC) == 0))
1608 {
1609 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_MISENC,
1610 _("message transfer"),
1611 _("version2"),
1612 _("none"));
1613 }
1614 else if ((conn->client_entry->ency_flag != 0) &&
1615 ((conn->head[0] & SH_MASK_ENC) !=
1616 conn->client_entry->ency_flag))
1617 {
1618 sh_error_handle(SH_ERR_NOTICE, FIL__, __LINE__, 0,
1619 MSG_TCP_MISENC,
1620 _("message transfer"),
1621 _("version2"),
1622 ((conn->head[0] & SH_PROTO_ENC) == SH_PROTO_ENC) ? _("version2") : _("invalid"));
1623 conn->client_entry->ency_flag =
1624 (conn->head[0] & SH_MASK_ENC);
1625 }
1626#else
1627 if ((conn->head[0] & SH_PROTO_ENC) != 0)
1628 {
1629 sh_error_handle((-1), FIL__, __LINE__, 0,
1630 MSG_TCP_MISENC,
1631 _("message transfer"),
1632 _("none"),
1633 ((conn->head[0] & SH_PROTO_ENC) == SH_PROTO_ENC) ? _("version2") : _("invalid"));
1634 }
1635#endif
1636
1637 TPT(( 0, FIL__, __LINE__, _("msg=<Rcvt %s.>\n"), conn->buf));
1638 /* get hash from message end, truncate message
1639 */
1640 sl_strlcpy(hash, &(conn->buf[len]), KEY_LEN+1);
1641 conn->buf[len] = '\0';
1642
1643 /* verify hash
1644 */
1645 buffer = sh_util_strconcat(conn->buf, conn->challenge, NULL);
1646 i = sl_ts_strncmp(hash,
1647 sh_util_siggen(conn->client_entry->session_key,
1648 buffer,
1649 sl_strlen(buffer),
1650 sigbuf, sizeof(sigbuf)),
1651 KEY_LEN);
1652 TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
1653 sh_util_siggen(conn->client_entry->session_key,
1654 buffer,
1655 sl_strlen(buffer),
1656 sigbuf, sizeof(sigbuf))));
1657
1658
1659 if (0 != i)
1660 {
1661 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
1662 status_update (conn->client_entry, CLT_FAILED);
1663 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
1664 _("Msg signature mismatch"), conn->peer);
1665 conn->client_entry->session_key_timer =
1666 time(NULL) - (2*TIMEOUT_KEY);
1667 sh_xfer_do_free (conn);
1668 SL_RETURN(0, _("do_message_transfer"));
1669 }
1670 else
1671 {
1672 conn->client_entry->last_connect = time (NULL);
1673
1674 if (NULL != sl_strstr(conn->buf, _("EXIT")))
1675 {
1676 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
1677 conn->client_entry->exit_flag = 1;
1678 status_update (conn->client_entry, CLT_EXITED);
1679 }
1680 else if (NULL != sl_strstr(conn->buf, _("PANIC")))
1681 {
1682 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
1683 status_update (conn->client_entry, CLT_PANIC);
1684 }
1685 else if (NULL != sl_strstr(conn->buf, _("SUSPEND")))
1686 {
1687 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
1688 status_update (conn->client_entry, CLT_SUSPEND);
1689 }
1690 else if (NULL != sl_strstr(conn->buf, _("POLICY")))
1691 {
1692 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
1693 status_update (conn->client_entry, CLT_POLICY);
1694 }
1695 else if (NULL != sl_strstr(conn->buf,
1696 _("File check completed")))
1697 {
1698 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
1699 status_update (conn->client_entry, CLT_CHECK);
1700 }
1701 else if (NULL != sl_strstr(conn->buf, _("START")))
1702 {
1703 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
1704 sh_socket_add2reload (conn->client_entry->hostname);
1705 if (conn->client_entry->status_now == CLT_SUSPEND) {
1706 status_update (conn->client_entry, CLT_ILLEGAL);
1707 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_ILL,
1708 conn->peer);
1709 }
1710 else
1711 status_update (conn->client_entry, CLT_STARTED);
1712 }
1713 else
1714 {
1715 TPT((0, FIL__, __LINE__, _("msg=<update status>\n")));
1716 if (NULL != sl_strstr(conn->buf,
1717 _("Runtime configuration reloaded")))
1718 {
1719 sh_socket_add2reload (conn->client_entry->hostname);
1720 }
1721 status_update (conn->client_entry, CLT_MSG);
1722 }
1723
1724 TPT((0, FIL__, __LINE__, _("msg=<status updated>\n")));
1725 clt_sev = atoi(conn->buf);
1726 clt_class = (-1);
1727 ptok = strchr(conn->buf, '?');
1728 if (ptok != NULL)
1729 {
1730 ++ptok;
1731 if (ptok != NULL && sh.flag.client_class == S_TRUE)
1732 clt_class = atoi(ptok); /* is a global */
1733 ptok = strchr(ptok, '?');
1734 if (ptok != NULL)
1735 ++ptok;
1736 }
1737 if (sh.flag.client_severity == S_FALSE)
1738 clt_sev = (-1);
1739
1740 /* here we expect an xml formatted message, thus we don't
1741 escape xml special chars (flag == 0) */
1742 ptok =
1743 sh_tools_safe_name ((ptok!=NULL) ? ptok : conn->buf, 0);
1744
1745 /* push client name to error routine
1746 */
1747#if defined(SH_WITH_SERVER) && defined(HAVE_LIBPRELUDE)
1748 {
1749 char peer_ip[SH_IP_BUF];
1750 sh_ipvx_ntoa(peer_ip, sizeof(peer_ip), &(conn->addr_peer));
1751 sh_error_set_peer_ip( peer_ip );
1752 }
1753#endif
1754 {
1755 char * pstrip = sh_strip_domain (conn->peer);
1756 sh_error_set_peer(pstrip);
1757 sh_error_handle(clt_sev, FIL__, __LINE__, 0, MSG_TCP_MSG,
1758 pstrip,
1759 ptok);
1760 SH_FREE(pstrip);
1761 sh_error_set_peer(NULL);
1762 }
1763#if defined(SH_WITH_SERVER) && defined(HAVE_LIBPRELUDE)
1764 sh_error_set_peer_ip(NULL);
1765#endif
1766
1767 TPT((0, FIL__, __LINE__, _("msg=<%s>\n"), ptok));
1768 SH_FREE(ptok);
1769 clt_class = (-1);
1770 }
1771 memset(buffer, 0, sl_strlen(buffer));
1772 SH_FREE(buffer);
1773
1774 /* SERVER CONF SEND
1775 */
1776 buffer = sh_util_strconcat(conn->buf,
1777 conn->challenge,
1778 NULL);
1779 sl_strlcpy(hash,
1780 sh_util_siggen ( conn->client_entry->session_key,
1781 buffer,
1782 sl_strlen(buffer),
1783 sigbuf, sizeof(sigbuf)),
1784 KEY_LEN+1);
1785
1786 /* --- SERVER CMD --- */
1787 cmd = sh_socket_check (conn->peer);
1788
1789 if (cmd != NULL)
1790 {
1791 /* max cmd size is SH_MAXMSGLEN bytes
1792 */
1793 sl_strlcpy(&hash[KEY_LEN], cmd, SH_MAXMSGLEN);
1794 sl_strlcat(&hash[KEY_LEN],
1795 sh_util_siggen ( conn->client_entry->session_key,
1796 &hash[KEY_LEN],
1797 sl_strlen(&hash[KEY_LEN]),
1798 sigbuf, sizeof(sigbuf)),
1799 SH_MAXMSGLEN+KEY_LEN+1);
1800
1801 TPT((0, FIL__, __LINE__, _("CONF SEND <0> <%s>\n"),
1802 &hash[KEY_LEN]));
1803
1804 } else {
1805
1806 TPT((0, FIL__, __LINE__, _("CONF SEND <0> <[NULL]>\n")));
1807
1808 }
1809 /* --- SERVER CMD END --- */
1810
1811 TPT((0, FIL__, __LINE__, _("msg=<sign %s.>\n"),
1812 sh_util_siggen(conn->client_entry->session_key,
1813 buffer,
1814 sl_strlen(buffer),
1815 sigbuf, sizeof(sigbuf))));
1816
1817#ifdef SH_ENCRYPT
1818 sh_xfer_send_crypt (conn, hash,
1819 sl_strlen(hash) /* KEY_LEN */,
1820 _("CONF"),
1821 SH_PROTO_MSG|SH_PROTO_END|conn->client_entry->ency_flag);
1822#else
1823 sh_xfer_send_crypt (conn, hash,
1824 sl_strlen(hash) /* KEY_LEN */,
1825 _("CONF"),
1826 SH_PROTO_MSG|SH_PROTO_END);
1827#endif
1828
1829 memset(buffer, 0, sl_strlen(buffer));
1830 SH_FREE(buffer);
1831
1832 /* sh_xfer_do_free (conn); */
1833 }
1834
1835 /* client does something unexpected
1836 */
1837 else /* ---- ??? ----- */
1838 {
1839 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
1840 2, conn->pass, conn->peer,
1841 '\\', conn->head[3], '\\',conn->head[4],
1842 '\\', conn->head[5], '\\',conn->head[6]);
1843 status_update (conn->client_entry, CLT_FAILED);
1844 conn->client_entry->session_key_timer =
1845 time(NULL) - (2*TIMEOUT_KEY);
1846 sh_xfer_do_free (conn);
1847 }
1848 }
1849
1850 else if (state == SH_DO_WRITE) /* finished writing */
1851 {
1852 if (0 != (conn->head[0] & SH_PROTO_END))
1853 {
1854 if (flag_err_debug == S_TRUE)
1855 {
1856 char * pstrip = sh_strip_domain (conn->peer);
1857 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_OKMSG,
1858 pstrip);
1859 SH_FREE(pstrip);
1860 }
1861 sh_xfer_do_free (conn);
1862 SL_RETURN(0, _("do_message_transfer"));
1863 }
1864
1865 TPT(( 0, FIL__, __LINE__, _("msg=<Msg transfer - (wait).>\n")));
1866
1867 /* challenge is sent, now wait for message from client
1868 */
1869 conn->headcount = 0;
1870 conn->bytecount = 0;
1871 conn->bytes_to_send = 0;
1872 conn->bytes_to_get = 0;
1873 if (conn->buf != NULL)
1874 {
1875 SH_FREE(conn->buf);
1876 conn->buf = NULL;
1877 }
1878 conn->state = CONN_READING;
1879 }
1880 TPT((0, FIL__, __LINE__, _("msg=<return>\n") ));
1881 SL_RETURN(0, _("do_message_transfer"));
1882}
1883
1884/* ------------------------------------------------------
1885 *
1886 * AUTHENTICATION
1887 *
1888 * ------------------------------------------------------ */
1889
1890static void check_probe(sh_conn_t * conn)
1891{
1892 if (conn && conn->client_entry)
1893 {
1894 /* If client has sent probe, change ivst_flag and clear probe in head[0].
1895 */
1896 conn->head[0] = sh_tools_probe_store(conn->head[0],
1897 &(conn->client_entry->ivst_flag));
1898 }
1899}
1900
1901client_t * do_auth_start(sh_conn_t * conn)
1902{
1903 client_t * this_client;
1904
1905 TPT((0, FIL__, __LINE__,
1906 _("msg=<Authentication - SALT (1).>\n")));
1907
1908 if (conn->buf == NULL || sl_strlen(conn->buf) == 0)
1909 {
1910 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NOCLT);
1911 sh_xfer_do_free (conn);
1912 return NULL;
1913 }
1914
1915 /* search the register
1916 */
1917
1918 this_client = search_register (conn, 0);
1919 if (NULL == this_client)
1920 return NULL;
1921
1922 conn->client_entry = this_client;
1923 sl_strlcpy (conn->peer, conn->buf, SH_MINIBUF+1);
1924
1925 if (0 != check_request_s((char *)&(conn->head[3]),
1926 _("SALT"),conn->peer))
1927 {
1928 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
1929 _("No salt requested"), conn->peer);
1930 status_update (conn->client_entry, CLT_FAILED);
1931 conn->client_entry->session_key_timer =
1932 time(NULL) - (2*TIMEOUT_KEY);
1933 sh_xfer_do_free (conn);
1934 return NULL;
1935 }
1936
1937 check_probe(conn);
1938 return this_client;
1939}
1940
1941#if !defined(USE_SRP_PROTOCOL)
1942
1943int do_auth(sh_conn_t * conn)
1944{
1945 client_t * this_client;
1946 UINT32 ticks;
1947 char u[5] = "OOOO";
1948#ifdef SH_ENCRYPT
1949 int err_num;
1950 char expbuf[SH_ERRBUF_SIZE];
1951#endif
1952 char hash[SH_MAXMSGLEN + KEY_LEN + KEY_LEN + 1];
1953 char hashbuf[KEYBUF_SIZE];
1954
1955 /* first pass -- client request salt
1956 */
1957 if (conn->pass == 1)
1958 {
1959 this_client = do_auth_start(conn);
1960
1961 if (!this_client)
1962 return -1;
1963
1964 /* -- create server nounce v --
1965 */
1966 ticks = (UINT32) taus_get ();
1967
1968 if (conn->A != NULL)
1969 {
1970 SH_FREE(conn->A);
1971 conn->A = NULL;
1972 }
1973 conn->A = SH_ALLOC(KEY_LEN+1);
1974
1975 sl_strlcpy(conn->A,
1976 sh_tiger_hash((char *) &ticks,
1977 TIGER_DATA, sizeof(UINT32),
1978 hashbuf, sizeof(hashbuf)),
1979 KEY_LEN+1);
1980 u[0] = 'I'; u[1] = 'N'; u[2] = 'I'; u[3] = 'T'; u[4] = '\0';
1981
1982 if (conn->M1 != NULL)
1983 {
1984 SH_FREE(conn->M1);
1985 conn->M1 = NULL;
1986 }
1987 conn->M1 = SH_ALLOC(2*KEY_LEN+1);
1988
1989 /* compute hash key H(v(server), P)v(server)
1990 */
1991 sh_passwd (conn->A, conn->client_entry->verifier,
1992 NULL, conn->M1);
1993
1994 sl_strlcat(conn->M1, conn->A, 2*KEY_LEN+1);
1995
1996
1997 /* --- send H(v(server), P)v(server) ----
1998 */
1999 sh_xfer_prep_send (conn,
2000 conn->M1,
2001 sl_strlen(conn->M1),
2002 u,
2003 (conn->head[0]|SH_PROTO_SRP));
2004
2005 SH_FREE(conn->M1);
2006 conn->M1 = NULL;
2007 }
2008
2009 /* client -- third pass
2010 * Message is H(H(u,v),P)u
2011 *
2012 * A := v, verifier := H(password),
2013 */
2014 else if (conn->pass == 3 &&
2015 conn->client_entry != NULL)
2016 {
2017
2018 TPT((0, FIL__, __LINE__,
2019 _("msg=<Authentication - PASS (3).>\n")));
2020
2021 if (0 != check_request_s((char *) &(conn->head[3]), _("PASS"),
2022 conn->peer) ||
2023 sl_strlen(conn->buf) <= KEY_LEN ||
2024 conn->A == NULL)
2025 {
2026 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
2027 _("Invalid client request"), conn->peer);
2028 status_update (conn->client_entry, CLT_FAILED);
2029 conn->client_entry->session_key_timer =
2030 time(NULL) - (2*TIMEOUT_KEY);
2031 sh_xfer_do_free (conn);
2032 return -1;
2033 }
2034
2035 check_probe(conn);
2036
2037 /* store random nonce u from client
2038 */
2039 if (conn->K != NULL)
2040 {
2041 SH_FREE(conn->K);
2042 conn->K = NULL;
2043 }
2044 conn->K = SH_ALLOC(KEY_LEN+1);
2045 sl_strlcpy(conn->K, &(conn->buf[KEY_LEN]), KEY_LEN+1);
2046
2047 /* verify random nonce u from client
2048 */
2049 if (conn->M1 != NULL)
2050 {
2051 SH_FREE(conn->M1);
2052 conn->M1 = NULL;
2053 }
2054 conn->M1 = sh_util_strconcat(conn->K, conn->A, NULL);
2055
2056 TPT((0, FIL__, __LINE__, _("msg=<c/r: K = %s>\n"), conn->K));
2057 TPT((0, FIL__, __LINE__, _("msg=<c/r: A = %s>\n"), conn->A));
2058 TPT((0, FIL__, __LINE__, _("msg=<c/r: M = %s>\n"), conn->M1));
2059
2060 sl_strlcpy(hash, sh_tiger_hash (conn->M1,
2061 TIGER_DATA,
2062 sl_strlen(conn->M1),
2063 hashbuf, sizeof(hashbuf)),
2064 KEY_LEN+1);
2065 sh_passwd (hash, conn->client_entry->verifier, NULL, conn->M1);
2066
2067 TPT((0, FIL__, __LINE__, _("msg=<c/r: H = %s>\n"), hash));
2068 TPT((0, FIL__, __LINE__, _("msg=<c/r: P = %s>\n"), conn->M1));
2069
2070 if ( 0 != sl_ts_strncmp(conn->M1, conn->buf, KEY_LEN))
2071 {
2072 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
2073 _("Session key mismatch"), conn->peer);
2074 status_update (conn->client_entry, CLT_FAILED);
2075 conn->client_entry->session_key_timer =
2076 time(NULL) - (2*TIMEOUT_KEY);
2077 sh_xfer_do_free (conn);
2078 return -1;
2079 }
2080
2081 /* ---- compute hash key H(v, P, u) ----
2082 */
2083 sh_passwd (conn->A, conn->client_entry->verifier, conn->K,
2084 conn->M1);
2085
2086 sl_strlcpy(conn->client_entry->session_key,
2087 conn->M1, KEY_LEN+1);
2088 TPT((0, FIL__, __LINE__, _("msg=<c/r: Key = %s>\n"),
2089 conn->client_entry->session_key));
2090
2091#ifdef SH_ENCRYPT
2092 err_num = rijndael_makeKey(&(conn->client_entry->keyInstE),
2093 DIR_ENCRYPT, 192,
2094 conn->client_entry->session_key);
2095 if (err_num < 0)
2096 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
2097 errorExplain(err_num, expbuf, sizeof(expbuf)),
2098 _("check_protocol: makeKey"));
2099 err_num = rijndael_makeKey(&(conn->client_entry->keyInstD),
2100 DIR_DECRYPT, 192,
2101 conn->client_entry->session_key);
2102 if (err_num < 0)
2103 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
2104 errorExplain(err_num, expbuf, sizeof(expbuf)),
2105 _("check_protocol: makeKey"));
2106#endif
2107
2108 if (conn->K != NULL) SH_FREE (conn->K);
2109 conn->K = NULL;
2110 if (conn->A != NULL) SH_FREE (conn->A);
2111 conn->A = NULL;
2112 if (conn->M1 != NULL) SH_FREE (conn->M1);
2113 conn->M1 = NULL;
2114
2115 /* if (conn->client_entry->status_now == CLT_STARTED */
2116 if (((conn->client_entry->status_now != CLT_INACTIVE) &&
2117 (conn->client_entry->status_now != CLT_EXITED) &&
2118 (conn->client_entry->status_now != CLT_SUSPEND))
2119 && conn->client_entry->session_key_timer > (time_t) 1)
2120 {
2121 status_update (conn->client_entry, CLT_ILLEGAL);
2122 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_ILL,
2123 conn->peer);
2124 }
2125 else if (conn->client_entry->session_key_timer == (time_t) 0)
2126 {
2127 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_NEW,
2128 conn->peer);
2129 if (conn->client_entry->status_now != CLT_SUSPEND)
2130 status_update (conn->client_entry, CLT_STARTED);
2131 }
2132
2133 conn->client_entry->session_key_timer = time (NULL);
2134 conn->client_entry->last_connect = time (NULL);
2135
2136 /* put in read state
2137 */
2138 sh_xfer_prep_send (conn,
2139 _("AUTH"),
2140 5,
2141 _("AUTH"),
2142 (conn->head[0]|SH_PROTO_SRP));
2143
2144 }
2145 else
2146 {
2147 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
2148 3, conn->pass, conn->peer,
2149 '\\', conn->head[3], '\\', conn->head[4],
2150 '\\', conn->head[5], '\\', conn->head[6]);
2151 sh_xfer_do_free (conn);
2152 }
2153 return 0;
2154}
2155
2156#else
2157
2158static void noise()
2159{
2160 UINT32 n = taus_get();
2161 retry_msleep(0, (n & 0x0000007F));
2162 return;
2163}
2164
2165/* use SRP */
2166
2167int do_auth(sh_conn_t * conn)
2168{
2169 client_t * this_client;
2170 UINT32 ticks;
2171 char u[5] = "OOOO";
2172#ifdef SH_ENCRYPT
2173 int err_num;
2174 char expbuf[SH_ERRBUF_SIZE];
2175#endif
2176 size_t len;
2177 char * test;
2178 char * foo_B;
2179 char * foo_Ss;
2180 char hashbuf[KEYBUF_SIZE];
2181
2182 /* use SRP
2183 */
2184 if (conn->pass == 1)
2185 {
2186 this_client = do_auth_start(conn);
2187 if (!this_client)
2188 return -1;
2189
2190 u[0] = 'I'; u[1] = 'N'; u[2] = 'I'; u[3] = 'T'; u[4] = '\0';
2191 sh_xfer_prep_send (conn,
2192 conn->client_entry->salt,
2193 sl_strlen(conn->client_entry->salt),
2194 u,
2195 (conn->head[0]|SH_PROTO_SRP));
2196 }
2197
2198 /* client has sent A -- third pass
2199 */
2200 else if (conn->pass == 3 &&
2201 conn->client_entry != NULL)
2202 {
2203
2204 TPT((0, FIL__, __LINE__,
2205 _("msg=<Authentication - PC01 (3).>\n")));
2206
2207 if (0 != check_request_s((char *)&(conn->head[3]),_("PC01"),conn->peer)||
2208 conn->buf == NULL
2209 )
2210 {
2211 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
2212 _("Invalid client request"), conn->peer);
2213 status_update (conn->client_entry, CLT_FAILED);
2214 conn->client_entry->session_key_timer =
2215 time(NULL) - (2*TIMEOUT_KEY);
2216 sh_xfer_do_free (conn);
2217 return -1;
2218 }
2219
2220 check_probe(conn); noise();
2221
2222 if (0 != sh_srp_init())
2223 {
2224 status_update (conn->client_entry, CLT_FAILED);
2225 sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0,
2226 MSG_TCP_EBGN);
2227 sh_xfer_do_free (conn);
2228 return -1;
2229 }
2230
2231
2232 /* check A, only send B if correct
2233 */
2234 if ( sl_strlen(conn->buf) < SH_BUFSIZE &&
2235 0 == sh_srp_check_zero (conn->buf) )
2236 {
2237 len = sl_strlen(conn->buf)+1;
2238
2239 if (conn->A != NULL)
2240 {
2241 SH_FREE(conn->A);
2242 conn->A = NULL;
2243 }
2244 conn->A = SH_ALLOC(len);
2245 sl_strlcpy (conn->A, conn->buf, len);
2246
2247 /*
2248 * compute B
2249 */
2250 if (0 != sh_srp_make_a ()) /* b random number */
2251 {
2252 status_update (conn->client_entry, CLT_FAILED);
2253
2254 sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0,
2255 MSG_TCP_EBGN);
2256 sh_srp_exit();
2257 sh_xfer_do_free (conn);
2258 return -1;
2259 }
2260
2261 foo_B = sh_srp_B /* B = v + g^b */
2262 (conn->client_entry->verifier);
2263
2264 if (foo_B == NULL)
2265 {
2266 status_update (conn->client_entry, CLT_FAILED);
2267
2268 sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0,
2269 MSG_TCP_EBGN);
2270 sh_srp_exit();
2271 sh_xfer_do_free (conn);
2272 return -1;
2273 }
2274
2275 TPT((0, FIL__, __LINE__, _("msg=<srp: A = %s>\n"), conn->A));
2276 TPT((0, FIL__, __LINE__, _("msg=<srp: B = %s>\n"), foo_B));
2277
2278 /*
2279 * create nonce u
2280 */
2281 ticks = (UINT32) taus_get ();
2282
2283 test = (char *) &ticks;
2284 sh_util_cpylong (u, test, 4); /* u nounce */
2285 u[4] = '\0';
2286 sl_strlcpy(conn->challenge,
2287 sh_tiger_hash(u, TIGER_DATA, 4, hashbuf, sizeof(hashbuf)),
2288 SH_CHALLENGE_SIZE);
2289
2290 TPT((0, FIL__, __LINE__, _("msg=<srp: u = %03o-%03o-%03o-%03o>\n"), u[0], u[1], u[2], u[3]));
2291 TPT((0, FIL__, __LINE__, _("msg=<srp: U = %s>\n"),
2292 conn->challenge));
2293
2294 /*
2295 * compute the session key K and M1 = Hash(A,B,K)
2296 */
2297 foo_Ss = sh_srp_S_s (conn->challenge,
2298 conn->A,
2299 conn->client_entry->verifier);
2300
2301 if (foo_Ss == NULL || 0 != sh_srp_check_zero (foo_Ss))
2302 {
2303 status_update (conn->client_entry, CLT_FAILED);
2304
2305 sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0,
2306 MSG_TCP_EBGN);
2307 sh_srp_exit();
2308 sh_xfer_do_free (conn);
2309 return -1;
2310 }
2311
2312 if (conn->K != NULL)
2313 {
2314 SH_FREE(conn->K);
2315 conn->K = NULL;
2316 }
2317 conn->K = SH_ALLOC(KEY_LEN+1);
2318 sl_strlcpy(conn->K,
2319 sh_tiger_hash(foo_Ss, TIGER_DATA,
2320 sl_strlen(foo_Ss),
2321 hashbuf, sizeof(hashbuf)),
2322 KEY_LEN+1);
2323
2324 if (conn->M1 != NULL)
2325 {
2326 SH_FREE(conn->M1);
2327 conn->M1 = NULL;
2328 }
2329 conn->M1 = SH_ALLOC(KEY_LEN+1);
2330 sh_srp_M (conn->A, foo_B, conn->K, conn->M1, KEY_LEN+1);
2331
2332 TPT((0, FIL__, __LINE__, _("msg=<srp:Ss = %s>\n"), foo_Ss));
2333 TPT((0, FIL__, __LINE__, _("msg=<srp: K = %s>\n"), conn->K));
2334 TPT((0, FIL__, __LINE__, _("msg=<srp:M1 = %s>\n"),conn->M1));
2335
2336 /*
2337 * send B
2338 */
2339 sh_xfer_prep_send (conn,
2340 foo_B,
2341 sl_strlen(foo_B)+1,
2342 u,
2343 (conn->head[0]|SH_PROTO_SRP));
2344 if (foo_Ss != NULL)
2345 {
2346 SH_FREE(foo_Ss);
2347 foo_Ss = NULL;
2348 }
2349 if (foo_B != NULL)
2350 {
2351 SH_FREE(foo_B);
2352 foo_B = NULL;
2353 }
2354 }
2355 else
2356 {
2357 status_update (conn->client_entry, CLT_FAILED);
2358
2359 sh_error_handle(SH_ERR_SEVERE, FIL__, __LINE__, 0,
2360 MSG_TCP_EZERO);
2361 sh_xfer_do_free (conn);
2362 }
2363
2364 sh_srp_exit();
2365 }
2366
2367 /* client has sent M1 -- fifth pass
2368 */
2369 else if (conn->pass == 5 &&
2370 conn->client_entry != NULL)
2371 {
2372 TPT((0, FIL__, __LINE__,
2373 _("msg=<Authentication - PC02 (5).>\n")));
2374
2375 /* check that the state is valid
2376 */
2377 if (0 != check_request_s((char *)&(conn->head[3]), _("PC02"),
2378 conn->peer) ||
2379 conn->A == NULL || conn->K == NULL || conn->M1 == NULL)
2380 {
2381 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
2382 _("Invalid client request"), conn->peer);
2383 status_update (conn->client_entry, CLT_FAILED);
2384 conn->client_entry->session_key_timer =
2385 time(NULL) - (2*TIMEOUT_KEY);
2386 sh_xfer_do_free (conn);
2387 return -1;
2388 }
2389
2390 check_probe(conn); noise();
2391
2392 /* ------ verify M1 = H(A, B, K) -------
2393 * ----- send M2 = H(A, M1, K) -------
2394 */
2395 if (conn->buf != NULL &&
2396 sl_ts_strncmp(conn->buf, conn->M1, KEY_LEN) == 0)
2397 {
2398 /*
2399 * send M2
2400 */
2401 char M_buf[KEY_LEN+1];
2402 sh_xfer_prep_send (conn,
2403 sh_srp_M (conn->A, conn->M1, conn->K,
2404 M_buf, sizeof(M_buf)),
2405 KEY_LEN+1,
2406 _("PARP"),
2407 (conn->head[0]|SH_PROTO_SRP));
2408
2409 if (conn->A != NULL) SH_FREE(conn->A); conn->A = NULL;
2410 if (conn->M1 != NULL) SH_FREE(conn->M1); conn->M1 = NULL;
2411 sl_strlcpy(conn->client_entry->session_key,
2412 conn->K, KEY_LEN+1);
2413 TPT((0, FIL__, __LINE__, _("msg=<key %s>\n"),
2414 conn->client_entry->session_key));
2415
2416#ifdef SH_ENCRYPT
2417 err_num = rijndael_makeKey(&(conn->client_entry->keyInstE),
2418 DIR_ENCRYPT, 192,
2419 conn->client_entry->session_key);
2420 if (err_num < 0)
2421 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
2422 errorExplain(err_num, expbuf, sizeof(expbuf)),
2423 _("sh_xfer_prep_send_int: makeKey"));
2424 err_num = rijndael_makeKey(&(conn->client_entry->keyInstD),
2425 DIR_DECRYPT, 192,
2426 conn->client_entry->session_key);
2427 if (err_num < 0)
2428 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
2429 errorExplain(err_num, expbuf, sizeof(expbuf)),
2430 _("sh_xfer_prep_send_int: makeKey"));
2431#endif
2432
2433 if (conn->K != NULL) SH_FREE(conn->K); conn->K = NULL;
2434
2435 conn->client_entry->last_connect = time (NULL);
2436
2437 if (((conn->client_entry->status_now != CLT_INACTIVE) &&
2438 (conn->client_entry->status_now != CLT_EXITED) &&
2439 (conn->client_entry->status_now != CLT_SUSPEND))
2440 && conn->client_entry->session_key_timer > (time_t) 1)
2441 {
2442 status_update (conn->client_entry, CLT_ILLEGAL);
2443
2444 sh_error_handle((-1), FIL__, __LINE__, 0,
2445 MSG_TCP_ILL,
2446 conn->peer);
2447 }
2448 else if (conn->client_entry->session_key_timer == (time_t) 0)
2449 {
2450 sh_error_handle((-1), FIL__, __LINE__, 0,
2451 MSG_TCP_NEW,
2452 conn->peer);
2453 if (conn->client_entry->status_now != CLT_SUSPEND)
2454 status_update (conn->client_entry, CLT_STARTED);
2455 }
2456 conn->client_entry->session_key_timer = time (NULL);
2457
2458 }
2459 else
2460 {
2461 status_update (conn->client_entry, CLT_FAILED);
2462 conn->client_entry->session_key_timer =
2463 time(NULL) - (2*TIMEOUT_KEY);
2464
2465 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN,
2466 _("Session key mismatch"), conn->peer);
2467 sh_xfer_do_free (conn);
2468 }
2469 }
2470
2471 else
2472 {
2473 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_FINV,
2474 4, conn->pass, conn->peer,
2475 '\\', conn->head[3], '\\', conn->head[4],
2476 '\\', conn->head[5], '\\', conn->head[6]);
2477 sh_xfer_do_free (conn);
2478 }
2479 return 0;
2480}
2481#endif
2482
2483/************************************************************************
2484 *
2485 * Here we check the message received, and decide on the answer to send
2486 * (if any). The connection is in CONN_PAUSED state, thus we must:
2487 * (i) define the proper reaction
2488 * (ii) reset to CONN_READING or CONN_WRITING or CONN_FREE
2489 * (iii) eventually reset the connection entry
2490 *
2491 *************************************************************************/
2492static
2493void check_protocol(sh_conn_t * conn, int state)
2494{
2495 SL_ENTER(_("check_protocol"));
2496
2497 /* seed / re-seed the PRNG if required
2498 */
2499 (void) taus_seed();
2500
2501 /* protocols:
2502 * -- (iii) file transfer
2503 * -- (ii) authenticated message transfer
2504 * -- (i) SRP key exchange
2505 */
2506
2507 /* --------- FILE TRANSFER -----------
2508 */
2509 if ( (conn->head[0] & SH_PROTO_SRP) == 0 &&
2510 (conn->head[0] & SH_PROTO_BIG) != 0 /* is set */ )
2511 {
2512 /* nonzero means re-authentication is required
2513 */
2514 if (0 == do_file_transfer(conn, state))
2515 SL_RET0(_("check_protocol"));
2516 }
2517 /* --------- END FILE TRANSFER ----------- */
2518
2519
2520 /* --------- message exchange -----------
2521 */
2522 else if ((conn->head[0] & SH_PROTO_SRP) == 0 &&
2523 (conn->head[0] & SH_PROTO_MSG) != 0 /* is set */ )
2524 {
2525 /* nonzero means re-authentication is required
2526 */
2527 if (0 == do_message_transfer(conn, state))
2528 SL_RET0(_("check_protocol"));
2529 }
2530 /* --------- END MESSAGE TRANSFER ------ */
2531
2532 /* --------- authentication -----------
2533 */
2534 if ( (conn->head[0] & SH_PROTO_SRP) != 0 /* is set */ )
2535 {
2536 if (state == SH_DO_READ) /* finished reading */
2537 {
2538 do_auth(conn);
2539 }
2540
2541 else if (state == SH_DO_WRITE) /* finished writing */
2542 {
2543 TPT((0, FIL__, __LINE__, _("msg=<Authentication -- (wait).>\n")));
2544
2545 conn->headcount = 0;
2546 conn->bytecount = 0;
2547 conn->bytes_to_send = 0;
2548 conn->bytes_to_get = 0;
2549 if (conn->buf != NULL)
2550 {
2551 SH_FREE(conn->buf);
2552 conn->buf = NULL;
2553 }
2554 conn->state = CONN_READING;
2555 }
2556 }
2557 SL_RET0(_("check_protocol"));
2558}
2559
2560
2561/***********************************************************
2562 *
2563 * SERVER RECEIVE FUNCTION
2564 *
2565 ***********************************************************
2566 */
2567int sh_xfer_do_read (sh_conn_t * conn)
2568{
2569 unsigned long byteread; /* bytes read */
2570
2571 SL_ENTER(_("sh_xfer_do_read"));
2572
2573 if (conn->state == CONN_SENDING)
2574 {
2575 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_SYNC,
2576 conn->peer);
2577 SL_RETURN( (-1), _("sh_xfer_do_read"));
2578 }
2579
2580 if (conn->headcount < SH_HEADER_SIZE)
2581 {
2582 conn->bytes_to_get = SH_HEADER_SIZE - conn->headcount;
2583 byteread = read (conn->fd, &(conn->head[conn->headcount]),
2584 conn->bytes_to_get);
2585 if (byteread > 0 || errno == EINTR)
2586 {
2587 if (byteread > 0)
2588 conn->headcount += byteread;
2589 if (conn->headcount == SH_HEADER_SIZE)
2590 {
2591 conn->bytes_to_get = (256 * (unsigned int)conn->head[1] +
2592 (unsigned int)conn->head[2]);
2593 SH_SHOWPROT(conn->head, '<');
2594 conn->bytecount = 0;
2595 }
2596 }
2597 else
2598 goto conn_reset;
2599 SL_RETURN( (0), _("sh_xfer_do_read"));
2600 }
2601
2602 /* limit message size
2603 */
2604 conn->bytes_to_get = (conn->bytes_to_get > TRANS_BYTES) ?
2605 TRANS_BYTES : conn->bytes_to_get;
2606
2607 if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_get > 0)
2608 {
2609 if ( conn->bytecount == 0)
2610 {
2611 if (conn->buf != NULL)
2612 SH_FREE (conn->buf);
2613 conn->buf = SH_ALLOC(conn->bytes_to_get + 1); /* <= TRANS_BYTES+1 */
2614 }
2615
2616 byteread = read (conn->fd, &(conn->buf[conn->bytecount]),
2617 conn->bytes_to_get - conn->bytecount);
2618 if (byteread > 0 || errno == EINTR)
2619 {
2620 if (byteread > 0)
2621 conn->bytecount += byteread;
2622 if (conn->bytecount == conn->bytes_to_get)
2623 {
2624 ++conn->pass;
2625 /* always terminate with NULL - we might use sl_strcmp() */
2626 conn->buf[conn->bytecount] = '\0';
2627 conn->state = CONN_PAUSE;
2628
2629#ifdef SH_ENCRYPT
2630 if ((conn->head[0] & SH_PROTO_ENC) != 0)
2631 {
2632 conn->buf = sh_tools_revertPack (conn->head,
2633 conn->client_entry->ivst_flag,
2634 conn->buf,
2635 &(conn->client_entry->keyInstD),
2636 conn->bytecount);
2637 }
2638#endif
2639 /* ------ HERE CALL check_protocol(conn) ------- */
2640 check_protocol(conn, SH_DO_READ);
2641 }
2642 }
2643 else
2644 goto conn_reset;
2645 }
2646
2647 else if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_get == 0)
2648 {
2649 if (conn->buf != NULL)
2650 SH_FREE (conn->buf);
2651 conn->buf = NULL;
2652 conn->bytecount = 0;
2653 ++conn->pass;
2654 conn->state = CONN_PAUSE;
2655 /* ------ HERE CALL check_protocol(conn) ------- */
2656 check_protocol(conn, SH_DO_READ);
2657 }
2658
2659 SL_RETURN( (0), _("sh_xfer_do_read"));
2660
2661 conn_reset:
2662 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_RESET,
2663 conn->peer);
2664 sh_xfer_do_free ( conn );
2665 SL_RETURN( (-1), _("sh_xfer_do_read"));
2666}
2667
2668#if !defined(O_NONBLOCK)
2669#if defined(O_NDELAY)
2670#define O_NONBLOCK O_NDELAY
2671#else
2672#define O_NONBLOCK 0
2673#endif
2674#endif
2675
2676/* send to the client
2677 */
2678int sh_xfer_do_write (sh_conn_t * conn)
2679{
2680 int flags;
2681 long arg = 0;
2682 long bytesent; /* bytes read */
2683
2684 SL_ENTER(_("sh_xfer_do_write"));
2685
2686 /* ---- consistency check ------
2687 */
2688 if (conn->state == CONN_READING)
2689 {
2690 sh_error_handle( (-1), FIL__, __LINE__, 0, MSG_TCP_SYNC,
2691 conn->peer);
2692 SL_RETURN( (-1), _("sh_xfer_do_write"));
2693 }
2694
2695 flags = retry_fcntl (FIL__, __LINE__, conn->fd, F_GETFL, arg);
2696 retry_fcntl (FIL__, __LINE__, conn->fd, F_SETFL, flags|O_NONBLOCK);
2697
2698 /* ---- send the header ------
2699 */
2700 if (conn->headcount < SH_HEADER_SIZE)
2701 {
2702 conn->bytes_to_send = SH_HEADER_SIZE - conn->headcount;
2703 bytesent = write (conn->fd, &(conn->head[conn->headcount]),
2704 conn->bytes_to_send);
2705 if (bytesent >= 0 || errno == EINTR || errno == EAGAIN)
2706 {
2707 if (bytesent > 0)
2708 conn->headcount += bytesent;
2709 if (conn->headcount == SH_HEADER_SIZE)
2710 conn->bytes_to_send =
2711 (256 * (int)conn->head[1] + (int)conn->head[2]);
2712 }
2713 else
2714 goto conn_reset_w;
2715
2716 if (conn->fd >= 0)
2717 retry_fcntl (FIL__, __LINE__, conn->fd, F_SETFL, flags);
2718 SL_RETURN( (0), _("sh_xfer_do_write"));
2719 }
2720
2721 /* ---- send the body ------
2722 */
2723
2724 if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_send > 0 &&
2725 conn->buf != NULL)
2726 {
2727 bytesent = write (conn->fd, &(conn->buf[conn->bytecount]),
2728 conn->bytes_to_send - conn->bytecount);
2729 if (bytesent >= 0 || errno == EINTR || errno == EAGAIN)
2730 {
2731 if (bytesent > 0)
2732 conn->bytecount += bytesent;
2733 if (conn->bytecount == conn->bytes_to_send)
2734 {
2735 ++conn->pass;
2736 conn->state = CONN_PAUSE;
2737 /* ------ HERE CALL check_protocol(conn) ------- */
2738 check_protocol(conn, SH_DO_WRITE);
2739 }
2740 }
2741 else
2742 goto conn_reset_w;
2743 }
2744
2745 else if (conn->headcount == SH_HEADER_SIZE && conn->bytes_to_send == 0)
2746 {
2747 ++conn->pass;
2748 conn->state = CONN_PAUSE;
2749 /* ------ HERE CALL check_protocol(conn) ------- */
2750 check_protocol(conn, SH_DO_WRITE);
2751 }
2752
2753 if (conn->fd >= 0)
2754 retry_fcntl (FIL__, __LINE__, conn->fd, F_SETFL, flags);
2755 SL_RETURN( (0), _("sh_xfer_do_write"));
2756
2757 conn_reset_w:
2758 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_RESET,
2759 conn->peer);
2760 sh_xfer_do_free ( conn );
2761 SL_RETURN( (-1), _("sh_xfer_do_write"));
2762}
2763
2764/* accept a connection from a client
2765 */
2766#include <syslog.h>
2767#ifdef SH_USE_LIBWRAP
2768#include <tcpd.h>
2769
2770#ifndef ALLOW_SEVERITY
2771#define ALLOW_SEVERITY LOG_INFO
2772#define DENY_SEVERITY LOG_WARNING
2773#endif
2774
2775int allow_severity;
2776int deny_severity;
2777#endif
2778
2779#ifdef SH_USE_LIBWRAP
2780static int check_libwrap(int rc, sh_conn_t * newconn)
2781{
2782 struct request_info request;
2783 char errbuf[128];
2784 char daemon[128];
2785
2786 sl_strlcpy(daemon, SH_INSTALL_NAME, sizeof(daemon));
2787 request_init(&request, RQ_DAEMON, daemon, RQ_FILE, rc, 0);
2788 fromhost(&request);
2789 if (!hosts_access(&request))
2790 {
2791 sl_strlcpy(errbuf, _("Refused connection from "), sizeof(errbuf));
2792 sl_strlcat(errbuf, eval_client(&request), sizeof(errbuf));
2793
2794 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
2795 errbuf, _("libwrap"));
2796 newconn->fd = -1;
2797 newconn->state = CONN_FREE;
2798 sl_close_fd(FIL__, __LINE__, rc);
2799 return -1;
2800 }
2801 return 0;
2802}
2803#endif
2804
2805int sh_xfer_accept (int sock, sh_conn_t * newconn)
2806{
2807 int errflag;
2808 int rc;
2809 struct sh_sockaddr addr;
2810
2811 /* handle AIX (size_t addrlen) in wrapper
2812 */
2813 int addrlen = sizeof(addr);
2814
2815 SL_ENTER(_("sh_xfer_accept"));
2816
2817 rc = retry_accept(FIL__, __LINE__, sock, &addr, &addrlen);
2818
2819 if (rc < 0)
2820 {
2821 char err_buf[SH_ERRBUF_SIZE];
2822 errflag = errno;
2823 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
2824 sh_error_message(errflag,err_buf, sizeof(err_buf)), _("accept"));
2825 newconn->fd = -1;
2826 newconn->state = CONN_FREE;
2827 SL_RETURN( (-1), _("sh_xfer_accept"));
2828 }
2829
2830 if (addrlen == 0)
2831 {
2832 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN,
2833 _("Connecting entity unknown"), _("accept"));
2834 newconn->fd = -1;
2835 newconn->state = CONN_FREE;
2836 sl_close_fd(FIL__, __LINE__, rc);
2837 SL_RETURN( (-1), _("sh_xfer_accept"));
2838 }
2839
2840#ifdef SH_USE_LIBWRAP
2841 if (check_libwrap(rc, newconn) < 0)
2842 SL_RETURN( (-1), _("sh_xfer_accept"));
2843#endif
2844
2845 memcpy (&(newconn->addr_peer), &addr, sizeof(struct sh_sockaddr));
2846
2847 /* prepare for usage of connection
2848 */
2849 (void) retry_fcntl( FIL__, __LINE__, rc, F_SETFD, 1 );
2850 newconn->fd = rc;
2851 newconn->state = CONN_READING;
2852 newconn->timer = (unsigned long) time (NULL);
2853
2854 if (flag_err_info == S_TRUE)
2855 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_CNEW, newconn->fd);
2856
2857 SL_RETURN( (0), _("sh_xfer_accept"));
2858}
2859
2860extern char sh_sig_msg[64]; /* defined in sh_unix.c */
2861
2862/* ------------ port and interface -------
2863 */
2864static unsigned int server_port = SH_DEFAULT_PORT;
2865
2866int sh_xfer_set_port (const char * str)
2867{
2868 int retval = 0;
2869 unsigned long i;
2870 char * endptr;
2871
2872 SL_ENTER(_("sh_xfer_set_port"));
2873 i = strtoul (str, &endptr, 0);
2874 if (endptr == str) {
2875 retval = -1;
2876 } else if (i > 65535) {
2877 retval = -1;
2878 } else {
2879 server_port = i;
2880 }
2881 SL_RETURN( (retval), _("sh_xfer_set_port"));
2882}
2883
2884static struct sh_sockaddr server_interface;
2885static int use_server_interface = 0;
2886
2887int sh_xfer_set_interface (const char * str)
2888{
2889 if (0 == strcmp(str, _("INADDR_ANY")))
2890 {
2891 use_server_interface = 0;
2892 return 0;
2893 }
2894
2895 if (0 == sh_ipvx_aton(str, &server_interface))
2896 {
2897 use_server_interface = 0;
2898 return -1;
2899 }
2900
2901 use_server_interface = 1;
2902 return 0;
2903}
2904
2905/* ------------ print error --------------
2906 */
2907struct sock_err_st {
2908 char msg[128];
2909 int errnum;
2910 int port;
2911 int line;
2912 int euid;
2913};
2914
2915static struct sock_err_st sock_err[2];
2916
2917void sh_xfer_printerr(char * str, int errnum, unsigned int port, int line)
2918{
2919 int slot = 0;
2920
2921 if (port != server_port)
2922 slot = 1;
2923 if (str == NULL)
2924 sock_err[slot].msg[0] = '\0';
2925 else
2926 sl_strlcpy(sock_err[slot].msg, str, 128);
2927 sock_err[slot].errnum = errnum;
2928 sock_err[slot].port = port;
2929 sock_err[slot].line = line;
2930 sock_err[slot].euid = (int) geteuid();
2931}
2932
2933int sh_xfer_printerr_final(int slot)
2934{
2935 char errbuf[SH_ERRBUF_SIZE];
2936
2937 SL_ENTER(_("sh_xfer_printerr_final"));
2938 if (sock_err[slot].msg[0] != '\0')
2939 {
2940 dlog(1, FIL__, __LINE__,
2941 _("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"),
2942 sh_error_message(sock_err[slot].errnum, errbuf, sizeof(errbuf)),
2943 sock_err[slot].euid,
2944 sock_err[slot].port);
2945 sh_error_handle((-1), FIL__, sock_err[slot].line,
2946 sock_err[slot].errnum, MSG_EXIT_ABORTS,
2947 sh_error_message(sock_err[slot].errnum, errbuf, sizeof(errbuf)),
2948 sh.prg_name,
2949 sock_err[slot].msg);
2950 SL_RETURN((-1), _("sh_xfer_printerr_final"));
2951 }
2952 SL_RETURN(0, _("sh_xfer_printerr_final"));
2953}
2954
2955#define TIME_OUT_DEF 900
2956static unsigned long time_out_val = TIME_OUT_DEF;
2957
2958int sh_xfer_set_timeout (const char * c)
2959{
2960 long val;
2961
2962 SL_ENTER(_("sh_xfer_set_time_out"));
2963
2964 val = strtol (c, (char **)NULL, 10);
2965
2966 if (val == 0)
2967 {
2968 val = TIME_OUT_DEF;
2969 }
2970 else if (val < 0)
2971 {
2972 time_out_val = TIME_OUT_DEF;
2973 SL_RETURN( (-1), _("sh_xfer_set_time_out"));
2974 }
2975
2976 time_out_val = (unsigned long) val;
2977 SL_RETURN( (0), _("sh_xfer_set_time_out"));
2978}
2979
2980
2981static sh_conn_t * conns = NULL;
2982static int maxconn = 0; /* maximum number of simultaneous connections */
2983
2984
2985#ifdef INET_SYSLOG
2986#define INET_SUSPEND_TIME 180 /* equal to 3 minutes */
2987#define SH_MINSOCK_DEFAULT 3
2988int sh_xfer_syslog_sock[SH_SOCKMAX] = { -1 };
2989extern int sh_xfer_recv_syslog_socket (int fd);
2990int sh_xfer_syslog_sock_n = 0;
2991#else
2992#define SH_MINSOCK_DEFAULT 2
2993#endif
2994
2995int SH_MINSOCK = SH_MINSOCK_DEFAULT;
2996
2997extern int pf_unix_fd;
2998
2999/* the tcp socket, and the function to establish it
3000 */
3001static int sh_tcp_sock[SH_SOCKMAX] = { -1 };
3002static int sh_tcp_sock_n = 0;
3003
3004static int do_socket(int domain, int type, int protocol,
3005 struct sockaddr * sa, int salen)
3006{
3007 int sock = -1;
3008 int errnum = 0;
3009 int flag = 1; /* non-zero to enable an option */
3010
3011 /* create the socket, bind() it and listen()
3012 */
3013 if ((sock = socket(domain, type, protocol)) < 0 )
3014 {
3015 errnum = errno;
3016 sh_xfer_printerr (_("socket"), errnum, server_port, __LINE__);
3017 return -1;
3018 }
3019 (void) retry_fcntl( FIL__, __LINE__, sock, F_SETFD, 1 );
3020
3021 if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
3022 (void *) &flag, sizeof(flag)) < 0 )
3023 {
3024 errnum = errno;
3025 sh_xfer_printerr (_("setsockopt"), errnum, server_port, __LINE__);
3026 sl_close_fd (FIL__, __LINE__, sock);
3027 return -1;
3028 }
3029
3030 if ( bind(sock, (struct sockaddr *) sa, salen) < 0)
3031 {
3032 if (errno != EADDRINUSE)
3033 {
3034 errnum = errno;
3035 sh_xfer_printerr (_("bind"), errnum, server_port, __LINE__);
3036 sl_close_fd (FIL__, __LINE__, sock);
3037 return -1;
3038 }
3039 else
3040 {
3041 sl_close_fd (FIL__, __LINE__, sock);
3042 return -2;
3043 }
3044 }
3045
3046 if ( retry_fcntl( FIL__, __LINE__, sock, F_SETFL, O_NONBLOCK ) < 0 )
3047 {
3048 errnum = errno;
3049 sh_xfer_printerr (_("fcntl"), errnum, server_port, __LINE__);
3050 sl_close_fd (FIL__, __LINE__, sock);
3051 return -1;
3052 }
3053
3054 if ( listen(sock, 64) < 0)
3055 {
3056 errnum = errno;
3057 sh_xfer_printerr (_("listen"), errnum, server_port, __LINE__);
3058 sl_close_fd (FIL__, __LINE__, sock);
3059 return -1;
3060 }
3061
3062 return sock;
3063}
3064
3065int sh_create_tcp_socket (void)
3066{
3067#if defined(USE_IPVX)
3068 struct addrinfo *ai;
3069 struct addrinfo *p;
3070 struct addrinfo hints;
3071 char port[32];
3072#else
3073 struct sockaddr_in addr;
3074 int addrlen = sizeof(addr);
3075#endif
3076
3077 int sock = -1;
3078
3079 SL_ENTER(_("sh_create_tcp_socket"));
3080
3081 sh_xfer_printerr (NULL, 0, server_port, __LINE__);
3082
3083#if defined(USE_IPVX)
3084 if (use_server_interface == 0) /* INADDR_ANY, listen on all interfaces */
3085 {
3086 memset (&hints, 0, sizeof (hints));
3087 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
3088 hints.ai_socktype = SOCK_STREAM;
3089 hints.ai_family = AF_UNSPEC;
3090 sl_snprintf(port, sizeof(port), "%d", server_port);
3091
3092 if (getaddrinfo (NULL, port, &hints, &ai) != 0)
3093 {
3094 int errnum = errno;
3095 sh_xfer_printerr (_("getaddrinfo"), errnum, server_port, __LINE__);
3096 sl_close_fd (FIL__, __LINE__, sock);
3097 SL_RETURN((-1), _("sl_create_tcp_socket"));
3098 }
3099
3100 p = ai;
3101
3102 while (p != NULL && sh_tcp_sock_n < SH_SOCKMAX)
3103 {
3104 sock = do_socket(p->ai_family, p->ai_socktype, p->ai_protocol,
3105 p->ai_addr, p->ai_addrlen);
3106
3107 if (sock >= 0) {
3108 if (sh_tcp_sock_n < SH_SOCKMAX) {
3109 sh_tcp_sock[sh_tcp_sock_n] = sock;
3110 ++sh_tcp_sock_n;
3111 }
3112 else {
3113 sl_close_fd (FIL__, __LINE__, sock);
3114 }
3115 } else if (sock == -1) {
3116 freeaddrinfo (ai);
3117 goto end;
3118 }
3119 p = p->ai_next;
3120 }
3121
3122 freeaddrinfo (ai);
3123 }
3124 else
3125 {
3126 sh_ipvx_set_port(&server_interface, server_port);
3127
3128 sock = do_socket(server_interface.ss_family, SOCK_STREAM, 0,
3129 sh_ipvx_sockaddr_cast(&server_interface),
3130 SH_SS_LEN(server_interface));
3131
3132 if (sock >= 0) {
3133 sh_tcp_sock[0] = sock;
3134 sh_tcp_sock_n = 1;
3135 }
3136 }
3137#else
3138 if (use_server_interface == 0)
3139 addr.sin_addr.s_addr = INADDR_ANY;
3140 else
3141 memcpy(&addr, sh_ipvx_sockaddr_cast(&server_interface), addrlen);
3142 addr.sin_family = AF_INET;
3143 addr.sin_port = htons(server_port);
3144
3145 sock = do_socket(AF_INET, SOCK_STREAM, 0, (struct sockaddr *) &addr, addrlen);
3146
3147 if (sock >= 0) {
3148 sh_tcp_sock[0] = sock;
3149 sh_tcp_sock_n = 1;
3150 }
3151
3152#endif
3153
3154#if defined(USE_IPVX)
3155 end:
3156#endif
3157 if (sh_tcp_sock_n > 1)
3158 SH_MINSOCK += (sh_tcp_sock_n - 1);
3159
3160 SL_RETURN((sh_tcp_sock_n), _("sl_create_tcp_socket"));
3161}
3162
3163/*****************************************
3164 *
3165 * This is the server main loop.
3166 *
3167 * The server is set up for listening, and
3168 * and starts a select() loop.
3169 *
3170 *****************************************/
3171
3172void sh_xfer_start_server()
3173{
3174#ifdef SH_USE_XML
3175 extern int sh_log_file (char * message, char * inet_peer);
3176#endif
3177
3178 /* Use volatile to circumvent a gcc4 problem on RH/CentOS 4.8 (?) */
3179 volatile int sock = -1;
3180 sh_conn_t * cx;
3181 fd_set readset;
3182 fd_set writeset;
3183 struct timeval tv;
3184 int num_sel;
3185 int errnum;
3186 int nowconn;
3187 int status;
3188 int high_fd = -1;
3189 register int i;
3190 long dummy = 0;
3191 unsigned long time_now;
3192 unsigned long time_last = 0;
3193 unsigned long time_out = time_out_val;
3194
3195 time_t told;
3196 time_t tcurrent;
3197
3198 unsigned long tchkold;
3199
3200 int setsize_fd;
3201
3202 int sock_tcp[2];
3203 int sock_unix;
3204#ifdef INET_SYSLOG
3205 int sock_log[2];
3206#endif
3207
3208 SL_ENTER(_("sh_xfer_start_server"));
3209
3210 if ( sh_xfer_printerr_final(0) < 0)
3211 {
3212 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
3213 }
3214
3215 sock = sh_tcp_sock[0];
3216
3217 /* ****************************************************************
3218 *
3219 * This is a non-forking server. We use select() on the listen()
3220 * socket to watch for new connections. For new connections, accept()
3221 * will return a new socket that is put in the read/write filesets.
3222 * Data about active connections are kept in the 'conns' table.
3223 *
3224 ******************************************************************/
3225
3226 /* The table to hold info on sockets.
3227 * We reserve 6 file descriptors for misc. use.
3228 * The POSIX lower limit on open files seems to be eight.
3229 */
3230 maxconn = get_open_max() - 6;
3231
3232 /* ugly fix for FreeBSD compiler warning; casting FD_SETSIZE in the
3233 * conditional expression does not suppress the warning... */
3234 setsize_fd = (int)FD_SETSIZE;
3235 maxconn = (setsize_fd < maxconn) ? setsize_fd : maxconn;
3236
3237 if (maxconn < 0 || !sl_ok_muls(maxconn, sizeof(sh_conn_t)))
3238 {
3239 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_START_SRV,
3240 0, sock);
3241 aud_exit (FIL__, __LINE__, EXIT_FAILURE);
3242 }
3243 conns = SH_ALLOC (sizeof(sh_conn_t) * maxconn);
3244
3245 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_START_SRV,
3246 (maxconn-1), sock);
3247
3248 /* timer
3249 */
3250 tcurrent = (unsigned long) time (NULL);
3251 told = tcurrent;
3252
3253 tchkold = tcurrent;
3254
3255 for (i = SH_MINSOCK; i < maxconn; ++i)
3256 {
3257 conns[i].buf = NULL;
3258 conns[i].K = NULL;
3259 conns[i].A = NULL;
3260 conns[i].M1 = NULL;
3261 conns[i].FileName = NULL;
3262 conns[i].fd = -1;
3263 sh_xfer_do_free ( &conns[i]);
3264 }
3265
3266 /* status init
3267 */
3268 server_status.conn_open = 0;
3269 server_status.conn_total = 0;
3270 server_status.conn_max = maxconn-1;
3271 server_status.start = time (NULL);
3272 server_status.last = (time_t) 0;
3273
3274 nowconn = 1;
3275 tv.tv_sec = 5;
3276 tv.tv_usec = 0;
3277
3278 /* conns[0] is the listen() socket. Always in read mode.
3279 */
3280 sock = 0;
3281
3282 sock_tcp[0] = 0;
3283 while (sock < sh_tcp_sock_n)
3284 {
3285 conns[sock].fd = sh_tcp_sock[sock];
3286 conns[sock].state = CONN_READING;
3287 /* high_fd = (sh_tcp_sock[sock] > high_fd) ? sh_tcp_sock[sock] : high_fd; */
3288 ++sock;
3289 }
3290 sock_tcp[1] = sock;
3291
3292 conns[sock].fd = pf_unix_fd;
3293 conns[sock].state = CONN_READING;
3294 /* high_fd = (pf_unix_fd > high_fd) ? pf_unix_fd : high_fd; */
3295
3296 sock_unix = sock;
3297
3298 ++sock;
3299
3300#ifdef INET_SYSLOG
3301 conns[sock].fd = -1;
3302
3303 if ( sh_xfer_printerr_final(1) < 0)
3304 {
3305 SH_FREE(conns);
3306 conns = NULL;
3307 aud_exit(FIL__, __LINE__, EXIT_FAILURE);
3308 }
3309
3310 sock_log[0] = sock;
3311 sock_log[1] = sock;
3312
3313 if (sh_xfer_syslog_sock_n > 0)
3314 {
3315 int s2;
3316 for (s2 = 0; s2 < sh_xfer_syslog_sock_n; ++s2)
3317 {
3318 conns[sock].fd = sh_xfer_syslog_sock[s2];
3319 conns[sock].state = CONN_READING;
3320 /* high_fd = (high_fd > conns[sock].fd) ? high_fd : conns[sock].fd; */
3321 ++sock;
3322 }
3323 sock_log[1] = sock;
3324
3325 }
3326#endif
3327
3328 sh_html_write(all_clients);
3329
3330 /* This is the select() loop.
3331 */
3332 while (1 == 1)
3333 {
3334
3335 if (sig_raised > 0)
3336 {
3337 TPT((0, FIL__, __LINE__, _("msg=<Process a signal.>\n")))
3338
3339 if (sig_termfast == 1) /* SIGTERM */
3340 {
3341 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
3342 strncpy (sh_sig_msg, _("SIGTERM"), 20);
3343 --sig_raised; --sig_urgent;
3344 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
3345 }
3346
3347 if (sig_config_read_again == 1)
3348 {
3349 TPT((0, FIL__, __LINE__, _("msg=<Re-read configuration.>\n")));
3350 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_RECONF);
3351
3352
3353 /* -- Delete the name server cache. --
3354 */
3355
3356 delete_cache();
3357#if defined(WITH_EXTERNAL)
3358 /* -- Delete list of external tasks. --
3359 */
3360 (void) sh_ext_cleanup();
3361#endif
3362#if defined(SH_WITH_MAIL)
3363 sh_nmail_free();
3364#endif
3365 /* - mark all clients dead
3366 * - read configuration file
3367 * - remove clients still dead
3368 */
3369 sh_xfer_mark_dead ();
3370
3371 reset_count_dev_console();
3372 reset_count_dev_time();
3373 sl_trust_purge_user();
3374
3375 (void) sh_readconf_read ();
3376
3377 for (i = SH_MINSOCK; i < maxconn; ++i)
3378 if (conns[i].state != CONN_FREE &&
3379 conns[i].client_entry != NULL &&
3380 conns[i].client_entry->dead_flag == 1)
3381 sh_xfer_do_free ( &conns[i]);
3382 sh_xfer_clean_tree ();
3383
3384 sig_config_read_again = 0;
3385 --sig_raised;
3386 }
3387
3388 if (sig_fresh_trail == 1) /* SIGIOT */
3389 {
3390 /* Logfile access
3391 */
3392#ifdef SH_USE_XML
3393 sh_log_file (NULL, NULL);
3394#endif
3395 TPT((0, FIL__, __LINE__, _("msg=<Logfile stop/restart.>\n")));
3396 sh_error_only_stderr (S_TRUE);
3397 sh_unix_rm_lock_file(sh.srvlog.name);
3398 retry_msleep(3, 0);
3399 sh.flag.log_start = S_TRUE;
3400 sh_error_only_stderr (S_FALSE);
3401 sig_fresh_trail = 0;
3402 --sig_raised;
3403 }
3404
3405
3406 if (sig_terminate == 1 && nowconn < 2) /* SIGQUIT */
3407 {
3408 TPT((0, FIL__, __LINE__, _("msg=<Terminate.>\n")));
3409 strncpy (sh_sig_msg, _("SIGQUIT"), 20);
3410 --sig_raised; --sig_urgent;
3411 aud_exit (FIL__, __LINE__, EXIT_SUCCESS);
3412 }
3413
3414
3415 if (sig_debug_switch == 1) /* SIGUSR1 */
3416 {
3417 TPT((0, FIL__, __LINE__, _("msg=<Debug switch.>\n")));
3418 sh_error_dbg_switch();
3419 sig_debug_switch = 0;
3420 --sig_raised;
3421 }
3422
3423 if (sig_suspend_switch > 0) /* SIGUSR2 */
3424 {
3425 TPT((0, FIL__, __LINE__, _("msg=<Suspend switch.>\n")));
3426 if (sh_global_suspend_flag == 1) {
3427 sh_global_suspend_flag = 0;
3428 } else {
3429 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_SUSPEND,
3430 sh.prg_name);
3431 sh_global_suspend_flag = 1;
3432 }
3433 --sig_suspend_switch;
3434 --sig_raised; --sig_urgent;
3435 }
3436
3437 sig_raised = (sig_raised < 0) ? 0 : sig_raised;
3438 sig_urgent = (sig_urgent < 0) ? 0 : sig_urgent;
3439 TPT((0, FIL__, __LINE__, _("msg=<End signal processing.>\n")));
3440 }
3441
3442 if (sh_global_suspend_flag == 1)
3443 {
3444 (void) retry_msleep (1, 0);
3445 continue;
3446 }
3447
3448 /* Recompute the descriptor set. select() modifies it,
3449 * thus we update it using the info from the connection table.
3450 * Also recompute the number of open connections.
3451 */
3452 FD_ZERO( &readset );
3453 FD_ZERO( &writeset );
3454 high_fd = conns[0].fd;
3455
3456 for (sock = sock_tcp[0]; sock < sock_tcp[1]; ++sock)
3457 {
3458 FD_SET(conns[sock].fd, &readset );
3459 high_fd = (high_fd > conns[sock].fd) ? high_fd : conns[sock].fd;
3460 }
3461
3462 if (conns[sock_unix].fd > -1)
3463 {
3464 FD_SET(conns[sock_unix].fd, &readset );
3465 high_fd = (high_fd > conns[sock_unix].fd) ? high_fd : conns[sock_unix].fd;
3466 }
3467
3468#ifdef INET_SYSLOG
3469 for (sock = sock_log[0]; sock < sock_log[1]; ++sock)
3470 {
3471 if (conns[sock].fd > -1)
3472 {
3473 FD_SET(conns[sock].fd, &readset );
3474 high_fd = (high_fd > conns[sock].fd) ? high_fd : conns[sock].fd;
3475 }
3476 }
3477#endif
3478
3479 time_now = (unsigned long) time (NULL);
3480 nowconn = 1;
3481
3482 for (i = SH_MINSOCK; i < maxconn; ++i)
3483 {
3484 /* eliminate timed out connections
3485 */
3486 if (conns[i].state != CONN_FREE)
3487 {
3488 if (time_now-conns[i].timer > time_out)
3489 {
3490 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_TIMOUT,
3491 conns[i].peer);
3492 sh_xfer_do_free ( &conns[i]);
3493 }
3494 else
3495 ++nowconn;
3496 }
3497
3498
3499 if (conns[i].state == CONN_READING)
3500 {
3501 FD_SET(conns[i].fd, &readset);
3502 high_fd = (high_fd < conns[i].fd ? conns[i].fd : high_fd);
3503 }
3504 else if (conns[i].state == CONN_SENDING)
3505 {
3506 FD_SET(conns[i].fd, &writeset);
3507 high_fd = (high_fd < conns[i].fd ? conns[i].fd : high_fd);
3508 }
3509 }
3510
3511 /* -- Exponentially reduce timeout limit if more than 1/2 full. --
3512 */
3513 /* Eliminate this, will cause problems when too much clients are
3514 * starting up. */
3515#if 0
3516 if (nowconn > (maxconn/2))
3517 time_out = ( (time_out/2) > 1) ? (time_out/2) : 1;
3518 else
3519 time_out = time_out_val;
3520#endif
3521
3522
3523 /* -- Do the select(). --
3524 */
3525 num_sel = select(high_fd+1, &readset, &writeset, NULL, &tv);
3526 errnum = errno;
3527
3528 /* reset timeout - modified by select() on some systems
3529 */
3530 tv.tv_sec = 5;
3531 tv.tv_usec = 0;
3532
3533
3534 if ( (time_now - time_last) > 2L)
3535 {
3536 time_last = time_now;
3537 if (sh_html_write(all_clients) < 0)
3538 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_HTML);
3539 }
3540
3541
3542 /* Error handling.
3543 */
3544 if ( num_sel < 0 ) /* some error */
3545 {
3546 char errbuf[SH_ERRBUF_SIZE];
3547
3548 if (sig_raised == 1)
3549 {
3550 sig_raised = 2;
3551 continue;
3552 }
3553
3554 if ( errnum == EINTR)
3555 continue; /* try again */
3556
3557 if ( errnum == EBADF)
3558 {
3559 /* seek and destroy the bad fd
3560 */
3561 for (i = SH_MINSOCK; i < high_fd; ++i)
3562 {
3563 if ((conns[i].state == CONN_READING) ||
3564 (conns[i].state == CONN_SENDING))
3565 {
3566 if (-1 == retry_fcntl(FIL__, __LINE__,
3567 conns[i].fd, F_GETFL, dummy))
3568 sh_xfer_do_free ( &conns[i]);
3569 }
3570 }
3571 continue;
3572 }
3573
3574 sh_error_handle((-1), FIL__, __LINE__, errnum, MSG_EXIT_ABORTS,
3575 sh_error_message(errnum, errbuf, sizeof(errbuf)),
3576 sh.prg_name,
3577 _("select"));
3578 aud_exit(FIL__, __LINE__, EXIT_FAILURE );
3579 }
3580
3581
3582 /* log the timestamp
3583 */
3584 if ((tcurrent - told) > sh.looptime )
3585 {
3586 told = tcurrent;
3587#ifdef MEM_DEBUG
3588 sh_mem_check();
3589 sh_unix_count_mlock();
3590#else
3591 sh_error_handle ((-1), FIL__, __LINE__, 0, MSG_STAMP);
3592#endif
3593 }
3594
3595#if defined(SH_WITH_MAIL)
3596 /*
3597 * flush the mail queue
3598 */
3599 if (tcurrent - sh.mailTime.alarm_last > sh.mailTime.alarm_interval)
3600 {
3601 TPT((0, FIL__, __LINE__, _("msg=<Flush mail queue.>\n")))
3602 (void) sh_nmail_flush ();
3603 sh.mailTime.alarm_last = tcurrent;
3604 }
3605#endif
3606#ifdef MEM_DEBUG
3607 sh_mem_dump();
3608#endif
3609
3610 tcurrent = (unsigned long) time (NULL);
3611
3612 /* check for time limit exceeded
3613 */
3614 if ((tcurrent - tchkold) > (unsigned int) 3 )
3615 {
3616 tchkold = tcurrent;
3617 client_time_check(/* all_clients */);
3618 /* reset cache */
3619 sh_userid_destroy();
3620 }
3621
3622 /* seed / re-seed the PRNG if required
3623 */
3624 (void) taus_seed();
3625
3626 /* select() timeout handling.
3627 */
3628 if ( num_sel == 0 ) /* timeout - no connection */
3629 {
3630 if (sh_html_write(all_clients) < 0)
3631 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_HTML);
3632 continue;
3633 }
3634
3635 /* New connection.
3636 */
3637 for (sock = sock_tcp[0]; sock < sock_tcp[1]; ++sock)
3638 {
3639 if ( FD_ISSET(conns[sock].fd , &readset )) /* a new connection */
3640 {
3641 --num_sel;
3642 status = 0;
3643 if (nowconn < maxconn && sig_terminate == 0 && sig_termfast == 0)
3644 {
3645 /* Find a free slot to accept the connection
3646 */
3647 i = SH_MINSOCK;
3648 while (i < maxconn)
3649 {
3650 if (conns[i].state == CONN_FREE)
3651 {
3652 /* Here we run the accept() and copy the peer to
3653 * the free slot.
3654 */
3655 status = sh_xfer_accept(conns[sock].fd, &conns[i]);
3656
3657 if (status == 0)
3658 {
3659 high_fd =
3660 (high_fd > conns[i].fd ? high_fd : conns[i].fd);
3661 ++server_status.conn_open;
3662 ++server_status.conn_total;
3663 server_status.last = time (NULL);
3664 }
3665 break;
3666 }
3667 ++i;
3668 }
3669 }
3670 /* This re-runs select to accept data on the new
3671 * connection, rather than first dealing with old
3672 * connections.
3673 */
3674 if (status == 0)
3675 continue;
3676 }
3677 }
3678
3679 /* check for commands on the socket
3680 */
3681 if (conns[sock_unix].fd > (-1) && FD_ISSET(conns[sock_unix].fd , &readset ))
3682 {
3683 sh_socket_poll();
3684 }
3685
3686#ifdef INET_SYSLOG
3687 for (sock = sock_log[0]; sock < sock_log[1]; ++sock)
3688 {
3689 if (conns[sock].fd > (-1) && FD_ISSET(conns[sock].fd , &readset ))
3690 {
3691 sh_xfer_recv_syslog_socket (conns[sock].fd);
3692 }
3693 }
3694#endif
3695
3696 /* Check for pending read/write on the rest of the sockets.
3697 */
3698 for ( i = SH_MINSOCK; num_sel > 0 && i < maxconn; ++i )
3699 {
3700 if (sig_termfast == 1)
3701 break;
3702
3703 cx = &conns[i];
3704 if ( cx->state == CONN_READING &&
3705 FD_ISSET( cx->fd, &readset ) )
3706 {
3707 --num_sel;
3708 sh_xfer_do_read ( cx );
3709 }
3710 else if ( cx->state == CONN_SENDING &&
3711 FD_ISSET( cx->fd, &writeset ) )
3712 {
3713 --num_sel;
3714 sh_xfer_do_write ( cx );
3715 }
3716 }
3717 /* continue */
3718 }
3719 /* notreached */
3720}
3721
3722void free_client_tree (void)
3723{
3724 SL_ENTER(_("free_client_tree"));
3725 zAVLFreeTree (all_clients, free_client);
3726 SL_RET0(_("free_client_tree"));
3727}
3728
3729void sh_xfer_free_all ()
3730{
3731 register int i;
3732
3733 SL_ENTER(_("sh_xfer_free_all"));
3734
3735 if (conns != NULL)
3736 for (i = SH_MINSOCK; i < maxconn; ++i)
3737 {
3738 sh_xfer_do_free ( &conns[i]);
3739 }
3740
3741
3742 free_client_tree ();
3743
3744 if (conns != NULL)
3745 SH_FREE (conns);
3746
3747 SL_RET0(_("sh_xfer_free_all"));
3748}
3749
3750
3751
3752/* #ifdef SH_WITH_SERVER */
3753#endif
3754
3755
3756
3757
3758
3759
Note: See TracBrowser for help on using the repository browser.