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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
|
/*
* ****************************************************************************
* customers.c: Functions related to customer processing for the PDF erasure *
* certificate. *
* ****************************************************************************
*
* 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 <stdio.h>
#include "nwipe.h"
#include "context.h"
#include "gui.h"
#include "logging.h"
#include "conf.h"
#include "customers.h"
#include <sys/stat.h>
void customer_processes( int mode )
{
/* This function reads the customers.csv file, counts the number of lines,
* converts line feeds to NULL and constructs a array of pointers that point
* to the variable length strings.
*
* Depending on the value of mode the pointer array is passed to either
* the select_customers() or delete_customer() functions.
*/
int idx;
int idx2;
FILE* fptr;
struct stat st;
intmax_t size = 0;
int lines;
int list_idx;
int current_list_size;
size_t result_size;
extern char nwipe_customers_file[];
/* Determine size of customers.csv file */
stat( nwipe_customers_file, &st );
size = st.st_size;
current_list_size = 0;
nwipe_customers_buffer_t raw_buffer = (nwipe_customers_buffer_t) calloc( 1, size + 1 );
/* Allocate storage for the contents of customers.csv */
nwipe_customers_buffer_t buffer = (nwipe_customers_buffer_t) calloc( 1, size + 1 );
/* Allocate storage for the processed version of customers.csv,
* i.e we convert the csv format to strings without the quotes
* and semi colon delimiters
*/
nwipe_customers_pointers_t list = (nwipe_customers_pointers_t) calloc( 1, sizeof( char* ) );
current_list_size += sizeof( char* );
/* Open customers.csv */
if( ( fptr = fopen( nwipe_customers_file, "rb" ) ) == NULL )
{
nwipe_log( NWIPE_LOG_ERROR, "Unable to open %s", nwipe_customers_file );
free( buffer );
free( *list );
return;
}
/* Read the customers.csv file and populate the list array with the data */
result_size = fread( raw_buffer, size, 1, fptr );
fclose( fptr );
/* Validate csv contents. With the exception of line feeds,
* remove non printable characters and move to a secondary buffer.
*/
idx = 0;
idx2 = 0;
while( idx < size )
{
if( ( raw_buffer[idx] > 0x1F && raw_buffer[idx] < 0x7F ) || raw_buffer[idx] == 0x0A )
{
/* copy printable characters and line feeds */
buffer[idx2++] = raw_buffer[idx];
}
idx++;
}
/* Construct a array of pointers that point to each line of the csv file
*/
idx = 0;
lines = 0;
list_idx = 0;
while( idx < size )
{
if( buffer[idx] == 0x0A )
{
buffer[idx] = 0;
/* increment the line counter, but don't count
* the first line as that is the csv header line.
*/
if( idx != 0 )
{
lines++;
/* Change the line feed to a NULL, string terminator */
buffer[idx] = 0;
/* Save the pointer to the first data line of the csv. */
list[list_idx++] = &buffer[idx + 1];
current_list_size += sizeof( char* );
/* Expand allocated memory by the size of one pointer */
if( ( list = realloc( list, current_list_size ) ) == NULL )
{
nwipe_log( NWIPE_LOG_ERROR, "Unable to realloc customer list array, out of memory?" );
break;
}
current_list_size += sizeof( char* );
}
}
else
{
/* Replace colons with commas */
if( buffer[idx] == ';' )
{
buffer[idx] = ',';
}
}
idx++;
}
/* Sync lines variable to actual number of lines */
if( lines > 0 )
lines--;
if( idx == size )
{
/* makesure the very last entry is NULL terminated */
buffer[idx] = 0;
}
/* Select the requested mode, customer or delete customer.
*/
switch( mode )
{
case SELECT_CUSTOMER:
select_customers( lines, list );
break;
case DELETE_CUSTOMER:
delete_customer( lines, list );
break;
}
free( raw_buffer );
free( buffer );
}
void select_customers( int count, char** customer_list_array )
{
int selected_entry = 0;
char window_title[] = " Select Customer For PDF Report ";
/* Display the customer selection window */
nwipe_gui_list( count, window_title, customer_list_array, &selected_entry );
/* Save the selected customer details to nwipe's config file /etc/nwipe/nwipe.conf
* If selected entry equals 0, then the customer did not select an entry so skip save.
*/
if( selected_entry != 0 )
{
save_selected_customer( &customer_list_array[selected_entry - 1] );
}
}
void delete_customer( int count, char** customer_list_array )
{
char window_title[] = " Delete Customer ";
int selected_entry = 0;
nwipe_gui_list( count, window_title, customer_list_array, &selected_entry );
if( selected_entry != 0 )
{
delete_customer_csv_entry( &selected_entry );
}
}
void write_customer_csv_entry( char* customer_name,
char* customer_address,
char* customer_contact_name,
char* customer_contact_phone )
{
/**
* Write the attached strings in csv format to the first
* line after the header (line 2 of file)
*/
FILE* fptr = 0;
FILE* fptr2 = 0;
size_t result_size;
/* General index variables */
int idx1, idx2, idx3;
/* Length of the new customer line */
int csv_line_length;
/* Size of the new buffer that holds old contents plus new entry */
int new_customers_buffer_size;
struct stat st;
extern char nwipe_customers_file[];
extern char nwipe_customers_file_backup[];
extern char nwipe_customers_file_backup_tmp[];
intmax_t existing_file_size = 0;
/* pointer to the new customer entry in csv format. */
char* csv_buffer = 0;
/* pointer to the buffer containing the existing customer file */
char* customers_buffer = 0;
/* pointer to the buffer containing the existing customer file plus the new entry */
char* new_customers_buffer = 0;
size_t new_customers_buffer_length;
/* Determine length of all four strings and malloc sufficient storage + 12 = 8 quotes + three colons + null */
csv_line_length = strlen( customer_name ) + strlen( customer_address ) + strlen( customer_contact_name )
+ strlen( customer_contact_phone ) + 12;
if( !( csv_buffer = calloc( 1, csv_line_length == 0 ) ) )
{
nwipe_log( NWIPE_LOG_ERROR, "func:nwipe_gui_add_customer:csv_buffer, calloc returned NULL " );
}
else
{
/* Determine current size of the csv file containing the customers */
stat( nwipe_customers_file, &st );
existing_file_size = st.st_size;
/* calloc sufficient storage to hold the existing customers file */
if( !( customers_buffer = calloc( 1, existing_file_size + 1 ) ) )
{
nwipe_log( NWIPE_LOG_ERROR, "func:nwipe_gui_add_customer:customers_buffer, calloc returned NULL " );
}
else
{
/* create a third buffer which is the combined size of the previous two, i.e existing file size, plus the
* new customer entry + 1 (NULL) */
new_customers_buffer_size = existing_file_size + csv_line_length + 1;
if( !( new_customers_buffer = calloc( 1, new_customers_buffer_size ) ) )
{
nwipe_log( NWIPE_LOG_ERROR, "func:nwipe_gui_add_customer:customers_buffer, calloc returned NULL " );
}
else
{
/* Read the whole of customers.csv file into customers_buffer */
if( ( fptr = fopen( nwipe_customers_file, "rb" ) ) == NULL )
{
nwipe_log( NWIPE_LOG_ERROR, "Unable to open %s", nwipe_customers_file );
}
else
{
/* Read the customers.csv file and populate the list array with the data */
if( ( result_size = fread( customers_buffer, existing_file_size, 1, fptr ) ) != 1 )
{
nwipe_log(
NWIPE_LOG_ERROR,
"func:nwipe_gui_add_customer:Error reading customers file, # bytes read not as expected "
"%i bytes",
result_size );
}
else
{
/* --------------------------------------------------------------------
* Read the first line which is the csv header from the existing customer
* buffer & write to the new buffer.
*/
idx1 = 0; // Index for the current csv buffer
idx2 = 0; // Index for the new csv buffer
idx3 = 0; // Index for new customer fields
while( idx1 < existing_file_size && idx2 < new_customers_buffer_size )
{
if( customers_buffer[idx1] != LINEFEED )
{
new_customers_buffer[idx2++] = customers_buffer[idx1++];
}
else
{
new_customers_buffer[idx2++] = LINEFEED;
break;
}
}
/* --------------------------------------------------------------------------
* Copy the new customer name entry so it is immediately after the csv header
*/
/* Start with first entries opening quote */
new_customers_buffer[idx2++] = '"';
/* Copy the customer_name string */
while( idx3 < FIELD_LENGTH && idx2 < new_customers_buffer_size && customer_name[idx3] != 0 )
{
new_customers_buffer[idx2++] = customer_name[idx3++];
}
/* Close customer name field with a quote */
new_customers_buffer[idx2++] = '"';
/* Insert field delimiters, we use a semi-colon, not a comma ';' */
new_customers_buffer[idx2++] = ';';
/* -----------------------------------------------------------------------------
* Copy the new customer address entry so it is immediately after the csv header
*/
idx3 = 0;
/* Start with first entries opening quote */
new_customers_buffer[idx2++] = '\"';
/* Copy the customer_name string */
while( idx3 < FIELD_LENGTH && idx2 < new_customers_buffer_size && customer_address[idx3] != 0 )
{
new_customers_buffer[idx2++] = customer_address[idx3++];
}
/* Close customer name field with a quote */
new_customers_buffer[idx2++] = '\"';
/* Insert field delimiters, we use a semi-colon, not a comma ';' */
new_customers_buffer[idx2++] = ';';
/* -----------------------------------------------------------------------------
* Copy the new customer contact name entry so it is immediately after the csv header
*/
idx3 = 0;
/* Start with first entries opening quote */
new_customers_buffer[idx2++] = '\"';
/* Copy the customer_name string */
while( idx3 < FIELD_LENGTH && idx2 < new_customers_buffer_size
&& customer_contact_name[idx3] != 0 )
{
new_customers_buffer[idx2++] = customer_contact_name[idx3++];
}
/* Close customer name field with a quote */
new_customers_buffer[idx2++] = '\"';
/* Insert field delimiters, we use a semi-colon, not a comma ';' */
new_customers_buffer[idx2++] = ';';
/* -----------------------------------------------------------------------------
* Copy the new customer contact phone entry so it is immediately after the csv header
*/
idx3 = 0;
/* Start with first entries opening quote */
new_customers_buffer[idx2++] = '\"';
/* Copy the customer_name string */
while( idx3 < FIELD_LENGTH && idx2 < new_customers_buffer_size
&& customer_contact_phone[idx3] != 0 )
{
new_customers_buffer[idx2++] = customer_contact_phone[idx3++];
}
/* Close customer name field with a quote */
new_customers_buffer[idx2++] = '\"';
/* Insert a line feed to finish the new entry */
new_customers_buffer[idx2++] = LINEFEED;
/* skip any LINEFEEDS in the existing customer entry as we just inserted one */
while( customers_buffer[idx1] != 0 && customers_buffer[idx1] == LINEFEED )
{
idx1++;
}
/* -------------------------------------------------------------------------------
* Now copy the existing customer entries, if any, immediately after the new entry
*/
while( idx1 < existing_file_size && idx2 < new_customers_buffer_size )
{
/* Removes any nulls when copying and pasting, which would break this process? */
if( customers_buffer[idx1] == 0 )
{
while( idx1 < existing_file_size && customers_buffer[idx1] == 0 )
{
idx1++;
}
}
new_customers_buffer[idx2++] = customers_buffer[idx1++];
}
/* Rename the customers.csv file to customers.csv.backup */
if( rename( nwipe_customers_file, nwipe_customers_file_backup_tmp ) != 0 )
{
nwipe_log( NWIPE_LOG_ERROR,
"Unable to rename %s to %s",
nwipe_customers_file,
nwipe_customers_file_backup_tmp );
}
else
{
/* Create/open the customers.csv file */
if( ( fptr2 = fopen( nwipe_customers_file, "wb" ) ) == NULL )
{
nwipe_log( NWIPE_LOG_ERROR, "Unable to open %s", nwipe_customers_file );
}
else
{
/* write the new customers.csv file */
new_customers_buffer_length = strlen( new_customers_buffer );
if( ( result_size = fwrite(
new_customers_buffer, sizeof( char ), new_customers_buffer_length, fptr2 ) )
!= new_customers_buffer_length )
{
nwipe_log(
NWIPE_LOG_ERROR,
"func:write_customer_csv_entry:fwrite: Error result_size = %i not as expected",
result_size );
}
else
{
/* Remove the customer.csv.backup file if it exists */
if( remove( nwipe_customers_file_backup ) != 0 )
{
nwipe_log(
NWIPE_LOG_ERROR, "Unable to remove %s", nwipe_customers_file_backup_tmp );
}
else
{
/* Rename the customers.csv.backup.tmp file to customers.csv.backup */
if( rename( nwipe_customers_file_backup_tmp, nwipe_customers_file_backup )
!= 0 )
{
nwipe_log( NWIPE_LOG_ERROR,
"Unable to rename %s to %s",
nwipe_customers_file,
nwipe_customers_file_backup_tmp );
}
nwipe_log( NWIPE_LOG_INFO,
"Succesfully write new customer entry to %s",
nwipe_customers_file );
}
}
fclose( fptr2 );
}
}
fclose( fptr );
}
}
free( new_customers_buffer );
}
free( customers_buffer );
}
free( csv_buffer );
}
}
void delete_customer_csv_entry( int* selected_entry )
{
/**
* Deletes a line from the csv file. The line to be deleted is determined
* by the value of selected_entry
*/
FILE* fptr = 0;
FILE* fptr2 = 0;
size_t result_size;
struct stat st;
extern char nwipe_customers_file[];
extern char nwipe_customers_file_backup[];
extern char nwipe_customers_file_backup_tmp[];
intmax_t existing_file_size = 0;
int linecount;
/* General index variables */
int idx1, idx2, idx3;
/* pointer to the buffer containing the existing customer file */
char* customers_buffer = 0;
/* pointer to the buffer containing the existing customer minus the deleted entry */
char* new_customers_buffer = 0;
int status_flag = 0;
size_t new_customers_buffer_length;
/* Determine current size of the csv file containing the customers */
stat( nwipe_customers_file, &st );
existing_file_size = st.st_size;
/* calloc sufficient storage to hold the existing customers file */
if( !( customers_buffer = calloc( 1, existing_file_size + 1 ) ) )
{
nwipe_log( NWIPE_LOG_ERROR,
"func:nwipe_gui_delete_customer_csv_entry:customers_buffer, calloc returned NULL " );
}
else
{
/* create a second buffer which is identical in size to the first, it will store the customer
* csv file minus the one selected entry
*/
if( !( new_customers_buffer = calloc( 1, existing_file_size + 1 ) ) )
{
nwipe_log( NWIPE_LOG_ERROR,
"func:nwipe_gui_delete_customer_csv_entry:customers_buffer, calloc returned NULL " );
}
else
{
/* Read the whole of customers.csv file into customers_buffer */
if( ( fptr = fopen( nwipe_customers_file, "rb" ) ) == NULL )
{
nwipe_log( NWIPE_LOG_ERROR,
"func:nwipe_gui_delete_customer_csv_entry:Unable to open %s",
nwipe_customers_file );
}
else
{
/* Read the customers.csv file and populate the list array with the data */
if( ( result_size = fread( customers_buffer, existing_file_size, 1, fptr ) ) != 1 )
{
nwipe_log( NWIPE_LOG_ERROR,
"func:nwipe_gui_delete_customer_csv_entry:Error reading customers file, # elements read "
"not as expected "
"%i elements",
result_size );
}
else
{
/* --------------------------------------------------------------------
* Read the first line which is the csv header from the existing customer
* buffer & write to the new buffer.
*/
idx1 = 0; // Index for the current csv buffer
idx2 = 0; // Index for the new csv buffer
linecount = 1; // count the lines in the csv file starting at line 1
while( idx1 < existing_file_size && idx2 < existing_file_size )
{
if( customers_buffer[idx1] != LINEFEED )
{
new_customers_buffer[idx2++] = customers_buffer[idx1++];
}
else
{
new_customers_buffer[idx2++] = customers_buffer[idx1++];
break;
}
}
/* -------------------------------------------------------------------------------
* Now copy the existing customer entries, counting the lines as we go and when we
* get to the the line selected for deletion we skip over it and then carry on
* copying.
*/
while( idx1 < existing_file_size && idx2 < existing_file_size )
{
/* Don't copy nulls */
if( customers_buffer[idx1] == 0 )
{
idx1++;
continue;
}
/* Is this the line to delete? */
if( linecount == *selected_entry )
{
/* skip all the characters in this line */
while( idx1 < existing_file_size && customers_buffer[idx1] != LINEFEED )
{
idx1++;
}
/* skip the trailing linefeed if it exists, may not exist if last line */
if( customers_buffer[idx1] == LINEFEED )
{
idx1++;
}
linecount++;
nwipe_log( NWIPE_LOG_INFO, "Deleted customer entry from cache" );
status_flag = 1;
}
else
{
/* Is the character a LINEFEED? */
if( customers_buffer[idx1] == LINEFEED )
{
linecount++;
}
/* Copy a character */
new_customers_buffer[idx2++] = customers_buffer[idx1++];
}
}
/* Rename the customers.csv file to customers.csv.backup */
if( rename( nwipe_customers_file, nwipe_customers_file_backup_tmp ) != 0 )
{
nwipe_log( NWIPE_LOG_ERROR,
"func:delete_customer_csv_entry:Unable to rename %s to %s",
nwipe_customers_file,
nwipe_customers_file_backup_tmp );
}
else
{
/* Create/open the customers.csv file */
if( ( fptr2 = fopen( nwipe_customers_file, "wb" ) ) == NULL )
{
nwipe_log( NWIPE_LOG_ERROR,
"func:delete_customer_csv_entry:Unable to open %s",
nwipe_customers_file );
}
else
{
/* write the new customers.csv file */
new_customers_buffer_length = strlen( new_customers_buffer );
if( ( result_size = fwrite(
new_customers_buffer, sizeof( char ), new_customers_buffer_length, fptr2 ) )
!= new_customers_buffer_length )
{
nwipe_log(
NWIPE_LOG_ERROR,
"func:delete_customer_csv_entry:fwrite: Error result_size = %i not as expected",
result_size );
}
else
{
/* Remove the customer.csv.backup file if it exists */
if( remove( nwipe_customers_file_backup ) != 0 )
{
nwipe_log( NWIPE_LOG_ERROR,
"func:delete_customer_csv_entry:Unable to remove %s",
nwipe_customers_file_backup_tmp );
}
else
{
/* Rename the customers.csv.backup.tmp file to customers.csv.backup */
if( rename( nwipe_customers_file_backup_tmp, nwipe_customers_file_backup ) != 0 )
{
nwipe_log( NWIPE_LOG_ERROR,
"func:delete_customer_csv_entry:Unable to rename %s to %s",
nwipe_customers_file,
nwipe_customers_file_backup_tmp );
}
if( status_flag == 1 )
{
nwipe_log(
NWIPE_LOG_INFO, "Deleted customer entry in %s", nwipe_customers_file );
}
else
{
nwipe_log( NWIPE_LOG_INFO,
"Failed to delete customer entry in %s",
nwipe_customers_file );
}
}
}
fclose( fptr2 );
}
}
}
fclose( fptr );
}
free( new_customers_buffer );
}
free( customers_buffer );
}
}
|