/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

removed keyring pre-requirement for starting password-request.
        Keys will now be imported in run-time to a run-time created keyring

Changed seckey and pubkey to be paths to private and public keys of the pgp encrypted password and gnutls authentication credentials.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
48
48
                                   sockaddr_in6, PF_INET6,
49
49
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
50
 
                                   uid_t, gid_t */
51
 
#include <inttypes.h>           /* PRIu16 */
 
50
                                   uid_t, gid_t, open(), opendir(), DIR */
 
51
#include <sys/stat.h>           /* open() */
52
52
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
53
53
                                   struct in6_addr, inet_pton(),
54
54
                                   connect() */
 
55
#include <fcntl.h>              /* open() */
 
56
#include <dirent.h>             /* opendir(), struct dirent, readdir() */
 
57
#include <inttypes.h>           /* PRIu16 */
55
58
#include <assert.h>             /* assert() */
56
59
#include <errno.h>              /* perror(), errno */
57
60
#include <time.h>               /* time() */
58
61
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
59
62
                                   SIOCSIFFLAGS, if_indextoname(),
60
63
                                   if_nametoindex(), IF_NAMESIZE */
 
64
#include <netinet/in.h>
61
65
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
62
66
                                   getuid(), getgid(), setuid(),
63
67
                                   setgid() */
64
 
#include <netinet/in.h>
65
68
#include <arpa/inet.h>          /* inet_pton(), htons */
66
69
#include <iso646.h>             /* not, and */
67
70
#include <argp.h>               /* struct argp_option, error_t, struct
98
101
 
99
102
#define BUFFER_SIZE 256
100
103
 
 
104
/*
 
105
  #define PATHDIR "/conf/conf.d/mandos"
 
106
*/
 
107
 
 
108
#define PATHDIR "/conf/conf.d/mandos"
 
109
#define SECKEY "seckey.txt"
 
110
#define PUBKEY "pupkey.txt"
 
111
 
101
112
bool debug = false;
102
 
static const char *keydir = "/conf/conf.d/mandos";
103
113
static const char mandos_protocol_version[] = "1";
104
114
const char *argp_program_version = "password-request 1.0";
105
115
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
112
122
  unsigned int dh_bits;
113
123
  gnutls_dh_params_t dh_params;
114
124
  const char *priority;
 
125
  gpgme_ctx_t ctx;
115
126
} mandos_context;
116
127
 
117
128
/*
132
143
}
133
144
 
134
145
/* 
135
 
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
136
 
 * Returns -1 on error
 
146
 * Initialize GPGME.
137
147
 */
138
 
static ssize_t pgp_packet_decrypt (const char *cryptotext,
139
 
                                   size_t crypto_size,
140
 
                                   char **plaintext,
141
 
                                   const char *homedir){
142
 
  gpgme_data_t dh_crypto, dh_plain;
143
 
  gpgme_ctx_t ctx;
 
148
static bool init_gpgme(mandos_context *mc, const char *seckey,
 
149
                       const char *pubkey, const char *tempdir){
 
150
  int ret;
144
151
  gpgme_error_t rc;
145
 
  ssize_t ret;
146
 
  size_t plaintext_capacity = 0;
147
 
  ssize_t plaintext_length = 0;
148
152
  gpgme_engine_info_t engine_info;
 
153
 
 
154
  
 
155
  /*
 
156
   * Helper function to insert pub and seckey to the enigne keyring.
 
157
   */
 
158
  bool import_key(const char *filename){
 
159
    int fd;
 
160
    gpgme_data_t pgp_data;
 
161
    
 
162
    fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
 
163
    if(fd == -1){
 
164
      perror("open");
 
165
      return false;
 
166
    }
 
167
    
 
168
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
 
169
    if (rc != GPG_ERR_NO_ERROR){
 
170
      fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
 
171
              gpgme_strsource(rc), gpgme_strerror(rc));
 
172
      return false;
 
173
    }
 
174
 
 
175
    rc = gpgme_op_import(mc->ctx, pgp_data);
 
176
    if (rc != GPG_ERR_NO_ERROR){
 
177
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
 
178
              gpgme_strsource(rc), gpgme_strerror(rc));
 
179
      return false;
 
180
    }
 
181
 
 
182
    ret = TEMP_FAILURE_RETRY(close(fd));
 
183
    if(ret == -1){
 
184
      perror("close");
 
185
    }
 
186
    gpgme_data_release(pgp_data);
 
187
    return true;
 
188
  }
149
189
  
150
190
  if (debug){
151
 
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
 
191
    fprintf(stderr, "Initialize gpgme\n");
152
192
  }
153
 
  
 
193
 
154
194
  /* Init GPGME */
155
195
  gpgme_check_version(NULL);
156
196
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
157
197
  if (rc != GPG_ERR_NO_ERROR){
158
198
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
159
199
            gpgme_strsource(rc), gpgme_strerror(rc));
160
 
    return -1;
 
200
    return false;
161
201
  }
162
 
  
163
 
  /* Set GPGME home directory for the OpenPGP engine only */
 
202
 
 
203
    /* Set GPGME home directory for the OpenPGP engine only */
164
204
  rc = gpgme_get_engine_info (&engine_info);
165
205
  if (rc != GPG_ERR_NO_ERROR){
166
206
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
167
207
            gpgme_strsource(rc), gpgme_strerror(rc));
168
 
    return -1;
 
208
    return false;
169
209
  }
170
210
  while(engine_info != NULL){
171
211
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
172
212
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
173
 
                            engine_info->file_name, homedir);
 
213
                            engine_info->file_name, tempdir);
174
214
      break;
175
215
    }
176
216
    engine_info = engine_info->next;
177
217
  }
178
218
  if(engine_info == NULL){
179
 
    fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
180
 
    return -1;
 
219
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
 
220
    return false;
 
221
  }
 
222
 
 
223
  /* Create new GPGME "context" */
 
224
  rc = gpgme_new(&(mc->ctx));
 
225
  if (rc != GPG_ERR_NO_ERROR){
 
226
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
 
227
            gpgme_strsource(rc), gpgme_strerror(rc));
 
228
    return false;
 
229
  }
 
230
  
 
231
  if (not import_key(pubkey) or not import_key(seckey)){
 
232
    return false;
 
233
  }
 
234
  
 
235
  return true; 
 
236
}
 
237
 
 
238
/* 
 
239
 * Decrypt OpenPGP data.
 
240
 * Returns -1 on error
 
241
 */
 
242
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
 
243
                                   const char *cryptotext,
 
244
                                   size_t crypto_size,
 
245
                                   char **plaintext){
 
246
  gpgme_data_t dh_crypto, dh_plain;
 
247
  gpgme_error_t rc;
 
248
  ssize_t ret;
 
249
  size_t plaintext_capacity = 0;
 
250
  ssize_t plaintext_length = 0;
 
251
  
 
252
  if (debug){
 
253
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
181
254
  }
182
255
  
183
256
  /* Create new GPGME data buffer from memory cryptotext */
198
271
    return -1;
199
272
  }
200
273
  
201
 
  /* Create new GPGME "context" */
202
 
  rc = gpgme_new(&ctx);
203
 
  if (rc != GPG_ERR_NO_ERROR){
204
 
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
205
 
            gpgme_strsource(rc), gpgme_strerror(rc));
206
 
    plaintext_length = -1;
207
 
    goto decrypt_end;
208
 
  }
209
 
  
210
274
  /* Decrypt data from the cryptotext data buffer to the plaintext
211
275
     data buffer */
212
 
  rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
 
276
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
213
277
  if (rc != GPG_ERR_NO_ERROR){
214
278
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
215
279
            gpgme_strsource(rc), gpgme_strerror(rc));
216
280
    plaintext_length = -1;
217
281
    if (debug){
218
282
      gpgme_decrypt_result_t result;
219
 
      result = gpgme_op_decrypt_result(ctx);
 
283
      result = gpgme_op_decrypt_result(mc->ctx);
220
284
      if (result == NULL){
221
285
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
222
286
      } else {
360
424
    fprintf(stderr,
361
425
            "Error[%d] while reading the OpenPGP key pair ('%s',"
362
426
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
363
 
    fprintf(stdout, "The GnuTLS error is: %s\n",
 
427
    fprintf(stderr, "The GnuTLS error is: %s\n",
364
428
            safer_gnutls_strerror(ret));
365
429
    goto globalfail;
366
430
  }
611
675
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
612
676
  
613
677
  if (buffer_length > 0){
614
 
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
 
678
    decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
615
679
                                               buffer_length,
616
 
                                               &decrypted_buffer,
617
 
                                               keydir);
 
680
                                               &decrypted_buffer);
618
681
    if (decrypted_buffer_size >= 0){
619
682
      written = 0;
620
683
      while(written < (size_t) decrypted_buffer_size){
643
706
  
644
707
 mandos_end:
645
708
  free(buffer);
646
 
  close(tcp_sd);
 
709
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
 
710
  if(ret == -1){
 
711
    perror("close");
 
712
  }
647
713
  gnutls_deinit (session);
648
714
  return retval;
649
715
}
745
811
  }
746
812
}
747
813
 
748
 
/* Combines file name and path and returns the malloced new
749
 
   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){
754
 
    return NULL;
755
 
  }
756
 
  return tmp;
757
 
}
758
 
 
759
 
 
760
814
int main(int argc, char *argv[]){
761
815
    AvahiSServiceBrowser *sb = NULL;
762
816
    int error;
768
822
    uid_t uid;
769
823
    gid_t gid;
770
824
    char *connect_to = NULL;
 
825
    char tempdir[] = "/tmp/mandosXXXXXX";
771
826
    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";
 
827
    const char *seckey = PATHDIR "/" SECKEY;
 
828
    const char *pubkey = PATHDIR "/" PUBKEY;
 
829
    
776
830
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
777
831
                          .dh_bits = 1024, .priority = "SECURE256"};
778
832
    bool gnutls_initalized = false;
 
833
    bool pgpme_initalized = false;
779
834
    
780
835
    {
781
836
      struct argp_option options[] = {
789
844
          .arg = "INTERFACE",
790
845
          .doc = "Interface that Avahi will conntect through",
791
846
          .group = 1 },
792
 
        { .name = "keydir", .key = 'd',
793
 
          .arg = "KEYDIR",
794
 
          .doc = "Directory where the openpgp keyring is",
795
 
          .group = 1 },
796
847
        { .name = "seckey", .key = 's',
797
848
          .arg = "SECKEY",
798
849
          .doc = "Secret openpgp key for gnutls authentication",
826
877
        case 'i':
827
878
          interface = arg;
828
879
          break;
829
 
        case 'd':
830
 
          keydir = arg;
831
 
          break;
832
880
        case 's':
833
 
          seckeyname = arg;
 
881
          seckey = arg;
834
882
          break;
835
883
        case 'p':
836
 
          pubkeyname = arg;
 
884
          pubkey = arg;
837
885
          break;
838
886
        case 129:
839
887
          errno = 0;
867
915
        goto end;
868
916
      }
869
917
    }
870
 
      
871
 
    pubkeyfilename = combinepath(keydir, pubkeyname);
872
 
    if (pubkeyfilename == NULL){
873
 
      perror("combinepath");
874
 
      exitcode = EXIT_FAILURE;
875
 
      goto end;
876
 
    }
877
 
    
878
 
    seckeyfilename = combinepath(keydir, seckeyname);
879
 
    if (seckeyfilename == NULL){
880
 
      perror("combinepath");
881
 
      exitcode = EXIT_FAILURE;
882
 
      goto end;
883
 
    }
884
918
 
885
 
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
 
919
    ret = init_gnutls_global(&mc, pubkey, seckey);
886
920
    if (ret == -1){
887
921
      fprintf(stderr, "init_gnutls_global failed\n");
888
922
      exitcode = EXIT_FAILURE;
890
924
    } else {
891
925
      gnutls_initalized = true;
892
926
    }
 
927
 
 
928
    if(mkdtemp(tempdir) == NULL){
 
929
      perror("mkdtemp");
 
930
      tempdir[0] = '\0';
 
931
      goto end;
 
932
    }
 
933
    
 
934
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
 
935
      fprintf(stderr, "pgpme_initalized failed\n");
 
936
      exitcode = EXIT_FAILURE;
 
937
      goto end;
 
938
    } else {
 
939
      pgpme_initalized = true;
 
940
    }
893
941
    
894
942
    /* If the interface is down, bring it up */
895
943
    {
915
963
          goto end;
916
964
        }
917
965
      }
918
 
      close(sd);
 
966
      ret = TEMP_FAILURE_RETRY(close(sd));
 
967
      if(ret == -1){
 
968
        perror("close");
 
969
      }
919
970
    }
920
971
    
921
972
    uid = getuid();
1041
1092
 
1042
1093
    if (mc.simple_poll != NULL)
1043
1094
        avahi_simple_poll_free(mc.simple_poll);
1044
 
    free(pubkeyfilename);
1045
 
    free(seckeyfilename);
1046
1095
 
1047
1096
    if (gnutls_initalized){
1048
1097
      gnutls_certificate_free_credentials(mc.cred);
1049
1098
      gnutls_global_deinit ();
1050
1099
    }
1051
 
    
 
1100
 
 
1101
    if(pgpme_initalized){
 
1102
      gpgme_release(mc.ctx);
 
1103
    }
 
1104
 
 
1105
    /* Removes the temp directory used by GPGME */
 
1106
    if(tempdir[0] != '\0'){
 
1107
      DIR *d;
 
1108
      struct dirent *direntry;
 
1109
      d = opendir(tempdir);
 
1110
      if(d == NULL){
 
1111
        perror("opendir");
 
1112
      } else {
 
1113
        while(true){
 
1114
          direntry = readdir(d);
 
1115
          if(direntry == NULL){
 
1116
            break;
 
1117
          }
 
1118
          if (direntry->d_type == DT_REG){
 
1119
            char *fullname = NULL;
 
1120
            ret = asprintf(&fullname, "%s/%s", tempdir,
 
1121
                           direntry->d_name);
 
1122
            if(ret < 0){
 
1123
              perror("asprintf");
 
1124
              continue;
 
1125
            }
 
1126
            ret = unlink(fullname);
 
1127
            if(ret == -1){
 
1128
              fprintf(stderr, "unlink(\"%s\"): %s",
 
1129
                      fullname, strerror(errno));
 
1130
            }
 
1131
            free(fullname);
 
1132
          }
 
1133
        }
 
1134
      }
 
1135
      ret = rmdir(tempdir);
 
1136
      if(ret == -1){
 
1137
        perror("rmdir");
 
1138
      }
 
1139
    }
 
1140
          
1052
1141
    return exitcode;
1053
1142
}