/mandos/release

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

« back to all changes in this revision

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

* mandos (MandosServer.handle_ipc): Better log message.
  (main/MandosDBusService.ClientNotFound): Add "address" argument.
                                           All callers changed.
* mandos-monitor (MandosClientWidget.__init__): Add "logger" argument.
  (MandosClientWidget.checker_completed,
  MandosClientWidget.checker_started, MandosClientWidget.got_secret,
  MandosClientWidget.rejected): New methods, connected to signals.
  (MandosClientWidget.update): Improve display.
  (UserInterface.client_not_found): New method, conneced to signal.
  (UserInterface.log_message): New; log with timestamp.
  (UserInterface.log_message_raw): Same as old "log_message".  Bug
                                  fix; always do "refresh()".

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
                                   stdout, ferror(), remove() */
44
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
 
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
 
                                   srand(), strtof() */
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
 
47
                                   strtof(), abort() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
49
#include <string.h>             /* memset(), strcmp(), strlen(),
50
50
                                   strerror(), asprintf(), strcpy() */
72
72
                                */
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
74
                                   getuid(), getgid(), seteuid(),
75
 
                                   setgid() */
 
75
                                   setgid(), pause() */
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
82
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
83
                                   sigaction(), SIGTERM, sig_atomic_t,
84
84
                                   raise() */
 
85
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
 
86
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
85
87
 
86
88
#ifdef __linux__
87
89
#include <sys/klog.h>           /* klogctl() */
142
144
                      .dh_bits = 1024, .priority = "SECURE256"
143
145
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
144
146
 
 
147
sig_atomic_t quit_now = 0;
 
148
int signal_received = 0;
 
149
 
145
150
/*
146
151
 * Make additional room in "buffer" for at least BUFFER_SIZE more
147
152
 * bytes. "buffer_capacity" is how much is currently allocated,
476
481
  /* GnuTLS session creation */
477
482
  do {
478
483
    ret = gnutls_init(session, GNUTLS_SERVER);
 
484
    if(quit_now){
 
485
      return -1;
 
486
    }
479
487
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
480
488
  if(ret != GNUTLS_E_SUCCESS){
481
489
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
486
494
    const char *err;
487
495
    do {
488
496
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
 
497
      if(quit_now){
 
498
        gnutls_deinit(*session);
 
499
        return -1;
 
500
      }
489
501
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
490
502
    if(ret != GNUTLS_E_SUCCESS){
491
503
      fprintf(stderr, "Syntax error at: %s\n", err);
499
511
  do {
500
512
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
501
513
                                 mc.cred);
 
514
    if(quit_now){
 
515
      gnutls_deinit(*session);
 
516
      return -1;
 
517
    }
502
518
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
503
519
  if(ret != GNUTLS_E_SUCCESS){
504
520
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
519
535
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
520
536
                      __attribute__((unused)) const char *txt){}
521
537
 
522
 
sig_atomic_t quit_now = 0;
523
 
int signal_received = 0;
524
 
 
525
538
/* Called when a Mandos server is found */
526
539
static int start_mandos_communication(const char *ip, uint16_t port,
527
540
                                      AvahiIfIndex if_index,
533
546
    struct sockaddr_in6 in6;
534
547
  } to;
535
548
  char *buffer = NULL;
536
 
  char *decrypted_buffer;
 
549
  char *decrypted_buffer = NULL;
537
550
  size_t buffer_length = 0;
538
551
  size_t buffer_capacity = 0;
539
552
  size_t written;
540
 
  int retval = 0;
 
553
  int retval = -1;
541
554
  gnutls_session_t session;
542
555
  int pf;                       /* Protocol family */
543
556
  
 
557
  errno = 0;
 
558
  
544
559
  if(quit_now){
 
560
    errno = EINTR;
545
561
    return -1;
546
562
  }
547
563
  
554
570
    break;
555
571
  default:
556
572
    fprintf(stderr, "Bad address family: %d\n", af);
 
573
    errno = EINVAL;
557
574
    return -1;
558
575
  }
559
576
  
569
586
  
570
587
  tcp_sd = socket(pf, SOCK_STREAM, 0);
571
588
  if(tcp_sd < 0){
 
589
    int e = errno;
572
590
    perror("socket");
573
 
    retval = -1;
 
591
    errno = e;
574
592
    goto mandos_end;
575
593
  }
576
594
  
577
595
  if(quit_now){
 
596
    errno = EINTR;
578
597
    goto mandos_end;
579
598
  }
580
599
  
587
606
    ret = inet_pton(af, ip, &to.in.sin_addr);
588
607
  }
589
608
  if(ret < 0 ){
 
609
    int e = errno;
590
610
    perror("inet_pton");
591
 
    retval = -1;
 
611
    errno = e;
592
612
    goto mandos_end;
593
613
  }
594
614
  if(ret == 0){
 
615
    int e = errno;
595
616
    fprintf(stderr, "Bad address: %s\n", ip);
596
 
    retval = -1;
 
617
    errno = e;
597
618
    goto mandos_end;
598
619
  }
599
620
  if(af == AF_INET6){
607
628
      if(if_index == AVAHI_IF_UNSPEC){
608
629
        fprintf(stderr, "An IPv6 link-local address is incomplete"
609
630
                " without a network interface\n");
610
 
        retval = -1;
 
631
        errno = EINVAL;
611
632
        goto mandos_end;
612
633
      }
613
634
      /* Set the network interface number as scope */
620
641
  }
621
642
  
622
643
  if(quit_now){
 
644
    errno = EINTR;
623
645
    goto mandos_end;
624
646
  }
625
647
  
656
678
  }
657
679
  
658
680
  if(quit_now){
 
681
    errno = EINTR;
659
682
    goto mandos_end;
660
683
  }
661
684
  
665
688
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
666
689
  }
667
690
  if(ret < 0){
 
691
    int e = errno;
668
692
    perror("connect");
669
 
    retval = -1;
 
693
    errno = e;
670
694
    goto mandos_end;
671
695
  }
672
696
  
673
697
  if(quit_now){
 
698
    errno = EINTR;
674
699
    goto mandos_end;
675
700
  }
676
701
  
681
706
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
682
707
                                   out_size - written));
683
708
    if(ret == -1){
 
709
      int e = errno;
684
710
      perror("write");
685
 
      retval = -1;
 
711
      errno = e;
686
712
      goto mandos_end;
687
713
    }
688
714
    written += (size_t)ret;
698
724
    }
699
725
  
700
726
    if(quit_now){
 
727
      errno = EINTR;
701
728
      goto mandos_end;
702
729
    }
703
730
  }
707
734
  }
708
735
  
709
736
  if(quit_now){
 
737
    errno = EINTR;
710
738
    goto mandos_end;
711
739
  }
712
740
  
713
741
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
714
742
  
715
743
  if(quit_now){
 
744
    errno = EINTR;
716
745
    goto mandos_end;
717
746
  }
718
747
  
719
748
  do {
720
749
    ret = gnutls_handshake(session);
721
750
    if(quit_now){
 
751
      errno = EINTR;
722
752
      goto mandos_end;
723
753
    }
724
754
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
728
758
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
729
759
      gnutls_perror(ret);
730
760
    }
731
 
    retval = -1;
 
761
    errno = EPROTO;
732
762
    goto mandos_end;
733
763
  }
734
764
  
742
772
  while(true){
743
773
    
744
774
    if(quit_now){
 
775
      errno = EINTR;
745
776
      goto mandos_end;
746
777
    }
747
778
    
748
779
    buffer_capacity = incbuffer(&buffer, buffer_length,
749
780
                                   buffer_capacity);
750
781
    if(buffer_capacity == 0){
 
782
      int e = errno;
751
783
      perror("incbuffer");
752
 
      retval = -1;
 
784
      errno = e;
753
785
      goto mandos_end;
754
786
    }
755
787
    
756
788
    if(quit_now){
 
789
      errno = EINTR;
757
790
      goto mandos_end;
758
791
    }
759
792
    
772
805
          ret = gnutls_handshake(session);
773
806
          
774
807
          if(quit_now){
 
808
            errno = EINTR;
775
809
            goto mandos_end;
776
810
          }
777
811
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
778
812
        if(ret < 0){
779
813
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
780
814
          gnutls_perror(ret);
781
 
          retval = -1;
 
815
          errno = EPROTO;
782
816
          goto mandos_end;
783
817
        }
784
818
        break;
785
819
      default:
786
820
        fprintf(stderr, "Unknown error while reading data from"
787
821
                " encrypted session with Mandos server\n");
788
 
        retval = -1;
789
822
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
823
        errno = EIO;
790
824
        goto mandos_end;
791
825
      }
792
826
    } else {
799
833
  }
800
834
  
801
835
  if(quit_now){
802
 
    goto mandos_end;
803
 
  }
804
 
  
805
 
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
806
 
  
807
 
  if(quit_now){
808
 
    goto mandos_end;
809
 
  }
 
836
    errno = EINTR;
 
837
    goto mandos_end;
 
838
  }
 
839
  
 
840
  do {
 
841
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
842
    if(quit_now){
 
843
      errno = EINTR;
 
844
      goto mandos_end;
 
845
    }
 
846
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
810
847
  
811
848
  if(buffer_length > 0){
812
849
    ssize_t decrypted_buffer_size;
818
855
      written = 0;
819
856
      while(written < (size_t) decrypted_buffer_size){
820
857
        if(quit_now){
 
858
          errno = EINTR;
821
859
          goto mandos_end;
822
860
        }
823
861
        
825
863
                          (size_t)decrypted_buffer_size - written,
826
864
                          stdout);
827
865
        if(ret == 0 and ferror(stdout)){
 
866
          int e = errno;
828
867
          if(debug){
829
868
            fprintf(stderr, "Error writing encrypted data: %s\n",
830
869
                    strerror(errno));
831
870
          }
832
 
          retval = -1;
833
 
          break;
 
871
          errno = e;
 
872
          goto mandos_end;
834
873
        }
835
874
        written += (size_t)ret;
836
875
      }
837
 
      free(decrypted_buffer);
838
 
    } else {
839
 
      retval = -1;
 
876
      retval = 0;
840
877
    }
841
 
  } else {
842
 
    retval = -1;
843
878
  }
844
879
  
845
880
  /* Shutdown procedure */
846
881
  
847
882
 mandos_end:
848
 
  free(buffer);
849
 
  if(tcp_sd >= 0){
850
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
851
 
  }
852
 
  if(ret == -1){
853
 
    perror("close");
854
 
  }
855
 
  gnutls_deinit(session);
856
 
  if(quit_now){
857
 
    retval = -1;
 
883
  {
 
884
    int e = errno;
 
885
    free(decrypted_buffer);
 
886
    free(buffer);
 
887
    if(tcp_sd >= 0){
 
888
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
889
    }
 
890
    if(ret == -1){
 
891
      if(e == 0){
 
892
        e = errno;
 
893
      }
 
894
      perror("close");
 
895
    }
 
896
    gnutls_deinit(session);
 
897
    if(quit_now){
 
898
      e = EINTR;
 
899
      retval = -1;
 
900
    }
 
901
    errno = e;
858
902
  }
859
903
  return retval;
860
904
}
1058
1102
        .arg = "SECONDS",
1059
1103
        .doc = "Maximum delay to wait for interface startup",
1060
1104
        .group = 2 },
 
1105
      /*
 
1106
       * These reproduce what we would get without ARGP_NO_HELP
 
1107
       */
 
1108
      { .name = "help", .key = '?',
 
1109
        .doc = "Give this help list", .group = -1 },
 
1110
      { .name = "usage", .key = -3,
 
1111
        .doc = "Give a short usage message", .group = -1 },
 
1112
      { .name = "version", .key = 'V',
 
1113
        .doc = "Print program version", .group = -1 },
1061
1114
      { .name = NULL }
1062
1115
    };
1063
1116
    
1064
1117
    error_t parse_opt(int key, char *arg,
1065
1118
                      struct argp_state *state){
 
1119
      errno = 0;
1066
1120
      switch(key){
1067
1121
      case 128:                 /* --debug */
1068
1122
        debug = true;
1084
1138
        tmpmax = strtoimax(arg, &tmp, 10);
1085
1139
        if(errno != 0 or tmp == arg or *tmp != '\0'
1086
1140
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1087
 
          fprintf(stderr, "Bad number of DH bits\n");
1088
 
          exit(EXIT_FAILURE);
 
1141
          argp_error(state, "Bad number of DH bits");
1089
1142
        }
1090
1143
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1091
1144
        break;
1096
1149
        errno = 0;
1097
1150
        delay = strtof(arg, &tmp);
1098
1151
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1099
 
          fprintf(stderr, "Bad delay\n");
1100
 
          exit(EXIT_FAILURE);
 
1152
          argp_error(state, "Bad delay");
1101
1153
        }
1102
1154
        break;
1103
 
      case ARGP_KEY_ARG:
1104
 
        argp_usage(state);
1105
 
      case ARGP_KEY_END:
 
1155
        /*
 
1156
         * These reproduce what we would get without ARGP_NO_HELP
 
1157
         */
 
1158
      case '?':                 /* --help */
 
1159
        argp_state_help(state, state->out_stream,
 
1160
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
 
1161
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
 
1162
      case -3:                  /* --usage */
 
1163
        argp_state_help(state, state->out_stream,
 
1164
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
 
1165
      case 'V':                 /* --version */
 
1166
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
1167
        exit(argp_err_exit_status);
1106
1168
        break;
1107
1169
      default:
1108
1170
        return ARGP_ERR_UNKNOWN;
1109
1171
      }
1110
 
      return 0;
 
1172
      return errno;
1111
1173
    }
1112
1174
    
1113
1175
    struct argp argp = { .options = options, .parser = parse_opt,
1114
1176
                         .args_doc = "",
1115
1177
                         .doc = "Mandos client -- Get and decrypt"
1116
1178
                         " passwords from a Mandos server" };
1117
 
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1118
 
    if(ret == ARGP_ERR_UNKNOWN){
1119
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
1120
 
      exitcode = EXIT_FAILURE;
 
1179
    ret = argp_parse(&argp, argc, argv,
 
1180
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
1181
    switch(ret){
 
1182
    case 0:
 
1183
      break;
 
1184
    case ENOMEM:
 
1185
    default:
 
1186
      errno = ret;
 
1187
      perror("argp_parse");
 
1188
      exitcode = EX_OSERR;
 
1189
      goto end;
 
1190
    case EINVAL:
 
1191
      exitcode = EX_USAGE;
1121
1192
      goto end;
1122
1193
    }
1123
1194
  }
1133
1204
  mc.simple_poll = avahi_simple_poll_new();
1134
1205
  if(mc.simple_poll == NULL){
1135
1206
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1136
 
    exitcode = EXIT_FAILURE;
 
1207
    exitcode = EX_UNAVAILABLE;
1137
1208
    goto end;
1138
1209
  }
1139
1210
  
1141
1212
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1142
1213
  if(ret == -1){
1143
1214
    perror("sigaddset");
1144
 
    exitcode = EXIT_FAILURE;
 
1215
    exitcode = EX_OSERR;
1145
1216
    goto end;
1146
1217
  }
1147
1218
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1148
1219
  if(ret == -1){
1149
1220
    perror("sigaddset");
1150
 
    exitcode = EXIT_FAILURE;
 
1221
    exitcode = EX_OSERR;
1151
1222
    goto end;
1152
1223
  }
1153
1224
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1154
1225
  if(ret == -1){
1155
1226
    perror("sigaddset");
1156
 
    exitcode = EXIT_FAILURE;
 
1227
    exitcode = EX_OSERR;
1157
1228
    goto end;
1158
1229
  }
1159
1230
  /* Need to check if the handler is SIG_IGN before handling:
1163
1234
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1164
1235
  if(ret == -1){
1165
1236
    perror("sigaction");
1166
 
    return EXIT_FAILURE;
 
1237
    return EX_OSERR;
1167
1238
  }
1168
1239
  if(old_sigterm_action.sa_handler != SIG_IGN){
1169
1240
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1170
1241
    if(ret == -1){
1171
1242
      perror("sigaction");
1172
 
      exitcode = EXIT_FAILURE;
 
1243
      exitcode = EX_OSERR;
1173
1244
      goto end;
1174
1245
    }
1175
1246
  }
1176
1247
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1177
1248
  if(ret == -1){
1178
1249
    perror("sigaction");
1179
 
    return EXIT_FAILURE;
 
1250
    return EX_OSERR;
1180
1251
  }
1181
1252
  if(old_sigterm_action.sa_handler != SIG_IGN){
1182
1253
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1183
1254
    if(ret == -1){
1184
1255
      perror("sigaction");
1185
 
      exitcode = EXIT_FAILURE;
 
1256
      exitcode = EX_OSERR;
1186
1257
      goto end;
1187
1258
    }
1188
1259
  }
1189
1260
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1190
1261
  if(ret == -1){
1191
1262
    perror("sigaction");
1192
 
    return EXIT_FAILURE;
 
1263
    return EX_OSERR;
1193
1264
  }
1194
1265
  if(old_sigterm_action.sa_handler != SIG_IGN){
1195
1266
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1196
1267
    if(ret == -1){
1197
1268
      perror("sigaction");
1198
 
      exitcode = EXIT_FAILURE;
 
1269
      exitcode = EX_OSERR;
1199
1270
      goto end;
1200
1271
    }
1201
1272
  }
1205
1276
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1206
1277
    if(if_index == 0){
1207
1278
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1208
 
      exitcode = EXIT_FAILURE;
 
1279
      exitcode = EX_UNAVAILABLE;
1209
1280
      goto end;
1210
1281
    }
1211
1282
    
1222
1293
    
1223
1294
#ifdef __linux__
1224
1295
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1225
 
       messages to mess up the prompt */
 
1296
       messages about the network interface to mess up the prompt */
1226
1297
    ret = klogctl(8, NULL, 5);
1227
1298
    bool restore_loglevel = true;
1228
1299
    if(ret == -1){
1234
1305
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1235
1306
    if(sd < 0){
1236
1307
      perror("socket");
1237
 
      exitcode = EXIT_FAILURE;
 
1308
      exitcode = EX_OSERR;
1238
1309
#ifdef __linux__
1239
1310
      if(restore_loglevel){
1240
1311
        ret = klogctl(7, NULL, 0);
1263
1334
        }
1264
1335
      }
1265
1336
#endif  /* __linux__ */
1266
 
      exitcode = EXIT_FAILURE;
 
1337
      exitcode = EX_OSERR;
1267
1338
      /* Lower privileges */
1268
1339
      errno = 0;
1269
1340
      ret = seteuid(uid);
1279
1350
      if(ret == -1){
1280
1351
        take_down_interface = false;
1281
1352
        perror("ioctl SIOCSIFFLAGS");
1282
 
        exitcode = EXIT_FAILURE;
 
1353
        exitcode = EX_OSERR;
1283
1354
#ifdef __linux__
1284
1355
        if(restore_loglevel){
1285
1356
          ret = klogctl(7, NULL, 0);
1351
1422
  ret = init_gnutls_global(pubkey, seckey);
1352
1423
  if(ret == -1){
1353
1424
    fprintf(stderr, "init_gnutls_global failed\n");
1354
 
    exitcode = EXIT_FAILURE;
 
1425
    exitcode = EX_UNAVAILABLE;
1355
1426
    goto end;
1356
1427
  } else {
1357
1428
    gnutls_initialized = true;
1374
1445
  
1375
1446
  if(not init_gpgme(pubkey, seckey, tempdir)){
1376
1447
    fprintf(stderr, "init_gpgme failed\n");
1377
 
    exitcode = EXIT_FAILURE;
 
1448
    exitcode = EX_UNAVAILABLE;
1378
1449
    goto end;
1379
1450
  } else {
1380
1451
    gpgme_initialized = true;
1390
1461
    char *address = strrchr(connect_to, ':');
1391
1462
    if(address == NULL){
1392
1463
      fprintf(stderr, "No colon in address\n");
1393
 
      exitcode = EXIT_FAILURE;
 
1464
      exitcode = EX_USAGE;
1394
1465
      goto end;
1395
1466
    }
1396
1467
    
1404
1475
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1405
1476
       or tmpmax != (uint16_t)tmpmax){
1406
1477
      fprintf(stderr, "Bad port number\n");
1407
 
      exitcode = EXIT_FAILURE;
 
1478
      exitcode = EX_USAGE;
1408
1479
      goto end;
1409
1480
    }
1410
1481
  
1429
1500
    
1430
1501
    ret = start_mandos_communication(address, port, if_index, af);
1431
1502
    if(ret < 0){
1432
 
      exitcode = EXIT_FAILURE;
 
1503
      switch(errno){
 
1504
      case ENETUNREACH:
 
1505
      case EHOSTDOWN:
 
1506
      case EHOSTUNREACH:
 
1507
        exitcode = EX_NOHOST;
 
1508
        break;
 
1509
      case EINVAL:
 
1510
        exitcode = EX_USAGE;
 
1511
        break;
 
1512
      case EIO:
 
1513
        exitcode = EX_IOERR;
 
1514
        break;
 
1515
      case EPROTO:
 
1516
        exitcode = EX_PROTOCOL;
 
1517
        break;
 
1518
      default:
 
1519
        exitcode = EX_OSERR;
 
1520
        break;
 
1521
      }
1433
1522
    } else {
1434
1523
      exitcode = EXIT_SUCCESS;
1435
1524
    }
1462
1551
  if(mc.server == NULL){
1463
1552
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1464
1553
            avahi_strerror(error));
1465
 
    exitcode = EXIT_FAILURE;
 
1554
    exitcode = EX_UNAVAILABLE;
1466
1555
    goto end;
1467
1556
  }
1468
1557
  
1477
1566
  if(sb == NULL){
1478
1567
    fprintf(stderr, "Failed to create service browser: %s\n",
1479
1568
            avahi_strerror(avahi_server_errno(mc.server)));
1480
 
    exitcode = EXIT_FAILURE;
 
1569
    exitcode = EX_UNAVAILABLE;
1481
1570
    goto end;
1482
1571
  }
1483
1572
  
1532
1621
      if(ret == -1){
1533
1622
        perror("ioctl SIOCGIFFLAGS");
1534
1623
      } else if(network.ifr_flags & IFF_UP) {
1535
 
        network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1624
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1536
1625
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1537
1626
        if(ret == -1){
1538
1627
          perror("ioctl SIOCSIFFLAGS");
1598
1687
  if(quit_now){
1599
1688
    sigemptyset(&old_sigterm_action.sa_mask);
1600
1689
    old_sigterm_action.sa_handler = SIG_DFL;
1601
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
 
1690
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
1691
                                            &old_sigterm_action,
 
1692
                                            NULL));
1602
1693
    if(ret == -1){
1603
1694
      perror("sigaction");
1604
1695
    }
1605
 
    raise(signal_received);
 
1696
    do {
 
1697
      ret = raise(signal_received);
 
1698
    } while(ret != 0 and errno == EINTR);
 
1699
    if(ret != 0){
 
1700
      perror("raise");
 
1701
      abort();
 
1702
    }
 
1703
    TEMP_FAILURE_RETRY(pause());
1606
1704
  }
1607
1705
  
1608
1706
  return exitcode;