2 * $Id: queries.c,v 1.20 2009-02-02 12:46:45 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 unsigned int count = 0;
402 while (( *p == ' ' || *p == '\t' ) && p < stop ) {
407 while ( *p != ' ' && *p != '\t' &&
408 *p != '\n' && *p != '\r' && p < stop && count < sizeof(buf)) {
416 append( out, "/", 1 );
417 append( out, buf, strlen( buf ));
418 append( out, ":", 1 );
420 if (( pfo = ppd_font( buf )) == NULL ) {
421 append( out, "No\n", 3 );
423 append( out, "Yes\n", 4 );
431 int cq_font( in, out )
432 struct papfile *in, *out;
434 char *start, *stop, *p;
435 int linelength, crlflength;
436 struct papd_comment *comment = compeek();
439 switch ( markline( in, &start, &linelength, &crlflength )) {
450 stop = start + linelength;
452 if ( comgetflags() == 0 ) {
455 for ( p = start; p < stop; p++ ) {
462 cq_font_answer( p, stop, out );
464 if ( comgetflags() == 1 &&
465 comcmp( start, stop, comcont, 0 ) == 0 ) {
468 for ( p = start; p < stop; p++ ) {
475 cq_font_answer( p, stop, out );
478 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
479 append( out, "*\n", 2 );
481 CONSUME( in, linelength + crlflength );
487 CONSUME( in, linelength + crlflength );
491 int cq_feature( in, out )
492 struct papfile *in, *out;
494 char *start, *stop, *p;
495 int linelength, crlflength;
496 struct papd_comment *comment = compeek();
497 struct ppd_feature *pfe;
500 switch ( markline( in, &start, &linelength, &crlflength )) {
511 stop = start + linelength;
513 if ( comgetflags() == 0 ) {
516 /* parse for feature */
517 for ( p = start; p < stop; p++ ) {
523 while ( *p == ' ' ) {
527 if (( pfe = ppd_feature( p, stop - p )) == NULL ) {
528 if ( comswitch( queries, cq_default ) < 0 ) {
529 LOG(log_error, logtype_papd, "cq_feature: can't find default!" );
535 append( out, pfe->pd_value, strlen( pfe->pd_value ));
536 append( out, "\r", 1 );
538 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
540 CONSUME( in, linelength + crlflength );
545 CONSUME( in, linelength + crlflength );
549 static const char *psver = "*PSVersion\n";
550 static const char *prod = "*Product\n";
552 int cq_printer( in, out )
553 struct papfile *in, *out;
556 int linelength, crlflength;
557 struct papd_comment *comment = compeek();
558 struct ppd_feature *pdpsver, *pdprod;
561 switch ( markline( in, &start, &linelength, &crlflength )) {
572 if ( comgetflags() == 0 ) {
575 if (( pdpsver = ppd_feature( psver, strlen( psver ))) == NULL ) {
576 if ( comswitch( queries, cq_default ) < 0 ) {
577 LOG(log_error, logtype_papd, "cq_printer: can't find default!" );
583 for ( p = pdpsver->pd_value; *p != '\0'; p++ ) {
589 LOG(log_error, logtype_papd, "cq_printer: can't parse PSVersion!" );
590 if ( comswitch( queries, cq_default ) < 0 ) {
591 LOG(log_error, logtype_papd, "cq_printer: can't find default!" );
597 if (( pdprod = ppd_feature( prod, strlen( prod ))) == NULL ) {
598 if ( comswitch( queries, cq_default ) < 0 ) {
599 LOG(log_error, logtype_papd, "cq_printer: can't find default!" );
606 append( out, p + 1, strlen( p + 1 ));
607 append( out, "\r", 1 );
610 append( out, pdpsver->pd_value, p - pdpsver->pd_value );
611 append( out, "\r", 1 );
614 append( out, pdprod->pd_value, strlen( pdprod->pd_value ));
615 append( out, "\r", 1 );
617 if ( comcmp( start, start+linelength, comment->c_end, 0 ) == 0 ) {
619 CONSUME( in, linelength + crlflength );
624 CONSUME( in, linelength + crlflength );
630 static const char *rmjobfailed = "Failed\n";
631 static const char *rmjobok = "Ok\n";
633 int cq_rmjob( in, out )
634 struct papfile *in, *out;
636 char *start, *stop, *p;
637 int linelength, crlflength;
640 switch ( markline( in, &start, &linelength, &crlflength )) {
651 stop = start + linelength;
653 for ( p = start; p < stop; p++ ) {
654 if ( *p == ' ' || *p == '\t' ) {
658 for ( ; p < stop; p++ ) {
659 if ( *p != ' ' && *p != '\t' ) {
665 if ( p < stop && ( job = atoi( p )) > 0 ) {
667 append( out, rmjobok, strlen( rmjobok ));
669 append( out, rmjobfailed, strlen( rmjobfailed ));
673 CONSUME( in, linelength + crlflength );
677 int cq_listq( in, out )
678 struct papfile *in, *out;
681 int linelength, crlflength;
683 switch ( markline( in, &start, &linelength, &crlflength )) {
694 if ( lp_queue( out )) {
695 LOG(log_error, logtype_papd, "cq_listq: lp_queue failed" );
699 CONSUME( in, linelength + crlflength );
702 #endif /* HAVE_CUPS */
706 * Handler for RBILogin
709 static struct uam_obj *papd_uam = NULL;
710 static const char *rbiloginok = "0\r";
711 static const char *rbiloginbad = "-1\r";
712 static const char *rbiloginerrstr = "%%[Error: SecurityError; \
713 SecurityViolation: Unknown user, incorrect password or log on is \
714 disabled ]%%\r%%[Flushing: rest of job (to end-of-file) will be \
717 int cq_rbilogin( in, out )
718 struct papfile *in, *out;
720 char *start, *stop, *p, *begin;
721 int linelength, crlflength;
722 char username[UAM_USERNAMELEN + 1] = "\0";
723 struct papd_comment *comment = compeek();
727 switch ( markline( in, &start, &linelength, &crlflength )) {
738 stop = start + linelength;
740 if ( comgetflags() == 0 ) { /* first line */
741 begin = start + strlen(comment->c_begin);
744 while (*p != ' ' && p < stop) {
748 memset(uamtype, 0, sizeof(uamtype));
749 if ((size_t)(p -begin) <= sizeof(uamtype) -1) {
750 strncpy(uamtype, begin, p - begin);
753 if ( !*uamtype || (papd_uam = auth_uamfind(UAM_SERVER_PRINTAUTH,
754 uamtype, strlen(uamtype))) == NULL) {
755 LOG(log_info, logtype_papd, "Could not find uam: %s", uamtype);
756 append(out, rbiloginbad, strlen(rbiloginbad));
757 append(out, rbiloginerrstr, strlen(rbiloginerrstr));
759 if ( (papd_uam->u.uam_printer(p,stop,username,out)) == 0 ) {
760 lp_person( username );
761 append(out, rbiloginok, strlen( rbiloginok ));
762 LOG(log_info, logtype_papd, "RBILogin: Logged in '%s'", username);
764 append(out, rbiloginbad, strlen( rbiloginbad));
765 append(out, rbiloginerrstr, strlen(rbiloginerrstr));
770 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
776 CONSUME( in, linelength + crlflength );
782 * All queries start with %%?Begin and end with %%?End. Note that the
783 * "Begin"/"End" general queries have to be last.
785 struct papd_comment queries[] = {
787 { "%%Login: UMICHKerberosIV", NULL, cq_k4login, 0 },
788 { "%%?BeginUAMethodsQuery", "%%?EndUAMethodsQuery:", cq_uameth,C_FULL },
791 { "%UMICHListQueue", NULL, cq_listq, C_FULL },
792 { "%UMICHDeleteJob", NULL, cq_rmjob, 0 },
793 #endif /* HAVE_CUPS */
794 { "%%?BeginQuery: RBILogin ", "%%?EndQuery", cq_rbilogin, 0 },
795 { "%%?BeginQuery", "%%?EndQuery", cq_query, 0 },
796 { "%%?BeginFeatureQuery", "%%?EndFeatureQuery", cq_feature, 0 },
797 { "%%?BeginFontQuery", "%%?EndFontQuery", cq_font, 0 },
798 { "%%?BeginPrinterQuery", "%%?EndPrinterQuery", cq_printer,C_FULL },
799 { "%%?Begin", "%%?End", cq_default, 0 },
800 { NULL, NULL, NULL, 0 },