1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
|
/*
* conf.c: functions that handle the nwipe.conf configuration file
* and the creation of the nwipe_customers.csv file. nwipe.conf uses
* libconfig format, while nwipe_customers.csv uses comma separted
* values. CSV is used so that the user can build there own customer
* listing using spreadsheets rather than enter all the customer
* information via the nwipe GUI interface.
*
* Copyright PartialVolume <https://github.com/PartialVolume>.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*
*
*/
#include <libconfig.h>
#include <unistd.h>
#include <sys/types.h>
#include "nwipe.h"
#include "context.h"
#include "logging.h"
#include "method.h"
#include "options.h"
#include "conf.h"
config_t nwipe_cfg;
config_setting_t *nwipe_conf_setting, *group_organisation, *root, *group, *previous_group, *setting;
const char* nwipe_conf_str;
char nwipe_config_directory[] = "/etc/nwipe";
char nwipe_config_file[] = "/etc/nwipe/nwipe.conf";
char nwipe_customers_file[] = "/etc/nwipe/nwipe_customers.csv";
char nwipe_customers_file_backup[] = "/etc/nwipe/nwipe_customers.csv.backup";
char nwipe_customers_file_backup_tmp[] = "/etc/nwipe/nwipe_customers.csv.backup.tmp";
/*
* Checks for the existence of nwipe.conf and nwipe_customers.csv
* If either one does not exist, they are created and formatted
* with a basic configuration.
*/
int nwipe_conf_init()
{
FILE* fp_config;
FILE* fp_customers;
config_init( &nwipe_cfg );
char nwipe_customers_initial_content[] =
"\"Customer Name\";\"Contact Name\";\"Customer Address\";\"Contact Phone\"\n"
"\"Not Applicable\";\"Not Applicable\";\"Not Applicable\";\"Not Applicable\"\n";
/* Read /etc/nwipe/nwipe.conf. If there is an error, determine whether
* it's because it doesn't exist. If it doesn't exist create it and
* populate it with a basic structure.
*/
/* Does the /etc/nwipe/nwipe.conf file exist? If not, then create it */
if( access( nwipe_config_file, F_OK ) == 0 )
{
nwipe_log( NWIPE_LOG_INFO, "Nwipes config file %s exists", nwipe_config_file );
/* Read the nwipe.conf configuration file and report any errors */
nwipe_log( NWIPE_LOG_INFO, "Reading nwipe's config file %s", nwipe_config_file );
if( !config_read_file( &nwipe_cfg, nwipe_config_file ) )
{
nwipe_log( NWIPE_LOG_ERROR,
"Syntax error: %s:%d - %s\n",
config_error_file( &nwipe_cfg ),
config_error_line( &nwipe_cfg ),
config_error_text( &nwipe_cfg ) );
}
}
else
{
nwipe_log( NWIPE_LOG_WARNING, "%s does not exist", nwipe_config_file );
/* We assume the /etc/nwipe directory doesn't exist, so try to create it */
mkdir( nwipe_config_directory, 0755 );
/* create the nwipe.conf file */
if( !( fp_config = fopen( nwipe_config_file, "w" ) ) )
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to create %s", nwipe_config_file );
}
else
{
nwipe_log( NWIPE_LOG_INFO, "Created %s", nwipe_config_file );
fclose( fp_config );
}
}
root = config_root_setting( &nwipe_cfg );
/**
* If they don't already exist, populate nwipe.conf with groups, settings and values.
* This will also fill in missing group or settings if they have been corrupted or
* accidently deleted by the user. It will also update an existing nwipe.conf
* file as new groups and settings are added to nwipe. If new settings are added
* to nwipes conf file they MUST appear below in this list of groups and settings.
*/
nwipe_conf_populate( "Organisation_Details.Business_Name", "Not Applicable (BN)" );
nwipe_conf_populate( "Organisation_Details.Business_Address", "Not Applicable (BA)" );
nwipe_conf_populate( "Organisation_Details.Contact_Name", "Not Applicable (BCN)" );
nwipe_conf_populate( "Organisation_Details.Contact_Phone", "Not Applicable (BCP)" );
nwipe_conf_populate( "Organisation_Details.Op_Tech_Name", "Not Applicable (OTN)" );
/**
* Add PDF Certificate/Report settings
*/
nwipe_conf_populate( "PDF_Certificate.PDF_Enable", "ENABLED" );
nwipe_conf_populate( "PDF_Certificate.PDF_Preview", "DISABLED" );
/**
* The currently selected customer that will be printed on the report
*/
nwipe_conf_populate( "Selected_Customer.Customer_Name", "Not Applicable (CN)" );
nwipe_conf_populate( "Selected_Customer.Customer_Address", "Not Applicable (CA)" );
nwipe_conf_populate( "Selected_Customer.Contact_Name", "Not Applicable (CCN)" );
nwipe_conf_populate( "Selected_Customer.Contact_Phone", "Not Applicable (CP)" );
/**
* Write out the new configuration.
*/
if( !config_write_file( &nwipe_cfg, nwipe_config_file ) )
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to write nwipe config to %s", nwipe_config_file );
}
else
{
nwipe_log( NWIPE_LOG_INFO, "Sucessfully written nwipe config to %s", nwipe_config_file );
}
/* Read the nwipe.conf configuration file and report any errors */
if( !config_read_file( &nwipe_cfg, nwipe_config_file ) )
{
nwipe_log( NWIPE_LOG_ERROR,
"Syntax error: %s:%d - %s\n",
config_error_file( &nwipe_cfg ),
config_error_line( &nwipe_cfg ),
config_error_text( &nwipe_cfg ) );
}
/* -----------------------------------------------------------------------------
* Now check nwipe_customers.csv exists, if not create it with a basic structure
*/
if( access( nwipe_customers_file, F_OK ) == 0 )
{
nwipe_log( NWIPE_LOG_INFO, "Nwipes customer file %s exists", nwipe_customers_file );
}
else
{
nwipe_log( NWIPE_LOG_WARNING, "%s does not exist", nwipe_customers_file );
/* We assume the /etc/nwipe directory doesn't exist, so try to create it */
mkdir( nwipe_config_directory, 0755 );
/* create the nwipe_customers.csv file */
if( !( fp_customers = fopen( nwipe_customers_file, "w" ) ) )
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to create %s", nwipe_customers_file );
}
else
{
nwipe_log( NWIPE_LOG_INFO, "Created %s", nwipe_customers_file );
/* Now populate the csv header and first entry, Lines 1 and 2 */
if( fwrite( nwipe_customers_initial_content, sizeof( nwipe_customers_initial_content ), 1, fp_customers )
== 1 )
{
nwipe_log( NWIPE_LOG_INFO, "Populated %s with basic config", nwipe_customers_file );
}
else
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to write basic config to %s", nwipe_customers_file );
}
fclose( fp_customers );
}
}
return ( 0 );
}
void save_selected_customer( char** customer )
{
/* This function saves the user selected customer
* to nwipe's config file /etc/nwipe/nwipe.conf
* for later use by the PDF creation functions.
*/
int idx;
int field_count;
int field_idx;
char field_1[FIELD_LENGTH];
char field_2[FIELD_LENGTH];
char field_3[FIELD_LENGTH];
char field_4[FIELD_LENGTH];
/* zero the field strings */
for( idx = 0; idx < FIELD_LENGTH; idx++ )
field_1[idx] = 0;
for( idx = 0; idx < FIELD_LENGTH; idx++ )
field_2[idx] = 0;
for( idx = 0; idx < FIELD_LENGTH; idx++ )
field_3[idx] = 0;
for( idx = 0; idx < FIELD_LENGTH; idx++ )
field_4[idx] = 0;
/* Extract the field contents from the csv string
*/
idx = 0;
field_idx = 0;
field_count = 1;
while( *( *customer + idx ) != 0 && field_count < NUMBER_OF_FIELDS + 1 )
{
/* Start of a field? */
if( *( *customer + idx ) == '\"' )
{
idx++;
while( *( *customer + idx ) != '\"' && *( *customer + idx ) != 0 )
{
if( field_count == 1 && field_idx < ( FIELD_LENGTH - 1 ) )
{
field_1[field_idx++] = *( *customer + idx );
}
else
{
if( field_count == 2 && field_idx < ( FIELD_LENGTH - 1 ) )
{
field_2[field_idx++] = *( *customer + idx );
}
else
{
if( field_count == 3 && field_idx < ( FIELD_LENGTH - 1 ) )
{
field_3[field_idx++] = *( *customer + idx );
}
else
{
if( field_count == 4 && field_idx < ( FIELD_LENGTH - 1 ) )
{
field_4[field_idx++] = *( *customer + idx );
}
}
}
}
idx++;
}
if( *( *customer + idx ) == '\"' )
{
/* Makesure the field string is terminated */
switch( field_count )
{
case 1:
field_1[field_idx] = 0;
break;
case 2:
field_2[field_idx] = 0;
break;
case 3:
field_3[field_idx] = 0;
break;
case 4:
field_4[field_idx] = 0;
break;
}
field_count++;
field_idx = 0;
}
}
idx++;
}
/* All 4 fields present? */
if( field_count != NUMBER_OF_FIELDS + 1 )
{
nwipe_log( NWIPE_LOG_ERROR,
"Insuffient fields in customer entry, expected %i, actual(field_count) %i, idx=%i",
NUMBER_OF_FIELDS,
field_count,
idx );
return;
}
/* -------------------------------------------------------------
* Write the fields to nwipe's config file /etc/nwipe/nwipe.conf
*/
if( ( setting = config_lookup( &nwipe_cfg, "Selected_Customer.Customer_Name" ) ) )
{
config_setting_set_string( setting, field_1 );
}
else
{
nwipe_log( NWIPE_LOG_ERROR, "Can't find \"Selected Customers.Customer_Name\" in %s", nwipe_config_file );
}
if( ( setting = config_lookup( &nwipe_cfg, "Selected_Customer.Customer_Address" ) ) )
{
config_setting_set_string( setting, field_2 );
}
else
{
nwipe_log( NWIPE_LOG_ERROR, "Can't find \"Selected Customers.Customer_Address\" in %s", nwipe_config_file );
}
if( ( setting = config_lookup( &nwipe_cfg, "Selected_Customer.Contact_Name" ) ) )
{
config_setting_set_string( setting, field_3 );
}
else
{
nwipe_log( NWIPE_LOG_ERROR, "Can't find \"Selected Customers.Contact_Name\" in %s", nwipe_config_file );
}
if( ( setting = config_lookup( &nwipe_cfg, "Selected_Customer.Contact_Phone" ) ) )
{
config_setting_set_string( setting, field_4 );
}
else
{
nwipe_log( NWIPE_LOG_ERROR, "Can't find \"Selected Customers.Contact_Phone\" in %s", nwipe_config_file );
}
/* Write out the new configuration. */
if( !config_write_file( &nwipe_cfg, nwipe_config_file ) )
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to write user selected customer to %s", nwipe_config_file );
}
else
{
nwipe_log( NWIPE_LOG_INFO, "Populated %s with user selected customer", nwipe_config_file );
}
}
int nwipe_conf_update_setting( char* group_name_setting_name, char* setting_value )
{
/* You would call this function of you wanted to update an existing setting in nwipe.conf, i.e
*
* nwipe_conf_update_setting( "PDF_Certificate.PDF_Enable", "ENABLED" )
*
* It is NOT used for creating a new group or setting name.
*/
/* -------------------------------------------------------------
* Write the field to nwipe's config file /etc/nwipe/nwipe.conf
*/
if( ( setting = config_lookup( &nwipe_cfg, group_name_setting_name ) ) )
{
config_setting_set_string( setting, setting_value );
}
else
{
nwipe_log(
NWIPE_LOG_ERROR, "Can't find group.setting_name %s in %s", group_name_setting_name, nwipe_config_file );
return 1;
}
/* Write the new configuration to nwipe.conf
*/
if( !config_write_file( &nwipe_cfg, nwipe_config_file ) )
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to write %s to %s", group_name_setting_name, nwipe_config_file );
return 2;
}
else
{
nwipe_log( NWIPE_LOG_INFO,
"Updated %s with value %s in %s",
group_name_setting_name,
setting_value,
nwipe_config_file );
}
return 0;
} // end nwipe_conf_update_setting()
int nwipe_conf_read_setting( char* group_name_setting_name, const char** setting_value )
{
/* You would call this function if you wanted to read a settings value in nwipe.conf, i.e
*
* const char ** pReturnString;
* nwipe_conf_read_setting( "PDF_Certificate", "PDF_Enable", pReturnString );
*
*/
/* Separate group_name_setting_name i.e "PDF_Certificate.PDF_Enable" string
* into two separate strings by replacing the period with a NULL.
*/
int return_status;
int length = strlen( group_name_setting_name );
char* group_name = malloc( length );
char* setting_name = malloc( length );
int idx = 0;
while( group_name_setting_name[idx] != 0 && group_name_setting_name[idx] != '.' )
{
if( group_name_setting_name[idx] == '.' )
{
break;
}
idx++;
}
memcpy( group_name, group_name_setting_name, idx );
strcpy( setting_name, &group_name_setting_name[idx + 1] );
if( !( setting = config_lookup( &nwipe_cfg, group_name ) ) )
{
nwipe_log( NWIPE_LOG_ERROR, "Can't find group name %s.%s in %s", group_name, setting_name, nwipe_config_file );
return_status = -1;
}
else
{
/* Retrieve data from nwipe.conf */
if( CONFIG_TRUE == config_setting_lookup_string( setting, setting_name, setting_value ) )
{
return_status = 0; /* Success */
}
else
{
nwipe_log(
NWIPE_LOG_ERROR, "Can't find setting_name %s.%s in %s", group_name, setting_name, nwipe_config_file );
return_status = -2;
}
}
free( group_name );
free( setting_name );
return ( return_status );
} // end nwipe_conf_read_setting()
int nwipe_conf_populate( char* path, char* value )
{
/* This function will check that a path containing a group or multiple groups that lead to a setting all exist,
* if they don't exist, the group/s, settings and associated value are created.
*
* The path, a string consisting of one or more groups separted by a period symbol
* '.' with the final element in the path being the setting name. For instance the path might be
* PDF_Certificate.PDF_Enable. Where PDF_Certificate is the group name and PDF_Enable is the setting name.
* If one or other don't exist then they will be created.
*
* An arbitary depth of four groups are allowed for nwipe's configuration file, although we only currently, as of
* October 2023 use a depth of one group. The number of groups can be increased in the future if required by
* changing the definition MAX_GROUP_DEPTH in conf.h
*/
char* path_copy;
char* path_build;
char* group_start[MAX_GROUP_DEPTH + 2]; // A pointer to the start of each group string
char* setting_start;
int idx; // General index
int group_count; // Counts the number of groups in the path
/* Initialise the pointers */
memset( group_start, 0, MAX_GROUP_DEPTH + 2 );
memset( &setting_start, 0, 1 );
/* Allocate enough memory for a copy of the path and initialise to zero */
path_copy = calloc( strlen( path ) + 1, sizeof( char ) );
path_build = calloc( strlen( path ) + 1, sizeof( char ) );
/* Duplicate the path */
strcpy( path_copy, path );
/* Create individual strings by replacing the period '.' with NULL, counting the number of groups. */
idx = 0;
group_count = 0;
/* pointer to first group */
group_start[group_count] = path_copy;
while( *( path_copy + idx ) != 0 )
{
if( group_count > MAX_GROUP_DEPTH )
{
nwipe_log( NWIPE_LOG_ERROR,
"Too many groups in path, specified = %i, allowed = %i ",
group_count,
MAX_GROUP_DEPTH );
return 1;
}
if( *( path_copy + idx ) == '.' )
{
*( path_copy + idx ) = 0;
group_count++;
group_start[group_count] = path_copy + idx + 1;
}
idx++;
}
/* setting_start points to a string that represents the setting to be created */
setting_start = group_start[group_count];
/* Remove the last entry from group_start as that would be the setting and not a group */
group_start[group_count] = 0;
if( group_count == 0 )
{
nwipe_log( NWIPE_LOG_ERROR, "No groups specified in path, i.e. no period . separator found." );
return 2;
}
/**
* Now determine whether the group/s in the path exist, if not create them.
*/
idx = 0;
while( group_start[idx] != 0 )
{
strcat( path_build, group_start[idx] );
if( !( group = config_lookup( &nwipe_cfg, path_build ) ) )
{
if( idx == 0 )
{
group = config_setting_add( root, path_build, CONFIG_TYPE_GROUP );
previous_group = group;
}
else
{
group = config_setting_add( previous_group, group_start[idx], CONFIG_TYPE_GROUP );
previous_group = group;
}
if( group )
{
nwipe_log( NWIPE_LOG_INFO, "Created group [%s] in %s", path_build, nwipe_config_file );
}
else
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to create group [%s] in %s", path_build, nwipe_config_file );
}
}
else
{
previous_group = group;
}
idx++;
if( group_start[idx] != 0 )
{
strcat( path_build, "." );
}
}
/**
* And finally determine whether the setting already exists, if not create it and assign the value to it
*/
/* Does the full path exist ? i.e AAA.BBB */
if( ( group = config_lookup( &nwipe_cfg, path_build ) ) )
{
/* Does the path and setting exist? AAA.BBB.SETTING_NAME */
if( !( setting = config_lookup( &nwipe_cfg, path ) ) )
{
/* Add the new SETTING_NAME */
if( ( setting = config_setting_add( group, setting_start, CONFIG_TYPE_STRING ) ) )
{
nwipe_log( NWIPE_LOG_INFO, "Created setting name %s in %s", path, nwipe_config_file );
}
else
{
nwipe_log(
NWIPE_LOG_ERROR, "Failed to create setting name %s in %s", setting_start, nwipe_config_file );
}
if( config_setting_set_string( setting, value ) == CONFIG_TRUE )
{
nwipe_log( NWIPE_LOG_INFO, "Set value for %s in %s to %s", path, nwipe_config_file, value );
}
else
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to set value for %s in %s to %s", path, nwipe_config_file, value );
}
}
else
{
if( nwipe_options.verbose )
{
nwipe_log( NWIPE_LOG_INFO, "Setting already exists [%s] in %s", path, nwipe_config_file );
}
}
}
else
{
nwipe_log( NWIPE_LOG_INFO, "Couldn't find constructed path [%s] in %s", path_build, nwipe_config_file );
}
free( path_copy );
free( path_build );
return 0;
}
void nwipe_conf_close()
{
config_destroy( &nwipe_cfg );
}
|