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(), sigaction(), SIGTERM, sigaction */
79
80
#include <sys/klog.h> /* klogctl() */
150
154
* Initialize GPGME.
152
static bool init_gpgme(mandos_context *mc, const char *seckey,
156
static bool init_gpgme(const char *seckey,
153
157
const char *pubkey, const char *tempdir){
155
159
gpgme_error_t rc;
179
rc = gpgme_op_import(mc->ctx, pgp_data);
183
rc = gpgme_op_import(mc.ctx, pgp_data);
180
184
if(rc != GPG_ERR_NO_ERROR){
181
185
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
182
186
gpgme_strsource(rc), gpgme_strerror(rc));
227
231
/* Create new GPGME "context" */
228
rc = gpgme_new(&(mc->ctx));
232
rc = gpgme_new(&(mc.ctx));
229
233
if(rc != GPG_ERR_NO_ERROR){
230
234
fprintf(stderr, "bad gpgme_new: %s: %s\n",
231
235
gpgme_strsource(rc), gpgme_strerror(rc));
243
247
* Decrypt OpenPGP data.
244
248
* Returns -1 on error
246
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
247
const char *cryptotext,
250
static ssize_t pgp_packet_decrypt(const char *cryptotext,
248
251
size_t crypto_size,
249
252
char **plaintext){
250
253
gpgme_data_t dh_crypto, dh_plain;
278
281
/* Decrypt data from the cryptotext data buffer to the plaintext
280
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
283
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
281
284
if(rc != GPG_ERR_NO_ERROR){
282
285
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
283
286
gpgme_strsource(rc), gpgme_strerror(rc));
284
287
plaintext_length = -1;
286
289
gpgme_decrypt_result_t result;
287
result = gpgme_op_decrypt_result(mc->ctx);
290
result = gpgme_op_decrypt_result(mc.ctx);
288
291
if(result == NULL){
289
292
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
382
385
fprintf(stderr, "GnuTLS: %s", string);
385
static int init_gnutls_global(mandos_context *mc,
386
const char *pubkeyfilename,
388
static int init_gnutls_global(const char *pubkeyfilename,
387
389
const char *seckeyfilename){
409
411
/* OpenPGP credentials */
410
gnutls_certificate_allocate_credentials(&mc->cred);
412
gnutls_certificate_allocate_credentials(&mc.cred);
411
413
if(ret != GNUTLS_E_SUCCESS){
412
414
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
427
429
ret = gnutls_certificate_set_openpgp_key_file
428
(mc->cred, pubkeyfilename, seckeyfilename,
430
(mc.cred, pubkeyfilename, seckeyfilename,
429
431
GNUTLS_OPENPGP_FMT_BASE64);
430
432
if(ret != GNUTLS_E_SUCCESS){
439
441
/* GnuTLS server initialization */
440
ret = gnutls_dh_params_init(&mc->dh_params);
442
ret = gnutls_dh_params_init(&mc.dh_params);
441
443
if(ret != GNUTLS_E_SUCCESS){
442
444
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
443
445
" %s\n", safer_gnutls_strerror(ret));
446
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
448
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
447
449
if(ret != GNUTLS_E_SUCCESS){
448
450
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
449
451
safer_gnutls_strerror(ret));
453
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
455
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
459
gnutls_certificate_free_credentials(mc->cred);
461
gnutls_certificate_free_credentials(mc.cred);
460
462
gnutls_global_deinit();
461
gnutls_dh_params_deinit(mc->dh_params);
463
gnutls_dh_params_deinit(mc.dh_params);
465
static int init_gnutls_session(mandos_context *mc,
466
gnutls_session_t *session){
467
static int init_gnutls_session(gnutls_session_t *session){
468
469
/* GnuTLS session creation */
469
470
ret = gnutls_init(session, GNUTLS_SERVER);
477
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
478
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
478
479
if(ret != GNUTLS_E_SUCCESS){
479
480
fprintf(stderr, "Syntax error at: %s\n", err);
480
481
fprintf(stderr, "GnuTLS error: %s\n",
487
488
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
489
490
if(ret != GNUTLS_E_SUCCESS){
490
491
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
491
492
safer_gnutls_strerror(ret));
497
498
gnutls_certificate_server_set_request(*session,
498
499
GNUTLS_CERT_IGNORE);
500
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
501
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
509
510
/* Called when a Mandos server is found */
510
511
static int start_mandos_communication(const char *ip, uint16_t port,
511
512
AvahiIfIndex if_index,
512
mandos_context *mc, int af){
733
734
gnutls_bye(session, GNUTLS_SHUT_RDWR);
735
736
if(buffer_length > 0){
736
decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
737
decrypted_buffer_size = pgp_packet_decrypt(buffer,
738
739
&decrypted_buffer);
739
740
if(decrypted_buffer_size >= 0){
785
786
AVAHI_GCC_UNUSED AvahiStringList *txt,
786
787
AVAHI_GCC_UNUSED AvahiLookupResultFlags
789
mandos_context *mc = userdata;
789
__attribute__((unused)) void* userdata){
792
792
/* Called whenever a service has been resolved successfully or
797
797
case AVAHI_RESOLVER_FAILURE:
798
798
fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
799
799
" of type '%s' in domain '%s': %s\n", name, type, domain,
800
avahi_strerror(avahi_server_errno(mc->server)));
800
avahi_strerror(avahi_server_errno(mc.server)));
803
803
case AVAHI_RESOLVER_FOUND:
809
809
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
810
810
ip, (intmax_t)interface, port);
812
int ret = start_mandos_communication(ip, port, interface, mc,
812
int ret = start_mandos_communication(ip, port, interface,
813
813
avahi_proto_to_af(proto));
815
avahi_simple_poll_quit(mc->simple_poll);
815
avahi_simple_poll_quit(mc.simple_poll);
840
839
case AVAHI_BROWSER_FAILURE:
842
841
fprintf(stderr, "(Avahi browser) %s\n",
843
avahi_strerror(avahi_server_errno(mc->server)));
844
avahi_simple_poll_quit(mc->simple_poll);
842
avahi_strerror(avahi_server_errno(mc.server)));
843
avahi_simple_poll_quit(mc.simple_poll);
847
846
case AVAHI_BROWSER_NEW:
850
849
the callback function is called the Avahi server will free the
851
850
resolver for us. */
853
if(!(avahi_s_service_resolver_new(mc->server, interface,
852
if(!(avahi_s_service_resolver_new(mc.server, interface,
854
853
protocol, name, type, domain,
855
854
AVAHI_PROTO_INET6, 0,
856
resolve_callback, mc)))
855
resolve_callback, NULL)))
857
856
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
858
name, avahi_strerror(avahi_server_errno(mc->server)));
857
name, avahi_strerror(avahi_server_errno(mc.server)));
861
860
case AVAHI_BROWSER_REMOVE:
872
static void handle_sigterm(__attribute__((unused)) int sig){
873
int old_errno = errno;
874
avahi_simple_poll_quit(mc.simple_poll);
873
878
int main(int argc, char *argv[]){
874
879
AvahiSServiceBrowser *sb = NULL;
889
894
const char *seckey = PATHDIR "/" SECKEY;
890
895
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
897
bool gnutls_initialized = false;
896
898
bool gpgme_initialized = false;
897
899
double delay = 2.5;
901
struct sigaction old_sigterm_action;
902
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
904
/* Initialize mandos context */
905
mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
906
.dh_bits = 1024, .priority = "SECURE256"
907
":!CTYPE-X.509:+CTYPE-OPENPGP" };
900
910
struct argp_option options[] = {
1094
1104
perror("setuid");
1097
ret = init_gnutls_global(&mc, pubkey, seckey);
1107
ret = init_gnutls_global(pubkey, seckey);
1099
1109
fprintf(stderr, "init_gnutls_global failed\n");
1100
1110
exitcode = EXIT_FAILURE;
1110
1120
tempdir_created = true;
1112
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1122
if(not init_gpgme(pubkey, seckey, tempdir)){
1113
1123
fprintf(stderr, "init_gpgme failed\n");
1114
1124
exitcode = EXIT_FAILURE;
1156
ret = start_mandos_communication(address, port, if_index, &mc,
1166
ret = start_mandos_communication(address, port, if_index,
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)));
1214
1224
exitcode = EXIT_FAILURE;
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 */