[695] | 1 | # PyKota |
---|
| 2 | # |
---|
[952] | 3 | # PyKota : Print Quotas for CUPS and LPRng |
---|
[695] | 4 | # |
---|
| 5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 6 | # This program is free software; you can redistribute it and/or modify |
---|
| 7 | # it under the terms of the GNU General Public License as published by |
---|
| 8 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 9 | # (at your option) any later version. |
---|
[695] | 10 | # |
---|
[873] | 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
| 15 | # |
---|
| 16 | # You should have received a copy of the GNU General Public License |
---|
| 17 | # along with this program; if not, write to the Free Software |
---|
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
[695] | 19 | # |
---|
| 20 | # $Id$ |
---|
| 21 | # |
---|
| 22 | # $Log$ |
---|
[1087] | 23 | # Revision 1.33 2003/07/16 21:53:07 jalet |
---|
| 24 | # Really big modifications wrt new configuration file's location and content. |
---|
| 25 | # |
---|
[1077] | 26 | # Revision 1.32 2003/07/08 19:43:51 jalet |
---|
| 27 | # Configurable warning messages. |
---|
| 28 | # Poor man's treshold value added. |
---|
| 29 | # |
---|
[1068] | 30 | # Revision 1.31 2003/07/07 11:49:24 jalet |
---|
| 31 | # Lots of small fixes with the help of PyChecker |
---|
| 32 | # |
---|
[1041] | 33 | # Revision 1.30 2003/06/25 14:10:01 jalet |
---|
| 34 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 35 | # |
---|
[1029] | 36 | # Revision 1.29 2003/06/14 22:44:21 jalet |
---|
| 37 | # More work on LDAP storage backend. |
---|
| 38 | # |
---|
[1021] | 39 | # Revision 1.28 2003/06/10 16:37:54 jalet |
---|
| 40 | # Deletion of the second user which is not needed anymore. |
---|
| 41 | # Added a debug configuration field in /etc/pykota.conf |
---|
| 42 | # All queries can now be sent to the logger in debug mode, this will |
---|
| 43 | # greatly help improve performance when time for this will come. |
---|
| 44 | # |
---|
[1000] | 45 | # Revision 1.27 2003/05/27 23:00:21 jalet |
---|
| 46 | # Big rewrite of external accounting methods. |
---|
| 47 | # Should work well now. |
---|
| 48 | # |
---|
[980] | 49 | # Revision 1.26 2003/04/30 19:53:58 jalet |
---|
| 50 | # 1.05 |
---|
| 51 | # |
---|
[976] | 52 | # Revision 1.25 2003/04/30 13:36:40 jalet |
---|
| 53 | # Stupid accounting method was added. |
---|
| 54 | # |
---|
[973] | 55 | # Revision 1.24 2003/04/29 18:37:54 jalet |
---|
| 56 | # Pluggable accounting methods (actually doesn't support external scripts) |
---|
| 57 | # |
---|
[956] | 58 | # Revision 1.23 2003/04/24 11:53:48 jalet |
---|
| 59 | # Default policy for unknown users/groups is to DENY printing instead |
---|
| 60 | # of the previous default to ALLOW printing. This is to solve an accuracy |
---|
| 61 | # problem. If you set the policy to ALLOW, jobs printed by in nexistant user |
---|
| 62 | # (from PyKota's POV) will be charged to the next user who prints on the |
---|
| 63 | # same printer. |
---|
| 64 | # |
---|
[952] | 65 | # Revision 1.22 2003/04/23 22:13:57 jalet |
---|
| 66 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
| 67 | # |
---|
[873] | 68 | # Revision 1.21 2003/03/29 13:45:27 jalet |
---|
| 69 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 70 | # Two README files were added. |
---|
| 71 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 72 | # |
---|
[872] | 73 | # Revision 1.20 2003/03/29 13:08:28 jalet |
---|
| 74 | # Configuration is now expected to be found in /etc/pykota.conf instead of |
---|
| 75 | # in /etc/cups/pykota.conf |
---|
| 76 | # Installation script can move old config files to the new location if needed. |
---|
| 77 | # Better error handling if configuration file is absent. |
---|
| 78 | # |
---|
[854] | 79 | # Revision 1.19 2003/03/16 09:56:52 jalet |
---|
| 80 | # Mailto option now accepts some additional values which all mean that |
---|
| 81 | # nobody will receive any email message. |
---|
| 82 | # Mailto option now works. Version 1.01 is now officially out. |
---|
| 83 | # |
---|
[853] | 84 | # Revision 1.18 2003/03/16 08:00:50 jalet |
---|
| 85 | # Default hard coded options are now used if they are not set in the |
---|
| 86 | # configuration file. |
---|
| 87 | # |
---|
[852] | 88 | # Revision 1.17 2003/03/15 23:01:28 jalet |
---|
| 89 | # New mailto option in configuration file added. |
---|
| 90 | # No time to test this tonight (although it should work). |
---|
| 91 | # |
---|
[804] | 92 | # Revision 1.16 2003/02/17 23:01:56 jalet |
---|
| 93 | # Typos |
---|
| 94 | # |
---|
[802] | 95 | # Revision 1.15 2003/02/17 22:55:01 jalet |
---|
| 96 | # More options can now be set per printer or globally : |
---|
| 97 | # |
---|
[804] | 98 | # admin |
---|
| 99 | # adminmail |
---|
| 100 | # gracedelay |
---|
| 101 | # requester |
---|
[802] | 102 | # |
---|
| 103 | # the printer option has priority when both are defined. |
---|
| 104 | # |
---|
[800] | 105 | # Revision 1.14 2003/02/17 22:05:50 jalet |
---|
| 106 | # Storage backend now supports admin and user passwords (untested) |
---|
| 107 | # |
---|
[789] | 108 | # Revision 1.13 2003/02/10 11:47:39 jalet |
---|
| 109 | # Moved some code down into the requesters |
---|
| 110 | # |
---|
[786] | 111 | # Revision 1.12 2003/02/10 10:36:33 jalet |
---|
| 112 | # Small problem wrt external requester |
---|
| 113 | # |
---|
[785] | 114 | # Revision 1.11 2003/02/10 08:50:45 jalet |
---|
| 115 | # External requester seems to be finally ok now |
---|
| 116 | # |
---|
[783] | 117 | # Revision 1.10 2003/02/10 08:19:57 jalet |
---|
| 118 | # tell ConfigParser to return raw data, this allows our own strings |
---|
| 119 | # interpolations in the requester |
---|
| 120 | # |
---|
[781] | 121 | # Revision 1.9 2003/02/10 00:44:38 jalet |
---|
| 122 | # Typos |
---|
| 123 | # |
---|
[780] | 124 | # Revision 1.8 2003/02/10 00:42:17 jalet |
---|
| 125 | # External requester should be ok (untested) |
---|
| 126 | # New syntax for configuration file wrt requesters |
---|
| 127 | # |
---|
[773] | 128 | # Revision 1.7 2003/02/09 13:05:43 jalet |
---|
| 129 | # Internationalization continues... |
---|
| 130 | # |
---|
[747] | 131 | # Revision 1.6 2003/02/07 22:00:09 jalet |
---|
| 132 | # Bad cut&paste |
---|
| 133 | # |
---|
[731] | 134 | # Revision 1.5 2003/02/06 23:58:05 jalet |
---|
| 135 | # repykota should be ok |
---|
| 136 | # |
---|
[713] | 137 | # Revision 1.4 2003/02/06 09:19:02 jalet |
---|
| 138 | # More robust behavior (hopefully) when the user or printer is not managed |
---|
| 139 | # correctly by the Quota System : e.g. cupsFilter added in ppd file, but |
---|
| 140 | # printer and/or user not 'yet?' in storage. |
---|
| 141 | # |
---|
[708] | 142 | # Revision 1.3 2003/02/05 23:26:22 jalet |
---|
| 143 | # Incorrect handling of grace delay |
---|
| 144 | # |
---|
[707] | 145 | # Revision 1.2 2003/02/05 23:09:20 jalet |
---|
| 146 | # Name conflict |
---|
| 147 | # |
---|
[695] | 148 | # Revision 1.1 2003/02/05 21:28:17 jalet |
---|
| 149 | # Initial import into CVS |
---|
| 150 | # |
---|
| 151 | # |
---|
| 152 | # |
---|
| 153 | |
---|
| 154 | import os |
---|
| 155 | import ConfigParser |
---|
| 156 | |
---|
| 157 | class PyKotaConfigError(Exception): |
---|
| 158 | """An exception for PyKota config related stuff.""" |
---|
| 159 | def __init__(self, message = ""): |
---|
| 160 | self.message = message |
---|
| 161 | Exception.__init__(self, message) |
---|
| 162 | def __repr__(self): |
---|
| 163 | return self.message |
---|
| 164 | __str__ = __repr__ |
---|
| 165 | |
---|
| 166 | class PyKotaConfig : |
---|
| 167 | """A class to deal with PyKota's configuration.""" |
---|
| 168 | def __init__(self, directory) : |
---|
| 169 | """Reads and checks the configuration file.""" |
---|
| 170 | self.filename = os.path.join(directory, "pykota.conf") |
---|
[872] | 171 | if not os.path.isfile(self.filename) : |
---|
| 172 | raise PyKotaConfigError, _("Configuration file %s not found.") % self.filename |
---|
[695] | 173 | self.config = ConfigParser.ConfigParser() |
---|
| 174 | self.config.read([self.filename]) |
---|
| 175 | |
---|
| 176 | def getPrinterNames(self) : |
---|
| 177 | """Returns the list of configured printers, i.e. all sections names minus 'global'.""" |
---|
| 178 | return [pname for pname in self.config.sections() if pname != "global"] |
---|
| 179 | |
---|
[802] | 180 | def getGlobalOption(self, option, ignore=0) : |
---|
| 181 | """Returns an option from the global section, or raises a PyKotaConfigError if ignore is not set, else returns None.""" |
---|
| 182 | try : |
---|
| 183 | return self.config.get("global", option, raw=1) |
---|
| 184 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
| 185 | if ignore : |
---|
| 186 | return |
---|
| 187 | else : |
---|
| 188 | raise PyKotaConfigError, _("Option %s not found in section global of %s") % (option, self.filename) |
---|
| 189 | |
---|
| 190 | def getPrinterOption(self, printer, option) : |
---|
| 191 | """Returns an option from the printer section, or the global section, or raises a PyKotaConfigError.""" |
---|
| 192 | globaloption = self.getGlobalOption(option, ignore=1) |
---|
| 193 | try : |
---|
| 194 | return self.config.get(printer, option, raw=1) |
---|
| 195 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
| 196 | if globaloption is not None : |
---|
| 197 | return globaloption |
---|
| 198 | else : |
---|
| 199 | raise PyKotaConfigError, _("Option %s not found in section %s of %s") % (option, printer, self.filename) |
---|
| 200 | |
---|
[695] | 201 | def getStorageBackend(self) : |
---|
[800] | 202 | """Returns the storage backend information as a Python mapping.""" |
---|
| 203 | backendinfo = {} |
---|
[695] | 204 | for option in [ "storagebackend", "storageserver", \ |
---|
[1087] | 205 | "storagename", "storageuser", \ |
---|
[695] | 206 | ] : |
---|
[804] | 207 | backendinfo[option] = self.getGlobalOption(option) |
---|
[1087] | 208 | backendinfo["storageuserpw"] = self.getGlobalOption("storageuserpw", ignore=1) # password is optional |
---|
| 209 | backendinfo["storageadmin"] = None |
---|
| 210 | backendinfo["storageadminpw"] = None |
---|
| 211 | adminconf = ConfigParser.ConfigParser() |
---|
| 212 | adminconf.read(["/etc/pykota/pykotadmin.conf"]) |
---|
| 213 | if adminconf.sections() : # were we able to read the file ? |
---|
| 214 | try : |
---|
| 215 | backendinfo["storageadmin"] = adminconf.get("global", "storageadmin", raw=1) |
---|
| 216 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
| 217 | raise PyKotaConfigError, _("Option %s not found in section global of %s") % ("storageadmin", "/etc/pykota/pykotadmin.conf") |
---|
| 218 | try : |
---|
| 219 | backendinfo["storageadminpw"] = adminconf.get("global", "storageadminpw", raw=1) |
---|
| 220 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
| 221 | pass # Password is optional |
---|
[800] | 222 | return backendinfo |
---|
[695] | 223 | |
---|
[1029] | 224 | def getLDAPInfo(self) : |
---|
| 225 | """Returns some hints for the LDAP backend.""" |
---|
| 226 | ldapinfo = {} |
---|
| 227 | for option in [ "userbase", "userrdn", \ |
---|
[1041] | 228 | "balancebase", "balancerdn", \ |
---|
[1029] | 229 | "groupbase", "grouprdn", "groupmembers", \ |
---|
| 230 | "printerbase", "printerrdn", \ |
---|
| 231 | "userquotabase", "groupquotabase", \ |
---|
| 232 | "jobbase", "lastjobbase", \ |
---|
| 233 | ] : |
---|
| 234 | ldapinfo[option] = self.getGlobalOption(option) |
---|
| 235 | return ldapinfo |
---|
| 236 | |
---|
[695] | 237 | def getLoggingBackend(self) : |
---|
| 238 | """Returns the logging backend information.""" |
---|
[802] | 239 | validloggers = [ "stderr", "system" ] |
---|
[853] | 240 | try : |
---|
| 241 | logger = self.getGlobalOption("logger").lower() |
---|
| 242 | except PyKotaConfigError : |
---|
| 243 | logger = "system" |
---|
[802] | 244 | if logger not in validloggers : |
---|
| 245 | raise PyKotaConfigError, _("Option logger only supports values in %s") % str(validloggers) |
---|
| 246 | return logger |
---|
[695] | 247 | |
---|
[973] | 248 | def getAccounterBackend(self, printer) : |
---|
| 249 | """Returns the accounter backend to use for a given printer. |
---|
| 250 | |
---|
| 251 | if it is not set, it defaults to 'querying' which means ask printer |
---|
| 252 | for its internal lifetime page counter. |
---|
| 253 | """ |
---|
[980] | 254 | validaccounters = [ "querying", "stupid", "external" ] |
---|
[973] | 255 | try : |
---|
[1000] | 256 | fullaccounter = self.getPrinterOption(printer, "accounter").strip() |
---|
[973] | 257 | except PyKotaConfigError : |
---|
[980] | 258 | fullaccounter = "querying" |
---|
[1000] | 259 | if fullaccounter.lower().startswith("external") : |
---|
[980] | 260 | try : |
---|
| 261 | (accounter, args) = [x.strip() for x in fullaccounter.split('(', 1)] |
---|
| 262 | except ValueError : |
---|
| 263 | raise PyKotaConfigError, _("Invalid external accounter %s for printer %s") % (fullaccounter, printer) |
---|
| 264 | if args.endswith(')') : |
---|
| 265 | args = args[:-1] |
---|
| 266 | if not args : |
---|
| 267 | raise PyKotaConfigError, _("Invalid external accounter %s for printer %s") % (fullaccounter, printer) |
---|
[1000] | 268 | return (accounter.lower(), args) |
---|
| 269 | elif fullaccounter.lower() not in validaccounters : |
---|
[973] | 270 | raise PyKotaConfigError, _("Option accounter in section %s only supports values in %s") % (printer, str(validaccounters)) |
---|
[980] | 271 | else : |
---|
[1000] | 272 | return (fullaccounter.lower(), None) |
---|
[973] | 273 | |
---|
[695] | 274 | def getRequesterBackend(self, printer) : |
---|
[780] | 275 | """Returns the requester backend to use for a given printer, with its arguments.""" |
---|
[802] | 276 | try : |
---|
[973] | 277 | fullrequester = self.getPrinterOption(printer, "requester") |
---|
| 278 | except PyKotaConfigError : |
---|
| 279 | # No requester defined, maybe it is not needed if accounting method |
---|
| 280 | # is not set to 'querying', but if we are called, then the accounting |
---|
| 281 | # method really IS 'querying', and so there's a big problem. |
---|
| 282 | raise PyKotaConfigError, _("Option requester for printer %s was not set") % printer |
---|
| 283 | else : |
---|
| 284 | try : |
---|
| 285 | (requester, args) = [x.strip() for x in fullrequester.split('(', 1)] |
---|
| 286 | except ValueError : |
---|
| 287 | raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printer) |
---|
| 288 | if args.endswith(')') : |
---|
| 289 | args = args[:-1] |
---|
| 290 | if not args : |
---|
| 291 | raise PyKotaConfigError, _("Invalid requester %s for printer %s") % (fullrequester, printer) |
---|
| 292 | validrequesters = [ "snmp", "external" ] # TODO : add more requesters |
---|
[1000] | 293 | requester = requester.lower() |
---|
[973] | 294 | if requester not in validrequesters : |
---|
| 295 | raise PyKotaConfigError, _("Option requester for printer %s only supports values in %s") % (printer, str(validrequesters)) |
---|
| 296 | return (requester, args) |
---|
[695] | 297 | |
---|
| 298 | def getPrinterPolicy(self, printer) : |
---|
| 299 | """Returns the default policy for the current printer.""" |
---|
[802] | 300 | validpolicies = [ "ALLOW", "DENY" ] |
---|
[853] | 301 | try : |
---|
| 302 | policy = self.getPrinterOption(printer, "policy").upper() |
---|
| 303 | except PyKotaConfigError : |
---|
[956] | 304 | policy = "DENY" |
---|
[802] | 305 | if policy not in validpolicies : |
---|
| 306 | raise PyKotaConfigError, _("Option policy in section %s only supports values in %s") % (printer, str(validpolicies)) |
---|
| 307 | return policy |
---|
[695] | 308 | |
---|
| 309 | def getSMTPServer(self) : |
---|
| 310 | """Returns the SMTP server to use to send messages to users.""" |
---|
[853] | 311 | try : |
---|
| 312 | return self.getGlobalOption("smtpserver") |
---|
| 313 | except PyKotaConfigError : |
---|
| 314 | return "localhost" |
---|
[695] | 315 | |
---|
[802] | 316 | def getAdminMail(self, printer) : |
---|
[695] | 317 | """Returns the Email address of the Print Quota Administrator.""" |
---|
[853] | 318 | try : |
---|
| 319 | return self.getPrinterOption(printer, "adminmail") |
---|
| 320 | except PyKotaConfigError : |
---|
| 321 | return "root@localhost" |
---|
[695] | 322 | |
---|
[802] | 323 | def getAdmin(self, printer) : |
---|
[695] | 324 | """Returns the full name of the Print Quota Administrator.""" |
---|
[853] | 325 | try : |
---|
| 326 | return self.getPrinterOption(printer, "admin") |
---|
| 327 | except PyKotaConfigError : |
---|
| 328 | return "root" |
---|
[708] | 329 | |
---|
[852] | 330 | def getMailTo(self, printer) : |
---|
| 331 | """Returns the recipient of email messages.""" |
---|
[854] | 332 | validmailtos = [ "NOBODY", "NONE", "NOONE", "BITBUCKET", "DEVNULL", "BOTH", "USER", "ADMIN" ] |
---|
[853] | 333 | try : |
---|
| 334 | mailto = self.getPrinterOption(printer, "mailto").upper() |
---|
| 335 | except PyKotaConfigError : |
---|
| 336 | mailto = "BOTH" |
---|
[852] | 337 | if mailto not in validmailtos : |
---|
| 338 | raise PyKotaConfigError, _("Option mailto in section %s only supports values in %s") % (printer, str(validmailtos)) |
---|
| 339 | return mailto |
---|
| 340 | |
---|
[804] | 341 | def getGraceDelay(self, printer) : |
---|
[708] | 342 | """Returns the grace delay in days.""" |
---|
[731] | 343 | try : |
---|
[853] | 344 | gd = self.getPrinterOption(printer, "gracedelay") |
---|
| 345 | except PyKotaConfigError : |
---|
| 346 | gd = 7 |
---|
| 347 | try : |
---|
[731] | 348 | return int(gd) |
---|
[1077] | 349 | except (TypeError, ValueError) : |
---|
[773] | 350 | raise PyKotaConfigError, _("Invalid grace delay %s") % gd |
---|
[1021] | 351 | |
---|
[1077] | 352 | def getPoorMan(self) : |
---|
| 353 | """Returns the poor man's treshold.""" |
---|
| 354 | try : |
---|
| 355 | pm = self.getGlobalOption("poorman") |
---|
| 356 | except PyKotaConfigError : |
---|
| 357 | pm = 1.0 |
---|
| 358 | try : |
---|
| 359 | return float(pm) |
---|
| 360 | except (TypeError, ValueError) : |
---|
| 361 | raise PyKotaConfigError, _("Invalid poor man's treshold %s") % pm |
---|
| 362 | |
---|
| 363 | def getPoorWarn(self) : |
---|
| 364 | """Returns the poor man's warning message.""" |
---|
| 365 | try : |
---|
| 366 | return self.getGlobalOption("poorwarn") |
---|
| 367 | except PyKotaConfigError : |
---|
| 368 | return _("Your Print Quota account balance is Low.\nSoon you'll not be allowed to print anymore.\nPlease contact the Print Quota Administrator to solve the problem.") |
---|
| 369 | |
---|
| 370 | def getHardWarn(self, printer) : |
---|
| 371 | """Returns the hard limit error message.""" |
---|
| 372 | try : |
---|
| 373 | return self.getPrinterOption(printer, "hardwarn") |
---|
| 374 | except PyKotaConfigError : |
---|
| 375 | return _("You are not allowed to print anymore because\nyour Print Quota is exceeded on printer %s.") % printer |
---|
| 376 | |
---|
| 377 | def getSoftWarn(self, printer) : |
---|
| 378 | """Returns the soft limit error message.""" |
---|
| 379 | try : |
---|
| 380 | return self.getPrinterOption(printer, "softwarn") |
---|
| 381 | except PyKotaConfigError : |
---|
| 382 | return _("You will soon be forbidden to print anymore because\nyour Print Quota is almost reached on printer %s.") % printer |
---|
| 383 | |
---|
[1021] | 384 | def getDebug(self) : |
---|
| 385 | """Returns 1 if debugging is activated, else 0.""" |
---|
| 386 | debug = self.getGlobalOption("debug", ignore=1) |
---|
| 387 | if (debug is not None) and (debug.upper().strip() in ['Y', 'YES', '1', 'ON', 'O']) : |
---|
| 388 | return 1 |
---|
| 389 | else : |
---|
| 390 | return 0 |
---|