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