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$ |
---|
26 | # Revision 1.1 2004/07/17 20:37:27 jalet |
---|
27 | # Missing file... Am I really stupid ? |
---|
28 | # |
---|
29 | # |
---|
30 | # |
---|
31 | |
---|
32 | import sys |
---|
33 | import os |
---|
34 | |
---|
35 | from pykota.tool import PyKotaFilterOrBackend, PyKotaToolError, crashed |
---|
36 | from pykota.config import PyKotaConfigError |
---|
37 | from pykota.storage import PyKotaStorageError |
---|
38 | from pykota.accounter import PyKotaAccounterError |
---|
39 | |
---|
40 | # Exit codes |
---|
41 | JSUCC = 0 # filter succeeded |
---|
42 | JFAIL = 1 # filter failed, print server should retry later |
---|
43 | JABORT = 2 # filter failed, print server should suspend the queue |
---|
44 | JREMOVE = 3 # job will be removed from print queue |
---|
45 | JHOLD = 6 # job will be prevented from printing until lpc release |
---|
46 | |
---|
47 | # Environment variables |
---|
48 | # PRINTER = printer name |
---|
49 | # PRINTCAP_ENTRY = complete printcap entry for this printer |
---|
50 | # HF = job hold file contents |
---|
51 | # SPOOL_DIR = spool directory |
---|
52 | |
---|
53 | # HF contains df_name which is DataFile_Name created in SPOOL_DIR |
---|
54 | |
---|
55 | class PyKotaFilter(PyKotaFilterOrBackend) : |
---|
56 | """A class for the pykota filter for LPRng.""" |
---|
57 | def acceptJob(self) : |
---|
58 | """Returns the appropriate exit code to tell LPRng all is OK.""" |
---|
59 | return JSUCC |
---|
60 | |
---|
61 | def removeJob(self) : |
---|
62 | """Returns the appropriate exit code to tell LPRng job has to be removed.""" |
---|
63 | return JREMOVE |
---|
64 | |
---|
65 | def getJobOriginatingHostname(self, printername, username, jobid) : |
---|
66 | """Retrieves the job-originating-hostname if possible.""" |
---|
67 | try : |
---|
68 | return [line[11:] for line in os.environ.get("HF", "").split() if line.startswith("remotehost=")][0] |
---|
69 | except IndexError : |
---|
70 | return None |
---|
71 | |
---|
72 | def doWork(self, policy, printer, user, userpquota) : |
---|
73 | """Most of the work is done here.""" |
---|
74 | # Two different values possible for policy here : |
---|
75 | # ALLOW means : Either printer, user or user print quota doesn't exist, |
---|
76 | # but the job should be allowed anyway. |
---|
77 | # OK means : Both printer, user and user print quota exist, job should |
---|
78 | # be allowed if current user is allowed to print on this printer |
---|
79 | if policy == "ALLOW" : |
---|
80 | # nothing to do, just accept the job |
---|
81 | return self.acceptJob() |
---|
82 | else : |
---|
83 | # exports user information with initial values |
---|
84 | self.exportUserInfo(userpquota) |
---|
85 | |
---|
86 | if (not printer.LastJob.Exists) or (printer.LastJob.JobId != self.jobid) : |
---|
87 | # Last job for current printer has a different JobId than |
---|
88 | # the current job, so we know we are in the first pass |
---|
89 | # tries to extract job-originating-hostname |
---|
90 | clienthost = self.getJobOriginatingHostname(printer.Name, user.Name, self.jobid) |
---|
91 | self.logdebug("Client Hostname : %s" % (clienthost or "Unknown")) |
---|
92 | os.environ["PYKOTAJOBORIGINATINGHOSTNAME"] = str(clienthost or "") |
---|
93 | |
---|
94 | # indicates first pass |
---|
95 | os.environ["PYKOTAPHASE"] = "BEFORE" |
---|
96 | |
---|
97 | # do we want strict or laxist quota enforcement ? |
---|
98 | if self.config.getPrinterEnforcement(printer.Name) == "STRICT" : |
---|
99 | self.softwareJobSize = self.precomputeJobSize() |
---|
100 | self.softwareJobPrice = userpquota.computeJobPrice(self.softwareJobSize) |
---|
101 | self.sendBackChannelData("Precomputed job's size is %s pages, price is %s units" % (self.softwareJobSize, self.softwareJobPrice)) |
---|
102 | os.environ["PYKOTAPRECOMPUTEDJOBSIZE"] = str(self.softwareJobSize) |
---|
103 | os.environ["PYKOTAPRECOMPUTEDJOBPRICE"] = str(self.softwareJobPrice) |
---|
104 | |
---|
105 | # checks the user's quota |
---|
106 | action = self.warnUserPQuota(userpquota) |
---|
107 | |
---|
108 | # if no data to pass to real backend, probably a filter |
---|
109 | # higher in the chain failed because of a misconfiguration. |
---|
110 | # we deny the job in this case (nothing to print anyway) |
---|
111 | if not self.jobSizeBytes : |
---|
112 | self.printInfo(_("Job contains no data. Printing is denied."), "warn") |
---|
113 | action = "DENY" |
---|
114 | |
---|
115 | # exports some new environment variables |
---|
116 | os.environ["PYKOTAACTION"] = action |
---|
117 | |
---|
118 | # launches the pre hook |
---|
119 | self.prehook(userpquota) |
---|
120 | |
---|
121 | self.sendBackChannelData("Job accounting begins.") |
---|
122 | self.accounter.beginJob(userpquota) |
---|
123 | |
---|
124 | jobsize = None |
---|
125 | if self.accounter.isSoftware : |
---|
126 | self.accounter.endJob(userpquota) |
---|
127 | jobsize = self.accounter.getJobSize() |
---|
128 | self.sendBackChannelData("Job accounting ends.") |
---|
129 | |
---|
130 | if action == "DENY" : |
---|
131 | jobsize = 0 |
---|
132 | self.sendBackChannelData("Job size forced to 0 because printing was denied.") |
---|
133 | |
---|
134 | if jobsize is not None : |
---|
135 | # update the quota for the current user on this printer |
---|
136 | self.sendBackChannelData("Job size : %i" % jobsize) |
---|
137 | self.sendBackChannelData("Updating user %s's quota on printer %s" % (user.Name, printer.Name)) |
---|
138 | jobprice = userpquota.increasePagesUsage(jobsize) |
---|
139 | |
---|
140 | printer.addJobToHistory(self.jobid, user, self.accounter.getLastPageCounter(), action, jobsize, jobprice, self.preserveinputfile, self.title, self.copies, self.options, clienthost, self.jobSizeBytes) |
---|
141 | self.sendBackChannelData("Job added to history.") |
---|
142 | |
---|
143 | # exports some new environment variables |
---|
144 | os.environ["PYKOTAPHASE"] = "AFTER" |
---|
145 | os.environ["PYKOTAJOBSIZE"] = str(jobsize) |
---|
146 | os.environ["PYKOTAJOBPRICE"] = str(jobprice) |
---|
147 | |
---|
148 | # then re-export user information with new value |
---|
149 | self.exportUserInfo(userpquota) |
---|
150 | |
---|
151 | # Launches the post hook |
---|
152 | self.posthook(userpquota) |
---|
153 | |
---|
154 | # here software accounting was completed. |
---|
155 | else : |
---|
156 | 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) |
---|
157 | self.sendBackChannelData("Job added to history during first pass : Job's size and price are still unknown.") |
---|
158 | |
---|
159 | if action == "DENY" : |
---|
160 | return self.removeJob() |
---|
161 | else : |
---|
162 | return self.acceptJob() |
---|
163 | else : |
---|
164 | # Last job for current printer has the same JobId than |
---|
165 | # the current job, so we know we are in the second pass |
---|
166 | if self.accounter.isSoftware : |
---|
167 | # Software accounting method was used, and we are |
---|
168 | # in second pass, so all work is already done, |
---|
169 | # now we just have to exit successfully |
---|
170 | self.printInfo("Software accounting already done in first pass. Exiting.") |
---|
171 | else : |
---|
172 | # Now we have to check if accounting was completely finished : |
---|
173 | # if it is, then a big problem occured because with hardware |
---|
174 | # accounting, only the second pass can know the job's size. |
---|
175 | if (printer.LastJob.JobSize is not None) : |
---|
176 | raise PyKotaToolError, "Hardware accounting already finished ! This should be impossible, please report this problem ASAP." |
---|
177 | |
---|
178 | # indicate phase change |
---|
179 | os.environ["PYKOTAPHASE"] = "AFTER" |
---|
180 | |
---|
181 | # stops accounting. |
---|
182 | self.accounter.endJob(userpquota) |
---|
183 | self.sendBackChannelData("Job accounting ends.") |
---|
184 | |
---|
185 | # retrieve the job size |
---|
186 | jobsize = self.accounter.getJobSize() |
---|
187 | self.sendBackChannelData("Job size : %i" % jobsize) |
---|
188 | raise PyKotaToolError, "Not implemented !" |
---|
189 | return self.acceptJob() |
---|
190 | |
---|
191 | if __name__ == "__main__" : |
---|
192 | retcode = JSUCC |
---|
193 | try : |
---|
194 | try : |
---|
195 | # Initializes the backend |
---|
196 | kotabackend = PyKotaFilter() |
---|
197 | except SystemExit : |
---|
198 | retcode = JABORT |
---|
199 | except : |
---|
200 | crashed("lprngpykota filter initialization failed") |
---|
201 | retcode = JABORT |
---|
202 | else : |
---|
203 | retcode = kotabackend.mainWork() |
---|
204 | kotabackend.storage.close() |
---|
205 | kotabackend.closeJobDataStream() |
---|
206 | except : |
---|
207 | try : |
---|
208 | kotabackend.crashed("lprngpykota filter failed") |
---|
209 | except : |
---|
210 | crashed("lprngpykota filter failed") |
---|
211 | retcode = JABORT |
---|
212 | |
---|
213 | sys.exit(retcode) |
---|