/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: 2010-10-02 17:41:05 UTC
  • mto: (24.1.169 mandos)
  • mto: This revision was merged to the branch mainline in revision 453.
  • Revision ID: teddy@fukt.bsnet.se-20101002174105-xfqa0j8y1puvedo3
* debian/source/format: New; contains "3.0 (quilt)".

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,2009 Teddy Hogeborn
13
 
 * Copyright © 2008,2009 Björn Påhlsson
 
12
 * Copyright © 2008-2010 Teddy Hogeborn
 
13
 * Copyright © 2008-2010 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, EXIT_FAILURE,
47
 
                                   srand(), strtof(), abort() */
 
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() */
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
65
#include <errno.h>              /* perror(), errno */
66
 
#include <time.h>               /* nanosleep(), time() */
 
66
#include <time.h>               /* nanosleep(), time(), sleep() */
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 */
 
85
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
 
86
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
86
87
 
87
88
#ifdef __linux__
88
89
#include <sys/klog.h>           /* klogctl() */
126
127
static const char mandos_protocol_version[] = "1";
127
128
const char *argp_program_version = "mandos-client " VERSION;
128
129
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;
129
132
 
130
133
/* Used for passing in values through the Avahi callback functions */
131
134
typedef struct {
553
556
  gnutls_session_t session;
554
557
  int pf;                       /* Protocol family */
555
558
  
 
559
  errno = 0;
 
560
  
556
561
  if(quit_now){
 
562
    errno = EINTR;
557
563
    return -1;
558
564
  }
559
565
  
566
572
    break;
567
573
  default:
568
574
    fprintf(stderr, "Bad address family: %d\n", af);
 
575
    errno = EINVAL;
569
576
    return -1;
570
577
  }
571
578
  
581
588
  
582
589
  tcp_sd = socket(pf, SOCK_STREAM, 0);
583
590
  if(tcp_sd < 0){
 
591
    int e = errno;
584
592
    perror("socket");
 
593
    errno = e;
585
594
    goto mandos_end;
586
595
  }
587
596
  
588
597
  if(quit_now){
 
598
    errno = EINTR;
589
599
    goto mandos_end;
590
600
  }
591
601
  
598
608
    ret = inet_pton(af, ip, &to.in.sin_addr);
599
609
  }
600
610
  if(ret < 0 ){
 
611
    int e = errno;
601
612
    perror("inet_pton");
 
613
    errno = e;
602
614
    goto mandos_end;
603
615
  }
604
616
  if(ret == 0){
 
617
    int e = errno;
605
618
    fprintf(stderr, "Bad address: %s\n", ip);
 
619
    errno = e;
606
620
    goto mandos_end;
607
621
  }
608
622
  if(af == AF_INET6){
616
630
      if(if_index == AVAHI_IF_UNSPEC){
617
631
        fprintf(stderr, "An IPv6 link-local address is incomplete"
618
632
                " without a network interface\n");
 
633
        errno = EINVAL;
619
634
        goto mandos_end;
620
635
      }
621
636
      /* Set the network interface number as scope */
628
643
  }
629
644
  
630
645
  if(quit_now){
 
646
    errno = EINTR;
631
647
    goto mandos_end;
632
648
  }
633
649
  
664
680
  }
665
681
  
666
682
  if(quit_now){
 
683
    errno = EINTR;
667
684
    goto mandos_end;
668
685
  }
669
686
  
673
690
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
674
691
  }
675
692
  if(ret < 0){
676
 
    perror("connect");
 
693
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
694
      int e = errno;
 
695
      perror("connect");
 
696
      errno = e;
 
697
    }
677
698
    goto mandos_end;
678
699
  }
679
700
  
680
701
  if(quit_now){
 
702
    errno = EINTR;
681
703
    goto mandos_end;
682
704
  }
683
705
  
688
710
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
689
711
                                   out_size - written));
690
712
    if(ret == -1){
 
713
      int e = errno;
691
714
      perror("write");
 
715
      errno = e;
692
716
      goto mandos_end;
693
717
    }
694
718
    written += (size_t)ret;
704
728
    }
705
729
  
706
730
    if(quit_now){
 
731
      errno = EINTR;
707
732
      goto mandos_end;
708
733
    }
709
734
  }
713
738
  }
714
739
  
715
740
  if(quit_now){
 
741
    errno = EINTR;
716
742
    goto mandos_end;
717
743
  }
718
744
  
719
745
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
720
746
  
721
747
  if(quit_now){
 
748
    errno = EINTR;
722
749
    goto mandos_end;
723
750
  }
724
751
  
725
752
  do {
726
753
    ret = gnutls_handshake(session);
727
754
    if(quit_now){
 
755
      errno = EINTR;
728
756
      goto mandos_end;
729
757
    }
730
758
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
734
762
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
735
763
      gnutls_perror(ret);
736
764
    }
 
765
    errno = EPROTO;
737
766
    goto mandos_end;
738
767
  }
739
768
  
747
776
  while(true){
748
777
    
749
778
    if(quit_now){
 
779
      errno = EINTR;
750
780
      goto mandos_end;
751
781
    }
752
782
    
753
783
    buffer_capacity = incbuffer(&buffer, buffer_length,
754
784
                                   buffer_capacity);
755
785
    if(buffer_capacity == 0){
 
786
      int e = errno;
756
787
      perror("incbuffer");
 
788
      errno = e;
757
789
      goto mandos_end;
758
790
    }
759
791
    
760
792
    if(quit_now){
 
793
      errno = EINTR;
761
794
      goto mandos_end;
762
795
    }
763
796
    
776
809
          ret = gnutls_handshake(session);
777
810
          
778
811
          if(quit_now){
 
812
            errno = EINTR;
779
813
            goto mandos_end;
780
814
          }
781
815
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
782
816
        if(ret < 0){
783
817
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
784
818
          gnutls_perror(ret);
 
819
          errno = EPROTO;
785
820
          goto mandos_end;
786
821
        }
787
822
        break;
789
824
        fprintf(stderr, "Unknown error while reading data from"
790
825
                " encrypted session with Mandos server\n");
791
826
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
827
        errno = EIO;
792
828
        goto mandos_end;
793
829
      }
794
830
    } else {
801
837
  }
802
838
  
803
839
  if(quit_now){
 
840
    errno = EINTR;
804
841
    goto mandos_end;
805
842
  }
806
843
  
807
844
  do {
808
845
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
809
846
    if(quit_now){
 
847
      errno = EINTR;
810
848
      goto mandos_end;
811
849
    }
812
850
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
821
859
      written = 0;
822
860
      while(written < (size_t) decrypted_buffer_size){
823
861
        if(quit_now){
 
862
          errno = EINTR;
824
863
          goto mandos_end;
825
864
        }
826
865
        
828
867
                          (size_t)decrypted_buffer_size - written,
829
868
                          stdout);
830
869
        if(ret == 0 and ferror(stdout)){
 
870
          int e = errno;
831
871
          if(debug){
832
872
            fprintf(stderr, "Error writing encrypted data: %s\n",
833
873
                    strerror(errno));
834
874
          }
 
875
          errno = e;
835
876
          goto mandos_end;
836
877
        }
837
878
        written += (size_t)ret;
843
884
  /* Shutdown procedure */
844
885
  
845
886
 mandos_end:
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;
 
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;
857
906
  }
858
907
  return retval;
859
908
}
975
1024
  errno = old_errno;
976
1025
}
977
1026
 
 
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
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
 
1036
                     if_entry->d_name);
 
1037
  if(ret < 0){
 
1038
    perror("asprintf");
 
1039
    return 0;
 
1040
  }
 
1041
  if(if_entry->d_name[0] == '.'){
 
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
    return 0;
 
1048
  }
 
1049
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
 
1050
  /* read line from flags_fd */
 
1051
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
 
1052
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
 
1053
  flagstring[(size_t)to_read] = '\0';
 
1054
  if(flagstring == NULL){
 
1055
    perror("malloc");
 
1056
    close(flags_fd);
 
1057
    return 0;
 
1058
  }
 
1059
  while(to_read > 0){
 
1060
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
 
1061
                                             (size_t)to_read));
 
1062
    if(ssret == -1){
 
1063
      perror("read");
 
1064
      free(flagstring);
 
1065
      close(flags_fd);
 
1066
      return 0;
 
1067
    }
 
1068
    to_read -= ssret;
 
1069
    if(ssret == 0){
 
1070
      break;
 
1071
    }
 
1072
  }
 
1073
  close(flags_fd);
 
1074
  intmax_t tmpmax;
 
1075
  char *tmp;
 
1076
  errno = 0;
 
1077
  tmpmax = strtoimax(flagstring, &tmp, 0);
 
1078
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
 
1079
                                         and not (isspace(*tmp)))
 
1080
     or tmpmax != (ifreq_flags)tmpmax){
 
1081
    if(debug){
 
1082
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
 
1083
              flagstring, if_entry->d_name);
 
1084
    }
 
1085
    free(flagstring);
 
1086
    return 0;
 
1087
  }
 
1088
  free(flagstring);
 
1089
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1090
  /* Reject the loopback device */
 
1091
  if(flags & IFF_LOOPBACK){
 
1092
    if(debug){
 
1093
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1094
              if_entry->d_name);
 
1095
    }
 
1096
    return 0;
 
1097
  }
 
1098
  /* Accept point-to-point devices only if connect_to is specified */
 
1099
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1100
    if(debug){
 
1101
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1102
              if_entry->d_name);
 
1103
    }
 
1104
    return 1;
 
1105
  }
 
1106
  /* Otherwise, reject non-broadcast-capable devices */
 
1107
  if(not (flags & IFF_BROADCAST)){
 
1108
    if(debug){
 
1109
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1110
              if_entry->d_name);
 
1111
    }
 
1112
    return 0;
 
1113
  }
 
1114
  /* Accept this device */
 
1115
  if(debug){
 
1116
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
 
1117
            if_entry->d_name);
 
1118
  }
 
1119
  return 1;
 
1120
}
 
1121
 
978
1122
int main(int argc, char *argv[]){
979
1123
  AvahiSServiceBrowser *sb = NULL;
980
1124
  int error;
982
1126
  intmax_t tmpmax;
983
1127
  char *tmp;
984
1128
  int exitcode = EXIT_SUCCESS;
985
 
  const char *interface = "eth0";
 
1129
  const char *interface = "";
986
1130
  struct ifreq network;
987
1131
  int sd = -1;
988
1132
  bool take_down_interface = false;
989
1133
  uid_t uid;
990
1134
  gid_t gid;
991
 
  char *connect_to = NULL;
992
1135
  char tempdir[] = "/tmp/mandosXXXXXX";
993
1136
  bool tempdir_created = false;
994
1137
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1151
1294
  if(not debug){
1152
1295
    avahi_set_log_function(empty_log);
1153
1296
  }
 
1297
 
 
1298
  if(interface[0] == '\0'){
 
1299
    struct dirent **direntries;
 
1300
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1301
                  alphasort);
 
1302
    if(ret >= 1){
 
1303
      /* Pick the first good interface */
 
1304
      interface = strdup(direntries[0]->d_name);
 
1305
      if(debug){
 
1306
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1307
      }
 
1308
      if(interface == NULL){
 
1309
        perror("malloc");
 
1310
        free(direntries);
 
1311
        exitcode = EXIT_FAILURE;
 
1312
        goto end;
 
1313
      }
 
1314
      free(direntries);
 
1315
    } else {
 
1316
      free(direntries);
 
1317
      fprintf(stderr, "Could not find a network interface\n");
 
1318
      exitcode = EXIT_FAILURE;
 
1319
      goto end;
 
1320
    }
 
1321
  }
1154
1322
  
1155
1323
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1156
1324
     from the signal handler */
1159
1327
  mc.simple_poll = avahi_simple_poll_new();
1160
1328
  if(mc.simple_poll == NULL){
1161
1329
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1162
 
    exitcode = EXIT_FAILURE;
 
1330
    exitcode = EX_UNAVAILABLE;
1163
1331
    goto end;
1164
1332
  }
1165
1333
  
1167
1335
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1168
1336
  if(ret == -1){
1169
1337
    perror("sigaddset");
1170
 
    exitcode = EXIT_FAILURE;
 
1338
    exitcode = EX_OSERR;
1171
1339
    goto end;
1172
1340
  }
1173
1341
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1174
1342
  if(ret == -1){
1175
1343
    perror("sigaddset");
1176
 
    exitcode = EXIT_FAILURE;
 
1344
    exitcode = EX_OSERR;
1177
1345
    goto end;
1178
1346
  }
1179
1347
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1180
1348
  if(ret == -1){
1181
1349
    perror("sigaddset");
1182
 
    exitcode = EXIT_FAILURE;
 
1350
    exitcode = EX_OSERR;
1183
1351
    goto end;
1184
1352
  }
1185
1353
  /* Need to check if the handler is SIG_IGN before handling:
1189
1357
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1190
1358
  if(ret == -1){
1191
1359
    perror("sigaction");
1192
 
    return EXIT_FAILURE;
 
1360
    return EX_OSERR;
1193
1361
  }
1194
1362
  if(old_sigterm_action.sa_handler != SIG_IGN){
1195
1363
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1196
1364
    if(ret == -1){
1197
1365
      perror("sigaction");
1198
 
      exitcode = EXIT_FAILURE;
 
1366
      exitcode = EX_OSERR;
1199
1367
      goto end;
1200
1368
    }
1201
1369
  }
1202
1370
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1203
1371
  if(ret == -1){
1204
1372
    perror("sigaction");
1205
 
    return EXIT_FAILURE;
 
1373
    return EX_OSERR;
1206
1374
  }
1207
1375
  if(old_sigterm_action.sa_handler != SIG_IGN){
1208
1376
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1209
1377
    if(ret == -1){
1210
1378
      perror("sigaction");
1211
 
      exitcode = EXIT_FAILURE;
 
1379
      exitcode = EX_OSERR;
1212
1380
      goto end;
1213
1381
    }
1214
1382
  }
1215
1383
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1216
1384
  if(ret == -1){
1217
1385
    perror("sigaction");
1218
 
    return EXIT_FAILURE;
 
1386
    return EX_OSERR;
1219
1387
  }
1220
1388
  if(old_sigterm_action.sa_handler != SIG_IGN){
1221
1389
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1222
1390
    if(ret == -1){
1223
1391
      perror("sigaction");
1224
 
      exitcode = EXIT_FAILURE;
 
1392
      exitcode = EX_OSERR;
1225
1393
      goto end;
1226
1394
    }
1227
1395
  }
1228
1396
  
1229
1397
  /* If the interface is down, bring it up */
1230
 
  if(interface[0] != '\0'){
 
1398
  if(strcmp(interface, "none") != 0){
1231
1399
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1232
1400
    if(if_index == 0){
1233
1401
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1234
 
      exitcode = EXIT_FAILURE;
 
1402
      exitcode = EX_UNAVAILABLE;
1235
1403
      goto end;
1236
1404
    }
1237
1405
    
1260
1428
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1261
1429
    if(sd < 0){
1262
1430
      perror("socket");
1263
 
      exitcode = EXIT_FAILURE;
 
1431
      exitcode = EX_OSERR;
1264
1432
#ifdef __linux__
1265
1433
      if(restore_loglevel){
1266
1434
        ret = klogctl(7, NULL, 0);
1289
1457
        }
1290
1458
      }
1291
1459
#endif  /* __linux__ */
1292
 
      exitcode = EXIT_FAILURE;
 
1460
      exitcode = EX_OSERR;
1293
1461
      /* Lower privileges */
1294
1462
      errno = 0;
1295
1463
      ret = seteuid(uid);
1304
1472
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1305
1473
      if(ret == -1){
1306
1474
        take_down_interface = false;
1307
 
        perror("ioctl SIOCSIFFLAGS");
1308
 
        exitcode = EXIT_FAILURE;
 
1475
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1476
        exitcode = EX_OSERR;
1309
1477
#ifdef __linux__
1310
1478
        if(restore_loglevel){
1311
1479
          ret = klogctl(7, NULL, 0);
1377
1545
  ret = init_gnutls_global(pubkey, seckey);
1378
1546
  if(ret == -1){
1379
1547
    fprintf(stderr, "init_gnutls_global failed\n");
1380
 
    exitcode = EXIT_FAILURE;
 
1548
    exitcode = EX_UNAVAILABLE;
1381
1549
    goto end;
1382
1550
  } else {
1383
1551
    gnutls_initialized = true;
1400
1568
  
1401
1569
  if(not init_gpgme(pubkey, seckey, tempdir)){
1402
1570
    fprintf(stderr, "init_gpgme failed\n");
1403
 
    exitcode = EXIT_FAILURE;
 
1571
    exitcode = EX_UNAVAILABLE;
1404
1572
    goto end;
1405
1573
  } else {
1406
1574
    gpgme_initialized = true;
1416
1584
    char *address = strrchr(connect_to, ':');
1417
1585
    if(address == NULL){
1418
1586
      fprintf(stderr, "No colon in address\n");
1419
 
      exitcode = EXIT_FAILURE;
 
1587
      exitcode = EX_USAGE;
1420
1588
      goto end;
1421
1589
    }
1422
1590
    
1430
1598
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1431
1599
       or tmpmax != (uint16_t)tmpmax){
1432
1600
      fprintf(stderr, "Bad port number\n");
1433
 
      exitcode = EXIT_FAILURE;
 
1601
      exitcode = EX_USAGE;
1434
1602
      goto end;
1435
1603
    }
1436
1604
  
1452
1620
    if(quit_now){
1453
1621
      goto end;
1454
1622
    }
1455
 
    
1456
 
    ret = start_mandos_communication(address, port, if_index, af);
1457
 
    if(ret < 0){
1458
 
      exitcode = EXIT_FAILURE;
1459
 
    } else {
 
1623
 
 
1624
    while(not quit_now){
 
1625
      ret = start_mandos_communication(address, port, if_index, af);
 
1626
      if(quit_now or ret == 0){
 
1627
        break;
 
1628
      }
 
1629
      sleep(15);
 
1630
    };
 
1631
 
 
1632
    if (not quit_now){
1460
1633
      exitcode = EXIT_SUCCESS;
1461
1634
    }
 
1635
 
1462
1636
    goto end;
1463
1637
  }
1464
1638
  
1488
1662
  if(mc.server == NULL){
1489
1663
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1490
1664
            avahi_strerror(error));
1491
 
    exitcode = EXIT_FAILURE;
 
1665
    exitcode = EX_UNAVAILABLE;
1492
1666
    goto end;
1493
1667
  }
1494
1668
  
1503
1677
  if(sb == NULL){
1504
1678
    fprintf(stderr, "Failed to create service browser: %s\n",
1505
1679
            avahi_strerror(avahi_server_errno(mc.server)));
1506
 
    exitcode = EXIT_FAILURE;
 
1680
    exitcode = EX_UNAVAILABLE;
1507
1681
    goto end;
1508
1682
  }
1509
1683
  
1561
1735
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1562
1736
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1563
1737
        if(ret == -1){
1564
 
          perror("ioctl SIOCSIFFLAGS");
 
1738
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1565
1739
        }
1566
1740
      }
1567
1741
      ret = (int)TEMP_FAILURE_RETRY(close(sd));