From 44a7d0409cacd7de5036690b1ca3af78c918dc95 Mon Sep 17 00:00:00 2001
From: Douglas Hubler <dhubler@ezuce.com>
Date: Thu, 20 Oct 2011 13:48:39 -0400
Subject: [PATCH 1/3] SERVER-4178 shared library did not compile, needed to
 separate client and server security code that were
 mingled

---
 SConstruct                    |    9 ++---
 db/cmdline.cpp                |    2 +-
 db/repl/connections.h         |    2 +-
 db/security_common.cpp        |   26 -----------------
 db/security_common_server.cpp |   62 +++++++++++++++++++++++++++++++++++++++++
 db/security_common_server.h   |   31 ++++++++++++++++++++
 s/security.cpp                |    2 +-
 7 files changed, 100 insertions(+), 34 deletions(-)
 create mode 100644 db/security_common_server.cpp
 create mode 100644 db/security_common_server.h

diff --git a/SConstruct b/SConstruct
index 4e46052..3716bb9 100644
--- a/SConstruct
+++ b/SConstruct
@@ -327,7 +327,7 @@ if has_option( "full" ):
 
 # ------    SOURCE FILE SETUP -----------
 
-commonFiles = Split( "pch.cpp buildinfo.cpp db/indexkey.cpp db/jsobj.cpp bson/oid.cpp db/json.cpp db/lasterror.cpp db/nonce.cpp db/queryutil.cpp db/querypattern.cpp db/projection.cpp shell/mongo.cpp db/security_common.cpp db/security_commands.cpp" )
+commonFiles = Split( "pch.cpp buildinfo.cpp db/indexkey.cpp db/jsobj.cpp bson/oid.cpp db/json.cpp db/lasterror.cpp db/nonce.cpp db/queryutil.cpp db/querypattern.cpp db/projection.cpp shell/mongo.cpp db/security_common.cpp" )
 commonFiles += [ "util/background.cpp" , "util/util.cpp" , "util/file_allocator.cpp" ,
                  "util/assert_util.cpp" , "util/log.cpp" , "util/ramlog.cpp" , "util/md5main.cpp" , "util/base64.cpp", "util/concurrency/vars.cpp", "util/concurrency/task.cpp", "util/debug_util.cpp",
                  "util/concurrency/thread_pool.cpp", "util/password.cpp", "util/version.cpp", "util/signal_handlers.cpp",  
@@ -343,7 +343,7 @@ coreDbFiles = [ "db/commands.cpp" ]
 coreServerFiles = [ "util/net/message_server_port.cpp" , 
                     "client/parallel.cpp" , "db/common.cpp", 
                     "util/net/miniwebserver.cpp" , "db/dbwebserver.cpp" , 
-                    "db/matcher.cpp" , "db/dbcommands_generic.cpp" , "db/dbmessage.cpp" ]
+                    "db/matcher.cpp" , "db/dbcommands_generic.cpp" , "db/dbmessage.cpp", "db/security_common_server.cpp", "db/security_commands.cpp" ]
 
 mmapFiles = [ "util/mmap.cpp" ]
 
@@ -416,8 +416,7 @@ for x in os.listdir( "db/modules/" ):
     else:
         serverOnlyFiles += Glob( modRoot + "src/*.cpp" )
 
-
-allClientFiles = commonFiles + coreDbFiles + [ "client/clientOnly.cpp" , "client/gridfs.cpp" ];
+allClientFiles = commonFiles + coreDbFiles + [ "client/clientOnly.cpp" , "client/gridfs.cpp" ]
 
 # ---- other build setup -----
 
@@ -1131,7 +1130,7 @@ if darwin or clientEnv["_HAVEPCAP"]:
         sniffEnv.Append( LIBS=[ "wpcap" ] )
 
     sniffEnv.Prepend( LIBPATH=["."] )
-    sniffEnv.Append( LIBS=[ "mongotestfiles" ] )
+    sniffEnv.Append( LIBS=[ "mongotestfiles" , "boost_filesystem" ] )
 
     sniffEnv.Program( "mongosniff" , "tools/sniffer.cpp" )
 
diff --git a/db/cmdline.cpp b/db/cmdline.cpp
index fd759a7..2537542 100644
--- a/db/cmdline.cpp
+++ b/db/cmdline.cpp
@@ -22,7 +22,7 @@
 #include "../util/password.h"
 #include "../util/processinfo.h"
 #include "../util/net/listen.h"
-#include "security_common.h"
+#include "security_common_server.h"
 
 #ifdef _WIN32
 #include <direct.h>
diff --git a/db/repl/connections.h b/db/repl/connections.h
index 78cfb30..17d6205 100644
--- a/db/repl/connections.h
+++ b/db/repl/connections.h
@@ -20,7 +20,7 @@
 
 #include <map>
 #include "../../client/dbclient.h"
-#include "../security_common.h"
+#include "../security_common_server.h"
 
 namespace mongo {
 
diff --git a/db/security_common.cpp b/db/security_common.cpp
index 04cea99..42e3e13 100644
--- a/db/security_common.cpp
+++ b/db/security_common.cpp
@@ -33,7 +33,6 @@
 
 namespace mongo {
 
-    bool noauth = true;
     AuthInfo internalSecurity;
 
     bool setUpSecurityKey(const string& filename) {
@@ -106,29 +105,4 @@ namespace mongo {
 
         return true;
     }
-
-    bool AuthenticationInfo::_isAuthorized(const string& dbname, int level) const {
-        {
-            scoped_spinlock lk(_lock);
-
-            if ( _isAuthorizedSingle_inlock( dbname , level ) )
-                return true;
-
-            if ( noauth )
-                return true;
-
-            if ( _isAuthorizedSingle_inlock( "admin" , level ) )
-                return true;
-
-            if ( _isAuthorizedSingle_inlock( "local" , level ) )
-                return true;
-        }
-        return _isAuthorizedSpecialChecks( dbname );
-    }
-
-    bool AuthenticationInfo::_isAuthorizedSingle_inlock(const string& dbname, int level) const {
-        MA::const_iterator i = _dbs.find(dbname);
-        return i != _dbs.end() && i->second.level >= level;
-    }
-
 } // namespace mongo
diff --git a/db/security_common_server.cpp b/db/security_common_server.cpp
new file mode 100644
index 0000000..ee6ca06
--- /dev/null
+++ b/db/security_common_server.cpp
@@ -0,0 +1,62 @@
+// security_common.cpp
+/*
+ *    Copyright (C) 2010 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/>.
+ */
+
+/**
+ * This file contains inter-mongo instance security helpers.  Due to the
+ * requirement that it be possible to compile this into mongos and mongod, it
+ * should not depend on much external stuff.
+ */
+
+#include "pch.h"
+#include "security.h"
+#include "security_common_server.h"
+#include "../client/dbclient.h"
+#include "commands.h"
+#include "nonce.h"
+#include "../util/md5.hpp"
+
+#include <sys/stat.h>
+
+namespace mongo {
+
+    bool noauth = true;
+
+    bool AuthenticationInfo::_isAuthorized(const string& dbname, int level) const {
+        {
+            scoped_spinlock lk(_lock);
+
+            if ( _isAuthorizedSingle_inlock( dbname , level ) )
+                return true;
+
+            if ( noauth )
+                return true;
+
+            if ( _isAuthorizedSingle_inlock( "admin" , level ) )
+                return true;
+
+            if ( _isAuthorizedSingle_inlock( "local" , level ) )
+                return true;
+        }
+        return _isAuthorizedSpecialChecks( dbname );
+    }
+
+    bool AuthenticationInfo::_isAuthorizedSingle_inlock(const string& dbname, int level) const {
+        MA::const_iterator i = _dbs.find(dbname);
+        return i != _dbs.end() && i->second.level >= level;
+    }
+
+} // namespace mongo
diff --git a/db/security_common_server.h b/db/security_common_server.h
new file mode 100644
index 0000000..f5372cc
--- /dev/null
+++ b/db/security_common_server.h
@@ -0,0 +1,31 @@
+// security_common.h
+
+/**
+*    Copyright (C) 2009 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/>.
+*/
+
+#pragma once
+
+#include "commands.h"
+#include "concurrency.h"
+#include "security_common.h"
+#include "../util/concurrency/spin_lock.h"
+
+namespace mongo {
+
+    // --noauth cmd line option
+    extern bool noauth;
+
+} // namespace mongo
diff --git a/s/security.cpp b/s/security.cpp
index 0b8954e..9a10969 100644
--- a/s/security.cpp
+++ b/s/security.cpp
@@ -18,7 +18,7 @@
 // security.cpp
 
 #include "pch.h"
-#include "../db/security_common.h"
+#include "../db/security_common_server.h"
 #include "../db/security.h"
 #include "config.h"
 #include "client.h"
-- 
1.7.4.4

