1
1
/* -*- coding: utf-8 -*- */
3
* Mandos-client - get and decrypt data from a Mandos server
3
* Mandos client - get and decrypt data from a Mandos server
5
5
* This program is partly derived from an example program for an Avahi
6
6
* service browser, downloaded from
9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2008,2009 Teddy Hogeborn
13
* Copyright © 2008,2009 Björn Påhlsson
12
* Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
15
14
* This program is free software: you can redistribute it and/or
16
15
* modify it under the terms of the GNU General Public License as
36
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
38
37
#include <stdio.h> /* fprintf(), stderr, fwrite(),
39
stdout, ferror(), sscanf(),
41
39
#include <stdint.h> /* uint16_t, uint32_t */
42
40
#include <stddef.h> /* NULL, size_t, ssize_t */
43
41
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
49
47
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
50
48
sockaddr_in6, PF_INET6,
51
49
SOCK_STREAM, INET6_ADDRSTRLEN,
52
uid_t, gid_t, open(), opendir(),
50
uid_t, gid_t, open(), opendir(), DIR */
54
51
#include <sys/stat.h> /* open() */
55
52
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
56
53
struct in6_addr, inet_pton(),
58
55
#include <fcntl.h> /* open() */
59
#include <dirent.h> /* opendir(), struct dirent, readdir()
61
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
56
#include <dirent.h> /* opendir(), struct dirent, readdir() */
57
#include <inttypes.h> /* PRIu16 */
62
58
#include <assert.h> /* assert() */
63
59
#include <errno.h> /* perror(), errno */
64
#include <time.h> /* nanosleep(), time() */
60
#include <time.h> /* time() */
65
61
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
66
62
SIOCSIFFLAGS, if_indextoname(),
67
63
if_nametoindex(), IF_NAMESIZE */
70
66
getuid(), getgid(), setuid(),
72
68
#include <arpa/inet.h> /* inet_pton(), htons */
73
#include <iso646.h> /* not, and, or */
69
#include <iso646.h> /* not, and */
74
70
#include <argp.h> /* struct argp_option, error_t, struct
75
71
argp_state, struct argp,
76
72
argp_parse(), ARGP_KEY_ARG,
77
73
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
#include <sys/klog.h> /* klogctl() */
81
76
/* All Avahi types, constants and functions
95
90
init_gnutls_session(),
97
#include <gnutls/openpgp.h>
98
/* gnutls_certificate_set_openpgp_key_file(),
92
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
99
93
GNUTLS_OPENPGP_FMT_BASE64 */
108
102
#define BUFFER_SIZE 256
105
#define PATHDIR "/conf/conf.d/mandos"
110
108
#define PATHDIR "/conf/conf.d/mandos"
111
109
#define SECKEY "seckey.txt"
112
#define PUBKEY "pubkey.txt"
110
#define PUBKEY "pupkey.txt"
114
112
bool debug = false;
115
113
static const char mandos_protocol_version[] = "1";
116
const char *argp_program_version = "mandos-client " VERSION;
114
const char *argp_program_version = "password-request 1.0";
117
115
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
119
117
/* Used for passing in values through the Avahi callback functions */
135
133
size_t adjustbuffer(char **buffer, size_t buffer_length,
136
134
size_t buffer_capacity){
137
if(buffer_length + BUFFER_SIZE > buffer_capacity){
135
if (buffer_length + BUFFER_SIZE > buffer_capacity){
138
136
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
142
140
buffer_capacity += BUFFER_SIZE;
153
151
gpgme_error_t rc;
154
152
gpgme_engine_info_t engine_info;
158
* Helper function to insert pub and seckey to the engine keyring.
156
* Helper function to insert pub and seckey to the enigne keyring.
160
158
bool import_key(const char *filename){
162
160
gpgme_data_t pgp_data;
164
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
162
fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
170
168
rc = gpgme_data_new_from_fd(&pgp_data, fd);
171
if(rc != GPG_ERR_NO_ERROR){
169
if (rc != GPG_ERR_NO_ERROR){
172
170
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
173
171
gpgme_strsource(rc), gpgme_strerror(rc));
177
175
rc = gpgme_op_import(mc->ctx, pgp_data);
178
if(rc != GPG_ERR_NO_ERROR){
176
if (rc != GPG_ERR_NO_ERROR){
179
177
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
180
178
gpgme_strsource(rc), gpgme_strerror(rc));
184
ret = (int)TEMP_FAILURE_RETRY(close(fd));
182
ret = TEMP_FAILURE_RETRY(close(fd));
193
191
fprintf(stderr, "Initialize gpgme\n");
197
195
gpgme_check_version(NULL);
198
196
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
199
if(rc != GPG_ERR_NO_ERROR){
197
if (rc != GPG_ERR_NO_ERROR){
200
198
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
201
199
gpgme_strsource(rc), gpgme_strerror(rc));
205
203
/* Set GPGME home directory for the OpenPGP engine only */
206
rc = gpgme_get_engine_info(&engine_info);
207
if(rc != GPG_ERR_NO_ERROR){
204
rc = gpgme_get_engine_info (&engine_info);
205
if (rc != GPG_ERR_NO_ERROR){
208
206
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
209
207
gpgme_strsource(rc), gpgme_strerror(rc));
221
219
fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
225
223
/* Create new GPGME "context" */
226
224
rc = gpgme_new(&(mc->ctx));
227
if(rc != GPG_ERR_NO_ERROR){
225
if (rc != GPG_ERR_NO_ERROR){
228
226
fprintf(stderr, "bad gpgme_new: %s: %s\n",
229
227
gpgme_strsource(rc), gpgme_strerror(rc));
233
if(not import_key(pubkey) or not import_key(seckey)){
231
if (not import_key(pubkey) or not import_key(seckey)){
241
239
* Decrypt OpenPGP data.
242
240
* Returns -1 on error
244
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
245
const char *cryptotext,
242
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
243
const char *cryptotext,
248
246
gpgme_data_t dh_crypto, dh_plain;
249
247
gpgme_error_t rc;
251
249
size_t plaintext_capacity = 0;
252
250
ssize_t plaintext_length = 0;
255
253
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
258
256
/* Create new GPGME data buffer from memory cryptotext */
259
257
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
261
if(rc != GPG_ERR_NO_ERROR){
259
if (rc != GPG_ERR_NO_ERROR){
262
260
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
263
261
gpgme_strsource(rc), gpgme_strerror(rc));
267
265
/* Create new empty GPGME data buffer for the plaintext */
268
266
rc = gpgme_data_new(&dh_plain);
269
if(rc != GPG_ERR_NO_ERROR){
267
if (rc != GPG_ERR_NO_ERROR){
270
268
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
271
269
gpgme_strsource(rc), gpgme_strerror(rc));
272
270
gpgme_data_release(dh_crypto);
276
274
/* Decrypt data from the cryptotext data buffer to the plaintext
278
276
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
279
if(rc != GPG_ERR_NO_ERROR){
277
if (rc != GPG_ERR_NO_ERROR){
280
278
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
281
279
gpgme_strsource(rc), gpgme_strerror(rc));
282
280
plaintext_length = -1;
284
282
gpgme_decrypt_result_t result;
285
283
result = gpgme_op_decrypt_result(mc->ctx);
287
285
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
289
287
fprintf(stderr, "Unsupported algorithm: %s\n",
318
316
/* Seek back to the beginning of the GPGME plaintext data buffer */
319
if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
320
perror("gpgme_data_seek");
317
if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
318
perror("pgpme_data_seek");
321
319
plaintext_length = -1;
322
320
goto decrypt_end;
327
325
plaintext_capacity = adjustbuffer(plaintext,
328
326
(size_t)plaintext_length,
329
327
plaintext_capacity);
330
if(plaintext_capacity == 0){
328
if (plaintext_capacity == 0){
331
329
perror("adjustbuffer");
332
330
plaintext_length = -1;
333
331
goto decrypt_end;
366
364
return plaintext_length;
369
static const char * safer_gnutls_strerror(int value) {
370
const char *ret = gnutls_strerror(value); /* Spurious warning from
371
-Wunreachable-code */
367
static const char * safer_gnutls_strerror (int value) {
368
const char *ret = gnutls_strerror (value); /* Spurious warning */
373
370
ret = "(unknown)";
392
389
ret = gnutls_global_init();
393
if(ret != GNUTLS_E_SUCCESS) {
394
fprintf(stderr, "GnuTLS global_init: %s\n",
395
safer_gnutls_strerror(ret));
390
if (ret != GNUTLS_E_SUCCESS) {
391
fprintf (stderr, "GnuTLS global_init: %s\n",
392
safer_gnutls_strerror(ret));
400
397
/* "Use a log level over 10 to enable all debugging options."
401
398
* - GnuTLS manual
407
404
/* OpenPGP credentials */
408
405
gnutls_certificate_allocate_credentials(&mc->cred);
409
if(ret != GNUTLS_E_SUCCESS){
410
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
414
safer_gnutls_strerror(ret));
415
gnutls_global_deinit();
406
if (ret != GNUTLS_E_SUCCESS){
407
fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
409
safer_gnutls_strerror(ret));
410
gnutls_global_deinit ();
425
420
ret = gnutls_certificate_set_openpgp_key_file
426
421
(mc->cred, pubkeyfilename, seckeyfilename,
427
422
GNUTLS_OPENPGP_FMT_BASE64);
428
if(ret != GNUTLS_E_SUCCESS) {
423
if (ret != GNUTLS_E_SUCCESS) {
430
425
"Error[%d] while reading the OpenPGP key pair ('%s',"
431
426
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
437
432
/* GnuTLS server initialization */
438
433
ret = gnutls_dh_params_init(&mc->dh_params);
439
if(ret != GNUTLS_E_SUCCESS) {
440
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
441
" %s\n", safer_gnutls_strerror(ret));
434
if (ret != GNUTLS_E_SUCCESS) {
435
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
436
" %s\n", safer_gnutls_strerror(ret));
444
439
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
445
if(ret != GNUTLS_E_SUCCESS) {
446
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
447
safer_gnutls_strerror(ret));
440
if (ret != GNUTLS_E_SUCCESS) {
441
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
442
safer_gnutls_strerror(ret));
466
460
/* GnuTLS session creation */
467
461
ret = gnutls_init(session, GNUTLS_SERVER);
468
if(ret != GNUTLS_E_SUCCESS){
462
if (ret != GNUTLS_E_SUCCESS){
469
463
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
470
464
safer_gnutls_strerror(ret));
475
469
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
476
if(ret != GNUTLS_E_SUCCESS) {
470
if (ret != GNUTLS_E_SUCCESS) {
477
471
fprintf(stderr, "Syntax error at: %s\n", err);
478
472
fprintf(stderr, "GnuTLS error: %s\n",
479
473
safer_gnutls_strerror(ret));
480
gnutls_deinit(*session);
474
gnutls_deinit (*session);
485
479
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
487
if(ret != GNUTLS_E_SUCCESS) {
481
if (ret != GNUTLS_E_SUCCESS) {
488
482
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
489
483
safer_gnutls_strerror(ret));
490
gnutls_deinit(*session);
484
gnutls_deinit (*session);
494
488
/* ignore client certificate if any. */
495
gnutls_certificate_server_set_request(*session,
489
gnutls_certificate_server_set_request (*session,
498
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
492
gnutls_dh_set_prime_bits (*session, mc->dh_bits);
521
514
char interface[IF_NAMESIZE];
522
515
gnutls_session_t session;
524
ret = init_gnutls_session(mc, &session);
517
ret = init_gnutls_session (mc, &session);
558
551
fprintf(stderr, "Bad address: %s\n", ip);
561
to.in6.sin6_port = htons(port); /* Spurious warnings from
563
-Wunreachable-code */
554
to.in6.sin6_port = htons(port); /* Spurious warning */
565
556
to.in6.sin6_scope_id = (uint32_t)if_index;
581
572
ret = connect(tcp_sd, &to.in, sizeof(to));
583
574
perror("connect");
587
578
const char *out = mandos_protocol_version;
590
581
size_t out_size = strlen(out);
591
ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
582
ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
592
583
out_size - written));
612
603
fprintf(stderr, "Establishing TLS session with %s\n", ip);
615
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
606
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
618
ret = gnutls_handshake(session);
609
ret = gnutls_handshake (session);
619
610
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
621
if(ret != GNUTLS_E_SUCCESS){
612
if (ret != GNUTLS_E_SUCCESS){
623
614
fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
638
629
buffer_capacity = adjustbuffer(&buffer, buffer_length,
639
630
buffer_capacity);
640
if(buffer_capacity == 0){
631
if (buffer_capacity == 0){
641
632
perror("adjustbuffer");
646
sret = gnutls_record_recv(session, buffer+buffer_length,
637
ret = gnutls_record_recv(session, buffer+buffer_length,
653
644
case GNUTLS_E_INTERRUPTED:
654
645
case GNUTLS_E_AGAIN:
656
647
case GNUTLS_E_REHANDSHAKE:
658
ret = gnutls_handshake(session);
649
ret = gnutls_handshake (session);
659
650
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
661
652
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
668
659
fprintf(stderr, "Unknown error while reading data from"
669
660
" encrypted session with Mandos server\n");
671
gnutls_bye(session, GNUTLS_SHUT_RDWR);
662
gnutls_bye (session, GNUTLS_SHUT_RDWR);
675
buffer_length += (size_t) sret;
666
buffer_length += (size_t) ret;
680
671
fprintf(stderr, "Closing TLS session\n");
683
gnutls_bye(session, GNUTLS_SHUT_RDWR);
674
gnutls_bye (session, GNUTLS_SHUT_RDWR);
685
if(buffer_length > 0){
676
if (buffer_length > 0){
686
677
decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
688
679
&decrypted_buffer);
689
if(decrypted_buffer_size >= 0){
680
if (decrypted_buffer_size >= 0){
691
682
while(written < (size_t) decrypted_buffer_size){
692
ret = (int)fwrite(decrypted_buffer + written, 1,
693
(size_t)decrypted_buffer_size - written,
683
ret = (int)fwrite (decrypted_buffer + written, 1,
684
(size_t)decrypted_buffer_size - written,
695
686
if(ret == 0 and ferror(stdout)){
697
688
fprintf(stderr, "Error writing encrypted data: %s\n",
756
747
avahi_address_snprint(ip, sizeof(ip), address);
758
749
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
759
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
760
ip, (intmax_t)interface, port);
750
PRIu16 ") on port %d\n", name, host_name, ip,
762
753
int ret = start_mandos_communication(ip, port, interface, mc);
764
755
avahi_simple_poll_quit(mc->simple_poll);
799
790
the callback function is called the Avahi server will free the
800
791
resolver for us. */
802
if(!(avahi_s_service_resolver_new(mc->server, interface,
793
if (!(avahi_s_service_resolver_new(mc->server, interface,
803
794
protocol, name, type, domain,
804
795
AVAHI_PROTO_INET6, 0,
805
796
resolve_callback, mc)))
834
823
char *connect_to = NULL;
835
824
char tempdir[] = "/tmp/mandosXXXXXX";
836
bool tempdir_created = false;
837
825
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
838
826
const char *seckey = PATHDIR "/" SECKEY;
839
827
const char *pubkey = PATHDIR "/" PUBKEY;
841
829
mandos_context mc = { .simple_poll = NULL, .server = NULL,
842
830
.dh_bits = 1024, .priority = "SECURE256"
843
831
":!CTYPE-X.509:+CTYPE-OPENPGP" };
844
bool gnutls_initialized = false;
845
bool gpgme_initialized = false;
832
bool gnutls_initalized = false;
833
bool pgpme_initalized = false;
849
836
struct argp_option options[] = {
876
863
.doc = "GnuTLS priority string for the TLS handshake",
878
{ .name = "delay", .key = 131,
880
.doc = "Maximum delay to wait for interface startup",
885
error_t parse_opt(int key, char *arg,
886
struct argp_state *state) {
868
error_t parse_opt (int key, char *arg,
869
struct argp_state *state) {
870
/* Get the INPUT argument from `argp_parse', which we know is
871
a pointer to our plugin list pointer. */
888
873
case 128: /* --debug */
903
888
case 129: /* --dh-bits */
904
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
905
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
906
or arg[numchars] != '\0'){
907
fprintf(stderr, "Bad number of DH bits\n");
890
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
908
893
exit(EXIT_FAILURE);
910
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
912
896
case 130: /* --priority */
913
897
mc.priority = arg;
915
case 131: /* --delay */
916
ret = sscanf(arg, "%lf%n", &delay, &numchars);
917
if(ret < 1 or arg[numchars] != '\0'){
918
fprintf(stderr, "Bad delay\n");
922
899
case ARGP_KEY_ARG:
924
901
case ARGP_KEY_END:
934
911
.doc = "Mandos client -- Get and decrypt"
935
912
" passwords from a Mandos server" };
936
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
937
if(ret == ARGP_ERR_UNKNOWN){
913
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
914
if (ret == ARGP_ERR_UNKNOWN){
938
915
fprintf(stderr, "Unknown error while parsing arguments\n");
939
916
exitcode = EXIT_FAILURE;
921
ret = init_gnutls_global(&mc, pubkey, seckey);
923
fprintf(stderr, "init_gnutls_global failed\n");
924
exitcode = EXIT_FAILURE;
927
gnutls_initalized = true;
930
if(mkdtemp(tempdir) == NULL){
936
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
937
fprintf(stderr, "pgpme_initalized failed\n");
938
exitcode = EXIT_FAILURE;
941
pgpme_initalized = true;
944
944
/* If the interface is down, bring it up */
946
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
947
messages to mess up the prompt */
948
ret = klogctl(8, NULL, 5);
953
946
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
955
948
perror("socket");
956
949
exitcode = EXIT_FAILURE;
957
ret = klogctl(7, NULL, 0);
963
952
strcpy(network.ifr_name, interface);
964
953
ret = ioctl(sd, SIOCGIFFLAGS, &network);
966
955
perror("ioctl SIOCGIFFLAGS");
967
ret = klogctl(7, NULL, 0);
971
956
exitcode = EXIT_FAILURE;
978
963
perror("ioctl SIOCSIFFLAGS");
979
964
exitcode = EXIT_FAILURE;
980
ret = klogctl(7, NULL, 0);
987
/* sleep checking until interface is running */
988
for(int i=0; i < delay * 4; i++){
989
ret = ioctl(sd, SIOCGIFFLAGS, &network);
991
perror("ioctl SIOCGIFFLAGS");
992
} else if(network.ifr_flags & IFF_RUNNING){
995
struct timespec sleeptime = { .tv_nsec = 250000000 };
996
nanosleep(&sleeptime, NULL);
998
ret = (int)TEMP_FAILURE_RETRY(close(sd));
968
ret = TEMP_FAILURE_RETRY(close(sd));
1000
970
perror("close");
1002
/* Restores kernel loglevel to default */
1003
ret = klogctl(7, NULL, 0);
1014
984
perror("setgid");
1022
ret = init_gnutls_global(&mc, pubkey, seckey);
1024
fprintf(stderr, "init_gnutls_global failed\n");
1025
exitcode = EXIT_FAILURE;
1028
gnutls_initialized = true;
1031
if(mkdtemp(tempdir) == NULL){
1035
tempdir_created = true;
1037
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1038
fprintf(stderr, "init_gpgme failed\n");
1039
exitcode = EXIT_FAILURE;
1042
gpgme_initialized = true;
1045
987
if_index = (AvahiIfIndex) if_nametoindex(interface);
1046
988
if(if_index == 0){
1047
989
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1048
exitcode = EXIT_FAILURE;
1052
993
if(connect_to != NULL){
1058
999
exitcode = EXIT_FAILURE;
1062
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1063
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1064
or address[numchars+1] != '\0'){
1065
fprintf(stderr, "Bad port number\n");
1003
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
1005
perror("Bad port number");
1066
1006
exitcode = EXIT_FAILURE;
1069
port = (uint16_t)tmpmax;
1070
1009
*address = '\0';
1071
1010
address = connect_to;
1072
1011
ret = start_mandos_communication(address, port, if_index, &mc);
1088
1027
/* Allocate main Avahi loop object */
1089
1028
mc.simple_poll = avahi_simple_poll_new();
1090
if(mc.simple_poll == NULL) {
1029
if (mc.simple_poll == NULL) {
1091
1030
fprintf(stderr, "Avahi: Failed to create simple poll"
1093
1032
exitcode = EXIT_FAILURE;
1135
1074
/* Run the main loop */
1138
1077
fprintf(stderr, "Starting Avahi loop search\n");
1141
1080
avahi_simple_poll_loop(mc.simple_poll);
1146
1085
fprintf(stderr, "%s exiting\n", argv[0]);
1149
1088
/* Cleanup things */
1151
1090
avahi_s_service_browser_free(sb);
1153
if(mc.server != NULL)
1092
if (mc.server != NULL)
1154
1093
avahi_server_free(mc.server);
1156
if(mc.simple_poll != NULL)
1095
if (mc.simple_poll != NULL)
1157
1096
avahi_simple_poll_free(mc.simple_poll);
1159
if(gnutls_initialized){
1098
if (gnutls_initalized){
1160
1099
gnutls_certificate_free_credentials(mc.cred);
1161
gnutls_global_deinit();
1162
gnutls_dh_params_deinit(mc.dh_params);
1100
gnutls_global_deinit ();
1165
if(gpgme_initialized){
1103
if(pgpme_initalized){
1166
1104
gpgme_release(mc.ctx);
1169
1107
/* Removes the temp directory used by GPGME */
1170
if(tempdir_created){
1108
if(tempdir[0] != '\0'){
1172
1110
struct dirent *direntry;
1173
1111
d = opendir(tempdir);
1175
if(errno != ENOENT){
1180
1116
direntry = readdir(d);
1181
1117
if(direntry == NULL){
1184
/* Skip "." and ".." */
1185
if(direntry->d_name[0] == '.'
1186
and (direntry->d_name[1] == '\0'
1187
or (direntry->d_name[1] == '.'
1188
and direntry->d_name[2] == '\0'))){
1191
char *fullname = NULL;
1192
ret = asprintf(&fullname, "%s/%s", tempdir,
1198
ret = remove(fullname);
1200
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
1120
if (direntry->d_type == DT_REG){
1121
char *fullname = NULL;
1122
ret = asprintf(&fullname, "%s/%s", tempdir,
1128
ret = unlink(fullname);
1130
fprintf(stderr, "unlink(\"%s\"): %s",
1131
fullname, strerror(errno));
1207
1137
ret = rmdir(tempdir);
1208
if(ret == -1 and errno != ENOENT){
1209
1139
perror("rmdir");
1213
1143
return exitcode;