summaryrefslogtreecommitdiffstats
path: root/include/security.c
blob: eb9f8718ff954af16f3fb403273cb68ef74f4d08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* 
** Zabbix
** Copyright (C) 2000,2001,2002,2003,2004 Alexei Vladishev
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <string.h>

#include <errno.h>

/* config.h is required for socklen_t (undefined under Solaris) */
#include "config.h"
#include "common.h"
#include "log.h"

int	check_security(int sockfd, char *ip_list, int allow_if_empty)
{
	char	*sname;
	struct	sockaddr_in name;
	int	i;
	char	*s;

	char	tmp[MAX_STRING_LEN];

        zabbix_log( LOG_LEVEL_DEBUG, "In check_security()");

	if( (1 == allow_if_empty) && (strlen(ip_list)==0) )
	{
		return SUCCEED;
	}

	i=sizeof(name);

/*	if(getpeername(sockfd,  (struct sockaddr *)&name, (size_t *)&i) == 0)*/
	if(getpeername(sockfd,  (struct sockaddr *)&name, (socklen_t *)&i) == 0)
	{
		i=sizeof(struct sockaddr_in);

		sname=inet_ntoa(name.sin_addr);

		zabbix_log( LOG_LEVEL_DEBUG, "Connection from [%s]. Allowed servers [%s] ",sname, ip_list);

		strscpy(tmp,ip_list);
        	s=(char *)strtok(tmp,",");
		while(s!=NULL)
		{
			if(strcmp(sname, s)==0)
			{
				return	SUCCEED;
			}
                	s=(char *)strtok(NULL,",");
		}
	}
	else
	{
		zabbix_log( LOG_LEVEL_WARNING, "Error getpeername [%s]",strerror(errno));
		zabbix_log( LOG_LEVEL_WARNING, "Connection rejected");
		return FAIL;
	}
	zabbix_log( LOG_LEVEL_WARNING, "Connection from [%s] rejected. Allowed server is [%s] ",sname, ip_list);
	return	FAIL;
}