75
75
argp_state, struct argp,
76
76
argp_parse(), ARGP_KEY_ARG,
77
77
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
#include <signal.h> /* sigemptyset(), sigaddset(),
79
sigaction(), SIGTERM, sigaction */
79
82
#include <sys/klog.h> /* klogctl() */
150
156
* Initialize GPGME.
152
static bool init_gpgme(mandos_context *mc, const char *seckey,
158
static bool init_gpgme(const char *seckey,
153
159
const char *pubkey, const char *tempdir){
155
161
gpgme_error_t rc;
179
rc = gpgme_op_import(mc->ctx, pgp_data);
185
rc = gpgme_op_import(mc.ctx, pgp_data);
180
186
if(rc != GPG_ERR_NO_ERROR){
181
187
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
182
188
gpgme_strsource(rc), gpgme_strerror(rc));
227
233
/* Create new GPGME "context" */
228
rc = gpgme_new(&(mc->ctx));
234
rc = gpgme_new(&(mc.ctx));
229
235
if(rc != GPG_ERR_NO_ERROR){
230
236
fprintf(stderr, "bad gpgme_new: %s: %s\n",
231
237
gpgme_strsource(rc), gpgme_strerror(rc));
243
249
* Decrypt OpenPGP data.
244
250
* Returns -1 on error
246
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
247
const char *cryptotext,
252
static ssize_t pgp_packet_decrypt(const char *cryptotext,
248
253
size_t crypto_size,
249
254
char **plaintext){
250
255
gpgme_data_t dh_crypto, dh_plain;
278
283
/* Decrypt data from the cryptotext data buffer to the plaintext
280
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
285
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
281
286
if(rc != GPG_ERR_NO_ERROR){
282
287
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
283
288
gpgme_strsource(rc), gpgme_strerror(rc));
284
289
plaintext_length = -1;
286
291
gpgme_decrypt_result_t result;
287
result = gpgme_op_decrypt_result(mc->ctx);
292
result = gpgme_op_decrypt_result(mc.ctx);
288
293
if(result == NULL){
289
294
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
382
387
fprintf(stderr, "GnuTLS: %s", string);
385
static int init_gnutls_global(mandos_context *mc,
386
const char *pubkeyfilename,
390
static int init_gnutls_global(const char *pubkeyfilename,
387
391
const char *seckeyfilename){
409
413
/* OpenPGP credentials */
410
gnutls_certificate_allocate_credentials(&mc->cred);
414
gnutls_certificate_allocate_credentials(&mc.cred);
411
415
if(ret != GNUTLS_E_SUCCESS){
412
416
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
427
431
ret = gnutls_certificate_set_openpgp_key_file
428
(mc->cred, pubkeyfilename, seckeyfilename,
432
(mc.cred, pubkeyfilename, seckeyfilename,
429
433
GNUTLS_OPENPGP_FMT_BASE64);
430
434
if(ret != GNUTLS_E_SUCCESS){
439
443
/* GnuTLS server initialization */
440
ret = gnutls_dh_params_init(&mc->dh_params);
444
ret = gnutls_dh_params_init(&mc.dh_params);
441
445
if(ret != GNUTLS_E_SUCCESS){
442
446
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
443
447
" %s\n", safer_gnutls_strerror(ret));
446
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
450
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
447
451
if(ret != GNUTLS_E_SUCCESS){
448
452
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
449
453
safer_gnutls_strerror(ret));
453
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
457
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
459
gnutls_certificate_free_credentials(mc->cred);
463
gnutls_certificate_free_credentials(mc.cred);
460
464
gnutls_global_deinit();
461
gnutls_dh_params_deinit(mc->dh_params);
465
gnutls_dh_params_deinit(mc.dh_params);
465
static int init_gnutls_session(mandos_context *mc,
466
gnutls_session_t *session){
469
static int init_gnutls_session(gnutls_session_t *session){
468
471
/* GnuTLS session creation */
469
472
ret = gnutls_init(session, GNUTLS_SERVER);
477
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
480
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
478
481
if(ret != GNUTLS_E_SUCCESS){
479
482
fprintf(stderr, "Syntax error at: %s\n", err);
480
483
fprintf(stderr, "GnuTLS error: %s\n",
487
490
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
489
492
if(ret != GNUTLS_E_SUCCESS){
490
493
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
491
494
safer_gnutls_strerror(ret));
497
500
gnutls_certificate_server_set_request(*session,
498
501
GNUTLS_CERT_IGNORE);
500
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
503
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
509
512
/* Called when a Mandos server is found */
510
513
static int start_mandos_communication(const char *ip, uint16_t port,
511
514
AvahiIfIndex if_index,
512
mandos_context *mc, int af){
733
736
gnutls_bye(session, GNUTLS_SHUT_RDWR);
735
738
if(buffer_length > 0){
736
decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
739
decrypted_buffer_size = pgp_packet_decrypt(buffer,
738
741
&decrypted_buffer);
739
742
if(decrypted_buffer_size >= 0){
785
788
AVAHI_GCC_UNUSED AvahiStringList *txt,
786
789
AVAHI_GCC_UNUSED AvahiLookupResultFlags
789
mandos_context *mc = userdata;
791
AVAHI_GCC_UNUSED void* userdata){
792
794
/* Called whenever a service has been resolved successfully or
797
799
case AVAHI_RESOLVER_FAILURE:
798
800
fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
799
801
" of type '%s' in domain '%s': %s\n", name, type, domain,
800
avahi_strerror(avahi_server_errno(mc->server)));
802
avahi_strerror(avahi_server_errno(mc.server)));
803
805
case AVAHI_RESOLVER_FOUND:
809
811
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
810
812
ip, (intmax_t)interface, port);
812
int ret = start_mandos_communication(ip, port, interface, mc,
814
int ret = start_mandos_communication(ip, port, interface,
813
815
avahi_proto_to_af(proto));
815
avahi_simple_poll_quit(mc->simple_poll);
817
avahi_simple_poll_quit(mc.simple_poll);
840
841
case AVAHI_BROWSER_FAILURE:
842
843
fprintf(stderr, "(Avahi browser) %s\n",
843
avahi_strerror(avahi_server_errno(mc->server)));
844
avahi_simple_poll_quit(mc->simple_poll);
844
avahi_strerror(avahi_server_errno(mc.server)));
845
avahi_simple_poll_quit(mc.simple_poll);
847
848
case AVAHI_BROWSER_NEW:
850
851
the callback function is called the Avahi server will free the
851
852
resolver for us. */
853
if(!(avahi_s_service_resolver_new(mc->server, interface,
854
if(!(avahi_s_service_resolver_new(mc.server, interface,
854
855
protocol, name, type, domain,
855
856
AVAHI_PROTO_INET6, 0,
856
resolve_callback, mc)))
857
resolve_callback, NULL)))
857
858
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
858
name, avahi_strerror(avahi_server_errno(mc->server)));
859
name, avahi_strerror(avahi_server_errno(mc.server)));
861
862
case AVAHI_BROWSER_REMOVE:
874
static void handle_sigterm(__attribute__((unused)) int sig){
875
int old_errno = errno;
876
avahi_simple_poll_quit(mc.simple_poll);
873
880
int main(int argc, char *argv[]){
874
881
AvahiSServiceBrowser *sb = NULL;
889
896
const char *seckey = PATHDIR "/" SECKEY;
890
897
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" };
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" };
895
903
bool gnutls_initialized = false;
896
904
bool gpgme_initialized = false;
897
905
double delay = 2.5;
907
struct sigaction old_sigterm_action;
908
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
900
911
struct argp_option options[] = {
1094
1105
perror("setuid");
1097
ret = init_gnutls_global(&mc, pubkey, seckey);
1108
ret = init_gnutls_global(pubkey, seckey);
1099
1110
fprintf(stderr, "init_gnutls_global failed\n");
1100
1111
exitcode = EXIT_FAILURE;
1110
1121
tempdir_created = true;
1112
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1123
if(not init_gpgme(pubkey, seckey, tempdir)){
1113
1124
fprintf(stderr, "init_gpgme failed\n");
1114
1125
exitcode = EXIT_FAILURE;
1156
ret = start_mandos_communication(address, port, if_index, &mc,
1167
ret = start_mandos_communication(address, port, if_index, af);
1159
1169
exitcode = EXIT_FAILURE;
1207
1217
/* Create the Avahi service browser */
1208
1218
sb = avahi_s_service_browser_new(mc.server, if_index,
1209
1219
AVAHI_PROTO_INET6, "_mandos._tcp",
1210
NULL, 0, browse_callback, &mc);
1220
NULL, 0, browse_callback, NULL);
1211
1221
if(sb == NULL){
1212
1222
fprintf(stderr, "Failed to create service browser: %s\n",
1213
1223
avahi_strerror(avahi_server_errno(mc.server)));
1228
sigemptyset(&sigterm_action.sa_mask);
1229
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1231
perror("sigaddset");
1232
exitcode = EXIT_FAILURE;
1235
ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1237
perror("sigaction");
1238
exitcode = EXIT_FAILURE;
1218
1242
/* Run the main loop */