r14190: Coverity bug #33. Missing initialization of pipe_hnd.
[Samba/nascimento.git] / source3 / printing / print_iprint.c
blob04b096a8a5b3bc6244bf7123737a2492e8e11868
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 2 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, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "printing.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
38 * 'iprint_passwd_cb()' - The iPrint password callback...
41 static const char * /* O - Password or NULL */
42 iprint_passwd_cb(const char *prompt) /* I - Prompt */
45 * Always return NULL to indicate that no password is available...
48 return (NULL);
51 static const char *iprint_server(void)
53 if ((lp_iprint_server() != NULL) && (strlen(lp_iprint_server()) > 0)) {
54 DEBUG(10, ("iprint server explicitly set to %s\n",
55 lp_iprint_server()));
56 return lp_iprint_server();
59 DEBUG(10, ("iprint server left to default %s\n", cupsServer()));
60 return cupsServer();
64 * Pass in an already connected http_t*
65 * Returns the server version if one can be found, multiplied by
66 * -1 for all NetWare versions. Returns 0 if a server version
67 * cannot be determined
70 static int iprint_get_server_version(http_t *http, char* serviceUri)
72 ipp_t *request = NULL, /* IPP Request */
73 *response = NULL; /* IPP Response */
74 ipp_attribute_t *attr; /* Current attribute */
75 cups_lang_t *language = NULL; /* Default language */
76 char *ver; /* server version pointer */
77 char *vertmp; /* server version tmp pointer */
78 int serverVersion = 0; /* server version */
79 char *os; /* server os */
80 int osFlag = 0; /* 0 for NetWare, 1 for anything else */
81 char *temp; /* pointer for string manipulation */
84 * Build an OPERATION_NOVELL_MGMT("get-server-version") request,
85 * which requires the following attributes:
87 * attributes-charset
88 * attributes-natural-language
89 * operation-name
90 * service-uri
93 request = ippNew();
95 request->request.op.operation_id = OPERATION_NOVELL_MGMT;
96 request->request.op.request_id = 1;
98 language = cupsLangDefault();
100 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
101 "attributes-charset", NULL, "utf-8");
103 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
104 "attributes-natural-language", NULL, language->language);
106 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
107 "service-uri", NULL, serviceUri);
109 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
110 "operation-name", NULL, "get-server-version");
113 * Do the request and get back a response...
116 if (((response = cupsDoRequest(http, request, "/ipp/")) == NULL) ||
117 (response->request.status.status_code >= IPP_OK_CONFLICT))
118 goto out;
120 if (((attr = ippFindAttribute(response, "server-version",
121 IPP_TAG_STRING)) != NULL)) {
122 if ((ver = strstr(attr->values[0].string.text,
123 NOVELL_SERVER_VERSION_STRING)) != NULL) {
124 ver += strlen(NOVELL_SERVER_VERSION_STRING);
126 * Strangely, libcups stores a IPP_TAG_STRING (octet
127 * string) as a null-terminated string with no length
128 * even though it could be binary data with nulls in
129 * it. Luckily, in this case the value is not binary.
131 serverVersion = strtol(ver, &vertmp, 10);
133 /* Check for not found, overflow or negative version */
134 if ((ver == vertmp) || (serverVersion < 0))
135 serverVersion = 0;
138 if ((os = strstr(attr->values[0].string.text,
139 NOVELL_SERVER_SYSNAME)) != NULL) {
140 os += strlen(NOVELL_SERVER_SYSNAME);
141 if ((temp = strchr(os,'<')) != NULL)
142 *temp = '\0';
143 if (strcmp(os,NOVELL_SERVER_SYSNAME_NETWARE))
144 osFlag = 1; /* 1 for non-NetWare systems */
148 out:
149 if (response)
150 ippDelete(response);
152 if (language)
153 cupsLangFree(language);
155 if (osFlag == 0)
156 serverVersion *= -1;
158 return serverVersion;
162 static int iprint_cache_add_printer(http_t *http,
163 int reqId,
164 char* url)
166 ipp_t *request = NULL, /* IPP Request */
167 *response = NULL; /* IPP Response */
168 ipp_attribute_t *attr; /* Current attribute */
169 cups_lang_t *language = NULL; /* Default language */
170 char *name, /* printer-name attribute */
171 *info, /* printer-info attribute */
172 smb_enabled, /* smb-enabled attribute */
173 secure; /* security-enabled attrib. */
175 char *httpPath; /* path portion of the printer-uri */
177 static const char *pattrs[] = /* Requested printer attributes */
179 "printer-name",
180 "security-enabled",
181 "printer-info",
182 "smb-enabled"
185 request = ippNew();
187 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
188 request->request.op.request_id = reqId;
190 language = cupsLangDefault();
192 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
193 "attributes-charset", NULL, "utf-8");
195 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
196 "attributes-natural-language", NULL, language->language);
198 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, url);
200 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
201 "requested-attributes",
202 (sizeof(pattrs) / sizeof(pattrs[0])),
203 NULL, pattrs);
206 * Do the request and get back a response...
209 if ((httpPath = strstr(url,"://")) == NULL ||
210 (httpPath = strchr(httpPath+3,'/')) == NULL)
212 ippDelete(request);
213 request = NULL;
214 goto out;
217 if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
218 ipp_status_t lastErr = cupsLastError();
221 * Ignore printers that cannot be queried without credentials
223 if (lastErr == IPP_FORBIDDEN ||
224 lastErr == IPP_NOT_AUTHENTICATED ||
225 lastErr == IPP_NOT_AUTHORIZED)
226 goto out;
228 DEBUG(0,("Unable to get printer list - %s\n",
229 ippErrorString(lastErr)));
230 goto out;
233 for (attr = response->attrs; attr != NULL;) {
235 * Skip leading attributes until we hit a printer...
238 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
239 attr = attr->next;
241 if (attr == NULL)
242 break;
245 * Pull the needed attributes from this printer...
248 name = NULL;
249 info = NULL;
250 smb_enabled= 1;
251 secure = 0;
253 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
254 if (strcmp(attr->name, "printer-name") == 0 &&
255 attr->value_tag == IPP_TAG_NAME)
256 name = attr->values[0].string.text;
258 if (strcmp(attr->name, "printer-info") == 0 &&
259 (attr->value_tag == IPP_TAG_TEXT ||
260 attr->value_tag == IPP_TAG_TEXTLANG))
261 info = attr->values[0].string.text;
264 * If the smb-enabled attribute is present and the
265 * value is set to 0, don't show the printer.
266 * If the attribute is not present, assume that the
267 * printer should show up
269 if (!strcmp(attr->name, "smb-enabled") &&
270 ((attr->value_tag == IPP_TAG_INTEGER &&
271 !attr->values[0].integer) ||
272 (attr->value_tag == IPP_TAG_BOOLEAN &&
273 !attr->values[0].boolean)))
274 smb_enabled = 0;
277 * If the security-enabled attribute is present and the
278 * value is set to 1, don't show the printer.
279 * If the attribute is not present, assume that the
280 * printer should show up
282 if (!strcmp(attr->name, "security-enabled") &&
283 ((attr->value_tag == IPP_TAG_INTEGER &&
284 attr->values[0].integer) ||
285 (attr->value_tag == IPP_TAG_BOOLEAN &&
286 attr->values[0].boolean)))
287 secure = 1;
289 attr = attr->next;
293 * See if we have everything needed...
294 * Make sure the printer is not a secure printer
295 * and make sure smb printing hasn't been explicitly
296 * disabled for the printer
299 if (name != NULL && !secure && smb_enabled)
300 pcap_cache_add(name, info);
303 out:
304 if (response)
305 ippDelete(response);
306 return(0);
309 BOOL iprint_cache_reload(void)
311 http_t *http = NULL; /* HTTP connection to server */
312 ipp_t *request = NULL, /* IPP Request */
313 *response = NULL; /* IPP Response */
314 ipp_attribute_t *attr; /* Current attribute */
315 cups_lang_t *language = NULL; /* Default language */
316 int i;
317 BOOL ret = False;
319 DEBUG(5, ("reloading iprint printcap cache\n"));
322 * Make sure we don't ask for passwords...
325 cupsSetPasswordCB(iprint_passwd_cb);
328 * Try to connect to the server...
331 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
332 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
333 iprint_server(), strerror(errno)));
334 goto out;
338 * Build a OPERATION_NOVELL_LIST_PRINTERS request, which requires the following attributes:
340 * attributes-charset
341 * attributes-natural-language
344 request = ippNew();
346 request->request.op.operation_id = OPERATION_NOVELL_LIST_PRINTERS;
347 request->request.op.request_id = 1;
349 language = cupsLangDefault();
351 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
352 "attributes-charset", NULL, "utf-8");
354 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
355 "attributes-natural-language", NULL, language->language);
357 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
358 "ipp-server", NULL, "ippSrvr");
361 * Do the request and get back a response...
364 if ((response = cupsDoRequest(http, request, "/ipp")) == NULL) {
365 DEBUG(0,("Unable to get printer list - %s\n",
366 ippErrorString(cupsLastError())));
367 goto out;
370 for (attr = response->attrs; attr != NULL;) {
372 * Skip leading attributes until we hit a printer...
375 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
376 attr = attr->next;
378 if (attr == NULL)
379 break;
382 * Pull the needed attributes from this printer...
385 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
387 if (strcmp(attr->name, "printer-name") == 0 &&
388 (attr->value_tag == IPP_TAG_URI ||
389 attr->value_tag == IPP_TAG_NAME ||
390 attr->value_tag == IPP_TAG_TEXT ||
391 attr->value_tag == IPP_TAG_NAMELANG ||
392 attr->value_tag == IPP_TAG_TEXTLANG))
394 for (i = 0; i<attr->num_values; i++)
396 char *url = attr->values[i].string.text;
397 if (!url || !strlen(url))
398 continue;
399 iprint_cache_add_printer(http, i+2, url);
402 attr = attr->next;
406 ret = True;
408 out:
409 if (response)
410 ippDelete(response);
412 if (language)
413 cupsLangFree(language);
415 if (http)
416 httpClose(http);
418 return ret;
423 * 'iprint_job_delete()' - Delete a job.
426 static int iprint_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
428 int ret = 1; /* Return value */
429 http_t *http = NULL; /* HTTP connection to server */
430 ipp_t *request = NULL, /* IPP Request */
431 *response = NULL; /* IPP Response */
432 cups_lang_t *language = NULL; /* Default language */
433 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
434 char httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
437 DEBUG(5,("iprint_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
440 * Make sure we don't ask for passwords...
443 cupsSetPasswordCB(iprint_passwd_cb);
446 * Try to connect to the server...
449 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
450 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
451 iprint_server(), strerror(errno)));
452 goto out;
456 * Build an IPP_CANCEL_JOB request, which uses the following
457 * attributes:
459 * attributes-charset
460 * attributes-natural-language
461 * printer-uri
462 * job-id
463 * requesting-user-name
466 request = ippNew();
468 request->request.op.operation_id = IPP_CANCEL_JOB;
469 request->request.op.request_id = 1;
471 language = cupsLangDefault();
473 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
474 "attributes-charset", NULL, "utf-8");
476 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
477 "attributes-natural-language", NULL, language->language);
479 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), sharename);
481 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
483 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
485 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
486 NULL, pjob->user);
489 * Do the request and get back a response...
492 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", sharename);
494 if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
495 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
496 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
497 ippErrorString(cupsLastError())));
498 } else {
499 ret = 0;
501 } else {
502 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
503 ippErrorString(cupsLastError())));
506 out:
507 if (response)
508 ippDelete(response);
510 if (language)
511 cupsLangFree(language);
513 if (http)
514 httpClose(http);
516 return ret;
521 * 'iprint_job_pause()' - Pause a job.
524 static int iprint_job_pause(int snum, struct printjob *pjob)
526 int ret = 1; /* Return value */
527 http_t *http = NULL; /* HTTP connection to server */
528 ipp_t *request = NULL, /* IPP Request */
529 *response = NULL; /* IPP Response */
530 cups_lang_t *language = NULL; /* Default language */
531 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
532 char httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
535 DEBUG(5,("iprint_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
538 * Make sure we don't ask for passwords...
541 cupsSetPasswordCB(iprint_passwd_cb);
544 * Try to connect to the server...
547 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
548 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
549 iprint_server(), strerror(errno)));
550 goto out;
554 * Build an IPP_HOLD_JOB request, which requires the following
555 * attributes:
557 * attributes-charset
558 * attributes-natural-language
559 * printer-uri
560 * job-id
561 * requesting-user-name
564 request = ippNew();
566 request->request.op.operation_id = IPP_HOLD_JOB;
567 request->request.op.request_id = 1;
569 language = cupsLangDefault();
571 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
572 "attributes-charset", NULL, "utf-8");
574 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
575 "attributes-natural-language", NULL, language->language);
577 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), PRINTERNAME(snum));
579 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
581 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
583 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
584 NULL, pjob->user);
587 * Do the request and get back a response...
590 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", PRINTERNAME(snum));
592 if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
593 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
594 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
595 ippErrorString(cupsLastError())));
596 } else {
597 ret = 0;
599 } else {
600 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
601 ippErrorString(cupsLastError())));
604 out:
605 if (response)
606 ippDelete(response);
608 if (language)
609 cupsLangFree(language);
611 if (http)
612 httpClose(http);
614 return ret;
619 * 'iprint_job_resume()' - Resume a paused job.
622 static int iprint_job_resume(int snum, struct printjob *pjob)
624 int ret = 1; /* Return value */
625 http_t *http = NULL; /* HTTP connection to server */
626 ipp_t *request = NULL, /* IPP Request */
627 *response = NULL; /* IPP Response */
628 cups_lang_t *language = NULL; /* Default language */
629 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
630 char httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
633 DEBUG(5,("iprint_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
636 * Make sure we don't ask for passwords...
639 cupsSetPasswordCB(iprint_passwd_cb);
642 * Try to connect to the server...
645 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
646 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
647 iprint_server(), strerror(errno)));
648 goto out;
652 * Build an IPP_RELEASE_JOB request, which requires the following
653 * attributes:
655 * attributes-charset
656 * attributes-natural-language
657 * printer-uri
658 * job-id
659 * requesting-user-name
662 request = ippNew();
664 request->request.op.operation_id = IPP_RELEASE_JOB;
665 request->request.op.request_id = 1;
667 language = cupsLangDefault();
669 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
670 "attributes-charset", NULL, "utf-8");
672 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
673 "attributes-natural-language", NULL, language->language);
675 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), PRINTERNAME(snum));
677 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
679 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
681 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
682 NULL, pjob->user);
685 * Do the request and get back a response...
688 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", PRINTERNAME(snum));
690 if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
691 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
692 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
693 ippErrorString(cupsLastError())));
694 } else {
695 ret = 0;
697 } else {
698 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
699 ippErrorString(cupsLastError())));
702 out:
703 if (response)
704 ippDelete(response);
706 if (language)
707 cupsLangFree(language);
709 if (http)
710 httpClose(http);
712 return ret;
717 * 'iprint_job_submit()' - Submit a job for printing.
720 static int iprint_job_submit(int snum, struct printjob *pjob)
722 int ret = 1; /* Return value */
723 http_t *http = NULL; /* HTTP connection to server */
724 ipp_t *request = NULL, /* IPP Request */
725 *response = NULL; /* IPP Response */
726 ipp_attribute_t *attr; /* Current attribute */
727 cups_lang_t *language = NULL; /* Default language */
728 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
729 char *clientname = NULL; /* hostname of client for job-originating-host attribute */
731 DEBUG(5,("iprint_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
734 * Make sure we don't ask for passwords...
737 cupsSetPasswordCB(iprint_passwd_cb);
740 * Try to connect to the server...
743 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
744 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
745 iprint_server(), strerror(errno)));
746 goto out;
750 * Build an IPP_PRINT_JOB request, which requires the following
751 * attributes:
753 * attributes-charset
754 * attributes-natural-language
755 * printer-uri
756 * requesting-user-name
757 * [document-data]
760 request = ippNew();
762 request->request.op.operation_id = IPP_PRINT_JOB;
763 request->request.op.request_id = 1;
765 language = cupsLangDefault();
767 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
768 "attributes-charset", NULL, "utf-8");
770 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
771 "attributes-natural-language", NULL, language->language);
773 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), PRINTERNAME(snum));
775 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
776 "printer-uri", NULL, uri);
778 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
779 NULL, pjob->user);
781 clientname = client_name();
782 if (strcmp(clientname, "UNKNOWN") == 0) {
783 clientname = client_addr();
786 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
787 "job-originating-host-name", NULL,
788 clientname);
790 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
791 pjob->jobname);
794 * Do the request and get back a response...
797 slprintf(uri, sizeof(uri) - 1, "/ipp/%s", PRINTERNAME(snum));
799 if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
800 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
801 DEBUG(0,("Unable to print file to %s - %s\n", PRINTERNAME(snum),
802 ippErrorString(cupsLastError())));
803 } else {
804 ret = 0;
806 } else {
807 DEBUG(0,("Unable to print file to `%s' - %s\n", PRINTERNAME(snum),
808 ippErrorString(cupsLastError())));
811 if ( ret == 0 )
812 unlink(pjob->filename);
813 /* else print_job_end will do it for us */
815 if ( ret == 0 ) {
817 attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
818 if (attr != NULL && attr->group_tag == IPP_TAG_JOB)
820 pjob->sysjob = attr->values[0].integer;
824 out:
825 if (response)
826 ippDelete(response);
828 if (language)
829 cupsLangFree(language);
831 if (http)
832 httpClose(http);
834 return ret;
838 * 'iprint_queue_get()' - Get all the jobs in the print queue.
841 static int iprint_queue_get(const char *sharename,
842 enum printing_types printing_type,
843 char *lpq_command,
844 print_queue_struct **q,
845 print_status_struct *status)
847 fstring printername;
848 http_t *http = NULL; /* HTTP connection to server */
849 ipp_t *request = NULL, /* IPP Request */
850 *response = NULL; /* IPP Response */
851 ipp_attribute_t *attr = NULL; /* Current attribute */
852 cups_lang_t *language = NULL; /* Default language */
853 char uri[HTTP_MAX_URI]; /* printer-uri attribute */
854 char serviceUri[HTTP_MAX_URI]; /* service-uri attribute */
855 char httpPath[HTTP_MAX_URI]; /* path portion of the uri */
856 int jobUseUnixTime = 0; /* Whether job times should
857 * be assumed to be Unix time */
858 int qcount = 0, /* Number of active queue entries */
859 qalloc = 0; /* Number of queue entries allocated */
860 print_queue_struct *queue = NULL, /* Queue entries */
861 *temp; /* Temporary pointer for queue */
862 const char *user_name, /* job-originating-user-name attribute */
863 *job_name; /* job-name attribute */
864 int job_id; /* job-id attribute */
865 int job_k_octets; /* job-k-octets attribute */
866 time_t job_time; /* time-at-creation attribute */
867 time_t printer_current_time = 0; /* printer's current time */
868 time_t printer_up_time = 0; /* printer's uptime */
869 ipp_jstate_t job_status; /* job-status attribute */
870 int job_priority; /* job-priority attribute */
871 static const char *jattrs[] = /* Requested job attributes */
873 "job-id",
874 "job-k-octets",
875 "job-name",
876 "job-originating-user-name",
877 "job-priority",
878 "job-state",
879 "time-at-creation",
881 static const char *pattrs[] = /* Requested printer attributes */
883 "printer-state",
884 "printer-state-message",
885 "printer-current-time",
886 "printer-up-time"
889 *q = NULL;
891 /* HACK ALERT!!! The porblem with support the 'printer name'
892 option is that we key the tdb off the sharename. So we will
893 overload the lpq_command string to pass in the printername
894 (which is basically what we do for non-cups printers ... using
895 the lpq_command to get the queue listing). */
897 fstrcpy( printername, lpq_command );
899 DEBUG(5,("iprint_queue_get(%s, %p, %p)\n", printername, q, status));
902 * Make sure we don't ask for passwords...
905 cupsSetPasswordCB(iprint_passwd_cb);
908 * Try to connect to the server...
911 if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
912 DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
913 iprint_server(), strerror(errno)));
914 goto out;
918 * Generate the printer URI and the service URI that goes with it...
921 slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), printername);
922 slprintf(serviceUri, sizeof(serviceUri) - 1, "ipp://%s/ipp/", iprint_server());
925 * For Linux iPrint servers from OES SP1 on, the iPrint server
926 * uses Unix time for job start times unless it detects the iPrint
927 * client in an http User-Agent header. (This was done to accomodate
928 * CUPS broken behavior. According to RFC 2911, section 4.3.14, job
929 * start times are supposed to be relative to how long the printer has
930 * been up.) Since libcups doesn't allow us to set that header before
931 * the request is sent, this ugly hack allows us to detect the server
932 * version and decide how to interpret the job time.
934 if (iprint_get_server_version(http, serviceUri) >=
935 NOVELL_SERVER_VERSION_OES_SP1)
936 jobUseUnixTime = 1;
938 request = ippNew();
940 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
941 request->request.op.request_id = 2;
943 language = cupsLangDefault();
945 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
946 "attributes-charset", NULL, "utf-8");
948 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
949 "attributes-natural-language", NULL, language->language);
951 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
952 "printer-uri", NULL, uri);
954 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
955 "requested-attributes",
956 (sizeof(pattrs) / sizeof(pattrs[0])),
957 NULL, pattrs);
960 * Do the request and get back a response...
963 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", printername);
965 if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
966 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
967 ippErrorString(cupsLastError())));
968 *q = queue;
969 goto out;
972 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
973 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
974 ippErrorString(response->request.status.status_code)));
975 *q = queue;
976 goto out;
980 * Get the current printer status and convert it to the SAMBA values.
983 if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
984 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
985 status->status = LPSTAT_STOPPED;
986 else
987 status->status = LPSTAT_OK;
990 if ((attr = ippFindAttribute(response, "printer-state-message",
991 IPP_TAG_TEXT)) != NULL)
992 fstrcpy(status->message, attr->values[0].string.text);
994 if ((attr = ippFindAttribute(response, "printer-current-time",
995 IPP_TAG_DATE)) != NULL)
996 printer_current_time = ippDateToTime(attr->values[0].date);
998 if ((attr = ippFindAttribute(response, "printer-up-time",
999 IPP_TAG_INTEGER)) != NULL)
1000 printer_up_time = attr->values[0].integer;
1002 ippDelete(response);
1003 response = NULL;
1006 * Build an IPP_GET_JOBS request, which requires the following
1007 * attributes:
1009 * attributes-charset
1010 * attributes-natural-language
1011 * requested-attributes
1012 * printer-uri
1015 request = ippNew();
1017 request->request.op.operation_id = IPP_GET_JOBS;
1018 request->request.op.request_id = 3;
1020 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1021 "attributes-charset", NULL, "utf-8");
1023 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1024 "attributes-natural-language", NULL, language->language);
1026 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1027 "printer-uri", NULL, uri);
1029 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1030 "requested-attributes",
1031 (sizeof(jattrs) / sizeof(jattrs[0])),
1032 NULL, jattrs);
1035 * Do the request and get back a response...
1038 slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", printername);
1040 if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
1041 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1042 ippErrorString(cupsLastError())));
1043 goto out;
1046 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1047 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1048 ippErrorString(response->request.status.status_code)));
1049 goto out;
1053 * Process the jobs...
1056 qcount = 0;
1057 qalloc = 0;
1058 queue = NULL;
1060 for (attr = response->attrs; attr != NULL; attr = attr->next) {
1062 * Skip leading attributes until we hit a job...
1065 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1066 attr = attr->next;
1068 if (attr == NULL)
1069 break;
1072 * Allocate memory as needed...
1074 if (qcount >= qalloc) {
1075 qalloc += 16;
1077 queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1079 if (queue == NULL) {
1080 DEBUG(0,("iprint_queue_get: Not enough memory!"));
1081 qcount = 0;
1082 goto out;
1086 temp = queue + qcount;
1087 memset(temp, 0, sizeof(print_queue_struct));
1090 * Pull the needed attributes from this job...
1093 job_id = 0;
1094 job_priority = 50;
1095 job_status = IPP_JOB_PENDING;
1096 job_time = 0;
1097 job_k_octets = 0;
1098 user_name = NULL;
1099 job_name = NULL;
1101 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1102 if (attr->name == NULL) {
1103 attr = attr->next;
1104 break;
1107 if (strcmp(attr->name, "job-id") == 0 &&
1108 attr->value_tag == IPP_TAG_INTEGER)
1109 job_id = attr->values[0].integer;
1111 if (strcmp(attr->name, "job-k-octets") == 0 &&
1112 attr->value_tag == IPP_TAG_INTEGER)
1113 job_k_octets = attr->values[0].integer;
1115 if (strcmp(attr->name, "job-priority") == 0 &&
1116 attr->value_tag == IPP_TAG_INTEGER)
1117 job_priority = attr->values[0].integer;
1119 if (strcmp(attr->name, "job-state") == 0 &&
1120 attr->value_tag == IPP_TAG_ENUM)
1121 job_status = (ipp_jstate_t)(attr->values[0].integer);
1123 if (strcmp(attr->name, "time-at-creation") == 0 &&
1124 attr->value_tag == IPP_TAG_INTEGER)
1127 * If jobs times are in Unix time, the accuracy of the job
1128 * start time depends upon the iPrint server's time being
1129 * set correctly. Otherwise, the accuracy depends upon
1130 * the Samba server's time being set correctly
1133 if (jobUseUnixTime)
1134 job_time = attr->values[0].integer;
1135 else
1136 job_time = time(NULL) - printer_up_time + attr->values[0].integer;
1139 if (strcmp(attr->name, "job-name") == 0 &&
1140 (attr->value_tag == IPP_TAG_NAMELANG ||
1141 attr->value_tag == IPP_TAG_NAME))
1142 job_name = attr->values[0].string.text;
1144 if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1145 (attr->value_tag == IPP_TAG_NAMELANG ||
1146 attr->value_tag == IPP_TAG_NAME))
1147 user_name = attr->values[0].string.text;
1149 attr = attr->next;
1153 * See if we have everything needed...
1156 if (user_name == NULL || job_name == NULL || job_id == 0) {
1157 if (attr == NULL)
1158 break;
1159 else
1160 continue;
1163 temp->job = job_id;
1164 temp->size = job_k_octets * 1024;
1165 temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1166 job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1167 job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1168 LPQ_PRINTING;
1169 temp->priority = job_priority;
1170 temp->time = job_time;
1171 strncpy(temp->fs_user, user_name, sizeof(temp->fs_user) - 1);
1172 strncpy(temp->fs_file, job_name, sizeof(temp->fs_file) - 1);
1174 qcount ++;
1176 if (attr == NULL)
1177 break;
1181 * Return the job queue...
1184 *q = queue;
1186 out:
1187 if (response)
1188 ippDelete(response);
1190 if (language)
1191 cupsLangFree(language);
1193 if (http)
1194 httpClose(http);
1196 return qcount;
1201 * 'iprint_queue_pause()' - Pause a print queue.
1204 static int iprint_queue_pause(int snum)
1206 return(-1); /* Not supported without credentials */
1211 * 'iprint_queue_resume()' - Restart a print queue.
1214 static int iprint_queue_resume(int snum)
1216 return(-1); /* Not supported without credentials */
1219 /*******************************************************************
1220 * iPrint printing interface definitions...
1221 ******************************************************************/
1223 struct printif iprint_printif =
1225 PRINT_IPRINT,
1226 iprint_queue_get,
1227 iprint_queue_pause,
1228 iprint_queue_resume,
1229 iprint_job_delete,
1230 iprint_job_pause,
1231 iprint_job_resume,
1232 iprint_job_submit,
1235 #else
1236 /* this keeps fussy compilers happy */
1237 void print_iprint_dummy(void) {}
1238 #endif /* HAVE_IPRINT */