summaryrefslogtreecommitdiff
path: root/node/SoftwareUpdater.cpp
blob: c515d5db98ec834543a3812b028f87948b19893d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * ZeroTier One - Global Peer to Peer Ethernet
 * Copyright (C) 2012-2013  ZeroTier Networks LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * --
 *
 * ZeroTier may be used and distributed under the terms of the GPLv3, which
 * are available at: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * If you would like to embed ZeroTier into a commercial application or
 * redistribute it in a modified binary form, please contact ZeroTier Networks
 * LLC. Start here: http://www.zerotier.com/
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../version.h"

#include "SoftwareUpdater.hpp"
#include "Dictionary.hpp"
#include "C25519.hpp"
#include "Identity.hpp"
#include "Logger.hpp"
#include "RuntimeEnvironment.hpp"
#include "Thread.hpp"
#include "Node.hpp"

#ifdef __UNIX_LIKE__
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif

namespace ZeroTier {

SoftwareUpdater::SoftwareUpdater(const RuntimeEnvironment *renv) :
	_r(renv),
	_myVersion(packVersion(ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION)),
	_lastUpdateAttempt(0),
	_status(UPDATE_STATUS_IDLE),
	_die(false),
	_lock()
{
}

SoftwareUpdater::~SoftwareUpdater()
{
	_die = true;
	for(;;) {
		_lock.lock();
		bool ip = (_status != UPDATE_STATUS_IDLE);
		_lock.unlock();
		if (ip)
			Thread::sleep(500);
		else break;
	}
}

void SoftwareUpdater::_cbHandleGetLatestVersionInfo(void *arg,int code,const std::string &url,bool onDisk,const std::string &body)
{
	SoftwareUpdater *upd = (SoftwareUpdater *)arg;
	const RuntimeEnvironment *_r = (const RuntimeEnvironment *)upd->_r;
	Mutex::Lock _l(upd->_lock);

	if ((upd->_die)||(upd->_status != UPDATE_STATUS_GETTING_NFO)) {
		upd->_status = UPDATE_STATUS_IDLE;
		return;
	}

	if (code != 200) {
		LOG("unable to check for software updates, response code %d (%s)",code,body.c_str());
		upd->_status = UPDATE_STATUS_IDLE;
		return;
	}

	try {
		Dictionary nfo(body);
		const unsigned int vMajor = Utils::strToUInt(nfo.get("vMajor").c_str());
		const unsigned int vMinor = Utils::strToUInt(nfo.get("vMinor").c_str());
		const unsigned int vRevision = Utils::strToUInt(nfo.get("vRevision").c_str());
		const Address signedBy(nfo.get("signedBy"));
		const std::string signature(Utils::unhex(nfo.get("ed25519")));
		const std::string &url = nfo.get("url");

		if (signature.length() != ZT_C25519_SIGNATURE_LEN) {
			LOG("software update aborted: .nfo file invalid: bad Ed25519 signature");
			upd->_status = UPDATE_STATUS_IDLE;
			return;
		}
		if ((url.length() <= 7)||(url.substr(0,7) != "http://")) {
			LOG("software update aborted: .nfo file invalid: update URL must begin with http://");
			upd->_status = UPDATE_STATUS_IDLE;
			return;
		}
		if (packVersion(vMajor,vMinor,vRevision) <= upd->_myVersion) {
			LOG("software update aborted: .nfo file invalid: version on web site <= my version");
			upd->_status = UPDATE_STATUS_IDLE;
			return;
		}

		if (!ZT_DEFAULTS.updateAuthorities.count(signedBy)) {
			LOG("software update aborted: .nfo file specifies unknown signing authority");
			upd->_status = UPDATE_STATUS_IDLE;
			return;
		}

		upd->_status = UPDATE_STATUS_GETTING_FILE;
		upd->_signedBy = signedBy;
		upd->_signature = signature;

		HttpClient::GET(url,HttpClient::NO_HEADERS,ZT_UPDATE_HTTP_TIMEOUT,&_cbHandleGetLatestVersionBinary,arg);
	} catch ( ... ) {
		LOG("software update check failed: .nfo file invalid: fields missing or invalid dictionary format");
		upd->_status = UPDATE_STATUS_IDLE;
	}
}

void SoftwareUpdater::_cbHandleGetLatestVersionBinary(void *arg,int code,const std::string &url,bool onDisk,const std::string &body)
{
	SoftwareUpdater *upd = (SoftwareUpdater *)arg;
	const RuntimeEnvironment *_r = (const RuntimeEnvironment *)upd->_r;
	Mutex::Lock _l(upd->_lock);

	std::map< Address,Identity >::const_iterator updateAuthority = ZT_DEFAULTS.updateAuthorities.find(upd->_signedBy);
	if (updateAuthority == ZT_DEFAULTS.updateAuthorities.end()) { // sanity check, shouldn't happen
		LOG("software update aborted: .nfo file specifies unknown signing authority");
		upd->_status = UPDATE_STATUS_IDLE;
		return;
	}

	// The all-important authenticity check... :)
	if (!updateAuthority->second.verify(body.data(),body.length(),upd->_signature.data(),upd->_signature.length())) {
		LOG("software update aborted: update fetched from '%s' failed certificate check against signer %s",url.c_str(),updateAuthority->first.toString().c_str());
		upd->_status = UPDATE_STATUS_IDLE;
		return;
	}

#ifdef __UNIX_LIKE__
	size_t lastSlash = url.rfind('/');
	if (lastSlash == std::string::npos) { // sanity check, shouldn't happen
		LOG("software update aborted: invalid URL");
		upd->_status = UPDATE_STATUS_IDLE;
		return;
	}
	std::string updatesDir(_r->homePath + ZT_PATH_SEPARATOR_S + "updates.d");
	std::string updatePath(updatesDir + ZT_PATH_SEPARATOR_S + url.substr(lastSlash + 1));
	mkdir(updatesDir.c_str(),0755);

	int fd = ::open(updatePath.c_str(),O_WRONLY|O_CREAT|O_TRUNC,0755);
	if (fd <= 0) {
		LOG("software update aborted: unable to open %s for writing",updatePath.c_str());
		upd->_status = UPDATE_STATUS_IDLE;
		return;
	}
	if ((long)::write(fd,body.data(),body.length()) != (long)body.length()) {
		LOG("software update aborted: unable to write to %s",updatePath.c_str());
		upd->_status = UPDATE_STATUS_IDLE;
		return;
	}
	::close(fd);
	::chmod(updatePath.c_str(),0755);

	upd->_status = UPDATE_STATUS_IDLE;

	_r->node->terminate(Node::NODE_RESTART_FOR_UPGRADE,updatePath.c_str());
#endif

#ifdef __WINDOWS__
	todo;
#endif
}

} // namespace ZeroTier