]> git.ipfire.org Git - thirdparty/cups.git/blame - berkeley/lprm.c
Add httpConnectEncrypt() and fix all code that uses the
[thirdparty/cups.git] / berkeley / lprm.c
CommitLineData
bd0b97ff 1/*
a1793153 2 * "$Id: lprm.c,v 1.16 2001/05/06 00:11:22 mike Exp $"
bd0b97ff 3 *
4 * "lprm" command for the Common UNIX Printing System (CUPS).
5 *
d2935a0f 6 * Copyright 1997-2001 by Easy Software Products.
bd0b97ff 7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
8784b6a6 17 * 44141 Airport View Drive, Suite 204
bd0b97ff 18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Parse options and cancel jobs.
27 */
28
29/*
30 * Include necessary headers...
31 */
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <ctype.h>
36
37#include <cups/cups.h>
38#include <cups/language.h>
39
40
41/*
42 * 'main()' - Parse options and cancel jobs.
43 */
44
45int /* O - Exit status */
46main(int argc, /* I - Number of command-line arguments */
47 char *argv[]) /* I - Command-line arguments */
48{
49 http_t *http; /* HTTP connection to server */
50 int i; /* Looping var */
51 int job_id; /* Job ID */
48574397 52 const char *dest; /* Destination printer */
79e9fc64 53 char *instance; /* Pointer to instance name */
bd0b97ff 54 char uri[1024]; /* Printer or job URI */
55 ipp_t *request; /* IPP request */
56 ipp_t *response; /* IPP response */
57 ipp_op_t op; /* Operation */
58 cups_lang_t *language; /* Language */
99cc28d6 59 int num_dests; /* Number of destinations */
60 cups_dest_t *dests; /* Destinations */
1c9e0181 61 http_encryption_t encryption; /* Encryption? */
bd0b97ff 62
63
64 /*
65 * Setup to cancel individual print jobs...
66 */
67
1c9e0181 68 op = IPP_CANCEL_JOB;
69 job_id = 0;
70 dest = NULL;
71 response = NULL;
72 http = NULL;
73 encryption = cupsEncryption();
bd0b97ff 74
99cc28d6 75 num_dests = cupsGetDests(&dests);
76
77 for (i = 0; i < num_dests; i ++)
78 if (dests[i].is_default)
79 dest = dests[i].name;
80
bd0b97ff 81 /*
82 * Open a connection to the server...
83 */
84
a1793153 85 if ((http = httpConnectEncrypt(cupsServer(), ippPort(), encryption)) == NULL)
bd0b97ff 86 {
87 fputs("lprm: Unable to contact server!\n", stderr);
99cc28d6 88 cupsFreeDests(num_dests, dests);
bd0b97ff 89 return (1);
90 }
91
92 /*
93 * Process command-line arguments...
94 */
95
96 for (i = 1; i < argc; i ++)
97 if (argv[i][0] == '-' && argv[i][1] != '\0')
98 switch (argv[i][1])
99 {
1c9e0181 100 case 'E' : /* Encrypt */
101#ifdef HAVE_LIBSSL
102 encryption = HTTP_ENCRYPT_REQUIRED;
103
104 httpEncryption(http, encryption);
105#else
106 fprintf(stderr, "%s: Sorry, no encryption support compiled in!\n",
107 argv[0]);
108#endif /* HAVE_LIBSSL */
109 break;
110
bd0b97ff 111 case 'P' : /* Cancel jobs on a printer */
112 if (argv[i][2])
113 dest = argv[i] + 2;
114 else
115 {
116 i ++;
117 dest = argv[i];
118 }
79e9fc64 119
120 if ((instance = strchr(dest, '/')) != NULL)
121 *instance = '\0';
bd0b97ff 122 break;
123
124 default :
125 fprintf(stderr, "lprm: Unknown option \'%c\'!\n", argv[i][1]);
99cc28d6 126 cupsFreeDests(num_dests, dests);
127 httpClose(http);
bd0b97ff 128 return (1);
129 }
130 else
131 {
132 /*
133 * Cancel a job or printer...
134 */
135
136 if (isdigit(argv[i][0]))
137 {
138 dest = NULL;
139 op = IPP_CANCEL_JOB;
140 job_id = atoi(argv[i]);
141 }
142 else if (strcmp(argv[i], "-") == 0)
143 {
144 /*
145 * Cancel all jobs
146 */
147
148 op = IPP_PURGE_JOBS;
149 }
150 else
50b9556e 151 {
152 dest = argv[i];
bd0b97ff 153 job_id = 0;
50b9556e 154 }
bd0b97ff 155
156 /*
157 * Build an IPP request, which requires the following
158 * attributes:
159 *
160 * attributes-charset
161 * attributes-natural-language
162 * printer-uri + job-id *or* job-uri
163 * [requesting-user-name]
164 */
165
166 request = ippNew();
167
168 request->request.op.operation_id = op;
169 request->request.op.request_id = 1;
170
171 language = cupsLangDefault();
172
173 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
174 "attributes-charset", NULL, cupsLangEncoding(language));
175
176 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
177 "attributes-natural-language", NULL, language->language);
178
179 if (dest)
180 {
970017a4 181 snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", dest);
bd0b97ff 182 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
183 "printer-uri", NULL, uri);
184 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
185 job_id);
186 }
187 else
188 {
f8ab8b97 189 sprintf(uri, "ipp://localhost/jobs/%d", job_id);
bd0b97ff 190 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
191 uri);
192 }
193
8dbf3e6a 194 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
195 "requesting-user-name", NULL, cupsUser());
196
bd0b97ff 197 /*
198 * Do the request and get back a response...
199 */
200
201 if (op == IPP_PURGE_JOBS)
202 response = cupsDoRequest(http, request, "/admin/");
203 else
204 response = cupsDoRequest(http, request, "/jobs/");
205
206 if (response != NULL)
67e0502b 207 {
8dbf3e6a 208 switch (response->request.status.status_code)
209 {
210 case IPP_NOT_FOUND :
211 fputs("lprm: Job or printer not found!\n", stderr);
212 break;
213 case IPP_NOT_AUTHORIZED :
214 fputs("lprm: Not authorized to lprm job(s)!\n", stderr);
215 break;
216 case IPP_FORBIDDEN :
217 fprintf(stderr, "lprm: You don't own job ID %d!\n", job_id);
218 break;
219 default :
220 if (response->request.status.status_code > IPP_OK_CONFLICT)
221 fputs("lprm: Unable to lprm job(s)!\n", stderr);
222 break;
223 }
67e0502b 224
50b9556e 225 if (response->request.status.status_code > IPP_OK_CONFLICT)
226 {
227 ippDelete(response);
228 cupsFreeDests(num_dests, dests);
229 httpClose(http);
230 return (1);
231 }
232
bd0b97ff 233 ippDelete(response);
67e0502b 234 }
bd0b97ff 235 else
236 {
237 fputs("lprm: Unable to cancel job(s)!\n", stderr);
99cc28d6 238 cupsFreeDests(num_dests, dests);
239 httpClose(http);
bd0b97ff 240 return (1);
241 }
242 }
243
244 /*
245 * If nothing has been cancelled yet, cancel the current job on the specified
246 * (or default) printer...
247 */
248
249 if (response == NULL)
250 if (!cupsCancelJob(dest, 0))
251 {
252 fputs("lprm: Unable to cancel job(s)!\n", stderr);
99cc28d6 253 cupsFreeDests(num_dests, dests);
254 httpClose(http);
bd0b97ff 255 return (1);
256 }
257
99cc28d6 258 cupsFreeDests(num_dests, dests);
259 httpClose(http);
260
bd0b97ff 261 return (0);
262}
263
264
265/*
a1793153 266 * End of "$Id: lprm.c,v 1.16 2001/05/06 00:11:22 mike Exp $".
bd0b97ff 267 */