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,
47
srand(), strtof(), abort() */
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() */
71
67
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
73
69
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
74
getuid(), getgid(), seteuid(),
70
getuid(), getgid(), setuid(),
76
72
#include <arpa/inet.h> /* inet_pton(), htons */
77
73
#include <iso646.h> /* not, or, and */
78
74
#include <argp.h> /* struct argp_option, error_t, struct
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
83
#include <sys/klog.h> /* klogctl() */
140
134
} mandos_context;
142
136
/* global context so signal handler can reach it*/
143
mandos_context mc = { .simple_poll = NULL, .server = NULL,
144
.dh_bits = 1024, .priority = "SECURE256"
145
":!CTYPE-X.509:+CTYPE-OPENPGP" };
147
sig_atomic_t quit_now = 0;
148
int signal_received = 0;
151
140
* Make additional room in "buffer" for at least BUFFER_SIZE more
315
304
gpgme_recipient_t recipient;
316
305
recipient = result->recipients;
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;
307
while(recipient != NULL){
308
fprintf(stderr, "Public key algorithm: %s\n",
309
gpgme_pubkey_algo_name(recipient->pubkey_algo));
310
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
311
fprintf(stderr, "Secret key available: %s\n",
312
recipient->status == GPG_ERR_NO_SECKEY
314
recipient = recipient->next;
479
470
static int init_gnutls_session(gnutls_session_t *session){
481
472
/* GnuTLS session creation */
483
ret = gnutls_init(session, GNUTLS_SERVER);
487
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
473
ret = gnutls_init(session, GNUTLS_SERVER);
488
474
if(ret != GNUTLS_E_SUCCESS){
489
475
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
490
476
safer_gnutls_strerror(ret));
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);
481
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
502
482
if(ret != GNUTLS_E_SUCCESS){
503
483
fprintf(stderr, "Syntax error at: %s\n", err);
504
484
fprintf(stderr, "GnuTLS error: %s\n",
512
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
515
gnutls_deinit(*session);
518
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
491
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
519
493
if(ret != GNUTLS_E_SUCCESS){
520
494
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
521
495
safer_gnutls_strerror(ret));
539
514
static int start_mandos_communication(const char *ip, uint16_t port,
540
515
AvahiIfIndex if_index,
542
int ret, tcp_sd = -1;
545
520
struct sockaddr_in in;
546
521
struct sockaddr_in6 in6;
548
523
char *buffer = NULL;
549
char *decrypted_buffer = NULL;
524
char *decrypted_buffer;
550
525
size_t buffer_length = 0;
551
526
size_t buffer_capacity = 0;
527
ssize_t decrypted_buffer_size;
554
530
gnutls_session_t session;
555
531
int pf; /* Protocol family */
583
555
tcp_sd = socket(pf, SOCK_STREAM, 0);
585
557
perror("socket");
593
561
memset(&to, 0, sizeof(to));
594
562
if(af == AF_INET6){
595
to.in6.sin6_family = (sa_family_t)af;
563
to.in6.sin6_family = (uint16_t)af;
596
564
ret = inet_pton(af, ip, &to.in6.sin6_addr);
597
565
} else { /* IPv4 */
598
566
to.in.sin_family = (sa_family_t)af;
773
708
case GNUTLS_E_AGAIN:
775
710
case GNUTLS_E_REHANDSHAKE:
777
712
ret = gnutls_handshake(session);
782
713
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
784
715
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
785
716
gnutls_perror(ret);
790
722
fprintf(stderr, "Unknown error while reading data from"
791
723
" encrypted session with Mandos server\n");
792
725
gnutls_bye(session, GNUTLS_SHUT_RDWR);
801
734
fprintf(stderr, "Closing TLS session\n");
809
ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
813
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
737
gnutls_bye(session, GNUTLS_SHUT_RDWR);
815
739
if(buffer_length > 0){
816
ssize_t decrypted_buffer_size;
817
740
decrypted_buffer_size = pgp_packet_decrypt(buffer,
819
742
&decrypted_buffer);
820
743
if(decrypted_buffer_size >= 0){
823
745
while(written < (size_t) decrypted_buffer_size){
828
746
ret = (int)fwrite(decrypted_buffer + written, 1,
829
747
(size_t)decrypted_buffer_size - written,
943
852
the callback function is called the Avahi server will free the
944
853
resolver for us. */
946
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
947
name, type, domain, protocol, 0,
948
resolve_callback, NULL) == NULL)
855
if(!(avahi_s_service_resolver_new(mc.server, interface,
856
protocol, name, type, domain,
857
AVAHI_PROTO_INET6, 0,
858
resolve_callback, NULL)))
949
859
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
950
860
name, avahi_strerror(avahi_server_errno(mc.server)));
976
886
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 */
1053
889
int main(int argc, char *argv[]){
1054
890
AvahiSServiceBrowser *sb = NULL;
1057
893
intmax_t tmpmax;
1059
895
int exitcode = EXIT_SUCCESS;
1060
const char *interface = "";
896
const char *interface = "eth0";
1061
897
struct ifreq network;
1063
bool take_down_interface = false;
901
char *connect_to = NULL;
1066
902
char tempdir[] = "/tmp/mandosXXXXXX";
1067
903
bool tempdir_created = false;
1068
904
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1069
905
const char *seckey = PATHDIR "/" SECKEY;
1070
906
const char *pubkey = PATHDIR "/" PUBKEY;
908
/* Initialize Mandos context */
909
mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
910
.dh_bits = 1024, .priority = "SECURE256"
911
":!CTYPE-X.509:+CTYPE-OPENPGP" };
1072
912
bool gnutls_initialized = false;
1073
913
bool gpgme_initialized = false;
1076
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
916
struct sigaction old_sigterm_action;
1077
917
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) */
1101
920
struct argp_option options[] = {
1102
921
{ .name = "debug", .key = 128,
1155
974
case 129: /* --dh-bits */
1157
tmpmax = strtoimax(arg, &tmp, 10);
1158
if(errno != 0 or tmp == arg or *tmp != '\0'
1159
or tmpmax != (typeof(mc.dh_bits))tmpmax){
975
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
976
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
977
or arg[numchars] != '\0'){
1160
978
fprintf(stderr, "Bad number of DH bits\n");
1161
979
exit(EXIT_FAILURE);
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;
1224
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1225
from the signal handler */
1226
/* Initialize the pseudo-RNG for Avahi */
1227
srand((unsigned int) time(NULL));
1228
mc.simple_poll = avahi_simple_poll_new();
1229
if(mc.simple_poll == NULL){
1230
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1231
exitcode = EXIT_FAILURE;
1235
sigemptyset(&sigterm_action.sa_mask);
1236
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1238
perror("sigaddset");
1239
exitcode = EXIT_FAILURE;
1242
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1244
perror("sigaddset");
1245
exitcode = EXIT_FAILURE;
1248
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1250
perror("sigaddset");
1251
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;
1298
1015
/* If the interface is down, bring it up */
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 */
1016
if(interface[0] != '\0'){
1318
1017
#ifdef __linux__
1319
1018
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1320
1019
messages to mess up the prompt */
1488
1155
exitcode = EXIT_FAILURE;
1498
tmpmax = strtoimax(address+1, &tmp, 10);
1499
if(errno != 0 or tmp == address+1 or *tmp != '\0'
1500
or tmpmax != (uint16_t)tmpmax){
1159
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1160
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1161
or address[numchars+1] != '\0'){
1501
1162
fprintf(stderr, "Bad port number\n");
1502
1163
exitcode = EXIT_FAILURE;
1510
1166
port = (uint16_t)tmpmax;
1511
1167
*address = '\0';
1512
1168
address = connect_to;
1186
avahi_set_log_function(empty_log);
1189
/* Initialize the pseudo-RNG for Avahi */
1190
srand((unsigned int) time(NULL));
1192
/* Allocate main Avahi loop object */
1193
mc.simple_poll = avahi_simple_poll_new();
1194
if(mc.simple_poll == NULL){
1195
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1196
exitcode = EXIT_FAILURE;
1568
1226
/* Create the Avahi service browser */
1569
1227
sb = avahi_s_service_browser_new(mc.server, if_index,
1570
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1228
AVAHI_PROTO_INET6, "_mandos._tcp",
1571
1229
NULL, 0, browse_callback, NULL);
1572
1230
if(sb == NULL){
1573
1231
fprintf(stderr, "Failed to create service browser: %s\n",
1614
1294
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 */
1649
1297
/* Removes the temp directory used by GPGME */
1650
1298
if(tempdir_created){