/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-09-03 19:13:50 UTC
  • mfrom: (24.1.83 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080903191350-la2y2wuxt67xjslb
* mandos-keygen.xml (BUGS): Commented out.

* mandos.xml (BUGS): Note non-checking of expire time of OpenPGP keys.

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 {
610
674
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
611
675
  
612
676
  if (buffer_length > 0){
613
 
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
 
677
    decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
614
678
                                               buffer_length,
615
 
                                               &decrypted_buffer,
616
 
                                               keydir);
 
679
                                               &decrypted_buffer);
617
680
    if (decrypted_buffer_size >= 0){
618
681
      written = 0;
619
682
      while(written < (size_t) decrypted_buffer_size){
642
705
  
643
706
 mandos_end:
644
707
  free(buffer);
645
 
  close(tcp_sd);
 
708
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
 
709
  if(ret == -1){
 
710
    perror("close");
 
711
  }
646
712
  gnutls_deinit (session);
647
713
  return retval;
648
714
}
744
810
  }
745
811
}
746
812
 
747
 
/* Combines file name and path and returns the malloced new
748
 
   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){
753
 
    return NULL;
754
 
  }
755
 
  return tmp;
756
 
}
757
 
 
758
 
 
759
813
int main(int argc, char *argv[]){
760
814
    AvahiSServiceBrowser *sb = NULL;
761
815
    int error;
767
821
    uid_t uid;
768
822
    gid_t gid;
769
823
    char *connect_to = NULL;
 
824
    char tempdir[] = "/tmp/mandosXXXXXX";
770
825
    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";
 
826
    const char *seckey = PATHDIR "/" SECKEY;
 
827
    const char *pubkey = PATHDIR "/" PUBKEY;
 
828
    
775
829
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
776
830
                          .dh_bits = 1024, .priority = "SECURE256"
777
831
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
778
832
    bool gnutls_initalized = false;
 
833
    bool pgpme_initalized = false;
779
834
    
780
835
    {
781
836
      struct argp_option options[] = {
790
845
          .doc = "Interface that will be used to search for Mandos"
791
846
          " servers",
792
847
          .group = 1 },
793
 
        { .name = "keydir", .key = 'd',
794
 
          .arg = "DIRECTORY",
795
 
          .doc = "Directory to read the OpenPGP key files from",
796
 
          .group = 1 },
797
848
        { .name = "seckey", .key = 's',
798
849
          .arg = "FILE",
799
850
          .doc = "OpenPGP secret key file base name",
828
879
        case 'i':               /* --interface */
829
880
          interface = arg;
830
881
          break;
831
 
        case 'd':               /* --keydir */
832
 
          keydir = arg;
833
 
          break;
834
882
        case 's':               /* --seckey */
835
 
          seckeyname = arg;
 
883
          seckey = arg;
836
884
          break;
837
885
        case 'p':               /* --pubkey */
838
 
          pubkeyname = arg;
 
886
          pubkey = arg;
839
887
          break;
840
888
        case 129:               /* --dh-bits */
841
889
          errno = 0;
870
918
      }
871
919
    }
872
920
    
873
 
    pubkeyfilename = combinepath(keydir, pubkeyname);
874
 
    if (pubkeyfilename == NULL){
875
 
      perror("combinepath");
876
 
      exitcode = EXIT_FAILURE;
877
 
      goto end;
878
 
    }
879
 
    
880
 
    seckeyfilename = combinepath(keydir, seckeyname);
881
 
    if (seckeyfilename == NULL){
882
 
      perror("combinepath");
883
 
      exitcode = EXIT_FAILURE;
884
 
      goto end;
885
 
    }
886
 
    
887
 
    ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
 
921
    ret = init_gnutls_global(&mc, pubkey, seckey);
888
922
    if (ret == -1){
889
923
      fprintf(stderr, "init_gnutls_global failed\n");
890
924
      exitcode = EXIT_FAILURE;
892
926
    } else {
893
927
      gnutls_initalized = true;
894
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
    }
895
943
    
896
944
    /* If the interface is down, bring it up */
897
945
    {
917
965
          goto end;
918
966
        }
919
967
      }
920
 
      close(sd);
 
968
      ret = TEMP_FAILURE_RETRY(close(sd));
 
969
      if(ret == -1){
 
970
        perror("close");
 
971
      }
921
972
    }
922
973
    
923
974
    uid = getuid();
1043
1094
    
1044
1095
    if (mc.simple_poll != NULL)
1045
1096
        avahi_simple_poll_free(mc.simple_poll);
1046
 
    free(pubkeyfilename);
1047
 
    free(seckeyfilename);
1048
1097
    
1049
1098
    if (gnutls_initalized){
1050
1099
      gnutls_certificate_free_credentials(mc.cred);
1051
1100
      gnutls_global_deinit ();
1052
1101
    }
1053
 
    
 
1102
 
 
1103
    if(pgpme_initalized){
 
1104
      gpgme_release(mc.ctx);
 
1105
    }
 
1106
 
 
1107
    /* Removes the temp directory used by GPGME */
 
1108
    if(tempdir[0] != '\0'){
 
1109
      DIR *d;
 
1110
      struct dirent *direntry;
 
1111
      d = opendir(tempdir);
 
1112
      if(d == NULL){
 
1113
        perror("opendir");
 
1114
      } else {
 
1115
        while(true){
 
1116
          direntry = readdir(d);
 
1117
          if(direntry == NULL){
 
1118
            break;
 
1119
          }
 
1120
          if (direntry->d_type == DT_REG){
 
1121
            char *fullname = NULL;
 
1122
            ret = asprintf(&fullname, "%s/%s", tempdir,
 
1123
                           direntry->d_name);
 
1124
            if(ret < 0){
 
1125
              perror("asprintf");
 
1126
              continue;
 
1127
            }
 
1128
            ret = unlink(fullname);
 
1129
            if(ret == -1){
 
1130
              fprintf(stderr, "unlink(\"%s\"): %s",
 
1131
                      fullname, strerror(errno));
 
1132
            }
 
1133
            free(fullname);
 
1134
          }
 
1135
        }
 
1136
      }
 
1137
      ret = rmdir(tempdir);
 
1138
      if(ret == -1){
 
1139
        perror("rmdir");
 
1140
      }
 
1141
    }
 
1142
          
1054
1143
    return exitcode;
1055
1144
}