2003-06-14 20:15:49 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-09-26 18:11:06 +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-14 20:15:49 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-06-14 20:15:51 +00:00
|
|
|
//
|
2002-09-26 18:11:06 +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-06-14 20:15:51 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-09-26 18:11:06 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-06-14 20:15:49 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-06-14 20:15:51 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-09-26 18:11:06 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2002-09-20 21:17:47 +00:00
|
|
|
// The BOINC file upload handler.
|
2002-12-02 04:29:40 +00:00
|
|
|
// See doc/upload.html for protocol spec.
|
2002-07-05 05:33:40 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2002-07-12 00:01:11 +00:00
|
|
|
#include <stdlib.h>
|
2002-07-05 05:33:40 +00:00
|
|
|
#include <string.h>
|
2003-01-07 08:11:16 +00:00
|
|
|
#include <time.h>
|
2002-08-07 22:52:10 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <errno.h>
|
2002-07-05 05:33:40 +00:00
|
|
|
|
|
|
|
#include "parse.h"
|
2003-03-06 17:42:49 +00:00
|
|
|
#include "util.h"
|
2002-10-03 18:33:46 +00:00
|
|
|
#include "config.h"
|
2003-05-16 19:22:57 +00:00
|
|
|
#include "sched_util.h"
|
2002-07-05 05:33:40 +00:00
|
|
|
#include "crypt.h"
|
|
|
|
|
2002-10-03 18:33:46 +00:00
|
|
|
CONFIG config;
|
|
|
|
|
2003-05-08 17:13:57 +00:00
|
|
|
#define ERR_TRANSIENT true
|
|
|
|
#define ERR_PERMANENT false
|
|
|
|
|
2003-06-14 20:15:50 +00:00
|
|
|
#define DEBUG_LEVEL SchedMessages::NORMAL
|
2003-06-14 20:15:44 +00:00
|
|
|
|
2003-06-14 20:15:52 +00:00
|
|
|
// #define STDERR_FILENAME "file_upload_handler.out"
|
|
|
|
static const char* STDERR_FILENAME = "../log/file_upload_handler.log";
|
2002-12-02 04:29:40 +00:00
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
#define MAX_FILES 32
|
|
|
|
|
|
|
|
struct FILE_INFO {
|
|
|
|
char name[256];
|
|
|
|
double max_nbytes;
|
2002-07-07 20:39:24 +00:00
|
|
|
char* xml_signature;
|
2002-07-05 05:33:40 +00:00
|
|
|
char* signed_xml;
|
|
|
|
int parse(FILE*);
|
|
|
|
};
|
|
|
|
|
|
|
|
int FILE_INFO::parse(FILE* in) {
|
2003-06-14 20:15:47 +00:00
|
|
|
char buf[256];
|
2002-07-05 05:33:40 +00:00
|
|
|
int retval;
|
2002-11-09 20:26:50 +00:00
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
memset(this, 0, sizeof(FILE_INFO));
|
|
|
|
signed_xml = strdup("");
|
|
|
|
while (fgets(buf, 256, in)) {
|
|
|
|
if (match_tag(buf, "</file_info>")) return 0;
|
2002-07-07 20:39:24 +00:00
|
|
|
else if (match_tag(buf, "<xml_signature>")) {
|
|
|
|
retval = dup_element_contents(in, "</xml_signature>", &xml_signature);
|
2002-07-05 05:33:40 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strcatdup(signed_xml, buf);
|
2002-09-22 23:27:14 +00:00
|
|
|
if (parse_str(buf, "<name>", name, sizeof(name))) continue;
|
2002-07-05 05:33:40 +00:00
|
|
|
if (parse_double(buf, "<max_nbytes>", max_nbytes)) continue;
|
2003-02-14 18:35:12 +00:00
|
|
|
if (match_tag(buf, "<generated_locally/>")) continue;
|
|
|
|
if (match_tag(buf, "<upload_when_present/>")) continue;
|
|
|
|
if (match_tag(buf, "<url>")) continue;
|
2003-06-14 20:15:50 +00:00
|
|
|
log_messages.printf(SchedMessages::NORMAL, "FILE_INFO::parse: unrecognized: %s \n", buf);
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-06-14 20:15:54 +00:00
|
|
|
inline static const char* get_remote_addr()
|
|
|
|
{
|
|
|
|
return getenv("REMOTE_ADDR");
|
|
|
|
}
|
|
|
|
|
2003-06-14 20:15:50 +00:00
|
|
|
int return_error(bool transient, const char* message, ...) {
|
2002-12-02 04:29:40 +00:00
|
|
|
printf(
|
|
|
|
"Content-type: text/plain\n\n"
|
|
|
|
"<data_server_reply>\n"
|
2003-05-08 17:13:57 +00:00
|
|
|
" <status>%d</status>\n"
|
2002-12-02 04:29:40 +00:00
|
|
|
" <message>%s</message>\n"
|
|
|
|
"</data_server_reply>\n",
|
2003-05-08 17:13:57 +00:00
|
|
|
transient?1:-1,
|
2002-12-02 04:29:40 +00:00
|
|
|
message
|
2003-06-14 20:15:53 +00:00
|
|
|
);
|
2003-06-14 20:15:50 +00:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, message);
|
|
|
|
char buf[1024];
|
|
|
|
vsprintf(buf, message, va);
|
|
|
|
va_end(va);
|
|
|
|
|
2003-06-14 20:15:53 +00:00
|
|
|
log_messages.printf(
|
2003-06-14 20:15:54 +00:00
|
|
|
SchedMessages::NORMAL, "Returning error to client %s: %s (%s)\n",
|
|
|
|
get_remote_addr(), buf,
|
2003-06-14 20:15:53 +00:00
|
|
|
transient?"transient":"permanent"
|
|
|
|
);
|
2002-12-02 04:29:40 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int return_success(char* text) {
|
|
|
|
printf(
|
|
|
|
"Content-type: text/plain\n\n"
|
|
|
|
"<data_server_reply>\n"
|
|
|
|
" <status>0</status>\n"
|
|
|
|
);
|
2003-05-08 17:13:57 +00:00
|
|
|
if (text) {
|
|
|
|
printf(" %s\n", text);
|
|
|
|
}
|
|
|
|
printf("</data_server_reply>\n");
|
2002-07-05 05:33:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BLOCK_SIZE 16382
|
|
|
|
|
|
|
|
// read from socket, write to file
|
|
|
|
//
|
2002-07-07 20:39:24 +00:00
|
|
|
int copy_socket_to_file(FILE* in, char* path, double offset, double nbytes) {
|
2002-07-05 05:33:40 +00:00
|
|
|
unsigned char buf[BLOCK_SIZE];
|
2002-12-02 04:29:40 +00:00
|
|
|
char buf2[256];
|
2002-07-05 05:33:40 +00:00
|
|
|
FILE* out;
|
|
|
|
int retval, n, m;
|
|
|
|
double bytes_left;
|
2002-11-09 20:26:50 +00:00
|
|
|
|
2002-10-18 00:01:56 +00:00
|
|
|
out = fopen(path, "ab");
|
2002-07-05 05:33:40 +00:00
|
|
|
if (!out) {
|
2003-05-08 17:13:57 +00:00
|
|
|
return return_error(ERR_TRANSIENT, "can't open file");
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use a 64-bit variant
|
2003-06-14 20:15:47 +00:00
|
|
|
|
2002-10-18 00:01:56 +00:00
|
|
|
retval = fseek(out, (long)offset, SEEK_CUR);
|
2003-06-14 20:15:47 +00:00
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
if (retval) {
|
|
|
|
fclose(out);
|
2003-05-08 17:13:57 +00:00
|
|
|
return return_error(ERR_TRANSIENT, "can't fseek file");
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
bytes_left = nbytes - offset;
|
2002-10-09 04:56:41 +00:00
|
|
|
if (bytes_left == 0) {
|
2002-12-02 04:29:40 +00:00
|
|
|
fclose(out);
|
2003-06-14 20:15:50 +00:00
|
|
|
log_messages.printf(SchedMessages::DEBUG, "offset == nbytes: %f\n", nbytes);
|
2003-05-08 17:13:57 +00:00
|
|
|
return return_success(0);
|
2002-10-09 04:56:41 +00:00
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
while (1) {
|
|
|
|
m = BLOCK_SIZE;
|
|
|
|
if (m > bytes_left) m = (int)bytes_left;
|
|
|
|
n = fread(buf, 1, m, in);
|
|
|
|
if (n <= 0) {
|
2002-12-02 04:29:40 +00:00
|
|
|
fclose(out);
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf2, "fread: asked for %d, got %d", m, n);
|
2003-05-08 17:13:57 +00:00
|
|
|
return return_error(ERR_TRANSIENT, buf2);
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
m = fwrite(buf, 1, n, out);
|
|
|
|
if (m != n) {
|
2003-06-14 20:15:45 +00:00
|
|
|
fclose(out);
|
2003-05-08 17:13:57 +00:00
|
|
|
return return_error(ERR_TRANSIENT, "can't fwrite file");
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
bytes_left -= n;
|
|
|
|
if (bytes_left == 0) break;
|
|
|
|
}
|
|
|
|
fclose(out);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-14 20:15:46 +00:00
|
|
|
// read from socket, discard data
|
|
|
|
//
|
|
|
|
void copy_socket_to_null(FILE* in) {
|
|
|
|
unsigned char buf[BLOCK_SIZE];
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int n = fread(buf, 1, BLOCK_SIZE, in);
|
|
|
|
if (n <= 0) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-02 04:29:40 +00:00
|
|
|
int handle_file_upload(FILE* in, R_RSA_PUBLIC_KEY& key) {
|
|
|
|
char buf[256], path[256];
|
2002-07-05 05:33:40 +00:00
|
|
|
FILE_INFO file_info;
|
|
|
|
int retval;
|
2002-12-02 04:29:40 +00:00
|
|
|
double nbytes=-1, offset=0;
|
2002-07-05 05:33:40 +00:00
|
|
|
bool is_valid;
|
2002-11-09 20:26:50 +00:00
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
while (fgets(buf, 256, in)) {
|
2003-06-14 20:15:50 +00:00
|
|
|
log_messages.printf_multiline(SchedMessages::DEBUG, buf, "handle_file_upload: ");
|
2002-07-05 05:33:40 +00:00
|
|
|
if (match_tag(buf, "<file_info>")) {
|
|
|
|
retval = file_info.parse(in);
|
2002-07-18 21:29:50 +00:00
|
|
|
if (retval) {
|
2003-05-08 17:13:57 +00:00
|
|
|
return return_error(ERR_PERMANENT, "FILE_INFO::parse");
|
2002-08-28 21:50:51 +00:00
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
retval = verify_string(
|
2002-07-07 20:39:24 +00:00
|
|
|
file_info.signed_xml, file_info.xml_signature, key, is_valid
|
2002-07-05 05:33:40 +00:00
|
|
|
);
|
|
|
|
if (retval || !is_valid) {
|
2003-06-14 20:15:50 +00:00
|
|
|
log_messages.printf_multiline(SchedMessages::NORMAL, file_info.signed_xml, "signed xml: ");
|
|
|
|
log_messages.printf_multiline(SchedMessages::NORMAL, file_info.xml_signature, "signature: ");
|
2003-05-08 17:13:57 +00:00
|
|
|
return return_error(ERR_PERMANENT, "invalid signature");
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (parse_double(buf, "<offset>", offset)) continue;
|
|
|
|
else if (parse_double(buf, "<nbytes>", nbytes)) continue;
|
|
|
|
else if (match_tag(buf, "<data>")) {
|
2002-10-09 18:48:26 +00:00
|
|
|
if (nbytes < 0) {
|
2003-05-08 17:13:57 +00:00
|
|
|
return return_error(ERR_PERMANENT, "nbytes missing or negative");
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// enforce limits in signed XML
|
2002-12-02 04:29:40 +00:00
|
|
|
//
|
2002-07-05 05:33:40 +00:00
|
|
|
if (nbytes > file_info.max_nbytes) {
|
|
|
|
sprintf(buf,
|
2003-05-16 19:22:57 +00:00
|
|
|
"file size (%d KB) exceeds limit (%d KB)",
|
|
|
|
(int)(nbytes/1024), (int)(file_info.max_nbytes/1024)
|
2002-07-05 05:33:40 +00:00
|
|
|
);
|
2003-06-14 20:15:46 +00:00
|
|
|
copy_socket_to_null(in);
|
2003-05-08 17:13:57 +00:00
|
|
|
return return_error(ERR_PERMANENT, buf);
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
|
2002-09-22 23:47:13 +00:00
|
|
|
// make sure filename is legit
|
2002-12-02 04:29:40 +00:00
|
|
|
//
|
2002-09-22 23:47:13 +00:00
|
|
|
if (strstr(file_info.name, "..")) {
|
2003-06-14 20:15:50 +00:00
|
|
|
return return_error(ERR_PERMANENT,
|
2003-06-14 20:15:53 +00:00
|
|
|
"file_upload_handler: .. found in filename: %s",
|
|
|
|
file_info.name
|
|
|
|
);
|
2002-09-22 23:47:13 +00:00
|
|
|
}
|
|
|
|
|
2002-10-03 18:33:46 +00:00
|
|
|
sprintf(path, "%s/%s", config.upload_dir, file_info.name);
|
2003-06-14 20:15:53 +00:00
|
|
|
log_messages.printf(
|
|
|
|
SchedMessages::NORMAL,
|
2003-06-14 20:15:54 +00:00
|
|
|
"Handling upload of %s from %s [offset=%.0f, nbytes=%.0f]\n",
|
|
|
|
file_info.name,
|
|
|
|
get_remote_addr(),
|
|
|
|
offset, nbytes
|
2003-06-14 20:15:53 +00:00
|
|
|
);
|
2002-07-07 20:39:24 +00:00
|
|
|
retval = copy_socket_to_file(in, path, offset, nbytes);
|
2002-12-02 04:29:40 +00:00
|
|
|
if (!retval) {
|
2003-05-08 17:13:57 +00:00
|
|
|
return_success(0);
|
2002-08-28 21:50:51 +00:00
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-02 04:29:40 +00:00
|
|
|
int handle_get_file_size(char* file_name) {
|
|
|
|
struct stat sbuf;
|
|
|
|
char path[256], buf[256];
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
// TODO: check to ensure path doesn't point somewhere bad
|
|
|
|
// Use 64-bit variant
|
|
|
|
//
|
|
|
|
sprintf(path, "%s/%s", config.upload_dir, file_name );
|
|
|
|
retval = stat( path, &sbuf );
|
|
|
|
if (retval && errno != ENOENT) {
|
2003-06-14 20:15:51 +00:00
|
|
|
log_messages.printf(SchedMessages::DEBUG, "handle_get_file_size(): [%s] returning error\n", file_name);
|
2003-05-08 17:13:57 +00:00
|
|
|
return return_error(ERR_TRANSIENT, "cannot open file" );
|
2002-12-02 04:29:40 +00:00
|
|
|
} else if (retval) {
|
2003-06-14 20:15:51 +00:00
|
|
|
log_messages.printf(SchedMessages::DEBUG, "handle_get_file_size(): [%s] returning zero\n", file_name);
|
2002-12-02 04:29:40 +00:00
|
|
|
return return_success("<file_size>0</file_size>");
|
|
|
|
} else {
|
2003-06-14 20:15:53 +00:00
|
|
|
log_messages.printf(
|
|
|
|
SchedMessages::DEBUG, "handle_get_file_size(): [%s] returning %d\n",
|
|
|
|
file_name, (int)sbuf.st_size
|
|
|
|
);
|
2003-05-08 17:13:57 +00:00
|
|
|
sprintf(buf, "<file_size>%d</file_size>", (int)sbuf.st_size);
|
2002-12-02 04:29:40 +00:00
|
|
|
return return_success(buf);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int handle_request(FILE* in, R_RSA_PUBLIC_KEY& key) {
|
|
|
|
char buf[256];
|
|
|
|
char file_name[256];
|
|
|
|
int major;
|
|
|
|
bool got_version = false;
|
|
|
|
|
|
|
|
while (fgets(buf, 256, in)) {
|
2003-06-14 20:15:50 +00:00
|
|
|
log_messages.printf_multiline(SchedMessages::DEBUG, buf, "handle_request: ");
|
2002-12-02 04:29:40 +00:00
|
|
|
if (parse_int(buf, "<core_client_major_version>", major)) {
|
|
|
|
if (major != MAJOR_VERSION) {
|
2003-06-14 20:15:50 +00:00
|
|
|
return return_error(ERR_PERMANENT,
|
2003-06-14 20:15:53 +00:00
|
|
|
"Core client has major version %d; "
|
|
|
|
"expected %d.",
|
|
|
|
major, MAJOR_VERSION
|
|
|
|
);
|
2002-12-02 04:29:40 +00:00
|
|
|
} else {
|
|
|
|
got_version = true;
|
|
|
|
}
|
|
|
|
} else if (match_tag(buf, "<file_upload>")) {
|
|
|
|
if (!got_version) {
|
2003-05-08 17:13:57 +00:00
|
|
|
return return_error(ERR_PERMANENT, "Missing version");
|
2002-12-02 04:29:40 +00:00
|
|
|
} else {
|
|
|
|
return handle_file_upload(in, key);
|
|
|
|
}
|
|
|
|
} else if (parse_str(buf, "<get_file_size>", file_name, sizeof(file_name))) {
|
|
|
|
if (!got_version) {
|
2003-05-08 17:13:57 +00:00
|
|
|
return return_error(ERR_PERMANENT, "Missing version");
|
2002-12-02 04:29:40 +00:00
|
|
|
} else {
|
|
|
|
return handle_get_file_size(file_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
int get_key(R_RSA_PUBLIC_KEY& key) {
|
|
|
|
FILE* f;
|
|
|
|
int retval;
|
2002-07-12 18:27:29 +00:00
|
|
|
char buf[256];
|
2002-10-03 18:33:46 +00:00
|
|
|
sprintf(buf, "%s/upload_public", config.key_dir);
|
2002-07-12 18:27:29 +00:00
|
|
|
f = fopen(buf, "r");
|
2002-07-05 05:33:40 +00:00
|
|
|
if (!f) return -1;
|
|
|
|
retval = scan_key_hex(f, (KEY*)&key, sizeof(key));
|
|
|
|
fclose(f);
|
|
|
|
if (retval) return retval;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
int retval;
|
|
|
|
R_RSA_PUBLIC_KEY key;
|
|
|
|
|
2003-04-03 18:35:40 +00:00
|
|
|
if (!freopen(STDERR_FILENAME, "a", stderr)) {
|
|
|
|
fprintf(stderr, "Can't redirect stderr\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-06-14 20:15:50 +00:00
|
|
|
log_messages.set_debug_level(DEBUG_LEVEL);
|
2003-06-14 20:15:44 +00:00
|
|
|
|
2002-10-03 18:33:46 +00:00
|
|
|
retval = config.parse_file();
|
|
|
|
if (retval) {
|
2003-05-08 17:13:57 +00:00
|
|
|
return_error(ERR_TRANSIENT, "can't read config file");
|
2002-12-02 04:29:40 +00:00
|
|
|
exit(1);
|
2002-10-03 18:33:46 +00:00
|
|
|
}
|
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
retval = get_key(key);
|
|
|
|
if (retval) {
|
2003-05-08 17:13:57 +00:00
|
|
|
return_error(ERR_TRANSIENT, "can't read key file");
|
2002-12-02 04:29:40 +00:00
|
|
|
exit(1);
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
2003-06-14 20:15:47 +00:00
|
|
|
|
2002-12-02 04:29:40 +00:00
|
|
|
handle_request(stdin, key);
|
2002-07-05 05:33:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|