/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

* plugins.d/mandos-client.c (good_interface): Add error message.
  (up_interface): Use get_flags() and good_flags().  Also check the
                  IFF_RUNNING flags.
  (runnable_hook): Bug fix: Add NULL to execl() call.
  (main): Use network interfaces which are up and running in
          preference to any other interfaces.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id$ */
2
 
 
3
 
/* PLEASE NOTE *
4
 
 * This file demonstrates how to use Avahi's core API, this is
5
 
 * the embeddable mDNS stack for embedded applications.
 
1
/*  -*- coding: utf-8 -*- */
 
2
/*
 
3
 * Mandos-client - get and decrypt data from a Mandos server
6
4
 *
7
 
 * End user applications should *not* use this API and should use
8
 
 * the D-Bus or C APIs, please see
9
 
 * client-browse-services.c and glib-integration.c
10
 
 * 
11
 
 * I repeat, you probably do *not* want to use this example.
 
5
 * This program is partly derived from an example program for an Avahi
 
6
 * service browser, downloaded from
 
7
 * <http://avahi.org/browser/examples/core-browse-services.c>.  This
 
8
 * includes the following functions: "resolve_callback",
 
9
 * "browse_callback", and parts of "main".
 
10
 * 
 
11
 * Everything else is
 
12
 * Copyright © 2008-2011 Teddy Hogeborn
 
13
 * Copyright © 2008-2011 Björn Påhlsson
 
14
 * 
 
15
 * This program is free software: you can redistribute it and/or
 
16
 * modify it under the terms of the GNU General Public License as
 
17
 * published by the Free Software Foundation, either version 3 of the
 
18
 * License, or (at your option) any later version.
 
19
 * 
 
20
 * This program is distributed in the hope that it will be useful, but
 
21
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
23
 * General Public License for more details.
 
24
 * 
 
25
 * You should have received a copy of the GNU General Public License
 
26
 * along with this program.  If not, see
 
27
 * <http://www.gnu.org/licenses/>.
 
28
 * 
 
29
 * Contact the authors at <mandos@recompile.se>.
12
30
 */
13
31
 
14
 
/***
15
 
  This file is part of avahi.
16
 
 
17
 
  avahi is free software; you can redistribute it and/or modify it
18
 
  under the terms of the GNU Lesser General Public License as
19
 
  published by the Free Software Foundation; either version 2.1 of the
20
 
  License, or (at your option) any later version.
21
 
 
22
 
  avahi is distributed in the hope that it will be useful, but WITHOUT
23
 
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24
 
  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
25
 
  Public License for more details.
26
 
 
27
 
  You should have received a copy of the GNU Lesser General Public
28
 
  License along with avahi; if not, write to the Free Software
29
 
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30
 
  USA.
31
 
***/
32
 
 
 
32
/* Needed by GPGME, specifically gpgme_data_seek() */
 
33
#ifndef _LARGEFILE_SOURCE
33
34
#define _LARGEFILE_SOURCE
 
35
#endif
 
36
#ifndef _FILE_OFFSET_BITS
34
37
#define _FILE_OFFSET_BITS 64
35
 
 
36
 
#include <stdio.h>
37
 
#include <assert.h>
38
 
#include <stdlib.h>
39
 
#include <time.h>
40
 
#include <net/if.h>             /* if_nametoindex */
41
 
 
 
38
#endif
 
39
 
 
40
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
 
41
 
 
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
 
43
                                   stdout, ferror(), remove() */
 
44
#include <stdint.h>             /* uint16_t, uint32_t */
 
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
 
47
                                   strtof(), abort() */
 
48
#include <stdbool.h>            /* bool, false, true */
 
49
#include <string.h>             /* memset(), strcmp(), strlen(),
 
50
                                   strerror(), asprintf(), strcpy() */
 
51
#include <sys/ioctl.h>          /* ioctl */
 
52
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
 
53
                                   sockaddr_in6, PF_INET6,
 
54
                                   SOCK_STREAM, uid_t, gid_t, open(),
 
55
                                   opendir(), DIR */
 
56
#include <sys/stat.h>           /* open(), S_ISREG */
 
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
 
58
                                   inet_pton(), connect() */
 
59
#include <fcntl.h>              /* open() */
 
60
#include <dirent.h>             /* opendir(), struct dirent, readdir()
 
61
                                 */
 
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
 
63
                                   strtoimax() */
 
64
#include <assert.h>             /* assert() */
 
65
#include <errno.h>              /* perror(), errno,
 
66
                                   program_invocation_short_name */
 
67
#include <time.h>               /* nanosleep(), time(), sleep() */
 
68
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
 
69
                                   SIOCSIFFLAGS, if_indextoname(),
 
70
                                   if_nametoindex(), IF_NAMESIZE */
 
71
#include <netinet/in.h>         /* IN6_IS_ADDR_LINKLOCAL,
 
72
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
 
73
                                */
 
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
 
75
                                   getuid(), getgid(), seteuid(),
 
76
                                   setgid(), pause() */
 
77
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
78
#include <iso646.h>             /* not, or, and */
 
79
#include <argp.h>               /* struct argp_option, error_t, struct
 
80
                                   argp_state, struct argp,
 
81
                                   argp_parse(), ARGP_KEY_ARG,
 
82
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
 
83
#include <signal.h>             /* sigemptyset(), sigaddset(),
 
84
                                   sigaction(), SIGTERM, sig_atomic_t,
 
85
                                   raise() */
 
86
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
 
87
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
 
88
 
 
89
#ifdef __linux__
 
90
#include <sys/klog.h>           /* klogctl() */
 
91
#endif  /* __linux__ */
 
92
 
 
93
/* Avahi */
 
94
/* All Avahi types, constants and functions
 
95
 Avahi*, avahi_*,
 
96
 AVAHI_* */
42
97
#include <avahi-core/core.h>
43
98
#include <avahi-core/lookup.h>
44
99
#include <avahi-core/log.h>
46
101
#include <avahi-common/malloc.h>
47
102
#include <avahi-common/error.h>
48
103
 
49
 
//mandos client part
50
 
#include <sys/types.h>          /* socket(), setsockopt(), inet_pton() */
51
 
#include <sys/socket.h>         /* socket(), setsockopt(), struct sockaddr_in6, struct in6_addr, inet_pton() */
52
 
#include <gnutls/gnutls.h>      /* ALL GNUTLS STUFF */
53
 
#include <gnutls/openpgp.h>     /* gnutls with openpgp stuff */
54
 
 
55
 
#include <unistd.h>             /* close() */
56
 
#include <netinet/in.h>
57
 
#include <stdbool.h>            /* true */
58
 
#include <string.h>             /* memset */
59
 
#include <arpa/inet.h>          /* inet_pton() */
60
 
#include <iso646.h>             /* not */
61
 
 
62
 
// gpgme
63
 
#include <errno.h>              /* perror() */
64
 
#include <gpgme.h>
65
 
 
66
 
 
67
 
#ifndef CERT_ROOT
68
 
#define CERT_ROOT "/conf/conf.d/cryptkeyreq/"
69
 
#endif
70
 
#define CERTFILE CERT_ROOT "openpgp-client.txt"
71
 
#define KEYFILE CERT_ROOT "openpgp-client-key.txt"
 
104
/* GnuTLS */
 
105
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and
 
106
                                   functions:
 
107
                                   gnutls_*
 
108
                                   init_gnutls_session(),
 
109
                                   GNUTLS_* */
 
110
#include <gnutls/openpgp.h>
 
111
                          /* gnutls_certificate_set_openpgp_key_file(),
 
112
                                   GNUTLS_OPENPGP_FMT_BASE64 */
 
113
 
 
114
/* GPGME */
 
115
#include <gpgme.h>              /* All GPGME types, constants and
 
116
                                   functions:
 
117
                                   gpgme_*
 
118
                                   GPGME_PROTOCOL_OpenPGP,
 
119
                                   GPG_ERR_NO_* */
 
120
 
72
121
#define BUFFER_SIZE 256
73
 
#define DH_BITS 1024
74
 
 
 
122
 
 
123
#define PATHDIR "/conf/conf.d/mandos"
 
124
#define SECKEY "seckey.txt"
 
125
#define PUBKEY "pubkey.txt"
 
126
#define HOOKDIR "/lib/mandos/network-hooks.d"
 
127
 
 
128
bool debug = false;
 
129
static const char mandos_protocol_version[] = "1";
 
130
const char *argp_program_version = "mandos-client " VERSION;
 
131
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
132
static const char sys_class_net[] = "/sys/class/net";
 
133
char *connect_to = NULL;
 
134
 
 
135
/* Doubly linked list that need to be circularly linked when used */
 
136
typedef struct server{
 
137
  const char *ip;
 
138
  uint16_t port;
 
139
  AvahiIfIndex if_index;
 
140
  int af;
 
141
  struct timespec last_seen;
 
142
  struct server *next;
 
143
  struct server *prev;
 
144
} server;
 
145
 
 
146
/* Used for passing in values through the Avahi callback functions */
75
147
typedef struct {
76
 
  gnutls_session_t session;
 
148
  AvahiSimplePoll *simple_poll;
 
149
  AvahiServer *server;
77
150
  gnutls_certificate_credentials_t cred;
 
151
  unsigned int dh_bits;
78
152
  gnutls_dh_params_t dh_params;
79
 
} encrypted_session;
80
 
 
81
 
 
82
 
ssize_t gpg_packet_decrypt (char *packet, size_t packet_size, char **new_packet, char *homedir){
83
 
  gpgme_data_t dh_crypto, dh_plain;
 
153
  const char *priority;
84
154
  gpgme_ctx_t ctx;
 
155
  server *current_server;
 
156
} mandos_context;
 
157
 
 
158
/* global context so signal handler can reach it*/
 
159
mandos_context mc = { .simple_poll = NULL, .server = NULL,
 
160
                      .dh_bits = 1024, .priority = "SECURE256"
 
161
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
 
162
                      .current_server = NULL };
 
163
 
 
164
sig_atomic_t quit_now = 0;
 
165
int signal_received = 0;
 
166
 
 
167
/* Function to use when printing errors */
 
168
void perror_plus(const char *print_text){
 
169
  fprintf(stderr, "Mandos plugin %s: ",
 
170
          program_invocation_short_name);
 
171
  perror(print_text);
 
172
}
 
173
 
 
174
/*
 
175
 * Make additional room in "buffer" for at least BUFFER_SIZE more
 
176
 * bytes. "buffer_capacity" is how much is currently allocated,
 
177
 * "buffer_length" is how much is already used.
 
178
 */
 
179
size_t incbuffer(char **buffer, size_t buffer_length,
 
180
                  size_t buffer_capacity){
 
181
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
 
182
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
 
183
    if(buffer == NULL){
 
184
      return 0;
 
185
    }
 
186
    buffer_capacity += BUFFER_SIZE;
 
187
  }
 
188
  return buffer_capacity;
 
189
}
 
190
 
 
191
/* Add server to set of servers to retry periodically */
 
192
int add_server(const char *ip, uint16_t port,
 
193
                 AvahiIfIndex if_index,
 
194
                 int af){
 
195
  int ret;
 
196
  server *new_server = malloc(sizeof(server));
 
197
  if(new_server == NULL){
 
198
    perror_plus("malloc");
 
199
    return -1;
 
200
  }
 
201
  *new_server = (server){ .ip = strdup(ip),
 
202
                         .port = port,
 
203
                         .if_index = if_index,
 
204
                         .af = af };
 
205
  if(new_server->ip == NULL){
 
206
    perror_plus("strdup");
 
207
    return -1;
 
208
  }
 
209
  /* Special case of first server */
 
210
  if (mc.current_server == NULL){
 
211
    new_server->next = new_server;
 
212
    new_server->prev = new_server;
 
213
    mc.current_server = new_server;
 
214
  /* Place the new server last in the list */
 
215
  } else {
 
216
    new_server->next = mc.current_server;
 
217
    new_server->prev = mc.current_server->prev;
 
218
    new_server->prev->next = new_server;
 
219
    mc.current_server->prev = new_server;
 
220
  }
 
221
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
 
222
  if(ret == -1){
 
223
    perror_plus("clock_gettime");
 
224
    return -1;
 
225
  }
 
226
  return 0;
 
227
}
 
228
 
 
229
/* 
 
230
 * Initialize GPGME.
 
231
 */
 
232
static bool init_gpgme(const char *seckey,
 
233
                       const char *pubkey, const char *tempdir){
85
234
  gpgme_error_t rc;
86
 
  ssize_t ret;
87
 
  size_t new_packet_capacity = 0;
88
 
  size_t new_packet_length = 0;
89
235
  gpgme_engine_info_t engine_info;
90
 
 
 
236
  
 
237
  
 
238
  /*
 
239
   * Helper function to insert pub and seckey to the engine keyring.
 
240
   */
 
241
  bool import_key(const char *filename){
 
242
    int ret;
 
243
    int fd;
 
244
    gpgme_data_t pgp_data;
 
245
    
 
246
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
 
247
    if(fd == -1){
 
248
      perror_plus("open");
 
249
      return false;
 
250
    }
 
251
    
 
252
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
 
253
    if(rc != GPG_ERR_NO_ERROR){
 
254
      fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
 
255
              gpgme_strsource(rc), gpgme_strerror(rc));
 
256
      return false;
 
257
    }
 
258
    
 
259
    rc = gpgme_op_import(mc.ctx, pgp_data);
 
260
    if(rc != GPG_ERR_NO_ERROR){
 
261
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
 
262
              gpgme_strsource(rc), gpgme_strerror(rc));
 
263
      return false;
 
264
    }
 
265
    
 
266
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
 
267
    if(ret == -1){
 
268
      perror_plus("close");
 
269
    }
 
270
    gpgme_data_release(pgp_data);
 
271
    return true;
 
272
  }
 
273
  
 
274
  if(debug){
 
275
    fprintf(stderr, "Initializing GPGME\n");
 
276
  }
 
277
  
91
278
  /* Init GPGME */
92
279
  gpgme_check_version(NULL);
93
 
  gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
 
280
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
 
281
  if(rc != GPG_ERR_NO_ERROR){
 
282
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
 
283
            gpgme_strsource(rc), gpgme_strerror(rc));
 
284
    return false;
 
285
  }
94
286
  
95
 
  /* Set GPGME home directory */
96
 
  rc = gpgme_get_engine_info (&engine_info);
97
 
  if (rc != GPG_ERR_NO_ERROR){
 
287
  /* Set GPGME home directory for the OpenPGP engine only */
 
288
  rc = gpgme_get_engine_info(&engine_info);
 
289
  if(rc != GPG_ERR_NO_ERROR){
98
290
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
99
291
            gpgme_strsource(rc), gpgme_strerror(rc));
100
 
    return -1;
 
292
    return false;
101
293
  }
102
294
  while(engine_info != NULL){
103
295
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
104
296
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
105
 
                            engine_info->file_name, homedir);
 
297
                            engine_info->file_name, tempdir);
106
298
      break;
107
299
    }
108
300
    engine_info = engine_info->next;
109
301
  }
110
302
  if(engine_info == NULL){
111
 
    fprintf(stderr, "Could not set home dir to %s\n", homedir);
112
 
    return -1;
113
 
  }
114
 
  
115
 
  /* Create new GPGME data buffer from packet buffer */
116
 
  rc = gpgme_data_new_from_mem(&dh_crypto, packet, packet_size, 0);
117
 
  if (rc != GPG_ERR_NO_ERROR){
 
303
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
 
304
    return false;
 
305
  }
 
306
  
 
307
  /* Create new GPGME "context" */
 
308
  rc = gpgme_new(&(mc.ctx));
 
309
  if(rc != GPG_ERR_NO_ERROR){
 
310
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
 
311
            gpgme_strsource(rc), gpgme_strerror(rc));
 
312
    return false;
 
313
  }
 
314
  
 
315
  if(not import_key(pubkey) or not import_key(seckey)){
 
316
    return false;
 
317
  }
 
318
  
 
319
  return true;
 
320
}
 
321
 
 
322
/* 
 
323
 * Decrypt OpenPGP data.
 
324
 * Returns -1 on error
 
325
 */
 
326
static ssize_t pgp_packet_decrypt(const char *cryptotext,
 
327
                                  size_t crypto_size,
 
328
                                  char **plaintext){
 
329
  gpgme_data_t dh_crypto, dh_plain;
 
330
  gpgme_error_t rc;
 
331
  ssize_t ret;
 
332
  size_t plaintext_capacity = 0;
 
333
  ssize_t plaintext_length = 0;
 
334
  
 
335
  if(debug){
 
336
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
 
337
  }
 
338
  
 
339
  /* Create new GPGME data buffer from memory cryptotext */
 
340
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
 
341
                               0);
 
342
  if(rc != GPG_ERR_NO_ERROR){
118
343
    fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
119
344
            gpgme_strsource(rc), gpgme_strerror(rc));
120
345
    return -1;
122
347
  
123
348
  /* Create new empty GPGME data buffer for the plaintext */
124
349
  rc = gpgme_data_new(&dh_plain);
125
 
  if (rc != GPG_ERR_NO_ERROR){
 
350
  if(rc != GPG_ERR_NO_ERROR){
126
351
    fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
127
352
            gpgme_strsource(rc), gpgme_strerror(rc));
128
 
    return -1;
129
 
  }
130
 
  
131
 
  /* Create new GPGME "context" */
132
 
  rc = gpgme_new(&ctx);
133
 
  if (rc != GPG_ERR_NO_ERROR){
134
 
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
135
 
            gpgme_strsource(rc), gpgme_strerror(rc));
136
 
    return -1;
137
 
  }
138
 
  
139
 
  /* Decrypt data from the FILE pointer to the plaintext data buffer */
140
 
  rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
141
 
  if (rc != GPG_ERR_NO_ERROR){
 
353
    gpgme_data_release(dh_crypto);
 
354
    return -1;
 
355
  }
 
356
  
 
357
  /* Decrypt data from the cryptotext data buffer to the plaintext
 
358
     data buffer */
 
359
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
 
360
  if(rc != GPG_ERR_NO_ERROR){
142
361
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
143
362
            gpgme_strsource(rc), gpgme_strerror(rc));
144
 
    return -1;
 
363
    plaintext_length = -1;
 
364
    if(debug){
 
365
      gpgme_decrypt_result_t result;
 
366
      result = gpgme_op_decrypt_result(mc.ctx);
 
367
      if(result == NULL){
 
368
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
369
      } else {
 
370
        fprintf(stderr, "Unsupported algorithm: %s\n",
 
371
                result->unsupported_algorithm);
 
372
        fprintf(stderr, "Wrong key usage: %u\n",
 
373
                result->wrong_key_usage);
 
374
        if(result->file_name != NULL){
 
375
          fprintf(stderr, "File name: %s\n", result->file_name);
 
376
        }
 
377
        gpgme_recipient_t recipient;
 
378
        recipient = result->recipients;
 
379
        while(recipient != NULL){
 
380
          fprintf(stderr, "Public key algorithm: %s\n",
 
381
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
382
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
383
          fprintf(stderr, "Secret key available: %s\n",
 
384
                  recipient->status == GPG_ERR_NO_SECKEY
 
385
                  ? "No" : "Yes");
 
386
          recipient = recipient->next;
 
387
        }
 
388
      }
 
389
    }
 
390
    goto decrypt_end;
145
391
  }
146
392
  
147
 
/*   gpgme_decrypt_result_t result; */
148
 
/*   result = gpgme_op_decrypt_result(ctx); */
149
 
/*   fprintf(stderr, "Unsupported algorithm: %s\n", result->unsupported_algorithm); */
150
 
/*   fprintf(stderr, "Wrong key usage: %d\n", result->wrong_key_usage); */
151
 
/*   if(result->file_name != NULL){ */
152
 
/*     fprintf(stderr, "File name: %s\n", result->file_name); */
153
 
/*   } */
154
 
/*   gpgme_recipient_t recipient; */
155
 
/*   recipient = result->recipients; */
156
 
/*   if(recipient){ */
157
 
/*     while(recipient != NULL){ */
158
 
/*       fprintf(stderr, "Public key algorithm: %s\n", */
159
 
/*            gpgme_pubkey_algo_name(recipient->pubkey_algo)); */
160
 
/*       fprintf(stderr, "Key ID: %s\n", recipient->keyid); */
161
 
/*       fprintf(stderr, "Secret key available: %s\n", */
162
 
/*            recipient->status == GPG_ERR_NO_SECKEY ? "No" : "Yes"); */
163
 
/*       recipient = recipient->next; */
164
 
/*     } */
165
 
/*   } */
166
 
 
167
 
  /* Delete the GPGME FILE pointer cryptotext data buffer */
168
 
  gpgme_data_release(dh_crypto);
 
393
  if(debug){
 
394
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
 
395
  }
169
396
  
170
397
  /* Seek back to the beginning of the GPGME plaintext data buffer */
171
 
  gpgme_data_seek(dh_plain, 0, SEEK_SET);
172
 
 
173
 
  *new_packet = 0;
 
398
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
 
399
    perror_plus("gpgme_data_seek");
 
400
    plaintext_length = -1;
 
401
    goto decrypt_end;
 
402
  }
 
403
  
 
404
  *plaintext = NULL;
174
405
  while(true){
175
 
    if (new_packet_length + BUFFER_SIZE > new_packet_capacity){
176
 
      *new_packet = realloc(*new_packet, new_packet_capacity + BUFFER_SIZE);
177
 
      if (*new_packet == NULL){
178
 
        perror("realloc");
179
 
        return -1;
180
 
      }
181
 
      new_packet_capacity += BUFFER_SIZE;
 
406
    plaintext_capacity = incbuffer(plaintext,
 
407
                                      (size_t)plaintext_length,
 
408
                                      plaintext_capacity);
 
409
    if(plaintext_capacity == 0){
 
410
        perror_plus("incbuffer");
 
411
        plaintext_length = -1;
 
412
        goto decrypt_end;
182
413
    }
183
414
    
184
 
    ret = gpgme_data_read(dh_plain, *new_packet + new_packet_length, BUFFER_SIZE);
 
415
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
 
416
                          BUFFER_SIZE);
185
417
    /* Print the data, if any */
186
 
    if (ret == 0){
187
 
      /* If password is empty, then a incorrect error will be printed */
 
418
    if(ret == 0){
 
419
      /* EOF */
188
420
      break;
189
421
    }
190
422
    if(ret < 0){
191
 
      perror("gpgme_data_read");
192
 
      return -1;
193
 
    }
194
 
    new_packet_length += ret;
195
 
  }
196
 
 
197
 
   /* Delete the GPGME plaintext data buffer */
 
423
      perror_plus("gpgme_data_read");
 
424
      plaintext_length = -1;
 
425
      goto decrypt_end;
 
426
    }
 
427
    plaintext_length += ret;
 
428
  }
 
429
  
 
430
  if(debug){
 
431
    fprintf(stderr, "Decrypted password is: ");
 
432
    for(ssize_t i = 0; i < plaintext_length; i++){
 
433
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
 
434
    }
 
435
    fprintf(stderr, "\n");
 
436
  }
 
437
  
 
438
 decrypt_end:
 
439
  
 
440
  /* Delete the GPGME cryptotext data buffer */
 
441
  gpgme_data_release(dh_crypto);
 
442
  
 
443
  /* Delete the GPGME plaintext data buffer */
198
444
  gpgme_data_release(dh_plain);
199
 
  return new_packet_length;
 
445
  return plaintext_length;
200
446
}
201
447
 
202
 
static const char * safer_gnutls_strerror (int value) {
203
 
  const char *ret = gnutls_strerror (value);
204
 
  if (ret == NULL)
 
448
static const char * safer_gnutls_strerror(int value){
 
449
  const char *ret = gnutls_strerror(value); /* Spurious warning from
 
450
                                               -Wunreachable-code */
 
451
  if(ret == NULL)
205
452
    ret = "(unknown)";
206
453
  return ret;
207
454
}
208
455
 
209
 
void debuggnutls(int level, const char* string){
210
 
  fprintf(stderr, "%s", string);
 
456
/* GnuTLS log function callback */
 
457
static void debuggnutls(__attribute__((unused)) int level,
 
458
                        const char* string){
 
459
  fprintf(stderr, "GnuTLS: %s", string);
211
460
}
212
461
 
213
 
int initgnutls(encrypted_session *es){
214
 
  const char *err;
 
462
static int init_gnutls_global(const char *pubkeyfilename,
 
463
                              const char *seckeyfilename){
215
464
  int ret;
216
465
  
217
 
  if ((ret = gnutls_global_init ())
218
 
      != GNUTLS_E_SUCCESS) {
219
 
    fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
220
 
    return -1;
221
 
  }
222
 
 
223
 
  /* Uncomment to enable full debuggin on the gnutls library */
224
 
  /*   gnutls_global_set_log_level(11); */
225
 
  /*   gnutls_global_set_log_function(debuggnutls); */
226
 
 
227
 
 
228
 
  /* openpgp credentials */
229
 
  if ((ret = gnutls_certificate_allocate_credentials (&es->cred))
230
 
      != GNUTLS_E_SUCCESS) {
231
 
    fprintf (stderr, "memory error: %s\n", safer_gnutls_strerror(ret));
232
 
    return -1;
233
 
  }
234
 
 
 
466
  if(debug){
 
467
    fprintf(stderr, "Initializing GnuTLS\n");
 
468
  }
 
469
  
 
470
  ret = gnutls_global_init();
 
471
  if(ret != GNUTLS_E_SUCCESS){
 
472
    fprintf(stderr, "GnuTLS global_init: %s\n",
 
473
            safer_gnutls_strerror(ret));
 
474
    return -1;
 
475
  }
 
476
  
 
477
  if(debug){
 
478
    /* "Use a log level over 10 to enable all debugging options."
 
479
     * - GnuTLS manual
 
480
     */
 
481
    gnutls_global_set_log_level(11);
 
482
    gnutls_global_set_log_function(debuggnutls);
 
483
  }
 
484
  
 
485
  /* OpenPGP credentials */
 
486
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
487
  if(ret != GNUTLS_E_SUCCESS){
 
488
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
489
            safer_gnutls_strerror(ret));
 
490
    gnutls_global_deinit();
 
491
    return -1;
 
492
  }
 
493
  
 
494
  if(debug){
 
495
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
 
496
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
 
497
            seckeyfilename);
 
498
  }
 
499
  
235
500
  ret = gnutls_certificate_set_openpgp_key_file
236
 
    (es->cred, CERTFILE, KEYFILE, GNUTLS_OPENPGP_FMT_BASE64);
237
 
  if (ret != GNUTLS_E_SUCCESS) {
238
 
    fprintf
239
 
      (stderr, "Error[%d] while reading the OpenPGP key pair ('%s', '%s')\n",
240
 
       ret, CERTFILE, KEYFILE);
241
 
    fprintf(stdout, "The Error is: %s\n",
242
 
            safer_gnutls_strerror(ret));
243
 
    return -1;
244
 
  }
245
 
 
246
 
  //Gnutls server initialization
247
 
  if ((ret = gnutls_dh_params_init (&es->dh_params))
248
 
      != GNUTLS_E_SUCCESS) {
249
 
    fprintf (stderr, "Error in dh parameter initialization: %s\n",
250
 
             safer_gnutls_strerror(ret));
251
 
    return -1;
252
 
  }
253
 
 
254
 
  if ((ret = gnutls_dh_params_generate2 (es->dh_params, DH_BITS))
255
 
      != GNUTLS_E_SUCCESS) {
256
 
    fprintf (stderr, "Error in prime generation: %s\n",
257
 
             safer_gnutls_strerror(ret));
258
 
    return -1;
259
 
  }
260
 
 
261
 
  gnutls_certificate_set_dh_params (es->cred, es->dh_params);
262
 
 
263
 
  // Gnutls session creation
264
 
  if ((ret = gnutls_init (&es->session, GNUTLS_SERVER))
265
 
      != GNUTLS_E_SUCCESS){
266
 
    fprintf(stderr, "Error in gnutls session initialization: %s\n",
267
 
            safer_gnutls_strerror(ret));
268
 
  }
269
 
 
270
 
  if ((ret = gnutls_priority_set_direct (es->session, "NORMAL", &err))
271
 
      != GNUTLS_E_SUCCESS) {
272
 
    fprintf(stderr, "Syntax error at: %s\n", err);
273
 
    fprintf(stderr, "Gnutls error: %s\n",
274
 
            safer_gnutls_strerror(ret));
275
 
    return -1;
276
 
  }
277
 
 
278
 
  if ((ret = gnutls_credentials_set
279
 
       (es->session, GNUTLS_CRD_CERTIFICATE, es->cred))
280
 
      != GNUTLS_E_SUCCESS) {
281
 
    fprintf(stderr, "Error setting a credentials set: %s\n",
282
 
            safer_gnutls_strerror(ret));
283
 
    return -1;
284
 
  }
285
 
 
 
501
    (mc.cred, pubkeyfilename, seckeyfilename,
 
502
     GNUTLS_OPENPGP_FMT_BASE64);
 
503
  if(ret != GNUTLS_E_SUCCESS){
 
504
    fprintf(stderr,
 
505
            "Error[%d] while reading the OpenPGP key pair ('%s',"
 
506
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
507
    fprintf(stderr, "The GnuTLS error is: %s\n",
 
508
            safer_gnutls_strerror(ret));
 
509
    goto globalfail;
 
510
  }
 
511
  
 
512
  /* GnuTLS server initialization */
 
513
  ret = gnutls_dh_params_init(&mc.dh_params);
 
514
  if(ret != GNUTLS_E_SUCCESS){
 
515
    fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
 
516
            " %s\n", safer_gnutls_strerror(ret));
 
517
    goto globalfail;
 
518
  }
 
519
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
 
520
  if(ret != GNUTLS_E_SUCCESS){
 
521
    fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
 
522
            safer_gnutls_strerror(ret));
 
523
    goto globalfail;
 
524
  }
 
525
  
 
526
  gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
 
527
  
 
528
  return 0;
 
529
  
 
530
 globalfail:
 
531
  
 
532
  gnutls_certificate_free_credentials(mc.cred);
 
533
  gnutls_global_deinit();
 
534
  gnutls_dh_params_deinit(mc.dh_params);
 
535
  return -1;
 
536
}
 
537
 
 
538
static int init_gnutls_session(gnutls_session_t *session){
 
539
  int ret;
 
540
  /* GnuTLS session creation */
 
541
  do {
 
542
    ret = gnutls_init(session, GNUTLS_SERVER);
 
543
    if(quit_now){
 
544
      return -1;
 
545
    }
 
546
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
547
  if(ret != GNUTLS_E_SUCCESS){
 
548
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
 
549
            safer_gnutls_strerror(ret));
 
550
  }
 
551
  
 
552
  {
 
553
    const char *err;
 
554
    do {
 
555
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
 
556
      if(quit_now){
 
557
        gnutls_deinit(*session);
 
558
        return -1;
 
559
      }
 
560
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
561
    if(ret != GNUTLS_E_SUCCESS){
 
562
      fprintf(stderr, "Syntax error at: %s\n", err);
 
563
      fprintf(stderr, "GnuTLS error: %s\n",
 
564
              safer_gnutls_strerror(ret));
 
565
      gnutls_deinit(*session);
 
566
      return -1;
 
567
    }
 
568
  }
 
569
  
 
570
  do {
 
571
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
572
                                 mc.cred);
 
573
    if(quit_now){
 
574
      gnutls_deinit(*session);
 
575
      return -1;
 
576
    }
 
577
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
578
  if(ret != GNUTLS_E_SUCCESS){
 
579
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
 
580
            safer_gnutls_strerror(ret));
 
581
    gnutls_deinit(*session);
 
582
    return -1;
 
583
  }
 
584
  
286
585
  /* ignore client certificate if any. */
287
 
  gnutls_certificate_server_set_request (es->session, GNUTLS_CERT_IGNORE);
 
586
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
288
587
  
289
 
  gnutls_dh_set_prime_bits (es->session, DH_BITS);
 
588
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
290
589
  
291
590
  return 0;
292
591
}
293
592
 
294
 
void empty_log(AvahiLogLevel level, const char *txt){}
 
593
/* Avahi log function callback */
 
594
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
 
595
                      __attribute__((unused)) const char *txt){}
295
596
 
296
 
int start_mandos_communcation(char *ip, uint16_t port){
297
 
  int ret, tcp_sd;
298
 
  struct sockaddr_in6 to;
299
 
  struct in6_addr ip_addr;
300
 
  encrypted_session es;
 
597
/* Called when a Mandos server is found */
 
598
static int start_mandos_communication(const char *ip, uint16_t port,
 
599
                                      AvahiIfIndex if_index,
 
600
                                      int af){
 
601
  int ret, tcp_sd = -1;
 
602
  ssize_t sret;
 
603
  union {
 
604
    struct sockaddr_in in;
 
605
    struct sockaddr_in6 in6;
 
606
  } to;
301
607
  char *buffer = NULL;
302
 
  char *decrypted_buffer;
 
608
  char *decrypted_buffer = NULL;
303
609
  size_t buffer_length = 0;
304
610
  size_t buffer_capacity = 0;
305
 
  ssize_t decrypted_buffer_size;
306
 
  int retval = 0;
307
 
 
308
 
  
309
 
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
310
 
  if(tcp_sd < 0) {
311
 
    perror("socket");
312
 
    return -1;
313
 
  }
314
 
  
315
 
  ret = setsockopt(tcp_sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
316
 
  if(tcp_sd < 0) {
317
 
    perror("setsockopt bindtodevice");
318
 
    return -1;
319
 
  }
320
 
  
321
 
  memset(&to,0,sizeof(to));
322
 
  to.sin6_family = AF_INET6;
323
 
  ret = inet_pton(AF_INET6, ip, &ip_addr);
324
 
  if (ret < 0 ){
325
 
    perror("inet_pton");
326
 
    return -1;
327
 
  }  
 
611
  size_t written;
 
612
  int retval = -1;
 
613
  gnutls_session_t session;
 
614
  int pf;                       /* Protocol family */
 
615
  
 
616
  errno = 0;
 
617
  
 
618
  if(quit_now){
 
619
    errno = EINTR;
 
620
    return -1;
 
621
  }
 
622
  
 
623
  switch(af){
 
624
  case AF_INET6:
 
625
    pf = PF_INET6;
 
626
    break;
 
627
  case AF_INET:
 
628
    pf = PF_INET;
 
629
    break;
 
630
  default:
 
631
    fprintf(stderr, "Bad address family: %d\n", af);
 
632
    errno = EINVAL;
 
633
    return -1;
 
634
  }
 
635
  
 
636
  ret = init_gnutls_session(&session);
 
637
  if(ret != 0){
 
638
    return -1;
 
639
  }
 
640
  
 
641
  if(debug){
 
642
    fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
 
643
            "\n", ip, port);
 
644
  }
 
645
  
 
646
  tcp_sd = socket(pf, SOCK_STREAM, 0);
 
647
  if(tcp_sd < 0){
 
648
    int e = errno;
 
649
    perror_plus("socket");
 
650
    errno = e;
 
651
    goto mandos_end;
 
652
  }
 
653
  
 
654
  if(quit_now){
 
655
    errno = EINTR;
 
656
    goto mandos_end;
 
657
  }
 
658
  
 
659
  memset(&to, 0, sizeof(to));
 
660
  if(af == AF_INET6){
 
661
    to.in6.sin6_family = (sa_family_t)af;
 
662
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
 
663
  } else {                      /* IPv4 */
 
664
    to.in.sin_family = (sa_family_t)af;
 
665
    ret = inet_pton(af, ip, &to.in.sin_addr);
 
666
  }
 
667
  if(ret < 0 ){
 
668
    int e = errno;
 
669
    perror_plus("inet_pton");
 
670
    errno = e;
 
671
    goto mandos_end;
 
672
  }
328
673
  if(ret == 0){
 
674
    int e = errno;
329
675
    fprintf(stderr, "Bad address: %s\n", ip);
330
 
    return -1;
331
 
  }
332
 
  to.sin6_port = htons(port);
333
 
  to.sin6_scope_id = if_nametoindex("eth0");
334
 
  
335
 
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
336
 
  if (ret < 0){
337
 
    perror("connect");
338
 
    return -1;
339
 
  }
340
 
  
341
 
  ret = initgnutls (&es);
342
 
  if (ret != 0){
343
 
    retval = -1;
344
 
    return -1;
345
 
  }
346
 
    
347
 
  
348
 
  gnutls_transport_set_ptr (es.session, (gnutls_transport_ptr_t) tcp_sd);
349
 
 
350
 
  ret = gnutls_handshake (es.session);
351
 
  
352
 
  if (ret != GNUTLS_E_SUCCESS){
353
 
    fprintf(stderr, "\n*** Handshake failed ***\n");
354
 
    gnutls_perror (ret);
355
 
    retval = -1;
356
 
    goto exit;
357
 
  }
358
 
 
359
 
  //retrive password
360
 
  while(true){
361
 
    if (buffer_length + BUFFER_SIZE > buffer_capacity){
362
 
      buffer = realloc(buffer, buffer_capacity + BUFFER_SIZE);
363
 
      if (buffer == NULL){
364
 
        perror("realloc");
365
 
        goto exit;
366
 
      }
367
 
      buffer_capacity += BUFFER_SIZE;
368
 
    }
369
 
    
370
 
    ret = gnutls_record_recv
371
 
      (es.session, buffer+buffer_length, BUFFER_SIZE);
372
 
    if (ret == 0){
 
676
    errno = e;
 
677
    goto mandos_end;
 
678
  }
 
679
  if(af == AF_INET6){
 
680
    to.in6.sin6_port = htons(port); /* Spurious warnings from
 
681
                                       -Wconversion and
 
682
                                       -Wunreachable-code */
 
683
    
 
684
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
 
685
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
 
686
                              -Wunreachable-code*/
 
687
      if(if_index == AVAHI_IF_UNSPEC){
 
688
        fprintf(stderr, "An IPv6 link-local address is incomplete"
 
689
                " without a network interface\n");
 
690
        errno = EINVAL;
 
691
        goto mandos_end;
 
692
      }
 
693
      /* Set the network interface number as scope */
 
694
      to.in6.sin6_scope_id = (uint32_t)if_index;
 
695
    }
 
696
  } else {
 
697
    to.in.sin_port = htons(port); /* Spurious warnings from
 
698
                                     -Wconversion and
 
699
                                     -Wunreachable-code */
 
700
  }
 
701
  
 
702
  if(quit_now){
 
703
    errno = EINTR;
 
704
    goto mandos_end;
 
705
  }
 
706
  
 
707
  if(debug){
 
708
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
 
709
      char interface[IF_NAMESIZE];
 
710
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
711
        perror_plus("if_indextoname");
 
712
      } else {
 
713
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
 
714
                ip, interface, port);
 
715
      }
 
716
    } else {
 
717
      fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
718
              port);
 
719
    }
 
720
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
 
721
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
 
722
    const char *pcret;
 
723
    if(af == AF_INET6){
 
724
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
 
725
                        sizeof(addrstr));
 
726
    } else {
 
727
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
 
728
                        sizeof(addrstr));
 
729
    }
 
730
    if(pcret == NULL){
 
731
      perror_plus("inet_ntop");
 
732
    } else {
 
733
      if(strcmp(addrstr, ip) != 0){
 
734
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
 
735
      }
 
736
    }
 
737
  }
 
738
  
 
739
  if(quit_now){
 
740
    errno = EINTR;
 
741
    goto mandos_end;
 
742
  }
 
743
  
 
744
  if(af == AF_INET6){
 
745
    ret = connect(tcp_sd, &to.in6, sizeof(to));
 
746
  } else {
 
747
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
 
748
  }
 
749
  if(ret < 0){
 
750
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
751
      int e = errno;
 
752
      perror_plus("connect");
 
753
      errno = e;
 
754
    }
 
755
    goto mandos_end;
 
756
  }
 
757
  
 
758
  if(quit_now){
 
759
    errno = EINTR;
 
760
    goto mandos_end;
 
761
  }
 
762
  
 
763
  const char *out = mandos_protocol_version;
 
764
  written = 0;
 
765
  while(true){
 
766
    size_t out_size = strlen(out);
 
767
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
 
768
                                   out_size - written));
 
769
    if(ret == -1){
 
770
      int e = errno;
 
771
      perror_plus("write");
 
772
      errno = e;
 
773
      goto mandos_end;
 
774
    }
 
775
    written += (size_t)ret;
 
776
    if(written < out_size){
 
777
      continue;
 
778
    } else {
 
779
      if(out == mandos_protocol_version){
 
780
        written = 0;
 
781
        out = "\r\n";
 
782
      } else {
 
783
        break;
 
784
      }
 
785
    }
 
786
  
 
787
    if(quit_now){
 
788
      errno = EINTR;
 
789
      goto mandos_end;
 
790
    }
 
791
  }
 
792
  
 
793
  if(debug){
 
794
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
 
795
  }
 
796
  
 
797
  if(quit_now){
 
798
    errno = EINTR;
 
799
    goto mandos_end;
 
800
  }
 
801
  
 
802
  /* Spurious warning from -Wint-to-pointer-cast */
 
803
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
 
804
  
 
805
  if(quit_now){
 
806
    errno = EINTR;
 
807
    goto mandos_end;
 
808
  }
 
809
  
 
810
  do {
 
811
    ret = gnutls_handshake(session);
 
812
    if(quit_now){
 
813
      errno = EINTR;
 
814
      goto mandos_end;
 
815
    }
 
816
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
817
  
 
818
  if(ret != GNUTLS_E_SUCCESS){
 
819
    if(debug){
 
820
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
 
821
      gnutls_perror(ret);
 
822
    }
 
823
    errno = EPROTO;
 
824
    goto mandos_end;
 
825
  }
 
826
  
 
827
  /* Read OpenPGP packet that contains the wanted password */
 
828
  
 
829
  if(debug){
 
830
    fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
 
831
            ip);
 
832
  }
 
833
  
 
834
  while(true){
 
835
    
 
836
    if(quit_now){
 
837
      errno = EINTR;
 
838
      goto mandos_end;
 
839
    }
 
840
    
 
841
    buffer_capacity = incbuffer(&buffer, buffer_length,
 
842
                                   buffer_capacity);
 
843
    if(buffer_capacity == 0){
 
844
      int e = errno;
 
845
      perror_plus("incbuffer");
 
846
      errno = e;
 
847
      goto mandos_end;
 
848
    }
 
849
    
 
850
    if(quit_now){
 
851
      errno = EINTR;
 
852
      goto mandos_end;
 
853
    }
 
854
    
 
855
    sret = gnutls_record_recv(session, buffer+buffer_length,
 
856
                              BUFFER_SIZE);
 
857
    if(sret == 0){
373
858
      break;
374
859
    }
375
 
    if (ret < 0){
376
 
      switch(ret){
 
860
    if(sret < 0){
 
861
      switch(sret){
377
862
      case GNUTLS_E_INTERRUPTED:
378
863
      case GNUTLS_E_AGAIN:
379
864
        break;
380
865
      case GNUTLS_E_REHANDSHAKE:
381
 
        ret = gnutls_handshake (es.session);
382
 
        if (ret < 0){
383
 
          fprintf(stderr, "\n*** Handshake failed ***\n");
384
 
          gnutls_perror (ret);
385
 
          retval = -1;
386
 
          goto exit;
 
866
        do {
 
867
          ret = gnutls_handshake(session);
 
868
          
 
869
          if(quit_now){
 
870
            errno = EINTR;
 
871
            goto mandos_end;
 
872
          }
 
873
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
874
        if(ret < 0){
 
875
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
 
876
          gnutls_perror(ret);
 
877
          errno = EPROTO;
 
878
          goto mandos_end;
387
879
        }
388
880
        break;
389
881
      default:
390
 
        fprintf(stderr, "Unknown error while reading data from encrypted session with mandos server\n");
391
 
        retval = -1;
392
 
        gnutls_bye (es.session, GNUTLS_SHUT_RDWR);
393
 
        goto exit;
 
882
        fprintf(stderr, "Unknown error while reading data from"
 
883
                " encrypted session with Mandos server\n");
 
884
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
885
        errno = EIO;
 
886
        goto mandos_end;
394
887
      }
395
888
    } else {
396
 
      buffer_length += ret;
397
 
    }
398
 
  }
399
 
 
400
 
  if (buffer_length > 0){
401
 
    if ((decrypted_buffer_size = gpg_packet_decrypt(buffer, buffer_length, &decrypted_buffer, CERT_ROOT)) == 0){
 
889
      buffer_length += (size_t) sret;
 
890
    }
 
891
  }
 
892
  
 
893
  if(debug){
 
894
    fprintf(stderr, "Closing TLS session\n");
 
895
  }
 
896
  
 
897
  if(quit_now){
 
898
    errno = EINTR;
 
899
    goto mandos_end;
 
900
  }
 
901
  
 
902
  do {
 
903
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
904
    if(quit_now){
 
905
      errno = EINTR;
 
906
      goto mandos_end;
 
907
    }
 
908
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
909
  
 
910
  if(buffer_length > 0){
 
911
    ssize_t decrypted_buffer_size;
 
912
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
 
913
                                               buffer_length,
 
914
                                               &decrypted_buffer);
 
915
    if(decrypted_buffer_size >= 0){
 
916
      
 
917
      written = 0;
 
918
      while(written < (size_t) decrypted_buffer_size){
 
919
        if(quit_now){
 
920
          errno = EINTR;
 
921
          goto mandos_end;
 
922
        }
 
923
        
 
924
        ret = (int)fwrite(decrypted_buffer + written, 1,
 
925
                          (size_t)decrypted_buffer_size - written,
 
926
                          stdout);
 
927
        if(ret == 0 and ferror(stdout)){
 
928
          int e = errno;
 
929
          if(debug){
 
930
            fprintf(stderr, "Error writing encrypted data: %s\n",
 
931
                    strerror(errno));
 
932
          }
 
933
          errno = e;
 
934
          goto mandos_end;
 
935
        }
 
936
        written += (size_t)ret;
 
937
      }
 
938
      retval = 0;
 
939
    }
 
940
  }
 
941
  
 
942
  /* Shutdown procedure */
 
943
  
 
944
 mandos_end:
 
945
  {
 
946
    int e = errno;
 
947
    free(decrypted_buffer);
 
948
    free(buffer);
 
949
    if(tcp_sd >= 0){
 
950
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
951
    }
 
952
    if(ret == -1){
 
953
      if(e == 0){
 
954
        e = errno;
 
955
      }
 
956
      perror_plus("close");
 
957
    }
 
958
    gnutls_deinit(session);
 
959
    errno = e;
 
960
    if(quit_now){
 
961
      errno = EINTR;
402
962
      retval = -1;
403
 
    } else {
404
 
      fwrite (decrypted_buffer, 1, decrypted_buffer_size, stdout);
405
 
      free(decrypted_buffer);
406
963
    }
407
964
  }
408
 
 
409
 
  free(buffer);
410
 
 
411
 
  //shutdown procedure
412
 
  gnutls_bye (es.session, GNUTLS_SHUT_RDWR);
413
 
 exit:
414
 
  close(tcp_sd);
415
 
  gnutls_deinit (es.session);
416
 
  gnutls_certificate_free_credentials (es.cred);
417
 
  gnutls_global_deinit ();
418
965
  return retval;
419
966
}
420
967
 
421
 
static AvahiSimplePoll *simple_poll = NULL;
422
 
static AvahiServer *server = NULL;
423
 
 
424
 
static void resolve_callback(
425
 
    AvahiSServiceResolver *r,
426
 
    AVAHI_GCC_UNUSED AvahiIfIndex interface,
427
 
    AVAHI_GCC_UNUSED AvahiProtocol protocol,
428
 
    AvahiResolverEvent event,
429
 
    const char *name,
430
 
    const char *type,
431
 
    const char *domain,
432
 
    const char *host_name,
433
 
    const AvahiAddress *address,
434
 
    uint16_t port,
435
 
    AvahiStringList *txt,
436
 
    AvahiLookupResultFlags flags,
437
 
    AVAHI_GCC_UNUSED void* userdata) {
438
 
    
439
 
    assert(r);
440
 
 
441
 
    /* Called whenever a service has been resolved successfully or timed out */
442
 
 
443
 
    switch (event) {
444
 
        case AVAHI_RESOLVER_FAILURE:
445
 
            fprintf(stderr, "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_server_errno(server)));
446
 
            break;
447
 
 
448
 
        case AVAHI_RESOLVER_FOUND: {
449
 
          char ip[AVAHI_ADDRESS_STR_MAX];
450
 
            avahi_address_snprint(ip, sizeof(ip), address);
451
 
            int ret = start_mandos_communcation(ip, port);
452
 
            if (ret == 0){
453
 
              exit(EXIT_SUCCESS);
454
 
            } else {
455
 
              exit(EXIT_FAILURE);
456
 
            }
457
 
        }
458
 
    }
459
 
    avahi_s_service_resolver_free(r);
460
 
}
461
 
 
462
 
static void browse_callback(
463
 
    AvahiSServiceBrowser *b,
464
 
    AvahiIfIndex interface,
465
 
    AvahiProtocol protocol,
466
 
    AvahiBrowserEvent event,
467
 
    const char *name,
468
 
    const char *type,
469
 
    const char *domain,
470
 
    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
471
 
    void* userdata) {
472
 
    
473
 
    AvahiServer *s = userdata;
474
 
    assert(b);
475
 
 
476
 
    /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
477
 
 
478
 
    switch (event) {
479
 
 
480
 
        case AVAHI_BROWSER_FAILURE:
481
 
            
482
 
            fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_server_errno(server)));
483
 
            avahi_simple_poll_quit(simple_poll);
484
 
            return;
485
 
 
486
 
        case AVAHI_BROWSER_NEW:
487
 
            /* We ignore the returned resolver object. In the callback
488
 
               function we free it. If the server is terminated before
489
 
               the callback function is called the server will free
490
 
               the resolver for us. */
491
 
            
492
 
            if (!(avahi_s_service_resolver_new(s, interface, protocol, name, type, domain, AVAHI_PROTO_INET6, 0, resolve_callback, s)))
493
 
                fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_server_errno(s)));
494
 
            
495
 
            break;
496
 
 
497
 
        case AVAHI_BROWSER_REMOVE:
498
 
            break;
499
 
 
500
 
        case AVAHI_BROWSER_ALL_FOR_NOW:
501
 
        case AVAHI_BROWSER_CACHE_EXHAUSTED:
502
 
            break;
503
 
    }
504
 
}
505
 
 
506
 
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
 
968
static void resolve_callback(AvahiSServiceResolver *r,
 
969
                             AvahiIfIndex interface,
 
970
                             AvahiProtocol proto,
 
971
                             AvahiResolverEvent event,
 
972
                             const char *name,
 
973
                             const char *type,
 
974
                             const char *domain,
 
975
                             const char *host_name,
 
976
                             const AvahiAddress *address,
 
977
                             uint16_t port,
 
978
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
 
979
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
 
980
                             flags,
 
981
                             AVAHI_GCC_UNUSED void* userdata){
 
982
  assert(r);
 
983
  
 
984
  /* Called whenever a service has been resolved successfully or
 
985
     timed out */
 
986
  
 
987
  if(quit_now){
 
988
    return;
 
989
  }
 
990
  
 
991
  switch(event){
 
992
  default:
 
993
  case AVAHI_RESOLVER_FAILURE:
 
994
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
 
995
            " of type '%s' in domain '%s': %s\n", name, type, domain,
 
996
            avahi_strerror(avahi_server_errno(mc.server)));
 
997
    break;
 
998
    
 
999
  case AVAHI_RESOLVER_FOUND:
 
1000
    {
 
1001
      char ip[AVAHI_ADDRESS_STR_MAX];
 
1002
      avahi_address_snprint(ip, sizeof(ip), address);
 
1003
      if(debug){
 
1004
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
1005
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
 
1006
                ip, (intmax_t)interface, port);
 
1007
      }
 
1008
      int ret = start_mandos_communication(ip, port, interface,
 
1009
                                           avahi_proto_to_af(proto));
 
1010
      if(ret == 0){
 
1011
        avahi_simple_poll_quit(mc.simple_poll);
 
1012
      } else {
 
1013
        ret = add_server(ip, port, interface,
 
1014
                         avahi_proto_to_af(proto));
 
1015
      }
 
1016
    }
 
1017
  }
 
1018
  avahi_s_service_resolver_free(r);
 
1019
}
 
1020
 
 
1021
static void browse_callback(AvahiSServiceBrowser *b,
 
1022
                            AvahiIfIndex interface,
 
1023
                            AvahiProtocol protocol,
 
1024
                            AvahiBrowserEvent event,
 
1025
                            const char *name,
 
1026
                            const char *type,
 
1027
                            const char *domain,
 
1028
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
 
1029
                            flags,
 
1030
                            AVAHI_GCC_UNUSED void* userdata){
 
1031
  assert(b);
 
1032
  
 
1033
  /* Called whenever a new services becomes available on the LAN or
 
1034
     is removed from the LAN */
 
1035
  
 
1036
  if(quit_now){
 
1037
    return;
 
1038
  }
 
1039
  
 
1040
  switch(event){
 
1041
  default:
 
1042
  case AVAHI_BROWSER_FAILURE:
 
1043
    
 
1044
    fprintf(stderr, "(Avahi browser) %s\n",
 
1045
            avahi_strerror(avahi_server_errno(mc.server)));
 
1046
    avahi_simple_poll_quit(mc.simple_poll);
 
1047
    return;
 
1048
    
 
1049
  case AVAHI_BROWSER_NEW:
 
1050
    /* We ignore the returned Avahi resolver object. In the callback
 
1051
       function we free it. If the Avahi server is terminated before
 
1052
       the callback function is called the Avahi server will free the
 
1053
       resolver for us. */
 
1054
    
 
1055
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
 
1056
                                    name, type, domain, protocol, 0,
 
1057
                                    resolve_callback, NULL) == NULL)
 
1058
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
 
1059
              name, avahi_strerror(avahi_server_errno(mc.server)));
 
1060
    break;
 
1061
    
 
1062
  case AVAHI_BROWSER_REMOVE:
 
1063
    break;
 
1064
    
 
1065
  case AVAHI_BROWSER_ALL_FOR_NOW:
 
1066
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
 
1067
    if(debug){
 
1068
      fprintf(stderr, "No Mandos server found, still searching...\n");
 
1069
    }
 
1070
    break;
 
1071
  }
 
1072
}
 
1073
 
 
1074
/* Signal handler that stops main loop after SIGTERM */
 
1075
static void handle_sigterm(int sig){
 
1076
  if(quit_now){
 
1077
    return;
 
1078
  }
 
1079
  quit_now = 1;
 
1080
  signal_received = sig;
 
1081
  int old_errno = errno;
 
1082
  /* set main loop to exit */
 
1083
  if(mc.simple_poll != NULL){
 
1084
    avahi_simple_poll_quit(mc.simple_poll);
 
1085
  }
 
1086
  errno = old_errno;
 
1087
}
 
1088
 
 
1089
bool get_flags(const char *ifname, struct ifreq *ifr){
 
1090
  int ret;
 
1091
  
 
1092
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1093
  if(s < 0){
 
1094
    perror_plus("socket");
 
1095
    return false;
 
1096
  }
 
1097
  strcpy(ifr->ifr_name, ifname);
 
1098
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
 
1099
  if(ret == -1){
 
1100
    if(debug){
 
1101
      perror_plus("ioctl SIOCGIFFLAGS");
 
1102
    }
 
1103
    return false;
 
1104
  }
 
1105
  return true;
 
1106
}
 
1107
 
 
1108
bool good_flags(const char *ifname, const struct ifreq *ifr){
 
1109
  
 
1110
  /* Reject the loopback device */
 
1111
  if(ifr->ifr_flags & IFF_LOOPBACK){
 
1112
    if(debug){
 
1113
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1114
              ifname);
 
1115
    }
 
1116
    return false;
 
1117
  }
 
1118
  /* Accept point-to-point devices only if connect_to is specified */
 
1119
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
 
1120
    if(debug){
 
1121
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1122
              ifname);
 
1123
    }
 
1124
    return true;
 
1125
  }
 
1126
  /* Otherwise, reject non-broadcast-capable devices */
 
1127
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
 
1128
    if(debug){
 
1129
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1130
              ifname);
 
1131
    }
 
1132
    return false;
 
1133
  }
 
1134
  /* Reject non-ARP interfaces (including dummy interfaces) */
 
1135
  if(ifr->ifr_flags & IFF_NOARP){
 
1136
    if(debug){
 
1137
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1138
    }
 
1139
    return false;
 
1140
  }
 
1141
  
 
1142
  /* Accept this device */
 
1143
  if(debug){
 
1144
    fprintf(stderr, "Interface \"%s\" is good\n", ifname);
 
1145
  }
 
1146
  return true;
 
1147
}
 
1148
 
 
1149
/* 
 
1150
 * This function determines if a directory entry in /sys/class/net
 
1151
 * corresponds to an acceptable network device.
 
1152
 * (This function is passed to scandir(3) as a filter function.)
 
1153
 */
 
1154
int good_interface(const struct dirent *if_entry){
 
1155
  if(if_entry->d_name[0] == '.'){
 
1156
    return 0;
 
1157
  }
 
1158
  
 
1159
  struct ifreq ifr;
 
1160
  if(not get_flags(if_entry->d_name, &ifr)){
 
1161
    if(debug){
 
1162
      fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
 
1163
              if_entry->d_name);
 
1164
    }
 
1165
    return 0;
 
1166
  }
 
1167
  
 
1168
  if(not good_flags(if_entry->d_name, &ifr)){
 
1169
    return 0;
 
1170
  }
 
1171
  return 1;
 
1172
}
 
1173
 
 
1174
/* 
 
1175
 * This function determines if a directory entry in /sys/class/net
 
1176
 * corresponds to an acceptable network device which is up.
 
1177
 * (This function is passed to scandir(3) as a filter function.)
 
1178
 */
 
1179
int up_interface(const struct dirent *if_entry){
 
1180
  if(if_entry->d_name[0] == '.'){
 
1181
    return 0;
 
1182
  }
 
1183
  
 
1184
  struct ifreq ifr;
 
1185
  if(not get_flags(if_entry->d_name, &ifr)){
 
1186
    if(debug){
 
1187
      fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
 
1188
              if_entry->d_name);
 
1189
    }
 
1190
    return 0;
 
1191
  }
 
1192
  
 
1193
  /* Reject down interfaces */
 
1194
  if(not (ifr.ifr_flags & IFF_UP)){
 
1195
    if(debug){
 
1196
      fprintf(stderr, "Rejecting down interface \"%s\"\n",
 
1197
              if_entry->d_name);
 
1198
    }
 
1199
    return 0;
 
1200
  }
 
1201
  
 
1202
  /* Reject non-running interfaces */
 
1203
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1204
    if(debug){
 
1205
      fprintf(stderr, "Rejecting non-running interface \"%s\"\n",
 
1206
              if_entry->d_name);
 
1207
    }
 
1208
    return 0;
 
1209
  }
 
1210
  
 
1211
  if(not good_flags(if_entry->d_name, &ifr)){
 
1212
    return 0;
 
1213
  }
 
1214
  return 1;
 
1215
}
 
1216
 
 
1217
int notdotentries(const struct dirent *direntry){
 
1218
  /* Skip "." and ".." */
 
1219
  if(direntry->d_name[0] == '.'
 
1220
     and (direntry->d_name[1] == '\0'
 
1221
          or (direntry->d_name[1] == '.'
 
1222
              and direntry->d_name[2] == '\0'))){
 
1223
    return 0;
 
1224
  }
 
1225
  return 1;
 
1226
}
 
1227
 
 
1228
/* Is this directory entry a runnable program? */
 
1229
int runnable_hook(const struct dirent *direntry){
 
1230
  int ret;
 
1231
  struct stat st;
 
1232
  
 
1233
  if((direntry->d_name)[0] == '\0'){
 
1234
    /* Empty name? */
 
1235
    return 0;
 
1236
  }
 
1237
  
 
1238
  /* Save pointer to last character */
 
1239
  char *end = strchr(direntry->d_name, '\0')-1;
 
1240
  
 
1241
  if(*end == '~'){
 
1242
    /* Backup name~ */
 
1243
    return 0;
 
1244
  }
 
1245
  
 
1246
  if(((direntry->d_name)[0] == '#')
 
1247
     and (*end == '#')){
 
1248
    /* Temporary #name# */
 
1249
    return 0;
 
1250
  }
 
1251
  
 
1252
  /* XXX more rules here */
 
1253
  
 
1254
  ret = stat(direntry->d_name, &st);
 
1255
  if(ret == -1){
 
1256
    if(debug){
 
1257
      perror_plus("Could not stat plugin");
 
1258
    }
 
1259
    return 0;
 
1260
  }
 
1261
  if(not (S_ISREG(st.st_mode))){
 
1262
    /* Not a regular file */
 
1263
    return 0;
 
1264
  }
 
1265
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
 
1266
    /* Not executable */
 
1267
    return 0;
 
1268
  }
 
1269
  return 1;
 
1270
}
 
1271
 
 
1272
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
 
1273
  int ret;
 
1274
  struct timespec now;
 
1275
  struct timespec waited_time;
 
1276
  intmax_t block_time;
 
1277
  
 
1278
  while(true){
 
1279
    if(mc.current_server == NULL){
 
1280
      if (debug){
 
1281
        fprintf(stderr,
 
1282
                "Wait until first server is found. No timeout!\n");
 
1283
      }
 
1284
      ret = avahi_simple_poll_iterate(s, -1);
 
1285
    } else {
 
1286
      if (debug){
 
1287
        fprintf(stderr, "Check current_server if we should run it,"
 
1288
                " or wait\n");
 
1289
      }
 
1290
      /* the current time */
 
1291
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
 
1292
      if(ret == -1){
 
1293
        perror_plus("clock_gettime");
 
1294
        return -1;
 
1295
      }
 
1296
      /* Calculating in ms how long time between now and server
 
1297
         who we visted longest time ago. Now - last seen.  */
 
1298
      waited_time.tv_sec = (now.tv_sec
 
1299
                            - mc.current_server->last_seen.tv_sec);
 
1300
      waited_time.tv_nsec = (now.tv_nsec
 
1301
                             - mc.current_server->last_seen.tv_nsec);
 
1302
      /* total time is 10s/10,000ms.
 
1303
         Converting to s from ms by dividing by 1,000,
 
1304
         and ns to ms by dividing by 1,000,000. */
 
1305
      block_time = ((retry_interval
 
1306
                     - ((intmax_t)waited_time.tv_sec * 1000))
 
1307
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
 
1308
      
 
1309
      if (debug){
 
1310
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
 
1311
      }
 
1312
      
 
1313
      if(block_time <= 0){
 
1314
        ret = start_mandos_communication(mc.current_server->ip,
 
1315
                                         mc.current_server->port,
 
1316
                                         mc.current_server->if_index,
 
1317
                                         mc.current_server->af);
 
1318
        if(ret == 0){
 
1319
          avahi_simple_poll_quit(mc.simple_poll);
 
1320
          return 0;
 
1321
        }
 
1322
        ret = clock_gettime(CLOCK_MONOTONIC,
 
1323
                            &mc.current_server->last_seen);
 
1324
        if(ret == -1){
 
1325
          perror_plus("clock_gettime");
 
1326
          return -1;
 
1327
        }
 
1328
        mc.current_server = mc.current_server->next;
 
1329
        block_time = 0;         /* Call avahi to find new Mandos
 
1330
                                   servers, but don't block */
 
1331
      }
 
1332
      
 
1333
      ret = avahi_simple_poll_iterate(s, (int)block_time);
 
1334
    }
 
1335
    if(ret != 0){
 
1336
      if (ret > 0 or errno != EINTR) {
 
1337
        return (ret != 1) ? ret : 0;
 
1338
      }
 
1339
    }
 
1340
  }
 
1341
}
 
1342
 
 
1343
int main(int argc, char *argv[]){
 
1344
  AvahiSServiceBrowser *sb = NULL;
 
1345
  int error;
 
1346
  int ret;
 
1347
  intmax_t tmpmax;
 
1348
  char *tmp;
 
1349
  int exitcode = EXIT_SUCCESS;
 
1350
  const char *interface = "";
 
1351
  struct ifreq network;
 
1352
  int sd = -1;
 
1353
  bool take_down_interface = false;
 
1354
  uid_t uid;
 
1355
  gid_t gid;
 
1356
  char tempdir[] = "/tmp/mandosXXXXXX";
 
1357
  bool tempdir_created = false;
 
1358
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
 
1359
  const char *seckey = PATHDIR "/" SECKEY;
 
1360
  const char *pubkey = PATHDIR "/" PUBKEY;
 
1361
  
 
1362
  bool gnutls_initialized = false;
 
1363
  bool gpgme_initialized = false;
 
1364
  float delay = 2.5f;
 
1365
  double retry_interval = 10; /* 10s between trying a server and
 
1366
                                 retrying the same server again */
 
1367
  
 
1368
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
 
1369
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
 
1370
  
 
1371
  uid = getuid();
 
1372
  gid = getgid();
 
1373
  
 
1374
  /* Lower any group privileges we might have, just to be safe */
 
1375
  errno = 0;
 
1376
  ret = setgid(gid);
 
1377
  if(ret == -1){
 
1378
    perror_plus("setgid");
 
1379
  }
 
1380
  
 
1381
  /* Lower user privileges (temporarily) */
 
1382
  errno = 0;
 
1383
  ret = seteuid(uid);
 
1384
  if(ret == -1){
 
1385
    perror_plus("seteuid");
 
1386
  }
 
1387
  
 
1388
  if(quit_now){
 
1389
    goto end;
 
1390
  }
 
1391
  
 
1392
  {
 
1393
    struct argp_option options[] = {
 
1394
      { .name = "debug", .key = 128,
 
1395
        .doc = "Debug mode", .group = 3 },
 
1396
      { .name = "connect", .key = 'c',
 
1397
        .arg = "ADDRESS:PORT",
 
1398
        .doc = "Connect directly to a specific Mandos server",
 
1399
        .group = 1 },
 
1400
      { .name = "interface", .key = 'i',
 
1401
        .arg = "NAME",
 
1402
        .doc = "Network interface that will be used to search for"
 
1403
        " Mandos servers",
 
1404
        .group = 1 },
 
1405
      { .name = "seckey", .key = 's',
 
1406
        .arg = "FILE",
 
1407
        .doc = "OpenPGP secret key file base name",
 
1408
        .group = 1 },
 
1409
      { .name = "pubkey", .key = 'p',
 
1410
        .arg = "FILE",
 
1411
        .doc = "OpenPGP public key file base name",
 
1412
        .group = 2 },
 
1413
      { .name = "dh-bits", .key = 129,
 
1414
        .arg = "BITS",
 
1415
        .doc = "Bit length of the prime number used in the"
 
1416
        " Diffie-Hellman key exchange",
 
1417
        .group = 2 },
 
1418
      { .name = "priority", .key = 130,
 
1419
        .arg = "STRING",
 
1420
        .doc = "GnuTLS priority string for the TLS handshake",
 
1421
        .group = 1 },
 
1422
      { .name = "delay", .key = 131,
 
1423
        .arg = "SECONDS",
 
1424
        .doc = "Maximum delay to wait for interface startup",
 
1425
        .group = 2 },
 
1426
      { .name = "retry", .key = 132,
 
1427
        .arg = "SECONDS",
 
1428
        .doc = "Retry interval used when denied by the mandos server",
 
1429
        .group = 2 },
 
1430
      /*
 
1431
       * These reproduce what we would get without ARGP_NO_HELP
 
1432
       */
 
1433
      { .name = "help", .key = '?',
 
1434
        .doc = "Give this help list", .group = -1 },
 
1435
      { .name = "usage", .key = -3,
 
1436
        .doc = "Give a short usage message", .group = -1 },
 
1437
      { .name = "version", .key = 'V',
 
1438
        .doc = "Print program version", .group = -1 },
 
1439
      { .name = NULL }
 
1440
    };
 
1441
    
 
1442
    error_t parse_opt(int key, char *arg,
 
1443
                      struct argp_state *state){
 
1444
      errno = 0;
 
1445
      switch(key){
 
1446
      case 128:                 /* --debug */
 
1447
        debug = true;
 
1448
        break;
 
1449
      case 'c':                 /* --connect */
 
1450
        connect_to = arg;
 
1451
        break;
 
1452
      case 'i':                 /* --interface */
 
1453
        interface = arg;
 
1454
        break;
 
1455
      case 's':                 /* --seckey */
 
1456
        seckey = arg;
 
1457
        break;
 
1458
      case 'p':                 /* --pubkey */
 
1459
        pubkey = arg;
 
1460
        break;
 
1461
      case 129:                 /* --dh-bits */
 
1462
        errno = 0;
 
1463
        tmpmax = strtoimax(arg, &tmp, 10);
 
1464
        if(errno != 0 or tmp == arg or *tmp != '\0'
 
1465
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
 
1466
          argp_error(state, "Bad number of DH bits");
 
1467
        }
 
1468
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
 
1469
        break;
 
1470
      case 130:                 /* --priority */
 
1471
        mc.priority = arg;
 
1472
        break;
 
1473
      case 131:                 /* --delay */
 
1474
        errno = 0;
 
1475
        delay = strtof(arg, &tmp);
 
1476
        if(errno != 0 or tmp == arg or *tmp != '\0'){
 
1477
          argp_error(state, "Bad delay");
 
1478
        }
 
1479
      case 132:                 /* --retry */
 
1480
        errno = 0;
 
1481
        retry_interval = strtod(arg, &tmp);
 
1482
        if(errno != 0 or tmp == arg or *tmp != '\0'
 
1483
           or (retry_interval * 1000) > INT_MAX
 
1484
           or retry_interval < 0){
 
1485
          argp_error(state, "Bad retry interval");
 
1486
        }
 
1487
        break;
 
1488
        /*
 
1489
         * These reproduce what we would get without ARGP_NO_HELP
 
1490
         */
 
1491
      case '?':                 /* --help */
 
1492
        argp_state_help(state, state->out_stream,
 
1493
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
 
1494
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
 
1495
      case -3:                  /* --usage */
 
1496
        argp_state_help(state, state->out_stream,
 
1497
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
 
1498
      case 'V':                 /* --version */
 
1499
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
1500
        exit(argp_err_exit_status);
 
1501
        break;
 
1502
      default:
 
1503
        return ARGP_ERR_UNKNOWN;
 
1504
      }
 
1505
      return errno;
 
1506
    }
 
1507
    
 
1508
    struct argp argp = { .options = options, .parser = parse_opt,
 
1509
                         .args_doc = "",
 
1510
                         .doc = "Mandos client -- Get and decrypt"
 
1511
                         " passwords from a Mandos server" };
 
1512
    ret = argp_parse(&argp, argc, argv,
 
1513
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
1514
    switch(ret){
 
1515
    case 0:
 
1516
      break;
 
1517
    case ENOMEM:
 
1518
    default:
 
1519
      errno = ret;
 
1520
      perror_plus("argp_parse");
 
1521
      exitcode = EX_OSERR;
 
1522
      goto end;
 
1523
    case EINVAL:
 
1524
      exitcode = EX_USAGE;
 
1525
      goto end;
 
1526
    }
 
1527
  }
 
1528
    
 
1529
  {
 
1530
    /* Work around Debian bug #633582:
 
1531
       <http://bugs.debian.org/633582> */
 
1532
    struct stat st;
 
1533
    
 
1534
    /* Re-raise priviliges */
 
1535
    errno = 0;
 
1536
    ret = seteuid(0);
 
1537
    if(ret == -1){
 
1538
      perror_plus("seteuid");
 
1539
    }
 
1540
    
 
1541
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1542
      int seckey_fd = open(seckey, O_RDONLY);
 
1543
      if(seckey_fd == -1){
 
1544
        perror_plus("open");
 
1545
      } else {
 
1546
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1547
        if(ret == -1){
 
1548
          perror_plus("fstat");
 
1549
        } else {
 
1550
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1551
            ret = fchown(seckey_fd, uid, gid);
 
1552
            if(ret == -1){
 
1553
              perror_plus("fchown");
 
1554
            }
 
1555
          }
 
1556
        }
 
1557
        TEMP_FAILURE_RETRY(close(seckey_fd));
 
1558
      }
 
1559
    }
 
1560
    
 
1561
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1562
      int pubkey_fd = open(pubkey, O_RDONLY);
 
1563
      if(pubkey_fd == -1){
 
1564
        perror_plus("open");
 
1565
      } else {
 
1566
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1567
        if(ret == -1){
 
1568
          perror_plus("fstat");
 
1569
        } else {
 
1570
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1571
            ret = fchown(pubkey_fd, uid, gid);
 
1572
            if(ret == -1){
 
1573
              perror_plus("fchown");
 
1574
            }
 
1575
          }
 
1576
        }
 
1577
        TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1578
      }
 
1579
    }
 
1580
    
 
1581
    /* Lower privileges */
 
1582
    errno = 0;
 
1583
    ret = seteuid(uid);
 
1584
    if(ret == -1){
 
1585
      perror_plus("seteuid");
 
1586
    }
 
1587
  }
 
1588
  
 
1589
  /* Find network hooks and run them */
 
1590
  {
 
1591
    struct dirent **direntries;
 
1592
    struct dirent *direntry;
 
1593
    int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
 
1594
                           alphasort);
 
1595
    int devnull = open("/dev/null", O_RDONLY);
 
1596
    for(int i = 0; i < numhooks; i++){
 
1597
      direntry = direntries[0];
 
1598
      char *fullname = NULL;
 
1599
      ret = asprintf(&fullname, "%s/%s", tempdir,
 
1600
                     direntry->d_name);
 
1601
      if(ret < 0){
 
1602
        perror_plus("asprintf");
 
1603
        continue;
 
1604
      }
 
1605
      pid_t hook_pid = fork();
 
1606
      if(hook_pid == 0){
 
1607
        /* Child */
 
1608
        dup2(devnull, STDIN_FILENO);
 
1609
        close(devnull);
 
1610
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1611
        setenv("DEVICE", interface, 1);
 
1612
        setenv("VERBOSE", debug ? "1" : "0", 1);
 
1613
        setenv("MODE", "start", 1);
 
1614
        /* setenv( XXX more here */
 
1615
        ret = execl(fullname, direntry->d_name, "start", NULL);
 
1616
        perror_plus("execl");
 
1617
      }
 
1618
      free(fullname);
 
1619
      if(quit_now){
 
1620
        goto end;
 
1621
      }
 
1622
    }
 
1623
    close(devnull);
 
1624
  }
 
1625
  
 
1626
  if(not debug){
 
1627
    avahi_set_log_function(empty_log);
 
1628
  }
 
1629
  
 
1630
  if(interface[0] == '\0'){
 
1631
    struct dirent **direntries;
 
1632
    /* First look for interfaces that are up */
 
1633
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1634
                  alphasort);
 
1635
    if(ret == 0){
 
1636
      /* No up interfaces, look for any good interfaces */
 
1637
      free(direntries);
 
1638
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1639
                    alphasort);
 
1640
    }
 
1641
    if(ret >= 1){
 
1642
      /* Pick the first interface returned */
 
1643
      interface = strdup(direntries[0]->d_name);
 
1644
      if(debug){
 
1645
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1646
      }
 
1647
      if(interface == NULL){
 
1648
        perror_plus("malloc");
 
1649
        free(direntries);
 
1650
        exitcode = EXIT_FAILURE;
 
1651
        goto end;
 
1652
      }
 
1653
      free(direntries);
 
1654
    } else {
 
1655
      free(direntries);
 
1656
      fprintf(stderr, "Could not find a network interface\n");
 
1657
      exitcode = EXIT_FAILURE;
 
1658
      goto end;
 
1659
    }
 
1660
  }
 
1661
  
 
1662
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
 
1663
     from the signal handler */
 
1664
  /* Initialize the pseudo-RNG for Avahi */
 
1665
  srand((unsigned int) time(NULL));
 
1666
  mc.simple_poll = avahi_simple_poll_new();
 
1667
  if(mc.simple_poll == NULL){
 
1668
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1669
    exitcode = EX_UNAVAILABLE;
 
1670
    goto end;
 
1671
  }
 
1672
  
 
1673
  sigemptyset(&sigterm_action.sa_mask);
 
1674
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
 
1675
  if(ret == -1){
 
1676
    perror_plus("sigaddset");
 
1677
    exitcode = EX_OSERR;
 
1678
    goto end;
 
1679
  }
 
1680
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
 
1681
  if(ret == -1){
 
1682
    perror_plus("sigaddset");
 
1683
    exitcode = EX_OSERR;
 
1684
    goto end;
 
1685
  }
 
1686
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
 
1687
  if(ret == -1){
 
1688
    perror_plus("sigaddset");
 
1689
    exitcode = EX_OSERR;
 
1690
    goto end;
 
1691
  }
 
1692
  /* Need to check if the handler is SIG_IGN before handling:
 
1693
     | [[info:libc:Initial Signal Actions]] |
 
1694
     | [[info:libc:Basic Signal Handling]]  |
 
1695
  */
 
1696
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
 
1697
  if(ret == -1){
 
1698
    perror_plus("sigaction");
 
1699
    return EX_OSERR;
 
1700
  }
 
1701
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1702
    ret = sigaction(SIGINT, &sigterm_action, NULL);
 
1703
    if(ret == -1){
 
1704
      perror_plus("sigaction");
 
1705
      exitcode = EX_OSERR;
 
1706
      goto end;
 
1707
    }
 
1708
  }
 
1709
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
 
1710
  if(ret == -1){
 
1711
    perror_plus("sigaction");
 
1712
    return EX_OSERR;
 
1713
  }
 
1714
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1715
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
 
1716
    if(ret == -1){
 
1717
      perror_plus("sigaction");
 
1718
      exitcode = EX_OSERR;
 
1719
      goto end;
 
1720
    }
 
1721
  }
 
1722
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
 
1723
  if(ret == -1){
 
1724
    perror_plus("sigaction");
 
1725
    return EX_OSERR;
 
1726
  }
 
1727
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1728
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
 
1729
    if(ret == -1){
 
1730
      perror_plus("sigaction");
 
1731
      exitcode = EX_OSERR;
 
1732
      goto end;
 
1733
    }
 
1734
  }
 
1735
  
 
1736
  /* If the interface is down, bring it up */
 
1737
  if(strcmp(interface, "none") != 0){
 
1738
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1739
    if(if_index == 0){
 
1740
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1741
      exitcode = EX_UNAVAILABLE;
 
1742
      goto end;
 
1743
    }
 
1744
    
 
1745
    if(quit_now){
 
1746
      goto end;
 
1747
    }
 
1748
    
 
1749
    /* Re-raise priviliges */
 
1750
    errno = 0;
 
1751
    ret = seteuid(0);
 
1752
    if(ret == -1){
 
1753
      perror_plus("seteuid");
 
1754
    }
 
1755
    
 
1756
#ifdef __linux__
 
1757
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1758
       messages about the network interface to mess up the prompt */
 
1759
    ret = klogctl(8, NULL, 5);
 
1760
    bool restore_loglevel = true;
 
1761
    if(ret == -1){
 
1762
      restore_loglevel = false;
 
1763
      perror_plus("klogctl");
 
1764
    }
 
1765
#endif  /* __linux__ */
 
1766
    
 
1767
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1768
    if(sd < 0){
 
1769
      perror_plus("socket");
 
1770
      exitcode = EX_OSERR;
 
1771
#ifdef __linux__
 
1772
      if(restore_loglevel){
 
1773
        ret = klogctl(7, NULL, 0);
 
1774
        if(ret == -1){
 
1775
          perror_plus("klogctl");
 
1776
        }
 
1777
      }
 
1778
#endif  /* __linux__ */
 
1779
      /* Lower privileges */
 
1780
      errno = 0;
 
1781
      ret = seteuid(uid);
 
1782
      if(ret == -1){
 
1783
        perror_plus("seteuid");
 
1784
      }
 
1785
      goto end;
 
1786
    }
 
1787
    strcpy(network.ifr_name, interface);
 
1788
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1789
    if(ret == -1){
 
1790
      perror_plus("ioctl SIOCGIFFLAGS");
 
1791
#ifdef __linux__
 
1792
      if(restore_loglevel){
 
1793
        ret = klogctl(7, NULL, 0);
 
1794
        if(ret == -1){
 
1795
          perror_plus("klogctl");
 
1796
        }
 
1797
      }
 
1798
#endif  /* __linux__ */
 
1799
      exitcode = EX_OSERR;
 
1800
      /* Lower privileges */
 
1801
      errno = 0;
 
1802
      ret = seteuid(uid);
 
1803
      if(ret == -1){
 
1804
        perror_plus("seteuid");
 
1805
      }
 
1806
      goto end;
 
1807
    }
 
1808
    if((network.ifr_flags & IFF_UP) == 0){
 
1809
      network.ifr_flags |= IFF_UP;
 
1810
      take_down_interface = true;
 
1811
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1812
      if(ret == -1){
 
1813
        take_down_interface = false;
 
1814
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1815
        exitcode = EX_OSERR;
 
1816
#ifdef __linux__
 
1817
        if(restore_loglevel){
 
1818
          ret = klogctl(7, NULL, 0);
 
1819
          if(ret == -1){
 
1820
            perror_plus("klogctl");
 
1821
          }
 
1822
        }
 
1823
#endif  /* __linux__ */
 
1824
        /* Lower privileges */
 
1825
        errno = 0;
 
1826
        ret = seteuid(uid);
 
1827
        if(ret == -1){
 
1828
          perror_plus("seteuid");
 
1829
        }
 
1830
        goto end;
 
1831
      }
 
1832
    }
 
1833
    /* Sleep checking until interface is running.
 
1834
       Check every 0.25s, up to total time of delay */
 
1835
    for(int i=0; i < delay * 4; i++){
 
1836
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1837
      if(ret == -1){
 
1838
        perror_plus("ioctl SIOCGIFFLAGS");
 
1839
      } else if(network.ifr_flags & IFF_RUNNING){
 
1840
        break;
 
1841
      }
 
1842
      struct timespec sleeptime = { .tv_nsec = 250000000 };
 
1843
      ret = nanosleep(&sleeptime, NULL);
 
1844
      if(ret == -1 and errno != EINTR){
 
1845
        perror_plus("nanosleep");
 
1846
      }
 
1847
    }
 
1848
    if(not take_down_interface){
 
1849
      /* We won't need the socket anymore */
 
1850
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1851
      if(ret == -1){
 
1852
        perror_plus("close");
 
1853
      }
 
1854
    }
 
1855
#ifdef __linux__
 
1856
    if(restore_loglevel){
 
1857
      /* Restores kernel loglevel to default */
 
1858
      ret = klogctl(7, NULL, 0);
 
1859
      if(ret == -1){
 
1860
        perror_plus("klogctl");
 
1861
      }
 
1862
    }
 
1863
#endif  /* __linux__ */
 
1864
    /* Lower privileges */
 
1865
    errno = 0;
 
1866
    if(take_down_interface){
 
1867
      /* Lower privileges */
 
1868
      ret = seteuid(uid);
 
1869
      if(ret == -1){
 
1870
        perror_plus("seteuid");
 
1871
      }
 
1872
    } else {
 
1873
      /* Lower privileges permanently */
 
1874
      ret = setuid(uid);
 
1875
      if(ret == -1){
 
1876
        perror_plus("setuid");
 
1877
      }
 
1878
    }
 
1879
  }
 
1880
  
 
1881
  if(quit_now){
 
1882
    goto end;
 
1883
  }
 
1884
  
 
1885
  ret = init_gnutls_global(pubkey, seckey);
 
1886
  if(ret == -1){
 
1887
    fprintf(stderr, "init_gnutls_global failed\n");
 
1888
    exitcode = EX_UNAVAILABLE;
 
1889
    goto end;
 
1890
  } else {
 
1891
    gnutls_initialized = true;
 
1892
  }
 
1893
  
 
1894
  if(quit_now){
 
1895
    goto end;
 
1896
  }
 
1897
  
 
1898
  if(mkdtemp(tempdir) == NULL){
 
1899
    perror_plus("mkdtemp");
 
1900
    goto end;
 
1901
  }
 
1902
  tempdir_created = true;
 
1903
  
 
1904
  if(quit_now){
 
1905
    goto end;
 
1906
  }
 
1907
  
 
1908
  if(not init_gpgme(pubkey, seckey, tempdir)){
 
1909
    fprintf(stderr, "init_gpgme failed\n");
 
1910
    exitcode = EX_UNAVAILABLE;
 
1911
    goto end;
 
1912
  } else {
 
1913
    gpgme_initialized = true;
 
1914
  }
 
1915
  
 
1916
  if(quit_now){
 
1917
    goto end;
 
1918
  }
 
1919
  
 
1920
  if(connect_to != NULL){
 
1921
    /* Connect directly, do not use Zeroconf */
 
1922
    /* (Mainly meant for debugging) */
 
1923
    char *address = strrchr(connect_to, ':');
 
1924
    if(address == NULL){
 
1925
      fprintf(stderr, "No colon in address\n");
 
1926
      exitcode = EX_USAGE;
 
1927
      goto end;
 
1928
    }
 
1929
    
 
1930
    if(quit_now){
 
1931
      goto end;
 
1932
    }
 
1933
    
 
1934
    uint16_t port;
 
1935
    errno = 0;
 
1936
    tmpmax = strtoimax(address+1, &tmp, 10);
 
1937
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
 
1938
       or tmpmax != (uint16_t)tmpmax){
 
1939
      fprintf(stderr, "Bad port number\n");
 
1940
      exitcode = EX_USAGE;
 
1941
      goto end;
 
1942
    }
 
1943
  
 
1944
    if(quit_now){
 
1945
      goto end;
 
1946
    }
 
1947
    
 
1948
    port = (uint16_t)tmpmax;
 
1949
    *address = '\0';
 
1950
    /* Colon in address indicates IPv6 */
 
1951
    int af;
 
1952
    if(strchr(connect_to, ':') != NULL){
 
1953
      af = AF_INET6;
 
1954
      /* Accept [] around IPv6 address - see RFC 5952 */
 
1955
      if(connect_to[0] == '[' and address[-1] == ']')
 
1956
        {
 
1957
          connect_to++;
 
1958
          address[-1] = '\0';
 
1959
        }
 
1960
    } else {
 
1961
      af = AF_INET;
 
1962
    }
 
1963
    address = connect_to;
 
1964
    
 
1965
    if(quit_now){
 
1966
      goto end;
 
1967
    }
 
1968
    
 
1969
    while(not quit_now){
 
1970
      ret = start_mandos_communication(address, port, if_index, af);
 
1971
      if(quit_now or ret == 0){
 
1972
        break;
 
1973
      }
 
1974
      if(debug){
 
1975
        fprintf(stderr, "Retrying in %d seconds\n",
 
1976
                (int)retry_interval);
 
1977
      }
 
1978
      sleep((int)retry_interval);
 
1979
    }
 
1980
    
 
1981
    if (not quit_now){
 
1982
      exitcode = EXIT_SUCCESS;
 
1983
    }
 
1984
    
 
1985
    goto end;
 
1986
  }
 
1987
  
 
1988
  if(quit_now){
 
1989
    goto end;
 
1990
  }
 
1991
  
 
1992
  {
507
1993
    AvahiServerConfig config;
508
 
    AvahiSServiceBrowser *sb = NULL;
509
 
    int error;
510
 
    int ret = 1;
511
 
 
512
 
    avahi_set_log_function(empty_log);
513
 
    
514
 
    /* Initialize the psuedo-RNG */
515
 
    srand(time(NULL));
516
 
 
517
 
    /* Allocate main loop object */
518
 
    if (!(simple_poll = avahi_simple_poll_new())) {
519
 
        fprintf(stderr, "Failed to create simple poll object.\n");
520
 
        goto fail;
521
 
    }
522
 
 
523
 
    /* Do not publish any local records */
 
1994
    /* Do not publish any local Zeroconf records */
524
1995
    avahi_server_config_init(&config);
525
1996
    config.publish_hinfo = 0;
526
1997
    config.publish_addresses = 0;
527
1998
    config.publish_workstation = 0;
528
1999
    config.publish_domain = 0;
529
 
 
530
 
/*     /\* Set a unicast DNS server for wide area DNS-SD *\/ */
531
 
/*     avahi_address_parse("193.11.177.11", AVAHI_PROTO_UNSPEC, &config.wide_area_servers[0]); */
532
 
/*     config.n_wide_area_servers = 1; */
533
 
/*     config.enable_wide_area = 1; */
534
2000
    
535
2001
    /* Allocate a new server */
536
 
    server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, NULL, NULL, &error);
537
 
 
538
 
    /* Free the configuration data */
 
2002
    mc.server = avahi_server_new(avahi_simple_poll_get
 
2003
                                 (mc.simple_poll), &config, NULL,
 
2004
                                 NULL, &error);
 
2005
    
 
2006
    /* Free the Avahi configuration data */
539
2007
    avahi_server_config_free(&config);
540
 
 
541
 
    /* Check wether creating the server object succeeded */
542
 
    if (!server) {
543
 
        fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error));
544
 
        goto fail;
545
 
    }
546
 
    
547
 
    /* Create the service browser */
548
 
    if (!(sb = avahi_s_service_browser_new(server, if_nametoindex("eth0"), AVAHI_PROTO_INET6, "_mandos._tcp", NULL, 0, browse_callback, server))) {
549
 
        fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_server_errno(server)));
550
 
        goto fail;
551
 
    }
552
 
    
553
 
    /* Run the main loop */
554
 
    avahi_simple_poll_loop(simple_poll);
555
 
    
556
 
    ret = 0;
557
 
    
558
 
fail:
559
 
    
560
 
    /* Cleanup things */
561
 
    if (sb)
562
 
        avahi_s_service_browser_free(sb);
563
 
    
564
 
    if (server)
565
 
        avahi_server_free(server);
566
 
 
567
 
    if (simple_poll)
568
 
        avahi_simple_poll_free(simple_poll);
569
 
 
570
 
    return ret;
 
2008
  }
 
2009
  
 
2010
  /* Check if creating the Avahi server object succeeded */
 
2011
  if(mc.server == NULL){
 
2012
    fprintf(stderr, "Failed to create Avahi server: %s\n",
 
2013
            avahi_strerror(error));
 
2014
    exitcode = EX_UNAVAILABLE;
 
2015
    goto end;
 
2016
  }
 
2017
  
 
2018
  if(quit_now){
 
2019
    goto end;
 
2020
  }
 
2021
  
 
2022
  /* Create the Avahi service browser */
 
2023
  sb = avahi_s_service_browser_new(mc.server, if_index,
 
2024
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
 
2025
                                   NULL, 0, browse_callback, NULL);
 
2026
  if(sb == NULL){
 
2027
    fprintf(stderr, "Failed to create service browser: %s\n",
 
2028
            avahi_strerror(avahi_server_errno(mc.server)));
 
2029
    exitcode = EX_UNAVAILABLE;
 
2030
    goto end;
 
2031
  }
 
2032
  
 
2033
  if(quit_now){
 
2034
    goto end;
 
2035
  }
 
2036
  
 
2037
  /* Run the main loop */
 
2038
  
 
2039
  if(debug){
 
2040
    fprintf(stderr, "Starting Avahi loop search\n");
 
2041
  }
 
2042
 
 
2043
  ret = avahi_loop_with_timeout(mc.simple_poll,
 
2044
                                (int)(retry_interval * 1000));
 
2045
  if(debug){
 
2046
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
 
2047
            (ret == 0) ? "successfully" : "with error");
 
2048
  }
 
2049
  
 
2050
 end:
 
2051
  
 
2052
  if(debug){
 
2053
    fprintf(stderr, "%s exiting\n", argv[0]);
 
2054
  }
 
2055
  
 
2056
  /* Cleanup things */
 
2057
  if(sb != NULL)
 
2058
    avahi_s_service_browser_free(sb);
 
2059
  
 
2060
  if(mc.server != NULL)
 
2061
    avahi_server_free(mc.server);
 
2062
  
 
2063
  if(mc.simple_poll != NULL)
 
2064
    avahi_simple_poll_free(mc.simple_poll);
 
2065
  
 
2066
  if(gnutls_initialized){
 
2067
    gnutls_certificate_free_credentials(mc.cred);
 
2068
    gnutls_global_deinit();
 
2069
    gnutls_dh_params_deinit(mc.dh_params);
 
2070
  }
 
2071
  
 
2072
  if(gpgme_initialized){
 
2073
    gpgme_release(mc.ctx);
 
2074
  }
 
2075
 
 
2076
  /* Cleans up the circular linked list of Mandos servers the client
 
2077
     has seen */
 
2078
  if(mc.current_server != NULL){
 
2079
    mc.current_server->prev->next = NULL;
 
2080
    while(mc.current_server != NULL){
 
2081
      server *next = mc.current_server->next;
 
2082
      free(mc.current_server);
 
2083
      mc.current_server = next;
 
2084
    }
 
2085
  }
 
2086
  
 
2087
  /* XXX run network hooks "stop" here  */
 
2088
  
 
2089
  /* Take down the network interface */
 
2090
  if(take_down_interface){
 
2091
    /* Re-raise priviliges */
 
2092
    errno = 0;
 
2093
    ret = seteuid(0);
 
2094
    if(ret == -1){
 
2095
      perror_plus("seteuid");
 
2096
    }
 
2097
    if(geteuid() == 0){
 
2098
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2099
      if(ret == -1){
 
2100
        perror_plus("ioctl SIOCGIFFLAGS");
 
2101
      } else if(network.ifr_flags & IFF_UP) {
 
2102
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
2103
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
2104
        if(ret == -1){
 
2105
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
2106
        }
 
2107
      }
 
2108
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2109
      if(ret == -1){
 
2110
        perror_plus("close");
 
2111
      }
 
2112
      /* Lower privileges permanently */
 
2113
      errno = 0;
 
2114
      ret = setuid(uid);
 
2115
      if(ret == -1){
 
2116
        perror_plus("setuid");
 
2117
      }
 
2118
    }
 
2119
  }
 
2120
  
 
2121
  /* Removes the GPGME temp directory and all files inside */
 
2122
  if(tempdir_created){
 
2123
    struct dirent **direntries = NULL;
 
2124
    struct dirent *direntry = NULL;
 
2125
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2126
                             alphasort);
 
2127
    if (numentries > 0){
 
2128
      for(int i = 0; i < numentries; i++){
 
2129
        direntry = direntries[i];
 
2130
        char *fullname = NULL;
 
2131
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
2132
                       direntry->d_name);
 
2133
        if(ret < 0){
 
2134
          perror_plus("asprintf");
 
2135
          continue;
 
2136
        }
 
2137
        ret = remove(fullname);
 
2138
        if(ret == -1){
 
2139
          fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
 
2140
                  strerror(errno));
 
2141
        }
 
2142
        free(fullname);
 
2143
      }
 
2144
    }
 
2145
 
 
2146
    /* need to clean even if 0 because man page doesn't specify */
 
2147
    free(direntries);
 
2148
    if (numentries == -1){
 
2149
      perror_plus("scandir");
 
2150
    }
 
2151
    ret = rmdir(tempdir);
 
2152
    if(ret == -1 and errno != ENOENT){
 
2153
      perror_plus("rmdir");
 
2154
    }
 
2155
  }
 
2156
  
 
2157
  if(quit_now){
 
2158
    sigemptyset(&old_sigterm_action.sa_mask);
 
2159
    old_sigterm_action.sa_handler = SIG_DFL;
 
2160
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
2161
                                            &old_sigterm_action,
 
2162
                                            NULL));
 
2163
    if(ret == -1){
 
2164
      perror_plus("sigaction");
 
2165
    }
 
2166
    do {
 
2167
      ret = raise(signal_received);
 
2168
    } while(ret != 0 and errno == EINTR);
 
2169
    if(ret != 0){
 
2170
      perror_plus("raise");
 
2171
      abort();
 
2172
    }
 
2173
    TEMP_FAILURE_RETRY(pause());
 
2174
  }
 
2175
  
 
2176
  return exitcode;
571
2177
}