1 /* $OSSEC, os_xml_node_variables.c, v0.3, 2005/04/12, Daniel B. Cid$ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
13 * Available at http://www.ossec.net/
23 int OS_ApplyVariables(OS_XML *_lxml)
25 int i = 0,j = 0,s = 0;
35 /* Getting all variables */
36 for(;i<_lxml->cur;i++)
38 if(_lxml->tp[i] == XML_VARIABLE_BEGIN)
43 for(;j<_lxml->cur;j++)
45 if(_lxml->rl[j] < _lxml->rl[i])
48 else if(_lxml->tp[j] == XML_ATTR)
50 if((_lxml->el[j])&&(strcasecmp(_lxml->el[j],"name") == 0))
55 /* If not used, it will be cleaned latter */
56 snprintf(_lxml->err, 128, "XML_ERR: Memory error");
58 var = (char**)realloc(var,(s+1)*sizeof(char *));
62 var[s] = strdup(_lxml->ct[j]);
66 /* Cleaning the lxml->err */
67 strncpy(_lxml->err," ", 3);
74 snprintf(_lxml->err, 128,
75 "XML_ERR: Only \"name\" is allowed"
76 " as an attribute for a variable");
83 if((_found_var == 0)||(!_lxml->ct[i]))
85 snprintf(_lxml->err,128,
86 "XML_ERR: Bad formed variable. No value set");
91 snprintf(_lxml->err,128, "XML_ERR: Memory error");
93 value = (char**)realloc(value,(s+1)*sizeof(char *));
97 value[s] = strdup(_lxml->ct[i]);
101 strncpy(_lxml->err," ", 3);
104 } /* initial FOR to get the variables */
112 /* Looping again and modifying where found the variables */
114 for(;i<_lxml->cur;i++)
116 if(((_lxml->tp[i] == XML_ELEM) || (_lxml->tp[i] == XML_ATTR))&&
119 int tp = 0,init = 0,final = 0;
122 char lvar[256]; /* MAX Var size */
125 if(strlen(_lxml->ct[i]) <= 2)
129 /* Duplicating string */
130 p = strdup(_lxml->ct[i]);
135 snprintf(_lxml->err, 128, "XML_ERR: Memory error");
140 /* Reading the whole string */
143 if(*p == XML_VARIABLE_BEGIN)
147 memset(lvar, '\0', 256);
151 if((*p == XML_VARIABLE_BEGIN)
162 /* Looking for var */
167 /* Store everything up the variable name */
174 if(strcasecmp(var[j], lvar) != 0)
180 tsize = strlen(_lxml->ct[i]) +
181 strlen(value[j]) - tp + 1;
183 var_placeh = strdup(_lxml->ct[i]);
187 _lxml->ct[i] = (char*)calloc(tsize +2,
190 if(_lxml->ct[i] == NULL || var_placeh == NULL)
192 snprintf(_lxml->err,128, "XML_ERR: Memory "
198 strncpy(_lxml->ct[i], var_placeh, tsize);
201 _lxml->ct[i][init] = '\0';
202 strncat(_lxml->ct[i], value[j],tsize - init);
205 init = strlen(_lxml->ct[i]);
206 strncat(_lxml->ct[i], p,
207 tsize - strlen(_lxml->ct[i]));
215 /* Variale not found */
216 if((j == s) && (strlen(lvar) >= 1))
218 snprintf(_lxml->err,128,
219 "XML_ERR: Unknown variable"
231 /* Maximum size for a variable */
234 snprintf(_lxml->err,128, "XML_ERR: Invalid "
244 } /* IF XML_VAR_BEGIN */
264 /* Cleaning the variables */
272 if((value)&&(value[i]))