/mandos/trunk

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

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2009-02-12 18:56:52 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090212185652-ast00yprt2pe2l4p
Overflows are not detected by sscanf(), so stop using it:

* plugin-runner.c (main/parse_opt): Change from using "sscanf()" to
                                    "strtoimax()".
* plugins.d/mandos-client.c (main/parse_opt, main): Change from using
                                                    "sscanf()" to
                                                    "strtoimax()" and
                                                    "strtof()".
* splashy.c (main): Change from using "sscanf()" to "strtoimax()".
* usplash.c (main): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 */
31
31
 
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
 
#ifndef _LARGEFILE_SOURCE
34
33
#define _LARGEFILE_SOURCE
35
 
#endif
36
 
#ifndef _FILE_OFFSET_BITS
37
34
#define _FILE_OFFSET_BITS 64
38
 
#endif
39
35
 
40
36
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
37
 
71
67
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
68
                                */
73
69
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
 
                                   getuid(), getgid(), seteuid(),
 
70
                                   getuid(), getgid(), setuid(),
75
71
                                   setgid() */
76
72
#include <arpa/inet.h>          /* inet_pton(), htons */
77
73
#include <iso646.h>             /* not, or, and */
80
76
                                   argp_parse(), ARGP_KEY_ARG,
81
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
78
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sig_atomic_t,
84
 
                                   raise() */
 
79
                                   sigaction(), SIGTERM, sigaction,
 
80
                                   sig_atomic_t */
85
81
 
86
82
#ifdef __linux__
87
83
#include <sys/klog.h>           /* klogctl() */
142
138
                      .dh_bits = 1024, .priority = "SECURE256"
143
139
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
144
140
 
145
 
sig_atomic_t quit_now = 0;
146
 
int signal_received = 0;
147
 
 
148
141
/*
149
142
 * Make additional room in "buffer" for at least BUFFER_SIZE more
150
143
 * bytes. "buffer_capacity" is how much is currently allocated,
167
160
 */
168
161
static bool init_gpgme(const char *seckey,
169
162
                       const char *pubkey, const char *tempdir){
 
163
  int ret;
170
164
  gpgme_error_t rc;
171
165
  gpgme_engine_info_t engine_info;
172
166
  
175
169
   * Helper function to insert pub and seckey to the engine keyring.
176
170
   */
177
171
  bool import_key(const char *filename){
178
 
    int ret;
179
172
    int fd;
180
173
    gpgme_data_t pgp_data;
181
174
    
252
245
    return false;
253
246
  }
254
247
  
255
 
  return true;
 
248
  return true; 
256
249
}
257
250
 
258
251
/* 
312
305
        }
313
306
        gpgme_recipient_t recipient;
314
307
        recipient = result->recipients;
315
 
        while(recipient != NULL){
316
 
          fprintf(stderr, "Public key algorithm: %s\n",
317
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
318
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
319
 
          fprintf(stderr, "Secret key available: %s\n",
320
 
                  recipient->status == GPG_ERR_NO_SECKEY
321
 
                  ? "No" : "Yes");
322
 
          recipient = recipient->next;
 
308
        if(recipient){
 
309
          while(recipient != NULL){
 
310
            fprintf(stderr, "Public key algorithm: %s\n",
 
311
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
312
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
313
            fprintf(stderr, "Secret key available: %s\n",
 
314
                    recipient->status == GPG_ERR_NO_SECKEY
 
315
                    ? "No" : "Yes");
 
316
            recipient = recipient->next;
 
317
          }
323
318
        }
324
319
      }
325
320
    }
477
472
static int init_gnutls_session(gnutls_session_t *session){
478
473
  int ret;
479
474
  /* GnuTLS session creation */
480
 
  do {
481
 
    ret = gnutls_init(session, GNUTLS_SERVER);
482
 
    if(quit_now){
483
 
      return -1;
484
 
    }
485
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
475
  ret = gnutls_init(session, GNUTLS_SERVER);
486
476
  if(ret != GNUTLS_E_SUCCESS){
487
477
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
488
478
            safer_gnutls_strerror(ret));
490
480
  
491
481
  {
492
482
    const char *err;
493
 
    do {
494
 
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
495
 
      if(quit_now){
496
 
        gnutls_deinit(*session);
497
 
        return -1;
498
 
      }
499
 
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
483
    ret = gnutls_priority_set_direct(*session, mc.priority, &err);
500
484
    if(ret != GNUTLS_E_SUCCESS){
501
485
      fprintf(stderr, "Syntax error at: %s\n", err);
502
486
      fprintf(stderr, "GnuTLS error: %s\n",
506
490
    }
507
491
  }
508
492
  
509
 
  do {
510
 
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
511
 
                                 mc.cred);
512
 
    if(quit_now){
513
 
      gnutls_deinit(*session);
514
 
      return -1;
515
 
    }
516
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
493
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
494
                               mc.cred);
517
495
  if(ret != GNUTLS_E_SUCCESS){
518
496
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
519
497
            safer_gnutls_strerror(ret));
522
500
  }
523
501
  
524
502
  /* ignore client certificate if any. */
525
 
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
 
503
  gnutls_certificate_server_set_request(*session,
 
504
                                        GNUTLS_CERT_IGNORE);
526
505
  
527
506
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
528
507
  
537
516
static int start_mandos_communication(const char *ip, uint16_t port,
538
517
                                      AvahiIfIndex if_index,
539
518
                                      int af){
540
 
  int ret, tcp_sd = -1;
 
519
  int ret, tcp_sd;
541
520
  ssize_t sret;
542
521
  union {
543
522
    struct sockaddr_in in;
547
526
  char *decrypted_buffer;
548
527
  size_t buffer_length = 0;
549
528
  size_t buffer_capacity = 0;
 
529
  ssize_t decrypted_buffer_size;
550
530
  size_t written;
551
531
  int retval = 0;
552
532
  gnutls_session_t session;
553
533
  int pf;                       /* Protocol family */
554
534
  
555
 
  if(quit_now){
556
 
    return -1;
557
 
  }
558
 
  
559
535
  switch(af){
560
536
  case AF_INET6:
561
537
    pf = PF_INET6;
581
557
  tcp_sd = socket(pf, SOCK_STREAM, 0);
582
558
  if(tcp_sd < 0){
583
559
    perror("socket");
584
 
    retval = -1;
585
 
    goto mandos_end;
586
 
  }
587
 
  
588
 
  if(quit_now){
589
 
    retval = -1;
590
 
    goto mandos_end;
 
560
    return -1;
591
561
  }
592
562
  
593
563
  memset(&to, 0, sizeof(to));
594
564
  if(af == AF_INET6){
595
 
    to.in6.sin6_family = (sa_family_t)af;
 
565
    to.in6.sin6_family = (uint16_t)af;
596
566
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
597
567
  } else {                      /* IPv4 */
598
568
    to.in.sin_family = (sa_family_t)af;
600
570
  }
601
571
  if(ret < 0 ){
602
572
    perror("inet_pton");
603
 
    retval = -1;
604
 
    goto mandos_end;
 
573
    return -1;
605
574
  }
606
575
  if(ret == 0){
607
576
    fprintf(stderr, "Bad address: %s\n", ip);
608
 
    retval = -1;
609
 
    goto mandos_end;
 
577
    return -1;
610
578
  }
611
579
  if(af == AF_INET6){
612
580
    to.in6.sin6_port = htons(port); /* Spurious warnings from
619
587
      if(if_index == AVAHI_IF_UNSPEC){
620
588
        fprintf(stderr, "An IPv6 link-local address is incomplete"
621
589
                " without a network interface\n");
622
 
        retval = -1;
623
 
        goto mandos_end;
 
590
        return -1;
624
591
      }
625
592
      /* Set the network interface number as scope */
626
593
      to.in6.sin6_scope_id = (uint32_t)if_index;
631
598
                                     -Wunreachable-code */
632
599
  }
633
600
  
634
 
  if(quit_now){
635
 
    retval = -1;
636
 
    goto mandos_end;
637
 
  }
638
 
  
639
601
  if(debug){
640
602
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
641
603
      char interface[IF_NAMESIZE];
668
630
    }
669
631
  }
670
632
  
671
 
  if(quit_now){
672
 
    retval = -1;
673
 
    goto mandos_end;
674
 
  }
675
 
  
676
633
  if(af == AF_INET6){
677
634
    ret = connect(tcp_sd, &to.in6, sizeof(to));
678
635
  } else {
680
637
  }
681
638
  if(ret < 0){
682
639
    perror("connect");
683
 
    retval = -1;
684
 
    goto mandos_end;
685
 
  }
686
 
  
687
 
  if(quit_now){
688
 
    retval = -1;
689
 
    goto mandos_end;
 
640
    return -1;
690
641
  }
691
642
  
692
643
  const char *out = mandos_protocol_version;
711
662
        break;
712
663
      }
713
664
    }
714
 
  
715
 
    if(quit_now){
716
 
      retval = -1;
717
 
      goto mandos_end;
718
 
    }
719
665
  }
720
666
  
721
667
  if(debug){
722
668
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
723
669
  }
724
670
  
725
 
  if(quit_now){
726
 
    retval = -1;
727
 
    goto mandos_end;
728
 
  }
729
 
  
730
671
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
731
672
  
732
 
  if(quit_now){
733
 
    retval = -1;
734
 
    goto mandos_end;
735
 
  }
736
 
  
737
 
  do {
 
673
  do{
738
674
    ret = gnutls_handshake(session);
739
 
    if(quit_now){
740
 
      retval = -1;
741
 
      goto mandos_end;
742
 
    }
743
675
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
744
676
  
745
677
  if(ret != GNUTLS_E_SUCCESS){
759
691
  }
760
692
  
761
693
  while(true){
762
 
    
763
 
    if(quit_now){
764
 
      retval = -1;
765
 
      goto mandos_end;
766
 
    }
767
 
    
768
694
    buffer_capacity = incbuffer(&buffer, buffer_length,
769
695
                                   buffer_capacity);
770
696
    if(buffer_capacity == 0){
773
699
      goto mandos_end;
774
700
    }
775
701
    
776
 
    if(quit_now){
777
 
      retval = -1;
778
 
      goto mandos_end;
779
 
    }
780
 
    
781
702
    sret = gnutls_record_recv(session, buffer+buffer_length,
782
703
                              BUFFER_SIZE);
783
704
    if(sret == 0){
789
710
      case GNUTLS_E_AGAIN:
790
711
        break;
791
712
      case GNUTLS_E_REHANDSHAKE:
792
 
        do {
 
713
        do{
793
714
          ret = gnutls_handshake(session);
794
 
          
795
 
          if(quit_now){
796
 
            retval = -1;
797
 
            goto mandos_end;
798
 
          }
799
715
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
800
716
        if(ret < 0){
801
717
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
820
736
    fprintf(stderr, "Closing TLS session\n");
821
737
  }
822
738
  
823
 
  if(quit_now){
824
 
    retval = -1;
825
 
    goto mandos_end;
826
 
  }
827
 
  
828
 
  do {
829
 
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
830
 
    if(quit_now){
831
 
      retval = -1;
832
 
      goto mandos_end;
833
 
    }
834
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
739
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
835
740
  
836
741
  if(buffer_length > 0){
837
 
    ssize_t decrypted_buffer_size;
838
742
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
839
743
                                               buffer_length,
840
744
                                               &decrypted_buffer);
841
745
    if(decrypted_buffer_size >= 0){
842
 
      
843
746
      written = 0;
844
747
      while(written < (size_t) decrypted_buffer_size){
845
 
        if(quit_now){
846
 
          retval = -1;
847
 
          goto mandos_end;
848
 
        }
849
 
        
850
748
        ret = (int)fwrite(decrypted_buffer + written, 1,
851
749
                          (size_t)decrypted_buffer_size - written,
852
750
                          stdout);
872
770
  
873
771
 mandos_end:
874
772
  free(buffer);
875
 
  if(tcp_sd >= 0){
876
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
877
 
  }
 
773
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
878
774
  if(ret == -1){
879
775
    perror("close");
880
776
  }
881
777
  gnutls_deinit(session);
882
 
  if(quit_now){
883
 
    retval = -1;
884
 
  }
885
778
  return retval;
886
779
}
887
780
 
904
797
  /* Called whenever a service has been resolved successfully or
905
798
     timed out */
906
799
  
907
 
  if(quit_now){
908
 
    return;
909
 
  }
910
 
  
911
800
  switch(event){
912
801
  default:
913
802
  case AVAHI_RESOLVER_FAILURE:
950
839
  /* Called whenever a new services becomes available on the LAN or
951
840
     is removed from the LAN */
952
841
  
953
 
  if(quit_now){
954
 
    return;
955
 
  }
956
 
  
957
842
  switch(event){
958
843
  default:
959
844
  case AVAHI_BROWSER_FAILURE:
969
854
       the callback function is called the Avahi server will free the
970
855
       resolver for us. */
971
856
    
972
 
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
973
 
                                    name, type, domain, protocol, 0,
974
 
                                    resolve_callback, NULL) == NULL)
 
857
    if(!(avahi_s_service_resolver_new(mc.server, interface,
 
858
                                       protocol, name, type, domain,
 
859
                                       AVAHI_PROTO_INET6, 0,
 
860
                                       resolve_callback, NULL)))
975
861
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
976
862
              name, avahi_strerror(avahi_server_errno(mc.server)));
977
863
    break;
988
874
  }
989
875
}
990
876
 
 
877
sig_atomic_t quit_now = 0;
 
878
 
991
879
/* stop main loop after sigterm has been called */
992
 
static void handle_sigterm(int sig){
 
880
static void handle_sigterm(__attribute__((unused)) int sig){
993
881
  if(quit_now){
994
882
    return;
995
883
  }
996
884
  quit_now = 1;
997
 
  signal_received = sig;
998
885
  int old_errno = errno;
999
886
  if(mc.simple_poll != NULL){
1000
887
    avahi_simple_poll_quit(mc.simple_poll);
1011
898
  int exitcode = EXIT_SUCCESS;
1012
899
  const char *interface = "eth0";
1013
900
  struct ifreq network;
1014
 
  int sd = -1;
1015
 
  bool take_down_interface = false;
 
901
  int sd;
1016
902
  uid_t uid;
1017
903
  gid_t gid;
1018
904
  char *connect_to = NULL;
1022
908
  const char *seckey = PATHDIR "/" SECKEY;
1023
909
  const char *pubkey = PATHDIR "/" PUBKEY;
1024
910
  
 
911
  /* Initialize Mandos context */
 
912
  mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
 
913
                         .dh_bits = 1024, .priority = "SECURE256"
 
914
                         ":!CTYPE-X.509:+CTYPE-OPENPGP" };
1025
915
  bool gnutls_initialized = false;
1026
916
  bool gpgme_initialized = false;
1027
917
  float delay = 2.5f;
1028
918
  
1029
 
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
 
919
  struct sigaction old_sigterm_action;
1030
920
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1031
921
  
1032
 
  uid = getuid();
1033
 
  gid = getgid();
1034
 
  
1035
 
  /* Lower any group privileges we might have, just to be safe */
1036
 
  errno = 0;
1037
 
  ret = setgid(gid);
1038
 
  if(ret == -1){
1039
 
    perror("setgid");
1040
 
  }
1041
 
  
1042
 
  /* Lower user privileges (temporarily) */
1043
 
  errno = 0;
1044
 
  ret = seteuid(uid);
1045
 
  if(ret == -1){
1046
 
    perror("seteuid");
1047
 
  }
1048
 
  
1049
 
  if(quit_now){
1050
 
    goto end;
1051
 
  }
1052
 
  
1053
922
  {
1054
923
    struct argp_option options[] = {
1055
924
      { .name = "debug", .key = 128,
1182
1051
    exitcode = EXIT_FAILURE;
1183
1052
    goto end;
1184
1053
  }
1185
 
  /* Need to check if the handler is SIG_IGN before handling:
1186
 
     | [[info:libc:Initial Signal Actions]] |
1187
 
     | [[info:libc:Basic Signal Handling]]  |
1188
 
  */
1189
 
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1190
 
  if(ret == -1){
1191
 
    perror("sigaction");
1192
 
    return EXIT_FAILURE;
1193
 
  }
1194
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1195
 
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1196
 
    if(ret == -1){
1197
 
      perror("sigaction");
1198
 
      exitcode = EXIT_FAILURE;
1199
 
      goto end;
1200
 
    }
1201
 
  }
1202
 
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1203
 
  if(ret == -1){
1204
 
    perror("sigaction");
1205
 
    return EXIT_FAILURE;
1206
 
  }
1207
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1208
 
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1209
 
    if(ret == -1){
1210
 
      perror("sigaction");
1211
 
      exitcode = EXIT_FAILURE;
1212
 
      goto end;
1213
 
    }
1214
 
  }
1215
 
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1216
 
  if(ret == -1){
1217
 
    perror("sigaction");
1218
 
    return EXIT_FAILURE;
1219
 
  }
1220
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1221
 
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1222
 
    if(ret == -1){
1223
 
      perror("sigaction");
1224
 
      exitcode = EXIT_FAILURE;
1225
 
      goto end;
1226
 
    }
1227
 
  }
 
1054
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1055
  if(ret == -1){
 
1056
    perror("sigaction");
 
1057
    exitcode = EXIT_FAILURE;
 
1058
    goto end;
 
1059
  }  
1228
1060
  
1229
1061
  /* If the interface is down, bring it up */
1230
1062
  if(interface[0] != '\0'){
1231
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1232
 
    if(if_index == 0){
1233
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1234
 
      exitcode = EXIT_FAILURE;
1235
 
      goto end;
1236
 
    }
1237
 
    
1238
 
    if(quit_now){
1239
 
      goto end;
1240
 
    }
1241
 
    
1242
 
    /* Re-raise priviliges */
1243
 
    errno = 0;
1244
 
    ret = seteuid(0);
1245
 
    if(ret == -1){
1246
 
      perror("seteuid");
1247
 
    }
1248
 
    
1249
1063
#ifdef __linux__
1250
1064
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1251
1065
       messages to mess up the prompt */
1269
1083
        }
1270
1084
      }
1271
1085
#endif  /* __linux__ */
1272
 
      /* Lower privileges */
1273
 
      errno = 0;
1274
 
      ret = seteuid(uid);
1275
 
      if(ret == -1){
1276
 
        perror("seteuid");
1277
 
      }
1278
1086
      goto end;
1279
1087
    }
1280
1088
    strcpy(network.ifr_name, interface);
1290
1098
      }
1291
1099
#endif  /* __linux__ */
1292
1100
      exitcode = EXIT_FAILURE;
1293
 
      /* Lower privileges */
1294
 
      errno = 0;
1295
 
      ret = seteuid(uid);
1296
 
      if(ret == -1){
1297
 
        perror("seteuid");
1298
 
      }
1299
1101
      goto end;
1300
1102
    }
1301
1103
    if((network.ifr_flags & IFF_UP) == 0){
1302
1104
      network.ifr_flags |= IFF_UP;
1303
 
      take_down_interface = true;
1304
1105
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1305
1106
      if(ret == -1){
1306
 
        take_down_interface = false;
1307
1107
        perror("ioctl SIOCSIFFLAGS");
1308
1108
        exitcode = EXIT_FAILURE;
1309
1109
#ifdef __linux__
1314
1114
          }
1315
1115
        }
1316
1116
#endif  /* __linux__ */
1317
 
        /* Lower privileges */
1318
 
        errno = 0;
1319
 
        ret = seteuid(uid);
1320
 
        if(ret == -1){
1321
 
          perror("seteuid");
1322
 
        }
1323
1117
        goto end;
1324
1118
      }
1325
1119
    }
1337
1131
        perror("nanosleep");
1338
1132
      }
1339
1133
    }
1340
 
    if(not take_down_interface){
1341
 
      /* We won't need the socket anymore */
1342
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1343
 
      if(ret == -1){
1344
 
        perror("close");
1345
 
      }
 
1134
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1135
    if(ret == -1){
 
1136
      perror("close");
1346
1137
    }
1347
1138
#ifdef __linux__
1348
1139
    if(restore_loglevel){
1353
1144
      }
1354
1145
    }
1355
1146
#endif  /* __linux__ */
1356
 
    /* Lower privileges */
1357
 
    errno = 0;
1358
 
    if(take_down_interface){
1359
 
      /* Lower privileges */
1360
 
      ret = seteuid(uid);
1361
 
      if(ret == -1){
1362
 
        perror("seteuid");
1363
 
      }
1364
 
    } else {
1365
 
      /* Lower privileges permanently */
1366
 
      ret = setuid(uid);
1367
 
      if(ret == -1){
1368
 
        perror("setuid");
1369
 
      }
1370
 
    }
1371
 
  }
1372
 
  
1373
 
  if(quit_now){
1374
 
    goto end;
 
1147
  }
 
1148
  
 
1149
  uid = getuid();
 
1150
  gid = getgid();
 
1151
  
 
1152
  errno = 0;
 
1153
  setgid(gid);
 
1154
  if(ret == -1){
 
1155
    perror("setgid");
 
1156
  }
 
1157
  
 
1158
  ret = setuid(uid);
 
1159
  if(ret == -1){
 
1160
    perror("setuid");
1375
1161
  }
1376
1162
  
1377
1163
  ret = init_gnutls_global(pubkey, seckey);
1383
1169
    gnutls_initialized = true;
1384
1170
  }
1385
1171
  
1386
 
  if(quit_now){
1387
 
    goto end;
1388
 
  }
1389
 
  
1390
 
  tempdir_created = true;
1391
1172
  if(mkdtemp(tempdir) == NULL){
1392
 
    tempdir_created = false;
1393
1173
    perror("mkdtemp");
1394
1174
    goto end;
1395
1175
  }
1396
 
  
1397
 
  if(quit_now){
1398
 
    goto end;
1399
 
  }
 
1176
  tempdir_created = true;
1400
1177
  
1401
1178
  if(not init_gpgme(pubkey, seckey, tempdir)){
1402
1179
    fprintf(stderr, "init_gpgme failed\n");
1406
1183
    gpgme_initialized = true;
1407
1184
  }
1408
1185
  
1409
 
  if(quit_now){
1410
 
    goto end;
 
1186
  if(interface[0] != '\0'){
 
1187
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1188
    if(if_index == 0){
 
1189
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1190
      exitcode = EXIT_FAILURE;
 
1191
      goto end;
 
1192
    }
1411
1193
  }
1412
1194
  
1413
1195
  if(connect_to != NULL){
1419
1201
      exitcode = EXIT_FAILURE;
1420
1202
      goto end;
1421
1203
    }
1422
 
    
1423
 
    if(quit_now){
1424
 
      goto end;
1425
 
    }
1426
 
    
1427
1204
    uint16_t port;
1428
1205
    errno = 0;
1429
1206
    tmpmax = strtoimax(address+1, &tmp, 10);
1433
1210
      exitcode = EXIT_FAILURE;
1434
1211
      goto end;
1435
1212
    }
1436
 
  
1437
 
    if(quit_now){
1438
 
      goto end;
1439
 
    }
1440
 
    
1441
1213
    port = (uint16_t)tmpmax;
1442
1214
    *address = '\0';
1443
1215
    address = connect_to;
1448
1220
    } else {
1449
1221
      af = AF_INET;
1450
1222
    }
1451
 
    
1452
 
    if(quit_now){
1453
 
      goto end;
1454
 
    }
1455
 
    
1456
1223
    ret = start_mandos_communication(address, port, if_index, af);
1457
1224
    if(ret < 0){
1458
1225
      exitcode = EXIT_FAILURE;
1461
1228
    }
1462
1229
    goto end;
1463
1230
  }
1464
 
  
1465
 
  if(quit_now){
1466
 
    goto end;
1467
 
  }
1468
 
  
 
1231
    
1469
1232
  {
1470
1233
    AvahiServerConfig config;
1471
1234
    /* Do not publish any local Zeroconf records */
1492
1255
    goto end;
1493
1256
  }
1494
1257
  
1495
 
  if(quit_now){
1496
 
    goto end;
1497
 
  }
1498
 
  
1499
1258
  /* Create the Avahi service browser */
1500
1259
  sb = avahi_s_service_browser_new(mc.server, if_index,
1501
 
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
 
1260
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
1502
1261
                                   NULL, 0, browse_callback, NULL);
1503
1262
  if(sb == NULL){
1504
1263
    fprintf(stderr, "Failed to create service browser: %s\n",
1507
1266
    goto end;
1508
1267
  }
1509
1268
  
1510
 
  if(quit_now){
1511
 
    goto end;
1512
 
  }
1513
 
  
1514
1269
  /* Run the main loop */
1515
1270
  
1516
1271
  if(debug){
1545
1300
    gpgme_release(mc.ctx);
1546
1301
  }
1547
1302
  
1548
 
  /* Take down the network interface */
1549
 
  if(take_down_interface){
1550
 
    /* Re-raise priviliges */
1551
 
    errno = 0;
1552
 
    ret = seteuid(0);
1553
 
    if(ret == -1){
1554
 
      perror("seteuid");
1555
 
    }
1556
 
    if(geteuid() == 0){
1557
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1558
 
      if(ret == -1){
1559
 
        perror("ioctl SIOCGIFFLAGS");
1560
 
      } else if(network.ifr_flags & IFF_UP) {
1561
 
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1562
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1563
 
        if(ret == -1){
1564
 
          perror("ioctl SIOCSIFFLAGS");
1565
 
        }
1566
 
      }
1567
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1568
 
      if(ret == -1){
1569
 
        perror("close");
1570
 
      }
1571
 
      /* Lower privileges permanently */
1572
 
      errno = 0;
1573
 
      ret = setuid(uid);
1574
 
      if(ret == -1){
1575
 
        perror("setuid");
1576
 
      }
1577
 
    }
1578
 
  }
1579
 
  
1580
1303
  /* Removes the temp directory used by GPGME */
1581
1304
  if(tempdir_created){
1582
1305
    DIR *d;
1621
1344
    }
1622
1345
  }
1623
1346
  
1624
 
  if(quit_now){
1625
 
    sigemptyset(&old_sigterm_action.sa_mask);
1626
 
    old_sigterm_action.sa_handler = SIG_DFL;
1627
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
1628
 
    if(ret == -1){
1629
 
      perror("sigaction");
1630
 
    }
1631
 
    raise(signal_received);
1632
 
  }
1633
 
  
1634
1347
  return exitcode;
1635
1348
}