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() */
134
140
} mandos_context;
136
142
/* 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;
140
151
* Make additional room in "buffer" for at least BUFFER_SIZE more
304
315
gpgme_recipient_t recipient;
305
316
recipient = result->recipients;
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;
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;
470
479
static int init_gnutls_session(gnutls_session_t *session){
472
481
/* GnuTLS session creation */
473
ret = gnutls_init(session, GNUTLS_SERVER);
483
ret = gnutls_init(session, GNUTLS_SERVER);
487
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
474
488
if(ret != GNUTLS_E_SUCCESS){
475
489
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
476
490
safer_gnutls_strerror(ret));
481
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);
482
502
if(ret != GNUTLS_E_SUCCESS){
483
503
fprintf(stderr, "Syntax error at: %s\n", err);
484
504
fprintf(stderr, "GnuTLS error: %s\n",
491
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);
493
519
if(ret != GNUTLS_E_SUCCESS){
494
520
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
495
521
safer_gnutls_strerror(ret));
514
539
static int start_mandos_communication(const char *ip, uint16_t port,
515
540
AvahiIfIndex if_index,
542
int ret, tcp_sd = -1;
520
545
struct sockaddr_in in;
521
546
struct sockaddr_in6 in6;
523
548
char *buffer = NULL;
524
char *decrypted_buffer;
549
char *decrypted_buffer = NULL;
525
550
size_t buffer_length = 0;
526
551
size_t buffer_capacity = 0;
527
ssize_t decrypted_buffer_size;
530
554
gnutls_session_t session;
531
555
int pf; /* Protocol family */
555
583
tcp_sd = socket(pf, SOCK_STREAM, 0);
557
585
perror("socket");
561
593
memset(&to, 0, sizeof(to));
562
594
if(af == AF_INET6){
563
to.in6.sin6_family = (uint16_t)af;
595
to.in6.sin6_family = (sa_family_t)af;
564
596
ret = inet_pton(af, ip, &to.in6.sin6_addr);
565
597
} else { /* IPv4 */
566
598
to.in.sin_family = (sa_family_t)af;
708
773
case GNUTLS_E_AGAIN:
710
775
case GNUTLS_E_REHANDSHAKE:
712
777
ret = gnutls_handshake(session);
713
782
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
715
784
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
716
785
gnutls_perror(ret);
722
790
fprintf(stderr, "Unknown error while reading data from"
723
791
" encrypted session with Mandos server\n");
725
792
gnutls_bye(session, GNUTLS_SHUT_RDWR);
734
801
fprintf(stderr, "Closing TLS session\n");
737
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);
739
815
if(buffer_length > 0){
816
ssize_t decrypted_buffer_size;
740
817
decrypted_buffer_size = pgp_packet_decrypt(buffer,
742
819
&decrypted_buffer);
743
820
if(decrypted_buffer_size >= 0){
745
823
while(written < (size_t) decrypted_buffer_size){
746
828
ret = (int)fwrite(decrypted_buffer + written, 1,
747
829
(size_t)decrypted_buffer_size - written,
852
943
the callback function is called the Avahi server will free the
853
944
resolver for us. */
855
if(!(avahi_s_service_resolver_new(mc.server, interface,
856
protocol, name, type, domain,
857
AVAHI_PROTO_INET6, 0,
858
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)
859
949
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
860
950
name, avahi_strerror(avahi_server_errno(mc.server)));
886
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 */
889
1053
int main(int argc, char *argv[]){
890
1054
AvahiSServiceBrowser *sb = NULL;
893
1057
intmax_t tmpmax;
895
1059
int exitcode = EXIT_SUCCESS;
896
const char *interface = "eth0";
1060
const char *interface = "";
897
1061
struct ifreq network;
1063
bool take_down_interface = false;
901
char *connect_to = NULL;
902
1066
char tempdir[] = "/tmp/mandosXXXXXX";
903
1067
bool tempdir_created = false;
904
1068
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
905
1069
const char *seckey = PATHDIR "/" SECKEY;
906
1070
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" };
912
1072
bool gnutls_initialized = false;
913
1073
bool gpgme_initialized = false;
916
struct sigaction old_sigterm_action;
1076
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
917
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) */
920
1101
struct argp_option options[] = {
921
1102
{ .name = "debug", .key = 128,
974
1155
case 129: /* --dh-bits */
975
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
976
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
977
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){
978
1160
fprintf(stderr, "Bad number of DH bits\n");
979
1161
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;
1015
1298
/* If the interface is down, bring it up */
1016
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 */
1017
1318
#ifdef __linux__
1018
1319
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1019
1320
messages to mess up the prompt */
1155
1488
exitcode = EXIT_FAILURE;
1159
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1160
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1161
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){
1162
1501
fprintf(stderr, "Bad port number\n");
1163
1502
exitcode = EXIT_FAILURE;
1166
1510
port = (uint16_t)tmpmax;
1167
1511
*address = '\0';
1168
1512
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;
1226
1568
/* Create the Avahi service browser */
1227
1569
sb = avahi_s_service_browser_new(mc.server, if_index,
1228
AVAHI_PROTO_INET6, "_mandos._tcp",
1570
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1229
1571
NULL, 0, browse_callback, NULL);
1230
1572
if(sb == NULL){
1231
1573
fprintf(stderr, "Failed to create service browser: %s\n",
1294
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 */
1297
1649
/* Removes the temp directory used by GPGME */
1298
1650
if(tempdir_created){