Пример #1
0
END_TEST

START_TEST(test_fail_match1)
{

    int i;
    const char *tests[][3] = {
        {"abc", "abb", ""},
        {"^ab", " ab", ""},
        {"test", "tes", ""},
        {"abcd", "abc", ""},
        {"abbb", "abb", ""},
        {"abbbbbbbb", "abbbbbbb", ""},
        {"a|b|c| ", "def", ""},
        {"lala$", "lalalalalal", ""},
        {"^ab$", "abc", ""},
        {"zzzz$", "zzzzzzzzzzzz ", ""},
        {"^bin$|^shell$", "bina", ""},
        {"^bin$|^shell$", "shella", ""},
        {"^bin$|^shell$", "ashell", ""},
        {NULL, NULL, NULL}
    };

    for(i=0; tests[i][0] != NULL ; i++) {
        ck_assert_msg(!OS_Match2(tests[i][0],tests[i][1]),
                      "%s should have OS_Match2 false with %s: Ref: %s",
                      tests[i][0], tests[i][1], tests[i][2]);
    }
}
Пример #2
0
int rk_check_dir(const char *dir, const char *file, char *pattern)
{
    int ret_code = 0;
    char f_name[PATH_MAX + 2];
    struct dirent *entry;
    struct stat statbuf_local;
    DIR *dp = NULL;

    f_name[PATH_MAX + 1] = '\0';

    dp = opendir(dir);
    if (!dp) {
        return (0);
    }

    while ((entry = readdir(dp)) != NULL) {
        /* Ignore . and ..  */
        if ((strcmp(entry->d_name, ".") == 0) ||
                (strcmp(entry->d_name, "..") == 0)) {
            continue;
        }

        /* Create new file + path string */
        snprintf(f_name, PATH_MAX + 1, "%s/%s", dir, entry->d_name);

        /* Check if the read entry matches the provided file name */
        if (strncasecmp(file, "r:", 2) == 0) {
            if (OS_Regex(file + 2, entry->d_name)) {
                if (rk_check_file(f_name, pattern)) {
                    ret_code = 1;
                }
            }
        } else {
            /* ... otherwise try without regex */
            if (OS_Match2(file, entry->d_name)) {
                if (rk_check_file(f_name, pattern)) {
                    ret_code = 1;
                }
            }
        }

        /* Check if file is a directory */
        if (lstat(f_name, &statbuf_local) == 0) {
            if (S_ISDIR(statbuf_local.st_mode)) {
                if (rk_check_dir(f_name, file, pattern)) {
                    ret_code = 1;
                }
            }
        }
    }

    closedir(dp);
    return (ret_code);

}
Пример #3
0
int main(int argc,char **argv)
{

    if(argc != 3)
    {
        printf("%s regex word\n",argv[0]);
        exit(1);
    }

    printf("for MATCH: ");	
    if(OS_Match2(argv[1],argv[2]))
        printf("TRUE\n");
    else
        printf("FALSE\n");

    return(0);
}
Пример #4
0
/* ReadConfig(int modules, char *cfgfile)
 * Read the config files
 */
int ReadConfig(int modules, char *cfgfile, void *d1, void *d2)
{
    int i;
    OS_XML xml;
    XML_NODE node;


    /** XML definitions **/
    /* Global */
    char *xml_start_ossec = "ossec_config";
    char *xml_start_ospatrol = "ospatrol_config";
    char *xml_start_agent = "agent_config";

    /* Attributes of the <agent_config> tag */
    char *xml_agent_name = "name";
    char *xml_agent_os = "os";
    char *xml_agent_overwrite = "overwrite";
    /* cmoraes */
    char *xml_agent_profile = "profile";


    if(OS_ReadXML(cfgfile,&xml) < 0)
    {
        if(modules & CAGENT_CONFIG)
        {
            #ifndef CLIENT
            merror(XML_ERROR, ARGV0, cfgfile, xml.err, xml.err_line);
            #endif
        }
        else
        {
            merror(XML_ERROR, ARGV0, cfgfile, xml.err, xml.err_line);
        }
        return(OS_INVALID);
    }


    node = OS_GetElementsbyNode(&xml, NULL);
    if(!node)
    {
        return(0);
    }


    /* Reading the main configuration */
    i = 0;
    while(node[i])
    {
        if(!node[i]->element)
        {
            merror(XML_ELEMNULL, ARGV0);
            return(OS_INVALID);
        }
        else if(!(modules & CAGENT_CONFIG) && 
                 ( 
                   (strcmp(node[i]->element, xml_start_ossec) == 0) || 
                   (strcmp(node[i]->element, xml_start_ospatrol) == 0) 
                 ) 
               )
        {
            XML_NODE chld_node = NULL;
            chld_node = OS_GetElementsbyNode(&xml,node[i]);

            /* Main element does not need to have any child */
            if(chld_node)
            {
                if(read_main_elements(xml, modules, chld_node, d1, d2) < 0)
                {
                    merror(CONFIG_ERROR, ARGV0, cfgfile);
                    return(OS_INVALID);
                }

                OS_ClearNode(chld_node);
            }
        }
        else if((modules & CAGENT_CONFIG) &&
                (strcmp(node[i]->element, xml_start_agent) == 0))
        {
            int passed_agent_test = 1;
            int attrs = 0;
            XML_NODE chld_node = NULL;
            chld_node = OS_GetElementsbyNode(&xml,node[i]);


            /* Checking if this is specific to any agent. */
            if(node[i]->attributes && node[i]->values)
            {
                while(node[i]->attributes[attrs] && node[i]->values[attrs])
                {
                    /* Checking if there is an "name=" attribute */
                    if(strcmp(xml_agent_name, node[i]->attributes[attrs]) == 0)
                    {
                        #ifdef CLIENT
                        char *agentname = os_read_agent_name();

                        if(!agentname)
                        {
                            passed_agent_test = 0;
                        }
                        else
                        {
                            if(!OS_Match2(node[i]->values[attrs], agentname))
                            {
                                passed_agent_test = 0;
                            }
                            free(agentname);
                        }
                        #endif
                    }
                    else if(strcmp(xml_agent_os, node[i]->attributes[attrs]) == 0)
                    {
                        #ifdef CLIENT
                        char *agentos = getuname();

                        if(agentos)
                        {
                            if(!OS_Match2(node[i]->values[attrs], agentos))
                            {
                                passed_agent_test = 0;
                            }
                            free(agentos);
                        }
                        else
                        {
                            passed_agent_test = 0;
                            merror("%s: ERROR: Unable to retrieve uname.", ARGV0);
                        }
                        #endif
                    }
                    else if(strcmp(xml_agent_profile, node[i]->attributes[attrs]) == 0)
                    {
                        #ifdef CLIENT
                        char *agentprofile = os_read_agent_profile();
                        debug2("Read agent config profile name [%s]", agentprofile);

                        if(!agentprofile)
                        {
                            passed_agent_test = 0;
                        }
                        else
                        {
                            /* match the profile name of this <agent_config> section
                             * with a comma separated list of values in agent's
                             * <config-profile> tag.
                             */
                            if(!OS_Match2(node[i]->values[attrs], agentprofile))
                            {
                                passed_agent_test = 0;
                                debug2("[%s] did not match agent config profile name [%s]",
                                       node[i]->values[attrs], agentprofile);
                            }
                            else
                            {
                                debug2("Matched agent config profile name [%s]", agentprofile);
                            }
                            free(agentprofile);
                        }
                        #endif
                    }
                    /* cmoraes: end add */
                    else if(strcmp(xml_agent_overwrite, node[i]->attributes[attrs]) == 0)
                    {
                    }
                    else
                    {
                        merror(XML_INVATTR, ARGV0, node[i]->attributes[attrs],
                                cfgfile);
                    }
                    attrs++;
                }
            }
            #ifdef CLIENT
            else
            {
                debug2("agent_config element does not have any attributes.");

                /* if node does not have any attributes, it is a generic config block.
                 * check if agent has a profile name
                 * if agent does not have profile name, then only read this generic
                 * agent_config block
                 */

                if (!os_read_agent_profile())
                {
                    debug2("but agent has a profile name.");
                    passed_agent_test = 0;
                }
            }
            #endif

            /* Main element does not need to have any child */
            if(chld_node)
            {
                if(passed_agent_test && read_main_elements(xml, modules, chld_node, d1, d2) < 0)
                {
                    merror(CONFIG_ERROR, ARGV0, cfgfile);
                    return(OS_INVALID);
                }

                OS_ClearNode(chld_node);
            }
        }
        else
        {
            merror(XML_INVELEM, ARGV0, node[i]->element);
            return(OS_INVALID);
        }
        i++;
    }

    /* Clearing node and xml */
    OS_ClearNode(node);
    OS_ClearXML(&xml);	
    return(0);
}
Пример #5
0
/** main **/
int main(int argc, char **argv)
{
    char *pattern;
    
    char msg[OS_MAXSTR +1];
    memset(msg, '\0', OS_MAXSTR +1);
    OSRegex regex; 
    OSMatch matcher; 

    OS_SetName(ARGV0);
        
    
    /* user arguments */
    if(argc != 2)
    {
        helpmsg();
        return(-1);
    }
    
    /* User options */
    if(strcmp(argv[1], "-h") == 0)
    {
        helpmsg();
        return(-1);
    }

    os_strdup(argv[1], pattern);
    if(!OSRegex_Compile(pattern, &regex, 0)) 
    { 
        printf("pattern does not compile with OSRegex_Compile\n");
        return(-1); 
    }
    if(!OSMatch_Compile(pattern, &matcher, 0))
    {
        printf("pattern does not compile with OSMatch_Compile\n");
        return(-1);
    }


    while((fgets(msg, OS_MAXSTR, stdin)) != NULL)
    {        
        /* Removing new line. */                                                                                                                                       
        if(msg[strlen(msg) -1] == '\n')
            msg[strlen(msg) -1] = '\0';

        /* Make sure we ignore blank lines. */                                                                                                                         
        if(strlen(msg) < 2) { continue; }                                                            

        if(OSRegex_Execute(msg, &regex))
            printf("+OSRegex_Execute: %s\n",msg); 
        /*
        else
            printf("-OSRegex_Execute: \n"); 
            */

        if(OS_Regex(pattern, msg)) 
            printf("+OS_Regex       : %s\n", msg);
        /*
        else
            printf("-OS_Regex: \n"); 
            */

        if(OSMatch_Execute(msg, strlen(msg), &matcher)) 
            printf("+OSMatch_Compile: %s\n", msg); 
        
        if(OS_Match2(pattern, msg)) 
            printf("+OS_Match2      : %s\n", msg); 
    }
    return(0);
}