/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/password-request.c

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#define _LARGEFILE_SOURCE
33
33
#define _FILE_OFFSET_BITS 64
34
34
 
35
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
 
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
36
36
 
37
 
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
38
 
                                   stdout, ferror() */
 
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(), stdout,
 
38
                                   ferror() */
39
39
#include <stdint.h>             /* uint16_t, uint32_t */
40
40
#include <stddef.h>             /* NULL, size_t, ssize_t */
41
41
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
42
42
                                   srand() */
43
43
#include <stdbool.h>            /* bool, true */
44
44
#include <string.h>             /* memset(), strcmp(), strlen(),
45
 
                                   strerror(), asprintf(), strcpy() */
 
45
                                   strerror(), memcpy(), strcpy() */
46
46
#include <sys/ioctl.h>          /* ioctl */
 
47
#include <net/if.h>             /* ifreq, SIOCGIFFLAGS, SIOCSIFFLAGS,
 
48
                                   IFF_UP */
47
49
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
48
50
                                   sockaddr_in6, PF_INET6,
49
51
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
81
83
#include <avahi-common/error.h>
82
84
 
83
85
/* GnuTLS */
84
 
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and
85
 
                                   functions:
 
86
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and functions
86
87
                                   gnutls_*
87
88
                                   init_gnutls_session(),
88
89
                                   GNUTLS_* */
90
91
                                   GNUTLS_OPENPGP_FMT_BASE64 */
91
92
 
92
93
/* GPGME */
93
 
#include <gpgme.h>              /* All GPGME types, constants and
94
 
                                   functions:
 
94
#include <gpgme.h>              /* All GPGME types, constants and functions
95
95
                                   gpgme_*
96
96
                                   GPGME_PROTOCOL_OpenPGP,
97
97
                                   GPG_ERR_NO_* */
302
302
}
303
303
 
304
304
static const char * safer_gnutls_strerror (int value) {
305
 
  const char *ret = gnutls_strerror (value); /* Spurious warning */
 
305
  const char *ret = gnutls_strerror (value);
306
306
  if (ret == NULL)
307
307
    ret = "(unknown)";
308
308
  return ret;
315
315
}
316
316
 
317
317
static int init_gnutls_global(mandos_context *mc,
318
 
                              const char *pubkeyfilename,
319
 
                              const char *seckeyfilename){
 
318
                              const char *pubkeyfile,
 
319
                              const char *seckeyfile){
320
320
  int ret;
321
321
  
322
322
  if(debug){
341
341
  /* OpenPGP credentials */
342
342
  gnutls_certificate_allocate_credentials(&mc->cred);
343
343
  if (ret != GNUTLS_E_SUCCESS){
344
 
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
345
 
                                                     warning */
 
344
    fprintf (stderr, "GnuTLS memory error: %s\n",
346
345
             safer_gnutls_strerror(ret));
347
346
    gnutls_global_deinit ();
348
347
    return -1;
350
349
  
351
350
  if(debug){
352
351
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
353
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
354
 
            seckeyfilename);
 
352
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
353
            seckeyfile);
355
354
  }
356
355
  
357
356
  ret = gnutls_certificate_set_openpgp_key_file
358
 
    (mc->cred, pubkeyfilename, seckeyfilename,
359
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
357
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
360
358
  if (ret != GNUTLS_E_SUCCESS) {
361
359
    fprintf(stderr,
362
360
            "Error[%d] while reading the OpenPGP key pair ('%s',"
363
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
361
            " '%s')\n", ret, pubkeyfile, seckeyfile);
364
362
    fprintf(stdout, "The GnuTLS error is: %s\n",
365
363
            safer_gnutls_strerror(ret));
366
364
    goto globalfail;
476
474
    fprintf(stderr, "Binding to interface %s\n", interface);
477
475
  }
478
476
  
479
 
  memset(&to, 0, sizeof(to));
 
477
  memset(&to,0,sizeof(to));     /* Spurious warning */
480
478
  to.in6.sin6_family = AF_INET6;
481
479
  /* It would be nice to have a way to detect if we were passed an
482
480
     IPv4 address here.   Now we assume an IPv6 address. */
489
487
    fprintf(stderr, "Bad address: %s\n", ip);
490
488
    return -1;
491
489
  }
492
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
490
  to.in6.sin6_port = htons(port);       /* Spurious warning */
493
491
  
494
492
  to.in6.sin6_scope_id = (uint32_t)if_index;
495
493
  
636
634
    } else {
637
635
      retval = -1;
638
636
    }
639
 
  } else {
640
 
    retval = -1;
641
637
  }
642
638
  
643
639
  /* Shutdown procedure */
664
660
                             flags,
665
661
                             void* userdata) {
666
662
  mandos_context *mc = userdata;
667
 
  assert(r);
 
663
  assert(r);                    /* Spurious warning */
668
664
  
669
665
  /* Called whenever a service has been resolved successfully or
670
666
     timed out */
688
684
      }
689
685
      int ret = start_mandos_communication(ip, port, interface, mc);
690
686
      if (ret == 0){
691
 
        avahi_simple_poll_quit(mc->simple_poll);
 
687
        exit(EXIT_SUCCESS);
692
688
      }
693
689
    }
694
690
  }
706
702
                             flags,
707
703
                             void* userdata) {
708
704
  mandos_context *mc = userdata;
709
 
  assert(b);
 
705
  assert(b);                    /* Spurious warning */
710
706
  
711
707
  /* Called whenever a new services becomes available on the LAN or
712
708
     is removed from the LAN */
748
744
 
749
745
/* Combines file name and path and returns the malloced new
750
746
   string. some sane checks could/should be added */
751
 
static char *combinepath(const char *first, const char *second){
752
 
  char *tmp;
753
 
  int ret = asprintf(&tmp, "%s/%s", first, second);
754
 
  if(ret < 0){
 
747
static const char *combinepath(const char *first, const char *second){
 
748
  size_t f_len = strlen(first);
 
749
  size_t s_len = strlen(second);
 
750
  char *tmp = malloc(f_len + s_len + 2);
 
751
  if (tmp == NULL){
755
752
    return NULL;
756
753
  }
 
754
  if(f_len > 0){
 
755
    memcpy(tmp, first, f_len);  /* Spurious warning */
 
756
  }
 
757
  tmp[f_len] = '/';
 
758
  if(s_len > 0){
 
759
    memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
 
760
  }
 
761
  tmp[f_len + 1 + s_len] = '\0';
757
762
  return tmp;
758
763
}
759
764
 
770
775
    gid_t gid;
771
776
    char *connect_to = NULL;
772
777
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
773
 
    char *pubkeyfilename = NULL;
774
 
    char *seckeyfilename = NULL;
775
 
    const char *pubkeyname = "pubkey.txt";
776
 
    const char *seckeyname = "seckey.txt";
 
778
    const char *pubkeyfile = "pubkey.txt";
 
779
    const char *seckeyfile = "seckey.txt";
777
780
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
778
781
                          .dh_bits = 1024, .priority = "SECURE256"};
779
782
    bool gnutls_initalized = false;
831
834
          keydir = arg;
832
835
          break;
833
836
        case 's':
834
 
          seckeyname = arg;
 
837
          seckeyfile = arg;
835
838
          break;
836
839
        case 'p':
837
 
          pubkeyname = arg;
 
840
          pubkeyfile = arg;
838
841
          break;
839
842
        case 129:
840
843
          errno = 0;
849
852
          break;
850
853
        case ARGP_KEY_ARG:
851
854
          argp_usage (state);
852
 
        case ARGP_KEY_END:
853
855
          break;
 
856
          case ARGP_KEY_END:
 
857
            break;
854
858
        default:
855
859
          return ARGP_ERR_UNKNOWN;
856
860
        }
863
867
                           " passwords from mandos server" };
864
868
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
865
869
      if (ret == ARGP_ERR_UNKNOWN){
866
 
        fprintf(stderr, "Unknown error while parsing arguments\n");
 
870
        fprintf(stderr, "Unkown error while parsing arguments\n");
867
871
        exitcode = EXIT_FAILURE;
868
872
        goto end;
869
873
      }
870
874
    }
871
875
      
872
 
    pubkeyfilename = combinepath(keydir, pubkeyname);
873
 
    if (pubkeyfilename == NULL){
 
876
    pubkeyfile = combinepath(keydir, pubkeyfile);
 
877
    if (pubkeyfile == NULL){
874
878
      perror("combinepath");
875
879
      exitcode = EXIT_FAILURE;
876
880
      goto end;
877
881
    }
878
882
    
879
 
    seckeyfilename = combinepath(keydir, seckeyname);
880
 
    if (seckeyfilename == NULL){
 
883
    seckeyfile = combinepath(keydir, seckeyfile);
 
884
    if (seckeyfile == NULL){
881
885
      perror("combinepath");
882
 
      exitcode = EXIT_FAILURE;
883
886
      goto end;
884
887
    }
885
888
 
886
 
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
 
889
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
887
890
    if (ret == -1){
888
 
      fprintf(stderr, "init_gnutls_global failed\n");
889
 
      exitcode = EXIT_FAILURE;
 
891
      fprintf(stderr, "init_gnutls_global\n");
890
892
      goto end;
891
893
    } else {
892
894
      gnutls_initalized = true;
893
895
    }
894
 
    
895
 
    /* If the interface is down, bring it up */
896
 
    {
897
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
898
 
      if(sd < 0) {
899
 
        perror("socket");
900
 
        exitcode = EXIT_FAILURE;
901
 
        goto end;
902
 
      }
903
 
      strcpy(network.ifr_name, interface);
904
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
905
 
      if(ret == -1){
906
 
        perror("ioctl SIOCGIFFLAGS");
907
 
        exitcode = EXIT_FAILURE;
908
 
        goto end;
909
 
      }
910
 
      if((network.ifr_flags & IFF_UP) == 0){
911
 
        network.ifr_flags |= IFF_UP;
912
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
913
 
        if(ret == -1){
914
 
          perror("ioctl SIOCSIFFLAGS");
915
 
          exitcode = EXIT_FAILURE;
916
 
          goto end;
917
 
        }
918
 
      }
919
 
      close(sd);
920
 
    }
921
 
    
 
896
 
922
897
    uid = getuid();
923
898
    gid = getgid();
924
 
    
 
899
 
925
900
    ret = setuid(uid);
926
901
    if (ret == -1){
927
902
      perror("setuid");
965
940
      goto end;
966
941
    }
967
942
    
 
943
    /* If the interface is down, bring it up */
 
944
    {
 
945
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
946
      if(sd < 0) {
 
947
        perror("socket");
 
948
        exitcode = EXIT_FAILURE;
 
949
        goto end;
 
950
      }
 
951
      strcpy(network.ifr_name, interface); /* Spurious warning */
 
952
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
953
      if(ret == -1){
 
954
        perror("ioctl SIOCGIFFLAGS");
 
955
        exitcode = EXIT_FAILURE;
 
956
        goto end;
 
957
      }
 
958
      if((network.ifr_flags & IFF_UP) == 0){
 
959
        network.ifr_flags |= IFF_UP;
 
960
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
961
        if(ret == -1){
 
962
          perror("ioctl SIOCSIFFLAGS");
 
963
          exitcode = EXIT_FAILURE;
 
964
          goto end;
 
965
        }
 
966
      }
 
967
      close(sd);
 
968
    }
 
969
    
968
970
    if (not debug){
969
971
      avahi_set_log_function(empty_log);
970
972
    }
1042
1044
 
1043
1045
    if (mc.simple_poll != NULL)
1044
1046
        avahi_simple_poll_free(mc.simple_poll);
1045
 
    free(pubkeyfilename);
1046
 
    free(seckeyfilename);
 
1047
    free(pubkeyfile);
 
1048
    free(seckeyfile);
1047
1049
 
1048
1050
    if (gnutls_initalized){
1049
1051
      gnutls_certificate_free_credentials(mc.cred);