/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-09-19 17:41:18 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090919174118-274yt9wptmtx0ykn
* plugins.d/password-prompt.c (main): Fix "-Wconversion" warning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
 
                                   srand(), strtof() */
 
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() */
71
71
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
72
                                */
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
 
                                   getuid(), getgid(), setuid(),
75
 
                                   setgid() */
 
74
                                   getuid(), getgid(), seteuid(),
 
75
                                   setgid(), pause() */
76
76
#include <arpa/inet.h>          /* inet_pton(), htons */
77
77
#include <iso646.h>             /* not, or, and */
78
78
#include <argp.h>               /* struct argp_option, error_t, struct
142
142
                      .dh_bits = 1024, .priority = "SECURE256"
143
143
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
144
144
 
 
145
sig_atomic_t quit_now = 0;
 
146
int signal_received = 0;
 
147
 
145
148
/*
146
149
 * Make additional room in "buffer" for at least BUFFER_SIZE more
147
150
 * bytes. "buffer_capacity" is how much is currently allocated,
164
167
 */
165
168
static bool init_gpgme(const char *seckey,
166
169
                       const char *pubkey, const char *tempdir){
167
 
  int ret;
168
170
  gpgme_error_t rc;
169
171
  gpgme_engine_info_t engine_info;
170
172
  
173
175
   * Helper function to insert pub and seckey to the engine keyring.
174
176
   */
175
177
  bool import_key(const char *filename){
 
178
    int ret;
176
179
    int fd;
177
180
    gpgme_data_t pgp_data;
178
181
    
474
477
static int init_gnutls_session(gnutls_session_t *session){
475
478
  int ret;
476
479
  /* GnuTLS session creation */
477
 
  ret = gnutls_init(session, GNUTLS_SERVER);
 
480
  do {
 
481
    ret = gnutls_init(session, GNUTLS_SERVER);
 
482
    if(quit_now){
 
483
      return -1;
 
484
    }
 
485
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
478
486
  if(ret != GNUTLS_E_SUCCESS){
479
487
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
480
488
            safer_gnutls_strerror(ret));
482
490
  
483
491
  {
484
492
    const char *err;
485
 
    ret = gnutls_priority_set_direct(*session, mc.priority, &err);
 
493
    do {
 
494
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
 
495
      if(quit_now){
 
496
        gnutls_deinit(*session);
 
497
        return -1;
 
498
      }
 
499
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
486
500
    if(ret != GNUTLS_E_SUCCESS){
487
501
      fprintf(stderr, "Syntax error at: %s\n", err);
488
502
      fprintf(stderr, "GnuTLS error: %s\n",
492
506
    }
493
507
  }
494
508
  
495
 
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
496
 
                               mc.cred);
 
509
  do {
 
510
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
511
                                 mc.cred);
 
512
    if(quit_now){
 
513
      gnutls_deinit(*session);
 
514
      return -1;
 
515
    }
 
516
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
497
517
  if(ret != GNUTLS_E_SUCCESS){
498
518
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
499
519
            safer_gnutls_strerror(ret));
502
522
  }
503
523
  
504
524
  /* ignore client certificate if any. */
505
 
  gnutls_certificate_server_set_request(*session,
506
 
                                        GNUTLS_CERT_IGNORE);
 
525
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
507
526
  
508
527
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
509
528
  
514
533
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
515
534
                      __attribute__((unused)) const char *txt){}
516
535
 
517
 
sig_atomic_t quit_now = 0;
518
 
int signal_received = 0;
519
 
 
520
536
/* Called when a Mandos server is found */
521
537
static int start_mandos_communication(const char *ip, uint16_t port,
522
538
                                      AvahiIfIndex if_index,
523
539
                                      int af){
524
 
  int ret, tcp_sd;
 
540
  int ret, tcp_sd = -1;
525
541
  ssize_t sret;
526
542
  union {
527
543
    struct sockaddr_in in;
528
544
    struct sockaddr_in6 in6;
529
545
  } to;
530
546
  char *buffer = NULL;
531
 
  char *decrypted_buffer;
 
547
  char *decrypted_buffer = NULL;
532
548
  size_t buffer_length = 0;
533
549
  size_t buffer_capacity = 0;
534
 
  ssize_t decrypted_buffer_size;
535
550
  size_t written;
536
 
  int retval = 0;
 
551
  int retval = -1;
537
552
  gnutls_session_t session;
538
553
  int pf;                       /* Protocol family */
539
554
  
 
555
  if(quit_now){
 
556
    return -1;
 
557
  }
 
558
  
540
559
  switch(af){
541
560
  case AF_INET6:
542
561
    pf = PF_INET6;
562
581
  tcp_sd = socket(pf, SOCK_STREAM, 0);
563
582
  if(tcp_sd < 0){
564
583
    perror("socket");
565
 
    return -1;
 
584
    goto mandos_end;
 
585
  }
 
586
  
 
587
  if(quit_now){
 
588
    goto mandos_end;
566
589
  }
567
590
  
568
591
  memset(&to, 0, sizeof(to));
575
598
  }
576
599
  if(ret < 0 ){
577
600
    perror("inet_pton");
578
 
    return -1;
 
601
    goto mandos_end;
579
602
  }
580
603
  if(ret == 0){
581
604
    fprintf(stderr, "Bad address: %s\n", ip);
582
 
    return -1;
 
605
    goto mandos_end;
583
606
  }
584
607
  if(af == AF_INET6){
585
608
    to.in6.sin6_port = htons(port); /* Spurious warnings from
592
615
      if(if_index == AVAHI_IF_UNSPEC){
593
616
        fprintf(stderr, "An IPv6 link-local address is incomplete"
594
617
                " without a network interface\n");
595
 
        return -1;
 
618
        goto mandos_end;
596
619
      }
597
620
      /* Set the network interface number as scope */
598
621
      to.in6.sin6_scope_id = (uint32_t)if_index;
603
626
                                     -Wunreachable-code */
604
627
  }
605
628
  
 
629
  if(quit_now){
 
630
    goto mandos_end;
 
631
  }
 
632
  
606
633
  if(debug){
607
634
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
608
635
      char interface[IF_NAMESIZE];
635
662
    }
636
663
  }
637
664
  
 
665
  if(quit_now){
 
666
    goto mandos_end;
 
667
  }
 
668
  
638
669
  if(af == AF_INET6){
639
670
    ret = connect(tcp_sd, &to.in6, sizeof(to));
640
671
  } else {
642
673
  }
643
674
  if(ret < 0){
644
675
    perror("connect");
645
 
    return -1;
 
676
    goto mandos_end;
 
677
  }
 
678
  
 
679
  if(quit_now){
 
680
    goto mandos_end;
646
681
  }
647
682
  
648
683
  const char *out = mandos_protocol_version;
653
688
                                   out_size - written));
654
689
    if(ret == -1){
655
690
      perror("write");
656
 
      retval = -1;
657
691
      goto mandos_end;
658
692
    }
659
693
    written += (size_t)ret;
667
701
        break;
668
702
      }
669
703
    }
 
704
  
 
705
    if(quit_now){
 
706
      goto mandos_end;
 
707
    }
670
708
  }
671
709
  
672
710
  if(debug){
673
711
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
674
712
  }
675
713
  
 
714
  if(quit_now){
 
715
    goto mandos_end;
 
716
  }
 
717
  
676
718
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
677
719
  
678
 
  do{
 
720
  if(quit_now){
 
721
    goto mandos_end;
 
722
  }
 
723
  
 
724
  do {
679
725
    ret = gnutls_handshake(session);
 
726
    if(quit_now){
 
727
      goto mandos_end;
 
728
    }
680
729
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
681
730
  
682
731
  if(ret != GNUTLS_E_SUCCESS){
684
733
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
685
734
      gnutls_perror(ret);
686
735
    }
687
 
    retval = -1;
688
736
    goto mandos_end;
689
737
  }
690
738
  
696
744
  }
697
745
  
698
746
  while(true){
 
747
    
 
748
    if(quit_now){
 
749
      goto mandos_end;
 
750
    }
 
751
    
699
752
    buffer_capacity = incbuffer(&buffer, buffer_length,
700
753
                                   buffer_capacity);
701
754
    if(buffer_capacity == 0){
702
755
      perror("incbuffer");
703
 
      retval = -1;
 
756
      goto mandos_end;
 
757
    }
 
758
    
 
759
    if(quit_now){
704
760
      goto mandos_end;
705
761
    }
706
762
    
715
771
      case GNUTLS_E_AGAIN:
716
772
        break;
717
773
      case GNUTLS_E_REHANDSHAKE:
718
 
        do{
 
774
        do {
719
775
          ret = gnutls_handshake(session);
 
776
          
 
777
          if(quit_now){
 
778
            goto mandos_end;
 
779
          }
720
780
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
721
781
        if(ret < 0){
722
782
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
723
783
          gnutls_perror(ret);
724
 
          retval = -1;
725
784
          goto mandos_end;
726
785
        }
727
786
        break;
728
787
      default:
729
788
        fprintf(stderr, "Unknown error while reading data from"
730
789
                " encrypted session with Mandos server\n");
731
 
        retval = -1;
732
790
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
733
791
        goto mandos_end;
734
792
      }
741
799
    fprintf(stderr, "Closing TLS session\n");
742
800
  }
743
801
  
744
 
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
802
  if(quit_now){
 
803
    goto mandos_end;
 
804
  }
 
805
  
 
806
  do {
 
807
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
808
    if(quit_now){
 
809
      goto mandos_end;
 
810
    }
 
811
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
745
812
  
746
813
  if(buffer_length > 0){
 
814
    ssize_t decrypted_buffer_size;
747
815
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
748
816
                                               buffer_length,
749
817
                                               &decrypted_buffer);
750
818
    if(decrypted_buffer_size >= 0){
 
819
      
751
820
      written = 0;
752
821
      while(written < (size_t) decrypted_buffer_size){
 
822
        if(quit_now){
 
823
          goto mandos_end;
 
824
        }
 
825
        
753
826
        ret = (int)fwrite(decrypted_buffer + written, 1,
754
827
                          (size_t)decrypted_buffer_size - written,
755
828
                          stdout);
758
831
            fprintf(stderr, "Error writing encrypted data: %s\n",
759
832
                    strerror(errno));
760
833
          }
761
 
          retval = -1;
762
 
          break;
 
834
          goto mandos_end;
763
835
        }
764
836
        written += (size_t)ret;
765
837
      }
766
 
      free(decrypted_buffer);
767
 
    } else {
768
 
      retval = -1;
 
838
      retval = 0;
769
839
    }
770
 
  } else {
771
 
    retval = -1;
772
840
  }
773
841
  
774
842
  /* Shutdown procedure */
775
843
  
776
844
 mandos_end:
 
845
  free(decrypted_buffer);
777
846
  free(buffer);
778
 
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
847
  if(tcp_sd >= 0){
 
848
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
849
  }
779
850
  if(ret == -1){
780
851
    perror("close");
781
852
  }
782
853
  gnutls_deinit(session);
 
854
  if(quit_now){
 
855
    retval = -1;
 
856
  }
783
857
  return retval;
784
858
}
785
859
 
848
922
  /* Called whenever a new services becomes available on the LAN or
849
923
     is removed from the LAN */
850
924
  
 
925
  if(quit_now){
 
926
    return;
 
927
  }
 
928
  
851
929
  switch(event){
852
930
  default:
853
931
  case AVAHI_BROWSER_FAILURE:
920
998
  bool gpgme_initialized = false;
921
999
  float delay = 2.5f;
922
1000
  
923
 
  struct sigaction old_sigterm_action;
 
1001
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
924
1002
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
925
1003
  
 
1004
  uid = getuid();
 
1005
  gid = getgid();
 
1006
  
 
1007
  /* Lower any group privileges we might have, just to be safe */
 
1008
  errno = 0;
 
1009
  ret = setgid(gid);
 
1010
  if(ret == -1){
 
1011
    perror("setgid");
 
1012
  }
 
1013
  
 
1014
  /* Lower user privileges (temporarily) */
 
1015
  errno = 0;
 
1016
  ret = seteuid(uid);
 
1017
  if(ret == -1){
 
1018
    perror("seteuid");
 
1019
  }
 
1020
  
 
1021
  if(quit_now){
 
1022
    goto end;
 
1023
  }
 
1024
  
926
1025
  {
927
1026
    struct argp_option options[] = {
928
1027
      { .name = "debug", .key = 128,
1055
1154
    exitcode = EXIT_FAILURE;
1056
1155
    goto end;
1057
1156
  }
1058
 
  ret = sigaction(SIGINT, &sigterm_action, &old_sigterm_action);
1059
 
  if(ret == -1){
1060
 
    perror("sigaction");
1061
 
    exitcode = EXIT_FAILURE;
1062
 
    goto end;
1063
 
  }
1064
 
  ret = sigaction(SIGHUP, &sigterm_action, NULL);
1065
 
  if(ret == -1){
1066
 
    perror("sigaction");
1067
 
    exitcode = EXIT_FAILURE;
1068
 
    goto end;
1069
 
  }
1070
 
  ret = sigaction(SIGTERM, &sigterm_action, NULL);
1071
 
  if(ret == -1){
1072
 
    perror("sigaction");
1073
 
    exitcode = EXIT_FAILURE;
1074
 
    goto end;
 
1157
  /* Need to check if the handler is SIG_IGN before handling:
 
1158
     | [[info:libc:Initial Signal Actions]] |
 
1159
     | [[info:libc:Basic Signal Handling]]  |
 
1160
  */
 
1161
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
 
1162
  if(ret == -1){
 
1163
    perror("sigaction");
 
1164
    return EXIT_FAILURE;
 
1165
  }
 
1166
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1167
    ret = sigaction(SIGINT, &sigterm_action, NULL);
 
1168
    if(ret == -1){
 
1169
      perror("sigaction");
 
1170
      exitcode = EXIT_FAILURE;
 
1171
      goto end;
 
1172
    }
 
1173
  }
 
1174
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
 
1175
  if(ret == -1){
 
1176
    perror("sigaction");
 
1177
    return EXIT_FAILURE;
 
1178
  }
 
1179
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1180
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
 
1181
    if(ret == -1){
 
1182
      perror("sigaction");
 
1183
      exitcode = EXIT_FAILURE;
 
1184
      goto end;
 
1185
    }
 
1186
  }
 
1187
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
 
1188
  if(ret == -1){
 
1189
    perror("sigaction");
 
1190
    return EXIT_FAILURE;
 
1191
  }
 
1192
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1193
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
 
1194
    if(ret == -1){
 
1195
      perror("sigaction");
 
1196
      exitcode = EXIT_FAILURE;
 
1197
      goto end;
 
1198
    }
1075
1199
  }
1076
1200
  
1077
1201
  /* If the interface is down, bring it up */
1087
1211
      goto end;
1088
1212
    }
1089
1213
    
 
1214
    /* Re-raise priviliges */
 
1215
    errno = 0;
 
1216
    ret = seteuid(0);
 
1217
    if(ret == -1){
 
1218
      perror("seteuid");
 
1219
    }
 
1220
    
1090
1221
#ifdef __linux__
1091
1222
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1092
1223
       messages to mess up the prompt */
1110
1241
        }
1111
1242
      }
1112
1243
#endif  /* __linux__ */
 
1244
      /* Lower privileges */
 
1245
      errno = 0;
 
1246
      ret = seteuid(uid);
 
1247
      if(ret == -1){
 
1248
        perror("seteuid");
 
1249
      }
1113
1250
      goto end;
1114
1251
    }
1115
1252
    strcpy(network.ifr_name, interface);
1125
1262
      }
1126
1263
#endif  /* __linux__ */
1127
1264
      exitcode = EXIT_FAILURE;
 
1265
      /* Lower privileges */
 
1266
      errno = 0;
 
1267
      ret = seteuid(uid);
 
1268
      if(ret == -1){
 
1269
        perror("seteuid");
 
1270
      }
1128
1271
      goto end;
1129
1272
    }
1130
1273
    if((network.ifr_flags & IFF_UP) == 0){
1143
1286
          }
1144
1287
        }
1145
1288
#endif  /* __linux__ */
 
1289
        /* Lower privileges */
 
1290
        errno = 0;
 
1291
        ret = seteuid(uid);
 
1292
        if(ret == -1){
 
1293
          perror("seteuid");
 
1294
        }
1146
1295
        goto end;
1147
1296
      }
1148
1297
    }
1176
1325
      }
1177
1326
    }
1178
1327
#endif  /* __linux__ */
1179
 
  }
1180
 
  
1181
 
  if(quit_now){
1182
 
    goto end;
1183
 
  }
1184
 
  
1185
 
  uid = getuid();
1186
 
  gid = getgid();
1187
 
  
1188
 
  errno = 0;
1189
 
  setgid(gid);
1190
 
  if(ret == -1){
1191
 
    perror("setgid");
1192
 
  }
1193
 
  
1194
 
  ret = setuid(uid);
1195
 
  if(ret == -1){
1196
 
    perror("setuid");
 
1328
    /* Lower privileges */
 
1329
    errno = 0;
 
1330
    if(take_down_interface){
 
1331
      /* Lower privileges */
 
1332
      ret = seteuid(uid);
 
1333
      if(ret == -1){
 
1334
        perror("seteuid");
 
1335
      }
 
1336
    } else {
 
1337
      /* Lower privileges permanently */
 
1338
      ret = setuid(uid);
 
1339
      if(ret == -1){
 
1340
        perror("setuid");
 
1341
      }
 
1342
    }
1197
1343
  }
1198
1344
  
1199
1345
  if(quit_now){
1373
1519
  
1374
1520
  /* Take down the network interface */
1375
1521
  if(take_down_interface){
1376
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1522
    /* Re-raise priviliges */
 
1523
    errno = 0;
 
1524
    ret = seteuid(0);
1377
1525
    if(ret == -1){
1378
 
      perror("ioctl SIOCGIFFLAGS");
1379
 
    } else if(network.ifr_flags & IFF_UP) {
1380
 
      network.ifr_flags &= ~IFF_UP; /* clear flag */
1381
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1382
 
      if(ret == -1){
1383
 
        perror("ioctl SIOCSIFFLAGS");
1384
 
      }
 
1526
      perror("seteuid");
1385
1527
    }
1386
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1387
 
    if(ret == -1){
1388
 
      perror("close");
 
1528
    if(geteuid() == 0){
 
1529
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1530
      if(ret == -1){
 
1531
        perror("ioctl SIOCGIFFLAGS");
 
1532
      } else if(network.ifr_flags & IFF_UP) {
 
1533
        network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1534
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1535
        if(ret == -1){
 
1536
          perror("ioctl SIOCSIFFLAGS");
 
1537
        }
 
1538
      }
 
1539
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1540
      if(ret == -1){
 
1541
        perror("close");
 
1542
      }
 
1543
      /* Lower privileges permanently */
 
1544
      errno = 0;
 
1545
      ret = setuid(uid);
 
1546
      if(ret == -1){
 
1547
        perror("setuid");
 
1548
      }
1389
1549
    }
1390
1550
  }
1391
1551
  
1434
1594
  }
1435
1595
  
1436
1596
  if(quit_now){
1437
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
 
1597
    sigemptyset(&old_sigterm_action.sa_mask);
 
1598
    old_sigterm_action.sa_handler = SIG_DFL;
 
1599
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
1600
                                            &old_sigterm_action,
 
1601
                                            NULL));
1438
1602
    if(ret == -1){
1439
1603
      perror("sigaction");
1440
1604
    }
1441
 
    raise(signal_received);
 
1605
    do {
 
1606
      ret = raise(signal_received);
 
1607
    } while(ret != 0 and errno == EINTR);
 
1608
    if(ret != 0){
 
1609
      perror("raise");
 
1610
      abort();
 
1611
    }
 
1612
    TEMP_FAILURE_RETRY(pause());
1442
1613
  }
1443
1614
  
1444
1615
  return exitcode;