/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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-12 19:22:34 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080812192234-8bm17713ltih9ud1
* initramfs-tools-hook: New.
* initramfs-tools-hook-conf: New.

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
47
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
48
48
                                   sockaddr_in6, PF_INET6,
81
81
#include <avahi-common/error.h>
82
82
 
83
83
/* GnuTLS */
84
 
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and
85
 
                                   functions:
 
84
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and functions
86
85
                                   gnutls_*
87
86
                                   init_gnutls_session(),
88
87
                                   GNUTLS_* */
90
89
                                   GNUTLS_OPENPGP_FMT_BASE64 */
91
90
 
92
91
/* GPGME */
93
 
#include <gpgme.h>              /* All GPGME types, constants and
94
 
                                   functions:
 
92
#include <gpgme.h>              /* All GPGME types, constants and functions
95
93
                                   gpgme_*
96
94
                                   GPGME_PROTOCOL_OpenPGP,
97
95
                                   GPG_ERR_NO_* */
214
212
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
215
213
            gpgme_strsource(rc), gpgme_strerror(rc));
216
214
    plaintext_length = -1;
217
 
    if (debug){
218
 
      gpgme_decrypt_result_t result;
219
 
      result = gpgme_op_decrypt_result(ctx);
220
 
      if (result == NULL){
221
 
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
222
 
      } else {
223
 
        fprintf(stderr, "Unsupported algorithm: %s\n",
224
 
                result->unsupported_algorithm);
225
 
        fprintf(stderr, "Wrong key usage: %u\n",
226
 
                result->wrong_key_usage);
227
 
        if(result->file_name != NULL){
228
 
          fprintf(stderr, "File name: %s\n", result->file_name);
229
 
        }
230
 
        gpgme_recipient_t recipient;
231
 
        recipient = result->recipients;
232
 
        if(recipient){
233
 
          while(recipient != NULL){
234
 
            fprintf(stderr, "Public key algorithm: %s\n",
235
 
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
236
 
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
237
 
            fprintf(stderr, "Secret key available: %s\n",
238
 
                    recipient->status == GPG_ERR_NO_SECKEY
239
 
                    ? "No" : "Yes");
240
 
            recipient = recipient->next;
241
 
          }
242
 
        }
243
 
      }
244
 
    }
245
215
    goto decrypt_end;
246
216
  }
247
217
  
249
219
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
250
220
  }
251
221
  
 
222
  if (debug){
 
223
    gpgme_decrypt_result_t result;
 
224
    result = gpgme_op_decrypt_result(ctx);
 
225
    if (result == NULL){
 
226
      fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
227
    } else {
 
228
      fprintf(stderr, "Unsupported algorithm: %s\n",
 
229
              result->unsupported_algorithm);
 
230
      fprintf(stderr, "Wrong key usage: %u\n",
 
231
              result->wrong_key_usage);
 
232
      if(result->file_name != NULL){
 
233
        fprintf(stderr, "File name: %s\n", result->file_name);
 
234
      }
 
235
      gpgme_recipient_t recipient;
 
236
      recipient = result->recipients;
 
237
      if(recipient){
 
238
        while(recipient != NULL){
 
239
          fprintf(stderr, "Public key algorithm: %s\n",
 
240
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
241
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
242
          fprintf(stderr, "Secret key available: %s\n",
 
243
                  recipient->status == GPG_ERR_NO_SECKEY
 
244
                  ? "No" : "Yes");
 
245
          recipient = recipient->next;
 
246
        }
 
247
      }
 
248
    }
 
249
  }
 
250
  
252
251
  /* Seek back to the beginning of the GPGME plaintext data buffer */
253
252
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
254
253
    perror("pgpme_data_seek");
301
300
}
302
301
 
303
302
static const char * safer_gnutls_strerror (int value) {
304
 
  const char *ret = gnutls_strerror (value); /* Spurious warning */
 
303
  const char *ret = gnutls_strerror (value);
305
304
  if (ret == NULL)
306
305
    ret = "(unknown)";
307
306
  return ret;
314
313
}
315
314
 
316
315
static int init_gnutls_global(mandos_context *mc,
317
 
                              const char *pubkeyfilename,
318
 
                              const char *seckeyfilename){
 
316
                              const char *pubkeyfile,
 
317
                              const char *seckeyfile){
319
318
  int ret;
320
319
  
321
320
  if(debug){
340
339
  /* OpenPGP credentials */
341
340
  gnutls_certificate_allocate_credentials(&mc->cred);
342
341
  if (ret != GNUTLS_E_SUCCESS){
343
 
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
344
 
                                                     warning */
 
342
    fprintf (stderr, "GnuTLS memory error: %s\n",
345
343
             safer_gnutls_strerror(ret));
346
344
    gnutls_global_deinit ();
347
345
    return -1;
349
347
  
350
348
  if(debug){
351
349
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
352
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
353
 
            seckeyfilename);
 
350
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
351
            seckeyfile);
354
352
  }
355
353
  
356
354
  ret = gnutls_certificate_set_openpgp_key_file
357
 
    (mc->cred, pubkeyfilename, seckeyfilename,
358
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
355
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
359
356
  if (ret != GNUTLS_E_SUCCESS) {
360
357
    fprintf(stderr,
361
358
            "Error[%d] while reading the OpenPGP key pair ('%s',"
362
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
359
            " '%s')\n", ret, pubkeyfile, seckeyfile);
363
360
    fprintf(stdout, "The GnuTLS error is: %s\n",
364
361
            safer_gnutls_strerror(ret));
365
362
    goto globalfail;
475
472
    fprintf(stderr, "Binding to interface %s\n", interface);
476
473
  }
477
474
  
478
 
  memset(&to, 0, sizeof(to));
 
475
  memset(&to,0,sizeof(to));     /* Spurious warning */
479
476
  to.in6.sin6_family = AF_INET6;
480
477
  /* It would be nice to have a way to detect if we were passed an
481
478
     IPv4 address here.   Now we assume an IPv6 address. */
635
632
    } else {
636
633
      retval = -1;
637
634
    }
638
 
  } else {
639
 
    retval = -1;
640
635
  }
641
636
  
642
637
  /* Shutdown procedure */
663
658
                             flags,
664
659
                             void* userdata) {
665
660
  mandos_context *mc = userdata;
666
 
  assert(r);
 
661
  assert(r);                    /* Spurious warning */
667
662
  
668
663
  /* Called whenever a service has been resolved successfully or
669
664
     timed out */
687
682
      }
688
683
      int ret = start_mandos_communication(ip, port, interface, mc);
689
684
      if (ret == 0){
690
 
        avahi_simple_poll_quit(mc->simple_poll);
 
685
        exit(EXIT_SUCCESS);
691
686
      }
692
687
    }
693
688
  }
705
700
                             flags,
706
701
                             void* userdata) {
707
702
  mandos_context *mc = userdata;
708
 
  assert(b);
 
703
  assert(b);                    /* Spurious warning */
709
704
  
710
705
  /* Called whenever a new services becomes available on the LAN or
711
706
     is removed from the LAN */
747
742
 
748
743
/* Combines file name and path and returns the malloced new
749
744
   string. some sane checks could/should be added */
750
 
static char *combinepath(const char *first, const char *second){
751
 
  char *tmp;
752
 
  int ret = asprintf(&tmp, "%s/%s", first, second);
753
 
  if(ret < 0){
 
745
static const char *combinepath(const char *first, const char *second){
 
746
  size_t f_len = strlen(first);
 
747
  size_t s_len = strlen(second);
 
748
  char *tmp = malloc(f_len + s_len + 2);
 
749
  if (tmp == NULL){
754
750
    return NULL;
755
751
  }
 
752
  if(f_len > 0){
 
753
    memcpy(tmp, first, f_len);  /* Spurious warning */
 
754
  }
 
755
  tmp[f_len] = '/';
 
756
  if(s_len > 0){
 
757
    memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
 
758
  }
 
759
  tmp[f_len + 1 + s_len] = '\0';
756
760
  return tmp;
757
761
}
758
762
 
769
773
    gid_t gid;
770
774
    char *connect_to = NULL;
771
775
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
772
 
    char *pubkeyfilename = NULL;
773
 
    char *seckeyfilename = NULL;
774
 
    const char *pubkeyname = "pubkey.txt";
775
 
    const char *seckeyname = "seckey.txt";
 
776
    const char *pubkeyfile = "pubkey.txt";
 
777
    const char *seckeyfile = "seckey.txt";
776
778
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
777
779
                          .dh_bits = 1024, .priority = "SECURE256"};
778
780
    bool gnutls_initalized = false;
830
832
          keydir = arg;
831
833
          break;
832
834
        case 's':
833
 
          seckeyname = arg;
 
835
          seckeyfile = arg;
834
836
          break;
835
837
        case 'p':
836
 
          pubkeyname = arg;
 
838
          pubkeyfile = arg;
837
839
          break;
838
840
        case 129:
839
841
          errno = 0;
848
850
          break;
849
851
        case ARGP_KEY_ARG:
850
852
          argp_usage (state);
851
 
        case ARGP_KEY_END:
852
853
          break;
 
854
          case ARGP_KEY_END:
 
855
            break;
853
856
        default:
854
857
          return ARGP_ERR_UNKNOWN;
855
858
        }
868
871
      }
869
872
    }
870
873
      
871
 
    pubkeyfilename = combinepath(keydir, pubkeyname);
872
 
    if (pubkeyfilename == NULL){
 
874
    pubkeyfile = combinepath(keydir, pubkeyfile);
 
875
    if (pubkeyfile == NULL){
873
876
      perror("combinepath");
874
877
      exitcode = EXIT_FAILURE;
875
878
      goto end;
876
879
    }
877
880
    
878
 
    seckeyfilename = combinepath(keydir, seckeyname);
879
 
    if (seckeyfilename == NULL){
 
881
    seckeyfile = combinepath(keydir, seckeyfile);
 
882
    if (seckeyfile == NULL){
880
883
      perror("combinepath");
881
884
      exitcode = EXIT_FAILURE;
882
885
      goto end;
883
886
    }
884
887
 
885
 
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
 
888
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
886
889
    if (ret == -1){
887
890
      fprintf(stderr, "init_gnutls_global failed\n");
888
891
      exitcode = EXIT_FAILURE;
899
902
        exitcode = EXIT_FAILURE;
900
903
        goto end;
901
904
      }
902
 
      strcpy(network.ifr_name, interface);
 
905
      strcpy(network.ifr_name, interface); /* Spurious warning */
903
906
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
904
907
      if(ret == -1){
905
908
        perror("ioctl SIOCGIFFLAGS");
1041
1044
 
1042
1045
    if (mc.simple_poll != NULL)
1043
1046
        avahi_simple_poll_free(mc.simple_poll);
1044
 
    free(pubkeyfilename);
1045
 
    free(seckeyfilename);
 
1047
    free(pubkeyfile);
 
1048
    free(seckeyfile);
1046
1049
 
1047
1050
    if (gnutls_initalized){
1048
1051
      gnutls_certificate_free_credentials(mc.cred);