/* Read key control file and return the number of specs stored or a negative * error code. * * As the specs are read in the from and until time values are compared to * present time. Only those keys which are valid are stored. */ int n2n_read_keyfile(n2n_cipherspec_t *specs, /* fill out this array of cipherspecs */ size_t numspecs, /* number of slots in the array. */ const char *ctrlfile_path) /* path to control file */ { /* Each line contains one cipherspec. */ int retval = 0; FILE *fp = NULL; size_t idx = 0; time_t now = time(NULL); traceDebug("Reading '%s'\n", ctrlfile_path); fp = fopen(ctrlfile_path, "r"); if (fp) { /* Read the file a line a time with fgets. */ char line[N2N_KEYFILE_LINESIZE]; size_t lineNum = 0; while (idx < numspecs) { n2n_cipherspec_t *k = &(specs[idx]); fgets(line, N2N_KEYFILE_LINESIZE, fp); ++lineNum; if (strlen(line) > 1) { if (0 == parseKeyLine(k, line)) { if (k->valid_until > now) { traceInfo(" --> [%u] from %lu, until %lu, transform=%hu, data=%s\n", idx, k->valid_from, k->valid_until, k->t, k->opaque); ++retval; ++idx; } else { traceInfo(" --X [%u] from %lu, until %lu, transform=%hu, data=%s\n", idx, k->valid_from, k->valid_until, k->t, k->opaque); } } else { traceWarning("Failed to decode line %u\n", lineNum); } } if (feof(fp)) { break; } line[0] = 0; /* this line has been consumed */ } fclose(fp); fp = NULL; } else { traceError("Failed to open '%s'\n", ctrlfile_path); retval = -1; } return retval; }
/* Read key control file and return the number of specs stored or a negative * error code. * * As the specs are read in the from and until time values are compared to * present time. Only those keys which are valid are stored. */ int n2n_read_keyfile( n2n_cipherspec_t * specs, /* fill out this array of cipherspecs */ size_t numspecs, /* number of slots in the array. */ const char * ctrlfile_path ) /* path to control file */ { /* Each line contains one cipherspec. */ int retval=0; FILE * fp=NULL; size_t idx=0; time_t now = time(NULL); traceEvent( TRACE_DEBUG, "Reading '%s'\n", ctrlfile_path ); char* line_buffer[N2N_KEYFILE_LINESIZE]; int line_counter = 0; fp = fopen( ctrlfile_path, "r" ); if ( fp ) { /* Read the file a line a time with fgets. */ char line[N2N_KEYFILE_LINESIZE]; size_t lineNum=0; while ( idx < numspecs ) { n2n_cipherspec_t * k = &(specs[idx]); fgets( line, N2N_KEYFILE_LINESIZE, fp ); ++lineNum; if ( strlen(line) > 1 ) { if ( 0 == parseKeyLine( k, line ) ) { if ( k->valid_until > now ) { traceEvent( TRACE_INFO, " --> [%u] from %lu, until %lu, transform=%hu, data=%s\n", idx, k->valid_from, k->valid_until, k->t, k->opaque ); ++retval; ++idx; line_buffer[line_counter] = line; line_counter++; } else { traceEvent( TRACE_INFO, " --X [%u] from %lu, until %lu, transform=%hu, data=%s\n", idx, k->valid_from, k->valid_until, k->t, k->opaque ); } } else { traceEvent( TRACE_WARNING, "Failed to decode line %u\n", lineNum ); } } if ( feof(fp) ) { break; } line[0]=0; /* this line has been consumed */ } fclose( fp); fp=NULL; int i; for (i = 0; i < line_counter; i++) { traceEvent( TRACE_INFO, "LINE IN BUFFER: %s\n", *line_buffer[i] ); } } else { traceEvent( TRACE_ERROR, "Failed to open '%s'\n", ctrlfile_path ); retval = -1; } return retval; }