[1144] | 1 | # PyKota |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
[695] | 3 | |
---|
[952] | 4 | # PyKota - Print Quotas for CUPS and LPRng |
---|
[695] | 5 | # |
---|
[3133] | 6 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
[695] | 11 | # |
---|
[873] | 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
[2302] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[695] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[2093] | 23 | # |
---|
[695] | 24 | |
---|
| 25 | import sys |
---|
[1193] | 26 | import os |
---|
[1960] | 27 | import pwd |
---|
[817] | 28 | import fnmatch |
---|
[715] | 29 | import getopt |
---|
[695] | 30 | import smtplib |
---|
[772] | 31 | import gettext |
---|
[782] | 32 | import locale |
---|
[1469] | 33 | import socket |
---|
[2783] | 34 | import time |
---|
[2643] | 35 | from email.MIMEText import MIMEText |
---|
| 36 | from email.Header import Header |
---|
[3013] | 37 | import email.Utils |
---|
[695] | 38 | |
---|
[708] | 39 | from mx import DateTime |
---|
| 40 | |
---|
[3006] | 41 | try : |
---|
| 42 | import chardet |
---|
| 43 | except ImportError : |
---|
| 44 | def detectCharset(text) : |
---|
| 45 | """Fakes a charset detection if the chardet module is not installed.""" |
---|
| 46 | return "ISO-8859-15" |
---|
| 47 | else : |
---|
| 48 | def detectCharset(text) : |
---|
| 49 | """Uses the chardet module to workaround CUPS lying to us.""" |
---|
| 50 | return chardet.detect(text)["encoding"] |
---|
| 51 | |
---|
[2830] | 52 | from pykota import config, storage, logger |
---|
[2344] | 53 | from pykota.version import __version__, __author__, __years__, __gplblurb__ |
---|
[695] | 54 | |
---|
[1795] | 55 | def N_(message) : |
---|
| 56 | """Fake translation marker for translatable strings extraction.""" |
---|
| 57 | return message |
---|
| 58 | |
---|
[695] | 59 | class PyKotaToolError(Exception): |
---|
[2512] | 60 | """An exception for PyKota related stuff.""" |
---|
[695] | 61 | def __init__(self, message = ""): |
---|
| 62 | self.message = message |
---|
| 63 | Exception.__init__(self, message) |
---|
| 64 | def __repr__(self): |
---|
| 65 | return self.message |
---|
| 66 | __str__ = __repr__ |
---|
| 67 | |
---|
[2512] | 68 | class PyKotaCommandLineError(PyKotaToolError) : |
---|
| 69 | """An exception for Pykota command line tools.""" |
---|
| 70 | pass |
---|
| 71 | |
---|
[2229] | 72 | def crashed(message="Bug in PyKota") : |
---|
[1546] | 73 | """Minimal crash method.""" |
---|
| 74 | import traceback |
---|
| 75 | lines = [] |
---|
| 76 | for line in traceback.format_exception(*sys.exc_info()) : |
---|
| 77 | lines.extend([l for l in line.split("\n") if l]) |
---|
[2344] | 78 | msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKota v%s" % __version__, message] + lines)]) |
---|
[1546] | 79 | sys.stderr.write(msg) |
---|
| 80 | sys.stderr.flush() |
---|
| 81 | return msg |
---|
| 82 | |
---|
[2782] | 83 | class Percent : |
---|
| 84 | """A class to display progress.""" |
---|
[2783] | 85 | def __init__(self, app, size=None) : |
---|
[2782] | 86 | """Initializes the engine.""" |
---|
| 87 | self.app = app |
---|
[2783] | 88 | self.size = None |
---|
| 89 | if size : |
---|
| 90 | self.setSize(size) |
---|
| 91 | self.previous = None |
---|
| 92 | self.before = time.time() |
---|
| 93 | |
---|
| 94 | def setSize(self, size) : |
---|
| 95 | """Sets the total size.""" |
---|
| 96 | self.number = 0 |
---|
[2782] | 97 | self.size = size |
---|
[2793] | 98 | if size : |
---|
| 99 | self.factor = 100.0 / float(size) |
---|
[2782] | 100 | |
---|
| 101 | def display(self, msg) : |
---|
| 102 | """Displays the value.""" |
---|
| 103 | self.app.display(msg) |
---|
| 104 | |
---|
| 105 | def oneMore(self) : |
---|
| 106 | """Increments internal counter.""" |
---|
[2783] | 107 | if self.size : |
---|
| 108 | self.number += 1 |
---|
| 109 | percent = "%.02f" % (float(self.number) * self.factor) |
---|
| 110 | if percent != self.previous : # optimize for large number of items |
---|
| 111 | self.display("\r%s%%" % percent) |
---|
| 112 | self.previous = percent |
---|
[2782] | 113 | |
---|
| 114 | def done(self) : |
---|
| 115 | """Displays the 'done' message.""" |
---|
[2783] | 116 | after = time.time() |
---|
| 117 | if self.size : |
---|
[2788] | 118 | speed = self.size / (after - self.before) |
---|
| 119 | self.display("\r100.00%%\r \r%s. %s : %.2f %s.\n" \ |
---|
[2783] | 120 | % (_("Done"), _("Average speed"), speed, _("entries per second"))) |
---|
| 121 | else : |
---|
| 122 | self.display("\r100.00%%\r \r%s.\n" % _("Done")) |
---|
[2782] | 123 | |
---|
[1911] | 124 | class Tool : |
---|
| 125 | """Base class for tools with no database access.""" |
---|
[2344] | 126 | def __init__(self, lang="", charset=None, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") : |
---|
[695] | 127 | """Initializes the command line tool.""" |
---|
[2006] | 128 | # did we drop priviledges ? |
---|
| 129 | self.privdropped = 0 |
---|
| 130 | |
---|
[772] | 131 | # locale stuff |
---|
[788] | 132 | try : |
---|
[3055] | 133 | locale.setlocale(locale.LC_ALL, (lang, charset)) |
---|
[788] | 134 | except (locale.Error, IOError) : |
---|
[3055] | 135 | locale.setlocale(locale.LC_ALL, None) |
---|
| 136 | (self.language, self.charset) = locale.getlocale() |
---|
| 137 | self.language = self.language or "C" |
---|
[3061] | 138 | try : |
---|
| 139 | self.charset = self.charset or locale.getpreferredencoding() |
---|
| 140 | except locale.Error : |
---|
| 141 | self.charset = sys.getfilesystemencoding() |
---|
[3055] | 142 | |
---|
| 143 | # translation stuff |
---|
[1898] | 144 | try : |
---|
[3055] | 145 | try : |
---|
| 146 | trans = gettext.translation("pykota", languages=["%s.%s" % (self.language, self.charset)], codeset=self.charset) |
---|
| 147 | except TypeError : # Python <2.4 |
---|
| 148 | trans = gettext.translation("pykota", languages=["%s.%s" % (self.language, self.charset)]) |
---|
| 149 | trans.install() |
---|
[1898] | 150 | except : |
---|
[788] | 151 | gettext.NullTranslations().install() |
---|
[772] | 152 | |
---|
| 153 | # pykota specific stuff |
---|
[715] | 154 | self.documentation = doc |
---|
[1960] | 155 | |
---|
[2210] | 156 | def deferredInit(self) : |
---|
| 157 | """Deferred initialization.""" |
---|
[3045] | 158 | confdir = os.environ.get("PYKOTA_HOME") |
---|
| 159 | environHome = True |
---|
| 160 | missingUser = False |
---|
| 161 | if confdir is None : |
---|
| 162 | environHome = False |
---|
| 163 | # check for config files in the 'pykota' user's home directory. |
---|
| 164 | try : |
---|
| 165 | self.pykotauser = pwd.getpwnam("pykota") |
---|
| 166 | confdir = self.pykotauser[5] |
---|
| 167 | except KeyError : |
---|
| 168 | self.pykotauser = None |
---|
| 169 | confdir = "/etc/pykota" |
---|
| 170 | missingUser = True |
---|
[1960] | 171 | |
---|
[2210] | 172 | self.config = config.PyKotaConfig(confdir) |
---|
| 173 | self.debug = self.config.getDebug() |
---|
| 174 | self.smtpserver = self.config.getSMTPServer() |
---|
| 175 | self.maildomain = self.config.getMailDomain() |
---|
| 176 | self.logger = logger.openLogger(self.config.getLoggingBackend()) |
---|
[1542] | 177 | |
---|
[2006] | 178 | # now drop priviledge if possible |
---|
| 179 | self.dropPriv() |
---|
| 180 | |
---|
[1960] | 181 | # We NEED this here, even when not in an accounting filter/backend |
---|
[1497] | 182 | self.softwareJobSize = 0 |
---|
[1495] | 183 | self.softwareJobPrice = 0.0 |
---|
[1960] | 184 | |
---|
[3045] | 185 | if environHome : |
---|
| 186 | self.printInfo("PYKOTA_HOME environment variable is set. Configuration files were searched in %s" % confdir, "info") |
---|
| 187 | else : |
---|
| 188 | if missingUser : |
---|
| 189 | self.printInfo("The 'pykota' system account is missing. Configuration files were searched in %s instead." % confdir, "warn") |
---|
[1960] | 190 | |
---|
[3055] | 191 | self.logdebug("Language in use : %s" % self.language) |
---|
[1761] | 192 | self.logdebug("Charset in use : %s" % self.charset) |
---|
[3055] | 193 | |
---|
[1872] | 194 | arguments = " ".join(['"%s"' % arg for arg in sys.argv]) |
---|
| 195 | self.logdebug("Command line arguments : %s" % arguments) |
---|
[695] | 196 | |
---|
[2006] | 197 | def dropPriv(self) : |
---|
| 198 | """Drops priviledges.""" |
---|
| 199 | uid = os.geteuid() |
---|
[2889] | 200 | try : |
---|
| 201 | self.originalUserName = pwd.getpwuid(uid)[0] |
---|
| 202 | except (KeyError, IndexError), msg : |
---|
| 203 | self.printInfo(_("Strange problem with uid(%s) : %s") % (uid, msg), "warn") |
---|
| 204 | self.originalUserName = None |
---|
| 205 | else : |
---|
| 206 | if uid : |
---|
| 207 | self.logdebug(_("Running as user '%s'.") % self.originalUserName) |
---|
[2006] | 208 | else : |
---|
[2889] | 209 | if self.pykotauser is None : |
---|
| 210 | self.logdebug(_("No user named 'pykota'. Not dropping priviledges.")) |
---|
[2006] | 211 | else : |
---|
[2889] | 212 | try : |
---|
| 213 | os.setegid(self.pykotauser[3]) |
---|
| 214 | os.seteuid(self.pykotauser[2]) |
---|
| 215 | except OSError, msg : |
---|
| 216 | self.printInfo(_("Impossible to drop priviledges : %s") % msg, "warn") |
---|
| 217 | else : |
---|
| 218 | self.logdebug(_("Priviledges dropped. Now running as user 'pykota'.")) |
---|
| 219 | self.privdropped = 1 |
---|
[2006] | 220 | |
---|
| 221 | def regainPriv(self) : |
---|
| 222 | """Drops priviledges.""" |
---|
| 223 | if self.privdropped : |
---|
| 224 | try : |
---|
| 225 | os.seteuid(0) |
---|
| 226 | os.setegid(0) |
---|
| 227 | except OSError, msg : |
---|
| 228 | self.printInfo(_("Impossible to regain priviledges : %s") % msg, "warn") |
---|
| 229 | else : |
---|
[2008] | 230 | self.logdebug(_("Regained priviledges. Now running as root.")) |
---|
[2006] | 231 | self.privdropped = 0 |
---|
| 232 | |
---|
[2804] | 233 | def UTF8ToUserCharset(self, text) : |
---|
| 234 | """Converts from UTF-8 to user's charset.""" |
---|
[3006] | 235 | if text is None : |
---|
| 236 | return None |
---|
| 237 | try : |
---|
| 238 | return text.decode("UTF-8").encode(self.charset, "replace") |
---|
| 239 | except (UnicodeError, AttributeError) : |
---|
[2804] | 240 | try : |
---|
[3006] | 241 | # Maybe already in Unicode ? |
---|
| 242 | return text.encode(self.charset, "replace") |
---|
| 243 | except (UnicodeError, AttributeError) : |
---|
| 244 | # Try to autodetect the charset |
---|
| 245 | return text.decode(detectCharset(text), "replace").encode(self.charset, "replace") |
---|
[2804] | 246 | |
---|
| 247 | def userCharsetToUTF8(self, text) : |
---|
| 248 | """Converts from user's charset to UTF-8.""" |
---|
[3006] | 249 | if text is None : |
---|
| 250 | return None |
---|
| 251 | try : |
---|
| 252 | # We don't necessarily trust the default charset, because |
---|
| 253 | # xprint sends us titles in UTF-8 but CUPS gives us an ISO-8859-1 charset ! |
---|
| 254 | # So we first try to see if the text is already in UTF-8 or not, and |
---|
| 255 | # if it is, we delete characters which can't be converted to the user's charset, |
---|
| 256 | # then convert back to UTF-8. PostgreSQL 7.3.x used to reject some unicode characters, |
---|
| 257 | # this is fixed by the ugly line below : |
---|
| 258 | return text.decode("UTF-8").encode(self.charset, "replace").decode(self.charset).encode("UTF-8", "replace") |
---|
| 259 | except (UnicodeError, AttributeError) : |
---|
[2804] | 260 | try : |
---|
[3006] | 261 | return text.decode(self.charset).encode("UTF-8", "replace") |
---|
| 262 | except (UnicodeError, AttributeError) : |
---|
[2804] | 263 | try : |
---|
[3006] | 264 | # Maybe already in Unicode ? |
---|
| 265 | return text.encode("UTF-8", "replace") |
---|
| 266 | except (UnicodeError, AttributeError) : |
---|
| 267 | # Try to autodetect the charset |
---|
| 268 | return text.decode(detectCharset(text), "replace").encode("UTF-8", "replace") |
---|
| 269 | return newtext |
---|
[2804] | 270 | |
---|
[2657] | 271 | def display(self, message) : |
---|
| 272 | """Display a message but only if stdout is a tty.""" |
---|
| 273 | if sys.stdout.isatty() : |
---|
| 274 | sys.stdout.write(message) |
---|
| 275 | sys.stdout.flush() |
---|
| 276 | |
---|
[1130] | 277 | def logdebug(self, message) : |
---|
| 278 | """Logs something to debug output if debug is enabled.""" |
---|
| 279 | if self.debug : |
---|
| 280 | self.logger.log_message(message, "debug") |
---|
[1582] | 281 | |
---|
[1584] | 282 | def printInfo(self, message, level="info") : |
---|
[1582] | 283 | """Sends a message to standard error.""" |
---|
[1584] | 284 | sys.stderr.write("%s: %s\n" % (level.upper(), message)) |
---|
[1582] | 285 | sys.stderr.flush() |
---|
[1130] | 286 | |
---|
[2210] | 287 | def matchString(self, s, patterns) : |
---|
[2650] | 288 | """Returns True if the string s matches one of the patterns, else False.""" |
---|
| 289 | if not patterns : |
---|
| 290 | return True # No pattern, always matches. |
---|
| 291 | else : |
---|
| 292 | for pattern in patterns : |
---|
| 293 | if fnmatch.fnmatchcase(s, pattern) : |
---|
| 294 | return True |
---|
| 295 | return False |
---|
[2210] | 296 | |
---|
[2762] | 297 | def sanitizeNames(self, options, names) : |
---|
| 298 | """Ensures that an user can only see the datas he is allowed to see, by modifying the list of names.""" |
---|
| 299 | if not self.config.isAdmin : |
---|
| 300 | username = pwd.getpwuid(os.geteuid())[0] |
---|
| 301 | if not options["list"] : |
---|
| 302 | raise PyKotaCommandLineError, "%s : %s" % (username, _("You're not allowed to use this command.")) |
---|
| 303 | else : |
---|
| 304 | if options["groups"] : |
---|
| 305 | user = self.storage.getUser(username) |
---|
| 306 | if user.Exists : |
---|
| 307 | return [ g.Name for g in self.storage.getUserGroups(user) ] |
---|
| 308 | return [ username ] |
---|
| 309 | elif not names : |
---|
| 310 | return ["*"] |
---|
| 311 | else : |
---|
| 312 | return names |
---|
| 313 | |
---|
[715] | 314 | def display_version_and_quit(self) : |
---|
| 315 | """Displays version number, then exists successfully.""" |
---|
[1923] | 316 | try : |
---|
| 317 | self.clean() |
---|
| 318 | except AttributeError : |
---|
| 319 | pass |
---|
[2344] | 320 | print __version__ |
---|
[715] | 321 | sys.exit(0) |
---|
| 322 | |
---|
| 323 | def display_usage_and_quit(self) : |
---|
| 324 | """Displays command line usage, then exists successfully.""" |
---|
[1923] | 325 | try : |
---|
| 326 | self.clean() |
---|
| 327 | except AttributeError : |
---|
| 328 | pass |
---|
[2344] | 329 | print _(self.documentation) % globals() |
---|
| 330 | print __gplblurb__ |
---|
| 331 | print |
---|
| 332 | print _("Please report bugs to :"), __author__ |
---|
[715] | 333 | sys.exit(0) |
---|
| 334 | |
---|
[2229] | 335 | def crashed(self, message="Bug in PyKota") : |
---|
[1517] | 336 | """Outputs a crash message, and optionally sends it to software author.""" |
---|
[1546] | 337 | msg = crashed(message) |
---|
[2229] | 338 | fullmessage = "========== Traceback :\n\n%s\n\n========== sys.argv :\n\n%s\n\n========== Environment :\n\n%s\n" % \ |
---|
| 339 | (msg, \ |
---|
| 340 | "\n".join([" %s" % repr(a) for a in sys.argv]), \ |
---|
| 341 | "\n".join([" %s=%s" % (k, v) for (k, v) in os.environ.items()])) |
---|
[1541] | 342 | try : |
---|
| 343 | crashrecipient = self.config.getCrashRecipient() |
---|
| 344 | if crashrecipient : |
---|
[1517] | 345 | admin = self.config.getAdminMail("global") # Nice trick, isn't it ? |
---|
| 346 | server = smtplib.SMTP(self.smtpserver) |
---|
[2795] | 347 | msg = MIMEText(fullmessage, _charset=self.charset) |
---|
| 348 | msg["Subject"] = str(Header("PyKota v%s crash traceback !" \ |
---|
| 349 | % __version__, charset=self.charset)) |
---|
| 350 | msg["From"] = admin |
---|
| 351 | msg["To"] = crashrecipient |
---|
| 352 | msg["Cc"] = admin |
---|
[3013] | 353 | msg["Date"] = email.Utils.formatdate(localtime=True) |
---|
[2795] | 354 | server.sendmail(admin, [admin, crashrecipient], msg.as_string()) |
---|
[1517] | 355 | server.quit() |
---|
[1541] | 356 | except : |
---|
| 357 | pass |
---|
[2229] | 358 | return fullmessage |
---|
[1517] | 359 | |
---|
[729] | 360 | def parseCommandline(self, argv, short, long, allownothing=0) : |
---|
[715] | 361 | """Parses the command line, controlling options.""" |
---|
| 362 | # split options in two lists: those which need an argument, those which don't need any |
---|
[2605] | 363 | short = "%sA:" % short |
---|
| 364 | long.append("arguments=") |
---|
[715] | 365 | withoutarg = [] |
---|
| 366 | witharg = [] |
---|
| 367 | lgs = len(short) |
---|
| 368 | i = 0 |
---|
| 369 | while i < lgs : |
---|
| 370 | ii = i + 1 |
---|
| 371 | if (ii < lgs) and (short[ii] == ':') : |
---|
| 372 | # needs an argument |
---|
| 373 | witharg.append(short[i]) |
---|
| 374 | ii = ii + 1 # skip the ':' |
---|
| 375 | else : |
---|
| 376 | # doesn't need an argument |
---|
| 377 | withoutarg.append(short[i]) |
---|
| 378 | i = ii |
---|
| 379 | |
---|
| 380 | for option in long : |
---|
| 381 | if option[-1] == '=' : |
---|
| 382 | # needs an argument |
---|
| 383 | witharg.append(option[:-1]) |
---|
| 384 | else : |
---|
| 385 | # doesn't need an argument |
---|
| 386 | withoutarg.append(option) |
---|
| 387 | |
---|
| 388 | # then we parse the command line |
---|
[2605] | 389 | done = 0 |
---|
| 390 | while not done : |
---|
| 391 | # we begin with all possible options unset |
---|
| 392 | parsed = {} |
---|
| 393 | for option in withoutarg + witharg : |
---|
| 394 | parsed[option] = None |
---|
| 395 | args = [] # to not break if something unexpected happened |
---|
| 396 | try : |
---|
| 397 | options, args = getopt.getopt(argv, short, long) |
---|
| 398 | if options : |
---|
| 399 | for (o, v) in options : |
---|
| 400 | # we skip the '-' chars |
---|
| 401 | lgo = len(o) |
---|
| 402 | i = 0 |
---|
| 403 | while (i < lgo) and (o[i] == '-') : |
---|
| 404 | i = i + 1 |
---|
| 405 | o = o[i:] |
---|
| 406 | if o in witharg : |
---|
| 407 | # needs an argument : set it |
---|
| 408 | parsed[o] = v |
---|
| 409 | elif o in withoutarg : |
---|
| 410 | # doesn't need an argument : boolean |
---|
| 411 | parsed[o] = 1 |
---|
| 412 | else : |
---|
| 413 | # should never occur |
---|
[2610] | 414 | raise PyKotaCommandLineError, "Unexpected problem when parsing command line" |
---|
[2605] | 415 | elif (not args) and (not allownothing) and sys.stdin.isatty() : # no option and no argument, we display help if we are a tty |
---|
| 416 | self.display_usage_and_quit() |
---|
| 417 | except getopt.error, msg : |
---|
[2610] | 418 | raise PyKotaCommandLineError, str(msg) |
---|
[2605] | 419 | else : |
---|
| 420 | if parsed["arguments"] or parsed["A"] : |
---|
| 421 | # arguments are in a file, we ignore all other arguments |
---|
| 422 | # and reset the list of arguments to the lines read from |
---|
| 423 | # the file. |
---|
| 424 | argsfile = open(parsed["arguments"] or parsed["A"], "r") |
---|
| 425 | argv = [ l.strip() for l in argsfile.readlines() ] |
---|
| 426 | argsfile.close() |
---|
| 427 | for i in range(len(argv)) : |
---|
| 428 | argi = argv[i] |
---|
| 429 | if argi.startswith('"') and argi.endswith('"') : |
---|
| 430 | argv[i] = argi[1:-1] |
---|
| 431 | else : |
---|
| 432 | done = 1 |
---|
[715] | 433 | return (parsed, args) |
---|
| 434 | |
---|
[1911] | 435 | class PyKotaTool(Tool) : |
---|
| 436 | """Base class for all PyKota command line tools.""" |
---|
[2344] | 437 | def __init__(self, lang="", charset=None, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") : |
---|
[1911] | 438 | """Initializes the command line tool and opens the database.""" |
---|
| 439 | Tool.__init__(self, lang, charset, doc) |
---|
[2210] | 440 | |
---|
| 441 | def deferredInit(self) : |
---|
| 442 | """Deferred initialization.""" |
---|
| 443 | Tool.deferredInit(self) |
---|
| 444 | self.storage = storage.openConnection(self) |
---|
| 445 | if self.config.isAdmin : # TODO : We don't know this before, fix this ! |
---|
| 446 | self.logdebug("Beware : running as a PyKota administrator !") |
---|
[2093] | 447 | else : |
---|
[2210] | 448 | self.logdebug("Don't Panic : running as a mere mortal !") |
---|
[1911] | 449 | |
---|
| 450 | def clean(self) : |
---|
| 451 | """Ensures that the database is closed.""" |
---|
| 452 | try : |
---|
| 453 | self.storage.close() |
---|
| 454 | except (TypeError, NameError, AttributeError) : |
---|
| 455 | pass |
---|
| 456 | |
---|
[764] | 457 | def isValidName(self, name) : |
---|
| 458 | """Checks if a user or printer name is valid.""" |
---|
[1725] | 459 | invalidchars = "/@?*,;&|" |
---|
[1637] | 460 | for c in list(invalidchars) : |
---|
[1593] | 461 | if c in name : |
---|
[1394] | 462 | return 0 |
---|
| 463 | return 1 |
---|
[764] | 464 | |
---|
[802] | 465 | def sendMessage(self, adminmail, touser, fullmessage) : |
---|
[695] | 466 | """Sends an email message containing headers to some user.""" |
---|
[1469] | 467 | try : |
---|
| 468 | server = smtplib.SMTP(self.smtpserver) |
---|
| 469 | except socket.error, msg : |
---|
[1584] | 470 | self.printInfo(_("Impossible to connect to SMTP server : %s") % msg, "error") |
---|
[1469] | 471 | else : |
---|
| 472 | try : |
---|
[2795] | 473 | server.sendmail(adminmail, [touser], fullmessage) |
---|
[1469] | 474 | except smtplib.SMTPException, answer : |
---|
| 475 | for (k, v) in answer.recipients.items() : |
---|
[1584] | 476 | self.printInfo(_("Impossible to send mail to %s, error %s : %s") % (k, v[0], v[1]), "error") |
---|
[1469] | 477 | server.quit() |
---|
[695] | 478 | |
---|
[1079] | 479 | def sendMessageToUser(self, admin, adminmail, user, subject, message) : |
---|
[695] | 480 | """Sends an email message to a user.""" |
---|
[802] | 481 | message += _("\n\nPlease contact your system administrator :\n\n\t%s - <%s>\n") % (admin, adminmail) |
---|
[2795] | 482 | usermail = user.Email or user.Name |
---|
| 483 | if "@" not in usermail : |
---|
| 484 | usermail = "%s@%s" % (usermail, self.maildomain or self.smtpserver or "localhost") |
---|
| 485 | msg = MIMEText(message, _charset=self.charset) |
---|
| 486 | msg["Subject"] = str(Header(subject, charset=self.charset)) |
---|
| 487 | msg["From"] = adminmail |
---|
| 488 | msg["To"] = usermail |
---|
[3013] | 489 | msg["Date"] = email.Utils.formatdate(localtime=True) |
---|
[2795] | 490 | self.sendMessage(adminmail, usermail, msg.as_string()) |
---|
[695] | 491 | |
---|
[802] | 492 | def sendMessageToAdmin(self, adminmail, subject, message) : |
---|
[695] | 493 | """Sends an email message to the Print Quota administrator.""" |
---|
[2795] | 494 | if "@" not in adminmail : |
---|
| 495 | adminmail = "%s@%s" % (adminmail, self.maildomain or self.smtpserver or "localhost") |
---|
| 496 | msg = MIMEText(message, _charset=self.charset) |
---|
| 497 | msg["Subject"] = str(Header(subject, charset=self.charset)) |
---|
| 498 | msg["From"] = adminmail |
---|
| 499 | msg["To"] = adminmail |
---|
| 500 | self.sendMessage(adminmail, adminmail, msg.as_string()) |
---|
[695] | 501 | |
---|
[1365] | 502 | def _checkUserPQuota(self, userpquota) : |
---|
| 503 | """Checks the user quota on a printer and deny or accept the job.""" |
---|
| 504 | # then we check the user's own quota |
---|
| 505 | # if we get there we are sure that policy is not EXTERNAL |
---|
| 506 | user = userpquota.User |
---|
| 507 | printer = userpquota.Printer |
---|
[1495] | 508 | enforcement = self.config.getPrinterEnforcement(printer.Name) |
---|
[1365] | 509 | self.logdebug("Checking user %s's quota on printer %s" % (user.Name, printer.Name)) |
---|
| 510 | (policy, dummy) = self.config.getPrinterPolicy(userpquota.Printer.Name) |
---|
| 511 | if not userpquota.Exists : |
---|
| 512 | # Unknown userquota |
---|
| 513 | if policy == "ALLOW" : |
---|
| 514 | action = "POLICY_ALLOW" |
---|
| 515 | else : |
---|
| 516 | action = "POLICY_DENY" |
---|
[1584] | 517 | self.printInfo(_("Unable to match user %s on printer %s, applying default policy (%s)") % (user.Name, printer.Name, action)) |
---|
[1365] | 518 | else : |
---|
| 519 | pagecounter = int(userpquota.PageCounter or 0) |
---|
[1495] | 520 | if enforcement == "STRICT" : |
---|
| 521 | pagecounter += self.softwareJobSize |
---|
[1365] | 522 | if userpquota.SoftLimit is not None : |
---|
| 523 | softlimit = int(userpquota.SoftLimit) |
---|
| 524 | if pagecounter < softlimit : |
---|
| 525 | action = "ALLOW" |
---|
| 526 | else : |
---|
| 527 | if userpquota.HardLimit is None : |
---|
| 528 | # only a soft limit, this is equivalent to having only a hard limit |
---|
| 529 | action = "DENY" |
---|
| 530 | else : |
---|
| 531 | hardlimit = int(userpquota.HardLimit) |
---|
| 532 | if softlimit <= pagecounter < hardlimit : |
---|
| 533 | now = DateTime.now() |
---|
| 534 | if userpquota.DateLimit is not None : |
---|
[3050] | 535 | datelimit = DateTime.ISO.ParseDateTime(str(userpquota.DateLimit)[:19]) |
---|
[1365] | 536 | else : |
---|
| 537 | datelimit = now + self.config.getGraceDelay(printer.Name) |
---|
| 538 | userpquota.setDateLimit(datelimit) |
---|
| 539 | if now < datelimit : |
---|
| 540 | action = "WARN" |
---|
| 541 | else : |
---|
| 542 | action = "DENY" |
---|
| 543 | else : |
---|
| 544 | action = "DENY" |
---|
| 545 | else : |
---|
| 546 | if userpquota.HardLimit is not None : |
---|
| 547 | # no soft limit, only a hard one. |
---|
| 548 | hardlimit = int(userpquota.HardLimit) |
---|
| 549 | if pagecounter < hardlimit : |
---|
| 550 | action = "ALLOW" |
---|
| 551 | else : |
---|
| 552 | action = "DENY" |
---|
| 553 | else : |
---|
| 554 | # Both are unset, no quota, i.e. accounting only |
---|
| 555 | action = "ALLOW" |
---|
| 556 | return action |
---|
| 557 | |
---|
[1041] | 558 | def checkGroupPQuota(self, grouppquota) : |
---|
[927] | 559 | """Checks the group quota on a printer and deny or accept the job.""" |
---|
[1041] | 560 | group = grouppquota.Group |
---|
| 561 | printer = grouppquota.Printer |
---|
[1495] | 562 | enforcement = self.config.getPrinterEnforcement(printer.Name) |
---|
[1365] | 563 | self.logdebug("Checking group %s's quota on printer %s" % (group.Name, printer.Name)) |
---|
[1061] | 564 | if group.LimitBy and (group.LimitBy.lower() == "balance") : |
---|
[1666] | 565 | val = group.AccountBalance or 0.0 |
---|
[1495] | 566 | if enforcement == "STRICT" : |
---|
| 567 | val -= self.softwareJobPrice # use precomputed size. |
---|
[2692] | 568 | balancezero = self.config.getBalanceZero() |
---|
| 569 | if val <= balancezero : |
---|
[1041] | 570 | action = "DENY" |
---|
[1495] | 571 | elif val <= self.config.getPoorMan() : |
---|
[1077] | 572 | action = "WARN" |
---|
[927] | 573 | else : |
---|
[1041] | 574 | action = "ALLOW" |
---|
[2692] | 575 | if (enforcement == "STRICT") and (val == balancezero) : |
---|
[1529] | 576 | action = "WARN" # we can still print until account is 0 |
---|
[927] | 577 | else : |
---|
[1665] | 578 | val = grouppquota.PageCounter or 0 |
---|
[1495] | 579 | if enforcement == "STRICT" : |
---|
[2992] | 580 | val += int(self.softwareJobSize) # TODO : this is not a fix, problem is elsewhere in grouppquota.PageCounter |
---|
[1041] | 581 | if grouppquota.SoftLimit is not None : |
---|
| 582 | softlimit = int(grouppquota.SoftLimit) |
---|
[1495] | 583 | if val < softlimit : |
---|
[1041] | 584 | action = "ALLOW" |
---|
[927] | 585 | else : |
---|
[1041] | 586 | if grouppquota.HardLimit is None : |
---|
| 587 | # only a soft limit, this is equivalent to having only a hard limit |
---|
| 588 | action = "DENY" |
---|
[927] | 589 | else : |
---|
[1041] | 590 | hardlimit = int(grouppquota.HardLimit) |
---|
[1495] | 591 | if softlimit <= val < hardlimit : |
---|
[1041] | 592 | now = DateTime.now() |
---|
| 593 | if grouppquota.DateLimit is not None : |
---|
[3050] | 594 | datelimit = DateTime.ISO.ParseDateTime(str(grouppquota.DateLimit)[:19]) |
---|
[1041] | 595 | else : |
---|
| 596 | datelimit = now + self.config.getGraceDelay(printer.Name) |
---|
| 597 | grouppquota.setDateLimit(datelimit) |
---|
| 598 | if now < datelimit : |
---|
| 599 | action = "WARN" |
---|
| 600 | else : |
---|
[927] | 601 | action = "DENY" |
---|
[1041] | 602 | else : |
---|
[927] | 603 | action = "DENY" |
---|
[1041] | 604 | else : |
---|
| 605 | if grouppquota.HardLimit is not None : |
---|
| 606 | # no soft limit, only a hard one. |
---|
| 607 | hardlimit = int(grouppquota.HardLimit) |
---|
[1495] | 608 | if val < hardlimit : |
---|
[927] | 609 | action = "ALLOW" |
---|
[1041] | 610 | else : |
---|
| 611 | action = "DENY" |
---|
| 612 | else : |
---|
| 613 | # Both are unset, no quota, i.e. accounting only |
---|
| 614 | action = "ALLOW" |
---|
[927] | 615 | return action |
---|
| 616 | |
---|
[1041] | 617 | def checkUserPQuota(self, userpquota) : |
---|
[1365] | 618 | """Checks the user quota on a printer and all its parents and deny or accept the job.""" |
---|
[1041] | 619 | user = userpquota.User |
---|
| 620 | printer = userpquota.Printer |
---|
| 621 | |
---|
[1365] | 622 | # indicates that a warning needs to be sent |
---|
| 623 | warned = 0 |
---|
| 624 | |
---|
[927] | 625 | # first we check any group the user is a member of |
---|
[1041] | 626 | for group in self.storage.getUserGroups(user) : |
---|
[2452] | 627 | # No need to check anything if the group is in noquota mode |
---|
| 628 | if group.LimitBy != "noquota" : |
---|
| 629 | grouppquota = self.storage.getGroupPQuota(group, printer) |
---|
| 630 | # for the printer and all its parents |
---|
| 631 | for gpquota in [ grouppquota ] + grouppquota.ParentPrintersGroupPQuota : |
---|
| 632 | if gpquota.Exists : |
---|
| 633 | action = self.checkGroupPQuota(gpquota) |
---|
| 634 | if action == "DENY" : |
---|
| 635 | return action |
---|
| 636 | elif action == "WARN" : |
---|
| 637 | warned = 1 |
---|
[1365] | 638 | |
---|
| 639 | # Then we check the user's account balance |
---|
[1152] | 640 | # if we get there we are sure that policy is not EXTERNAL |
---|
| 641 | (policy, dummy) = self.config.getPrinterPolicy(printer.Name) |
---|
[1061] | 642 | if user.LimitBy and (user.LimitBy.lower() == "balance") : |
---|
[1365] | 643 | self.logdebug("Checking account balance for user %s" % user.Name) |
---|
[1041] | 644 | if user.AccountBalance is None : |
---|
[956] | 645 | if policy == "ALLOW" : |
---|
[925] | 646 | action = "POLICY_ALLOW" |
---|
| 647 | else : |
---|
| 648 | action = "POLICY_DENY" |
---|
[1584] | 649 | self.printInfo(_("Unable to find user %s's account balance, applying default policy (%s) for printer %s") % (user.Name, action, printer.Name)) |
---|
[1365] | 650 | return action |
---|
[713] | 651 | else : |
---|
[2054] | 652 | if user.OverCharge == 0.0 : |
---|
| 653 | self.printInfo(_("User %s will not be charged for printing.") % user.Name) |
---|
| 654 | action = "ALLOW" |
---|
[1529] | 655 | else : |
---|
[2054] | 656 | val = float(user.AccountBalance or 0.0) |
---|
| 657 | enforcement = self.config.getPrinterEnforcement(printer.Name) |
---|
| 658 | if enforcement == "STRICT" : |
---|
| 659 | val -= self.softwareJobPrice # use precomputed size. |
---|
[2692] | 660 | balancezero = self.config.getBalanceZero() |
---|
| 661 | if val <= balancezero : |
---|
[2054] | 662 | action = "DENY" |
---|
| 663 | elif val <= self.config.getPoorMan() : |
---|
| 664 | action = "WARN" |
---|
| 665 | else : |
---|
| 666 | action = "ALLOW" |
---|
[2692] | 667 | if (enforcement == "STRICT") and (val == balancezero) : |
---|
[2054] | 668 | action = "WARN" # we can still print until account is 0 |
---|
[1529] | 669 | return action |
---|
[925] | 670 | else : |
---|
[1365] | 671 | # Then check the user quota on current printer and all its parents. |
---|
| 672 | policyallowed = 0 |
---|
| 673 | for upquota in [ userpquota ] + userpquota.ParentPrintersUserPQuota : |
---|
| 674 | action = self._checkUserPQuota(upquota) |
---|
| 675 | if action in ("DENY", "POLICY_DENY") : |
---|
| 676 | return action |
---|
| 677 | elif action == "WARN" : |
---|
| 678 | warned = 1 |
---|
| 679 | elif action == "POLICY_ALLOW" : |
---|
| 680 | policyallowed = 1 |
---|
| 681 | if warned : |
---|
| 682 | return "WARN" |
---|
| 683 | elif policyallowed : |
---|
| 684 | return "POLICY_ALLOW" |
---|
[925] | 685 | else : |
---|
[1365] | 686 | return "ALLOW" |
---|
| 687 | |
---|
[1196] | 688 | def externalMailTo(self, cmd, action, user, printer, message) : |
---|
[1192] | 689 | """Warns the user with an external command.""" |
---|
| 690 | username = user.Name |
---|
[1196] | 691 | printername = printer.Name |
---|
[1192] | 692 | email = user.Email or user.Name |
---|
| 693 | if "@" not in email : |
---|
[1353] | 694 | email = "%s@%s" % (email, self.maildomain or self.smtpserver) |
---|
[1192] | 695 | os.system(cmd % locals()) |
---|
| 696 | |
---|
[1196] | 697 | def formatCommandLine(self, cmd, user, printer) : |
---|
| 698 | """Executes an external command.""" |
---|
| 699 | username = user.Name |
---|
| 700 | printername = printer.Name |
---|
| 701 | return cmd % locals() |
---|
| 702 | |
---|
[1041] | 703 | def warnGroupPQuota(self, grouppquota) : |
---|
[927] | 704 | """Checks a group quota and send messages if quota is exceeded on current printer.""" |
---|
[1041] | 705 | group = grouppquota.Group |
---|
| 706 | printer = grouppquota.Printer |
---|
| 707 | admin = self.config.getAdmin(printer.Name) |
---|
| 708 | adminmail = self.config.getAdminMail(printer.Name) |
---|
[1192] | 709 | (mailto, arguments) = self.config.getMailTo(printer.Name) |
---|
[2547] | 710 | if group.LimitBy in ("noquota", "nochange") : |
---|
| 711 | action = "ALLOW" |
---|
| 712 | else : |
---|
| 713 | action = self.checkGroupPQuota(grouppquota) |
---|
| 714 | if action.startswith("POLICY_") : |
---|
| 715 | action = action[7:] |
---|
| 716 | if action == "DENY" : |
---|
| 717 | adminmessage = _("Print Quota exceeded for group %s on printer %s") % (group.Name, printer.Name) |
---|
| 718 | self.printInfo(adminmessage) |
---|
| 719 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
| 720 | self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) |
---|
| 721 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
| 722 | for user in self.storage.getGroupMembers(group) : |
---|
| 723 | if mailto != "EXTERNAL" : |
---|
| 724 | self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), self.config.getHardWarn(printer.Name)) |
---|
| 725 | else : |
---|
| 726 | self.externalMailTo(arguments, action, user, printer, self.config.getHardWarn(printer.Name)) |
---|
| 727 | elif action == "WARN" : |
---|
| 728 | adminmessage = _("Print Quota low for group %s on printer %s") % (group.Name, printer.Name) |
---|
| 729 | self.printInfo(adminmessage) |
---|
| 730 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
| 731 | self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) |
---|
| 732 | if group.LimitBy and (group.LimitBy.lower() == "balance") : |
---|
| 733 | message = self.config.getPoorWarn() |
---|
| 734 | else : |
---|
| 735 | message = self.config.getSoftWarn(printer.Name) |
---|
| 736 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
| 737 | for user in self.storage.getGroupMembers(group) : |
---|
| 738 | if mailto != "EXTERNAL" : |
---|
| 739 | self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message) |
---|
| 740 | else : |
---|
| 741 | self.externalMailTo(arguments, action, user, printer, message) |
---|
[927] | 742 | return action |
---|
[728] | 743 | |
---|
[1041] | 744 | def warnUserPQuota(self, userpquota) : |
---|
[728] | 745 | """Checks a user quota and send him a message if quota is exceeded on current printer.""" |
---|
[1041] | 746 | user = userpquota.User |
---|
[1245] | 747 | printer = userpquota.Printer |
---|
| 748 | admin = self.config.getAdmin(printer.Name) |
---|
| 749 | adminmail = self.config.getAdminMail(printer.Name) |
---|
| 750 | (mailto, arguments) = self.config.getMailTo(printer.Name) |
---|
[2547] | 751 | |
---|
| 752 | if user.LimitBy in ("noquota", "nochange") : |
---|
| 753 | action = "ALLOW" |
---|
| 754 | elif user.LimitBy == "noprint" : |
---|
| 755 | action = "DENY" |
---|
[2558] | 756 | message = _("User %s is not allowed to print at this time.") % user.Name |
---|
| 757 | self.printInfo(message) |
---|
[1245] | 758 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
| 759 | if mailto != "EXTERNAL" : |
---|
[2547] | 760 | self.sendMessageToUser(admin, adminmail, user, _("Printing denied."), message) |
---|
[1245] | 761 | else : |
---|
| 762 | self.externalMailTo(arguments, action, user, printer, message) |
---|
| 763 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
[2547] | 764 | self.sendMessageToAdmin(adminmail, _("Print Quota"), message) |
---|
| 765 | else : |
---|
| 766 | action = self.checkUserPQuota(userpquota) |
---|
| 767 | if action.startswith("POLICY_") : |
---|
| 768 | action = action[7:] |
---|
| 769 | |
---|
| 770 | if action == "DENY" : |
---|
| 771 | adminmessage = _("Print Quota exceeded for user %s on printer %s") % (user.Name, printer.Name) |
---|
| 772 | self.printInfo(adminmessage) |
---|
| 773 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
| 774 | message = self.config.getHardWarn(printer.Name) |
---|
| 775 | if mailto != "EXTERNAL" : |
---|
| 776 | self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message) |
---|
| 777 | else : |
---|
| 778 | self.externalMailTo(arguments, action, user, printer, message) |
---|
| 779 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
| 780 | self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) |
---|
| 781 | elif action == "WARN" : |
---|
| 782 | adminmessage = _("Print Quota low for user %s on printer %s") % (user.Name, printer.Name) |
---|
| 783 | self.printInfo(adminmessage) |
---|
| 784 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
| 785 | if user.LimitBy and (user.LimitBy.lower() == "balance") : |
---|
| 786 | message = self.config.getPoorWarn() |
---|
| 787 | else : |
---|
| 788 | message = self.config.getSoftWarn(printer.Name) |
---|
| 789 | if mailto != "EXTERNAL" : |
---|
| 790 | self.sendMessageToUser(admin, adminmail, user, _("Print Quota Low"), message) |
---|
| 791 | else : |
---|
| 792 | self.externalMailTo(arguments, action, user, printer, message) |
---|
| 793 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
| 794 | self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) |
---|
[1245] | 795 | return action |
---|
[1196] | 796 | |
---|