32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
#ifndef _LARGEFILE_SOURCE
34
33
#define _LARGEFILE_SOURCE
36
#ifndef _FILE_OFFSET_BITS
37
34
#define _FILE_OFFSET_BITS 64
40
36
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
42
38
#include <stdio.h> /* fprintf(), stderr, fwrite(),
43
stdout, ferror(), remove() */
39
stdout, ferror(), sscanf(),
44
41
#include <stdint.h> /* uint16_t, uint32_t */
45
42
#include <stddef.h> /* NULL, size_t, ssize_t */
46
43
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
48
45
#include <stdbool.h> /* bool, false, true */
49
46
#include <string.h> /* memset(), strcmp(), strlen(),
50
47
strerror(), asprintf(), strcpy() */
59
56
#include <fcntl.h> /* open() */
60
57
#include <dirent.h> /* opendir(), struct dirent, readdir()
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
59
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
64
60
#include <assert.h> /* assert() */
65
61
#include <errno.h> /* perror(), errno */
66
62
#include <time.h> /* nanosleep(), time() */
80
76
argp_parse(), ARGP_KEY_ARG,
81
77
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
78
#include <signal.h> /* sigemptyset(), sigaddset(),
83
sigaction(), SIGTERM, sig_atomic_t,
79
sigaction(), SIGTERM, sigaction */
87
82
#include <sys/klog.h> /* klogctl() */
88
#endif /* __linux__ */
91
86
/* All Avahi types, constants and functions
138
133
} mandos_context;
140
135
/* global context so signal handler can reach it*/
141
mandos_context mc = { .simple_poll = NULL, .server = NULL,
142
.dh_bits = 1024, .priority = "SECURE256"
143
":!CTYPE-X.509:+CTYPE-OPENPGP" };
146
* Make additional room in "buffer" for at least BUFFER_SIZE more
147
* bytes. "buffer_capacity" is how much is currently allocated,
148
* "buffer_length" is how much is already used.
139
* Make additional room in "buffer" for at least BUFFER_SIZE
140
* additional bytes. "buffer_capacity" is how much is currently
141
* allocated, "buffer_length" is how much is already used.
150
143
size_t incbuffer(char **buffer, size_t buffer_length,
151
144
size_t buffer_capacity){
310
303
gpgme_recipient_t recipient;
311
304
recipient = result->recipients;
312
while(recipient != NULL){
313
fprintf(stderr, "Public key algorithm: %s\n",
314
gpgme_pubkey_algo_name(recipient->pubkey_algo));
315
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
316
fprintf(stderr, "Secret key available: %s\n",
317
recipient->status == GPG_ERR_NO_SECKEY
319
recipient = recipient->next;
306
while(recipient != NULL){
307
fprintf(stderr, "Public key algorithm: %s\n",
308
gpgme_pubkey_algo_name(recipient->pubkey_algo));
309
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
310
fprintf(stderr, "Secret key available: %s\n",
311
recipient->status == GPG_ERR_NO_SECKEY
313
recipient = recipient->next;
514
509
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
515
510
__attribute__((unused)) const char *txt){}
517
sig_atomic_t quit_now = 0;
518
int signal_received = 0;
520
512
/* Called when a Mandos server is found */
521
513
static int start_mandos_communication(const char *ip, uint16_t port,
522
514
AvahiIfIndex if_index,
524
int ret, tcp_sd = -1;
527
519
struct sockaddr_in in;
531
523
char *decrypted_buffer;
532
524
size_t buffer_length = 0;
533
525
size_t buffer_capacity = 0;
526
ssize_t decrypted_buffer_size;
536
529
gnutls_session_t session;
537
530
int pf; /* Protocol family */
565
554
tcp_sd = socket(pf, SOCK_STREAM, 0);
567
556
perror("socket");
576
560
memset(&to, 0, sizeof(to));
577
561
if(af == AF_INET6){
578
to.in6.sin6_family = (sa_family_t)af;
562
to.in6.sin6_family = (uint16_t)af;
579
563
ret = inet_pton(af, ip, &to.in6.sin6_addr);
580
564
} else { /* IPv4 */
581
565
to.in.sin_family = (sa_family_t)af;
602
584
if(if_index == AVAHI_IF_UNSPEC){
603
585
fprintf(stderr, "An IPv6 link-local address is incomplete"
604
586
" without a network interface\n");
608
589
/* Set the network interface number as scope */
609
590
to.in6.sin6_scope_id = (uint32_t)if_index;
701
665
fprintf(stderr, "Establishing TLS session with %s\n", ip);
708
668
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
715
671
ret = gnutls_handshake(session);
719
672
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
721
674
if(ret != GNUTLS_E_SUCCESS){
763
707
case GNUTLS_E_AGAIN:
765
709
case GNUTLS_E_REHANDSHAKE:
767
711
ret = gnutls_handshake(session);
772
712
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
774
714
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
793
733
fprintf(stderr, "Closing TLS session\n");
800
736
gnutls_bye(session, GNUTLS_SHUT_RDWR);
806
738
if(buffer_length > 0){
807
ssize_t decrypted_buffer_size;
808
739
decrypted_buffer_size = pgp_packet_decrypt(buffer,
810
741
&decrypted_buffer);
811
742
if(decrypted_buffer_size >= 0){
814
744
while(written < (size_t) decrypted_buffer_size){
819
745
ret = (int)fwrite(decrypted_buffer + written, 1,
820
746
(size_t)decrypted_buffer_size - written,
938
851
the callback function is called the Avahi server will free the
939
852
resolver for us. */
941
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
942
name, type, domain, protocol, 0,
943
resolve_callback, NULL) == NULL)
854
if(!(avahi_s_service_resolver_new(mc.server, interface,
855
protocol, name, type, domain,
856
AVAHI_PROTO_INET6, 0,
857
resolve_callback, NULL)))
944
858
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
945
859
name, avahi_strerror(avahi_server_errno(mc.server)));
960
/* stop main loop after sigterm has been called */
961
static void handle_sigterm(int sig){
966
signal_received = sig;
874
static void handle_sigterm(__attribute__((unused)) int sig){
967
875
int old_errno = errno;
968
if(mc.simple_poll != NULL){
969
avahi_simple_poll_quit(mc.simple_poll);
876
avahi_simple_poll_quit(mc.simple_poll);
971
877
errno = old_errno;
991
896
const char *seckey = PATHDIR "/" SECKEY;
992
897
const char *pubkey = PATHDIR "/" PUBKEY;
899
/* Initialize Mandos context */
900
mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
901
.dh_bits = 1024, .priority = "SECURE256"
902
":!CTYPE-X.509:+CTYPE-OPENPGP" };
994
903
bool gnutls_initialized = false;
995
904
bool gpgme_initialized = false;
998
907
struct sigaction old_sigterm_action;
999
908
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1056
965
case 129: /* --dh-bits */
1058
tmpmax = strtoimax(arg, &tmp, 10);
1059
if(errno != 0 or tmp == arg or *tmp != '\0'
1060
or tmpmax != (typeof(mc.dh_bits))tmpmax){
966
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
967
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
968
or arg[numchars] != '\0'){
1061
969
fprintf(stderr, "Bad number of DH bits\n");
1062
970
exit(EXIT_FAILURE);
1067
975
mc.priority = arg;
1069
977
case 131: /* --delay */
1071
delay = strtof(arg, &tmp);
1072
if(errno != 0 or tmp == arg or *tmp != '\0'){
978
ret = sscanf(arg, "%lf%n", &delay, &numchars);
979
if(ret < 1 or arg[numchars] != '\0'){
1073
980
fprintf(stderr, "Bad delay\n");
1074
981
exit(EXIT_FAILURE);
1100
avahi_set_log_function(empty_log);
1103
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1104
from the signal handler */
1105
/* Initialize the pseudo-RNG for Avahi */
1106
srand((unsigned int) time(NULL));
1107
mc.simple_poll = avahi_simple_poll_new();
1108
if(mc.simple_poll == NULL){
1109
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1110
exitcode = EXIT_FAILURE;
1114
sigemptyset(&sigterm_action.sa_mask);
1115
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1117
perror("sigaddset");
1118
exitcode = EXIT_FAILURE;
1121
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1123
perror("sigaddset");
1124
exitcode = EXIT_FAILURE;
1127
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1129
perror("sigaddset");
1130
exitcode = EXIT_FAILURE;
1133
/* Need to check if the handler is SIG_IGN before handling:
1134
| [[info:libc:Initial Signal Actions]] |
1135
| [[info:libc:Basic Signal Handling]] |
1137
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1139
perror("sigaction");
1140
return EXIT_FAILURE;
1142
if(old_sigterm_action.sa_handler != SIG_IGN){
1143
ret = sigaction(SIGINT, &sigterm_action, NULL);
1145
perror("sigaction");
1146
exitcode = EXIT_FAILURE;
1150
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1152
perror("sigaction");
1153
return EXIT_FAILURE;
1155
if(old_sigterm_action.sa_handler != SIG_IGN){
1156
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1158
perror("sigaction");
1159
exitcode = EXIT_FAILURE;
1163
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1165
perror("sigaction");
1166
return EXIT_FAILURE;
1168
if(old_sigterm_action.sa_handler != SIG_IGN){
1169
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1171
perror("sigaction");
1172
exitcode = EXIT_FAILURE;
1177
1006
/* If the interface is down, bring it up */
1178
1007
if(interface[0] != '\0'){
1179
if_index = (AvahiIfIndex) if_nametoindex(interface);
1181
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1182
exitcode = EXIT_FAILURE;
1190
1008
#ifdef __linux__
1191
1009
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1192
1010
messages to mess up the prompt */
1223
1041
perror("klogctl");
1226
#endif /* __linux__ */
1227
1045
exitcode = EXIT_FAILURE;
1230
1048
if((network.ifr_flags & IFF_UP) == 0){
1231
1049
network.ifr_flags |= IFF_UP;
1232
take_down_interface = true;
1233
1050
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1235
take_down_interface = false;
1236
1052
perror("ioctl SIOCSIFFLAGS");
1237
1053
exitcode = EXIT_FAILURE;
1238
1054
#ifdef __linux__
1309
1114
gnutls_initialized = true;
1316
tempdir_created = true;
1317
1117
if(mkdtemp(tempdir) == NULL){
1318
tempdir_created = false;
1319
1118
perror("mkdtemp");
1121
tempdir_created = true;
1327
1123
if(not init_gpgme(pubkey, seckey, tempdir)){
1328
1124
fprintf(stderr, "init_gpgme failed\n");
1332
1128
gpgme_initialized = true;
1131
if(interface[0] != '\0'){
1132
if_index = (AvahiIfIndex) if_nametoindex(interface);
1134
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1135
exitcode = EXIT_FAILURE;
1339
1140
if(connect_to != NULL){
1345
1146
exitcode = EXIT_FAILURE;
1355
tmpmax = strtoimax(address+1, &tmp, 10);
1356
if(errno != 0 or tmp == address+1 or *tmp != '\0'
1357
or tmpmax != (uint16_t)tmpmax){
1150
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1151
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1152
or address[numchars+1] != '\0'){
1358
1153
fprintf(stderr, "Bad port number\n");
1359
1154
exitcode = EXIT_FAILURE;
1367
1157
port = (uint16_t)tmpmax;
1368
1158
*address = '\0';
1369
1159
address = connect_to;
1177
avahi_set_log_function(empty_log);
1180
/* Initialize the pseudo-RNG for Avahi */
1181
srand((unsigned int) time(NULL));
1183
/* Allocate main Avahi loop object */
1184
mc.simple_poll = avahi_simple_poll_new();
1185
if(mc.simple_poll == NULL){
1186
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1187
exitcode = EXIT_FAILURE;
1425
1217
/* Create the Avahi service browser */
1426
1218
sb = avahi_s_service_browser_new(mc.server, if_index,
1427
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1219
AVAHI_PROTO_INET6, "_mandos._tcp",
1428
1220
NULL, 0, browse_callback, NULL);
1429
1221
if(sb == NULL){
1430
1222
fprintf(stderr, "Failed to create service browser: %s\n",
1471
1273
gpgme_release(mc.ctx);
1474
/* Take down the network interface */
1475
if(take_down_interface){
1476
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1478
perror("ioctl SIOCGIFFLAGS");
1479
} else if(network.ifr_flags & IFF_UP) {
1480
network.ifr_flags &= ~IFF_UP; /* clear flag */
1481
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1483
perror("ioctl SIOCSIFFLAGS");
1486
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1492
1276
/* Removes the temp directory used by GPGME */
1493
1277
if(tempdir_created){