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/>.
24 #include "printing/pcap.h"
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
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
)
56 return (ipp
->current
= ipp
->attrs
);
59 static ipp_attribute_t
*
60 ippNextAttribute(ipp_t
*ipp
)
62 if (!ipp
|| !ipp
->current
)
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
;
73 static int ippSetRequestId(ipp_t
*ipp
, int request_id
)
75 ipp
->request
.any
.request_id
= request_id
;
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...
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",
104 DEBUG(10, ("iprint server left to default %s\n", 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:
133 * attributes-natural-language
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
))
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))
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
)
188 if (strcmp(os
,NOVELL_SERVER_SYSNAME_NETWARE
))
189 osFlag
= 1; /* 1 for non-NetWare systems */
198 cupsLangFree(language
);
203 return serverVersion
;
207 static int iprint_cache_add_printer(http_t
*http
,
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 char *name
, /* printer-name attribute */
217 *info
, /* printer-info attribute */
218 smb_enabled
, /* smb-enabled attribute */
219 secure
; /* security-enabled attrib. */
221 char *httpPath
; /* path portion of the printer-uri */
223 static const char *pattrs
[] = /* Requested printer attributes */
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])),
252 * Do the request and get back a response...
255 if ((httpPath
= strstr(url
,"://")) == NULL
||
256 (httpPath
= strchr(httpPath
+3,'/')) == NULL
)
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
)
274 DEBUG(0,("Unable to get printer list - %s\n",
275 ippErrorString(lastErr
)));
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
);
291 * Pull the needed attributes from this printer...
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))))
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))))
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
);
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 */
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
)));
385 * Build a OPERATION_NOVELL_LIST_PRINTERS request, which requires the following attributes:
388 * attributes-natural-language
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())));
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
);
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 char *url
= ippGetString(attr
, i
, NULL
);
444 if (!url
|| !strlen(url
))
446 iprint_cache_add_printer(http
, i
+2, url
,
450 attr
= ippNextAttribute(response
);
462 cupsLangFree(language
);
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
)));
505 * Build an IPP_CANCEL_JOB request, which uses the following
509 * attributes-natural-language
512 * requesting-user-name
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",
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())));
551 DEBUG(0,("Unable to cancel job %d - %s\n", pjob
->sysjob
,
552 ippErrorString(cupsLastError())));
560 cupsLangFree(language
);
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
)));
603 * Build an IPP_HOLD_JOB request, which requires the following
607 * attributes-natural-language
610 * requesting-user-name
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",
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())));
651 DEBUG(0,("Unable to hold job %d - %s\n", pjob
->sysjob
,
652 ippErrorString(cupsLastError())));
660 cupsLangFree(language
);
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
)));
703 * Build an IPP_RELEASE_JOB request, which requires the following
707 * attributes-natural-language
710 * requesting-user-name
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",
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())));
751 DEBUG(0,("Unable to release job %d - %s\n", pjob
->sysjob
,
752 ippErrorString(cupsLastError())));
760 cupsLangFree(language
);
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
,
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
)));
804 * Build an IPP_PRINT_JOB request, which requires the following
808 * attributes-natural-language
810 * requesting-user-name
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",
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
,
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())));
858 DEBUG(0,("Unable to print file to `%s' - %s\n",
859 lp_printername(talloc_tos(), snum
),
860 ippErrorString(cupsLastError())));
864 unlink(pjob
->filename
);
865 /* else print_job_end will do it for us */
869 attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
);
870 if (attr
!= NULL
&& ippGetGroupTag(attr
) == IPP_TAG_JOB
)
872 pjob
->sysjob
= ippGetInteger(attr
, 0);
881 cupsLangFree(language
);
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
,
896 print_queue_struct
**q
,
897 print_status_struct
*status
)
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 */
927 "job-originating-user-name",
932 static const char *pattrs
[] = /* Requested printer attributes */
935 "printer-state-message",
936 "printer-current-time",
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
)));
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
)
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])),
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())));
1023 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1024 DEBUG(0,("Unable to get printer status for %s - %s\n", printername
,
1025 ippErrorString(ippGetStatusCode(response
))));
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
;
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
);
1053 * Build an IPP_GET_JOBS request, which requires the following
1056 * attributes-charset
1057 * attributes-natural-language
1058 * requested-attributes
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])),
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())));
1093 if (ippGetStatusCode(response
) >= IPP_OK_CONFLICT
) {
1094 DEBUG(0,("Unable to get jobs for %s - %s\n", uri
,
1095 ippErrorString(ippGetStatusCode(response
))));
1100 * Process the jobs...
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
);
1119 * Allocate memory as needed...
1121 if (qcount
>= qalloc
) {
1124 queue
= SMB_REALLOC_ARRAY(queue
, print_queue_struct
, qalloc
);
1126 if (queue
== NULL
) {
1127 DEBUG(0,("iprint_queue_get: Not enough memory!"));
1133 temp
= queue
+ qcount
;
1134 memset(temp
, 0, sizeof(print_queue_struct
));
1137 * Pull the needed attributes from this job...
1142 job_status
= IPP_JOB_PENDING
;
1148 while (attr
!= NULL
&& ippGetGroupTag(attr
) == IPP_TAG_JOB
) {
1149 if (ippGetName(attr
) == NULL
) {
1150 attr
= ippNextAttribute(response
);
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
1181 job_time
= ippGetInteger(attr
, 0);
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) {
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
:
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);
1228 * Return the job queue...
1235 ippDelete(response
);
1238 cupsLangFree(language
);
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
=
1275 iprint_queue_resume
,
1283 /* this keeps fussy compilers happy */
1284 void print_iprint_dummy(void);
1285 void print_iprint_dummy(void) {}
1286 #endif /* HAVE_IPRINT */