Changeset 295 for trunk/src/sh_forward.c
- Timestamp:
- Oct 31, 2010, 10:36:04 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/sh_forward.c
r283 r295 121 121 #endif 122 122 123 #include "sh_ipvx.h" 123 124 #include "samhain.h" 124 125 #include "sh_tiger.h" … … 239 240 * --> last part must be kept 240 241 */ 241 if (0 != is_numeric(name))242 if (0 != sh_ipvx_is_numeric(name)) 242 243 { 243 244 SL_RETURN( name, _("sh_strip_domain")); … … 2187 2188 char FileType[5]; 2188 2189 2189 struct s ockaddr_inaddr_peer;2190 struct sh_sockaddr addr_peer; 2190 2191 } sh_conn_t; 2191 2192 … … 2592 2593 #endif 2593 2594 2594 int check_addr (const char * claim, struct s ockaddr_inaddr_peer)2595 int check_addr (const char * claim, struct sh_sockaddr * addr_peer) 2595 2596 { 2596 2597 char h_name[MAXHOSTNAMELEN + 1]; 2597 2598 char h_peer[MAXHOSTNAMELEN + 1]; 2598 char h_peer_IP[16]; 2599 char tmp_peer_IP[16]; 2600 struct hostent * he; 2601 char ** p = NULL; 2602 int i; 2603 int flag = 0; 2599 char h_peer_IP[SH_IP_BUF]; 2600 char tmp_peer_IP[SH_IP_BUF]; 2601 char * canonical; 2602 char numeric[SH_IP_BUF]; 2604 2603 2605 2604 SL_ENTER(_("check_addr")); … … 2614 2613 /* Make sure we have the canonical name for the client 2615 2614 */ 2616 he = sh_gethostbyname (claim); 2617 2618 if (he != NULL && he->h_name != NULL) 2619 { 2620 if (NULL == strchr(he->h_name, '.') && he->h_addr_list != NULL) 2621 { 2622 he = sh_gethostbyaddr(he->h_addr_list[0], 2623 he->h_length, 2624 he->h_addrtype); 2625 } 2626 } 2615 canonical = sh_ipvx_canonical(claim, numeric, sizeof(numeric)); 2627 2616 2628 2617 /* copy canonical name into h_name 2629 2618 */ 2630 if (he != NULL && he->h_name != NULL) 2631 { 2632 sl_strlcpy(h_name, he->h_name, MAXHOSTNAMELEN + 1); 2619 if (canonical != NULL) 2620 { 2621 sl_strlcpy(h_name, canonical, MAXHOSTNAMELEN + 1); 2622 SH_FREE(canonical); 2633 2623 } 2634 2624 else … … 2642 2632 /* get canonical name of socket peer 2643 2633 */ 2644 he = sh_gethostbyaddr ((char *) &(addr_peer.sin_addr), 2645 sizeof(addr_peer.sin_addr), 2646 AF_INET); 2647 2648 if (he != NULL && he->h_name != NULL) 2649 { 2650 if (0 == sl_strcmp(he->h_name, _("localhost"))) 2634 canonical = sh_ipvx_addrtoname(addr_peer); 2635 2636 if (canonical) 2637 { 2638 if (0 == sl_strcmp(canonical, _("localhost"))) 2651 2639 sl_strlcpy(h_peer, sh.host.name, MAXHOSTNAMELEN + 1); 2652 2640 else 2653 sl_strlcpy(h_peer, he->h_name, MAXHOSTNAMELEN + 1); 2641 sl_strlcpy(h_peer, canonical, MAXHOSTNAMELEN + 1); 2642 SH_FREE(canonical); 2654 2643 } 2655 2644 else 2656 2645 { 2657 sl_strlcpy(tmp_peer_IP, 2658 inet_ntoa (*(struct in_addr *) &(addr_peer.sin_addr)), 2659 16); 2646 sh_ipvx_ntoa (tmp_peer_IP, sizeof(tmp_peer_IP), addr_peer); 2660 2647 sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_RESPEER, 2661 2648 claim, tmp_peer_IP); … … 2663 2650 } 2664 2651 2665 sl_strlcpy(h_peer_IP, 2666 inet_ntoa (*(struct in_addr *) he->h_addr), 2667 16); 2652 sh_ipvx_ntoa (h_peer_IP, sizeof(h_peer_IP), addr_peer); 2668 2653 2669 2654 #if 0 … … 2676 2661 /* reverse lookup 2677 2662 */ 2678 if (0 != sl_strncmp(_("127."), 2679 inet_ntoa (*(struct in_addr *) &(addr_peer.sin_addr)), 2680 4)) 2681 { 2682 he = sh_gethostbyname(h_peer); 2683 2684 if (he != NULL) 2685 { 2686 for (p = he->h_addr_list; *p; ++p) 2687 { 2688 if (0 == memcmp (*p, &(addr_peer.sin_addr), 2689 sizeof(addr_peer.sin_addr))) 2690 break; 2691 } 2692 } 2693 if (he == NULL || *p == NULL) 2694 { 2695 sl_strlcpy(tmp_peer_IP, 2696 inet_ntoa (*(struct in_addr *) &(addr_peer.sin_addr)), 2697 16); 2698 sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_LOOKERS, 2699 claim, h_peer, tmp_peer_IP); 2700 SL_RETURN ((0), _("check_addr")); 2701 } 2702 } 2703 2704 sh_tolower(h_peer); 2705 sh_tolower(h_name); 2663 if (0 == sh_ipvx_reverse_check_ok (h_peer, ServerPort, addr_peer)) 2664 { 2665 sh_ipvx_ntoa (tmp_peer_IP, sizeof(tmp_peer_IP), addr_peer); 2666 2667 sh_error_handle(lookup_err, FIL__, __LINE__, 0, MSG_TCP_LOOKERS, 2668 claim, h_peer, tmp_peer_IP); 2669 SL_RETURN ((0), _("check_addr")); 2670 } 2671 2672 /* Check whether claim and peer are identical 2673 */ 2674 2675 sh_tolower(h_peer); /* Canonical name of what the peer is */ 2676 sh_tolower(h_name); /* Canonical name of what the peer claims */ 2706 2677 2707 2678 if ((0 == sl_strcmp(h_peer, h_name)) || (0 == sl_strcmp(h_peer_IP, h_name))) … … 2709 2680 SL_RETURN ((0), _("check_addr")); 2710 2681 } 2682 #if !defined(USE_IPVX) 2711 2683 else 2712 2684 { 2713 i = 0; 2685 struct hostent * he = sh_gethostbyname(h_peer); 2686 int i = 0; 2687 int flag = 0; 2688 2714 2689 while (he->h_aliases[i] != NULL) 2715 2690 { … … 2725 2700 claim, h_peer); 2726 2701 } 2702 #endif 2727 2703 2728 2704 SL_RETURN ((0), _("check_addr")); … … 2742 2718 { 2743 2719 client_t * this_client; 2744 char peer_ip[16]; 2720 char peer_ip[SH_IP_BUF]; 2721 char numerical[SH_IP_BUF]; 2745 2722 char peer_name[MAXHOSTNAMELEN+1]; 2746 2723 char * search_string; 2747 struct sockaddr_in peer_addr; 2748 struct hostent * he;2749 char ** p = NULL;2724 2725 struct sh_sockaddr peer_addr; 2726 char * canonical; 2750 2727 2751 2728 SL_ENTER(_("search_register")); … … 2753 2730 if (UseSocketPeer == S_TRUE) 2754 2731 { 2755 peer_addr = conn->addr_peer; 2756 sl_strlcpy(peer_ip, 2757 inet_ntoa (*(struct in_addr *) &(peer_addr.sin_addr)), 16); 2732 memcpy(&peer_addr, &(conn->addr_peer), sizeof(struct sh_sockaddr)); 2733 sh_ipvx_ntoa (peer_ip, sizeof(peer_ip), &peer_addr); 2758 2734 2759 2735 /* get canonical name of socket peer 2760 2736 */ 2761 he = sh_gethostbyaddr ((char *) &(peer_addr.sin_addr), 2762 sizeof(peer_addr.sin_addr), 2763 AF_INET); 2764 2765 if (he != NULL && he->h_name != NULL) 2766 { 2767 if (0 == sl_strcmp(he->h_name, _("localhost"))) 2737 canonical = sh_ipvx_canonical(peer_ip, numerical, sizeof(numerical)); 2738 2739 if (canonical != NULL) 2740 { 2741 if (0 == sl_strcmp(canonical, _("localhost"))) 2768 2742 sl_strlcpy(peer_name, sh.host.name, MAXHOSTNAMELEN + 1); 2769 2743 else 2770 sl_strlcpy(peer_name, he->h_name, MAXHOSTNAMELEN + 1); 2771 2772 /* Reverse lookup 2773 */ 2774 if (0 != sl_strncmp(peer_ip, _("127."), 4)) 2775 { 2776 he = sh_gethostbyname(peer_name); 2777 2778 if (he != NULL) 2779 { 2780 for (p = he->h_addr_list; *p; ++p) 2781 { 2782 if (0 == memcmp (*p, &(peer_addr.sin_addr), 2783 sizeof(peer_addr.sin_addr))) 2784 break; 2785 } 2786 } 2787 if (he == NULL || *p == NULL) 2788 { 2789 /* 2790 sh_error_handle(lookup_err, FIL__, __LINE__, 0, 2791 MSG_TCP_LOOKERS, 2792 conn->buf[pos], peer_name, peer_ip); 2793 */ 2794 sl_strlcpy(peer_name, peer_ip, MAXHOSTNAMELEN + 1); 2795 } 2796 } 2797 } 2798 else 2744 sl_strlcpy(peer_name, canonical, MAXHOSTNAMELEN + 1); 2745 SH_FREE(canonical); 2746 } 2747 2748 if (0 == sh_ipvx_reverse_check_ok (peer_name, ServerPort, &peer_addr)) 2799 2749 { 2800 2750 sl_strlcpy(peer_name, peer_ip, MAXHOSTNAMELEN + 1); 2801 2751 } 2752 2802 2753 search_string = peer_name; 2803 2754 } … … 2806 2757 search_string = &(conn->buf[pos]); 2807 2758 2808 if (0 != check_addr (search_string, conn->addr_peer))2759 if (0 != check_addr (search_string, &(conn->addr_peer))) 2809 2760 { 2810 2761 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_TCP_BADCONN, … … 3662 3613 */ 3663 3614 #if defined(SH_WITH_SERVER) && defined(HAVE_LIBPRELUDE) 3664 sh_error_set_peer_ip( inet_ntoa (*(struct in_addr *) &(conn->addr_peer.sin_addr)) ); 3615 { 3616 char peer_ip[SH_IP_BUF]; 3617 sh_ipvx_ntoa(peer_ip, sizeof(peer_ip), conn->addr_peer); 3618 sh_error_set_peer_ip( peer_ip ); 3619 } 3665 3620 #endif 3666 3621 sh_error_set_peer(sh_strip_domain (conn->peer)); … … 4684 4639 int errflag; 4685 4640 int rc; 4686 struct s ockaddr_inaddr;4641 struct sh_sockaddr addr; 4687 4642 #ifdef SH_USE_LIBWRAP 4688 4643 struct request_info request; … … 4697 4652 SL_ENTER(_("sh_forward_accept")); 4698 4653 4699 rc = retry_accept(FIL__, __LINE__, sock, 4700 (struct sockaddr *) &addr, &addrlen); 4654 rc = retry_accept(FIL__, __LINE__, sock, &addr, &addrlen); 4701 4655 4702 4656 if (rc >= 0) … … 4714 4668 4715 4669 #ifdef SH_USE_LIBWRAP 4716 sl_strlcpy(daemon, SH_INSTALL_NAME, 128);4670 sl_strlcpy(daemon, SH_INSTALL_NAME, sizeof(daemon)); 4717 4671 request_init(&request, RQ_DAEMON, daemon, RQ_FILE, rc, 0); 4718 4672 fromhost(&request); 4719 4673 if (!hosts_access(&request)) 4720 4674 { 4721 sl_strlcpy(errbuf, _("Refused connection from "), 128);4722 sl_strlcat(errbuf, eval_client(&request), 128);4675 sl_strlcpy(errbuf, _("Refused connection from "), sizeof(errbuf)); 4676 sl_strlcat(errbuf, eval_client(&request), sizeof(errbuf)); 4723 4677 4724 4678 sh_error_handle((-1), FIL__, __LINE__, 0, MSG_E_SUBGEN, … … 4731 4685 #endif 4732 4686 4733 memcpy (&(newconn->addr_peer), &addr, sizeof(struct s ockaddr_in));4687 memcpy (&(newconn->addr_peer), &addr, sizeof(struct sh_sockaddr)); 4734 4688 4735 4689 /* prepare for usage of connection … … 4781 4735 } 4782 4736 4783 static struct in_addr server_interface;4737 static struct sh_sockaddr server_interface; 4784 4738 static int use_server_interface = 0; 4785 4739 … … 4791 4745 return 0; 4792 4746 } 4793 if (0 == /*@-unrecog@*/inet_aton(str, &server_interface)/*@+unrecog@*/) 4747 4748 if (0 == sh_ipvx_aton(str, &server_interface)) 4794 4749 { 4795 4750 use_server_interface = 0; 4796 4751 return -1; 4797 4752 } 4753 4798 4754 use_server_interface = 1; 4799 4755 return 0; … … 4882 4838 #ifdef INET_SYSLOG 4883 4839 #define INET_SUSPEND_TIME 180 /* equal to 3 minutes */ 4884 #define SH_MINSOCK 34840 #define SH_MINSOCK_DEFAULT 3 4885 4841 int create_syslog_socket (int flag); 4886 4842 static int recv_syslog_socket (int fd); 4887 static int syslog_sock = -1; 4843 static int syslog_sock[SH_SOCKMAX] = { -1 }; 4844 static int syslog_sock_n = 0; 4888 4845 #else 4889 #define SH_MINSOCK 2 4890 #endif 4891 4846 #define SH_MINSOCK_DEFAULT 2 4847 #endif 4848 4849 static int SH_MINSOCK = SH_MINSOCK_DEFAULT; 4892 4850 extern int pf_unix_fd; 4893 4851 4894 4852 /* the tcp socket, and the function to establish it 4895 4853 */ 4896 static int sh_tcp_sock = -1; 4854 static int sh_tcp_sock[SH_SOCKMAX] = { -1 }; 4855 static int sh_tcp_sock_n = 0; 4856 4857 static int do_socket(int domain, int type, int protocol, 4858 struct sockaddr * sa, int salen) 4859 { 4860 int sock = -1; 4861 int errnum = 0; 4862 int flag = 1; /* non-zero to enable an option */ 4863 4864 /* create the socket, bind() it and listen() 4865 */ 4866 if ((sock = socket(domain, type, protocol)) < 0 ) 4867 { 4868 errnum = errno; 4869 sh_forward_printerr (_("socket"), errnum, server_port, __LINE__); 4870 return -1; 4871 } 4872 (void) retry_fcntl( FIL__, __LINE__, sock, F_SETFD, 1 ); 4873 4874 if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 4875 (void *) &flag, sizeof(flag)) < 0 ) 4876 { 4877 errnum = errno; 4878 sh_forward_printerr (_("setsockopt"), errnum, server_port, __LINE__); 4879 sl_close_fd (FIL__, __LINE__, sock); 4880 return -1; 4881 } 4882 4883 if ( bind(sock, (struct sockaddr *) sa, salen) < 0) 4884 { 4885 if (errno != EADDRINUSE) 4886 { 4887 errnum = errno; 4888 sh_forward_printerr (_("bind"), errnum, server_port, __LINE__); 4889 sl_close_fd (FIL__, __LINE__, sock); 4890 return -1; 4891 } 4892 else 4893 { 4894 sl_close_fd (FIL__, __LINE__, sock); 4895 return -2; 4896 } 4897 } 4898 4899 if ( retry_fcntl( FIL__, __LINE__, sock, F_SETFL, O_NONBLOCK ) < 0 ) 4900 { 4901 errnum = errno; 4902 sh_forward_printerr (_("fcntl"), errnum, server_port, __LINE__); 4903 sl_close_fd (FIL__, __LINE__, sock); 4904 return -1; 4905 } 4906 4907 if ( listen(sock, 64) < 0) 4908 { 4909 errnum = errno; 4910 sh_forward_printerr (_("listen"), errnum, server_port, __LINE__); 4911 sl_close_fd (FIL__, __LINE__, sock); 4912 return -1; 4913 } 4914 4915 return sock; 4916 } 4897 4917 4898 4918 int sh_create_tcp_socket (void) 4899 4919 { 4920 #if defined(USE_IPVX) 4921 struct addrinfo *ai; 4922 struct addrinfo *p; 4923 struct addrinfo hints; 4924 char port[32]; 4925 #else 4900 4926 struct sockaddr_in addr; 4901 4927 int addrlen = sizeof(addr); 4928 #endif 4902 4929 4903 4930 int sock = -1; 4904 int errnum = 0;4905 int flag = 1; /* non-zero to enable an option */4906 4931 4907 4932 SL_ENTER(_("sh_create_tcp_socket")); … … 4909 4934 sh_forward_printerr (NULL, 0, server_port, __LINE__); 4910 4935 4911 /* create the socket, bind() it and listen() 4912 */ 4913 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) 4914 { 4915 errnum = errno; 4916 sh_forward_printerr (_("socket"), errnum, server_port, __LINE__); 4917 SL_RETURN((-1), _("sl_create_tcp_socket")); 4918 } 4919 (void) retry_fcntl( FIL__, __LINE__, sock, F_SETFD, 1 ); 4920 4921 if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 4922 (void *) &flag, sizeof(flag)) < 0 ) 4923 { 4924 errnum = errno; 4925 sh_forward_printerr (_("setsockopt"), errnum, server_port, __LINE__); 4926 SL_RETURN((-1), _("sl_create_tcp_socket")); 4927 } 4928 4929 addr.sin_family = AF_INET; 4930 addr.sin_port = htons(server_port); 4936 #if defined(USE_IPVX) 4937 if (use_server_interface == 0) 4938 { 4939 memset (&hints, '\0', sizeof (hints)); 4940 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; 4941 hints.ai_socktype = SOCK_STREAM; 4942 sl_snprintf(port, sizeof(port), "%d", server_port); 4943 4944 if (getaddrinfo (NULL, port, &hints, &ai) != 0) 4945 { 4946 int errnum = errno; 4947 sh_forward_printerr (_("getaddrinfo"), errnum, server_port, __LINE__); 4948 sl_close_fd (FIL__, __LINE__, sock); 4949 SL_RETURN((-1), _("sl_create_tcp_socket")); 4950 } 4951 4952 p = ai; 4953 4954 while (p != NULL && sh_tcp_sock_n < SH_SOCKMAX) 4955 { 4956 sock = do_socket(p->ai_family, p->ai_socktype, p->ai_protocol, 4957 p->ai_addr, p->ai_addrlen); 4958 4959 if (sock >= 0) { 4960 if (sh_tcp_sock_n < SH_SOCKMAX) { 4961 sh_tcp_sock[sh_tcp_sock_n] = sock; 4962 ++sh_tcp_sock_n; 4963 } 4964 else { 4965 sl_close_fd (FIL__, __LINE__, sock); 4966 } 4967 } else if (sock == -1) { 4968 freeaddrinfo (ai); 4969 goto end; 4970 } 4971 p = p->ai_next; 4972 } 4973 4974 freeaddrinfo (ai); 4975 } 4976 else 4977 { 4978 sh_ipvx_set_port(&server_interface, server_port); 4979 4980 sock = do_socket(server_interface.ss_family, SOCK_STREAM, 0, 4981 sh_ipvx_sockaddr_cast(&server_interface), 4982 SH_SS_LEN(server_interface)); 4983 4984 if (sock >= 0) { 4985 sh_tcp_sock[0] = sock; 4986 sh_tcp_sock_n = 1; 4987 } 4988 } 4989 #else 4931 4990 if (use_server_interface == 0) 4932 4991 addr.sin_addr.s_addr = INADDR_ANY; 4933 4992 else 4934 memcpy(&addr.sin_addr, &server_interface, sizeof(struct in_addr)); 4993 memcpy(&addr, sh_ipvx_sockaddr_cast(&server_interface), addrlen); 4994 addr.sin_family = AF_INET; 4995 addr.sin_port = htons(server_port); 4935 4996 4936 if ( bind(sock, (struct sockaddr *) &addr, addrlen) < 0) 4937 { 4938 errnum = errno; 4939 sh_forward_printerr (_("bind"), errnum, server_port, __LINE__); 4940 SL_RETURN((-1), _("sl_create_tcp_socket")); 4941 } 4942 4943 if ( retry_fcntl( FIL__, __LINE__, sock, F_SETFL, O_NONBLOCK ) < 0 ) 4944 { 4945 errnum = errno; 4946 sh_forward_printerr (_("fcntl"), errnum, server_port, __LINE__); 4947 SL_RETURN((-1), _("sl_create_tcp_socket")); 4948 } 4949 4950 if ( listen(sock, 5) < 0) 4951 { 4952 errnum = errno; 4953 sh_forward_printerr (_("listen"), errnum, server_port, __LINE__); 4954 SL_RETURN((-1), _("sl_create_tcp_socket")); 4955 } 4956 4957 sh_tcp_sock = sock; 4958 4959 SL_RETURN((sock), _("sl_create_tcp_socket")); 4997 sock = do_socket(AF_INET, SOCK_STREAM, 0, (struct sockaddr *) &addr, addrlen); 4998 4999 if (sock >= 0) { 5000 sh_tcp_sock[0] = sock; 5001 sh_tcp_sock_n = 1; 5002 } 5003 5004 #endif 5005 5006 #if defined(USE_IPVX) 5007 end: 5008 #endif 5009 if (sh_tcp_sock_n > 1) 5010 SH_MINSOCK += (sh_tcp_sock_n - 1); 5011 5012 SL_RETURN((sh_tcp_sock_n), _("sl_create_tcp_socket")); 4960 5013 } 4961 5014 … … 4984 5037 int nowconn; 4985 5038 int status; 4986 int high_fd ;5039 int high_fd = -1; 4987 5040 register int i; 4988 5041 long dummy = 0; … … 5000 5053 5001 5054 int setsize_fd; 5055 5056 int sock_tcp[2]; 5057 int sock_unix; 5058 #ifdef INET_SYSLOG 5059 int sock_log[2]; 5060 #endif 5002 5061 5003 5062 SL_ENTER(_("sh_receive")); … … 5015 5074 aud_exit(FIL__, __LINE__, EXIT_FAILURE); 5016 5075 } 5017 sock = sh_tcp_sock; 5076 5077 sock = sh_tcp_sock[0]; 5018 5078 5019 5079 /* **************************************************************** … … 5031 5091 */ 5032 5092 maxconn = get_open_max() - 6; 5093 5033 5094 /* ugly fix for FreeBSD compiler warning; casting FD_SETSIZE in the 5034 5095 * conditional expression does not suppress the warning... */ … … 5079 5140 /* conns[0] is the listen() socket. Always in read mode. 5080 5141 */ 5081 conns[0].fd = sock; 5082 conns[0].state = CONN_READING; 5083 high_fd = sock; 5142 sock = 0; 5143 5144 sock_tcp[0] = 0; 5145 while (sock < sh_tcp_sock_n) 5146 { 5147 conns[sock].fd = sh_tcp_sock[sock]; 5148 conns[sock].state = CONN_READING; 5149 high_fd = (sh_tcp_sock[sock] > high_fd) ? sh_tcp_sock[sock] : high_fd; 5150 ++sock; 5151 } 5152 sock_tcp[1] = sock; 5084 5153 5085 conns[ 1].fd = pf_unix_fd;5086 conns[ 1].state = CONN_READING;5154 conns[sock].fd = pf_unix_fd; 5155 conns[sock].state = CONN_READING; 5087 5156 high_fd = (pf_unix_fd > high_fd) ? pf_unix_fd : high_fd; 5088 5157 5158 sock_unix = sock; 5159 5160 ++sock; 5161 5089 5162 #ifdef INET_SYSLOG 5090 conns[2].fd = -1; 5163 conns[sock].fd = -1; 5164 5091 5165 if ( sh_forward_printerr_final(1) < 0) 5092 5166 { … … 5095 5169 aud_exit(FIL__, __LINE__, EXIT_FAILURE); 5096 5170 } 5097 sock = syslog_sock; 5098 5099 if (sock >= 0) 5100 { 5101 conns[2].fd = sock; 5102 conns[2].state = CONN_READING; 5103 high_fd = (high_fd > conns[2].fd) ? high_fd : conns[2].fd; 5171 5172 sock_log[0] = sock; 5173 sock_log[1] = sock; 5174 5175 if (syslog_sock_n > 0) 5176 { 5177 int s2; 5178 for (s2 = 0; s2 < syslog_sock_n; ++s2) 5179 { 5180 conns[sock].fd = syslog_sock[s2]; 5181 conns[sock].state = CONN_READING; 5182 high_fd = (high_fd > conns[sock].fd) ? high_fd : conns[sock].fd; 5183 ++sock; 5184 } 5185 sock_log[1] = sock; 5186 5104 5187 } 5105 5188 #endif … … 5153 5236 5154 5237 (void) sh_readconf_read (); 5238 5155 5239 for (i = SH_MINSOCK; i < maxconn; ++i) 5156 5240 if (conns[i].state != CONN_FREE && … … 5230 5314 FD_ZERO( &readset ); 5231 5315 FD_ZERO( &writeset ); 5232 FD_SET(conns[0].fd, &readset ); 5233 high_fd = conns[0].fd; 5234 5235 if (conns[1].fd > -1) 5236 { 5237 FD_SET(conns[1].fd, &readset ); 5238 high_fd = (high_fd > conns[1].fd) ? high_fd : conns[1].fd; 5316 high_fd = conns[0].fd; 5317 5318 for (sock = sock_tcp[0]; sock < sock_tcp[1]; ++sock) 5319 { 5320 FD_SET(conns[sock].fd, &readset ); 5321 high_fd = (high_fd > conns[sock].fd) ? high_fd : conns[sock].fd; 5322 } 5323 5324 if (conns[sock_unix].fd > -1) 5325 { 5326 FD_SET(conns[sock_unix].fd, &readset ); 5327 high_fd = (high_fd > conns[sock_unix].fd) ? high_fd : conns[sock_unix].fd; 5239 5328 } 5240 5329 5241 5330 #ifdef INET_SYSLOG 5242 if (conns[2].fd > -1) 5243 { 5244 FD_SET(conns[2].fd, &readset ); 5245 high_fd = (high_fd > conns[2].fd) ? high_fd : conns[2].fd; 5331 for (sock = sock_log[0]; sock < sock_log[1]; ++sock) 5332 { 5333 if (conns[sock].fd > -1) 5334 { 5335 FD_SET(conns[sock].fd, &readset ); 5336 high_fd = (high_fd > conns[sock].fd) ? high_fd : conns[sock].fd; 5337 } 5246 5338 } 5247 5339 #endif … … 5405 5497 /* New connection. 5406 5498 */ 5407 if ( FD_ISSET(conns[0].fd , &readset )) /* a new connection */ 5408 { 5409 --num_sel; 5410 status = 0; 5411 if (nowconn < maxconn && sig_terminate == 0 && sig_termfast == 0) 5499 for (sock = sock_tcp[0]; sock < sock_tcp[1]; ++sock) 5500 { 5501 if ( FD_ISSET(conns[sock].fd , &readset )) /* a new connection */ 5412 5502 { 5413 i = SH_MINSOCK; 5414 while (i < maxconn) 5503 --num_sel; 5504 status = 0; 5505 if (nowconn < maxconn && sig_terminate == 0 && sig_termfast == 0) 5415 5506 { 5416 if (conns[i].state == CONN_FREE) 5507 /* Find a free slot to accept the connection 5508 */ 5509 i = SH_MINSOCK; 5510 while (i < maxconn) 5417 5511 { 5418 status = sh_forward_accept (conns[0].fd, &conns[i]); 5419 if (status == 0) 5512 if (conns[i].state == CONN_FREE) 5420 5513 { 5421 high_fd = 5422 (high_fd > conns[i].fd ? high_fd : conns[i].fd); 5423 ++server_status.conn_open; 5424 ++server_status.conn_total; 5425 server_status.last = time (NULL); 5514 /* Here we run the accept() and copy the peer to 5515 * the free slot. 5516 */ 5517 status = sh_forward_accept(conns[sock].fd, &conns[i]); 5518 5519 if (status == 0) 5520 { 5521 high_fd = 5522 (high_fd > conns[i].fd ? high_fd : conns[i].fd); 5523 ++server_status.conn_open; 5524 ++server_status.conn_total; 5525 server_status.last = time (NULL); 5526 } 5527 break; 5426 5528 } 5427 break;5529 ++i; 5428 5530 } 5429 ++i;5430 5531 } 5532 /* This re-runs select to accept data on the new 5533 * connection, rather than first dealing with old 5534 * connections. 5535 */ 5536 if (status == 0) 5537 continue; 5431 5538 } 5432 if (status == 0)5433 continue;5434 5539 } 5435 5540 5436 5541 /* check for commands on the socket 5437 5542 */ 5438 if (conns[ 1].fd > (-1) && FD_ISSET(conns[1].fd , &readset ))5543 if (conns[sock_unix].fd > (-1) && FD_ISSET(conns[sock_unix].fd , &readset )) 5439 5544 { 5440 5545 sh_socket_poll(); … … 5442 5547 5443 5548 #ifdef INET_SYSLOG 5444 if (conns[2].fd > (-1) && FD_ISSET(conns[2].fd , &readset )) 5445 { 5446 recv_syslog_socket (conns[2].fd); 5549 for (sock = sock_log[0]; sock < sock_log[1]; ++sock) 5550 { 5551 if (conns[sock].fd > (-1) && FD_ISSET(conns[sock].fd , &readset )) 5552 { 5553 recv_syslog_socket (conns[sock].fd); 5554 } 5447 5555 } 5448 5556 #endif … … 5503 5611 5504 5612 #ifdef INET_SYSLOG 5505 5506 #ifdef HAVE_INET_ATON5507 static char * my_inet_ntoa(struct in_addr in)5508 {5509 return inet_ntoa(in);5510 }5511 #else5512 static char * my_inet_ntoa(struct in_addr in)5513 {5514 unsigned char a, b, c, d;5515 static char foo[16];5516 char bar[4];5517 memcpy (bar, &(in.s_addr), 4); /* memory alignment (?) */5518 memcpy (&a, &bar[0], 1);5519 memcpy (&b, &bar[1], 1);5520 memcpy (&c, &bar[2], 1);5521 memcpy (&d, &bar[3], 1);5522 sprintf(foo, "%d.%d.%d.%d", /* known to fit */5523 (int) a, (int) b, (int) c, (int) d);5524 return foo;5525 }5526 #endif5527 5528 5613 5529 5614 /* Unlike Linux / FreeBSD, most systems don't define the stuff below … … 5675 5760 char errbuf[SH_ERRBUF_SIZE]; 5676 5761 5762 struct sh_sockaddr ss; 5763 struct sockaddr * sa = (struct sockaddr *) &from; 5764 char namebuf[SH_BUFSIZE]; 5765 5677 5766 /* The 6th argument in recvfrom is *socklen_t in Linux and *BSD, 5678 5767 * but *int everywhere else. Because socklen_t is unsigned int, there … … 5696 5785 res = recvfrom(fd, buf, 1047, 0, (struct sockaddr *) &from, &fromlen); 5697 5786 5787 sh_ipvx_save(&ss, sa->sa_family, &from); 5788 sh_ipvx_ntoa(namebuf, sizeof(namebuf), &ss); 5789 5698 5790 if (res > 0) 5699 5791 { … … 5715 5807 sh_error_handle((-1), FIL__, __LINE__, res, MSG_ERR_SYSLOG, 5716 5808 sh_error_message(res, errbuf, sizeof(errbuf)), 5717 my_inet_ntoa(from.sin_addr));5809 namebuf); 5718 5810 SL_RETURN( (-1), _("recv_syslog_socket")); 5719 5811 } 5720 5812 5721 TPT(( 0, FIL__, __LINE__, _("msg=<UDP message from %s>\n"), 5722 my_inet_ntoa(from.sin_addr))); 5813 TPT(( 0, FIL__, __LINE__, _("msg=<UDP message from %s>\n"), namebuf )); 5814 5723 5815 ptr = bptr; 5724 5816 i = 0; … … 5775 5867 sh_error_handle((-1), FIL__, __LINE__, res, MSG_ERR_SYSLOG, 5776 5868 sh_error_message(res, errbuf, sizeof(errbuf)), 5777 my_inet_ntoa(from.sin_addr));5869 namebuf); 5778 5870 5779 5871 /* don't accept anything the next 2 seconds … … 5790 5882 } 5791 5883 5792 /* callerFlag == S_TRUE means override the enable_syslog_socket flag 5793 */ 5794 int create_syslog_socket (int callerFlag) 5884 static int do_syslog_socket(int domain, int type, int protocol, 5885 struct sockaddr * sa, int salen) 5795 5886 { 5796 5887 int flag = 1; /* non-zero to enable an option */ … … 5798 5889 int errnum; 5799 5890 int res; 5891 5892 /* create the socket, bind() it and listen() 5893 */ 5894 sock = socket(domain, type, protocol); 5895 5896 if (sock < 0) 5897 { 5898 errnum = errno; 5899 sh_forward_printerr (_("syslog socket"), errnum, 514, __LINE__); 5900 return -1; 5901 } 5902 (void) retry_fcntl( FIL__, __LINE__, sock, F_SETFD, 1 ); 5903 5904 if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 5905 (void *) &flag, sizeof(flag)) < 0 ) 5906 { 5907 errnum = errno; 5908 sh_forward_printerr (_("syslog setsockopt SO_REUSEADDR"), 5909 errnum, 514, __LINE__); 5910 return -1; 5911 } 5912 5913 #if defined(SO_BSDCOMPAT) 5914 if ( setsockopt(sock, SOL_SOCKET, SO_BSDCOMPAT, 5915 (void *) &flag, sizeof(flag)) < 0 ) 5916 { 5917 errnum = errno; 5918 sh_forward_printerr (_("syslog setsockopt SO_BSDCOMPAT"), 5919 errnum, 514, __LINE__); 5920 return -1; 5921 } 5922 #endif 5923 5924 res = bind(sock, sa, salen); 5925 5926 if ( res < 0) 5927 { 5928 errnum = errno; 5929 sh_forward_printerr (_("syslog bind"), errnum, 514, __LINE__); 5930 sl_close_fd(FIL__, __LINE__, sock); 5931 return -1; 5932 } 5933 return sock; 5934 } 5935 5936 /* callerFlag == S_TRUE means override the enable_syslog_socket flag 5937 */ 5938 int create_syslog_socket (int callerFlag) 5939 { 5940 int sock; 5941 5942 #if defined(USE_IPVX) 5943 struct addrinfo *ai; 5944 struct addrinfo *p; 5945 struct addrinfo hints; 5946 #else 5800 5947 struct sockaddr_in addr; 5801 5948 int addrlen = sizeof(addr); 5949 #endif 5802 5950 5803 5951 SL_ENTER(_("create_syslog_socket")); … … 5811 5959 TPT(( 0, FIL__, __LINE__, _("msg=<close syslog socket>\n"))); 5812 5960 sl_close_fd(FIL__, __LINE__, syslog_sock); 5813 syslog_sock = -1;5961 syslog_sock[0] = -1; 5814 5962 } 5815 5963 SL_RETURN((-1), _("create_syslog_socket")); … … 5818 5966 sh_forward_printerr (NULL, 0, 514, __LINE__); 5819 5967 5820 /* create the socket, bind() it and listen() 5821 */ 5822 sock = socket(AF_INET, SOCK_DGRAM, 0); 5823 5824 if (sock < 0) 5825 { 5826 errnum = errno; 5827 sh_forward_printerr (_("syslog socket"), errnum, 514, __LINE__); 5828 SL_RETURN((-1), _("create_syslog_socket")); 5829 } 5830 (void) retry_fcntl( FIL__, __LINE__, sock, F_SETFD, 1 ); 5831 5832 if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 5833 (void *) &flag, sizeof(flag)) < 0 ) 5834 { 5835 errnum = errno; 5836 sh_forward_printerr (_("syslog setsockopt SO_REUSEADDR"), 5837 errnum, 514, __LINE__); 5838 SL_RETURN((-1), _("create_syslog_socket")); 5839 } 5840 5841 #if defined(SO_BSDCOMPAT) 5842 if ( setsockopt(sock, SOL_SOCKET, SO_BSDCOMPAT, 5843 (void *) &flag, sizeof(flag)) < 0 ) 5844 { 5845 errnum = errno; 5846 sh_forward_printerr (_("syslog setsockopt SO_BSDCOMPAT"), 5847 errnum, 514, __LINE__); 5848 SL_RETURN((-1), _("create_syslog_socket")); 5849 } 5850 #endif 5851 5968 #if !defined(USE_IPVX) 5969 5852 5970 memset(&addr, 0, sizeof(addr)); 5853 5971 addr.sin_family = AF_INET; 5854 5972 addr.sin_port = htons(514); 5855 5973 5856 res = bind(sock, (struct sockaddr *) &addr, addrlen); 5857 5858 if ( res < 0) 5974 do_syslog_socket(AF_INET, SOCK_DGRAM, 0, (struct sockaddr *) &addr, addrlen); 5975 5976 if (sock >= 0) { 5977 syslog_sock[0] = sock; 5978 syslog_sock_n = 1; 5979 } 5980 5981 #else 5982 memset (&hints, '\0', sizeof (hints)); 5983 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; 5984 hints.ai_socktype = SOCK_DGRAM; 5985 if (getaddrinfo (NULL, "syslog", &hints, &ai) != 0) 5859 5986 { 5860 5987 errnum = errno; 5861 sh_forward_printerr (_(" syslog bind"), errnum, 514, __LINE__);5862 sl_close_fd (FIL__, __LINE__, sock);5988 sh_forward_printerr (_("getaddrinfo"), errnum, 514, __LINE__); 5989 sl_close_fd (FIL__, __LINE__, sock); 5863 5990 SL_RETURN((-1), _("create_syslog_socket")); 5864 5991 } 5865 5866 syslog_sock = sock; 5867 5868 SL_RETURN((sock), _("create_syslog_socket")); 5992 5993 p = ai; 5994 5995 while (p != NULL && syslog_sock_n < SH_SOCKMAX) 5996 { 5997 sock = do_syslog_socket(p->ai_family, p->ai_socktype, p->ai_protocol, 5998 p->ai_addr, p->ai_addrlen); 5999 6000 if (sock >= 0) { 6001 if (syslog_sock_n < SH_SOCKMAX) { 6002 syslog_sock[syslog_sock_n] = sock; 6003 ++syslog_sock_n; 6004 } 6005 else { 6006 sl_close_fd (FIL__, __LINE__, sock); 6007 } 6008 } else if (sock == -1) { 6009 freeaddrinfo (ai); 6010 goto end; 6011 } 6012 p = p->ai_next; 6013 } 6014 freeaddrinfo (ai); 6015 #endif 6016 6017 end: 6018 if (syslog_sock_n > 1) 6019 SH_MINSOCK += (syslog_sock_n - 1); 6020 6021 SL_RETURN((syslog_sock_n), _("create_syslog_socket")); 5869 6022 } 5870 6023 /* #ifdef INET_SYSLOG */
Note:
See TracChangeset
for help on using the changeset viewer.