1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef BOINC_GUI_RPC_SERVER_H
19 #define BOINC_GUI_RPC_SERVER_H
20 
21 #include "network.h"
22 #include "acct_setup.h"
23 
24 // FSM states for auto-update
25 
26 #define AU_SS_INIT          0
27     // no get_screensaver_mode() yet
28 #define AU_SS_GOT           1
29     // got a get_screensaver_mode()
30 #define AU_SS_QUIT_REQ      2
31     // send a QUIT next time
32 #define AU_SS_QUIT_SENT     3
33     // QUIT sent
34 
35 #define AU_MGR_INIT         0
36 #define AU_MGR_GOT          1
37 #define AU_MGR_QUIT_REQ     2
38 #define AU_MGR_QUIT_SENT    3
39 
40 #define GUI_RPC_REQ_MSG_SIZE    100000
41 
42 class GUI_RPC_CONN {
43 public:
44     int sock;
45     MIOFILE mfout;
46     MFILE mout;
47     MIOFILE mfin;
48     XML_PARSER xp;
49     char request_msg[GUI_RPC_REQ_MSG_SIZE+1];
50     int request_nbytes;
51     char nonce[256];
52     bool auth_needed;
53         // if true, don't allow operations other than authentication
54     bool got_auth1;
55     bool got_auth2;
56         // keep track of whether we've got the 2 authentication msgs;
57         // don't accept more than one of each (to prevent DoS)
58     bool sent_unauthorized;
59         // we've send one <unauthorized>.
60         // On next auth failure, disconnect
61     bool is_local;
62         // connection is from local host
63     bool quit_flag;
64     int au_ss_state;
65     int au_mgr_state;
66     GUI_HTTP gui_http;
67     GET_PROJECT_CONFIG_OP get_project_config_op;
68     LOOKUP_ACCOUNT_OP lookup_account_op;
69     CREATE_ACCOUNT_OP create_account_op;
70 private:
71     bool notice_refresh;
72         // next time we get a get_notices RPC,
73         // send a -1 seqno, then the whole list
74 public:
set_notice_refresh()75     void set_notice_refresh() {
76         notice_refresh = true;
77     }
clear_notice_refresh()78     void clear_notice_refresh() {
79         notice_refresh = false;
80     }
get_notice_refresh()81     bool get_notice_refresh() {
82         return notice_refresh;
83     }
84     GUI_RPC_CONN(int);
85     ~GUI_RPC_CONN();
86     int handle_rpc();
87     void handle_auth1(MIOFILE&);
88     int handle_auth2(char*, MIOFILE&);
89 };
90 
91 // authentication for GUI RPCs:
92 // 1) if a host-list file is found, accept only from those hosts
93 // 2) if a password file file is found, ALSO demand password auth
94 
95 class GUI_RPC_CONN_SET {
96     std::vector<GUI_RPC_CONN*> gui_rpcs;
97     std::vector<sockaddr_storage> allowed_remote_ip_addresses;
98     int get_allowed_hosts();
99     void get_password();
100     int insert(GUI_RPC_CONN*);
101     bool check_allowed_list(sockaddr_storage& ip_addr);
102     bool remote_hosts_file_exists;
103 public:
104     int lsock;
105     double time_of_last_rpc_needing_network;
106         // time of the last RPC that needs network access to handle
107 
108     GUI_RPC_CONN_SET();
109     char password[256];
110     void get_fdset(FDSET_GROUP&, FDSET_GROUP&);
111     void got_select(FDSET_GROUP&);
112     int init_tcp(bool last_time);
113     int init_unix_domain();
114     void close();
115     bool recent_rpc_needs_network(double interval);
116     void send_quits();
117     bool quits_sent();
118     bool poll();
set_notice_refresh()119     void set_notice_refresh() {
120         for (unsigned int i=0; i<gui_rpcs.size(); i++) {
121             gui_rpcs[i]->set_notice_refresh();
122         }
123     }
124 };
125 
126 #endif
127