/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/mandosclient.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-03 16:05:52 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080803160552-mvqy5klry3wn57x1
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
                                         own code block.
  (start_mandos_communication): Moved call to "initgnutls" to
                                beginning.  Renamed label "exit" to
                                "mandos_end".  Bug fix: set return
                                code if failure to realloc buffer.
                                Close TLS session as early as
                                possible.
  (resolve_callback): Also print interface number when debugging.
  (main): Moved "debug_int" and "config" into their own respective
          code blocks.

Show diffs side-by-side

added added

removed removed

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