/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-13 05:38:21 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090213053821-03e696gckk4nbjps
Support not using IPv6 in server:

* mandos (AvahiService.__init__): Take new "protocol" parameter.  All
                                  callers changed.
  (IPv6_TCPServer.__init__): Take new "use_ipv6" parameter.  All
                             callers changed.
  (IPv6_TCPServer.server_bind): Create IPv4 socket if not using IPv6.
  (main): New "--no-ipv6" command line option.  New "use_ipv6" config
          option.
* mandos-options.xml ([@id="address"]): Document conditional IPv4
                                        address support.
  ([@id="ipv6"]): New paragraph.
* mandos.conf (use_ipv6): New config option.
* mandos.conf.xml (OPTIONS): Document new "use_dbus" option.
  (EXAMPLE): Changed to use IPv6 link-local address.  Added "use_ipv6"
             option.
* mandos.xml (SYNOPSIS): New "--no-ipv6" option.
  (OPTIONS): Document new "--no-ipv6" option.

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() */
164
160
 */
165
161
static bool init_gpgme(const char *seckey,
166
162
                       const char *pubkey, const char *tempdir){
 
163
  int ret;
167
164
  gpgme_error_t rc;
168
165
  gpgme_engine_info_t engine_info;
169
166
  
172
169
   * Helper function to insert pub and seckey to the engine keyring.
173
170
   */
174
171
  bool import_key(const char *filename){
175
 
    int ret;
176
172
    int fd;
177
173
    gpgme_data_t pgp_data;
178
174
    
249
245
    return false;
250
246
  }
251
247
  
252
 
  return true;
 
248
  return true; 
253
249
}
254
250
 
255
251
/* 
309
305
        }
310
306
        gpgme_recipient_t recipient;
311
307
        recipient = result->recipients;
312
 
        while(recipient != NULL){
313
 
          fprintf(stderr, "Public key algorithm: %s\n",
314
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
315
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
316
 
          fprintf(stderr, "Secret key available: %s\n",
317
 
                  recipient->status == GPG_ERR_NO_SECKEY
318
 
                  ? "No" : "Yes");
319
 
          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
          }
320
318
        }
321
319
      }
322
320
    }
474
472
static int init_gnutls_session(gnutls_session_t *session){
475
473
  int ret;
476
474
  /* GnuTLS session creation */
477
 
  do {
478
 
    ret = gnutls_init(session, GNUTLS_SERVER);
479
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
475
  ret = gnutls_init(session, GNUTLS_SERVER);
480
476
  if(ret != GNUTLS_E_SUCCESS){
481
477
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
482
478
            safer_gnutls_strerror(ret));
484
480
  
485
481
  {
486
482
    const char *err;
487
 
    do {
488
 
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
489
 
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
483
    ret = gnutls_priority_set_direct(*session, mc.priority, &err);
490
484
    if(ret != GNUTLS_E_SUCCESS){
491
485
      fprintf(stderr, "Syntax error at: %s\n", err);
492
486
      fprintf(stderr, "GnuTLS error: %s\n",
496
490
    }
497
491
  }
498
492
  
499
 
  do {
500
 
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
501
 
                                 mc.cred);
502
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
493
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
494
                               mc.cred);
503
495
  if(ret != GNUTLS_E_SUCCESS){
504
496
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
505
497
            safer_gnutls_strerror(ret));
508
500
  }
509
501
  
510
502
  /* ignore client certificate if any. */
511
 
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
 
503
  gnutls_certificate_server_set_request(*session,
 
504
                                        GNUTLS_CERT_IGNORE);
512
505
  
513
506
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
514
507
  
519
512
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
520
513
                      __attribute__((unused)) const char *txt){}
521
514
 
522
 
sig_atomic_t quit_now = 0;
523
 
int signal_received = 0;
524
 
 
525
515
/* Called when a Mandos server is found */
526
516
static int start_mandos_communication(const char *ip, uint16_t port,
527
517
                                      AvahiIfIndex if_index,
528
518
                                      int af){
529
 
  int ret, tcp_sd = -1;
 
519
  int ret, tcp_sd;
530
520
  ssize_t sret;
531
521
  union {
532
522
    struct sockaddr_in in;
536
526
  char *decrypted_buffer;
537
527
  size_t buffer_length = 0;
538
528
  size_t buffer_capacity = 0;
 
529
  ssize_t decrypted_buffer_size;
539
530
  size_t written;
540
531
  int retval = 0;
541
532
  gnutls_session_t session;
542
533
  int pf;                       /* Protocol family */
543
534
  
544
 
  if(quit_now){
545
 
    return -1;
546
 
  }
547
 
  
548
535
  switch(af){
549
536
  case AF_INET6:
550
537
    pf = PF_INET6;
570
557
  tcp_sd = socket(pf, SOCK_STREAM, 0);
571
558
  if(tcp_sd < 0){
572
559
    perror("socket");
573
 
    retval = -1;
574
 
    goto mandos_end;
575
 
  }
576
 
  
577
 
  if(quit_now){
578
 
    goto mandos_end;
 
560
    return -1;
579
561
  }
580
562
  
581
563
  memset(&to, 0, sizeof(to));
582
564
  if(af == AF_INET6){
583
 
    to.in6.sin6_family = (sa_family_t)af;
 
565
    to.in6.sin6_family = (uint16_t)af;
584
566
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
585
567
  } else {                      /* IPv4 */
586
568
    to.in.sin_family = (sa_family_t)af;
588
570
  }
589
571
  if(ret < 0 ){
590
572
    perror("inet_pton");
591
 
    retval = -1;
592
 
    goto mandos_end;
 
573
    return -1;
593
574
  }
594
575
  if(ret == 0){
595
576
    fprintf(stderr, "Bad address: %s\n", ip);
596
 
    retval = -1;
597
 
    goto mandos_end;
 
577
    return -1;
598
578
  }
599
579
  if(af == AF_INET6){
600
580
    to.in6.sin6_port = htons(port); /* Spurious warnings from
607
587
      if(if_index == AVAHI_IF_UNSPEC){
608
588
        fprintf(stderr, "An IPv6 link-local address is incomplete"
609
589
                " without a network interface\n");
610
 
        retval = -1;
611
 
        goto mandos_end;
 
590
        return -1;
612
591
      }
613
592
      /* Set the network interface number as scope */
614
593
      to.in6.sin6_scope_id = (uint32_t)if_index;
619
598
                                     -Wunreachable-code */
620
599
  }
621
600
  
622
 
  if(quit_now){
623
 
    goto mandos_end;
624
 
  }
625
 
  
626
601
  if(debug){
627
602
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
628
603
      char interface[IF_NAMESIZE];
655
630
    }
656
631
  }
657
632
  
658
 
  if(quit_now){
659
 
    goto mandos_end;
660
 
  }
661
 
  
662
633
  if(af == AF_INET6){
663
634
    ret = connect(tcp_sd, &to.in6, sizeof(to));
664
635
  } else {
666
637
  }
667
638
  if(ret < 0){
668
639
    perror("connect");
669
 
    retval = -1;
670
 
    goto mandos_end;
671
 
  }
672
 
  
673
 
  if(quit_now){
674
 
    goto mandos_end;
 
640
    return -1;
675
641
  }
676
642
  
677
643
  const char *out = mandos_protocol_version;
696
662
        break;
697
663
      }
698
664
    }
699
 
  
700
 
    if(quit_now){
701
 
      goto mandos_end;
702
 
    }
703
665
  }
704
666
  
705
667
  if(debug){
706
668
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
707
669
  }
708
670
  
709
 
  if(quit_now){
710
 
    goto mandos_end;
711
 
  }
712
 
  
713
671
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
714
672
  
715
 
  if(quit_now){
716
 
    goto mandos_end;
717
 
  }
718
 
  
719
 
  do {
 
673
  do{
720
674
    ret = gnutls_handshake(session);
721
 
    if(quit_now){
722
 
      goto mandos_end;
723
 
    }
724
675
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
725
676
  
726
677
  if(ret != GNUTLS_E_SUCCESS){
740
691
  }
741
692
  
742
693
  while(true){
743
 
    
744
 
    if(quit_now){
745
 
      goto mandos_end;
746
 
    }
747
 
    
748
694
    buffer_capacity = incbuffer(&buffer, buffer_length,
749
695
                                   buffer_capacity);
750
696
    if(buffer_capacity == 0){
753
699
      goto mandos_end;
754
700
    }
755
701
    
756
 
    if(quit_now){
757
 
      goto mandos_end;
758
 
    }
759
 
    
760
702
    sret = gnutls_record_recv(session, buffer+buffer_length,
761
703
                              BUFFER_SIZE);
762
704
    if(sret == 0){
768
710
      case GNUTLS_E_AGAIN:
769
711
        break;
770
712
      case GNUTLS_E_REHANDSHAKE:
771
 
        do {
 
713
        do{
772
714
          ret = gnutls_handshake(session);
773
 
          
774
 
          if(quit_now){
775
 
            goto mandos_end;
776
 
          }
777
715
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
778
716
        if(ret < 0){
779
717
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
798
736
    fprintf(stderr, "Closing TLS session\n");
799
737
  }
800
738
  
801
 
  if(quit_now){
802
 
    goto mandos_end;
803
 
  }
804
 
  
805
739
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
806
740
  
807
 
  if(quit_now){
808
 
    goto mandos_end;
809
 
  }
810
 
  
811
741
  if(buffer_length > 0){
812
 
    ssize_t decrypted_buffer_size;
813
742
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
814
743
                                               buffer_length,
815
744
                                               &decrypted_buffer);
816
745
    if(decrypted_buffer_size >= 0){
817
 
      
818
746
      written = 0;
819
747
      while(written < (size_t) decrypted_buffer_size){
820
 
        if(quit_now){
821
 
          goto mandos_end;
822
 
        }
823
 
        
824
748
        ret = (int)fwrite(decrypted_buffer + written, 1,
825
749
                          (size_t)decrypted_buffer_size - written,
826
750
                          stdout);
846
770
  
847
771
 mandos_end:
848
772
  free(buffer);
849
 
  if(tcp_sd >= 0){
850
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
851
 
  }
 
773
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
852
774
  if(ret == -1){
853
775
    perror("close");
854
776
  }
855
777
  gnutls_deinit(session);
856
 
  if(quit_now){
857
 
    retval = -1;
858
 
  }
859
778
  return retval;
860
779
}
861
780
 
878
797
  /* Called whenever a service has been resolved successfully or
879
798
     timed out */
880
799
  
881
 
  if(quit_now){
882
 
    return;
883
 
  }
884
 
  
885
800
  switch(event){
886
801
  default:
887
802
  case AVAHI_RESOLVER_FAILURE:
924
839
  /* Called whenever a new services becomes available on the LAN or
925
840
     is removed from the LAN */
926
841
  
927
 
  if(quit_now){
928
 
    return;
929
 
  }
930
 
  
931
842
  switch(event){
932
843
  default:
933
844
  case AVAHI_BROWSER_FAILURE:
962
873
  }
963
874
}
964
875
 
 
876
sig_atomic_t quit_now = 0;
 
877
 
965
878
/* stop main loop after sigterm has been called */
966
 
static void handle_sigterm(int sig){
 
879
static void handle_sigterm(__attribute__((unused)) int sig){
967
880
  if(quit_now){
968
881
    return;
969
882
  }
970
883
  quit_now = 1;
971
 
  signal_received = sig;
972
884
  int old_errno = errno;
973
885
  if(mc.simple_poll != NULL){
974
886
    avahi_simple_poll_quit(mc.simple_poll);
985
897
  int exitcode = EXIT_SUCCESS;
986
898
  const char *interface = "eth0";
987
899
  struct ifreq network;
988
 
  int sd = -1;
989
 
  bool take_down_interface = false;
 
900
  int sd;
990
901
  uid_t uid;
991
902
  gid_t gid;
992
903
  char *connect_to = NULL;
1000
911
  bool gpgme_initialized = false;
1001
912
  float delay = 2.5f;
1002
913
  
1003
 
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
 
914
  struct sigaction old_sigterm_action;
1004
915
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1005
916
  
1006
 
  uid = getuid();
1007
 
  gid = getgid();
1008
 
  
1009
 
  /* Lower any group privileges we might have, just to be safe */
1010
 
  errno = 0;
1011
 
  ret = setgid(gid);
1012
 
  if(ret == -1){
1013
 
    perror("setgid");
1014
 
  }
1015
 
  
1016
 
  /* Lower user privileges (temporarily) */
1017
 
  errno = 0;
1018
 
  ret = seteuid(uid);
1019
 
  if(ret == -1){
1020
 
    perror("seteuid");
1021
 
  }
1022
 
  
1023
 
  if(quit_now){
1024
 
    goto end;
1025
 
  }
1026
 
  
1027
917
  {
1028
918
    struct argp_option options[] = {
1029
919
      { .name = "debug", .key = 128,
1156
1046
    exitcode = EXIT_FAILURE;
1157
1047
    goto end;
1158
1048
  }
1159
 
  /* Need to check if the handler is SIG_IGN before handling:
1160
 
     | [[info:libc:Initial Signal Actions]] |
1161
 
     | [[info:libc:Basic Signal Handling]]  |
1162
 
  */
1163
 
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1164
 
  if(ret == -1){
1165
 
    perror("sigaction");
1166
 
    return EXIT_FAILURE;
1167
 
  }
1168
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1169
 
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1170
 
    if(ret == -1){
1171
 
      perror("sigaction");
1172
 
      exitcode = EXIT_FAILURE;
1173
 
      goto end;
1174
 
    }
1175
 
  }
1176
 
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1177
 
  if(ret == -1){
1178
 
    perror("sigaction");
1179
 
    return EXIT_FAILURE;
1180
 
  }
1181
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1182
 
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1183
 
    if(ret == -1){
1184
 
      perror("sigaction");
1185
 
      exitcode = EXIT_FAILURE;
1186
 
      goto end;
1187
 
    }
1188
 
  }
1189
 
  ret = sigaction(SIGTERM, 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(SIGTERM, &sigterm_action, NULL);
1196
 
    if(ret == -1){
1197
 
      perror("sigaction");
1198
 
      exitcode = EXIT_FAILURE;
1199
 
      goto end;
1200
 
    }
1201
 
  }
 
1049
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1050
  if(ret == -1){
 
1051
    perror("sigaction");
 
1052
    exitcode = EXIT_FAILURE;
 
1053
    goto end;
 
1054
  }  
1202
1055
  
1203
1056
  /* If the interface is down, bring it up */
1204
1057
  if(interface[0] != '\0'){
1205
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1206
 
    if(if_index == 0){
1207
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1208
 
      exitcode = EXIT_FAILURE;
1209
 
      goto end;
1210
 
    }
1211
 
    
1212
 
    if(quit_now){
1213
 
      goto end;
1214
 
    }
1215
 
    
1216
 
    /* Re-raise priviliges */
1217
 
    errno = 0;
1218
 
    ret = seteuid(0);
1219
 
    if(ret == -1){
1220
 
      perror("seteuid");
1221
 
    }
1222
 
    
1223
1058
#ifdef __linux__
1224
1059
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1225
1060
       messages to mess up the prompt */
1243
1078
        }
1244
1079
      }
1245
1080
#endif  /* __linux__ */
1246
 
      /* Lower privileges */
1247
 
      errno = 0;
1248
 
      ret = seteuid(uid);
1249
 
      if(ret == -1){
1250
 
        perror("seteuid");
1251
 
      }
1252
1081
      goto end;
1253
1082
    }
1254
1083
    strcpy(network.ifr_name, interface);
1264
1093
      }
1265
1094
#endif  /* __linux__ */
1266
1095
      exitcode = EXIT_FAILURE;
1267
 
      /* Lower privileges */
1268
 
      errno = 0;
1269
 
      ret = seteuid(uid);
1270
 
      if(ret == -1){
1271
 
        perror("seteuid");
1272
 
      }
1273
1096
      goto end;
1274
1097
    }
1275
1098
    if((network.ifr_flags & IFF_UP) == 0){
1276
1099
      network.ifr_flags |= IFF_UP;
1277
 
      take_down_interface = true;
1278
1100
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1279
1101
      if(ret == -1){
1280
 
        take_down_interface = false;
1281
1102
        perror("ioctl SIOCSIFFLAGS");
1282
1103
        exitcode = EXIT_FAILURE;
1283
1104
#ifdef __linux__
1288
1109
          }
1289
1110
        }
1290
1111
#endif  /* __linux__ */
1291
 
        /* Lower privileges */
1292
 
        errno = 0;
1293
 
        ret = seteuid(uid);
1294
 
        if(ret == -1){
1295
 
          perror("seteuid");
1296
 
        }
1297
1112
        goto end;
1298
1113
      }
1299
1114
    }
1311
1126
        perror("nanosleep");
1312
1127
      }
1313
1128
    }
1314
 
    if(not take_down_interface){
1315
 
      /* We won't need the socket anymore */
1316
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1317
 
      if(ret == -1){
1318
 
        perror("close");
1319
 
      }
 
1129
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1130
    if(ret == -1){
 
1131
      perror("close");
1320
1132
    }
1321
1133
#ifdef __linux__
1322
1134
    if(restore_loglevel){
1327
1139
      }
1328
1140
    }
1329
1141
#endif  /* __linux__ */
1330
 
    /* Lower privileges */
1331
 
    errno = 0;
1332
 
    if(take_down_interface){
1333
 
      /* Lower privileges */
1334
 
      ret = seteuid(uid);
1335
 
      if(ret == -1){
1336
 
        perror("seteuid");
1337
 
      }
1338
 
    } else {
1339
 
      /* Lower privileges permanently */
1340
 
      ret = setuid(uid);
1341
 
      if(ret == -1){
1342
 
        perror("setuid");
1343
 
      }
1344
 
    }
1345
 
  }
1346
 
  
1347
 
  if(quit_now){
1348
 
    goto end;
 
1142
  }
 
1143
  
 
1144
  uid = getuid();
 
1145
  gid = getgid();
 
1146
  
 
1147
  errno = 0;
 
1148
  setgid(gid);
 
1149
  if(ret == -1){
 
1150
    perror("setgid");
 
1151
  }
 
1152
  
 
1153
  ret = setuid(uid);
 
1154
  if(ret == -1){
 
1155
    perror("setuid");
1349
1156
  }
1350
1157
  
1351
1158
  ret = init_gnutls_global(pubkey, seckey);
1357
1164
    gnutls_initialized = true;
1358
1165
  }
1359
1166
  
1360
 
  if(quit_now){
1361
 
    goto end;
1362
 
  }
1363
 
  
1364
 
  tempdir_created = true;
1365
1167
  if(mkdtemp(tempdir) == NULL){
1366
 
    tempdir_created = false;
1367
1168
    perror("mkdtemp");
1368
1169
    goto end;
1369
1170
  }
1370
 
  
1371
 
  if(quit_now){
1372
 
    goto end;
1373
 
  }
 
1171
  tempdir_created = true;
1374
1172
  
1375
1173
  if(not init_gpgme(pubkey, seckey, tempdir)){
1376
1174
    fprintf(stderr, "init_gpgme failed\n");
1380
1178
    gpgme_initialized = true;
1381
1179
  }
1382
1180
  
1383
 
  if(quit_now){
1384
 
    goto end;
 
1181
  if(interface[0] != '\0'){
 
1182
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1183
    if(if_index == 0){
 
1184
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1185
      exitcode = EXIT_FAILURE;
 
1186
      goto end;
 
1187
    }
1385
1188
  }
1386
1189
  
1387
1190
  if(connect_to != NULL){
1393
1196
      exitcode = EXIT_FAILURE;
1394
1197
      goto end;
1395
1198
    }
1396
 
    
1397
 
    if(quit_now){
1398
 
      goto end;
1399
 
    }
1400
 
    
1401
1199
    uint16_t port;
1402
1200
    errno = 0;
1403
1201
    tmpmax = strtoimax(address+1, &tmp, 10);
1407
1205
      exitcode = EXIT_FAILURE;
1408
1206
      goto end;
1409
1207
    }
1410
 
  
1411
 
    if(quit_now){
1412
 
      goto end;
1413
 
    }
1414
 
    
1415
1208
    port = (uint16_t)tmpmax;
1416
1209
    *address = '\0';
1417
1210
    address = connect_to;
1422
1215
    } else {
1423
1216
      af = AF_INET;
1424
1217
    }
1425
 
    
1426
 
    if(quit_now){
1427
 
      goto end;
1428
 
    }
1429
 
    
1430
1218
    ret = start_mandos_communication(address, port, if_index, af);
1431
1219
    if(ret < 0){
1432
1220
      exitcode = EXIT_FAILURE;
1435
1223
    }
1436
1224
    goto end;
1437
1225
  }
1438
 
  
1439
 
  if(quit_now){
1440
 
    goto end;
1441
 
  }
1442
 
  
 
1226
    
1443
1227
  {
1444
1228
    AvahiServerConfig config;
1445
1229
    /* Do not publish any local Zeroconf records */
1466
1250
    goto end;
1467
1251
  }
1468
1252
  
1469
 
  if(quit_now){
1470
 
    goto end;
1471
 
  }
1472
 
  
1473
1253
  /* Create the Avahi service browser */
1474
1254
  sb = avahi_s_service_browser_new(mc.server, if_index,
1475
1255
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1481
1261
    goto end;
1482
1262
  }
1483
1263
  
1484
 
  if(quit_now){
1485
 
    goto end;
1486
 
  }
1487
 
  
1488
1264
  /* Run the main loop */
1489
1265
  
1490
1266
  if(debug){
1519
1295
    gpgme_release(mc.ctx);
1520
1296
  }
1521
1297
  
1522
 
  /* Take down the network interface */
1523
 
  if(take_down_interface){
1524
 
    /* Re-raise priviliges */
1525
 
    errno = 0;
1526
 
    ret = seteuid(0);
1527
 
    if(ret == -1){
1528
 
      perror("seteuid");
1529
 
    }
1530
 
    if(geteuid() == 0){
1531
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1532
 
      if(ret == -1){
1533
 
        perror("ioctl SIOCGIFFLAGS");
1534
 
      } else if(network.ifr_flags & IFF_UP) {
1535
 
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1536
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1537
 
        if(ret == -1){
1538
 
          perror("ioctl SIOCSIFFLAGS");
1539
 
        }
1540
 
      }
1541
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1542
 
      if(ret == -1){
1543
 
        perror("close");
1544
 
      }
1545
 
      /* Lower privileges permanently */
1546
 
      errno = 0;
1547
 
      ret = setuid(uid);
1548
 
      if(ret == -1){
1549
 
        perror("setuid");
1550
 
      }
1551
 
    }
1552
 
  }
1553
 
  
1554
1298
  /* Removes the temp directory used by GPGME */
1555
1299
  if(tempdir_created){
1556
1300
    DIR *d;
1595
1339
    }
1596
1340
  }
1597
1341
  
1598
 
  if(quit_now){
1599
 
    sigemptyset(&old_sigterm_action.sa_mask);
1600
 
    old_sigterm_action.sa_handler = SIG_DFL;
1601
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
1602
 
    if(ret == -1){
1603
 
      perror("sigaction");
1604
 
    }
1605
 
    raise(signal_received);
1606
 
  }
1607
 
  
1608
1342
  return exitcode;
1609
1343
}