/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-09-30 20:12:00 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080930201200-3zkcd1op8pg2gw1s
Tags: version-1-0, version-1.0
* debian/changelog: Initial release closes ITP bug.

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