summaryrefslogtreecommitdiffstats
path: root/src/zabbix_agent/zabbix_agent.c
blob: 1f9dfcc31406d4909d42ce19ed8fd1be4d91a565 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*#define	TEST_PARAMETERS*/

#include "config.h"

#include <stdlib.h>
#include <stdio.h>

#include <unistd.h>
#include <signal.h>

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

/* For bcopy */
#include <string.h>

/* For config file operations */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "common.h"
#include "cfg.h"
#include "sysinfo.h"
#include "zabbix_agent.h"

static	char	*CONFIG_HOSTS_ALLOWED	= NULL;
static	int	CONFIG_TIMEOUT		= AGENT_TIMEOUT;

void	signal_handler( int sig )
{
	if( SIGALRM == sig )
	{
		signal( SIGALRM, signal_handler );
	}
 
	if( SIGQUIT == sig || SIGINT == sig || SIGTERM == sig )
	{
	}
	exit( FAIL );
}

int	add_parameter(char *value)
{
	char	*value2;

	value2=strstr(value,",");
	if(NULL == value2)
	{
		return	FAIL;
	}
	value2[0]=0;
	value2++;
	add_user_parameter(value, value2);
	return	SUCCEED;
}

void    init_config(void)
{
	struct cfg_line cfg[]=
	{
/*               PARAMETER      ,VAR    ,FUNC,  TYPE(0i,1s),MANDATORY,MIN,MAX
*/
		{"Server",&CONFIG_HOSTS_ALLOWED,0,TYPE_STRING,PARM_MAND,0,0},
		{"Timeout",&CONFIG_TIMEOUT,0,TYPE_INT,PARM_OPT,1,30},
		{"UserParameter",0,&add_parameter,0,0,0,0},
		{0}
	};

	parse_cfg_file("/etc/zabbix/zabbix_agent.conf",cfg);
}

int	check_security(void)
{
	char	*sname;
	struct	sockaddr_in name;
	int	i;
	char	*s;
	char	*tmp;

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

		sname=inet_ntoa(name.sin_addr);

		tmp=strdup(CONFIG_HOSTS_ALLOWED);
                s=(char *)strtok(tmp,",");
                while(s!=NULL)
                {
                        if(strcmp(sname, s)==0)
                        {
                                return  SUCCEED;
                        }
                        s=(char *)strtok(NULL,",");
                }
	}
        else
	{
/*		syslog( LOG_WARNING, "Error getpeername [%m]");*/
/*		syslog( LOG_WARNING, "Connection rejected");*/
		return FAIL;
	}
	return	FAIL;
}

int	main()
{
	char	s[MAX_STRING_LEN+1];
	char	value[MAX_STRING_LEN+1];

#ifdef	TEST_PARAMETERS
	init_config();
	test_parameters();
	return	SUCCEED;
#endif

	signal( SIGINT,  signal_handler );
	signal( SIGQUIT, signal_handler );
	signal( SIGTERM, signal_handler );
	signal( SIGALRM, signal_handler );

	init_config();

	alarm(CONFIG_TIMEOUT);

	if(check_security() == FAIL)
	{
		exit(FAIL);
	}

	fgets(s,MAX_STRING_LEN,stdin);
	process(s,value);

	printf("%s\n",value);

	fflush(stdout);

	alarm(0);

	return SUCCEED;
}