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() */
75
79
argp_state, struct argp,
76
80
argp_parse(), ARGP_KEY_ARG,
77
81
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
#include <signal.h> /* sigemptyset(), sigaddset(),
83
sigaction(), SIGTERM, sigaction,
79
87
#include <sys/klog.h> /* klogctl() */
88
#endif /* __linux__ */
83
91
/* All Avahi types, constants and functions
130
138
} mandos_context;
140
/* 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" };
133
* Make additional room in "buffer" for at least BUFFER_SIZE
134
* additional bytes. "buffer_capacity" is how much is currently
135
* 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.
137
150
size_t incbuffer(char **buffer, size_t buffer_length,
138
151
size_t buffer_capacity){
150
163
* Initialize GPGME.
152
static bool init_gpgme(mandos_context *mc, const char *seckey,
165
static bool init_gpgme(const char *seckey,
153
166
const char *pubkey, const char *tempdir){
155
168
gpgme_error_t rc;
179
rc = gpgme_op_import(mc->ctx, pgp_data);
192
rc = gpgme_op_import(mc.ctx, pgp_data);
180
193
if(rc != GPG_ERR_NO_ERROR){
181
194
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
182
195
gpgme_strsource(rc), gpgme_strerror(rc));
227
240
/* Create new GPGME "context" */
228
rc = gpgme_new(&(mc->ctx));
241
rc = gpgme_new(&(mc.ctx));
229
242
if(rc != GPG_ERR_NO_ERROR){
230
243
fprintf(stderr, "bad gpgme_new: %s: %s\n",
231
244
gpgme_strsource(rc), gpgme_strerror(rc));
243
256
* Decrypt OpenPGP data.
244
257
* Returns -1 on error
246
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
247
const char *cryptotext,
259
static ssize_t pgp_packet_decrypt(const char *cryptotext,
248
260
size_t crypto_size,
249
261
char **plaintext){
250
262
gpgme_data_t dh_crypto, dh_plain;
278
290
/* Decrypt data from the cryptotext data buffer to the plaintext
280
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
292
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
281
293
if(rc != GPG_ERR_NO_ERROR){
282
294
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
283
295
gpgme_strsource(rc), gpgme_strerror(rc));
284
296
plaintext_length = -1;
286
298
gpgme_decrypt_result_t result;
287
result = gpgme_op_decrypt_result(mc->ctx);
299
result = gpgme_op_decrypt_result(mc.ctx);
288
300
if(result == NULL){
289
301
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
382
394
fprintf(stderr, "GnuTLS: %s", string);
385
static int init_gnutls_global(mandos_context *mc,
386
const char *pubkeyfilename,
397
static int init_gnutls_global(const char *pubkeyfilename,
387
398
const char *seckeyfilename){
409
420
/* OpenPGP credentials */
410
gnutls_certificate_allocate_credentials(&mc->cred);
421
gnutls_certificate_allocate_credentials(&mc.cred);
411
422
if(ret != GNUTLS_E_SUCCESS){
412
423
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
427
438
ret = gnutls_certificate_set_openpgp_key_file
428
(mc->cred, pubkeyfilename, seckeyfilename,
439
(mc.cred, pubkeyfilename, seckeyfilename,
429
440
GNUTLS_OPENPGP_FMT_BASE64);
430
441
if(ret != GNUTLS_E_SUCCESS){
439
450
/* GnuTLS server initialization */
440
ret = gnutls_dh_params_init(&mc->dh_params);
451
ret = gnutls_dh_params_init(&mc.dh_params);
441
452
if(ret != GNUTLS_E_SUCCESS){
442
453
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
443
454
" %s\n", safer_gnutls_strerror(ret));
446
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
457
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
447
458
if(ret != GNUTLS_E_SUCCESS){
448
459
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
449
460
safer_gnutls_strerror(ret));
453
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
464
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
459
gnutls_certificate_free_credentials(mc->cred);
470
gnutls_certificate_free_credentials(mc.cred);
460
471
gnutls_global_deinit();
461
gnutls_dh_params_deinit(mc->dh_params);
472
gnutls_dh_params_deinit(mc.dh_params);
465
static int init_gnutls_session(mandos_context *mc,
466
gnutls_session_t *session){
476
static int init_gnutls_session(gnutls_session_t *session){
468
478
/* GnuTLS session creation */
469
479
ret = gnutls_init(session, GNUTLS_SERVER);
477
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
487
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
478
488
if(ret != GNUTLS_E_SUCCESS){
479
489
fprintf(stderr, "Syntax error at: %s\n", err);
480
490
fprintf(stderr, "GnuTLS error: %s\n",
487
497
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
489
499
if(ret != GNUTLS_E_SUCCESS){
490
500
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
491
501
safer_gnutls_strerror(ret));
497
507
gnutls_certificate_server_set_request(*session,
498
508
GNUTLS_CERT_IGNORE);
500
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
510
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
509
519
/* Called when a Mandos server is found */
510
520
static int start_mandos_communication(const char *ip, uint16_t port,
511
521
AvahiIfIndex if_index,
512
mandos_context *mc, int af){
733
743
gnutls_bye(session, GNUTLS_SHUT_RDWR);
735
745
if(buffer_length > 0){
736
decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
746
decrypted_buffer_size = pgp_packet_decrypt(buffer,
738
748
&decrypted_buffer);
739
749
if(decrypted_buffer_size >= 0){
785
795
AVAHI_GCC_UNUSED AvahiStringList *txt,
786
796
AVAHI_GCC_UNUSED AvahiLookupResultFlags
789
mandos_context *mc = userdata;
798
AVAHI_GCC_UNUSED void* userdata){
792
801
/* Called whenever a service has been resolved successfully or
797
806
case AVAHI_RESOLVER_FAILURE:
798
807
fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
799
808
" of type '%s' in domain '%s': %s\n", name, type, domain,
800
avahi_strerror(avahi_server_errno(mc->server)));
809
avahi_strerror(avahi_server_errno(mc.server)));
803
812
case AVAHI_RESOLVER_FOUND:
809
818
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
810
819
ip, (intmax_t)interface, port);
812
int ret = start_mandos_communication(ip, port, interface, mc,
821
int ret = start_mandos_communication(ip, port, interface,
813
822
avahi_proto_to_af(proto));
815
avahi_simple_poll_quit(mc->simple_poll);
824
avahi_simple_poll_quit(mc.simple_poll);
840
848
case AVAHI_BROWSER_FAILURE:
842
850
fprintf(stderr, "(Avahi browser) %s\n",
843
avahi_strerror(avahi_server_errno(mc->server)));
844
avahi_simple_poll_quit(mc->simple_poll);
851
avahi_strerror(avahi_server_errno(mc.server)));
852
avahi_simple_poll_quit(mc.simple_poll);
847
855
case AVAHI_BROWSER_NEW:
850
858
the callback function is called the Avahi server will free the
851
859
resolver for us. */
853
if(!(avahi_s_service_resolver_new(mc->server, interface,
854
protocol, name, type, domain,
855
AVAHI_PROTO_INET6, 0,
856
resolve_callback, mc)))
861
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
862
name, type, domain, protocol, 0,
863
resolve_callback, NULL) == NULL)
857
864
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
858
name, avahi_strerror(avahi_server_errno(mc->server)));
865
name, avahi_strerror(avahi_server_errno(mc.server)));
861
868
case AVAHI_BROWSER_REMOVE:
880
sig_atomic_t quit_now = 0;
882
/* stop main loop after sigterm has been called */
883
static void handle_sigterm(__attribute__((unused)) int sig){
888
int old_errno = errno;
889
if(mc.simple_poll != NULL){
890
avahi_simple_poll_quit(mc.simple_poll);
873
895
int main(int argc, char *argv[]){
874
896
AvahiSServiceBrowser *sb = NULL;
879
901
int exitcode = EXIT_SUCCESS;
880
902
const char *interface = "eth0";
881
903
struct ifreq network;
889
911
const char *seckey = PATHDIR "/" SECKEY;
890
912
const char *pubkey = PATHDIR "/" PUBKEY;
892
mandos_context mc = { .simple_poll = NULL, .server = NULL,
893
.dh_bits = 1024, .priority = "SECURE256"
894
":!CTYPE-X.509:+CTYPE-OPENPGP" };
895
914
bool gnutls_initialized = false;
896
915
bool gpgme_initialized = false;
918
struct sigaction old_sigterm_action;
919
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
900
922
struct argp_option options[] = {
954
976
case 129: /* --dh-bits */
955
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
956
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
957
or arg[numchars] != '\0'){
978
tmpmax = strtoimax(arg, &tmp, 10);
979
if(errno != 0 or tmp == arg or *tmp != '\0'
980
or tmpmax != (typeof(mc.dh_bits))tmpmax){
958
981
fprintf(stderr, "Bad number of DH bits\n");
959
982
exit(EXIT_FAILURE);
964
987
mc.priority = arg;
966
989
case 131: /* --delay */
967
ret = sscanf(arg, "%lf%n", &delay, &numchars);
968
if(ret < 1 or arg[numchars] != '\0'){
991
delay = strtof(arg, &tmp);
992
if(errno != 0 or tmp == arg or *tmp != '\0'){
969
993
fprintf(stderr, "Bad delay\n");
970
994
exit(EXIT_FAILURE);
1020
avahi_set_log_function(empty_log);
1023
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1024
from the signal handler */
1025
/* Initialize the pseudo-RNG for Avahi */
1026
srand((unsigned int) time(NULL));
1027
mc.simple_poll = avahi_simple_poll_new();
1028
if(mc.simple_poll == NULL){
1029
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1030
exitcode = EXIT_FAILURE;
1034
sigemptyset(&sigterm_action.sa_mask);
1035
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1037
perror("sigaddset");
1038
exitcode = EXIT_FAILURE;
1041
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1043
perror("sigaddset");
1044
exitcode = EXIT_FAILURE;
1047
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1049
perror("sigaddset");
1050
exitcode = EXIT_FAILURE;
1053
ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1055
perror("sigaction");
1056
exitcode = EXIT_FAILURE;
995
1060
/* If the interface is down, bring it up */
996
1061
if(interface[0] != '\0'){
997
1062
#ifdef __linux__
1094
1159
perror("setuid");
1097
ret = init_gnutls_global(&mc, pubkey, seckey);
1162
ret = init_gnutls_global(pubkey, seckey);
1099
1164
fprintf(stderr, "init_gnutls_global failed\n");
1100
1165
exitcode = EXIT_FAILURE;
1110
1175
tempdir_created = true;
1112
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1177
if(not init_gpgme(pubkey, seckey, tempdir)){
1113
1178
fprintf(stderr, "init_gpgme failed\n");
1114
1179
exitcode = EXIT_FAILURE;
1139
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1140
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1141
or address[numchars+1] != '\0'){
1205
tmpmax = strtoimax(address+1, &tmp, 10);
1206
if(errno != 0 or tmp == address+1 or *tmp != '\0'
1207
or tmpmax != (uint16_t)tmpmax){
1142
1208
fprintf(stderr, "Bad port number\n");
1143
1209
exitcode = EXIT_FAILURE;
1156
ret = start_mandos_communication(address, port, if_index, &mc,
1222
ret = start_mandos_communication(address, port, if_index, af);
1159
1224
exitcode = EXIT_FAILURE;
1167
avahi_set_log_function(empty_log);
1170
/* Initialize the pseudo-RNG for Avahi */
1171
srand((unsigned int) time(NULL));
1173
/* Allocate main Avahi loop object */
1174
mc.simple_poll = avahi_simple_poll_new();
1175
if(mc.simple_poll == NULL){
1176
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1177
exitcode = EXIT_FAILURE;
1182
1232
AvahiServerConfig config;
1183
1233
/* Do not publish any local Zeroconf records */
1207
1257
/* Create the Avahi service browser */
1208
1258
sb = avahi_s_service_browser_new(mc.server, if_index,
1209
AVAHI_PROTO_INET6, "_mandos._tcp",
1210
NULL, 0, browse_callback, &mc);
1259
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1260
NULL, 0, browse_callback, NULL);
1211
1261
if(sb == NULL){
1212
1262
fprintf(stderr, "Failed to create service browser: %s\n",
1213
1263
avahi_strerror(avahi_server_errno(mc.server)));