[2778] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
| 4 | # A notifier for PyKota |
---|
| 5 | # |
---|
| 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 7 | # |
---|
| 8 | # (c) 2003, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
| 25 | # |
---|
| 26 | |
---|
| 27 | import sys |
---|
[2780] | 28 | import socket |
---|
[3075] | 29 | import errno |
---|
[2799] | 30 | import signal |
---|
[2779] | 31 | import xmlrpclib |
---|
[2778] | 32 | |
---|
[2785] | 33 | try : |
---|
| 34 | import PAM |
---|
| 35 | except ImportError : |
---|
| 36 | hasPAM = 0 |
---|
| 37 | else : |
---|
| 38 | hasPAM = 1 |
---|
| 39 | |
---|
[3076] | 40 | from pykota.tool import Tool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ |
---|
[2778] | 41 | |
---|
| 42 | __doc__ = N_("""pknotify v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
| 43 | |
---|
| 44 | Notifies or ask questions to end users who launched the PyKotIcon application. |
---|
| 45 | |
---|
| 46 | command line usage : |
---|
| 47 | |
---|
| 48 | pknotify [options] [arguments] |
---|
| 49 | |
---|
| 50 | options : |
---|
| 51 | |
---|
[2822] | 52 | -v | --version Prints pknotify's version number then exits. |
---|
[2779] | 53 | -h | --help Prints this message then exits. |
---|
[2778] | 54 | |
---|
[2779] | 55 | -d | --destination h[:p] Sets the destination hostname and optional |
---|
| 56 | port onto which contact the remote PyKotIcon |
---|
| 57 | application. This option is mandatory. |
---|
| 58 | When not specified, the port defaults to 7654. |
---|
| 59 | |
---|
| 60 | -a | --ask Tells pknotify to ask something to the end |
---|
| 61 | user. Then pknotify will output the result. |
---|
| 62 | |
---|
[2786] | 63 | -C | --checkauth When --ask is used and both an 'username' and a |
---|
[2785] | 64 | 'password' are asked to the end user, then |
---|
| 65 | pknotify will try to authenticate the user |
---|
| 66 | through PAM. If authentified, this program |
---|
| 67 | will print "AUTH=YES", else "AUTH=NO". |
---|
| 68 | If a field is missing, "AUTH=IMPOSSIBLE" will |
---|
[2786] | 69 | be printed. If the user is authenticated, then |
---|
| 70 | "USERNAME=xxxx" will be printed as well. |
---|
[2785] | 71 | |
---|
[2779] | 72 | -c | --confirm Tells pknotify to ask for either a confirmation |
---|
| 73 | or abortion. |
---|
| 74 | |
---|
[2799] | 75 | -D | --denyafter N With --checkauth above, makes pknotify loop |
---|
| 76 | up to N times if the password is incorrect. |
---|
| 77 | After having reached the limit, "DENY" will |
---|
| 78 | be printed, which effectively rejects the job. |
---|
| 79 | The default value of N is 1, meaning the job |
---|
| 80 | is denied after the first unsuccessful try. |
---|
| 81 | |
---|
[3075] | 82 | -N | --noremote action If it's impossible to connect to the remote |
---|
| 83 | PyKotIcon machine, do this action instead. |
---|
| 84 | Allowed actions are 'CONTINUE' and 'CANCEL', |
---|
| 85 | which will respectively allow the processing |
---|
| 86 | of the print job to continue, or the job to |
---|
| 87 | be cancelled. The default value is CANCEL. |
---|
| 88 | |
---|
[2779] | 89 | -n | --notify Tells pknotify to send an informational message |
---|
[2823] | 90 | to the end user. |
---|
[2779] | 91 | |
---|
| 92 | -q | --quit Tells pknotify to send a message asking the |
---|
| 93 | PyKotIcon application to exit. This option can |
---|
| 94 | be combined with the other ones to make PyKotIcon |
---|
| 95 | exit after having sent the answer from the dialog. |
---|
| 96 | |
---|
[2799] | 97 | -t | --timeout T Tells pknotify to ignore the end user's answer if |
---|
[2824] | 98 | it comes past T seconds after the dialog box being |
---|
[2799] | 99 | opened. The default value is 0 seconds, which |
---|
| 100 | tells pknotify to wait indefinitely. |
---|
| 101 | Use this option to avoid having an user who |
---|
| 102 | leaved his computer stall a whole print queue. |
---|
| 103 | |
---|
[2779] | 104 | You MUST specify either --ask, --confirm, --notify or --quit. |
---|
| 105 | |
---|
| 106 | arguments : |
---|
| 107 | |
---|
[2825] | 108 | -a | --ask : Several arguments are accepted, of the form |
---|
[2779] | 109 | "label:varname:defaultvalue". The result will |
---|
| 110 | be printed to stdout in the following format : |
---|
| 111 | VAR1NAME=VAR1VALUE |
---|
| 112 | VAR2NAME=VAR2VALUE |
---|
| 113 | ... |
---|
[2785] | 114 | If the dialog was cancelled, nothing will be |
---|
[2779] | 115 | printed. If one of the varname is 'password' |
---|
| 116 | then this field is asked as a password (you won't |
---|
[2785] | 117 | see what you type in), and is NOT printed. Although |
---|
| 118 | it is not printed, it will be used to check if |
---|
| 119 | authentication is valid if you specify --checkauth. |
---|
[2779] | 120 | |
---|
| 121 | -c | --confirm : A single argument is expected, representing the |
---|
| 122 | message to display. If the dialog is confirmed |
---|
| 123 | then pknotify will print OK, else CANCEL. |
---|
| 124 | |
---|
| 125 | -n | --notify : A single argument is expected, representing the |
---|
| 126 | message to display. In this case pknotify will |
---|
| 127 | always print OK. |
---|
| 128 | |
---|
| 129 | examples : |
---|
| 130 | |
---|
[3075] | 131 | pknotify -d client:7654 --noremote CONTINUE --confirm "This job costs 10 credits" |
---|
[2779] | 132 | |
---|
[3075] | 133 | Would display the cost of the print job and asks for confirmation. |
---|
| 134 | If the end user doesn't have PyKotIcon running and accepting connections |
---|
| 135 | from the print server, PyKota will consider that the end user accepted |
---|
| 136 | to print this job. |
---|
[2785] | 137 | |
---|
| 138 | pknotify --destination $PYKOTAJOBORIGINATINGHOSTNAME:7654 \\ |
---|
| 139 | --checkauth --ask "Your name:username:" "Your password:password:" |
---|
| 140 | |
---|
| 141 | Asks an username and password, and checks if they are valid. |
---|
| 142 | NB : The PYKOTAJOBORIGINATINGHOSTNAME environment variable is |
---|
| 143 | only set if you launch pknotify from cupspykota through a directive |
---|
| 144 | in ~pykota/pykota.conf |
---|
[2799] | 145 | |
---|
| 146 | The TCP port you'll use must be reachable on the client from the |
---|
| 147 | print server. |
---|
[2778] | 148 | """) |
---|
| 149 | |
---|
[2799] | 150 | class TimeoutError(Exception) : |
---|
| 151 | """An exception for timeouts.""" |
---|
| 152 | def __init__(self, message = ""): |
---|
| 153 | self.message = message |
---|
| 154 | Exception.__init__(self, message) |
---|
| 155 | def __repr__(self): |
---|
| 156 | return self.message |
---|
| 157 | __str__ = __repr__ |
---|
| 158 | |
---|
[2778] | 159 | class PyKotaNotify(Tool) : |
---|
| 160 | """A class for pknotify.""" |
---|
[2779] | 161 | def sanitizeMessage(self, msg) : |
---|
| 162 | """Replaces \\n and returns a messagee in xmlrpclib Binary format.""" |
---|
[2804] | 163 | return xmlrpclib.Binary(self.userCharsetToUTF8(msg.replace("\\n", "\n"))) |
---|
[2779] | 164 | |
---|
[2802] | 165 | def convPAM(self, auth, queries=[], userdata=None) : |
---|
[2785] | 166 | """Prepares PAM datas.""" |
---|
| 167 | response = [] |
---|
| 168 | for (query, qtype) in queries : |
---|
| 169 | if qtype == PAM.PAM_PROMPT_ECHO_OFF : |
---|
| 170 | response.append((self.password, 0)) |
---|
| 171 | elif qtype in (PAM.PAM_PROMPT_ECHO_ON, PAM.PAM_ERROR_MSG, PAM.PAM_TEXT_INFO) : |
---|
| 172 | self.printInfo("Unexpected PAM query : %s (%s)" % (query, qtype), "warn") |
---|
| 173 | response.append(('', 0)) |
---|
| 174 | else: |
---|
| 175 | return None |
---|
| 176 | return response |
---|
| 177 | |
---|
| 178 | def checkAuth(self, username, password) : |
---|
| 179 | """Checks if we could authenticate an username with a password.""" |
---|
| 180 | if not hasPAM : |
---|
[3076] | 181 | raise PyKotaToolError, _("You MUST install PyPAM for this functionnality to work !") |
---|
[2785] | 182 | else : |
---|
| 183 | retcode = False |
---|
| 184 | self.password = password |
---|
| 185 | self.regainPriv() |
---|
| 186 | auth = PAM.pam() |
---|
| 187 | auth.start("passwd") |
---|
| 188 | auth.set_item(PAM.PAM_USER, username) |
---|
| 189 | auth.set_item(PAM.PAM_CONV, self.convPAM) |
---|
| 190 | try : |
---|
| 191 | auth.authenticate() |
---|
| 192 | auth.acct_mgmt() |
---|
| 193 | except PAM.error, resp : |
---|
| 194 | self.printInfo(_("Authentication error for user %s : %s") % (username, resp), "warn") |
---|
| 195 | except : |
---|
| 196 | self.printInfo(_("Internal error : can't authenticate user %s") % username, "error") |
---|
| 197 | else : |
---|
| 198 | self.logdebug(_("Password correct for user %s") % username) |
---|
| 199 | retcode = True |
---|
| 200 | self.dropPriv() |
---|
| 201 | return retcode |
---|
| 202 | |
---|
[2799] | 203 | def alarmHandler(self, signum, frame) : |
---|
| 204 | """Alarm handler.""" |
---|
[2802] | 205 | raise TimeoutError, _("The end user at %s:%i didn't answer within %i seconds. The print job will be cancelled.") % (self.destination, self.port, self.timeout) |
---|
[2799] | 206 | |
---|
[2778] | 207 | def main(self, arguments, options) : |
---|
| 208 | """Notifies or asks questions to end users through PyKotIcon.""" |
---|
[2779] | 209 | try : |
---|
[2799] | 210 | (self.destination, self.port) = options["destination"].split(":") |
---|
| 211 | self.port = int(self.port) |
---|
[2779] | 212 | except ValueError : |
---|
[2799] | 213 | self.destination = options["destination"] |
---|
| 214 | self.port = 7654 |
---|
| 215 | |
---|
| 216 | try : |
---|
| 217 | denyafter = int(options["denyafter"]) |
---|
| 218 | if denyafter < 1 : |
---|
| 219 | raise ValueError |
---|
| 220 | except (ValueError, TypeError) : |
---|
| 221 | denyafter = 1 |
---|
| 222 | |
---|
[2780] | 223 | try : |
---|
[2799] | 224 | self.timeout = int(options["timeout"]) |
---|
| 225 | if self.timeout < 0 : |
---|
| 226 | raise ValueError |
---|
| 227 | except (ValueError, TypeError) : |
---|
| 228 | self.timeout = 0 |
---|
| 229 | |
---|
| 230 | if self.timeout : |
---|
| 231 | signal.signal(signal.SIGALRM, self.alarmHandler) |
---|
| 232 | signal.alarm(self.timeout) |
---|
| 233 | |
---|
| 234 | try : |
---|
| 235 | server = xmlrpclib.ServerProxy("http://%s:%s" % (self.destination, self.port)) |
---|
[2780] | 236 | if options["ask"] : |
---|
[2805] | 237 | try : |
---|
| 238 | denyafter = int(options["denyafter"]) |
---|
| 239 | if denyafter < 1 : |
---|
| 240 | raise ValueError |
---|
| 241 | except (ValueError, TypeError) : |
---|
| 242 | denyafter = 1 |
---|
[2780] | 243 | labels = [] |
---|
| 244 | varnames = [] |
---|
| 245 | varvalues = {} |
---|
| 246 | for arg in arguments : |
---|
| 247 | try : |
---|
| 248 | (label, varname, varvalue) = arg.split(":", 2) |
---|
| 249 | except ValueError : |
---|
| 250 | raise PyKotaCommandLineError, "argument '%s' is invalid !" % arg |
---|
| 251 | labels.append(self.sanitizeMessage(label)) |
---|
| 252 | varname = varname.lower() |
---|
| 253 | varnames.append(varname) |
---|
| 254 | varvalues[varname] = self.sanitizeMessage(varvalue) |
---|
[2805] | 255 | |
---|
| 256 | passnumber = 1 |
---|
| 257 | authok = None |
---|
| 258 | while (authok != "AUTH=YES") and (passnumber <= denyafter) : |
---|
| 259 | result = server.askDatas(labels, varnames, varvalues) |
---|
| 260 | if not options["checkauth"] : |
---|
| 261 | break |
---|
| 262 | if result["isValid"] : |
---|
[2785] | 263 | if ("username" in varnames) and ("password" in varnames) : |
---|
[2804] | 264 | if self.checkAuth(self.UTF8ToUserCharset(result["username"].data[:]), |
---|
| 265 | self.UTF8ToUserCharset(result["password"].data[:])) : |
---|
[2786] | 266 | authok = "AUTH=YES" |
---|
[2785] | 267 | else : |
---|
[2786] | 268 | authok = "AUTH=NO" |
---|
[2785] | 269 | else : |
---|
[2786] | 270 | authok = "AUTH=IMPOSSIBLE" |
---|
[2805] | 271 | passnumber += 1 |
---|
| 272 | |
---|
[2806] | 273 | if options["checkauth"] and options["denyafter"] \ |
---|
| 274 | and (passnumber > denyafter) \ |
---|
| 275 | and (authok != "AUTH=YES") : |
---|
| 276 | print "DENY" |
---|
[2805] | 277 | if result["isValid"] : |
---|
[2786] | 278 | for varname in varnames : |
---|
| 279 | if (varname != "password") \ |
---|
[2799] | 280 | and ((varname != "username") or (authok in (None, "AUTH=YES"))) : |
---|
[2804] | 281 | print "%s=%s" % (varname.upper(), self.UTF8ToUserCharset(result[varname].data[:])) |
---|
[2786] | 282 | if authok is not None : |
---|
| 283 | print authok |
---|
[2780] | 284 | elif options["confirm"] : |
---|
| 285 | print server.showDialog(self.sanitizeMessage(arguments[0]), True) |
---|
| 286 | elif options["notify"] : |
---|
| 287 | print server.showDialog(self.sanitizeMessage(arguments[0]), False) |
---|
| 288 | |
---|
| 289 | if options["quit"] : |
---|
| 290 | server.quitApplication() |
---|
| 291 | except (socket.error, socket.gaierror), msg : |
---|
[3075] | 292 | print options["noremote"] |
---|
| 293 | try : |
---|
| 294 | errnum = msg.args[0] |
---|
| 295 | except (AttributeError, IndexError) : |
---|
| 296 | pass |
---|
| 297 | else : |
---|
| 298 | if errnum == errno.ECONNREFUSED : |
---|
| 299 | self.printInfo(_("Are you sure that PyKotIcon is running and accepting incoming connections on %s:%s ?") % (self.destination, self.port), "error") |
---|
[3076] | 300 | raise PyKotaToolError, "%s : %s" % (_("Connection error"), str(msg)) |
---|
[2799] | 301 | except TimeoutError, msg : |
---|
| 302 | self.printInfo(msg, "warn") |
---|
[2802] | 303 | print "CANCEL" # Timeout occured : job is cancelled. |
---|
[2799] | 304 | |
---|
| 305 | if self.timeout : |
---|
| 306 | signal.alarm(0) |
---|
[2779] | 307 | |
---|
[2778] | 308 | if __name__ == "__main__" : |
---|
| 309 | retcode = 0 |
---|
| 310 | try : |
---|
| 311 | defaults = { \ |
---|
[2799] | 312 | "timeout" : 0, |
---|
[3075] | 313 | "noremote" : "CANCEL", |
---|
[2778] | 314 | } |
---|
[3075] | 315 | short_options = "vhd:acnqCD:t:N:" |
---|
[2799] | 316 | long_options = ["help", "version", "destination=", "denyafter=", \ |
---|
| 317 | "timeout=", "ask", "checkauth", "confirm", "notify", \ |
---|
[3075] | 318 | "quit", "noremote=" ] |
---|
[2778] | 319 | |
---|
| 320 | # Initializes the command line tool |
---|
| 321 | notifier = PyKotaNotify(doc=__doc__) |
---|
| 322 | notifier.deferredInit() |
---|
| 323 | |
---|
| 324 | # parse and checks the command line |
---|
[2779] | 325 | (options, args) = notifier.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
[2778] | 326 | |
---|
| 327 | # sets long options |
---|
| 328 | options["help"] = options["h"] or options["help"] |
---|
| 329 | options["version"] = options["v"] or options["version"] |
---|
[2779] | 330 | options["destination"] = options["d"] or options["destination"] |
---|
| 331 | options["ask"] = options["a"] or options["ask"] |
---|
| 332 | options["confirm"] = options["c"] or options["confirm"] |
---|
| 333 | options["notify"] = options["n"] or options["notify"] |
---|
| 334 | options["quit"] = options["q"] or options["quit"] |
---|
[2785] | 335 | options["checkauth"] = options["C"] or options["checkauth"] |
---|
[2805] | 336 | options["denyafter"] = options["D"] or options["denyafter"] |
---|
[2799] | 337 | options["timeout"] = options["t"] or options["timeout"] or defaults["timeout"] |
---|
[3075] | 338 | options["noremote"] = (options["N"] or options["noremote"] or defaults["noremote"]).upper() |
---|
[2778] | 339 | |
---|
| 340 | if options["help"] : |
---|
| 341 | notifier.display_usage_and_quit() |
---|
| 342 | elif options["version"] : |
---|
| 343 | notifier.display_version_and_quit() |
---|
[2779] | 344 | elif (options["ask"] and (options["confirm"] or options["notify"])) \ |
---|
| 345 | or (options["confirm"] and (options["ask"] or options["notify"])) \ |
---|
[2799] | 346 | or ((options["checkauth"] or options["denyafter"]) and not options["ask"]) \ |
---|
[2779] | 347 | or (options["notify"] and (options["ask"] or options["confirm"])) : |
---|
| 348 | raise PyKotaCommandLineError, _("incompatible options, see help.") |
---|
| 349 | elif (not options["destination"]) \ |
---|
| 350 | or not (options["quit"] or options["ask"] or options["confirm"] or options["notify"]) : |
---|
| 351 | raise PyKotaCommandLineError, _("some options are mandatory, see help.") |
---|
[3075] | 352 | elif options["noremote"] not in ("CANCEL", "CONTINUE") : |
---|
| 353 | raise PyKotaCommandLineError, _("incorrect value for the --noremote command line switch, see help.") |
---|
[2779] | 354 | elif (not args) and (not options["quit"]) : |
---|
| 355 | raise PyKotaCommandLineError, _("some options require arguments, see help.") |
---|
[2778] | 356 | else : |
---|
| 357 | retcode = notifier.main(args, options) |
---|
| 358 | except KeyboardInterrupt : |
---|
| 359 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
| 360 | retcode = -3 |
---|
| 361 | except PyKotaCommandLineError, msg : |
---|
| 362 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[3076] | 363 | print "CANCEL" # Forces the cancellation of the print job if a command line switch is incorrect |
---|
[2778] | 364 | retcode = -2 |
---|
| 365 | except SystemExit : |
---|
| 366 | pass |
---|
| 367 | except : |
---|
| 368 | try : |
---|
| 369 | notifier.crashed("%s failed" % sys.argv[0]) |
---|
| 370 | except : |
---|
| 371 | crashed("%s failed" % sys.argv[0]) |
---|
| 372 | retcode = -1 |
---|
| 373 | |
---|
| 374 | sys.exit(retcode) |
---|