2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2003-06-14 20:15:51 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
2003-06-14 20:15:51 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2002-09-26 18:11:06 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
2007-10-09 11:35:47 +00:00
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-01-20 23:22:22 +00:00
|
|
|
|
2002-09-20 21:17:47 +00:00
|
|
|
// The BOINC file upload handler.
|
2007-03-05 18:34:30 +00:00
|
|
|
// See doc/upload.php for protocol spec.
|
2003-12-17 19:14:17 +00:00
|
|
|
//
|
2002-07-05 05:33:40 +00:00
|
|
|
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <ctime>
|
2002-08-07 22:52:10 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cerrno>
|
2003-12-17 19:11:47 +00:00
|
|
|
#include <unistd.h>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <csignal>
|
2005-04-19 14:30:07 +00:00
|
|
|
#include <fcntl.h>
|
2002-07-05 05:33:40 +00:00
|
|
|
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "crypt.h"
|
2002-07-05 05:33:40 +00:00
|
|
|
#include "parse.h"
|
2003-03-06 17:42:49 +00:00
|
|
|
#include "util.h"
|
2004-10-06 20:45:21 +00:00
|
|
|
#include "error_numbers.h"
|
2006-05-31 20:39:32 +00:00
|
|
|
#include "filesys.h"
|
2004-04-08 08:15:23 +00:00
|
|
|
|
2003-08-15 00:45:25 +00:00
|
|
|
#include "sched_config.h"
|
2003-05-16 19:22:57 +00:00
|
|
|
#include "sched_util.h"
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "sched_msgs.h"
|
2002-07-05 05:33:40 +00:00
|
|
|
|
2005-12-14 22:48:05 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
#include "fcgi_stdio.h"
|
|
|
|
#endif
|
|
|
|
|
2003-09-02 21:16:55 +00:00
|
|
|
SCHED_CONFIG config;
|
2002-10-03 18:33:46 +00:00
|
|
|
|
2003-05-08 17:13:57 +00:00
|
|
|
#define ERR_TRANSIENT true
|
|
|
|
#define ERR_PERMANENT false
|
|
|
|
|
2005-04-11 09:00:50 +00:00
|
|
|
char this_filename[256];
|
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
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)) {
|
2005-09-26 23:28:48 +00:00
|
|
|
//log_messages.printf(SCHED_MSG_LOG::MSG_DEBUG, buf, "FILE_INFO::parse: ");
|
2002-07-05 05:33:40 +00:00
|
|
|
if (match_tag(buf, "</file_info>")) return 0;
|
2007-06-14 18:02:00 +00:00
|
|
|
if (match_tag(buf, "<xml_signature>")) {
|
2002-07-07 20:39:24 +00:00
|
|
|
retval = dup_element_contents(in, "</xml_signature>", &xml_signature);
|
2002-07-05 05:33:40 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
continue;
|
|
|
|
}
|
2006-03-02 22:51:41 +00:00
|
|
|
retval = strcatdup(signed_xml, buf);
|
|
|
|
if (retval) return retval;
|
2005-04-11 09:00:50 +00:00
|
|
|
if (parse_str(buf, "<name>", name, sizeof(name))) {
|
|
|
|
strcpy(this_filename, 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;
|
2006-06-21 17:34:55 +00:00
|
|
|
if (match_tag(buf, "<gzip_when_done")) continue;
|
2007-06-14 18:02:00 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_NORMAL,
|
|
|
|
"FILE_INFO::parse: unrecognized: %s \n", buf
|
|
|
|
);
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
2004-10-06 20:45:21 +00:00
|
|
|
return ERR_XML_PARSE;
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
|
2004-01-22 01:35:09 +00:00
|
|
|
inline static const char* get_remote_addr() {
|
|
|
|
char* p = getenv("REMOTE_ADDR");
|
|
|
|
if (p) return p;
|
|
|
|
return "Unknown remote address";
|
2003-06-14 20:15:54 +00:00
|
|
|
}
|
|
|
|
|
2003-06-14 20:15:50 +00:00
|
|
|
int return_error(bool transient, const char* message, ...) {
|
2004-10-07 17:39:13 +00:00
|
|
|
va_list va;
|
|
|
|
va_start(va, message);
|
|
|
|
char buf[10240];
|
|
|
|
|
|
|
|
vsprintf(buf, message, va);
|
|
|
|
va_end(va);
|
|
|
|
|
2005-12-14 22:48:05 +00:00
|
|
|
fprintf(stdout,
|
2002-12-02 04:29:40 +00:00
|
|
|
"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,
|
2004-10-07 17:39:13 +00:00
|
|
|
buf
|
2003-06-14 20:15:53 +00:00
|
|
|
);
|
2003-06-14 20:15:50 +00:00
|
|
|
|
2003-06-14 20:15:53 +00:00
|
|
|
log_messages.printf(
|
2005-09-26 23:28:48 +00:00
|
|
|
SCHED_MSG_LOG::MSG_NORMAL, "Returning error to client %s: %s (%s)\n",
|
2003-06-14 20:15:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-02-16 23:17:43 +00:00
|
|
|
int return_success(const char* text) {
|
2005-12-14 22:48:05 +00:00
|
|
|
fprintf(stdout,
|
2002-12-02 04:29:40 +00:00
|
|
|
"Content-type: text/plain\n\n"
|
|
|
|
"<data_server_reply>\n"
|
|
|
|
" <status>0</status>\n"
|
|
|
|
);
|
2003-05-08 17:13:57 +00:00
|
|
|
if (text) {
|
2005-12-14 22:48:05 +00:00
|
|
|
fprintf(stdout, " %s\n", text);
|
2003-05-08 17:13:57 +00:00
|
|
|
}
|
2005-12-14 22:48:05 +00:00
|
|
|
fprintf(stdout, "</data_server_reply>\n");
|
2002-07-05 05:33:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BLOCK_SIZE 16382
|
2005-04-19 14:30:07 +00:00
|
|
|
double bytes_left=-1;
|
2002-07-05 05:33:40 +00:00
|
|
|
|
|
|
|
// read from socket, write to file
|
2004-01-22 01:35:09 +00:00
|
|
|
// ALWAYS returns an HTML reply
|
2002-07-05 05:33:40 +00:00
|
|
|
//
|
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];
|
2005-04-15 19:44:26 +00:00
|
|
|
struct stat sbuf;
|
2005-04-19 14:30:07 +00:00
|
|
|
int pid;
|
2002-11-09 20:26:50 +00:00
|
|
|
|
2006-01-11 22:08:39 +00:00
|
|
|
// open file. Use raw IO not buffered IO so that we can use reliable
|
|
|
|
// posix file locking.
|
|
|
|
// Advisory file locking is not guaranteed reliable when
|
2005-04-19 14:30:07 +00:00
|
|
|
// used with stream buffered IO.
|
2006-01-11 22:08:39 +00:00
|
|
|
//
|
2007-06-27 20:09:18 +00:00
|
|
|
int fd = open(path,
|
|
|
|
O_WRONLY|O_CREAT|O_APPEND,
|
|
|
|
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
|
|
|
|
);
|
2005-04-12 14:34:07 +00:00
|
|
|
if (fd<0) {
|
2006-01-11 22:08:39 +00:00
|
|
|
return return_error(ERR_TRANSIENT,
|
|
|
|
"can't open file %s: %s\n", path, strerror(errno)
|
|
|
|
);
|
2005-04-12 14:34:07 +00:00
|
|
|
}
|
|
|
|
|
2006-01-11 22:08:39 +00:00
|
|
|
// Put an advisory lock on the file.
|
|
|
|
// This will prevent OTHER instances of file_upload_handler
|
2005-04-12 14:34:07 +00:00
|
|
|
// from being able to write to the file.
|
2006-01-11 22:08:39 +00:00
|
|
|
//
|
2005-04-19 14:30:07 +00:00
|
|
|
if ((pid=mylockf(fd))) {
|
|
|
|
close(fd);
|
2006-01-11 22:08:39 +00:00
|
|
|
return return_error(ERR_TRANSIENT,
|
|
|
|
"can't lock file %s: %s locked by PID=%d\n",
|
|
|
|
path, strerror(errno), pid
|
|
|
|
);
|
2005-04-12 14:34:07 +00:00
|
|
|
}
|
|
|
|
|
2005-04-15 19:44:26 +00:00
|
|
|
// check that file length corresponds to offset
|
2006-01-11 22:08:39 +00:00
|
|
|
// TODO: use a 64-bit variant
|
|
|
|
//
|
2005-04-19 14:30:07 +00:00
|
|
|
if (stat(path, &sbuf)) {
|
|
|
|
close(fd);
|
2006-01-11 22:08:39 +00:00
|
|
|
return return_error(ERR_TRANSIENT,
|
|
|
|
"can't stat file %s: %s\n", path, strerror(errno)
|
|
|
|
);
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
2006-01-11 22:08:39 +00:00
|
|
|
if (sbuf.st_size < offset) {
|
2005-04-19 14:30:07 +00:00
|
|
|
close(fd);
|
2006-01-11 22:08:39 +00:00
|
|
|
return return_error(ERR_TRANSIENT,
|
|
|
|
"length of file %s %d bytes < offset %.0f bytes",
|
|
|
|
path, (int)sbuf.st_size, offset
|
|
|
|
);
|
2005-04-15 19:44:26 +00:00
|
|
|
}
|
2006-01-12 15:36:46 +00:00
|
|
|
if (sbuf.st_size > offset) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
|
|
|
"file %s length on disk %d bytes; host upload starting at %.0f bytes.\n",
|
|
|
|
this_filename, (int)sbuf.st_size, offset
|
|
|
|
);
|
|
|
|
}
|
2005-04-15 19:44:26 +00:00
|
|
|
|
2005-04-19 14:30:07 +00:00
|
|
|
// caller guarantees that nbytes > offset
|
2006-01-11 22:08:39 +00:00
|
|
|
//
|
2002-07-05 05:33:40 +00:00
|
|
|
bytes_left = nbytes - offset;
|
2003-08-15 19:23:44 +00:00
|
|
|
|
2005-04-19 16:35:59 +00:00
|
|
|
while (bytes_left > 0) {
|
2005-04-19 14:30:07 +00:00
|
|
|
|
2007-01-26 18:38:17 +00:00
|
|
|
int n, m, to_write;
|
2005-04-19 14:30:07 +00:00
|
|
|
|
|
|
|
m = bytes_left<(double)BLOCK_SIZE ? (int)bytes_left : BLOCK_SIZE;
|
|
|
|
|
2005-04-19 16:35:59 +00:00
|
|
|
// try to get m bytes from socket (n>=0 is number actually returned)
|
2006-01-11 22:08:39 +00:00
|
|
|
//
|
2002-07-05 05:33:40 +00:00
|
|
|
n = fread(buf, 1, m, in);
|
2005-04-19 14:30:07 +00:00
|
|
|
|
|
|
|
// try to write n bytes to file
|
2006-01-11 22:08:39 +00:00
|
|
|
//
|
2005-04-19 14:30:07 +00:00
|
|
|
to_write=n;
|
|
|
|
while (to_write > 0) {
|
2007-03-05 18:34:30 +00:00
|
|
|
ssize_t ret = write(fd, buf+n-to_write, to_write);
|
2005-04-19 14:30:07 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
close(fd);
|
2007-06-20 22:34:06 +00:00
|
|
|
const char* errmsg;
|
2007-03-05 18:34:30 +00:00
|
|
|
if (errno == ENOSPC) {
|
|
|
|
errmsg = "No space left on server";
|
|
|
|
} else {
|
|
|
|
errmsg = strerror(errno);
|
|
|
|
}
|
2006-01-11 22:08:39 +00:00
|
|
|
return return_error(ERR_TRANSIENT,
|
2007-03-05 18:34:30 +00:00
|
|
|
"can't write file %s: %s\n", path, errmsg
|
2006-01-11 22:08:39 +00:00
|
|
|
);
|
2005-04-19 14:30:07 +00:00
|
|
|
}
|
|
|
|
to_write -= ret;
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
2005-04-19 14:30:07 +00:00
|
|
|
|
|
|
|
// check that we got all bytes from socket that were requested
|
2007-01-26 18:38:17 +00:00
|
|
|
// Note: fread() reads less than requested only if there's
|
|
|
|
// an error or EOF (see the man page)
|
2006-01-11 22:08:39 +00:00
|
|
|
//
|
2005-04-19 14:30:07 +00:00
|
|
|
if (n != m) {
|
|
|
|
close(fd);
|
2007-01-26 18:38:17 +00:00
|
|
|
if (feof(in)) {
|
|
|
|
return return_error(ERR_TRANSIENT,
|
|
|
|
"EOF on socket read : asked for %d, got %d\n",
|
|
|
|
m, n
|
|
|
|
);
|
|
|
|
} else if (ferror(in)) {
|
|
|
|
return return_error(ERR_TRANSIENT,
|
|
|
|
"error %d (%s) on socket read: asked for %d, got %d\n",
|
|
|
|
ferror(in), strerror(ferror(in)), m, n
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return return_error(ERR_TRANSIENT,
|
|
|
|
"incomplete socket read: asked for %d, got %d\n",
|
|
|
|
m, n
|
|
|
|
);
|
|
|
|
}
|
2005-04-19 14:30:07 +00:00
|
|
|
}
|
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
bytes_left -= n;
|
|
|
|
}
|
2005-04-19 14:30:07 +00:00
|
|
|
close(fd);
|
2004-01-22 01:35:09 +00:00
|
|
|
return return_success(0);
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-22 01:35:09 +00:00
|
|
|
// ALWAYS generates an HTML reply
|
|
|
|
//
|
2002-12-02 04:29:40 +00:00
|
|
|
int handle_file_upload(FILE* in, R_RSA_PUBLIC_KEY& key) {
|
2007-06-14 18:02:00 +00:00
|
|
|
char buf[256], path[512], temp[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)) {
|
2004-10-06 20:45:21 +00:00
|
|
|
#if 0
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_NORMAL,
|
2004-10-06 20:45:21 +00:00
|
|
|
"got:%s\n", buf
|
|
|
|
);
|
|
|
|
#endif
|
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
|
|
|
}
|
2004-10-06 20:45:21 +00:00
|
|
|
#if 0
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_NORMAL,
|
2004-10-06 20:45:21 +00:00
|
|
|
"file info:\n%s\n", file_info.signed_xml
|
|
|
|
);
|
|
|
|
#endif
|
2004-08-04 13:11:21 +00:00
|
|
|
if (!config.ignore_upload_certificates) {
|
|
|
|
if (!file_info.signed_xml || !file_info.xml_signature) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
2004-08-04 13:11:21 +00:00
|
|
|
"file info is missing signature\n"
|
|
|
|
);
|
2004-06-07 03:34:07 +00:00
|
|
|
return return_error(ERR_PERMANENT, "invalid signature");
|
2004-08-04 13:11:21 +00:00
|
|
|
} else {
|
|
|
|
retval = verify_string(
|
|
|
|
file_info.signed_xml, file_info.xml_signature, key, is_valid
|
|
|
|
);
|
|
|
|
if (retval || !is_valid) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
2004-08-04 13:11:21 +00:00
|
|
|
"verify_string() [%s] [%s] retval %d, is_valid = %d\n",
|
|
|
|
file_info.signed_xml, file_info.xml_signature,
|
|
|
|
retval, is_valid
|
|
|
|
);
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_NORMAL,
|
2004-08-04 13:11:21 +00:00
|
|
|
"signed xml: %s", file_info.signed_xml
|
|
|
|
);
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_NORMAL,
|
2004-08-04 13:11:21 +00:00
|
|
|
"signature: %s", file_info.xml_signature
|
|
|
|
);
|
|
|
|
return return_error(ERR_PERMANENT, "invalid signature");
|
|
|
|
}
|
2004-06-07 03:34:07 +00:00
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
2004-08-04 13:11:21 +00:00
|
|
|
continue;
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
2007-06-14 18:02:00 +00:00
|
|
|
if (parse_double(buf, "<offset>", offset)) continue;
|
|
|
|
if (parse_double(buf, "<nbytes>", nbytes)) continue;
|
|
|
|
if (parse_str(buf, "<md5_cksum>", temp, sizeof(temp))) continue;
|
|
|
|
if (match_tag(buf, "<data>")) {
|
2004-10-29 04:42:37 +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
|
|
|
//
|
2004-10-06 20:45:21 +00:00
|
|
|
if (!config.ignore_upload_certificates) {
|
|
|
|
if (nbytes > file_info.max_nbytes) {
|
|
|
|
sprintf(buf,
|
|
|
|
"file size (%d KB) exceeds limit (%d KB)",
|
|
|
|
(int)(nbytes/1024), (int)(file_info.max_nbytes/1024)
|
|
|
|
);
|
|
|
|
copy_socket_to_null(in);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-10-06 20:45:21 +00:00
|
|
|
if (strlen(file_info.name) == 0) {
|
|
|
|
return return_error(ERR_PERMANENT,
|
|
|
|
"file_upload_handler: no filename; nbytes %f", nbytes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2004-08-06 11:42:41 +00:00
|
|
|
retval = dir_hier_path(
|
2005-09-23 21:09:00 +00:00
|
|
|
file_info.name, config.upload_dir, config.uldl_dir_fanout,
|
2004-08-06 11:42:41 +00:00
|
|
|
path, true
|
|
|
|
);
|
2006-01-11 22:08:39 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
|
|
|
"Failed to find/create directory for file '%s' in '%s'\n",
|
|
|
|
file_info.name, config.upload_dir
|
|
|
|
);
|
|
|
|
return return_error(ERR_TRANSIENT, "can't open file");
|
|
|
|
}
|
2003-06-14 20:15:53 +00:00
|
|
|
log_messages.printf(
|
2005-09-26 23:28:48 +00:00
|
|
|
SCHED_MSG_LOG::MSG_NORMAL,
|
2005-04-19 14:30:07 +00:00
|
|
|
"Starting upload of %s from %s [offset=%.0f, nbytes=%.0f]\n",
|
2003-06-14 20:15:54 +00:00
|
|
|
file_info.name,
|
|
|
|
get_remote_addr(),
|
|
|
|
offset, nbytes
|
2003-06-14 20:15:53 +00:00
|
|
|
);
|
2005-12-14 22:48:05 +00:00
|
|
|
#ifndef _USING_FCGI_
|
2004-06-16 20:18:57 +00:00
|
|
|
fflush(stderr);
|
2005-12-14 22:48:05 +00:00
|
|
|
#endif
|
2004-01-15 21:24:55 +00:00
|
|
|
if (offset >= nbytes) {
|
|
|
|
log_messages.printf(
|
2005-09-26 23:28:48 +00:00
|
|
|
SCHED_MSG_LOG::MSG_CRITICAL,
|
2004-01-15 21:24:55 +00:00
|
|
|
"ERROR: offset >= nbytes!!\n"
|
|
|
|
);
|
2004-01-22 01:35:09 +00:00
|
|
|
return return_success(0);
|
2002-08-28 21:50:51 +00:00
|
|
|
}
|
2004-06-16 20:18:57 +00:00
|
|
|
retval = copy_socket_to_file(in, path, offset, nbytes);
|
|
|
|
log_messages.printf(
|
2005-09-26 23:28:48 +00:00
|
|
|
SCHED_MSG_LOG::MSG_NORMAL,
|
2005-04-19 14:30:07 +00:00
|
|
|
"Ended upload of %s from %s; retval %d\n",
|
2004-06-16 20:18:57 +00:00
|
|
|
file_info.name,
|
|
|
|
get_remote_addr(),
|
|
|
|
retval
|
|
|
|
);
|
2005-12-14 22:48:05 +00:00
|
|
|
#ifndef _USING_FCGI_
|
2004-06-16 20:18:57 +00:00
|
|
|
fflush(stderr);
|
2005-12-14 22:48:05 +00:00
|
|
|
#endif
|
2004-06-16 20:18:57 +00:00
|
|
|
return retval;
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
2007-06-14 18:02:00 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
|
|
|
"unrecognized: %s", buf
|
|
|
|
);
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
2004-01-22 01:35:09 +00:00
|
|
|
return return_error(ERR_PERMANENT, "Missing <data> tag");
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
|
|
|
|
2007-09-12 08:25:22 +00:00
|
|
|
bool volume_full(char* path) {
|
|
|
|
double total, avail;
|
|
|
|
int retval = get_filesystem_info(total, avail, path);
|
|
|
|
if (retval) return false;
|
2007-09-27 15:08:40 +00:00
|
|
|
if (avail<1e6) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-12 08:25:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-01-22 01:35:09 +00:00
|
|
|
// always returns HTML reply
|
|
|
|
//
|
2002-12-02 04:29:40 +00:00
|
|
|
int handle_get_file_size(char* file_name) {
|
|
|
|
struct stat sbuf;
|
2005-10-28 06:26:58 +00:00
|
|
|
char path[512], buf[256];
|
2005-04-19 14:30:07 +00:00
|
|
|
int retval, pid, fd;
|
2002-12-02 04:29:40 +00:00
|
|
|
|
|
|
|
// TODO: check to ensure path doesn't point somewhere bad
|
|
|
|
// Use 64-bit variant
|
|
|
|
//
|
2006-01-11 22:08:39 +00:00
|
|
|
retval = dir_hier_path(
|
|
|
|
file_name, config.upload_dir, config.uldl_dir_fanout, path
|
|
|
|
);
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
|
|
|
"Failed to find/create directory for file '%s' in '%s'.\n",
|
|
|
|
file_name, config.upload_dir
|
|
|
|
);
|
|
|
|
return return_error(ERR_TRANSIENT, "can't open file");
|
2005-09-30 12:54:12 +00:00
|
|
|
}
|
|
|
|
|
2007-09-12 08:25:22 +00:00
|
|
|
// if the volume is full, report a transient error
|
|
|
|
// to prevent the client from starting a transfer
|
|
|
|
//
|
2007-09-27 15:08:40 +00:00
|
|
|
if (volume_full(config.upload_dir)) {
|
2007-10-04 17:30:28 +00:00
|
|
|
return return_error(ERR_TRANSIENT, "Server is out of disk space");
|
2007-09-12 08:25:22 +00:00
|
|
|
}
|
|
|
|
|
2006-01-11 22:08:39 +00:00
|
|
|
fd = open(path, O_WRONLY|O_APPEND);
|
2005-04-19 14:30:07 +00:00
|
|
|
|
|
|
|
if (fd<0 && ENOENT==errno) {
|
2005-04-14 18:01:54 +00:00
|
|
|
// file does not exist: return zero length
|
|
|
|
//
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_NORMAL,
|
|
|
|
"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>");
|
2005-04-19 14:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fd<0) {
|
|
|
|
// can't get file descriptor: try again later
|
2005-04-14 18:01:54 +00:00
|
|
|
//
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
2005-09-30 12:54:12 +00:00
|
|
|
"handle_get_file_size(): cannot open [%s] %s\n",
|
2005-09-26 23:28:48 +00:00
|
|
|
file_name, strerror(errno)
|
|
|
|
);
|
2005-04-19 14:30:07 +00:00
|
|
|
return return_error(ERR_TRANSIENT, "can't open file");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pid=mylockf(fd))) {
|
2005-04-14 18:01:54 +00:00
|
|
|
// file locked by another file_upload_handler: try again later
|
|
|
|
//
|
2005-04-19 14:30:07 +00:00
|
|
|
close(fd);
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
|
|
|
"handle_get_file_size(): [%s] returning error\n", file_name
|
|
|
|
);
|
2006-01-11 22:08:39 +00:00
|
|
|
return return_error(ERR_TRANSIENT,
|
|
|
|
"[%s] locked by file_upload_handler PID=%d", file_name, pid
|
|
|
|
);
|
2005-04-19 14:30:07 +00:00
|
|
|
}
|
|
|
|
// file exists, writable, not locked by anyone else, so return length.
|
|
|
|
//
|
|
|
|
retval = stat(path, &sbuf);
|
|
|
|
close(fd);
|
|
|
|
if (retval) {
|
|
|
|
// file DOES perhaps exist, but can't stat it: try again later
|
2005-04-14 18:01:54 +00:00
|
|
|
//
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
|
|
|
"handle_get_file_size(): [%s] returning error %s\n",
|
|
|
|
file_name, strerror(errno)
|
|
|
|
);
|
2005-04-19 14:30:07 +00:00
|
|
|
return return_error(ERR_TRANSIENT, "cannot stat file" );
|
2002-12-02 04:29:40 +00:00
|
|
|
}
|
2005-04-19 14:30:07 +00:00
|
|
|
|
|
|
|
log_messages.printf(
|
2005-09-26 23:28:48 +00:00
|
|
|
SCHED_MSG_LOG::MSG_NORMAL,
|
|
|
|
"handle_get_file_size(): [%s] returning %d\n",
|
2005-04-19 14:30:07 +00:00
|
|
|
file_name, (int)sbuf.st_size
|
|
|
|
);
|
|
|
|
sprintf(buf, "<file_size>%d</file_size>", (int)sbuf.st_size);
|
|
|
|
return return_success(buf);
|
2002-12-02 04:29:40 +00:00
|
|
|
}
|
|
|
|
|
2004-01-22 01:35:09 +00:00
|
|
|
// always generates an HTML reply
|
|
|
|
//
|
2002-12-02 04:29:40 +00:00
|
|
|
int handle_request(FILE* in, R_RSA_PUBLIC_KEY& key) {
|
|
|
|
char buf[256];
|
|
|
|
char file_name[256];
|
2005-10-04 18:30:49 +00:00
|
|
|
int major, minor, release, retval=0;
|
2005-12-13 08:04:57 +00:00
|
|
|
bool got_version = true;
|
2004-01-22 01:35:09 +00:00
|
|
|
bool did_something = false;
|
2002-12-02 04:29:40 +00:00
|
|
|
|
|
|
|
while (fgets(buf, 256, in)) {
|
2005-10-04 18:30:49 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_DEBUG, "handle_request: %s", buf);
|
2002-12-02 04:29:40 +00:00
|
|
|
if (parse_int(buf, "<core_client_major_version>", major)) {
|
2005-12-13 08:04:57 +00:00
|
|
|
continue;
|
2004-01-22 01:35:09 +00:00
|
|
|
} else if (parse_int(buf, "<core_client_minor_version>", minor)) {
|
|
|
|
continue;
|
2005-10-04 18:30:49 +00:00
|
|
|
} else if (parse_int(buf, "<core_client_release>", release)) {
|
|
|
|
continue;
|
2002-12-02 04:29:40 +00:00
|
|
|
} else if (match_tag(buf, "<file_upload>")) {
|
2005-12-14 22:48:05 +00:00
|
|
|
|
2002-12-02 04:29:40 +00:00
|
|
|
if (!got_version) {
|
2004-01-22 01:35:09 +00:00
|
|
|
retval = return_error(ERR_PERMANENT, "Missing version");
|
2002-12-02 04:29:40 +00:00
|
|
|
} else {
|
2004-01-22 01:35:09 +00:00
|
|
|
retval = handle_file_upload(in, key);
|
2002-12-02 04:29:40 +00:00
|
|
|
}
|
2004-01-22 01:35:09 +00:00
|
|
|
did_something = true;
|
|
|
|
break;
|
2002-12-02 04:29:40 +00:00
|
|
|
} else if (parse_str(buf, "<get_file_size>", file_name, sizeof(file_name))) {
|
2005-10-28 14:32:40 +00:00
|
|
|
if (strstr(file_name, "..")) {
|
2006-01-11 22:08:39 +00:00
|
|
|
return return_error(ERR_PERMANENT, "Bad filename");
|
|
|
|
}
|
2002-12-02 04:29:40 +00:00
|
|
|
if (!got_version) {
|
2004-01-22 01:35:09 +00:00
|
|
|
retval = return_error(ERR_PERMANENT, "Missing version");
|
2002-12-02 04:29:40 +00:00
|
|
|
} else {
|
2004-01-22 01:35:09 +00:00
|
|
|
retval = handle_get_file_size(file_name);
|
2002-12-02 04:29:40 +00:00
|
|
|
}
|
2004-01-22 01:35:09 +00:00
|
|
|
did_something = true;
|
|
|
|
break;
|
2005-04-14 18:01:54 +00:00
|
|
|
} else if (match_tag(buf, "<data_server_request>")) {
|
|
|
|
// DO NOTHING
|
2004-01-22 01:35:09 +00:00
|
|
|
} else {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_DEBUG, "handle_request: unrecognized %s\n", buf);
|
2002-12-02 04:29:40 +00:00
|
|
|
}
|
|
|
|
}
|
2004-01-22 01:35:09 +00:00
|
|
|
if (!did_something) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "handle_request: no command\n");
|
2004-01-22 01:35:09 +00:00
|
|
|
return return_error(ERR_PERMANENT, "no command");
|
|
|
|
}
|
|
|
|
|
2007-06-14 18:02:00 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_DEBUG, "elapsed time %f seconds\n", elapsed_wallclock_time());
|
2005-05-04 16:31:25 +00:00
|
|
|
|
2004-01-22 01:35:09 +00:00
|
|
|
return retval;
|
2002-12-02 04:29:40 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-04-11 09:00:50 +00:00
|
|
|
void boinc_catch_signal(int signal_num) {
|
2006-01-12 15:36:46 +00:00
|
|
|
char buffer[512]="";
|
2007-06-14 18:02:00 +00:00
|
|
|
if (this_filename[0]) {
|
|
|
|
sprintf(buffer, "FILE=%s (%.0f bytes left) ", this_filename, bytes_left);
|
|
|
|
}
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
2007-06-14 18:02:00 +00:00
|
|
|
"%sIP=%s caught signal %d [%s] elapsed time %f seconds\n",
|
2006-01-12 15:36:46 +00:00
|
|
|
buffer, get_remote_addr(),
|
2005-09-15 15:07:41 +00:00
|
|
|
signal_num, strsignal(signal_num), elapsed_wallclock_time()
|
2005-04-11 09:00:50 +00:00
|
|
|
);
|
2005-04-19 14:30:07 +00:00
|
|
|
|
2007-06-14 18:02:00 +00:00
|
|
|
// there is no point in trying to return an error.
|
|
|
|
// At this point Apache has broken the connection
|
|
|
|
// so a write to stdout will just generate a SIGPIPE
|
2005-04-19 14:30:07 +00:00
|
|
|
//
|
|
|
|
// return_error(ERR_TRANSIENT, "while downloading %s server caught signal %d", this_filename, signal_num);
|
2005-04-11 09:00:50 +00:00
|
|
|
exit(1);
|
2004-01-22 01:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void installer() {
|
|
|
|
signal(SIGHUP, boinc_catch_signal); // terminal line hangup
|
|
|
|
signal(SIGINT, boinc_catch_signal); // interrupt program
|
2005-04-11 09:00:50 +00:00
|
|
|
signal(SIGQUIT, boinc_catch_signal); // quit program
|
2004-01-22 01:35:09 +00:00
|
|
|
signal(SIGILL, boinc_catch_signal); // illegal instruction
|
2005-04-11 09:00:50 +00:00
|
|
|
signal(SIGTRAP, boinc_catch_signal); // illegal instruction
|
2004-01-22 01:35:09 +00:00
|
|
|
signal(SIGABRT, boinc_catch_signal); // abort(2) call
|
|
|
|
signal(SIGFPE, boinc_catch_signal); // bus error
|
2005-04-11 09:00:50 +00:00
|
|
|
signal(SIGKILL, boinc_catch_signal); // bus error
|
2004-01-22 01:35:09 +00:00
|
|
|
signal(SIGBUS, boinc_catch_signal); // bus error
|
|
|
|
signal(SIGSEGV, boinc_catch_signal); // segmentation violation
|
|
|
|
signal(SIGSYS, boinc_catch_signal); // system call given invalid argument
|
|
|
|
signal(SIGPIPE, boinc_catch_signal); // write on a pipe with no reader
|
2005-04-11 09:00:50 +00:00
|
|
|
signal(SIGTERM, boinc_catch_signal); // terminate process
|
2004-01-22 01:35:09 +00:00
|
|
|
}
|
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
int main() {
|
|
|
|
int retval;
|
|
|
|
R_RSA_PUBLIC_KEY key;
|
2003-12-15 02:31:29 +00:00
|
|
|
char log_path[256];
|
2007-06-20 22:34:06 +00:00
|
|
|
#ifdef _USING_FCGI_
|
2007-05-23 20:42:45 +00:00
|
|
|
unsigned int counter=0;
|
2007-06-20 22:34:06 +00:00
|
|
|
#endif
|
2005-09-15 15:07:41 +00:00
|
|
|
elapsed_wallclock_time();
|
2003-12-15 02:31:29 +00:00
|
|
|
|
2005-04-11 09:00:50 +00:00
|
|
|
installer();
|
|
|
|
|
2005-12-14 22:48:05 +00:00
|
|
|
#ifndef _USING_FCGI_
|
2004-12-06 22:41:19 +00:00
|
|
|
get_log_path(log_path, "file_upload_handler.log");
|
2003-12-15 02:31:29 +00:00
|
|
|
if (!freopen(log_path, "a", stderr)) {
|
|
|
|
fprintf(stderr, "Can't open log file\n");
|
2004-10-14 02:18:06 +00:00
|
|
|
return_error(ERR_TRANSIENT, "can't open log file");
|
2003-04-03 18:35:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2005-12-14 22:48:05 +00:00
|
|
|
#endif
|
2003-04-03 18:35:40 +00:00
|
|
|
|
2006-06-07 04:10:04 +00:00
|
|
|
retval = config.parse_file("..");
|
|
|
|
if (retval) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-04-18 05:37:31 +00:00
|
|
|
log_messages.pid = getpid();
|
2006-06-07 04:10:04 +00:00
|
|
|
log_messages.set_debug_level(config.fuh_debug_level);
|
2003-06-14 20:15:44 +00:00
|
|
|
|
2006-05-31 20:39:32 +00:00
|
|
|
if (boinc_file_exists("../stop_upload")) {
|
|
|
|
return_error(ERR_TRANSIENT, "Maintenance underway: file uploads are temporarily disabled.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-12-13 08:04:57 +00:00
|
|
|
if (!config.ignore_upload_certificates) {
|
|
|
|
retval = get_key(key);
|
|
|
|
if (retval) {
|
|
|
|
return_error(ERR_TRANSIENT, "can't read key file");
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
2003-06-14 20:15:47 +00:00
|
|
|
|
2005-12-14 22:48:05 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
while(FCGI_Accept() >= 0) {
|
2007-05-23 20:42:45 +00:00
|
|
|
counter++;
|
|
|
|
fprintf(stderr, "file_upload_handler (FCGI): counter: %d\n", counter);
|
2005-12-14 22:48:05 +00:00
|
|
|
#endif
|
2002-12-02 04:29:40 +00:00
|
|
|
handle_request(stdin, key);
|
2005-12-14 22:48:05 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
}
|
|
|
|
// when exiting, write headers back to apache so it won't complain
|
|
|
|
// about "incomplete headers"
|
|
|
|
fprintf(stdout,"Content-type: text/plain\n\n");
|
|
|
|
#endif
|
2002-07-05 05:33:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_470a0d4d11 = "$Id$";
|