[1601] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
| 4 | # LPRngPyKota accounting filter |
---|
| 5 | # |
---|
| 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 7 | # |
---|
| 8 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
| 9 | # This program is free software; you can redistribute it and/or modify |
---|
| 10 | # it under the terms of the GNU General Public License as published by |
---|
| 11 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 12 | # (at your option) any later version. |
---|
| 13 | # |
---|
| 14 | # This program is distributed in the hope that it will be useful, |
---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | # GNU General Public License for more details. |
---|
| 18 | # |
---|
| 19 | # You should have received a copy of the GNU General Public License |
---|
| 20 | # along with this program; if not, write to the Free Software |
---|
| 21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
| 25 | # $Log$ |
---|
[1820] | 26 | # Revision 1.8 2004/10/13 20:51:27 jalet |
---|
| 27 | # Made debugging levels be the same in cupspykota and lprngpykota. |
---|
| 28 | # Now outputs more information in informational messages : user, printer, jobid |
---|
| 29 | # |
---|
[1713] | 30 | # Revision 1.7 2004/09/13 16:02:44 jalet |
---|
| 31 | # Added fix for incorrect job's size when hardware accounting fails |
---|
| 32 | # |
---|
[1696] | 33 | # Revision 1.6 2004/09/02 14:40:13 jalet |
---|
| 34 | # Another bunch of LPRng fixes |
---|
| 35 | # |
---|
[1629] | 36 | # Revision 1.5 2004/07/23 11:19:48 jalet |
---|
| 37 | # 1.19beta is out ! |
---|
| 38 | # |
---|
[1624] | 39 | # Revision 1.4 2004/07/22 22:41:48 jalet |
---|
| 40 | # Hardware accounting for LPRng should be OK now. UNTESTED. |
---|
| 41 | # |
---|
[1611] | 42 | # Revision 1.3 2004/07/21 09:35:48 jalet |
---|
| 43 | # Software accounting seems to be OK with LPRng support now |
---|
| 44 | # |
---|
[1609] | 45 | # Revision 1.2 2004/07/20 22:47:38 jalet |
---|
| 46 | # Sanitizing |
---|
| 47 | # |
---|
[1601] | 48 | # Revision 1.1 2004/07/17 20:37:27 jalet |
---|
| 49 | # Missing file... Am I really stupid ? |
---|
| 50 | # |
---|
| 51 | # |
---|
| 52 | # |
---|
| 53 | |
---|
| 54 | import sys |
---|
| 55 | import os |
---|
| 56 | |
---|
| 57 | from pykota.tool import PyKotaFilterOrBackend, PyKotaToolError, crashed |
---|
| 58 | from pykota.config import PyKotaConfigError |
---|
| 59 | from pykota.storage import PyKotaStorageError |
---|
| 60 | from pykota.accounter import PyKotaAccounterError |
---|
| 61 | |
---|
| 62 | # Exit codes |
---|
| 63 | JSUCC = 0 # filter succeeded |
---|
| 64 | JFAIL = 1 # filter failed, print server should retry later |
---|
| 65 | JABORT = 2 # filter failed, print server should suspend the queue |
---|
| 66 | JREMOVE = 3 # job will be removed from print queue |
---|
| 67 | JHOLD = 6 # job will be prevented from printing until lpc release |
---|
| 68 | |
---|
| 69 | # Environment variables |
---|
| 70 | # PRINTER = printer name |
---|
| 71 | # PRINTCAP_ENTRY = complete printcap entry for this printer |
---|
| 72 | # HF = job hold file contents |
---|
| 73 | # SPOOL_DIR = spool directory |
---|
| 74 | |
---|
| 75 | # HF contains df_name which is DataFile_Name created in SPOOL_DIR |
---|
| 76 | |
---|
| 77 | class PyKotaFilter(PyKotaFilterOrBackend) : |
---|
| 78 | """A class for the pykota filter for LPRng.""" |
---|
| 79 | def acceptJob(self) : |
---|
| 80 | """Returns the appropriate exit code to tell LPRng all is OK.""" |
---|
| 81 | return JSUCC |
---|
| 82 | |
---|
| 83 | def removeJob(self) : |
---|
| 84 | """Returns the appropriate exit code to tell LPRng job has to be removed.""" |
---|
| 85 | return JREMOVE |
---|
| 86 | |
---|
| 87 | def getJobOriginatingHostname(self, printername, username, jobid) : |
---|
| 88 | """Retrieves the job-originating-hostname if possible.""" |
---|
| 89 | try : |
---|
| 90 | return [line[11:] for line in os.environ.get("HF", "").split() if line.startswith("remotehost=")][0] |
---|
| 91 | except IndexError : |
---|
[1696] | 92 | try : |
---|
| 93 | return [line[1:] for line in os.environ.get("CONTROL", "").split() if line.startswith("H")][0] |
---|
| 94 | except IndexError : |
---|
| 95 | return None |
---|
[1601] | 96 | |
---|
[1611] | 97 | def firstPass(self, policy, printer, user, userpquota) : |
---|
| 98 | """First pass done here.""" |
---|
| 99 | # first we have to check if previous job was correctly accounted for |
---|
[1629] | 100 | if printer.LastJob.Exists and not printer.LastJob.JobSize : |
---|
[1611] | 101 | # here we know that previous job wasn't accounted for correctly |
---|
| 102 | # we are sure (?) that it was hardware accounting which was used |
---|
| 103 | # and that the second pass didn't work or wasn't even launched |
---|
| 104 | # we know have to act just as if we were in second pass |
---|
[1624] | 105 | # for previous user on this printer, then we will continue |
---|
| 106 | # with normal processing of current user. |
---|
| 107 | self.secondPass(policy, printer, None, None) |
---|
[1611] | 108 | |
---|
[1624] | 109 | # export user info with initial values |
---|
| 110 | self.exportUserInfo(userpquota) |
---|
| 111 | |
---|
[1611] | 112 | # tries to extract job-originating-hostname |
---|
| 113 | clienthost = self.getJobOriginatingHostname(printer.Name, user.Name, self.jobid) |
---|
| 114 | self.logdebug("Client Hostname : %s" % (clienthost or "Unknown")) |
---|
| 115 | os.environ["PYKOTAJOBORIGINATINGHOSTNAME"] = str(clienthost or "") |
---|
| 116 | |
---|
| 117 | # indicates first pass |
---|
| 118 | os.environ["PYKOTAPHASE"] = "BEFORE" |
---|
| 119 | |
---|
| 120 | # do we want strict or laxist quota enforcement ? |
---|
| 121 | if self.config.getPrinterEnforcement(printer.Name) == "STRICT" : |
---|
| 122 | self.softwareJobSize = self.precomputeJobSize() |
---|
| 123 | self.softwareJobPrice = userpquota.computeJobPrice(self.softwareJobSize) |
---|
| 124 | self.logdebug("Precomputed job's size is %s pages, price is %s units" % (self.softwareJobSize, self.softwareJobPrice)) |
---|
| 125 | os.environ["PYKOTAPRECOMPUTEDJOBSIZE"] = str(self.softwareJobSize) |
---|
| 126 | os.environ["PYKOTAPRECOMPUTEDJOBPRICE"] = str(self.softwareJobPrice) |
---|
| 127 | |
---|
| 128 | # if no data to pass to real backend, probably a filter |
---|
| 129 | # higher in the chain failed because of a misconfiguration. |
---|
| 130 | # we deny the job in this case (nothing to print anyway) |
---|
| 131 | if not self.jobSizeBytes : |
---|
[1820] | 132 | self.printMoreInfo(user, printer, _("Job contains no data. Printing is denied."), "warn") |
---|
[1611] | 133 | action = "DENY" |
---|
| 134 | else : |
---|
| 135 | # checks the user's quota |
---|
| 136 | action = self.warnUserPQuota(userpquota) |
---|
| 137 | |
---|
| 138 | # exports some new environment variables |
---|
| 139 | os.environ["PYKOTAACTION"] = action |
---|
| 140 | |
---|
| 141 | # launches the pre hook |
---|
| 142 | self.prehook(userpquota) |
---|
| 143 | |
---|
[1820] | 144 | self.printMoreInfo(user, printer, _("Job accounting begins.")) |
---|
[1624] | 145 | self.accounter.beginJob(printer) |
---|
[1611] | 146 | |
---|
| 147 | jobsize = None |
---|
| 148 | if self.accounter.isSoftware : |
---|
[1624] | 149 | self.accounter.endJob(printer) |
---|
[1713] | 150 | jobsize = self.accounter.getJobSize(printer) |
---|
[1820] | 151 | self.printMoreInfo(user, printer, _("Job accounting ends.")) |
---|
[1611] | 152 | |
---|
| 153 | if action == "DENY" : |
---|
| 154 | jobsize = 0 |
---|
[1820] | 155 | self.printMoreInfo(user, printer, _("Job size forced to 0 because printing is denied.")) |
---|
[1611] | 156 | |
---|
[1629] | 157 | if (self.accounter.isSoftware) or (action == "DENY") : |
---|
[1611] | 158 | # update the quota for the current user on this printer |
---|
[1820] | 159 | self.printMoreInfo(user, printer, _("Job size : %i") % jobsize) |
---|
[1611] | 160 | self.logdebug("Updating user %s's quota on printer %s" % (user.Name, printer.Name)) |
---|
| 161 | jobprice = userpquota.increasePagesUsage(jobsize) |
---|
| 162 | |
---|
| 163 | printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), action, jobsize, jobprice, self.preserveinputfile, self.title, self.copies, self.options, clienthost, self.jobSizeBytes) |
---|
[1820] | 164 | self.printMoreInfo(user, printer, _("Job added to history.")) |
---|
[1611] | 165 | |
---|
| 166 | # exports some new environment variables |
---|
| 167 | os.environ["PYKOTAPHASE"] = "AFTER" |
---|
| 168 | os.environ["PYKOTAJOBSIZE"] = str(jobsize) |
---|
| 169 | os.environ["PYKOTAJOBPRICE"] = str(jobprice) |
---|
| 170 | |
---|
| 171 | # then re-export user information with new value |
---|
| 172 | self.exportUserInfo(userpquota) |
---|
| 173 | |
---|
| 174 | # Launches the post hook |
---|
| 175 | self.posthook(userpquota) |
---|
| 176 | |
---|
[1629] | 177 | # here accounting was completed, either software, or hardware but over quota |
---|
[1611] | 178 | else : |
---|
| 179 | printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), action, filename=self.preserveinputfile, title=self.title, copies=self.copies, options=self.options, clienthost=clienthost, jobsizebytes=self.jobSizeBytes) |
---|
| 180 | self.logdebug("Job added to history during first pass : Job's size and price are still unknown.") |
---|
| 181 | |
---|
| 182 | if action == "DENY" : |
---|
| 183 | return self.removeJob() |
---|
| 184 | else : |
---|
| 185 | return self.acceptJob() |
---|
| 186 | |
---|
| 187 | def secondPass(self, policy, printer, user, userpquota) : |
---|
| 188 | """Second pass done here.""" |
---|
| 189 | # Last job for current printer has the same JobId than |
---|
| 190 | # the current job, so we know we are in the second pass |
---|
| 191 | if self.accounter.isSoftware : |
---|
| 192 | # Software accounting method was used, and we are |
---|
| 193 | # in second pass, so all work is already done, |
---|
| 194 | # now we just have to exit successfully |
---|
[1820] | 195 | self.printMoreInfo(user, printer, _("Software accounting already done in first pass. Ignoring.")) |
---|
[1629] | 196 | elif printer.LastJob.JobAction == "DENY" : |
---|
| 197 | # Hardware accounting method was used, but job |
---|
| 198 | # was rejected during first pass, so nothing to do |
---|
[1820] | 199 | self.printMoreInfo(user, printer, _("Hardware accounting already done in first pass. Ignoring.")) |
---|
[1611] | 200 | else : |
---|
[1624] | 201 | # here if user and userpquota are both None |
---|
| 202 | # then it's a special second pass for a job |
---|
| 203 | # which should have had one but didn't, so |
---|
| 204 | # we need to get the last user, not the current one. |
---|
| 205 | if (user is None) and (userpquota is None) : |
---|
| 206 | user = printer.LastJob.User |
---|
| 207 | userpquota = self.storage.getUserPQuota(user, printer) |
---|
| 208 | |
---|
| 209 | # exports user info for last user |
---|
| 210 | self.exportUserInfo(userpquota) |
---|
| 211 | |
---|
[1611] | 212 | # indicate phase change |
---|
| 213 | os.environ["PYKOTAPHASE"] = "AFTER" |
---|
| 214 | |
---|
[1624] | 215 | # fakes beginning of job with old page counter |
---|
| 216 | self.accounter.LastPageCounter = int(printer.LastJob.PrinterPageCounter or 0) |
---|
| 217 | self.accounter.fakeBeginJob() |
---|
| 218 | self.logdebug("Fakes beginning of job with LastPageCounter: %s" % self.accounter.getLastPageCounter()) |
---|
| 219 | |
---|
[1611] | 220 | # stops accounting. |
---|
[1624] | 221 | self.accounter.endJob(printer) |
---|
[1611] | 222 | self.logdebug("Job accounting ends.") |
---|
| 223 | |
---|
| 224 | # retrieve the job size |
---|
[1713] | 225 | jobsize = self.accounter.getJobSize(printer) |
---|
[1624] | 226 | |
---|
[1820] | 227 | self.printMoreInfo(user, printer, _("Job size : %i") % jobsize) |
---|
| 228 | self.printInfo(_("Updating user %s's quota on printer %s") % (user.Name, printer.Name)) |
---|
[1624] | 229 | jobprice = userpquota.increasePagesUsage(jobsize) |
---|
| 230 | |
---|
| 231 | self.storage.writeLastJobSize(printer.LastJob, jobsize, jobprice) |
---|
[1820] | 232 | self.printMoreInfo(user, printer, _("Job size and price now set in history.")) |
---|
[1624] | 233 | |
---|
| 234 | # exports some new environment variables |
---|
| 235 | os.environ["PYKOTAPHASE"] = "AFTER" |
---|
| 236 | os.environ["PYKOTAJOBSIZE"] = str(jobsize) |
---|
| 237 | os.environ["PYKOTAJOBPRICE"] = str(jobprice) |
---|
| 238 | |
---|
| 239 | # then re-export user information with new value |
---|
| 240 | self.exportUserInfo(userpquota) |
---|
| 241 | |
---|
| 242 | # Launches the post hook |
---|
| 243 | self.posthook(userpquota) |
---|
| 244 | |
---|
| 245 | # here hardware accounting was completed. |
---|
[1611] | 246 | return self.acceptJob() |
---|
| 247 | |
---|
[1601] | 248 | def doWork(self, policy, printer, user, userpquota) : |
---|
| 249 | """Most of the work is done here.""" |
---|
| 250 | # Two different values possible for policy here : |
---|
| 251 | # ALLOW means : Either printer, user or user print quota doesn't exist, |
---|
| 252 | # but the job should be allowed anyway. |
---|
| 253 | # OK means : Both printer, user and user print quota exist, job should |
---|
| 254 | # be allowed if current user is allowed to print on this printer |
---|
| 255 | if policy == "ALLOW" : |
---|
| 256 | # nothing to do, just accept the job |
---|
| 257 | return self.acceptJob() |
---|
| 258 | else : |
---|
[1696] | 259 | if printer.LastJob.Exists and (printer.LastJob.JobId == self.jobid) : |
---|
| 260 | # here we know we are in second pass. |
---|
| 261 | return self.secondPass(policy, printer, user, userpquota) |
---|
| 262 | else : |
---|
[1601] | 263 | # Last job for current printer has a different JobId than |
---|
[1611] | 264 | # the current job, so we know we are in the first pass. |
---|
| 265 | return self.firstPass(policy, printer, user, userpquota) |
---|
[1601] | 266 | |
---|
| 267 | if __name__ == "__main__" : |
---|
| 268 | retcode = JSUCC |
---|
| 269 | try : |
---|
| 270 | try : |
---|
| 271 | # Initializes the backend |
---|
| 272 | kotabackend = PyKotaFilter() |
---|
| 273 | except SystemExit : |
---|
| 274 | retcode = JABORT |
---|
| 275 | except : |
---|
| 276 | crashed("lprngpykota filter initialization failed") |
---|
| 277 | retcode = JABORT |
---|
| 278 | else : |
---|
| 279 | retcode = kotabackend.mainWork() |
---|
| 280 | kotabackend.storage.close() |
---|
| 281 | kotabackend.closeJobDataStream() |
---|
| 282 | except : |
---|
| 283 | try : |
---|
| 284 | kotabackend.crashed("lprngpykota filter failed") |
---|
| 285 | except : |
---|
| 286 | crashed("lprngpykota filter failed") |
---|
| 287 | retcode = JABORT |
---|
| 288 | |
---|
| 289 | sys.exit(retcode) |
---|