/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: 2017-02-22 21:45:35 UTC
  • Revision ID: teddy@recompile.se-20170222214535-milf6w0b1krwpaex
Server bug fix: Use the mandos.conf "zeroconf" and "restore" options

Allow the "zeroconf" and "restore" options to actually work from the
mandos.conf file, in addition to command line options.

Closes: 855589
Reported-by: Pablo Abelenda <pabelenda@igalia.com>
Suggested-by: Pablo Abelenda <pabelenda@igalia.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2018 Teddy Hogeborn
13
 
 * Copyright © 2008-2018 Björn Påhlsson
14
 
 * 
15
 
 * This file is part of Mandos.
16
 
 * 
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.
21
 
 * 
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
 
14
 * 
 
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.
 
19
 * 
 
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.
26
24
 * 
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/>.
29
28
 * 
30
29
 * Contact the authors at <mandos@recompile.se>.
31
30
 */
272
271
  return true;
273
272
}
274
273
 
275
 
/* Set effective uid to 0, return errno */
276
 
__attribute__((warn_unused_result))
277
 
int raise_privileges(void){
278
 
  int old_errno = errno;
279
 
  int ret = 0;
280
 
  if(seteuid(0) == -1){
281
 
    ret = errno;
282
 
  }
283
 
  errno = old_errno;
284
 
  return ret;
285
 
}
286
 
 
287
 
/* Set effective and real user ID to 0.  Return errno. */
288
 
__attribute__((warn_unused_result))
289
 
int raise_privileges_permanently(void){
290
 
  int old_errno = errno;
291
 
  int ret = raise_privileges();
292
 
  if(ret != 0){
293
 
    errno = old_errno;
294
 
    return ret;
295
 
  }
296
 
  if(setuid(0) == -1){
297
 
    ret = errno;
298
 
  }
299
 
  errno = old_errno;
300
 
  return ret;
301
 
}
302
 
 
303
 
/* Set effective user ID to unprivileged saved user ID */
304
 
__attribute__((warn_unused_result))
305
 
int lower_privileges(void){
306
 
  int old_errno = errno;
307
 
  int ret = 0;
308
 
  if(seteuid(uid) == -1){
309
 
    ret = errno;
310
 
  }
311
 
  errno = old_errno;
312
 
  return ret;
313
 
}
314
 
 
315
 
/* Lower privileges permanently */
316
 
__attribute__((warn_unused_result))
317
 
int lower_privileges_permanently(void){
318
 
  int old_errno = errno;
319
 
  int ret = 0;
320
 
  if(setuid(uid) == -1){
321
 
    ret = errno;
322
 
  }
323
 
  errno = old_errno;
324
 
  return ret;
325
 
}
326
 
 
327
274
/* 
328
275
 * Initialize GPGME.
329
276
 */
349
296
      return false;
350
297
    }
351
298
    
352
 
    /* Workaround for systems without a real-time clock; see also
353
 
       Debian bug #894495: <https://bugs.debian.org/894495> */
354
 
    do {
355
 
      {
356
 
        time_t currtime = time(NULL);
357
 
        if(currtime != (time_t)-1){
358
 
          struct tm tm;
359
 
          if(gmtime_r(&currtime, &tm) == NULL) {
360
 
            perror_plus("gmtime_r");
361
 
            break;
362
 
          }
363
 
          if(tm.tm_year != 70 or tm.tm_mon != 0){
364
 
            break;
365
 
          }
366
 
          if(debug){
367
 
            fprintf_plus(stderr, "System clock is January 1970");
368
 
          }
369
 
        } else {
370
 
          if(debug){
371
 
            fprintf_plus(stderr, "System clock is invalid");
372
 
          }
373
 
        }
374
 
      }
375
 
      struct stat keystat;
376
 
      ret = fstat(fd, &keystat);
377
 
      if(ret != 0){
378
 
        perror_plus("fstat");
379
 
        break;
380
 
      }
381
 
      ret = raise_privileges();
382
 
      if(ret != 0){
383
 
        errno = ret;
384
 
        perror_plus("Failed to raise privileges");
385
 
        break;
386
 
      }
387
 
      if(debug){
388
 
        fprintf_plus(stderr,
389
 
                     "Setting system clock to key file mtime");
390
 
      }
391
 
      time_t keytime = keystat.st_mtim.tv_sec;
392
 
      if(stime(&keytime) != 0){
393
 
        perror_plus("stime");
394
 
      }
395
 
      ret = lower_privileges();
396
 
      if(ret != 0){
397
 
        errno = ret;
398
 
        perror_plus("Failed to lower privileges");
399
 
      }
400
 
    } while(false);
401
 
 
402
299
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
403
300
    if(rc != GPG_ERR_NO_ERROR){
404
301
      fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
412
309
                   gpgme_strsource(rc), gpgme_strerror(rc));
413
310
      return false;
414
311
    }
415
 
    {
416
 
      gpgme_import_result_t import_result
417
 
        = gpgme_op_import_result(mc->ctx);
418
 
      if((import_result->imported < 1
419
 
          or import_result->not_imported > 0)
420
 
         and import_result->unchanged == 0){
421
 
        fprintf_plus(stderr, "bad gpgme_op_import_results:\n");
422
 
        fprintf_plus(stderr,
423
 
                     "The total number of considered keys: %d\n",
424
 
                     import_result->considered);
425
 
        fprintf_plus(stderr,
426
 
                     "The number of keys without user ID: %d\n",
427
 
                     import_result->no_user_id);
428
 
        fprintf_plus(stderr,
429
 
                     "The total number of imported keys: %d\n",
430
 
                     import_result->imported);
431
 
        fprintf_plus(stderr, "The number of imported RSA keys: %d\n",
432
 
                     import_result->imported_rsa);
433
 
        fprintf_plus(stderr, "The number of unchanged keys: %d\n",
434
 
                     import_result->unchanged);
435
 
        fprintf_plus(stderr, "The number of new user IDs: %d\n",
436
 
                     import_result->new_user_ids);
437
 
        fprintf_plus(stderr, "The number of new sub keys: %d\n",
438
 
                     import_result->new_sub_keys);
439
 
        fprintf_plus(stderr, "The number of new signatures: %d\n",
440
 
                     import_result->new_signatures);
441
 
        fprintf_plus(stderr, "The number of new revocations: %d\n",
442
 
                     import_result->new_revocations);
443
 
        fprintf_plus(stderr,
444
 
                     "The total number of secret keys read: %d\n",
445
 
                     import_result->secret_read);
446
 
        fprintf_plus(stderr,
447
 
                     "The number of imported secret keys: %d\n",
448
 
                     import_result->secret_imported);
449
 
        fprintf_plus(stderr,
450
 
                     "The number of unchanged secret keys: %d\n",
451
 
                     import_result->secret_unchanged);
452
 
        fprintf_plus(stderr, "The number of keys not imported: %d\n",
453
 
                     import_result->not_imported);
454
 
        for(gpgme_import_status_t import_status
455
 
              = import_result->imports;
456
 
            import_status != NULL;
457
 
            import_status = import_status->next){
458
 
          fprintf_plus(stderr, "Import status for key: %s\n",
459
 
                       import_status->fpr);
460
 
          if(import_status->result != GPG_ERR_NO_ERROR){
461
 
            fprintf_plus(stderr, "Import result: %s: %s\n",
462
 
                         gpgme_strsource(import_status->result),
463
 
                         gpgme_strerror(import_status->result));
464
 
          }
465
 
          fprintf_plus(stderr, "Key status:\n");
466
 
          fprintf_plus(stderr,
467
 
                       import_status->status & GPGME_IMPORT_NEW
468
 
                       ? "The key was new.\n"
469
 
                       : "The key was not new.\n");
470
 
          fprintf_plus(stderr,
471
 
                       import_status->status & GPGME_IMPORT_UID
472
 
                       ? "The key contained new user IDs.\n"
473
 
                       : "The key did not contain new user IDs.\n");
474
 
          fprintf_plus(stderr,
475
 
                       import_status->status & GPGME_IMPORT_SIG
476
 
                       ? "The key contained new signatures.\n"
477
 
                       : "The key did not contain new signatures.\n");
478
 
          fprintf_plus(stderr,
479
 
                       import_status->status & GPGME_IMPORT_SUBKEY
480
 
                       ? "The key contained new sub keys.\n"
481
 
                       : "The key did not contain new sub keys.\n");
482
 
          fprintf_plus(stderr,
483
 
                       import_status->status & GPGME_IMPORT_SECRET
484
 
                       ? "The key contained a secret key.\n"
485
 
                       : "The key did not contain a secret key.\n");
486
 
        }
487
 
        return false;
488
 
      }
489
 
    }
490
312
    
491
313
    ret = close(fd);
492
314
    if(ret == -1){
533
355
  /* Create new GPGME "context" */
534
356
  rc = gpgme_new(&(mc->ctx));
535
357
  if(rc != GPG_ERR_NO_ERROR){
536
 
    fprintf_plus(stderr, "bad gpgme_new: %s: %s\n",
537
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
358
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
 
359
                 "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
360
                 gpgme_strerror(rc));
538
361
    return false;
539
362
  }
540
363
  
576
399
  /* Create new empty GPGME data buffer for the plaintext */
577
400
  rc = gpgme_data_new(&dh_plain);
578
401
  if(rc != GPG_ERR_NO_ERROR){
579
 
    fprintf_plus(stderr, "bad gpgme_data_new: %s: %s\n",
 
402
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
 
403
                 "bad gpgme_data_new: %s: %s\n",
580
404
                 gpgme_strsource(rc), gpgme_strerror(rc));
581
405
    gpgme_data_release(dh_crypto);
582
406
    return -1;
595
419
      if(result == NULL){
596
420
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
597
421
      } else {
598
 
        if(result->unsupported_algorithm != NULL) {
599
 
          fprintf_plus(stderr, "Unsupported algorithm: %s\n",
600
 
                       result->unsupported_algorithm);
601
 
        }
602
 
        fprintf_plus(stderr, "Wrong key usage: %s\n",
603
 
                     result->wrong_key_usage ? "Yes" : "No");
 
422
        fprintf_plus(stderr, "Unsupported algorithm: %s\n",
 
423
                     result->unsupported_algorithm);
 
424
        fprintf_plus(stderr, "Wrong key usage: %u\n",
 
425
                     result->wrong_key_usage);
604
426
        if(result->file_name != NULL){
605
427
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
606
428
        }
607
 
 
608
 
        for(gpgme_recipient_t r = result->recipients; r != NULL;
609
 
            r = r->next){
 
429
        gpgme_recipient_t recipient;
 
430
        recipient = result->recipients;
 
431
        while(recipient != NULL){
610
432
          fprintf_plus(stderr, "Public key algorithm: %s\n",
611
 
                       gpgme_pubkey_algo_name(r->pubkey_algo));
612
 
          fprintf_plus(stderr, "Key ID: %s\n", r->keyid);
 
433
                       gpgme_pubkey_algo_name
 
434
                       (recipient->pubkey_algo));
 
435
          fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
613
436
          fprintf_plus(stderr, "Secret key available: %s\n",
614
 
                       r->status == GPG_ERR_NO_SECKEY ? "No" : "Yes");
 
437
                       recipient->status == GPG_ERR_NO_SECKEY
 
438
                       ? "No" : "Yes");
 
439
          recipient = recipient->next;
615
440
        }
616
441
      }
617
442
    }
787
612
        }
788
613
        params.size += (unsigned int)bytes_read;
789
614
      }
790
 
      ret = close(dhpfile);
791
 
      if(ret == -1){
792
 
        perror_plus("close");
793
 
      }
794
615
      if(params.data == NULL){
795
616
        dhparamsfilename = NULL;
796
617
      }
996
817
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
997
818
                      __attribute__((unused)) const char *txt){}
998
819
 
 
820
/* Set effective uid to 0, return errno */
 
821
__attribute__((warn_unused_result))
 
822
int raise_privileges(void){
 
823
  int old_errno = errno;
 
824
  int ret = 0;
 
825
  if(seteuid(0) == -1){
 
826
    ret = errno;
 
827
  }
 
828
  errno = old_errno;
 
829
  return ret;
 
830
}
 
831
 
 
832
/* Set effective and real user ID to 0.  Return errno. */
 
833
__attribute__((warn_unused_result))
 
834
int raise_privileges_permanently(void){
 
835
  int old_errno = errno;
 
836
  int ret = raise_privileges();
 
837
  if(ret != 0){
 
838
    errno = old_errno;
 
839
    return ret;
 
840
  }
 
841
  if(setuid(0) == -1){
 
842
    ret = errno;
 
843
  }
 
844
  errno = old_errno;
 
845
  return ret;
 
846
}
 
847
 
 
848
/* Set effective user ID to unprivileged saved user ID */
 
849
__attribute__((warn_unused_result))
 
850
int lower_privileges(void){
 
851
  int old_errno = errno;
 
852
  int ret = 0;
 
853
  if(seteuid(uid) == -1){
 
854
    ret = errno;
 
855
  }
 
856
  errno = old_errno;
 
857
  return ret;
 
858
}
 
859
 
 
860
/* Lower privileges permanently */
 
861
__attribute__((warn_unused_result))
 
862
int lower_privileges_permanently(void){
 
863
  int old_errno = errno;
 
864
  int ret = 0;
 
865
  if(setuid(uid) == -1){
 
866
    ret = errno;
 
867
  }
 
868
  errno = old_errno;
 
869
  return ret;
 
870
}
 
871
 
999
872
/* Helper function to add_local_route() and delete_local_route() */
1000
873
__attribute__((nonnull, warn_unused_result))
1001
874
static bool add_delete_local_route(const bool add,
1781
1654
      perror_plus("ioctl SIOCGIFFLAGS");
1782
1655
      errno = old_errno;
1783
1656
    }
1784
 
    if((close(s) == -1) and debug){
1785
 
      old_errno = errno;
1786
 
      perror_plus("close");
1787
 
      errno = old_errno;
1788
 
    }
1789
1657
    return false;
1790
1658
  }
1791
 
  if((close(s) == -1) and debug){
1792
 
    old_errno = errno;
1793
 
    perror_plus("close");
1794
 
    errno = old_errno;
1795
 
  }
1796
1659
  return true;
1797
1660
}
1798
1661
 
2059
1922
      return;
2060
1923
    }
2061
1924
  }
2062
 
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
2063
 
  if(devnull == -1){
2064
 
    perror_plus("open(\"/dev/null\", O_RDONLY)");
2065
 
    return;
2066
 
  }
2067
1925
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
2068
1926
                           runnable_hook, alphasort);
2069
1927
  if(numhooks == -1){
2070
1928
    perror_plus("scandir");
2071
 
    close(devnull);
2072
1929
    return;
2073
1930
  }
2074
1931
  struct dirent *direntry;
2075
1932
  int ret;
 
1933
  int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
 
1934
  if(devnull == -1){
 
1935
    perror_plus("open(\"/dev/null\", O_RDONLY)");
 
1936
    return;
 
1937
  }
2076
1938
  for(int i = 0; i < numhooks; i++){
2077
1939
    direntry = direntries[i];
2078
1940
    if(debug){
3198
3060
                                                | O_PATH));
3199
3061
    if(dir_fd == -1){
3200
3062
      perror_plus("open");
3201
 
      return;
3202
3063
    }
3203
3064
    int numentries = scandirat(dir_fd, ".", &direntries,
3204
3065
                               notdotentries, alphasort);
3221
3082
            clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
3222
3083
            dret = 0;
3223
3084
          }
3224
 
          if((dret == -1) and (errno != ENOENT)){
 
3085
          if(dret == -1){
3225
3086
            fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
3226
3087
                         direntries[i]->d_name, strerror(errno));
3227
3088
          }
3231
3092
      
3232
3093
      /* need to clean even if 0 because man page doesn't specify */
3233
3094
      free(direntries);
 
3095
      if(numentries == -1){
 
3096
        perror_plus("scandirat");
 
3097
      }
3234
3098
      dret = unlinkat(base, dirname, AT_REMOVEDIR);
3235
3099
      if(dret == -1 and errno != ENOENT){
3236
3100
        perror_plus("rmdir");