2005-02-06 21:26:21 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "boinc_win.h"
|
|
|
|
#endif
|
2005-11-22 02:11:35 +00:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2005-11-22 02:11:35 +00:00
|
|
|
#endif
|
|
|
|
|
2005-02-06 21:26:21 +00:00
|
|
|
#include "parse.h"
|
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "client_msgs.h"
|
2005-06-17 06:31:32 +00:00
|
|
|
#include "util.h"
|
2005-02-06 21:26:21 +00:00
|
|
|
#include "file_names.h"
|
|
|
|
#include "filesys.h"
|
|
|
|
#include "client_state.h"
|
2005-08-05 18:41:46 +00:00
|
|
|
#include "gui_http.h"
|
2005-02-06 21:26:21 +00:00
|
|
|
|
|
|
|
#include "acct_mgr.h"
|
|
|
|
|
2005-12-09 07:12:56 +00:00
|
|
|
static const char *run_mode_name[] = {"", "always", "auto", "never"};
|
2005-06-21 08:22:18 +00:00
|
|
|
|
2005-11-26 00:12:44 +00:00
|
|
|
int ACCT_MGR_OP::do_rpc(
|
2005-12-14 01:44:11 +00:00
|
|
|
std::string url, std::string name, std::string password_hash
|
2005-11-26 00:12:44 +00:00
|
|
|
) {
|
2005-02-06 21:26:21 +00:00
|
|
|
int retval;
|
2005-12-09 22:29:21 +00:00
|
|
|
unsigned int i;
|
2005-02-06 21:26:21 +00:00
|
|
|
char buf[256];
|
|
|
|
|
2005-06-17 06:31:32 +00:00
|
|
|
strcpy(buf, url.c_str());
|
2005-06-28 23:56:56 +00:00
|
|
|
|
2005-09-15 00:16:55 +00:00
|
|
|
error_num = ERR_IN_PROGRESS;
|
|
|
|
|
2005-06-28 23:56:56 +00:00
|
|
|
if (!strlen(buf) && strlen(gstate.acct_mgr_info.acct_mgr_url)) {
|
2005-09-15 00:16:55 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "Removing account manager info");
|
2005-06-28 23:56:56 +00:00
|
|
|
gstate.acct_mgr_info.clear();
|
|
|
|
boinc_delete_file(ACCT_MGR_URL_FILENAME);
|
|
|
|
boinc_delete_file(ACCT_MGR_LOGIN_FILENAME);
|
2005-09-15 00:16:55 +00:00
|
|
|
error_num = 0;
|
2005-06-28 23:56:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-17 06:31:32 +00:00
|
|
|
canonicalize_master_url(buf);
|
|
|
|
if (!valid_master_url(buf)) {
|
2005-09-15 00:16:55 +00:00
|
|
|
error_num = ERR_INVALID_URL;
|
2005-06-17 06:31:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-06-28 23:56:56 +00:00
|
|
|
strcpy(ami.acct_mgr_url, url.c_str());
|
|
|
|
strcpy(ami.acct_mgr_name, "");
|
|
|
|
strcpy(ami.login_name, name.c_str());
|
2005-12-14 01:44:11 +00:00
|
|
|
strcpy(ami.password_hash, password_hash.c_str());
|
2005-06-21 08:22:18 +00:00
|
|
|
|
2005-12-09 22:29:21 +00:00
|
|
|
FILE* f = boinc_fopen(ACCT_MGR_REQUEST_FILENAME, "w");
|
|
|
|
if (!f) return ERR_FOPEN;
|
|
|
|
fprintf(f,
|
|
|
|
"<acct_mgr_request>\n"
|
|
|
|
" <name>%s</name>\n"
|
2005-12-14 01:44:11 +00:00
|
|
|
" <password_hash>%s</password_hash>\n"
|
2005-12-09 22:29:21 +00:00
|
|
|
" <host_cpid>%s</host_cpid>\n"
|
|
|
|
" <client_version>%d.%d.%d</client_version>\n"
|
|
|
|
" <run_mode>%s</run_mode>\n",
|
2005-12-14 01:44:11 +00:00
|
|
|
name.c_str(), password_hash.c_str(),
|
2005-11-28 00:40:54 +00:00
|
|
|
gstate.host_info.host_cpid,
|
|
|
|
gstate.core_client_major_version,
|
|
|
|
gstate.core_client_minor_version,
|
2005-12-08 19:21:56 +00:00
|
|
|
gstate.core_client_release,
|
|
|
|
run_mode_name[gstate.user_run_request]
|
2005-11-28 00:40:54 +00:00
|
|
|
);
|
2005-12-09 22:29:21 +00:00
|
|
|
for (i=0; i<gstate.projects.size(); i++) {
|
|
|
|
PROJECT* p = gstate.projects[i];
|
|
|
|
if (p->attached_via_acct_mgr) {
|
|
|
|
fprintf(f,
|
|
|
|
" <project>\n"
|
|
|
|
" <url>%s</url>\n"
|
|
|
|
" <project_name>%s</project_name>\n"
|
|
|
|
" <suspended_via_gui>%d</suspended_via_gui>\n"
|
2005-12-14 01:44:11 +00:00
|
|
|
" <account_key>%s</account_key>\n"
|
2005-12-09 22:29:21 +00:00
|
|
|
" </project>\n",
|
|
|
|
p->master_url,
|
|
|
|
p->project_name,
|
2005-12-14 01:44:11 +00:00
|
|
|
p->suspended_via_gui,
|
|
|
|
p->authenticator
|
2005-12-09 22:29:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(f, "</acct_mgr_request>\n");
|
|
|
|
fclose(f);
|
|
|
|
sprintf(buf, "%srpc.php", url.c_str());
|
|
|
|
retval = gstate.gui_http.do_rpc_post(
|
|
|
|
this, buf, ACCT_MGR_REQUEST_FILENAME, ACCT_MGR_REPLY_FILENAME
|
|
|
|
);
|
2005-06-17 06:31:32 +00:00
|
|
|
if (retval) {
|
2005-09-15 00:16:55 +00:00
|
|
|
error_num = retval;
|
2005-06-17 06:31:32 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2005-10-02 18:23:38 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "Contacting account manager at %s", url.c_str());
|
2005-03-11 00:33:25 +00:00
|
|
|
|
2005-02-06 21:26:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-05 18:41:46 +00:00
|
|
|
|
2005-02-06 21:26:21 +00:00
|
|
|
|
2005-11-26 00:12:44 +00:00
|
|
|
int AM_ACCOUNT::parse(MIOFILE& in) {
|
2005-03-10 00:43:22 +00:00
|
|
|
char buf[256];
|
2005-12-08 19:21:56 +00:00
|
|
|
detach = false;
|
2005-03-10 00:43:22 +00:00
|
|
|
while (in.fgets(buf, sizeof(buf))) {
|
|
|
|
if (match_tag(buf, "</account>")) {
|
|
|
|
if (url.length() && authenticator.length()) return 0;
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
|
|
|
if (parse_str(buf, "<url>", url)) continue;
|
|
|
|
if (parse_str(buf, "<authenticator>", authenticator)) continue;
|
2005-12-08 19:21:56 +00:00
|
|
|
if (parse_bool(buf, "detach", detach)) continue;
|
2005-03-10 00:43:22 +00:00
|
|
|
}
|
|
|
|
return ERR_XML_PARSE;
|
2005-02-06 21:26:21 +00:00
|
|
|
}
|
|
|
|
|
2005-08-05 22:00:19 +00:00
|
|
|
int ACCT_MGR_OP::parse(MIOFILE& in) {
|
2005-12-09 22:29:21 +00:00
|
|
|
char buf[256];
|
2005-02-06 21:26:21 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
accounts.clear();
|
2005-06-21 08:22:18 +00:00
|
|
|
error_str = "";
|
2005-11-26 00:12:44 +00:00
|
|
|
repeat_sec = 0;
|
2005-03-10 00:43:22 +00:00
|
|
|
while (in.fgets(buf, sizeof(buf))) {
|
2005-06-21 08:22:18 +00:00
|
|
|
if (match_tag(buf, "</acct_mgr_reply>")) return 0;
|
2005-06-28 23:56:56 +00:00
|
|
|
if (parse_str(buf, "<name>", ami.acct_mgr_name, 256)) continue;
|
2005-06-21 08:22:18 +00:00
|
|
|
if (parse_str(buf, "<error>", error_str)) continue;
|
2005-11-26 00:12:44 +00:00
|
|
|
if (parse_double(buf, "<repeat_sec>", repeat_sec)) continue;
|
2005-03-10 00:43:22 +00:00
|
|
|
if (match_tag(buf, "<account>")) {
|
2005-11-26 00:12:44 +00:00
|
|
|
AM_ACCOUNT account;
|
2005-03-10 00:43:22 +00:00
|
|
|
retval = account.parse(in);
|
|
|
|
if (!retval) accounts.push_back(account);
|
|
|
|
}
|
|
|
|
}
|
2005-02-06 21:26:21 +00:00
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
|
|
|
|
2005-08-05 22:00:19 +00:00
|
|
|
void ACCT_MGR_OP::handle_reply(int http_op_retval) {
|
2005-03-10 00:43:22 +00:00
|
|
|
unsigned int i;
|
2005-08-05 18:41:46 +00:00
|
|
|
int retval;
|
2005-11-26 00:12:44 +00:00
|
|
|
PROJECT* pp;
|
2005-08-05 18:41:46 +00:00
|
|
|
|
|
|
|
if (http_op_retval == 0) {
|
|
|
|
FILE* f = fopen(ACCT_MGR_REPLY_FILENAME, "r");
|
|
|
|
if (f) {
|
|
|
|
MIOFILE mf;
|
|
|
|
mf.init_file(f);
|
|
|
|
retval = parse(mf);
|
|
|
|
fclose(f);
|
|
|
|
} else {
|
|
|
|
retval = ERR_FOPEN;
|
|
|
|
}
|
2005-09-15 00:16:55 +00:00
|
|
|
} else if (error_str.size()) {
|
|
|
|
retval = ERR_XML_PARSE; // ?? what should we use here ??
|
2005-08-05 18:41:46 +00:00
|
|
|
} else {
|
|
|
|
retval = http_op_retval;
|
|
|
|
}
|
2005-09-15 00:16:55 +00:00
|
|
|
error_num = retval;
|
|
|
|
if (retval) return;
|
2005-06-21 08:22:18 +00:00
|
|
|
|
2005-06-28 23:56:56 +00:00
|
|
|
gstate.acct_mgr_info = ami;
|
2005-06-21 08:22:18 +00:00
|
|
|
gstate.acct_mgr_info.write_info();
|
|
|
|
|
2005-11-26 00:12:44 +00:00
|
|
|
// attach to new projects
|
|
|
|
//
|
2005-03-10 00:43:22 +00:00
|
|
|
for (i=0; i<accounts.size(); i++) {
|
2005-11-26 00:12:44 +00:00
|
|
|
AM_ACCOUNT& acct = accounts[i];
|
|
|
|
pp = gstate.lookup_project(acct.url.c_str());
|
2005-03-10 00:43:22 +00:00
|
|
|
if (pp) {
|
2005-12-08 19:21:56 +00:00
|
|
|
if (acct.detach) {
|
|
|
|
gstate.detach_project(pp);
|
2005-03-11 00:33:25 +00:00
|
|
|
} else {
|
2005-12-08 19:21:56 +00:00
|
|
|
if (strcmp(pp->authenticator, acct.authenticator.c_str())) {
|
|
|
|
msg_printf(pp, MSG_ERROR,
|
|
|
|
"Already attached under another account"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
msg_printf(pp, MSG_INFO, "Already attached");
|
2005-12-09 22:29:21 +00:00
|
|
|
pp->attached_via_acct_mgr = true;
|
2005-12-08 19:21:56 +00:00
|
|
|
}
|
2005-03-10 00:43:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-12-08 19:21:56 +00:00
|
|
|
if (!acct.detach) {
|
|
|
|
msg_printf(NULL, MSG_INFO, "Attaching to %s", acct.url.c_str());
|
2005-12-09 22:29:21 +00:00
|
|
|
gstate.add_project(acct.url.c_str(), acct.authenticator.c_str(), true);
|
2005-12-08 19:21:56 +00:00
|
|
|
}
|
2005-11-26 00:12:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (repeat_sec) {
|
|
|
|
gstate.acct_mgr_info.next_rpc_time = gstate.now + repeat_sec;
|
|
|
|
} else {
|
|
|
|
gstate.acct_mgr_info.next_rpc_time = gstate.now + 86400;
|
2005-03-11 00:33:25 +00:00
|
|
|
}
|
2005-12-09 22:29:21 +00:00
|
|
|
gstate.set_client_state_dirty("account manager RPC");
|
|
|
|
gstate.acct_mgr_info.write_info();
|
2005-02-06 21:26:21 +00:00
|
|
|
}
|
2005-06-21 08:22:18 +00:00
|
|
|
|
|
|
|
int ACCT_MGR_INFO::write_info() {
|
|
|
|
FILE* p;
|
2005-06-28 23:56:56 +00:00
|
|
|
if (strlen(acct_mgr_url)) {
|
2005-06-21 08:22:18 +00:00
|
|
|
p = fopen(ACCT_MGR_URL_FILENAME, "w");
|
|
|
|
if (p) {
|
|
|
|
fprintf(
|
|
|
|
p,
|
|
|
|
"<acct_mgr>\n"
|
|
|
|
" <name>%s</name>\n"
|
|
|
|
" <url>%s</url>\n"
|
|
|
|
"</acct_mgr>\n",
|
2005-06-28 23:56:56 +00:00
|
|
|
acct_mgr_name,
|
|
|
|
acct_mgr_url
|
2005-06-21 08:22:18 +00:00
|
|
|
);
|
|
|
|
fclose(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-28 23:56:56 +00:00
|
|
|
if (strlen(login_name)) {
|
2005-06-21 08:22:18 +00:00
|
|
|
p = fopen(ACCT_MGR_LOGIN_FILENAME, "w");
|
|
|
|
if (p) {
|
|
|
|
fprintf(
|
|
|
|
p,
|
|
|
|
"<acct_mgr_login>\n"
|
|
|
|
" <login>%s</login>\n"
|
2005-12-14 01:44:11 +00:00
|
|
|
" <password_hash>%s</password_hash>\n"
|
2005-11-26 00:12:44 +00:00
|
|
|
" <next_rpc_time>%f</next_rpc_time>\n"
|
2005-06-21 08:22:18 +00:00
|
|
|
"</acct_mgr_login>\n",
|
2005-06-28 23:56:56 +00:00
|
|
|
login_name,
|
2005-12-14 01:44:11 +00:00
|
|
|
password_hash,
|
2005-11-26 00:12:44 +00:00
|
|
|
next_rpc_time
|
2005-06-21 08:22:18 +00:00
|
|
|
);
|
|
|
|
fclose(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-28 23:56:56 +00:00
|
|
|
void ACCT_MGR_INFO::clear() {
|
|
|
|
strcpy(acct_mgr_name, "");
|
|
|
|
strcpy(acct_mgr_url, "");
|
|
|
|
strcpy(login_name, "");
|
2005-12-14 01:44:11 +00:00
|
|
|
strcpy(password_hash, "");
|
2005-12-09 22:29:21 +00:00
|
|
|
next_rpc_time = 0;
|
2005-06-28 23:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ACCT_MGR_INFO::ACCT_MGR_INFO() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2005-06-21 08:22:18 +00:00
|
|
|
int ACCT_MGR_INFO::init() {
|
|
|
|
char buf[256];
|
|
|
|
MIOFILE mf;
|
|
|
|
FILE* p;
|
|
|
|
|
2005-06-28 23:56:56 +00:00
|
|
|
clear();
|
2005-06-21 08:22:18 +00:00
|
|
|
p = fopen(ACCT_MGR_URL_FILENAME, "r");
|
|
|
|
if (p) {
|
|
|
|
mf.init_file(p);
|
|
|
|
while(mf.fgets(buf, sizeof(buf))) {
|
|
|
|
if (match_tag(buf, "</acct_mgr>")) break;
|
2005-06-28 23:56:56 +00:00
|
|
|
else if (parse_str(buf, "<name>", acct_mgr_name, 256)) continue;
|
|
|
|
else if (parse_str(buf, "<url>", acct_mgr_url, 256)) continue;
|
2005-06-21 08:22:18 +00:00
|
|
|
}
|
|
|
|
fclose(p);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = fopen(ACCT_MGR_LOGIN_FILENAME, "r");
|
|
|
|
if (p) {
|
|
|
|
mf.init_file(p);
|
|
|
|
while(mf.fgets(buf, sizeof(buf))) {
|
|
|
|
if (match_tag(buf, "</acct_mgr_login>")) break;
|
2005-06-28 23:56:56 +00:00
|
|
|
else if (parse_str(buf, "<login>", login_name, 256)) continue;
|
2005-12-14 01:44:11 +00:00
|
|
|
else if (parse_str(buf, "<password_hash>", password_hash, 256)) continue;
|
2005-11-26 00:12:44 +00:00
|
|
|
else if (parse_double(buf, "<next_rpc_time>", next_rpc_time)) continue;
|
2005-06-21 08:22:18 +00:00
|
|
|
}
|
|
|
|
fclose(p);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-22 08:46:51 +00:00
|
|
|
|
2005-11-26 00:12:44 +00:00
|
|
|
bool ACCT_MGR_INFO::poll() {
|
|
|
|
if (gstate.acct_mgr_op.error_num == ERR_IN_PROGRESS) return false;
|
|
|
|
if (gstate.now > next_rpc_time) {
|
2005-11-28 06:28:55 +00:00
|
|
|
next_rpc_time = gstate.now + 86400;
|
2005-12-14 01:44:11 +00:00
|
|
|
gstate.acct_mgr_op.do_rpc(acct_mgr_url, login_name, password_hash);
|
2005-11-26 00:12:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-02-11 04:46:01 +00:00
|
|
|
const char *BOINC_RCSID_8fd9e873bf="$Id$";
|