/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-24 06:17:02 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080824061702-zxrru4r1vxmx4tuq
* Makefile (PREFIX, CONFDIR, MANDIR): Use $(DESTDIR).
  (install-server, install-client): Use "install --directory" instead
                                    of mkdir.

* mandos-keygen: New options --subtype and --sublength.
  (trap): Added semicolons and backslashes.
  (gpg): Added "--enable-dsa2" to all invocations.

* mandos-keygen.xml: Changed single quotes to double quotes for
                     consistency.
  (/refentry/refentryinfo/copyright) Split copyright holders.
  (SYNOPSIS): Added "--subtype", "--sublength", "-s", and "-L".
  (OPTIONS): Document the subtype and sublength options.
  (SECURITY): Also note the "--subtype" and "--sublength" options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008 Teddy Hogeborn & Björn Påhlsson
 
12
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
13
13
 * 
14
14
 * This program is free software: you can redistribute it and/or
15
15
 * modify it under the terms of the GNU General Public License as
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
 
#define PATHDIR "/conf/conf.d/mandos"
105
 
#define SECKEY "seckey.txt"
106
 
#define PUBKEY "pubkey.txt"
107
 
 
108
101
bool debug = false;
 
102
static const char *keydir = "/conf/conf.d/mandos";
109
103
static const char mandos_protocol_version[] = "1";
110
 
const char *argp_program_version = "mandos-client " VERSION;
 
104
const char *argp_program_version = "password-request 1.0";
111
105
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
112
106
 
113
107
/* Used for passing in values through the Avahi callback functions */
118
112
  unsigned int dh_bits;
119
113
  gnutls_dh_params_t dh_params;
120
114
  const char *priority;
121
 
  gpgme_ctx_t ctx;
122
115
} mandos_context;
123
116
 
124
117
/*
139
132
}
140
133
 
141
134
/* 
142
 
 * Initialize GPGME.
 
135
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
 
136
 * Returns -1 on error
143
137
 */
144
 
static bool init_gpgme(mandos_context *mc, const char *seckey,
145
 
                       const char *pubkey, const char *tempdir){
146
 
  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;
147
144
  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
 
  
186
150
  if (debug){
187
 
    fprintf(stderr, "Initialize gpgme\n");
 
151
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
188
152
  }
189
153
  
190
154
  /* Init GPGME */
193
157
  if (rc != GPG_ERR_NO_ERROR){
194
158
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
195
159
            gpgme_strsource(rc), gpgme_strerror(rc));
196
 
    return false;
 
160
    return -1;
197
161
  }
198
162
  
199
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
163
  /* Set GPGME home directory for the OpenPGP engine only */
200
164
  rc = gpgme_get_engine_info (&engine_info);
201
165
  if (rc != GPG_ERR_NO_ERROR){
202
166
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
203
167
            gpgme_strsource(rc), gpgme_strerror(rc));
204
 
    return false;
 
168
    return -1;
205
169
  }
206
170
  while(engine_info != NULL){
207
171
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
208
172
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
209
 
                            engine_info->file_name, tempdir);
 
173
                            engine_info->file_name, homedir);
210
174
      break;
211
175
    }
212
176
    engine_info = engine_info->next;
213
177
  }
214
178
  if(engine_info == NULL){
215
 
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
216
 
    return false;
217
 
  }
218
 
  
219
 
  /* Create new GPGME "context" */
220
 
  rc = gpgme_new(&(mc->ctx));
221
 
  if (rc != GPG_ERR_NO_ERROR){
222
 
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
223
 
            gpgme_strsource(rc), gpgme_strerror(rc));
224
 
    return false;
225
 
  }
226
 
  
227
 
  if (not import_key(pubkey) or not import_key(seckey)){
228
 
    return false;
229
 
  }
230
 
  
231
 
  return true; 
232
 
}
233
 
 
234
 
/* 
235
 
 * Decrypt OpenPGP data.
236
 
 * Returns -1 on error
237
 
 */
238
 
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
239
 
                                   const char *cryptotext,
240
 
                                   size_t crypto_size,
241
 
                                   char **plaintext){
242
 
  gpgme_data_t dh_crypto, dh_plain;
243
 
  gpgme_error_t rc;
244
 
  ssize_t ret;
245
 
  size_t plaintext_capacity = 0;
246
 
  ssize_t plaintext_length = 0;
247
 
  
248
 
  if (debug){
249
 
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
 
179
    fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
 
180
    return -1;
250
181
  }
251
182
  
252
183
  /* Create new GPGME data buffer from memory cryptotext */
267
198
    return -1;
268
199
  }
269
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
  
270
210
  /* Decrypt data from the cryptotext data buffer to the plaintext
271
211
     data buffer */
272
 
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
 
212
  rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
273
213
  if (rc != GPG_ERR_NO_ERROR){
274
214
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
275
215
            gpgme_strsource(rc), gpgme_strerror(rc));
276
216
    plaintext_length = -1;
277
 
    if (debug){
278
 
      gpgme_decrypt_result_t result;
279
 
      result = gpgme_op_decrypt_result(mc->ctx);
280
 
      if (result == NULL){
281
 
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
282
 
      } else {
283
 
        fprintf(stderr, "Unsupported algorithm: %s\n",
284
 
                result->unsupported_algorithm);
285
 
        fprintf(stderr, "Wrong key usage: %u\n",
286
 
                result->wrong_key_usage);
287
 
        if(result->file_name != NULL){
288
 
          fprintf(stderr, "File name: %s\n", result->file_name);
289
 
        }
290
 
        gpgme_recipient_t recipient;
291
 
        recipient = result->recipients;
292
 
        if(recipient){
293
 
          while(recipient != NULL){
294
 
            fprintf(stderr, "Public key algorithm: %s\n",
295
 
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
296
 
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
297
 
            fprintf(stderr, "Secret key available: %s\n",
298
 
                    recipient->status == GPG_ERR_NO_SECKEY
299
 
                    ? "No" : "Yes");
300
 
            recipient = recipient->next;
301
 
          }
302
 
        }
303
 
      }
304
 
    }
305
217
    goto decrypt_end;
306
218
  }
307
219
  
309
221
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
310
222
  }
311
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
  
312
253
  /* Seek back to the beginning of the GPGME plaintext data buffer */
313
254
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
314
 
    perror("gpgme_data_seek");
 
255
    perror("pgpme_data_seek");
315
256
    plaintext_length = -1;
316
257
    goto decrypt_end;
317
258
  }
341
282
    }
342
283
    plaintext_length += ret;
343
284
  }
344
 
  
 
285
 
345
286
  if(debug){
346
287
    fprintf(stderr, "Decrypted password is: ");
347
288
    for(ssize_t i = 0; i < plaintext_length; i++){
408
349
  }
409
350
  
410
351
  if(debug){
411
 
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
412
 
            " 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,
413
354
            seckeyfilename);
414
355
  }
415
356
  
420
361
    fprintf(stderr,
421
362
            "Error[%d] while reading the OpenPGP key pair ('%s',"
422
363
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
423
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
 
364
    fprintf(stdout, "The GnuTLS error is: %s\n",
424
365
            safer_gnutls_strerror(ret));
425
366
    goto globalfail;
426
367
  }
440
381
  }
441
382
  
442
383
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
443
 
  
 
384
 
444
385
  return 0;
445
 
  
 
386
 
446
387
 globalfail:
447
 
  
 
388
 
448
389
  gnutls_certificate_free_credentials(mc->cred);
449
390
  gnutls_global_deinit();
450
 
  gnutls_dh_params_deinit(mc->dh_params);
451
391
  return -1;
 
392
 
452
393
}
453
394
 
454
395
static int init_gnutls_session(mandos_context *mc,
526
467
    perror("socket");
527
468
    return -1;
528
469
  }
529
 
  
 
470
 
530
471
  if(debug){
531
472
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
532
473
      perror("if_indextoname");
571
512
    perror("connect");
572
513
    return -1;
573
514
  }
574
 
  
 
515
 
575
516
  const char *out = mandos_protocol_version;
576
517
  written = 0;
577
518
  while (true){
595
536
      }
596
537
    }
597
538
  }
598
 
  
 
539
 
599
540
  if(debug){
600
541
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
601
542
  }
602
543
  
603
544
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
604
 
  
 
545
 
605
546
  do{
606
547
    ret = gnutls_handshake (session);
607
548
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
621
562
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
622
563
            ip);
623
564
  }
624
 
  
 
565
 
625
566
  while(true){
626
567
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
627
568
                                   buffer_capacity);
671
612
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
672
613
  
673
614
  if (buffer_length > 0){
674
 
    decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
 
615
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
675
616
                                               buffer_length,
676
 
                                               &decrypted_buffer);
 
617
                                               &decrypted_buffer,
 
618
                                               keydir);
677
619
    if (decrypted_buffer_size >= 0){
678
620
      written = 0;
679
621
      while(written < (size_t) decrypted_buffer_size){
694
636
    } else {
695
637
      retval = -1;
696
638
    }
697
 
  } else {
698
 
    retval = -1;
699
639
  }
700
640
  
701
641
  /* Shutdown procedure */
702
642
  
703
643
 mandos_end:
704
644
  free(buffer);
705
 
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
706
 
  if(ret == -1){
707
 
    perror("close");
708
 
  }
 
645
  close(tcp_sd);
709
646
  gnutls_deinit (session);
710
647
  return retval;
711
648
}
807
744
  }
808
745
}
809
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
 
810
759
int main(int argc, char *argv[]){
811
760
    AvahiSServiceBrowser *sb = NULL;
812
761
    int error;
818
767
    uid_t uid;
819
768
    gid_t gid;
820
769
    char *connect_to = NULL;
821
 
    char tempdir[] = "/tmp/mandosXXXXXX";
822
770
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
823
 
    const char *seckey = PATHDIR "/" SECKEY;
824
 
    const char *pubkey = PATHDIR "/" PUBKEY;
825
 
    
 
771
    char *pubkeyfilename = NULL;
 
772
    char *seckeyfilename = NULL;
 
773
    const char *pubkeyname = "pubkey.txt";
 
774
    const char *seckeyname = "seckey.txt";
826
775
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
827
 
                          .dh_bits = 1024, .priority = "SECURE256"
828
 
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
776
                          .dh_bits = 1024, .priority = "SECURE256"};
829
777
    bool gnutls_initalized = false;
830
 
    bool gpgme_initalized = false;
831
778
    
832
779
    {
833
780
      struct argp_option options[] = {
834
781
        { .name = "debug", .key = 128,
835
782
          .doc = "Debug mode", .group = 3 },
836
783
        { .name = "connect", .key = 'c',
837
 
          .arg = "ADDRESS:PORT",
838
 
          .doc = "Connect directly to a specific Mandos server",
 
784
          .arg = "IP",
 
785
          .doc = "Connect directly to a sepcified mandos server",
839
786
          .group = 1 },
840
787
        { .name = "interface", .key = 'i',
841
 
          .arg = "NAME",
842
 
          .doc = "Interface that will be used to search for Mandos"
843
 
          " 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",
844
794
          .group = 1 },
845
795
        { .name = "seckey", .key = 's',
846
 
          .arg = "FILE",
847
 
          .doc = "OpenPGP secret key file base name",
 
796
          .arg = "SECKEY",
 
797
          .doc = "Secret openpgp key for gnutls authentication",
848
798
          .group = 1 },
849
799
        { .name = "pubkey", .key = 'p',
850
 
          .arg = "FILE",
851
 
          .doc = "OpenPGP public key file base name",
 
800
          .arg = "PUBKEY",
 
801
          .doc = "Public openpgp key for gnutls authentication",
852
802
          .group = 2 },
853
803
        { .name = "dh-bits", .key = 129,
854
804
          .arg = "BITS",
855
 
          .doc = "Bit length of the prime number used in the"
856
 
          " Diffie-Hellman key exchange",
 
805
          .doc = "dh-bits to use in gnutls communication",
857
806
          .group = 2 },
858
807
        { .name = "priority", .key = 130,
859
 
          .arg = "STRING",
860
 
          .doc = "GnuTLS priority string for the TLS handshake",
861
 
          .group = 1 },
 
808
          .arg = "PRIORITY",
 
809
          .doc = "GNUTLS priority", .group = 1 },
862
810
        { .name = NULL }
863
811
      };
 
812
 
864
813
      
865
814
      error_t parse_opt (int key, char *arg,
866
815
                         struct argp_state *state) {
867
816
        /* Get the INPUT argument from `argp_parse', which we know is
868
817
           a pointer to our plugin list pointer. */
869
818
        switch (key) {
870
 
        case 128:               /* --debug */
 
819
        case 128:
871
820
          debug = true;
872
821
          break;
873
 
        case 'c':               /* --connect */
 
822
        case 'c':
874
823
          connect_to = arg;
875
824
          break;
876
 
        case 'i':               /* --interface */
 
825
        case 'i':
877
826
          interface = arg;
878
827
          break;
879
 
        case 's':               /* --seckey */
880
 
          seckey = arg;
881
 
          break;
882
 
        case 'p':               /* --pubkey */
883
 
          pubkey = arg;
884
 
          break;
885
 
        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:
886
838
          errno = 0;
887
839
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
888
840
          if (errno){
890
842
            exit(EXIT_FAILURE);
891
843
          }
892
844
          break;
893
 
        case 130:               /* --priority */
 
845
        case 130:
894
846
          mc.priority = arg;
895
847
          break;
896
848
        case ARGP_KEY_ARG:
902
854
        }
903
855
        return 0;
904
856
      }
905
 
      
 
857
 
906
858
      struct argp argp = { .options = options, .parser = parse_opt,
907
859
                           .args_doc = "",
908
860
                           .doc = "Mandos client -- Get and decrypt"
909
 
                           " passwords from a Mandos server" };
 
861
                           " passwords from mandos server" };
910
862
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
911
863
      if (ret == ARGP_ERR_UNKNOWN){
912
864
        fprintf(stderr, "Unknown error while parsing arguments\n");
914
866
        goto end;
915
867
      }
916
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
    }
917
892
    
918
893
    /* If the interface is down, bring it up */
919
894
    {
939
914
          goto end;
940
915
        }
941
916
      }
942
 
      ret = TEMP_FAILURE_RETRY(close(sd));
943
 
      if(ret == -1){
944
 
        perror("close");
945
 
      }
 
917
      close(sd);
946
918
    }
947
919
    
948
920
    uid = getuid();
958
930
      perror("setgid");
959
931
    }
960
932
    
961
 
    ret = init_gnutls_global(&mc, pubkey, seckey);
962
 
    if (ret == -1){
963
 
      fprintf(stderr, "init_gnutls_global failed\n");
964
 
      exitcode = EXIT_FAILURE;
965
 
      goto end;
966
 
    } else {
967
 
      gnutls_initalized = true;
968
 
    }
969
 
    
970
 
    if(mkdtemp(tempdir) == NULL){
971
 
      perror("mkdtemp");
972
 
      tempdir[0] = '\0';
973
 
      goto end;
974
 
    }
975
 
    
976
 
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
977
 
      fprintf(stderr, "gpgme_initalized failed\n");
978
 
      exitcode = EXIT_FAILURE;
979
 
      goto end;
980
 
    } else {
981
 
      gpgme_initalized = true;
982
 
    }
983
 
    
984
933
    if_index = (AvahiIfIndex) if_nametoindex(interface);
985
934
    if(if_index == 0){
986
935
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1029
978
        exitcode = EXIT_FAILURE;
1030
979
        goto end;
1031
980
    }
1032
 
    
 
981
 
1033
982
    {
1034
983
      AvahiServerConfig config;
1035
984
      /* Do not publish any local Zeroconf records */
1038
987
      config.publish_addresses = 0;
1039
988
      config.publish_workstation = 0;
1040
989
      config.publish_domain = 0;
1041
 
      
 
990
 
1042
991
      /* Allocate a new server */
1043
992
      mc.server = avahi_server_new(avahi_simple_poll_get
1044
993
                                   (mc.simple_poll), &config, NULL,
1045
994
                                   NULL, &error);
1046
 
      
 
995
    
1047
996
      /* Free the Avahi configuration data */
1048
997
      avahi_server_config_free(&config);
1049
998
    }
1069
1018
    }
1070
1019
    
1071
1020
    /* Run the main loop */
1072
 
    
 
1021
 
1073
1022
    if (debug){
1074
1023
      fprintf(stderr, "Starting Avahi loop search\n");
1075
1024
    }
1077
1026
    avahi_simple_poll_loop(mc.simple_poll);
1078
1027
    
1079
1028
 end:
1080
 
    
 
1029
 
1081
1030
    if (debug){
1082
1031
      fprintf(stderr, "%s exiting\n", argv[0]);
1083
1032
    }
1088
1037
    
1089
1038
    if (mc.server != NULL)
1090
1039
        avahi_server_free(mc.server);
1091
 
    
 
1040
 
1092
1041
    if (mc.simple_poll != NULL)
1093
1042
        avahi_simple_poll_free(mc.simple_poll);
1094
 
    
 
1043
    free(pubkeyfilename);
 
1044
    free(seckeyfilename);
 
1045
 
1095
1046
    if (gnutls_initalized){
1096
1047
      gnutls_certificate_free_credentials(mc.cred);
1097
1048
      gnutls_global_deinit ();
1098
 
      gnutls_dh_params_deinit(mc.dh_params);
1099
 
    }
1100
 
    
1101
 
    if(gpgme_initalized){
1102
 
      gpgme_release(mc.ctx);
1103
 
    }
1104
 
    
1105
 
    /* Removes the temp directory used by GPGME */
1106
 
    if(tempdir[0] != '\0'){
1107
 
      DIR *d;
1108
 
      struct dirent *direntry;
1109
 
      d = opendir(tempdir);
1110
 
      if(d == NULL){
1111
 
        perror("opendir");
1112
 
      } else {
1113
 
        while(true){
1114
 
          direntry = readdir(d);
1115
 
          if(direntry == NULL){
1116
 
            break;
1117
 
          }
1118
 
          if (direntry->d_type == DT_REG){
1119
 
            char *fullname = NULL;
1120
 
            ret = asprintf(&fullname, "%s/%s", tempdir,
1121
 
                           direntry->d_name);
1122
 
            if(ret < 0){
1123
 
              perror("asprintf");
1124
 
              continue;
1125
 
            }
1126
 
            ret = unlink(fullname);
1127
 
            if(ret == -1){
1128
 
              fprintf(stderr, "unlink(\"%s\"): %s",
1129
 
                      fullname, strerror(errno));
1130
 
            }
1131
 
            free(fullname);
1132
 
          }
1133
 
        }
1134
 
        closedir(d);
1135
 
      }
1136
 
      ret = rmdir(tempdir);
1137
 
      if(ret == -1){
1138
 
        perror("rmdir");
1139
 
      }
1140
 
    }
1141
 
          
 
1049
    }
 
1050
    
1142
1051
    return exitcode;
1143
1052
}