/mandos/release

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

« back to all changes in this revision

Viewing changes to plugins.d/password-request.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-29 05:53:59 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080829055359-wkdasnyxtylmnxus
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
                        "&COMMANDNAME;".

* plugins.d/password-prompt.c (main): Improved some documentation
                                      strings.  Do perror() of
                                      tcgetattr() fails.  Add debug
                                      output if interrupted by signal.
                                      Loop over write() instead of
                                      using fwrite() when outputting
                                      password.  Add debug output if
                                      getline() returns 0, unless it
                                      was caused by a signal.  Add
                                      exit status code to debug
                                      output.

* plugins.d/password-prompt.xml: Changed all single quotes to double
                                 quotes for consistency.  Removed
                                 <?xml-stylesheet>.
  (ENTITY TIMESTAMP): New.  Automatically updated by Emacs time-stamp
                      by using Emacs local variables.
  (/refentry/refentryinfo/title): Changed to "Mandos Manual".
  (/refentry/refentryinfo/productname): Changed to "Mandos".
  (/refentry/refentryinfo/date): New; set to "&TIMESTAMP;".
  (/refentry/refentryinfo/copyright): Split copyright holders.
  (/refentry/refnamediv/refpurpose): Improved wording.
  (SYNOPSIS): Fix to use correct markup.  Add short options.
  (DESCRIPTION, OPTIONS): Improved wording.
  (OPTIONS): Improved wording.  Use more correct markup.  Document
             short options.
  (EXIT STATUS): Add text.
  (ENVIRONMENT): Document use of "cryptsource" and "crypttarget".
  (FILES): REMOVED.
  (BUGS): Add text.
  (EXAMPLE): Added some examples.
  (SECURITY): Added text.
  (SEE ALSO): Remove reference to mandos(8).  Add reference to
              crypttab(5).

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