1 /* @(#) $Id: ./src/shared/validate_op.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
14 * Available at http://www.ossec.net
20 char *ip_address_regex =
21 "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}/?"
22 "([0-9]{0,2}|[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})$";
29 /* Read the file and return a string the matches the following
30 * format: high_name.low_name.
31 * If return is not null, value must be free.
33 static char *_read_file(char *high_name, char *low_name, char *defines_file)
36 char def_file[OS_FLSIZE +1];
37 char buf[OS_SIZE_1024 +1];
45 snprintf(def_file,OS_FLSIZE,"%s", defines_file);
49 snprintf(def_file,OS_FLSIZE,"%s%s",DEFAULTDIR, defines_file);
52 snprintf(def_file,OS_FLSIZE,"%s", defines_file);
56 fp = fopen(def_file, "r");
59 if(strcmp(defines_file, OSSEC_LDEFINES) != 0)
61 merror(FOPEN_ERROR, __local_name, def_file);
67 if(!high_name || !low_name)
69 merror(NULL_ERROR, __local_name);
75 buf[OS_SIZE_1024] = '\0';
76 while(fgets(buf, OS_SIZE_1024 , fp) != NULL)
78 /* Commented or blank lines */
79 if(buf[0] == '#' || buf[0] == ' ' || buf[0] == '\n')
84 /* Messages not formatted correctly */
85 buf_pt = strchr(buf, '.');
88 merror(FGETS_ERROR, __local_name, def_file, buf);
92 /* Checking for the high name */
93 *buf_pt = '\0'; buf_pt++;
94 if(strcmp(buf, high_name) != 0)
101 /* Getting the equal */
102 buf_pt = strchr(buf_pt, '=');
105 merror(FGETS_ERROR, __local_name, def_file, buf);
109 /* Checking for the low name */
110 *buf_pt = '\0'; buf_pt++;
111 if(strcmp(tmp_buffer, low_name) != 0)
116 /* Removing new lines or anything that we cause errors */
117 tmp_buffer = strrchr(buf_pt, '\n');
123 tmp_buffer = strrchr(buf_pt, '\r');
129 os_strdup(buf_pt, ret);
140 /* Getting the netmask based on the integer value. */
141 int getNetmask(int mask, char *strmask, int size)
149 snprintf(strmask, size, "/any");
155 if(htonl(_netmasks[i]) == mask)
157 snprintf(strmask, size, "/%d", i);
167 /* Initialize netmasks -- took from snort util.c */
172 _netmasks[1] = 0x80000000;
173 _netmasks[2] = 0xC0000000;
174 _netmasks[3] = 0xE0000000;
175 _netmasks[4] = 0xF0000000;
176 _netmasks[5] = 0xF8000000;
177 _netmasks[6] = 0xFC000000;
178 _netmasks[7] = 0xFE000000;
179 _netmasks[8] = 0xFF000000;
180 _netmasks[9] = 0xFF800000;
181 _netmasks[10] = 0xFFC00000;
182 _netmasks[11] = 0xFFE00000;
183 _netmasks[12] = 0xFFF00000;
184 _netmasks[13] = 0xFFF80000;
185 _netmasks[14] = 0xFFFC0000;
186 _netmasks[15] = 0xFFFE0000;
187 _netmasks[16] = 0xFFFF0000;
188 _netmasks[17] = 0xFFFF8000;
189 _netmasks[18] = 0xFFFFC000;
190 _netmasks[19] = 0xFFFFE000;
191 _netmasks[20] = 0xFFFFF000;
192 _netmasks[21] = 0xFFFFF800;
193 _netmasks[22] = 0xFFFFFC00;
194 _netmasks[23] = 0xFFFFFE00;
195 _netmasks[24] = 0xFFFFFF00;
196 _netmasks[25] = 0xFFFFFF80;
197 _netmasks[26] = 0xFFFFFFC0;
198 _netmasks[27] = 0xFFFFFFE0;
199 _netmasks[28] = 0xFFFFFFF0;
200 _netmasks[29] = 0xFFFFFFF8;
201 _netmasks[30] = 0xFFFFFFFC;
202 _netmasks[31] = 0xFFFFFFFE;
203 _netmasks[32] = 0xFFFFFFFF;
208 * Gets an integer definition. This function always return on
209 * success or exit on error.
211 int getDefine_Int(char *high_name, char *low_name, int min, int max)
218 /* We first try to read from the local define file. */
219 value = _read_file(high_name, low_name, OSSEC_LDEFINES);
222 value = _read_file(high_name, low_name, OSSEC_DEFINES);
224 ErrorExit(DEF_NOT_FOUND, __local_name, high_name, low_name);
230 if(!isdigit((int)*pt))
232 ErrorExit(INV_DEF, __local_name, high_name, low_name, value);
238 if((ret < min) || (ret > max))
240 ErrorExit(INV_DEF, __local_name, high_name, low_name, value);
243 /* Clearing memory */
250 /** int OS_IPFound(char *ip_address, os_ip *that_ip)
251 * Checks if ip_address is present at that_ip.
252 * Returns 1 on success or 0 on failure.
254 int OS_IPFound(char *ip_address, os_ip *that_ip)
259 /* Extracting ip address */
260 if((net.s_addr = inet_addr(ip_address)) <= 0)
265 /* If negate is set */
266 if(that_ip->ip[0] == '!')
271 /* Checking if ip is in thatip & netmask */
272 if((net.s_addr & that_ip->netmask) == that_ip->ip_address)
282 /** int OS_IPFoundList(char *ip_address, os_ip **list_of_ips)
283 * Checks if ip_address is present on the "list_of_ips".
284 * Returns 1 on success or 0 on failure.
285 * The list MUST be NULL terminated
287 int OS_IPFoundList(char *ip_address, os_ip **list_of_ips)
292 /* Extracting ip address */
293 if((net.s_addr = inet_addr(ip_address)) <= 0)
300 os_ip *l_ip = *list_of_ips;
302 if(l_ip->ip[0] == '!')
307 if((net.s_addr & l_ip->netmask) == l_ip->ip_address)
318 /** int OS_IsValidIP(char *ip)
319 * Validates if an ip address is in the right
321 * Returns 0 if doesn't match or 1 if it is an ip or 2 an ip with cidr.
322 * ** On success this function may modify the value of ip_address
324 int OS_IsValidIP(char *ip_address, os_ip *final_ip)
326 unsigned int nmask = 0;
335 /* Assigning the ip address */
338 os_strdup(ip_address, final_ip->ip);
341 if(*ip_address == '!')
347 /* checking against the basic regex */
348 if(!OS_PRegex(ip_address, ip_address_regex))
350 if(strcmp(ip_address, "any") != 0)
357 if(strcmp(ip_address, "any") != 0)
362 while(*tmp_ip != '\0')
376 if(dots < 3 || dots > 6)
383 /* Getting the cidr/netmask if available */
384 tmp_str = strchr(ip_address,'/');
394 if(strlen(tmp_str) <= 2)
396 cidr = atoi(tmp_str);
397 if((cidr >= 0) && (cidr <= 32))
401 nmask = _netmasks[cidr];
402 nmask = htonl(nmask);
416 if(strcmp(tmp_str, "255.255.255.255") == 0)
418 nmask = htonl(_netmasks[32]);
422 if((nmask = inet_addr(ip_address)) <= 0)
429 if((net.s_addr = inet_addr(ip_address)) <= 0)
431 if(strcmp("0.0.0.0", ip_address) == 0)
443 final_ip->ip_address = net.s_addr & nmask;
444 final_ip->netmask = nmask;
453 /* No cidr available */
459 if(strcmp("any", ip_address) == 0)
464 else if((net.s_addr = inet_addr(ip_address)) <= 0)
471 final_ip->ip_address = net.s_addr;
476 final_ip->netmask = htonl(_netmasks[nmask]);
479 /* Ip without cidr */
488 /* Should never reach here */
493 /** int OS_IsonTime(char *time_str, char *ossec_time)
494 * Must be a valid string, called after OS_IsValidTime.
495 * Returns 1 on success or 0 on failure.
497 int OS_IsonTime(char *time_str, char *ossec_time)
501 if(*ossec_time == '!')
507 /* Comparing against min/max value */
508 if((strncmp(time_str, ossec_time, 5) >= 0)&&
509 (strncmp(time_str, ossec_time+5,5) <= 0))
518 /** char *OS_IsValidTime(char *time_str)
519 * Validates if a time is in an acceptable format
521 * Returns 0 if doesn't match or a valid string for
522 * ossec usage in success.
523 * ** On success this function may modify the value of date
524 * Acceptable formats:
525 * hh:mm - hh:mm (24 hour format)
526 * !hh:mm -hh:mm (24 hour format)
527 * hh - hh (24 hour format)
528 * hh:mm am - hh:mm pm (12 hour format)
529 * hh am - hh pm (12 hour format)
531 #define RM_WHITE(x)while(*x == ' ')x++;
532 char *__gethour(char *str, char *ossec_hour)
538 /* Invalid time format */
539 if(!isdigit((int)*str))
541 merror(INVALID_TIME, __local_name, str);
549 /* Getting a valid hour */
550 if(chour < 0 || chour >= 24)
552 merror(INVALID_TIME, __local_name, str);
557 /* Going after the hour */
558 while(isdigit((int)*str))
567 merror(INVALID_TIME, __local_name, str);
576 if((!isdigit((int)*str)||
577 !isdigit((int)*(str +1))) && isdigit((int)*(str +2)))
579 merror(INVALID_TIME, __local_name, str);
587 /* Removing spaces */
590 if((*str == 'a') || (*str == 'A'))
593 if((*str == 'm') || (*str == 'M'))
595 snprintf(ossec_hour, 6, "%02d:%02d", chour, cmin);
600 else if((*str == 'p') || (*str == 'P'))
603 if((*str == 'm') || (*str == 'M'))
607 /* New hour must be valid */
608 if(chour < 0 || chour >= 24)
610 merror(INVALID_TIME, __local_name, str);
614 snprintf(ossec_hour, 6, "%02d:%02d", chour, cmin);
622 snprintf(ossec_hour, 6, "%02d:%02d", chour, cmin);
627 merror(INVALID_TIME, __local_name, str);
632 char *OS_IsValidTime(char *time_str)
639 /* Must be not null */
644 /* Clearing memory */
645 memset(first_hour, '\0', 7);
646 memset(second_hour, '\0', 7);
649 /* Removing white spaces */
653 /* Checking for negative */
659 /* We may have white spaces after the '!' */
664 /* Getting first hour */
665 time_str = __gethour(time_str, first_hour);
669 /* Removing white spaces */
679 /* Removing white spaces */
682 /* Getting second hour */
683 time_str = __gethour(time_str, second_hour);
688 if(*time_str != '\0')
693 os_calloc(13, sizeof(char), ret);
695 /* Fixing dump hours */
696 if(strcmp(first_hour,second_hour) > 0)
698 snprintf(ret, 12, "!%s%s", second_hour, first_hour);
702 /* For the normal times */
703 snprintf(ret, 12, "%c%s%s", ng == 0?'.':'!', first_hour, second_hour);
709 /** int OS_IsAfterTime(char *time_str, char *ossec_time)
710 * Checks if the current time is the same or has passed the
713 int OS_IsAfterTime(char *time_str, char *ossec_time)
715 /* Unique times can't have a !. */
716 if(*ossec_time == '!')
722 /* Comparing against min/max value */
723 if(strncmp(time_str, ossec_time, 5) >= 0)
733 /** char *OS_IsValidUniqueTime(char *time_str)
734 * Creates a unique time, not a range. Must be used with OS_IsAfterTime.
736 char *OS_IsValidUniqueTime(char *time_str)
743 memset(mytime, '\0', 128 +1);
744 snprintf(mytime, 128, "%s-%s", time_str, time_str);
747 return(OS_IsValidTime(mytime));
752 /** int OS_IsonDay(int week_day, char *ossec_day)
753 * Checks if the specified week day is in the
756 int OS_IsonDay(int week_day, char *ossec_day)
761 if(ossec_day[7] == '!')
764 if(week_day < 0 || week_day > 7)
769 /* It is on the right day */
770 if(ossec_day[week_day] == 1)
778 /** char *OS_IsValidDay(char *day_str)
779 * Validates if an day is in an acceptable format
781 * Returns 0 if doesn't match or a valid string for
782 * ossec usage in success.
783 * ** On success this function may modify the value of date
784 * Acceptable formats:
785 * weekdays, weekends, monday, tuesday, thursday,..
789 #define RM_SEP(x)while((*x == ' ') || (*x == ','))x++;
790 #define IS_SEP(x) (*x == ' ' || *x == ',')
791 char *OS_IsValidDay(char *day_str)
795 char day_ret[9] = {0,0,0,0,0,0,0,0,0};
798 "sunday", "sun", "monday", "mon", "tuesday", "tue",
799 "wednesday", "wed", "thursday", "thu", "friday",
800 "fri", "saturday", "sat", "weekdays", "weekends", NULL
802 int days_int[] = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,8};
804 /* Must be a valid string */
811 /* checking for negatives */
818 while(*day_str != '\0')
823 if(strncasecmp(day_str, days[i], strlen(days[i])) == 0)
835 else if(days_int[i] == 8)
842 day_ret[days_int[i]] = 1;
851 merror(INVALID_DAY, __local_name, day_str);
855 day_str += strlen(days[i]);
862 else if(*day_str == '\0')
866 merror(INVALID_DAY, __local_name, day_str);
871 /* Assigning values */
872 os_calloc(9, sizeof(char), ret);
875 /* Setting nevative */
882 /* Checking if some is checked */
888 /* At least one day must be checked */
892 merror(INVALID_DAY, __local_name, day_str);