Skip to content

Commit

Permalink
Revert "Merge pull request #1 from edmurphy16/filefake"
Browse files Browse the repository at this point in the history
need to put this in a seperate library, I think.

This reverts commit 03fd387, reversing
changes made to bc6ee8f.
  • Loading branch information
zcutlip committed Jan 1, 2015
1 parent fb41088 commit 18bdc5e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 221 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $(INI_OBJ):
$(CC) $(INCLUDES) $(CFLAGS) -fPIC -c -o $@ $<

$(LIB): $(OBJS) $(INI_OBJ)
$(CC) -shared -o $@ $^ -Wl,-nostdlib -ldl
$(CC) -shared -o $@ $^ -Wl,-nostdlib

clean:
-rm *.o
Expand Down
32 changes: 4 additions & 28 deletions contrib/inih/ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,20 @@ home page for more info:
#define MAX_SECTION 50
#define MAX_NAME 50

/* writing my own implementation of isspace */
int myisspace(int c){
if(c == ' ')
return 1;
else if(c == '\t')
return 1;
else if(c == '\v')
return 1;
else if(c == '\r')
return 1;
else if(c == '\n')
return 1;
else
return 0;
}

/* Strip whitespace chars off end of given string, in place. Return s. */
static char* rstrip(char* s)
{
int i;
char* p;

p=s+strlen(s);
while (p > s && myisspace((int)(*(--p)))){
char* p = s + strlen(s);
while (p > s && isspace((unsigned char)(*--p)))
*p = '\0';
}

return s;
}

/* Return pointer to first non-whitespace char in given string. */
static char* lskip(const char* s)
{
while (*s && myisspace((unsigned char)(*s))){
while (*s && isspace((unsigned char)(*s)))
s++;
}
return (char*)s;
}

Expand All @@ -66,7 +44,7 @@ static char* find_char_or_comment(const char* s, char c)
{
int was_whitespace = 0;
while (*s && *s != c && !(was_whitespace && *s == ';')) {
was_whitespace = myisspace((unsigned char)(*s));
was_whitespace = isspace((unsigned char)(*s));
s++;
}
return (char*)s;
Expand Down Expand Up @@ -114,15 +92,13 @@ int ini_parse_file(FILE* file,
lineno++;

start = line;

#if INI_ALLOW_BOM
if (lineno == 1 && (unsigned char)start[0] == 0xEF &&
(unsigned char)start[1] == 0xBB &&
(unsigned char)start[2] == 0xBF) {
start += 3;
}
#endif

start = lskip(rstrip(start));

if (*start == ';' || *start == '#') {
Expand Down
198 changes: 24 additions & 174 deletions nvram-faker.c
Original file line number Diff line number Diff line change
@@ -1,171 +1,92 @@
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <dlfcn.h>
#include "nvram-faker.h"
#include "ini.h"

#define RED_ON "\033[22;31m"
#define RED_OFF "\033[22;00m"
#define DEFAULT_BUFFER_LEN 1024
#define DEFAULT_KV_PAIR_LEN 1024

#ifndef INI_FILE_PATH
#define INI_FILE_PATH "/nvram.ini"
#endif

#ifndef LOG_FILE_PATH
#define LOG_FILE_PATH "/tmp/faker.log"
#endif

static int kv_count=0;
static int fn_count=0;
static int key_value_pair_len=DEFAULT_BUFFER_LEN;
static int fake_filenames_len=DEFAULT_BUFFER_LEN;
ini_info_t *ii=NULL;
static int key_value_pair_len=DEFAULT_KV_PAIR_LEN;
static char **key_value_pairs=NULL;
static char **fake_filenames=NULL;
FILE* flog;

void initialize_ini(void) __attribute__((constructor));
void end(void) __attribute__((destructor));

int fname_check(const char *filename);

static int ini_handler(void *user, const char *section, const char *name,const char *value)
{

int old_kv_len;
ini_info_t *new_ii;
char **kv;
char **fn;
char **new_kv;
int i;

if(NULL == user || NULL == section || NULL == name || NULL == value)
{
fprintf(flog,"bad parameter to ini_handler\n");
printf("bad parameter to ini_handler\n");
return 0;
}

new_ii = *((ini_info_t **) user);

if(NULL == new_ii)
kv = *((char ***)user);
if(NULL == kv)
{
fprintf(flog,"ini_info struct is NULL\n");
printf("kv is NULL\n");
return 0;
}

kv = new_ii->kv_pairs;
fn = new_ii->fake_fns;


if(kv_count >= key_value_pair_len)
{
old_kv_len=key_value_pair_len;
key_value_pair_len=(key_value_pair_len * 2);
new_kv=(char **)malloc(key_value_pair_len);
if(NULL == new_kv)
if(NULL == kv)
{
fprintf(flog,"Failed to reallocate key value array.\n");
printf("Failed to reallocate key value array.\n");
return 0;
}
for(i=0;i<old_kv_len;i++)
{
new_kv[i]=kv[i];
}

free(new_ii->kv_pairs);
free(*(char ***)user);
kv=new_kv;
new_ii->kv_pairs=kv;
}

if(fn_count >= fake_filenames_len)
{
fprintf(flog,"Sorry you are limited to 1024 filenames to fake\n");
fprintf(flog,"Seriously it will probably make things painfully slow\n");
fprintf(flog,"if you had more.\n");
return 0;
}

fprintf(flog,"Got %s:%s\n",name,value);
if(strstr(name,"fake_filename")){
fn[fn_count++]=strdup(value);
}else{
kv[kv_count++]=strdup(name);
kv[kv_count++]=strdup(value);
*(char ***)user=kv;
}
printf("Got %s:%s\n",name,value);
kv[kv_count++]=strdup(name);
kv[kv_count++]=strdup(value);

return 1;
}

void initialize_ini(void)
{
int ret;
char *flog_path=NULL;
flog_path = getenv("FAKER_LOG");


if(NULL == flog_path || '\0' == flog_path[0]){
//if no log provided log to stderr
flog = stderr;
fprintf(flog,RED_ON"**************************************************\n");
fprintf(flog,"**\t\tTO LOG TO A FILE\t\t**\n");
fprintf(flog,"**\t SET \"FAKER_LOG\" ENV VARIABLE\t\t**\n");
fprintf(flog,"**************************************************\n"RED_OFF);
}else{
flog = fopen(flog_path,"w+");
if( NULL == flog){
fprintf(stderr,"Failed to open log file \"%s\"\n",flog_path);
exit(1);
}
}

setvbuf(flog,NULL,_IONBF,0);
fprintf(flog,"Initializing.\n");
if(NULL == ii){
ii = malloc(sizeof(ini_info_t));
if(NULL == ii){
fprintf(flog,"Failed to allocate memory for update info struct. Terminating.\n");
exit(1);
}

}

printf("Initializing.\n");
if (NULL == key_value_pairs)
{
key_value_pairs=malloc(key_value_pair_len);
}
if(NULL == key_value_pairs)
{
fprintf(flog,"Failed to allocate memory for key value array. Terminating.\n");
printf("Failed to allocate memory for key value array. Terminating.\n");
exit(1);
}

if(NULL == fake_filenames)
{
fake_filenames = malloc(fake_filenames_len);
}
if(NULL == fake_filenames)
{
fprintf(flog,"Failed to allocate memory for fake filenames array. Terminating.\n");
exit(1);
}

ii->kv_pairs = key_value_pairs;
ii->fake_fns = fake_filenames;

ret = ini_parse(INI_FILE_PATH,ini_handler,(void *)&ii);
fprintf(flog,"ret from ini_parse was: %d\n",ret);

ret = ini_parse(INI_FILE_PATH,ini_handler,(void *)&key_value_pairs);
printf("ret from ini_parse was: %d\n",ret);
if (0 != ret)
{
fprintf(flog,"INI parse failed. Terminating\n");
printf("INI parse failed. Terminating\n");
free(key_value_pairs);
key_value_pairs=NULL;
exit(1);
}



return;

Expand All @@ -176,23 +97,10 @@ void end(void)
int i;
for (i=0;i<kv_count;i++)
{
if(NULL != key_value_pairs[i])
free(key_value_pairs[i]);
}

for (i=0;i<fn_count;i++)
{
if(NULL != fake_filenames[i])
free(fake_filenames[i]);
free(key_value_pairs[i]);
}
free(key_value_pairs);
free(fake_filenames);
key_value_pairs=NULL;
fake_filenames=NULL;
free(ii);
ii=NULL;

fclose(flog);

return;
}
Expand All @@ -207,7 +115,7 @@ char *nvram_get(const char *key)
{
if(strcmp(key,key_value_pairs[i]) == 0)
{
fprintf(flog,"%s=%s\n",key,key_value_pairs[i+1]);
printf("%s=%s\n",key,key_value_pairs[i+1]);
found = 1;
value=key_value_pairs[i+1];
break;
Expand All @@ -217,10 +125,7 @@ char *nvram_get(const char *key)
ret = NULL;
if(!found)
{
if(flog == stderr)
fprintf(flog, RED_ON"%s=Unknown\n"RED_OFF,key);
else
fprintf(flog, "%s=Unknown (FAILED LOOKUP)\n",key);
printf( RED_ON"%s=Unknown\n"RED_OFF,key);
}else
{

Expand All @@ -229,59 +134,4 @@ char *nvram_get(const char *key)
return ret;
}

FILE* fopen(const char *filename, const char *mode){
static FILE* (*my_fopen) (const char *filename, const char *mode) = NULL;
char *base;
char fakename[64];

if(!my_fopen)
my_fopen = dlsym(RTLD_NEXT,"fopen");
FILE* p = my_fopen(filename,mode);
if(p==0){
if( fname_check(filename) ){
base = basename(filename);
if(strlen(base) > 54)
base[53]='\0';
snprintf(fakename,64,"/tmp/%s.fake",base);

p = my_fopen(fakename,"a+");
}
}
return p;
}

int fname_check(const char *filename){
int i=0;

for(i=0; i<fn_count; i++){
if(strstr(filename,fake_filenames[i])){
fprintf(flog,"FILE %s not found providing false positive\n",filename);
return 1;
}
}

return 0;
}

int open(const char *fn, int flags){
static int (*my_open) (const char *fn, int flags) = NULL;
char *base;
char fakename[64];


if(!my_open)
my_open = dlsym(RTLD_NEXT, "open");

int p = my_open(fn,flags);
if(p==-1){
if( fname_check(fn) ){
base = basename(fn);
if(strlen(base)>54)
base[53]='\0';
snprintf(fakename,64,"/tmp/%s.fake",base);
p = my_open(fakename,O_RDWR|O_CREAT|O_APPEND);
}
}

return p;
}
Loading

0 comments on commit 18bdc5e

Please sign in to comment.