2 * $Id: queries.c,v 1.8 2001-07-31 19:50:14 srittau 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 <sys/syslog.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 * );
53 int cq_listq( struct papfile *, struct papfile * );
54 int cq_rbilogin( struct papfile *, struct papfile * );
58 int cq_default( in, out )
59 struct papfile *in, *out;
61 char *start, *stop, *p;
62 int linelength, crlflength;
63 struct papd_comment *comment = compeek();
66 switch ( markline( in, &start, &linelength, &crlflength )) {
74 stop = start+linelength;
76 if ( comgetflags() == 0 ) { /* started */
77 if ( comment->c_end ) {
81 CONSUME( in, linelength + crlflength );
86 if ( comcmp( start, start+linelength, comment->c_end, 0 ) == 0 ) {
87 for ( p = start; p < stop; p++ ) {
97 append( out, p, stop - p + crlflength );
99 CONSUME( in, linelength + crlflength );
104 CONSUME( in, linelength + crlflength );
109 char *LoginOK = "LoginOK\n";
110 char *LoginFailed = "LoginFailed\n";
112 #define h2b(x) (isdigit((x))?(x)-'0':(isupper((x))?(x)-'A':(x)-'a')+10)
114 int cq_k4login( in, out )
115 struct papfile *in, *out;
118 int linelength, crlflength;
120 struct papd_comment *comment = compeek();
125 switch ( markline( in, &start, &linelength, &crlflength )) {
133 p = start + strlen( comment->c_begin );
134 while ( *p == ' ' ) {
138 bzero( &tkt, sizeof( tkt ));
139 stop = start+linelength;
140 for ( i = 0, t = tkt.dat; p < stop; p += 2, t++, i++ ) {
141 *t = ( h2b( (unsigned char)*p ) << 4 ) +
142 h2b( (unsigned char)*( p + 1 ));
146 if (( rc = krb_rd_req( &tkt, "LaserWriter", printer->p_name,
147 0, &ad, "" )) != RD_AP_OK ) {
148 syslog( LOG_ERR, "cq_k4login: %s", krb_err_txt[ rc ] );
149 append( out, LoginFailed, strlen( LoginFailed ));
151 CONSUME( in, linelength + crlflength );
154 syslog( LOG_INFO, "cq_k4login: %s.%s@%s", ad.pname, ad.pinst,
156 lp_person( ad.pname );
157 lp_host( ad.prealm );
159 append( out, LoginOK, strlen( LoginOK ));
161 CONSUME( in, linelength + crlflength);
165 char *uameth = "UMICHKerberosIV\n*\n";
167 int cq_uameth( in, out )
168 struct papfile *in, *out;
171 int linelength, crlflength;
172 struct papd_comment *c, *comment = compeek();
175 switch ( markline( in, &start, &linelength, &crlflength )) {
183 if ( comgetflags() == 0 ) { /* start */
184 if (( printer->p_flags & P_KRB ) == 0 ) { /* no kerberos */
185 if ( comswitch( queries, cq_default ) < 0 ) {
186 syslog( LOG_ERR, "cq_uameth: can't find default!" );
193 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) { /* end */
194 append( out, uameth, strlen( uameth ));
200 CONSUME( in, linelength + crlflength );
208 if ( printer->p_flags & P_SPOOLED ) {
209 append( out, "true\n", 5 );
216 int gq_pagecost( out )
221 /* check for spooler? XXX */
222 if ( printer->p_pagecost_msg != NULL ) {
223 append( out, printer->p_pagecost_msg,
224 strlen( printer->p_pagecost_msg ));
225 } else if ( printer->p_flags & P_ACCOUNT ) {
228 #endif /* ABS_PRINT */
229 sprintf( cost, "%d", printer->p_pagecost );
230 append( out, cost, strlen( cost ));
234 append( out, "\n", 1 );
239 int gq_balance( out )
244 if ( lp_pagecost() != 0 ) {
247 sprintf( balance, "$%1.2f\n", printer->p_balance );
248 append( out, balance, strlen( balance ));
251 #endif /* ABS_PRINT */
255 * Handler for RBISpoolerID
258 static const char *spoolerid = "(PAPD Spooler) 2.1 (2.1.4 pre-release)\n";
260 int gq_rbispoolerid( out )
263 append( out, spoolerid, strlen( spoolerid ));
270 * Handler for RBIUAMListQuery
273 static const char *nouams = "*\n";
275 int gq_rbiuamlist( out )
278 char uamnames[128] = "\0";
280 if (printer->p_flags & P_AUTH_PSSP) {
281 if (getuamnames(UAM_SERVER_PRINTAUTH, uamnames) < 0) {
282 append(out, nouams, strlen(nouams));
285 append(out, uamnames, strlen(uamnames));
289 append(out, nouams, strlen(nouams));
299 { "UMICHCostPerPage", gq_pagecost },
301 { "UMICHUserBalance", gq_balance },
303 { "RBISpoolerID", gq_rbispoolerid },
304 { "RBIUAMListQuery", gq_rbiuamlist },
305 { "UMICHListQueue", gq_true },
306 { "UMICHDeleteJob", gq_true },
310 int cq_query( in, out )
311 struct papfile *in, *out;
313 char *start, *stop, *p, *q;
314 int linelength, crlflength;
315 struct papd_comment *comment = compeek();
320 switch ( markline( in, &start, &linelength, &crlflength )) {
328 stop = start+linelength;
330 if ( comgetflags() == 0 ) { /* started */
333 for ( p = start; p < stop; p++ ) {
339 for ( p++; p < stop; p++ ) {
340 if ( *p != ' ' && *p != '\t' ) {
345 for ( q = p; q < stop; q++ ) {
346 if ( *q == ' ' || *q == '\t' || *q == '\r' || *q == '\n' ) {
351 for ( gq = genqueries; gq->gq_name; gq++ ) {
352 if (( strlen( gq->gq_name ) == q - p ) &&
353 ( strncmp( gq->gq_name, p, q - p ) == 0 )) {
357 if ( gq->gq_name == NULL || gq->gq_handler == NULL ||
358 (gq->gq_handler)( out ) < 0 ) {
359 if ( comswitch( queries, cq_default ) < 0 ) {
360 syslog( LOG_ERR, "cq_feature: can't find default!" );
366 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
368 CONSUME( in, linelength + crlflength );
373 CONSUME( in, linelength + crlflength );
377 void cq_font_answer( start, stop, out )
381 char *p, *q, buf[ 256 ];
382 struct ppd_font *pfo;
386 while (( *p == ' ' || *p == '\t' ) && p < stop ) {
391 while ( *p != ' ' && *p != '\t' &&
392 *p != '\n' && *p != '\r' && p < stop ) {
399 append( out, "/", 1 );
400 append( out, buf, strlen( buf ));
401 append( out, ":", 1 );
403 if (( pfo = ppd_font( buf )) == NULL ) {
404 append( out, "No\n", 3 );
406 append( out, "Yes\n", 4 );
414 int cq_font( in, out )
415 struct papfile *in, *out;
417 char *start, *stop, *p;
418 int linelength, crlflength;
419 struct papd_comment *comment = compeek();
422 switch ( markline( in, &start, &linelength, &crlflength )) {
430 stop = start + linelength;
432 if ( comgetflags() == 0 ) {
435 for ( p = start; p < stop; p++ ) {
442 cq_font_answer( p, stop, out );
444 if ( comgetflags() == 1 &&
445 comcmp( start, stop, comcont, 0 ) == 0 ) {
448 for ( p = start; p < stop; p++ ) {
455 cq_font_answer( p, stop, out );
458 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
459 append( out, "*\n", 2 );
461 CONSUME( in, linelength + crlflength );
467 CONSUME( in, linelength + crlflength );
471 int cq_feature( in, out )
472 struct papfile *in, *out;
474 char *start, *stop, *p;
475 int linelength, crlflength;
476 struct papd_comment *comment = compeek();
477 struct ppd_feature *pfe;
480 switch ( markline( in, &start, &linelength, &crlflength )) {
488 stop = start + linelength;
490 if ( comgetflags() == 0 ) {
493 /* parse for feature */
494 for ( p = start; p < stop; p++ ) {
500 while ( *p == ' ' ) {
504 if (( pfe = ppd_feature( p, stop - p )) == NULL ) {
505 if ( comswitch( queries, cq_default ) < 0 ) {
506 syslog( LOG_ERR, "cq_feature: can't find default!" );
512 append( out, pfe->pd_value, strlen( pfe->pd_value ));
513 append( out, "\r", 1 );
515 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
517 CONSUME( in, linelength + crlflength );
522 CONSUME( in, linelength + crlflength );
526 static const char *psver = "*PSVersion\n";
527 static const char *prod = "*Product\n";
529 int cq_printer( in, out )
530 struct papfile *in, *out;
533 int linelength, crlflength;
534 struct papd_comment *comment = compeek();
535 struct ppd_feature *pdpsver, *pdprod;
538 switch ( markline( in, &start, &linelength, &crlflength )) {
546 if ( comgetflags() == 0 ) {
549 if (( pdpsver = ppd_feature( psver, strlen( psver ))) == NULL ) {
550 if ( comswitch( queries, cq_default ) < 0 ) {
551 syslog( LOG_ERR, "cq_printer: can't find default!" );
557 for ( p = pdpsver->pd_value; *p != '\0'; p++ ) {
563 syslog( LOG_ERR, "cq_printer: can't parse PSVersion!" );
564 if ( comswitch( queries, cq_default ) < 0 ) {
565 syslog( LOG_ERR, "cq_printer: can't find default!" );
571 if (( pdprod = ppd_feature( prod, strlen( prod ))) == NULL ) {
572 if ( comswitch( queries, cq_default ) < 0 ) {
573 syslog( LOG_ERR, "cq_printer: can't find default!" );
580 append( out, p + 1, strlen( p + 1 ));
581 append( out, "\r", 1 );
584 append( out, pdpsver->pd_value, p - pdpsver->pd_value );
585 append( out, "\r", 1 );
588 append( out, pdprod->pd_value, strlen( pdprod->pd_value ));
589 append( out, "\r", 1 );
591 if ( comcmp( start, start+linelength, comment->c_end, 0 ) == 0 ) {
593 CONSUME( in, linelength + crlflength );
598 CONSUME( in, linelength + crlflength );
602 static const char *rmjobfailed = "Failed\n";
603 static const char *rmjobok = "Ok\n";
605 int cq_rmjob( in, out )
606 struct papfile *in, *out;
608 char *start, *stop, *p;
609 int linelength, crlflength;
612 switch ( markline( in, &start, &linelength, &crlflength )) {
620 stop = start + linelength;
622 for ( p = start; p < stop; p++ ) {
623 if ( *p == ' ' || *p == '\t' ) {
627 for ( ; p < stop; p++ ) {
628 if ( *p != ' ' && *p != '\t' ) {
634 if ( p < stop && ( job = atoi( p )) > 0 ) {
636 append( out, rmjobok, strlen( rmjobok ));
638 append( out, rmjobfailed, strlen( rmjobfailed ));
642 CONSUME( in, linelength + crlflength );
646 int cq_listq( in, out )
647 struct papfile *in, *out;
650 int linelength, crlflength;
652 switch ( markline( in, &start, &linelength, &crlflength )) {
660 if ( lp_queue( out )) {
661 syslog( LOG_ERR, "cq_listq: lp_queue failed" );
665 CONSUME( in, linelength + crlflength );
671 * Handler for RBILogin
674 static struct uam_obj *papd_uam = NULL;
675 static const char *rbiloginok = "0\r";
676 static const char *rbiloginbad = "-1\r";
677 static const char *rbiloginerrstr = "%%[Error: SecurityError; \
678 SecurityViolation: Unknown user, incorrect password or log on is \
679 disabled ]%%\r%%Flushing: rest of job (to end-of-file) will be \
682 int cq_rbilogin( in, out )
683 struct papfile *in, *out;
685 char *start, *stop, *p, *begin;
686 int linelength, crlflength;
687 char username[9] = "\0";
688 struct papd_comment *comment = compeek();
689 char uamtype[20] = "\0";
692 switch ( markline( in, &start, &linelength, &crlflength )) {
700 stop = start + linelength;
702 if ( comgetflags() == 0 ) { /* first line */
703 begin = start + strlen(comment->c_begin);
710 strncat(uamtype, begin, p - begin);
712 if ((papd_uam = auth_uamfind(UAM_SERVER_PRINTAUTH,
713 uamtype, strlen(uamtype))) == NULL) {
714 syslog(LOG_INFO, "Could not find uam: %s", uamtype);
715 append(out, rbiloginbad, strlen(rbiloginbad));
716 append(out, rbiloginerrstr, strlen(rbiloginerrstr));
718 if ( (papd_uam->u.uam_printer(p,stop,username,out)) == 0 ) {
719 lp_person( username );
721 append(out, rbiloginbad, strlen( rbiloginbad));
722 append(out, rbiloginerrstr, strlen(rbiloginerrstr));
727 if ( comcmp( start, stop, comment->c_end, 0 ) == 0 ) {
733 CONSUME( in, linelength + crlflength );
739 * All queries start with %%?Begin and end with %%?End. Note that the
740 * "Begin"/"End" general queries have to be last.
742 struct papd_comment queries[] = {
744 { "%%Login: UMICHKerberosIV", 0, cq_k4login, 0 },
745 { "%%?BeginUAMethodsQuery", "%%?EndUAMethodsQuery:", cq_uameth, C_FULL },
747 { "%UMICHListQueue", 0, cq_listq, C_FULL },
748 { "%UMICHDeleteJob", 0, cq_rmjob, 0 },
749 { "%%?BeginQuery: RBILogin ", "%%?EndQuery", cq_rbilogin, 0 },
750 { "%%?BeginQuery", "%%?EndQuery", cq_query, 0 },
751 { "%%?BeginFeatureQuery", "%%?EndFeatureQuery", cq_feature, 0 },
752 { "%%?BeginFontQuery", "%%?EndFontQuery", cq_font, 0 },
753 { "%%?BeginPrinterQuery", "%%?EndPrinterQuery", cq_printer, C_FULL },
754 { "%%?Begin", "%%?End", cq_default, 0 },