xref: /aosp_15_r20/external/libcups/cgi-bin/jobs.c (revision 5e7646d21f1134fb0638875d812ef646c12ab91e)
1*5e7646d2SAndroid Build Coastguard Worker /*
2*5e7646d2SAndroid Build Coastguard Worker  * Job status CGI for CUPS.
3*5e7646d2SAndroid Build Coastguard Worker  *
4*5e7646d2SAndroid Build Coastguard Worker  * Copyright 2007-2014 by Apple Inc.
5*5e7646d2SAndroid Build Coastguard Worker  * Copyright 1997-2006 by Easy Software Products.
6*5e7646d2SAndroid Build Coastguard Worker  *
7*5e7646d2SAndroid Build Coastguard Worker  * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
8*5e7646d2SAndroid Build Coastguard Worker  */
9*5e7646d2SAndroid Build Coastguard Worker 
10*5e7646d2SAndroid Build Coastguard Worker /*
11*5e7646d2SAndroid Build Coastguard Worker  * Include necessary headers...
12*5e7646d2SAndroid Build Coastguard Worker  */
13*5e7646d2SAndroid Build Coastguard Worker 
14*5e7646d2SAndroid Build Coastguard Worker #include "cgi-private.h"
15*5e7646d2SAndroid Build Coastguard Worker 
16*5e7646d2SAndroid Build Coastguard Worker 
17*5e7646d2SAndroid Build Coastguard Worker /*
18*5e7646d2SAndroid Build Coastguard Worker  * Local functions...
19*5e7646d2SAndroid Build Coastguard Worker  */
20*5e7646d2SAndroid Build Coastguard Worker 
21*5e7646d2SAndroid Build Coastguard Worker static void	do_job_op(http_t *http, int job_id, ipp_op_t op);
22*5e7646d2SAndroid Build Coastguard Worker 
23*5e7646d2SAndroid Build Coastguard Worker 
24*5e7646d2SAndroid Build Coastguard Worker /*
25*5e7646d2SAndroid Build Coastguard Worker  * 'main()' - Main entry for CGI.
26*5e7646d2SAndroid Build Coastguard Worker  */
27*5e7646d2SAndroid Build Coastguard Worker 
28*5e7646d2SAndroid Build Coastguard Worker int					/* O - Exit status */
main(void)29*5e7646d2SAndroid Build Coastguard Worker main(void)
30*5e7646d2SAndroid Build Coastguard Worker {
31*5e7646d2SAndroid Build Coastguard Worker   http_t	*http;			/* Connection to the server */
32*5e7646d2SAndroid Build Coastguard Worker   const char	*op;			/* Operation name */
33*5e7646d2SAndroid Build Coastguard Worker   const char	*job_id_var;		/* Job ID form variable */
34*5e7646d2SAndroid Build Coastguard Worker   int		job_id;			/* Job ID */
35*5e7646d2SAndroid Build Coastguard Worker 
36*5e7646d2SAndroid Build Coastguard Worker 
37*5e7646d2SAndroid Build Coastguard Worker  /*
38*5e7646d2SAndroid Build Coastguard Worker   * Get any form variables...
39*5e7646d2SAndroid Build Coastguard Worker   */
40*5e7646d2SAndroid Build Coastguard Worker 
41*5e7646d2SAndroid Build Coastguard Worker   cgiInitialize();
42*5e7646d2SAndroid Build Coastguard Worker 
43*5e7646d2SAndroid Build Coastguard Worker  /*
44*5e7646d2SAndroid Build Coastguard Worker   * Set the web interface section...
45*5e7646d2SAndroid Build Coastguard Worker   */
46*5e7646d2SAndroid Build Coastguard Worker 
47*5e7646d2SAndroid Build Coastguard Worker   cgiSetVariable("SECTION", "jobs");
48*5e7646d2SAndroid Build Coastguard Worker   cgiSetVariable("REFRESH_PAGE", "");
49*5e7646d2SAndroid Build Coastguard Worker 
50*5e7646d2SAndroid Build Coastguard Worker  /*
51*5e7646d2SAndroid Build Coastguard Worker   * Connect to the HTTP server...
52*5e7646d2SAndroid Build Coastguard Worker   */
53*5e7646d2SAndroid Build Coastguard Worker 
54*5e7646d2SAndroid Build Coastguard Worker   http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
55*5e7646d2SAndroid Build Coastguard Worker 
56*5e7646d2SAndroid Build Coastguard Worker  /*
57*5e7646d2SAndroid Build Coastguard Worker   * Get the job ID, if any...
58*5e7646d2SAndroid Build Coastguard Worker   */
59*5e7646d2SAndroid Build Coastguard Worker 
60*5e7646d2SAndroid Build Coastguard Worker   if ((job_id_var = cgiGetVariable("JOB_ID")) != NULL)
61*5e7646d2SAndroid Build Coastguard Worker     job_id = atoi(job_id_var);
62*5e7646d2SAndroid Build Coastguard Worker   else
63*5e7646d2SAndroid Build Coastguard Worker     job_id = 0;
64*5e7646d2SAndroid Build Coastguard Worker 
65*5e7646d2SAndroid Build Coastguard Worker  /*
66*5e7646d2SAndroid Build Coastguard Worker   * Do the operation...
67*5e7646d2SAndroid Build Coastguard Worker   */
68*5e7646d2SAndroid Build Coastguard Worker 
69*5e7646d2SAndroid Build Coastguard Worker   if ((op = cgiGetVariable("OP")) != NULL && job_id > 0 && cgiIsPOST())
70*5e7646d2SAndroid Build Coastguard Worker   {
71*5e7646d2SAndroid Build Coastguard Worker    /*
72*5e7646d2SAndroid Build Coastguard Worker     * Do the operation...
73*5e7646d2SAndroid Build Coastguard Worker     */
74*5e7646d2SAndroid Build Coastguard Worker 
75*5e7646d2SAndroid Build Coastguard Worker     if (!strcmp(op, "cancel-job"))
76*5e7646d2SAndroid Build Coastguard Worker       do_job_op(http, job_id, IPP_CANCEL_JOB);
77*5e7646d2SAndroid Build Coastguard Worker     else if (!strcmp(op, "hold-job"))
78*5e7646d2SAndroid Build Coastguard Worker       do_job_op(http, job_id, IPP_HOLD_JOB);
79*5e7646d2SAndroid Build Coastguard Worker     else if (!strcmp(op, "move-job"))
80*5e7646d2SAndroid Build Coastguard Worker       cgiMoveJobs(http, NULL, job_id);
81*5e7646d2SAndroid Build Coastguard Worker     else if (!strcmp(op, "release-job"))
82*5e7646d2SAndroid Build Coastguard Worker       do_job_op(http, job_id, IPP_RELEASE_JOB);
83*5e7646d2SAndroid Build Coastguard Worker     else if (!strcmp(op, "restart-job"))
84*5e7646d2SAndroid Build Coastguard Worker       do_job_op(http, job_id, IPP_RESTART_JOB);
85*5e7646d2SAndroid Build Coastguard Worker     else
86*5e7646d2SAndroid Build Coastguard Worker     {
87*5e7646d2SAndroid Build Coastguard Worker      /*
88*5e7646d2SAndroid Build Coastguard Worker       * Bad operation code...  Display an error...
89*5e7646d2SAndroid Build Coastguard Worker       */
90*5e7646d2SAndroid Build Coastguard Worker 
91*5e7646d2SAndroid Build Coastguard Worker       cgiStartHTML(cgiText(_("Jobs")));
92*5e7646d2SAndroid Build Coastguard Worker       cgiCopyTemplateLang("error-op.tmpl");
93*5e7646d2SAndroid Build Coastguard Worker       cgiEndHTML();
94*5e7646d2SAndroid Build Coastguard Worker     }
95*5e7646d2SAndroid Build Coastguard Worker   }
96*5e7646d2SAndroid Build Coastguard Worker   else
97*5e7646d2SAndroid Build Coastguard Worker   {
98*5e7646d2SAndroid Build Coastguard Worker    /*
99*5e7646d2SAndroid Build Coastguard Worker     * Show a list of jobs...
100*5e7646d2SAndroid Build Coastguard Worker     */
101*5e7646d2SAndroid Build Coastguard Worker 
102*5e7646d2SAndroid Build Coastguard Worker     cgiStartHTML(cgiText(_("Jobs")));
103*5e7646d2SAndroid Build Coastguard Worker     cgiShowJobs(http, NULL);
104*5e7646d2SAndroid Build Coastguard Worker     cgiEndHTML();
105*5e7646d2SAndroid Build Coastguard Worker   }
106*5e7646d2SAndroid Build Coastguard Worker 
107*5e7646d2SAndroid Build Coastguard Worker  /*
108*5e7646d2SAndroid Build Coastguard Worker   * Close the HTTP server connection...
109*5e7646d2SAndroid Build Coastguard Worker   */
110*5e7646d2SAndroid Build Coastguard Worker 
111*5e7646d2SAndroid Build Coastguard Worker   httpClose(http);
112*5e7646d2SAndroid Build Coastguard Worker 
113*5e7646d2SAndroid Build Coastguard Worker  /*
114*5e7646d2SAndroid Build Coastguard Worker   * Return with no errors...
115*5e7646d2SAndroid Build Coastguard Worker   */
116*5e7646d2SAndroid Build Coastguard Worker 
117*5e7646d2SAndroid Build Coastguard Worker   return (0);
118*5e7646d2SAndroid Build Coastguard Worker }
119*5e7646d2SAndroid Build Coastguard Worker 
120*5e7646d2SAndroid Build Coastguard Worker 
121*5e7646d2SAndroid Build Coastguard Worker /*
122*5e7646d2SAndroid Build Coastguard Worker  * 'do_job_op()' - Do a job operation.
123*5e7646d2SAndroid Build Coastguard Worker  */
124*5e7646d2SAndroid Build Coastguard Worker 
125*5e7646d2SAndroid Build Coastguard Worker static void
do_job_op(http_t * http,int job_id,ipp_op_t op)126*5e7646d2SAndroid Build Coastguard Worker do_job_op(http_t      *http,		/* I - HTTP connection */
127*5e7646d2SAndroid Build Coastguard Worker           int         job_id,		/* I - Job ID */
128*5e7646d2SAndroid Build Coastguard Worker 	  ipp_op_t    op)		/* I - Operation to perform */
129*5e7646d2SAndroid Build Coastguard Worker {
130*5e7646d2SAndroid Build Coastguard Worker   ipp_t		*request;		/* IPP request */
131*5e7646d2SAndroid Build Coastguard Worker   char		uri[HTTP_MAX_URI];	/* Job URI */
132*5e7646d2SAndroid Build Coastguard Worker   const char	*user;			/* Username */
133*5e7646d2SAndroid Build Coastguard Worker 
134*5e7646d2SAndroid Build Coastguard Worker 
135*5e7646d2SAndroid Build Coastguard Worker  /*
136*5e7646d2SAndroid Build Coastguard Worker   * Build a job request, which requires the following
137*5e7646d2SAndroid Build Coastguard Worker   * attributes:
138*5e7646d2SAndroid Build Coastguard Worker   *
139*5e7646d2SAndroid Build Coastguard Worker   *    attributes-charset
140*5e7646d2SAndroid Build Coastguard Worker   *    attributes-natural-language
141*5e7646d2SAndroid Build Coastguard Worker   *    job-uri or printer-uri (purge-jobs)
142*5e7646d2SAndroid Build Coastguard Worker   *    requesting-user-name
143*5e7646d2SAndroid Build Coastguard Worker   */
144*5e7646d2SAndroid Build Coastguard Worker 
145*5e7646d2SAndroid Build Coastguard Worker   request = ippNewRequest(op);
146*5e7646d2SAndroid Build Coastguard Worker 
147*5e7646d2SAndroid Build Coastguard Worker   snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
148*5e7646d2SAndroid Build Coastguard Worker 
149*5e7646d2SAndroid Build Coastguard Worker   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
150*5e7646d2SAndroid Build Coastguard Worker                NULL, uri);
151*5e7646d2SAndroid Build Coastguard Worker 
152*5e7646d2SAndroid Build Coastguard Worker   if ((user = getenv("REMOTE_USER")) == NULL)
153*5e7646d2SAndroid Build Coastguard Worker     user = "guest";
154*5e7646d2SAndroid Build Coastguard Worker 
155*5e7646d2SAndroid Build Coastguard Worker   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
156*5e7646d2SAndroid Build Coastguard Worker                "requesting-user-name", NULL, user);
157*5e7646d2SAndroid Build Coastguard Worker 
158*5e7646d2SAndroid Build Coastguard Worker  /*
159*5e7646d2SAndroid Build Coastguard Worker   * Do the request and get back a response...
160*5e7646d2SAndroid Build Coastguard Worker   */
161*5e7646d2SAndroid Build Coastguard Worker 
162*5e7646d2SAndroid Build Coastguard Worker   ippDelete(cupsDoRequest(http, request, "/jobs"));
163*5e7646d2SAndroid Build Coastguard Worker 
164*5e7646d2SAndroid Build Coastguard Worker   if (cupsLastError() <= IPP_OK_CONFLICT && getenv("HTTP_REFERER"))
165*5e7646d2SAndroid Build Coastguard Worker   {
166*5e7646d2SAndroid Build Coastguard Worker    /*
167*5e7646d2SAndroid Build Coastguard Worker     * Redirect successful updates back to the parent page...
168*5e7646d2SAndroid Build Coastguard Worker     */
169*5e7646d2SAndroid Build Coastguard Worker 
170*5e7646d2SAndroid Build Coastguard Worker     char	url[1024];		/* Encoded URL */
171*5e7646d2SAndroid Build Coastguard Worker 
172*5e7646d2SAndroid Build Coastguard Worker 
173*5e7646d2SAndroid Build Coastguard Worker     strlcpy(url, "5;URL=", sizeof(url));
174*5e7646d2SAndroid Build Coastguard Worker     cgiFormEncode(url + 6, getenv("HTTP_REFERER"), sizeof(url) - 6);
175*5e7646d2SAndroid Build Coastguard Worker     cgiSetVariable("refresh_page", url);
176*5e7646d2SAndroid Build Coastguard Worker   }
177*5e7646d2SAndroid Build Coastguard Worker   else if (cupsLastError() == IPP_NOT_AUTHORIZED)
178*5e7646d2SAndroid Build Coastguard Worker   {
179*5e7646d2SAndroid Build Coastguard Worker     puts("Status: 401\n");
180*5e7646d2SAndroid Build Coastguard Worker     exit(0);
181*5e7646d2SAndroid Build Coastguard Worker   }
182*5e7646d2SAndroid Build Coastguard Worker 
183*5e7646d2SAndroid Build Coastguard Worker   cgiStartHTML(cgiText(_("Jobs")));
184*5e7646d2SAndroid Build Coastguard Worker 
185*5e7646d2SAndroid Build Coastguard Worker   if (cupsLastError() > IPP_OK_CONFLICT)
186*5e7646d2SAndroid Build Coastguard Worker     cgiShowIPPError(_("Job operation failed"));
187*5e7646d2SAndroid Build Coastguard Worker   else if (op == IPP_CANCEL_JOB)
188*5e7646d2SAndroid Build Coastguard Worker     cgiCopyTemplateLang("job-cancel.tmpl");
189*5e7646d2SAndroid Build Coastguard Worker   else if (op == IPP_HOLD_JOB)
190*5e7646d2SAndroid Build Coastguard Worker     cgiCopyTemplateLang("job-hold.tmpl");
191*5e7646d2SAndroid Build Coastguard Worker   else if (op == IPP_RELEASE_JOB)
192*5e7646d2SAndroid Build Coastguard Worker     cgiCopyTemplateLang("job-release.tmpl");
193*5e7646d2SAndroid Build Coastguard Worker   else if (op == IPP_RESTART_JOB)
194*5e7646d2SAndroid Build Coastguard Worker     cgiCopyTemplateLang("job-restart.tmpl");
195*5e7646d2SAndroid Build Coastguard Worker 
196*5e7646d2SAndroid Build Coastguard Worker   cgiEndHTML();
197*5e7646d2SAndroid Build Coastguard Worker }
198