9
9
 * "browse_callback", and parts of "main".
 
11
11
 * Everything else is
 
12
 
 * Copyright © 2008-2018 Teddy Hogeborn
 
13
 
 * Copyright © 2008-2018 Björn Påhlsson
 
15
 
 * This file is part of Mandos.
 
17
 
 * Mandos is free software: you can redistribute it and/or modify it
 
18
 
 * under the terms of the GNU General Public License as published by
 
19
 
 * the Free Software Foundation, either version 3 of the License, or
 
20
 
 * (at your option) any later version.
 
22
 
 * Mandos is distributed in the hope that it will be useful, but
 
 
12
 * Copyright © 2008-2016 Teddy Hogeborn
 
 
13
 * Copyright © 2008-2016 Björn Påhlsson
 
 
15
 * This program is free software: you can redistribute it and/or
 
 
16
 * modify it under the terms of the GNU General Public License as
 
 
17
 * published by the Free Software Foundation, either version 3 of the
 
 
18
 * License, or (at your option) any later version.
 
 
20
 * This program is distributed in the hope that it will be useful, but
 
23
21
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
24
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
25
23
 * General Public License for more details.
 
27
25
 * You should have received a copy of the GNU General Public License
 
28
 
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
 
26
 * along with this program.  If not, see
 
 
27
 * <http://www.gnu.org/licenses/>.
 
30
29
 * Contact the authors at <mandos@recompile.se>.
 
 
310
308
                   gpgme_strsource(rc), gpgme_strerror(rc));
 
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");
 
321
 
                     "The total number of considered keys: %d\n",
 
322
 
                     import_result->considered);
 
324
 
                     "The number of keys without user ID: %d\n",
 
325
 
                     import_result->no_user_id);
 
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);
 
342
 
                     "The total number of secret keys read: %d\n",
 
343
 
                     import_result->secret_read);
 
345
 
                     "The number of imported secret keys: %d\n",
 
346
 
                     import_result->secret_imported);
 
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",
 
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));
 
363
 
          fprintf_plus(stderr, "Key status:\n");
 
365
 
                       import_status->status & GPGME_IMPORT_NEW
 
366
 
                       ? "The key was new.\n"
 
367
 
                       : "The key was not new.\n");
 
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");
 
373
 
                       import_status->status & GPGME_IMPORT_SIG
 
374
 
                       ? "The key contained new signatures.\n"
 
375
 
                       : "The key did not contain new signatures.\n");
 
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");
 
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");
 
 
493
418
      if(result == NULL){
 
494
419
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
 
496
 
        if(result->unsupported_algorithm != NULL) {
 
497
 
          fprintf_plus(stderr, "Unsupported algorithm: %s\n",
 
498
 
                       result->unsupported_algorithm);
 
500
 
        fprintf_plus(stderr, "Wrong key usage: %s\n",
 
501
 
                     result->wrong_key_usage ? "Yes" : "No");
 
 
421
        fprintf_plus(stderr, "Unsupported algorithm: %s\n",
 
 
422
                     result->unsupported_algorithm);
 
 
423
        fprintf_plus(stderr, "Wrong key usage: %u\n",
 
 
424
                     result->wrong_key_usage);
 
502
425
        if(result->file_name != NULL){
 
503
426
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
 
506
 
        for(gpgme_recipient_t r = result->recipients; r != NULL;
 
 
428
        gpgme_recipient_t recipient;
 
 
429
        recipient = result->recipients;
 
 
430
        while(recipient != NULL){
 
508
431
          fprintf_plus(stderr, "Public key algorithm: %s\n",
 
509
 
                       gpgme_pubkey_algo_name(r->pubkey_algo));
 
510
 
          fprintf_plus(stderr, "Key ID: %s\n", r->keyid);
 
 
432
                       gpgme_pubkey_algo_name
 
 
433
                       (recipient->pubkey_algo));
 
 
434
          fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
 
511
435
          fprintf_plus(stderr, "Secret key available: %s\n",
 
512
 
                       r->status == GPG_ERR_NO_SECKEY ? "No" : "Yes");
 
 
436
                       recipient->status == GPG_ERR_NO_SECKEY
 
 
438
          recipient = recipient->next;
 
 
897
818
/* Set effective uid to 0, return errno */
 
898
819
__attribute__((warn_unused_result))
 
899
 
int raise_privileges(void){
 
900
 
  int old_errno = errno;
 
 
820
error_t raise_privileges(void){
 
 
821
  error_t old_errno = errno;
 
 
822
  error_t ret_errno = 0;
 
902
823
  if(seteuid(0) == -1){
 
905
826
  errno = old_errno;
 
909
830
/* Set effective and real user ID to 0.  Return errno. */
 
910
831
__attribute__((warn_unused_result))
 
911
 
int raise_privileges_permanently(void){
 
912
 
  int old_errno = errno;
 
913
 
  int ret = raise_privileges();
 
 
832
error_t raise_privileges_permanently(void){
 
 
833
  error_t old_errno = errno;
 
 
834
  error_t ret_errno = raise_privileges();
 
915
836
    errno = old_errno;
 
918
839
  if(setuid(0) == -1){
 
921
842
  errno = old_errno;
 
925
846
/* Set effective user ID to unprivileged saved user ID */
 
926
847
__attribute__((warn_unused_result))
 
927
 
int lower_privileges(void){
 
928
 
  int old_errno = errno;
 
 
848
error_t lower_privileges(void){
 
 
849
  error_t old_errno = errno;
 
 
850
  error_t ret_errno = 0;
 
930
851
  if(seteuid(uid) == -1){
 
933
854
  errno = old_errno;
 
937
858
/* Lower privileges permanently */
 
938
859
__attribute__((warn_unused_result))
 
939
 
int lower_privileges_permanently(void){
 
940
 
  int old_errno = errno;
 
 
860
error_t lower_privileges_permanently(void){
 
 
861
  error_t old_errno = errno;
 
 
862
  error_t ret_errno = 0;
 
942
863
  if(setuid(uid) == -1){
 
945
866
  errno = old_errno;
 
949
870
/* Helper function to add_local_route() and delete_local_route() */
 
 
2012
 
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
2014
 
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
2017
1912
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
 
2018
1913
                           runnable_hook, alphasort);
 
2019
1914
  if(numhooks == -1){
 
2020
1915
    perror_plus("scandir");
 
2024
1918
  struct dirent *direntry;
 
 
1920
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
 
1922
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
2026
1925
  for(int i = 0; i < numhooks; i++){
 
2027
1926
    direntry = direntries[i];
 
 
2567
2464
                         .args_doc = "",
 
2568
2465
                         .doc = "Mandos client -- Get and decrypt"
 
2569
2466
                         " passwords from a Mandos server" };
 
2570
 
    ret_errno = argp_parse(&argp, argc, argv,
 
2571
 
                           ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
 
2467
    ret = argp_parse(&argp, argc, argv,
 
 
2468
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
2578
2475
      perror_plus("argp_parse");
 
2579
2476
      exitcode = EX_OSERR;