File: recfix.c

package info (click to toggle)
recutils 1.5-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 10,212 kB
  • sloc: ansic: 55,227; sh: 16,411; lisp: 1,431; yacc: 1,373; makefile: 304; lex: 223; sed: 16
file content (568 lines) | stat: -rw-r--r-- 13,703 bytes parent folder | download
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
/* -*- mode: C -*-
 *
 *       File:         recfix.c
 *       Date:         Tue Apr 27 12:21:48 2010
 *
 *       GNU recutils - recfix
 *
 */

/* Copyright (C) 2010, 2011, 2012 Jose E. Marchesi */

/* 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, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <getopt.h>
#include <stdlib.h>
#include <xalloc.h>
#include <getpass.h>
#include <gettext.h>
#define _(str) gettext (str)

#include <rec.h>
#include <recutl.h>

/* Forward prototypes.  */
static void recfix_parse_args (int argc, char **argv);
static bool recfix_check_database (rec_db_t db);

static int recfix_do_check (void);
static int recfix_do_sort (void);
#if defined REC_CRYPT_SUPPORT
static int recfix_do_crypt (void);
#endif
static int recfix_do_auto (void);

/*
 * Data types.
 */

/* recfix supports several operations, enumerated in the following
   type.  */

enum recfix_op
{
  RECFIX_OP_INVALID,
  RECFIX_OP_CHECK,
#if defined REC_CRYPT_SUPPORT
  RECFIX_OP_ENCRYPT,
  RECFIX_OP_DECRYPT,
#endif
  RECFIX_OP_SORT,
  RECFIX_OP_AUTO
};

/*
 * Global variables.
 */

bool  recfix_external = true;
char *recfix_file     = NULL;
int   recfix_op       = RECFIX_OP_INVALID;
char *recfix_password = NULL;
bool  recfix_force    = false;

/*
 * Command line options management.
 */

enum
{
  COMMON_ARGS,
  NO_EXTERNAL_ARG,
  FORCE_ARG,
  OP_SORT_ARG,
#if defined REC_CRYPT_SUPPORT
  PASSWORD_ARG,
  OP_ENCRYPT_ARG,
  OP_DECRYPT_ARG,
#endif
  OP_CHECK_ARG,
  OP_AUTO_ARG
};

static const struct option GNU_longOptions[] =
  {
    COMMON_LONG_ARGS,
    {"no-external", no_argument, NULL, NO_EXTERNAL_ARG},
    {"force", no_argument, NULL, FORCE_ARG},
    {"check", no_argument, NULL, OP_CHECK_ARG},
    {"sort", no_argument, NULL, OP_SORT_ARG},
#if defined REC_CRYPT_SUPPORT
    {"password", required_argument, NULL, PASSWORD_ARG},
    {"encrypt", no_argument, NULL, OP_ENCRYPT_ARG},
    {"decrypt", no_argument, NULL, OP_DECRYPT_ARG},
#endif
    {"auto", no_argument, NULL, OP_AUTO_ARG},
    {NULL, 0, NULL, 0}
  };

void
recutl_print_help (void)
{
  /* TRANSLATORS: --help output, recfix synopsis.
     no-wrap */
  printf (_("\
Usage: recfix [OPTION]... [OPERATION] [OP_OPTION]... [FILE]\n"));

  /* TRANSLATORS: --help output, recfix short description.
     no-wrap */
  fputs (_("\
Check and fix rec files.\n"),
         stdout);

  puts ("");
  /* TRANSLATORS: --help output, recfix global arguments.
     no-wrap */
  fputs (_("\
  -t, --type=TYPE                     process only records of the given type.\n\
      --no-external                   don't use external descriptors.\n\
      --force                         force the requested operation.\n"),
         stdout);

  recutl_print_help_common ();

  puts("");
  /* TRANSLATORS: --help output, recfix operations.
     no-wrap */
  fputs (_("\
Operations:\n\
      --check                         check integrity of the specified file.  Default.\n\
      --sort                          sort the records in the specified file.\n\
      --auto                          insert auto-generated fields in records missing them.\n"),
         stdout);

#if defined REC_CRYPT_SUPPORT
    /* TRANSLATORS: --help output, recfix operations related with encryption.
       no-wrap */
    fputs (_("\
      --encrypt                       encrypt confidential fields in the specified file.\n\
      --decrypt                       decrypt confidential fields in the specified file.\n"),
         stdout);

  puts("");
  /* TRANSLATORS: --help output, recfix encryption and decryption
     options.
     no-wrap */
  fputs (_("\
De/Encryption options:\n\
  -s, --password=PASSWORD             encrypt/decrypt with this password.\n"),
         stdout);
#endif /* REC_CRYPT_SUPPORT */

  puts("");
  /* TRANSLATORS: --help output, notes on recfix.
     no-wrap */
  fputs (_("\
If no FILE is specified then the command acts like a filter, getting\n\
the data from standard input and writing the result to standard output.\n"), stdout);

  puts("");
  recutl_print_help_footer ();
}

static void
recfix_parse_args (int argc,
                   char **argv)
{
  char c;
  int ret;

  while ((ret = getopt_long (argc,
                             argv,
                             ENCRYPTION_SHORT_ARGS,
                             GNU_longOptions,
                             NULL)) != -1)
    {
      c = ret;
      switch (c)
        {
          COMMON_ARGS_CASES
        case NO_EXTERNAL_ARG:
          {
            recfix_external = false;
            break;
          }
        case FORCE_ARG:
          {
            recfix_force = true;
            break;
          }
#if defined REC_CRYPT_SUPPORT
        case 's':
        case PASSWORD_ARG:
          {
            if (recfix_op == RECFIX_OP_INVALID)
              {
                recutl_fatal (_("--password|-s must be used as an operation argument.\n"));
              }

            if ((recfix_op != RECFIX_OP_ENCRYPT)
                && (recfix_op != RECFIX_OP_DECRYPT))
              {
                recutl_fatal (_("the specified operation does not require a password.\n"));
              }

            if (recfix_password != NULL)
              {
                recutl_fatal (_("please specify just one password.\n"));
              }

            recfix_password = xstrdup (optarg);
            break;
          }
#endif /* REC_CRYPT_SUPPORT */
        case OP_CHECK_ARG:
          {
            if (recfix_op != RECFIX_OP_INVALID)
              {
                recutl_fatal (_("please specify just one operation.\n"));
              }

            recfix_op = RECFIX_OP_CHECK;
            break;
          }
        case OP_SORT_ARG:
          {
            if (recfix_op != RECFIX_OP_INVALID)
              {
                recutl_fatal (_("please specify just one operation.\n"));
              }

            recfix_op = RECFIX_OP_SORT;
            break;
          }
        case OP_AUTO_ARG:
          {
            if (recfix_op != RECFIX_OP_INVALID)
              {
                recutl_fatal (_("please specify just one operation.\n"));
              }

            recfix_op = RECFIX_OP_AUTO;
            break;
          }
#if defined REC_CRYPT_SUPPORT
        case OP_ENCRYPT_ARG:
          {
            if (recfix_op != RECFIX_OP_INVALID)
              {
                recutl_fatal (_("please specify just one operation.\n"));
              }

            recfix_op = RECFIX_OP_ENCRYPT;
            break;
          }
        case OP_DECRYPT_ARG:
          {
            if (recfix_op != RECFIX_OP_INVALID)
              {
                recutl_fatal (_("please specify just one operation.\n"));
              }

            recfix_op = RECFIX_OP_DECRYPT;
            break;
          }
#endif /* REC_CRYPT_SUPPORT */
        default:
          {
            exit (EXIT_FAILURE);
          }
        }
    }

  /* The default operation is check, in case the user did not specify
     any in the command line.  */

  if (recfix_op == RECFIX_OP_INVALID)
    {
      recfix_op = RECFIX_OP_CHECK;
    }

#if defined REC_CRYPT_SUPPORT
  /* The encrypt and decrypt operations require the user to specify a
     password.  If no password was specified with -s and the program
     is running in a terminal, prompt the user to provide the
     password.  */

  if (((recfix_op == RECFIX_OP_ENCRYPT)
       || (recfix_op == RECFIX_OP_DECRYPT))
      && (recfix_password == NULL))
    {
      if (recutl_interactive ())
        {
          recfix_password = getpass (_("Password: "));
        }

      if (!recfix_password || (strlen (recfix_password) == 0))
        {
          recutl_fatal ("please specify a password.\n");
        }
    }
#endif /* REC_CRYPT_SUPPORT */

  /* Read the name of the file to work on.  */
  if (optind < argc)
    {
      if ((argc - optind) != 1)
        {
          recutl_print_help ();
          exit (EXIT_FAILURE);
        }

      recfix_file = argv[optind++];
    }
}

static bool
recfix_check_database (rec_db_t db)
{
  bool ret;
  char *errors;
  size_t errors_size;
  rec_buf_t buf;

  buf = rec_buf_new (&errors, &errors_size);
  ret = (rec_int_check_db (db,
                           true,            /* Check descriptors.  */
                           recfix_external, /* Use external descriptors.  */
                           buf) == 0);
  rec_buf_close (buf);
  fprintf (stderr, "%s", errors);

  return ret;
}

static int
recfix_do_check ()
{
  rec_db_t db;

  /* Read the database from the specified file and check its
     integrity.  */

  db = recutl_read_db_from_file (recfix_file);
  if (!db)
    {
      return EXIT_FAILURE;
    }

  if (!recfix_check_database (db))
    {
      return EXIT_FAILURE;
    }

  return EXIT_SUCCESS;
}

static int
recfix_do_sort ()
{
  rec_db_t db     = NULL;
  size_t n_rset   = 0;
  rec_rset_t rset = NULL;

  /* Read the database from the specified file.  */

  db = recutl_read_db_from_file (recfix_file);
  if (!db)
    {
      return EXIT_FAILURE;
    }

  /* Sort all the record sets contained in the database.  */

  for (n_rset = 0; n_rset < rec_db_size (db); n_rset++)
    {
      rset = rec_db_get_rset (db, n_rset);
      rec_rset_sort (rset, NULL);
    }
  
  if (!recfix_check_database (db))
    {
      return EXIT_FAILURE;
    }

  recutl_write_db_to_file (db, recfix_file);
  return EXIT_SUCCESS;
}

#if defined REC_CRYPT_SUPPORT

static int
recfix_do_crypt ()
{
  rec_db_t db;
  size_t n_rset;

  /* Read the database from the specified file. */

  db = recutl_read_db_from_file (recfix_file);
  if (!db)
    {
      return EXIT_FAILURE;
    }

  /* Encrypt/decrypt any unencrypted/encrypted field marked as
     "confidential" using the given password.  */

  for (n_rset = 0; n_rset < rec_db_size (db); n_rset++)
    {
      rec_mset_iterator_t iter;
      rec_fex_t confidential_fields;
      rec_record_t record;
      rec_rset_t rset =
        rec_db_get_rset (db, n_rset);

      /* Skip record sets not having any confidential fields.  */

      confidential_fields = rec_rset_confidential (rset);
      if (confidential_fields == NULL)
        {
          continue;
        }

      /* Process every record of the record set.  */

      iter = rec_mset_iterator (rec_rset_mset (rset));
      while (rec_mset_iterator_next (&iter, MSET_RECORD, (const void **) &record, NULL))
        {
          if (recfix_op == RECFIX_OP_ENCRYPT)
            {
              /* Encrypt any unencrypted confidential field in this
                 record.  */

              if (!rec_encrypt_record (rset, record, recfix_password)
                  && !recfix_force)
                {
                  recutl_error (_("the database contains already encrypted fields\n"));
                  recutl_fatal (_("please use --force or --decrypt\n"));
                }
            }
          else
            {
              /* Decrypt any encrypted confidential field in this
                 record.  */

              rec_decrypt_record (rset, record, recfix_password);
            }
        }

      rec_mset_iterator_free (&iter);
    }

  /* Write the modified database back to the file.  */

  recutl_write_db_to_file (db, recfix_file);

  return EXIT_SUCCESS;
}

#endif /* REC_CRYPT_SUPPORT */

static int
recfix_do_auto ()
{
  rec_db_t db    = NULL;
  size_t n_rset  = 0;

  /* Read the database from the especified file.  */

  db = recutl_read_db_from_file (recfix_file);
  if (!db)
    {
      return EXIT_FAILURE;
    }

  /* Add auto fields to any record in the database not having it, in
     record sets featuring auto fields.  */

  for (n_rset = 0; n_rset < rec_db_size (db); n_rset++)
    {
      rec_mset_iterator_t iter;
      rec_record_t record;
      rec_rset_t rset = rec_db_get_rset (db, n_rset);

      iter = rec_mset_iterator (rec_rset_mset (rset));
      while (rec_mset_iterator_next (&iter, MSET_RECORD, (const void**) &record, NULL))
        {
          rec_rset_add_auto_fields (rset, record);
        }

      rec_mset_iterator_free (&iter);
    }

  if (!recfix_check_database (db))
    {
      return EXIT_FAILURE;
    }

  recutl_write_db_to_file (db, recfix_file);
  return EXIT_SUCCESS;
}

int
main (int argc, char *argv[])
{
  int res     = EXIT_SUCCESS;

  recutl_init ("recfix");

  /* Parse arguments.  */

  recfix_parse_args (argc, argv);

  /* Execute the proper operation as specified in the recfix_op
     variable.  */

  switch (recfix_op)
    {
    case RECFIX_OP_CHECK:
      {
        res = recfix_do_check ();
        break;
      }
    case RECFIX_OP_SORT:
      {
        res = recfix_do_sort ();
        break;
      }
    case RECFIX_OP_AUTO:
      {
        res = recfix_do_auto ();
        break;
      }
#if defined REC_CRYPT_SUPPORT
    case RECFIX_OP_ENCRYPT:
    case RECFIX_OP_DECRYPT:
      {
        res = recfix_do_crypt ();
        break;
      }
#endif /* REC_CRYPT_SUPPORT */
    default:
      {
        /* This point shall not be reached.  */

        res = EXIT_FAILURE;
        recutl_fatal (_("unknown operation in recfix: please report this as a bug.\n"));
      }
    }

  return res;
}

/* End of recfix.c */