/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-15 09:28:06 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090215092806-l0uq42v3j1rq3gmi
* INSTALL: Changed "Python 2.4" to "Python 2.5".
* mandos.lsm: - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
 
                                   srand(), strtof(), abort() */
 
47
                                   srand(), strtof() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
49
#include <string.h>             /* memset(), strcmp(), strlen(),
50
50
                                   strerror(), asprintf(), strcpy() */
71
71
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
72
                                */
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
 
                                   getuid(), getgid(), seteuid(),
75
 
                                   setgid(), pause() */
 
74
                                   getuid(), getgid(), setuid(),
 
75
                                   setgid() */
76
76
#include <arpa/inet.h>          /* inet_pton(), htons */
77
77
#include <iso646.h>             /* not, or, and */
78
78
#include <argp.h>               /* struct argp_option, error_t, struct
80
80
                                   argp_parse(), ARGP_KEY_ARG,
81
81
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sig_atomic_t,
84
 
                                   raise() */
 
83
                                   sigaction(), SIGTERM, sigaction,
 
84
                                   sig_atomic_t */
85
85
 
86
86
#ifdef __linux__
87
87
#include <sys/klog.h>           /* klogctl() */
142
142
                      .dh_bits = 1024, .priority = "SECURE256"
143
143
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
144
144
 
145
 
sig_atomic_t quit_now = 0;
146
 
int signal_received = 0;
147
 
 
148
145
/*
149
146
 * Make additional room in "buffer" for at least BUFFER_SIZE more
150
147
 * bytes. "buffer_capacity" is how much is currently allocated,
167
164
 */
168
165
static bool init_gpgme(const char *seckey,
169
166
                       const char *pubkey, const char *tempdir){
 
167
  int ret;
170
168
  gpgme_error_t rc;
171
169
  gpgme_engine_info_t engine_info;
172
170
  
175
173
   * Helper function to insert pub and seckey to the engine keyring.
176
174
   */
177
175
  bool import_key(const char *filename){
178
 
    int ret;
179
176
    int fd;
180
177
    gpgme_data_t pgp_data;
181
178
    
252
249
    return false;
253
250
  }
254
251
  
255
 
  return true;
 
252
  return true; 
256
253
}
257
254
 
258
255
/* 
312
309
        }
313
310
        gpgme_recipient_t recipient;
314
311
        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;
 
312
        if(recipient){
 
313
          while(recipient != NULL){
 
314
            fprintf(stderr, "Public key algorithm: %s\n",
 
315
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
316
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
317
            fprintf(stderr, "Secret key available: %s\n",
 
318
                    recipient->status == GPG_ERR_NO_SECKEY
 
319
                    ? "No" : "Yes");
 
320
            recipient = recipient->next;
 
321
          }
323
322
        }
324
323
      }
325
324
    }
477
476
static int init_gnutls_session(gnutls_session_t *session){
478
477
  int ret;
479
478
  /* 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);
 
479
  ret = gnutls_init(session, GNUTLS_SERVER);
486
480
  if(ret != GNUTLS_E_SUCCESS){
487
481
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
488
482
            safer_gnutls_strerror(ret));
490
484
  
491
485
  {
492
486
    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);
 
487
    ret = gnutls_priority_set_direct(*session, mc.priority, &err);
500
488
    if(ret != GNUTLS_E_SUCCESS){
501
489
      fprintf(stderr, "Syntax error at: %s\n", err);
502
490
      fprintf(stderr, "GnuTLS error: %s\n",
506
494
    }
507
495
  }
508
496
  
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);
 
497
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
498
                               mc.cred);
517
499
  if(ret != GNUTLS_E_SUCCESS){
518
500
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
519
501
            safer_gnutls_strerror(ret));
522
504
  }
523
505
  
524
506
  /* ignore client certificate if any. */
525
 
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
 
507
  gnutls_certificate_server_set_request(*session,
 
508
                                        GNUTLS_CERT_IGNORE);
526
509
  
527
510
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
528
511
  
537
520
static int start_mandos_communication(const char *ip, uint16_t port,
538
521
                                      AvahiIfIndex if_index,
539
522
                                      int af){
540
 
  int ret, tcp_sd = -1;
 
523
  int ret, tcp_sd;
541
524
  ssize_t sret;
542
525
  union {
543
526
    struct sockaddr_in in;
544
527
    struct sockaddr_in6 in6;
545
528
  } to;
546
529
  char *buffer = NULL;
547
 
  char *decrypted_buffer = NULL;
 
530
  char *decrypted_buffer;
548
531
  size_t buffer_length = 0;
549
532
  size_t buffer_capacity = 0;
 
533
  ssize_t decrypted_buffer_size;
550
534
  size_t written;
551
 
  int retval = -1;
 
535
  int retval = 0;
552
536
  gnutls_session_t session;
553
537
  int pf;                       /* Protocol family */
554
538
  
555
 
  if(quit_now){
556
 
    return -1;
557
 
  }
558
 
  
559
539
  switch(af){
560
540
  case AF_INET6:
561
541
    pf = PF_INET6;
581
561
  tcp_sd = socket(pf, SOCK_STREAM, 0);
582
562
  if(tcp_sd < 0){
583
563
    perror("socket");
584
 
    goto mandos_end;
585
 
  }
586
 
  
587
 
  if(quit_now){
588
 
    goto mandos_end;
 
564
    return -1;
589
565
  }
590
566
  
591
567
  memset(&to, 0, sizeof(to));
592
568
  if(af == AF_INET6){
593
 
    to.in6.sin6_family = (sa_family_t)af;
 
569
    to.in6.sin6_family = (uint16_t)af;
594
570
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
595
571
  } else {                      /* IPv4 */
596
572
    to.in.sin_family = (sa_family_t)af;
598
574
  }
599
575
  if(ret < 0 ){
600
576
    perror("inet_pton");
601
 
    goto mandos_end;
 
577
    return -1;
602
578
  }
603
579
  if(ret == 0){
604
580
    fprintf(stderr, "Bad address: %s\n", ip);
605
 
    goto mandos_end;
 
581
    return -1;
606
582
  }
607
583
  if(af == AF_INET6){
608
584
    to.in6.sin6_port = htons(port); /* Spurious warnings from
615
591
      if(if_index == AVAHI_IF_UNSPEC){
616
592
        fprintf(stderr, "An IPv6 link-local address is incomplete"
617
593
                " without a network interface\n");
618
 
        goto mandos_end;
 
594
        return -1;
619
595
      }
620
596
      /* Set the network interface number as scope */
621
597
      to.in6.sin6_scope_id = (uint32_t)if_index;
626
602
                                     -Wunreachable-code */
627
603
  }
628
604
  
629
 
  if(quit_now){
630
 
    goto mandos_end;
631
 
  }
632
 
  
633
605
  if(debug){
634
606
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
635
607
      char interface[IF_NAMESIZE];
662
634
    }
663
635
  }
664
636
  
665
 
  if(quit_now){
666
 
    goto mandos_end;
667
 
  }
668
 
  
669
637
  if(af == AF_INET6){
670
638
    ret = connect(tcp_sd, &to.in6, sizeof(to));
671
639
  } else {
673
641
  }
674
642
  if(ret < 0){
675
643
    perror("connect");
676
 
    goto mandos_end;
677
 
  }
678
 
  
679
 
  if(quit_now){
680
 
    goto mandos_end;
 
644
    return -1;
681
645
  }
682
646
  
683
647
  const char *out = mandos_protocol_version;
688
652
                                   out_size - written));
689
653
    if(ret == -1){
690
654
      perror("write");
 
655
      retval = -1;
691
656
      goto mandos_end;
692
657
    }
693
658
    written += (size_t)ret;
701
666
        break;
702
667
      }
703
668
    }
704
 
  
705
 
    if(quit_now){
706
 
      goto mandos_end;
707
 
    }
708
669
  }
709
670
  
710
671
  if(debug){
711
672
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
712
673
  }
713
674
  
714
 
  if(quit_now){
715
 
    goto mandos_end;
716
 
  }
717
 
  
718
675
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
719
676
  
720
 
  if(quit_now){
721
 
    goto mandos_end;
722
 
  }
723
 
  
724
 
  do {
 
677
  do{
725
678
    ret = gnutls_handshake(session);
726
 
    if(quit_now){
727
 
      goto mandos_end;
728
 
    }
729
679
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
730
680
  
731
681
  if(ret != GNUTLS_E_SUCCESS){
733
683
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
734
684
      gnutls_perror(ret);
735
685
    }
 
686
    retval = -1;
736
687
    goto mandos_end;
737
688
  }
738
689
  
744
695
  }
745
696
  
746
697
  while(true){
747
 
    
748
 
    if(quit_now){
749
 
      goto mandos_end;
750
 
    }
751
 
    
752
698
    buffer_capacity = incbuffer(&buffer, buffer_length,
753
699
                                   buffer_capacity);
754
700
    if(buffer_capacity == 0){
755
701
      perror("incbuffer");
756
 
      goto mandos_end;
757
 
    }
758
 
    
759
 
    if(quit_now){
 
702
      retval = -1;
760
703
      goto mandos_end;
761
704
    }
762
705
    
771
714
      case GNUTLS_E_AGAIN:
772
715
        break;
773
716
      case GNUTLS_E_REHANDSHAKE:
774
 
        do {
 
717
        do{
775
718
          ret = gnutls_handshake(session);
776
 
          
777
 
          if(quit_now){
778
 
            goto mandos_end;
779
 
          }
780
719
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
781
720
        if(ret < 0){
782
721
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
783
722
          gnutls_perror(ret);
 
723
          retval = -1;
784
724
          goto mandos_end;
785
725
        }
786
726
        break;
787
727
      default:
788
728
        fprintf(stderr, "Unknown error while reading data from"
789
729
                " encrypted session with Mandos server\n");
 
730
        retval = -1;
790
731
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
791
732
        goto mandos_end;
792
733
      }
799
740
    fprintf(stderr, "Closing TLS session\n");
800
741
  }
801
742
  
802
 
  if(quit_now){
803
 
    goto mandos_end;
804
 
  }
805
 
  
806
 
  do {
807
 
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
808
 
    if(quit_now){
809
 
      goto mandos_end;
810
 
    }
811
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
743
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
812
744
  
813
745
  if(buffer_length > 0){
814
 
    ssize_t decrypted_buffer_size;
815
746
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
816
747
                                               buffer_length,
817
748
                                               &decrypted_buffer);
818
749
    if(decrypted_buffer_size >= 0){
819
 
      
820
750
      written = 0;
821
751
      while(written < (size_t) decrypted_buffer_size){
822
 
        if(quit_now){
823
 
          goto mandos_end;
824
 
        }
825
 
        
826
752
        ret = (int)fwrite(decrypted_buffer + written, 1,
827
753
                          (size_t)decrypted_buffer_size - written,
828
754
                          stdout);
831
757
            fprintf(stderr, "Error writing encrypted data: %s\n",
832
758
                    strerror(errno));
833
759
          }
834
 
          goto mandos_end;
 
760
          retval = -1;
 
761
          break;
835
762
        }
836
763
        written += (size_t)ret;
837
764
      }
838
 
      retval = 0;
 
765
      free(decrypted_buffer);
 
766
    } else {
 
767
      retval = -1;
839
768
    }
 
769
  } else {
 
770
    retval = -1;
840
771
  }
841
772
  
842
773
  /* Shutdown procedure */
843
774
  
844
775
 mandos_end:
845
 
  free(decrypted_buffer);
846
776
  free(buffer);
847
 
  if(tcp_sd >= 0){
848
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
849
 
  }
 
777
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
850
778
  if(ret == -1){
851
779
    perror("close");
852
780
  }
853
781
  gnutls_deinit(session);
854
 
  if(quit_now){
855
 
    retval = -1;
856
 
  }
857
782
  return retval;
858
783
}
859
784
 
876
801
  /* Called whenever a service has been resolved successfully or
877
802
     timed out */
878
803
  
879
 
  if(quit_now){
880
 
    return;
881
 
  }
882
 
  
883
804
  switch(event){
884
805
  default:
885
806
  case AVAHI_RESOLVER_FAILURE:
922
843
  /* Called whenever a new services becomes available on the LAN or
923
844
     is removed from the LAN */
924
845
  
925
 
  if(quit_now){
926
 
    return;
927
 
  }
928
 
  
929
846
  switch(event){
930
847
  default:
931
848
  case AVAHI_BROWSER_FAILURE:
960
877
  }
961
878
}
962
879
 
 
880
sig_atomic_t quit_now = 0;
 
881
 
963
882
/* stop main loop after sigterm has been called */
964
 
static void handle_sigterm(int sig){
 
883
static void handle_sigterm(__attribute__((unused)) int sig){
965
884
  if(quit_now){
966
885
    return;
967
886
  }
968
887
  quit_now = 1;
969
 
  signal_received = sig;
970
888
  int old_errno = errno;
971
889
  if(mc.simple_poll != NULL){
972
890
    avahi_simple_poll_quit(mc.simple_poll);
983
901
  int exitcode = EXIT_SUCCESS;
984
902
  const char *interface = "eth0";
985
903
  struct ifreq network;
986
 
  int sd = -1;
987
 
  bool take_down_interface = false;
 
904
  int sd;
988
905
  uid_t uid;
989
906
  gid_t gid;
990
907
  char *connect_to = NULL;
998
915
  bool gpgme_initialized = false;
999
916
  float delay = 2.5f;
1000
917
  
1001
 
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
 
918
  struct sigaction old_sigterm_action;
1002
919
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1003
920
  
1004
 
  uid = getuid();
1005
 
  gid = getgid();
1006
 
  
1007
 
  /* Lower any group privileges we might have, just to be safe */
1008
 
  errno = 0;
1009
 
  ret = setgid(gid);
1010
 
  if(ret == -1){
1011
 
    perror("setgid");
1012
 
  }
1013
 
  
1014
 
  /* Lower user privileges (temporarily) */
1015
 
  errno = 0;
1016
 
  ret = seteuid(uid);
1017
 
  if(ret == -1){
1018
 
    perror("seteuid");
1019
 
  }
1020
 
  
1021
 
  if(quit_now){
1022
 
    goto end;
1023
 
  }
1024
 
  
1025
921
  {
1026
922
    struct argp_option options[] = {
1027
923
      { .name = "debug", .key = 128,
1154
1050
    exitcode = EXIT_FAILURE;
1155
1051
    goto end;
1156
1052
  }
1157
 
  /* Need to check if the handler is SIG_IGN before handling:
1158
 
     | [[info:libc:Initial Signal Actions]] |
1159
 
     | [[info:libc:Basic Signal Handling]]  |
1160
 
  */
1161
 
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1162
 
  if(ret == -1){
1163
 
    perror("sigaction");
1164
 
    return EXIT_FAILURE;
1165
 
  }
1166
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1167
 
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1168
 
    if(ret == -1){
1169
 
      perror("sigaction");
1170
 
      exitcode = EXIT_FAILURE;
1171
 
      goto end;
1172
 
    }
1173
 
  }
1174
 
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1175
 
  if(ret == -1){
1176
 
    perror("sigaction");
1177
 
    return EXIT_FAILURE;
1178
 
  }
1179
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1180
 
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1181
 
    if(ret == -1){
1182
 
      perror("sigaction");
1183
 
      exitcode = EXIT_FAILURE;
1184
 
      goto end;
1185
 
    }
1186
 
  }
1187
 
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1188
 
  if(ret == -1){
1189
 
    perror("sigaction");
1190
 
    return EXIT_FAILURE;
1191
 
  }
1192
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1193
 
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1194
 
    if(ret == -1){
1195
 
      perror("sigaction");
1196
 
      exitcode = EXIT_FAILURE;
1197
 
      goto end;
1198
 
    }
1199
 
  }
 
1053
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1054
  if(ret == -1){
 
1055
    perror("sigaction");
 
1056
    exitcode = EXIT_FAILURE;
 
1057
    goto end;
 
1058
  }  
1200
1059
  
1201
1060
  /* If the interface is down, bring it up */
1202
1061
  if(interface[0] != '\0'){
1203
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1204
 
    if(if_index == 0){
1205
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1206
 
      exitcode = EXIT_FAILURE;
1207
 
      goto end;
1208
 
    }
1209
 
    
1210
 
    if(quit_now){
1211
 
      goto end;
1212
 
    }
1213
 
    
1214
 
    /* Re-raise priviliges */
1215
 
    errno = 0;
1216
 
    ret = seteuid(0);
1217
 
    if(ret == -1){
1218
 
      perror("seteuid");
1219
 
    }
1220
 
    
1221
1062
#ifdef __linux__
1222
1063
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1223
1064
       messages to mess up the prompt */
1241
1082
        }
1242
1083
      }
1243
1084
#endif  /* __linux__ */
1244
 
      /* Lower privileges */
1245
 
      errno = 0;
1246
 
      ret = seteuid(uid);
1247
 
      if(ret == -1){
1248
 
        perror("seteuid");
1249
 
      }
1250
1085
      goto end;
1251
1086
    }
1252
1087
    strcpy(network.ifr_name, interface);
1262
1097
      }
1263
1098
#endif  /* __linux__ */
1264
1099
      exitcode = EXIT_FAILURE;
1265
 
      /* Lower privileges */
1266
 
      errno = 0;
1267
 
      ret = seteuid(uid);
1268
 
      if(ret == -1){
1269
 
        perror("seteuid");
1270
 
      }
1271
1100
      goto end;
1272
1101
    }
1273
1102
    if((network.ifr_flags & IFF_UP) == 0){
1274
1103
      network.ifr_flags |= IFF_UP;
1275
 
      take_down_interface = true;
1276
1104
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1277
1105
      if(ret == -1){
1278
 
        take_down_interface = false;
1279
1106
        perror("ioctl SIOCSIFFLAGS");
1280
1107
        exitcode = EXIT_FAILURE;
1281
1108
#ifdef __linux__
1286
1113
          }
1287
1114
        }
1288
1115
#endif  /* __linux__ */
1289
 
        /* Lower privileges */
1290
 
        errno = 0;
1291
 
        ret = seteuid(uid);
1292
 
        if(ret == -1){
1293
 
          perror("seteuid");
1294
 
        }
1295
1116
        goto end;
1296
1117
      }
1297
1118
    }
1309
1130
        perror("nanosleep");
1310
1131
      }
1311
1132
    }
1312
 
    if(not take_down_interface){
1313
 
      /* We won't need the socket anymore */
1314
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1315
 
      if(ret == -1){
1316
 
        perror("close");
1317
 
      }
 
1133
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1134
    if(ret == -1){
 
1135
      perror("close");
1318
1136
    }
1319
1137
#ifdef __linux__
1320
1138
    if(restore_loglevel){
1325
1143
      }
1326
1144
    }
1327
1145
#endif  /* __linux__ */
1328
 
    /* Lower privileges */
1329
 
    errno = 0;
1330
 
    if(take_down_interface){
1331
 
      /* Lower privileges */
1332
 
      ret = seteuid(uid);
1333
 
      if(ret == -1){
1334
 
        perror("seteuid");
1335
 
      }
1336
 
    } else {
1337
 
      /* Lower privileges permanently */
1338
 
      ret = setuid(uid);
1339
 
      if(ret == -1){
1340
 
        perror("setuid");
1341
 
      }
1342
 
    }
1343
 
  }
1344
 
  
1345
 
  if(quit_now){
1346
 
    goto end;
 
1146
  }
 
1147
  
 
1148
  uid = getuid();
 
1149
  gid = getgid();
 
1150
  
 
1151
  errno = 0;
 
1152
  setgid(gid);
 
1153
  if(ret == -1){
 
1154
    perror("setgid");
 
1155
  }
 
1156
  
 
1157
  ret = setuid(uid);
 
1158
  if(ret == -1){
 
1159
    perror("setuid");
1347
1160
  }
1348
1161
  
1349
1162
  ret = init_gnutls_global(pubkey, seckey);
1355
1168
    gnutls_initialized = true;
1356
1169
  }
1357
1170
  
1358
 
  if(quit_now){
1359
 
    goto end;
1360
 
  }
1361
 
  
1362
 
  tempdir_created = true;
1363
1171
  if(mkdtemp(tempdir) == NULL){
1364
 
    tempdir_created = false;
1365
1172
    perror("mkdtemp");
1366
1173
    goto end;
1367
1174
  }
1368
 
  
1369
 
  if(quit_now){
1370
 
    goto end;
1371
 
  }
 
1175
  tempdir_created = true;
1372
1176
  
1373
1177
  if(not init_gpgme(pubkey, seckey, tempdir)){
1374
1178
    fprintf(stderr, "init_gpgme failed\n");
1378
1182
    gpgme_initialized = true;
1379
1183
  }
1380
1184
  
1381
 
  if(quit_now){
1382
 
    goto end;
 
1185
  if(interface[0] != '\0'){
 
1186
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1187
    if(if_index == 0){
 
1188
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1189
      exitcode = EXIT_FAILURE;
 
1190
      goto end;
 
1191
    }
1383
1192
  }
1384
1193
  
1385
1194
  if(connect_to != NULL){
1391
1200
      exitcode = EXIT_FAILURE;
1392
1201
      goto end;
1393
1202
    }
1394
 
    
1395
 
    if(quit_now){
1396
 
      goto end;
1397
 
    }
1398
 
    
1399
1203
    uint16_t port;
1400
1204
    errno = 0;
1401
1205
    tmpmax = strtoimax(address+1, &tmp, 10);
1405
1209
      exitcode = EXIT_FAILURE;
1406
1210
      goto end;
1407
1211
    }
1408
 
  
1409
 
    if(quit_now){
1410
 
      goto end;
1411
 
    }
1412
 
    
1413
1212
    port = (uint16_t)tmpmax;
1414
1213
    *address = '\0';
1415
1214
    address = connect_to;
1420
1219
    } else {
1421
1220
      af = AF_INET;
1422
1221
    }
1423
 
    
1424
 
    if(quit_now){
1425
 
      goto end;
1426
 
    }
1427
 
    
1428
1222
    ret = start_mandos_communication(address, port, if_index, af);
1429
1223
    if(ret < 0){
1430
1224
      exitcode = EXIT_FAILURE;
1433
1227
    }
1434
1228
    goto end;
1435
1229
  }
1436
 
  
1437
 
  if(quit_now){
1438
 
    goto end;
1439
 
  }
1440
 
  
 
1230
    
1441
1231
  {
1442
1232
    AvahiServerConfig config;
1443
1233
    /* Do not publish any local Zeroconf records */
1464
1254
    goto end;
1465
1255
  }
1466
1256
  
1467
 
  if(quit_now){
1468
 
    goto end;
1469
 
  }
1470
 
  
1471
1257
  /* Create the Avahi service browser */
1472
1258
  sb = avahi_s_service_browser_new(mc.server, if_index,
1473
1259
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1479
1265
    goto end;
1480
1266
  }
1481
1267
  
1482
 
  if(quit_now){
1483
 
    goto end;
1484
 
  }
1485
 
  
1486
1268
  /* Run the main loop */
1487
1269
  
1488
1270
  if(debug){
1517
1299
    gpgme_release(mc.ctx);
1518
1300
  }
1519
1301
  
1520
 
  /* Take down the network interface */
1521
 
  if(take_down_interface){
1522
 
    /* Re-raise priviliges */
1523
 
    errno = 0;
1524
 
    ret = seteuid(0);
1525
 
    if(ret == -1){
1526
 
      perror("seteuid");
1527
 
    }
1528
 
    if(geteuid() == 0){
1529
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1530
 
      if(ret == -1){
1531
 
        perror("ioctl SIOCGIFFLAGS");
1532
 
      } else if(network.ifr_flags & IFF_UP) {
1533
 
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1534
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1535
 
        if(ret == -1){
1536
 
          perror("ioctl SIOCSIFFLAGS");
1537
 
        }
1538
 
      }
1539
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1540
 
      if(ret == -1){
1541
 
        perror("close");
1542
 
      }
1543
 
      /* Lower privileges permanently */
1544
 
      errno = 0;
1545
 
      ret = setuid(uid);
1546
 
      if(ret == -1){
1547
 
        perror("setuid");
1548
 
      }
1549
 
    }
1550
 
  }
1551
 
  
1552
1302
  /* Removes the temp directory used by GPGME */
1553
1303
  if(tempdir_created){
1554
1304
    DIR *d;
1593
1343
    }
1594
1344
  }
1595
1345
  
1596
 
  if(quit_now){
1597
 
    sigemptyset(&old_sigterm_action.sa_mask);
1598
 
    old_sigterm_action.sa_handler = SIG_DFL;
1599
 
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
1600
 
                                            &old_sigterm_action,
1601
 
                                            NULL));
1602
 
    if(ret == -1){
1603
 
      perror("sigaction");
1604
 
    }
1605
 
    do {
1606
 
      ret = raise(signal_received);
1607
 
    } while(ret != 0 and errno == EINTR);
1608
 
    if(ret != 0){
1609
 
      perror("raise");
1610
 
      abort();
1611
 
    }
1612
 
    TEMP_FAILURE_RETRY(pause());
1613
 
  }
1614
 
  
1615
1346
  return exitcode;
1616
1347
}