/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/mandos-client.c

* plugin-runner.c: Only space changes.
* plugins.d/mandos-client.c: - '' -
* plugins.d/password-prompt.c: - '' -

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