// 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 #ifndef _WIN32 #include #include #include #include #include #include #include #include #include #endif #include "util.h" #include "error_numbers.h" #include "parse.h" #include "network.h" #include "filesys.h" #include "file_names.h" #include "client_msgs.h" #include "client_state.h" using std::string; using std::vector; GUI_RPC_CONN::GUI_RPC_CONN(int s) { sock = s; auth_needed = false; } GUI_RPC_CONN::~GUI_RPC_CONN() { boinc_close_socket(sock); } static void handle_get_project_status(MIOFILE& fout) { unsigned int i; fout.printf("\n"); for (i=0; iwrite_state(fout, true); } fout.printf("\n"); } static void handle_get_disk_usage(MIOFILE& fout) { unsigned int i; double size; fout.printf("\n"); for (i=0; i\n" " %s\n" " %f\n" "\n", p->master_url, size ); } fout.printf("\n"); } static PROJECT* get_project(char* buf, MIOFILE& fout) { string url; if (!parse_str(buf, "", url)) { fout.printf("Missing project URL\n"); return 0; } PROJECT* p = gstate.lookup_project(url.c_str()); if (!p) { fout.printf("No such project\n"); return 0 ; } return p; } static void handle_result_show_graphics(char* buf, MIOFILE& fout) { string result_name; GRAPHICS_MSG gm; ACTIVE_TASK* atp; if (match_tag(buf, "")) { gm.mode = MODE_FULLSCREEN; } else { gm.mode = MODE_WINDOW; } parse_str(buf, "", gm.window_station, sizeof(gm.window_station)); parse_str(buf, "", gm.desktop, sizeof(gm.desktop)); parse_str(buf, "", gm.display, sizeof(gm.display)); if (parse_str(buf, "", result_name)) { PROJECT* p = get_project(buf, fout); if (!p) { fout.printf("No such project\n"); return; } RESULT* rp = gstate.lookup_result(p, result_name.c_str()); if (!rp) { fout.printf("No such result\n"); return; } atp = gstate.lookup_active_task_by_result(rp); if (!atp) { fout.printf("no such result\n"); return; } atp->request_graphics_mode(gm); } else { for (unsigned int i=0; ischeduler_state != CPU_SCHED_SCHEDULED) continue; atp->request_graphics_mode(gm); } } fout.printf("\n"); } static void handle_project_op(char* buf, MIOFILE& fout, const char* op) { PROJECT* p = get_project(buf, fout); if (!p) { fout.printf("no such project\n"); return; } if (!strcmp(op, "reset")) { gstate.reset_project(p); } else if (!strcmp(op, "suspend")) { if (p->non_cpu_intensive) { msg_printf(p, MSG_ERROR, "Can't suspend non-CPU-intensive project"); } else { p->suspended_via_gui = true; } } else if (!strcmp(op, "resume")) { p->suspended_via_gui = false; } else if (!strcmp(op, "detach")) { gstate.detach_project(p); } else if (!strcmp(op, "update")) { p->sched_rpc_pending = true; p->min_rpc_time = 0; } else if (!strcmp(op, "nomorework")) { p->dont_request_more_work = true; } else if (!strcmp(op, "allowmorework")) { p->dont_request_more_work = false; } gstate.request_schedule_cpus("project op"); gstate.set_client_state_dirty("Project RPC"); fout.printf("\n"); } static void handle_project_attach(char* buf, MIOFILE& fout) { string url, authenticator; if (!parse_str(buf, "", url)) { fout.printf("Missing URL\n"); return; } if (!parse_str(buf, "", authenticator)) { fout.printf("Missing authenticator\n"); return; } gstate.add_project(url.c_str(), authenticator.c_str()); fout.printf("\n"); } static void handle_set_run_mode(char* buf, MIOFILE& fout) { if (match_tag(buf, "Missing mode\n"); return; } gstate.set_client_state_dirty("Set run mode RPC"); fout.printf("\n"); } static void handle_get_run_mode(char* , MIOFILE& fout) { fout.printf("\n"); switch (gstate.user_run_request) { case USER_RUN_REQUEST_ALWAYS: fout.printf("\n"); break; case USER_RUN_REQUEST_NEVER: fout.printf("\n"); break; case USER_RUN_REQUEST_AUTO: fout.printf("\n"); break; default: fout.printf("Unknown run mode\n"); } fout.printf("\n"); } static void handle_set_network_mode(char* buf, MIOFILE& fout) { if (match_tag(buf, "Missing mode\n"); return; } gstate.set_client_state_dirty("Set network mode RPC"); fout.printf("\n"); } static void handle_get_network_mode(char* , MIOFILE& fout) { fout.printf("\n"); switch (gstate.user_network_request) { case USER_RUN_REQUEST_ALWAYS: fout.printf("\n"); break; case USER_RUN_REQUEST_NEVER: fout.printf("\n"); break; default: fout.printf("Unknown network mode\n"); } fout.printf("\n"); } static void handle_run_benchmarks(char* , MIOFILE& fout) { gstate.start_cpu_benchmarks(); fout.printf("\n"); } static void handle_set_proxy_settings(char* buf, MIOFILE& fout) { MIOFILE in; in.init_buf(buf); gstate.proxy_info.parse(in); gstate.set_client_state_dirty("Set proxy settings RPC"); fout.printf("\n"); // tell running apps to reread app_info file (for F@h) // gstate.active_tasks.request_reread_app_info(); } static void handle_get_proxy_settings(char* , MIOFILE& fout) { gstate.proxy_info.write(fout); } static void handle_get_activity_state(char* , MIOFILE& fout) { fout.printf("\n"); if ( gstate.activities_suspended ) { fout.printf(" \n"); } if ( gstate.network_suspended ) { fout.printf(" \n"); } fout.printf("\n"); } // params: // [ n ] // return only msgs with seqno > n; if absent or zero, return all // void handle_get_messages(char* buf, MIOFILE& fout) { int seqno=0, i, j; unsigned int k; MESSAGE_DESC* mdp; bool found=false; parse_int(buf, "", seqno); j = message_descs.size()-1; for (k=0; kseqno <= seqno) { found = true; j = k-1; break; } } fout.printf("\n"); for (i=j; i>=0; i--) { mdp = message_descs[i]; fout.printf( "\n" " %s\n" " %d\n" " %d\n" " \n%s\n\n" " \n", mdp->project_name, mdp->priority, mdp->seqno, mdp->message.c_str(), mdp->timestamp ); fout.printf("\n"); } fout.printf("\n"); } // // XXX // XXX // // static void handle_file_transfer_op(char* buf, MIOFILE& fout, const char* op) { string filename; PROJECT* p = get_project(buf, fout); if (!p) { fout.printf("No such project\n"); return; } if (!parse_str(buf, "", filename)) { fout.printf("Missing filename\n"); return; } FILE_INFO* f = gstate.lookup_file_info(p, filename.c_str()); if (!f) { fout.printf("No such file\n"); return; } if (!f->pers_file_xfer) { fout.printf("No such transfer waiting\n"); return; } if (!strcmp(op, "retry")) { f->pers_file_xfer->next_request_time = 0; } else if (!strcmp(op, "abort")) { f->pers_file_xfer->abort(); } else { fout.printf("unknown op\n"); return; } gstate.set_client_state_dirty("File transfer RPC"); fout.printf("\n"); } static void handle_result_op(char* buf, MIOFILE& fout, const char* op) { RESULT* rp; char result_name[256]; ACTIVE_TASK* atp; PROJECT* p = get_project(buf, fout); if (!p) { fout.printf("No such project\n"); return; } if (!parse_str(buf, "", result_name, sizeof(result_name))) { fout.printf("Missing result name\n"); return; } rp = gstate.lookup_result(p, result_name); if (!rp) { fout.printf("no such result\n"); return; } if (!strcmp(op, "abort")) { atp = gstate.lookup_active_task_by_result(rp); if (atp) { atp->abort_task("aborted via GUI RPC"); } else { rp->aborted_via_gui = true; } } else if (!strcmp(op, "suspend")) { if (p->non_cpu_intensive) { msg_printf(p, MSG_ERROR, "Can't suspend non-CPU-intensive result"); } else { rp->suspended_via_gui = true; } } else if (!strcmp(op, "resume")) { rp->suspended_via_gui = false; } gstate.request_schedule_cpus("result op"); gstate.set_client_state_dirty("Result RPC"); fout.printf("\n"); } static void handle_get_host_info(char*, MIOFILE& fout) { gstate.host_info.write(fout); } static void handle_get_screensaver_mode(char*, MIOFILE& fout) { int ss_result = gstate.ss_logic.get_ss_status(); fout.printf( "\n" " %d\n" "\n", ss_result ); } static void handle_set_screensaver_mode(char* buf, MIOFILE& fout) { double blank_time = 0.0; GRAPHICS_MSG gm; parse_double(buf, "", blank_time); parse_str(buf, "", gm.desktop, sizeof(gm.desktop)); parse_str(buf, "", gm.window_station, sizeof(gm.window_station)); parse_str(buf, "", gm.display, sizeof(gm.display)); if (match_tag(buf, "\n"); } static void handle_quit(char* buf, MIOFILE& fout) { gstate.requested_exit = true; fout.printf("\n"); } static void handle_acct_mgr_rpc(char* buf, MIOFILE& fout) { std::string url, name, password; bool bad_arg = false; if (!parse_str(buf, "", url)) bad_arg = true; if (!parse_str(buf, "", name)) bad_arg = true; if (!parse_str(buf, "", password)) bad_arg = true; if (bad_arg) { fout.printf("bad arg\n"); } else { gstate.acct_mgr.do_rpc(url, name, password); fout.printf("\n"); } } static void auth_failure(MIOFILE& fout) { fout.printf("\n"); } void GUI_RPC_CONN::handle_auth1(MIOFILE& fout) { sprintf(nonce, "%f", dtime()); fout.printf("%s\n", nonce); } void GUI_RPC_CONN::handle_auth2(char* buf, MIOFILE& fout) { char nonce_hash[256], nonce_hash_correct[256], buf2[256]; if (!parse_str(buf, "", nonce_hash, 256)) { auth_failure(fout); return; } sprintf(buf2, "%s%s", nonce, gstate.gui_rpcs.password); md5_block((const unsigned char*)buf2, strlen(buf2), nonce_hash_correct); if (strcmp(nonce_hash, nonce_hash_correct)) { auth_failure(fout); return; } fout.printf("\n"); auth_needed = false; } void handle_get_statistics(char*, MIOFILE& fout) { fout.printf("\n"); for (std::vector::iterator i=gstate.projects.begin(); i!=gstate.projects.end();++i ) { (*i)->write_statistics(fout,true); } fout.printf("\n"); } int GUI_RPC_CONN::handle_rpc() { char request_msg[4096]; int n; MIOFILE mf; MFILE m; char* p; int client_version; mf.init_mfile(&m); SCOPE_MSG_LOG scope_messages(log_messages, CLIENT_MSG_LOG::DEBUG_GUIRPC); // read the request message in one read() // so that the core client won't hang because // of malformed request msgs // #ifdef _WIN32 n = recv(sock, request_msg, 4095, 0); #else n = read(sock, request_msg, 4095); #endif if (n <= 0) return ERR_READ; request_msg[n] = 0; scope_messages.printf("GUI RPC Command = '%s'\n", request_msg); // get client version. not used for now // parse_int(request_msg, "", client_version); mf.printf( "\n" "%d\n", gstate.version() ); if (match_tag(request_msg, "unrecognized op\n"); } mf.printf("\n\003"); m.get_buf(p, n); if (p) { scope_messages.printf("GUI RPC reply: '%s'\n", p); send(sock, p, n, 0); free(p); } return 0; } int GUI_RPC_CONN_SET::get_password() { strcpy(password, ""); if (boinc_file_exists(GUI_RPC_PASSWD_FILE)) { FILE* f = fopen(GUI_RPC_PASSWD_FILE, "r"); if (f) { fgets(password, 256, f); strip_whitespace(password); fclose(f); } } return 0; } int GUI_RPC_CONN_SET::get_allowed_hosts() { int ipaddr, retval; char buf[256], msg[256]; SCOPE_MSG_LOG scope_messages(log_messages, CLIENT_MSG_LOG::DEBUG_STATE); // open file remote_hosts.cfg and read in the // allowed host list and resolve them to an ip address // FILE* f = fopen(REMOTEHOST_FILE_NAME, "r"); if (f != NULL) { scope_messages.printf( "GUI_RPC_CONN_SET::get_allowed_hosts(): found allowed hosts list\n" ); // read in each line, if it is not a comment // then resolve the address and add to our allowed list // memset(buf,0,sizeof(buf)); while (fgets(buf, 256, f) != NULL) { strip_whitespace(buf); if (!(buf[0] =='#' || buf[0] == ';') && strlen(buf) > 0 ) { retval = resolve_hostname(buf, ipaddr, msg); if (!retval) { allowed_remote_ip_addresses.push_back((int)ntohl(ipaddr)); } } } fclose(f); } return 0; } int GUI_RPC_CONN_SET::insert(GUI_RPC_CONN* p) { gui_rpcs.push_back(p); return 0; } int GUI_RPC_CONN_SET::init() { sockaddr_in addr; int retval; get_allowed_hosts(); get_password(); lsock = socket(AF_INET, SOCK_STREAM, 0); if (lsock < 0) { msg_printf(NULL, MSG_ERROR, "GUI RPC failed to create socket: %d\n", lsock ); return ERR_SOCKET; } addr.sin_family = AF_INET; addr.sin_port = htons(GUI_RPC_PORT); #ifdef __APPLE__ addr.sin_addr.s_addr = htonl(INADDR_ANY); #else if (gstate.allow_remote_gui_rpc || allowed_remote_ip_addresses.size() > 0) { addr.sin_addr.s_addr = htonl(INADDR_ANY); msg_printf(NULL, MSG_INFO, "Remote control allowed\n"); } else { addr.sin_addr.s_addr = inet_addr("127.0.0.1"); msg_printf(NULL, MSG_INFO, "Remote control not allowed; using loopback address\n"); } #endif int one = 1; setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, (char*)&one, 4); retval = bind(lsock, (const sockaddr*)(&addr), (boinc_socklen_t)sizeof(addr)); if (retval) { addr.sin_port = htons(GUI_RPC_PORT_ALT); retval = bind(lsock, (const sockaddr*)(&addr), (boinc_socklen_t)sizeof(addr)); if (retval) { msg_printf(NULL, MSG_ERROR, "GUI RPC bind failed: %d\n", retval); boinc_close_socket(lsock); lsock = -1; return ERR_BIND; } } retval = listen(lsock, 999); if (retval) { msg_printf(NULL, MSG_ERROR, "GUI RPC listen failed: %d\n", retval); boinc_close_socket(lsock); lsock = -1; return ERR_LISTEN; } return 0; } static void show_connect_error(in_addr ia) { static double last_time=0; static int count=0; double now = dtime(); if (last_time == 0) { last_time = now; count = 1; } else { if (now-last_time < 600) { count++; return; } last_time = now; } msg_printf( NULL, MSG_ERROR, "GUI RPC request from non-allowed address %s\n", inet_ntoa(ia) ); if (count > 1) { msg_printf( NULL, MSG_ERROR, "%d connections rejected in last 10 minutes\n", count ); } count = 0; } bool GUI_RPC_CONN_SET::poll(double) { int n = 0; unsigned int i; fd_set read_fds, error_fds; int sock, retval; vector::iterator iter; GUI_RPC_CONN* gr; struct timeval tv; bool is_local = false; if (lsock < 0) return false; FD_ZERO(&read_fds); FD_ZERO(&error_fds); FD_SET(lsock, &read_fds); for (i=0; isock, &read_fds); FD_SET(gr->sock, &error_fds); } memset(&tv, 0, sizeof(tv)); n = select(FD_SETSIZE, &read_fds, 0, &error_fds, &tv); if (FD_ISSET(lsock, &read_fds)) { struct sockaddr_in addr; boinc_socklen_t addr_len = sizeof(addr); sock = accept(lsock, (struct sockaddr*)&addr, &addr_len); int peer_ip = (int) ntohl(addr.sin_addr.s_addr); // check list of allowed remote hosts bool allowed = false; vector::iterator remote_iter; remote_iter = allowed_remote_ip_addresses.begin(); while (remote_iter != allowed_remote_ip_addresses.end() ) { int remote_host = *remote_iter; if (peer_ip == remote_host) allowed = true; remote_iter++; } // accept the connection if: // 1) allow_remote_gui_rpc is set or // 2) client host is included in "remote_hosts" file or // 3) client is on localhost // if (peer_ip == 0x7f000001) { allowed = true; is_local = true; } if (!(gstate.allow_remote_gui_rpc) && !(allowed)) { in_addr ia; ia.s_addr = htonl(peer_ip); show_connect_error(ia); boinc_close_socket(sock); } else { gr = new GUI_RPC_CONN(sock); if (strlen(password)) { gr->auth_needed = true; } gr->is_local = is_local; insert(gr); } } iter = gui_rpcs.begin(); while (iter != gui_rpcs.end()) { gr = *iter; if (FD_ISSET(gr->sock, &error_fds)) { delete gr; gui_rpcs.erase(iter); } else { iter++; } } iter = gui_rpcs.begin(); while (iter != gui_rpcs.end()) { gr = *iter; if (FD_ISSET(gr->sock, &read_fds)) { retval = gr->handle_rpc(); if (retval) { delete gr; gui_rpcs.erase(iter); continue; } } iter++; } return (n > 0); } const char *BOINC_RCSID_88dd75dd85 = "$Id$";