Exemplo n.º 1
0
Arquivo: gpgtar.c Projeto: gpg/gnupg
/* gpgtar main. */
int
main (int argc, char **argv)
{
    gpg_error_t err;
    const char *fname;
    ARGPARSE_ARGS pargs;

    assert (sizeof (struct ustar_raw_header) == 512);

    gnupg_reopen_std (GPGTAR_NAME);
    set_strusage (my_strusage);
    log_set_prefix (GPGTAR_NAME, 1);

    /* Make sure that our subsystems are ready.  */
    i18n_init();
    init_common_subsystems (&argc, &argv);

    /* Parse the command line. */
    pargs.argc  = &argc;
    pargs.argv  = &argv;
    pargs.flags = ARGPARSE_FLAG_KEEP;
    parse_arguments (&pargs, opts);

    if ((files_from && !null_names) || (!files_from && null_names))
        log_error ("--files-from and --null may only be used in conjunction\n");
    if (files_from && strcmp (files_from, "-"))
        log_error ("--files-from only supports argument \"-\"\n");

    if (log_get_errorcount (0))
        exit (2);

    /* Print a warning if an argument looks like an option.  */
    if (!opt.quiet && !(pargs.flags & ARGPARSE_FLAG_STOP_SEEN))
    {
        int i;

        for (i=0; i < argc; i++)
            if (argv[i][0] == '-' && argv[i][1] == '-')
                log_info (_("NOTE: '%s' is not considered an option\n"), argv[i]);
    }

    if (! opt.gpg_program)
        opt.gpg_program = gnupg_module_name (GNUPG_MODULE_NAME_GPG);

    if (opt.verbose > 1)
        opt.debug_level = 1024;

    switch (cmd)
    {
    case aList:
        if (argc > 1)
            usage (1);
        fname = argc ? *argv : NULL;
        if (opt.filename)
            log_info ("note: ignoring option --set-filename\n");
        if (files_from)
            log_info ("note: ignoring option --files-from\n");
        err = gpgtar_list (fname, !skip_crypto);
        if (err && log_get_errorcount (0) == 0)
            log_error ("listing archive failed: %s\n", gpg_strerror (err));
        break;

    case aEncrypt:
    case aSign:
    case aSignEncrypt:
        if ((!argc && !null_names)
                || (argc && null_names))
            usage (1);
        if (opt.filename)
            log_info ("note: ignoring option --set-filename\n");
        err = gpgtar_create (null_names? NULL :argv,
                             !skip_crypto
                             && (cmd == aEncrypt || cmd == aSignEncrypt),
                             cmd == aSign || cmd == aSignEncrypt);
        if (err && log_get_errorcount (0) == 0)
            log_error ("creating archive failed: %s\n", gpg_strerror (err));
        break;

    case aDecrypt:
        if (argc != 1)
            usage (1);
        if (opt.outfile)
            log_info ("note: ignoring option --output\n");
        if (files_from)
            log_info ("note: ignoring option --files-from\n");
        fname = argc ? *argv : NULL;
        err = gpgtar_extract (fname, !skip_crypto);
        if (err && log_get_errorcount (0) == 0)
            log_error ("extracting archive failed: %s\n", gpg_strerror (err));
        break;

    default:
        log_error (_("invalid command (there is no implicit command)\n"));
        break;
    }

    return log_get_errorcount (0)? 1:0;
}
Exemplo n.º 2
0
/* gpgtar main. */
int
main (int argc, char **argv)
{
  ARGPARSE_ARGS pargs;
  const char *fname;
  int no_more_options = 0;
  enum cmd_and_opt_values cmd = 0;
  int skip_crypto = 0;
  const char *files_from = NULL;
  int null_names = 0;

  assert (sizeof (struct ustar_raw_header) == 512);

  gnupg_reopen_std ("gpgtar");
  set_strusage (my_strusage);
  log_set_prefix ("gpgtar", 1);

  /* Make sure that our subsystems are ready.  */
  i18n_init();
  init_common_subsystems ();

  /* Parse the command line. */
  pargs.argc  = &argc;
  pargs.argv  = &argv;
  pargs.flags = ARGPARSE_FLAG_KEEP;
  while (!no_more_options && optfile_parse (NULL, NULL, NULL, &pargs, opts))
    {
      switch (pargs.r_opt)
        {
        case oOutput:    opt.outfile = pargs.r.ret_str; break;
        case oSetFilename: opt.filename = pargs.r.ret_str; break;
	case oQuiet:     opt.quiet = 1; break;
        case oVerbose:   opt.verbose++; break;
        case oNoVerbose: opt.verbose = 0; break;
        case oFilesFrom: files_from = pargs.r.ret_str; break;
        case oNull: null_names = 1; break;
          
	case aList:
        case aDecrypt:
        case aEncrypt:
        case aSign:
          set_cmd (&cmd, pargs.r_opt);
	  break;

        case oSymmetric:
          set_cmd (&cmd, aEncrypt);
          opt.symmetric = 1;
          break;

        case oSkipCrypto:
          skip_crypto = 1;
          break;

        case oOpenPGP: /* Dummy option for now.  */ break;
        case oCMS:     /* Dummy option for now.  */ break;

        default: pargs.err = 2; break;
	}
    }
  
  if ((files_from && !null_names) || (!files_from && null_names))
    log_error ("--files-from and --null may only be used in conjunction\n");
  if (files_from && strcmp (files_from, "-"))
    log_error ("--files-from only supports argument \"-\"\n");

  if (log_get_errorcount (0))
    exit (2);

  switch (cmd)
    {
    case aList:
      if (argc > 1)
        usage (1);
      fname = argc ? *argv : NULL;
      if (opt.filename)
        log_info ("note: ignoring option --set-filename\n");
      if (files_from)
        log_info ("note: ignoring option --files-from\n");
      if (skip_crypto)
        gpgtar_list (fname);
      else
        decrypt_and_list (fname);
      break;

    case aEncrypt:
      if ((!argc && !null_names)
          || (argc && null_names))
        usage (1);
      if (opt.filename)
        log_info ("note: ignoring option --set-filename\n");
      if (skip_crypto)
        gpgtar_create (null_names? NULL :argv);
      else
        tar_and_encrypt (null_names? NULL : argv);
      break;

    case aDecrypt:
      if (argc != 1)
        usage (1);
      if (opt.outfile)
        log_info ("note: ignoring option --output\n");
      if (files_from)
        log_info ("note: ignoring option --files-from\n");
      fname = argc ? *argv : NULL;
      if (skip_crypto)
        gpgtar_extract (fname);
      else
        decrypt_and_untar (fname);
      break;

    default:
      log_error (_("invalid command (there is no implicit command)\n"));
      break;
    }

  return log_get_errorcount (0)? 1:0;
}