2 * $Id: queries.c,v 1.21 2009-02-02 16:04:33 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++ ) {
104 append( out, p, stop - p + crlflength );
106 CONSUME( in, linelength + crlflength );
111 CONSUME( in, linelength + crlflength );
116 char *LoginOK = "LoginOK\n";
117 char *LoginFailed = "LoginFailed\n";
119 #define h2b(x) (isdigit((x))?(x)-'0':(isupper((x))?(x)-'A':(x)-'a')+10)
121 int cq_k4login( in, out )
122 struct papfile *in, *out;
125 int linelength, crlflength;
127 struct papd_comment *comment = compeek();
132 switch ( markline( in, &start, &linelength, &crlflength )) {
143 p = start + strlen( comment->c_begin );
144 while ( *p == ' ' ) {
148 bzero( &tkt, sizeof( tkt ));
149 stop = start+linelength;
151 for ( i = 0, t = tkt.dat; p < stop; p += 2, t++, i++ ) {
152 *t = ( h2b( (unsigned char)*p ) << 4 ) +
153 h2b( (unsigned char)*( p + 1 ));
157 if (( rc = krb_rd_req( &tkt, "LaserWriter", printer->p_name,
158 0, &ad, "" )) != RD_AP_OK ) {
159 LOG(log_error, logtype_papd, "cq_k4login: %s", krb_err_txt[ rc ] );
160 append( out, LoginFailed, strlen( LoginFailed ));
162 CONSUME( in, linelength + crlflength );
165 LOG(log_info, logtype_papd, "cq_k4login: %s.%s@%s", ad.pname, ad.pinst,
167 lp_person( ad.pname );
168 lp_host( ad.prealm );
170 append( out, LoginOK, strlen( LoginOK ));
172 CONSUME( in, linelength + crlflength);
176 char *uameth = "UMICHKerberosIV\n*\n";
178 int cq_uameth( in, out )
179 struct papfile *in, *out;
182 int linelength, crlflength;
183 struct papd_comment *c, *comment = compeek();
186 switch ( markline( in, &start, &linelength, &crlflength )) {
197 if ( comgetflags() == 0 ) { /* start */
198 if (( printer->p_flags & P_KRB ) == 0 ) { /* no kerberos */
199 if ( comswitch( queries, cq_default ) < 0 ) {
200 LOG(log_error, logtype_papd, "cq_uameth: can't find default!" );
207 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) { /* end */
208 append( out, uameth, strlen( uameth ));
214 CONSUME( in, linelength + crlflength );
222 if ( printer->p_flags & P_SPOOLED ) {
223 append( out, "true\n", 5 );
230 int gq_pagecost( out )
235 /* check for spooler? XXX */
236 if ( printer->p_pagecost_msg != NULL ) {
237 append( out, printer->p_pagecost_msg,
238 strlen( printer->p_pagecost_msg ));
239 } else if ( printer->p_flags & P_ACCOUNT ) {
242 #endif /* ABS_PRINT */
243 sprintf( cost, "%d", printer->p_pagecost );
244 append( out, cost, strlen( cost ));
248 append( out, "\n", 1 );
253 int gq_balance( out )
258 if ( lp_pagecost() != 0 ) {
261 sprintf( balance, "$%1.2f\n", printer->p_balance );
262 append( out, balance, strlen( balance ));
265 #endif /* ABS_PRINT */
269 * Handler for RBISpoolerID
272 static const char *spoolerid = "(PAPD Spooler) 1.0 (" VERSION ")\n";
274 int gq_rbispoolerid( out )
277 append( out, spoolerid, strlen( spoolerid ));
284 * Handler for RBIUAMListQuery
287 static const char *nouams = "*\n";
289 int gq_rbiuamlist( out )
292 char uamnames[128] = "\0";
294 if (printer->p_flags & P_AUTH_PSSP) {
295 if (getuamnames(UAM_SERVER_PRINTAUTH, uamnames) < 0) {
296 append(out, nouams, strlen(nouams));
299 append(out, uamnames, strlen(uamnames));
303 append(out, nouams, strlen(nouams));
313 { "UMICHCostPerPage", gq_pagecost },
315 { "UMICHUserBalance", gq_balance },
317 { "RBISpoolerID", gq_rbispoolerid },
318 { "RBIUAMListQuery", gq_rbiuamlist },
319 { "ADOIsBinaryOK?", gq_true },
320 { "UMICHListQueue", gq_true },
321 { "UMICHDeleteJob", gq_true },
325 int cq_query( in, out )
326 struct papfile *in, *out;
328 char *start, *stop, *p, *q;
329 int linelength, crlflength;
330 struct papd_comment *comment = compeek();
335 switch ( markline( in, &start, &linelength, &crlflength )) {
346 stop = start+linelength;
348 if ( comgetflags() == 0 ) { /* started */
351 for ( p = start; p < stop; p++ ) {
357 if (p < stop) for ( p++; p < stop; p++ ) {
358 if ( *p != ' ' && *p != '\t' ) {
363 for ( q = p; q < stop; q++ ) {
364 if ( *q == ' ' || *q == '\t' || *q == '\r' || *q == '\n' ) {
369 for ( gq = genqueries; gq->gq_name; gq++ ) {
370 if (( strlen( gq->gq_name ) == (size_t)(q - p) ) &&
371 ( strncmp( gq->gq_name, p, q - p ) == 0 )) {
375 if ( gq->gq_name == NULL || gq->gq_handler == NULL ||
376 (gq->gq_handler)( out ) < 0 ) {
377 if ( comswitch( queries, cq_default ) < 0 ) {
378 LOG(log_error, logtype_papd, "cq_feature: can't find default!" );
384 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
386 CONSUME( in, linelength + crlflength );
391 CONSUME( in, linelength + crlflength );
395 void cq_font_answer( start, stop, out )
399 char *p, *q, buf[ 256 ];
400 struct ppd_font *pfo;
404 unsigned int count = 0;
405 while (( *p == ' ' || *p == '\t' ) && p < stop ) {
410 while ( *p != ' ' && *p != '\t' &&
411 *p != '\n' && *p != '\r' && p < stop && count < sizeof(buf)) {
419 append( out, "/", 1 );
420 append( out, buf, strlen( buf ));
421 append( out, ":", 1 );
423 if (( pfo = ppd_font( buf )) == NULL ) {
424 append( out, "No\n", 3 );
426 append( out, "Yes\n", 4 );
434 int cq_font( in, out )
435 struct papfile *in, *out;
437 char *start, *stop, *p;
438 int linelength, crlflength;
439 struct papd_comment *comment = compeek();
442 switch ( markline( in, &start, &linelength, &crlflength )) {
453 stop = start + linelength;
455 if ( comgetflags() == 0 ) {
458 for ( p = start; p < stop; p++ ) {
466 cq_font_answer( p, stop, out );
468 if ( comgetflags() == 1 &&
469 comcmp( start, stop, comcont, 0 ) == 0 ) {
472 for ( p = start; p < stop; p++ ) {
480 cq_font_answer( p, stop, out );
483 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
484 append( out, "*\n", 2 );
486 CONSUME( in, linelength + crlflength );
492 CONSUME( in, linelength + crlflength );
496 int cq_feature( in, out )
497 struct papfile *in, *out;
499 char *start, *stop, *p;
500 int linelength, crlflength;
501 struct papd_comment *comment = compeek();
502 struct ppd_feature *pfe;
505 switch ( markline( in, &start, &linelength, &crlflength )) {
516 stop = start + linelength;
518 if ( comgetflags() == 0 ) {
521 /* parse for feature */
522 for ( p = start; p < stop; p++ ) {
529 while ( *p == ' ' ) {
533 if (( pfe = ppd_feature( p, stop - p )) == NULL ) {
534 if ( comswitch( queries, cq_default ) < 0 ) {
535 LOG(log_error, logtype_papd, "cq_feature: can't find default!" );
541 append( out, pfe->pd_value, strlen( pfe->pd_value ));
542 append( out, "\r", 1 );
544 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
546 CONSUME( in, linelength + crlflength );
551 CONSUME( in, linelength + crlflength );
555 static const char *psver = "*PSVersion\n";
556 static const char *prod = "*Product\n";
558 int cq_printer( in, out )
559 struct papfile *in, *out;
562 int linelength, crlflength;
563 struct papd_comment *comment = compeek();
564 struct ppd_feature *pdpsver, *pdprod;
567 switch ( markline( in, &start, &linelength, &crlflength )) {
578 if ( comgetflags() == 0 ) {
581 if (( pdpsver = ppd_feature( psver, strlen( psver ))) == NULL ) {
582 if ( comswitch( queries, cq_default ) < 0 ) {
583 LOG(log_error, logtype_papd, "cq_printer: can't find default!" );
589 for ( p = pdpsver->pd_value; *p != '\0'; p++ ) {
595 LOG(log_error, logtype_papd, "cq_printer: can't parse PSVersion!" );
596 if ( comswitch( queries, cq_default ) < 0 ) {
597 LOG(log_error, logtype_papd, "cq_printer: can't find default!" );
603 if (( pdprod = ppd_feature( prod, strlen( prod ))) == NULL ) {
604 if ( comswitch( queries, cq_default ) < 0 ) {
605 LOG(log_error, logtype_papd, "cq_printer: can't find default!" );
612 append( out, p + 1, strlen( p + 1 ));
613 append( out, "\r", 1 );
616 append( out, pdpsver->pd_value, p - pdpsver->pd_value );
617 append( out, "\r", 1 );
620 append( out, pdprod->pd_value, strlen( pdprod->pd_value ));
621 append( out, "\r", 1 );
623 if ( comcmp( start, start+linelength, comment->c_end, 0 ) == 0 ) {
625 CONSUME( in, linelength + crlflength );
630 CONSUME( in, linelength + crlflength );
636 static const char *rmjobfailed = "Failed\n";
637 static const char *rmjobok = "Ok\n";
639 int cq_rmjob( in, out )
640 struct papfile *in, *out;
642 char *start, *stop, *p;
643 int linelength, crlflength;
646 switch ( markline( in, &start, &linelength, &crlflength )) {
657 stop = start + linelength;
659 for ( p = start; p < stop; p++ ) {
660 if ( *p == ' ' || *p == '\t' ) {
664 for ( ; p < stop; p++ ) {
665 if ( *p != ' ' && *p != '\t' ) {
671 if ( p < stop && ( job = atoi( p )) > 0 ) {
673 append( out, rmjobok, strlen( rmjobok ));
675 append( out, rmjobfailed, strlen( rmjobfailed ));
679 CONSUME( in, linelength + crlflength );
683 int cq_listq( in, out )
684 struct papfile *in, *out;
687 int linelength, crlflength;
689 switch ( markline( in, &start, &linelength, &crlflength )) {
700 if ( lp_queue( out )) {
701 LOG(log_error, logtype_papd, "cq_listq: lp_queue failed" );
705 CONSUME( in, linelength + crlflength );
708 #endif /* HAVE_CUPS */
712 * Handler for RBILogin
715 static struct uam_obj *papd_uam = NULL;
716 static const char *rbiloginok = "0\r";
717 static const char *rbiloginbad = "-1\r";
718 static const char *rbiloginerrstr = "%%[Error: SecurityError; \
719 SecurityViolation: Unknown user, incorrect password or log on is \
720 disabled ]%%\r%%[Flushing: rest of job (to end-of-file) will be \
723 int cq_rbilogin( in, out )
724 struct papfile *in, *out;
726 char *start, *stop, *p, *begin;
727 int linelength, crlflength;
728 char username[UAM_USERNAMELEN + 1] = "\0";
729 struct papd_comment *comment = compeek();
733 switch ( markline( in, &start, &linelength, &crlflength )) {
744 stop = start + linelength;
746 if ( comgetflags() == 0 ) { /* first line */
747 begin = start + strlen(comment->c_begin);
750 while (*p != ' ' && p < stop) {
754 memset(uamtype, 0, sizeof(uamtype));
755 if ((size_t)(p -begin) <= sizeof(uamtype) -1) {
756 strncpy(uamtype, begin, p - begin);
759 if ( !*uamtype || (papd_uam = auth_uamfind(UAM_SERVER_PRINTAUTH,
760 uamtype, strlen(uamtype))) == NULL) {
761 LOG(log_info, logtype_papd, "Could not find uam: %s", uamtype);
762 append(out, rbiloginbad, strlen(rbiloginbad));
763 append(out, rbiloginerrstr, strlen(rbiloginerrstr));
765 if ( (papd_uam->u.uam_printer(p,stop,username,out)) == 0 ) {
766 lp_person( username );
767 append(out, rbiloginok, strlen( rbiloginok ));
768 LOG(log_info, logtype_papd, "RBILogin: Logged in '%s'", username);
770 append(out, rbiloginbad, strlen( rbiloginbad));
771 append(out, rbiloginerrstr, strlen(rbiloginerrstr));
776 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
782 CONSUME( in, linelength + crlflength );
788 * All queries start with %%?Begin and end with %%?End. Note that the
789 * "Begin"/"End" general queries have to be last.
791 struct papd_comment queries[] = {
793 { "%%Login: UMICHKerberosIV", NULL, cq_k4login, 0 },
794 { "%%?BeginUAMethodsQuery", "%%?EndUAMethodsQuery:", cq_uameth,C_FULL },
797 { "%UMICHListQueue", NULL, cq_listq, C_FULL },
798 { "%UMICHDeleteJob", NULL, cq_rmjob, 0 },
799 #endif /* HAVE_CUPS */
800 { "%%?BeginQuery: RBILogin ", "%%?EndQuery", cq_rbilogin, 0 },
801 { "%%?BeginQuery", "%%?EndQuery", cq_query, 0 },
802 { "%%?BeginFeatureQuery", "%%?EndFeatureQuery", cq_feature, 0 },
803 { "%%?BeginFontQuery", "%%?EndFontQuery", cq_font, 0 },
804 { "%%?BeginPrinterQuery", "%%?EndPrinterQuery", cq_printer,C_FULL },
805 { "%%?Begin", "%%?End", cq_default, 0 },
806 { NULL, NULL, NULL, 0 },