/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-09-12 03:00:40 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100912030040-b0uopyennste9fdh
Documentation changes:

* DBUS-API: New file documenting the server D-Bus interface.

* clients.conf: Add examples of new approval settings.

* debian/mandos.docs: Added "DBUS-API".

* mandos-clients.conf.xml (OPTIONS): Added "approved_by_default",
                                     "approval_delay", and
                                     "approval_duration".
* mandos.xml (D-BUS INTERFACE): Refer to the "DBUS-API" file.
  (BUGS): Remove mention of lack of a remote query interface.

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(), 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() */
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() */
554
556
  gnutls_session_t session;
555
557
  int pf;                       /* Protocol family */
556
558
  
 
559
  errno = 0;
 
560
  
557
561
  if(quit_now){
 
562
    errno = EINTR;
558
563
    return -1;
559
564
  }
560
565
  
567
572
    break;
568
573
  default:
569
574
    fprintf(stderr, "Bad address family: %d\n", af);
 
575
    errno = EINVAL;
570
576
    return -1;
571
577
  }
572
578
  
582
588
  
583
589
  tcp_sd = socket(pf, SOCK_STREAM, 0);
584
590
  if(tcp_sd < 0){
 
591
    int e = errno;
585
592
    perror("socket");
 
593
    errno = e;
586
594
    goto mandos_end;
587
595
  }
588
596
  
589
597
  if(quit_now){
 
598
    errno = EINTR;
590
599
    goto mandos_end;
591
600
  }
592
601
  
599
608
    ret = inet_pton(af, ip, &to.in.sin_addr);
600
609
  }
601
610
  if(ret < 0 ){
 
611
    int e = errno;
602
612
    perror("inet_pton");
 
613
    errno = e;
603
614
    goto mandos_end;
604
615
  }
605
616
  if(ret == 0){
 
617
    int e = errno;
606
618
    fprintf(stderr, "Bad address: %s\n", ip);
 
619
    errno = e;
607
620
    goto mandos_end;
608
621
  }
609
622
  if(af == AF_INET6){
617
630
      if(if_index == AVAHI_IF_UNSPEC){
618
631
        fprintf(stderr, "An IPv6 link-local address is incomplete"
619
632
                " without a network interface\n");
 
633
        errno = EINVAL;
620
634
        goto mandos_end;
621
635
      }
622
636
      /* Set the network interface number as scope */
629
643
  }
630
644
  
631
645
  if(quit_now){
 
646
    errno = EINTR;
632
647
    goto mandos_end;
633
648
  }
634
649
  
665
680
  }
666
681
  
667
682
  if(quit_now){
 
683
    errno = EINTR;
668
684
    goto mandos_end;
669
685
  }
670
686
  
674
690
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
675
691
  }
676
692
  if(ret < 0){
 
693
    int e = errno;
677
694
    perror("connect");
 
695
    errno = e;
678
696
    goto mandos_end;
679
697
  }
680
698
  
681
699
  if(quit_now){
 
700
    errno = EINTR;
682
701
    goto mandos_end;
683
702
  }
684
703
  
689
708
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
690
709
                                   out_size - written));
691
710
    if(ret == -1){
 
711
      int e = errno;
692
712
      perror("write");
 
713
      errno = e;
693
714
      goto mandos_end;
694
715
    }
695
716
    written += (size_t)ret;
705
726
    }
706
727
  
707
728
    if(quit_now){
 
729
      errno = EINTR;
708
730
      goto mandos_end;
709
731
    }
710
732
  }
714
736
  }
715
737
  
716
738
  if(quit_now){
 
739
    errno = EINTR;
717
740
    goto mandos_end;
718
741
  }
719
742
  
720
743
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
721
744
  
722
745
  if(quit_now){
 
746
    errno = EINTR;
723
747
    goto mandos_end;
724
748
  }
725
749
  
726
750
  do {
727
751
    ret = gnutls_handshake(session);
728
752
    if(quit_now){
 
753
      errno = EINTR;
729
754
      goto mandos_end;
730
755
    }
731
756
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
735
760
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
736
761
      gnutls_perror(ret);
737
762
    }
 
763
    errno = EPROTO;
738
764
    goto mandos_end;
739
765
  }
740
766
  
748
774
  while(true){
749
775
    
750
776
    if(quit_now){
 
777
      errno = EINTR;
751
778
      goto mandos_end;
752
779
    }
753
780
    
754
781
    buffer_capacity = incbuffer(&buffer, buffer_length,
755
782
                                   buffer_capacity);
756
783
    if(buffer_capacity == 0){
 
784
      int e = errno;
757
785
      perror("incbuffer");
 
786
      errno = e;
758
787
      goto mandos_end;
759
788
    }
760
789
    
761
790
    if(quit_now){
 
791
      errno = EINTR;
762
792
      goto mandos_end;
763
793
    }
764
794
    
777
807
          ret = gnutls_handshake(session);
778
808
          
779
809
          if(quit_now){
 
810
            errno = EINTR;
780
811
            goto mandos_end;
781
812
          }
782
813
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
783
814
        if(ret < 0){
784
815
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
785
816
          gnutls_perror(ret);
 
817
          errno = EPROTO;
786
818
          goto mandos_end;
787
819
        }
788
820
        break;
790
822
        fprintf(stderr, "Unknown error while reading data from"
791
823
                " encrypted session with Mandos server\n");
792
824
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
825
        errno = EIO;
793
826
        goto mandos_end;
794
827
      }
795
828
    } else {
802
835
  }
803
836
  
804
837
  if(quit_now){
 
838
    errno = EINTR;
805
839
    goto mandos_end;
806
840
  }
807
841
  
808
842
  do {
809
843
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
810
844
    if(quit_now){
 
845
      errno = EINTR;
811
846
      goto mandos_end;
812
847
    }
813
848
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
822
857
      written = 0;
823
858
      while(written < (size_t) decrypted_buffer_size){
824
859
        if(quit_now){
 
860
          errno = EINTR;
825
861
          goto mandos_end;
826
862
        }
827
863
        
829
865
                          (size_t)decrypted_buffer_size - written,
830
866
                          stdout);
831
867
        if(ret == 0 and ferror(stdout)){
 
868
          int e = errno;
832
869
          if(debug){
833
870
            fprintf(stderr, "Error writing encrypted data: %s\n",
834
871
                    strerror(errno));
835
872
          }
 
873
          errno = e;
836
874
          goto mandos_end;
837
875
        }
838
876
        written += (size_t)ret;
844
882
  /* Shutdown procedure */
845
883
  
846
884
 mandos_end:
847
 
  free(decrypted_buffer);
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;
 
885
  {
 
886
    int e = errno;
 
887
    free(decrypted_buffer);
 
888
    free(buffer);
 
889
    if(tcp_sd >= 0){
 
890
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
891
    }
 
892
    if(ret == -1){
 
893
      if(e == 0){
 
894
        e = errno;
 
895
      }
 
896
      perror("close");
 
897
    }
 
898
    gnutls_deinit(session);
 
899
    if(quit_now){
 
900
      e = EINTR;
 
901
      retval = -1;
 
902
    }
 
903
    errno = e;
858
904
  }
859
905
  return retval;
860
906
}
1001
1047
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1002
1048
  /* read line from flags_fd */
1003
1049
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1004
 
  char *flagstring = malloc((size_t)to_read);
 
1050
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
 
1051
  flagstring[(size_t)to_read] = '\0';
1005
1052
  if(flagstring == NULL){
1006
1053
    perror("malloc");
1007
1054
    close(flags_fd);
1029
1076
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1030
1077
                                         and not (isspace(*tmp)))
1031
1078
     or tmpmax != (ifreq_flags)tmpmax){
 
1079
    if(debug){
 
1080
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
 
1081
              flagstring, if_entry->d_name);
 
1082
    }
1032
1083
    free(flagstring);
1033
1084
    return 0;
1034
1085
  }
1036
1087
  ifreq_flags flags = (ifreq_flags)tmpmax;
1037
1088
  /* Reject the loopback device */
1038
1089
  if(flags & IFF_LOOPBACK){
 
1090
    if(debug){
 
1091
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1092
              if_entry->d_name);
 
1093
    }
1039
1094
    return 0;
1040
1095
  }
1041
1096
  /* Accept point-to-point devices only if connect_to is specified */
1042
1097
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1098
    if(debug){
 
1099
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1100
              if_entry->d_name);
 
1101
    }
1043
1102
    return 1;
1044
1103
  }
1045
1104
  /* Otherwise, reject non-broadcast-capable devices */
1046
1105
  if(not (flags & IFF_BROADCAST)){
 
1106
    if(debug){
 
1107
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1108
              if_entry->d_name);
 
1109
    }
1047
1110
    return 0;
1048
1111
  }
1049
1112
  /* Accept this device */
 
1113
  if(debug){
 
1114
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
 
1115
            if_entry->d_name);
 
1116
  }
1050
1117
  return 1;
1051
1118
}
1052
1119
 
1131
1198
        .arg = "SECONDS",
1132
1199
        .doc = "Maximum delay to wait for interface startup",
1133
1200
        .group = 2 },
 
1201
      /*
 
1202
       * These reproduce what we would get without ARGP_NO_HELP
 
1203
       */
 
1204
      { .name = "help", .key = '?',
 
1205
        .doc = "Give this help list", .group = -1 },
 
1206
      { .name = "usage", .key = -3,
 
1207
        .doc = "Give a short usage message", .group = -1 },
 
1208
      { .name = "version", .key = 'V',
 
1209
        .doc = "Print program version", .group = -1 },
1134
1210
      { .name = NULL }
1135
1211
    };
1136
1212
    
1137
1213
    error_t parse_opt(int key, char *arg,
1138
1214
                      struct argp_state *state){
 
1215
      errno = 0;
1139
1216
      switch(key){
1140
1217
      case 128:                 /* --debug */
1141
1218
        debug = true;
1157
1234
        tmpmax = strtoimax(arg, &tmp, 10);
1158
1235
        if(errno != 0 or tmp == arg or *tmp != '\0'
1159
1236
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1160
 
          fprintf(stderr, "Bad number of DH bits\n");
1161
 
          exit(EXIT_FAILURE);
 
1237
          argp_error(state, "Bad number of DH bits");
1162
1238
        }
1163
1239
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1164
1240
        break;
1169
1245
        errno = 0;
1170
1246
        delay = strtof(arg, &tmp);
1171
1247
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1172
 
          fprintf(stderr, "Bad delay\n");
1173
 
          exit(EXIT_FAILURE);
 
1248
          argp_error(state, "Bad delay");
1174
1249
        }
1175
1250
        break;
1176
 
      case ARGP_KEY_ARG:
1177
 
        argp_usage(state);
1178
 
      case ARGP_KEY_END:
 
1251
        /*
 
1252
         * These reproduce what we would get without ARGP_NO_HELP
 
1253
         */
 
1254
      case '?':                 /* --help */
 
1255
        argp_state_help(state, state->out_stream,
 
1256
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
 
1257
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
 
1258
      case -3:                  /* --usage */
 
1259
        argp_state_help(state, state->out_stream,
 
1260
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
 
1261
      case 'V':                 /* --version */
 
1262
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
1263
        exit(argp_err_exit_status);
1179
1264
        break;
1180
1265
      default:
1181
1266
        return ARGP_ERR_UNKNOWN;
1182
1267
      }
1183
 
      return 0;
 
1268
      return errno;
1184
1269
    }
1185
1270
    
1186
1271
    struct argp argp = { .options = options, .parser = parse_opt,
1187
1272
                         .args_doc = "",
1188
1273
                         .doc = "Mandos client -- Get and decrypt"
1189
1274
                         " passwords from a Mandos server" };
1190
 
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1191
 
    if(ret == ARGP_ERR_UNKNOWN){
1192
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
1193
 
      exitcode = EXIT_FAILURE;
 
1275
    ret = argp_parse(&argp, argc, argv,
 
1276
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
1277
    switch(ret){
 
1278
    case 0:
 
1279
      break;
 
1280
    case ENOMEM:
 
1281
    default:
 
1282
      errno = ret;
 
1283
      perror("argp_parse");
 
1284
      exitcode = EX_OSERR;
 
1285
      goto end;
 
1286
    case EINVAL:
 
1287
      exitcode = EX_USAGE;
1194
1288
      goto end;
1195
1289
    }
1196
1290
  }
1206
1300
    if(ret >= 1){
1207
1301
      /* Pick the first good interface */
1208
1302
      interface = strdup(direntries[0]->d_name);
 
1303
      if(debug){
 
1304
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1305
      }
1209
1306
      if(interface == NULL){
1210
1307
        perror("malloc");
1211
1308
        free(direntries);
1228
1325
  mc.simple_poll = avahi_simple_poll_new();
1229
1326
  if(mc.simple_poll == NULL){
1230
1327
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1231
 
    exitcode = EXIT_FAILURE;
 
1328
    exitcode = EX_UNAVAILABLE;
1232
1329
    goto end;
1233
1330
  }
1234
1331
  
1236
1333
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1237
1334
  if(ret == -1){
1238
1335
    perror("sigaddset");
1239
 
    exitcode = EXIT_FAILURE;
 
1336
    exitcode = EX_OSERR;
1240
1337
    goto end;
1241
1338
  }
1242
1339
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1243
1340
  if(ret == -1){
1244
1341
    perror("sigaddset");
1245
 
    exitcode = EXIT_FAILURE;
 
1342
    exitcode = EX_OSERR;
1246
1343
    goto end;
1247
1344
  }
1248
1345
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1249
1346
  if(ret == -1){
1250
1347
    perror("sigaddset");
1251
 
    exitcode = EXIT_FAILURE;
 
1348
    exitcode = EX_OSERR;
1252
1349
    goto end;
1253
1350
  }
1254
1351
  /* Need to check if the handler is SIG_IGN before handling:
1258
1355
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1259
1356
  if(ret == -1){
1260
1357
    perror("sigaction");
1261
 
    return EXIT_FAILURE;
 
1358
    return EX_OSERR;
1262
1359
  }
1263
1360
  if(old_sigterm_action.sa_handler != SIG_IGN){
1264
1361
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1265
1362
    if(ret == -1){
1266
1363
      perror("sigaction");
1267
 
      exitcode = EXIT_FAILURE;
 
1364
      exitcode = EX_OSERR;
1268
1365
      goto end;
1269
1366
    }
1270
1367
  }
1271
1368
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1272
1369
  if(ret == -1){
1273
1370
    perror("sigaction");
1274
 
    return EXIT_FAILURE;
 
1371
    return EX_OSERR;
1275
1372
  }
1276
1373
  if(old_sigterm_action.sa_handler != SIG_IGN){
1277
1374
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1278
1375
    if(ret == -1){
1279
1376
      perror("sigaction");
1280
 
      exitcode = EXIT_FAILURE;
 
1377
      exitcode = EX_OSERR;
1281
1378
      goto end;
1282
1379
    }
1283
1380
  }
1284
1381
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1285
1382
  if(ret == -1){
1286
1383
    perror("sigaction");
1287
 
    return EXIT_FAILURE;
 
1384
    return EX_OSERR;
1288
1385
  }
1289
1386
  if(old_sigterm_action.sa_handler != SIG_IGN){
1290
1387
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1291
1388
    if(ret == -1){
1292
1389
      perror("sigaction");
1293
 
      exitcode = EXIT_FAILURE;
 
1390
      exitcode = EX_OSERR;
1294
1391
      goto end;
1295
1392
    }
1296
1393
  }
1300
1397
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1301
1398
    if(if_index == 0){
1302
1399
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1303
 
      exitcode = EXIT_FAILURE;
 
1400
      exitcode = EX_UNAVAILABLE;
1304
1401
      goto end;
1305
1402
    }
1306
1403
    
1317
1414
    
1318
1415
#ifdef __linux__
1319
1416
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1320
 
       messages to mess up the prompt */
 
1417
       messages about the network interface to mess up the prompt */
1321
1418
    ret = klogctl(8, NULL, 5);
1322
1419
    bool restore_loglevel = true;
1323
1420
    if(ret == -1){
1329
1426
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1330
1427
    if(sd < 0){
1331
1428
      perror("socket");
1332
 
      exitcode = EXIT_FAILURE;
 
1429
      exitcode = EX_OSERR;
1333
1430
#ifdef __linux__
1334
1431
      if(restore_loglevel){
1335
1432
        ret = klogctl(7, NULL, 0);
1358
1455
        }
1359
1456
      }
1360
1457
#endif  /* __linux__ */
1361
 
      exitcode = EXIT_FAILURE;
 
1458
      exitcode = EX_OSERR;
1362
1459
      /* Lower privileges */
1363
1460
      errno = 0;
1364
1461
      ret = seteuid(uid);
1373
1470
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1374
1471
      if(ret == -1){
1375
1472
        take_down_interface = false;
1376
 
        perror("ioctl SIOCSIFFLAGS");
1377
 
        exitcode = EXIT_FAILURE;
 
1473
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1474
        exitcode = EX_OSERR;
1378
1475
#ifdef __linux__
1379
1476
        if(restore_loglevel){
1380
1477
          ret = klogctl(7, NULL, 0);
1446
1543
  ret = init_gnutls_global(pubkey, seckey);
1447
1544
  if(ret == -1){
1448
1545
    fprintf(stderr, "init_gnutls_global failed\n");
1449
 
    exitcode = EXIT_FAILURE;
 
1546
    exitcode = EX_UNAVAILABLE;
1450
1547
    goto end;
1451
1548
  } else {
1452
1549
    gnutls_initialized = true;
1469
1566
  
1470
1567
  if(not init_gpgme(pubkey, seckey, tempdir)){
1471
1568
    fprintf(stderr, "init_gpgme failed\n");
1472
 
    exitcode = EXIT_FAILURE;
 
1569
    exitcode = EX_UNAVAILABLE;
1473
1570
    goto end;
1474
1571
  } else {
1475
1572
    gpgme_initialized = true;
1485
1582
    char *address = strrchr(connect_to, ':');
1486
1583
    if(address == NULL){
1487
1584
      fprintf(stderr, "No colon in address\n");
1488
 
      exitcode = EXIT_FAILURE;
 
1585
      exitcode = EX_USAGE;
1489
1586
      goto end;
1490
1587
    }
1491
1588
    
1499
1596
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1500
1597
       or tmpmax != (uint16_t)tmpmax){
1501
1598
      fprintf(stderr, "Bad port number\n");
1502
 
      exitcode = EXIT_FAILURE;
 
1599
      exitcode = EX_USAGE;
1503
1600
      goto end;
1504
1601
    }
1505
1602
  
1524
1621
    
1525
1622
    ret = start_mandos_communication(address, port, if_index, af);
1526
1623
    if(ret < 0){
1527
 
      exitcode = EXIT_FAILURE;
 
1624
      switch(errno){
 
1625
      case ENETUNREACH:
 
1626
      case EHOSTDOWN:
 
1627
      case EHOSTUNREACH:
 
1628
        exitcode = EX_NOHOST;
 
1629
        break;
 
1630
      case EINVAL:
 
1631
        exitcode = EX_USAGE;
 
1632
        break;
 
1633
      case EIO:
 
1634
        exitcode = EX_IOERR;
 
1635
        break;
 
1636
      case EPROTO:
 
1637
        exitcode = EX_PROTOCOL;
 
1638
        break;
 
1639
      default:
 
1640
        exitcode = EX_OSERR;
 
1641
        break;
 
1642
      }
1528
1643
    } else {
1529
1644
      exitcode = EXIT_SUCCESS;
1530
1645
    }
1557
1672
  if(mc.server == NULL){
1558
1673
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1559
1674
            avahi_strerror(error));
1560
 
    exitcode = EXIT_FAILURE;
 
1675
    exitcode = EX_UNAVAILABLE;
1561
1676
    goto end;
1562
1677
  }
1563
1678
  
1572
1687
  if(sb == NULL){
1573
1688
    fprintf(stderr, "Failed to create service browser: %s\n",
1574
1689
            avahi_strerror(avahi_server_errno(mc.server)));
1575
 
    exitcode = EXIT_FAILURE;
 
1690
    exitcode = EX_UNAVAILABLE;
1576
1691
    goto end;
1577
1692
  }
1578
1693
  
1627
1742
      if(ret == -1){
1628
1743
        perror("ioctl SIOCGIFFLAGS");
1629
1744
      } else if(network.ifr_flags & IFF_UP) {
1630
 
        network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1745
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1631
1746
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1632
1747
        if(ret == -1){
1633
 
          perror("ioctl SIOCSIFFLAGS");
 
1748
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1634
1749
        }
1635
1750
      }
1636
1751
      ret = (int)TEMP_FAILURE_RETRY(close(sd));