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
|
// isself.cpp
#include "pch.h"
#include "../../util/net/listen.h"
#include "../commands.h"
#include "../../client/dbclient.h"
#include "../security.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() {
ifaddrs * addrs;
int status = getifaddrs(&addrs);
massert(13469, "getifaddrs failure: " + errnoWithDescription(errno), status == 0);
vector<string> out;
// 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 (logLevel >= 1) {
log(1) << "getMyAddrs():";
for (vector<string>::const_iterator it=out.begin(), end=out.end(); it!=end; ++it) {
log(1) << " [" << *it << ']';
}
log(1) << endl;
}
return out;
}
vector<string> getAllIPs(StringData 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.data(), portNum.c_str(), &hints, &addrs);
if ( ret ) {
warning() << "getaddrinfo(\"" << iporhost.data() << "\") 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 (logLevel >= 1) {
log(1) << "getallIPs(\"" << iporhost << "\"):";
for (vector<string>::const_iterator it=out.begin(), end=out.end(); it!=end; ++it) {
log(1) << " [" << *it << ']';
}
log(1) << 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";
}
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 == -1 ? CmdLine::DefaultDBPort : _port;
if( p != cmdLine.port ) {
// shortcut - ports have to match at the very least
return false;
}
string host = str::stream() << _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 (!noauth && cmdLine.keyFile &&
!conn.auth("local", internalSecurity.user, internalSecurity.pwd, errmsg, false)) {
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;
}
}
|