32
33
#define _LARGEFILE_SOURCE
33
34
#define _FILE_OFFSET_BITS 64
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY() */
36
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
41
#include <net/if.h> /* if_nametoindex */
42
#include <sys/ioctl.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
38
#include <stdio.h> /* fprintf(), stderr, fwrite(),
39
stdout, ferror(), sscanf(),
41
#include <stdint.h> /* uint16_t, uint32_t */
42
#include <stddef.h> /* NULL, size_t, ssize_t */
43
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
45
#include <stdbool.h> /* bool, false, true */
46
#include <string.h> /* memset(), strcmp(), strlen(),
47
strerror(), asprintf(), strcpy() */
48
#include <sys/ioctl.h> /* ioctl */
49
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
50
sockaddr_in6, PF_INET6,
51
SOCK_STREAM, uid_t, gid_t, open(),
53
#include <sys/stat.h> /* open() */
54
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
55
inet_pton(), connect() */
56
#include <fcntl.h> /* open() */
57
#include <dirent.h> /* opendir(), struct dirent, readdir()
59
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
60
#include <assert.h> /* assert() */
61
#include <errno.h> /* perror(), errno */
62
#include <time.h> /* nanosleep(), time() */
44
63
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
64
SIOCSIFFLAGS, if_indextoname(),
65
if_nametoindex(), IF_NAMESIZE */
66
#include <netinet/in.h> /* IN6_IS_ADDR_LINKLOCAL,
67
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
69
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
70
getuid(), getgid(), setuid(),
72
#include <arpa/inet.h> /* inet_pton(), htons */
73
#include <iso646.h> /* not, or, and */
74
#include <argp.h> /* struct argp_option, error_t, struct
75
argp_state, struct argp,
76
argp_parse(), ARGP_KEY_ARG,
77
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
#include <signal.h> /* sigemptyset(), sigaddset(),
79
sigaction(), SIGTERM, sigaction */
82
#include <sys/klog.h> /* klogctl() */
86
/* All Avahi types, constants and functions
47
89
#include <avahi-core/core.h>
48
90
#include <avahi-core/lookup.h>
49
91
#include <avahi-core/log.h>
111
* Decrypt OpenPGP data using keyrings in HOMEDIR.
112
* Returns -1 on error
114
static ssize_t pgp_packet_decrypt (const char *cryptotext,
117
const char *homedir){
118
gpgme_data_t dh_crypto, dh_plain;
158
static bool init_gpgme(const char *seckey,
159
const char *pubkey, const char *tempdir){
120
161
gpgme_error_t rc;
122
size_t plaintext_capacity = 0;
123
ssize_t plaintext_length = 0;
124
162
gpgme_engine_info_t engine_info;
127
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
166
* Helper function to insert pub and seckey to the engine keyring.
168
bool import_key(const char *filename){
170
gpgme_data_t pgp_data;
172
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
178
rc = gpgme_data_new_from_fd(&pgp_data, fd);
179
if(rc != GPG_ERR_NO_ERROR){
180
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
181
gpgme_strsource(rc), gpgme_strerror(rc));
185
rc = gpgme_op_import(mc.ctx, pgp_data);
186
if(rc != GPG_ERR_NO_ERROR){
187
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
188
gpgme_strsource(rc), gpgme_strerror(rc));
192
ret = (int)TEMP_FAILURE_RETRY(close(fd));
196
gpgme_data_release(pgp_data);
201
fprintf(stderr, "Initializing GPGME\n");
131
205
gpgme_check_version(NULL);
132
206
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
133
if (rc != GPG_ERR_NO_ERROR){
207
if(rc != GPG_ERR_NO_ERROR){
134
208
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
135
209
gpgme_strsource(rc), gpgme_strerror(rc));
139
/* Set GPGME home directory for the OpenPGP engine only */
140
rc = gpgme_get_engine_info (&engine_info);
141
if (rc != GPG_ERR_NO_ERROR){
213
/* Set GPGME home directory for the OpenPGP engine only */
214
rc = gpgme_get_engine_info(&engine_info);
215
if(rc != GPG_ERR_NO_ERROR){
142
216
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
143
217
gpgme_strsource(rc), gpgme_strerror(rc));
146
220
while(engine_info != NULL){
147
221
if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
148
222
gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
149
engine_info->file_name, homedir);
223
engine_info->file_name, tempdir);
152
226
engine_info = engine_info->next;
154
228
if(engine_info == NULL){
155
fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
229
fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
233
/* Create new GPGME "context" */
234
rc = gpgme_new(&(mc.ctx));
235
if(rc != GPG_ERR_NO_ERROR){
236
fprintf(stderr, "bad gpgme_new: %s: %s\n",
237
gpgme_strsource(rc), gpgme_strerror(rc));
241
if(not import_key(pubkey) or not import_key(seckey)){
249
* Decrypt OpenPGP data.
250
* Returns -1 on error
252
static ssize_t pgp_packet_decrypt(const char *cryptotext,
255
gpgme_data_t dh_crypto, dh_plain;
258
size_t plaintext_capacity = 0;
259
ssize_t plaintext_length = 0;
262
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
159
265
/* Create new GPGME data buffer from memory cryptotext */
160
266
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
162
if (rc != GPG_ERR_NO_ERROR){
268
if(rc != GPG_ERR_NO_ERROR){
163
269
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
164
270
gpgme_strsource(rc), gpgme_strerror(rc));
317
413
/* OpenPGP credentials */
318
if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
319
!= GNUTLS_E_SUCCESS) {
320
fprintf (stderr, "GnuTLS memory error: %s\n",
321
safer_gnutls_strerror(ret));
322
gnutls_global_deinit ();
414
gnutls_certificate_allocate_credentials(&mc.cred);
415
if(ret != GNUTLS_E_SUCCESS){
416
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
420
safer_gnutls_strerror(ret));
421
gnutls_global_deinit();
327
fprintf(stderr, "Attempting to use OpenPGP certificate %s"
328
" and keyfile %s as GnuTLS credentials\n", pubkeyfile,
426
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
427
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
332
431
ret = gnutls_certificate_set_openpgp_key_file
333
(mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
334
if (ret != GNUTLS_E_SUCCESS) {
432
(mc.cred, pubkeyfilename, seckeyfilename,
433
GNUTLS_OPENPGP_FMT_BASE64);
434
if(ret != GNUTLS_E_SUCCESS){
336
436
"Error[%d] while reading the OpenPGP key pair ('%s',"
337
" '%s')\n", ret, pubkeyfile, seckeyfile);
338
fprintf(stdout, "The GnuTLS error is: %s\n",
437
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
438
fprintf(stderr, "The GnuTLS error is: %s\n",
339
439
safer_gnutls_strerror(ret));
343
443
/* GnuTLS server initialization */
344
ret = gnutls_dh_params_init(&mc->dh_params);
345
if (ret != GNUTLS_E_SUCCESS) {
346
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
347
" %s\n", safer_gnutls_strerror(ret));
350
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
351
if (ret != GNUTLS_E_SUCCESS) {
352
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
353
safer_gnutls_strerror(ret));
357
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
444
ret = gnutls_dh_params_init(&mc.dh_params);
445
if(ret != GNUTLS_E_SUCCESS){
446
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
447
" %s\n", safer_gnutls_strerror(ret));
450
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
451
if(ret != GNUTLS_E_SUCCESS){
452
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
453
safer_gnutls_strerror(ret));
457
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
363
gnutls_certificate_free_credentials (mc->cred);
364
gnutls_global_deinit ();
463
gnutls_certificate_free_credentials(mc.cred);
464
gnutls_global_deinit();
465
gnutls_dh_params_deinit(mc.dh_params);
369
static int init_gnutls_session(mandos_context *mc,
370
gnutls_session_t *session){
469
static int init_gnutls_session(gnutls_session_t *session){
372
471
/* GnuTLS session creation */
373
472
ret = gnutls_init(session, GNUTLS_SERVER);
374
if (ret != GNUTLS_E_SUCCESS){
473
if(ret != GNUTLS_E_SUCCESS){
375
474
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
376
475
safer_gnutls_strerror(ret));
381
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
382
if (ret != GNUTLS_E_SUCCESS) {
480
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
481
if(ret != GNUTLS_E_SUCCESS){
383
482
fprintf(stderr, "Syntax error at: %s\n", err);
384
483
fprintf(stderr, "GnuTLS error: %s\n",
385
484
safer_gnutls_strerror(ret));
386
gnutls_deinit (*session);
485
gnutls_deinit(*session);
391
490
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
393
if (ret != GNUTLS_E_SUCCESS) {
492
if(ret != GNUTLS_E_SUCCESS){
394
493
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
395
494
safer_gnutls_strerror(ret));
396
gnutls_deinit (*session);
495
gnutls_deinit(*session);
400
499
/* ignore client certificate if any. */
401
gnutls_certificate_server_set_request (*session,
500
gnutls_certificate_server_set_request(*session,
404
gnutls_dh_set_prime_bits (*session, mc->dh_bits);
503
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
715
/* Combines file name and path and returns the malloced new
716
string. some sane checks could/should be added */
717
static const char *combinepath(const char *first, const char *second){
718
size_t f_len = strlen(first);
719
size_t s_len = strlen(second);
720
char *tmp = malloc(f_len + s_len + 2);
725
memcpy(tmp, first, f_len); /* Spurious warning */
729
memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
731
tmp[f_len + 1 + s_len] = '\0';
874
static void handle_sigterm(__attribute__((unused)) int sig){
875
int old_errno = errno;
876
avahi_simple_poll_quit(mc.simple_poll);
736
880
int main(int argc, char *argv[]){
737
AvahiSServiceBrowser *sb = NULL;
740
int exitcode = EXIT_SUCCESS;
741
const char *interface = "eth0";
742
struct ifreq network;
746
char *connect_to = NULL;
747
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
748
const char *pubkeyfile = "pubkey.txt";
749
const char *seckeyfile = "seckey.txt";
750
mandos_context mc = { .simple_poll = NULL, .server = NULL,
751
.dh_bits = 1024, .priority = "SECURE256"};
752
bool gnutls_initalized = false;
755
struct argp_option options[] = {
756
{ .name = "debug", .key = 128,
757
.doc = "Debug mode", .group = 3 },
758
{ .name = "connect", .key = 'c',
760
.doc = "Connect directly to a sepcified mandos server",
762
{ .name = "interface", .key = 'i',
764
.doc = "Interface that Avahi will conntect through",
766
{ .name = "keydir", .key = 'd',
768
.doc = "Directory where the openpgp keyring is",
770
{ .name = "seckey", .key = 's',
772
.doc = "Secret openpgp key for gnutls authentication",
774
{ .name = "pubkey", .key = 'p',
776
.doc = "Public openpgp key for gnutls authentication",
778
{ .name = "dh-bits", .key = 129,
780
.doc = "dh-bits to use in gnutls communication",
782
{ .name = "priority", .key = 130,
784
.doc = "GNUTLS priority", .group = 1 },
881
AvahiSServiceBrowser *sb = NULL;
886
int exitcode = EXIT_SUCCESS;
887
const char *interface = "eth0";
888
struct ifreq network;
892
char *connect_to = NULL;
893
char tempdir[] = "/tmp/mandosXXXXXX";
894
bool tempdir_created = false;
895
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
896
const char *seckey = PATHDIR "/" SECKEY;
897
const char *pubkey = PATHDIR "/" PUBKEY;
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" };
903
bool gnutls_initialized = false;
904
bool gpgme_initialized = false;
789
error_t parse_opt (int key, char *arg,
790
struct argp_state *state) {
791
/* Get the INPUT argument from `argp_parse', which we know is
792
a pointer to our plugin list pointer. */
814
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
907
struct sigaction old_sigterm_action;
908
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
911
struct argp_option options[] = {
912
{ .name = "debug", .key = 128,
913
.doc = "Debug mode", .group = 3 },
914
{ .name = "connect", .key = 'c',
915
.arg = "ADDRESS:PORT",
916
.doc = "Connect directly to a specific Mandos server",
918
{ .name = "interface", .key = 'i',
920
.doc = "Network interface that will be used to search for"
923
{ .name = "seckey", .key = 's',
925
.doc = "OpenPGP secret key file base name",
927
{ .name = "pubkey", .key = 'p',
929
.doc = "OpenPGP public key file base name",
931
{ .name = "dh-bits", .key = 129,
933
.doc = "Bit length of the prime number used in the"
934
" Diffie-Hellman key exchange",
936
{ .name = "priority", .key = 130,
938
.doc = "GnuTLS priority string for the TLS handshake",
940
{ .name = "delay", .key = 131,
942
.doc = "Maximum delay to wait for interface startup",
947
error_t parse_opt(int key, char *arg,
948
struct argp_state *state){
950
case 128: /* --debug */
953
case 'c': /* --connect */
956
case 'i': /* --interface */
959
case 's': /* --seckey */
962
case 'p': /* --pubkey */
965
case 129: /* --dh-bits */
966
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
967
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
968
or arg[numchars] != '\0'){
969
fprintf(stderr, "Bad number of DH bits\n");
972
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
974
case 130: /* --priority */
977
case 131: /* --delay */
978
ret = sscanf(arg, "%lf%n", &delay, &numchars);
979
if(ret < 1 or arg[numchars] != '\0'){
980
fprintf(stderr, "Bad delay\n");
989
return ARGP_ERR_UNKNOWN;
994
struct argp argp = { .options = options, .parser = parse_opt,
996
.doc = "Mandos client -- Get and decrypt"
997
" passwords from a Mandos server" };
998
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
999
if(ret == ARGP_ERR_UNKNOWN){
1000
fprintf(stderr, "Unknown error while parsing arguments\n");
1001
exitcode = EXIT_FAILURE;
1006
/* If the interface is down, bring it up */
1007
if(interface[0] != '\0'){
1009
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1010
messages to mess up the prompt */
1011
ret = klogctl(8, NULL, 5);
1012
bool restore_loglevel = true;
1014
restore_loglevel = false;
1019
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1022
exitcode = EXIT_FAILURE;
1024
if(restore_loglevel){
1025
ret = klogctl(7, NULL, 0);
1033
strcpy(network.ifr_name, interface);
1034
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1036
perror("ioctl SIOCGIFFLAGS");
1038
if(restore_loglevel){
1039
ret = klogctl(7, NULL, 0);
1045
exitcode = EXIT_FAILURE;
1048
if((network.ifr_flags & IFF_UP) == 0){
1049
network.ifr_flags |= IFF_UP;
1050
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1052
perror("ioctl SIOCSIFFLAGS");
1053
exitcode = EXIT_FAILURE;
1055
if(restore_loglevel){
1056
ret = klogctl(7, NULL, 0);
829
return ARGP_ERR_UNKNOWN;
834
struct argp argp = { .options = options, .parser = parse_opt,
836
.doc = "Mandos client -- Get and decrypt"
837
" passwords from mandos server" };
838
argp_parse (&argp, argc, argv, 0, 0, NULL);
841
pubkeyfile = combinepath(keydir, pubkeyfile);
842
if (pubkeyfile == NULL){
843
perror("combinepath");
844
exitcode = EXIT_FAILURE;
848
seckeyfile = combinepath(keydir, seckeyfile);
849
if (seckeyfile == NULL){
850
perror("combinepath");
854
ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
856
fprintf(stderr, "init_gnutls_global\n");
859
gnutls_initalized = true;
1065
/* sleep checking until interface is running */
1066
for(int i=0; i < delay * 4; i++){
1067
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1069
perror("ioctl SIOCGIFFLAGS");
1070
} else if(network.ifr_flags & IFF_RUNNING){
1073
struct timespec sleeptime = { .tv_nsec = 250000000 };
1074
ret = nanosleep(&sleeptime, NULL);
1075
if(ret == -1 and errno != EINTR){
1076
perror("nanosleep");
1079
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1084
if(restore_loglevel){
1085
/* Restores kernel loglevel to default */
1086
ret = klogctl(7, NULL, 0);
1108
ret = init_gnutls_global(pubkey, seckey);
1110
fprintf(stderr, "init_gnutls_global failed\n");
1111
exitcode = EXIT_FAILURE;
1114
gnutls_initialized = true;
1117
if(mkdtemp(tempdir) == NULL){
1121
tempdir_created = true;
1123
if(not init_gpgme(pubkey, seckey, tempdir)){
1124
fprintf(stderr, "init_gpgme failed\n");
1125
exitcode = EXIT_FAILURE;
1128
gpgme_initialized = true;
1131
if(interface[0] != '\0'){
875
1132
if_index = (AvahiIfIndex) if_nametoindex(interface);
876
1133
if(if_index == 0){
877
1134
fprintf(stderr, "No such interface: \"%s\"\n", interface);
881
if(connect_to != NULL){
882
/* Connect directly, do not use Zeroconf */
883
/* (Mainly meant for debugging) */
884
char *address = strrchr(connect_to, ':');
886
fprintf(stderr, "No colon in address\n");
887
exitcode = EXIT_FAILURE;
891
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
893
perror("Bad port number");
894
exitcode = EXIT_FAILURE;
898
address = connect_to;
899
ret = start_mandos_communication(address, port, if_index, &mc);
901
exitcode = EXIT_FAILURE;
903
exitcode = EXIT_SUCCESS;
908
/* If the interface is down, bring it up */
910
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
913
exitcode = EXIT_FAILURE;
916
strcpy(network.ifr_name, interface); /* Spurious warning */
917
ret = ioctl(sd, SIOCGIFFLAGS, &network);
919
perror("ioctl SIOCGIFFLAGS");
920
exitcode = EXIT_FAILURE;
923
if((network.ifr_flags & IFF_UP) == 0){
924
network.ifr_flags |= IFF_UP;
925
ret = ioctl(sd, SIOCSIFFLAGS, &network);
927
perror("ioctl SIOCSIFFLAGS");
928
exitcode = EXIT_FAILURE;
936
avahi_set_log_function(empty_log);
939
/* Initialize the pseudo-RNG for Avahi */
940
srand((unsigned int) time(NULL));
942
/* Allocate main Avahi loop object */
943
mc.simple_poll = avahi_simple_poll_new();
944
if (mc.simple_poll == NULL) {
945
fprintf(stderr, "Avahi: Failed to create simple poll"
947
exitcode = EXIT_FAILURE;
952
AvahiServerConfig config;
953
/* Do not publish any local Zeroconf records */
954
avahi_server_config_init(&config);
955
config.publish_hinfo = 0;
956
config.publish_addresses = 0;
957
config.publish_workstation = 0;
958
config.publish_domain = 0;
960
/* Allocate a new server */
961
mc.server = avahi_server_new(avahi_simple_poll_get
962
(mc.simple_poll), &config, NULL,
965
/* Free the Avahi configuration data */
966
avahi_server_config_free(&config);
969
/* Check if creating the Avahi server object succeeded */
970
if (mc.server == NULL) {
971
fprintf(stderr, "Failed to create Avahi server: %s\n",
972
avahi_strerror(error));
973
exitcode = EXIT_FAILURE;
977
/* Create the Avahi service browser */
978
sb = avahi_s_service_browser_new(mc.server, if_index,
980
"_mandos._tcp", NULL, 0,
981
browse_callback, &mc);
983
fprintf(stderr, "Failed to create service browser: %s\n",
984
avahi_strerror(avahi_server_errno(mc.server)));
985
exitcode = EXIT_FAILURE;
989
/* Run the main loop */
992
fprintf(stderr, "Starting Avahi loop search\n");
995
avahi_simple_poll_loop(mc.simple_poll);
1135
exitcode = EXIT_FAILURE;
1140
if(connect_to != NULL){
1141
/* Connect directly, do not use Zeroconf */
1142
/* (Mainly meant for debugging) */
1143
char *address = strrchr(connect_to, ':');
1144
if(address == NULL){
1145
fprintf(stderr, "No colon in address\n");
1146
exitcode = EXIT_FAILURE;
1150
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1151
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1152
or address[numchars+1] != '\0'){
1153
fprintf(stderr, "Bad port number\n");
1154
exitcode = EXIT_FAILURE;
1157
port = (uint16_t)tmpmax;
1159
address = connect_to;
1160
/* Colon in address indicates IPv6 */
1162
if(strchr(address, ':') != NULL){
1167
ret = start_mandos_communication(address, port, if_index, af);
1169
exitcode = EXIT_FAILURE;
1171
exitcode = EXIT_SUCCESS;
1177
avahi_set_log_function(empty_log);
1180
/* Initialize the pseudo-RNG for Avahi */
1181
srand((unsigned int) time(NULL));
1183
/* Allocate main Avahi loop object */
1184
mc.simple_poll = avahi_simple_poll_new();
1185
if(mc.simple_poll == NULL){
1186
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1187
exitcode = EXIT_FAILURE;
1192
AvahiServerConfig config;
1193
/* Do not publish any local Zeroconf records */
1194
avahi_server_config_init(&config);
1195
config.publish_hinfo = 0;
1196
config.publish_addresses = 0;
1197
config.publish_workstation = 0;
1198
config.publish_domain = 0;
1200
/* Allocate a new server */
1201
mc.server = avahi_server_new(avahi_simple_poll_get
1202
(mc.simple_poll), &config, NULL,
1205
/* Free the Avahi configuration data */
1206
avahi_server_config_free(&config);
1209
/* Check if creating the Avahi server object succeeded */
1210
if(mc.server == NULL){
1211
fprintf(stderr, "Failed to create Avahi server: %s\n",
1212
avahi_strerror(error));
1213
exitcode = EXIT_FAILURE;
1217
/* Create the Avahi service browser */
1218
sb = avahi_s_service_browser_new(mc.server, if_index,
1219
AVAHI_PROTO_INET6, "_mandos._tcp",
1220
NULL, 0, browse_callback, NULL);
1222
fprintf(stderr, "Failed to create service browser: %s\n",
1223
avahi_strerror(avahi_server_errno(mc.server)));
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;
1242
/* Run the main loop */
1245
fprintf(stderr, "Starting Avahi loop search\n");
1248
avahi_simple_poll_loop(mc.simple_poll);
1000
fprintf(stderr, "%s exiting\n", argv[0]);
1003
/* Cleanup things */
1005
avahi_s_service_browser_free(sb);
1007
if (mc.server != NULL)
1008
avahi_server_free(mc.server);
1010
if (mc.simple_poll != NULL)
1011
avahi_simple_poll_free(mc.simple_poll);
1015
if (gnutls_initalized){
1016
gnutls_certificate_free_credentials (mc.cred);
1017
gnutls_global_deinit ();
1253
fprintf(stderr, "%s exiting\n", argv[0]);
1256
/* Cleanup things */
1258
avahi_s_service_browser_free(sb);
1260
if(mc.server != NULL)
1261
avahi_server_free(mc.server);
1263
if(mc.simple_poll != NULL)
1264
avahi_simple_poll_free(mc.simple_poll);
1266
if(gnutls_initialized){
1267
gnutls_certificate_free_credentials(mc.cred);
1268
gnutls_global_deinit();
1269
gnutls_dh_params_deinit(mc.dh_params);
1272
if(gpgme_initialized){
1273
gpgme_release(mc.ctx);
1276
/* Removes the temp directory used by GPGME */
1277
if(tempdir_created){
1279
struct dirent *direntry;
1280
d = opendir(tempdir);
1282
if(errno != ENOENT){
1287
direntry = readdir(d);
1288
if(direntry == NULL){
1291
/* Skip "." and ".." */
1292
if(direntry->d_name[0] == '.'
1293
and (direntry->d_name[1] == '\0'
1294
or (direntry->d_name[1] == '.'
1295
and direntry->d_name[2] == '\0'))){
1298
char *fullname = NULL;
1299
ret = asprintf(&fullname, "%s/%s", tempdir,
1305
ret = remove(fullname);
1307
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
1314
ret = rmdir(tempdir);
1315
if(ret == -1 and errno != ENOENT){