/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/mandosclient.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-04 16:23:20 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080804162320-f53t0btf99kjn57e
* plugins.d/mandosclient.c: Cosmetic changes.

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() */
39
 
#include <stdint.h>             /* uint16_t, uint32_t */
40
 
#include <stddef.h>             /* NULL, size_t, ssize_t */
41
 
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
42
 
                                   srand() */
43
 
#include <stdbool.h>            /* bool, true */
44
 
#include <string.h>             /* memset(), strcmp(), strlen(),
45
 
                                   strerror(), asprintf(), strcpy() */
46
 
#include <sys/ioctl.h>          /* ioctl */
47
 
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
48
 
                                   sockaddr_in6, PF_INET6,
49
 
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
50
 
                                   uid_t, gid_t */
51
 
#include <inttypes.h>           /* PRIu16 */
52
 
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
53
 
                                   struct in6_addr, inet_pton(),
54
 
                                   connect() */
55
 
#include <assert.h>             /* assert() */
56
 
#include <errno.h>              /* perror(), errno */
57
 
#include <time.h>               /* time() */
 
37
#include <stdio.h>
 
38
#include <assert.h>
 
39
#include <stdlib.h>
 
40
#include <time.h>
 
41
#include <net/if.h>             /* if_nametoindex */
 
42
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
 
43
                                   SIOCSIFFLAGS */
58
44
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
59
 
                                   SIOCSIFFLAGS, if_indextoname(),
60
 
                                   if_nametoindex(), IF_NAMESIZE */
61
 
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
62
 
                                   getuid(), getgid(), setuid(),
63
 
                                   setgid() */
64
 
#include <netinet/in.h>
65
 
#include <arpa/inet.h>          /* inet_pton(), htons */
66
 
#include <iso646.h>             /* not, and */
67
 
#include <argp.h>               /* struct argp_option, error_t, struct
68
 
                                   argp_state, struct argp,
69
 
                                   argp_parse(), ARGP_KEY_ARG,
70
 
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
 
45
                                   SIOCSIFFLAGS */
71
46
 
72
 
/* Avahi */
73
 
/* All Avahi types, constants and functions
74
 
 Avahi*, avahi_*,
75
 
 AVAHI_* */
76
47
#include <avahi-core/core.h>
77
48
#include <avahi-core/lookup.h>
78
49
#include <avahi-core/log.h>
80
51
#include <avahi-common/malloc.h>
81
52
#include <avahi-common/error.h>
82
53
 
83
 
/* GnuTLS */
84
 
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and
85
 
                                   functions:
86
 
                                   gnutls_*
87
 
                                   init_gnutls_session(),
88
 
                                   GNUTLS_* */
89
 
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
90
 
                                   GNUTLS_OPENPGP_FMT_BASE64 */
 
54
/* Mandos client part */
 
55
#include <sys/types.h>          /* socket(), inet_pton() */
 
56
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
 
57
                                   struct in6_addr, inet_pton() */
 
58
#include <gnutls/gnutls.h>      /* All GnuTLS stuff */
 
59
#include <gnutls/openpgp.h>     /* GnuTLS with openpgp stuff */
91
60
 
 
61
#include <unistd.h>             /* close() */
 
62
#include <netinet/in.h>
 
63
#include <stdbool.h>            /* true */
 
64
#include <string.h>             /* memset */
 
65
#include <arpa/inet.h>          /* inet_pton() */
 
66
#include <iso646.h>             /* not */
 
67
#include <net/if.h>             /* IF_NAMESIZE */
 
68
#include <argp.h>               /* struct argp_option,
 
69
                                   struct argp_state, struct argp,
 
70
                                   argp_parse() */
92
71
/* GPGME */
93
 
#include <gpgme.h>              /* All GPGME types, constants and
94
 
                                   functions:
95
 
                                   gpgme_*
96
 
                                   GPGME_PROTOCOL_OpenPGP,
97
 
                                   GPG_ERR_NO_* */
 
72
#include <errno.h>              /* perror() */
 
73
#include <gpgme.h>
98
74
 
99
75
#define BUFFER_SIZE 256
100
76
 
101
77
bool debug = false;
102
78
static const char *keydir = "/conf/conf.d/mandos";
103
79
static const char mandos_protocol_version[] = "1";
104
 
const char *argp_program_version = "password-request 1.0";
 
80
const char *argp_program_version = "mandosclient 0.9";
105
81
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
106
82
 
107
83
/* Used for passing in values through the Avahi callback functions */
229
205
    } else {
230
206
      fprintf(stderr, "Unsupported algorithm: %s\n",
231
207
              result->unsupported_algorithm);
232
 
      fprintf(stderr, "Wrong key usage: %u\n",
 
208
      fprintf(stderr, "Wrong key usage: %d\n",
233
209
              result->wrong_key_usage);
234
210
      if(result->file_name != NULL){
235
211
        fprintf(stderr, "File name: %s\n", result->file_name);
302
278
}
303
279
 
304
280
static const char * safer_gnutls_strerror (int value) {
305
 
  const char *ret = gnutls_strerror (value); /* Spurious warning */
 
281
  const char *ret = gnutls_strerror (value);
306
282
  if (ret == NULL)
307
283
    ret = "(unknown)";
308
284
  return ret;
315
291
}
316
292
 
317
293
static int init_gnutls_global(mandos_context *mc,
318
 
                              const char *pubkeyfilename,
319
 
                              const char *seckeyfilename){
 
294
                              const char *pubkeyfile,
 
295
                              const char *seckeyfile){
320
296
  int ret;
321
297
  
322
298
  if(debug){
323
299
    fprintf(stderr, "Initializing GnuTLS\n");
324
300
  }
325
 
  
326
 
  ret = gnutls_global_init();
327
 
  if (ret != GNUTLS_E_SUCCESS) {
 
301
 
 
302
  if ((ret = gnutls_global_init ())
 
303
      != GNUTLS_E_SUCCESS) {
328
304
    fprintf (stderr, "GnuTLS global_init: %s\n",
329
305
             safer_gnutls_strerror(ret));
330
306
    return -1;
339
315
  }
340
316
  
341
317
  /* OpenPGP credentials */
342
 
  gnutls_certificate_allocate_credentials(&mc->cred);
343
 
  if (ret != GNUTLS_E_SUCCESS){
344
 
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
345
 
                                                     warning */
 
318
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
319
      != GNUTLS_E_SUCCESS) {
 
320
    fprintf (stderr, "GnuTLS memory error: %s\n",
346
321
             safer_gnutls_strerror(ret));
347
 
    gnutls_global_deinit ();
348
322
    return -1;
349
323
  }
350
324
  
351
325
  if(debug){
352
326
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
353
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
354
 
            seckeyfilename);
 
327
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
328
            seckeyfile);
355
329
  }
356
330
  
357
331
  ret = gnutls_certificate_set_openpgp_key_file
358
 
    (mc->cred, pubkeyfilename, seckeyfilename,
359
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
332
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
360
333
  if (ret != GNUTLS_E_SUCCESS) {
361
334
    fprintf(stderr,
362
335
            "Error[%d] while reading the OpenPGP key pair ('%s',"
363
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
336
            " '%s')\n", ret, pubkeyfile, seckeyfile);
364
337
    fprintf(stdout, "The GnuTLS error is: %s\n",
365
338
            safer_gnutls_strerror(ret));
366
 
    goto globalfail;
 
339
    return -1;
367
340
  }
368
341
  
369
342
  /* GnuTLS server initialization */
371
344
  if (ret != GNUTLS_E_SUCCESS) {
372
345
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
373
346
             " %s\n", safer_gnutls_strerror(ret));
374
 
    goto globalfail;
 
347
    return -1;
375
348
  }
376
349
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
377
350
  if (ret != GNUTLS_E_SUCCESS) {
378
351
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
379
352
             safer_gnutls_strerror(ret));
380
 
    goto globalfail;
 
353
    return -1;
381
354
  }
382
355
  
383
356
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
384
357
 
385
358
  return 0;
386
 
 
387
 
 globalfail:
388
 
 
389
 
  gnutls_certificate_free_credentials(mc->cred);
390
 
  gnutls_global_deinit();
391
 
  return -1;
392
 
 
393
359
}
394
360
 
395
361
static int init_gnutls_session(mandos_context *mc,
409
375
      fprintf(stderr, "Syntax error at: %s\n", err);
410
376
      fprintf(stderr, "GnuTLS error: %s\n",
411
377
              safer_gnutls_strerror(ret));
412
 
      gnutls_deinit (*session);
413
378
      return -1;
414
379
    }
415
380
  }
419
384
  if (ret != GNUTLS_E_SUCCESS) {
420
385
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
421
386
            safer_gnutls_strerror(ret));
422
 
    gnutls_deinit (*session);
423
387
    return -1;
424
388
  }
425
389
  
458
422
  }
459
423
  
460
424
  if(debug){
461
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
462
 
            "\n", ip, port);
 
425
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
 
426
            ip, port);
463
427
  }
464
428
  
465
429
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
476
440
    fprintf(stderr, "Binding to interface %s\n", interface);
477
441
  }
478
442
  
479
 
  memset(&to, 0, sizeof(to));
 
443
  memset(&to,0,sizeof(to));     /* Spurious warning */
480
444
  to.in6.sin6_family = AF_INET6;
481
445
  /* It would be nice to have a way to detect if we were passed an
482
446
     IPv4 address here.   Now we assume an IPv6 address. */
489
453
    fprintf(stderr, "Bad address: %s\n", ip);
490
454
    return -1;
491
455
  }
492
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
456
  to.in6.sin6_port = htons(port);       /* Spurious warning */
493
457
  
494
458
  to.in6.sin6_scope_id = (uint32_t)if_index;
495
459
  
496
460
  if(debug){
497
 
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
498
 
            port);
 
461
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
499
462
    char addrstr[INET6_ADDRSTRLEN] = "";
500
463
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
501
464
                 sizeof(addrstr)) == NULL){
542
505
  }
543
506
  
544
507
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
545
 
 
546
 
  do{
547
 
    ret = gnutls_handshake (session);
548
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
508
  
 
509
  ret = gnutls_handshake (session);
549
510
  
550
511
  if (ret != GNUTLS_E_SUCCESS){
551
512
    if(debug){
583
544
      case GNUTLS_E_AGAIN:
584
545
        break;
585
546
      case GNUTLS_E_REHANDSHAKE:
586
 
        do{
587
 
          ret = gnutls_handshake (session);
588
 
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
547
        ret = gnutls_handshake (session);
589
548
        if (ret < 0){
590
549
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
591
550
          gnutls_perror (ret);
644
603
  free(buffer);
645
604
  close(tcp_sd);
646
605
  gnutls_deinit (session);
 
606
  gnutls_certificate_free_credentials (mc->cred);
 
607
  gnutls_global_deinit ();
647
608
  return retval;
648
609
}
649
610
 
662
623
                             flags,
663
624
                             void* userdata) {
664
625
  mandos_context *mc = userdata;
665
 
  assert(r);
 
626
  assert(r);                    /* Spurious warning */
666
627
  
667
628
  /* Called whenever a service has been resolved successfully or
668
629
     timed out */
680
641
      char ip[AVAHI_ADDRESS_STR_MAX];
681
642
      avahi_address_snprint(ip, sizeof(ip), address);
682
643
      if(debug){
683
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
684
 
                PRIu16 ") on port %d\n", name, host_name, ip,
685
 
                interface, port);
 
644
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
 
645
                " port %d\n", name, host_name, ip, interface, port);
686
646
      }
687
647
      int ret = start_mandos_communication(ip, port, interface, mc);
688
648
      if (ret == 0){
689
 
        avahi_simple_poll_quit(mc->simple_poll);
 
649
        exit(EXIT_SUCCESS);
690
650
      }
691
651
    }
692
652
  }
704
664
                             flags,
705
665
                             void* userdata) {
706
666
  mandos_context *mc = userdata;
707
 
  assert(b);
 
667
  assert(b);                    /* Spurious warning */
708
668
  
709
669
  /* Called whenever a new services becomes available on the LAN or
710
670
     is removed from the LAN */
746
706
 
747
707
/* Combines file name and path and returns the malloced new
748
708
   string. some sane checks could/should be added */
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){
 
709
static const char *combinepath(const char *first, const char *second){
 
710
  size_t f_len = strlen(first);
 
711
  size_t s_len = strlen(second);
 
712
  char *tmp = malloc(f_len + s_len + 2);
 
713
  if (tmp == NULL){
753
714
    return NULL;
754
715
  }
 
716
  if(f_len > 0){
 
717
    memcpy(tmp, first, f_len);  /* Spurious warning */
 
718
  }
 
719
  tmp[f_len] = '/';
 
720
  if(s_len > 0){
 
721
    memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
 
722
  }
 
723
  tmp[f_len + 1 + s_len] = '\0';
755
724
  return tmp;
756
725
}
757
726
 
768
737
    gid_t gid;
769
738
    char *connect_to = NULL;
770
739
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
771
 
    char *pubkeyfilename = NULL;
772
 
    char *seckeyfilename = NULL;
773
 
    const char *pubkeyname = "pubkey.txt";
774
 
    const char *seckeyname = "seckey.txt";
 
740
    const char *pubkeyfile = "pubkey.txt";
 
741
    const char *seckeyfile = "seckey.txt";
775
742
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
776
743
                          .dh_bits = 1024, .priority = "SECURE256"};
777
 
    bool gnutls_initalized = false;
778
744
    
779
745
    {
780
746
      struct argp_option options[] = {
829
795
          keydir = arg;
830
796
          break;
831
797
        case 's':
832
 
          seckeyname = arg;
 
798
          seckeyfile = arg;
833
799
          break;
834
800
        case 'p':
835
 
          pubkeyname = arg;
 
801
          pubkeyfile = arg;
836
802
          break;
837
803
        case 129:
838
804
          errno = 0;
847
813
          break;
848
814
        case ARGP_KEY_ARG:
849
815
          argp_usage (state);
850
 
        case ARGP_KEY_END:
851
816
          break;
 
817
          case ARGP_KEY_END:
 
818
            break;
852
819
        default:
853
820
          return ARGP_ERR_UNKNOWN;
854
821
        }
859
826
                           .args_doc = "",
860
827
                           .doc = "Mandos client -- Get and decrypt"
861
828
                           " passwords from mandos server" };
862
 
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
863
 
      if (ret == ARGP_ERR_UNKNOWN){
864
 
        fprintf(stderr, "Unknown error while parsing arguments\n");
865
 
        exitcode = EXIT_FAILURE;
866
 
        goto end;
867
 
      }
 
829
      argp_parse (&argp, argc, argv, 0, 0, NULL);
868
830
    }
869
831
      
870
 
    pubkeyfilename = combinepath(keydir, pubkeyname);
871
 
    if (pubkeyfilename == NULL){
 
832
    pubkeyfile = combinepath(keydir, pubkeyfile);
 
833
    if (pubkeyfile == NULL){
872
834
      perror("combinepath");
873
835
      exitcode = EXIT_FAILURE;
874
836
      goto end;
875
837
    }
876
838
    
877
 
    seckeyfilename = combinepath(keydir, seckeyname);
878
 
    if (seckeyfilename == NULL){
 
839
    seckeyfile = combinepath(keydir, seckeyfile);
 
840
    if (seckeyfile == NULL){
879
841
      perror("combinepath");
880
 
      exitcode = EXIT_FAILURE;
881
842
      goto end;
882
843
    }
883
844
 
884
 
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
 
845
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
885
846
    if (ret == -1){
886
 
      fprintf(stderr, "init_gnutls_global failed\n");
887
 
      exitcode = EXIT_FAILURE;
 
847
      fprintf(stderr, "init_gnutls_global\n");
888
848
      goto end;
889
 
    } else {
890
 
      gnutls_initalized = true;
891
 
    }
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
 
    
 
849
    }
 
850
 
920
851
    uid = getuid();
921
852
    gid = getgid();
922
 
    
 
853
 
923
854
    ret = setuid(uid);
924
855
    if (ret == -1){
925
856
      perror("setuid");
963
894
      goto end;
964
895
    }
965
896
    
 
897
    /* If the interface is down, bring it up */
 
898
    {
 
899
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
900
      if(sd < 0) {
 
901
        perror("socket");
 
902
        exitcode = EXIT_FAILURE;
 
903
        goto end;
 
904
      }
 
905
      strcpy(network.ifr_name, interface); /* Spurious warning */
 
906
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
907
      if(ret == -1){
 
908
        perror("ioctl SIOCGIFFLAGS");
 
909
        exitcode = EXIT_FAILURE;
 
910
        goto end;
 
911
      }
 
912
      if((network.ifr_flags & IFF_UP) == 0){
 
913
        network.ifr_flags |= IFF_UP;
 
914
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
915
        if(ret == -1){
 
916
          perror("ioctl SIOCSIFFLAGS");
 
917
          exitcode = EXIT_FAILURE;
 
918
          goto end;
 
919
        }
 
920
      }
 
921
      close(sd);
 
922
    }
 
923
    
966
924
    if (not debug){
967
925
      avahi_set_log_function(empty_log);
968
926
    }
1040
998
 
1041
999
    if (mc.simple_poll != NULL)
1042
1000
        avahi_simple_poll_free(mc.simple_poll);
1043
 
    free(pubkeyfilename);
1044
 
    free(seckeyfilename);
1045
 
 
1046
 
    if (gnutls_initalized){
1047
 
      gnutls_certificate_free_credentials(mc.cred);
1048
 
      gnutls_global_deinit ();
1049
 
    }
 
1001
    free(pubkeyfile);
 
1002
    free(seckeyfile);
1050
1003
    
1051
1004
    return exitcode;
1052
1005
}