Repository :
http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
---------------------------------------------------------------
commit 8d588474f80a7d13102b50ee3fbf00ae78804e4c
Author: Peter Hatina <phatina(a)redhat.com>
Date: Wed Apr 3 12:25:35 2013 +0200
introduce UDS for root@localhost
no authentication needed when uid == 0 and managed machine is either localhost,
127.0.0.1 or ::1
---------------------------------------------------------------
cli-tools/lmi/lmi_client_base.py | 11 ++++-
cli-tools/lmi/lmi_client_shell.py | 27 +++++++++++--
cli-tools/lmishell | 80 +++++++++++++++++++-----------------
3 files changed, 73 insertions(+), 45 deletions(-)
diff --git a/cli-tools/lmi/lmi_client_base.py b/cli-tools/lmi/lmi_client_base.py
index 2b34823..a69cdfe 100644
--- a/cli-tools/lmi/lmi_client_base.py
+++ b/cli-tools/lmi/lmi_client_base.py
@@ -78,16 +78,21 @@ class LmiReturnValue(_RValue):
class LmiBaseClient(object):
QUERY_LANG_CQL = "CQL"
QUERY_LANG_WQL = "WQL"
+ CONN_TYPE_WBEM, \
+ CONN_TYPE_PEGASUS_UDS = range(2)
use_exceptions_flag = False
- def __init__(self, hostname, username = "", password = ""):
+ def __init__(self, hostname, username = "", password = "",
conn_type = CONN_TYPE_WBEM):
self._hostname = hostname
self._username = username
if not self._hostname.startswith("http://") and \
not self._hostname.startswith("https://"):
self._hostname = "https://" + self._hostname
- self._cliconn = pywbem.WBEMConnection(self._hostname,
- (self._username, password))
+ if conn_type == LmiBaseClient.CONN_TYPE_PEGASUS_UDS:
+ self._cliconn = pywbem.PegasusUDSConnection()
+ else:
+ self._cliconn = pywbem.WBEMConnection(self._hostname,
+ (self._username, password))
def _get_instance_names(self, class_name, namespace = None, **kwargs):
filter_value = ""
diff --git a/cli-tools/lmi/lmi_client_shell.py b/cli-tools/lmi/lmi_client_shell.py
index 1aa593e..fb3a29e 100644
--- a/cli-tools/lmi/lmi_client_shell.py
+++ b/cli-tools/lmi/lmi_client_shell.py
@@ -1002,8 +1002,9 @@ class _LmiShellCache(object):
self._active = val
class _LmiShellClient(LmiBaseClient):
- def __init__(self, hostname, username = "", password = "",
interactive = False, use_cache = True):
- super(self.__class__, self).__init__(hostname, username, password)
+ def __init__(self, hostname, username = "", password = "",
interactive = False, use_cache = True, \
+ conn_type = LmiBaseClient.CONN_TYPE_WBEM):
+ super(self.__class__, self).__init__(hostname, username, password, conn_type)
self._interactive = interactive
self._cache = _LmiShellCache(use_cache)
@@ -1038,8 +1039,9 @@ class _LmiShellClient(LmiBaseClient):
self._interactive = bool(i)
class LmiConnection(object):
- def __init__(self, hostname, username = "", password = "",
interactive = False, use_cache = True):
- self._client = _LmiShellClient(hostname, username, password, interactive,
use_cache)
+ def __init__(self, hostname, username = "", password = "",
interactive = False, use_cache = True, \
+ conn_type = LmiBaseClient.CONN_TYPE_WBEM):
+ self._client = _LmiShellClient(hostname, username, password, interactive,
use_cache, conn_type)
@property
def hostname(self):
@@ -1058,6 +1060,23 @@ class LmiConnection(object):
def use_cache(self, active = True):
self._client._cache.active = active
+ def verify_credentials(self):
+ use_exceptions = LmiBaseClient._get_use_exceptions()
+ try:
+ LmiBaseClient._set_use_exceptions(True)
+ self._client._get_class("SomeNonExistingClass")
+ except pywbem.cim_operations.CIMError, e:
+ if e.args[0] == pywbem.cim_constants.CIM_ERR_NOT_FOUND:
+ return True
+ if use_exceptions:
+ raise
+ return False
+ except pywbem.cim_http.AuthError, e:
+ return False
+ finally:
+ LmiBaseClient._set_use_exceptions(use_exceptions)
+ return True
+
@property
def root(self):
return _LmiNamespaceRoot(self._client)
diff --git a/cli-tools/lmishell b/cli-tools/lmishell
index 882c30e..89844d8 100755
--- a/cli-tools/lmishell
+++ b/cli-tools/lmishell
@@ -20,6 +20,7 @@ import code
import copy
import pywbem
import readline
+import urlparse
import rlcompleter
import __builtin__
from lmi.lmi_client_base import LmiReturnValue
@@ -305,53 +306,56 @@ class LmiInteractiveShell(code.InteractiveConsole):
def clear_history(self):
readline.clear_history()
+def __lmi_raw_input(prompt, use_echo = True):
+ if not use_echo:
+ os.system("stty -echo")
+ try:
+ result = raw_input(prompt)
+ except EOFError, e:
+ if not use_echo:
+ os.system("stty echo")
+ sys.stdout.write("\n")
+ return None
+ except KeyboardInterrupt, e:
+ if not use_echo:
+ os.system("stty echo")
+ raise
+ if not use_echo:
+ os.system("stty echo")
+ sys.stdout.write("\n")
+ if result:
+ cur_hist_len = readline.get_current_history_length()
+ readline.remove_history_item(cur_hist_len - 1)
+ return result
+
def _lmi_interact(locals = None):
console = LmiInteractiveShell("> ", "... ", locals)
console.interact()
def _connect(hostname, username = "", password = "", interactive =
False, use_cache = True):
- if not username:
- while True:
- try:
- username = raw_input("username: ")
- if username:
- break
- except EOFError, e:
- sys.stdout.write("\n")
- continue
- except KeyboardInterrupt, e:
- sys.stdout.write("\n")
- return None
- readline.remove_history_item(readline.get_current_history_length() - 1)
- if not password:
+ connection = None
+ netloc = urlparse.urlparse(hostname).netloc
+ destination = netloc if netloc else hostname
+ if os.getuid() == 0 and destination in ("localhost", "127.0.0.1",
"::1") and \
+ os.path.exists("/var/run/tog-pegasus/cimxml.socket") and \
+ not username and not password:
+ connection = LmiConnection(hostname, None, None, interactive,
+ use_cache, LmiBaseClient.CONN_TYPE_PEGASUS_UDS)
+ if not connection.verify_credentials():
+ connection = None
+ if connection is None:
try:
- os.system("stty -echo")
- password = raw_input("password: ")
- os.system("stty echo")
- except EOFError, e:
- password = ""
+ if not username:
+ username = __lmi_raw_input("username: ", True)
+ if not password:
+ password = __lmi_raw_input("password: ", False)
except KeyboardInterrupt, e:
- os.system("stty echo")
sys.stdout.write("\n")
return None
- sys.stdout.write("\n")
- readline.remove_history_item(readline.get_current_history_length() - 1)
- # Try to get some non-existing class as a login check
- connection = LmiConnection(hostname, username, password, interactive, use_cache)
- use_exceptions = LmiBaseClient._get_use_exceptions()
- try:
- LmiBaseClient._set_use_exceptions(True)
- connection.root.cimv2.NonExistingClass
- except pywbem.cim_operations.CIMError, e:
- if e.args[0] == pywbem.cim_constants.CIM_ERR_NOT_FOUND:
- return connection
- if use_exceptions:
- raise
- return None
- except pywbem.cim_http.AuthError, e:
- return None
- finally:
- LmiBaseClient._set_use_exceptions(use_exceptions)
+ connection = LmiConnection(hostname, username, password, interactive,
+ use_cache, LmiBaseClient.CONN_TYPE_WBEM)
+ if not connection.verify_credentials():
+ return None
return connection
def _use_display_sugar(use = True):