/mandos/trunk

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

« back to all changes in this revision

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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-17 22:42:28 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080817224228-nhor2yuv230if01i
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
                           not rely on a stylesheet declaration.

* mandos.xml: Removed <?xml-stylesheet>.  New entity "&OVERVIEW;"
              refers to "overview.xml". Changed all single quotes to
              double quotes for consistency.
  (DESCRIPTION): Use the term "TLS" and not "GnuTLS" for the protocol.
                 Refer to the "OVERVIEW" section for reason for IPv6
                 link-local addresses.
  (PURPOSE): Shortened a lot.  Refer to "OVERVIEW" section for details.
  (OVERVIEW): New section.  Include &OVERVIEW; and add a paragraph
              about what the role of this program is.
  (SECURITY/CLIENTS): Refer to the "CHECKING" section for details on
                      checking.
  (SEE ALSO): Changed from an <itemizedlist> to a <variablelist>.
              Added a short text for each entry.  Removed reference to
              plugin-runner(8mandos).  Add reference to RFC 4291 and
              RFC 4346.

* overview.xml: New file, containing a single <para>.  The idea is to
                use this in all the man pages.

* plugins.d/password-request.c: Updated comments about spurious
                                warnings.

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,2009 Teddy Hogeborn
13
 
 * Copyright © 2008,2009 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
36
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
37
36
 
38
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
39
 
                                   stdout, ferror(), sscanf */
 
38
                                   stdout, ferror() */
40
39
#include <stdint.h>             /* uint16_t, uint32_t */
41
40
#include <stddef.h>             /* NULL, size_t, ssize_t */
42
41
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
48
47
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
49
48
                                   sockaddr_in6, PF_INET6,
50
49
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
51
 
                                   uid_t, gid_t, open(), opendir(),
52
 
                                   DIR */
53
 
#include <sys/stat.h>           /* open() */
 
50
                                   uid_t, gid_t */
 
51
#include <inttypes.h>           /* PRIu16 */
54
52
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
55
53
                                   struct in6_addr, inet_pton(),
56
54
                                   connect() */
57
 
#include <fcntl.h>              /* open() */
58
 
#include <dirent.h>             /* opendir(), struct dirent, readdir()
59
 
                                 */
60
 
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
61
55
#include <assert.h>             /* assert() */
62
56
#include <errno.h>              /* perror(), errno */
63
 
#include <time.h>               /* time(), nanosleep() */
 
57
#include <time.h>               /* time() */
64
58
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
65
59
                                   SIOCSIFFLAGS, if_indextoname(),
66
60
                                   if_nametoindex(), IF_NAMESIZE */
67
 
#include <netinet/in.h>
68
61
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
69
62
                                   getuid(), getgid(), setuid(),
70
63
                                   setgid() */
 
64
#include <netinet/in.h>
71
65
#include <arpa/inet.h>          /* inet_pton(), htons */
72
 
#include <iso646.h>             /* not, and, or */
 
66
#include <iso646.h>             /* not, and */
73
67
#include <argp.h>               /* struct argp_option, error_t, struct
74
68
                                   argp_state, struct argp,
75
69
                                   argp_parse(), ARGP_KEY_ARG,
76
70
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
77
 
#include <sys/klog.h>           /* klogctl() */
78
71
 
79
72
/* Avahi */
80
73
/* All Avahi types, constants and functions
93
86
                                   gnutls_*
94
87
                                   init_gnutls_session(),
95
88
                                   GNUTLS_* */
96
 
#include <gnutls/openpgp.h>
97
 
                          /* gnutls_certificate_set_openpgp_key_file(),
 
89
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
98
90
                                   GNUTLS_OPENPGP_FMT_BASE64 */
99
91
 
100
92
/* GPGME */
106
98
 
107
99
#define BUFFER_SIZE 256
108
100
 
109
 
#define PATHDIR "/conf/conf.d/mandos"
110
 
#define SECKEY "seckey.txt"
111
 
#define PUBKEY "pubkey.txt"
112
 
 
113
101
bool debug = false;
 
102
static const char *keydir = "/conf/conf.d/mandos";
114
103
static const char mandos_protocol_version[] = "1";
115
 
const char *argp_program_version = "mandos-client " VERSION;
 
104
const char *argp_program_version = "password-request 1.0";
116
105
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
117
106
 
118
107
/* Used for passing in values through the Avahi callback functions */
123
112
  unsigned int dh_bits;
124
113
  gnutls_dh_params_t dh_params;
125
114
  const char *priority;
126
 
  gpgme_ctx_t ctx;
127
115
} mandos_context;
128
116
 
129
117
/*
133
121
 */
134
122
size_t adjustbuffer(char **buffer, size_t buffer_length,
135
123
                  size_t buffer_capacity){
136
 
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
 
124
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
137
125
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
138
 
    if(buffer == NULL){
 
126
    if (buffer == NULL){
139
127
      return 0;
140
128
    }
141
129
    buffer_capacity += BUFFER_SIZE;
144
132
}
145
133
 
146
134
/* 
147
 
 * Initialize GPGME.
 
135
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
 
136
 * Returns -1 on error
148
137
 */
149
 
static bool init_gpgme(mandos_context *mc, const char *seckey,
150
 
                       const char *pubkey, const char *tempdir){
151
 
  int ret;
 
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;
152
144
  gpgme_error_t rc;
 
145
  ssize_t ret;
 
146
  size_t plaintext_capacity = 0;
 
147
  ssize_t plaintext_length = 0;
153
148
  gpgme_engine_info_t engine_info;
154
149
  
155
 
  
156
 
  /*
157
 
   * Helper function to insert pub and seckey to the enigne keyring.
158
 
   */
159
 
  bool import_key(const char *filename){
160
 
    int fd;
161
 
    gpgme_data_t pgp_data;
162
 
    
163
 
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
164
 
    if(fd == -1){
165
 
      perror("open");
166
 
      return false;
167
 
    }
168
 
    
169
 
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
170
 
    if(rc != GPG_ERR_NO_ERROR){
171
 
      fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
172
 
              gpgme_strsource(rc), gpgme_strerror(rc));
173
 
      return false;
174
 
    }
175
 
    
176
 
    rc = gpgme_op_import(mc->ctx, pgp_data);
177
 
    if(rc != GPG_ERR_NO_ERROR){
178
 
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
179
 
              gpgme_strsource(rc), gpgme_strerror(rc));
180
 
      return false;
181
 
    }
182
 
    
183
 
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
184
 
    if(ret == -1){
185
 
      perror("close");
186
 
    }
187
 
    gpgme_data_release(pgp_data);
188
 
    return true;
189
 
  }
190
 
  
191
 
  if(debug){
192
 
    fprintf(stderr, "Initialize gpgme\n");
 
150
  if (debug){
 
151
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
193
152
  }
194
153
  
195
154
  /* Init GPGME */
196
155
  gpgme_check_version(NULL);
197
156
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
198
 
  if(rc != GPG_ERR_NO_ERROR){
 
157
  if (rc != GPG_ERR_NO_ERROR){
199
158
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
200
159
            gpgme_strsource(rc), gpgme_strerror(rc));
201
 
    return false;
 
160
    return -1;
202
161
  }
203
162
  
204
 
    /* Set GPGME home directory for the OpenPGP engine only */
205
 
  rc = gpgme_get_engine_info(&engine_info);
206
 
  if(rc != GPG_ERR_NO_ERROR){
 
163
  /* Set GPGME home directory for the OpenPGP engine only */
 
164
  rc = gpgme_get_engine_info (&engine_info);
 
165
  if (rc != GPG_ERR_NO_ERROR){
207
166
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
208
167
            gpgme_strsource(rc), gpgme_strerror(rc));
209
 
    return false;
 
168
    return -1;
210
169
  }
211
170
  while(engine_info != NULL){
212
171
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
213
172
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
214
 
                            engine_info->file_name, tempdir);
 
173
                            engine_info->file_name, homedir);
215
174
      break;
216
175
    }
217
176
    engine_info = engine_info->next;
218
177
  }
219
178
  if(engine_info == NULL){
220
 
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
221
 
    return false;
222
 
  }
223
 
  
224
 
  /* Create new GPGME "context" */
225
 
  rc = gpgme_new(&(mc->ctx));
226
 
  if(rc != GPG_ERR_NO_ERROR){
227
 
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
228
 
            gpgme_strsource(rc), gpgme_strerror(rc));
229
 
    return false;
230
 
  }
231
 
  
232
 
  if(not import_key(pubkey) or not import_key(seckey)){
233
 
    return false;
234
 
  }
235
 
  
236
 
  return true; 
237
 
}
238
 
 
239
 
/* 
240
 
 * Decrypt OpenPGP data.
241
 
 * Returns -1 on error
242
 
 */
243
 
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
244
 
                                  const char *cryptotext,
245
 
                                  size_t crypto_size,
246
 
                                  char **plaintext){
247
 
  gpgme_data_t dh_crypto, dh_plain;
248
 
  gpgme_error_t rc;
249
 
  ssize_t ret;
250
 
  size_t plaintext_capacity = 0;
251
 
  ssize_t plaintext_length = 0;
252
 
  
253
 
  if(debug){
254
 
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
 
179
    fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
 
180
    return -1;
255
181
  }
256
182
  
257
183
  /* Create new GPGME data buffer from memory cryptotext */
258
184
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
259
185
                               0);
260
 
  if(rc != GPG_ERR_NO_ERROR){
 
186
  if (rc != GPG_ERR_NO_ERROR){
261
187
    fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
262
188
            gpgme_strsource(rc), gpgme_strerror(rc));
263
189
    return -1;
265
191
  
266
192
  /* Create new empty GPGME data buffer for the plaintext */
267
193
  rc = gpgme_data_new(&dh_plain);
268
 
  if(rc != GPG_ERR_NO_ERROR){
 
194
  if (rc != GPG_ERR_NO_ERROR){
269
195
    fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
270
196
            gpgme_strsource(rc), gpgme_strerror(rc));
271
197
    gpgme_data_release(dh_crypto);
272
198
    return -1;
273
199
  }
274
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
  
275
210
  /* Decrypt data from the cryptotext data buffer to the plaintext
276
211
     data buffer */
277
 
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
278
 
  if(rc != GPG_ERR_NO_ERROR){
 
212
  rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
 
213
  if (rc != GPG_ERR_NO_ERROR){
279
214
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
280
215
            gpgme_strsource(rc), gpgme_strerror(rc));
281
216
    plaintext_length = -1;
282
 
    if(debug){
283
 
      gpgme_decrypt_result_t result;
284
 
      result = gpgme_op_decrypt_result(mc->ctx);
285
 
      if(result == NULL){
286
 
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
287
 
      } else {
288
 
        fprintf(stderr, "Unsupported algorithm: %s\n",
289
 
                result->unsupported_algorithm);
290
 
        fprintf(stderr, "Wrong key usage: %u\n",
291
 
                result->wrong_key_usage);
292
 
        if(result->file_name != NULL){
293
 
          fprintf(stderr, "File name: %s\n", result->file_name);
294
 
        }
295
 
        gpgme_recipient_t recipient;
296
 
        recipient = result->recipients;
297
 
        if(recipient){
298
 
          while(recipient != NULL){
299
 
            fprintf(stderr, "Public key algorithm: %s\n",
300
 
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
301
 
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
302
 
            fprintf(stderr, "Secret key available: %s\n",
303
 
                    recipient->status == GPG_ERR_NO_SECKEY
304
 
                    ? "No" : "Yes");
305
 
            recipient = recipient->next;
306
 
          }
307
 
        }
308
 
      }
309
 
    }
310
217
    goto decrypt_end;
311
218
  }
312
219
  
314
221
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
315
222
  }
316
223
  
 
224
  if (debug){
 
225
    gpgme_decrypt_result_t result;
 
226
    result = gpgme_op_decrypt_result(ctx);
 
227
    if (result == NULL){
 
228
      fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
229
    } else {
 
230
      fprintf(stderr, "Unsupported algorithm: %s\n",
 
231
              result->unsupported_algorithm);
 
232
      fprintf(stderr, "Wrong key usage: %u\n",
 
233
              result->wrong_key_usage);
 
234
      if(result->file_name != NULL){
 
235
        fprintf(stderr, "File name: %s\n", result->file_name);
 
236
      }
 
237
      gpgme_recipient_t recipient;
 
238
      recipient = result->recipients;
 
239
      if(recipient){
 
240
        while(recipient != NULL){
 
241
          fprintf(stderr, "Public key algorithm: %s\n",
 
242
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
243
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
244
          fprintf(stderr, "Secret key available: %s\n",
 
245
                  recipient->status == GPG_ERR_NO_SECKEY
 
246
                  ? "No" : "Yes");
 
247
          recipient = recipient->next;
 
248
        }
 
249
      }
 
250
    }
 
251
  }
 
252
  
317
253
  /* Seek back to the beginning of the GPGME plaintext data buffer */
318
 
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
319
 
    perror("gpgme_data_seek");
 
254
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
 
255
    perror("pgpme_data_seek");
320
256
    plaintext_length = -1;
321
257
    goto decrypt_end;
322
258
  }
326
262
    plaintext_capacity = adjustbuffer(plaintext,
327
263
                                      (size_t)plaintext_length,
328
264
                                      plaintext_capacity);
329
 
    if(plaintext_capacity == 0){
 
265
    if (plaintext_capacity == 0){
330
266
        perror("adjustbuffer");
331
267
        plaintext_length = -1;
332
268
        goto decrypt_end;
335
271
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
336
272
                          BUFFER_SIZE);
337
273
    /* Print the data, if any */
338
 
    if(ret == 0){
 
274
    if (ret == 0){
339
275
      /* EOF */
340
276
      break;
341
277
    }
346
282
    }
347
283
    plaintext_length += ret;
348
284
  }
349
 
  
 
285
 
350
286
  if(debug){
351
287
    fprintf(stderr, "Decrypted password is: ");
352
288
    for(ssize_t i = 0; i < plaintext_length; i++){
365
301
  return plaintext_length;
366
302
}
367
303
 
368
 
static const char * safer_gnutls_strerror(int value) {
369
 
  const char *ret = gnutls_strerror(value); /* Spurious warning from
370
 
                                               -Wunreachable-code */
371
 
  if(ret == NULL)
 
304
static const char * safer_gnutls_strerror (int value) {
 
305
  const char *ret = gnutls_strerror (value); /* Spurious warning */
 
306
  if (ret == NULL)
372
307
    ret = "(unknown)";
373
308
  return ret;
374
309
}
389
324
  }
390
325
  
391
326
  ret = gnutls_global_init();
392
 
  if(ret != GNUTLS_E_SUCCESS) {
393
 
    fprintf(stderr, "GnuTLS global_init: %s\n",
394
 
            safer_gnutls_strerror(ret));
 
327
  if (ret != GNUTLS_E_SUCCESS) {
 
328
    fprintf (stderr, "GnuTLS global_init: %s\n",
 
329
             safer_gnutls_strerror(ret));
395
330
    return -1;
396
331
  }
397
332
  
398
 
  if(debug){
 
333
  if (debug){
399
334
    /* "Use a log level over 10 to enable all debugging options."
400
335
     * - GnuTLS manual
401
336
     */
405
340
  
406
341
  /* OpenPGP credentials */
407
342
  gnutls_certificate_allocate_credentials(&mc->cred);
408
 
  if(ret != GNUTLS_E_SUCCESS){
409
 
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
410
 
                                                  * from
411
 
                                                  * -Wunreachable-code
412
 
                                                  */
413
 
            safer_gnutls_strerror(ret));
414
 
    gnutls_global_deinit();
 
343
  if (ret != GNUTLS_E_SUCCESS){
 
344
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
 
345
                                                     warning */
 
346
             safer_gnutls_strerror(ret));
 
347
    gnutls_global_deinit ();
415
348
    return -1;
416
349
  }
417
350
  
418
351
  if(debug){
419
 
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
420
 
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
 
352
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
 
353
            " and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
421
354
            seckeyfilename);
422
355
  }
423
356
  
424
357
  ret = gnutls_certificate_set_openpgp_key_file
425
358
    (mc->cred, pubkeyfilename, seckeyfilename,
426
359
     GNUTLS_OPENPGP_FMT_BASE64);
427
 
  if(ret != GNUTLS_E_SUCCESS) {
 
360
  if (ret != GNUTLS_E_SUCCESS) {
428
361
    fprintf(stderr,
429
362
            "Error[%d] while reading the OpenPGP key pair ('%s',"
430
363
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
431
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
 
364
    fprintf(stdout, "The GnuTLS error is: %s\n",
432
365
            safer_gnutls_strerror(ret));
433
366
    goto globalfail;
434
367
  }
435
368
  
436
369
  /* GnuTLS server initialization */
437
370
  ret = gnutls_dh_params_init(&mc->dh_params);
438
 
  if(ret != GNUTLS_E_SUCCESS) {
439
 
    fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
440
 
            " %s\n", safer_gnutls_strerror(ret));
 
371
  if (ret != GNUTLS_E_SUCCESS) {
 
372
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
 
373
             " %s\n", safer_gnutls_strerror(ret));
441
374
    goto globalfail;
442
375
  }
443
376
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
444
 
  if(ret != GNUTLS_E_SUCCESS) {
445
 
    fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
446
 
            safer_gnutls_strerror(ret));
 
377
  if (ret != GNUTLS_E_SUCCESS) {
 
378
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
 
379
             safer_gnutls_strerror(ret));
447
380
    goto globalfail;
448
381
  }
449
382
  
450
383
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
451
 
  
 
384
 
452
385
  return 0;
453
 
  
 
386
 
454
387
 globalfail:
455
 
  
 
388
 
456
389
  gnutls_certificate_free_credentials(mc->cred);
457
390
  gnutls_global_deinit();
458
 
  gnutls_dh_params_deinit(mc->dh_params);
459
391
  return -1;
 
392
 
460
393
}
461
394
 
462
395
static int init_gnutls_session(mandos_context *mc,
464
397
  int ret;
465
398
  /* GnuTLS session creation */
466
399
  ret = gnutls_init(session, GNUTLS_SERVER);
467
 
  if(ret != GNUTLS_E_SUCCESS){
 
400
  if (ret != GNUTLS_E_SUCCESS){
468
401
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
469
402
            safer_gnutls_strerror(ret));
470
403
  }
472
405
  {
473
406
    const char *err;
474
407
    ret = gnutls_priority_set_direct(*session, mc->priority, &err);
475
 
    if(ret != GNUTLS_E_SUCCESS) {
 
408
    if (ret != GNUTLS_E_SUCCESS) {
476
409
      fprintf(stderr, "Syntax error at: %s\n", err);
477
410
      fprintf(stderr, "GnuTLS error: %s\n",
478
411
              safer_gnutls_strerror(ret));
479
 
      gnutls_deinit(*session);
 
412
      gnutls_deinit (*session);
480
413
      return -1;
481
414
    }
482
415
  }
483
416
  
484
417
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
485
418
                               mc->cred);
486
 
  if(ret != GNUTLS_E_SUCCESS) {
 
419
  if (ret != GNUTLS_E_SUCCESS) {
487
420
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
488
421
            safer_gnutls_strerror(ret));
489
 
    gnutls_deinit(*session);
 
422
    gnutls_deinit (*session);
490
423
    return -1;
491
424
  }
492
425
  
493
426
  /* ignore client certificate if any. */
494
 
  gnutls_certificate_server_set_request(*session,
495
 
                                        GNUTLS_CERT_IGNORE);
 
427
  gnutls_certificate_server_set_request (*session,
 
428
                                         GNUTLS_CERT_IGNORE);
496
429
  
497
 
  gnutls_dh_set_prime_bits(*session, mc->dh_bits);
 
430
  gnutls_dh_set_prime_bits (*session, mc->dh_bits);
498
431
  
499
432
  return 0;
500
433
}
508
441
                                      AvahiIfIndex if_index,
509
442
                                      mandos_context *mc){
510
443
  int ret, tcp_sd;
511
 
  ssize_t sret;
512
444
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
513
445
  char *buffer = NULL;
514
446
  char *decrypted_buffer;
520
452
  char interface[IF_NAMESIZE];
521
453
  gnutls_session_t session;
522
454
  
523
 
  ret = init_gnutls_session(mc, &session);
524
 
  if(ret != 0){
 
455
  ret = init_gnutls_session (mc, &session);
 
456
  if (ret != 0){
525
457
    return -1;
526
458
  }
527
459
  
535
467
    perror("socket");
536
468
    return -1;
537
469
  }
538
 
  
 
470
 
539
471
  if(debug){
540
472
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
541
473
      perror("if_indextoname");
549
481
  /* It would be nice to have a way to detect if we were passed an
550
482
     IPv4 address here.   Now we assume an IPv6 address. */
551
483
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
552
 
  if(ret < 0 ){
 
484
  if (ret < 0 ){
553
485
    perror("inet_pton");
554
486
    return -1;
555
487
  }
557
489
    fprintf(stderr, "Bad address: %s\n", ip);
558
490
    return -1;
559
491
  }
560
 
  to.in6.sin6_port = htons(port); /* Spurious warnings from
561
 
                                     -Wconversion and
562
 
                                     -Wunreachable-code */
 
492
  to.in6.sin6_port = htons(port); /* Spurious warning */
563
493
  
564
494
  to.in6.sin6_scope_id = (uint32_t)if_index;
565
495
  
578
508
  }
579
509
  
580
510
  ret = connect(tcp_sd, &to.in, sizeof(to));
581
 
  if(ret < 0){
 
511
  if (ret < 0){
582
512
    perror("connect");
583
513
    return -1;
584
514
  }
585
 
  
 
515
 
586
516
  const char *out = mandos_protocol_version;
587
517
  written = 0;
588
 
  while(true){
 
518
  while (true){
589
519
    size_t out_size = strlen(out);
590
 
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
 
520
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
591
521
                                   out_size - written));
592
 
    if(ret == -1){
 
522
    if (ret == -1){
593
523
      perror("write");
594
524
      retval = -1;
595
525
      goto mandos_end;
598
528
    if(written < out_size){
599
529
      continue;
600
530
    } else {
601
 
      if(out == mandos_protocol_version){
 
531
      if (out == mandos_protocol_version){
602
532
        written = 0;
603
533
        out = "\r\n";
604
534
      } else {
606
536
      }
607
537
    }
608
538
  }
609
 
  
 
539
 
610
540
  if(debug){
611
541
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
612
542
  }
613
543
  
614
 
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
615
 
  
 
544
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
 
545
 
616
546
  do{
617
 
    ret = gnutls_handshake(session);
 
547
    ret = gnutls_handshake (session);
618
548
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
619
549
  
620
 
  if(ret != GNUTLS_E_SUCCESS){
 
550
  if (ret != GNUTLS_E_SUCCESS){
621
551
    if(debug){
622
552
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
623
 
      gnutls_perror(ret);
 
553
      gnutls_perror (ret);
624
554
    }
625
555
    retval = -1;
626
556
    goto mandos_end;
632
562
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
633
563
            ip);
634
564
  }
635
 
  
 
565
 
636
566
  while(true){
637
567
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
638
568
                                   buffer_capacity);
639
 
    if(buffer_capacity == 0){
 
569
    if (buffer_capacity == 0){
640
570
      perror("adjustbuffer");
641
571
      retval = -1;
642
572
      goto mandos_end;
643
573
    }
644
574
    
645
 
    sret = gnutls_record_recv(session, buffer+buffer_length,
646
 
                              BUFFER_SIZE);
647
 
    if(sret == 0){
 
575
    ret = gnutls_record_recv(session, buffer+buffer_length,
 
576
                             BUFFER_SIZE);
 
577
    if (ret == 0){
648
578
      break;
649
579
    }
650
 
    if(sret < 0){
651
 
      switch(sret){
 
580
    if (ret < 0){
 
581
      switch(ret){
652
582
      case GNUTLS_E_INTERRUPTED:
653
583
      case GNUTLS_E_AGAIN:
654
584
        break;
655
585
      case GNUTLS_E_REHANDSHAKE:
656
586
        do{
657
 
          ret = gnutls_handshake(session);
 
587
          ret = gnutls_handshake (session);
658
588
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
659
 
        if(ret < 0){
 
589
        if (ret < 0){
660
590
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
661
 
          gnutls_perror(ret);
 
591
          gnutls_perror (ret);
662
592
          retval = -1;
663
593
          goto mandos_end;
664
594
        }
667
597
        fprintf(stderr, "Unknown error while reading data from"
668
598
                " encrypted session with Mandos server\n");
669
599
        retval = -1;
670
 
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
600
        gnutls_bye (session, GNUTLS_SHUT_RDWR);
671
601
        goto mandos_end;
672
602
      }
673
603
    } else {
674
 
      buffer_length += (size_t) sret;
 
604
      buffer_length += (size_t) ret;
675
605
    }
676
606
  }
677
607
  
679
609
    fprintf(stderr, "Closing TLS session\n");
680
610
  }
681
611
  
682
 
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
612
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
683
613
  
684
 
  if(buffer_length > 0){
685
 
    decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
 
614
  if (buffer_length > 0){
 
615
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
686
616
                                               buffer_length,
687
 
                                               &decrypted_buffer);
688
 
    if(decrypted_buffer_size >= 0){
 
617
                                               &decrypted_buffer,
 
618
                                               keydir);
 
619
    if (decrypted_buffer_size >= 0){
689
620
      written = 0;
690
621
      while(written < (size_t) decrypted_buffer_size){
691
 
        ret = (int)fwrite(decrypted_buffer + written, 1,
692
 
                          (size_t)decrypted_buffer_size - written,
693
 
                          stdout);
 
622
        ret = (int)fwrite (decrypted_buffer + written, 1,
 
623
                           (size_t)decrypted_buffer_size - written,
 
624
                           stdout);
694
625
        if(ret == 0 and ferror(stdout)){
695
626
          if(debug){
696
627
            fprintf(stderr, "Error writing encrypted data: %s\n",
705
636
    } else {
706
637
      retval = -1;
707
638
    }
708
 
  } else {
709
 
    retval = -1;
710
639
  }
711
640
  
712
641
  /* Shutdown procedure */
713
642
  
714
643
 mandos_end:
715
644
  free(buffer);
716
 
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
717
 
  if(ret == -1){
718
 
    perror("close");
719
 
  }
720
 
  gnutls_deinit(session);
 
645
  close(tcp_sd);
 
646
  gnutls_deinit (session);
721
647
  return retval;
722
648
}
723
649
 
741
667
  /* Called whenever a service has been resolved successfully or
742
668
     timed out */
743
669
  
744
 
  switch(event) {
 
670
  switch (event) {
745
671
  default:
746
672
  case AVAHI_RESOLVER_FAILURE:
747
673
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
755
681
      avahi_address_snprint(ip, sizeof(ip), address);
756
682
      if(debug){
757
683
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
758
 
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
759
 
                ip, (intmax_t)interface, port);
 
684
                PRIu16 ") on port %d\n", name, host_name, ip,
 
685
                interface, port);
760
686
      }
761
687
      int ret = start_mandos_communication(ip, port, interface, mc);
762
 
      if(ret == 0){
 
688
      if (ret == 0){
763
689
        avahi_simple_poll_quit(mc->simple_poll);
764
690
      }
765
691
    }
783
709
  /* Called whenever a new services becomes available on the LAN or
784
710
     is removed from the LAN */
785
711
  
786
 
  switch(event) {
 
712
  switch (event) {
787
713
  default:
788
714
  case AVAHI_BROWSER_FAILURE:
789
715
    
798
724
       the callback function is called the Avahi server will free the
799
725
       resolver for us. */
800
726
    
801
 
    if(!(avahi_s_service_resolver_new(mc->server, interface,
 
727
    if (!(avahi_s_service_resolver_new(mc->server, interface,
802
728
                                       protocol, name, type, domain,
803
729
                                       AVAHI_PROTO_INET6, 0,
804
730
                                       resolve_callback, mc)))
818
744
  }
819
745
}
820
746
 
 
747
/* Combines file name and path and returns the malloced new
 
748
   string. some sane checks could/should be added */
 
749
static char *combinepath(const char *first, const char *second){
 
750
  char *tmp;
 
751
  int ret = asprintf(&tmp, "%s/%s", first, second);
 
752
  if(ret < 0){
 
753
    return NULL;
 
754
  }
 
755
  return tmp;
 
756
}
 
757
 
 
758
 
821
759
int main(int argc, char *argv[]){
822
760
    AvahiSServiceBrowser *sb = NULL;
823
761
    int error;
824
762
    int ret;
825
 
    intmax_t tmpmax;
826
 
    int numchars;
827
763
    int exitcode = EXIT_SUCCESS;
828
764
    const char *interface = "eth0";
829
765
    struct ifreq network;
831
767
    uid_t uid;
832
768
    gid_t gid;
833
769
    char *connect_to = NULL;
834
 
    char tempdir[] = "/tmp/mandosXXXXXX";
835
770
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
836
 
    const char *seckey = PATHDIR "/" SECKEY;
837
 
    const char *pubkey = PATHDIR "/" PUBKEY;
838
 
    
 
771
    char *pubkeyfilename = NULL;
 
772
    char *seckeyfilename = NULL;
 
773
    const char *pubkeyname = "pubkey.txt";
 
774
    const char *seckeyname = "seckey.txt";
839
775
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
840
 
                          .dh_bits = 1024, .priority = "SECURE256"
841
 
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
776
                          .dh_bits = 1024, .priority = "SECURE256"};
842
777
    bool gnutls_initalized = false;
843
 
    bool gpgme_initalized = false;
844
 
    double delay = 2.5;
845
778
    
846
779
    {
847
780
      struct argp_option options[] = {
848
781
        { .name = "debug", .key = 128,
849
782
          .doc = "Debug mode", .group = 3 },
850
783
        { .name = "connect", .key = 'c',
851
 
          .arg = "ADDRESS:PORT",
852
 
          .doc = "Connect directly to a specific Mandos server",
 
784
          .arg = "IP",
 
785
          .doc = "Connect directly to a sepcified mandos server",
853
786
          .group = 1 },
854
787
        { .name = "interface", .key = 'i',
855
 
          .arg = "NAME",
856
 
          .doc = "Interface that will be used to search for Mandos"
857
 
          " servers",
 
788
          .arg = "INTERFACE",
 
789
          .doc = "Interface that Avahi will conntect through",
 
790
          .group = 1 },
 
791
        { .name = "keydir", .key = 'd',
 
792
          .arg = "KEYDIR",
 
793
          .doc = "Directory where the openpgp keyring is",
858
794
          .group = 1 },
859
795
        { .name = "seckey", .key = 's',
860
 
          .arg = "FILE",
861
 
          .doc = "OpenPGP secret key file base name",
 
796
          .arg = "SECKEY",
 
797
          .doc = "Secret openpgp key for gnutls authentication",
862
798
          .group = 1 },
863
799
        { .name = "pubkey", .key = 'p',
864
 
          .arg = "FILE",
865
 
          .doc = "OpenPGP public key file base name",
 
800
          .arg = "PUBKEY",
 
801
          .doc = "Public openpgp key for gnutls authentication",
866
802
          .group = 2 },
867
803
        { .name = "dh-bits", .key = 129,
868
804
          .arg = "BITS",
869
 
          .doc = "Bit length of the prime number used in the"
870
 
          " Diffie-Hellman key exchange",
 
805
          .doc = "dh-bits to use in gnutls communication",
871
806
          .group = 2 },
872
807
        { .name = "priority", .key = 130,
873
 
          .arg = "STRING",
874
 
          .doc = "GnuTLS priority string for the TLS handshake",
875
 
          .group = 1 },
876
 
        { .name = "delay", .key = 131,
877
 
          .arg = "SECONDS",
878
 
          .doc = "Maximum delay to wait for interface startup",
879
 
          .group = 2 },
 
808
          .arg = "PRIORITY",
 
809
          .doc = "GNUTLS priority", .group = 1 },
880
810
        { .name = NULL }
881
811
      };
 
812
 
882
813
      
883
 
      error_t parse_opt(int key, char *arg,
884
 
                        struct argp_state *state) {
885
 
        switch(key) {
886
 
        case 128:               /* --debug */
 
814
      error_t parse_opt (int key, char *arg,
 
815
                         struct argp_state *state) {
 
816
        /* Get the INPUT argument from `argp_parse', which we know is
 
817
           a pointer to our plugin list pointer. */
 
818
        switch (key) {
 
819
        case 128:
887
820
          debug = true;
888
821
          break;
889
 
        case 'c':               /* --connect */
 
822
        case 'c':
890
823
          connect_to = arg;
891
824
          break;
892
 
        case 'i':               /* --interface */
 
825
        case 'i':
893
826
          interface = arg;
894
827
          break;
895
 
        case 's':               /* --seckey */
896
 
          seckey = arg;
897
 
          break;
898
 
        case 'p':               /* --pubkey */
899
 
          pubkey = arg;
900
 
          break;
901
 
        case 129:               /* --dh-bits */
902
 
          ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
903
 
          if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
904
 
             or arg[numchars] != '\0'){
905
 
            fprintf(stderr, "Bad number of DH bits\n");
 
828
        case 'd':
 
829
          keydir = arg;
 
830
          break;
 
831
        case 's':
 
832
          seckeyname = arg;
 
833
          break;
 
834
        case 'p':
 
835
          pubkeyname = arg;
 
836
          break;
 
837
        case 129:
 
838
          errno = 0;
 
839
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
 
840
          if (errno){
 
841
            perror("strtol");
906
842
            exit(EXIT_FAILURE);
907
843
          }
908
 
          mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
909
844
          break;
910
 
        case 130:               /* --priority */
 
845
        case 130:
911
846
          mc.priority = arg;
912
847
          break;
913
 
        case 131:               /* --delay */
914
 
          ret = sscanf(arg, "%lf%n", &delay, &numchars);
915
 
          if(ret < 1 or arg[numchars] != '\0'){
916
 
            fprintf(stderr, "Bad delay\n");
917
 
            exit(EXIT_FAILURE);
918
 
          }
919
 
          break;
920
848
        case ARGP_KEY_ARG:
921
 
          argp_usage(state);
 
849
          argp_usage (state);
922
850
        case ARGP_KEY_END:
923
851
          break;
924
852
        default:
926
854
        }
927
855
        return 0;
928
856
      }
929
 
      
 
857
 
930
858
      struct argp argp = { .options = options, .parser = parse_opt,
931
859
                           .args_doc = "",
932
860
                           .doc = "Mandos client -- Get and decrypt"
933
 
                           " passwords from a Mandos server" };
934
 
      ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
935
 
      if(ret == ARGP_ERR_UNKNOWN){
 
861
                           " passwords from mandos server" };
 
862
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
863
      if (ret == ARGP_ERR_UNKNOWN){
936
864
        fprintf(stderr, "Unknown error while parsing arguments\n");
937
865
        exitcode = EXIT_FAILURE;
938
866
        goto end;
939
867
      }
940
868
    }
 
869
      
 
870
    pubkeyfilename = combinepath(keydir, pubkeyname);
 
871
    if (pubkeyfilename == NULL){
 
872
      perror("combinepath");
 
873
      exitcode = EXIT_FAILURE;
 
874
      goto end;
 
875
    }
 
876
    
 
877
    seckeyfilename = combinepath(keydir, seckeyname);
 
878
    if (seckeyfilename == NULL){
 
879
      perror("combinepath");
 
880
      exitcode = EXIT_FAILURE;
 
881
      goto end;
 
882
    }
 
883
 
 
884
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
 
885
    if (ret == -1){
 
886
      fprintf(stderr, "init_gnutls_global failed\n");
 
887
      exitcode = EXIT_FAILURE;
 
888
      goto end;
 
889
    } else {
 
890
      gnutls_initalized = true;
 
891
    }
941
892
    
942
893
    /* If the interface is down, bring it up */
943
894
    {
944
 
      // Lower kernel loglevel to KERN_NOTICE to avoid
945
 
      // KERN_INFO messages to mess up the prompt
946
 
      ret = klogctl(8, NULL, 5);
947
 
      if(ret == -1){
948
 
        perror("klogctl");
949
 
      }
950
 
 
951
895
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
952
896
      if(sd < 0) {
953
897
        perror("socket");
954
898
        exitcode = EXIT_FAILURE;
955
 
        ret = klogctl(7, NULL, 0);
956
 
        if(ret == -1){
957
 
          perror("klogctl");
958
 
        }
959
899
        goto end;
960
900
      }
961
901
      strcpy(network.ifr_name, interface);
962
902
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
963
903
      if(ret == -1){
964
904
        perror("ioctl SIOCGIFFLAGS");
965
 
        ret = klogctl(7, NULL, 0);
966
 
        if(ret == -1){
967
 
          perror("klogctl");
968
 
        }
969
905
        exitcode = EXIT_FAILURE;
970
906
        goto end;
971
907
      }
975
911
        if(ret == -1){
976
912
          perror("ioctl SIOCSIFFLAGS");
977
913
          exitcode = EXIT_FAILURE;
978
 
          ret = klogctl(7, NULL, 0);
979
 
          if(ret == -1){
980
 
            perror("klogctl");
981
 
          }
982
914
          goto end;
983
915
        }
984
916
      }
985
 
      // sleep checking until interface is running
986
 
      for(int i=0; i < delay * 4; i++){
987
 
        ret = ioctl(sd, SIOCGIFFLAGS, &network);
988
 
        if(ret == -1){
989
 
          perror("ioctl SIOCGIFFLAGS");
990
 
        } else if(network.ifr_flags & IFF_RUNNING){
991
 
          break;
992
 
        }
993
 
        struct timespec sleeptime = { .tv_nsec = 250000000 };
994
 
        nanosleep(&sleeptime, NULL);
995
 
      }
996
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
997
 
      if(ret == -1){
998
 
        perror("close");
999
 
      }
1000
 
      // Restores kernel loglevel to default
1001
 
      ret = klogctl(7, NULL, 0);
1002
 
      if(ret == -1){
1003
 
        perror("klogctl");
1004
 
      }
 
917
      close(sd);
1005
918
    }
1006
919
    
1007
920
    uid = getuid();
1008
921
    gid = getgid();
1009
922
    
1010
923
    ret = setuid(uid);
1011
 
    if(ret == -1){
 
924
    if (ret == -1){
1012
925
      perror("setuid");
1013
926
    }
1014
927
    
1015
928
    setgid(gid);
1016
 
    if(ret == -1){
 
929
    if (ret == -1){
1017
930
      perror("setgid");
1018
931
    }
1019
932
    
1020
 
    ret = init_gnutls_global(&mc, pubkey, seckey);
1021
 
    if(ret == -1){
1022
 
      fprintf(stderr, "init_gnutls_global failed\n");
1023
 
      exitcode = EXIT_FAILURE;
1024
 
      goto end;
1025
 
    } else {
1026
 
      gnutls_initalized = true;
1027
 
    }
1028
 
    
1029
 
    if(mkdtemp(tempdir) == NULL){
1030
 
      perror("mkdtemp");
1031
 
      tempdir[0] = '\0';
1032
 
      goto end;
1033
 
    }
1034
 
    
1035
 
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1036
 
      fprintf(stderr, "gpgme_initalized failed\n");
1037
 
      exitcode = EXIT_FAILURE;
1038
 
      goto end;
1039
 
    } else {
1040
 
      gpgme_initalized = true;
1041
 
    }
1042
 
    
1043
933
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1044
934
    if(if_index == 0){
1045
935
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1055
945
        exitcode = EXIT_FAILURE;
1056
946
        goto end;
1057
947
      }
1058
 
      uint16_t port;
1059
 
      ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1060
 
      if(ret < 1 or tmpmax != (uint16_t)tmpmax
1061
 
         or address[numchars+1] != '\0'){
1062
 
        fprintf(stderr, "Bad port number\n");
 
948
      errno = 0;
 
949
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
 
950
      if(errno){
 
951
        perror("Bad port number");
1063
952
        exitcode = EXIT_FAILURE;
1064
953
        goto end;
1065
954
      }
1066
 
      port = (uint16_t)tmpmax;
1067
955
      *address = '\0';
1068
956
      address = connect_to;
1069
957
      ret = start_mandos_communication(address, port, if_index, &mc);
1075
963
      goto end;
1076
964
    }
1077
965
    
1078
 
    if(not debug){
 
966
    if (not debug){
1079
967
      avahi_set_log_function(empty_log);
1080
968
    }
1081
969
    
1084
972
    
1085
973
    /* Allocate main Avahi loop object */
1086
974
    mc.simple_poll = avahi_simple_poll_new();
1087
 
    if(mc.simple_poll == NULL) {
 
975
    if (mc.simple_poll == NULL) {
1088
976
        fprintf(stderr, "Avahi: Failed to create simple poll"
1089
977
                " object.\n");
1090
978
        exitcode = EXIT_FAILURE;
1091
979
        goto end;
1092
980
    }
1093
 
    
 
981
 
1094
982
    {
1095
983
      AvahiServerConfig config;
1096
984
      /* Do not publish any local Zeroconf records */
1099
987
      config.publish_addresses = 0;
1100
988
      config.publish_workstation = 0;
1101
989
      config.publish_domain = 0;
1102
 
      
 
990
 
1103
991
      /* Allocate a new server */
1104
992
      mc.server = avahi_server_new(avahi_simple_poll_get
1105
993
                                   (mc.simple_poll), &config, NULL,
1106
994
                                   NULL, &error);
1107
 
      
 
995
    
1108
996
      /* Free the Avahi configuration data */
1109
997
      avahi_server_config_free(&config);
1110
998
    }
1111
999
    
1112
1000
    /* Check if creating the Avahi server object succeeded */
1113
 
    if(mc.server == NULL) {
 
1001
    if (mc.server == NULL) {
1114
1002
        fprintf(stderr, "Failed to create Avahi server: %s\n",
1115
1003
                avahi_strerror(error));
1116
1004
        exitcode = EXIT_FAILURE;
1122
1010
                                     AVAHI_PROTO_INET6,
1123
1011
                                     "_mandos._tcp", NULL, 0,
1124
1012
                                     browse_callback, &mc);
1125
 
    if(sb == NULL) {
 
1013
    if (sb == NULL) {
1126
1014
        fprintf(stderr, "Failed to create service browser: %s\n",
1127
1015
                avahi_strerror(avahi_server_errno(mc.server)));
1128
1016
        exitcode = EXIT_FAILURE;
1130
1018
    }
1131
1019
    
1132
1020
    /* Run the main loop */
1133
 
    
1134
 
    if(debug){
 
1021
 
 
1022
    if (debug){
1135
1023
      fprintf(stderr, "Starting Avahi loop search\n");
1136
1024
    }
1137
 
 
 
1025
    
1138
1026
    avahi_simple_poll_loop(mc.simple_poll);
1139
1027
    
1140
1028
 end:
1141
 
    
1142
 
    if(debug){
 
1029
 
 
1030
    if (debug){
1143
1031
      fprintf(stderr, "%s exiting\n", argv[0]);
1144
1032
    }
1145
1033
    
1146
1034
    /* Cleanup things */
1147
 
    if(sb != NULL)
 
1035
    if (sb != NULL)
1148
1036
        avahi_s_service_browser_free(sb);
1149
1037
    
1150
 
    if(mc.server != NULL)
 
1038
    if (mc.server != NULL)
1151
1039
        avahi_server_free(mc.server);
1152
 
    
1153
 
    if(mc.simple_poll != NULL)
 
1040
 
 
1041
    if (mc.simple_poll != NULL)
1154
1042
        avahi_simple_poll_free(mc.simple_poll);
1155
 
    
1156
 
    if(gnutls_initalized){
 
1043
    free(pubkeyfilename);
 
1044
    free(seckeyfilename);
 
1045
 
 
1046
    if (gnutls_initalized){
1157
1047
      gnutls_certificate_free_credentials(mc.cred);
1158
 
      gnutls_global_deinit();
1159
 
      gnutls_dh_params_deinit(mc.dh_params);
1160
 
    }
1161
 
    
1162
 
    if(gpgme_initalized){
1163
 
      gpgme_release(mc.ctx);
1164
 
    }
1165
 
    
1166
 
    /* Removes the temp directory used by GPGME */
1167
 
    if(tempdir[0] != '\0'){
1168
 
      DIR *d;
1169
 
      struct dirent *direntry;
1170
 
      d = opendir(tempdir);
1171
 
      if(d == NULL){
1172
 
        if(errno != ENOENT){
1173
 
          perror("opendir");
1174
 
        }
1175
 
      } else {
1176
 
        while(true){
1177
 
          direntry = readdir(d);
1178
 
          if(direntry == NULL){
1179
 
            break;
1180
 
          }
1181
 
          if(direntry->d_type == DT_REG){
1182
 
            char *fullname = NULL;
1183
 
            ret = asprintf(&fullname, "%s/%s", tempdir,
1184
 
                           direntry->d_name);
1185
 
            if(ret < 0){
1186
 
              perror("asprintf");
1187
 
              continue;
1188
 
            }
1189
 
            ret = unlink(fullname);
1190
 
            if(ret == -1){
1191
 
              fprintf(stderr, "unlink(\"%s\"): %s",
1192
 
                      fullname, strerror(errno));
1193
 
            }
1194
 
            free(fullname);
1195
 
          }
1196
 
        }
1197
 
        closedir(d);
1198
 
      }
1199
 
      ret = rmdir(tempdir);
1200
 
      if(ret == -1 and errno != ENOENT){
1201
 
        perror("rmdir");
1202
 
      }
1203
 
    }
1204
 
          
 
1048
      gnutls_global_deinit ();
 
1049
    }
 
1050
    
1205
1051
    return exitcode;
1206
1052
}