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

  • Committer: Teddy Hogeborn
  • Date: 2008-10-28 18:00:20 UTC
  • Revision ID: teddy@fukt.bsnet.se-20081028180020-qsiaedlurboi3nm4
* INSTALL: Fixed typo.

* Makefile: Split long lines.
  (install-html): Depend on "html", not "$(htmldocs)".

* README (Copyright): Modified legalese.

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