libgpo: apply some const.
[Samba.git] / source3 / printing / print_iprint.c
blob1c9e5a25922780993bf10a0f1fc5c112e7df4c3f
1 /*
2 * Support code for Novell iPrint using the Common UNIX Printing
3 * System ("CUPS") libraries
5 * Copyright 1999-2003 by Michael R Sweet.
6 * Portions Copyright 2005 by Joel J. Smith.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "printing.h"
24 #include "printing/pcap.h"
26 #ifdef HAVE_IPRINT
27 #include <cups/cups.h>
28 #include <cups/language.h>
30 #define OPERATION_NOVELL_LIST_PRINTERS 0x401A
31 #define OPERATION_NOVELL_MGMT 0x401C
32 #define NOVELL_SERVER_SYSNAME "sysname="
33 #define NOVELL_SERVER_SYSNAME_NETWARE "NetWare IA32"
34 #define NOVELL_SERVER_VERSION_STRING "iprintserverversion="
35 #define NOVELL_SERVER_VERSION_OES_SP1 33554432
37 #if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
38 #define HAVE_CUPS_1_6 1
39 #endif
41 #ifndef HAVE_CUPS_1_6
42 #define ippGetCount(attr) attr->num_values
43 #define ippGetGroupTag(attr) attr->group_tag
44 #define ippGetName(attr) attr->name
45 #define ippGetValueTag(attr) attr->value_tag
46 #define ippGetStatusCode(ipp) ipp->request.status.status_code
47 #define ippGetBoolean(attr, element) attr->values[element].boolean
48 #define ippGetInteger(attr, element) attr->values[element].integer
49 #define ippGetString(attr, element, language) attr->values[element].string.text
51 static ipp_attribute_t *
52 ippFirstAttribute(ipp_t *ipp)
54 if (!ipp)
55 return (NULL);
56 return (ipp->current = ipp->attrs);
59 static ipp_attribute_t *
60 ippNextAttribute(ipp_t *ipp)
62 if (!ipp || !ipp->current)
63 return (NULL);
64 return (ipp->current = ipp->current->next);
67 static int ippSetOperation(ipp_t *ipp, ipp_op_t op)
69 ipp->request.op.operation_id = op;
70 return (1);
73 static int ippSetRequestId(ipp_t *ipp, int request_id)
75 ipp->request.any.request_id = request_id;
76 return (1);
78 #endif
81 * 'iprint_passwd_cb()' - The iPrint password callback...
84 static const char * /* O - Password or NULL */
85 iprint_passwd_cb(const char *prompt) /* I - Prompt */
88 * Always return NULL to indicate that no password is available...
91 return (NULL);
94 static const char *iprint_server(void)
96 const char *server = lp_iprint_server(talloc_tos());
98 if ((server != NULL) && (strlen(server) > 0)) {
99 DEBUG(10, ("iprint server explicitly set to %s\n",
100 server));
101 return server;
104 DEBUG(10, ("iprint server left to default %s\n", cupsServer()));
105 return cupsServer();
109 * Pass in an already connected http_t*
110 * Returns the server version if one can be found, multiplied by
111 * -1 for all NetWare versions. Returns 0 if a server version
112 * cannot be determined
115 static int iprint_get_server_version(http_t *http, char* serviceUri)
117 ipp_t *request = NULL, /* IPP Request */
118 *response = NULL; /* IPP Response */
119 ipp_attribute_t *attr; /* Current attribute */
120 cups_lang_t *language = NULL; /* Default language */
121 char *ver; /* server version pointer */
122 char *vertmp; /* server version tmp pointer */
123 int serverVersion = 0; /* server version */
124 char *os; /* server os */
125 int osFlag = 0; /* 0 for NetWare, 1 for anything else */
126 char *temp; /* pointer for string manipulation */
129 * Build an OPERATION_NOVELL_MGMT("get-server-version") request,
130 * which requires the following attributes:
132 * attributes-charset
133 * attributes-natural-language
134 * operation-name
135 * service-uri
138 request = ippNew();
140 ippSetOperation(request, (ipp_op_t)OPERATION_NOVELL_MGMT);
141 ippSetRequestId(request, 1);
143 language = cupsLangDefault();
145 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
146 "attributes-charset", NULL, "utf-8");
148 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
149 "attributes-natural-language", NULL, language->language);
151 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
152 "service-uri", NULL, serviceUri);
154 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
155 "operation-name", NULL, "get-server-version");
158 * Do the request and get back a response...
161 if (((response = cupsDoRequest(http, request, "/ipp/")) == NULL) ||
162 (ippGetStatusCode(response) >= IPP_OK_CONFLICT))
163 goto out;
165 if (((attr = ippFindAttribute(response, "server-version",
166 IPP_TAG_STRING)) != NULL)) {
167 if ((ver = strstr(ippGetString(attr, 0, NULL),
168 NOVELL_SERVER_VERSION_STRING)) != NULL) {
169 ver += strlen(NOVELL_SERVER_VERSION_STRING);
171 * Strangely, libcups stores a IPP_TAG_STRING (octet
172 * string) as a null-terminated string with no length
173 * even though it could be binary data with nulls in
174 * it. Luckily, in this case the value is not binary.
176 serverVersion = strtol(ver, &vertmp, 10);
178 /* Check for not found, overflow or negative version */
179 if ((ver == vertmp) || (serverVersion < 0))
180 serverVersion = 0;
183 if ((os = strstr(ippGetString(attr, 0, NULL),
184 NOVELL_SERVER_SYSNAME)) != NULL) {
185 os += strlen(NOVELL_SERVER_SYSNAME);
186 if ((temp = strchr(os,'<')) != NULL)
187 *temp = '\0';
188 if (strcmp(os,NOVELL_SERVER_SYSNAME_NETWARE))
189 osFlag = 1; /* 1 for non-NetWare systems */
193 out:
194 if (response)
195 ippDelete(response);
197 if (language)
198 cupsLangFree(language);
200 if (osFlag == 0)
201 serverVersion *= -1;
203 return serverVersion;
207 static int iprint_cache_add_printer(http_t *http,
208 int reqId,
209 const char *url,
210 struct pcap_cache **pcache)
212 ipp_t *request = NULL, /* IPP Request */
213 *response = NULL; /* IPP Response */
214 ipp_attribute_t *attr; /* Current attribute */
215 cups_lang_t *language = NULL; /* Default language */
216 const char *name, /* printer-name attribute */
217 *info; /* printer-info attribute */
218 char smb_enabled, /* smb-enabled attribute */
219 secure; /* security-enabled attrib. */
221 const char *httpPath; /* path portion of the printer-uri */
223 static const char *pattrs[] = /* Requested printer attributes */
225 "printer-name",
226 "security-enabled",
227 "printer-info",
228 "smb-enabled"
231 request = ippNew();
233 ippSetOperation(request, IPP_GET_PRINTER_ATTRIBUTES);
234 ippSetRequestId(request, reqId);
236 language = cupsLangDefault();
238 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
239 "attributes-charset", NULL, "utf-8");
241 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
242 "attributes-natural-language", NULL, language->language);
244 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, url);
246 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
247 "requested-attributes",
248 (sizeof(pattrs) / sizeof(pattrs[0])),
249 NULL, pattrs);
252 * Do the request and get back a response...
255 if ((httpPath = strstr(url,"://")) == NULL ||
256 (httpPath = strchr(httpPath+3,'/')) == NULL)
258 ippDelete(request);
259 request = NULL;
260 goto out;
263 if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
264 ipp_status_t lastErr = cupsLastError();
267 * Ignore printers that cannot be queried without credentials
269 if (lastErr == IPP_FORBIDDEN ||
270 lastErr == IPP_NOT_AUTHENTICATED ||
271 lastErr == IPP_NOT_AUTHORIZED)
272 goto out;
274 DEBUG(0,("Unable to get printer list - %s\n",
275 ippErrorString(lastErr)));
276 goto out;
279 for (attr = ippFirstAttribute(response); attr != NULL;) {
281 * Skip leading attributes until we hit a printer...
284 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER)
285 attr = ippNextAttribute(response);
287 if (attr == NULL)
288 break;
291 * Pull the needed attributes from this printer...
294 name = NULL;
295 info = NULL;
296 smb_enabled= 1;
297 secure = 0;
299 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_PRINTER) {
300 if (strcmp(ippGetName(attr), "printer-name") == 0 &&
301 ippGetValueTag(attr) == IPP_TAG_NAME)
302 name = ippGetString(attr, 0, NULL);
304 if (strcmp(ippGetName(attr), "printer-info") == 0 &&
305 (ippGetValueTag(attr) == IPP_TAG_TEXT ||
306 ippGetValueTag(attr) == IPP_TAG_TEXTLANG))
307 info = ippGetString(attr, 0, NULL);
310 * If the smb-enabled attribute is present and the
311 * value is set to 0, don't show the printer.
312 * If the attribute is not present, assume that the
313 * printer should show up
315 if (!strcmp(ippGetName(attr), "smb-enabled") &&
316 ((ippGetValueTag(attr) == IPP_TAG_INTEGER &&
317 !ippGetInteger(attr, 0)) ||
318 (ippGetValueTag(attr) == IPP_TAG_BOOLEAN &&
319 !ippGetBoolean(attr, 0))))
320 smb_enabled = 0;
323 * If the security-enabled attribute is present and the
324 * value is set to 1, don't show the printer.
325 * If the attribute is not present, assume that the
326 * printer should show up
328 if (!strcmp(ippGetName(attr), "security-enabled") &&
329 ((ippGetValueTag(attr) == IPP_TAG_INTEGER &&
330 ippGetInteger(attr, 0)) ||
331 (ippGetValueTag(attr) == IPP_TAG_BOOLEAN &&
332 ippGetBoolean(attr, 0))))
333 secure = 1;
335 attr = ippNextAttribute(response);
339 * See if we have everything needed...
340 * Make sure the printer is not a secure printer
341 * and make sure smb printing hasn't been explicitly
342 * disabled for the printer
345 if (name != NULL && !secure && smb_enabled)
346 pcap_cache_add_specific(pcache, name, info, NULL);
349 out:
350 if (response)
351 ippDelete(response);
352 return(0);
355 bool iprint_cache_reload(struct pcap_cache **_pcache)
357 http_t *http = NULL; /* HTTP connection to server */
358 ipp_t *request = NULL, /* IPP Request */
359 *response = NULL; /* IPP Response */
360 ipp_attribute_t *attr; /* Current attribute */
361 cups_lang_t *language = NULL; /* Default language */
362 int i;
363 bool ret = false;
364 struct pcap_cache *pcache = NULL;
366 DEBUG(5, ("reloading iprint printcap cache\n"));
369 * Make sure we don't ask for passwords...
372 cupsSetPasswordCB(iprint_passwd_cb);
375 * Try to connect to the server...
378 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
379 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
380 iprint_server(), strerror(errno)));
381 goto out;
385 * Build a OPERATION_NOVELL_LIST_PRINTERS request, which requires the following attributes:
387 * attributes-charset
388 * attributes-natural-language
391 request = ippNew();
393 ippSetOperation(request, (ipp_op_t)OPERATION_NOVELL_LIST_PRINTERS);
394 ippSetRequestId(request, 1);
396 language = cupsLangDefault();
398 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
399 "attributes-charset", NULL, "utf-8");
401 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
402 "attributes-natural-language", NULL, language->language);
404 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
405 "ipp-server", NULL, "ippSrvr");
408 * Do the request and get back a response...
411 if ((response = cupsDoRequest(http, request, "/ipp")) == NULL) {
412 DEBUG(0,("Unable to get printer list - %s\n",
413 ippErrorString(cupsLastError())));
414 goto out;
417 for (attr = ippFirstAttribute(response); attr != NULL;) {
419 * Skip leading attributes until we hit a printer...
422 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER)
423 attr = ippNextAttribute(response);
425 if (attr == NULL)
426 break;
429 * Pull the needed attributes from this printer...
432 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_PRINTER)
434 if (strcmp(ippGetName(attr), "printer-name") == 0 &&
435 (ippGetValueTag(attr) == IPP_TAG_URI ||
436 ippGetValueTag(attr) == IPP_TAG_NAME ||
437 ippGetValueTag(attr) == IPP_TAG_TEXT ||
438 ippGetValueTag(attr) == IPP_TAG_NAMELANG ||
439 ippGetValueTag(attr) == IPP_TAG_TEXTLANG))
441 for (i = 0; i<ippGetCount(attr); i++)
443 const char *url = ippGetString(attr, i, NULL);
444 if (!url || !strlen(url))
445 continue;
446 iprint_cache_add_printer(http, i+2, url,
447 &pcache);
450 attr = ippNextAttribute(response);
454 ret = true;
455 *_pcache = pcache;
457 out:
458 if (response)
459 ippDelete(response);
461 if (language)
462 cupsLangFree(language);
464 if (http)
465 httpClose(http);
467 return ret;
472 * 'iprint_job_delete()' - Delete a job.
475 static int iprint_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
477 int ret = 1; /* Return value */
478 http_t *http = NULL; /* HTTP connection to server */
479 ipp_t *request = NULL, /* IPP Request */
480 *response = NULL; /* IPP Response */
481 cups_lang_t *language = NULL; /* Default language */
482 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
483 char httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
486 DEBUG(5,("iprint_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
489 * Make sure we don't ask for passwords...
492 cupsSetPasswordCB(iprint_passwd_cb);
495 * Try to connect to the server...
498 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
499 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
500 iprint_server(), strerror(errno)));
501 goto out;
505 * Build an IPP_CANCEL_JOB request, which uses the following
506 * attributes:
508 * attributes-charset
509 * attributes-natural-language
510 * printer-uri
511 * job-id
512 * requesting-user-name
515 request = ippNew();
517 ippSetOperation(request, IPP_CANCEL_JOB);
518 ippSetRequestId(request, 1);
520 language = cupsLangDefault();
522 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
523 "attributes-charset", NULL, "utf-8");
525 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
526 "attributes-natural-language", NULL, language->language);
528 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), sharename);
530 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
532 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
534 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
535 NULL, pjob->user);
538 * Do the request and get back a response...
541 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", sharename);
543 if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
544 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
545 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
546 ippErrorString(cupsLastError())));
547 } else {
548 ret = 0;
550 } else {
551 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
552 ippErrorString(cupsLastError())));
555 out:
556 if (response)
557 ippDelete(response);
559 if (language)
560 cupsLangFree(language);
562 if (http)
563 httpClose(http);
565 return ret;
570 * 'iprint_job_pause()' - Pause a job.
573 static int iprint_job_pause(int snum, struct printjob *pjob)
575 int ret = 1; /* Return value */
576 http_t *http = NULL; /* HTTP connection to server */
577 ipp_t *request = NULL, /* IPP Request */
578 *response = NULL; /* IPP Response */
579 cups_lang_t *language = NULL; /* Default language */
580 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
581 char httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
584 DEBUG(5,("iprint_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
587 * Make sure we don't ask for passwords...
590 cupsSetPasswordCB(iprint_passwd_cb);
593 * Try to connect to the server...
596 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
597 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
598 iprint_server(), strerror(errno)));
599 goto out;
603 * Build an IPP_HOLD_JOB request, which requires the following
604 * attributes:
606 * attributes-charset
607 * attributes-natural-language
608 * printer-uri
609 * job-id
610 * requesting-user-name
613 request = ippNew();
615 ippSetOperation(request, IPP_HOLD_JOB);
616 ippSetRequestId(request, 1);
618 language = cupsLangDefault();
620 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
621 "attributes-charset", NULL, "utf-8");
623 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
624 "attributes-natural-language", NULL, language->language);
626 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(),
627 lp_printername(talloc_tos(), snum));
629 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
631 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
633 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
634 NULL, pjob->user);
637 * Do the request and get back a response...
640 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s",
641 lp_printername(talloc_tos(), snum));
643 if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
644 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
645 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
646 ippErrorString(cupsLastError())));
647 } else {
648 ret = 0;
650 } else {
651 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
652 ippErrorString(cupsLastError())));
655 out:
656 if (response)
657 ippDelete(response);
659 if (language)
660 cupsLangFree(language);
662 if (http)
663 httpClose(http);
665 return ret;
670 * 'iprint_job_resume()' - Resume a paused job.
673 static int iprint_job_resume(int snum, struct printjob *pjob)
675 int ret = 1; /* Return value */
676 http_t *http = NULL; /* HTTP connection to server */
677 ipp_t *request = NULL, /* IPP Request */
678 *response = NULL; /* IPP Response */
679 cups_lang_t *language = NULL; /* Default language */
680 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
681 char httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
684 DEBUG(5,("iprint_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
687 * Make sure we don't ask for passwords...
690 cupsSetPasswordCB(iprint_passwd_cb);
693 * Try to connect to the server...
696 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
697 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
698 iprint_server(), strerror(errno)));
699 goto out;
703 * Build an IPP_RELEASE_JOB request, which requires the following
704 * attributes:
706 * attributes-charset
707 * attributes-natural-language
708 * printer-uri
709 * job-id
710 * requesting-user-name
713 request = ippNew();
715 ippSetOperation(request, IPP_RELEASE_JOB);
716 ippSetRequestId(request, 1);
718 language = cupsLangDefault();
720 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
721 "attributes-charset", NULL, "utf-8");
723 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
724 "attributes-natural-language", NULL, language->language);
726 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(),
727 lp_printername(talloc_tos(), snum));
729 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
731 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
733 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
734 NULL, pjob->user);
737 * Do the request and get back a response...
740 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s",
741 lp_printername(talloc_tos(), snum));
743 if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
744 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
745 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
746 ippErrorString(cupsLastError())));
747 } else {
748 ret = 0;
750 } else {
751 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
752 ippErrorString(cupsLastError())));
755 out:
756 if (response)
757 ippDelete(response);
759 if (language)
760 cupsLangFree(language);
762 if (http)
763 httpClose(http);
765 return ret;
770 * 'iprint_job_submit()' - Submit a job for printing.
773 static int iprint_job_submit(int snum, struct printjob *pjob,
774 enum printing_types printing_type,
775 char *lpq_cmd)
777 int ret = 1; /* Return value */
778 http_t *http = NULL; /* HTTP connection to server */
779 ipp_t *request = NULL, /* IPP Request */
780 *response = NULL; /* IPP Response */
781 ipp_attribute_t *attr; /* Current attribute */
782 cups_lang_t *language = NULL; /* Default language */
783 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
785 DEBUG(5,("iprint_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
788 * Make sure we don't ask for passwords...
791 cupsSetPasswordCB(iprint_passwd_cb);
794 * Try to connect to the server...
797 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
798 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
799 iprint_server(), strerror(errno)));
800 goto out;
804 * Build an IPP_PRINT_JOB request, which requires the following
805 * attributes:
807 * attributes-charset
808 * attributes-natural-language
809 * printer-uri
810 * requesting-user-name
811 * [document-data]
814 request = ippNew();
816 ippSetOperation(request, IPP_PRINT_JOB);
817 ippSetRequestId(request, 1);
819 language = cupsLangDefault();
821 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
822 "attributes-charset", NULL, "utf-8");
824 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
825 "attributes-natural-language", NULL, language->language);
827 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(),
828 lp_printername(talloc_tos(), snum));
830 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
831 "printer-uri", NULL, uri);
833 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
834 NULL, pjob->user);
836 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
837 "job-originating-host-name", NULL,
838 pjob->clientmachine);
840 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
841 pjob->jobname);
844 * Do the request and get back a response...
847 slprintf(uri, sizeof(uri) - 1, "/ipp/%s", lp_printername(talloc_tos(), snum));
849 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
850 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
851 DEBUG(0,("Unable to print file to %s - %s\n",
852 lp_printername(talloc_tos(), snum),
853 ippErrorString(cupsLastError())));
854 } else {
855 ret = 0;
857 } else {
858 DEBUG(0,("Unable to print file to `%s' - %s\n",
859 lp_printername(talloc_tos(), snum),
860 ippErrorString(cupsLastError())));
863 if ( ret == 0 )
864 unlink(pjob->filename);
865 /* else print_job_end will do it for us */
867 if ( ret == 0 ) {
869 attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
870 if (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_JOB)
872 pjob->sysjob = ippGetInteger(attr, 0);
876 out:
877 if (response)
878 ippDelete(response);
880 if (language)
881 cupsLangFree(language);
883 if (http)
884 httpClose(http);
886 return ret;
890 * 'iprint_queue_get()' - Get all the jobs in the print queue.
893 static int iprint_queue_get(const char *sharename,
894 enum printing_types printing_type,
895 char *lpq_command,
896 print_queue_struct **q,
897 print_status_struct *status)
899 fstring printername;
900 http_t *http = NULL; /* HTTP connection to server */
901 ipp_t *request = NULL, /* IPP Request */
902 *response = NULL; /* IPP Response */
903 ipp_attribute_t *attr = NULL; /* Current attribute */
904 cups_lang_t *language = NULL; /* Default language */
905 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
906 char serviceUri[HTTP_MAX_URI]; /* service-uri attribute */
907 char httpPath[HTTP_MAX_URI]; /* path portion of the uri */
908 int jobUseUnixTime = 0; /* Whether job times should
909 * be assumed to be Unix time */
910 int qcount = 0, /* Number of active queue entries */
911 qalloc = 0; /* Number of queue entries allocated */
912 print_queue_struct *queue = NULL, /* Queue entries */
913 *temp; /* Temporary pointer for queue */
914 const char *user_name, /* job-originating-user-name attribute */
915 *job_name; /* job-name attribute */
916 int job_id; /* job-id attribute */
917 int job_k_octets; /* job-k-octets attribute */
918 time_t job_time; /* time-at-creation attribute */
919 time_t printer_up_time = 0; /* printer's uptime */
920 ipp_jstate_t job_status; /* job-status attribute */
921 int job_priority; /* job-priority attribute */
922 static const char *jattrs[] = /* Requested job attributes */
924 "job-id",
925 "job-k-octets",
926 "job-name",
927 "job-originating-user-name",
928 "job-priority",
929 "job-state",
930 "time-at-creation",
932 static const char *pattrs[] = /* Requested printer attributes */
934 "printer-state",
935 "printer-state-message",
936 "printer-current-time",
937 "printer-up-time"
940 *q = NULL;
942 /* HACK ALERT!!! The porblem with support the 'printer name'
943 option is that we key the tdb off the sharename. So we will
944 overload the lpq_command string to pass in the printername
945 (which is basically what we do for non-cups printers ... using
946 the lpq_command to get the queue listing). */
948 fstrcpy( printername, lpq_command );
950 DEBUG(5,("iprint_queue_get(%s, %p, %p)\n", printername, q, status));
953 * Make sure we don't ask for passwords...
956 cupsSetPasswordCB(iprint_passwd_cb);
959 * Try to connect to the server...
962 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
963 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
964 iprint_server(), strerror(errno)));
965 goto out;
969 * Generate the printer URI and the service URI that goes with it...
972 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), printername);
973 slprintf(serviceUri, sizeof(serviceUri) - 1, "ipp://%s/ipp/", iprint_server());
976 * For Linux iPrint servers from OES SP1 on, the iPrint server
977 * uses Unix time for job start times unless it detects the iPrint
978 * client in an http User-Agent header. (This was done to accomodate
979 * CUPS broken behavior. According to RFC 2911, section 4.3.14, job
980 * start times are supposed to be relative to how long the printer has
981 * been up.) Since libcups doesn't allow us to set that header before
982 * the request is sent, this ugly hack allows us to detect the server
983 * version and decide how to interpret the job time.
985 if (iprint_get_server_version(http, serviceUri) >=
986 NOVELL_SERVER_VERSION_OES_SP1)
987 jobUseUnixTime = 1;
989 request = ippNew();
991 ippSetOperation(request, IPP_GET_PRINTER_ATTRIBUTES);
992 ippSetRequestId(request, 2);
994 language = cupsLangDefault();
996 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
997 "attributes-charset", NULL, "utf-8");
999 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1000 "attributes-natural-language", NULL, language->language);
1002 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1003 "printer-uri", NULL, uri);
1005 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1006 "requested-attributes",
1007 (sizeof(pattrs) / sizeof(pattrs[0])),
1008 NULL, pattrs);
1011 * Do the request and get back a response...
1014 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", printername);
1016 if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
1017 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1018 ippErrorString(cupsLastError())));
1019 *q = queue;
1020 goto out;
1023 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1024 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1025 ippErrorString(ippGetStatusCode(response))));
1026 *q = queue;
1027 goto out;
1031 * Get the current printer status and convert it to the SAMBA values.
1034 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1035 if (ippGetInteger(attr, 0) == IPP_PRINTER_STOPPED)
1036 status->status = LPSTAT_STOPPED;
1037 else
1038 status->status = LPSTAT_OK;
1041 if ((attr = ippFindAttribute(response, "printer-state-message",
1042 IPP_TAG_TEXT)) != NULL)
1043 fstrcpy(status->message, ippGetString(attr, 0, NULL));
1045 if ((attr = ippFindAttribute(response, "printer-up-time",
1046 IPP_TAG_INTEGER)) != NULL)
1047 printer_up_time = ippGetInteger(attr, 0);
1049 ippDelete(response);
1050 response = NULL;
1053 * Build an IPP_GET_JOBS request, which requires the following
1054 * attributes:
1056 * attributes-charset
1057 * attributes-natural-language
1058 * requested-attributes
1059 * printer-uri
1062 request = ippNew();
1064 ippSetOperation(request, IPP_GET_JOBS);
1065 ippSetRequestId(request, 3);
1067 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1068 "attributes-charset", NULL, "utf-8");
1070 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1071 "attributes-natural-language", NULL, language->language);
1073 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1074 "printer-uri", NULL, uri);
1076 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1077 "requested-attributes",
1078 (sizeof(jattrs) / sizeof(jattrs[0])),
1079 NULL, jattrs);
1082 * Do the request and get back a response...
1085 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", printername);
1087 if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
1088 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1089 ippErrorString(cupsLastError())));
1090 goto out;
1093 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1094 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1095 ippErrorString(ippGetStatusCode(response))));
1096 goto out;
1100 * Process the jobs...
1103 qcount = 0;
1104 qalloc = 0;
1105 queue = NULL;
1107 for (attr = ippFirstAttribute(response); attr != NULL; attr = ippNextAttribute(response)) {
1109 * Skip leading attributes until we hit a job...
1112 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_JOB)
1113 attr = ippNextAttribute(response);
1115 if (attr == NULL)
1116 break;
1119 * Allocate memory as needed...
1121 if (qcount >= qalloc) {
1122 qalloc += 16;
1124 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1126 if (queue == NULL) {
1127 DEBUG(0,("iprint_queue_get: Not enough memory!"));
1128 qcount = 0;
1129 goto out;
1133 temp = queue + qcount;
1134 memset(temp, 0, sizeof(print_queue_struct));
1137 * Pull the needed attributes from this job...
1140 job_id = 0;
1141 job_priority = 50;
1142 job_status = IPP_JOB_PENDING;
1143 job_time = 0;
1144 job_k_octets = 0;
1145 user_name = NULL;
1146 job_name = NULL;
1148 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_JOB) {
1149 if (ippGetName(attr) == NULL) {
1150 attr = ippNextAttribute(response);
1151 break;
1154 if (strcmp(ippGetName(attr), "job-id") == 0 &&
1155 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1156 job_id = ippGetInteger(attr, 0);
1158 if (strcmp(ippGetName(attr), "job-k-octets") == 0 &&
1159 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1160 job_k_octets = ippGetInteger(attr, 0);
1162 if (strcmp(ippGetName(attr), "job-priority") == 0 &&
1163 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1164 job_priority = ippGetInteger(attr, 0);
1166 if (strcmp(ippGetName(attr), "job-state") == 0 &&
1167 ippGetValueTag(attr) == IPP_TAG_ENUM)
1168 job_status = (ipp_jstate_t)ippGetInteger(attr, 0);
1170 if (strcmp(ippGetName(attr), "time-at-creation") == 0 &&
1171 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1174 * If jobs times are in Unix time, the accuracy of the job
1175 * start time depends upon the iPrint server's time being
1176 * set correctly. Otherwise, the accuracy depends upon
1177 * the Samba server's time being set correctly
1180 if (jobUseUnixTime)
1181 job_time = ippGetInteger(attr, 0);
1182 else
1183 job_time = time(NULL) - printer_up_time + ippGetInteger(attr, 0);
1186 if (strcmp(ippGetName(attr), "job-name") == 0 &&
1187 (ippGetValueTag(attr) == IPP_TAG_NAMELANG ||
1188 ippGetValueTag(attr) == IPP_TAG_NAME))
1189 job_name = ippGetString(attr, 0, NULL);
1191 if (strcmp(ippGetName(attr), "job-originating-user-name") == 0 &&
1192 (ippGetValueTag(attr) == IPP_TAG_NAMELANG ||
1193 ippGetValueTag(attr) == IPP_TAG_NAME))
1194 user_name = ippGetString(attr, 0, NULL);
1196 attr = ippNextAttribute(response);
1200 * See if we have everything needed...
1203 if (user_name == NULL || job_name == NULL || job_id == 0) {
1204 if (attr == NULL)
1205 break;
1206 else
1207 continue;
1210 temp->sysjob = job_id;
1211 temp->size = job_k_octets * 1024;
1212 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1213 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1214 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1215 LPQ_PRINTING;
1216 temp->priority = job_priority;
1217 temp->time = job_time;
1218 strncpy(temp->fs_user, user_name, sizeof(temp->fs_user) - 1);
1219 strncpy(temp->fs_file, job_name, sizeof(temp->fs_file) - 1);
1221 qcount ++;
1223 if (attr == NULL)
1224 break;
1228 * Return the job queue...
1231 *q = queue;
1233 out:
1234 if (response)
1235 ippDelete(response);
1237 if (language)
1238 cupsLangFree(language);
1240 if (http)
1241 httpClose(http);
1243 return qcount;
1248 * 'iprint_queue_pause()' - Pause a print queue.
1251 static int iprint_queue_pause(int snum)
1253 return(-1); /* Not supported without credentials */
1258 * 'iprint_queue_resume()' - Restart a print queue.
1261 static int iprint_queue_resume(int snum)
1263 return(-1); /* Not supported without credentials */
1266 /*******************************************************************
1267 * iPrint printing interface definitions...
1268 ******************************************************************/
1270 struct printif iprint_printif =
1272 PRINT_IPRINT,
1273 iprint_queue_get,
1274 iprint_queue_pause,
1275 iprint_queue_resume,
1276 iprint_job_delete,
1277 iprint_job_pause,
1278 iprint_job_resume,
1279 iprint_job_submit,
1282 #else
1283 /* this keeps fussy compilers happy */
1284 void print_iprint_dummy(void);
1285 void print_iprint_dummy(void) {}
1286 #endif /* HAVE_IPRINT */