2003-06-11 23:36:48 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-04-30 22:22:54 +00:00
|
|
|
// 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
|
2003-06-11 23:36:48 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-07-02 02:02:18 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// 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
|
2003-07-02 02:02:18 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-07-02 20:57:59 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-07-02 02:02:18 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2004-01-30 22:19:19 +00:00
|
|
|
// The "policy" part of file transfer is here.
|
|
|
|
// The "mechanism" part is in pers_file_xfer.C and file_xfer.C
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
|
|
|
|
2003-10-16 19:03:49 +00:00
|
|
|
#include "cpp.h"
|
2002-06-06 18:42:01 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2002-06-06 18:42:01 +00:00
|
|
|
#endif
|
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifndef _WIN32
|
2003-07-23 22:24:28 +00:00
|
|
|
#include <cassert>
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <sys/stat.h>
|
2002-06-06 18:42:01 +00:00
|
|
|
#include <sys/types.h>
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
#include "md5_file.h"
|
2002-07-07 20:39:24 +00:00
|
|
|
#include "crypt.h"
|
|
|
|
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "filesys.h"
|
|
|
|
#include "error_numbers.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
#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 "client_state.h"
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "client_msgs.h"
|
|
|
|
#include "file_xfer.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-12-02 22:47:32 +00:00
|
|
|
#define MAX_TRANSFERS_PER_PROJECT 2
|
|
|
|
|
2004-06-30 18:17:21 +00:00
|
|
|
using std::vector;
|
|
|
|
|
2003-03-02 19:24:09 +00:00
|
|
|
// Decide whether to consider starting a new file transfer
|
2002-08-30 22:11:36 +00:00
|
|
|
//
|
2003-12-02 22:47:32 +00:00
|
|
|
bool CLIENT_STATE::start_new_file_xfer(PERS_FILE_XFER& pfx) {
|
|
|
|
unsigned int i;
|
|
|
|
int n;
|
|
|
|
|
2004-04-03 00:43:55 +00:00
|
|
|
if (activities_suspended || network_suspended) return false;
|
2003-12-02 22:47:32 +00:00
|
|
|
|
|
|
|
// limit the number of file transfers per project
|
|
|
|
//
|
|
|
|
n = 0;
|
|
|
|
for (i=0; i<file_xfers->file_xfers.size(); i++) {
|
|
|
|
FILE_XFER* fxp = file_xfers->file_xfers[i];
|
|
|
|
if (pfx.fip->project == fxp->fip->project) {
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n >= MAX_TRANSFERS_PER_PROJECT) return false;
|
|
|
|
return true;
|
2002-08-30 22:11:36 +00:00
|
|
|
}
|
|
|
|
|
2003-05-09 22:44:35 +00:00
|
|
|
void CLIENT_STATE::trunc_stderr_stdout() {
|
|
|
|
double f_size;
|
|
|
|
|
2003-05-14 18:18:33 +00:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
|
2003-05-09 22:44:35 +00:00
|
|
|
// If the stderr.txt or stdout.txt files are too big, reset them
|
|
|
|
// TODO: should we tell the user we're resetting these?
|
2004-06-29 19:54:50 +00:00
|
|
|
// TODO: should rotate files, not truncate!!
|
|
|
|
//
|
2003-05-09 22:44:35 +00:00
|
|
|
file_size(STDERR_FILE_NAME, f_size);
|
|
|
|
if (f_size > MAX_STDERR_FILE_SIZE) {
|
|
|
|
freopen(STDERR_FILE_NAME, "w", stderr);
|
|
|
|
}
|
|
|
|
file_size(STDOUT_FILE_NAME, f_size);
|
|
|
|
if (f_size > MAX_STDOUT_FILE_SIZE) {
|
2003-10-22 18:39:22 +00:00
|
|
|
freopen(STDOUT_FILE_NAME, "w", stdout);
|
2003-05-09 22:44:35 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-30 22:11:36 +00:00
|
|
|
|
2003-03-02 19:24:09 +00:00
|
|
|
// Make a directory for each of the projects in the client state
|
2002-08-22 21:29:58 +00:00
|
|
|
//
|
|
|
|
int CLIENT_STATE::make_project_dirs() {
|
|
|
|
unsigned int i;
|
2003-03-08 23:48:05 +00:00
|
|
|
int retval;
|
2002-08-22 21:29:58 +00:00
|
|
|
for (i=0; i<projects.size(); i++) {
|
2003-03-08 23:48:05 +00:00
|
|
|
retval = make_project_dir(*projects[i]);
|
|
|
|
if (retval) return retval;
|
2002-08-22 21:29:58 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-24 23:33:46 +00:00
|
|
|
// Verify the validity of a downloaded file,
|
|
|
|
// through MD5 checksum or an RSA signature
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2004-09-02 16:23:21 +00:00
|
|
|
int FILE_INFO::verify_downloaded_file() {
|
|
|
|
char cksum[64], pathname[256];
|
2002-07-07 20:39:24 +00:00
|
|
|
bool verified;
|
|
|
|
int retval;
|
|
|
|
|
2004-09-02 16:23:21 +00:00
|
|
|
get_pathname(this, pathname);
|
|
|
|
if (signature_required) {
|
|
|
|
if (!file_signature) {
|
|
|
|
msg_printf(project, MSG_ERROR, "file %s missing signature", name);
|
|
|
|
error_msg = "missing signature";
|
2002-08-30 20:56:02 +00:00
|
|
|
return ERR_NO_SIGNATURE;
|
|
|
|
}
|
2002-07-07 20:39:24 +00:00
|
|
|
retval = verify_file2(
|
2004-09-02 16:23:21 +00:00
|
|
|
pathname, file_signature, project->code_sign_key, verified
|
2002-07-07 20:39:24 +00:00
|
|
|
);
|
2002-08-15 21:52:04 +00:00
|
|
|
if (retval) {
|
2003-10-13 00:41:02 +00:00
|
|
|
msg_printf(project, MSG_ERROR,
|
2004-09-02 16:23:21 +00:00
|
|
|
"signature verification error for %s",
|
|
|
|
name
|
2003-10-13 00:41:02 +00:00
|
|
|
);
|
2004-09-02 16:23:21 +00:00
|
|
|
error_msg = "signature verification error";
|
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) {
|
2003-10-13 00:41:02 +00:00
|
|
|
msg_printf(project, MSG_ERROR,
|
2004-09-02 16:23:21 +00:00
|
|
|
"signature verification failed for %s",
|
|
|
|
name
|
2003-10-13 00:41:02 +00:00
|
|
|
);
|
2004-09-02 16:23:21 +00:00
|
|
|
error_msg = "signature verification failed";
|
2002-08-13 22:36:54 +00:00
|
|
|
return ERR_RSA_FAILED;
|
2002-07-11 01:09:53 +00:00
|
|
|
}
|
2004-09-02 16:23:21 +00:00
|
|
|
} else if (strlen(md5_cksum)) {
|
|
|
|
retval = md5_file(pathname, cksum, nbytes);
|
2003-11-29 02:41:28 +00:00
|
|
|
if (retval) {
|
2003-10-13 00:41:02 +00:00
|
|
|
msg_printf(project, MSG_ERROR,
|
2004-09-02 16:23:21 +00:00
|
|
|
"MD5 computation error for %s: %d\n",
|
|
|
|
name, retval
|
2003-11-29 02:41:28 +00:00
|
|
|
);
|
2004-09-02 16:23:21 +00:00
|
|
|
error_msg = "MD5 computation error";
|
2003-11-29 02:41:28 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2004-09-02 16:23:21 +00:00
|
|
|
if (strcmp(cksum, md5_cksum)) {
|
2003-11-29 02:41:28 +00:00
|
|
|
msg_printf(project, MSG_ERROR,
|
2004-09-02 16:23:21 +00:00
|
|
|
"MD5 check failed for %s", name
|
2003-10-13 00:41:02 +00:00
|
|
|
);
|
2003-11-12 23:50:40 +00:00
|
|
|
msg_printf(project, MSG_ERROR,
|
2004-09-02 16:23:21 +00:00
|
|
|
"expected %s, got %s\n", md5_cksum, cksum
|
2003-11-12 23:50:40 +00:00
|
|
|
);
|
2004-09-02 16:23:21 +00:00
|
|
|
error_msg = "MD5 check failed";
|
2002-08-15 21:52:04 +00:00
|
|
|
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
|
|
|
//
|
2004-10-14 22:01:05 +00:00
|
|
|
bool CLIENT_STATE::handle_pers_file_xfers(double now) {
|
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;
|
2003-11-02 22:51:49 +00:00
|
|
|
int retval;
|
2004-10-14 22:01:05 +00:00
|
|
|
static double last_time;
|
|
|
|
|
|
|
|
if (now - last_time < 1.0) return false;
|
|
|
|
last_time = now;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-02-20 00:06:07 +00:00
|
|
|
// Look for FILE_INFOs for which we should start a transfer,
|
|
|
|
// and make PERS_FILE_XFERs for them
|
|
|
|
//
|
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) {
|
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;
|
2003-07-29 23:26:32 +00:00
|
|
|
pers_file_xfers->insert(fip->pers_file_xfer);
|
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) {
|
|
|
|
|
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;
|
2003-07-29 23:26:32 +00:00
|
|
|
pers_file_xfers->insert(fip->pers_file_xfer);
|
2002-04-30 22:22:54 +00:00
|
|
|
action = true;
|
2004-08-05 21:42:26 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-21 19:12:42 +00:00
|
|
|
|
2003-02-20 00:06:07 +00:00
|
|
|
// Scan existing PERS_FILE_XFERs, looking for those that are done,
|
|
|
|
// and deleting them
|
|
|
|
//
|
|
|
|
vector<PERS_FILE_XFER*>::iterator iter;
|
2003-07-29 23:26:32 +00:00
|
|
|
iter = pers_file_xfers->pers_file_xfers.begin();
|
|
|
|
while (iter != pers_file_xfers->pers_file_xfers.end()) {
|
2003-02-20 00:06:07 +00:00
|
|
|
pfx = *iter;
|
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
|
|
|
//
|
2004-09-04 05:21:33 +00:00
|
|
|
if (pfx->pers_xfer_done) {
|
2003-11-02 22:51:49 +00:00
|
|
|
fip = pfx->fip;
|
2004-07-06 18:31:56 +00:00
|
|
|
if (fip->generated_locally || fip->upload_when_present) {
|
2003-11-02 22:51:49 +00:00
|
|
|
// file has been uploaded - delete if not sticky
|
|
|
|
//
|
|
|
|
if (!fip->sticky) {
|
|
|
|
fip->delete_file();
|
|
|
|
}
|
|
|
|
fip->uploaded = true;
|
2004-06-03 21:19:18 +00:00
|
|
|
} else if (fip->status >= 0) {
|
|
|
|
// file transfer did not fail (non-negative status)
|
2003-11-02 22:51:49 +00:00
|
|
|
|
|
|
|
// verify the file with RSA or MD5, and change permissions
|
|
|
|
//
|
2004-09-02 16:23:21 +00:00
|
|
|
retval = fip->verify_downloaded_file();
|
2003-11-02 22:51:49 +00:00
|
|
|
if (retval) {
|
|
|
|
msg_printf(fip->project, MSG_ERROR, "Checksum or signature error for %s", fip->name);
|
|
|
|
fip->status = retval;
|
|
|
|
} else {
|
|
|
|
// Set the appropriate permissions depending on whether
|
|
|
|
// it's an executable or normal file
|
|
|
|
//
|
|
|
|
retval = fip->set_permissions();
|
|
|
|
fip->status = FILE_PRESENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's a user file, reread prefs for running apps
|
|
|
|
//
|
|
|
|
if (fip->is_user_file) {
|
|
|
|
active_tasks.request_reread_prefs(fip->project);
|
|
|
|
}
|
|
|
|
}
|
2003-07-29 23:26:32 +00:00
|
|
|
iter = pers_file_xfers->pers_file_xfers.erase(iter);
|
2002-08-21 19:12:42 +00:00
|
|
|
delete pfx;
|
|
|
|
action = true;
|
2003-07-23 22:24:28 +00:00
|
|
|
// `delete pfx' should have set pfx->fip->pfx to NULL
|
2003-11-02 22:51:49 +00:00
|
|
|
assert (fip == NULL || fip->pers_file_xfer == NULL);
|
2003-02-20 00:58:55 +00:00
|
|
|
} else {
|
|
|
|
iter++;
|
2002-08-21 19:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
2003-07-02 02:02:18 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
return action;
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_66410b3cab = "$Id$";
|