2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2004-06-16 19:21:11 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC 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 3 of the License, or (at your option) any later version.
|
2004-06-16 19:21:11 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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.
|
2004-06-16 19:21:11 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2004-06-16 19:21:11 +00:00
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#include "cpp.h"
|
|
|
|
|
2004-06-16 19:21:11 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2005-12-16 03:45:01 +00:00
|
|
|
#else
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2004-06-16 19:21:11 +00:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#if HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2006-04-08 11:00:45 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
#endif
|
|
|
|
|
2013-11-19 04:31:14 +00:00
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "file_names.h"
|
|
|
|
#include "mac_address.h"
|
2004-06-16 19:21:11 +00:00
|
|
|
#include "parse.h"
|
2013-11-19 04:31:14 +00:00
|
|
|
#include "str_replace.h"
|
|
|
|
#include "str_util.h"
|
2008-11-05 20:11:45 +00:00
|
|
|
#include "util.h"
|
2004-06-16 19:21:11 +00:00
|
|
|
|
2013-02-11 20:46:46 +00:00
|
|
|
#include "client_msgs.h"
|
2019-11-28 08:50:31 +00:00
|
|
|
#include "client_state.h"
|
2013-02-11 20:46:46 +00:00
|
|
|
|
2004-06-16 19:21:11 +00:00
|
|
|
#include "hostinfo.h"
|
|
|
|
|
2004-06-17 17:00:14 +00:00
|
|
|
// get domain name and IP address of this host
|
2013-08-23 06:50:13 +00:00
|
|
|
// Android: if domain_name is empty, set it to android_xxxxxxxx
|
2004-06-16 19:21:11 +00:00
|
|
|
//
|
2006-06-23 20:41:47 +00:00
|
|
|
int HOST_INFO::get_local_network_info() {
|
2016-02-16 02:53:07 +00:00
|
|
|
safe_strcpy(ip_addr, "");
|
2013-08-23 06:50:13 +00:00
|
|
|
|
|
|
|
#ifdef ANDROID
|
2013-08-27 19:13:18 +00:00
|
|
|
if (strlen(domain_name) && strcmp(domain_name, "localhost")) return 0;
|
2013-08-23 06:50:13 +00:00
|
|
|
char buf[256];
|
|
|
|
make_random_string("", buf);
|
|
|
|
buf[8] = 0;
|
2016-02-18 05:59:13 +00:00
|
|
|
snprintf(domain_name, sizeof(domain_name), "android_%s", buf);
|
2013-08-23 19:47:39 +00:00
|
|
|
return 0;
|
2013-08-23 06:50:13 +00:00
|
|
|
#endif
|
2013-08-23 19:47:39 +00:00
|
|
|
|
2010-09-15 17:41:25 +00:00
|
|
|
struct sockaddr_storage s;
|
2006-04-08 11:00:45 +00:00
|
|
|
|
2016-02-16 02:53:07 +00:00
|
|
|
safe_strcpy(domain_name, "");
|
2007-12-27 21:40:20 +00:00
|
|
|
|
|
|
|
// it seems like we should use getdomainname() instead of gethostname(),
|
|
|
|
// but on FC6 it returns "(none)".
|
|
|
|
//
|
2010-09-15 17:41:25 +00:00
|
|
|
if (gethostname(domain_name, 256)) {
|
2004-06-16 19:21:11 +00:00
|
|
|
return ERR_GETHOSTBYNAME;
|
|
|
|
}
|
2010-09-15 17:41:25 +00:00
|
|
|
int retval = resolve_hostname(domain_name, s);
|
|
|
|
if (retval) return retval;
|
2010-09-15 20:33:44 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
sockaddr_in* sin = (sockaddr_in*)&s;
|
|
|
|
strlcpy(ip_addr, inet_ntoa(sin->sin_addr), sizeof(ip_addr));
|
|
|
|
#else
|
2010-09-15 17:41:25 +00:00
|
|
|
if (s.ss_family == AF_INET) {
|
|
|
|
sockaddr_in* sin = (sockaddr_in*)&s;
|
|
|
|
inet_ntop(AF_INET, (void*)(&sin->sin_addr), ip_addr, 256);
|
|
|
|
} else {
|
|
|
|
sockaddr_in6* sin = (sockaddr_in6*)&s;
|
|
|
|
inet_ntop(AF_INET6, (void*)(&sin->sin6_addr), ip_addr, 256);
|
|
|
|
}
|
2010-09-15 20:33:44 +00:00
|
|
|
#endif
|
2020-04-20 03:35:37 +00:00
|
|
|
if (!cc_config.device_name.empty()) {
|
2020-04-21 03:11:14 +00:00
|
|
|
safe_strcpy(domain_name, cc_config.device_name.c_str());
|
2020-04-20 03:35:37 +00:00
|
|
|
}
|
2004-06-16 19:21:11 +00:00
|
|
|
return 0;
|
2004-06-17 17:54:05 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 00:08:40 +00:00
|
|
|
// make a random string using time of day and host info.
|
2008-11-05 20:11:45 +00:00
|
|
|
// Not recommended for password generation;
|
|
|
|
// use as a last resort if more secure methods fail
|
|
|
|
//
|
|
|
|
void HOST_INFO::make_random_string(const char* salt, char* out) {
|
|
|
|
char buf[1024];
|
|
|
|
|
2012-08-04 00:27:32 +00:00
|
|
|
#ifdef ANDROID
|
2016-02-18 05:59:13 +00:00
|
|
|
snprintf(buf, sizeof(buf),
|
|
|
|
"%f%s%s%f%s",
|
2012-08-04 00:27:32 +00:00
|
|
|
dtime(), domain_name, ip_addr, d_free, salt
|
|
|
|
);
|
|
|
|
#else
|
2016-02-18 05:59:13 +00:00
|
|
|
snprintf(buf, sizeof(buf),
|
|
|
|
"%d%.15e%s%s%f%s",
|
2012-04-14 04:48:20 +00:00
|
|
|
getpid(), dtime(), domain_name, ip_addr, d_free, salt
|
|
|
|
);
|
2012-08-04 00:27:32 +00:00
|
|
|
#endif
|
2008-11-05 20:11:45 +00:00
|
|
|
md5_block((const unsigned char*) buf, (int)strlen(buf), out);
|
|
|
|
}
|
|
|
|
|
2019-11-28 08:50:31 +00:00
|
|
|
void make_secure_random_string(char* out) {
|
|
|
|
int retval = make_secure_random_string_os(out);
|
|
|
|
if (retval) {
|
|
|
|
if (cc_config.os_random_only) {
|
|
|
|
msg_printf(
|
|
|
|
NULL, MSG_INTERNAL_ERROR,
|
|
|
|
"OS random string generation failed, exiting"
|
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
gstate.host_info.make_random_string("guirpc", out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-05 20:11:45 +00:00
|
|
|
// make a host cross-project ID.
|
|
|
|
// Should be unique across hosts with very high probability
|
|
|
|
//
|
|
|
|
void HOST_INFO::generate_host_cpid() {
|
2013-11-19 04:31:14 +00:00
|
|
|
int retval;
|
2014-02-03 23:58:52 +00:00
|
|
|
char buf[256+MAXPATHLEN];
|
|
|
|
char dir[MAXPATHLEN];
|
2008-11-05 20:11:45 +00:00
|
|
|
|
2013-11-19 04:31:14 +00:00
|
|
|
// if a MAC address is available, compute an ID based on it;
|
|
|
|
// this has the advantage of stability
|
|
|
|
// (a given host will get the same ID each time BOINC is reinstalled)
|
|
|
|
//
|
|
|
|
retval = get_mac_address(buf);
|
|
|
|
if (retval) {
|
|
|
|
make_random_string("", host_cpid);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-03 23:58:52 +00:00
|
|
|
|
|
|
|
// append the current dir to the MAC address;
|
|
|
|
// that way if there are multiple instances per host
|
|
|
|
// (used by some grid installations)
|
|
|
|
// the instances will get different CPIDs
|
|
|
|
//
|
|
|
|
boinc_getcwd(dir);
|
2016-02-16 02:53:07 +00:00
|
|
|
safe_strcat(buf, dir);
|
2013-11-19 04:31:14 +00:00
|
|
|
md5_block((const unsigned char*) buf, (int)strlen(buf), host_cpid);
|
|
|
|
}
|