/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

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
}
1152
1198
        .arg = "SECONDS",
1153
1199
        .doc = "Maximum delay to wait for interface startup",
1154
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 },
1155
1210
      { .name = NULL }
1156
1211
    };
1157
1212
    
1158
1213
    error_t parse_opt(int key, char *arg,
1159
1214
                      struct argp_state *state){
 
1215
      errno = 0;
1160
1216
      switch(key){
1161
1217
      case 128:                 /* --debug */
1162
1218
        debug = true;
1178
1234
        tmpmax = strtoimax(arg, &tmp, 10);
1179
1235
        if(errno != 0 or tmp == arg or *tmp != '\0'
1180
1236
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1181
 
          fprintf(stderr, "Bad number of DH bits\n");
1182
 
          exit(EXIT_FAILURE);
 
1237
          argp_error(state, "Bad number of DH bits");
1183
1238
        }
1184
1239
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1185
1240
        break;
1190
1245
        errno = 0;
1191
1246
        delay = strtof(arg, &tmp);
1192
1247
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1193
 
          fprintf(stderr, "Bad delay\n");
1194
 
          exit(EXIT_FAILURE);
 
1248
          argp_error(state, "Bad delay");
1195
1249
        }
1196
1250
        break;
1197
 
      case ARGP_KEY_ARG:
1198
 
        argp_usage(state);
1199
 
      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);
1200
1264
        break;
1201
1265
      default:
1202
1266
        return ARGP_ERR_UNKNOWN;
1203
1267
      }
1204
 
      return 0;
 
1268
      return errno;
1205
1269
    }
1206
1270
    
1207
1271
    struct argp argp = { .options = options, .parser = parse_opt,
1208
1272
                         .args_doc = "",
1209
1273
                         .doc = "Mandos client -- Get and decrypt"
1210
1274
                         " passwords from a Mandos server" };
1211
 
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1212
 
    if(ret == ARGP_ERR_UNKNOWN){
1213
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
1214
 
      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;
1215
1288
      goto end;
1216
1289
    }
1217
1290
  }
1252
1325
  mc.simple_poll = avahi_simple_poll_new();
1253
1326
  if(mc.simple_poll == NULL){
1254
1327
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1255
 
    exitcode = EXIT_FAILURE;
 
1328
    exitcode = EX_UNAVAILABLE;
1256
1329
    goto end;
1257
1330
  }
1258
1331
  
1260
1333
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1261
1334
  if(ret == -1){
1262
1335
    perror("sigaddset");
1263
 
    exitcode = EXIT_FAILURE;
 
1336
    exitcode = EX_OSERR;
1264
1337
    goto end;
1265
1338
  }
1266
1339
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1267
1340
  if(ret == -1){
1268
1341
    perror("sigaddset");
1269
 
    exitcode = EXIT_FAILURE;
 
1342
    exitcode = EX_OSERR;
1270
1343
    goto end;
1271
1344
  }
1272
1345
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1273
1346
  if(ret == -1){
1274
1347
    perror("sigaddset");
1275
 
    exitcode = EXIT_FAILURE;
 
1348
    exitcode = EX_OSERR;
1276
1349
    goto end;
1277
1350
  }
1278
1351
  /* Need to check if the handler is SIG_IGN before handling:
1282
1355
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1283
1356
  if(ret == -1){
1284
1357
    perror("sigaction");
1285
 
    return EXIT_FAILURE;
 
1358
    return EX_OSERR;
1286
1359
  }
1287
1360
  if(old_sigterm_action.sa_handler != SIG_IGN){
1288
1361
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1289
1362
    if(ret == -1){
1290
1363
      perror("sigaction");
1291
 
      exitcode = EXIT_FAILURE;
 
1364
      exitcode = EX_OSERR;
1292
1365
      goto end;
1293
1366
    }
1294
1367
  }
1295
1368
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1296
1369
  if(ret == -1){
1297
1370
    perror("sigaction");
1298
 
    return EXIT_FAILURE;
 
1371
    return EX_OSERR;
1299
1372
  }
1300
1373
  if(old_sigterm_action.sa_handler != SIG_IGN){
1301
1374
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1302
1375
    if(ret == -1){
1303
1376
      perror("sigaction");
1304
 
      exitcode = EXIT_FAILURE;
 
1377
      exitcode = EX_OSERR;
1305
1378
      goto end;
1306
1379
    }
1307
1380
  }
1308
1381
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1309
1382
  if(ret == -1){
1310
1383
    perror("sigaction");
1311
 
    return EXIT_FAILURE;
 
1384
    return EX_OSERR;
1312
1385
  }
1313
1386
  if(old_sigterm_action.sa_handler != SIG_IGN){
1314
1387
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1315
1388
    if(ret == -1){
1316
1389
      perror("sigaction");
1317
 
      exitcode = EXIT_FAILURE;
 
1390
      exitcode = EX_OSERR;
1318
1391
      goto end;
1319
1392
    }
1320
1393
  }
1324
1397
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1325
1398
    if(if_index == 0){
1326
1399
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1327
 
      exitcode = EXIT_FAILURE;
 
1400
      exitcode = EX_UNAVAILABLE;
1328
1401
      goto end;
1329
1402
    }
1330
1403
    
1341
1414
    
1342
1415
#ifdef __linux__
1343
1416
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1344
 
       messages to mess up the prompt */
 
1417
       messages about the network interface to mess up the prompt */
1345
1418
    ret = klogctl(8, NULL, 5);
1346
1419
    bool restore_loglevel = true;
1347
1420
    if(ret == -1){
1353
1426
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1354
1427
    if(sd < 0){
1355
1428
      perror("socket");
1356
 
      exitcode = EXIT_FAILURE;
 
1429
      exitcode = EX_OSERR;
1357
1430
#ifdef __linux__
1358
1431
      if(restore_loglevel){
1359
1432
        ret = klogctl(7, NULL, 0);
1382
1455
        }
1383
1456
      }
1384
1457
#endif  /* __linux__ */
1385
 
      exitcode = EXIT_FAILURE;
 
1458
      exitcode = EX_OSERR;
1386
1459
      /* Lower privileges */
1387
1460
      errno = 0;
1388
1461
      ret = seteuid(uid);
1398
1471
      if(ret == -1){
1399
1472
        take_down_interface = false;
1400
1473
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1401
 
        exitcode = EXIT_FAILURE;
 
1474
        exitcode = EX_OSERR;
1402
1475
#ifdef __linux__
1403
1476
        if(restore_loglevel){
1404
1477
          ret = klogctl(7, NULL, 0);
1470
1543
  ret = init_gnutls_global(pubkey, seckey);
1471
1544
  if(ret == -1){
1472
1545
    fprintf(stderr, "init_gnutls_global failed\n");
1473
 
    exitcode = EXIT_FAILURE;
 
1546
    exitcode = EX_UNAVAILABLE;
1474
1547
    goto end;
1475
1548
  } else {
1476
1549
    gnutls_initialized = true;
1493
1566
  
1494
1567
  if(not init_gpgme(pubkey, seckey, tempdir)){
1495
1568
    fprintf(stderr, "init_gpgme failed\n");
1496
 
    exitcode = EXIT_FAILURE;
 
1569
    exitcode = EX_UNAVAILABLE;
1497
1570
    goto end;
1498
1571
  } else {
1499
1572
    gpgme_initialized = true;
1509
1582
    char *address = strrchr(connect_to, ':');
1510
1583
    if(address == NULL){
1511
1584
      fprintf(stderr, "No colon in address\n");
1512
 
      exitcode = EXIT_FAILURE;
 
1585
      exitcode = EX_USAGE;
1513
1586
      goto end;
1514
1587
    }
1515
1588
    
1523
1596
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1524
1597
       or tmpmax != (uint16_t)tmpmax){
1525
1598
      fprintf(stderr, "Bad port number\n");
1526
 
      exitcode = EXIT_FAILURE;
 
1599
      exitcode = EX_USAGE;
1527
1600
      goto end;
1528
1601
    }
1529
1602
  
1548
1621
    
1549
1622
    ret = start_mandos_communication(address, port, if_index, af);
1550
1623
    if(ret < 0){
1551
 
      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
      }
1552
1643
    } else {
1553
1644
      exitcode = EXIT_SUCCESS;
1554
1645
    }
1581
1672
  if(mc.server == NULL){
1582
1673
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1583
1674
            avahi_strerror(error));
1584
 
    exitcode = EXIT_FAILURE;
 
1675
    exitcode = EX_UNAVAILABLE;
1585
1676
    goto end;
1586
1677
  }
1587
1678
  
1596
1687
  if(sb == NULL){
1597
1688
    fprintf(stderr, "Failed to create service browser: %s\n",
1598
1689
            avahi_strerror(avahi_server_errno(mc.server)));
1599
 
    exitcode = EXIT_FAILURE;
 
1690
    exitcode = EX_UNAVAILABLE;
1600
1691
    goto end;
1601
1692
  }
1602
1693
  
1651
1742
      if(ret == -1){
1652
1743
        perror("ioctl SIOCGIFFLAGS");
1653
1744
      } else if(network.ifr_flags & IFF_UP) {
1654
 
        network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1745
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1655
1746
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1656
1747
        if(ret == -1){
1657
1748
          perror("ioctl SIOCSIFFLAGS -IFF_UP");