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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
// isself.cpp
/**
* Copyright (C) 2012 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/pch.h"
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include "mongo/db/auth/action_set.h"
#include "mongo/db/auth/action_type.h"
#include "mongo/db/auth/authorization_manager.h"
#include "mongo/db/auth/privilege.h"
#include "mongo/db/auth/security_key.h"
#include "mongo/db/commands.h"
#include "mongo/db/jsobj.h"
#include "mongo/util/net/listen.h"
#include "mongo/util/net/hostandport.h"
#include "mongo/client/dbclientinterface.h"
#ifndef _WIN32
# ifndef __sunos__
# include <ifaddrs.h>
# endif
# include <sys/resource.h>
# include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#ifdef __openbsd__
# include <sys/uio.h>
#endif
#endif
namespace mongo {
#if !defined(_WIN32) && !defined(__sunos__)
vector<string> getMyAddrs() {
vector<string> out;
ifaddrs * addrs;
if ( ! cmdLine.bind_ip.empty() ) {
boost::split( out, cmdLine.bind_ip, boost::is_any_of( ", " ) );
return out;
}
int status = getifaddrs(&addrs);
massert(13469, "getifaddrs failure: " + errnoWithDescription(errno), status == 0);
// based on example code from linux getifaddrs manpage
for (ifaddrs * addr = addrs; addr != NULL; addr = addr->ifa_next) {
if ( addr->ifa_addr == NULL ) continue;
int family = addr->ifa_addr->sa_family;
char host[NI_MAXHOST];
if (family == AF_INET || family == AF_INET6) {
status = getnameinfo(addr->ifa_addr,
(family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6)),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if ( status != 0 ) {
freeifaddrs( addrs );
addrs = NULL;
msgasserted( 13470, string("getnameinfo() failed: ") + gai_strerror(status) );
}
out.push_back(host);
}
}
freeifaddrs( addrs );
addrs = NULL;
if (logger::globalLogDomain()->shouldLog(logger::LogSeverity::Debug(1))) {
LogstreamBuilder builder(logger::globalLogDomain(),
getThreadName(),
logger::LogSeverity::Debug(1));
builder << "getMyAddrs():";
for (vector<string>::const_iterator it=out.begin(), end=out.end(); it!=end; ++it) {
builder << " [" << *it << ']';
}
builder << endl;
}
return out;
}
vector<string> getAllIPs(const string& iporhost) {
addrinfo* addrs = NULL;
addrinfo hints;
memset(&hints, 0, sizeof(addrinfo));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = (IPv6Enabled() ? AF_UNSPEC : AF_INET);
static string portNum = BSONObjBuilder::numStr(cmdLine.port);
vector<string> out;
int ret = getaddrinfo(iporhost.c_str(), portNum.c_str(), &hints, &addrs);
if ( ret ) {
warning() << "getaddrinfo(\"" << iporhost << "\") failed: " << gai_strerror(ret) << endl;
return out;
}
for (addrinfo* addr = addrs; addr != NULL; addr = addr->ai_next) {
int family = addr->ai_family;
char host[NI_MAXHOST];
if (family == AF_INET || family == AF_INET6) {
int status = getnameinfo(addr->ai_addr, addr->ai_addrlen, host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
massert(13472, string("getnameinfo() failed: ") + gai_strerror(status), status == 0);
out.push_back(host);
}
}
freeaddrinfo(addrs);
if (logger::globalLogDomain()->shouldLog(logger::LogSeverity::Debug(1))) {
LogstreamBuilder builder(logger::globalLogDomain(),
getThreadName(),
logger::LogSeverity::Debug(1));
builder << "getallIPs(\"" << iporhost << "\"):";
for (vector<string>::const_iterator it=out.begin(), end=out.end(); it!=end; ++it) {
builder << " [" << *it << ']';
}
builder << endl;
}
return out;
}
#endif
class IsSelfCommand : public Command {
public:
IsSelfCommand() : Command("_isSelf") , _cacheLock( "IsSelfCommand::_cacheLock" ) {}
virtual bool slaveOk() const { return true; }
virtual LockType locktype() const { return NONE; }
virtual void help( stringstream &help ) const {
help << "{ _isSelf : 1 } INTERNAL ONLY";
}
virtual void addRequiredPrivileges(const std::string& dbname,
const BSONObj& cmdObj,
std::vector<Privilege>* out) {} // No auth required
bool run(const string& dbname, BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl ) {
init();
result.append( "id" , _id );
return true;
}
void init() {
scoped_lock lk( _cacheLock );
if ( ! _id.isSet() )
_id.init();
}
OID _id;
mongo::mutex _cacheLock;
map<string,bool> _cache;
} isSelfCommand;
bool HostAndPort::isSelf() const {
int _p = port();
int p = _p == -1 ? CmdLine::DefaultDBPort : _p;
if( p != cmdLine.port ) {
// shortcut - ports have to match at the very least
return false;
}
string host = str::stream() << this->host() << ":" << p;
{
// check cache for this host
// debatably something _could_ change, but I'm not sure right now (erh 10/14/2010)
scoped_lock lk( isSelfCommand._cacheLock );
map<string,bool>::const_iterator i = isSelfCommand._cache.find( host );
if ( i != isSelfCommand._cache.end() )
return i->second;
}
#if !defined(_WIN32) && !defined(__sunos__)
// on linux and os x we can do a quick check for an ip match
const vector<string> myaddrs = getMyAddrs();
const vector<string> addrs = getAllIPs(_host);
for (vector<string>::const_iterator i=myaddrs.begin(), iend=myaddrs.end(); i!=iend; ++i) {
for (vector<string>::const_iterator j=addrs.begin(), jend=addrs.end(); j!=jend; ++j) {
string a = *i;
string b = *j;
if ( a == b ||
( str::startsWith( a , "127." ) && str::startsWith( b , "127." ) ) // 127. is all loopback
) {
// add to cache
scoped_lock lk( isSelfCommand._cacheLock );
isSelfCommand._cache[host] = true;
return true;
}
}
}
#endif
if ( ! Listener::getTimeTracker() ) {
// this ensures we are actually running a server
// this may return true later, so may want to retry
return false;
}
try {
isSelfCommand.init();
DBClientConnection conn;
string errmsg;
if ( ! conn.connect( host , errmsg ) ) {
// should this go in the cache?
return false;
}
if (AuthorizationManager::isAuthEnabled() && isInternalAuthSet()) {
if (!authenticateInternalUser(&conn)) {
return false;
}
}
BSONObj out;
bool ok = conn.simpleCommand( "admin" , &out , "_isSelf" );
bool me = ok && out["id"].type() == jstOID && isSelfCommand._id == out["id"].OID();
// add to cache
scoped_lock lk( isSelfCommand._cacheLock );
isSelfCommand._cache[host] = me;
return me;
}
catch ( std::exception& e ) {
warning() << "could't check isSelf (" << host << ") " << e.what() << endl;
}
return false;
}
}
|