9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2008-2015 Teddy Hogeborn
13
* Copyright © 2008-2015 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
12
* Copyright © 2008-2020 Teddy Hogeborn
13
* Copyright © 2008-2020 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
21
23
* WITHOUT ANY WARRANTY; without even the implied warranty of
22
24
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
25
* General Public License for more details.
25
27
* You should have received a copy of the GNU General Public License
26
* along with this program. If not, see
27
* <http://www.gnu.org/licenses/>.
28
* along with Mandos. If not, see <http://www.gnu.org/licenses/>.
29
30
* Contact the authors at <mandos@recompile.se>.
46
47
#include <stdlib.h> /* free(), EXIT_SUCCESS, srand(),
47
48
strtof(), abort() */
48
49
#include <stdbool.h> /* bool, false, true */
49
#include <string.h> /* memset(), strcmp(), strlen(),
50
strerror(), asprintf(), strcpy() */
50
#include <string.h> /* strcmp(), strlen(), strerror(),
51
asprintf(), strncpy(), strsignal()
51
53
#include <sys/ioctl.h> /* ioctl */
52
54
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
53
55
sockaddr_in6, PF_INET6,
57
59
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
58
60
inet_pton(), connect(),
60
#include <fcntl.h> /* open(), unlinkat() */
62
#include <fcntl.h> /* open(), unlinkat(), AT_REMOVEDIR */
61
63
#include <dirent.h> /* opendir(), struct dirent, readdir()
63
65
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
65
#include <errno.h> /* perror(), errno,
67
#include <errno.h> /* perror(), errno, EINTR, EINVAL,
68
EAI_SYSTEM, ENETUNREACH,
69
EHOSTUNREACH, ECONNREFUSED, EPROTO,
70
EIO, ENOENT, ENXIO, ENOMEM, EISDIR,
66
72
program_invocation_short_name */
67
73
#include <time.h> /* nanosleep(), time(), sleep() */
68
74
#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){
270
336
* 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
if(clock_settime(CLOCK_REALTIME, &keystat.st_mtim) != 0){
400
perror_plus("clock_settime");
402
ret = lower_privileges();
405
perror_plus("Failed to lower privileges");
294
409
rc = gpgme_data_new_from_fd(&pgp_data, fd);
295
410
if(rc != GPG_ERR_NO_ERROR){
296
411
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
304
419
gpgme_strsource(rc), gpgme_strerror(rc));
423
gpgme_import_result_t import_result
424
= gpgme_op_import_result(mc->ctx);
425
if((import_result->imported < 1
426
or import_result->not_imported > 0)
427
and import_result->unchanged == 0){
428
fprintf_plus(stderr, "bad gpgme_op_import_results:\n");
430
"The total number of considered keys: %d\n",
431
import_result->considered);
433
"The number of keys without user ID: %d\n",
434
import_result->no_user_id);
436
"The total number of imported keys: %d\n",
437
import_result->imported);
438
fprintf_plus(stderr, "The number of imported RSA keys: %d\n",
439
import_result->imported_rsa);
440
fprintf_plus(stderr, "The number of unchanged keys: %d\n",
441
import_result->unchanged);
442
fprintf_plus(stderr, "The number of new user IDs: %d\n",
443
import_result->new_user_ids);
444
fprintf_plus(stderr, "The number of new sub keys: %d\n",
445
import_result->new_sub_keys);
446
fprintf_plus(stderr, "The number of new signatures: %d\n",
447
import_result->new_signatures);
448
fprintf_plus(stderr, "The number of new revocations: %d\n",
449
import_result->new_revocations);
451
"The total number of secret keys read: %d\n",
452
import_result->secret_read);
454
"The number of imported secret keys: %d\n",
455
import_result->secret_imported);
457
"The number of unchanged secret keys: %d\n",
458
import_result->secret_unchanged);
459
fprintf_plus(stderr, "The number of keys not imported: %d\n",
460
import_result->not_imported);
461
for(gpgme_import_status_t import_status
462
= import_result->imports;
463
import_status != NULL;
464
import_status = import_status->next){
465
fprintf_plus(stderr, "Import status for key: %s\n",
467
if(import_status->result != GPG_ERR_NO_ERROR){
468
fprintf_plus(stderr, "Import result: %s: %s\n",
469
gpgme_strsource(import_status->result),
470
gpgme_strerror(import_status->result));
472
fprintf_plus(stderr, "Key status:\n");
474
import_status->status & GPGME_IMPORT_NEW
475
? "The key was new.\n"
476
: "The key was not new.\n");
478
import_status->status & GPGME_IMPORT_UID
479
? "The key contained new user IDs.\n"
480
: "The key did not contain new user IDs.\n");
482
import_status->status & GPGME_IMPORT_SIG
483
? "The key contained new signatures.\n"
484
: "The key did not contain new signatures.\n");
486
import_status->status & GPGME_IMPORT_SUBKEY
487
? "The key contained new sub keys.\n"
488
: "The key did not contain new sub keys.\n");
490
import_status->status & GPGME_IMPORT_SECRET
491
? "The key contained a secret key.\n"
492
: "The key did not contain a secret key.\n");
308
ret = (int)TEMP_FAILURE_RETRY(close(fd));
310
500
perror_plus("close");
414
602
if(result == NULL){
415
603
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
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);
605
if(result->unsupported_algorithm != NULL) {
606
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
607
result->unsupported_algorithm);
609
fprintf_plus(stderr, "Wrong key usage: %s\n",
610
result->wrong_key_usage ? "Yes" : "No");
421
611
if(result->file_name != NULL){
422
612
fprintf_plus(stderr, "File name: %s\n", result->file_name);
424
gpgme_recipient_t recipient;
425
recipient = result->recipients;
426
while(recipient != NULL){
615
for(gpgme_recipient_t r = result->recipients; r != NULL;
427
617
fprintf_plus(stderr, "Public key algorithm: %s\n",
428
gpgme_pubkey_algo_name
429
(recipient->pubkey_algo));
430
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
618
gpgme_pubkey_algo_name(r->pubkey_algo));
619
fprintf_plus(stderr, "Key ID: %s\n", r->keyid);
431
620
fprintf_plus(stderr, "Secret key available: %s\n",
432
recipient->status == GPG_ERR_NO_SECKEY
434
recipient = recipient->next;
621
r->status == GPG_ERR_NO_SECKEY ? "No" : "Yes");
513
700
fprintf_plus(stderr, "GnuTLS: %s", string);
516
__attribute__((nonnull, warn_unused_result))
703
__attribute__((nonnull(1, 2, 4), warn_unused_result))
517
704
static int init_gnutls_global(const char *pubkeyfilename,
518
705
const char *seckeyfilename,
519
706
const char *dhparamsfilename,
520
707
mandos_context *mc){
525
711
fprintf_plus(stderr, "Initializing GnuTLS\n");
528
ret = gnutls_global_init();
529
if(ret != GNUTLS_E_SUCCESS){
530
fprintf_plus(stderr, "GnuTLS global_init: %s\n",
531
safer_gnutls_strerror(ret));
536
715
/* "Use a log level over 10 to enable all debugging options."
537
716
* - GnuTLS manual
545
724
if(ret != GNUTLS_E_SUCCESS){
546
725
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
547
726
safer_gnutls_strerror(ret));
548
gnutls_global_deinit();
553
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
554
" secret key %s as GnuTLS credentials\n",
731
fprintf_plus(stderr, "Attempting to use public key %s and"
732
" private key %s as GnuTLS credentials\n",
737
#if GNUTLS_VERSION_NUMBER >= 0x030606
738
ret = gnutls_certificate_set_rawpk_key_file
739
(mc->cred, pubkeyfilename, seckeyfilename,
740
GNUTLS_X509_FMT_PEM, /* format */
743
GNUTLS_KEY_DIGITAL_SIGNATURE | GNUTLS_KEY_KEY_ENCIPHERMENT,
745
0, /* names_length */
747
GNUTLS_PKCS_PLAIN | GNUTLS_PKCS_NULL_PASSWORD,
748
0); /* pkcs11_flags */
749
#elif GNUTLS_VERSION_NUMBER < 0x030600
559
750
ret = gnutls_certificate_set_openpgp_key_file
560
751
(mc->cred, pubkeyfilename, seckeyfilename,
561
752
GNUTLS_OPENPGP_FMT_BASE64);
754
#error "Needs GnuTLS 3.6.6 or later, or before 3.6.0"
562
756
if(ret != GNUTLS_E_SUCCESS){
563
757
fprintf_plus(stderr,
564
"Error[%d] while reading the OpenPGP key pair ('%s',"
758
"Error[%d] while reading the key pair ('%s',"
565
759
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
566
760
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
567
761
safer_gnutls_strerror(ret));
736
936
safer_gnutls_strerror(ret));
740
fprintf_plus(stderr, "DH bits explicitly set to %u\n",
743
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
744
if(ret != GNUTLS_E_SUCCESS){
745
fprintf_plus(stderr, "Error in GnuTLS prime generation (%u"
746
" bits): %s\n", mc->dh_bits,
747
safer_gnutls_strerror(ret));
940
} else { /* dh_bits != 0 */
942
fprintf_plus(stderr, "DH bits explicitly set to %u\n",
945
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
946
if(ret != GNUTLS_E_SUCCESS){
947
fprintf_plus(stderr, "Error in GnuTLS prime generation (%u"
948
" bits): %s\n", mc->dh_bits,
949
safer_gnutls_strerror(ret));
952
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
751
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
757
960
gnutls_certificate_free_credentials(mc->cred);
758
gnutls_global_deinit();
759
961
gnutls_dh_params_deinit(mc->dh_params);
820
1029
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
821
1030
__attribute__((unused)) const char *txt){}
823
/* Set effective uid to 0, return errno */
824
__attribute__((warn_unused_result))
825
error_t raise_privileges(void){
826
error_t old_errno = errno;
827
error_t ret_errno = 0;
828
if(seteuid(0) == -1){
835
/* Set effective and real user ID to 0. Return errno. */
836
__attribute__((warn_unused_result))
837
error_t raise_privileges_permanently(void){
838
error_t old_errno = errno;
839
error_t ret_errno = raise_privileges();
851
/* Set effective user ID to unprivileged saved user ID */
852
__attribute__((warn_unused_result))
853
error_t lower_privileges(void){
854
error_t old_errno = errno;
855
error_t ret_errno = 0;
856
if(seteuid(uid) == -1){
863
/* Lower privileges permanently */
864
__attribute__((warn_unused_result))
865
error_t lower_privileges_permanently(void){
866
error_t old_errno = errno;
867
error_t ret_errno = 0;
868
if(setuid(uid) == -1){
875
1032
/* Helper function to add_local_route() and delete_local_route() */
876
1033
__attribute__((nonnull, warn_unused_result))
877
1034
static bool add_delete_local_route(const bool add,
1130
1296
goto mandos_end;
1133
memset(&to, 0, sizeof(to));
1134
1299
if(af == AF_INET6){
1135
((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
1136
ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
1300
struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&to;
1301
*to6 = (struct sockaddr_in6){ .sin6_family = (sa_family_t)af };
1302
ret = inet_pton(af, ip, &to6->sin6_addr);
1137
1303
} else { /* IPv4 */
1138
((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
1139
ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
1304
struct sockaddr_in *to4 = (struct sockaddr_in *)&to;
1305
*to4 = (struct sockaddr_in){ .sin_family = (sa_family_t)af };
1306
ret = inet_pton(af, ip, &to4->sin_addr);
1626
1803
__attribute__((nonnull, warn_unused_result))
1627
1804
bool get_flags(const char *ifname, struct ifreq *ifr){
1631
1808
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1634
1811
perror_plus("socket");
1638
strcpy(ifr->ifr_name, ifname);
1815
strncpy(ifr->ifr_name, ifname, IF_NAMESIZE);
1816
ifr->ifr_name[IF_NAMESIZE-1] = '\0'; /* NUL terminate */
1639
1817
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1643
1821
perror_plus("ioctl SIOCGIFFLAGS");
1824
if((close(s) == -1) and debug){
1826
perror_plus("close");
1831
if((close(s) == -1) and debug){
1833
perror_plus("close");
1915
#if __GLIBC_PREREQ(2, 15)
2102
int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
2104
perror_plus("open(\"/dev/null\", O_RDONLY)");
1916
2107
int numhooks = scandirat(hookdir_fd, ".", &direntries,
1917
2108
runnable_hook, alphasort);
1918
#else /* not __GLIBC_PREREQ(2, 15) */
1919
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1921
#endif /* not __GLIBC_PREREQ(2, 15) */
1922
#else /* not __GLIBC__ */
1923
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1925
#endif /* not __GLIBC__ */
1926
2109
if(numhooks == -1){
1927
2110
perror_plus("scandir");
1930
2114
struct dirent *direntry;
1932
int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
1934
perror_plus("open(\"/dev/null\", O_RDONLY)");
1937
2116
for(int i = 0; i < numhooks; i++){
1938
2117
direntry = direntries[i];
2287
2468
int main(int argc, char *argv[]){
2288
2469
mandos_context mc = { .server = NULL, .dh_bits = 0,
2289
.priority = "SECURE256:!CTYPE-X.509:"
2290
"+CTYPE-OPENPGP:!RSA", .current_server = NULL,
2291
.interfaces = NULL, .interfaces_size = 0 };
2470
#if GNUTLS_VERSION_NUMBER >= 0x030606
2471
.priority = "SECURE128:!CTYPE-X.509"
2472
":+CTYPE-RAWPK:!RSA:!VERS-ALL:+VERS-TLS1.3"
2474
#elif GNUTLS_VERSION_NUMBER < 0x030600
2475
.priority = "SECURE256:!CTYPE-X.509"
2476
":+CTYPE-OPENPGP:!RSA:+SIGN-DSA-SHA256",
2478
#error "Needs GnuTLS 3.6.6 or later, or before 3.6.0"
2480
.current_server = NULL, .interfaces = NULL,
2481
.interfaces_size = 0 };
2292
2482
AvahiSServiceBrowser *sb = NULL;
2293
2483
error_t ret_errno;
2356
2550
{ .name = "pubkey", .key = 'p',
2358
2552
.doc = "OpenPGP public key file base name",
2554
{ .name = "tls-privkey", .key = 't',
2556
#if GNUTLS_VERSION_NUMBER >= 0x030606
2557
.doc = "TLS private key file base name",
2559
.doc = "Dummy; ignored (requires GnuTLS 3.6.6)",
2562
{ .name = "tls-pubkey", .key = 'T',
2564
#if GNUTLS_VERSION_NUMBER >= 0x030606
2565
.doc = "TLS public key file base name",
2567
.doc = "Dummy; ignored (requires GnuTLS 3.6.6)",
2360
2570
{ .name = "dh-bits", .key = 129,
2362
2572
.doc = "Bit length of the prime number used in the"
2458
2678
argp_state_help(state, state->out_stream,
2459
2679
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
2460
2680
& ~(unsigned int)ARGP_HELP_EXIT_OK);
2681
__builtin_unreachable();
2461
2682
case -3: /* --usage */
2462
2683
argp_state_help(state, state->out_stream,
2463
2684
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
2685
__builtin_unreachable();
2464
2686
case 'V': /* --version */
2465
2687
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
2466
2688
exit(argp_err_exit_status);
2475
2697
.args_doc = "",
2476
2698
.doc = "Mandos client -- Get and decrypt"
2477
2699
" passwords from a Mandos server" };
2478
ret = argp_parse(&argp, argc, argv,
2479
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2700
ret_errno = argp_parse(&argp, argc, argv,
2701
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
2486
2708
perror_plus("argp_parse");
2487
2709
exitcode = EX_OSERR;
3002
#if GNUTLS_VERSION_NUMBER >= 0x030606
3003
ret = init_gnutls_global(tls_pubkey, tls_privkey, dh_params_file, &mc);
3004
#elif GNUTLS_VERSION_NUMBER < 0x030600
2779
3005
ret = init_gnutls_global(pubkey, seckey, dh_params_file, &mc);
3007
#error "Needs GnuTLS 3.6.6 or later, or before 3.6.0"
2781
3010
fprintf_plus(stderr, "init_gnutls_global failed\n");
2782
3011
exitcode = EX_UNAVAILABLE;
3039
3273
free(interfaces_to_take_down);
3040
3274
free(interfaces_hooks);
3276
void clean_dir_at(int base, const char * const dirname,
3278
struct dirent **direntries = NULL;
3280
int dir_fd = (int)TEMP_FAILURE_RETRY(openat(base, dirname,
3286
perror_plus("open");
3289
int numentries = scandirat(dir_fd, ".", &direntries,
3290
notdotentries, alphasort);
3291
if(numentries >= 0){
3292
for(int i = 0; i < numentries; i++){
3294
fprintf_plus(stderr, "Unlinking \"%s/%s\"\n",
3295
dirname, direntries[i]->d_name);
3297
dret = unlinkat(dir_fd, direntries[i]->d_name, 0);
3299
if(errno == EISDIR){
3300
dret = unlinkat(dir_fd, direntries[i]->d_name,
3303
if((dret == -1) and (errno == ENOTEMPTY)
3304
and (strcmp(direntries[i]->d_name, "private-keys-v1.d")
3305
== 0) and (level == 0)){
3306
/* Recurse only in this special case */
3307
clean_dir_at(dir_fd, direntries[i]->d_name, level+1);
3310
if((dret == -1) and (errno != ENOENT)){
3311
fprintf_plus(stderr, "unlink(\"%s/%s\"): %s\n", dirname,
3312
direntries[i]->d_name, strerror(errno));
3315
free(direntries[i]);
3318
/* need to clean even if 0 because man page doesn't specify */
3320
dret = unlinkat(base, dirname, AT_REMOVEDIR);
3321
if(dret == -1 and errno != ENOENT){
3322
perror_plus("rmdir");
3325
perror_plus("scandirat");
3042
3330
/* Removes the GPGME temp directory and all files inside */
3043
3331
if(tempdir != NULL){
3044
struct dirent **direntries = NULL;
3045
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
3049
if(tempdir_fd == -1){
3050
perror_plus("open");
3053
#if __GLIBC_PREREQ(2, 15)
3054
int numentries = scandirat(tempdir_fd, ".", &direntries,
3055
notdotentries, alphasort);
3056
#else /* not __GLIBC_PREREQ(2, 15) */
3057
int numentries = scandir(tempdir, &direntries, notdotentries,
3059
#endif /* not __GLIBC_PREREQ(2, 15) */
3060
#else /* not __GLIBC__ */
3061
int numentries = scandir(tempdir, &direntries, notdotentries,
3063
#endif /* not __GLIBC__ */
3064
if(numentries >= 0){
3065
for(int i = 0; i < numentries; i++){
3066
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
3068
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
3069
" \"%s\", 0): %s\n", tempdir,
3070
direntries[i]->d_name, strerror(errno));
3072
free(direntries[i]);
3075
/* need to clean even if 0 because man page doesn't specify */
3077
if(numentries == -1){
3078
perror_plus("scandir");
3080
ret = rmdir(tempdir);
3081
if(ret == -1 and errno != ENOENT){
3082
perror_plus("rmdir");
3085
TEMP_FAILURE_RETRY(close(tempdir_fd));
3332
clean_dir_at(-1, tempdir, 0);