Apache Includes Parser

Wrote this script to read and parse all included files in httpd.conf or any file specified when calling the PHP script. Will output content from httpd.conf(or any file specified) and all included files found and output to stdout. I used this to pipe the output to grep when searching for various Apache directives. Useful to find directives that are in a included file that are overwriting your directives set in httpd.conf.


<?php
error_reporting
(0);
if (
$argc != 2) {
echo <<<FILE
#################################################

Reads all “Included files” in specified file and outputs to stdout.

Usage: php $argv[0] file
Example: php $argv
[0] httpd.conf
#####################################

FILE;
}
else
{
$file = file($argv[1]);
$content = “”;
try {
foreach (
$file as $line) {
$line = str_replace(“\n”,“”,$line);
if (
stristr($line,‘Include’) && !stristr($line,‘#’)) {
if (
preg_match(‘/^.*\*\.conf$/’,$line)) {
$str = str_replace(“Include “,“”,$line);
$str = str_replace(“*.conf”,“”,$str);
$dirFiles = @scandir($str);
foreach (
$dirFiles as $dirFile) {
if (
stristr($dirFile,“.conf”)) {
$content .= file_get_contents($str.$dirFile);
}
}
}
elseif (
preg_match(‘/^.*[a-zA-z0-9]\.conf$/’,$line)) {
$content .= file_get_contents(substr($line, 8));
}
else
{
$str = str_replace(“Include “,“”,$line);
$dirFiles = @scandir($str);
foreach (
$dirFiles as $dirFile) {
if (
stristr($dirFile,“.conf”)) {
$content .= file_get_contents($str.$dirFile);
}
}
}
}
print_r($content);
}
} catch (
Exception $e) {
echo
$e->getMessage();
}
}

Parse Apache Config for DocumentRoot in C

So I am now on my second week of learning the C language. I have been writing a lot of small programs just to get the hang of the code. I have written simple calculators, guess my number games, and simple quizzes. Tonight I was working on code that could parse a apache config and only display the DocumentRoot. The code I wrote to do this is shown below.


#include <stdlib.h>
#include <string.h>
#define MAXLINE 128

FILE *file;

int main(int argc, char *argv[])
{

    int i,x;
    char line[MAXLINE];
    char * temp;

    if (argc == 1)
    {
        puts("You must enter a file/files to search.");
        exit(1);
    }
    for (i=1; i<argc;i++)
    {
        file = fopen(argv[i], "r");
        if (file == NULL)
        {
            puts("Could'nt open file");
            exit(1);
        }
        while ( (fgets(line, MAXLINE, file)) != NULL)
        {
            temp = malloc(sizeof (char) * 128);
            for (i=0; i<MAXLINE; i++)
            {
                do 
                {
                    temp[i] = line[i];
                    i++;
                } while (line[i] != '\n');
            temp[i] = '\0';
            break;
            }
            if (strstr(temp, "DocumentRoot") != NULL) {
                printf("%s\n", temp);
            }
            free(temp);
        }
    }
}