/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

Tags: version-1.2-1
* Makefile (version): Changed to "1.2".
* NEWS (Version 1.2): New entry.
* debian/changelog (1.2): - '' -

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, 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){
677
 
    perror("connect");
 
693
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
694
      int e = errno;
 
695
      perror("connect");
 
696
      errno = e;
 
697
    }
678
698
    goto mandos_end;
679
699
  }
680
700
  
681
701
  if(quit_now){
 
702
    errno = EINTR;
682
703
    goto mandos_end;
683
704
  }
684
705
  
689
710
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
690
711
                                   out_size - written));
691
712
    if(ret == -1){
 
713
      int e = errno;
692
714
      perror("write");
 
715
      errno = e;
693
716
      goto mandos_end;
694
717
    }
695
718
    written += (size_t)ret;
705
728
    }
706
729
  
707
730
    if(quit_now){
 
731
      errno = EINTR;
708
732
      goto mandos_end;
709
733
    }
710
734
  }
714
738
  }
715
739
  
716
740
  if(quit_now){
 
741
    errno = EINTR;
717
742
    goto mandos_end;
718
743
  }
719
744
  
720
745
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
721
746
  
722
747
  if(quit_now){
 
748
    errno = EINTR;
723
749
    goto mandos_end;
724
750
  }
725
751
  
726
752
  do {
727
753
    ret = gnutls_handshake(session);
728
754
    if(quit_now){
 
755
      errno = EINTR;
729
756
      goto mandos_end;
730
757
    }
731
758
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
735
762
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
736
763
      gnutls_perror(ret);
737
764
    }
 
765
    errno = EPROTO;
738
766
    goto mandos_end;
739
767
  }
740
768
  
748
776
  while(true){
749
777
    
750
778
    if(quit_now){
 
779
      errno = EINTR;
751
780
      goto mandos_end;
752
781
    }
753
782
    
754
783
    buffer_capacity = incbuffer(&buffer, buffer_length,
755
784
                                   buffer_capacity);
756
785
    if(buffer_capacity == 0){
 
786
      int e = errno;
757
787
      perror("incbuffer");
 
788
      errno = e;
758
789
      goto mandos_end;
759
790
    }
760
791
    
761
792
    if(quit_now){
 
793
      errno = EINTR;
762
794
      goto mandos_end;
763
795
    }
764
796
    
777
809
          ret = gnutls_handshake(session);
778
810
          
779
811
          if(quit_now){
 
812
            errno = EINTR;
780
813
            goto mandos_end;
781
814
          }
782
815
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
783
816
        if(ret < 0){
784
817
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
785
818
          gnutls_perror(ret);
 
819
          errno = EPROTO;
786
820
          goto mandos_end;
787
821
        }
788
822
        break;
790
824
        fprintf(stderr, "Unknown error while reading data from"
791
825
                " encrypted session with Mandos server\n");
792
826
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
827
        errno = EIO;
793
828
        goto mandos_end;
794
829
      }
795
830
    } else {
802
837
  }
803
838
  
804
839
  if(quit_now){
 
840
    errno = EINTR;
805
841
    goto mandos_end;
806
842
  }
807
843
  
808
844
  do {
809
845
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
810
846
    if(quit_now){
 
847
      errno = EINTR;
811
848
      goto mandos_end;
812
849
    }
813
850
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
822
859
      written = 0;
823
860
      while(written < (size_t) decrypted_buffer_size){
824
861
        if(quit_now){
 
862
          errno = EINTR;
825
863
          goto mandos_end;
826
864
        }
827
865
        
829
867
                          (size_t)decrypted_buffer_size - written,
830
868
                          stdout);
831
869
        if(ret == 0 and ferror(stdout)){
 
870
          int e = errno;
832
871
          if(debug){
833
872
            fprintf(stderr, "Error writing encrypted data: %s\n",
834
873
                    strerror(errno));
835
874
          }
 
875
          errno = e;
836
876
          goto mandos_end;
837
877
        }
838
878
        written += (size_t)ret;
844
884
  /* Shutdown procedure */
845
885
  
846
886
 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;
 
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;
858
906
  }
859
907
  return retval;
860
908
}
1152
1200
        .arg = "SECONDS",
1153
1201
        .doc = "Maximum delay to wait for interface startup",
1154
1202
        .group = 2 },
 
1203
      /*
 
1204
       * These reproduce what we would get without ARGP_NO_HELP
 
1205
       */
 
1206
      { .name = "help", .key = '?',
 
1207
        .doc = "Give this help list", .group = -1 },
 
1208
      { .name = "usage", .key = -3,
 
1209
        .doc = "Give a short usage message", .group = -1 },
 
1210
      { .name = "version", .key = 'V',
 
1211
        .doc = "Print program version", .group = -1 },
1155
1212
      { .name = NULL }
1156
1213
    };
1157
1214
    
1158
1215
    error_t parse_opt(int key, char *arg,
1159
1216
                      struct argp_state *state){
 
1217
      errno = 0;
1160
1218
      switch(key){
1161
1219
      case 128:                 /* --debug */
1162
1220
        debug = true;
1178
1236
        tmpmax = strtoimax(arg, &tmp, 10);
1179
1237
        if(errno != 0 or tmp == arg or *tmp != '\0'
1180
1238
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1181
 
          fprintf(stderr, "Bad number of DH bits\n");
1182
 
          exit(EXIT_FAILURE);
 
1239
          argp_error(state, "Bad number of DH bits");
1183
1240
        }
1184
1241
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1185
1242
        break;
1190
1247
        errno = 0;
1191
1248
        delay = strtof(arg, &tmp);
1192
1249
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1193
 
          fprintf(stderr, "Bad delay\n");
1194
 
          exit(EXIT_FAILURE);
 
1250
          argp_error(state, "Bad delay");
1195
1251
        }
1196
1252
        break;
1197
 
      case ARGP_KEY_ARG:
1198
 
        argp_usage(state);
1199
 
      case ARGP_KEY_END:
 
1253
        /*
 
1254
         * These reproduce what we would get without ARGP_NO_HELP
 
1255
         */
 
1256
      case '?':                 /* --help */
 
1257
        argp_state_help(state, state->out_stream,
 
1258
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
 
1259
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
 
1260
      case -3:                  /* --usage */
 
1261
        argp_state_help(state, state->out_stream,
 
1262
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
 
1263
      case 'V':                 /* --version */
 
1264
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
1265
        exit(argp_err_exit_status);
1200
1266
        break;
1201
1267
      default:
1202
1268
        return ARGP_ERR_UNKNOWN;
1203
1269
      }
1204
 
      return 0;
 
1270
      return errno;
1205
1271
    }
1206
1272
    
1207
1273
    struct argp argp = { .options = options, .parser = parse_opt,
1208
1274
                         .args_doc = "",
1209
1275
                         .doc = "Mandos client -- Get and decrypt"
1210
1276
                         " 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;
 
1277
    ret = argp_parse(&argp, argc, argv,
 
1278
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
1279
    switch(ret){
 
1280
    case 0:
 
1281
      break;
 
1282
    case ENOMEM:
 
1283
    default:
 
1284
      errno = ret;
 
1285
      perror("argp_parse");
 
1286
      exitcode = EX_OSERR;
 
1287
      goto end;
 
1288
    case EINVAL:
 
1289
      exitcode = EX_USAGE;
1215
1290
      goto end;
1216
1291
    }
1217
1292
  }
1252
1327
  mc.simple_poll = avahi_simple_poll_new();
1253
1328
  if(mc.simple_poll == NULL){
1254
1329
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1255
 
    exitcode = EXIT_FAILURE;
 
1330
    exitcode = EX_UNAVAILABLE;
1256
1331
    goto end;
1257
1332
  }
1258
1333
  
1260
1335
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1261
1336
  if(ret == -1){
1262
1337
    perror("sigaddset");
1263
 
    exitcode = EXIT_FAILURE;
 
1338
    exitcode = EX_OSERR;
1264
1339
    goto end;
1265
1340
  }
1266
1341
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1267
1342
  if(ret == -1){
1268
1343
    perror("sigaddset");
1269
 
    exitcode = EXIT_FAILURE;
 
1344
    exitcode = EX_OSERR;
1270
1345
    goto end;
1271
1346
  }
1272
1347
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1273
1348
  if(ret == -1){
1274
1349
    perror("sigaddset");
1275
 
    exitcode = EXIT_FAILURE;
 
1350
    exitcode = EX_OSERR;
1276
1351
    goto end;
1277
1352
  }
1278
1353
  /* Need to check if the handler is SIG_IGN before handling:
1282
1357
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1283
1358
  if(ret == -1){
1284
1359
    perror("sigaction");
1285
 
    return EXIT_FAILURE;
 
1360
    return EX_OSERR;
1286
1361
  }
1287
1362
  if(old_sigterm_action.sa_handler != SIG_IGN){
1288
1363
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1289
1364
    if(ret == -1){
1290
1365
      perror("sigaction");
1291
 
      exitcode = EXIT_FAILURE;
 
1366
      exitcode = EX_OSERR;
1292
1367
      goto end;
1293
1368
    }
1294
1369
  }
1295
1370
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1296
1371
  if(ret == -1){
1297
1372
    perror("sigaction");
1298
 
    return EXIT_FAILURE;
 
1373
    return EX_OSERR;
1299
1374
  }
1300
1375
  if(old_sigterm_action.sa_handler != SIG_IGN){
1301
1376
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1302
1377
    if(ret == -1){
1303
1378
      perror("sigaction");
1304
 
      exitcode = EXIT_FAILURE;
 
1379
      exitcode = EX_OSERR;
1305
1380
      goto end;
1306
1381
    }
1307
1382
  }
1308
1383
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1309
1384
  if(ret == -1){
1310
1385
    perror("sigaction");
1311
 
    return EXIT_FAILURE;
 
1386
    return EX_OSERR;
1312
1387
  }
1313
1388
  if(old_sigterm_action.sa_handler != SIG_IGN){
1314
1389
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1315
1390
    if(ret == -1){
1316
1391
      perror("sigaction");
1317
 
      exitcode = EXIT_FAILURE;
 
1392
      exitcode = EX_OSERR;
1318
1393
      goto end;
1319
1394
    }
1320
1395
  }
1324
1399
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1325
1400
    if(if_index == 0){
1326
1401
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1327
 
      exitcode = EXIT_FAILURE;
 
1402
      exitcode = EX_UNAVAILABLE;
1328
1403
      goto end;
1329
1404
    }
1330
1405
    
1341
1416
    
1342
1417
#ifdef __linux__
1343
1418
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1344
 
       messages to mess up the prompt */
 
1419
       messages about the network interface to mess up the prompt */
1345
1420
    ret = klogctl(8, NULL, 5);
1346
1421
    bool restore_loglevel = true;
1347
1422
    if(ret == -1){
1353
1428
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1354
1429
    if(sd < 0){
1355
1430
      perror("socket");
1356
 
      exitcode = EXIT_FAILURE;
 
1431
      exitcode = EX_OSERR;
1357
1432
#ifdef __linux__
1358
1433
      if(restore_loglevel){
1359
1434
        ret = klogctl(7, NULL, 0);
1382
1457
        }
1383
1458
      }
1384
1459
#endif  /* __linux__ */
1385
 
      exitcode = EXIT_FAILURE;
 
1460
      exitcode = EX_OSERR;
1386
1461
      /* Lower privileges */
1387
1462
      errno = 0;
1388
1463
      ret = seteuid(uid);
1398
1473
      if(ret == -1){
1399
1474
        take_down_interface = false;
1400
1475
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1401
 
        exitcode = EXIT_FAILURE;
 
1476
        exitcode = EX_OSERR;
1402
1477
#ifdef __linux__
1403
1478
        if(restore_loglevel){
1404
1479
          ret = klogctl(7, NULL, 0);
1470
1545
  ret = init_gnutls_global(pubkey, seckey);
1471
1546
  if(ret == -1){
1472
1547
    fprintf(stderr, "init_gnutls_global failed\n");
1473
 
    exitcode = EXIT_FAILURE;
 
1548
    exitcode = EX_UNAVAILABLE;
1474
1549
    goto end;
1475
1550
  } else {
1476
1551
    gnutls_initialized = true;
1493
1568
  
1494
1569
  if(not init_gpgme(pubkey, seckey, tempdir)){
1495
1570
    fprintf(stderr, "init_gpgme failed\n");
1496
 
    exitcode = EXIT_FAILURE;
 
1571
    exitcode = EX_UNAVAILABLE;
1497
1572
    goto end;
1498
1573
  } else {
1499
1574
    gpgme_initialized = true;
1509
1584
    char *address = strrchr(connect_to, ':');
1510
1585
    if(address == NULL){
1511
1586
      fprintf(stderr, "No colon in address\n");
1512
 
      exitcode = EXIT_FAILURE;
 
1587
      exitcode = EX_USAGE;
1513
1588
      goto end;
1514
1589
    }
1515
1590
    
1523
1598
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1524
1599
       or tmpmax != (uint16_t)tmpmax){
1525
1600
      fprintf(stderr, "Bad port number\n");
1526
 
      exitcode = EXIT_FAILURE;
 
1601
      exitcode = EX_USAGE;
1527
1602
      goto end;
1528
1603
    }
1529
1604
  
1545
1620
    if(quit_now){
1546
1621
      goto end;
1547
1622
    }
1548
 
    
1549
 
    ret = start_mandos_communication(address, port, if_index, af);
1550
 
    if(ret < 0){
1551
 
      exitcode = EXIT_FAILURE;
1552
 
    } 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){
1553
1633
      exitcode = EXIT_SUCCESS;
1554
1634
    }
 
1635
 
1555
1636
    goto end;
1556
1637
  }
1557
1638
  
1581
1662
  if(mc.server == NULL){
1582
1663
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1583
1664
            avahi_strerror(error));
1584
 
    exitcode = EXIT_FAILURE;
 
1665
    exitcode = EX_UNAVAILABLE;
1585
1666
    goto end;
1586
1667
  }
1587
1668
  
1596
1677
  if(sb == NULL){
1597
1678
    fprintf(stderr, "Failed to create service browser: %s\n",
1598
1679
            avahi_strerror(avahi_server_errno(mc.server)));
1599
 
    exitcode = EXIT_FAILURE;
 
1680
    exitcode = EX_UNAVAILABLE;
1600
1681
    goto end;
1601
1682
  }
1602
1683
  
1651
1732
      if(ret == -1){
1652
1733
        perror("ioctl SIOCGIFFLAGS");
1653
1734
      } else if(network.ifr_flags & IFF_UP) {
1654
 
        network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1735
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1655
1736
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1656
1737
        if(ret == -1){
1657
1738
          perror("ioctl SIOCSIFFLAGS -IFF_UP");