/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-04-17 08:26:17 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090417082617-eltetak5lzjyz55o
* initramfs-tools-hook: Bug fix: Add "--userid" and "--groupid" to
                        start of "plugin-runner.conf" file instead of
                        appending, to allow any preexisting options to
                        override.
* plugin-runner.conf: Improved wording.

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, srand(),
47
 
                                   strtof(), abort() */
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
 
47
                                   srand(), strtof() */
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(), seteuid(),
75
 
                                   setgid(), pause() */
 
74
                                   getuid(), getgid(), setuid(),
 
75
                                   setgid() */
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
80
80
                                   argp_parse(), ARGP_KEY_ARG,
81
81
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sig_atomic_t,
84
 
                                   raise() */
85
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
86
 
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
 
83
                                   sigaction(), SIGTERM, sigaction,
 
84
                                   sig_atomic_t */
87
85
 
88
86
#ifdef __linux__
89
87
#include <sys/klog.h>           /* klogctl() */
144
142
                      .dh_bits = 1024, .priority = "SECURE256"
145
143
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
146
144
 
147
 
sig_atomic_t quit_now = 0;
148
 
int signal_received = 0;
149
 
 
150
145
/*
151
146
 * Make additional room in "buffer" for at least BUFFER_SIZE more
152
147
 * bytes. "buffer_capacity" is how much is currently allocated,
169
164
 */
170
165
static bool init_gpgme(const char *seckey,
171
166
                       const char *pubkey, const char *tempdir){
 
167
  int ret;
172
168
  gpgme_error_t rc;
173
169
  gpgme_engine_info_t engine_info;
174
170
  
177
173
   * Helper function to insert pub and seckey to the engine keyring.
178
174
   */
179
175
  bool import_key(const char *filename){
180
 
    int ret;
181
176
    int fd;
182
177
    gpgme_data_t pgp_data;
183
178
    
254
249
    return false;
255
250
  }
256
251
  
257
 
  return true;
 
252
  return true; 
258
253
}
259
254
 
260
255
/* 
314
309
        }
315
310
        gpgme_recipient_t recipient;
316
311
        recipient = result->recipients;
317
 
        while(recipient != NULL){
318
 
          fprintf(stderr, "Public key algorithm: %s\n",
319
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
320
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
321
 
          fprintf(stderr, "Secret key available: %s\n",
322
 
                  recipient->status == GPG_ERR_NO_SECKEY
323
 
                  ? "No" : "Yes");
324
 
          recipient = recipient->next;
 
312
        if(recipient){
 
313
          while(recipient != NULL){
 
314
            fprintf(stderr, "Public key algorithm: %s\n",
 
315
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
316
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
317
            fprintf(stderr, "Secret key available: %s\n",
 
318
                    recipient->status == GPG_ERR_NO_SECKEY
 
319
                    ? "No" : "Yes");
 
320
            recipient = recipient->next;
 
321
          }
325
322
        }
326
323
      }
327
324
    }
479
476
static int init_gnutls_session(gnutls_session_t *session){
480
477
  int ret;
481
478
  /* GnuTLS session creation */
482
 
  do {
483
 
    ret = gnutls_init(session, GNUTLS_SERVER);
484
 
    if(quit_now){
485
 
      return -1;
486
 
    }
487
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
479
  ret = gnutls_init(session, GNUTLS_SERVER);
488
480
  if(ret != GNUTLS_E_SUCCESS){
489
481
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
490
482
            safer_gnutls_strerror(ret));
492
484
  
493
485
  {
494
486
    const char *err;
495
 
    do {
496
 
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
497
 
      if(quit_now){
498
 
        gnutls_deinit(*session);
499
 
        return -1;
500
 
      }
501
 
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
487
    ret = gnutls_priority_set_direct(*session, mc.priority, &err);
502
488
    if(ret != GNUTLS_E_SUCCESS){
503
489
      fprintf(stderr, "Syntax error at: %s\n", err);
504
490
      fprintf(stderr, "GnuTLS error: %s\n",
508
494
    }
509
495
  }
510
496
  
511
 
  do {
512
 
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
513
 
                                 mc.cred);
514
 
    if(quit_now){
515
 
      gnutls_deinit(*session);
516
 
      return -1;
517
 
    }
518
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
497
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
498
                               mc.cred);
519
499
  if(ret != GNUTLS_E_SUCCESS){
520
500
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
521
501
            safer_gnutls_strerror(ret));
524
504
  }
525
505
  
526
506
  /* ignore client certificate if any. */
527
 
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
 
507
  gnutls_certificate_server_set_request(*session,
 
508
                                        GNUTLS_CERT_IGNORE);
528
509
  
529
510
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
530
511
  
539
520
static int start_mandos_communication(const char *ip, uint16_t port,
540
521
                                      AvahiIfIndex if_index,
541
522
                                      int af){
542
 
  int ret, tcp_sd = -1;
 
523
  int ret, tcp_sd;
543
524
  ssize_t sret;
544
525
  union {
545
526
    struct sockaddr_in in;
546
527
    struct sockaddr_in6 in6;
547
528
  } to;
548
529
  char *buffer = NULL;
549
 
  char *decrypted_buffer = NULL;
 
530
  char *decrypted_buffer;
550
531
  size_t buffer_length = 0;
551
532
  size_t buffer_capacity = 0;
 
533
  ssize_t decrypted_buffer_size;
552
534
  size_t written;
553
 
  int retval = -1;
 
535
  int retval = 0;
554
536
  gnutls_session_t session;
555
537
  int pf;                       /* Protocol family */
556
538
  
557
 
  errno = 0;
558
 
  
559
 
  if(quit_now){
560
 
    errno = EINTR;
561
 
    return -1;
562
 
  }
563
 
  
564
539
  switch(af){
565
540
  case AF_INET6:
566
541
    pf = PF_INET6;
570
545
    break;
571
546
  default:
572
547
    fprintf(stderr, "Bad address family: %d\n", af);
573
 
    errno = EINVAL;
574
548
    return -1;
575
549
  }
576
550
  
586
560
  
587
561
  tcp_sd = socket(pf, SOCK_STREAM, 0);
588
562
  if(tcp_sd < 0){
589
 
    int e = errno;
590
563
    perror("socket");
591
 
    errno = e;
592
 
    goto mandos_end;
593
 
  }
594
 
  
595
 
  if(quit_now){
596
 
    errno = EINTR;
597
 
    goto mandos_end;
 
564
    return -1;
598
565
  }
599
566
  
600
567
  memset(&to, 0, sizeof(to));
606
573
    ret = inet_pton(af, ip, &to.in.sin_addr);
607
574
  }
608
575
  if(ret < 0 ){
609
 
    int e = errno;
610
576
    perror("inet_pton");
611
 
    errno = e;
612
 
    goto mandos_end;
 
577
    return -1;
613
578
  }
614
579
  if(ret == 0){
615
 
    int e = errno;
616
580
    fprintf(stderr, "Bad address: %s\n", ip);
617
 
    errno = e;
618
 
    goto mandos_end;
 
581
    return -1;
619
582
  }
620
583
  if(af == AF_INET6){
621
584
    to.in6.sin6_port = htons(port); /* Spurious warnings from
628
591
      if(if_index == AVAHI_IF_UNSPEC){
629
592
        fprintf(stderr, "An IPv6 link-local address is incomplete"
630
593
                " without a network interface\n");
631
 
        errno = EINVAL;
632
 
        goto mandos_end;
 
594
        return -1;
633
595
      }
634
596
      /* Set the network interface number as scope */
635
597
      to.in6.sin6_scope_id = (uint32_t)if_index;
640
602
                                     -Wunreachable-code */
641
603
  }
642
604
  
643
 
  if(quit_now){
644
 
    errno = EINTR;
645
 
    goto mandos_end;
646
 
  }
647
 
  
648
605
  if(debug){
649
606
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
650
607
      char interface[IF_NAMESIZE];
677
634
    }
678
635
  }
679
636
  
680
 
  if(quit_now){
681
 
    errno = EINTR;
682
 
    goto mandos_end;
683
 
  }
684
 
  
685
637
  if(af == AF_INET6){
686
638
    ret = connect(tcp_sd, &to.in6, sizeof(to));
687
639
  } else {
688
640
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
689
641
  }
690
642
  if(ret < 0){
691
 
    int e = errno;
692
643
    perror("connect");
693
 
    errno = e;
694
 
    goto mandos_end;
695
 
  }
696
 
  
697
 
  if(quit_now){
698
 
    errno = EINTR;
699
 
    goto mandos_end;
 
644
    return -1;
700
645
  }
701
646
  
702
647
  const char *out = mandos_protocol_version;
706
651
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
707
652
                                   out_size - written));
708
653
    if(ret == -1){
709
 
      int e = errno;
710
654
      perror("write");
711
 
      errno = e;
 
655
      retval = -1;
712
656
      goto mandos_end;
713
657
    }
714
658
    written += (size_t)ret;
722
666
        break;
723
667
      }
724
668
    }
725
 
  
726
 
    if(quit_now){
727
 
      errno = EINTR;
728
 
      goto mandos_end;
729
 
    }
730
669
  }
731
670
  
732
671
  if(debug){
733
672
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
734
673
  }
735
674
  
736
 
  if(quit_now){
737
 
    errno = EINTR;
738
 
    goto mandos_end;
739
 
  }
740
 
  
741
675
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
742
676
  
743
 
  if(quit_now){
744
 
    errno = EINTR;
745
 
    goto mandos_end;
746
 
  }
747
 
  
748
 
  do {
 
677
  do{
749
678
    ret = gnutls_handshake(session);
750
 
    if(quit_now){
751
 
      errno = EINTR;
752
 
      goto mandos_end;
753
 
    }
754
679
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
755
680
  
756
681
  if(ret != GNUTLS_E_SUCCESS){
758
683
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
759
684
      gnutls_perror(ret);
760
685
    }
761
 
    errno = EPROTO;
 
686
    retval = -1;
762
687
    goto mandos_end;
763
688
  }
764
689
  
770
695
  }
771
696
  
772
697
  while(true){
773
 
    
774
 
    if(quit_now){
775
 
      errno = EINTR;
776
 
      goto mandos_end;
777
 
    }
778
 
    
779
698
    buffer_capacity = incbuffer(&buffer, buffer_length,
780
699
                                   buffer_capacity);
781
700
    if(buffer_capacity == 0){
782
 
      int e = errno;
783
701
      perror("incbuffer");
784
 
      errno = e;
785
 
      goto mandos_end;
786
 
    }
787
 
    
788
 
    if(quit_now){
789
 
      errno = EINTR;
 
702
      retval = -1;
790
703
      goto mandos_end;
791
704
    }
792
705
    
801
714
      case GNUTLS_E_AGAIN:
802
715
        break;
803
716
      case GNUTLS_E_REHANDSHAKE:
804
 
        do {
 
717
        do{
805
718
          ret = gnutls_handshake(session);
806
 
          
807
 
          if(quit_now){
808
 
            errno = EINTR;
809
 
            goto mandos_end;
810
 
          }
811
719
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
812
720
        if(ret < 0){
813
721
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
814
722
          gnutls_perror(ret);
815
 
          errno = EPROTO;
 
723
          retval = -1;
816
724
          goto mandos_end;
817
725
        }
818
726
        break;
819
727
      default:
820
728
        fprintf(stderr, "Unknown error while reading data from"
821
729
                " encrypted session with Mandos server\n");
 
730
        retval = -1;
822
731
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
823
 
        errno = EIO;
824
732
        goto mandos_end;
825
733
      }
826
734
    } else {
832
740
    fprintf(stderr, "Closing TLS session\n");
833
741
  }
834
742
  
835
 
  if(quit_now){
836
 
    errno = EINTR;
837
 
    goto mandos_end;
838
 
  }
839
 
  
840
 
  do {
841
 
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
842
 
    if(quit_now){
843
 
      errno = EINTR;
844
 
      goto mandos_end;
845
 
    }
846
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
743
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
847
744
  
848
745
  if(buffer_length > 0){
849
 
    ssize_t decrypted_buffer_size;
850
746
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
851
747
                                               buffer_length,
852
748
                                               &decrypted_buffer);
853
749
    if(decrypted_buffer_size >= 0){
854
 
      
855
750
      written = 0;
856
751
      while(written < (size_t) decrypted_buffer_size){
857
 
        if(quit_now){
858
 
          errno = EINTR;
859
 
          goto mandos_end;
860
 
        }
861
 
        
862
752
        ret = (int)fwrite(decrypted_buffer + written, 1,
863
753
                          (size_t)decrypted_buffer_size - written,
864
754
                          stdout);
865
755
        if(ret == 0 and ferror(stdout)){
866
 
          int e = errno;
867
756
          if(debug){
868
757
            fprintf(stderr, "Error writing encrypted data: %s\n",
869
758
                    strerror(errno));
870
759
          }
871
 
          errno = e;
872
 
          goto mandos_end;
 
760
          retval = -1;
 
761
          break;
873
762
        }
874
763
        written += (size_t)ret;
875
764
      }
876
 
      retval = 0;
 
765
      free(decrypted_buffer);
 
766
    } else {
 
767
      retval = -1;
877
768
    }
 
769
  } else {
 
770
    retval = -1;
878
771
  }
879
772
  
880
773
  /* Shutdown procedure */
881
774
  
882
775
 mandos_end:
883
 
  {
884
 
    int e = errno;
885
 
    free(decrypted_buffer);
886
 
    free(buffer);
887
 
    if(tcp_sd >= 0){
888
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
889
 
    }
890
 
    if(ret == -1){
891
 
      if(e == 0){
892
 
        e = errno;
893
 
      }
894
 
      perror("close");
895
 
    }
896
 
    gnutls_deinit(session);
897
 
    if(quit_now){
898
 
      e = EINTR;
899
 
      retval = -1;
900
 
    }
901
 
    errno = e;
 
776
  free(buffer);
 
777
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
778
  if(ret == -1){
 
779
    perror("close");
902
780
  }
 
781
  gnutls_deinit(session);
903
782
  return retval;
904
783
}
905
784
 
922
801
  /* Called whenever a service has been resolved successfully or
923
802
     timed out */
924
803
  
925
 
  if(quit_now){
926
 
    return;
927
 
  }
928
 
  
929
804
  switch(event){
930
805
  default:
931
806
  case AVAHI_RESOLVER_FAILURE:
968
843
  /* Called whenever a new services becomes available on the LAN or
969
844
     is removed from the LAN */
970
845
  
971
 
  if(quit_now){
972
 
    return;
973
 
  }
974
 
  
975
846
  switch(event){
976
847
  default:
977
848
  case AVAHI_BROWSER_FAILURE:
1006
877
  }
1007
878
}
1008
879
 
 
880
sig_atomic_t quit_now = 0;
 
881
 
1009
882
/* stop main loop after sigterm has been called */
1010
 
static void handle_sigterm(int sig){
 
883
static void handle_sigterm(__attribute__((unused)) int sig){
1011
884
  if(quit_now){
1012
885
    return;
1013
886
  }
1014
887
  quit_now = 1;
1015
 
  signal_received = sig;
1016
888
  int old_errno = errno;
1017
889
  if(mc.simple_poll != NULL){
1018
890
    avahi_simple_poll_quit(mc.simple_poll);
1029
901
  int exitcode = EXIT_SUCCESS;
1030
902
  const char *interface = "eth0";
1031
903
  struct ifreq network;
1032
 
  int sd = -1;
1033
 
  bool take_down_interface = false;
 
904
  int sd;
1034
905
  uid_t uid;
1035
906
  gid_t gid;
1036
907
  char *connect_to = NULL;
1044
915
  bool gpgme_initialized = false;
1045
916
  float delay = 2.5f;
1046
917
  
1047
 
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
 
918
  struct sigaction old_sigterm_action;
1048
919
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1049
920
  
1050
 
  uid = getuid();
1051
 
  gid = getgid();
1052
 
  
1053
 
  /* Lower any group privileges we might have, just to be safe */
1054
 
  errno = 0;
1055
 
  ret = setgid(gid);
1056
 
  if(ret == -1){
1057
 
    perror("setgid");
1058
 
  }
1059
 
  
1060
 
  /* Lower user privileges (temporarily) */
1061
 
  errno = 0;
1062
 
  ret = seteuid(uid);
1063
 
  if(ret == -1){
1064
 
    perror("seteuid");
1065
 
  }
1066
 
  
1067
 
  if(quit_now){
1068
 
    goto end;
1069
 
  }
1070
 
  
1071
921
  {
1072
922
    struct argp_option options[] = {
1073
923
      { .name = "debug", .key = 128,
1102
952
        .arg = "SECONDS",
1103
953
        .doc = "Maximum delay to wait for interface startup",
1104
954
        .group = 2 },
1105
 
      /*
1106
 
       * These reproduce what we would get without ARGP_NO_HELP
1107
 
       */
1108
 
      { .name = "help", .key = '?',
1109
 
        .doc = "Give this help list", .group = -1 },
1110
 
      { .name = "usage", .key = -3,
1111
 
        .doc = "Give a short usage message", .group = -1 },
1112
 
      { .name = "version", .key = 'V',
1113
 
        .doc = "Print program version", .group = -1 },
1114
955
      { .name = NULL }
1115
956
    };
1116
957
    
1117
958
    error_t parse_opt(int key, char *arg,
1118
959
                      struct argp_state *state){
1119
 
      errno = 0;
1120
960
      switch(key){
1121
961
      case 128:                 /* --debug */
1122
962
        debug = true;
1138
978
        tmpmax = strtoimax(arg, &tmp, 10);
1139
979
        if(errno != 0 or tmp == arg or *tmp != '\0'
1140
980
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1141
 
          argp_error(state, "Bad number of DH bits");
 
981
          fprintf(stderr, "Bad number of DH bits\n");
 
982
          exit(EXIT_FAILURE);
1142
983
        }
1143
984
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1144
985
        break;
1149
990
        errno = 0;
1150
991
        delay = strtof(arg, &tmp);
1151
992
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1152
 
          argp_error(state, "Bad delay");
 
993
          fprintf(stderr, "Bad delay\n");
 
994
          exit(EXIT_FAILURE);
1153
995
        }
1154
996
        break;
1155
 
        /*
1156
 
         * These reproduce what we would get without ARGP_NO_HELP
1157
 
         */
1158
 
      case '?':                 /* --help */
1159
 
        argp_state_help(state, state->out_stream,
1160
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1161
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1162
 
      case -3:                  /* --usage */
1163
 
        argp_state_help(state, state->out_stream,
1164
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1165
 
      case 'V':                 /* --version */
1166
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1167
 
        exit(argp_err_exit_status);
 
997
      case ARGP_KEY_ARG:
 
998
        argp_usage(state);
 
999
      case ARGP_KEY_END:
1168
1000
        break;
1169
1001
      default:
1170
1002
        return ARGP_ERR_UNKNOWN;
1171
1003
      }
1172
 
      return errno;
 
1004
      return 0;
1173
1005
    }
1174
1006
    
1175
1007
    struct argp argp = { .options = options, .parser = parse_opt,
1176
1008
                         .args_doc = "",
1177
1009
                         .doc = "Mandos client -- Get and decrypt"
1178
1010
                         " passwords from a Mandos server" };
1179
 
    ret = argp_parse(&argp, argc, argv,
1180
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1181
 
    switch(ret){
1182
 
    case 0:
1183
 
      break;
1184
 
    case ENOMEM:
1185
 
    default:
1186
 
      errno = ret;
1187
 
      perror("argp_parse");
1188
 
      exitcode = EX_OSERR;
1189
 
      goto end;
1190
 
    case EINVAL:
1191
 
      exitcode = EX_USAGE;
 
1011
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
1012
    if(ret == ARGP_ERR_UNKNOWN){
 
1013
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
1014
      exitcode = EXIT_FAILURE;
1192
1015
      goto end;
1193
1016
    }
1194
1017
  }
1204
1027
  mc.simple_poll = avahi_simple_poll_new();
1205
1028
  if(mc.simple_poll == NULL){
1206
1029
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1207
 
    exitcode = EX_UNAVAILABLE;
 
1030
    exitcode = EXIT_FAILURE;
1208
1031
    goto end;
1209
1032
  }
1210
1033
  
1212
1035
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1213
1036
  if(ret == -1){
1214
1037
    perror("sigaddset");
1215
 
    exitcode = EX_OSERR;
 
1038
    exitcode = EXIT_FAILURE;
1216
1039
    goto end;
1217
1040
  }
1218
1041
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1219
1042
  if(ret == -1){
1220
1043
    perror("sigaddset");
1221
 
    exitcode = EX_OSERR;
 
1044
    exitcode = EXIT_FAILURE;
1222
1045
    goto end;
1223
1046
  }
1224
1047
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1225
1048
  if(ret == -1){
1226
1049
    perror("sigaddset");
1227
 
    exitcode = EX_OSERR;
1228
 
    goto end;
1229
 
  }
1230
 
  /* Need to check if the handler is SIG_IGN before handling:
1231
 
     | [[info:libc:Initial Signal Actions]] |
1232
 
     | [[info:libc:Basic Signal Handling]]  |
1233
 
  */
1234
 
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1235
 
  if(ret == -1){
1236
 
    perror("sigaction");
1237
 
    return EX_OSERR;
1238
 
  }
1239
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1240
 
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1241
 
    if(ret == -1){
1242
 
      perror("sigaction");
1243
 
      exitcode = EX_OSERR;
1244
 
      goto end;
1245
 
    }
1246
 
  }
1247
 
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1248
 
  if(ret == -1){
1249
 
    perror("sigaction");
1250
 
    return EX_OSERR;
1251
 
  }
1252
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1253
 
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1254
 
    if(ret == -1){
1255
 
      perror("sigaction");
1256
 
      exitcode = EX_OSERR;
1257
 
      goto end;
1258
 
    }
1259
 
  }
1260
 
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1261
 
  if(ret == -1){
1262
 
    perror("sigaction");
1263
 
    return EX_OSERR;
1264
 
  }
1265
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1266
 
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1267
 
    if(ret == -1){
1268
 
      perror("sigaction");
1269
 
      exitcode = EX_OSERR;
1270
 
      goto end;
1271
 
    }
1272
 
  }
 
1050
    exitcode = EXIT_FAILURE;
 
1051
    goto end;
 
1052
  }
 
1053
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1054
  if(ret == -1){
 
1055
    perror("sigaction");
 
1056
    exitcode = EXIT_FAILURE;
 
1057
    goto end;
 
1058
  }  
1273
1059
  
1274
1060
  /* If the interface is down, bring it up */
1275
1061
  if(interface[0] != '\0'){
1276
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1277
 
    if(if_index == 0){
1278
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1279
 
      exitcode = EX_UNAVAILABLE;
1280
 
      goto end;
1281
 
    }
1282
 
    
1283
 
    if(quit_now){
1284
 
      goto end;
1285
 
    }
1286
 
    
1287
 
    /* Re-raise priviliges */
1288
 
    errno = 0;
1289
 
    ret = seteuid(0);
1290
 
    if(ret == -1){
1291
 
      perror("seteuid");
1292
 
    }
1293
 
    
1294
1062
#ifdef __linux__
1295
1063
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1296
 
       messages about the network interface to mess up the prompt */
 
1064
       messages to mess up the prompt */
1297
1065
    ret = klogctl(8, NULL, 5);
1298
1066
    bool restore_loglevel = true;
1299
1067
    if(ret == -1){
1305
1073
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1306
1074
    if(sd < 0){
1307
1075
      perror("socket");
1308
 
      exitcode = EX_OSERR;
 
1076
      exitcode = EXIT_FAILURE;
1309
1077
#ifdef __linux__
1310
1078
      if(restore_loglevel){
1311
1079
        ret = klogctl(7, NULL, 0);
1314
1082
        }
1315
1083
      }
1316
1084
#endif  /* __linux__ */
1317
 
      /* Lower privileges */
1318
 
      errno = 0;
1319
 
      ret = seteuid(uid);
1320
 
      if(ret == -1){
1321
 
        perror("seteuid");
1322
 
      }
1323
1085
      goto end;
1324
1086
    }
1325
1087
    strcpy(network.ifr_name, interface);
1334
1096
        }
1335
1097
      }
1336
1098
#endif  /* __linux__ */
1337
 
      exitcode = EX_OSERR;
1338
 
      /* Lower privileges */
1339
 
      errno = 0;
1340
 
      ret = seteuid(uid);
1341
 
      if(ret == -1){
1342
 
        perror("seteuid");
1343
 
      }
 
1099
      exitcode = EXIT_FAILURE;
1344
1100
      goto end;
1345
1101
    }
1346
1102
    if((network.ifr_flags & IFF_UP) == 0){
1347
1103
      network.ifr_flags |= IFF_UP;
1348
 
      take_down_interface = true;
1349
1104
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1350
1105
      if(ret == -1){
1351
 
        take_down_interface = false;
1352
1106
        perror("ioctl SIOCSIFFLAGS");
1353
 
        exitcode = EX_OSERR;
 
1107
        exitcode = EXIT_FAILURE;
1354
1108
#ifdef __linux__
1355
1109
        if(restore_loglevel){
1356
1110
          ret = klogctl(7, NULL, 0);
1359
1113
          }
1360
1114
        }
1361
1115
#endif  /* __linux__ */
1362
 
        /* Lower privileges */
1363
 
        errno = 0;
1364
 
        ret = seteuid(uid);
1365
 
        if(ret == -1){
1366
 
          perror("seteuid");
1367
 
        }
1368
1116
        goto end;
1369
1117
      }
1370
1118
    }
1382
1130
        perror("nanosleep");
1383
1131
      }
1384
1132
    }
1385
 
    if(not take_down_interface){
1386
 
      /* We won't need the socket anymore */
1387
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1388
 
      if(ret == -1){
1389
 
        perror("close");
1390
 
      }
 
1133
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1134
    if(ret == -1){
 
1135
      perror("close");
1391
1136
    }
1392
1137
#ifdef __linux__
1393
1138
    if(restore_loglevel){
1398
1143
      }
1399
1144
    }
1400
1145
#endif  /* __linux__ */
1401
 
    /* Lower privileges */
1402
 
    errno = 0;
1403
 
    if(take_down_interface){
1404
 
      /* Lower privileges */
1405
 
      ret = seteuid(uid);
1406
 
      if(ret == -1){
1407
 
        perror("seteuid");
1408
 
      }
1409
 
    } else {
1410
 
      /* Lower privileges permanently */
1411
 
      ret = setuid(uid);
1412
 
      if(ret == -1){
1413
 
        perror("setuid");
1414
 
      }
1415
 
    }
1416
 
  }
1417
 
  
1418
 
  if(quit_now){
1419
 
    goto end;
 
1146
  }
 
1147
  
 
1148
  uid = getuid();
 
1149
  gid = getgid();
 
1150
  
 
1151
  errno = 0;
 
1152
  setgid(gid);
 
1153
  if(ret == -1){
 
1154
    perror("setgid");
 
1155
  }
 
1156
  
 
1157
  ret = setuid(uid);
 
1158
  if(ret == -1){
 
1159
    perror("setuid");
1420
1160
  }
1421
1161
  
1422
1162
  ret = init_gnutls_global(pubkey, seckey);
1423
1163
  if(ret == -1){
1424
1164
    fprintf(stderr, "init_gnutls_global failed\n");
1425
 
    exitcode = EX_UNAVAILABLE;
 
1165
    exitcode = EXIT_FAILURE;
1426
1166
    goto end;
1427
1167
  } else {
1428
1168
    gnutls_initialized = true;
1429
1169
  }
1430
1170
  
1431
 
  if(quit_now){
1432
 
    goto end;
1433
 
  }
1434
 
  
1435
 
  tempdir_created = true;
1436
1171
  if(mkdtemp(tempdir) == NULL){
1437
 
    tempdir_created = false;
1438
1172
    perror("mkdtemp");
1439
1173
    goto end;
1440
1174
  }
1441
 
  
1442
 
  if(quit_now){
1443
 
    goto end;
1444
 
  }
 
1175
  tempdir_created = true;
1445
1176
  
1446
1177
  if(not init_gpgme(pubkey, seckey, tempdir)){
1447
1178
    fprintf(stderr, "init_gpgme failed\n");
1448
 
    exitcode = EX_UNAVAILABLE;
 
1179
    exitcode = EXIT_FAILURE;
1449
1180
    goto end;
1450
1181
  } else {
1451
1182
    gpgme_initialized = true;
1452
1183
  }
1453
1184
  
1454
 
  if(quit_now){
1455
 
    goto end;
 
1185
  if(interface[0] != '\0'){
 
1186
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1187
    if(if_index == 0){
 
1188
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1189
      exitcode = EXIT_FAILURE;
 
1190
      goto end;
 
1191
    }
1456
1192
  }
1457
1193
  
1458
1194
  if(connect_to != NULL){
1461
1197
    char *address = strrchr(connect_to, ':');
1462
1198
    if(address == NULL){
1463
1199
      fprintf(stderr, "No colon in address\n");
1464
 
      exitcode = EX_USAGE;
1465
 
      goto end;
1466
 
    }
1467
 
    
1468
 
    if(quit_now){
1469
 
      goto end;
1470
 
    }
1471
 
    
 
1200
      exitcode = EXIT_FAILURE;
 
1201
      goto end;
 
1202
    }
1472
1203
    uint16_t port;
1473
1204
    errno = 0;
1474
1205
    tmpmax = strtoimax(address+1, &tmp, 10);
1475
1206
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1476
1207
       or tmpmax != (uint16_t)tmpmax){
1477
1208
      fprintf(stderr, "Bad port number\n");
1478
 
      exitcode = EX_USAGE;
1479
 
      goto end;
1480
 
    }
1481
 
  
1482
 
    if(quit_now){
1483
 
      goto end;
1484
 
    }
1485
 
    
 
1209
      exitcode = EXIT_FAILURE;
 
1210
      goto end;
 
1211
    }
1486
1212
    port = (uint16_t)tmpmax;
1487
1213
    *address = '\0';
1488
1214
    address = connect_to;
1493
1219
    } else {
1494
1220
      af = AF_INET;
1495
1221
    }
1496
 
    
1497
 
    if(quit_now){
1498
 
      goto end;
1499
 
    }
1500
 
    
1501
1222
    ret = start_mandos_communication(address, port, if_index, af);
1502
1223
    if(ret < 0){
1503
 
      switch(errno){
1504
 
      case ENETUNREACH:
1505
 
      case EHOSTDOWN:
1506
 
      case EHOSTUNREACH:
1507
 
        exitcode = EX_NOHOST;
1508
 
        break;
1509
 
      case EINVAL:
1510
 
        exitcode = EX_USAGE;
1511
 
        break;
1512
 
      case EIO:
1513
 
        exitcode = EX_IOERR;
1514
 
        break;
1515
 
      case EPROTO:
1516
 
        exitcode = EX_PROTOCOL;
1517
 
        break;
1518
 
      default:
1519
 
        exitcode = EX_OSERR;
1520
 
        break;
1521
 
      }
 
1224
      exitcode = EXIT_FAILURE;
1522
1225
    } else {
1523
1226
      exitcode = EXIT_SUCCESS;
1524
1227
    }
1525
1228
    goto end;
1526
1229
  }
1527
 
  
1528
 
  if(quit_now){
1529
 
    goto end;
1530
 
  }
1531
 
  
 
1230
    
1532
1231
  {
1533
1232
    AvahiServerConfig config;
1534
1233
    /* Do not publish any local Zeroconf records */
1551
1250
  if(mc.server == NULL){
1552
1251
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1553
1252
            avahi_strerror(error));
1554
 
    exitcode = EX_UNAVAILABLE;
1555
 
    goto end;
1556
 
  }
1557
 
  
1558
 
  if(quit_now){
 
1253
    exitcode = EXIT_FAILURE;
1559
1254
    goto end;
1560
1255
  }
1561
1256
  
1566
1261
  if(sb == NULL){
1567
1262
    fprintf(stderr, "Failed to create service browser: %s\n",
1568
1263
            avahi_strerror(avahi_server_errno(mc.server)));
1569
 
    exitcode = EX_UNAVAILABLE;
1570
 
    goto end;
1571
 
  }
1572
 
  
1573
 
  if(quit_now){
 
1264
    exitcode = EXIT_FAILURE;
1574
1265
    goto end;
1575
1266
  }
1576
1267
  
1608
1299
    gpgme_release(mc.ctx);
1609
1300
  }
1610
1301
  
1611
 
  /* Take down the network interface */
1612
 
  if(take_down_interface){
1613
 
    /* Re-raise priviliges */
1614
 
    errno = 0;
1615
 
    ret = seteuid(0);
1616
 
    if(ret == -1){
1617
 
      perror("seteuid");
1618
 
    }
1619
 
    if(geteuid() == 0){
1620
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1621
 
      if(ret == -1){
1622
 
        perror("ioctl SIOCGIFFLAGS");
1623
 
      } else if(network.ifr_flags & IFF_UP) {
1624
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1625
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1626
 
        if(ret == -1){
1627
 
          perror("ioctl SIOCSIFFLAGS");
1628
 
        }
1629
 
      }
1630
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1631
 
      if(ret == -1){
1632
 
        perror("close");
1633
 
      }
1634
 
      /* Lower privileges permanently */
1635
 
      errno = 0;
1636
 
      ret = setuid(uid);
1637
 
      if(ret == -1){
1638
 
        perror("setuid");
1639
 
      }
1640
 
    }
1641
 
  }
1642
 
  
1643
1302
  /* Removes the temp directory used by GPGME */
1644
1303
  if(tempdir_created){
1645
1304
    DIR *d;
1684
1343
    }
1685
1344
  }
1686
1345
  
1687
 
  if(quit_now){
1688
 
    sigemptyset(&old_sigterm_action.sa_mask);
1689
 
    old_sigterm_action.sa_handler = SIG_DFL;
1690
 
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
1691
 
                                            &old_sigterm_action,
1692
 
                                            NULL));
1693
 
    if(ret == -1){
1694
 
      perror("sigaction");
1695
 
    }
1696
 
    do {
1697
 
      ret = raise(signal_received);
1698
 
    } while(ret != 0 and errno == EINTR);
1699
 
    if(ret != 0){
1700
 
      perror("raise");
1701
 
      abort();
1702
 
    }
1703
 
    TEMP_FAILURE_RETRY(pause());
1704
 
  }
1705
 
  
1706
1346
  return exitcode;
1707
1347
}