/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

Added configuration files support for mandos-client
Removed plus argument support for mandos-client

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() */
 
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
36
36
 
37
 
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
38
 
                                   stdout, ferror() */
 
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(), stdout,
 
38
                                   ferror() */
39
39
#include <stdint.h>             /* uint16_t, uint32_t */
40
40
#include <stddef.h>             /* NULL, size_t, ssize_t */
41
41
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
42
42
                                   srand() */
43
43
#include <stdbool.h>            /* bool, true */
44
44
#include <string.h>             /* memset(), strcmp(), strlen(),
45
 
                                   strerror(), asprintf(), strcpy() */
 
45
                                   strerror(), memcpy(), strcpy() */
46
46
#include <sys/ioctl.h>          /* ioctl */
47
47
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
48
48
                                   sockaddr_in6, PF_INET6,
49
49
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
50
 
                                   uid_t, gid_t, open(), opendir(), DIR */
51
 
#include <sys/stat.h>           /* open() */
 
50
                                   uid_t, gid_t */
 
51
#include <inttypes.h>           /* PRIu16 */
52
52
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
53
53
                                   struct in6_addr, inet_pton(),
54
54
                                   connect() */
55
 
#include <fcntl.h>              /* open() */
56
 
#include <dirent.h>             /* opendir(), struct dirent, readdir() */
57
 
#include <inttypes.h>           /* PRIu16 */
58
55
#include <assert.h>             /* assert() */
59
56
#include <errno.h>              /* perror(), errno */
60
57
#include <time.h>               /* time() */
61
58
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
62
59
                                   SIOCSIFFLAGS, if_indextoname(),
63
60
                                   if_nametoindex(), IF_NAMESIZE */
64
 
#include <netinet/in.h>
65
61
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
66
62
                                   getuid(), getgid(), setuid(),
67
63
                                   setgid() */
 
64
#include <netinet/in.h>
68
65
#include <arpa/inet.h>          /* inet_pton(), htons */
69
66
#include <iso646.h>             /* not, and */
70
67
#include <argp.h>               /* struct argp_option, error_t, struct
84
81
#include <avahi-common/error.h>
85
82
 
86
83
/* GnuTLS */
87
 
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and
88
 
                                   functions:
 
84
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and functions
89
85
                                   gnutls_*
90
86
                                   init_gnutls_session(),
91
87
                                   GNUTLS_* */
93
89
                                   GNUTLS_OPENPGP_FMT_BASE64 */
94
90
 
95
91
/* GPGME */
96
 
#include <gpgme.h>              /* All GPGME types, constants and
97
 
                                   functions:
 
92
#include <gpgme.h>              /* All GPGME types, constants and functions
98
93
                                   gpgme_*
99
94
                                   GPGME_PROTOCOL_OpenPGP,
100
95
                                   GPG_ERR_NO_* */
101
96
 
102
97
#define BUFFER_SIZE 256
103
98
 
104
 
#define PATHDIR "/conf/conf.d/mandos"
105
 
#define SECKEY "seckey.txt"
106
 
#define PUBKEY "pubkey.txt"
107
 
 
108
99
bool debug = false;
 
100
static const char *keydir = "/conf/conf.d/mandos";
109
101
static const char mandos_protocol_version[] = "1";
110
 
const char *argp_program_version = "mandos-client " VERSION;
 
102
const char *argp_program_version = "password-request 1.0";
111
103
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
112
104
 
113
105
/* Used for passing in values through the Avahi callback functions */
118
110
  unsigned int dh_bits;
119
111
  gnutls_dh_params_t dh_params;
120
112
  const char *priority;
121
 
  gpgme_ctx_t ctx;
122
113
} mandos_context;
123
114
 
124
115
/*
139
130
}
140
131
 
141
132
/* 
142
 
 * Initialize GPGME.
 
133
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
 
134
 * Returns -1 on error
143
135
 */
144
 
static bool init_gpgme(mandos_context *mc, const char *seckey,
145
 
                       const char *pubkey, const char *tempdir){
146
 
  int ret;
 
136
static ssize_t pgp_packet_decrypt (const char *cryptotext,
 
137
                                   size_t crypto_size,
 
138
                                   char **plaintext,
 
139
                                   const char *homedir){
 
140
  gpgme_data_t dh_crypto, dh_plain;
 
141
  gpgme_ctx_t ctx;
147
142
  gpgme_error_t rc;
 
143
  ssize_t ret;
 
144
  size_t plaintext_capacity = 0;
 
145
  ssize_t plaintext_length = 0;
148
146
  gpgme_engine_info_t engine_info;
149
147
  
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
148
  if (debug){
187
 
    fprintf(stderr, "Initialize gpgme\n");
 
149
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
188
150
  }
189
151
  
190
152
  /* Init GPGME */
193
155
  if (rc != GPG_ERR_NO_ERROR){
194
156
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
195
157
            gpgme_strsource(rc), gpgme_strerror(rc));
196
 
    return false;
 
158
    return -1;
197
159
  }
198
160
  
199
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
161
  /* Set GPGME home directory for the OpenPGP engine only */
200
162
  rc = gpgme_get_engine_info (&engine_info);
201
163
  if (rc != GPG_ERR_NO_ERROR){
202
164
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
203
165
            gpgme_strsource(rc), gpgme_strerror(rc));
204
 
    return false;
 
166
    return -1;
205
167
  }
206
168
  while(engine_info != NULL){
207
169
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
208
170
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
209
 
                            engine_info->file_name, tempdir);
 
171
                            engine_info->file_name, homedir);
210
172
      break;
211
173
    }
212
174
    engine_info = engine_info->next;
213
175
  }
214
176
  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");
 
177
    fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
 
178
    return -1;
250
179
  }
251
180
  
252
181
  /* Create new GPGME data buffer from memory cryptotext */
267
196
    return -1;
268
197
  }
269
198
  
 
199
  /* Create new GPGME "context" */
 
200
  rc = gpgme_new(&ctx);
 
201
  if (rc != GPG_ERR_NO_ERROR){
 
202
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
 
203
            gpgme_strsource(rc), gpgme_strerror(rc));
 
204
    plaintext_length = -1;
 
205
    goto decrypt_end;
 
206
  }
 
207
  
270
208
  /* Decrypt data from the cryptotext data buffer to the plaintext
271
209
     data buffer */
272
 
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
 
210
  rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
273
211
  if (rc != GPG_ERR_NO_ERROR){
274
212
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
275
213
            gpgme_strsource(rc), gpgme_strerror(rc));
276
214
    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
215
    goto decrypt_end;
306
216
  }
307
217
  
309
219
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
310
220
  }
311
221
  
 
222
  if (debug){
 
223
    gpgme_decrypt_result_t result;
 
224
    result = gpgme_op_decrypt_result(ctx);
 
225
    if (result == NULL){
 
226
      fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
227
    } else {
 
228
      fprintf(stderr, "Unsupported algorithm: %s\n",
 
229
              result->unsupported_algorithm);
 
230
      fprintf(stderr, "Wrong key usage: %u\n",
 
231
              result->wrong_key_usage);
 
232
      if(result->file_name != NULL){
 
233
        fprintf(stderr, "File name: %s\n", result->file_name);
 
234
      }
 
235
      gpgme_recipient_t recipient;
 
236
      recipient = result->recipients;
 
237
      if(recipient){
 
238
        while(recipient != NULL){
 
239
          fprintf(stderr, "Public key algorithm: %s\n",
 
240
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
241
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
242
          fprintf(stderr, "Secret key available: %s\n",
 
243
                  recipient->status == GPG_ERR_NO_SECKEY
 
244
                  ? "No" : "Yes");
 
245
          recipient = recipient->next;
 
246
        }
 
247
      }
 
248
    }
 
249
  }
 
250
  
312
251
  /* Seek back to the beginning of the GPGME plaintext data buffer */
313
252
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
314
 
    perror("gpgme_data_seek");
 
253
    perror("pgpme_data_seek");
315
254
    plaintext_length = -1;
316
255
    goto decrypt_end;
317
256
  }
341
280
    }
342
281
    plaintext_length += ret;
343
282
  }
344
 
  
 
283
 
345
284
  if(debug){
346
285
    fprintf(stderr, "Decrypted password is: ");
347
286
    for(ssize_t i = 0; i < plaintext_length; i++){
361
300
}
362
301
 
363
302
static const char * safer_gnutls_strerror (int value) {
364
 
  const char *ret = gnutls_strerror (value); /* Spurious warning */
 
303
  const char *ret = gnutls_strerror (value);
365
304
  if (ret == NULL)
366
305
    ret = "(unknown)";
367
306
  return ret;
374
313
}
375
314
 
376
315
static int init_gnutls_global(mandos_context *mc,
377
 
                              const char *pubkeyfilename,
378
 
                              const char *seckeyfilename){
 
316
                              const char *pubkeyfile,
 
317
                              const char *seckeyfile){
379
318
  int ret;
380
319
  
381
320
  if(debug){
400
339
  /* OpenPGP credentials */
401
340
  gnutls_certificate_allocate_credentials(&mc->cred);
402
341
  if (ret != GNUTLS_E_SUCCESS){
403
 
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
404
 
                                                     warning */
 
342
    fprintf (stderr, "GnuTLS memory error: %s\n",
405
343
             safer_gnutls_strerror(ret));
406
344
    gnutls_global_deinit ();
407
345
    return -1;
408
346
  }
409
347
  
410
348
  if(debug){
411
 
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
412
 
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
413
 
            seckeyfilename);
 
349
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
 
350
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
351
            seckeyfile);
414
352
  }
415
353
  
416
354
  ret = gnutls_certificate_set_openpgp_key_file
417
 
    (mc->cred, pubkeyfilename, seckeyfilename,
418
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
355
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
419
356
  if (ret != GNUTLS_E_SUCCESS) {
420
357
    fprintf(stderr,
421
358
            "Error[%d] while reading the OpenPGP key pair ('%s',"
422
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
423
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
 
359
            " '%s')\n", ret, pubkeyfile, seckeyfile);
 
360
    fprintf(stdout, "The GnuTLS error is: %s\n",
424
361
            safer_gnutls_strerror(ret));
425
362
    goto globalfail;
426
363
  }
440
377
  }
441
378
  
442
379
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
443
 
  
 
380
 
444
381
  return 0;
445
 
  
 
382
 
446
383
 globalfail:
447
 
  
 
384
 
448
385
  gnutls_certificate_free_credentials(mc->cred);
449
386
  gnutls_global_deinit();
450
 
  gnutls_dh_params_deinit(mc->dh_params);
451
387
  return -1;
 
388
 
452
389
}
453
390
 
454
391
static int init_gnutls_session(mandos_context *mc,
526
463
    perror("socket");
527
464
    return -1;
528
465
  }
529
 
  
 
466
 
530
467
  if(debug){
531
468
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
532
469
      perror("if_indextoname");
535
472
    fprintf(stderr, "Binding to interface %s\n", interface);
536
473
  }
537
474
  
538
 
  memset(&to, 0, sizeof(to));
 
475
  memset(&to,0,sizeof(to));     /* Spurious warning */
539
476
  to.in6.sin6_family = AF_INET6;
540
477
  /* It would be nice to have a way to detect if we were passed an
541
478
     IPv4 address here.   Now we assume an IPv6 address. */
571
508
    perror("connect");
572
509
    return -1;
573
510
  }
574
 
  
 
511
 
575
512
  const char *out = mandos_protocol_version;
576
513
  written = 0;
577
514
  while (true){
595
532
      }
596
533
    }
597
534
  }
598
 
  
 
535
 
599
536
  if(debug){
600
537
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
601
538
  }
602
539
  
603
540
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
604
 
  
 
541
 
605
542
  do{
606
543
    ret = gnutls_handshake (session);
607
544
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
621
558
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
622
559
            ip);
623
560
  }
624
 
  
 
561
 
625
562
  while(true){
626
563
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
627
564
                                   buffer_capacity);
671
608
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
672
609
  
673
610
  if (buffer_length > 0){
674
 
    decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
 
611
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
675
612
                                               buffer_length,
676
 
                                               &decrypted_buffer);
 
613
                                               &decrypted_buffer,
 
614
                                               keydir);
677
615
    if (decrypted_buffer_size >= 0){
678
616
      written = 0;
679
617
      while(written < (size_t) decrypted_buffer_size){
694
632
    } else {
695
633
      retval = -1;
696
634
    }
697
 
  } else {
698
 
    retval = -1;
699
635
  }
700
636
  
701
637
  /* Shutdown procedure */
702
638
  
703
639
 mandos_end:
704
640
  free(buffer);
705
 
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
706
 
  if(ret == -1){
707
 
    perror("close");
708
 
  }
 
641
  close(tcp_sd);
709
642
  gnutls_deinit (session);
710
643
  return retval;
711
644
}
725
658
                             flags,
726
659
                             void* userdata) {
727
660
  mandos_context *mc = userdata;
728
 
  assert(r);
 
661
  assert(r);                    /* Spurious warning */
729
662
  
730
663
  /* Called whenever a service has been resolved successfully or
731
664
     timed out */
767
700
                             flags,
768
701
                             void* userdata) {
769
702
  mandos_context *mc = userdata;
770
 
  assert(b);
 
703
  assert(b);                    /* Spurious warning */
771
704
  
772
705
  /* Called whenever a new services becomes available on the LAN or
773
706
     is removed from the LAN */
807
740
  }
808
741
}
809
742
 
 
743
/* Combines file name and path and returns the malloced new
 
744
   string. some sane checks could/should be added */
 
745
static const char *combinepath(const char *first, const char *second){
 
746
  size_t f_len = strlen(first);
 
747
  size_t s_len = strlen(second);
 
748
  char *tmp = malloc(f_len + s_len + 2);
 
749
  if (tmp == NULL){
 
750
    return NULL;
 
751
  }
 
752
  if(f_len > 0){
 
753
    memcpy(tmp, first, f_len);  /* Spurious warning */
 
754
  }
 
755
  tmp[f_len] = '/';
 
756
  if(s_len > 0){
 
757
    memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
 
758
  }
 
759
  tmp[f_len + 1 + s_len] = '\0';
 
760
  return tmp;
 
761
}
 
762
 
 
763
 
810
764
int main(int argc, char *argv[]){
811
765
    AvahiSServiceBrowser *sb = NULL;
812
766
    int error;
818
772
    uid_t uid;
819
773
    gid_t gid;
820
774
    char *connect_to = NULL;
821
 
    char tempdir[] = "/tmp/mandosXXXXXX";
822
775
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
823
 
    const char *seckey = PATHDIR "/" SECKEY;
824
 
    const char *pubkey = PATHDIR "/" PUBKEY;
825
 
    
 
776
    const char *pubkeyfile = "pubkey.txt";
 
777
    const char *seckeyfile = "seckey.txt";
826
778
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
827
 
                          .dh_bits = 1024, .priority = "SECURE256"
828
 
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
779
                          .dh_bits = 1024, .priority = "SECURE256"};
829
780
    bool gnutls_initalized = false;
830
 
    bool gpgme_initalized = false;
831
781
    
832
782
    {
833
783
      struct argp_option options[] = {
834
784
        { .name = "debug", .key = 128,
835
785
          .doc = "Debug mode", .group = 3 },
836
786
        { .name = "connect", .key = 'c',
837
 
          .arg = "ADDRESS:PORT",
838
 
          .doc = "Connect directly to a specific Mandos server",
 
787
          .arg = "IP",
 
788
          .doc = "Connect directly to a sepcified mandos server",
839
789
          .group = 1 },
840
790
        { .name = "interface", .key = 'i',
841
 
          .arg = "NAME",
842
 
          .doc = "Interface that will be used to search for Mandos"
843
 
          " servers",
 
791
          .arg = "INTERFACE",
 
792
          .doc = "Interface that Avahi will conntect through",
 
793
          .group = 1 },
 
794
        { .name = "keydir", .key = 'd',
 
795
          .arg = "KEYDIR",
 
796
          .doc = "Directory where the openpgp keyring is",
844
797
          .group = 1 },
845
798
        { .name = "seckey", .key = 's',
846
 
          .arg = "FILE",
847
 
          .doc = "OpenPGP secret key file base name",
 
799
          .arg = "SECKEY",
 
800
          .doc = "Secret openpgp key for gnutls authentication",
848
801
          .group = 1 },
849
802
        { .name = "pubkey", .key = 'p',
850
 
          .arg = "FILE",
851
 
          .doc = "OpenPGP public key file base name",
 
803
          .arg = "PUBKEY",
 
804
          .doc = "Public openpgp key for gnutls authentication",
852
805
          .group = 2 },
853
806
        { .name = "dh-bits", .key = 129,
854
807
          .arg = "BITS",
855
 
          .doc = "Bit length of the prime number used in the"
856
 
          " Diffie-Hellman key exchange",
 
808
          .doc = "dh-bits to use in gnutls communication",
857
809
          .group = 2 },
858
810
        { .name = "priority", .key = 130,
859
 
          .arg = "STRING",
860
 
          .doc = "GnuTLS priority string for the TLS handshake",
861
 
          .group = 1 },
 
811
          .arg = "PRIORITY",
 
812
          .doc = "GNUTLS priority", .group = 1 },
862
813
        { .name = NULL }
863
814
      };
 
815
 
864
816
      
865
817
      error_t parse_opt (int key, char *arg,
866
818
                         struct argp_state *state) {
867
819
        /* Get the INPUT argument from `argp_parse', which we know is
868
820
           a pointer to our plugin list pointer. */
869
821
        switch (key) {
870
 
        case 128:               /* --debug */
 
822
        case 128:
871
823
          debug = true;
872
824
          break;
873
 
        case 'c':               /* --connect */
 
825
        case 'c':
874
826
          connect_to = arg;
875
827
          break;
876
 
        case 'i':               /* --interface */
 
828
        case 'i':
877
829
          interface = arg;
878
830
          break;
879
 
        case 's':               /* --seckey */
880
 
          seckey = arg;
881
 
          break;
882
 
        case 'p':               /* --pubkey */
883
 
          pubkey = arg;
884
 
          break;
885
 
        case 129:               /* --dh-bits */
 
831
        case 'd':
 
832
          keydir = arg;
 
833
          break;
 
834
        case 's':
 
835
          seckeyfile = arg;
 
836
          break;
 
837
        case 'p':
 
838
          pubkeyfile = arg;
 
839
          break;
 
840
        case 129:
886
841
          errno = 0;
887
842
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
888
843
          if (errno){
890
845
            exit(EXIT_FAILURE);
891
846
          }
892
847
          break;
893
 
        case 130:               /* --priority */
 
848
        case 130:
894
849
          mc.priority = arg;
895
850
          break;
896
851
        case ARGP_KEY_ARG:
902
857
        }
903
858
        return 0;
904
859
      }
905
 
      
 
860
 
906
861
      struct argp argp = { .options = options, .parser = parse_opt,
907
862
                           .args_doc = "",
908
863
                           .doc = "Mandos client -- Get and decrypt"
909
 
                           " passwords from a Mandos server" };
 
864
                           " passwords from mandos server" };
910
865
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
911
866
      if (ret == ARGP_ERR_UNKNOWN){
912
867
        fprintf(stderr, "Unknown error while parsing arguments\n");
914
869
        goto end;
915
870
      }
916
871
    }
 
872
      
 
873
    pubkeyfile = combinepath(keydir, pubkeyfile);
 
874
    if (pubkeyfile == NULL){
 
875
      perror("combinepath");
 
876
      exitcode = EXIT_FAILURE;
 
877
      goto end;
 
878
    }
 
879
    
 
880
    seckeyfile = combinepath(keydir, seckeyfile);
 
881
    if (seckeyfile == NULL){
 
882
      perror("combinepath");
 
883
      exitcode = EXIT_FAILURE;
 
884
      goto end;
 
885
    }
 
886
 
 
887
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
 
888
    if (ret == -1){
 
889
      fprintf(stderr, "init_gnutls_global failed\n");
 
890
      exitcode = EXIT_FAILURE;
 
891
      goto end;
 
892
    } else {
 
893
      gnutls_initalized = true;
 
894
    }
917
895
    
918
896
    /* If the interface is down, bring it up */
919
897
    {
923
901
        exitcode = EXIT_FAILURE;
924
902
        goto end;
925
903
      }
926
 
      strcpy(network.ifr_name, interface);
 
904
      strcpy(network.ifr_name, interface); /* Spurious warning */
927
905
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
928
906
      if(ret == -1){
929
907
        perror("ioctl SIOCGIFFLAGS");
939
917
          goto end;
940
918
        }
941
919
      }
942
 
      ret = TEMP_FAILURE_RETRY(close(sd));
943
 
      if(ret == -1){
944
 
        perror("close");
945
 
      }
 
920
      close(sd);
946
921
    }
947
922
    
948
923
    uid = getuid();
958
933
      perror("setgid");
959
934
    }
960
935
    
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;
982
 
    }
983
 
    
984
936
    if_index = (AvahiIfIndex) if_nametoindex(interface);
985
937
    if(if_index == 0){
986
938
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1029
981
        exitcode = EXIT_FAILURE;
1030
982
        goto end;
1031
983
    }
1032
 
    
 
984
 
1033
985
    {
1034
986
      AvahiServerConfig config;
1035
987
      /* Do not publish any local Zeroconf records */
1038
990
      config.publish_addresses = 0;
1039
991
      config.publish_workstation = 0;
1040
992
      config.publish_domain = 0;
1041
 
      
 
993
 
1042
994
      /* Allocate a new server */
1043
995
      mc.server = avahi_server_new(avahi_simple_poll_get
1044
996
                                   (mc.simple_poll), &config, NULL,
1045
997
                                   NULL, &error);
1046
 
      
 
998
    
1047
999
      /* Free the Avahi configuration data */
1048
1000
      avahi_server_config_free(&config);
1049
1001
    }
1069
1021
    }
1070
1022
    
1071
1023
    /* Run the main loop */
1072
 
    
 
1024
 
1073
1025
    if (debug){
1074
1026
      fprintf(stderr, "Starting Avahi loop search\n");
1075
1027
    }
1077
1029
    avahi_simple_poll_loop(mc.simple_poll);
1078
1030
    
1079
1031
 end:
1080
 
    
 
1032
 
1081
1033
    if (debug){
1082
1034
      fprintf(stderr, "%s exiting\n", argv[0]);
1083
1035
    }
1088
1040
    
1089
1041
    if (mc.server != NULL)
1090
1042
        avahi_server_free(mc.server);
1091
 
    
 
1043
 
1092
1044
    if (mc.simple_poll != NULL)
1093
1045
        avahi_simple_poll_free(mc.simple_poll);
1094
 
    
 
1046
    free(pubkeyfile);
 
1047
    free(seckeyfile);
 
1048
 
1095
1049
    if (gnutls_initalized){
1096
1050
      gnutls_certificate_free_credentials(mc.cred);
1097
1051
      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
 
          
 
1052
    }
 
1053
    
1142
1054
    return exitcode;
1143
1055
}