9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2008-2019 Teddy Hogeborn
13
* Copyright © 2008-2019 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>.
59
57
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
60
58
inet_pton(), connect(),
62
#include <fcntl.h> /* open(), unlinkat(), AT_REMOVEDIR */
60
#include <fcntl.h> /* open(), unlinkat() */
63
61
#include <dirent.h> /* opendir(), struct dirent, readdir()
65
63
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
67
#include <errno.h> /* perror(), errno, EINTR, EINVAL,
68
EAI_SYSTEM, ENETUNREACH,
69
EHOSTUNREACH, ECONNREFUSED, EPROTO,
70
EIO, ENOENT, ENXIO, ENOMEM, EISDIR,
65
#include <errno.h> /* perror(), errno,
72
66
program_invocation_short_name */
73
67
#include <time.h> /* nanosleep(), time(), sleep() */
74
68
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
283
/* Set effective uid to 0, return errno */
284
__attribute__((warn_unused_result))
285
int raise_privileges(void){
286
int old_errno = errno;
288
if(seteuid(0) == -1){
295
/* Set effective and real user ID to 0. Return errno. */
296
__attribute__((warn_unused_result))
297
int raise_privileges_permanently(void){
298
int old_errno = errno;
299
int ret = raise_privileges();
311
/* Set effective user ID to unprivileged saved user ID */
312
__attribute__((warn_unused_result))
313
int lower_privileges(void){
314
int old_errno = errno;
316
if(seteuid(uid) == -1){
323
/* Lower privileges permanently */
324
__attribute__((warn_unused_result))
325
int lower_privileges_permanently(void){
326
int old_errno = errno;
328
if(setuid(uid) == -1){
336
270
* Initialize GPGME.
360
/* Workaround for systems without a real-time clock; see also
361
Debian bug #894495: <https://bugs.debian.org/894495> */
364
time_t currtime = time(NULL);
365
if(currtime != (time_t)-1){
367
if(gmtime_r(&currtime, &tm) == NULL) {
368
perror_plus("gmtime_r");
371
if(tm.tm_year != 70 or tm.tm_mon != 0){
375
fprintf_plus(stderr, "System clock is January 1970");
379
fprintf_plus(stderr, "System clock is invalid");
384
ret = fstat(fd, &keystat);
386
perror_plus("fstat");
389
ret = raise_privileges();
392
perror_plus("Failed to raise privileges");
397
"Setting system clock to key file mtime");
399
time_t keytime = keystat.st_mtim.tv_sec;
400
if(stime(&keytime) != 0){
401
perror_plus("stime");
403
ret = lower_privileges();
406
perror_plus("Failed to lower privileges");
410
294
rc = gpgme_data_new_from_fd(&pgp_data, fd);
411
295
if(rc != GPG_ERR_NO_ERROR){
412
296
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
420
304
gpgme_strsource(rc), gpgme_strerror(rc));
424
gpgme_import_result_t import_result
425
= gpgme_op_import_result(mc->ctx);
426
if((import_result->imported < 1
427
or import_result->not_imported > 0)
428
and import_result->unchanged == 0){
429
fprintf_plus(stderr, "bad gpgme_op_import_results:\n");
431
"The total number of considered keys: %d\n",
432
import_result->considered);
434
"The number of keys without user ID: %d\n",
435
import_result->no_user_id);
437
"The total number of imported keys: %d\n",
438
import_result->imported);
439
fprintf_plus(stderr, "The number of imported RSA keys: %d\n",
440
import_result->imported_rsa);
441
fprintf_plus(stderr, "The number of unchanged keys: %d\n",
442
import_result->unchanged);
443
fprintf_plus(stderr, "The number of new user IDs: %d\n",
444
import_result->new_user_ids);
445
fprintf_plus(stderr, "The number of new sub keys: %d\n",
446
import_result->new_sub_keys);
447
fprintf_plus(stderr, "The number of new signatures: %d\n",
448
import_result->new_signatures);
449
fprintf_plus(stderr, "The number of new revocations: %d\n",
450
import_result->new_revocations);
452
"The total number of secret keys read: %d\n",
453
import_result->secret_read);
455
"The number of imported secret keys: %d\n",
456
import_result->secret_imported);
458
"The number of unchanged secret keys: %d\n",
459
import_result->secret_unchanged);
460
fprintf_plus(stderr, "The number of keys not imported: %d\n",
461
import_result->not_imported);
462
for(gpgme_import_status_t import_status
463
= import_result->imports;
464
import_status != NULL;
465
import_status = import_status->next){
466
fprintf_plus(stderr, "Import status for key: %s\n",
468
if(import_status->result != GPG_ERR_NO_ERROR){
469
fprintf_plus(stderr, "Import result: %s: %s\n",
470
gpgme_strsource(import_status->result),
471
gpgme_strerror(import_status->result));
473
fprintf_plus(stderr, "Key status:\n");
475
import_status->status & GPGME_IMPORT_NEW
476
? "The key was new.\n"
477
: "The key was not new.\n");
479
import_status->status & GPGME_IMPORT_UID
480
? "The key contained new user IDs.\n"
481
: "The key did not contain new user IDs.\n");
483
import_status->status & GPGME_IMPORT_SIG
484
? "The key contained new signatures.\n"
485
: "The key did not contain new signatures.\n");
487
import_status->status & GPGME_IMPORT_SUBKEY
488
? "The key contained new sub keys.\n"
489
: "The key did not contain new sub keys.\n");
491
import_status->status & GPGME_IMPORT_SECRET
492
? "The key contained a secret key.\n"
493
: "The key did not contain a secret key.\n");
603
414
if(result == NULL){
604
415
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
606
if(result->unsupported_algorithm != NULL) {
607
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
608
result->unsupported_algorithm);
610
fprintf_plus(stderr, "Wrong key usage: %s\n",
611
result->wrong_key_usage ? "Yes" : "No");
417
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
418
result->unsupported_algorithm);
419
fprintf_plus(stderr, "Wrong key usage: %u\n",
420
result->wrong_key_usage);
612
421
if(result->file_name != NULL){
613
422
fprintf_plus(stderr, "File name: %s\n", result->file_name);
616
for(gpgme_recipient_t r = result->recipients; r != NULL;
424
gpgme_recipient_t recipient;
425
recipient = result->recipients;
426
while(recipient != NULL){
618
427
fprintf_plus(stderr, "Public key algorithm: %s\n",
619
gpgme_pubkey_algo_name(r->pubkey_algo));
620
fprintf_plus(stderr, "Key ID: %s\n", r->keyid);
428
gpgme_pubkey_algo_name
429
(recipient->pubkey_algo));
430
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
621
431
fprintf_plus(stderr, "Secret key available: %s\n",
622
r->status == GPG_ERR_NO_SECKEY ? "No" : "Yes");
432
recipient->status == GPG_ERR_NO_SECKEY
434
recipient = recipient->next;
732
fprintf_plus(stderr, "Attempting to use public key %s and"
733
" private key %s as GnuTLS credentials\n",
545
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
546
" secret key %s as GnuTLS credentials\n",
738
#if GNUTLS_VERSION_NUMBER >= 0x030606
739
ret = gnutls_certificate_set_rawpk_key_file
740
(mc->cred, pubkeyfilename, seckeyfilename,
741
GNUTLS_X509_FMT_PEM, /* format */
744
GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
746
0, /* names_length */
748
GNUTLS_PKCS_PLAIN | GNUTLS_PKCS_NULL_PASSWORD,
749
0); /* pkcs11_flags */
750
#elif GNUTLS_VERSION_NUMBER < 0x030600
751
551
ret = gnutls_certificate_set_openpgp_key_file
752
552
(mc->cred, pubkeyfilename, seckeyfilename,
753
553
GNUTLS_OPENPGP_FMT_BASE64);
755
#error "Needs GnuTLS 3.6.6 or later, or before 3.6.0"
757
554
if(ret != GNUTLS_E_SUCCESS){
758
555
fprintf_plus(stderr,
759
"Error[%d] while reading the key pair ('%s',"
556
"Error[%d] while reading the OpenPGP key pair ('%s',"
760
557
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
761
558
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
762
559
safer_gnutls_strerror(ret));
937
728
safer_gnutls_strerror(ret));
941
} else { /* dh_bits != 0 */
943
fprintf_plus(stderr, "DH bits explicitly set to %u\n",
946
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
947
if(ret != GNUTLS_E_SUCCESS){
948
fprintf_plus(stderr, "Error in GnuTLS prime generation (%u"
949
" bits): %s\n", mc->dh_bits,
950
safer_gnutls_strerror(ret));
953
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
732
fprintf_plus(stderr, "DH bits explicitly set to %u\n",
735
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
736
if(ret != GNUTLS_E_SUCCESS){
737
fprintf_plus(stderr, "Error in GnuTLS prime generation (%u"
738
" bits): %s\n", mc->dh_bits,
739
safer_gnutls_strerror(ret));
743
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
1030
811
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
1031
812
__attribute__((unused)) const char *txt){}
814
/* Set effective uid to 0, return errno */
815
__attribute__((warn_unused_result))
816
error_t raise_privileges(void){
817
error_t old_errno = errno;
818
error_t ret_errno = 0;
819
if(seteuid(0) == -1){
826
/* Set effective and real user ID to 0. Return errno. */
827
__attribute__((warn_unused_result))
828
error_t raise_privileges_permanently(void){
829
error_t old_errno = errno;
830
error_t ret_errno = raise_privileges();
842
/* Set effective user ID to unprivileged saved user ID */
843
__attribute__((warn_unused_result))
844
error_t lower_privileges(void){
845
error_t old_errno = errno;
846
error_t ret_errno = 0;
847
if(seteuid(uid) == -1){
854
/* Lower privileges permanently */
855
__attribute__((warn_unused_result))
856
error_t lower_privileges_permanently(void){
857
error_t old_errno = errno;
858
error_t ret_errno = 0;
859
if(setuid(uid) == -1){
1033
866
/* Helper function to add_local_route() and delete_local_route() */
1034
867
__attribute__((nonnull, warn_unused_result))
1035
868
static bool add_delete_local_route(const bool add,
2096
int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
2098
perror_plus("open(\"/dev/null\", O_RDONLY)");
2101
1908
int numhooks = scandirat(hookdir_fd, ".", &direntries,
2102
1909
runnable_hook, alphasort);
2103
1910
if(numhooks == -1){
2104
1911
perror_plus("scandir");
2108
1914
struct dirent *direntry;
1916
int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
1918
perror_plus("open(\"/dev/null\", O_RDONLY)");
2110
1921
for(int i = 0; i < numhooks; i++){
2111
1922
direntry = direntries[i];
2462
2271
int main(int argc, char *argv[]){
2463
2272
mandos_context mc = { .server = NULL, .dh_bits = 0,
2464
#if GNUTLS_VERSION_NUMBER >= 0x030606
2465
.priority = "SECURE128:!CTYPE-X.509"
2466
":+CTYPE-RAWPK:!RSA:!VERS-ALL:+VERS-TLS1.3"
2468
#elif GNUTLS_VERSION_NUMBER < 0x030600
2469
2273
.priority = "SECURE256:!CTYPE-X.509"
2470
2274
":+CTYPE-OPENPGP:!RSA:+SIGN-DSA-SHA256",
2472
#error "Needs GnuTLS 3.6.6 or later, or before 3.6.0"
2474
2275
.current_server = NULL, .interfaces = NULL,
2475
2276
.interfaces_size = 0 };
2476
2277
AvahiSServiceBrowser *sb = NULL;
2544
2341
{ .name = "pubkey", .key = 'p',
2546
2343
.doc = "OpenPGP public key file base name",
2548
{ .name = "tls-privkey", .key = 't',
2550
#if GNUTLS_VERSION_NUMBER >= 0x030606
2551
.doc = "TLS private key file base name",
2553
.doc = "Dummy; ignored (requires GnuTLS 3.6.6)",
2556
{ .name = "tls-pubkey", .key = 'T',
2558
#if GNUTLS_VERSION_NUMBER >= 0x030606
2559
.doc = "TLS public key file base name",
2561
.doc = "Dummy; ignored (requires GnuTLS 3.6.6)",
2564
2345
{ .name = "dh-bits", .key = 129,
2566
2347
.doc = "Bit length of the prime number used in the"
2672
2443
argp_state_help(state, state->out_stream,
2673
2444
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
2674
2445
& ~(unsigned int)ARGP_HELP_EXIT_OK);
2675
__builtin_unreachable();
2676
2446
case -3: /* --usage */
2677
2447
argp_state_help(state, state->out_stream,
2678
2448
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
2679
__builtin_unreachable();
2680
2449
case 'V': /* --version */
2681
2450
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
2682
2451
exit(argp_err_exit_status);
2691
2460
.args_doc = "",
2692
2461
.doc = "Mandos client -- Get and decrypt"
2693
2462
" passwords from a Mandos server" };
2694
ret_errno = argp_parse(&argp, argc, argv,
2695
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2463
ret = argp_parse(&argp, argc, argv,
2464
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2702
2471
perror_plus("argp_parse");
2703
2472
exitcode = EX_OSERR;
2996
#if GNUTLS_VERSION_NUMBER >= 0x030606
2997
ret = init_gnutls_global(tls_pubkey, tls_privkey, dh_params_file, &mc);
2998
#elif GNUTLS_VERSION_NUMBER < 0x030600
2999
2765
ret = init_gnutls_global(pubkey, seckey, dh_params_file, &mc);
3001
#error "Needs GnuTLS 3.6.6 or later, or before 3.6.0"
3004
2767
fprintf_plus(stderr, "init_gnutls_global failed\n");
3005
2768
exitcode = EX_UNAVAILABLE;
3267
3024
free(interfaces_to_take_down);
3268
3025
free(interfaces_hooks);
3270
void clean_dir_at(int base, const char * const dirname,
3272
struct dirent **direntries = NULL;
3274
int dir_fd = (int)TEMP_FAILURE_RETRY(openat(base, dirname,
3280
perror_plus("open");
3283
int numentries = scandirat(dir_fd, ".", &direntries,
3284
notdotentries, alphasort);
3285
if(numentries >= 0){
3286
for(int i = 0; i < numentries; i++){
3288
fprintf_plus(stderr, "Unlinking \"%s/%s\"\n",
3289
dirname, direntries[i]->d_name);
3291
dret = unlinkat(dir_fd, direntries[i]->d_name, 0);
3293
if(errno == EISDIR){
3294
dret = unlinkat(dir_fd, direntries[i]->d_name,
3297
if((dret == -1) and (errno == ENOTEMPTY)
3298
and (strcmp(direntries[i]->d_name, "private-keys-v1.d")
3299
== 0) and (level == 0)){
3300
/* Recurse only in this special case */
3301
clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
3304
if((dret == -1) and (errno != ENOENT)){
3305
fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
3306
direntries[i]->d_name, strerror(errno));
3309
free(direntries[i]);
3312
/* need to clean even if 0 because man page doesn't specify */
3314
dret = unlinkat(base, dirname, AT_REMOVEDIR);
3315
if(dret == -1 and errno != ENOENT){
3316
perror_plus("rmdir");
3319
perror_plus("scandirat");
3324
3027
/* Removes the GPGME temp directory and all files inside */
3325
3028
if(tempdir != NULL){
3326
clean_dir_at(-1, tempdir, 0);
3029
struct dirent **direntries = NULL;
3030
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
3034
if(tempdir_fd == -1){
3035
perror_plus("open");
3037
int numentries = scandirat(tempdir_fd, ".", &direntries,
3038
notdotentries, alphasort);
3039
if(numentries >= 0){
3040
for(int i = 0; i < numentries; i++){
3041
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
3043
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
3044
" \"%s\", 0): %s\n", tempdir,
3045
direntries[i]->d_name, strerror(errno));
3047
free(direntries[i]);
3050
/* need to clean even if 0 because man page doesn't specify */
3052
if(numentries == -1){
3053
perror_plus("scandir");
3055
ret = rmdir(tempdir);
3056
if(ret == -1 and errno != ENOENT){
3057
perror_plus("rmdir");