/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

  • Committer: Björn Påhlsson
  • Date: 2008-09-04 19:59:05 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: belorn@braxen-20080904195905-a3qfv1np32auumwm
fixed two bugs:
      A overbuffer flow in enviro variables causing seg fault
      A incorrect use of strsep causing seg fault

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
 
 * Mandos-client - get and decrypt data from a Mandos server
 
3
 * Mandos client - get and decrypt data from a Mandos server
4
4
 *
5
5
 * This program is partly derived from an example program for an Avahi
6
6
 * service browser, downloaded from
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008,2009 Teddy Hogeborn
13
 
 * Copyright © 2008,2009 Björn Påhlsson
 
12
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
14
13
 * 
15
14
 * This program is free software: you can redistribute it and/or
16
15
 * modify it under the terms of the GNU General Public License as
36
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
37
36
 
38
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
39
 
                                   stdout, ferror(), sscanf */
 
38
                                   stdout, ferror() */
40
39
#include <stdint.h>             /* uint16_t, uint32_t */
41
40
#include <stddef.h>             /* NULL, size_t, ssize_t */
42
41
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
55
54
                                   connect() */
56
55
#include <fcntl.h>              /* open() */
57
56
#include <dirent.h>             /* opendir(), struct dirent, readdir() */
58
 
#include <inttypes.h>           /* PRIu16, SCNu16 */
 
57
#include <inttypes.h>           /* PRIu16 */
59
58
#include <assert.h>             /* assert() */
60
59
#include <errno.h>              /* perror(), errno */
61
60
#include <time.h>               /* time() */
67
66
                                   getuid(), getgid(), setuid(),
68
67
                                   setgid() */
69
68
#include <arpa/inet.h>          /* inet_pton(), htons */
70
 
#include <iso646.h>             /* not, and, or */
 
69
#include <iso646.h>             /* not, and */
71
70
#include <argp.h>               /* struct argp_option, error_t, struct
72
71
                                   argp_state, struct argp,
73
72
                                   argp_parse(), ARGP_KEY_ARG,
102
101
 
103
102
#define BUFFER_SIZE 256
104
103
 
 
104
/*
 
105
  #define PATHDIR "/conf/conf.d/mandos"
 
106
*/
 
107
 
105
108
#define PATHDIR "/conf/conf.d/mandos"
106
109
#define SECKEY "seckey.txt"
107
 
#define PUBKEY "pubkey.txt"
 
110
#define PUBKEY "pupkey.txt"
108
111
 
109
112
bool debug = false;
110
113
static const char mandos_protocol_version[] = "1";
111
 
const char *argp_program_version = "mandos-client " VERSION;
 
114
const char *argp_program_version = "password-request 1.0";
112
115
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
113
116
 
114
117
/* Used for passing in values through the Avahi callback functions */
147
150
  int ret;
148
151
  gpgme_error_t rc;
149
152
  gpgme_engine_info_t engine_info;
150
 
  
 
153
 
151
154
  
152
155
  /*
153
156
   * Helper function to insert pub and seckey to the enigne keyring.
156
159
    int fd;
157
160
    gpgme_data_t pgp_data;
158
161
    
159
 
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
 
162
    fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
160
163
    if(fd == -1){
161
164
      perror("open");
162
165
      return false;
168
171
              gpgme_strsource(rc), gpgme_strerror(rc));
169
172
      return false;
170
173
    }
171
 
    
 
174
 
172
175
    rc = gpgme_op_import(mc->ctx, pgp_data);
173
176
    if (rc != GPG_ERR_NO_ERROR){
174
177
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
175
178
              gpgme_strsource(rc), gpgme_strerror(rc));
176
179
      return false;
177
180
    }
178
 
    
179
 
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
 
181
 
 
182
    ret = TEMP_FAILURE_RETRY(close(fd));
180
183
    if(ret == -1){
181
184
      perror("close");
182
185
    }
187
190
  if (debug){
188
191
    fprintf(stderr, "Initialize gpgme\n");
189
192
  }
190
 
  
 
193
 
191
194
  /* Init GPGME */
192
195
  gpgme_check_version(NULL);
193
196
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
196
199
            gpgme_strsource(rc), gpgme_strerror(rc));
197
200
    return false;
198
201
  }
199
 
  
 
202
 
200
203
    /* Set GPGME home directory for the OpenPGP engine only */
201
204
  rc = gpgme_get_engine_info (&engine_info);
202
205
  if (rc != GPG_ERR_NO_ERROR){
216
219
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
217
220
    return false;
218
221
  }
219
 
  
 
222
 
220
223
  /* Create new GPGME "context" */
221
224
  rc = gpgme_new(&(mc->ctx));
222
225
  if (rc != GPG_ERR_NO_ERROR){
312
315
  
313
316
  /* Seek back to the beginning of the GPGME plaintext data buffer */
314
317
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
315
 
    perror("gpgme_data_seek");
 
318
    perror("pgpme_data_seek");
316
319
    plaintext_length = -1;
317
320
    goto decrypt_end;
318
321
  }
448
451
  
449
452
  gnutls_certificate_free_credentials(mc->cred);
450
453
  gnutls_global_deinit();
451
 
  gnutls_dh_params_deinit(mc->dh_params);
452
454
  return -1;
453
455
}
454
456
 
501
503
                                      AvahiIfIndex if_index,
502
504
                                      mandos_context *mc){
503
505
  int ret, tcp_sd;
504
 
  ssize_t sret;
505
506
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
506
507
  char *buffer = NULL;
507
508
  char *decrypted_buffer;
578
579
  written = 0;
579
580
  while (true){
580
581
    size_t out_size = strlen(out);
581
 
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
 
582
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
582
583
                                   out_size - written));
583
584
    if (ret == -1){
584
585
      perror("write");
633
634
      goto mandos_end;
634
635
    }
635
636
    
636
 
    sret = gnutls_record_recv(session, buffer+buffer_length,
637
 
                              BUFFER_SIZE);
638
 
    if (sret == 0){
 
637
    ret = gnutls_record_recv(session, buffer+buffer_length,
 
638
                             BUFFER_SIZE);
 
639
    if (ret == 0){
639
640
      break;
640
641
    }
641
 
    if (sret < 0){
642
 
      switch(sret){
 
642
    if (ret < 0){
 
643
      switch(ret){
643
644
      case GNUTLS_E_INTERRUPTED:
644
645
      case GNUTLS_E_AGAIN:
645
646
        break;
662
663
        goto mandos_end;
663
664
      }
664
665
    } else {
665
 
      buffer_length += (size_t) sret;
 
666
      buffer_length += (size_t) ret;
666
667
    }
667
668
  }
668
669
  
704
705
  
705
706
 mandos_end:
706
707
  free(buffer);
707
 
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
708
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
708
709
  if(ret == -1){
709
710
    perror("close");
710
711
  }
829
830
                          .dh_bits = 1024, .priority = "SECURE256"
830
831
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
831
832
    bool gnutls_initalized = false;
832
 
    bool gpgme_initalized = false;
 
833
    bool pgpme_initalized = false;
833
834
    
834
835
    {
835
836
      struct argp_option options[] = {
866
867
      
867
868
      error_t parse_opt (int key, char *arg,
868
869
                         struct argp_state *state) {
 
870
        /* Get the INPUT argument from `argp_parse', which we know is
 
871
           a pointer to our plugin list pointer. */
869
872
        switch (key) {
870
873
        case 128:               /* --debug */
871
874
          debug = true;
883
886
          pubkey = arg;
884
887
          break;
885
888
        case 129:               /* --dh-bits */
886
 
          ret = sscanf(arg, "%u", &mc.dh_bits);
887
 
          if(ret == 0 or mc.dh_bits == 0){
888
 
            fprintf(stderr, "Bad number of DH bits\n");
 
889
          errno = 0;
 
890
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
 
891
          if (errno){
 
892
            perror("strtol");
889
893
            exit(EXIT_FAILURE);
890
894
          }
891
895
          break;
914
918
      }
915
919
    }
916
920
    
 
921
    ret = init_gnutls_global(&mc, pubkey, seckey);
 
922
    if (ret == -1){
 
923
      fprintf(stderr, "init_gnutls_global failed\n");
 
924
      exitcode = EXIT_FAILURE;
 
925
      goto end;
 
926
    } else {
 
927
      gnutls_initalized = true;
 
928
    }
 
929
 
 
930
    if(mkdtemp(tempdir) == NULL){
 
931
      perror("mkdtemp");
 
932
      tempdir[0] = '\0';
 
933
      goto end;
 
934
    }
 
935
    
 
936
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
 
937
      fprintf(stderr, "pgpme_initalized failed\n");
 
938
      exitcode = EXIT_FAILURE;
 
939
      goto end;
 
940
    } else {
 
941
      pgpme_initalized = true;
 
942
    }
 
943
    
917
944
    /* If the interface is down, bring it up */
918
945
    {
919
946
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
938
965
          goto end;
939
966
        }
940
967
      }
941
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
968
      ret = TEMP_FAILURE_RETRY(close(sd));
942
969
      if(ret == -1){
943
970
        perror("close");
944
971
      }
957
984
      perror("setgid");
958
985
    }
959
986
    
960
 
    ret = init_gnutls_global(&mc, pubkey, seckey);
961
 
    if (ret == -1){
962
 
      fprintf(stderr, "init_gnutls_global failed\n");
963
 
      exitcode = EXIT_FAILURE;
964
 
      goto end;
965
 
    } else {
966
 
      gnutls_initalized = true;
967
 
    }
968
 
    
969
 
    if(mkdtemp(tempdir) == NULL){
970
 
      perror("mkdtemp");
971
 
      tempdir[0] = '\0';
972
 
      goto end;
973
 
    }
974
 
    
975
 
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
976
 
      fprintf(stderr, "gpgme_initalized failed\n");
977
 
      exitcode = EXIT_FAILURE;
978
 
      goto end;
979
 
    } else {
980
 
      gpgme_initalized = true;
981
 
    }
982
 
    
983
987
    if_index = (AvahiIfIndex) if_nametoindex(interface);
984
988
    if(if_index == 0){
985
989
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
995
999
        exitcode = EXIT_FAILURE;
996
1000
        goto end;
997
1001
      }
998
 
      uint16_t port;
999
 
      ret = sscanf(address+1, "%" SCNu16, &port);
1000
 
      if(ret == 0 or port == 0){
1001
 
        fprintf(stderr, "Bad port number\n");
 
1002
      errno = 0;
 
1003
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
 
1004
      if(errno){
 
1005
        perror("Bad port number");
1002
1006
        exitcode = EXIT_FAILURE;
1003
1007
        goto end;
1004
1008
      }
1094
1098
    if (gnutls_initalized){
1095
1099
      gnutls_certificate_free_credentials(mc.cred);
1096
1100
      gnutls_global_deinit ();
1097
 
      gnutls_dh_params_deinit(mc.dh_params);
1098
1101
    }
1099
 
    
1100
 
    if(gpgme_initalized){
 
1102
 
 
1103
    if(pgpme_initalized){
1101
1104
      gpgme_release(mc.ctx);
1102
1105
    }
1103
 
    
 
1106
 
1104
1107
    /* Removes the temp directory used by GPGME */
1105
1108
    if(tempdir[0] != '\0'){
1106
1109
      DIR *d;
1107
1110
      struct dirent *direntry;
1108
1111
      d = opendir(tempdir);
1109
1112
      if(d == NULL){
1110
 
        if(errno != ENOENT){
1111
 
          perror("opendir");
1112
 
        }
 
1113
        perror("opendir");
1113
1114
      } else {
1114
1115
        while(true){
1115
1116
          direntry = readdir(d);
1132
1133
            free(fullname);
1133
1134
          }
1134
1135
        }
1135
 
        closedir(d);
1136
1136
      }
1137
1137
      ret = rmdir(tempdir);
1138
 
      if(ret == -1 and errno != ENOENT){
 
1138
      if(ret == -1){
1139
1139
        perror("rmdir");
1140
1140
      }
1141
1141
    }