1
0

bug readconfig

This commit is contained in:
Daniel Tartavel 2020-05-17 22:56:05 +02:00
parent 7aa7bce3d9
commit 4e441a90d4

18
main.c
View File

@ -63,6 +63,7 @@ int explode( char * str, char * separator, size_t m, size_t n, char exploded[m][
int readconfig( struct config * cfg ) int readconfig( struct config * cfg )
{ {
FILE * fh = NULL; FILE * fh = NULL;
FILE * fh1 = NULL;
char path[2][30] = {"/etc/sshdetect.conf", ""}; char path[2][30] = {"/etc/sshdetect.conf", ""};
int x; int x;
int retval=0; int retval=0;
@ -77,18 +78,19 @@ int readconfig( struct config * cfg )
{ {
sprintf( path[1], "%s%s", homepath, "/.config/sshdetect.conf" ); sprintf( path[1], "%s%s", homepath, "/.config/sshdetect.conf" );
} }
sprintf( logfilepath, "%s%s", homepath, "/.locale/share/sshdetect.log"); sprintf( logfilepath, "%s%s", homepath, "/.local/share/sshdetect.log");
for(x=0;x<2;x++) for(x=0;x<2;x++)
{ {
if ((fh = fopen( path[x], "r")) == NULL) if ((fh = fopen( path[x], "r")) == NULL)
{ {
if (debug >= 1) perror(path[x]); if (debug >= 1) perror(path[x]);
if(x==1) retval = -1; retval = -1;
}else }else
{ {
strcpy( cfg->configfile, path[x]); strcpy( cfg->configfile, path[x]);
if (debug >= 1) printf("Found config file: %s\n", path[x]); if (debug >= 1) printf("Found config file: %s\n", path[x]);
x = 3; x = 3;
retval = 0;
} }
} }
if (fh != NULL) if (fh != NULL)
@ -98,20 +100,22 @@ int readconfig( struct config * cfg )
explode(str, "= \n", 2, 1024, exploded); explode(str, "= \n", 2, 1024, exploded);
if ( strcmp( exploded[0], "commande") == 0 ) if ( strcmp( exploded[0], "commande") == 0 )
{ {
if ( fopen(exploded[1],"r") != NULL) if ( (fh1 = fopen(exploded[1],"r")) != NULL)
{ {
strcpy( cfg->commande, exploded[1] ); strcpy( cfg->commande, exploded[1] );
if (debug >= 1) printf("Found command: %s\n", cfg->commande); if (debug >= 1) printf("Found command: %s\n", cfg->commande);
fclose(fh1);
}else }else
{ {
if (debug >= 1) perror(exploded[1]); if (debug >= 1) perror(exploded[1]);
} }
}else if( strcmp( exploded[0], "logfile") == 0) }else if( strcmp( exploded[0], "logfile") == 0)
{ {
if ( fopen(exploded[1], "a") != NULL ) if ( (fh1 = fopen(exploded[1], "a")) != NULL )
{ {
strcpy( cfg->logfile, exploded[1] ); strcpy( cfg->logfile, exploded[1] );
if (debug >= 1) printf("Found logfile: %s\n", cfg->logfile); if (debug >= 1) printf("Found logfile: %s\n", cfg->logfile);
fclose(fh1);
} }
}else if( strcmp( exploded[0], "hostname") == 0 ) }else if( strcmp( exploded[0], "hostname") == 0 )
{ {
@ -122,12 +126,14 @@ int readconfig( struct config * cfg )
} }
if ( cfg->logfile[0] == 0 ) if ( cfg->logfile[0] == 0 )
{ {
if ( fopen("/var/log/sshdetect.log", "a") != NULL ) if ( (fh1 = fopen("/var/log/sshdetect.log", "a")) != NULL )
{ {
strcpy( cfg->logfile, "/var/log/sshdetect.log" ); strcpy( cfg->logfile, "/var/log/sshdetect.log" );
}else if ( fopen(logfilepath, "a") != NULL ) fclose(fh1);
}else if ( (fh1 = fopen(logfilepath, "a")) != NULL )
{ {
strcpy( cfg->logfile, logfilepath ); strcpy( cfg->logfile, logfilepath );
fclose(fh1);
}else }else
{ {
if (debug >= 1) perror(logfilepath); if (debug >= 1) perror(logfilepath);