32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
#ifndef _LARGEFILE_SOURCE
33
34
#define _LARGEFILE_SOURCE
36
#ifndef _FILE_OFFSET_BITS
34
37
#define _FILE_OFFSET_BITS 64
36
40
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
38
42
#include <stdio.h> /* fprintf(), stderr, fwrite(),
39
stdout, ferror(), sscanf(),
43
stdout, ferror(), remove() */
41
44
#include <stdint.h> /* uint16_t, uint32_t */
42
45
#include <stddef.h> /* NULL, size_t, ssize_t */
43
46
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
srand(), strtof(), abort() */
45
48
#include <stdbool.h> /* bool, false, true */
46
49
#include <string.h> /* memset(), strcmp(), strlen(),
47
50
strerror(), asprintf(), strcpy() */
56
59
#include <fcntl.h> /* open() */
57
60
#include <dirent.h> /* opendir(), struct dirent, readdir()
59
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
60
64
#include <assert.h> /* assert() */
61
65
#include <errno.h> /* perror(), errno */
62
66
#include <time.h> /* nanosleep(), time() */
67
71
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
69
73
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
70
getuid(), getgid(), setuid(),
74
getuid(), getgid(), seteuid(),
72
76
#include <arpa/inet.h> /* inet_pton(), htons */
73
77
#include <iso646.h> /* not, or, and */
74
78
#include <argp.h> /* struct argp_option, error_t, struct
76
80
argp_parse(), ARGP_KEY_ARG,
77
81
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
82
#include <signal.h> /* sigemptyset(), sigaddset(),
79
sigaction(), SIGTERM, sigaction,
83
sigaction(), SIGTERM, sig_atomic_t,
83
87
#include <sys/klog.h> /* klogctl() */
306
315
gpgme_recipient_t recipient;
307
316
recipient = result->recipients;
309
while(recipient != NULL){
310
fprintf(stderr, "Public key algorithm: %s\n",
311
gpgme_pubkey_algo_name(recipient->pubkey_algo));
312
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
313
fprintf(stderr, "Secret key available: %s\n",
314
recipient->status == GPG_ERR_NO_SECKEY
316
recipient = recipient->next;
317
while(recipient != NULL){
318
fprintf(stderr, "Public key algorithm: %s\n",
319
gpgme_pubkey_algo_name(recipient->pubkey_algo));
320
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
321
fprintf(stderr, "Secret key available: %s\n",
322
recipient->status == GPG_ERR_NO_SECKEY
324
recipient = recipient->next;
472
479
static int init_gnutls_session(gnutls_session_t *session){
474
481
/* GnuTLS session creation */
475
ret = gnutls_init(session, GNUTLS_SERVER);
483
ret = gnutls_init(session, GNUTLS_SERVER);
487
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
476
488
if(ret != GNUTLS_E_SUCCESS){
477
489
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
478
490
safer_gnutls_strerror(ret));
483
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
496
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
498
gnutls_deinit(*session);
501
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
484
502
if(ret != GNUTLS_E_SUCCESS){
485
503
fprintf(stderr, "Syntax error at: %s\n", err);
486
504
fprintf(stderr, "GnuTLS error: %s\n",
493
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
512
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
515
gnutls_deinit(*session);
518
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
495
519
if(ret != GNUTLS_E_SUCCESS){
496
520
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
497
521
safer_gnutls_strerror(ret));
516
539
static int start_mandos_communication(const char *ip, uint16_t port,
517
540
AvahiIfIndex if_index,
542
int ret, tcp_sd = -1;
522
545
struct sockaddr_in in;
523
546
struct sockaddr_in6 in6;
525
548
char *buffer = NULL;
526
char *decrypted_buffer;
549
char *decrypted_buffer = NULL;
527
550
size_t buffer_length = 0;
528
551
size_t buffer_capacity = 0;
529
ssize_t decrypted_buffer_size;
532
554
gnutls_session_t session;
533
555
int pf; /* Protocol family */
557
583
tcp_sd = socket(pf, SOCK_STREAM, 0);
559
585
perror("socket");
563
593
memset(&to, 0, sizeof(to));
564
594
if(af == AF_INET6){
565
to.in6.sin6_family = (uint16_t)af;
595
to.in6.sin6_family = (sa_family_t)af;
566
596
ret = inet_pton(af, ip, &to.in6.sin6_addr);
567
597
} else { /* IPv4 */
568
598
to.in.sin_family = (sa_family_t)af;
710
773
case GNUTLS_E_AGAIN:
712
775
case GNUTLS_E_REHANDSHAKE:
714
777
ret = gnutls_handshake(session);
715
782
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
717
784
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
718
785
gnutls_perror(ret);
724
790
fprintf(stderr, "Unknown error while reading data from"
725
791
" encrypted session with Mandos server\n");
727
792
gnutls_bye(session, GNUTLS_SHUT_RDWR);
736
801
fprintf(stderr, "Closing TLS session\n");
739
gnutls_bye(session, GNUTLS_SHUT_RDWR);
809
ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
813
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
741
815
if(buffer_length > 0){
816
ssize_t decrypted_buffer_size;
742
817
decrypted_buffer_size = pgp_packet_decrypt(buffer,
744
819
&decrypted_buffer);
745
820
if(decrypted_buffer_size >= 0){
747
823
while(written < (size_t) decrypted_buffer_size){
748
828
ret = (int)fwrite(decrypted_buffer + written, 1,
749
829
(size_t)decrypted_buffer_size - written,
854
943
the callback function is called the Avahi server will free the
855
944
resolver for us. */
857
if(!(avahi_s_service_resolver_new(mc.server, interface,
858
protocol, name, type, domain,
859
AVAHI_PROTO_INET6, 0,
860
resolve_callback, NULL)))
946
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
947
name, type, domain, protocol, 0,
948
resolve_callback, NULL) == NULL)
861
949
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
862
950
name, avahi_strerror(avahi_server_errno(mc.server)));
889
976
errno = old_errno;
980
* This function determines if a directory entry in /sys/class/net
981
* corresponds to an acceptable network device.
982
* (This function is passed to scandir(3) as a filter function.)
984
int good_interface(const struct dirent *if_entry){
986
char *flagname = NULL;
987
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
993
if(if_entry->d_name[0] == '.'){
996
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1001
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1002
/* read line from flags_fd */
1003
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1004
char *flagstring = malloc((size_t)to_read);
1005
if(flagstring == NULL){
1011
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1028
tmpmax = strtoimax(flagstring, &tmp, 0);
1029
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1030
and not (isspace(*tmp)))
1031
or tmpmax != (ifreq_flags)tmpmax){
1036
ifreq_flags flags = (ifreq_flags)tmpmax;
1037
/* Reject the loopback device */
1038
if(flags & IFF_LOOPBACK){
1041
/* Accept point-to-point devices only if connect_to is specified */
1042
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1045
/* Otherwise, reject non-broadcast-capable devices */
1046
if(not (flags & IFF_BROADCAST)){
1049
/* Accept this device */
892
1053
int main(int argc, char *argv[]){
893
1054
AvahiSServiceBrowser *sb = NULL;
896
1057
intmax_t tmpmax;
898
1059
int exitcode = EXIT_SUCCESS;
899
const char *interface = "eth0";
1060
const char *interface = "";
900
1061
struct ifreq network;
1063
bool take_down_interface = false;
904
char *connect_to = NULL;
905
1066
char tempdir[] = "/tmp/mandosXXXXXX";
906
1067
bool tempdir_created = false;
907
1068
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
908
1069
const char *seckey = PATHDIR "/" SECKEY;
909
1070
const char *pubkey = PATHDIR "/" PUBKEY;
911
/* Initialize Mandos context */
912
mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
913
.dh_bits = 1024, .priority = "SECURE256"
914
":!CTYPE-X.509:+CTYPE-OPENPGP" };
915
1072
bool gnutls_initialized = false;
916
1073
bool gpgme_initialized = false;
919
struct sigaction old_sigterm_action;
1076
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
920
1077
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1082
/* Lower any group privileges we might have, just to be safe */
1089
/* Lower user privileges (temporarily) */
923
1101
struct argp_option options[] = {
924
1102
{ .name = "debug", .key = 128,
977
1155
case 129: /* --dh-bits */
978
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
979
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
980
or arg[numchars] != '\0'){
1157
tmpmax = strtoimax(arg, &tmp, 10);
1158
if(errno != 0 or tmp == arg or *tmp != '\0'
1159
or tmpmax != (typeof(mc.dh_bits))tmpmax){
981
1160
fprintf(stderr, "Bad number of DH bits\n");
982
1161
exit(EXIT_FAILURE);
1019
1199
avahi_set_log_function(empty_log);
1202
if(interface[0] == '\0'){
1203
struct dirent **direntries;
1204
ret = scandir(sys_class_net, &direntries, good_interface,
1207
/* Pick the first good interface */
1208
interface = strdup(direntries[0]->d_name);
1209
if(interface == NULL){
1212
exitcode = EXIT_FAILURE;
1218
fprintf(stderr, "Could not find a network interface\n");
1219
exitcode = EXIT_FAILURE;
1022
1224
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1023
1225
from the signal handler */
1049
1251
exitcode = EXIT_FAILURE;
1052
ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1054
perror("sigaction");
1055
exitcode = EXIT_FAILURE;
1254
/* Need to check if the handler is SIG_IGN before handling:
1255
| [[info:libc:Initial Signal Actions]] |
1256
| [[info:libc:Basic Signal Handling]] |
1258
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1260
perror("sigaction");
1261
return EXIT_FAILURE;
1263
if(old_sigterm_action.sa_handler != SIG_IGN){
1264
ret = sigaction(SIGINT, &sigterm_action, NULL);
1266
perror("sigaction");
1267
exitcode = EXIT_FAILURE;
1271
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1273
perror("sigaction");
1274
return EXIT_FAILURE;
1276
if(old_sigterm_action.sa_handler != SIG_IGN){
1277
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1279
perror("sigaction");
1280
exitcode = EXIT_FAILURE;
1284
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1286
perror("sigaction");
1287
return EXIT_FAILURE;
1289
if(old_sigterm_action.sa_handler != SIG_IGN){
1290
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1292
perror("sigaction");
1293
exitcode = EXIT_FAILURE;
1059
1298
/* If the interface is down, bring it up */
1060
if(interface[0] != '\0'){
1299
if(strcmp(interface, "none") != 0){
1300
if_index = (AvahiIfIndex) if_nametoindex(interface);
1302
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1303
exitcode = EXIT_FAILURE;
1311
/* Re-raise priviliges */
1061
1318
#ifdef __linux__
1062
1319
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1063
1320
messages to mess up the prompt */
1199
1488
exitcode = EXIT_FAILURE;
1203
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1204
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1205
or address[numchars+1] != '\0'){
1498
tmpmax = strtoimax(address+1, &tmp, 10);
1499
if(errno != 0 or tmp == address+1 or *tmp != '\0'
1500
or tmpmax != (uint16_t)tmpmax){
1206
1501
fprintf(stderr, "Bad port number\n");
1207
1502
exitcode = EXIT_FAILURE;
1210
1510
port = (uint16_t)tmpmax;
1211
1511
*address = '\0';
1212
1512
address = connect_to;
1255
1568
/* Create the Avahi service browser */
1256
1569
sb = avahi_s_service_browser_new(mc.server, if_index,
1257
AVAHI_PROTO_INET6, "_mandos._tcp",
1570
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1258
1571
NULL, 0, browse_callback, NULL);
1259
1572
if(sb == NULL){
1260
1573
fprintf(stderr, "Failed to create service browser: %s\n",
1297
1614
gpgme_release(mc.ctx);
1617
/* Take down the network interface */
1618
if(take_down_interface){
1619
/* Re-raise priviliges */
1626
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1628
perror("ioctl SIOCGIFFLAGS");
1629
} else if(network.ifr_flags & IFF_UP) {
1630
network.ifr_flags &= ~IFF_UP; /* clear flag */
1631
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1633
perror("ioctl SIOCSIFFLAGS");
1636
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1640
/* Lower privileges permanently */
1300
1649
/* Removes the temp directory used by GPGME */
1301
1650
if(tempdir_created){