/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-10-30 16:23:43 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091030162343-1p2a8bf3gc084kc9
* plugins.d/password-prompt.c: Use environment variables and prompt
                               text from cryptsetup 1.1.
* plugins.d/password-prompt.xml (ENVIRONMENT): Document change in
                                               environment variables
                                               used.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2011 Teddy Hogeborn
13
 
 * Copyright © 2008-2011 Björn Påhlsson
 
12
 * Copyright © 2008,2009 Teddy Hogeborn
 
13
 * Copyright © 2008,2009 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
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, srand(),
47
 
                                   strtof(), abort() */
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
 
47
                                   srand(), strtof(), abort() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
49
#include <string.h>             /* memset(), strcmp(), strlen(),
50
50
                                   strerror(), asprintf(), strcpy() */
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
65
#include <errno.h>              /* perror(), errno */
66
 
#include <time.h>               /* nanosleep(), time(), sleep() */
 
66
#include <time.h>               /* nanosleep(), time() */
67
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
68
                                   SIOCSIFFLAGS, if_indextoname(),
69
69
                                   if_nametoindex(), IF_NAMESIZE */
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
#include <sysexits.h>           /* EX_OSERR, EX_USAGE */
87
86
 
88
87
#ifdef __linux__
89
88
#include <sys/klog.h>           /* klogctl() */
127
126
static const char mandos_protocol_version[] = "1";
128
127
const char *argp_program_version = "mandos-client " VERSION;
129
128
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
130
 
static const char sys_class_net[] = "/sys/class/net";
131
 
char *connect_to = NULL;
132
129
 
133
130
/* Used for passing in values through the Avahi callback functions */
134
131
typedef struct {
556
553
  gnutls_session_t session;
557
554
  int pf;                       /* Protocol family */
558
555
  
559
 
  errno = 0;
560
 
  
561
556
  if(quit_now){
562
 
    errno = EINTR;
563
557
    return -1;
564
558
  }
565
559
  
572
566
    break;
573
567
  default:
574
568
    fprintf(stderr, "Bad address family: %d\n", af);
575
 
    errno = EINVAL;
576
569
    return -1;
577
570
  }
578
571
  
588
581
  
589
582
  tcp_sd = socket(pf, SOCK_STREAM, 0);
590
583
  if(tcp_sd < 0){
591
 
    int e = errno;
592
584
    perror("socket");
593
 
    errno = e;
594
585
    goto mandos_end;
595
586
  }
596
587
  
597
588
  if(quit_now){
598
 
    errno = EINTR;
599
589
    goto mandos_end;
600
590
  }
601
591
  
608
598
    ret = inet_pton(af, ip, &to.in.sin_addr);
609
599
  }
610
600
  if(ret < 0 ){
611
 
    int e = errno;
612
601
    perror("inet_pton");
613
 
    errno = e;
614
602
    goto mandos_end;
615
603
  }
616
604
  if(ret == 0){
617
 
    int e = errno;
618
605
    fprintf(stderr, "Bad address: %s\n", ip);
619
 
    errno = e;
620
606
    goto mandos_end;
621
607
  }
622
608
  if(af == AF_INET6){
630
616
      if(if_index == AVAHI_IF_UNSPEC){
631
617
        fprintf(stderr, "An IPv6 link-local address is incomplete"
632
618
                " without a network interface\n");
633
 
        errno = EINVAL;
634
619
        goto mandos_end;
635
620
      }
636
621
      /* Set the network interface number as scope */
643
628
  }
644
629
  
645
630
  if(quit_now){
646
 
    errno = EINTR;
647
631
    goto mandos_end;
648
632
  }
649
633
  
680
664
  }
681
665
  
682
666
  if(quit_now){
683
 
    errno = EINTR;
684
667
    goto mandos_end;
685
668
  }
686
669
  
690
673
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
691
674
  }
692
675
  if(ret < 0){
693
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
694
 
      int e = errno;
695
 
      perror("connect");
696
 
      errno = e;
697
 
    }
 
676
    perror("connect");
698
677
    goto mandos_end;
699
678
  }
700
679
  
701
680
  if(quit_now){
702
 
    errno = EINTR;
703
681
    goto mandos_end;
704
682
  }
705
683
  
710
688
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
711
689
                                   out_size - written));
712
690
    if(ret == -1){
713
 
      int e = errno;
714
691
      perror("write");
715
 
      errno = e;
716
692
      goto mandos_end;
717
693
    }
718
694
    written += (size_t)ret;
728
704
    }
729
705
  
730
706
    if(quit_now){
731
 
      errno = EINTR;
732
707
      goto mandos_end;
733
708
    }
734
709
  }
738
713
  }
739
714
  
740
715
  if(quit_now){
741
 
    errno = EINTR;
742
716
    goto mandos_end;
743
717
  }
744
718
  
745
719
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
746
720
  
747
721
  if(quit_now){
748
 
    errno = EINTR;
749
722
    goto mandos_end;
750
723
  }
751
724
  
752
725
  do {
753
726
    ret = gnutls_handshake(session);
754
727
    if(quit_now){
755
 
      errno = EINTR;
756
728
      goto mandos_end;
757
729
    }
758
730
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
762
734
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
763
735
      gnutls_perror(ret);
764
736
    }
765
 
    errno = EPROTO;
766
737
    goto mandos_end;
767
738
  }
768
739
  
776
747
  while(true){
777
748
    
778
749
    if(quit_now){
779
 
      errno = EINTR;
780
750
      goto mandos_end;
781
751
    }
782
752
    
783
753
    buffer_capacity = incbuffer(&buffer, buffer_length,
784
754
                                   buffer_capacity);
785
755
    if(buffer_capacity == 0){
786
 
      int e = errno;
787
756
      perror("incbuffer");
788
 
      errno = e;
789
757
      goto mandos_end;
790
758
    }
791
759
    
792
760
    if(quit_now){
793
 
      errno = EINTR;
794
761
      goto mandos_end;
795
762
    }
796
763
    
809
776
          ret = gnutls_handshake(session);
810
777
          
811
778
          if(quit_now){
812
 
            errno = EINTR;
813
779
            goto mandos_end;
814
780
          }
815
781
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
816
782
        if(ret < 0){
817
783
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
818
784
          gnutls_perror(ret);
819
 
          errno = EPROTO;
820
785
          goto mandos_end;
821
786
        }
822
787
        break;
824
789
        fprintf(stderr, "Unknown error while reading data from"
825
790
                " encrypted session with Mandos server\n");
826
791
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
827
 
        errno = EIO;
828
792
        goto mandos_end;
829
793
      }
830
794
    } else {
837
801
  }
838
802
  
839
803
  if(quit_now){
840
 
    errno = EINTR;
841
804
    goto mandos_end;
842
805
  }
843
806
  
844
807
  do {
845
808
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
846
809
    if(quit_now){
847
 
      errno = EINTR;
848
810
      goto mandos_end;
849
811
    }
850
812
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
859
821
      written = 0;
860
822
      while(written < (size_t) decrypted_buffer_size){
861
823
        if(quit_now){
862
 
          errno = EINTR;
863
824
          goto mandos_end;
864
825
        }
865
826
        
867
828
                          (size_t)decrypted_buffer_size - written,
868
829
                          stdout);
869
830
        if(ret == 0 and ferror(stdout)){
870
 
          int e = errno;
871
831
          if(debug){
872
832
            fprintf(stderr, "Error writing encrypted data: %s\n",
873
833
                    strerror(errno));
874
834
          }
875
 
          errno = e;
876
835
          goto mandos_end;
877
836
        }
878
837
        written += (size_t)ret;
884
843
  /* Shutdown procedure */
885
844
  
886
845
 mandos_end:
887
 
  {
888
 
    int e = errno;
889
 
    free(decrypted_buffer);
890
 
    free(buffer);
891
 
    if(tcp_sd >= 0){
892
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
893
 
    }
894
 
    if(ret == -1){
895
 
      if(e == 0){
896
 
        e = errno;
897
 
      }
898
 
      perror("close");
899
 
    }
900
 
    gnutls_deinit(session);
901
 
    if(quit_now){
902
 
      e = EINTR;
903
 
      retval = -1;
904
 
    }
905
 
    errno = e;
 
846
  free(decrypted_buffer);
 
847
  free(buffer);
 
848
  if(tcp_sd >= 0){
 
849
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
850
  }
 
851
  if(ret == -1){
 
852
    perror("close");
 
853
  }
 
854
  gnutls_deinit(session);
 
855
  if(quit_now){
 
856
    retval = -1;
906
857
  }
907
858
  return retval;
908
859
}
1024
975
  errno = old_errno;
1025
976
}
1026
977
 
1027
 
/* 
1028
 
 * This function determines if a directory entry in /sys/class/net
1029
 
 * corresponds to an acceptable network device.
1030
 
 * (This function is passed to scandir(3) as a filter function.)
1031
 
 */
1032
 
int good_interface(const struct dirent *if_entry){
1033
 
  ssize_t ssret;
1034
 
  char *flagname = NULL;
1035
 
  if(if_entry->d_name[0] == '.'){
1036
 
    return 0;
1037
 
  }
1038
 
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1039
 
                     if_entry->d_name);
1040
 
  if(ret < 0){
1041
 
    perror("asprintf");
1042
 
    return 0;
1043
 
  }
1044
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1045
 
  if(flags_fd == -1){
1046
 
    perror("open");
1047
 
    free(flagname);
1048
 
    return 0;
1049
 
  }
1050
 
  free(flagname);
1051
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1052
 
  /* read line from flags_fd */
1053
 
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1054
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1055
 
  flagstring[(size_t)to_read] = '\0';
1056
 
  if(flagstring == NULL){
1057
 
    perror("malloc");
1058
 
    close(flags_fd);
1059
 
    return 0;
1060
 
  }
1061
 
  while(to_read > 0){
1062
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1063
 
                                             (size_t)to_read));
1064
 
    if(ssret == -1){
1065
 
      perror("read");
1066
 
      free(flagstring);
1067
 
      close(flags_fd);
1068
 
      return 0;
1069
 
    }
1070
 
    to_read -= ssret;
1071
 
    if(ssret == 0){
1072
 
      break;
1073
 
    }
1074
 
  }
1075
 
  close(flags_fd);
1076
 
  intmax_t tmpmax;
1077
 
  char *tmp;
1078
 
  errno = 0;
1079
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1080
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1081
 
                                         and not (isspace(*tmp)))
1082
 
     or tmpmax != (ifreq_flags)tmpmax){
1083
 
    if(debug){
1084
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1085
 
              flagstring, if_entry->d_name);
1086
 
    }
1087
 
    free(flagstring);
1088
 
    return 0;
1089
 
  }
1090
 
  free(flagstring);
1091
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
1092
 
  /* Reject the loopback device */
1093
 
  if(flags & IFF_LOOPBACK){
1094
 
    if(debug){
1095
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1096
 
              if_entry->d_name);
1097
 
    }
1098
 
    return 0;
1099
 
  }
1100
 
  /* Accept point-to-point devices only if connect_to is specified */
1101
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1102
 
    if(debug){
1103
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1104
 
              if_entry->d_name);
1105
 
    }
1106
 
    return 1;
1107
 
  }
1108
 
  /* Otherwise, reject non-broadcast-capable devices */
1109
 
  if(not (flags & IFF_BROADCAST)){
1110
 
    if(debug){
1111
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1112
 
              if_entry->d_name);
1113
 
    }
1114
 
    return 0;
1115
 
  }
1116
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1117
 
  if(flags & IFF_NOARP){
1118
 
    if(debug){
1119
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1120
 
              if_entry->d_name);
1121
 
    }
1122
 
    return 0;
1123
 
  }
1124
 
  /* Accept this device */
1125
 
  if(debug){
1126
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1127
 
            if_entry->d_name);
1128
 
  }
1129
 
  return 1;
1130
 
}
1131
 
 
1132
978
int main(int argc, char *argv[]){
1133
979
  AvahiSServiceBrowser *sb = NULL;
1134
980
  int error;
1136
982
  intmax_t tmpmax;
1137
983
  char *tmp;
1138
984
  int exitcode = EXIT_SUCCESS;
1139
 
  const char *interface = "";
 
985
  const char *interface = "eth0";
1140
986
  struct ifreq network;
1141
987
  int sd = -1;
1142
988
  bool take_down_interface = false;
1143
989
  uid_t uid;
1144
990
  gid_t gid;
 
991
  char *connect_to = NULL;
1145
992
  char tempdir[] = "/tmp/mandosXXXXXX";
1146
993
  bool tempdir_created = false;
1147
994
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1304
1151
  if(not debug){
1305
1152
    avahi_set_log_function(empty_log);
1306
1153
  }
1307
 
 
1308
 
  if(interface[0] == '\0'){
1309
 
    struct dirent **direntries;
1310
 
    ret = scandir(sys_class_net, &direntries, good_interface,
1311
 
                  alphasort);
1312
 
    if(ret >= 1){
1313
 
      /* Pick the first good interface */
1314
 
      interface = strdup(direntries[0]->d_name);
1315
 
      if(debug){
1316
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1317
 
      }
1318
 
      if(interface == NULL){
1319
 
        perror("malloc");
1320
 
        free(direntries);
1321
 
        exitcode = EXIT_FAILURE;
1322
 
        goto end;
1323
 
      }
1324
 
      free(direntries);
1325
 
    } else {
1326
 
      free(direntries);
1327
 
      fprintf(stderr, "Could not find a network interface\n");
1328
 
      exitcode = EXIT_FAILURE;
1329
 
      goto end;
1330
 
    }
1331
 
  }
1332
1154
  
1333
1155
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1334
1156
     from the signal handler */
1337
1159
  mc.simple_poll = avahi_simple_poll_new();
1338
1160
  if(mc.simple_poll == NULL){
1339
1161
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1340
 
    exitcode = EX_UNAVAILABLE;
 
1162
    exitcode = EXIT_FAILURE;
1341
1163
    goto end;
1342
1164
  }
1343
1165
  
1345
1167
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1346
1168
  if(ret == -1){
1347
1169
    perror("sigaddset");
1348
 
    exitcode = EX_OSERR;
 
1170
    exitcode = EXIT_FAILURE;
1349
1171
    goto end;
1350
1172
  }
1351
1173
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1352
1174
  if(ret == -1){
1353
1175
    perror("sigaddset");
1354
 
    exitcode = EX_OSERR;
 
1176
    exitcode = EXIT_FAILURE;
1355
1177
    goto end;
1356
1178
  }
1357
1179
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1358
1180
  if(ret == -1){
1359
1181
    perror("sigaddset");
1360
 
    exitcode = EX_OSERR;
 
1182
    exitcode = EXIT_FAILURE;
1361
1183
    goto end;
1362
1184
  }
1363
1185
  /* Need to check if the handler is SIG_IGN before handling:
1367
1189
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1368
1190
  if(ret == -1){
1369
1191
    perror("sigaction");
1370
 
    return EX_OSERR;
 
1192
    return EXIT_FAILURE;
1371
1193
  }
1372
1194
  if(old_sigterm_action.sa_handler != SIG_IGN){
1373
1195
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1374
1196
    if(ret == -1){
1375
1197
      perror("sigaction");
1376
 
      exitcode = EX_OSERR;
 
1198
      exitcode = EXIT_FAILURE;
1377
1199
      goto end;
1378
1200
    }
1379
1201
  }
1380
1202
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1381
1203
  if(ret == -1){
1382
1204
    perror("sigaction");
1383
 
    return EX_OSERR;
 
1205
    return EXIT_FAILURE;
1384
1206
  }
1385
1207
  if(old_sigterm_action.sa_handler != SIG_IGN){
1386
1208
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1387
1209
    if(ret == -1){
1388
1210
      perror("sigaction");
1389
 
      exitcode = EX_OSERR;
 
1211
      exitcode = EXIT_FAILURE;
1390
1212
      goto end;
1391
1213
    }
1392
1214
  }
1393
1215
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1394
1216
  if(ret == -1){
1395
1217
    perror("sigaction");
1396
 
    return EX_OSERR;
 
1218
    return EXIT_FAILURE;
1397
1219
  }
1398
1220
  if(old_sigterm_action.sa_handler != SIG_IGN){
1399
1221
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1400
1222
    if(ret == -1){
1401
1223
      perror("sigaction");
1402
 
      exitcode = EX_OSERR;
 
1224
      exitcode = EXIT_FAILURE;
1403
1225
      goto end;
1404
1226
    }
1405
1227
  }
1406
1228
  
1407
1229
  /* If the interface is down, bring it up */
1408
 
  if(strcmp(interface, "none") != 0){
 
1230
  if(interface[0] != '\0'){
1409
1231
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1410
1232
    if(if_index == 0){
1411
1233
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1412
 
      exitcode = EX_UNAVAILABLE;
 
1234
      exitcode = EXIT_FAILURE;
1413
1235
      goto end;
1414
1236
    }
1415
1237
    
1438
1260
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1439
1261
    if(sd < 0){
1440
1262
      perror("socket");
1441
 
      exitcode = EX_OSERR;
 
1263
      exitcode = EXIT_FAILURE;
1442
1264
#ifdef __linux__
1443
1265
      if(restore_loglevel){
1444
1266
        ret = klogctl(7, NULL, 0);
1467
1289
        }
1468
1290
      }
1469
1291
#endif  /* __linux__ */
1470
 
      exitcode = EX_OSERR;
 
1292
      exitcode = EXIT_FAILURE;
1471
1293
      /* Lower privileges */
1472
1294
      errno = 0;
1473
1295
      ret = seteuid(uid);
1482
1304
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1483
1305
      if(ret == -1){
1484
1306
        take_down_interface = false;
1485
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1486
 
        exitcode = EX_OSERR;
 
1307
        perror("ioctl SIOCSIFFLAGS");
 
1308
        exitcode = EXIT_FAILURE;
1487
1309
#ifdef __linux__
1488
1310
        if(restore_loglevel){
1489
1311
          ret = klogctl(7, NULL, 0);
1555
1377
  ret = init_gnutls_global(pubkey, seckey);
1556
1378
  if(ret == -1){
1557
1379
    fprintf(stderr, "init_gnutls_global failed\n");
1558
 
    exitcode = EX_UNAVAILABLE;
 
1380
    exitcode = EXIT_FAILURE;
1559
1381
    goto end;
1560
1382
  } else {
1561
1383
    gnutls_initialized = true;
1578
1400
  
1579
1401
  if(not init_gpgme(pubkey, seckey, tempdir)){
1580
1402
    fprintf(stderr, "init_gpgme failed\n");
1581
 
    exitcode = EX_UNAVAILABLE;
 
1403
    exitcode = EXIT_FAILURE;
1582
1404
    goto end;
1583
1405
  } else {
1584
1406
    gpgme_initialized = true;
1594
1416
    char *address = strrchr(connect_to, ':');
1595
1417
    if(address == NULL){
1596
1418
      fprintf(stderr, "No colon in address\n");
1597
 
      exitcode = EX_USAGE;
 
1419
      exitcode = EXIT_FAILURE;
1598
1420
      goto end;
1599
1421
    }
1600
1422
    
1608
1430
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1609
1431
       or tmpmax != (uint16_t)tmpmax){
1610
1432
      fprintf(stderr, "Bad port number\n");
1611
 
      exitcode = EX_USAGE;
 
1433
      exitcode = EXIT_FAILURE;
1612
1434
      goto end;
1613
1435
    }
1614
1436
  
1630
1452
    if(quit_now){
1631
1453
      goto end;
1632
1454
    }
1633
 
 
1634
 
    while(not quit_now){
1635
 
      ret = start_mandos_communication(address, port, if_index, af);
1636
 
      if(quit_now or ret == 0){
1637
 
        break;
1638
 
      }
1639
 
      sleep(15);
1640
 
    };
1641
 
 
1642
 
    if (not quit_now){
 
1455
    
 
1456
    ret = start_mandos_communication(address, port, if_index, af);
 
1457
    if(ret < 0){
 
1458
      exitcode = EXIT_FAILURE;
 
1459
    } else {
1643
1460
      exitcode = EXIT_SUCCESS;
1644
1461
    }
1645
 
 
1646
1462
    goto end;
1647
1463
  }
1648
1464
  
1672
1488
  if(mc.server == NULL){
1673
1489
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1674
1490
            avahi_strerror(error));
1675
 
    exitcode = EX_UNAVAILABLE;
 
1491
    exitcode = EXIT_FAILURE;
1676
1492
    goto end;
1677
1493
  }
1678
1494
  
1687
1503
  if(sb == NULL){
1688
1504
    fprintf(stderr, "Failed to create service browser: %s\n",
1689
1505
            avahi_strerror(avahi_server_errno(mc.server)));
1690
 
    exitcode = EX_UNAVAILABLE;
 
1506
    exitcode = EXIT_FAILURE;
1691
1507
    goto end;
1692
1508
  }
1693
1509
  
1745
1561
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1746
1562
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1747
1563
        if(ret == -1){
1748
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1564
          perror("ioctl SIOCSIFFLAGS");
1749
1565
        }
1750
1566
      }
1751
1567
      ret = (int)TEMP_FAILURE_RETRY(close(sd));