33
32
#define _LARGEFILE_SOURCE
34
33
#define _FILE_OFFSET_BITS 64
36
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY() */
38
#include <stdio.h> /* fprintf(), stderr, fwrite(),
39
stdout, ferror(), remove() */
37
#include <stdio.h> /* fprintf(), stderr, fwrite(), stdout, ferror() */
40
38
#include <stdint.h> /* uint16_t, uint32_t */
41
39
#include <stddef.h> /* NULL, size_t, ssize_t */
42
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
44
#include <stdbool.h> /* bool, false, true */
45
#include <string.h> /* memset(), strcmp(), strlen(),
46
strerror(), asprintf(), strcpy() */
47
#include <sys/ioctl.h> /* ioctl */
40
#include <stdlib.h> /* free() */
41
#include <stdbool.h> /* bool, true */
42
#include <string.h> /* memset(), strcmp(), strlen, strerror() */
43
#include <sys/ioctl.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
48
45
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
49
sockaddr_in6, PF_INET6,
50
SOCK_STREAM, uid_t, gid_t, open(),
52
#include <sys/stat.h> /* open() */
46
sockaddr_in6, PF_INET6, SOCK_STREAM, INET6_ADDRSTRLEN */
53
47
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
54
inet_pton(), connect() */
55
#include <fcntl.h> /* open() */
56
#include <dirent.h> /* opendir(), struct dirent, readdir()
58
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
60
#include <assert.h> /* assert() */
61
#include <errno.h> /* perror(), errno */
62
#include <time.h> /* nanosleep(), time() */
48
struct in6_addr, inet_pton(),
51
#include <errno.h> /* perror() */
63
53
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
64
54
SIOCSIFFLAGS, if_indextoname(),
65
55
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(),
56
#include <unistd.h> /* close(), SEEK_SET, off_t, write()*/
57
#include <netinet/in.h>
72
58
#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,
83
#include <sys/klog.h> /* klogctl() */
84
#endif /* __linux__ */
59
#include <iso646.h> /* not */
60
#include <argp.h> /* struct argp_option,
61
struct argp_state, struct argp,
87
/* All Avahi types, constants and functions
90
#include <avahi-core/core.h>
65
#include <avahi-core/core.h> /* AvahiSimplePoll, AvahiServer,
91
67
#include <avahi-core/lookup.h>
92
#include <avahi-core/log.h>
68
#include <avahi-core/log.h> /* AvahiLogLevel */
93
69
#include <avahi-common/simple-watch.h>
94
70
#include <avahi-common/malloc.h>
95
71
#include <avahi-common/error.h>
98
#include <gnutls/gnutls.h> /* All GnuTLS types, constants and
74
#include <gnutls/gnutls.h> /* gnutls_certificate_credentials_t,
78
gnutls_global_set_log_level(),
79
gnutls_global_set_log_function(),
80
gnutls_certificate_allocate_credentials(),
81
gnutls_global_deinit(),
82
gnutls_dh_params_init(),
83
gnutls_dh_params_generate(),
84
gnutls_certificate_set_dh_params(),
85
gnutls_certificate_free_credentials(),
86
gnutls_session_t, gnutls_init(),
87
gnutls_priority_set_direct(),
89
gnutls_credentials_set(),
90
gnutls_certificate_server_set_request(),
91
gnutls_dh_set_prime_bits(),
92
gnutls_transport_set_ptr(),
93
gnutls_transport_ptr_t,
96
gnutls_perror(), gnutls_bye(),
101
97
init_gnutls_session(),
103
#include <gnutls/openpgp.h>
104
/* gnutls_certificate_set_openpgp_key_file(),
105
GNUTLS_OPENPGP_FMT_BASE64 */
99
GNUTLS_CRD_CERTIFICATE,
101
GNUTLS_E_INTERRUPTED,
103
GNUTLS_E_REHANDSHAKE,
105
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
106
GNUTLS_OPENPGP_FMT_BASE64 */
108
#include <gpgme.h> /* All GPGME types, constants and
109
#include <gpgme.h> /* gpgme_data_t, gpgme_ctx_t,
110
gpgme_error_t, gpgme_engine_info_t,
111
gpgme_check_version(),
112
gpgme_engine_check_version(),
115
gpgme_get_engine_info(),
116
gpgme_set_engine_info(),
117
gpgme_data_new_from_mem(),
118
gpgme_data_new(), gpgme_new(),
120
gpgme_decrypt_result_t,
121
gpgme_op_decrypt_result(),
123
gpgme_pubkey_algo_name(),
111
127
GPGME_PROTOCOL_OpenPGP,
129
GPG_ERR_NO_SECKEY, */
114
131
#define BUFFER_SIZE 256
116
#define PATHDIR "/conf/conf.d/mandos"
117
#define SECKEY "seckey.txt"
118
#define PUBKEY "pubkey.txt"
120
133
bool debug = false;
134
static const char *keydir = "/conf/conf.d/mandos";
121
135
static const char mandos_protocol_version[] = "1";
122
const char *argp_program_version = "mandos-client " VERSION;
136
const char *argp_program_version = "mandosclient 0.9";
123
137
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
125
139
/* Used for passing in values through the Avahi callback functions */
167
* Decrypt OpenPGP data using keyrings in HOMEDIR.
168
* Returns -1 on error
161
static bool init_gpgme(const char *seckey,
162
const char *pubkey, const char *tempdir){
170
static ssize_t pgp_packet_decrypt (const char *cryptotext,
173
const char *homedir){
174
gpgme_data_t dh_crypto, dh_plain;
164
176
gpgme_error_t rc;
178
size_t plaintext_capacity = 0;
179
ssize_t plaintext_length = 0;
165
180
gpgme_engine_info_t engine_info;
169
* Helper function to insert pub and seckey to the engine keyring.
171
bool import_key(const char *filename){
173
gpgme_data_t pgp_data;
175
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
181
rc = gpgme_data_new_from_fd(&pgp_data, fd);
182
if(rc != GPG_ERR_NO_ERROR){
183
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
184
gpgme_strsource(rc), gpgme_strerror(rc));
188
rc = gpgme_op_import(mc.ctx, pgp_data);
189
if(rc != GPG_ERR_NO_ERROR){
190
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
191
gpgme_strsource(rc), gpgme_strerror(rc));
195
ret = (int)TEMP_FAILURE_RETRY(close(fd));
199
gpgme_data_release(pgp_data);
204
fprintf(stderr, "Initializing GPGME\n");
183
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
208
187
gpgme_check_version(NULL);
209
188
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
210
if(rc != GPG_ERR_NO_ERROR){
189
if (rc != GPG_ERR_NO_ERROR){
211
190
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
212
191
gpgme_strsource(rc), gpgme_strerror(rc));
216
/* Set GPGME home directory for the OpenPGP engine only */
217
rc = gpgme_get_engine_info(&engine_info);
218
if(rc != GPG_ERR_NO_ERROR){
195
/* Set GPGME home directory for the OpenPGP engine only */
196
rc = gpgme_get_engine_info (&engine_info);
197
if (rc != GPG_ERR_NO_ERROR){
219
198
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
220
199
gpgme_strsource(rc), gpgme_strerror(rc));
223
202
while(engine_info != NULL){
224
203
if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
225
204
gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
226
engine_info->file_name, tempdir);
205
engine_info->file_name, homedir);
229
208
engine_info = engine_info->next;
231
210
if(engine_info == NULL){
232
fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
236
/* Create new GPGME "context" */
237
rc = gpgme_new(&(mc.ctx));
238
if(rc != GPG_ERR_NO_ERROR){
239
fprintf(stderr, "bad gpgme_new: %s: %s\n",
240
gpgme_strsource(rc), gpgme_strerror(rc));
244
if(not import_key(pubkey) or not import_key(seckey)){
252
* Decrypt OpenPGP data.
253
* Returns -1 on error
255
static ssize_t pgp_packet_decrypt(const char *cryptotext,
258
gpgme_data_t dh_crypto, dh_plain;
261
size_t plaintext_capacity = 0;
262
ssize_t plaintext_length = 0;
265
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
211
fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
268
215
/* Create new GPGME data buffer from memory cryptotext */
269
216
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
271
if(rc != GPG_ERR_NO_ERROR){
218
if (rc != GPG_ERR_NO_ERROR){
272
219
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
273
220
gpgme_strsource(rc), gpgme_strerror(rc));
416
373
/* OpenPGP credentials */
417
gnutls_certificate_allocate_credentials(&mc.cred);
418
if(ret != GNUTLS_E_SUCCESS){
419
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
423
safer_gnutls_strerror(ret));
424
gnutls_global_deinit();
374
if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
375
!= GNUTLS_E_SUCCESS) {
376
fprintf (stderr, "GnuTLS memory error: %s\n",
377
safer_gnutls_strerror(ret));
378
gnutls_global_deinit ();
429
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
430
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
383
fprintf(stderr, "Attempting to use OpenPGP certificate %s"
384
" and keyfile %s as GnuTLS credentials\n", pubkeyfile,
434
388
ret = gnutls_certificate_set_openpgp_key_file
435
(mc.cred, pubkeyfilename, seckeyfilename,
436
GNUTLS_OPENPGP_FMT_BASE64);
437
if(ret != GNUTLS_E_SUCCESS){
389
(mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
390
if (ret != GNUTLS_E_SUCCESS) {
439
392
"Error[%d] while reading the OpenPGP key pair ('%s',"
440
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
441
fprintf(stderr, "The GnuTLS error is: %s\n",
393
" '%s')\n", ret, pubkeyfile, seckeyfile);
394
fprintf(stdout, "The GnuTLS error is: %s\n",
442
395
safer_gnutls_strerror(ret));
446
399
/* GnuTLS server initialization */
447
ret = gnutls_dh_params_init(&mc.dh_params);
448
if(ret != GNUTLS_E_SUCCESS){
449
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
450
" %s\n", safer_gnutls_strerror(ret));
453
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
454
if(ret != GNUTLS_E_SUCCESS){
455
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
456
safer_gnutls_strerror(ret));
460
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
400
ret = gnutls_dh_params_init(&mc->dh_params);
401
if (ret != GNUTLS_E_SUCCESS) {
402
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
403
" %s\n", safer_gnutls_strerror(ret));
406
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
407
if (ret != GNUTLS_E_SUCCESS) {
408
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
409
safer_gnutls_strerror(ret));
413
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
466
gnutls_certificate_free_credentials(mc.cred);
419
gnutls_certificate_free_credentials(mc->cred);
467
420
gnutls_global_deinit();
468
gnutls_dh_params_deinit(mc.dh_params);
472
static int init_gnutls_session(gnutls_session_t *session){
425
static int init_gnutls_session(mandos_context *mc,
426
gnutls_session_t *session){
474
428
/* GnuTLS session creation */
475
429
ret = gnutls_init(session, GNUTLS_SERVER);
476
if(ret != GNUTLS_E_SUCCESS){
430
if (ret != GNUTLS_E_SUCCESS){
477
431
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
478
432
safer_gnutls_strerror(ret));
483
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
484
if(ret != GNUTLS_E_SUCCESS){
437
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
438
if (ret != GNUTLS_E_SUCCESS) {
485
439
fprintf(stderr, "Syntax error at: %s\n", err);
486
440
fprintf(stderr, "GnuTLS error: %s\n",
487
441
safer_gnutls_strerror(ret));
488
gnutls_deinit(*session);
442
gnutls_deinit (*session);
493
447
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
495
if(ret != GNUTLS_E_SUCCESS){
449
if (ret != GNUTLS_E_SUCCESS) {
496
450
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
497
451
safer_gnutls_strerror(ret));
498
gnutls_deinit(*session);
452
gnutls_deinit (*session);
502
456
/* ignore client certificate if any. */
503
gnutls_certificate_server_set_request(*session,
457
gnutls_certificate_server_set_request (*session,
506
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
460
gnutls_dh_set_prime_bits (*session, mc->dh_bits);
877
sig_atomic_t quit_now = 0;
879
/* stop main loop after sigterm has been called */
880
static void handle_sigterm(__attribute__((unused)) int sig){
885
int old_errno = errno;
886
if(mc.simple_poll != NULL){
887
avahi_simple_poll_quit(mc.simple_poll);
771
/* Combines file name and path and returns the malloced new
772
string. some sane checks could/should be added */
773
static const char *combinepath(const char *first, const char *second){
774
size_t f_len = strlen(first);
775
size_t s_len = strlen(second);
776
char *tmp = malloc(f_len + s_len + 2);
781
memcpy(tmp, first, f_len); /* Spurious warning */
785
memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
787
tmp[f_len + 1 + s_len] = '\0';
892
792
int main(int argc, char *argv[]){
893
AvahiSServiceBrowser *sb = NULL;
898
int exitcode = EXIT_SUCCESS;
899
const char *interface = "eth0";
900
struct ifreq network;
904
char *connect_to = NULL;
905
char tempdir[] = "/tmp/mandosXXXXXX";
906
bool tempdir_created = false;
907
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
908
const char *seckey = PATHDIR "/" SECKEY;
909
const char *pubkey = PATHDIR "/" PUBKEY;
911
/* Initialize Mandos context */
912
mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
913
.dh_bits = 1024, .priority = "SECURE256"
914
":!CTYPE-X.509:+CTYPE-OPENPGP" };
915
bool gnutls_initialized = false;
916
bool gpgme_initialized = false;
919
struct sigaction old_sigterm_action;
920
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
923
struct argp_option options[] = {
924
{ .name = "debug", .key = 128,
925
.doc = "Debug mode", .group = 3 },
926
{ .name = "connect", .key = 'c',
927
.arg = "ADDRESS:PORT",
928
.doc = "Connect directly to a specific Mandos server",
930
{ .name = "interface", .key = 'i',
932
.doc = "Network interface that will be used to search for"
935
{ .name = "seckey", .key = 's',
937
.doc = "OpenPGP secret key file base name",
939
{ .name = "pubkey", .key = 'p',
941
.doc = "OpenPGP public key file base name",
943
{ .name = "dh-bits", .key = 129,
945
.doc = "Bit length of the prime number used in the"
946
" Diffie-Hellman key exchange",
948
{ .name = "priority", .key = 130,
950
.doc = "GnuTLS priority string for the TLS handshake",
952
{ .name = "delay", .key = 131,
954
.doc = "Maximum delay to wait for interface startup",
959
error_t parse_opt(int key, char *arg,
960
struct argp_state *state){
962
case 128: /* --debug */
965
case 'c': /* --connect */
968
case 'i': /* --interface */
971
case 's': /* --seckey */
974
case 'p': /* --pubkey */
977
case 129: /* --dh-bits */
979
tmpmax = strtoimax(arg, &tmp, 10);
980
if(errno != 0 or tmp == arg or *tmp != '\0'
981
or tmpmax != (typeof(mc.dh_bits))tmpmax){
982
fprintf(stderr, "Bad number of DH bits\n");
985
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
987
case 130: /* --priority */
990
case 131: /* --delay */
992
delay = strtof(arg, &tmp);
993
if(errno != 0 or tmp == arg or *tmp != '\0'){
994
fprintf(stderr, "Bad delay\n");
1003
return ARGP_ERR_UNKNOWN;
1008
struct argp argp = { .options = options, .parser = parse_opt,
1010
.doc = "Mandos client -- Get and decrypt"
1011
" passwords from a Mandos server" };
1012
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1013
if(ret == ARGP_ERR_UNKNOWN){
1014
fprintf(stderr, "Unknown error while parsing arguments\n");
1015
exitcode = EXIT_FAILURE;
1021
avahi_set_log_function(empty_log);
1024
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1025
from the signal handler */
1026
/* Initialize the pseudo-RNG for Avahi */
1027
srand((unsigned int) time(NULL));
1028
mc.simple_poll = avahi_simple_poll_new();
1029
if(mc.simple_poll == NULL){
1030
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1031
exitcode = EXIT_FAILURE;
1035
sigemptyset(&sigterm_action.sa_mask);
1036
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1038
perror("sigaddset");
1039
exitcode = EXIT_FAILURE;
1042
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1044
perror("sigaddset");
1045
exitcode = EXIT_FAILURE;
1048
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1050
perror("sigaddset");
1051
exitcode = EXIT_FAILURE;
1054
ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1056
perror("sigaction");
1057
exitcode = EXIT_FAILURE;
1061
/* If the interface is down, bring it up */
1062
if(interface[0] != '\0'){
1064
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1065
messages to mess up the prompt */
1066
ret = klogctl(8, NULL, 5);
1067
bool restore_loglevel = true;
1069
restore_loglevel = false;
1072
#endif /* __linux__ */
1074
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1077
exitcode = EXIT_FAILURE;
1079
if(restore_loglevel){
1080
ret = klogctl(7, NULL, 0);
1085
#endif /* __linux__ */
1088
strcpy(network.ifr_name, interface);
1089
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1091
perror("ioctl SIOCGIFFLAGS");
1093
if(restore_loglevel){
1094
ret = klogctl(7, NULL, 0);
1099
#endif /* __linux__ */
1100
exitcode = EXIT_FAILURE;
1103
if((network.ifr_flags & IFF_UP) == 0){
1104
network.ifr_flags |= IFF_UP;
1105
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1107
perror("ioctl SIOCSIFFLAGS");
1108
exitcode = EXIT_FAILURE;
1110
if(restore_loglevel){
1111
ret = klogctl(7, NULL, 0);
793
AvahiSServiceBrowser *sb = NULL;
796
int exitcode = EXIT_SUCCESS;
797
const char *interface = "eth0";
798
struct ifreq network;
802
char *connect_to = NULL;
803
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
804
const char *pubkeyfile = "pubkey.txt";
805
const char *seckeyfile = "seckey.txt";
806
mandos_context mc = { .simple_poll = NULL, .server = NULL,
807
.dh_bits = 1024, .priority = "SECURE256"};
808
bool gnutls_initalized = false;
811
struct argp_option options[] = {
812
{ .name = "debug", .key = 128,
813
.doc = "Debug mode", .group = 3 },
814
{ .name = "connect", .key = 'c',
816
.doc = "Connect directly to a sepcified mandos server",
818
{ .name = "interface", .key = 'i',
820
.doc = "Interface that Avahi will conntect through",
822
{ .name = "keydir", .key = 'd',
824
.doc = "Directory where the openpgp keyring is",
826
{ .name = "seckey", .key = 's',
828
.doc = "Secret openpgp key for gnutls authentication",
830
{ .name = "pubkey", .key = 'p',
832
.doc = "Public openpgp key for gnutls authentication",
834
{ .name = "dh-bits", .key = 129,
836
.doc = "dh-bits to use in gnutls communication",
838
{ .name = "priority", .key = 130,
840
.doc = "GNUTLS priority", .group = 1 },
845
error_t parse_opt (int key, char *arg,
846
struct argp_state *state) {
847
/* Get the INPUT argument from `argp_parse', which we know is
848
a pointer to our plugin list pointer. */
870
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
885
return ARGP_ERR_UNKNOWN;
1116
#endif /* __linux__ */
1120
/* sleep checking until interface is running */
1121
for(int i=0; i < delay * 4; i++){
1122
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1124
perror("ioctl SIOCGIFFLAGS");
1125
} else if(network.ifr_flags & IFF_RUNNING){
1128
struct timespec sleeptime = { .tv_nsec = 250000000 };
1129
ret = nanosleep(&sleeptime, NULL);
1130
if(ret == -1 and errno != EINTR){
1131
perror("nanosleep");
1134
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1139
if(restore_loglevel){
1140
/* Restores kernel loglevel to default */
1141
ret = klogctl(7, NULL, 0);
1146
#endif /* __linux__ */
1163
ret = init_gnutls_global(pubkey, seckey);
1165
fprintf(stderr, "init_gnutls_global failed\n");
1166
exitcode = EXIT_FAILURE;
1169
gnutls_initialized = true;
1172
if(mkdtemp(tempdir) == NULL){
1176
tempdir_created = true;
1178
if(not init_gpgme(pubkey, seckey, tempdir)){
1179
fprintf(stderr, "init_gpgme failed\n");
1180
exitcode = EXIT_FAILURE;
1183
gpgme_initialized = true;
1186
if(interface[0] != '\0'){
890
struct argp argp = { .options = options, .parser = parse_opt,
892
.doc = "Mandos client -- Get and decrypt"
893
" passwords from mandos server" };
894
argp_parse (&argp, argc, argv, 0, 0, NULL);
897
pubkeyfile = combinepath(keydir, pubkeyfile);
898
if (pubkeyfile == NULL){
899
perror("combinepath");
900
exitcode = EXIT_FAILURE;
904
seckeyfile = combinepath(keydir, seckeyfile);
905
if (seckeyfile == NULL){
906
perror("combinepath");
910
ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
912
fprintf(stderr, "init_gnutls_global\n");
915
gnutls_initalized = true;
1187
931
if_index = (AvahiIfIndex) if_nametoindex(interface);
1188
932
if(if_index == 0){
1189
933
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1190
exitcode = EXIT_FAILURE;
1195
if(connect_to != NULL){
1196
/* Connect directly, do not use Zeroconf */
1197
/* (Mainly meant for debugging) */
1198
char *address = strrchr(connect_to, ':');
1199
if(address == NULL){
1200
fprintf(stderr, "No colon in address\n");
1201
exitcode = EXIT_FAILURE;
1206
tmpmax = strtoimax(address+1, &tmp, 10);
1207
if(errno != 0 or tmp == address+1 or *tmp != '\0'
1208
or tmpmax != (uint16_t)tmpmax){
1209
fprintf(stderr, "Bad port number\n");
1210
exitcode = EXIT_FAILURE;
1213
port = (uint16_t)tmpmax;
1215
address = connect_to;
1216
/* Colon in address indicates IPv6 */
1218
if(strchr(address, ':') != NULL){
1223
ret = start_mandos_communication(address, port, if_index, af);
1225
exitcode = EXIT_FAILURE;
1227
exitcode = EXIT_SUCCESS;
1233
AvahiServerConfig config;
1234
/* Do not publish any local Zeroconf records */
1235
avahi_server_config_init(&config);
1236
config.publish_hinfo = 0;
1237
config.publish_addresses = 0;
1238
config.publish_workstation = 0;
1239
config.publish_domain = 0;
1241
/* Allocate a new server */
1242
mc.server = avahi_server_new(avahi_simple_poll_get
1243
(mc.simple_poll), &config, NULL,
1246
/* Free the Avahi configuration data */
1247
avahi_server_config_free(&config);
1250
/* Check if creating the Avahi server object succeeded */
1251
if(mc.server == NULL){
1252
fprintf(stderr, "Failed to create Avahi server: %s\n",
1253
avahi_strerror(error));
1254
exitcode = EXIT_FAILURE;
1258
/* Create the Avahi service browser */
1259
sb = avahi_s_service_browser_new(mc.server, if_index,
1260
AVAHI_PROTO_INET6, "_mandos._tcp",
1261
NULL, 0, browse_callback, NULL);
1263
fprintf(stderr, "Failed to create service browser: %s\n",
1264
avahi_strerror(avahi_server_errno(mc.server)));
1265
exitcode = EXIT_FAILURE;
1269
/* Run the main loop */
1272
fprintf(stderr, "Starting Avahi loop search\n");
1275
avahi_simple_poll_loop(mc.simple_poll);
937
if(connect_to != NULL){
938
/* Connect directly, do not use Zeroconf */
939
/* (Mainly meant for debugging) */
940
char *address = strrchr(connect_to, ':');
942
fprintf(stderr, "No colon in address\n");
943
exitcode = EXIT_FAILURE;
947
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
949
perror("Bad port number");
950
exitcode = EXIT_FAILURE;
954
address = connect_to;
955
ret = start_mandos_communication(address, port, if_index, &mc);
957
exitcode = EXIT_FAILURE;
959
exitcode = EXIT_SUCCESS;
964
/* If the interface is down, bring it up */
966
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
969
exitcode = EXIT_FAILURE;
972
strcpy(network.ifr_name, interface); /* Spurious warning */
973
ret = ioctl(sd, SIOCGIFFLAGS, &network);
975
perror("ioctl SIOCGIFFLAGS");
976
exitcode = EXIT_FAILURE;
979
if((network.ifr_flags & IFF_UP) == 0){
980
network.ifr_flags |= IFF_UP;
981
ret = ioctl(sd, SIOCSIFFLAGS, &network);
983
perror("ioctl SIOCSIFFLAGS");
984
exitcode = EXIT_FAILURE;
992
avahi_set_log_function(empty_log);
995
/* Initialize the pseudo-RNG for Avahi */
996
srand((unsigned int) time(NULL));
998
/* Allocate main Avahi loop object */
999
mc.simple_poll = avahi_simple_poll_new();
1000
if (mc.simple_poll == NULL) {
1001
fprintf(stderr, "Avahi: Failed to create simple poll"
1003
exitcode = EXIT_FAILURE;
1008
AvahiServerConfig config;
1009
/* Do not publish any local Zeroconf records */
1010
avahi_server_config_init(&config);
1011
config.publish_hinfo = 0;
1012
config.publish_addresses = 0;
1013
config.publish_workstation = 0;
1014
config.publish_domain = 0;
1016
/* Allocate a new server */
1017
mc.server = avahi_server_new(avahi_simple_poll_get
1018
(mc.simple_poll), &config, NULL,
1021
/* Free the Avahi configuration data */
1022
avahi_server_config_free(&config);
1025
/* Check if creating the Avahi server object succeeded */
1026
if (mc.server == NULL) {
1027
fprintf(stderr, "Failed to create Avahi server: %s\n",
1028
avahi_strerror(error));
1029
exitcode = EXIT_FAILURE;
1033
/* Create the Avahi service browser */
1034
sb = avahi_s_service_browser_new(mc.server, if_index,
1036
"_mandos._tcp", NULL, 0,
1037
browse_callback, &mc);
1039
fprintf(stderr, "Failed to create service browser: %s\n",
1040
avahi_strerror(avahi_server_errno(mc.server)));
1041
exitcode = EXIT_FAILURE;
1045
/* Run the main loop */
1048
fprintf(stderr, "Starting Avahi loop search\n");
1051
avahi_simple_poll_loop(mc.simple_poll);
1280
fprintf(stderr, "%s exiting\n", argv[0]);
1283
/* Cleanup things */
1285
avahi_s_service_browser_free(sb);
1287
if(mc.server != NULL)
1288
avahi_server_free(mc.server);
1290
if(mc.simple_poll != NULL)
1291
avahi_simple_poll_free(mc.simple_poll);
1293
if(gnutls_initialized){
1294
gnutls_certificate_free_credentials(mc.cred);
1295
gnutls_global_deinit();
1296
gnutls_dh_params_deinit(mc.dh_params);
1299
if(gpgme_initialized){
1300
gpgme_release(mc.ctx);
1303
/* Removes the temp directory used by GPGME */
1304
if(tempdir_created){
1306
struct dirent *direntry;
1307
d = opendir(tempdir);
1309
if(errno != ENOENT){
1314
direntry = readdir(d);
1315
if(direntry == NULL){
1318
/* Skip "." and ".." */
1319
if(direntry->d_name[0] == '.'
1320
and (direntry->d_name[1] == '\0'
1321
or (direntry->d_name[1] == '.'
1322
and direntry->d_name[2] == '\0'))){
1325
char *fullname = NULL;
1326
ret = asprintf(&fullname, "%s/%s", tempdir,
1332
ret = remove(fullname);
1334
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
1341
ret = rmdir(tempdir);
1342
if(ret == -1 and errno != ENOENT){
1056
fprintf(stderr, "%s exiting\n", argv[0]);
1059
/* Cleanup things */
1061
avahi_s_service_browser_free(sb);
1063
if (mc.server != NULL)
1064
avahi_server_free(mc.server);
1066
if (mc.simple_poll != NULL)
1067
avahi_simple_poll_free(mc.simple_poll);
1071
if (gnutls_initalized){
1072
gnutls_certificate_free_credentials (mc.cred);
1073
gnutls_global_deinit ();