/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-25 01:16:38 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080825011638-zgqej2cafhne05ay
* mandos-keygen: Strip 24-bit checksum of Radix-64 from output to make
                 output strictly base64.

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