/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-08-27 23:06:33 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090827230633-eyvww20kpem20il1
* mandos (ClientHandler.handle): Bug fix: add missing right
                                 parenthesis.

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
/* 
477
474
static int init_gnutls_session(gnutls_session_t *session){
478
475
  int ret;
479
476
  /* 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);
 
477
  ret = gnutls_init(session, GNUTLS_SERVER);
486
478
  if(ret != GNUTLS_E_SUCCESS){
487
479
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
488
480
            safer_gnutls_strerror(ret));
490
482
  
491
483
  {
492
484
    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);
 
485
    ret = gnutls_priority_set_direct(*session, mc.priority, &err);
500
486
    if(ret != GNUTLS_E_SUCCESS){
501
487
      fprintf(stderr, "Syntax error at: %s\n", err);
502
488
      fprintf(stderr, "GnuTLS error: %s\n",
506
492
    }
507
493
  }
508
494
  
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);
 
495
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
496
                               mc.cred);
517
497
  if(ret != GNUTLS_E_SUCCESS){
518
498
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
519
499
            safer_gnutls_strerror(ret));
522
502
  }
523
503
  
524
504
  /* ignore client certificate if any. */
525
 
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
 
505
  gnutls_certificate_server_set_request(*session,
 
506
                                        GNUTLS_CERT_IGNORE);
526
507
  
527
508
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
528
509
  
537
518
static int start_mandos_communication(const char *ip, uint16_t port,
538
519
                                      AvahiIfIndex if_index,
539
520
                                      int af){
540
 
  int ret, tcp_sd = -1;
 
521
  int ret, tcp_sd;
541
522
  ssize_t sret;
542
523
  union {
543
524
    struct sockaddr_in in;
544
525
    struct sockaddr_in6 in6;
545
526
  } to;
546
527
  char *buffer = NULL;
547
 
  char *decrypted_buffer = NULL;
 
528
  char *decrypted_buffer;
548
529
  size_t buffer_length = 0;
549
530
  size_t buffer_capacity = 0;
 
531
  ssize_t decrypted_buffer_size;
550
532
  size_t written;
551
 
  int retval = -1;
 
533
  int retval = 0;
552
534
  gnutls_session_t session;
553
535
  int pf;                       /* Protocol family */
554
536
  
555
 
  if(quit_now){
556
 
    return -1;
557
 
  }
558
 
  
559
537
  switch(af){
560
538
  case AF_INET6:
561
539
    pf = PF_INET6;
581
559
  tcp_sd = socket(pf, SOCK_STREAM, 0);
582
560
  if(tcp_sd < 0){
583
561
    perror("socket");
584
 
    goto mandos_end;
585
 
  }
586
 
  
587
 
  if(quit_now){
588
 
    goto mandos_end;
 
562
    return -1;
589
563
  }
590
564
  
591
565
  memset(&to, 0, sizeof(to));
598
572
  }
599
573
  if(ret < 0 ){
600
574
    perror("inet_pton");
601
 
    goto mandos_end;
 
575
    return -1;
602
576
  }
603
577
  if(ret == 0){
604
578
    fprintf(stderr, "Bad address: %s\n", ip);
605
 
    goto mandos_end;
 
579
    return -1;
606
580
  }
607
581
  if(af == AF_INET6){
608
582
    to.in6.sin6_port = htons(port); /* Spurious warnings from
615
589
      if(if_index == AVAHI_IF_UNSPEC){
616
590
        fprintf(stderr, "An IPv6 link-local address is incomplete"
617
591
                " without a network interface\n");
618
 
        goto mandos_end;
 
592
        return -1;
619
593
      }
620
594
      /* Set the network interface number as scope */
621
595
      to.in6.sin6_scope_id = (uint32_t)if_index;
626
600
                                     -Wunreachable-code */
627
601
  }
628
602
  
629
 
  if(quit_now){
630
 
    goto mandos_end;
631
 
  }
632
 
  
633
603
  if(debug){
634
604
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
635
605
      char interface[IF_NAMESIZE];
662
632
    }
663
633
  }
664
634
  
665
 
  if(quit_now){
666
 
    goto mandos_end;
667
 
  }
668
 
  
669
635
  if(af == AF_INET6){
670
636
    ret = connect(tcp_sd, &to.in6, sizeof(to));
671
637
  } else {
673
639
  }
674
640
  if(ret < 0){
675
641
    perror("connect");
676
 
    goto mandos_end;
677
 
  }
678
 
  
679
 
  if(quit_now){
680
 
    goto mandos_end;
 
642
    return -1;
681
643
  }
682
644
  
683
645
  const char *out = mandos_protocol_version;
688
650
                                   out_size - written));
689
651
    if(ret == -1){
690
652
      perror("write");
 
653
      retval = -1;
691
654
      goto mandos_end;
692
655
    }
693
656
    written += (size_t)ret;
701
664
        break;
702
665
      }
703
666
    }
704
 
  
705
 
    if(quit_now){
706
 
      goto mandos_end;
707
 
    }
708
667
  }
709
668
  
710
669
  if(debug){
711
670
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
712
671
  }
713
672
  
714
 
  if(quit_now){
715
 
    goto mandos_end;
716
 
  }
717
 
  
718
673
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
719
674
  
720
 
  if(quit_now){
721
 
    goto mandos_end;
722
 
  }
723
 
  
724
 
  do {
 
675
  do{
725
676
    ret = gnutls_handshake(session);
726
 
    if(quit_now){
727
 
      goto mandos_end;
728
 
    }
729
677
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
730
678
  
731
679
  if(ret != GNUTLS_E_SUCCESS){
733
681
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
734
682
      gnutls_perror(ret);
735
683
    }
 
684
    retval = -1;
736
685
    goto mandos_end;
737
686
  }
738
687
  
744
693
  }
745
694
  
746
695
  while(true){
747
 
    
748
 
    if(quit_now){
749
 
      goto mandos_end;
750
 
    }
751
 
    
752
696
    buffer_capacity = incbuffer(&buffer, buffer_length,
753
697
                                   buffer_capacity);
754
698
    if(buffer_capacity == 0){
755
699
      perror("incbuffer");
756
 
      goto mandos_end;
757
 
    }
758
 
    
759
 
    if(quit_now){
 
700
      retval = -1;
760
701
      goto mandos_end;
761
702
    }
762
703
    
771
712
      case GNUTLS_E_AGAIN:
772
713
        break;
773
714
      case GNUTLS_E_REHANDSHAKE:
774
 
        do {
 
715
        do{
775
716
          ret = gnutls_handshake(session);
776
 
          
777
 
          if(quit_now){
778
 
            goto mandos_end;
779
 
          }
780
717
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
781
718
        if(ret < 0){
782
719
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
783
720
          gnutls_perror(ret);
 
721
          retval = -1;
784
722
          goto mandos_end;
785
723
        }
786
724
        break;
787
725
      default:
788
726
        fprintf(stderr, "Unknown error while reading data from"
789
727
                " encrypted session with Mandos server\n");
 
728
        retval = -1;
790
729
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
791
730
        goto mandos_end;
792
731
      }
799
738
    fprintf(stderr, "Closing TLS session\n");
800
739
  }
801
740
  
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);
 
741
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
812
742
  
813
743
  if(buffer_length > 0){
814
 
    ssize_t decrypted_buffer_size;
815
744
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
816
745
                                               buffer_length,
817
746
                                               &decrypted_buffer);
818
747
    if(decrypted_buffer_size >= 0){
819
 
      
820
748
      written = 0;
821
749
      while(written < (size_t) decrypted_buffer_size){
822
 
        if(quit_now){
823
 
          goto mandos_end;
824
 
        }
825
 
        
826
750
        ret = (int)fwrite(decrypted_buffer + written, 1,
827
751
                          (size_t)decrypted_buffer_size - written,
828
752
                          stdout);
831
755
            fprintf(stderr, "Error writing encrypted data: %s\n",
832
756
                    strerror(errno));
833
757
          }
834
 
          goto mandos_end;
 
758
          retval = -1;
 
759
          break;
835
760
        }
836
761
        written += (size_t)ret;
837
762
      }
838
 
      retval = 0;
 
763
      free(decrypted_buffer);
 
764
    } else {
 
765
      retval = -1;
839
766
    }
 
767
  } else {
 
768
    retval = -1;
840
769
  }
841
770
  
842
771
  /* Shutdown procedure */
843
772
  
844
773
 mandos_end:
845
 
  free(decrypted_buffer);
846
774
  free(buffer);
847
 
  if(tcp_sd >= 0){
848
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
849
 
  }
 
775
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
850
776
  if(ret == -1){
851
777
    perror("close");
852
778
  }
853
779
  gnutls_deinit(session);
854
 
  if(quit_now){
855
 
    retval = -1;
856
 
  }
857
780
  return retval;
858
781
}
859
782
 
876
799
  /* Called whenever a service has been resolved successfully or
877
800
     timed out */
878
801
  
879
 
  if(quit_now){
880
 
    return;
881
 
  }
882
 
  
883
802
  switch(event){
884
803
  default:
885
804
  case AVAHI_RESOLVER_FAILURE:
922
841
  /* Called whenever a new services becomes available on the LAN or
923
842
     is removed from the LAN */
924
843
  
925
 
  if(quit_now){
926
 
    return;
927
 
  }
928
 
  
929
844
  switch(event){
930
845
  default:
931
846
  case AVAHI_BROWSER_FAILURE:
960
875
  }
961
876
}
962
877
 
 
878
sig_atomic_t quit_now = 0;
 
879
 
963
880
/* stop main loop after sigterm has been called */
964
 
static void handle_sigterm(int sig){
 
881
static void handle_sigterm(__attribute__((unused)) int sig){
965
882
  if(quit_now){
966
883
    return;
967
884
  }
968
885
  quit_now = 1;
969
 
  signal_received = sig;
970
886
  int old_errno = errno;
971
887
  if(mc.simple_poll != NULL){
972
888
    avahi_simple_poll_quit(mc.simple_poll);
984
900
  const char *interface = "eth0";
985
901
  struct ifreq network;
986
902
  int sd = -1;
987
 
  bool take_down_interface = false;
 
903
  bool interface_taken_up = false;
988
904
  uid_t uid;
989
905
  gid_t gid;
990
906
  char *connect_to = NULL;
998
914
  bool gpgme_initialized = false;
999
915
  float delay = 2.5f;
1000
916
  
1001
 
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
 
917
  struct sigaction old_sigterm_action;
1002
918
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1003
919
  
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
920
  {
1026
921
    struct argp_option options[] = {
1027
922
      { .name = "debug", .key = 128,
1154
1049
    exitcode = EXIT_FAILURE;
1155
1050
    goto end;
1156
1051
  }
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
 
  }
 
1052
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1053
  if(ret == -1){
 
1054
    perror("sigaction");
 
1055
    exitcode = EXIT_FAILURE;
 
1056
    goto end;
 
1057
  }  
1200
1058
  
1201
1059
  /* If the interface is down, bring it up */
1202
1060
  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
1061
#ifdef __linux__
1222
1062
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1223
1063
       messages to mess up the prompt */
1241
1081
        }
1242
1082
      }
1243
1083
#endif  /* __linux__ */
1244
 
      /* Lower privileges */
1245
 
      errno = 0;
1246
 
      ret = seteuid(uid);
1247
 
      if(ret == -1){
1248
 
        perror("seteuid");
1249
 
      }
1250
1084
      goto end;
1251
1085
    }
1252
1086
    strcpy(network.ifr_name, interface);
1262
1096
      }
1263
1097
#endif  /* __linux__ */
1264
1098
      exitcode = EXIT_FAILURE;
1265
 
      /* Lower privileges */
1266
 
      errno = 0;
1267
 
      ret = seteuid(uid);
1268
 
      if(ret == -1){
1269
 
        perror("seteuid");
1270
 
      }
1271
1099
      goto end;
1272
1100
    }
1273
1101
    if((network.ifr_flags & IFF_UP) == 0){
1274
1102
      network.ifr_flags |= IFF_UP;
1275
 
      take_down_interface = true;
1276
1103
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1277
1104
      if(ret == -1){
1278
 
        take_down_interface = false;
1279
1105
        perror("ioctl SIOCSIFFLAGS");
1280
1106
        exitcode = EXIT_FAILURE;
1281
1107
#ifdef __linux__
1286
1112
          }
1287
1113
        }
1288
1114
#endif  /* __linux__ */
1289
 
        /* Lower privileges */
1290
 
        errno = 0;
1291
 
        ret = seteuid(uid);
1292
 
        if(ret == -1){
1293
 
          perror("seteuid");
1294
 
        }
1295
1115
        goto end;
1296
1116
      }
 
1117
      interface_taken_up = true;
1297
1118
    }
1298
1119
    /* sleep checking until interface is running */
1299
1120
    for(int i=0; i < delay * 4; i++){
1309
1130
        perror("nanosleep");
1310
1131
      }
1311
1132
    }
1312
 
    if(not take_down_interface){
1313
 
      /* We won't need the socket anymore */
 
1133
    if(not interface_taken_up){
1314
1134
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1315
1135
      if(ret == -1){
1316
1136
        perror("close");
1325
1145
      }
1326
1146
    }
1327
1147
#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;
 
1148
  }
 
1149
  
 
1150
  uid = getuid();
 
1151
  gid = getgid();
 
1152
  
 
1153
  errno = 0;
 
1154
  setgid(gid);
 
1155
  if(ret == -1){
 
1156
    perror("setgid");
 
1157
  }
 
1158
  
 
1159
  ret = setuid(uid);
 
1160
  if(ret == -1){
 
1161
    perror("setuid");
1347
1162
  }
1348
1163
  
1349
1164
  ret = init_gnutls_global(pubkey, seckey);
1355
1170
    gnutls_initialized = true;
1356
1171
  }
1357
1172
  
1358
 
  if(quit_now){
1359
 
    goto end;
1360
 
  }
1361
 
  
1362
 
  tempdir_created = true;
1363
1173
  if(mkdtemp(tempdir) == NULL){
1364
 
    tempdir_created = false;
1365
1174
    perror("mkdtemp");
1366
1175
    goto end;
1367
1176
  }
1368
 
  
1369
 
  if(quit_now){
1370
 
    goto end;
1371
 
  }
 
1177
  tempdir_created = true;
1372
1178
  
1373
1179
  if(not init_gpgme(pubkey, seckey, tempdir)){
1374
1180
    fprintf(stderr, "init_gpgme failed\n");
1378
1184
    gpgme_initialized = true;
1379
1185
  }
1380
1186
  
1381
 
  if(quit_now){
1382
 
    goto end;
 
1187
  if(interface[0] != '\0'){
 
1188
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1189
    if(if_index == 0){
 
1190
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1191
      exitcode = EXIT_FAILURE;
 
1192
      goto end;
 
1193
    }
1383
1194
  }
1384
1195
  
1385
1196
  if(connect_to != NULL){
1391
1202
      exitcode = EXIT_FAILURE;
1392
1203
      goto end;
1393
1204
    }
1394
 
    
1395
 
    if(quit_now){
1396
 
      goto end;
1397
 
    }
1398
 
    
1399
1205
    uint16_t port;
1400
1206
    errno = 0;
1401
1207
    tmpmax = strtoimax(address+1, &tmp, 10);
1405
1211
      exitcode = EXIT_FAILURE;
1406
1212
      goto end;
1407
1213
    }
1408
 
  
1409
 
    if(quit_now){
1410
 
      goto end;
1411
 
    }
1412
 
    
1413
1214
    port = (uint16_t)tmpmax;
1414
1215
    *address = '\0';
1415
1216
    address = connect_to;
1420
1221
    } else {
1421
1222
      af = AF_INET;
1422
1223
    }
1423
 
    
1424
 
    if(quit_now){
1425
 
      goto end;
1426
 
    }
1427
 
    
1428
1224
    ret = start_mandos_communication(address, port, if_index, af);
1429
1225
    if(ret < 0){
1430
1226
      exitcode = EXIT_FAILURE;
1433
1229
    }
1434
1230
    goto end;
1435
1231
  }
1436
 
  
1437
 
  if(quit_now){
1438
 
    goto end;
1439
 
  }
1440
 
  
 
1232
    
1441
1233
  {
1442
1234
    AvahiServerConfig config;
1443
1235
    /* Do not publish any local Zeroconf records */
1464
1256
    goto end;
1465
1257
  }
1466
1258
  
1467
 
  if(quit_now){
1468
 
    goto end;
1469
 
  }
1470
 
  
1471
1259
  /* Create the Avahi service browser */
1472
1260
  sb = avahi_s_service_browser_new(mc.server, if_index,
1473
1261
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1479
1267
    goto end;
1480
1268
  }
1481
1269
  
1482
 
  if(quit_now){
1483
 
    goto end;
1484
 
  }
1485
 
  
1486
1270
  /* Run the main loop */
1487
1271
  
1488
1272
  if(debug){
1518
1302
  }
1519
1303
  
1520
1304
  /* Take down the network interface */
1521
 
  if(take_down_interface){
1522
 
    /* Re-raise priviliges */
1523
 
    errno = 0;
1524
 
    ret = seteuid(0);
 
1305
  if(interface_taken_up){
 
1306
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1525
1307
    if(ret == -1){
1526
 
      perror("seteuid");
 
1308
      perror("ioctl SIOCGIFFLAGS");
 
1309
    } else if(network.ifr_flags & IFF_UP) {
 
1310
      network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1311
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1312
      if(ret == -1){
 
1313
        perror("ioctl SIOCSIFFLAGS");
 
1314
      }
1527
1315
    }
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
 
      }
 
1316
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1317
    if(ret == -1){
 
1318
      perror("close");
1549
1319
    }
1550
1320
  }
1551
1321
  
1593
1363
    }
1594
1364
  }
1595
1365
  
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
1366
  return exitcode;
1616
1367
}