diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2014-12-13 10:45:55 -0500 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2014-12-13 10:46:33 -0500 |
commit | a12ffbaf5b84aeec0b78d55cc17ee5a392c138c7 (patch) | |
tree | d0c04ee54939f59e55c765510de243a10352c388 /src | |
parent | 4063f74b8d6b54b501eabeeadf5cb03030f37539 (diff) | |
download | biruda-a12ffbaf5b84aeec0b78d55cc17ee5a392c138c7.tar.gz biruda-a12ffbaf5b84aeec0b78d55cc17ee5a392c138c7.tar.bz2 |
added cpe detection for Centos 6
Diffstat (limited to 'src')
-rw-r--r-- | src/system.c | 150 |
1 files changed, 99 insertions, 51 deletions
diff --git a/src/system.c b/src/system.c index 57f39a0..fede36c 100644 --- a/src/system.c +++ b/src/system.c @@ -219,80 +219,128 @@ void system_os( char *name, size_t len ) break; } #elif defined( linux ) || defined( __linux ) || defined( __linux__ ) + bool has_os_release = false; + bool has_redhat_release = false; + /* new systemd-way on modern Linux systems */ FILE *f = fopen( "/etc/os-release", "r" ); if( f == NULL ) { f = fopen( "/usr/lib/os-release", "r" ); if( f == NULL ) { + f = fopen( "/etc/redhat-release", "r" ); + if( f == NULL ) { + snprintf( name, len, "cpe:/o:linux" ); + return; + } else { + has_redhat_release = true; + } /* TODO: test for /etc/xxx-release, /etc/xxx-version, not sure if we should also check and run lsb_release */ - snprintf( name, len, "cpe:/o:linux" ); - return; + } else { + has_os_release = true; } + } else { + has_os_release = true; } - char line[100]; - char *key; - char *id = NULL; - char *version_id = NULL; - char *cpe_name = NULL; - while( !feof( f ) ) { - fgets( line, sizeof( line ), f ); - if( line[0] == '#' ) continue; - char *value = strchr( line, '=' ); - if( value == NULL ) continue; - key = line; - *value++ = '\0'; - char *p = value; - char *q = p; - while( *p != '\0' ) { - if( *p == '\\' ) { - ++p; - if( *p == '\0' ) { - break; + if( has_os_release ) { + char line[100]; + char *key; + char *id = NULL; + char *version_id = NULL; + char *cpe_name = NULL; + while( !feof( f ) ) { + fgets( line, sizeof( line ), f ); + if( line[0] == '#' ) continue; + char *value = strchr( line, '=' ); + if( value == NULL ) continue; + key = line; + *value++ = '\0'; + char *p = value; + char *q = p; + while( *p != '\0' ) { + if( *p == '\\' ) { + ++p; + if( *p == '\0' ) { + break; + } + *q++ = *p++; + } + if( *p == '\"' || *p == '\'' || *p == '\n' ) { + p++; + } else { + *q++ = *p++; } - *q++ = *p++; - } - if( *p == '\"' || *p == '\'' || *p == '\n' ) { - p++; - } else { - *q++ = *p++; } - } - *q = '\0'; + *q = '\0'; - if( strcmp( key, "CPE_NAME" ) == 0 ) { - cpe_name = strdup( value ); - } else if( strcmp( key, "ID" ) == 0 ) { - id = strdup( value ); - } else if( strcmp( key, "VERSION_ID" ) == 0 ) { - version_id = strdup( value ); + if( strcmp( key, "CPE_NAME" ) == 0 ) { + cpe_name = strdup( value ); + } else if( strcmp( key, "ID" ) == 0 ) { + id = strdup( value ); + } else if( strcmp( key, "VERSION_ID" ) == 0 ) { + version_id = strdup( value ); + } } - } - fclose( f ) ; + /* do we have a CPE_NAME? In this case CPE_NAME takes + precedence */ + if( cpe_name != NULL ) { + snprintf( name, len, "%s", cpe_name ); + } else { + /* construct CPE_NAME from ID and VERSION_ID and any other + information we find */ - /* do we have a CPE_NAME? In this case CPE_NAME takes - precedence */ - if( cpe_name != NULL ) { - snprintf( name, len, "%s", cpe_name ); - } else { - /* construct CPE_NAME from ID and VERSION_ID and any other - information we find */ + /* no version information, so we have a rolling release */ + if( version_id == NULL ) { + version_id = strdup( "rolling" ); + } - /* no version information, so we have a rolling release */ - if( version_id == NULL ) { - version_id = strdup( "rolling" ); + snprintf( name, len, "cpe:/o:%s:%s:%s", id, id, version_id ); } - snprintf( name, len, "cpe:/o:%s:%s:%s", id, id, version_id ); + if( id != NULL ) free( id ); + if( version_id != NULL ) free( version_id ); + if( cpe_name != NULL ) free( cpe_name ); + } + + if( has_redhat_release ) { + //CentOS release 6.5 (Final) + char line[100]; + fgets( line, sizeof( line ), f ); + + if( strstr( line, "CentOS" ) != NULL ) { + char *p = strtok( line, " " ); + bool is_version = false; + char *version = "unknown"; + while( p != NULL ) { + if( strcmp( p, "release" ) == 0 ) { + is_version = true; + p = strtok( NULL, " " ); + continue; + } + if( is_version ) { + version = p; + is_version = false; + } + p = strtok( NULL, " " ); + } + + char *major = strtok( version, "." ); + if( p != NULL ) { + snprintf( name, len, "cpe:/o:centos:centos:%s", major ); + } else { + snprintf( name, len, "cpe:/o:centos:centos:%s", version ); + } + } else { + snprintf( name, len, "cpe:/o:redhat:unknown" ); + } } - if( id != NULL ) free( id ); - if( version_id != NULL ) free( version_id ); - if( cpe_name != NULL ) free( cpe_name ); + fclose( f ) ; + #elif defined( __FreeBSD__ ) /* resort to uname */ |