2 * $Id: queries.c,v 1.19 2009-02-02 10:31:32 didg Exp $
4 * Copyright (c) 1990,1994 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
15 #include <atalk/logger.h>
16 #include <sys/param.h>
18 #include <sys/types.h>
19 #include <netatalk/at.h>
20 #include <atalk/atp.h>
24 #include <kerberos/krb.h>
37 int cq_default( struct papfile *, struct papfile * );
38 int cq_k4login( struct papfile *, struct papfile * );
39 int cq_uameth( struct papfile *, struct papfile * );
41 int gq_balance( struct papfile * );
42 int gq_pagecost( struct papfile * );
43 int gq_true( struct papfile * );
44 int gq_rbispoolerid( struct papfile * );
45 int gq_rbiuamlist( struct papfile * );
47 int cq_query( struct papfile *, struct papfile * );
48 void cq_font_answer( char *, char *, struct papfile * );
49 int cq_font( struct papfile *, struct papfile * );
50 int cq_feature( struct papfile *, struct papfile * );
51 int cq_printer( struct papfile *, struct papfile * );
52 int cq_rmjob( struct papfile *, struct papfile * );
54 int cq_listq( struct papfile *, struct papfile * );
55 int cq_rbilogin( struct papfile *, struct papfile * );
56 #endif /* HAVE_CUPS */
60 int cq_default( in, out )
61 struct papfile *in, *out;
63 char *start, *stop, *p;
64 int linelength, crlflength;
65 struct papd_comment *comment = compeek();
68 switch ( markline( in, &start, &linelength, &crlflength )) {
79 stop = start+linelength;
81 if ( comgetflags() == 0 ) { /* started */
82 if ( comment->c_end ) {
86 CONSUME( in, linelength + crlflength );
91 if ( comcmp( start, start+linelength, comment->c_end, 0 ) == 0 ) {
92 for ( p = start; p < stop; p++ ) {
102 append( out, p, stop - p + crlflength );
104 CONSUME( in, linelength + crlflength );
109 CONSUME( in, linelength + crlflength );
114 char *LoginOK = "LoginOK\n";
115 char *LoginFailed = "LoginFailed\n";
117 #define h2b(x) (isdigit((x))?(x)-'0':(isupper((x))?(x)-'A':(x)-'a')+10)
119 int cq_k4login( in, out )
120 struct papfile *in, *out;
123 int linelength, crlflength;
125 struct papd_comment *comment = compeek();
130 switch ( markline( in, &start, &linelength, &crlflength )) {
141 p = start + strlen( comment->c_begin );
142 while ( *p == ' ' ) {
146 bzero( &tkt, sizeof( tkt ));
147 stop = start+linelength;
148 for ( i = 0, t = tkt.dat; p < stop; p += 2, t++, i++ ) {
149 *t = ( h2b( (unsigned char)*p ) << 4 ) +
150 h2b( (unsigned char)*( p + 1 ));
154 if (( rc = krb_rd_req( &tkt, "LaserWriter", printer->p_name,
155 0, &ad, "" )) != RD_AP_OK ) {
156 LOG(log_error, logtype_papd, "cq_k4login: %s", krb_err_txt[ rc ] );
157 append( out, LoginFailed, strlen( LoginFailed ));
159 CONSUME( in, linelength + crlflength );
162 LOG(log_info, logtype_papd, "cq_k4login: %s.%s@%s", ad.pname, ad.pinst,
164 lp_person( ad.pname );
165 lp_host( ad.prealm );
167 append( out, LoginOK, strlen( LoginOK ));
169 CONSUME( in, linelength + crlflength);
173 char *uameth = "UMICHKerberosIV\n*\n";
175 int cq_uameth( in, out )
176 struct papfile *in, *out;
179 int linelength, crlflength;
180 struct papd_comment *c, *comment = compeek();
183 switch ( markline( in, &start, &linelength, &crlflength )) {
194 if ( comgetflags() == 0 ) { /* start */
195 if (( printer->p_flags & P_KRB ) == 0 ) { /* no kerberos */
196 if ( comswitch( queries, cq_default ) < 0 ) {
197 LOG(log_error, logtype_papd, "cq_uameth: can't find default!" );
204 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) { /* end */
205 append( out, uameth, strlen( uameth ));
211 CONSUME( in, linelength + crlflength );
219 if ( printer->p_flags & P_SPOOLED ) {
220 append( out, "true\n", 5 );
227 int gq_pagecost( out )
232 /* check for spooler? XXX */
233 if ( printer->p_pagecost_msg != NULL ) {
234 append( out, printer->p_pagecost_msg,
235 strlen( printer->p_pagecost_msg ));
236 } else if ( printer->p_flags & P_ACCOUNT ) {
239 #endif /* ABS_PRINT */
240 sprintf( cost, "%d", printer->p_pagecost );
241 append( out, cost, strlen( cost ));
245 append( out, "\n", 1 );
250 int gq_balance( out )
255 if ( lp_pagecost() != 0 ) {
258 sprintf( balance, "$%1.2f\n", printer->p_balance );
259 append( out, balance, strlen( balance ));
262 #endif /* ABS_PRINT */
266 * Handler for RBISpoolerID
269 static const char *spoolerid = "(PAPD Spooler) 1.0 (" VERSION ")\n";
271 int gq_rbispoolerid( out )
274 append( out, spoolerid, strlen( spoolerid ));
281 * Handler for RBIUAMListQuery
284 static const char *nouams = "*\n";
286 int gq_rbiuamlist( out )
289 char uamnames[128] = "\0";
291 if (printer->p_flags & P_AUTH_PSSP) {
292 if (getuamnames(UAM_SERVER_PRINTAUTH, uamnames) < 0) {
293 append(out, nouams, strlen(nouams));
296 append(out, uamnames, strlen(uamnames));
300 append(out, nouams, strlen(nouams));
310 { "UMICHCostPerPage", gq_pagecost },
312 { "UMICHUserBalance", gq_balance },
314 { "RBISpoolerID", gq_rbispoolerid },
315 { "RBIUAMListQuery", gq_rbiuamlist },
316 { "ADOIsBinaryOK?", gq_true },
317 { "UMICHListQueue", gq_true },
318 { "UMICHDeleteJob", gq_true },
322 int cq_query( in, out )
323 struct papfile *in, *out;
325 char *start, *stop, *p, *q;
326 int linelength, crlflength;
327 struct papd_comment *comment = compeek();
332 switch ( markline( in, &start, &linelength, &crlflength )) {
343 stop = start+linelength;
345 if ( comgetflags() == 0 ) { /* started */
348 for ( p = start; p < stop; p++ ) {
354 for ( p++; p < stop; p++ ) {
355 if ( *p != ' ' && *p != '\t' ) {
360 for ( q = p; q < stop; q++ ) {
361 if ( *q == ' ' || *q == '\t' || *q == '\r' || *q == '\n' ) {
366 for ( gq = genqueries; gq->gq_name; gq++ ) {
367 if (( strlen( gq->gq_name ) == q - p ) &&
368 ( strncmp( gq->gq_name, p, q - p ) == 0 )) {
372 if ( gq->gq_name == NULL || gq->gq_handler == NULL ||
373 (gq->gq_handler)( out ) < 0 ) {
374 if ( comswitch( queries, cq_default ) < 0 ) {
375 LOG(log_error, logtype_papd, "cq_feature: can't find default!" );
381 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
383 CONSUME( in, linelength + crlflength );
388 CONSUME( in, linelength + crlflength );
392 void cq_font_answer( start, stop, out )
396 char *p, *q, buf[ 256 ];
397 struct ppd_font *pfo;
401 while (( *p == ' ' || *p == '\t' ) && p < stop ) {
406 while ( *p != ' ' && *p != '\t' &&
407 *p != '\n' && *p != '\r' && p < stop ) {
414 append( out, "/", 1 );
415 append( out, buf, strlen( buf ));
416 append( out, ":", 1 );
418 if (( pfo = ppd_font( buf )) == NULL ) {
419 append( out, "No\n", 3 );
421 append( out, "Yes\n", 4 );
429 int cq_font( in, out )
430 struct papfile *in, *out;
432 char *start, *stop, *p;
433 int linelength, crlflength;
434 struct papd_comment *comment = compeek();
437 switch ( markline( in, &start, &linelength, &crlflength )) {
448 stop = start + linelength;
450 if ( comgetflags() == 0 ) {
453 for ( p = start; p < stop; p++ ) {
460 cq_font_answer( p, stop, out );
462 if ( comgetflags() == 1 &&
463 comcmp( start, stop, comcont, 0 ) == 0 ) {
466 for ( p = start; p < stop; p++ ) {
473 cq_font_answer( p, stop, out );
476 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
477 append( out, "*\n", 2 );
479 CONSUME( in, linelength + crlflength );
485 CONSUME( in, linelength + crlflength );
489 int cq_feature( in, out )
490 struct papfile *in, *out;
492 char *start, *stop, *p;
493 int linelength, crlflength;
494 struct papd_comment *comment = compeek();
495 struct ppd_feature *pfe;
498 switch ( markline( in, &start, &linelength, &crlflength )) {
509 stop = start + linelength;
511 if ( comgetflags() == 0 ) {
514 /* parse for feature */
515 for ( p = start; p < stop; p++ ) {
521 while ( *p == ' ' ) {
525 if (( pfe = ppd_feature( p, stop - p )) == NULL ) {
526 if ( comswitch( queries, cq_default ) < 0 ) {
527 LOG(log_error, logtype_papd, "cq_feature: can't find default!" );
533 append( out, pfe->pd_value, strlen( pfe->pd_value ));
534 append( out, "\r", 1 );
536 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
538 CONSUME( in, linelength + crlflength );
543 CONSUME( in, linelength + crlflength );
547 static const char *psver = "*PSVersion\n";
548 static const char *prod = "*Product\n";
550 int cq_printer( in, out )
551 struct papfile *in, *out;
554 int linelength, crlflength;
555 struct papd_comment *comment = compeek();
556 struct ppd_feature *pdpsver, *pdprod;
559 switch ( markline( in, &start, &linelength, &crlflength )) {
570 if ( comgetflags() == 0 ) {
573 if (( pdpsver = ppd_feature( psver, strlen( psver ))) == NULL ) {
574 if ( comswitch( queries, cq_default ) < 0 ) {
575 LOG(log_error, logtype_papd, "cq_printer: can't find default!" );
581 for ( p = pdpsver->pd_value; *p != '\0'; p++ ) {
587 LOG(log_error, logtype_papd, "cq_printer: can't parse PSVersion!" );
588 if ( comswitch( queries, cq_default ) < 0 ) {
589 LOG(log_error, logtype_papd, "cq_printer: can't find default!" );
595 if (( pdprod = ppd_feature( prod, strlen( prod ))) == NULL ) {
596 if ( comswitch( queries, cq_default ) < 0 ) {
597 LOG(log_error, logtype_papd, "cq_printer: can't find default!" );
604 append( out, p + 1, strlen( p + 1 ));
605 append( out, "\r", 1 );
608 append( out, pdpsver->pd_value, p - pdpsver->pd_value );
609 append( out, "\r", 1 );
612 append( out, pdprod->pd_value, strlen( pdprod->pd_value ));
613 append( out, "\r", 1 );
615 if ( comcmp( start, start+linelength, comment->c_end, 0 ) == 0 ) {
617 CONSUME( in, linelength + crlflength );
622 CONSUME( in, linelength + crlflength );
628 static const char *rmjobfailed = "Failed\n";
629 static const char *rmjobok = "Ok\n";
631 int cq_rmjob( in, out )
632 struct papfile *in, *out;
634 char *start, *stop, *p;
635 int linelength, crlflength;
638 switch ( markline( in, &start, &linelength, &crlflength )) {
649 stop = start + linelength;
651 for ( p = start; p < stop; p++ ) {
652 if ( *p == ' ' || *p == '\t' ) {
656 for ( ; p < stop; p++ ) {
657 if ( *p != ' ' && *p != '\t' ) {
663 if ( p < stop && ( job = atoi( p )) > 0 ) {
665 append( out, rmjobok, strlen( rmjobok ));
667 append( out, rmjobfailed, strlen( rmjobfailed ));
671 CONSUME( in, linelength + crlflength );
675 int cq_listq( in, out )
676 struct papfile *in, *out;
679 int linelength, crlflength;
681 switch ( markline( in, &start, &linelength, &crlflength )) {
692 if ( lp_queue( out )) {
693 LOG(log_error, logtype_papd, "cq_listq: lp_queue failed" );
697 CONSUME( in, linelength + crlflength );
700 #endif /* HAVE_CUPS */
704 * Handler for RBILogin
707 static struct uam_obj *papd_uam = NULL;
708 static const char *rbiloginok = "0\r";
709 static const char *rbiloginbad = "-1\r";
710 static const char *rbiloginerrstr = "%%[Error: SecurityError; \
711 SecurityViolation: Unknown user, incorrect password or log on is \
712 disabled ]%%\r%%[Flushing: rest of job (to end-of-file) will be \
715 int cq_rbilogin( in, out )
716 struct papfile *in, *out;
718 char *start, *stop, *p, *begin;
719 int linelength, crlflength;
720 char username[UAM_USERNAMELEN + 1] = "\0";
721 struct papd_comment *comment = compeek();
722 char uamtype[20] = "\0";
725 switch ( markline( in, &start, &linelength, &crlflength )) {
736 stop = start + linelength;
738 if ( comgetflags() == 0 ) { /* first line */
739 begin = start + strlen(comment->c_begin);
746 strncat(uamtype, begin, p - begin);
748 if ((papd_uam = auth_uamfind(UAM_SERVER_PRINTAUTH,
749 uamtype, strlen(uamtype))) == NULL) {
750 LOG(log_info, logtype_papd, "Could not find uam: %s", uamtype);
751 append(out, rbiloginbad, strlen(rbiloginbad));
752 append(out, rbiloginerrstr, strlen(rbiloginerrstr));
754 if ( (papd_uam->u.uam_printer(p,stop,username,out)) == 0 ) {
755 lp_person( username );
756 append(out, rbiloginok, strlen( rbiloginok ));
757 LOG(log_info, logtype_papd, "RBILogin: Logged in '%s'", username);
759 append(out, rbiloginbad, strlen( rbiloginbad));
760 append(out, rbiloginerrstr, strlen(rbiloginerrstr));
765 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
771 CONSUME( in, linelength + crlflength );
777 * All queries start with %%?Begin and end with %%?End. Note that the
778 * "Begin"/"End" general queries have to be last.
780 struct papd_comment queries[] = {
782 { "%%Login: UMICHKerberosIV", NULL, cq_k4login, 0 },
783 { "%%?BeginUAMethodsQuery", "%%?EndUAMethodsQuery:", cq_uameth,C_FULL },
786 { "%UMICHListQueue", NULL, cq_listq, C_FULL },
787 { "%UMICHDeleteJob", NULL, cq_rmjob, 0 },
788 #endif /* HAVE_CUPS */
789 { "%%?BeginQuery: RBILogin ", "%%?EndQuery", cq_rbilogin, 0 },
790 { "%%?BeginQuery", "%%?EndQuery", cq_query, 0 },
791 { "%%?BeginFeatureQuery", "%%?EndFeatureQuery", cq_feature, 0 },
792 { "%%?BeginFontQuery", "%%?EndFontQuery", cq_font, 0 },
793 { "%%?BeginPrinterQuery", "%%?EndPrinterQuery", cq_printer,C_FULL },
794 { "%%?Begin", "%%?End", cq_default, 0 },
795 { NULL, NULL, NULL, 0 },