/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/password-request.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-09 04:56:00 UTC
  • mfrom: (24.1.27 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080809045600-m7jx3tj4wwunaffa
Merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#define _LARGEFILE_SOURCE
33
33
#define _FILE_OFFSET_BITS 64
34
34
 
35
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
 
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
36
36
 
37
 
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
38
 
                                   stdout, ferror() */
 
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(), stdout, ferror() */
39
38
#include <stdint.h>             /* uint16_t, uint32_t */
40
39
#include <stddef.h>             /* NULL, size_t, ssize_t */
41
 
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
42
 
                                   srand() */
 
40
#include <stdlib.h>             /* free() */
43
41
#include <stdbool.h>            /* bool, true */
44
 
#include <string.h>             /* memset(), strcmp(), strlen(),
45
 
                                   strerror(), asprintf(), strcpy() */
46
 
#include <sys/ioctl.h>          /* ioctl */
 
42
#include <string.h>             /* memset(), strcmp(), strlen, strerror() */
 
43
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
 
44
                                   SIOCSIFFLAGS */
47
45
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
48
 
                                   sockaddr_in6, PF_INET6,
49
 
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
50
 
                                   uid_t, gid_t */
51
 
#include <inttypes.h>           /* PRIu16 */
 
46
                                   sockaddr_in6, PF_INET6, SOCK_STREAM, INET6_ADDRSTRLEN */
52
47
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
53
48
                                   struct in6_addr, inet_pton(),
54
49
                                   connect() */
55
 
#include <assert.h>             /* assert() */
56
 
#include <errno.h>              /* perror(), errno */
57
 
#include <time.h>               /* time() */
 
50
#include <assert.h>
 
51
#include <errno.h>              /* perror() */
 
52
#include <time.h>
58
53
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
59
54
                                   SIOCSIFFLAGS, if_indextoname(),
60
55
                                   if_nametoindex(), IF_NAMESIZE */
61
 
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
62
 
                                   getuid(), getgid(), setuid(),
63
 
                                   setgid() */
 
56
#include <unistd.h>             /* close(), SEEK_SET, off_t, write()*/
64
57
#include <netinet/in.h>
65
58
#include <arpa/inet.h>          /* inet_pton(), htons */
66
 
#include <iso646.h>             /* not, and */
67
 
#include <argp.h>               /* struct argp_option, error_t, struct
68
 
                                   argp_state, struct argp,
69
 
                                   argp_parse(), ARGP_KEY_ARG,
70
 
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
 
59
#include <iso646.h>             /* not */
 
60
#include <argp.h>               /* struct argp_option,
 
61
                                   struct argp_state, struct argp,
 
62
                                   argp_parse() */
71
63
 
72
64
/* Avahi */
73
 
/* All Avahi types, constants and functions
74
 
 Avahi*, avahi_*,
75
 
 AVAHI_* */
76
 
#include <avahi-core/core.h>
 
65
#include <avahi-core/core.h>    /* AvahiSimplePoll, AvahiServer,
 
66
                                   AvahiIfIndex */
77
67
#include <avahi-core/lookup.h>
78
 
#include <avahi-core/log.h>
 
68
#include <avahi-core/log.h>     /* AvahiLogLevel */
79
69
#include <avahi-common/simple-watch.h>
80
70
#include <avahi-common/malloc.h>
81
71
#include <avahi-common/error.h>
82
72
 
83
73
/* GnuTLS */
84
 
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and
85
 
                                   functions:
86
 
                                   gnutls_*
 
74
#include <gnutls/gnutls.h>      /* gnutls_certificate_credentials_t,
 
75
                                   gnutls_dh_params_t,
 
76
                                   gnutls_strerror(),
 
77
                                   gnutls_global_init(),
 
78
                                   gnutls_global_set_log_level(),
 
79
                                   gnutls_global_set_log_function(),
 
80
                                   gnutls_certificate_allocate_credentials(),
 
81
                                   gnutls_global_deinit(),
 
82
                                   gnutls_dh_params_init(),
 
83
                                   gnutls_dh_params_generate(),
 
84
                                   gnutls_certificate_set_dh_params(),
 
85
                                   gnutls_certificate_free_credentials(),
 
86
                                   gnutls_session_t, gnutls_init(),
 
87
                                   gnutls_priority_set_direct(),
 
88
                                   gnutls_deinit(),
 
89
                                   gnutls_credentials_set(),
 
90
                                   gnutls_certificate_server_set_request(),
 
91
                                   gnutls_dh_set_prime_bits(),
 
92
                                   gnutls_transport_set_ptr(),
 
93
                                   gnutls_transport_ptr_t,
 
94
                                   gnutls_handshake(),
 
95
                                   gnutls_record_recv()
 
96
                                   gnutls_perror(), gnutls_bye(),
87
97
                                   init_gnutls_session(),
88
 
                                   GNUTLS_* */
89
 
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
90
 
                                   GNUTLS_OPENPGP_FMT_BASE64 */
 
98
                                   GNUTLS_E_SUCCESS,
 
99
                                   GNUTLS_CRD_CERTIFICATE,
 
100
                                   GNUTLS_CERT_IGNORE,
 
101
                                   GNUTLS_E_INTERRUPTED,
 
102
                                   GNUTLS_E_AGAIN,
 
103
                                   GNUTLS_E_REHANDSHAKE,
 
104
                                   GNUTLS_SHUT_RDWR, */
 
105
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
 
106
                               GNUTLS_OPENPGP_FMT_BASE64 */
91
107
 
92
108
/* GPGME */
93
 
#include <gpgme.h>              /* All GPGME types, constants and
94
 
                                   functions:
95
 
                                   gpgme_*
 
109
#include <gpgme.h>              /* gpgme_data_t, gpgme_ctx_t,
 
110
                                   gpgme_error_t, gpgme_engine_info_t,
 
111
                                   gpgme_check_version(),
 
112
                                   gpgme_engine_check_version(),
 
113
                                   gpgme_strsource(),
 
114
                                   gpgme_strerror(),
 
115
                                   gpgme_get_engine_info(),
 
116
                                   gpgme_set_engine_info(),
 
117
                                   gpgme_data_new_from_mem(),
 
118
                                   gpgme_data_new(), gpgme_new(),
 
119
                                   gpgme_op_decrypt(),
 
120
                                   gpgme_decrypt_result_t,
 
121
                                   gpgme_op_decrypt_result(),
 
122
                                   gpgme_recipient_t,
 
123
                                   gpgme_pubkey_algo_name(),
 
124
                                   gpgme_data_seek(),
 
125
                                   gpgme_data_read(),
 
126
                                   gpgme_data_release()
96
127
                                   GPGME_PROTOCOL_OpenPGP,
97
 
                                   GPG_ERR_NO_* */
 
128
                                   GPG_ERR_NO_ERROR,
 
129
                                   GPG_ERR_NO_SECKEY, */
98
130
 
99
131
#define BUFFER_SIZE 256
100
132
 
101
133
bool debug = false;
102
134
static const char *keydir = "/conf/conf.d/mandos";
103
135
static const char mandos_protocol_version[] = "1";
104
 
const char *argp_program_version = "password-request 1.0";
 
136
const char *argp_program_version = "mandosclient 0.9";
105
137
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
106
138
 
107
139
/* Used for passing in values through the Avahi callback functions */
214
246
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
215
247
            gpgme_strsource(rc), gpgme_strerror(rc));
216
248
    plaintext_length = -1;
217
 
    if (debug){
218
 
      gpgme_decrypt_result_t result;
219
 
      result = gpgme_op_decrypt_result(ctx);
220
 
      if (result == NULL){
221
 
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
222
 
      } else {
223
 
        fprintf(stderr, "Unsupported algorithm: %s\n",
224
 
                result->unsupported_algorithm);
225
 
        fprintf(stderr, "Wrong key usage: %u\n",
226
 
                result->wrong_key_usage);
227
 
        if(result->file_name != NULL){
228
 
          fprintf(stderr, "File name: %s\n", result->file_name);
229
 
        }
230
 
        gpgme_recipient_t recipient;
231
 
        recipient = result->recipients;
232
 
        if(recipient){
233
 
          while(recipient != NULL){
234
 
            fprintf(stderr, "Public key algorithm: %s\n",
235
 
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
236
 
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
237
 
            fprintf(stderr, "Secret key available: %s\n",
238
 
                    recipient->status == GPG_ERR_NO_SECKEY
239
 
                    ? "No" : "Yes");
240
 
            recipient = recipient->next;
241
 
          }
242
 
        }
243
 
      }
244
 
    }
245
249
    goto decrypt_end;
246
250
  }
247
251
  
249
253
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
250
254
  }
251
255
  
 
256
  if (debug){
 
257
    gpgme_decrypt_result_t result;
 
258
    result = gpgme_op_decrypt_result(ctx);
 
259
    if (result == NULL){
 
260
      fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
261
    } else {
 
262
      fprintf(stderr, "Unsupported algorithm: %s\n",
 
263
              result->unsupported_algorithm);
 
264
      fprintf(stderr, "Wrong key usage: %d\n",
 
265
              result->wrong_key_usage);
 
266
      if(result->file_name != NULL){
 
267
        fprintf(stderr, "File name: %s\n", result->file_name);
 
268
      }
 
269
      gpgme_recipient_t recipient;
 
270
      recipient = result->recipients;
 
271
      if(recipient){
 
272
        while(recipient != NULL){
 
273
          fprintf(stderr, "Public key algorithm: %s\n",
 
274
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
275
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
276
          fprintf(stderr, "Secret key available: %s\n",
 
277
                  recipient->status == GPG_ERR_NO_SECKEY
 
278
                  ? "No" : "Yes");
 
279
          recipient = recipient->next;
 
280
        }
 
281
      }
 
282
    }
 
283
  }
 
284
  
252
285
  /* Seek back to the beginning of the GPGME plaintext data buffer */
253
286
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
254
287
    perror("pgpme_data_seek");
301
334
}
302
335
 
303
336
static const char * safer_gnutls_strerror (int value) {
304
 
  const char *ret = gnutls_strerror (value); /* Spurious warning */
 
337
  const char *ret = gnutls_strerror (value);
305
338
  if (ret == NULL)
306
339
    ret = "(unknown)";
307
340
  return ret;
314
347
}
315
348
 
316
349
static int init_gnutls_global(mandos_context *mc,
317
 
                              const char *pubkeyfilename,
318
 
                              const char *seckeyfilename){
 
350
                              const char *pubkeyfile,
 
351
                              const char *seckeyfile){
319
352
  int ret;
320
353
  
321
354
  if(debug){
322
355
    fprintf(stderr, "Initializing GnuTLS\n");
323
356
  }
324
 
  
325
 
  ret = gnutls_global_init();
326
 
  if (ret != GNUTLS_E_SUCCESS) {
 
357
 
 
358
  if ((ret = gnutls_global_init ())
 
359
      != GNUTLS_E_SUCCESS) {
327
360
    fprintf (stderr, "GnuTLS global_init: %s\n",
328
361
             safer_gnutls_strerror(ret));
329
362
    return -1;
338
371
  }
339
372
  
340
373
  /* OpenPGP credentials */
341
 
  gnutls_certificate_allocate_credentials(&mc->cred);
342
 
  if (ret != GNUTLS_E_SUCCESS){
343
 
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
344
 
                                                     warning */
 
374
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
375
      != GNUTLS_E_SUCCESS) {
 
376
    fprintf (stderr, "GnuTLS memory error: %s\n",
345
377
             safer_gnutls_strerror(ret));
346
378
    gnutls_global_deinit ();
347
379
    return -1;
349
381
  
350
382
  if(debug){
351
383
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
352
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
353
 
            seckeyfilename);
 
384
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
385
            seckeyfile);
354
386
  }
355
387
  
356
388
  ret = gnutls_certificate_set_openpgp_key_file
357
 
    (mc->cred, pubkeyfilename, seckeyfilename,
358
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
389
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
359
390
  if (ret != GNUTLS_E_SUCCESS) {
360
391
    fprintf(stderr,
361
392
            "Error[%d] while reading the OpenPGP key pair ('%s',"
362
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
393
            " '%s')\n", ret, pubkeyfile, seckeyfile);
363
394
    fprintf(stdout, "The GnuTLS error is: %s\n",
364
395
            safer_gnutls_strerror(ret));
365
396
    goto globalfail;
457
488
  }
458
489
  
459
490
  if(debug){
460
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
461
 
            "\n", ip, port);
 
491
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
 
492
            ip, port);
462
493
  }
463
494
  
464
495
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
475
506
    fprintf(stderr, "Binding to interface %s\n", interface);
476
507
  }
477
508
  
478
 
  memset(&to, 0, sizeof(to));
 
509
  memset(&to,0,sizeof(to));     /* Spurious warning */
479
510
  to.in6.sin6_family = AF_INET6;
480
511
  /* It would be nice to have a way to detect if we were passed an
481
512
     IPv4 address here.   Now we assume an IPv6 address. */
488
519
    fprintf(stderr, "Bad address: %s\n", ip);
489
520
    return -1;
490
521
  }
491
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
522
  to.in6.sin6_port = htons(port);       /* Spurious warning */
492
523
  
493
524
  to.in6.sin6_scope_id = (uint32_t)if_index;
494
525
  
495
526
  if(debug){
496
 
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
497
 
            port);
 
527
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
498
528
    char addrstr[INET6_ADDRSTRLEN] = "";
499
529
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
500
530
                 sizeof(addrstr)) == NULL){
541
571
  }
542
572
  
543
573
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
544
 
 
545
 
  do{
546
 
    ret = gnutls_handshake (session);
547
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
574
  
 
575
  ret = gnutls_handshake (session);
548
576
  
549
577
  if (ret != GNUTLS_E_SUCCESS){
550
578
    if(debug){
582
610
      case GNUTLS_E_AGAIN:
583
611
        break;
584
612
      case GNUTLS_E_REHANDSHAKE:
585
 
        do{
586
 
          ret = gnutls_handshake (session);
587
 
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
613
        ret = gnutls_handshake (session);
588
614
        if (ret < 0){
589
615
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
590
616
          gnutls_perror (ret);
635
661
    } else {
636
662
      retval = -1;
637
663
    }
638
 
  } else {
639
 
    retval = -1;
640
664
  }
641
665
  
642
666
  /* Shutdown procedure */
663
687
                             flags,
664
688
                             void* userdata) {
665
689
  mandos_context *mc = userdata;
666
 
  assert(r);
 
690
  assert(r);                    /* Spurious warning */
667
691
  
668
692
  /* Called whenever a service has been resolved successfully or
669
693
     timed out */
681
705
      char ip[AVAHI_ADDRESS_STR_MAX];
682
706
      avahi_address_snprint(ip, sizeof(ip), address);
683
707
      if(debug){
684
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
685
 
                PRIu16 ") on port %d\n", name, host_name, ip,
686
 
                interface, port);
 
708
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
 
709
                " port %d\n", name, host_name, ip, interface, port);
687
710
      }
688
711
      int ret = start_mandos_communication(ip, port, interface, mc);
689
712
      if (ret == 0){
690
 
        avahi_simple_poll_quit(mc->simple_poll);
 
713
        exit(EXIT_SUCCESS);
691
714
      }
692
715
    }
693
716
  }
705
728
                             flags,
706
729
                             void* userdata) {
707
730
  mandos_context *mc = userdata;
708
 
  assert(b);
 
731
  assert(b);                    /* Spurious warning */
709
732
  
710
733
  /* Called whenever a new services becomes available on the LAN or
711
734
     is removed from the LAN */
747
770
 
748
771
/* Combines file name and path and returns the malloced new
749
772
   string. some sane checks could/should be added */
750
 
static char *combinepath(const char *first, const char *second){
751
 
  char *tmp;
752
 
  int ret = asprintf(&tmp, "%s/%s", first, second);
753
 
  if(ret < 0){
 
773
static const char *combinepath(const char *first, const char *second){
 
774
  size_t f_len = strlen(first);
 
775
  size_t s_len = strlen(second);
 
776
  char *tmp = malloc(f_len + s_len + 2);
 
777
  if (tmp == NULL){
754
778
    return NULL;
755
779
  }
 
780
  if(f_len > 0){
 
781
    memcpy(tmp, first, f_len);  /* Spurious warning */
 
782
  }
 
783
  tmp[f_len] = '/';
 
784
  if(s_len > 0){
 
785
    memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
 
786
  }
 
787
  tmp[f_len + 1 + s_len] = '\0';
756
788
  return tmp;
757
789
}
758
790
 
769
801
    gid_t gid;
770
802
    char *connect_to = NULL;
771
803
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
772
 
    char *pubkeyfilename = NULL;
773
 
    char *seckeyfilename = NULL;
774
 
    const char *pubkeyname = "pubkey.txt";
775
 
    const char *seckeyname = "seckey.txt";
 
804
    const char *pubkeyfile = "pubkey.txt";
 
805
    const char *seckeyfile = "seckey.txt";
776
806
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
777
807
                          .dh_bits = 1024, .priority = "SECURE256"};
778
808
    bool gnutls_initalized = false;
830
860
          keydir = arg;
831
861
          break;
832
862
        case 's':
833
 
          seckeyname = arg;
 
863
          seckeyfile = arg;
834
864
          break;
835
865
        case 'p':
836
 
          pubkeyname = arg;
 
866
          pubkeyfile = arg;
837
867
          break;
838
868
        case 129:
839
869
          errno = 0;
848
878
          break;
849
879
        case ARGP_KEY_ARG:
850
880
          argp_usage (state);
851
 
        case ARGP_KEY_END:
852
881
          break;
 
882
          case ARGP_KEY_END:
 
883
            break;
853
884
        default:
854
885
          return ARGP_ERR_UNKNOWN;
855
886
        }
860
891
                           .args_doc = "",
861
892
                           .doc = "Mandos client -- Get and decrypt"
862
893
                           " passwords from mandos server" };
863
 
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
864
 
      if (ret == ARGP_ERR_UNKNOWN){
865
 
        fprintf(stderr, "Unknown error while parsing arguments\n");
866
 
        exitcode = EXIT_FAILURE;
867
 
        goto end;
868
 
      }
 
894
      argp_parse (&argp, argc, argv, 0, 0, NULL);
869
895
    }
870
896
      
871
 
    pubkeyfilename = combinepath(keydir, pubkeyname);
872
 
    if (pubkeyfilename == NULL){
 
897
    pubkeyfile = combinepath(keydir, pubkeyfile);
 
898
    if (pubkeyfile == NULL){
873
899
      perror("combinepath");
874
900
      exitcode = EXIT_FAILURE;
875
901
      goto end;
876
902
    }
877
903
    
878
 
    seckeyfilename = combinepath(keydir, seckeyname);
879
 
    if (seckeyfilename == NULL){
 
904
    seckeyfile = combinepath(keydir, seckeyfile);
 
905
    if (seckeyfile == NULL){
880
906
      perror("combinepath");
881
 
      exitcode = EXIT_FAILURE;
882
907
      goto end;
883
908
    }
884
909
 
885
 
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
 
910
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
886
911
    if (ret == -1){
887
 
      fprintf(stderr, "init_gnutls_global failed\n");
888
 
      exitcode = EXIT_FAILURE;
 
912
      fprintf(stderr, "init_gnutls_global\n");
889
913
      goto end;
890
914
    } else {
891
915
      gnutls_initalized = true;
892
916
    }
893
 
    
894
 
    /* If the interface is down, bring it up */
895
 
    {
896
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
897
 
      if(sd < 0) {
898
 
        perror("socket");
899
 
        exitcode = EXIT_FAILURE;
900
 
        goto end;
901
 
      }
902
 
      strcpy(network.ifr_name, interface);
903
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
904
 
      if(ret == -1){
905
 
        perror("ioctl SIOCGIFFLAGS");
906
 
        exitcode = EXIT_FAILURE;
907
 
        goto end;
908
 
      }
909
 
      if((network.ifr_flags & IFF_UP) == 0){
910
 
        network.ifr_flags |= IFF_UP;
911
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
912
 
        if(ret == -1){
913
 
          perror("ioctl SIOCSIFFLAGS");
914
 
          exitcode = EXIT_FAILURE;
915
 
          goto end;
916
 
        }
917
 
      }
918
 
      close(sd);
919
 
    }
920
 
    
 
917
 
921
918
    uid = getuid();
922
919
    gid = getgid();
923
 
    
 
920
 
924
921
    ret = setuid(uid);
925
922
    if (ret == -1){
926
923
      perror("setuid");
964
961
      goto end;
965
962
    }
966
963
    
 
964
    /* If the interface is down, bring it up */
 
965
    {
 
966
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
967
      if(sd < 0) {
 
968
        perror("socket");
 
969
        exitcode = EXIT_FAILURE;
 
970
        goto end;
 
971
      }
 
972
      strcpy(network.ifr_name, interface); /* Spurious warning */
 
973
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
974
      if(ret == -1){
 
975
        perror("ioctl SIOCGIFFLAGS");
 
976
        exitcode = EXIT_FAILURE;
 
977
        goto end;
 
978
      }
 
979
      if((network.ifr_flags & IFF_UP) == 0){
 
980
        network.ifr_flags |= IFF_UP;
 
981
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
982
        if(ret == -1){
 
983
          perror("ioctl SIOCSIFFLAGS");
 
984
          exitcode = EXIT_FAILURE;
 
985
          goto end;
 
986
        }
 
987
      }
 
988
      close(sd);
 
989
    }
 
990
    
967
991
    if (not debug){
968
992
      avahi_set_log_function(empty_log);
969
993
    }
1041
1065
 
1042
1066
    if (mc.simple_poll != NULL)
1043
1067
        avahi_simple_poll_free(mc.simple_poll);
1044
 
    free(pubkeyfilename);
1045
 
    free(seckeyfilename);
 
1068
    free(pubkeyfile);
 
1069
    free(seckeyfile);
1046
1070
 
1047
1071
    if (gnutls_initalized){
1048
 
      gnutls_certificate_free_credentials(mc.cred);
 
1072
      gnutls_certificate_free_credentials (mc.cred);
1049
1073
      gnutls_global_deinit ();
1050
1074
    }
1051
1075