/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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-18 23:55:28 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080818235528-dn628nlbrtzl7z4f
* Makefile: Bug fix: fixed creation of man pages for section 5 pages.

* mandos (main): Changed from requiring "[server]" in mandos.conf(5)
                 to requiring "[DEFAULT]".

* mandos.conf ([server]): Renamed to "[DEFAULT]".

* mandos.conf.xml: Removed <?xml-stylesheet>.  New entity "&OVERVIEW;"
                   referring to "overview.xml".
  (DESCRIPTION): Updated to specify the syntax more precisely.  Use
                 <varname> around the option names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
101
98
 
102
99
#define BUFFER_SIZE 256
103
100
 
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
101
bool debug = false;
 
102
static const char *keydir = "/conf/conf.d/mandos";
113
103
static const char mandos_protocol_version[] = "1";
114
 
const char *argp_program_version = "mandos-client 1.0";
 
104
const char *argp_program_version = "password-request 1.0";
115
105
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
116
106
 
117
107
/* Used for passing in values through the Avahi callback functions */
122
112
  unsigned int dh_bits;
123
113
  gnutls_dh_params_t dh_params;
124
114
  const char *priority;
125
 
  gpgme_ctx_t ctx;
126
115
} mandos_context;
127
116
 
128
117
/*
143
132
}
144
133
 
145
134
/* 
146
 
 * Initialize GPGME.
 
135
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
 
136
 * Returns -1 on error
147
137
 */
148
 
static bool init_gpgme(mandos_context *mc, const char *seckey,
149
 
                       const char *pubkey, const char *tempdir){
150
 
  int ret;
 
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;
151
144
  gpgme_error_t rc;
 
145
  ssize_t ret;
 
146
  size_t plaintext_capacity = 0;
 
147
  ssize_t plaintext_length = 0;
152
148
  gpgme_engine_info_t engine_info;
153
149
  
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
150
  if (debug){
191
 
    fprintf(stderr, "Initialize gpgme\n");
 
151
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
192
152
  }
193
153
  
194
154
  /* Init GPGME */
197
157
  if (rc != GPG_ERR_NO_ERROR){
198
158
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
199
159
            gpgme_strsource(rc), gpgme_strerror(rc));
200
 
    return false;
 
160
    return -1;
201
161
  }
202
162
  
203
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
163
  /* Set GPGME home directory for the OpenPGP engine only */
204
164
  rc = gpgme_get_engine_info (&engine_info);
205
165
  if (rc != GPG_ERR_NO_ERROR){
206
166
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
207
167
            gpgme_strsource(rc), gpgme_strerror(rc));
208
 
    return false;
 
168
    return -1;
209
169
  }
210
170
  while(engine_info != NULL){
211
171
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
212
172
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
213
 
                            engine_info->file_name, tempdir);
 
173
                            engine_info->file_name, homedir);
214
174
      break;
215
175
    }
216
176
    engine_info = engine_info->next;
217
177
  }
218
178
  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");
 
179
    fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
 
180
    return -1;
254
181
  }
255
182
  
256
183
  /* Create new GPGME data buffer from memory cryptotext */
271
198
    return -1;
272
199
  }
273
200
  
 
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
  
274
210
  /* Decrypt data from the cryptotext data buffer to the plaintext
275
211
     data buffer */
276
 
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
 
212
  rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
277
213
  if (rc != GPG_ERR_NO_ERROR){
278
214
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
279
215
            gpgme_strsource(rc), gpgme_strerror(rc));
280
216
    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
217
    goto decrypt_end;
310
218
  }
311
219
  
313
221
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
314
222
  }
315
223
  
 
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
  
316
253
  /* Seek back to the beginning of the GPGME plaintext data buffer */
317
254
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
318
255
    perror("pgpme_data_seek");
345
282
    }
346
283
    plaintext_length += ret;
347
284
  }
348
 
  
 
285
 
349
286
  if(debug){
350
287
    fprintf(stderr, "Decrypted password is: ");
351
288
    for(ssize_t i = 0; i < plaintext_length; i++){
412
349
  }
413
350
  
414
351
  if(debug){
415
 
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
416
 
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
 
352
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
 
353
            " and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
417
354
            seckeyfilename);
418
355
  }
419
356
  
424
361
    fprintf(stderr,
425
362
            "Error[%d] while reading the OpenPGP key pair ('%s',"
426
363
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
427
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
 
364
    fprintf(stdout, "The GnuTLS error is: %s\n",
428
365
            safer_gnutls_strerror(ret));
429
366
    goto globalfail;
430
367
  }
444
381
  }
445
382
  
446
383
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
447
 
  
 
384
 
448
385
  return 0;
449
 
  
 
386
 
450
387
 globalfail:
451
 
  
 
388
 
452
389
  gnutls_certificate_free_credentials(mc->cred);
453
390
  gnutls_global_deinit();
454
391
  return -1;
 
392
 
455
393
}
456
394
 
457
395
static int init_gnutls_session(mandos_context *mc,
529
467
    perror("socket");
530
468
    return -1;
531
469
  }
532
 
  
 
470
 
533
471
  if(debug){
534
472
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
535
473
      perror("if_indextoname");
574
512
    perror("connect");
575
513
    return -1;
576
514
  }
577
 
  
 
515
 
578
516
  const char *out = mandos_protocol_version;
579
517
  written = 0;
580
518
  while (true){
598
536
      }
599
537
    }
600
538
  }
601
 
  
 
539
 
602
540
  if(debug){
603
541
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
604
542
  }
605
543
  
606
544
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
607
 
  
 
545
 
608
546
  do{
609
547
    ret = gnutls_handshake (session);
610
548
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
624
562
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
625
563
            ip);
626
564
  }
627
 
  
 
565
 
628
566
  while(true){
629
567
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
630
568
                                   buffer_capacity);
674
612
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
675
613
  
676
614
  if (buffer_length > 0){
677
 
    decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
 
615
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
678
616
                                               buffer_length,
679
 
                                               &decrypted_buffer);
 
617
                                               &decrypted_buffer,
 
618
                                               keydir);
680
619
    if (decrypted_buffer_size >= 0){
681
620
      written = 0;
682
621
      while(written < (size_t) decrypted_buffer_size){
697
636
    } else {
698
637
      retval = -1;
699
638
    }
700
 
  } else {
701
 
    retval = -1;
702
639
  }
703
640
  
704
641
  /* Shutdown procedure */
705
642
  
706
643
 mandos_end:
707
644
  free(buffer);
708
 
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
709
 
  if(ret == -1){
710
 
    perror("close");
711
 
  }
 
645
  close(tcp_sd);
712
646
  gnutls_deinit (session);
713
647
  return retval;
714
648
}
810
744
  }
811
745
}
812
746
 
 
747
/* Combines file name and path and returns the malloced new
 
748
   string. some sane checks could/should be added */
 
749
static char *combinepath(const char *first, const char *second){
 
750
  char *tmp;
 
751
  int ret = asprintf(&tmp, "%s/%s", first, second);
 
752
  if(ret < 0){
 
753
    return NULL;
 
754
  }
 
755
  return tmp;
 
756
}
 
757
 
 
758
 
813
759
int main(int argc, char *argv[]){
814
760
    AvahiSServiceBrowser *sb = NULL;
815
761
    int error;
821
767
    uid_t uid;
822
768
    gid_t gid;
823
769
    char *connect_to = NULL;
824
 
    char tempdir[] = "/tmp/mandosXXXXXX";
825
770
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
826
 
    const char *seckey = PATHDIR "/" SECKEY;
827
 
    const char *pubkey = PATHDIR "/" PUBKEY;
828
 
    
 
771
    char *pubkeyfilename = NULL;
 
772
    char *seckeyfilename = NULL;
 
773
    const char *pubkeyname = "pubkey.txt";
 
774
    const char *seckeyname = "seckey.txt";
829
775
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
830
 
                          .dh_bits = 1024, .priority = "SECURE256"
831
 
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
776
                          .dh_bits = 1024, .priority = "SECURE256"};
832
777
    bool gnutls_initalized = false;
833
 
    bool pgpme_initalized = false;
834
778
    
835
779
    {
836
780
      struct argp_option options[] = {
837
781
        { .name = "debug", .key = 128,
838
782
          .doc = "Debug mode", .group = 3 },
839
783
        { .name = "connect", .key = 'c',
840
 
          .arg = "ADDRESS:PORT",
841
 
          .doc = "Connect directly to a specific Mandos server",
 
784
          .arg = "IP",
 
785
          .doc = "Connect directly to a sepcified mandos server",
842
786
          .group = 1 },
843
787
        { .name = "interface", .key = 'i',
844
 
          .arg = "NAME",
845
 
          .doc = "Interface that will be used to search for Mandos"
846
 
          " servers",
 
788
          .arg = "INTERFACE",
 
789
          .doc = "Interface that Avahi will conntect through",
 
790
          .group = 1 },
 
791
        { .name = "keydir", .key = 'd',
 
792
          .arg = "KEYDIR",
 
793
          .doc = "Directory where the openpgp keyring is",
847
794
          .group = 1 },
848
795
        { .name = "seckey", .key = 's',
849
 
          .arg = "FILE",
850
 
          .doc = "OpenPGP secret key file base name",
 
796
          .arg = "SECKEY",
 
797
          .doc = "Secret openpgp key for gnutls authentication",
851
798
          .group = 1 },
852
799
        { .name = "pubkey", .key = 'p',
853
 
          .arg = "FILE",
854
 
          .doc = "OpenPGP public key file base name",
 
800
          .arg = "PUBKEY",
 
801
          .doc = "Public openpgp key for gnutls authentication",
855
802
          .group = 2 },
856
803
        { .name = "dh-bits", .key = 129,
857
804
          .arg = "BITS",
858
 
          .doc = "Bit length of the prime number used in the"
859
 
          " Diffie-Hellman key exchange",
 
805
          .doc = "dh-bits to use in gnutls communication",
860
806
          .group = 2 },
861
807
        { .name = "priority", .key = 130,
862
 
          .arg = "STRING",
863
 
          .doc = "GnuTLS priority string for the TLS handshake",
864
 
          .group = 1 },
 
808
          .arg = "PRIORITY",
 
809
          .doc = "GNUTLS priority", .group = 1 },
865
810
        { .name = NULL }
866
811
      };
 
812
 
867
813
      
868
814
      error_t parse_opt (int key, char *arg,
869
815
                         struct argp_state *state) {
870
816
        /* Get the INPUT argument from `argp_parse', which we know is
871
817
           a pointer to our plugin list pointer. */
872
818
        switch (key) {
873
 
        case 128:               /* --debug */
 
819
        case 128:
874
820
          debug = true;
875
821
          break;
876
 
        case 'c':               /* --connect */
 
822
        case 'c':
877
823
          connect_to = arg;
878
824
          break;
879
 
        case 'i':               /* --interface */
 
825
        case 'i':
880
826
          interface = arg;
881
827
          break;
882
 
        case 's':               /* --seckey */
883
 
          seckey = arg;
884
 
          break;
885
 
        case 'p':               /* --pubkey */
886
 
          pubkey = arg;
887
 
          break;
888
 
        case 129:               /* --dh-bits */
 
828
        case 'd':
 
829
          keydir = arg;
 
830
          break;
 
831
        case 's':
 
832
          seckeyname = arg;
 
833
          break;
 
834
        case 'p':
 
835
          pubkeyname = arg;
 
836
          break;
 
837
        case 129:
889
838
          errno = 0;
890
839
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
891
840
          if (errno){
893
842
            exit(EXIT_FAILURE);
894
843
          }
895
844
          break;
896
 
        case 130:               /* --priority */
 
845
        case 130:
897
846
          mc.priority = arg;
898
847
          break;
899
848
        case ARGP_KEY_ARG:
905
854
        }
906
855
        return 0;
907
856
      }
908
 
      
 
857
 
909
858
      struct argp argp = { .options = options, .parser = parse_opt,
910
859
                           .args_doc = "",
911
860
                           .doc = "Mandos client -- Get and decrypt"
912
 
                           " passwords from a Mandos server" };
 
861
                           " passwords from mandos server" };
913
862
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
914
863
      if (ret == ARGP_ERR_UNKNOWN){
915
864
        fprintf(stderr, "Unknown error while parsing arguments\n");
917
866
        goto end;
918
867
      }
919
868
    }
 
869
      
 
870
    pubkeyfilename = combinepath(keydir, pubkeyname);
 
871
    if (pubkeyfilename == NULL){
 
872
      perror("combinepath");
 
873
      exitcode = EXIT_FAILURE;
 
874
      goto end;
 
875
    }
 
876
    
 
877
    seckeyfilename = combinepath(keydir, seckeyname);
 
878
    if (seckeyfilename == NULL){
 
879
      perror("combinepath");
 
880
      exitcode = EXIT_FAILURE;
 
881
      goto end;
 
882
    }
 
883
 
 
884
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
 
885
    if (ret == -1){
 
886
      fprintf(stderr, "init_gnutls_global failed\n");
 
887
      exitcode = EXIT_FAILURE;
 
888
      goto end;
 
889
    } else {
 
890
      gnutls_initalized = true;
 
891
    }
920
892
    
921
893
    /* If the interface is down, bring it up */
922
894
    {
942
914
          goto end;
943
915
        }
944
916
      }
945
 
      ret = TEMP_FAILURE_RETRY(close(sd));
946
 
      if(ret == -1){
947
 
        perror("close");
948
 
      }
 
917
      close(sd);
949
918
    }
950
919
    
951
920
    uid = getuid();
961
930
      perror("setgid");
962
931
    }
963
932
    
964
 
    ret = init_gnutls_global(&mc, pubkey, seckey);
965
 
    if (ret == -1){
966
 
      fprintf(stderr, "init_gnutls_global failed\n");
967
 
      exitcode = EXIT_FAILURE;
968
 
      goto end;
969
 
    } else {
970
 
      gnutls_initalized = true;
971
 
    }
972
 
    
973
 
    if(mkdtemp(tempdir) == NULL){
974
 
      perror("mkdtemp");
975
 
      tempdir[0] = '\0';
976
 
      goto end;
977
 
    }
978
 
    
979
 
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
980
 
      fprintf(stderr, "pgpme_initalized failed\n");
981
 
      exitcode = EXIT_FAILURE;
982
 
      goto end;
983
 
    } else {
984
 
      pgpme_initalized = true;
985
 
    }
986
 
    
987
933
    if_index = (AvahiIfIndex) if_nametoindex(interface);
988
934
    if(if_index == 0){
989
935
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1032
978
        exitcode = EXIT_FAILURE;
1033
979
        goto end;
1034
980
    }
1035
 
    
 
981
 
1036
982
    {
1037
983
      AvahiServerConfig config;
1038
984
      /* Do not publish any local Zeroconf records */
1041
987
      config.publish_addresses = 0;
1042
988
      config.publish_workstation = 0;
1043
989
      config.publish_domain = 0;
1044
 
      
 
990
 
1045
991
      /* Allocate a new server */
1046
992
      mc.server = avahi_server_new(avahi_simple_poll_get
1047
993
                                   (mc.simple_poll), &config, NULL,
1048
994
                                   NULL, &error);
1049
 
      
 
995
    
1050
996
      /* Free the Avahi configuration data */
1051
997
      avahi_server_config_free(&config);
1052
998
    }
1072
1018
    }
1073
1019
    
1074
1020
    /* Run the main loop */
1075
 
    
 
1021
 
1076
1022
    if (debug){
1077
1023
      fprintf(stderr, "Starting Avahi loop search\n");
1078
1024
    }
1080
1026
    avahi_simple_poll_loop(mc.simple_poll);
1081
1027
    
1082
1028
 end:
1083
 
    
 
1029
 
1084
1030
    if (debug){
1085
1031
      fprintf(stderr, "%s exiting\n", argv[0]);
1086
1032
    }
1091
1037
    
1092
1038
    if (mc.server != NULL)
1093
1039
        avahi_server_free(mc.server);
1094
 
    
 
1040
 
1095
1041
    if (mc.simple_poll != NULL)
1096
1042
        avahi_simple_poll_free(mc.simple_poll);
1097
 
    
 
1043
    free(pubkeyfilename);
 
1044
    free(seckeyfilename);
 
1045
 
1098
1046
    if (gnutls_initalized){
1099
1047
      gnutls_certificate_free_credentials(mc.cred);
1100
1048
      gnutls_global_deinit ();
1101
1049
    }
1102
1050
    
1103
 
    if(pgpme_initalized){
1104
 
      gpgme_release(mc.ctx);
1105
 
    }
1106
 
    
1107
 
    /* Removes the temp directory used by GPGME */
1108
 
    if(tempdir[0] != '\0'){
1109
 
      DIR *d;
1110
 
      struct dirent *direntry;
1111
 
      d = opendir(tempdir);
1112
 
      if(d == NULL){
1113
 
        perror("opendir");
1114
 
      } else {
1115
 
        while(true){
1116
 
          direntry = readdir(d);
1117
 
          if(direntry == NULL){
1118
 
            break;
1119
 
          }
1120
 
          if (direntry->d_type == DT_REG){
1121
 
            char *fullname = NULL;
1122
 
            ret = asprintf(&fullname, "%s/%s", tempdir,
1123
 
                           direntry->d_name);
1124
 
            if(ret < 0){
1125
 
              perror("asprintf");
1126
 
              continue;
1127
 
            }
1128
 
            ret = unlink(fullname);
1129
 
            if(ret == -1){
1130
 
              fprintf(stderr, "unlink(\"%s\"): %s",
1131
 
                      fullname, strerror(errno));
1132
 
            }
1133
 
            free(fullname);
1134
 
          }
1135
 
        }
1136
 
      }
1137
 
      ret = rmdir(tempdir);
1138
 
      if(ret == -1){
1139
 
        perror("rmdir");
1140
 
      }
1141
 
    }
1142
 
          
1143
1051
    return exitcode;
1144
1052
}