/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:
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
 
/*
105
 
  #define PATHDIR "/conf/conf.d/mandos"
106
 
*/
107
 
 
108
 
#define PATHDIR "/conf/conf.d/mandos"
109
 
#define SECKEY "seckey.txt"
110
 
#define PUBKEY "pubkey.txt"
111
 
 
112
99
bool debug = false;
 
100
static const char *keydir = "/conf/conf.d/mandos";
113
101
static const char mandos_protocol_version[] = "1";
114
 
const char *argp_program_version = "mandos-client 1.0";
 
102
const char *argp_program_version = "password-request 1.0";
115
103
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
116
104
 
117
105
/* Used for passing in values through the Avahi callback functions */
122
110
  unsigned int dh_bits;
123
111
  gnutls_dh_params_t dh_params;
124
112
  const char *priority;
125
 
  gpgme_ctx_t ctx;
126
113
} mandos_context;
127
114
 
128
115
/*
143
130
}
144
131
 
145
132
/* 
146
 
 * Initialize GPGME.
 
133
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
 
134
 * Returns -1 on error
147
135
 */
148
 
static bool init_gpgme(mandos_context *mc, const char *seckey,
149
 
                       const char *pubkey, const char *tempdir){
150
 
  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;
151
142
  gpgme_error_t rc;
 
143
  ssize_t ret;
 
144
  size_t plaintext_capacity = 0;
 
145
  ssize_t plaintext_length = 0;
152
146
  gpgme_engine_info_t engine_info;
153
147
  
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 = 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 = 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
148
  if (debug){
191
 
    fprintf(stderr, "Initialize gpgme\n");
 
149
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
192
150
  }
193
151
  
194
152
  /* Init GPGME */
197
155
  if (rc != GPG_ERR_NO_ERROR){
198
156
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
199
157
            gpgme_strsource(rc), gpgme_strerror(rc));
200
 
    return false;
 
158
    return -1;
201
159
  }
202
160
  
203
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
161
  /* Set GPGME home directory for the OpenPGP engine only */
204
162
  rc = gpgme_get_engine_info (&engine_info);
205
163
  if (rc != GPG_ERR_NO_ERROR){
206
164
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
207
165
            gpgme_strsource(rc), gpgme_strerror(rc));
208
 
    return false;
 
166
    return -1;
209
167
  }
210
168
  while(engine_info != NULL){
211
169
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
212
170
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
213
 
                            engine_info->file_name, tempdir);
 
171
                            engine_info->file_name, homedir);
214
172
      break;
215
173
    }
216
174
    engine_info = engine_info->next;
217
175
  }
218
176
  if(engine_info == NULL){
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");
 
177
    fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
 
178
    return -1;
254
179
  }
255
180
  
256
181
  /* Create new GPGME data buffer from memory cryptotext */
271
196
    return -1;
272
197
  }
273
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
  
274
208
  /* Decrypt data from the cryptotext data buffer to the plaintext
275
209
     data buffer */
276
 
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
 
210
  rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
277
211
  if (rc != GPG_ERR_NO_ERROR){
278
212
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
279
213
            gpgme_strsource(rc), gpgme_strerror(rc));
280
214
    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
 
    }
309
215
    goto decrypt_end;
310
216
  }
311
217
  
313
219
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
314
220
  }
315
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
  
316
251
  /* Seek back to the beginning of the GPGME plaintext data buffer */
317
252
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
318
 
    perror("gpgme_data_seek");
 
253
    perror("pgpme_data_seek");
319
254
    plaintext_length = -1;
320
255
    goto decrypt_end;
321
256
  }
345
280
    }
346
281
    plaintext_length += ret;
347
282
  }
348
 
  
 
283
 
349
284
  if(debug){
350
285
    fprintf(stderr, "Decrypted password is: ");
351
286
    for(ssize_t i = 0; i < plaintext_length; i++){
365
300
}
366
301
 
367
302
static const char * safer_gnutls_strerror (int value) {
368
 
  const char *ret = gnutls_strerror (value); /* Spurious warning */
 
303
  const char *ret = gnutls_strerror (value);
369
304
  if (ret == NULL)
370
305
    ret = "(unknown)";
371
306
  return ret;
378
313
}
379
314
 
380
315
static int init_gnutls_global(mandos_context *mc,
381
 
                              const char *pubkeyfilename,
382
 
                              const char *seckeyfilename){
 
316
                              const char *pubkeyfile,
 
317
                              const char *seckeyfile){
383
318
  int ret;
384
319
  
385
320
  if(debug){
404
339
  /* OpenPGP credentials */
405
340
  gnutls_certificate_allocate_credentials(&mc->cred);
406
341
  if (ret != GNUTLS_E_SUCCESS){
407
 
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
408
 
                                                     warning */
 
342
    fprintf (stderr, "GnuTLS memory error: %s\n",
409
343
             safer_gnutls_strerror(ret));
410
344
    gnutls_global_deinit ();
411
345
    return -1;
412
346
  }
413
347
  
414
348
  if(debug){
415
 
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
416
 
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
417
 
            seckeyfilename);
 
349
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
 
350
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
351
            seckeyfile);
418
352
  }
419
353
  
420
354
  ret = gnutls_certificate_set_openpgp_key_file
421
 
    (mc->cred, pubkeyfilename, seckeyfilename,
422
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
355
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
423
356
  if (ret != GNUTLS_E_SUCCESS) {
424
357
    fprintf(stderr,
425
358
            "Error[%d] while reading the OpenPGP key pair ('%s',"
426
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
427
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
 
359
            " '%s')\n", ret, pubkeyfile, seckeyfile);
 
360
    fprintf(stdout, "The GnuTLS error is: %s\n",
428
361
            safer_gnutls_strerror(ret));
429
362
    goto globalfail;
430
363
  }
444
377
  }
445
378
  
446
379
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
447
 
  
 
380
 
448
381
  return 0;
449
 
  
 
382
 
450
383
 globalfail:
451
 
  
 
384
 
452
385
  gnutls_certificate_free_credentials(mc->cred);
453
386
  gnutls_global_deinit();
454
 
  gnutls_dh_params_deinit(mc->dh_params);
455
387
  return -1;
 
388
 
456
389
}
457
390
 
458
391
static int init_gnutls_session(mandos_context *mc,
530
463
    perror("socket");
531
464
    return -1;
532
465
  }
533
 
  
 
466
 
534
467
  if(debug){
535
468
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
536
469
      perror("if_indextoname");
539
472
    fprintf(stderr, "Binding to interface %s\n", interface);
540
473
  }
541
474
  
542
 
  memset(&to, 0, sizeof(to));
 
475
  memset(&to,0,sizeof(to));     /* Spurious warning */
543
476
  to.in6.sin6_family = AF_INET6;
544
477
  /* It would be nice to have a way to detect if we were passed an
545
478
     IPv4 address here.   Now we assume an IPv6 address. */
575
508
    perror("connect");
576
509
    return -1;
577
510
  }
578
 
  
 
511
 
579
512
  const char *out = mandos_protocol_version;
580
513
  written = 0;
581
514
  while (true){
599
532
      }
600
533
    }
601
534
  }
602
 
  
 
535
 
603
536
  if(debug){
604
537
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
605
538
  }
606
539
  
607
540
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
608
 
  
 
541
 
609
542
  do{
610
543
    ret = gnutls_handshake (session);
611
544
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
625
558
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
626
559
            ip);
627
560
  }
628
 
  
 
561
 
629
562
  while(true){
630
563
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
631
564
                                   buffer_capacity);
675
608
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
676
609
  
677
610
  if (buffer_length > 0){
678
 
    decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
 
611
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
679
612
                                               buffer_length,
680
 
                                               &decrypted_buffer);
 
613
                                               &decrypted_buffer,
 
614
                                               keydir);
681
615
    if (decrypted_buffer_size >= 0){
682
616
      written = 0;
683
617
      while(written < (size_t) decrypted_buffer_size){
698
632
    } else {
699
633
      retval = -1;
700
634
    }
701
 
  } else {
702
 
    retval = -1;
703
635
  }
704
636
  
705
637
  /* Shutdown procedure */
706
638
  
707
639
 mandos_end:
708
640
  free(buffer);
709
 
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
710
 
  if(ret == -1){
711
 
    perror("close");
712
 
  }
 
641
  close(tcp_sd);
713
642
  gnutls_deinit (session);
714
643
  return retval;
715
644
}
729
658
                             flags,
730
659
                             void* userdata) {
731
660
  mandos_context *mc = userdata;
732
 
  assert(r);
 
661
  assert(r);                    /* Spurious warning */
733
662
  
734
663
  /* Called whenever a service has been resolved successfully or
735
664
     timed out */
771
700
                             flags,
772
701
                             void* userdata) {
773
702
  mandos_context *mc = userdata;
774
 
  assert(b);
 
703
  assert(b);                    /* Spurious warning */
775
704
  
776
705
  /* Called whenever a new services becomes available on the LAN or
777
706
     is removed from the LAN */
811
740
  }
812
741
}
813
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
 
814
764
int main(int argc, char *argv[]){
815
765
    AvahiSServiceBrowser *sb = NULL;
816
766
    int error;
822
772
    uid_t uid;
823
773
    gid_t gid;
824
774
    char *connect_to = NULL;
825
 
    char tempdir[] = "/tmp/mandosXXXXXX";
826
775
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
827
 
    const char *seckey = PATHDIR "/" SECKEY;
828
 
    const char *pubkey = PATHDIR "/" PUBKEY;
829
 
    
 
776
    const char *pubkeyfile = "pubkey.txt";
 
777
    const char *seckeyfile = "seckey.txt";
830
778
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
831
 
                          .dh_bits = 1024, .priority = "SECURE256"
832
 
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
779
                          .dh_bits = 1024, .priority = "SECURE256"};
833
780
    bool gnutls_initalized = false;
834
 
    bool gpgme_initalized = false;
835
781
    
836
782
    {
837
783
      struct argp_option options[] = {
838
784
        { .name = "debug", .key = 128,
839
785
          .doc = "Debug mode", .group = 3 },
840
786
        { .name = "connect", .key = 'c',
841
 
          .arg = "ADDRESS:PORT",
842
 
          .doc = "Connect directly to a specific Mandos server",
 
787
          .arg = "IP",
 
788
          .doc = "Connect directly to a sepcified mandos server",
843
789
          .group = 1 },
844
790
        { .name = "interface", .key = 'i',
845
 
          .arg = "NAME",
846
 
          .doc = "Interface that will be used to search for Mandos"
847
 
          " 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",
848
797
          .group = 1 },
849
798
        { .name = "seckey", .key = 's',
850
 
          .arg = "FILE",
851
 
          .doc = "OpenPGP secret key file base name",
 
799
          .arg = "SECKEY",
 
800
          .doc = "Secret openpgp key for gnutls authentication",
852
801
          .group = 1 },
853
802
        { .name = "pubkey", .key = 'p',
854
 
          .arg = "FILE",
855
 
          .doc = "OpenPGP public key file base name",
 
803
          .arg = "PUBKEY",
 
804
          .doc = "Public openpgp key for gnutls authentication",
856
805
          .group = 2 },
857
806
        { .name = "dh-bits", .key = 129,
858
807
          .arg = "BITS",
859
 
          .doc = "Bit length of the prime number used in the"
860
 
          " Diffie-Hellman key exchange",
 
808
          .doc = "dh-bits to use in gnutls communication",
861
809
          .group = 2 },
862
810
        { .name = "priority", .key = 130,
863
 
          .arg = "STRING",
864
 
          .doc = "GnuTLS priority string for the TLS handshake",
865
 
          .group = 1 },
 
811
          .arg = "PRIORITY",
 
812
          .doc = "GNUTLS priority", .group = 1 },
866
813
        { .name = NULL }
867
814
      };
 
815
 
868
816
      
869
817
      error_t parse_opt (int key, char *arg,
870
818
                         struct argp_state *state) {
871
819
        /* Get the INPUT argument from `argp_parse', which we know is
872
820
           a pointer to our plugin list pointer. */
873
821
        switch (key) {
874
 
        case 128:               /* --debug */
 
822
        case 128:
875
823
          debug = true;
876
824
          break;
877
 
        case 'c':               /* --connect */
 
825
        case 'c':
878
826
          connect_to = arg;
879
827
          break;
880
 
        case 'i':               /* --interface */
 
828
        case 'i':
881
829
          interface = arg;
882
830
          break;
883
 
        case 's':               /* --seckey */
884
 
          seckey = arg;
885
 
          break;
886
 
        case 'p':               /* --pubkey */
887
 
          pubkey = arg;
888
 
          break;
889
 
        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:
890
841
          errno = 0;
891
842
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
892
843
          if (errno){
894
845
            exit(EXIT_FAILURE);
895
846
          }
896
847
          break;
897
 
        case 130:               /* --priority */
 
848
        case 130:
898
849
          mc.priority = arg;
899
850
          break;
900
851
        case ARGP_KEY_ARG:
906
857
        }
907
858
        return 0;
908
859
      }
909
 
      
 
860
 
910
861
      struct argp argp = { .options = options, .parser = parse_opt,
911
862
                           .args_doc = "",
912
863
                           .doc = "Mandos client -- Get and decrypt"
913
 
                           " passwords from a Mandos server" };
 
864
                           " passwords from mandos server" };
914
865
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
915
866
      if (ret == ARGP_ERR_UNKNOWN){
916
867
        fprintf(stderr, "Unknown error while parsing arguments\n");
918
869
        goto end;
919
870
      }
920
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
    }
921
895
    
922
896
    /* If the interface is down, bring it up */
923
897
    {
927
901
        exitcode = EXIT_FAILURE;
928
902
        goto end;
929
903
      }
930
 
      strcpy(network.ifr_name, interface);
 
904
      strcpy(network.ifr_name, interface); /* Spurious warning */
931
905
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
932
906
      if(ret == -1){
933
907
        perror("ioctl SIOCGIFFLAGS");
943
917
          goto end;
944
918
        }
945
919
      }
946
 
      ret = TEMP_FAILURE_RETRY(close(sd));
947
 
      if(ret == -1){
948
 
        perror("close");
949
 
      }
 
920
      close(sd);
950
921
    }
951
922
    
952
923
    uid = getuid();
962
933
      perror("setgid");
963
934
    }
964
935
    
965
 
    ret = init_gnutls_global(&mc, pubkey, seckey);
966
 
    if (ret == -1){
967
 
      fprintf(stderr, "init_gnutls_global failed\n");
968
 
      exitcode = EXIT_FAILURE;
969
 
      goto end;
970
 
    } else {
971
 
      gnutls_initalized = true;
972
 
    }
973
 
    
974
 
    if(mkdtemp(tempdir) == NULL){
975
 
      perror("mkdtemp");
976
 
      tempdir[0] = '\0';
977
 
      goto end;
978
 
    }
979
 
    
980
 
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
981
 
      fprintf(stderr, "gpgme_initalized failed\n");
982
 
      exitcode = EXIT_FAILURE;
983
 
      goto end;
984
 
    } else {
985
 
      gpgme_initalized = true;
986
 
    }
987
 
    
988
936
    if_index = (AvahiIfIndex) if_nametoindex(interface);
989
937
    if(if_index == 0){
990
938
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1033
981
        exitcode = EXIT_FAILURE;
1034
982
        goto end;
1035
983
    }
1036
 
    
 
984
 
1037
985
    {
1038
986
      AvahiServerConfig config;
1039
987
      /* Do not publish any local Zeroconf records */
1042
990
      config.publish_addresses = 0;
1043
991
      config.publish_workstation = 0;
1044
992
      config.publish_domain = 0;
1045
 
      
 
993
 
1046
994
      /* Allocate a new server */
1047
995
      mc.server = avahi_server_new(avahi_simple_poll_get
1048
996
                                   (mc.simple_poll), &config, NULL,
1049
997
                                   NULL, &error);
1050
 
      
 
998
    
1051
999
      /* Free the Avahi configuration data */
1052
1000
      avahi_server_config_free(&config);
1053
1001
    }
1073
1021
    }
1074
1022
    
1075
1023
    /* Run the main loop */
1076
 
    
 
1024
 
1077
1025
    if (debug){
1078
1026
      fprintf(stderr, "Starting Avahi loop search\n");
1079
1027
    }
1081
1029
    avahi_simple_poll_loop(mc.simple_poll);
1082
1030
    
1083
1031
 end:
1084
 
    
 
1032
 
1085
1033
    if (debug){
1086
1034
      fprintf(stderr, "%s exiting\n", argv[0]);
1087
1035
    }
1092
1040
    
1093
1041
    if (mc.server != NULL)
1094
1042
        avahi_server_free(mc.server);
1095
 
    
 
1043
 
1096
1044
    if (mc.simple_poll != NULL)
1097
1045
        avahi_simple_poll_free(mc.simple_poll);
1098
 
    
 
1046
    free(pubkeyfile);
 
1047
    free(seckeyfile);
 
1048
 
1099
1049
    if (gnutls_initalized){
1100
1050
      gnutls_certificate_free_credentials(mc.cred);
1101
1051
      gnutls_global_deinit ();
1102
 
      gnutls_dh_params_deinit(mc.dh_params);
1103
 
    }
1104
 
    
1105
 
    if(gpgme_initalized){
1106
 
      gpgme_release(mc.ctx);
1107
 
    }
1108
 
    
1109
 
    /* Removes the temp directory used by GPGME */
1110
 
    if(tempdir[0] != '\0'){
1111
 
      DIR *d;
1112
 
      struct dirent *direntry;
1113
 
      d = opendir(tempdir);
1114
 
      if(d == NULL){
1115
 
        perror("opendir");
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){
1142
 
        perror("rmdir");
1143
 
      }
1144
 
    }
1145
 
          
 
1052
    }
 
1053
    
1146
1054
    return exitcode;
1147
1055
}