~ubuntu-dev/ubuntu/lucid/zabbix/lucid-201002110857

« back to all changes in this revision

Viewing changes to src/libs/zbxcommon/comms.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Ablassmeier
  • Date: 2007-07-02 09:06:51 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070702090651-8l6fl3fjw9rh6l2u
Tags: 1:1.4.1-2
Add patch from SVN in order to fix Incorrect processing of character '%'
in user parameters and remote commands.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <string.h>
2
 
#include <stdio.h>
 
1
/* 
 
2
** ZABBIX
 
3
** Copyright (C) 2000-2005 SIA Zabbix
 
4
**
 
5
** This program is free software; you can redistribute it and/or modify
 
6
** it under the terms of the GNU General Public License as published by
 
7
** the Free Software Foundation; either version 2 of the License, or
 
8
** (at your option) any later version.
 
9
**
 
10
** This program is distributed in the hope that it will be useful,
 
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
** GNU General Public License for more details.
 
14
**
 
15
** You should have received a copy of the GNU General Public License
 
16
** along with this program; if not, write to the Free Software
 
17
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
**/
3
19
 
4
20
#include "common.h"
5
21
#include "log.h"
 
22
#include "base64.h"
6
23
 
7
 
int     comms_create_request(char *host, char *key, char *data, char *lastlogsize, char *request,int maxlen)
 
24
/******************************************************************************
 
25
 *                                                                            *
 
26
 * Function: comms_create_request                                             *
 
27
 *                                                                            *
 
28
 * Purpose: dinamical xml request generation                                  *
 
29
 *                                                                            *
 
30
 * Return value: XML request                                                  *
 
31
 *                                                                            *
 
32
 * Author: Eugene Grigorjev                                                   *
 
33
 *                                                                            *
 
34
 * Comments:  required free allocated string with function 'zbx_free'         *
 
35
 *                                                                            *
 
36
 ******************************************************************************/
 
37
char*   comms_create_request(
 
38
        const char              *host,
 
39
        const char              *key,
 
40
        const char              *data,
 
41
        long                    *lastlogsize,
 
42
        unsigned long   *timestamp,
 
43
        const char              *source,
 
44
        unsigned short  *severity
 
45
        )
8
46
{
9
 
        int ret = SUCCEED;
10
 
        char host_b64[MAX_STRING_LEN];
11
 
        char key_b64[MAX_STRING_LEN];
 
47
#define ADD_XML_DATA(tag_name, var) \
 
48
        data_b64[0] = '\0'; \
 
49
        str_base64_encode(var, data_b64, (int)strlen(var)); \
 
50
        request = zbx_strdcatf(request, "<" tag_name ">%s</" tag_name ">",      data_b64)
 
51
 
12
52
        char data_b64[ZBX_MAX_B64_LEN];
13
 
        char lastlogsize_b64[MAX_STRING_LEN];
14
 
 
15
 
        memset(request,0,maxlen);
16
 
        memset(host_b64,0,sizeof(host_b64));
17
 
        memset(key_b64,0,sizeof(key_b64));
 
53
        char *tmp_str = NULL;
 
54
        char *request = NULL;
 
55
        
 
56
        assert(host);
 
57
        assert(key);
 
58
        assert(data);
 
59
 
 
60
        /* zabbix_log(LOG_LEVEL_DEBUG, "comms_create_request host [%s] key [%s] data [%s]",host,key,data); */
 
61
 
18
62
        memset(data_b64,0,sizeof(data_b64));
19
 
        memset(lastlogsize_b64,0,sizeof(lastlogsize_b64));
20
 
 
21
 
        str_base64_encode(host, host_b64, strlen(host));
22
 
        str_base64_encode(key, key_b64, strlen(key));
23
 
        str_base64_encode(data, data_b64, strlen(data));
24
 
        if(lastlogsize[0]!=0)
25
 
        {
26
 
                str_base64_encode(lastlogsize, lastlogsize_b64, strlen(lastlogsize));
27
 
        }
28
 
 
29
 
/*      fprintf(stderr, "Data Base64 [%s]\n", data_b64);*/
30
 
 
31
 
        if(lastlogsize[0]==0)
32
 
        {
33
 
                snprintf(request,maxlen,"<req><host>%s</host><key>%s</key><data>%s</data></req>",host_b64,key_b64,data_b64);
34
 
        }
35
 
        else
36
 
        {
37
 
                snprintf(request,maxlen,"<req><host>%s</host><key>%s</key><data>%s</data><lastlogsize>%s</lastlogsize></req>",host_b64,key_b64,data_b64,lastlogsize_b64);
38
 
        }
39
 
/*      fprintf(stderr, "Max [%d] Result [%s][%d]\n", maxlen , request, strlen(request));*/
40
 
 
41
 
        return ret;
 
63
 
 
64
        request = zbx_dsprintf(NULL,"%s", "<req>");
 
65
        
 
66
        ADD_XML_DATA("host",    host);
 
67
        ADD_XML_DATA("key",             key);
 
68
        ADD_XML_DATA("data",    data);
 
69
 
 
70
        if(lastlogsize)
 
71
        {
 
72
                tmp_str = zbx_dsprintf(NULL, "%li", *lastlogsize);
 
73
                ADD_XML_DATA("lastlogsize",     tmp_str);
 
74
                zbx_free(tmp_str);
 
75
        }
 
76
 
 
77
        if(timestamp)
 
78
        {
 
79
                assert(source);
 
80
                assert(severity);
 
81
                
 
82
                tmp_str = zbx_dsprintf(NULL, "%lu", *timestamp);
 
83
                ADD_XML_DATA("timestamp",       tmp_str);
 
84
                zbx_free(tmp_str);
 
85
 
 
86
                ADD_XML_DATA("source",          source);
 
87
 
 
88
                tmp_str = zbx_dsprintf(NULL, "%u", *severity);
 
89
                ADD_XML_DATA("severity",        tmp_str);
 
90
                zbx_free(tmp_str);
 
91
        }
 
92
 
 
93
        return zbx_strdcat(request, "</req>");
42
94
}
 
95
 
43
96
int     comms_parse_response(char *xml,char *host,char *key, char *data, char *lastlogsize, char *timestamp,
44
97
               char *source, char *severity, int maxlen)
45
98
{
54
107
        char source_b64[ZBX_MAX_B64_LEN];
55
108
        char severity_b64[MAX_STRING_LEN];
56
109
 
 
110
        assert(key);
 
111
        assert(host);
 
112
        assert(data);
 
113
        assert(lastlogsize);
 
114
        assert(timestamp);
 
115
        assert(source);
 
116
        assert(severity);
 
117
 
57
118
        memset(host_b64,0,sizeof(host_b64));
58
119
        memset(key_b64,0,sizeof(key_b64));
59
120
        memset(data_b64,0,sizeof(data_b64));
89
150
        return ret;
90
151
}
91
152
 
92
 
int     zbx_fork()
93
 
{
94
 
        fflush(stdout);
95
 
        fflush(stderr);
96
 
        return fork();
97
 
}
98
 
 
99
 
void    *zbx_malloc(size_t size)
 
153
void    *zbx_malloc2(char *filename, int line, void *old, size_t size)
100
154
{
101
155
        register int max_attempts;
102
156
        void *ptr = NULL;
103
157
 
104
 
        for(    max_attempts = 10, size = MAX(size, 1);
 
158
/*      Old pointer must be NULL */
 
159
        if(old != NULL)
 
160
        {
 
161
                zabbix_log(LOG_LEVEL_CRIT,"[file:%s,line:%d] zbx_malloc: allocating already allocated memory. Please report this to ZABBIX developers.",
 
162
                        filename,
 
163
                        line);
 
164
                /* Exit if defined DEBUG. Ignore otherwise. */
 
165
                zbx_dbg_assert(0);
 
166
        }
 
167
 
 
168
/*      zabbix_log(LOG_LEVEL_DEBUG,"In zbx_malloc(size:%d)", size); */
 
169
 
 
170
        for(
 
171
                max_attempts = 10, size = MAX(size, 1);
105
172
                max_attempts > 0 && !ptr;
106
173
                ptr = malloc(size),
107
174
                max_attempts--
108
175
        );
109
176
 
110
 
        if(ptr) return ptr;
111
 
 
112
 
        zabbix_log(LOG_LEVEL_CRIT,"out of memory. requested '%i' bytes.", size);
113
 
        exit(FAIL);
114
 
 
115
 
         /* Program will never reach this point. */
116
 
        return ptr;
117
 
}
 
177
        if(ptr) return ptr;
 
178
 
 
179
        zabbix_log(LOG_LEVEL_CRIT,"zbx_malloc: out of memory. requested '%lu' bytes.", size);
 
180
        exit(FAIL);
 
181
 
 
182
        /* Program will never reach this point. */
 
183
        return ptr;
 
184
}
 
185
 
 
186
void    *zbx_realloc(void *src, size_t size)
 
187
{
 
188
        register int max_attempts;
 
189
        void *ptr = NULL;
 
190
 
 
191
        assert(src);
 
192
 
 
193
/*      zabbix_log(LOG_LEVEL_DEBUG,"In zbx_realloc(size:%d)", size); */
 
194
 
 
195
        for(
 
196
                max_attempts = 10, size = MAX(size, 1);
 
197
                max_attempts > 0 && !ptr;
 
198
                ptr = realloc(src, size),
 
199
                max_attempts--
 
200
        );
 
201
 
 
202
        if(ptr) return ptr;
 
203
 
 
204
        zabbix_log(LOG_LEVEL_CRIT,"zbx_realloc: out of memory. requested '%lu' bytes.", size);
 
205
        exit(FAIL);
 
206
 
 
207
        /* Program will never reach this point. */
 
208
        return ptr;
 
209
}
 
210
 
 
211