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,
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 */
75
79
argp_state, struct argp,
76
80
argp_parse(), ARGP_KEY_ARG,
77
81
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
#include <signal.h> /* sigemptyset(), sigaddset(), sigaction(), SIGTERM, sigaction */
82
#include <signal.h> /* sigemptyset(), sigaddset(),
83
sigaction(), SIGTERM, sig_atomic_t,
80
87
#include <sys/klog.h> /* klogctl() */
88
#endif /* __linux__ */
84
91
/* All Avahi types, constants and functions
136
143
":!CTYPE-X.509:+CTYPE-OPENPGP" };
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.
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.
143
150
size_t incbuffer(char **buffer, size_t buffer_length,
144
151
size_t buffer_capacity){
303
310
gpgme_recipient_t recipient;
304
311
recipient = result->recipients;
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;
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;
509
514
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
510
515
__attribute__((unused)) const char *txt){}
517
sig_atomic_t quit_now = 0;
518
int signal_received = 0;
512
520
/* Called when a Mandos server is found */
513
521
static int start_mandos_communication(const char *ip, uint16_t port,
514
522
AvahiIfIndex if_index,
524
int ret, tcp_sd = -1;
519
527
struct sockaddr_in in;
523
531
char *decrypted_buffer;
524
532
size_t buffer_length = 0;
525
533
size_t buffer_capacity = 0;
526
ssize_t decrypted_buffer_size;
529
536
gnutls_session_t session;
530
537
int pf; /* Protocol family */
554
565
tcp_sd = socket(pf, SOCK_STREAM, 0);
556
567
perror("socket");
560
576
memset(&to, 0, sizeof(to));
561
577
if(af == AF_INET6){
562
to.in6.sin6_family = (uint16_t)af;
578
to.in6.sin6_family = (sa_family_t)af;
563
579
ret = inet_pton(af, ip, &to.in6.sin6_addr);
564
580
} else { /* IPv4 */
565
581
to.in.sin_family = (sa_family_t)af;
584
602
if(if_index == AVAHI_IF_UNSPEC){
585
603
fprintf(stderr, "An IPv6 link-local address is incomplete"
586
604
" without a network interface\n");
589
608
/* Set the network interface number as scope */
590
609
to.in6.sin6_scope_id = (uint32_t)if_index;
665
701
fprintf(stderr, "Establishing TLS session with %s\n", ip);
668
708
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
671
715
ret = gnutls_handshake(session);
672
719
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
674
721
if(ret != GNUTLS_E_SUCCESS){
707
763
case GNUTLS_E_AGAIN:
709
765
case GNUTLS_E_REHANDSHAKE:
711
767
ret = gnutls_handshake(session);
712
772
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
714
774
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
733
793
fprintf(stderr, "Closing TLS session\n");
736
800
gnutls_bye(session, GNUTLS_SHUT_RDWR);
738
806
if(buffer_length > 0){
807
ssize_t decrypted_buffer_size;
739
808
decrypted_buffer_size = pgp_packet_decrypt(buffer,
741
810
&decrypted_buffer);
742
811
if(decrypted_buffer_size >= 0){
744
814
while(written < (size_t) decrypted_buffer_size){
745
819
ret = (int)fwrite(decrypted_buffer + written, 1,
746
820
(size_t)decrypted_buffer_size - written,
788
867
AVAHI_GCC_UNUSED AvahiStringList *txt,
789
868
AVAHI_GCC_UNUSED AvahiLookupResultFlags
791
__attribute__((unused)) void* userdata){
870
AVAHI_GCC_UNUSED void* userdata){
794
873
/* Called whenever a service has been resolved successfully or
799
882
case AVAHI_RESOLVER_FAILURE:
830
913
const char *domain,
831
914
AVAHI_GCC_UNUSED AvahiLookupResultFlags
833
__attribute__((unused)) void* userdata){
916
AVAHI_GCC_UNUSED void* userdata){
836
919
/* Called whenever a new services becomes available on the LAN or
837
920
is removed from the LAN */
841
928
case AVAHI_BROWSER_FAILURE:
851
938
the callback function is called the Avahi server will free the
852
939
resolver for us. */
854
if(!(avahi_s_service_resolver_new(mc.server, interface,
855
protocol, name, type, domain,
856
AVAHI_PROTO_INET6, 0,
857
resolve_callback, NULL)))
941
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
942
name, type, domain, protocol, 0,
943
resolve_callback, NULL) == NULL)
858
944
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
859
945
name, avahi_strerror(avahi_server_errno(mc.server)));
874
960
/* stop main loop after sigterm has been called */
875
static void handle_sigterm(__attribute__((unused)) int sig){
961
static void handle_sigterm(int sig){
966
signal_received = sig;
876
967
int old_errno = errno;
877
avahi_simple_poll_quit(mc.simple_poll);
968
if(mc.simple_poll != NULL){
969
avahi_simple_poll_quit(mc.simple_poll);
878
971
errno = old_errno;
900
994
bool gnutls_initialized = false;
901
995
bool gpgme_initialized = false;
904
998
struct sigaction old_sigterm_action;
905
999
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
908
1002
struct argp_option options[] = {
909
1003
{ .name = "debug", .key = 128,
962
1056
case 129: /* --dh-bits */
963
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
964
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
965
or arg[numchars] != '\0'){
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
1061
fprintf(stderr, "Bad number of DH bits\n");
967
1062
exit(EXIT_FAILURE);
972
1067
mc.priority = arg;
974
1069
case 131: /* --delay */
975
ret = sscanf(arg, "%lf%n", &delay, &numchars);
976
if(ret < 1 or arg[numchars] != '\0'){
1071
delay = strtof(arg, &tmp);
1072
if(errno != 0 or tmp == arg or *tmp != '\0'){
977
1073
fprintf(stderr, "Bad delay\n");
978
1074
exit(EXIT_FAILURE);
1014
1110
exitcode = EXIT_FAILURE;
1018
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;
1019
1127
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1021
1129
perror("sigaddset");
1022
1130
exitcode = EXIT_FAILURE;
1025
ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1027
perror("sigaction");
1028
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;
1033
1177
/* If the interface is down, bring it up */
1034
1178
if(interface[0] != '\0'){
1179
if_index = (AvahiIfIndex) if_nametoindex(interface);
1181
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1182
exitcode = EXIT_FAILURE;
1035
1190
#ifdef __linux__
1036
1191
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1037
1192
messages to mess up the prompt */
1068
1223
perror("klogctl");
1226
#endif /* __linux__ */
1072
1227
exitcode = EXIT_FAILURE;
1075
1230
if((network.ifr_flags & IFF_UP) == 0){
1076
1231
network.ifr_flags |= IFF_UP;
1232
take_down_interface = true;
1077
1233
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1235
take_down_interface = false;
1079
1236
perror("ioctl SIOCSIFFLAGS");
1080
1237
exitcode = EXIT_FAILURE;
1081
1238
#ifdef __linux__
1115
1275
perror("klogctl");
1278
#endif /* __linux__ */
1121
1285
uid = getuid();
1122
1286
gid = getgid();
1288
/* Drop any group privileges we might have, just to be safe */
1127
1292
perror("setgid");
1295
/* Drop user privileges */
1297
/* Will we need privileges later? */
1298
if(take_down_interface){
1299
/* Drop user privileges temporarily */
1305
/* Drop user privileges permanently */
1135
1316
ret = init_gnutls_global(pubkey, seckey);
1141
1322
gnutls_initialized = true;
1329
tempdir_created = true;
1144
1330
if(mkdtemp(tempdir) == NULL){
1331
tempdir_created = false;
1145
1332
perror("mkdtemp");
1148
tempdir_created = true;
1150
1340
if(not init_gpgme(pubkey, seckey, tempdir)){
1151
1341
fprintf(stderr, "init_gpgme failed\n");
1155
1345
gpgme_initialized = true;
1158
if(interface[0] != '\0'){
1159
if_index = (AvahiIfIndex) if_nametoindex(interface);
1161
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1162
exitcode = EXIT_FAILURE;
1167
1352
if(connect_to != NULL){
1173
1358
exitcode = EXIT_FAILURE;
1177
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1178
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1179
or address[numchars+1] != '\0'){
1368
tmpmax = strtoimax(address+1, &tmp, 10);
1369
if(errno != 0 or tmp == address+1 or *tmp != '\0'
1370
or tmpmax != (uint16_t)tmpmax){
1180
1371
fprintf(stderr, "Bad port number\n");
1181
1372
exitcode = EXIT_FAILURE;
1184
1380
port = (uint16_t)tmpmax;
1185
1381
*address = '\0';
1186
1382
address = connect_to;
1194
ret = start_mandos_communication(address, port, if_index,
1395
ret = start_mandos_communication(address, port, if_index, af);
1197
1397
exitcode = EXIT_FAILURE;
1230
1438
/* Create the Avahi service browser */
1231
1439
sb = avahi_s_service_browser_new(mc.server, if_index,
1232
AVAHI_PROTO_INET6, "_mandos._tcp",
1440
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1233
1441
NULL, 0, browse_callback, NULL);
1234
1442
if(sb == NULL){
1235
1443
fprintf(stderr, "Failed to create service browser: %s\n",
1272
1484
gpgme_release(mc.ctx);
1487
/* Take down the network interface */
1488
if(take_down_interface){
1489
/* Re-raise priviliges */
1496
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1498
perror("ioctl SIOCGIFFLAGS");
1499
} else if(network.ifr_flags & IFF_UP) {
1500
network.ifr_flags &= ~IFF_UP; /* clear flag */
1501
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1503
perror("ioctl SIOCSIFFLAGS");
1506
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1510
/* Lower privileges, permanently this time */
1275
1519
/* Removes the temp directory used by GPGME */
1276
1520
if(tempdir_created){