/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

* debian/control (Standards-Version): Changed to "3.8.1".

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
72
                                */
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
 
                                   getuid(), getgid(), seteuid(),
 
74
                                   getuid(), getgid(), setuid(),
75
75
                                   setgid() */
76
76
#include <arpa/inet.h>          /* inet_pton(), htons */
77
77
#include <iso646.h>             /* not, or, and */
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;
547
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
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
 
    retval = -1;
585
 
    goto mandos_end;
586
 
  }
587
 
  
588
 
  if(quit_now){
589
 
    retval = -1;
590
 
    goto mandos_end;
 
564
    return -1;
591
565
  }
592
566
  
593
567
  memset(&to, 0, sizeof(to));
594
568
  if(af == AF_INET6){
595
 
    to.in6.sin6_family = (sa_family_t)af;
 
569
    to.in6.sin6_family = (uint16_t)af;
596
570
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
597
571
  } else {                      /* IPv4 */
598
572
    to.in.sin_family = (sa_family_t)af;
600
574
  }
601
575
  if(ret < 0 ){
602
576
    perror("inet_pton");
603
 
    retval = -1;
604
 
    goto mandos_end;
 
577
    return -1;
605
578
  }
606
579
  if(ret == 0){
607
580
    fprintf(stderr, "Bad address: %s\n", ip);
608
 
    retval = -1;
609
 
    goto mandos_end;
 
581
    return -1;
610
582
  }
611
583
  if(af == AF_INET6){
612
584
    to.in6.sin6_port = htons(port); /* Spurious warnings from
619
591
      if(if_index == AVAHI_IF_UNSPEC){
620
592
        fprintf(stderr, "An IPv6 link-local address is incomplete"
621
593
                " without a network interface\n");
622
 
        retval = -1;
623
 
        goto mandos_end;
 
594
        return -1;
624
595
      }
625
596
      /* Set the network interface number as scope */
626
597
      to.in6.sin6_scope_id = (uint32_t)if_index;
631
602
                                     -Wunreachable-code */
632
603
  }
633
604
  
634
 
  if(quit_now){
635
 
    retval = -1;
636
 
    goto mandos_end;
637
 
  }
638
 
  
639
605
  if(debug){
640
606
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
641
607
      char interface[IF_NAMESIZE];
668
634
    }
669
635
  }
670
636
  
671
 
  if(quit_now){
672
 
    retval = -1;
673
 
    goto mandos_end;
674
 
  }
675
 
  
676
637
  if(af == AF_INET6){
677
638
    ret = connect(tcp_sd, &to.in6, sizeof(to));
678
639
  } else {
680
641
  }
681
642
  if(ret < 0){
682
643
    perror("connect");
683
 
    retval = -1;
684
 
    goto mandos_end;
685
 
  }
686
 
  
687
 
  if(quit_now){
688
 
    retval = -1;
689
 
    goto mandos_end;
 
644
    return -1;
690
645
  }
691
646
  
692
647
  const char *out = mandos_protocol_version;
711
666
        break;
712
667
      }
713
668
    }
714
 
  
715
 
    if(quit_now){
716
 
      retval = -1;
717
 
      goto mandos_end;
718
 
    }
719
669
  }
720
670
  
721
671
  if(debug){
722
672
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
723
673
  }
724
674
  
725
 
  if(quit_now){
726
 
    retval = -1;
727
 
    goto mandos_end;
728
 
  }
729
 
  
730
675
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
731
676
  
732
 
  if(quit_now){
733
 
    retval = -1;
734
 
    goto mandos_end;
735
 
  }
736
 
  
737
 
  do {
 
677
  do{
738
678
    ret = gnutls_handshake(session);
739
 
    if(quit_now){
740
 
      retval = -1;
741
 
      goto mandos_end;
742
 
    }
743
679
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
744
680
  
745
681
  if(ret != GNUTLS_E_SUCCESS){
759
695
  }
760
696
  
761
697
  while(true){
762
 
    
763
 
    if(quit_now){
764
 
      retval = -1;
765
 
      goto mandos_end;
766
 
    }
767
 
    
768
698
    buffer_capacity = incbuffer(&buffer, buffer_length,
769
699
                                   buffer_capacity);
770
700
    if(buffer_capacity == 0){
773
703
      goto mandos_end;
774
704
    }
775
705
    
776
 
    if(quit_now){
777
 
      retval = -1;
778
 
      goto mandos_end;
779
 
    }
780
 
    
781
706
    sret = gnutls_record_recv(session, buffer+buffer_length,
782
707
                              BUFFER_SIZE);
783
708
    if(sret == 0){
789
714
      case GNUTLS_E_AGAIN:
790
715
        break;
791
716
      case GNUTLS_E_REHANDSHAKE:
792
 
        do {
 
717
        do{
793
718
          ret = gnutls_handshake(session);
794
 
          
795
 
          if(quit_now){
796
 
            retval = -1;
797
 
            goto mandos_end;
798
 
          }
799
719
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
800
720
        if(ret < 0){
801
721
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
820
740
    fprintf(stderr, "Closing TLS session\n");
821
741
  }
822
742
  
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);
 
743
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
835
744
  
836
745
  if(buffer_length > 0){
837
 
    ssize_t decrypted_buffer_size;
838
746
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
839
747
                                               buffer_length,
840
748
                                               &decrypted_buffer);
841
749
    if(decrypted_buffer_size >= 0){
842
 
      
843
750
      written = 0;
844
751
      while(written < (size_t) decrypted_buffer_size){
845
 
        if(quit_now){
846
 
          retval = -1;
847
 
          goto mandos_end;
848
 
        }
849
 
        
850
752
        ret = (int)fwrite(decrypted_buffer + written, 1,
851
753
                          (size_t)decrypted_buffer_size - written,
852
754
                          stdout);
872
774
  
873
775
 mandos_end:
874
776
  free(buffer);
875
 
  if(tcp_sd >= 0){
876
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
877
 
  }
 
777
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
878
778
  if(ret == -1){
879
779
    perror("close");
880
780
  }
881
781
  gnutls_deinit(session);
882
 
  if(quit_now){
883
 
    retval = -1;
884
 
  }
885
782
  return retval;
886
783
}
887
784
 
904
801
  /* Called whenever a service has been resolved successfully or
905
802
     timed out */
906
803
  
907
 
  if(quit_now){
908
 
    return;
909
 
  }
910
 
  
911
804
  switch(event){
912
805
  default:
913
806
  case AVAHI_RESOLVER_FAILURE:
950
843
  /* Called whenever a new services becomes available on the LAN or
951
844
     is removed from the LAN */
952
845
  
953
 
  if(quit_now){
954
 
    return;
955
 
  }
956
 
  
957
846
  switch(event){
958
847
  default:
959
848
  case AVAHI_BROWSER_FAILURE:
988
877
  }
989
878
}
990
879
 
 
880
sig_atomic_t quit_now = 0;
 
881
 
991
882
/* stop main loop after sigterm has been called */
992
 
static void handle_sigterm(int sig){
 
883
static void handle_sigterm(__attribute__((unused)) int sig){
993
884
  if(quit_now){
994
885
    return;
995
886
  }
996
887
  quit_now = 1;
997
 
  signal_received = sig;
998
888
  int old_errno = errno;
999
889
  if(mc.simple_poll != NULL){
1000
890
    avahi_simple_poll_quit(mc.simple_poll);
1011
901
  int exitcode = EXIT_SUCCESS;
1012
902
  const char *interface = "eth0";
1013
903
  struct ifreq network;
1014
 
  int sd = -1;
1015
 
  bool take_down_interface = false;
 
904
  int sd;
1016
905
  uid_t uid;
1017
906
  gid_t gid;
1018
907
  char *connect_to = NULL;
1026
915
  bool gpgme_initialized = false;
1027
916
  float delay = 2.5f;
1028
917
  
1029
 
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
 
918
  struct sigaction old_sigterm_action;
1030
919
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1031
920
  
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
921
  {
1054
922
    struct argp_option options[] = {
1055
923
      { .name = "debug", .key = 128,
1182
1050
    exitcode = EXIT_FAILURE;
1183
1051
    goto end;
1184
1052
  }
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
 
  }
 
1053
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1054
  if(ret == -1){
 
1055
    perror("sigaction");
 
1056
    exitcode = EXIT_FAILURE;
 
1057
    goto end;
 
1058
  }  
1228
1059
  
1229
1060
  /* If the interface is down, bring it up */
1230
1061
  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
1062
#ifdef __linux__
1250
1063
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1251
1064
       messages to mess up the prompt */
1269
1082
        }
1270
1083
      }
1271
1084
#endif  /* __linux__ */
1272
 
      /* Lower privileges */
1273
 
      errno = 0;
1274
 
      ret = seteuid(uid);
1275
 
      if(ret == -1){
1276
 
        perror("seteuid");
1277
 
      }
1278
1085
      goto end;
1279
1086
    }
1280
1087
    strcpy(network.ifr_name, interface);
1290
1097
      }
1291
1098
#endif  /* __linux__ */
1292
1099
      exitcode = EXIT_FAILURE;
1293
 
      /* Lower privileges */
1294
 
      errno = 0;
1295
 
      ret = seteuid(uid);
1296
 
      if(ret == -1){
1297
 
        perror("seteuid");
1298
 
      }
1299
1100
      goto end;
1300
1101
    }
1301
1102
    if((network.ifr_flags & IFF_UP) == 0){
1302
1103
      network.ifr_flags |= IFF_UP;
1303
 
      take_down_interface = true;
1304
1104
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1305
1105
      if(ret == -1){
1306
 
        take_down_interface = false;
1307
1106
        perror("ioctl SIOCSIFFLAGS");
1308
1107
        exitcode = EXIT_FAILURE;
1309
1108
#ifdef __linux__
1314
1113
          }
1315
1114
        }
1316
1115
#endif  /* __linux__ */
1317
 
        /* Lower privileges */
1318
 
        errno = 0;
1319
 
        ret = seteuid(uid);
1320
 
        if(ret == -1){
1321
 
          perror("seteuid");
1322
 
        }
1323
1116
        goto end;
1324
1117
      }
1325
1118
    }
1337
1130
        perror("nanosleep");
1338
1131
      }
1339
1132
    }
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
 
      }
 
1133
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1134
    if(ret == -1){
 
1135
      perror("close");
1346
1136
    }
1347
1137
#ifdef __linux__
1348
1138
    if(restore_loglevel){
1353
1143
      }
1354
1144
    }
1355
1145
#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;
 
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");
1375
1160
  }
1376
1161
  
1377
1162
  ret = init_gnutls_global(pubkey, seckey);
1383
1168
    gnutls_initialized = true;
1384
1169
  }
1385
1170
  
1386
 
  if(quit_now){
1387
 
    goto end;
1388
 
  }
1389
 
  
1390
 
  tempdir_created = true;
1391
1171
  if(mkdtemp(tempdir) == NULL){
1392
 
    tempdir_created = false;
1393
1172
    perror("mkdtemp");
1394
1173
    goto end;
1395
1174
  }
1396
 
  
1397
 
  if(quit_now){
1398
 
    goto end;
1399
 
  }
 
1175
  tempdir_created = true;
1400
1176
  
1401
1177
  if(not init_gpgme(pubkey, seckey, tempdir)){
1402
1178
    fprintf(stderr, "init_gpgme failed\n");
1406
1182
    gpgme_initialized = true;
1407
1183
  }
1408
1184
  
1409
 
  if(quit_now){
1410
 
    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
    }
1411
1192
  }
1412
1193
  
1413
1194
  if(connect_to != NULL){
1419
1200
      exitcode = EXIT_FAILURE;
1420
1201
      goto end;
1421
1202
    }
1422
 
    
1423
 
    if(quit_now){
1424
 
      goto end;
1425
 
    }
1426
 
    
1427
1203
    uint16_t port;
1428
1204
    errno = 0;
1429
1205
    tmpmax = strtoimax(address+1, &tmp, 10);
1433
1209
      exitcode = EXIT_FAILURE;
1434
1210
      goto end;
1435
1211
    }
1436
 
  
1437
 
    if(quit_now){
1438
 
      goto end;
1439
 
    }
1440
 
    
1441
1212
    port = (uint16_t)tmpmax;
1442
1213
    *address = '\0';
1443
1214
    address = connect_to;
1448
1219
    } else {
1449
1220
      af = AF_INET;
1450
1221
    }
1451
 
    
1452
 
    if(quit_now){
1453
 
      goto end;
1454
 
    }
1455
 
    
1456
1222
    ret = start_mandos_communication(address, port, if_index, af);
1457
1223
    if(ret < 0){
1458
1224
      exitcode = EXIT_FAILURE;
1461
1227
    }
1462
1228
    goto end;
1463
1229
  }
1464
 
  
1465
 
  if(quit_now){
1466
 
    goto end;
1467
 
  }
1468
 
  
 
1230
    
1469
1231
  {
1470
1232
    AvahiServerConfig config;
1471
1233
    /* Do not publish any local Zeroconf records */
1492
1254
    goto end;
1493
1255
  }
1494
1256
  
1495
 
  if(quit_now){
1496
 
    goto end;
1497
 
  }
1498
 
  
1499
1257
  /* Create the Avahi service browser */
1500
1258
  sb = avahi_s_service_browser_new(mc.server, if_index,
1501
1259
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1507
1265
    goto end;
1508
1266
  }
1509
1267
  
1510
 
  if(quit_now){
1511
 
    goto end;
1512
 
  }
1513
 
  
1514
1268
  /* Run the main loop */
1515
1269
  
1516
1270
  if(debug){
1545
1299
    gpgme_release(mc.ctx);
1546
1300
  }
1547
1301
  
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
1302
  /* Removes the temp directory used by GPGME */
1581
1303
  if(tempdir_created){
1582
1304
    DIR *d;
1621
1343
    }
1622
1344
  }
1623
1345
  
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
1346
  return exitcode;
1635
1347
}