/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2018-04-01 22:08:50 UTC
  • Revision ID: teddy@recompile.se-20180401220850-11kc7596lknn4piy
Check for and report GPGME key import errors

* plugins.d/mandos-client.c (init_gpgme/import_key): After
  gpgme_op_import(), call gpgme_op_import_result() to see if it
  worked.  If not, print all available information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
310
310
                   gpgme_strsource(rc), gpgme_strerror(rc));
311
311
      return false;
312
312
    }
 
313
    {
 
314
      gpgme_import_result_t import_result
 
315
        = gpgme_op_import_result(mc->ctx);
 
316
      if((import_result->imported < 1
 
317
          or import_result->not_imported > 0)
 
318
         and import_result->unchanged == 0){
 
319
        fprintf_plus(stderr, "bad gpgme_op_import_results:\n");
 
320
        fprintf_plus(stderr,
 
321
                     "The total number of considered keys: %d\n",
 
322
                     import_result->considered);
 
323
        fprintf_plus(stderr,
 
324
                     "The number of keys without user ID: %d\n",
 
325
                     import_result->no_user_id);
 
326
        fprintf_plus(stderr,
 
327
                     "The total number of imported keys: %d\n",
 
328
                     import_result->imported);
 
329
        fprintf_plus(stderr, "The number of imported RSA keys: %d\n",
 
330
                     import_result->imported_rsa);
 
331
        fprintf_plus(stderr, "The number of unchanged keys: %d\n",
 
332
                     import_result->unchanged);
 
333
        fprintf_plus(stderr, "The number of new user IDs: %d\n",
 
334
                     import_result->new_user_ids);
 
335
        fprintf_plus(stderr, "The number of new sub keys: %d\n",
 
336
                     import_result->new_sub_keys);
 
337
        fprintf_plus(stderr, "The number of new signatures: %d\n",
 
338
                     import_result->new_signatures);
 
339
        fprintf_plus(stderr, "The number of new revocations: %d\n",
 
340
                     import_result->new_revocations);
 
341
        fprintf_plus(stderr,
 
342
                     "The total number of secret keys read: %d\n",
 
343
                     import_result->secret_read);
 
344
        fprintf_plus(stderr,
 
345
                     "The number of imported secret keys: %d\n",
 
346
                     import_result->secret_imported);
 
347
        fprintf_plus(stderr,
 
348
                     "The number of unchanged secret keys: %d\n",
 
349
                     import_result->secret_unchanged);
 
350
        fprintf_plus(stderr, "The number of keys not imported: %d\n",
 
351
                     import_result->not_imported);
 
352
        for(gpgme_import_status_t import_status
 
353
              = import_result->imports;
 
354
            import_status != NULL;
 
355
            import_status = import_status->next){
 
356
          fprintf_plus(stderr, "Import status for key: %s\n",
 
357
                       import_status->fpr);
 
358
          if(import_status->result != GPG_ERR_NO_ERROR){
 
359
            fprintf_plus(stderr, "Import result: %s: %s\n",
 
360
                         gpgme_strsource(import_status->result),
 
361
                         gpgme_strerror(import_status->result));
 
362
          }
 
363
          fprintf_plus(stderr, "Key status:\n");
 
364
          fprintf_plus(stderr,
 
365
                       import_status->status & GPGME_IMPORT_NEW
 
366
                       ? "The key was new.\n"
 
367
                       : "The key was not new.\n");
 
368
          fprintf_plus(stderr,
 
369
                       import_status->status & GPGME_IMPORT_UID
 
370
                       ? "The key contained new user IDs.\n"
 
371
                       : "The key did not contain new user IDs.\n");
 
372
          fprintf_plus(stderr,
 
373
                       import_status->status & GPGME_IMPORT_SIG
 
374
                       ? "The key contained new signatures.\n"
 
375
                       : "The key did not contain new signatures.\n");
 
376
          fprintf_plus(stderr,
 
377
                       import_status->status & GPGME_IMPORT_SUBKEY
 
378
                       ? "The key contained new sub keys.\n"
 
379
                       : "The key did not contain new sub keys.\n");
 
380
          fprintf_plus(stderr,
 
381
                       import_status->status & GPGME_IMPORT_SECRET
 
382
                       ? "The key contained a secret key.\n"
 
383
                       : "The key did not contain a secret key.\n");
 
384
        }
 
385
        return false;
 
386
      }
 
387
    }
313
388
    
314
389
    ret = close(fd);
315
390
    if(ret == -1){
356
431
  /* Create new GPGME "context" */
357
432
  rc = gpgme_new(&(mc->ctx));
358
433
  if(rc != GPG_ERR_NO_ERROR){
359
 
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
360
 
                 "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
361
 
                 gpgme_strerror(rc));
 
434
    fprintf_plus(stderr, "bad gpgme_new: %s: %s\n",
 
435
                 gpgme_strsource(rc), gpgme_strerror(rc));
362
436
    return false;
363
437
  }
364
438
  
400
474
  /* Create new empty GPGME data buffer for the plaintext */
401
475
  rc = gpgme_data_new(&dh_plain);
402
476
  if(rc != GPG_ERR_NO_ERROR){
403
 
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
404
 
                 "bad gpgme_data_new: %s: %s\n",
 
477
    fprintf_plus(stderr, "bad gpgme_data_new: %s: %s\n",
405
478
                 gpgme_strsource(rc), gpgme_strerror(rc));
406
479
    gpgme_data_release(dh_crypto);
407
480
    return -1;
420
493
      if(result == NULL){
421
494
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
422
495
      } else {
423
 
        fprintf_plus(stderr, "Unsupported algorithm: %s\n",
424
 
                     result->unsupported_algorithm);
425
 
        fprintf_plus(stderr, "Wrong key usage: %u\n",
426
 
                     result->wrong_key_usage);
 
496
        if(result->unsupported_algorithm != NULL) {
 
497
          fprintf_plus(stderr, "Unsupported algorithm: %s\n",
 
498
                       result->unsupported_algorithm);
 
499
        }
 
500
        fprintf_plus(stderr, "Wrong key usage: %s\n",
 
501
                     result->wrong_key_usage ? "Yes" : "No");
427
502
        if(result->file_name != NULL){
428
503
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
429
504
        }
613
688
        }
614
689
        params.size += (unsigned int)bytes_read;
615
690
      }
 
691
      ret = close(dhpfile);
 
692
      if(ret == -1){
 
693
        perror_plus("close");
 
694
      }
616
695
      if(params.data == NULL){
617
696
        dhparamsfilename = NULL;
618
697
      }
1655
1734
      perror_plus("ioctl SIOCGIFFLAGS");
1656
1735
      errno = old_errno;
1657
1736
    }
 
1737
    if((close(s) == -1) and debug){
 
1738
      old_errno = errno;
 
1739
      perror_plus("close");
 
1740
      errno = old_errno;
 
1741
    }
1658
1742
    return false;
1659
1743
  }
 
1744
  if((close(s) == -1) and debug){
 
1745
    old_errno = errno;
 
1746
    perror_plus("close");
 
1747
    errno = old_errno;
 
1748
  }
1660
1749
  return true;
1661
1750
}
1662
1751
 
1923
2012
      return;
1924
2013
    }
1925
2014
  }
 
2015
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
2016
  if(devnull == -1){
 
2017
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
2018
    return;
 
2019
  }
1926
2020
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1927
2021
                           runnable_hook, alphasort);
1928
2022
  if(numhooks == -1){
1929
2023
    perror_plus("scandir");
 
2024
    close(devnull);
1930
2025
    return;
1931
2026
  }
1932
2027
  struct dirent *direntry;
1933
2028
  int ret;
1934
 
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
1935
 
  if(devnull == -1){
1936
 
    perror_plus("open(\"/dev/null\", O_RDONLY)");
1937
 
    return;
1938
 
  }
1939
2029
  for(int i = 0; i < numhooks; i++){
1940
2030
    direntry = direntries[i];
1941
2031
    if(debug){
3061
3151
                                                | O_PATH));
3062
3152
    if(dir_fd == -1){
3063
3153
      perror_plus("open");
 
3154
      return;
3064
3155
    }
3065
3156
    int numentries = scandirat(dir_fd, ".", &direntries,
3066
3157
                               notdotentries, alphasort);
3083
3174
            clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
3084
3175
            dret = 0;
3085
3176
          }
3086
 
          if(dret == -1){
 
3177
          if((dret == -1) and (errno != ENOENT)){
3087
3178
            fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
3088
3179
                         direntries[i]->d_name, strerror(errno));
3089
3180
          }
3093
3184
      
3094
3185
      /* need to clean even if 0 because man page doesn't specify */
3095
3186
      free(direntries);
3096
 
      if(numentries == -1){
3097
 
        perror_plus("scandirat");
3098
 
      }
3099
3187
      dret = unlinkat(base, dirname, AT_REMOVEDIR);
3100
3188
      if(dret == -1 and errno != ENOENT){
3101
3189
        perror_plus("rmdir");