/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

  • Committer: Teddy Hogeborn
  • Date: 2008-09-21 04:22:50 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080921042250-r0qqjsbz2ulfo5le
* Makefile: Bug fix: fix syntax error.

* debian/control: Depend on "po-debconf".
  (mandos, mandos-client): Add "${misc:Depends}", used by
                           dh_installdebconf.

* debian/mandos-client.config: New; show note.
* debian/mandos-client.template: New.

* debian/mandos.README.Debian: New.

* debian/mandos.config: New; show note.
* debian/mandos.prerm: New; stop daemon.
* debian/mandos.template: New.

* debian/po/POTFILES.in: New.
* debian/po/sv.po: New.

* debian/rules (clean): Added "debconf-updatepo" as suggested by
                        po-debconf(7).
  (binary-common): Added "dh_installdebconf" to use "debian/*.config"
                   and "debian/*.template" files.

Show diffs side-by-side

added added

removed removed

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