2002-04-30 22:22:54 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
|
|
|
// functions relating to file transfer
|
|
|
|
//
|
|
|
|
|
2002-06-06 18:42:01 +00:00
|
|
|
#include "windows_cpp.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <sys/stat.h>
|
2002-06-06 18:42:01 +00:00
|
|
|
#include <sys/types.h>
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
#include "md5_file.h"
|
2002-07-07 20:39:24 +00:00
|
|
|
#include "crypt.h"
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "file_xfer.h"
|
|
|
|
#include "file_names.h"
|
2002-06-01 20:26:21 +00:00
|
|
|
#include "client_types.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "log_flags.h"
|
|
|
|
#include "client_state.h"
|
2002-07-11 01:09:53 +00:00
|
|
|
#include "error_numbers.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-08-30 22:11:36 +00:00
|
|
|
// Decide whether to start a new file transfer
|
|
|
|
//
|
|
|
|
bool CLIENT_STATE::start_new_file_xfer() {
|
|
|
|
// TODO: limit the number of file xfers in some way
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-22 21:29:58 +00:00
|
|
|
// Make a directory for each of the projects present
|
|
|
|
// in the client state
|
|
|
|
//
|
|
|
|
int CLIENT_STATE::make_project_dirs() {
|
|
|
|
unsigned int i;
|
|
|
|
for (i=0; i<projects.size(); i++) {
|
|
|
|
make_project_dir(*projects[i]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Verify the validity of a downloaded file, through MD5 checksum
|
|
|
|
// or an RSA signature
|
|
|
|
//
|
2002-07-07 20:39:24 +00:00
|
|
|
int verify_downloaded_file(char* pathname, FILE_INFO& file_info) {
|
|
|
|
char cksum[64];
|
|
|
|
PROJECT* project;
|
|
|
|
bool verified;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (file_info.signature_required) {
|
2002-08-30 20:56:02 +00:00
|
|
|
if (!file_info.file_signature) {
|
|
|
|
fprintf(stdout, "ERROR: file %s missing signature\n", file_info.name);
|
|
|
|
return ERR_NO_SIGNATURE;
|
|
|
|
}
|
2002-07-07 20:39:24 +00:00
|
|
|
project = file_info.project;
|
|
|
|
retval = verify_file2(
|
|
|
|
pathname, file_info.file_signature, project->code_sign_key, verified
|
|
|
|
);
|
2002-08-15 21:52:04 +00:00
|
|
|
if (retval) {
|
2002-07-11 01:09:53 +00:00
|
|
|
fprintf(stderr, "error: verify_file2: internal error\n");
|
2002-08-15 21:52:04 +00:00
|
|
|
return ERR_RSA_FAILED;
|
2002-07-11 01:09:53 +00:00
|
|
|
}
|
2002-08-15 21:52:04 +00:00
|
|
|
if (!verified) {
|
2002-07-11 01:09:53 +00:00
|
|
|
fprintf(stderr, "error: verify_file2: file not verified\n");
|
2002-08-13 22:36:54 +00:00
|
|
|
return ERR_RSA_FAILED;
|
2002-07-11 01:09:53 +00:00
|
|
|
}
|
2002-07-07 20:39:24 +00:00
|
|
|
} else if (file_info.md5_cksum) {
|
|
|
|
md5_file(pathname, cksum, file_info.nbytes);
|
2002-08-15 21:52:04 +00:00
|
|
|
if (strcmp(cksum, file_info.md5_cksum)) {
|
|
|
|
fprintf(stderr, "error: verify_file2: MD5 check failed\n");
|
|
|
|
return ERR_MD5_FAILED;
|
|
|
|
}
|
2002-07-07 20:39:24 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-21 19:12:42 +00:00
|
|
|
// scan all FILE_INFOs and PERS_FILE_XFERs.
|
|
|
|
// start and finish downloads and uploads as needed.
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2002-08-21 19:12:42 +00:00
|
|
|
bool CLIENT_STATE::handle_pers_file_xfers() {
|
2002-04-30 22:22:54 +00:00
|
|
|
unsigned int i;
|
|
|
|
FILE_INFO* fip;
|
2002-08-07 22:52:10 +00:00
|
|
|
PERS_FILE_XFER *pfx;
|
2002-04-30 22:22:54 +00:00
|
|
|
bool action = false;
|
2002-11-26 00:47:57 +00:00
|
|
|
int retval;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
for (i=0; i<file_infos.size(); i++) {
|
|
|
|
fip = file_infos[i];
|
2002-08-07 22:52:10 +00:00
|
|
|
pfx = fip->pers_file_xfer;
|
2002-08-28 21:50:51 +00:00
|
|
|
if (pfx) continue;
|
2002-08-23 00:53:00 +00:00
|
|
|
if (!fip->generated_locally && fip->status == FILE_NOT_PRESENT) {
|
|
|
|
|
|
|
|
// Set up the persistent file transfer object.
|
2002-08-28 21:50:51 +00:00
|
|
|
// This will start the download when there is available bandwidth
|
2002-08-07 22:52:10 +00:00
|
|
|
//
|
|
|
|
pfx = new PERS_FILE_XFER;
|
2002-08-15 21:52:04 +00:00
|
|
|
pfx->init(fip, false);
|
2002-08-07 22:52:10 +00:00
|
|
|
fip->pers_file_xfer = pfx;
|
2002-11-26 00:47:57 +00:00
|
|
|
retval = pers_xfers->insert(fip->pers_file_xfer);
|
|
|
|
//if (retval) gstate.report_project_error( *fip->result, retval, "" ); // *******
|
2002-04-30 22:22:54 +00:00
|
|
|
action = true;
|
2002-08-23 00:53:00 +00:00
|
|
|
} else if (fip->upload_when_present && fip->status == FILE_PRESENT && !fip->uploaded) {
|
|
|
|
|
|
|
|
// Set up the persistent file transfer object.
|
2002-08-28 21:50:51 +00:00
|
|
|
// This will start the upload when there is available bandwidth
|
2002-08-07 22:52:10 +00:00
|
|
|
//
|
|
|
|
pfx = new PERS_FILE_XFER;
|
2002-08-23 00:53:00 +00:00
|
|
|
pfx->init(fip, true);
|
2002-08-07 22:52:10 +00:00
|
|
|
fip->pers_file_xfer = pfx;
|
2002-11-26 00:47:57 +00:00
|
|
|
retval = pers_xfers->insert(fip->pers_file_xfer);
|
2002-04-30 22:22:54 +00:00
|
|
|
action = true;
|
|
|
|
}
|
|
|
|
}
|
2002-08-21 19:12:42 +00:00
|
|
|
|
|
|
|
for (i=0; i<pers_xfers->pers_file_xfers.size(); i++) {
|
|
|
|
pfx = pers_xfers->pers_file_xfers[i];
|
2002-08-23 00:53:00 +00:00
|
|
|
|
2002-08-21 19:12:42 +00:00
|
|
|
// If the transfer finished, remove the PERS_FILE_XFER object
|
|
|
|
// from the set and delete it
|
2002-08-28 21:50:51 +00:00
|
|
|
//
|
2002-08-21 19:12:42 +00:00
|
|
|
if (pfx->xfer_done) {
|
|
|
|
pfx->fip->pers_file_xfer = NULL;
|
|
|
|
pers_xfers->remove(pfx);
|
|
|
|
delete pfx;
|
2003-02-06 19:01:49 +00:00
|
|
|
i--;
|
2002-08-21 19:12:42 +00:00
|
|
|
action = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
return action;
|
|
|
|
}
|