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