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

  • Committer: Björn Påhlsson
  • Date: 2008-08-04 16:23:54 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: belorn@braxen-20080804162354-xi1jjin1b7pavc74
passprompt
        Using argp instead off getopt

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";
 
79
const char *argp_program_version = "mandosclient 0.9";
 
80
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
103
81
static const char mandos_protocol_version[] = "1";
104
 
const char *argp_program_version = "password-request 1.0";
105
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
106
82
 
107
83
/* Used for passing in values through the Avahi callback functions */
108
84
typedef struct {
114
90
  const char *priority;
115
91
} mandos_context;
116
92
 
117
 
/*
118
 
 * Make room in "buffer" for at least BUFFER_SIZE additional bytes.
 
93
/* Make room in "buffer" for at least BUFFER_SIZE additional bytes.
119
94
 * "buffer_capacity" is how much is currently allocated,
120
 
 * "buffer_length" is how much is already used.
121
 
 */
 
95
 * "buffer_length" is how much is already used. */
122
96
size_t adjustbuffer(char **buffer, size_t buffer_length,
123
97
                  size_t buffer_capacity){
124
98
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
229
203
    } else {
230
204
      fprintf(stderr, "Unsupported algorithm: %s\n",
231
205
              result->unsupported_algorithm);
232
 
      fprintf(stderr, "Wrong key usage: %u\n",
 
206
      fprintf(stderr, "Wrong key usage: %d\n",
233
207
              result->wrong_key_usage);
234
208
      if(result->file_name != NULL){
235
209
        fprintf(stderr, "File name: %s\n", result->file_name);
302
276
}
303
277
 
304
278
static const char * safer_gnutls_strerror (int value) {
305
 
  const char *ret = gnutls_strerror (value); /* Spurious warning */
 
279
  const char *ret = gnutls_strerror (value);
306
280
  if (ret == NULL)
307
281
    ret = "(unknown)";
308
282
  return ret;
315
289
}
316
290
 
317
291
static int init_gnutls_global(mandos_context *mc,
318
 
                              const char *pubkeyfilename,
319
 
                              const char *seckeyfilename){
 
292
                              const char *pubkeyfile,
 
293
                              const char *seckeyfile){
320
294
  int ret;
321
295
  
322
296
  if(debug){
323
297
    fprintf(stderr, "Initializing GnuTLS\n");
324
298
  }
325
 
  
326
 
  ret = gnutls_global_init();
327
 
  if (ret != GNUTLS_E_SUCCESS) {
 
299
 
 
300
  if ((ret = gnutls_global_init ())
 
301
      != GNUTLS_E_SUCCESS) {
328
302
    fprintf (stderr, "GnuTLS global_init: %s\n",
329
303
             safer_gnutls_strerror(ret));
330
304
    return -1;
339
313
  }
340
314
  
341
315
  /* 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 */
 
316
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
317
      != GNUTLS_E_SUCCESS) {
 
318
    fprintf (stderr, "GnuTLS memory error: %s\n",
346
319
             safer_gnutls_strerror(ret));
347
 
    gnutls_global_deinit ();
348
320
    return -1;
349
321
  }
350
322
  
351
323
  if(debug){
352
324
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
353
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
354
 
            seckeyfilename);
 
325
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
326
            seckeyfile);
355
327
  }
356
328
  
357
329
  ret = gnutls_certificate_set_openpgp_key_file
358
 
    (mc->cred, pubkeyfilename, seckeyfilename,
359
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
330
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
360
331
  if (ret != GNUTLS_E_SUCCESS) {
361
332
    fprintf(stderr,
362
333
            "Error[%d] while reading the OpenPGP key pair ('%s',"
363
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
334
            " '%s')\n", ret, pubkeyfile, seckeyfile);
364
335
    fprintf(stdout, "The GnuTLS error is: %s\n",
365
336
            safer_gnutls_strerror(ret));
366
 
    goto globalfail;
 
337
    return -1;
367
338
  }
368
339
  
369
340
  /* GnuTLS server initialization */
371
342
  if (ret != GNUTLS_E_SUCCESS) {
372
343
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
373
344
             " %s\n", safer_gnutls_strerror(ret));
374
 
    goto globalfail;
 
345
    return -1;
375
346
  }
376
347
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
377
348
  if (ret != GNUTLS_E_SUCCESS) {
378
349
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
379
350
             safer_gnutls_strerror(ret));
380
 
    goto globalfail;
 
351
    return -1;
381
352
  }
382
353
  
383
354
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
384
355
 
385
356
  return 0;
386
 
 
387
 
 globalfail:
388
 
 
389
 
  gnutls_certificate_free_credentials(mc->cred);
390
 
  gnutls_global_deinit();
391
 
  return -1;
392
 
 
393
357
}
394
358
 
395
359
static int init_gnutls_session(mandos_context *mc,
409
373
      fprintf(stderr, "Syntax error at: %s\n", err);
410
374
      fprintf(stderr, "GnuTLS error: %s\n",
411
375
              safer_gnutls_strerror(ret));
412
 
      gnutls_deinit (*session);
413
376
      return -1;
414
377
    }
415
378
  }
419
382
  if (ret != GNUTLS_E_SUCCESS) {
420
383
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
421
384
            safer_gnutls_strerror(ret));
422
 
    gnutls_deinit (*session);
423
385
    return -1;
424
386
  }
425
387
  
458
420
  }
459
421
  
460
422
  if(debug){
461
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
462
 
            "\n", ip, port);
 
423
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
 
424
            ip, port);
463
425
  }
464
426
  
465
427
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
476
438
    fprintf(stderr, "Binding to interface %s\n", interface);
477
439
  }
478
440
  
479
 
  memset(&to, 0, sizeof(to));
 
441
  memset(&to,0,sizeof(to));     /* Spurious warning */
480
442
  to.in6.sin6_family = AF_INET6;
481
443
  /* It would be nice to have a way to detect if we were passed an
482
444
     IPv4 address here.   Now we assume an IPv6 address. */
489
451
    fprintf(stderr, "Bad address: %s\n", ip);
490
452
    return -1;
491
453
  }
492
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
454
  to.in6.sin6_port = htons(port);       /* Spurious warning */
493
455
  
494
456
  to.in6.sin6_scope_id = (uint32_t)if_index;
495
457
  
496
458
  if(debug){
497
 
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
498
 
            port);
 
459
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
499
460
    char addrstr[INET6_ADDRSTRLEN] = "";
500
461
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
501
462
                 sizeof(addrstr)) == NULL){
542
503
  }
543
504
  
544
505
  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);
 
506
  
 
507
  ret = gnutls_handshake (session);
549
508
  
550
509
  if (ret != GNUTLS_E_SUCCESS){
551
510
    if(debug){
583
542
      case GNUTLS_E_AGAIN:
584
543
        break;
585
544
      case GNUTLS_E_REHANDSHAKE:
586
 
        do{
587
 
          ret = gnutls_handshake (session);
588
 
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
545
        ret = gnutls_handshake (session);
589
546
        if (ret < 0){
590
547
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
591
548
          gnutls_perror (ret);
644
601
  free(buffer);
645
602
  close(tcp_sd);
646
603
  gnutls_deinit (session);
 
604
  gnutls_certificate_free_credentials (mc->cred);
 
605
  gnutls_global_deinit ();
647
606
  return retval;
648
607
}
649
608
 
662
621
                             flags,
663
622
                             void* userdata) {
664
623
  mandos_context *mc = userdata;
665
 
  assert(r);
 
624
  assert(r);                    /* Spurious warning */
666
625
  
667
626
  /* Called whenever a service has been resolved successfully or
668
627
     timed out */
680
639
      char ip[AVAHI_ADDRESS_STR_MAX];
681
640
      avahi_address_snprint(ip, sizeof(ip), address);
682
641
      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);
 
642
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
 
643
                " port %d\n", name, host_name, ip, interface, port);
686
644
      }
687
645
      int ret = start_mandos_communication(ip, port, interface, mc);
688
646
      if (ret == 0){
689
 
        avahi_simple_poll_quit(mc->simple_poll);
 
647
        exit(EXIT_SUCCESS);
690
648
      }
691
649
    }
692
650
  }
704
662
                             flags,
705
663
                             void* userdata) {
706
664
  mandos_context *mc = userdata;
707
 
  assert(b);
 
665
  assert(b);                    /* Spurious warning */
708
666
  
709
667
  /* Called whenever a new services becomes available on the LAN or
710
668
     is removed from the LAN */
746
704
 
747
705
/* Combines file name and path and returns the malloced new
748
706
   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){
 
707
static const char *combinepath(const char *first, const char *second){
 
708
  size_t f_len = strlen(first);
 
709
  size_t s_len = strlen(second);
 
710
  char *tmp = malloc(f_len + s_len + 2);
 
711
  if (tmp == NULL){
753
712
    return NULL;
754
713
  }
 
714
  if(f_len > 0){
 
715
    memcpy(tmp, first, f_len);  /* Spurious warning */
 
716
  }
 
717
  tmp[f_len] = '/';
 
718
  if(s_len > 0){
 
719
    memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
 
720
  }
 
721
  tmp[f_len + 1 + s_len] = '\0';
755
722
  return tmp;
756
723
}
757
724
 
768
735
    gid_t gid;
769
736
    char *connect_to = NULL;
770
737
    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";
 
738
    const char *pubkeyfile = "pubkey.txt";
 
739
    const char *seckeyfile = "seckey.txt";
775
740
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
776
741
                          .dh_bits = 1024, .priority = "SECURE256"};
777
 
    bool gnutls_initalized = false;
778
742
    
779
743
    {
780
744
      struct argp_option options[] = {
829
793
          keydir = arg;
830
794
          break;
831
795
        case 's':
832
 
          seckeyname = arg;
 
796
          seckeyfile = arg;
833
797
          break;
834
798
        case 'p':
835
 
          pubkeyname = arg;
 
799
          pubkeyfile = arg;
836
800
          break;
837
801
        case 129:
838
802
          errno = 0;
847
811
          break;
848
812
        case ARGP_KEY_ARG:
849
813
          argp_usage (state);
850
 
        case ARGP_KEY_END:
851
814
          break;
 
815
          case ARGP_KEY_END:
 
816
            break;
852
817
        default:
853
818
          return ARGP_ERR_UNKNOWN;
854
819
        }
859
824
                           .args_doc = "",
860
825
                           .doc = "Mandos client -- Get and decrypt"
861
826
                           " 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
 
      }
 
827
      argp_parse (&argp, argc, argv, 0, 0, NULL);
868
828
    }
869
829
      
870
 
    pubkeyfilename = combinepath(keydir, pubkeyname);
871
 
    if (pubkeyfilename == NULL){
 
830
    pubkeyfile = combinepath(keydir, pubkeyfile);
 
831
    if (pubkeyfile == NULL){
872
832
      perror("combinepath");
873
833
      exitcode = EXIT_FAILURE;
874
834
      goto end;
875
835
    }
876
836
    
877
 
    seckeyfilename = combinepath(keydir, seckeyname);
878
 
    if (seckeyfilename == NULL){
 
837
    seckeyfile = combinepath(keydir, seckeyfile);
 
838
    if (seckeyfile == NULL){
879
839
      perror("combinepath");
880
 
      exitcode = EXIT_FAILURE;
881
840
      goto end;
882
841
    }
883
842
 
884
 
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
 
843
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
885
844
    if (ret == -1){
886
 
      fprintf(stderr, "init_gnutls_global failed\n");
887
 
      exitcode = EXIT_FAILURE;
 
845
      fprintf(stderr, "init_gnutls_global\n");
888
846
      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
 
    
 
847
    }
 
848
 
920
849
    uid = getuid();
921
850
    gid = getgid();
922
 
    
 
851
 
923
852
    ret = setuid(uid);
924
853
    if (ret == -1){
925
854
      perror("setuid");
963
892
      goto end;
964
893
    }
965
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); /* Spurious warning */
 
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
    
966
922
    if (not debug){
967
923
      avahi_set_log_function(empty_log);
968
924
    }
1040
996
 
1041
997
    if (mc.simple_poll != NULL)
1042
998
        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
 
    }
 
999
    free(pubkeyfile);
 
1000
    free(seckeyfile);
1050
1001
    
1051
1002
    return exitcode;
1052
1003
}