pdb: Fix array overrun by one. Reviewed-by: Alexander Bokovoy <ab@samba.org>
[Samba.git] / source3 / printing / print_iprint.c
blob529f0dd0d7c007ff213fcc43357824a4c8941300
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 if ((lp_iprint_server() != NULL) && (strlen(lp_iprint_server()) > 0)) {
97 DEBUG(10, ("iprint server explicitly set to %s\n",
98 lp_iprint_server()));
99 return lp_iprint_server();
102 DEBUG(10, ("iprint server left to default %s\n", cupsServer()));
103 return cupsServer();
107 * Pass in an already connected http_t*
108 * Returns the server version if one can be found, multiplied by
109 * -1 for all NetWare versions. Returns 0 if a server version
110 * cannot be determined
113 static int iprint_get_server_version(http_t *http, char* serviceUri)
115 ipp_t *request = NULL, /* IPP Request */
116 *response = NULL; /* IPP Response */
117 ipp_attribute_t *attr; /* Current attribute */
118 cups_lang_t *language = NULL; /* Default language */
119 char *ver; /* server version pointer */
120 char *vertmp; /* server version tmp pointer */
121 int serverVersion = 0; /* server version */
122 char *os; /* server os */
123 int osFlag = 0; /* 0 for NetWare, 1 for anything else */
124 char *temp; /* pointer for string manipulation */
127 * Build an OPERATION_NOVELL_MGMT("get-server-version") request,
128 * which requires the following attributes:
130 * attributes-charset
131 * attributes-natural-language
132 * operation-name
133 * service-uri
136 request = ippNew();
138 ippSetOperation(request, (ipp_op_t)OPERATION_NOVELL_MGMT);
139 ippSetRequestId(request, 1);
141 language = cupsLangDefault();
143 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
144 "attributes-charset", NULL, "utf-8");
146 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
147 "attributes-natural-language", NULL, language->language);
149 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
150 "service-uri", NULL, serviceUri);
152 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
153 "operation-name", NULL, "get-server-version");
156 * Do the request and get back a response...
159 if (((response = cupsDoRequest(http, request, "/ipp/")) == NULL) ||
160 (ippGetStatusCode(response) >= IPP_OK_CONFLICT))
161 goto out;
163 if (((attr = ippFindAttribute(response, "server-version",
164 IPP_TAG_STRING)) != NULL)) {
165 if ((ver = strstr(ippGetString(attr, 0, NULL),
166 NOVELL_SERVER_VERSION_STRING)) != NULL) {
167 ver += strlen(NOVELL_SERVER_VERSION_STRING);
169 * Strangely, libcups stores a IPP_TAG_STRING (octet
170 * string) as a null-terminated string with no length
171 * even though it could be binary data with nulls in
172 * it. Luckily, in this case the value is not binary.
174 serverVersion = strtol(ver, &vertmp, 10);
176 /* Check for not found, overflow or negative version */
177 if ((ver == vertmp) || (serverVersion < 0))
178 serverVersion = 0;
181 if ((os = strstr(ippGetString(attr, 0, NULL),
182 NOVELL_SERVER_SYSNAME)) != NULL) {
183 os += strlen(NOVELL_SERVER_SYSNAME);
184 if ((temp = strchr(os,'<')) != NULL)
185 *temp = '\0';
186 if (strcmp(os,NOVELL_SERVER_SYSNAME_NETWARE))
187 osFlag = 1; /* 1 for non-NetWare systems */
191 out:
192 if (response)
193 ippDelete(response);
195 if (language)
196 cupsLangFree(language);
198 if (osFlag == 0)
199 serverVersion *= -1;
201 return serverVersion;
205 static int iprint_cache_add_printer(http_t *http,
206 int reqId,
207 char* url)
209 ipp_t *request = NULL, /* IPP Request */
210 *response = NULL; /* IPP Response */
211 ipp_attribute_t *attr; /* Current attribute */
212 cups_lang_t *language = NULL; /* Default language */
213 char *name, /* printer-name attribute */
214 *info, /* printer-info attribute */
215 smb_enabled, /* smb-enabled attribute */
216 secure; /* security-enabled attrib. */
218 char *httpPath; /* path portion of the printer-uri */
220 static const char *pattrs[] = /* Requested printer attributes */
222 "printer-name",
223 "security-enabled",
224 "printer-info",
225 "smb-enabled"
228 request = ippNew();
230 ippSetOperation(request, IPP_GET_PRINTER_ATTRIBUTES);
231 ippSetRequestId(request, reqId);
233 language = cupsLangDefault();
235 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
236 "attributes-charset", NULL, "utf-8");
238 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
239 "attributes-natural-language", NULL, language->language);
241 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, url);
243 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
244 "requested-attributes",
245 (sizeof(pattrs) / sizeof(pattrs[0])),
246 NULL, pattrs);
249 * Do the request and get back a response...
252 if ((httpPath = strstr(url,"://")) == NULL ||
253 (httpPath = strchr(httpPath+3,'/')) == NULL)
255 ippDelete(request);
256 request = NULL;
257 goto out;
260 if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
261 ipp_status_t lastErr = cupsLastError();
264 * Ignore printers that cannot be queried without credentials
266 if (lastErr == IPP_FORBIDDEN ||
267 lastErr == IPP_NOT_AUTHENTICATED ||
268 lastErr == IPP_NOT_AUTHORIZED)
269 goto out;
271 DEBUG(0,("Unable to get printer list - %s\n",
272 ippErrorString(lastErr)));
273 goto out;
276 for (attr = ippFirstAttribute(response); attr != NULL;) {
278 * Skip leading attributes until we hit a printer...
281 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER)
282 attr = ippNextAttribute(response);
284 if (attr == NULL)
285 break;
288 * Pull the needed attributes from this printer...
291 name = NULL;
292 info = NULL;
293 smb_enabled= 1;
294 secure = 0;
296 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_PRINTER) {
297 if (strcmp(ippGetName(attr), "printer-name") == 0 &&
298 ippGetValueTag(attr) == IPP_TAG_NAME)
299 name = ippGetString(attr, 0, NULL);
301 if (strcmp(ippGetName(attr), "printer-info") == 0 &&
302 (ippGetValueTag(attr) == IPP_TAG_TEXT ||
303 ippGetValueTag(attr) == IPP_TAG_TEXTLANG))
304 info = ippGetString(attr, 0, NULL);
307 * If the smb-enabled attribute is present and the
308 * value is set to 0, don't show the printer.
309 * If the attribute is not present, assume that the
310 * printer should show up
312 if (!strcmp(ippGetName(attr), "smb-enabled") &&
313 ((ippGetValueTag(attr) == IPP_TAG_INTEGER &&
314 !ippGetInteger(attr, 0)) ||
315 (ippGetValueTag(attr) == IPP_TAG_BOOLEAN &&
316 !ippGetBoolean(attr, 0))))
317 smb_enabled = 0;
320 * If the security-enabled attribute is present and the
321 * value is set to 1, don't show the printer.
322 * If the attribute is not present, assume that the
323 * printer should show up
325 if (!strcmp(ippGetName(attr), "security-enabled") &&
326 ((ippGetValueTag(attr) == IPP_TAG_INTEGER &&
327 ippGetInteger(attr, 0)) ||
328 (ippGetValueTag(attr) == IPP_TAG_BOOLEAN &&
329 ippGetBoolean(attr, 0))))
330 secure = 1;
332 attr = ippNextAttribute(response);
336 * See if we have everything needed...
337 * Make sure the printer is not a secure printer
338 * and make sure smb printing hasn't been explicitly
339 * disabled for the printer
342 if (name != NULL && !secure && smb_enabled)
343 pcap_cache_add(name, info, NULL);
346 out:
347 if (response)
348 ippDelete(response);
349 return(0);
352 bool iprint_cache_reload(void)
354 http_t *http = NULL; /* HTTP connection to server */
355 ipp_t *request = NULL, /* IPP Request */
356 *response = NULL; /* IPP Response */
357 ipp_attribute_t *attr; /* Current attribute */
358 cups_lang_t *language = NULL; /* Default language */
359 int i;
360 bool ret = False;
362 DEBUG(5, ("reloading iprint printcap cache\n"));
365 * Make sure we don't ask for passwords...
368 cupsSetPasswordCB(iprint_passwd_cb);
371 * Try to connect to the server...
374 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
375 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
376 iprint_server(), strerror(errno)));
377 goto out;
381 * Build a OPERATION_NOVELL_LIST_PRINTERS request, which requires the following attributes:
383 * attributes-charset
384 * attributes-natural-language
387 request = ippNew();
389 ippSetOperation(request, (ipp_op_t)OPERATION_NOVELL_LIST_PRINTERS);
390 ippSetRequestId(request, 1);
392 language = cupsLangDefault();
394 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
395 "attributes-charset", NULL, "utf-8");
397 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
398 "attributes-natural-language", NULL, language->language);
400 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
401 "ipp-server", NULL, "ippSrvr");
404 * Do the request and get back a response...
407 if ((response = cupsDoRequest(http, request, "/ipp")) == NULL) {
408 DEBUG(0,("Unable to get printer list - %s\n",
409 ippErrorString(cupsLastError())));
410 goto out;
413 for (attr = ippFirstAttribute(response); attr != NULL;) {
415 * Skip leading attributes until we hit a printer...
418 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER)
419 attr = ippNextAttribute(response);
421 if (attr == NULL)
422 break;
425 * Pull the needed attributes from this printer...
428 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_PRINTER)
430 if (strcmp(ippGetName(attr), "printer-name") == 0 &&
431 (ippGetValueTag(attr) == IPP_TAG_URI ||
432 ippGetValueTag(attr) == IPP_TAG_NAME ||
433 ippGetValueTag(attr) == IPP_TAG_TEXT ||
434 ippGetValueTag(attr) == IPP_TAG_NAMELANG ||
435 ippGetValueTag(attr) == IPP_TAG_TEXTLANG))
437 for (i = 0; i<ippGetCount(attr); i++)
439 char *url = ippGetString(attr, i, NULL);
440 if (!url || !strlen(url))
441 continue;
442 iprint_cache_add_printer(http, i+2, url);
445 attr = ippNextAttribute(response);
449 ret = True;
451 out:
452 if (response)
453 ippDelete(response);
455 if (language)
456 cupsLangFree(language);
458 if (http)
459 httpClose(http);
461 return ret;
466 * 'iprint_job_delete()' - Delete a job.
469 static int iprint_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
471 int ret = 1; /* Return value */
472 http_t *http = NULL; /* HTTP connection to server */
473 ipp_t *request = NULL, /* IPP Request */
474 *response = NULL; /* IPP Response */
475 cups_lang_t *language = NULL; /* Default language */
476 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
477 char httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
480 DEBUG(5,("iprint_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
483 * Make sure we don't ask for passwords...
486 cupsSetPasswordCB(iprint_passwd_cb);
489 * Try to connect to the server...
492 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
493 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
494 iprint_server(), strerror(errno)));
495 goto out;
499 * Build an IPP_CANCEL_JOB request, which uses the following
500 * attributes:
502 * attributes-charset
503 * attributes-natural-language
504 * printer-uri
505 * job-id
506 * requesting-user-name
509 request = ippNew();
511 ippSetOperation(request, IPP_CANCEL_JOB);
512 ippSetRequestId(request, 1);
514 language = cupsLangDefault();
516 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
517 "attributes-charset", NULL, "utf-8");
519 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
520 "attributes-natural-language", NULL, language->language);
522 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), sharename);
524 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
526 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
528 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
529 NULL, pjob->user);
532 * Do the request and get back a response...
535 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", sharename);
537 if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
538 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
539 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
540 ippErrorString(cupsLastError())));
541 } else {
542 ret = 0;
544 } else {
545 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
546 ippErrorString(cupsLastError())));
549 out:
550 if (response)
551 ippDelete(response);
553 if (language)
554 cupsLangFree(language);
556 if (http)
557 httpClose(http);
559 return ret;
564 * 'iprint_job_pause()' - Pause a job.
567 static int iprint_job_pause(int snum, struct printjob *pjob)
569 int ret = 1; /* Return value */
570 http_t *http = NULL; /* HTTP connection to server */
571 ipp_t *request = NULL, /* IPP Request */
572 *response = NULL; /* IPP Response */
573 cups_lang_t *language = NULL; /* Default language */
574 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
575 char httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
578 DEBUG(5,("iprint_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
581 * Make sure we don't ask for passwords...
584 cupsSetPasswordCB(iprint_passwd_cb);
587 * Try to connect to the server...
590 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
591 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
592 iprint_server(), strerror(errno)));
593 goto out;
597 * Build an IPP_HOLD_JOB request, which requires the following
598 * attributes:
600 * attributes-charset
601 * attributes-natural-language
602 * printer-uri
603 * job-id
604 * requesting-user-name
607 request = ippNew();
609 ippSetOperation(request, IPP_HOLD_JOB);
610 ippSetRequestId(request, 1);
612 language = cupsLangDefault();
614 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
615 "attributes-charset", NULL, "utf-8");
617 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
618 "attributes-natural-language", NULL, language->language);
620 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(),
621 lp_printername(snum));
623 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
625 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
627 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
628 NULL, pjob->user);
631 * Do the request and get back a response...
634 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s",
635 lp_printername(snum));
637 if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
638 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
639 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
640 ippErrorString(cupsLastError())));
641 } else {
642 ret = 0;
644 } else {
645 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
646 ippErrorString(cupsLastError())));
649 out:
650 if (response)
651 ippDelete(response);
653 if (language)
654 cupsLangFree(language);
656 if (http)
657 httpClose(http);
659 return ret;
664 * 'iprint_job_resume()' - Resume a paused job.
667 static int iprint_job_resume(int snum, struct printjob *pjob)
669 int ret = 1; /* Return value */
670 http_t *http = NULL; /* HTTP connection to server */
671 ipp_t *request = NULL, /* IPP Request */
672 *response = NULL; /* IPP Response */
673 cups_lang_t *language = NULL; /* Default language */
674 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
675 char httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
678 DEBUG(5,("iprint_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
681 * Make sure we don't ask for passwords...
684 cupsSetPasswordCB(iprint_passwd_cb);
687 * Try to connect to the server...
690 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
691 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
692 iprint_server(), strerror(errno)));
693 goto out;
697 * Build an IPP_RELEASE_JOB request, which requires the following
698 * attributes:
700 * attributes-charset
701 * attributes-natural-language
702 * printer-uri
703 * job-id
704 * requesting-user-name
707 request = ippNew();
709 ippSetOperation(request, IPP_RELEASE_JOB);
710 ippSetRequestId(request, 1);
712 language = cupsLangDefault();
714 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
715 "attributes-charset", NULL, "utf-8");
717 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
718 "attributes-natural-language", NULL, language->language);
720 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(),
721 lp_printername(snum));
723 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
725 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
727 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
728 NULL, pjob->user);
731 * Do the request and get back a response...
734 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s",
735 lp_printername(snum));
737 if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
738 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
739 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
740 ippErrorString(cupsLastError())));
741 } else {
742 ret = 0;
744 } else {
745 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
746 ippErrorString(cupsLastError())));
749 out:
750 if (response)
751 ippDelete(response);
753 if (language)
754 cupsLangFree(language);
756 if (http)
757 httpClose(http);
759 return ret;
764 * 'iprint_job_submit()' - Submit a job for printing.
767 static int iprint_job_submit(int snum, struct printjob *pjob,
768 enum printing_types printing_type,
769 char *lpq_cmd)
771 int ret = 1; /* Return value */
772 http_t *http = NULL; /* HTTP connection to server */
773 ipp_t *request = NULL, /* IPP Request */
774 *response = NULL; /* IPP Response */
775 ipp_attribute_t *attr; /* Current attribute */
776 cups_lang_t *language = NULL; /* Default language */
777 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
779 DEBUG(5,("iprint_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
782 * Make sure we don't ask for passwords...
785 cupsSetPasswordCB(iprint_passwd_cb);
788 * Try to connect to the server...
791 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
792 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
793 iprint_server(), strerror(errno)));
794 goto out;
798 * Build an IPP_PRINT_JOB request, which requires the following
799 * attributes:
801 * attributes-charset
802 * attributes-natural-language
803 * printer-uri
804 * requesting-user-name
805 * [document-data]
808 request = ippNew();
810 ippSetOperation(request, IPP_PRINT_JOB);
811 ippSetRequestId(request, 1);
813 language = cupsLangDefault();
815 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
816 "attributes-charset", NULL, "utf-8");
818 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
819 "attributes-natural-language", NULL, language->language);
821 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(),
822 lp_printername(snum));
824 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
825 "printer-uri", NULL, uri);
827 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
828 NULL, pjob->user);
830 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
831 "job-originating-host-name", NULL,
832 pjob->clientmachine);
834 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
835 pjob->jobname);
838 * Do the request and get back a response...
841 slprintf(uri, sizeof(uri) - 1, "/ipp/%s", lp_printername(snum));
843 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
844 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
845 DEBUG(0,("Unable to print file to %s - %s\n",
846 lp_printername(snum),
847 ippErrorString(cupsLastError())));
848 } else {
849 ret = 0;
851 } else {
852 DEBUG(0,("Unable to print file to `%s' - %s\n",
853 lp_printername(snum),
854 ippErrorString(cupsLastError())));
857 if ( ret == 0 )
858 unlink(pjob->filename);
859 /* else print_job_end will do it for us */
861 if ( ret == 0 ) {
863 attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
864 if (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_JOB)
866 pjob->sysjob = ippGetInteger(attr, 0);
870 out:
871 if (response)
872 ippDelete(response);
874 if (language)
875 cupsLangFree(language);
877 if (http)
878 httpClose(http);
880 return ret;
884 * 'iprint_queue_get()' - Get all the jobs in the print queue.
887 static int iprint_queue_get(const char *sharename,
888 enum printing_types printing_type,
889 char *lpq_command,
890 print_queue_struct **q,
891 print_status_struct *status)
893 fstring printername;
894 http_t *http = NULL; /* HTTP connection to server */
895 ipp_t *request = NULL, /* IPP Request */
896 *response = NULL; /* IPP Response */
897 ipp_attribute_t *attr = NULL; /* Current attribute */
898 cups_lang_t *language = NULL; /* Default language */
899 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
900 char serviceUri[HTTP_MAX_URI]; /* service-uri attribute */
901 char httpPath[HTTP_MAX_URI]; /* path portion of the uri */
902 int jobUseUnixTime = 0; /* Whether job times should
903 * be assumed to be Unix time */
904 int qcount = 0, /* Number of active queue entries */
905 qalloc = 0; /* Number of queue entries allocated */
906 print_queue_struct *queue = NULL, /* Queue entries */
907 *temp; /* Temporary pointer for queue */
908 const char *user_name, /* job-originating-user-name attribute */
909 *job_name; /* job-name attribute */
910 int job_id; /* job-id attribute */
911 int job_k_octets; /* job-k-octets attribute */
912 time_t job_time; /* time-at-creation attribute */
913 time_t printer_up_time = 0; /* printer's uptime */
914 ipp_jstate_t job_status; /* job-status attribute */
915 int job_priority; /* job-priority attribute */
916 static const char *jattrs[] = /* Requested job attributes */
918 "job-id",
919 "job-k-octets",
920 "job-name",
921 "job-originating-user-name",
922 "job-priority",
923 "job-state",
924 "time-at-creation",
926 static const char *pattrs[] = /* Requested printer attributes */
928 "printer-state",
929 "printer-state-message",
930 "printer-current-time",
931 "printer-up-time"
934 *q = NULL;
936 /* HACK ALERT!!! The porblem with support the 'printer name'
937 option is that we key the tdb off the sharename. So we will
938 overload the lpq_command string to pass in the printername
939 (which is basically what we do for non-cups printers ... using
940 the lpq_command to get the queue listing). */
942 fstrcpy( printername, lpq_command );
944 DEBUG(5,("iprint_queue_get(%s, %p, %p)\n", printername, q, status));
947 * Make sure we don't ask for passwords...
950 cupsSetPasswordCB(iprint_passwd_cb);
953 * Try to connect to the server...
956 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
957 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
958 iprint_server(), strerror(errno)));
959 goto out;
963 * Generate the printer URI and the service URI that goes with it...
966 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), printername);
967 slprintf(serviceUri, sizeof(serviceUri) - 1, "ipp://%s/ipp/", iprint_server());
970 * For Linux iPrint servers from OES SP1 on, the iPrint server
971 * uses Unix time for job start times unless it detects the iPrint
972 * client in an http User-Agent header. (This was done to accomodate
973 * CUPS broken behavior. According to RFC 2911, section 4.3.14, job
974 * start times are supposed to be relative to how long the printer has
975 * been up.) Since libcups doesn't allow us to set that header before
976 * the request is sent, this ugly hack allows us to detect the server
977 * version and decide how to interpret the job time.
979 if (iprint_get_server_version(http, serviceUri) >=
980 NOVELL_SERVER_VERSION_OES_SP1)
981 jobUseUnixTime = 1;
983 request = ippNew();
985 ippSetOperation(request, IPP_GET_PRINTER_ATTRIBUTES);
986 ippSetRequestId(request, 2);
988 language = cupsLangDefault();
990 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
991 "attributes-charset", NULL, "utf-8");
993 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
994 "attributes-natural-language", NULL, language->language);
996 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
997 "printer-uri", NULL, uri);
999 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1000 "requested-attributes",
1001 (sizeof(pattrs) / sizeof(pattrs[0])),
1002 NULL, pattrs);
1005 * Do the request and get back a response...
1008 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", printername);
1010 if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
1011 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1012 ippErrorString(cupsLastError())));
1013 *q = queue;
1014 goto out;
1017 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1018 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
1019 ippErrorString(ippGetStatusCode(response))));
1020 *q = queue;
1021 goto out;
1025 * Get the current printer status and convert it to the SAMBA values.
1028 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
1029 if (ippGetInteger(attr, 0) == IPP_PRINTER_STOPPED)
1030 status->status = LPSTAT_STOPPED;
1031 else
1032 status->status = LPSTAT_OK;
1035 if ((attr = ippFindAttribute(response, "printer-state-message",
1036 IPP_TAG_TEXT)) != NULL)
1037 fstrcpy(status->message, ippGetString(attr, 0, NULL));
1039 if ((attr = ippFindAttribute(response, "printer-up-time",
1040 IPP_TAG_INTEGER)) != NULL)
1041 printer_up_time = ippGetInteger(attr, 0);
1043 ippDelete(response);
1044 response = NULL;
1047 * Build an IPP_GET_JOBS request, which requires the following
1048 * attributes:
1050 * attributes-charset
1051 * attributes-natural-language
1052 * requested-attributes
1053 * printer-uri
1056 request = ippNew();
1058 ippSetOperation(request, IPP_GET_JOBS);
1059 ippSetRequestId(request, 3);
1061 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1062 "attributes-charset", NULL, "utf-8");
1064 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1065 "attributes-natural-language", NULL, language->language);
1067 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1068 "printer-uri", NULL, uri);
1070 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1071 "requested-attributes",
1072 (sizeof(jattrs) / sizeof(jattrs[0])),
1073 NULL, jattrs);
1076 * Do the request and get back a response...
1079 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", printername);
1081 if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
1082 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1083 ippErrorString(cupsLastError())));
1084 goto out;
1087 if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
1088 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1089 ippErrorString(ippGetStatusCode(response))));
1090 goto out;
1094 * Process the jobs...
1097 qcount = 0;
1098 qalloc = 0;
1099 queue = NULL;
1101 for (attr = ippFirstAttribute(response); attr != NULL; attr = ippNextAttribute(response)) {
1103 * Skip leading attributes until we hit a job...
1106 while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_JOB)
1107 attr = ippNextAttribute(response);
1109 if (attr == NULL)
1110 break;
1113 * Allocate memory as needed...
1115 if (qcount >= qalloc) {
1116 qalloc += 16;
1118 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1120 if (queue == NULL) {
1121 DEBUG(0,("iprint_queue_get: Not enough memory!"));
1122 qcount = 0;
1123 goto out;
1127 temp = queue + qcount;
1128 memset(temp, 0, sizeof(print_queue_struct));
1131 * Pull the needed attributes from this job...
1134 job_id = 0;
1135 job_priority = 50;
1136 job_status = IPP_JOB_PENDING;
1137 job_time = 0;
1138 job_k_octets = 0;
1139 user_name = NULL;
1140 job_name = NULL;
1142 while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_JOB) {
1143 if (ippGetName(attr) == NULL) {
1144 attr = ippNextAttribute(response);
1145 break;
1148 if (strcmp(ippGetName(attr), "job-id") == 0 &&
1149 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1150 job_id = ippGetInteger(attr, 0);
1152 if (strcmp(ippGetName(attr), "job-k-octets") == 0 &&
1153 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1154 job_k_octets = ippGetInteger(attr, 0);
1156 if (strcmp(ippGetName(attr), "job-priority") == 0 &&
1157 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1158 job_priority = ippGetInteger(attr, 0);
1160 if (strcmp(ippGetName(attr), "job-state") == 0 &&
1161 ippGetValueTag(attr) == IPP_TAG_ENUM)
1162 job_status = (ipp_jstate_t)ippGetInteger(attr, 0);
1164 if (strcmp(ippGetName(attr), "time-at-creation") == 0 &&
1165 ippGetValueTag(attr) == IPP_TAG_INTEGER)
1168 * If jobs times are in Unix time, the accuracy of the job
1169 * start time depends upon the iPrint server's time being
1170 * set correctly. Otherwise, the accuracy depends upon
1171 * the Samba server's time being set correctly
1174 if (jobUseUnixTime)
1175 job_time = ippGetInteger(attr, 0);
1176 else
1177 job_time = time(NULL) - printer_up_time + ippGetInteger(attr, 0);
1180 if (strcmp(ippGetName(attr), "job-name") == 0 &&
1181 (ippGetValueTag(attr) == IPP_TAG_NAMELANG ||
1182 ippGetValueTag(attr) == IPP_TAG_NAME))
1183 job_name = ippGetString(attr, 0, NULL);
1185 if (strcmp(ippGetName(attr), "job-originating-user-name") == 0 &&
1186 (ippGetValueTag(attr) == IPP_TAG_NAMELANG ||
1187 ippGetValueTag(attr) == IPP_TAG_NAME))
1188 user_name = ippGetString(attr, 0, NULL);
1190 attr = ippNextAttribute(response);
1194 * See if we have everything needed...
1197 if (user_name == NULL || job_name == NULL || job_id == 0) {
1198 if (attr == NULL)
1199 break;
1200 else
1201 continue;
1204 temp->sysjob = job_id;
1205 temp->size = job_k_octets * 1024;
1206 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1207 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1208 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1209 LPQ_PRINTING;
1210 temp->priority = job_priority;
1211 temp->time = job_time;
1212 strncpy(temp->fs_user, user_name, sizeof(temp->fs_user) - 1);
1213 strncpy(temp->fs_file, job_name, sizeof(temp->fs_file) - 1);
1215 qcount ++;
1217 if (attr == NULL)
1218 break;
1222 * Return the job queue...
1225 *q = queue;
1227 out:
1228 if (response)
1229 ippDelete(response);
1231 if (language)
1232 cupsLangFree(language);
1234 if (http)
1235 httpClose(http);
1237 return qcount;
1242 * 'iprint_queue_pause()' - Pause a print queue.
1245 static int iprint_queue_pause(int snum)
1247 return(-1); /* Not supported without credentials */
1252 * 'iprint_queue_resume()' - Restart a print queue.
1255 static int iprint_queue_resume(int snum)
1257 return(-1); /* Not supported without credentials */
1260 /*******************************************************************
1261 * iPrint printing interface definitions...
1262 ******************************************************************/
1264 struct printif iprint_printif =
1266 PRINT_IPRINT,
1267 iprint_queue_get,
1268 iprint_queue_pause,
1269 iprint_queue_resume,
1270 iprint_job_delete,
1271 iprint_job_pause,
1272 iprint_job_resume,
1273 iprint_job_submit,
1276 #else
1277 /* this keeps fussy compilers happy */
1278 void print_iprint_dummy(void);
1279 void print_iprint_dummy(void) {}
1280 #endif /* HAVE_IPRINT */