[1144] | 1 | # PyKota |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
[695] | 3 | |
---|
[952] | 4 | # PyKota - Print Quotas for CUPS and LPRng |
---|
[695] | 5 | # |
---|
[1257] | 6 | # (c) 2003-2004 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 |
---|
[1434] | 33 | import signal |
---|
[1469] | 34 | import socket |
---|
[1492] | 35 | import tempfile |
---|
[1756] | 36 | import md5 |
---|
[1537] | 37 | import ConfigParser |
---|
[1917] | 38 | import popen2 |
---|
[695] | 39 | |
---|
[708] | 40 | from mx import DateTime |
---|
| 41 | |
---|
[2345] | 42 | from pykota import config, storage, logger, accounter |
---|
[2344] | 43 | from pykota.version import __version__, __author__, __years__, __gplblurb__ |
---|
[695] | 44 | |
---|
[2345] | 45 | try : |
---|
| 46 | from pkpgpdls import analyzer, pdlparser |
---|
[2347] | 47 | except ImportError : # TODO : Remove the try/except after release 1.24. |
---|
[2345] | 48 | sys.stderr.write("ERROR: pkpgcounter is now distributed separately, please grab it from http://www.librelogiciel.com/software/pkpgcounter/action_Download\n") |
---|
| 49 | |
---|
[1795] | 50 | def N_(message) : |
---|
| 51 | """Fake translation marker for translatable strings extraction.""" |
---|
| 52 | return message |
---|
| 53 | |
---|
[695] | 54 | class PyKotaToolError(Exception): |
---|
[2512] | 55 | """An exception for PyKota related stuff.""" |
---|
[695] | 56 | def __init__(self, message = ""): |
---|
| 57 | self.message = message |
---|
| 58 | Exception.__init__(self, message) |
---|
| 59 | def __repr__(self): |
---|
| 60 | return self.message |
---|
| 61 | __str__ = __repr__ |
---|
| 62 | |
---|
[2512] | 63 | class PyKotaCommandLineError(PyKotaToolError) : |
---|
| 64 | """An exception for Pykota command line tools.""" |
---|
| 65 | pass |
---|
| 66 | |
---|
[2229] | 67 | def crashed(message="Bug in PyKota") : |
---|
[1546] | 68 | """Minimal crash method.""" |
---|
| 69 | import traceback |
---|
| 70 | lines = [] |
---|
| 71 | for line in traceback.format_exception(*sys.exc_info()) : |
---|
| 72 | lines.extend([l for l in line.split("\n") if l]) |
---|
[2344] | 73 | msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKota v%s" % __version__, message] + lines)]) |
---|
[1546] | 74 | sys.stderr.write(msg) |
---|
| 75 | sys.stderr.flush() |
---|
| 76 | return msg |
---|
| 77 | |
---|
[1911] | 78 | class Tool : |
---|
| 79 | """Base class for tools with no database access.""" |
---|
[2344] | 80 | def __init__(self, lang="", charset=None, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") : |
---|
[695] | 81 | """Initializes the command line tool.""" |
---|
[2006] | 82 | # did we drop priviledges ? |
---|
| 83 | self.privdropped = 0 |
---|
| 84 | |
---|
[772] | 85 | # locale stuff |
---|
[2210] | 86 | self.defaultToCLocale = 0 |
---|
[788] | 87 | try : |
---|
[1171] | 88 | locale.setlocale(locale.LC_ALL, lang) |
---|
[788] | 89 | except (locale.Error, IOError) : |
---|
[2510] | 90 | # locale.setlocale(locale.LC_ALL, "C") |
---|
[2210] | 91 | self.defaultToCLocale = 1 |
---|
[1898] | 92 | try : |
---|
| 93 | gettext.install("pykota") |
---|
| 94 | except : |
---|
[788] | 95 | gettext.NullTranslations().install() |
---|
[1761] | 96 | |
---|
| 97 | # We can force the charset. |
---|
| 98 | # The CHARSET environment variable is set by CUPS when printing. |
---|
| 99 | # Else we use the current locale's one. |
---|
| 100 | # If nothing is set, we use ISO-8859-15 widely used in western Europe. |
---|
[1856] | 101 | localecharset = None |
---|
[1776] | 102 | try : |
---|
[1851] | 103 | try : |
---|
[2195] | 104 | localecharset = locale.nl_langinfo(locale.CODESET) |
---|
[1856] | 105 | except AttributeError : |
---|
| 106 | try : |
---|
[2195] | 107 | localecharset = locale.getpreferredencoding() |
---|
| 108 | except AttributeError : |
---|
| 109 | try : |
---|
[2214] | 110 | localecharset = locale.getlocale()[1] |
---|
[2195] | 111 | localecharset = localecharset or locale.getdefaultlocale()[1] |
---|
| 112 | except ValueError : |
---|
| 113 | pass # Unknown locale, strange... |
---|
[1856] | 114 | except locale.Error : |
---|
| 115 | pass |
---|
[1776] | 116 | self.charset = charset or os.environ.get("CHARSET") or localecharset or "ISO-8859-15" |
---|
[772] | 117 | |
---|
| 118 | # pykota specific stuff |
---|
[715] | 119 | self.documentation = doc |
---|
[1960] | 120 | |
---|
[2210] | 121 | def deferredInit(self) : |
---|
| 122 | """Deferred initialization.""" |
---|
[1960] | 123 | # try to find the configuration files in user's 'pykota' home directory. |
---|
[1537] | 124 | try : |
---|
[2006] | 125 | self.pykotauser = pwd.getpwnam("pykota") |
---|
[1960] | 126 | except KeyError : |
---|
[2006] | 127 | self.pykotauser = None |
---|
[1960] | 128 | confdir = "/etc/pykota" |
---|
| 129 | missingUser = 1 |
---|
| 130 | else : |
---|
[2006] | 131 | confdir = self.pykotauser[5] |
---|
[1960] | 132 | missingUser = 0 |
---|
| 133 | |
---|
[2210] | 134 | self.config = config.PyKotaConfig(confdir) |
---|
| 135 | self.debug = self.config.getDebug() |
---|
| 136 | self.smtpserver = self.config.getSMTPServer() |
---|
| 137 | self.maildomain = self.config.getMailDomain() |
---|
| 138 | self.logger = logger.openLogger(self.config.getLoggingBackend()) |
---|
[1542] | 139 | |
---|
[2006] | 140 | # now drop priviledge if possible |
---|
| 141 | self.dropPriv() |
---|
| 142 | |
---|
[1960] | 143 | # We NEED this here, even when not in an accounting filter/backend |
---|
[1497] | 144 | self.softwareJobSize = 0 |
---|
[1495] | 145 | self.softwareJobPrice = 0.0 |
---|
[1960] | 146 | |
---|
[2210] | 147 | if self.defaultToCLocale : |
---|
[2511] | 148 | self.printInfo("Incorrect locale settings. PyKota falls back to the default locale.", "warn") |
---|
[1960] | 149 | if missingUser : |
---|
| 150 | self.printInfo("The 'pykota' system account is missing. Configuration files were searched in /etc/pykota instead.", "warn") |
---|
| 151 | |
---|
[1761] | 152 | self.logdebug("Charset in use : %s" % self.charset) |
---|
[1872] | 153 | arguments = " ".join(['"%s"' % arg for arg in sys.argv]) |
---|
| 154 | self.logdebug("Command line arguments : %s" % arguments) |
---|
[695] | 155 | |
---|
[2006] | 156 | def dropPriv(self) : |
---|
| 157 | """Drops priviledges.""" |
---|
| 158 | uid = os.geteuid() |
---|
| 159 | if uid : |
---|
| 160 | try : |
---|
| 161 | username = pwd.getpwuid(uid)[0] |
---|
| 162 | except (KeyError, IndexError), msg : |
---|
| 163 | self.printInfo(_("Strange problem with uid(%s) : %s") % (uid, msg), "warn") |
---|
| 164 | else : |
---|
| 165 | self.logdebug(_("Running as user '%s'.") % username) |
---|
| 166 | else : |
---|
| 167 | if self.pykotauser is None : |
---|
| 168 | self.logdebug(_("No user named 'pykota'. Not dropping priviledges.")) |
---|
| 169 | else : |
---|
| 170 | try : |
---|
| 171 | os.setegid(self.pykotauser[3]) |
---|
| 172 | os.seteuid(self.pykotauser[2]) |
---|
| 173 | except OSError, msg : |
---|
| 174 | self.printInfo(_("Impossible to drop priviledges : %s") % msg, "warn") |
---|
| 175 | else : |
---|
| 176 | self.logdebug(_("Priviledges dropped. Now running as user 'pykota'.")) |
---|
| 177 | self.privdropped = 1 |
---|
| 178 | |
---|
| 179 | def regainPriv(self) : |
---|
| 180 | """Drops priviledges.""" |
---|
| 181 | if self.privdropped : |
---|
| 182 | try : |
---|
| 183 | os.seteuid(0) |
---|
| 184 | os.setegid(0) |
---|
| 185 | except OSError, msg : |
---|
| 186 | self.printInfo(_("Impossible to regain priviledges : %s") % msg, "warn") |
---|
| 187 | else : |
---|
[2008] | 188 | self.logdebug(_("Regained priviledges. Now running as root.")) |
---|
[2006] | 189 | self.privdropped = 0 |
---|
| 190 | |
---|
[1761] | 191 | def getCharset(self) : |
---|
| 192 | """Returns the charset in use.""" |
---|
| 193 | return self.charset |
---|
| 194 | |
---|
[1130] | 195 | def logdebug(self, message) : |
---|
| 196 | """Logs something to debug output if debug is enabled.""" |
---|
| 197 | if self.debug : |
---|
| 198 | self.logger.log_message(message, "debug") |
---|
[1582] | 199 | |
---|
[1584] | 200 | def printInfo(self, message, level="info") : |
---|
[1582] | 201 | """Sends a message to standard error.""" |
---|
[1584] | 202 | sys.stderr.write("%s: %s\n" % (level.upper(), message)) |
---|
[1582] | 203 | sys.stderr.flush() |
---|
[1130] | 204 | |
---|
[2210] | 205 | def matchString(self, s, patterns) : |
---|
| 206 | """Returns 1 if the string s matches one of the patterns, else 0.""" |
---|
| 207 | for pattern in patterns : |
---|
| 208 | if fnmatch.fnmatchcase(s, pattern) : |
---|
| 209 | return 1 |
---|
| 210 | return 0 |
---|
| 211 | |
---|
[715] | 212 | def display_version_and_quit(self) : |
---|
| 213 | """Displays version number, then exists successfully.""" |
---|
[1923] | 214 | try : |
---|
| 215 | self.clean() |
---|
| 216 | except AttributeError : |
---|
| 217 | pass |
---|
[2344] | 218 | print __version__ |
---|
[715] | 219 | sys.exit(0) |
---|
| 220 | |
---|
| 221 | def display_usage_and_quit(self) : |
---|
| 222 | """Displays command line usage, then exists successfully.""" |
---|
[1923] | 223 | try : |
---|
| 224 | self.clean() |
---|
| 225 | except AttributeError : |
---|
| 226 | pass |
---|
[2344] | 227 | print _(self.documentation) % globals() |
---|
| 228 | print __gplblurb__ |
---|
| 229 | print |
---|
| 230 | print _("Please report bugs to :"), __author__ |
---|
[715] | 231 | sys.exit(0) |
---|
| 232 | |
---|
[2229] | 233 | def crashed(self, message="Bug in PyKota") : |
---|
[1517] | 234 | """Outputs a crash message, and optionally sends it to software author.""" |
---|
[1546] | 235 | msg = crashed(message) |
---|
[2229] | 236 | fullmessage = "========== Traceback :\n\n%s\n\n========== sys.argv :\n\n%s\n\n========== Environment :\n\n%s\n" % \ |
---|
| 237 | (msg, \ |
---|
| 238 | "\n".join([" %s" % repr(a) for a in sys.argv]), \ |
---|
| 239 | "\n".join([" %s=%s" % (k, v) for (k, v) in os.environ.items()])) |
---|
[1541] | 240 | try : |
---|
| 241 | crashrecipient = self.config.getCrashRecipient() |
---|
| 242 | if crashrecipient : |
---|
[1517] | 243 | admin = self.config.getAdminMail("global") # Nice trick, isn't it ? |
---|
| 244 | server = smtplib.SMTP(self.smtpserver) |
---|
[1546] | 245 | server.sendmail(admin, [admin, crashrecipient], \ |
---|
[1560] | 246 | "From: %s\nTo: %s\nCc: %s\nSubject: PyKota v%s crash traceback !\n\n%s" % \ |
---|
[2344] | 247 | (admin, crashrecipient, admin, __version__, fullmessage)) |
---|
[1517] | 248 | server.quit() |
---|
[1541] | 249 | except : |
---|
| 250 | pass |
---|
[2229] | 251 | return fullmessage |
---|
[1517] | 252 | |
---|
[729] | 253 | def parseCommandline(self, argv, short, long, allownothing=0) : |
---|
[715] | 254 | """Parses the command line, controlling options.""" |
---|
| 255 | # split options in two lists: those which need an argument, those which don't need any |
---|
| 256 | withoutarg = [] |
---|
| 257 | witharg = [] |
---|
| 258 | lgs = len(short) |
---|
| 259 | i = 0 |
---|
| 260 | while i < lgs : |
---|
| 261 | ii = i + 1 |
---|
| 262 | if (ii < lgs) and (short[ii] == ':') : |
---|
| 263 | # needs an argument |
---|
| 264 | witharg.append(short[i]) |
---|
| 265 | ii = ii + 1 # skip the ':' |
---|
| 266 | else : |
---|
| 267 | # doesn't need an argument |
---|
| 268 | withoutarg.append(short[i]) |
---|
| 269 | i = ii |
---|
| 270 | |
---|
| 271 | for option in long : |
---|
| 272 | if option[-1] == '=' : |
---|
| 273 | # needs an argument |
---|
| 274 | witharg.append(option[:-1]) |
---|
| 275 | else : |
---|
| 276 | # doesn't need an argument |
---|
| 277 | withoutarg.append(option) |
---|
| 278 | |
---|
| 279 | # we begin with all possible options unset |
---|
| 280 | parsed = {} |
---|
| 281 | for option in withoutarg + witharg : |
---|
| 282 | parsed[option] = None |
---|
| 283 | |
---|
| 284 | # then we parse the command line |
---|
| 285 | args = [] # to not break if something unexpected happened |
---|
| 286 | try : |
---|
| 287 | options, args = getopt.getopt(argv, short, long) |
---|
| 288 | if options : |
---|
| 289 | for (o, v) in options : |
---|
| 290 | # we skip the '-' chars |
---|
| 291 | lgo = len(o) |
---|
| 292 | i = 0 |
---|
| 293 | while (i < lgo) and (o[i] == '-') : |
---|
| 294 | i = i + 1 |
---|
| 295 | o = o[i:] |
---|
| 296 | if o in witharg : |
---|
| 297 | # needs an argument : set it |
---|
| 298 | parsed[o] = v |
---|
| 299 | elif o in withoutarg : |
---|
| 300 | # doesn't need an argument : boolean |
---|
| 301 | parsed[o] = 1 |
---|
| 302 | else : |
---|
| 303 | # should never occur |
---|
| 304 | raise PyKotaToolError, "Unexpected problem when parsing command line" |
---|
[729] | 305 | elif (not args) and (not allownothing) and sys.stdin.isatty() : # no option and no argument, we display help if we are a tty |
---|
[715] | 306 | self.display_usage_and_quit() |
---|
| 307 | except getopt.error, msg : |
---|
[1584] | 308 | self.printInfo(msg) |
---|
[715] | 309 | self.display_usage_and_quit() |
---|
| 310 | return (parsed, args) |
---|
| 311 | |
---|
[1911] | 312 | class PyKotaTool(Tool) : |
---|
| 313 | """Base class for all PyKota command line tools.""" |
---|
[2344] | 314 | def __init__(self, lang="", charset=None, doc="PyKota v%(__version__)s (c) %(__years__)s %(__author__)s") : |
---|
[1911] | 315 | """Initializes the command line tool and opens the database.""" |
---|
| 316 | Tool.__init__(self, lang, charset, doc) |
---|
[2210] | 317 | |
---|
| 318 | def deferredInit(self) : |
---|
| 319 | """Deferred initialization.""" |
---|
| 320 | Tool.deferredInit(self) |
---|
| 321 | self.storage = storage.openConnection(self) |
---|
| 322 | if self.config.isAdmin : # TODO : We don't know this before, fix this ! |
---|
| 323 | self.logdebug("Beware : running as a PyKota administrator !") |
---|
[2093] | 324 | else : |
---|
[2210] | 325 | self.logdebug("Don't Panic : running as a mere mortal !") |
---|
[1911] | 326 | |
---|
| 327 | def clean(self) : |
---|
| 328 | """Ensures that the database is closed.""" |
---|
| 329 | try : |
---|
| 330 | self.storage.close() |
---|
| 331 | except (TypeError, NameError, AttributeError) : |
---|
| 332 | pass |
---|
| 333 | |
---|
[764] | 334 | def isValidName(self, name) : |
---|
| 335 | """Checks if a user or printer name is valid.""" |
---|
[1725] | 336 | invalidchars = "/@?*,;&|" |
---|
[1637] | 337 | for c in list(invalidchars) : |
---|
[1593] | 338 | if c in name : |
---|
[1394] | 339 | return 0 |
---|
| 340 | return 1 |
---|
[764] | 341 | |
---|
[802] | 342 | def sendMessage(self, adminmail, touser, fullmessage) : |
---|
[695] | 343 | """Sends an email message containing headers to some user.""" |
---|
| 344 | if "@" not in touser : |
---|
[1353] | 345 | touser = "%s@%s" % (touser, self.maildomain or self.smtpserver) |
---|
[1469] | 346 | try : |
---|
| 347 | server = smtplib.SMTP(self.smtpserver) |
---|
| 348 | except socket.error, msg : |
---|
[1584] | 349 | self.printInfo(_("Impossible to connect to SMTP server : %s") % msg, "error") |
---|
[1469] | 350 | else : |
---|
| 351 | try : |
---|
| 352 | server.sendmail(adminmail, [touser], "From: %s\nTo: %s\n%s" % (adminmail, touser, fullmessage)) |
---|
| 353 | except smtplib.SMTPException, answer : |
---|
| 354 | for (k, v) in answer.recipients.items() : |
---|
[1584] | 355 | self.printInfo(_("Impossible to send mail to %s, error %s : %s") % (k, v[0], v[1]), "error") |
---|
[1469] | 356 | server.quit() |
---|
[695] | 357 | |
---|
[1079] | 358 | def sendMessageToUser(self, admin, adminmail, user, subject, message) : |
---|
[695] | 359 | """Sends an email message to a user.""" |
---|
[802] | 360 | message += _("\n\nPlease contact your system administrator :\n\n\t%s - <%s>\n") % (admin, adminmail) |
---|
[1079] | 361 | self.sendMessage(adminmail, user.Email or user.Name, "Subject: %s\n\n%s" % (subject, message)) |
---|
[695] | 362 | |
---|
[802] | 363 | def sendMessageToAdmin(self, adminmail, subject, message) : |
---|
[695] | 364 | """Sends an email message to the Print Quota administrator.""" |
---|
[802] | 365 | self.sendMessage(adminmail, adminmail, "Subject: %s\n\n%s" % (subject, message)) |
---|
[695] | 366 | |
---|
[1365] | 367 | def _checkUserPQuota(self, userpquota) : |
---|
| 368 | """Checks the user quota on a printer and deny or accept the job.""" |
---|
| 369 | # then we check the user's own quota |
---|
| 370 | # if we get there we are sure that policy is not EXTERNAL |
---|
| 371 | user = userpquota.User |
---|
| 372 | printer = userpquota.Printer |
---|
[1495] | 373 | enforcement = self.config.getPrinterEnforcement(printer.Name) |
---|
[1365] | 374 | self.logdebug("Checking user %s's quota on printer %s" % (user.Name, printer.Name)) |
---|
| 375 | (policy, dummy) = self.config.getPrinterPolicy(userpquota.Printer.Name) |
---|
| 376 | if not userpquota.Exists : |
---|
| 377 | # Unknown userquota |
---|
| 378 | if policy == "ALLOW" : |
---|
| 379 | action = "POLICY_ALLOW" |
---|
| 380 | else : |
---|
| 381 | action = "POLICY_DENY" |
---|
[1584] | 382 | self.printInfo(_("Unable to match user %s on printer %s, applying default policy (%s)") % (user.Name, printer.Name, action)) |
---|
[1365] | 383 | else : |
---|
| 384 | pagecounter = int(userpquota.PageCounter or 0) |
---|
[1495] | 385 | if enforcement == "STRICT" : |
---|
| 386 | pagecounter += self.softwareJobSize |
---|
[1365] | 387 | if userpquota.SoftLimit is not None : |
---|
| 388 | softlimit = int(userpquota.SoftLimit) |
---|
| 389 | if pagecounter < softlimit : |
---|
| 390 | action = "ALLOW" |
---|
| 391 | else : |
---|
| 392 | if userpquota.HardLimit is None : |
---|
| 393 | # only a soft limit, this is equivalent to having only a hard limit |
---|
| 394 | action = "DENY" |
---|
| 395 | else : |
---|
| 396 | hardlimit = int(userpquota.HardLimit) |
---|
| 397 | if softlimit <= pagecounter < hardlimit : |
---|
| 398 | now = DateTime.now() |
---|
| 399 | if userpquota.DateLimit is not None : |
---|
| 400 | datelimit = DateTime.ISO.ParseDateTime(userpquota.DateLimit) |
---|
| 401 | else : |
---|
| 402 | datelimit = now + self.config.getGraceDelay(printer.Name) |
---|
| 403 | userpquota.setDateLimit(datelimit) |
---|
| 404 | if now < datelimit : |
---|
| 405 | action = "WARN" |
---|
| 406 | else : |
---|
| 407 | action = "DENY" |
---|
| 408 | else : |
---|
| 409 | action = "DENY" |
---|
| 410 | else : |
---|
| 411 | if userpquota.HardLimit is not None : |
---|
| 412 | # no soft limit, only a hard one. |
---|
| 413 | hardlimit = int(userpquota.HardLimit) |
---|
| 414 | if pagecounter < hardlimit : |
---|
| 415 | action = "ALLOW" |
---|
| 416 | else : |
---|
| 417 | action = "DENY" |
---|
| 418 | else : |
---|
| 419 | # Both are unset, no quota, i.e. accounting only |
---|
| 420 | action = "ALLOW" |
---|
| 421 | return action |
---|
| 422 | |
---|
[1041] | 423 | def checkGroupPQuota(self, grouppquota) : |
---|
[927] | 424 | """Checks the group quota on a printer and deny or accept the job.""" |
---|
[1041] | 425 | group = grouppquota.Group |
---|
| 426 | printer = grouppquota.Printer |
---|
[1495] | 427 | enforcement = self.config.getPrinterEnforcement(printer.Name) |
---|
[1365] | 428 | self.logdebug("Checking group %s's quota on printer %s" % (group.Name, printer.Name)) |
---|
[1061] | 429 | if group.LimitBy and (group.LimitBy.lower() == "balance") : |
---|
[1666] | 430 | val = group.AccountBalance or 0.0 |
---|
[1495] | 431 | if enforcement == "STRICT" : |
---|
| 432 | val -= self.softwareJobPrice # use precomputed size. |
---|
| 433 | if val <= 0.0 : |
---|
[1041] | 434 | action = "DENY" |
---|
[1495] | 435 | elif val <= self.config.getPoorMan() : |
---|
[1077] | 436 | action = "WARN" |
---|
[927] | 437 | else : |
---|
[1041] | 438 | action = "ALLOW" |
---|
[1529] | 439 | if (enforcement == "STRICT") and (val == 0.0) : |
---|
| 440 | action = "WARN" # we can still print until account is 0 |
---|
[927] | 441 | else : |
---|
[1665] | 442 | val = grouppquota.PageCounter or 0 |
---|
[1495] | 443 | if enforcement == "STRICT" : |
---|
| 444 | val += self.softwareJobSize |
---|
[1041] | 445 | if grouppquota.SoftLimit is not None : |
---|
| 446 | softlimit = int(grouppquota.SoftLimit) |
---|
[1495] | 447 | if val < softlimit : |
---|
[1041] | 448 | action = "ALLOW" |
---|
[927] | 449 | else : |
---|
[1041] | 450 | if grouppquota.HardLimit is None : |
---|
| 451 | # only a soft limit, this is equivalent to having only a hard limit |
---|
| 452 | action = "DENY" |
---|
[927] | 453 | else : |
---|
[1041] | 454 | hardlimit = int(grouppquota.HardLimit) |
---|
[1495] | 455 | if softlimit <= val < hardlimit : |
---|
[1041] | 456 | now = DateTime.now() |
---|
| 457 | if grouppquota.DateLimit is not None : |
---|
| 458 | datelimit = DateTime.ISO.ParseDateTime(grouppquota.DateLimit) |
---|
| 459 | else : |
---|
| 460 | datelimit = now + self.config.getGraceDelay(printer.Name) |
---|
| 461 | grouppquota.setDateLimit(datelimit) |
---|
| 462 | if now < datelimit : |
---|
| 463 | action = "WARN" |
---|
| 464 | else : |
---|
[927] | 465 | action = "DENY" |
---|
[1041] | 466 | else : |
---|
[927] | 467 | action = "DENY" |
---|
[1041] | 468 | else : |
---|
| 469 | if grouppquota.HardLimit is not None : |
---|
| 470 | # no soft limit, only a hard one. |
---|
| 471 | hardlimit = int(grouppquota.HardLimit) |
---|
[1495] | 472 | if val < hardlimit : |
---|
[927] | 473 | action = "ALLOW" |
---|
[1041] | 474 | else : |
---|
| 475 | action = "DENY" |
---|
| 476 | else : |
---|
| 477 | # Both are unset, no quota, i.e. accounting only |
---|
| 478 | action = "ALLOW" |
---|
[927] | 479 | return action |
---|
| 480 | |
---|
[1041] | 481 | def checkUserPQuota(self, userpquota) : |
---|
[1365] | 482 | """Checks the user quota on a printer and all its parents and deny or accept the job.""" |
---|
[1041] | 483 | user = userpquota.User |
---|
| 484 | printer = userpquota.Printer |
---|
| 485 | |
---|
[1365] | 486 | # indicates that a warning needs to be sent |
---|
| 487 | warned = 0 |
---|
| 488 | |
---|
[927] | 489 | # first we check any group the user is a member of |
---|
[1041] | 490 | for group in self.storage.getUserGroups(user) : |
---|
[2452] | 491 | # No need to check anything if the group is in noquota mode |
---|
| 492 | if group.LimitBy != "noquota" : |
---|
| 493 | grouppquota = self.storage.getGroupPQuota(group, printer) |
---|
| 494 | # for the printer and all its parents |
---|
| 495 | for gpquota in [ grouppquota ] + grouppquota.ParentPrintersGroupPQuota : |
---|
| 496 | if gpquota.Exists : |
---|
| 497 | action = self.checkGroupPQuota(gpquota) |
---|
| 498 | if action == "DENY" : |
---|
| 499 | return action |
---|
| 500 | elif action == "WARN" : |
---|
| 501 | warned = 1 |
---|
[1365] | 502 | |
---|
| 503 | # Then we check the user's account balance |
---|
[1152] | 504 | # if we get there we are sure that policy is not EXTERNAL |
---|
| 505 | (policy, dummy) = self.config.getPrinterPolicy(printer.Name) |
---|
[1061] | 506 | if user.LimitBy and (user.LimitBy.lower() == "balance") : |
---|
[1365] | 507 | self.logdebug("Checking account balance for user %s" % user.Name) |
---|
[1041] | 508 | if user.AccountBalance is None : |
---|
[956] | 509 | if policy == "ALLOW" : |
---|
[925] | 510 | action = "POLICY_ALLOW" |
---|
| 511 | else : |
---|
| 512 | action = "POLICY_DENY" |
---|
[1584] | 513 | self.printInfo(_("Unable to find user %s's account balance, applying default policy (%s) for printer %s") % (user.Name, action, printer.Name)) |
---|
[1365] | 514 | return action |
---|
[713] | 515 | else : |
---|
[2054] | 516 | if user.OverCharge == 0.0 : |
---|
| 517 | self.printInfo(_("User %s will not be charged for printing.") % user.Name) |
---|
| 518 | action = "ALLOW" |
---|
[1529] | 519 | else : |
---|
[2054] | 520 | val = float(user.AccountBalance or 0.0) |
---|
| 521 | enforcement = self.config.getPrinterEnforcement(printer.Name) |
---|
| 522 | if enforcement == "STRICT" : |
---|
| 523 | val -= self.softwareJobPrice # use precomputed size. |
---|
| 524 | if val <= 0.0 : |
---|
| 525 | action = "DENY" |
---|
| 526 | elif val <= self.config.getPoorMan() : |
---|
| 527 | action = "WARN" |
---|
| 528 | else : |
---|
| 529 | action = "ALLOW" |
---|
| 530 | if (enforcement == "STRICT") and (val == 0.0) : |
---|
| 531 | action = "WARN" # we can still print until account is 0 |
---|
[1529] | 532 | return action |
---|
[925] | 533 | else : |
---|
[1365] | 534 | # Then check the user quota on current printer and all its parents. |
---|
| 535 | policyallowed = 0 |
---|
| 536 | for upquota in [ userpquota ] + userpquota.ParentPrintersUserPQuota : |
---|
| 537 | action = self._checkUserPQuota(upquota) |
---|
| 538 | if action in ("DENY", "POLICY_DENY") : |
---|
| 539 | return action |
---|
| 540 | elif action == "WARN" : |
---|
| 541 | warned = 1 |
---|
| 542 | elif action == "POLICY_ALLOW" : |
---|
| 543 | policyallowed = 1 |
---|
| 544 | if warned : |
---|
| 545 | return "WARN" |
---|
| 546 | elif policyallowed : |
---|
| 547 | return "POLICY_ALLOW" |
---|
[925] | 548 | else : |
---|
[1365] | 549 | return "ALLOW" |
---|
| 550 | |
---|
[1196] | 551 | def externalMailTo(self, cmd, action, user, printer, message) : |
---|
[1192] | 552 | """Warns the user with an external command.""" |
---|
| 553 | username = user.Name |
---|
[1196] | 554 | printername = printer.Name |
---|
[1192] | 555 | email = user.Email or user.Name |
---|
| 556 | if "@" not in email : |
---|
[1353] | 557 | email = "%s@%s" % (email, self.maildomain or self.smtpserver) |
---|
[1192] | 558 | os.system(cmd % locals()) |
---|
| 559 | |
---|
[1196] | 560 | def formatCommandLine(self, cmd, user, printer) : |
---|
| 561 | """Executes an external command.""" |
---|
| 562 | username = user.Name |
---|
| 563 | printername = printer.Name |
---|
| 564 | return cmd % locals() |
---|
| 565 | |
---|
[1041] | 566 | def warnGroupPQuota(self, grouppquota) : |
---|
[927] | 567 | """Checks a group quota and send messages if quota is exceeded on current printer.""" |
---|
[1041] | 568 | group = grouppquota.Group |
---|
| 569 | printer = grouppquota.Printer |
---|
| 570 | admin = self.config.getAdmin(printer.Name) |
---|
| 571 | adminmail = self.config.getAdminMail(printer.Name) |
---|
[1192] | 572 | (mailto, arguments) = self.config.getMailTo(printer.Name) |
---|
[2547] | 573 | if group.LimitBy in ("noquota", "nochange") : |
---|
| 574 | action = "ALLOW" |
---|
| 575 | else : |
---|
| 576 | action = self.checkGroupPQuota(grouppquota) |
---|
| 577 | if action.startswith("POLICY_") : |
---|
| 578 | action = action[7:] |
---|
| 579 | if action == "DENY" : |
---|
| 580 | adminmessage = _("Print Quota exceeded for group %s on printer %s") % (group.Name, printer.Name) |
---|
| 581 | self.printInfo(adminmessage) |
---|
| 582 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
| 583 | self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) |
---|
| 584 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
| 585 | for user in self.storage.getGroupMembers(group) : |
---|
| 586 | if mailto != "EXTERNAL" : |
---|
| 587 | self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), self.config.getHardWarn(printer.Name)) |
---|
| 588 | else : |
---|
| 589 | self.externalMailTo(arguments, action, user, printer, self.config.getHardWarn(printer.Name)) |
---|
| 590 | elif action == "WARN" : |
---|
| 591 | adminmessage = _("Print Quota low for group %s on printer %s") % (group.Name, printer.Name) |
---|
| 592 | self.printInfo(adminmessage) |
---|
| 593 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
| 594 | self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) |
---|
| 595 | if group.LimitBy and (group.LimitBy.lower() == "balance") : |
---|
| 596 | message = self.config.getPoorWarn() |
---|
| 597 | else : |
---|
| 598 | message = self.config.getSoftWarn(printer.Name) |
---|
| 599 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
| 600 | for user in self.storage.getGroupMembers(group) : |
---|
| 601 | if mailto != "EXTERNAL" : |
---|
| 602 | self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message) |
---|
| 603 | else : |
---|
| 604 | self.externalMailTo(arguments, action, user, printer, message) |
---|
[927] | 605 | return action |
---|
[728] | 606 | |
---|
[1041] | 607 | def warnUserPQuota(self, userpquota) : |
---|
[728] | 608 | """Checks a user quota and send him a message if quota is exceeded on current printer.""" |
---|
[1041] | 609 | user = userpquota.User |
---|
[1245] | 610 | printer = userpquota.Printer |
---|
| 611 | admin = self.config.getAdmin(printer.Name) |
---|
| 612 | adminmail = self.config.getAdminMail(printer.Name) |
---|
| 613 | (mailto, arguments) = self.config.getMailTo(printer.Name) |
---|
[2547] | 614 | |
---|
| 615 | if user.LimitBy in ("noquota", "nochange") : |
---|
| 616 | action = "ALLOW" |
---|
| 617 | elif user.LimitBy == "noprint" : |
---|
| 618 | action = "DENY" |
---|
[2558] | 619 | message = _("User %s is not allowed to print at this time.") % user.Name |
---|
| 620 | self.printInfo(message) |
---|
[1245] | 621 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
| 622 | if mailto != "EXTERNAL" : |
---|
[2547] | 623 | self.sendMessageToUser(admin, adminmail, user, _("Printing denied."), message) |
---|
[1245] | 624 | else : |
---|
| 625 | self.externalMailTo(arguments, action, user, printer, message) |
---|
| 626 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
[2547] | 627 | self.sendMessageToAdmin(adminmail, _("Print Quota"), message) |
---|
| 628 | else : |
---|
| 629 | action = self.checkUserPQuota(userpquota) |
---|
| 630 | if action.startswith("POLICY_") : |
---|
| 631 | action = action[7:] |
---|
| 632 | |
---|
| 633 | if action == "DENY" : |
---|
| 634 | adminmessage = _("Print Quota exceeded for user %s on printer %s") % (user.Name, printer.Name) |
---|
| 635 | self.printInfo(adminmessage) |
---|
| 636 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
| 637 | message = self.config.getHardWarn(printer.Name) |
---|
| 638 | if mailto != "EXTERNAL" : |
---|
| 639 | self.sendMessageToUser(admin, adminmail, user, _("Print Quota Exceeded"), message) |
---|
| 640 | else : |
---|
| 641 | self.externalMailTo(arguments, action, user, printer, message) |
---|
| 642 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
| 643 | self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) |
---|
| 644 | elif action == "WARN" : |
---|
| 645 | adminmessage = _("Print Quota low for user %s on printer %s") % (user.Name, printer.Name) |
---|
| 646 | self.printInfo(adminmessage) |
---|
| 647 | if mailto in [ "BOTH", "USER", "EXTERNAL" ] : |
---|
| 648 | if user.LimitBy and (user.LimitBy.lower() == "balance") : |
---|
| 649 | message = self.config.getPoorWarn() |
---|
| 650 | else : |
---|
| 651 | message = self.config.getSoftWarn(printer.Name) |
---|
| 652 | if mailto != "EXTERNAL" : |
---|
| 653 | self.sendMessageToUser(admin, adminmail, user, _("Print Quota Low"), message) |
---|
| 654 | else : |
---|
| 655 | self.externalMailTo(arguments, action, user, printer, message) |
---|
| 656 | if mailto in [ "BOTH", "ADMIN" ] : |
---|
| 657 | self.sendMessageToAdmin(adminmail, _("Print Quota"), adminmessage) |
---|
[1245] | 658 | return action |
---|
[1196] | 659 | |
---|