aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPapoteur <papoteur@mageialinux-online.org>2015-07-05 11:54:47 +0200
committerThierry Vignaud <thierry.vignaud@gmail.com>2015-07-25 17:24:10 +0200
commit4fac47893db3ede8b27c2d682b1f6d9165f180a8 (patch)
treeb60fcca71959fc6599c2464ae39cf95c43292d91 /src
parent5134f51bbc3bcc9834cf8a92e1e9b3ab08d04782 (diff)
downloadmsec-4fac47893db3ede8b27c2d682b1f6d9165f180a8.tar
msec-4fac47893db3ede8b27c2d682b1f6d9165f180a8.tar.gz
msec-4fac47893db3ede8b27c2d682b1f6d9165f180a8.tar.bz2
msec-4fac47893db3ede8b27c2d682b1f6d9165f180a8.tar.xz
msec-4fac47893db3ede8b27c2d682b1f6d9165f180a8.zip
Add a Narg function to deal the encoding strings. #mga13837
v1: Thierry Vignaud v2: improved by patpoteur
Diffstat (limited to 'src')
-rwxr-xr-xsrc/msec/config.py41
-rwxr-xr-xsrc/msec/libmsec.py51
-rwxr-xr-xsrc/msec/msecgui.py17
-rwxr-xr-xsrc/msec/tools.py9
4 files changed, 81 insertions, 37 deletions
diff --git a/src/msec/config.py b/src/msec/config.py
index c8e87f6..9f41998 100755
--- a/src/msec/config.py
+++ b/src/msec/config.py
@@ -81,6 +81,9 @@ SETTINGS_PERIODIC = []
# checks that support exceptions - defined by 'audit' plugin
CHECKS_WITH_EXCEPTIONS = []
+# system encoding
+SYS_ENC = sys.getfilesystemencoding()
+
# localized help
try:
from help import HELP
@@ -168,6 +171,26 @@ def merge_with_baselevel(log, config, base_level, load_func, root=''):
config.merge(levelconf)
+def to_utf8(s):
+ """ Returs string after decoding if needed """
+ try:
+ s.decode()
+ return s
+ except:
+ return str(s).decode("utf-8")
+
+def Narg(s):
+ """ Returs string after decoding if needed """
+ try:
+ s.decode()
+ return s
+ except:
+ try:
+ t=str(s)
+ except:
+ return s
+ return t.decode(SYS_ENC)
+
# {{{ MsecConfig
class MsecConfig:
"""Msec configuration parser"""
@@ -208,7 +231,7 @@ class MsecConfig:
try:
fd = open(self.config)
except:
- self.log.error(_("Unable to load configuration file %s: %s") % (self.config, sys.exc_value[1]))
+ self.log.error(_("Unable to load configuration file %s: %s") % (self.config, Narg(sys.exc_value[1])))
return False
for line in fd.readlines():
line = line.strip()
@@ -222,7 +245,7 @@ class MsecConfig:
option, val = line.split("=", 1)
self.options[option] = val
except:
- self.log.warn(_("Bad config option: %s") % line)
+ self.log.warn(_("Bad config option: %s") % Narg(line))
continue
fd.close()
return True
@@ -257,7 +280,7 @@ class MsecConfig:
try:
fd = open(self.config, "w")
except:
- self.log.error(_("Unable to save %s: %s") % (self.config, sys.exc_value))
+ self.log.error(_("Unable to save %s: %s") % (self.config, Narg(sys.exc_value)))
return False
for comment in self.comments:
print >>fd, comment
@@ -316,7 +339,7 @@ class ExceptionConfig:
fd = open(self.config)
except:
# this file is optional, so if it is not found that's not fatal
- self.log.info(_("loading exceptions file %s: %s") % (self.config, sys.exc_value[1]))
+ self.log.info(_("loading exceptions file %s: %s") % (self.config, Narg(sys.exc_value[1])))
self.log.info(_("No exceptions loaded"))
return False
for line in fd.readlines():
@@ -331,7 +354,7 @@ class ExceptionConfig:
option, val = line.split(" ", 1)
self.options.append((option, val))
except:
- self.log.warn(_("Bad config option: %s") % line)
+ self.log.warn(_("Bad config option: %s") % Narg(line))
continue
fd.close()
return True
@@ -370,7 +393,7 @@ class ExceptionConfig:
try:
fd = open(self.config, "w")
except:
- self.log.error(_("Unable to save %s: %s") % (self.config, sys.exc_value))
+ self.log.error(_("Unable to save %s: %s") % (self.config, Narg(sys.exc_value)))
return False
for comment in self.comments:
print >>fd, comment
@@ -420,7 +443,7 @@ class PermConfig(MsecConfig):
try:
fd = open(self.config)
except:
- self.log.error(_("Unable to load configuration file %s: %s") % (self.config, sys.exc_value))
+ self.log.error(_("Unable to load configuration file %s: %s") % (self.config, Narg(sys.exc_value)))
return False
for line in fd.readlines():
line = line.strip()
@@ -439,7 +462,7 @@ class PermConfig(MsecConfig):
self.options_order.append(file)
except:
traceback.print_exc()
- self.log.warn(_("Bad config option: %s") % line)
+ self.log.warn(_("Bad config option: %s") % Narg(line))
continue
fd.close()
return True
@@ -465,7 +488,7 @@ class PermConfig(MsecConfig):
try:
fd = open(self.config, "w")
except:
- self.log.error(_("Unable to save %s: %s") % (self.config, sys.exc_value))
+ self.log.error(_("Unable to save %s: %s") % (self.config, Narg(sys.exc_value)))
return False
for comment in self.comments:
print >>fd, comment
diff --git a/src/msec/libmsec.py b/src/msec/libmsec.py
index d456eab..4b7e20d 100755
--- a/src/msec/libmsec.py
+++ b/src/msec/libmsec.py
@@ -262,7 +262,7 @@ class ConfigFiles:
if res:
s = substitute_re_result(res, a[1])
if commit:
- self.log.info(_('%s modified so launched command: %s') % (f, s))
+ self.log.info(_('%s modified so launched command: %s') % (Narg(f), Narg(s)))
cmd = commands.getstatusoutput(s)
cmd = [0, '']
if cmd[0] == 0:
@@ -367,32 +367,41 @@ class ConfigFile:
return link
def write(self):
+ if not (type(self.path) == unicode):
+ self.pathN = Narg(self.path)
+ else:
+ self.pathN = self.path
+
if self.is_deleted:
if self.exists():
try:
os.unlink(self.path)
except:
- error('unlink %s: %s' % (self.path, str(sys.exc_value)))
- self.log.info(_('deleted %s') % (self.path,))
+ error('unlink %s: %s' % (self.pathN, str(sys.exc_value)))
+ self.log.info(_('deleted %s') % (self.pathN,))
elif self.is_touched:
if os.path.exists(self.path):
try:
os.utime(self.path, None)
except:
- self.log.error('utime %s: %s' % (self.path, str(sys.exc_value)))
+ self.log.error('utime %s: %s' % (self.pathN, str(sys.exc_value)))
elif self.suffix and os.path.exists(self.path + self.suffix):
move(self.path + self.suffix, self.path)
try:
os.utime(self.path, None)
except:
- self.log.error('utime %s: %s' % (self.path, str(sys.exc_value)))
+ self.log.error('utime %s: %s' % (self.pathN, str(sys.exc_value)))
else:
self.lines = []
self.is_modified = 1
file = open(self.path, 'w')
file.close()
- self.log.info(_('touched file %s') % (self.path,))
+ self.log.info(_('touched file %s') % (self.pathN,))
elif self.sym_link:
+ if not (type(self.sym_link) == unicode):
+ self.sym_linkN = Narg(self.sym_link)
+ else:
+ self.sym_linkN = self.sym_link
done = 0
if self.exists():
full = os.lstat(self.path)
@@ -404,17 +413,21 @@ class ConfigFile:
try:
os.unlink(self.path)
except:
- self.log.error('unlink %s: %s' % (self.path, str(sys.exc_value)))
- self.log.info(_('deleted %s') % (self.path,))
+ self.log.error('unlink %s: %s' % (self.pathN, str(sys.exc_value)))
+ self.log.info(_('deleted %s') % (self.pathN,))
if not done:
try:
os.symlink(self.sym_link, self.path)
except:
- self.log.error('symlink %s %s: %s' % (self.sym_link, self.path, str(sys.exc_value)))
- self.log.info(_('made symbolic link from %s to %s') % (self.sym_link, self.path))
+ self.log.error('symlink %s %s: %s' % (self.sym_linkN, self.path, str(sys.exc_value)))
+ self.log.info(_('made symbolic link from %s to %s') % (self.sym_linkN, self.path))
elif self.is_moved:
+ if not (type(self.suffix) == unicode):
+ self.suffixN = Narg(self.suffix)
+ else:
+ self.suffixN = self.suffix
move(self.path, self.path + self.suffix)
- self.log.info(_('moved file %s to %s') % (self.path, self.path + self.suffix))
+ self.log.info(_('moved file %s to %s') % (self.path, self.path + self.suffixN))
self.meta.modified(self.path)
elif self.is_modified:
content = string.join(self.lines, "\n")
@@ -682,7 +695,7 @@ class MSEC:
self.plugins[plugin_name] = plugin
self.log.debug("Loaded plugin '%s'" % plugin_f)
except:
- self.log.error(_("Error loading plugin '%s' from %s: %s") % (plugin_f, file, sys.exc_value))
+ self.log.error(_("Error loading plugin '%s' from %s: %s") % (plugin_f, file, Narg(sys.exc_value)))
def reset(self):
"""Resets the configuration"""
@@ -699,7 +712,7 @@ class MSEC:
plugin_, callback = name.split(".", 1)
except:
# bad format?
- self.log.error(_("Invalid callback: %s") % (name))
+ self.log.error(_("Invalid callback: %s") % (Narg(name)))
return None
# is it a main function or a plugin?
if plugin_ == config.MAIN_LIB:
@@ -708,14 +721,14 @@ class MSEC:
if plugin_ in self.plugins:
plugin = self.plugins[plugin_]
else:
- self.log.info(_("Plugin %s not found") % plugin_)
+ self.log.info(_("Plugin %s not found") % to_utf8(plugin_))
return self.log.info
return None
try:
func = getattr(plugin, callback)
return func
except:
- self.log.info(_("Not supported function '%s' in '%s'") % (callback, plugin))
+ self.log.info(_("Not supported function '%s' in '%s'") % (Narg(callback), Narg(plugin)))
traceback.print_exc()
return None
@@ -871,7 +884,7 @@ class PERMS:
try:
os.chown(file, newuser, -1)
except:
- self.log.error(_("Error changing user on %s: %s") % (file, sys.exc_value))
+ self.log.error(_("Error changing user on %s: %s") % (file, Narg(sys.exc_value)))
else:
self.log.warn(_("Wrong owner of %s: should be %s") % (file, self.get_user_name(newuser)))
if newgroup != None:
@@ -880,7 +893,7 @@ class PERMS:
try:
os.chown(file, -1, newgroup)
except:
- self.log.error(_("Error changing group on %s: %s") % (file, sys.exc_value))
+ self.log.error(_("Error changing group on %s: %s") % (file, Narg(sys.exc_value)))
else:
self.log.warn(_("Wrong group of %s: should be %s") % (file, self.get_group_name(newgroup)))
# permissions should be last, as chown resets them
@@ -891,7 +904,7 @@ class PERMS:
try:
os.chmod(file, newperm)
except:
- self.log.error(_("Error changing permissions on %s: %s") % (file, sys.exc_value))
+ self.log.error(_("Error changing permissions on %s: %s") % (file, Narg(sys.exc_value)))
else:
self.log.warn(_("Wrong permissions of %s: should be %o") % (file, newperm))
@@ -913,7 +926,7 @@ class PERMS:
# problem setting setfacl
self.log.error(_("Unable to add filesystem-specific ACL %s to %s") % (acluser, file))
except:
- self.log.error(_("Error changing acl on %s: %s") % (file, sys.exc_value))
+ self.log.error(_("Error changing acl on %s: %s") % (file, Narg(str(sys.exc_value))))
else:
self.log.warn(_("Wrong acl of %s") % (file))
diff --git a/src/msec/msecgui.py b/src/msec/msecgui.py
index 7a2ceb7..d468b15 100755
--- a/src/msec/msecgui.py
+++ b/src/msec/msecgui.py
@@ -14,6 +14,8 @@ from textwrap import wrap
from threading import Thread
import time
+from config import Narg
+
# PyGTK
import warnings
warnings.filterwarnings('error', module='gtk')
@@ -379,14 +381,14 @@ class MsecGui:
# TODO: FIX
for name, changes in allchanges:
- label = Gtk.Label(label=_('<b>%s:</b> <i>%s</i>\n') % (name, changes))
+ label = Gtk.Label(label=_('<b>%s:</b> <i>%s</i>\n') % (Narg(name), Narg(changes)))
label.set_use_markup(True)
label.set_property("xalign", 0.0)
vbox.pack_start(label, False, False, padding=DEFAULT_SPACING)
# see if there were any changes to system files
for msg in messages['info']:
if msg.find(config.MODIFICATIONS_FOUND) != -1 or msg.find(config.MODIFICATIONS_NOT_FOUND) != -1:
- label = Gtk.Label(label=_("<b>MSEC test run results:</b> <i>%s</i>") % msg)
+ label = Gtk.Label(label=_("<b>MSEC test run results:</b> <i>%s</i>") % Narg(msg))
label.set_line_wrap(True)
label.set_use_markup(True)
label.set_property("xalign", 0.0)
@@ -659,7 +661,7 @@ class MsecGui:
for check, logfile, updated_n, updated in tools.periodic_check_status(log):
if not updated:
updated = _("Never")
- label = Gtk.Label(label=_("Check: %s. Last run: %s") % (check, updated))
+ label = Gtk.Label(label=_("Check: %s. Last run: %s") % (Narg(check), Narg(updated)))
label.set_property("xalign", 0.0)
table.attach(label, 2, 3, row, row + 1, Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL, 0, 0, 0)
@@ -691,7 +693,7 @@ class MsecGui:
with open(logfile, "r") as fd:
data = fd.readlines()
except:
- data = [_("Unable to read log file: %s") % sys.exc_value]
+ data = [_("Unable to read log file: %s") % Narg(sys.exc_value)]
dialog = Gtk.Dialog(_("Periodic check results"),
self.window,
0,
@@ -724,7 +726,7 @@ class MsecGui:
flags=0,
type=Gtk.MessageType.INFO,
buttons=Gtk.ButtonsType.YES_NO)
- dialog.set_markup(_("Do you want to run the <b>%s</b> periodic check? Please note that it could take a considerable time to finish.") % check)
+ dialog.set_markup(_("Do you want to run the <b>%s</b> periodic check? Please note that it could take a considerable time to finish.") % Narg(check))
dialog.show_all()
ret = dialog.run()
dialog.destroy()
@@ -1643,6 +1645,11 @@ class MsecGui:
val_def = conf_def.get(param)
+ for i in [param, descr, value]:
+ i = Narg(i)
+ # if not (type(i) == unicode) and not config.try_decode(i):
+ # i = i.decode(config.SYS_ENC)
+
# asks for new parameter value
dialog = Gtk.Dialog(_("Select new value for %s") % (param),
self.window, 0,
diff --git a/src/msec/tools.py b/src/msec/tools.py
index 3a079cd..cb9d1bf 100755
--- a/src/msec/tools.py
+++ b/src/msec/tools.py
@@ -9,6 +9,8 @@ import sys
import time
import locale
+from config import Narg, SYS_ENC
+
# localization
import gettext
try:
@@ -30,7 +32,7 @@ def find_firewall_info(log):
if l[:3] == "-A ":
firewall_entries.append(l.strip())
except:
- log.error(_("Unable to parse firewall configuration: %s") % sys.exc_value)
+ log.error(_("Unable to parse firewall configuration: %s") % Narg(sys.exc_value))
# not find out if the firewall is enabled
if len(firewall_entries) == 0:
firewall_status = _("Disabled")
@@ -44,10 +46,9 @@ def get_updates_status(log, updatedir="/var/lib/urpmi"):
try:
ret = os.stat(updatedir)
updated = time.localtime(ret[stat.ST_MTIME])
- updated_s = time.strftime(locale.nl_langinfo(locale.D_T_FMT), updated)
- status = _("Last updated: %s") % updated_s
+ status = _("Last updated: %s") % Narg(updated_s)
except:
- log.error(_("Unable to access %s: %s") % (updatedir, sys.exc_value))
+ log.error(_("Unable to access %s: %s") % (updatedir, Narg(sys.exc_value)))
status = _("Unable to determine update status")
return status